The format_arg Function Attribute

Function attribute format_arg provides a way to identify user-defined functions that modify format strings. Once the function is identified, calls to functions like printf, scanf, strftime, or strfmon, whose operands are a call to the user-defined function can be checked for errors. The language feature is an orthogonal extension to C89, C99, and Standard C++, and has been implemented to facilitate porting programs developed with GNU C and C++.

The syntax is shown in the following diagram.

>>-__attribute__--((--+-format_arg-----+--(--string_index--)--))-><
                      '-__format_arg__-'
 
 

where string_index is a constant integral expression that specifies which argument is the format string argument, starting from 1. For non-static member functions in C++, string_index starts from 2 because the first parameter is an implicit this parameter.

It is possible to specify multiple format_arg attributes on the same function, in which case, all apply.

extern char* my_dgettext(const char* my_format, const char* my_format2) 
          __attribute__((__format_arg__(1))) __attribute__((__format_arg__(2))); 
 
printf(my_dgettext("%","%")); 
//printf-style format diagnostics are performed on both "%" strings 
 

Related References

IBM Copyright 2003