format_arg

Previous Specifying Attributes of Functions Next

Syntax: format_arg (string-index)

The format_arg attribute specifies that a function takes a format string for a printf style function and modifies it (for example, to translate it into another language), so the result can be passed to a printf style function (with the remaining arguments to the format function the same as they would have been for the unmodified string). For example, the declaration

extern char *
my_dgettext (char *my_domain, const char *my_format)
      __attribute__ ((format_arg (2)));
causes the compiler to check the arguments in calls to a printf type function, whose format string argument is the result of a call to the my_dgettext function, for consistency with the format string argument my_format. If the format_arg attribute had not been specified, all the compiler could tell in such calls to format functions would be that the format string argument is not constant; this would generate a warning when '-Wformat-nonliteral' is used, but the calls could not be checked without the attribute.

The parameter string-index specifies which argument is the format string argument (starting from 1).

The format-arg attribute allows you to identify your own functions which modify format strings, so that GCC can check the calls to printf type functions whose operands are calls to one of your own functions. (The compiler always treats gettext, dgettext, and dcgettext in this manner except when strict ISO C support is requested by '-ansi' or an appropriate '-std' option, or '-ffreestanding' is used. See Options Controlling C Dialect.)