FROM:__attribute__ ((format (printf, 2, 3)));_陈嘉怡的专栏-优快云博客
这句主要作用是提示编译器,对这个函数的调用需要像printf一样,用对应的format字符串来check可变参数的数据类型。
例如:
extern int myprintf (void *other, const char *fmt, ...)
__attribute__ ((format (printf, 2, 3)));
format (printf, 2, 3)告诉编译器,fmt相当于printf的format,而可变参数是从myprintf 的第3个参数开始。
这样编译器就会在编译时用和printf一样的check法则来确认可变参数是否正确了。
————————————————
版权声明:本文为优快云博主「风云来」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/chenjiayi_yun/article/details/16849683
头文件里的定义
include\linux\compiler_attributes.h
/*
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute
* clang: https://clang.llvm.org/docs/AttributeReference.html#format
*/
#define __printf(a, b) __attribute__((__format__(printf, a, b)))
#define __scanf(a, b) __attribute__((__format__(scanf, a, b)))
extern int my_printf (void *my_object, const char *my_format, ...) __attribute__ ((format (printf, 2, 3)));
对于这个例子,格式化字符串 (my_format
)是my_print函数的第二个参数,需要检查的参数从第三个参数开始,所以format attribute正确的参数是2和3
In the example above, the format string (my_format
) is the second argument of the function my_print
, and the arguments to check start with the third argument, so the correct parameters for the format attribute are 2 and 3.
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute里的解释内容如下
format (archetype, string-index, first-to-check)
The format
attribute specifies that a function takes printf
, scanf
, strftime
or strfmon
style arguments that should be type-checked against a format string. For example, the declaration:
extern int my_printf (void *my_object, const char *my_format, ...) __attribute__ ((format (printf, 2, 3)));
causes the compiler to check the arguments in calls to my_printf
for consistency with the printf
style format string argument my_format
.
The parameter archetype determines how the format string is interpreted, and should be printf
, scanf
, strftime
, gnu_printf
, gnu_scanf
, gnu_strftime
or strfmon
. (You can also use __printf__
, __scanf__
, __strftime__
or __strfmon__
.) On MinGW targets, ms_printf
, ms_scanf
, and ms_strftime
are also present. archetype values such as printf
refer to the formats accepted by the system’s C runtime library, while values prefixed with ‘gnu_’ always refer to the formats accepted by the GNU C Library. On Microsoft Windows targets, values prefixed with ‘ms_’ refer to the formats accepted by the msvcrt.dll library. The parameter string-index specifies which argument is the format string argument (starting from 1), while first-to-check is the number of the first argument to check against the format string. For functions where the arguments are not available to be checked (such as vprintf
), specify the third parameter as zero. In this case the compiler only checks the format string for consistency. For strftime
formats, the third parameter is required to be zero. Since non-static C++ methods have an implicit this
argument, the arguments of such methods should be counted from two, not one, when giving values for string-index and first-to-check.
In the example above, the format string (my_format
) is the second argument of the function my_print
, and the arguments to check start with the third argument, so the correct parameters for the format attribute are 2 and 3.
The format
attribute allows you to identify your own functions that take format strings as arguments, so that GCC can check the calls to these functions for errors. The compiler always (unless -ffreestanding or -fno-builtin is used) checks formats for the standard library functions printf
, fprintf
, sprintf
, scanf
, fscanf
, sscanf
, strftime
, vprintf
, vfprintf
and vsprintf
whenever such warnings are requested (using -Wformat), so there is no need to modify the header file stdio.h. In C99 mode, the functions snprintf
, vsnprintf
, vscanf
, vfscanf
and vsscanf
are also checked. Except in strictly conforming C standard modes, the X/Open function strfmon
is also checked as are printf_unlocked
and fprintf_unlocked
. See Options Controlling C Dialect.
For Objective-C dialects, NSString
(or __NSString__
) is recognized in the same context. Declarations including these format attributes are parsed for correct syntax, however the result of checking of such format strings is not yet defined, and is not carried out by this version of the compiler.
The target may also provide additional types of format checks. See Format Checks Specific to Particular Target Machines.