dbus-macro.h里面有这样的代码
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)#define _DBUS_GNUC_PRINTF( format_idx, arg_idx ) \ __attribute__((__format__ (__printf__, format_idx, arg_idx)))#define _DBUS_GNUC_NORETURN \ __attribute__((__noreturn__))#else /* !__GNUC__ */#define _DBUS_GNUC_PRINTF( format_idx, arg_idx )#define _DBUS_GNUC_NORETURN#endif /* !__GNUC__ */对于这段代码的解释是
根据不同的gnuc的编译器版本,对printf的输入参数进行参数格式检查。
下面是对于部分代码的解释来源
GCC 中普通预定义宏
__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
These macros are defined by all GNU compilers that use the C preprocessor: C, C++, and Objective-C. Their values are the major version, minor version, and patch level of the compiler, as integer constants. For example, GCC 3.2.1 will define __GNUC__ to 3, __GNUC_MINOR__ to 2, and __GNUC_PATCHLEVEL__ to 1. They are defined only when the entire compiler is in use; if you invoke the preprocessor directly, they are not defined.
__GNUC_PATCHLEVEL__ is new to GCC 3.0; it is also present in the widely-used development snapshots leading up to 3.0 (which identify themselves as GCC 2.96 or 2.97, depending on which snapshot you have).
来源:http://whitewang1614.blog.hexun.com/23166427_d.html
The format function attribute provides a way to identify user-defined functions that take format strings as arguments so that calls to these functions will be type-checked against a format string, similar to the way the compiler checks calls to the functions printf, scanf, strftime, and strfmon for errors.
format function attribute syntax .-,--------------------------------------------------------------------------. V | >>-__attribute__--((----+-format-----+--(--+-printf-------+--,--string_index--,--first_to_check--)-+--))->< '-__format__-' +-scanf--------+ +-strftime-----+ +-strfmon------+ +-__printf__---+ +-__scanf__----+ +-__strftime__-+ '-__strfmon__--'
where
-
string_index
-
Is a constant integral expression that specifies which argument in the declaration of the user function is the format string argument.
In C++, the minimum value of
string_index for nonstatic member functions is 2 because the first argument is an implicit
this argument. This behavior is consistent with that of GNU C++.
first_to_check
- Is a constant integral expression that specifies the first argument to check against the format string. If there are no arguments to check against the format string (that is, diagnostics should only be performed on the format string syntax and semantics), first_to_check should have a value of 0. For strftime-style formats, first_to_check is required to be 0.
It is possible to specify multiple format attributes on the same function, in which case, all apply.
void my_fn(const char* a, const char* b, ...)
__attribute__((__format__(__printf__,1,0), __format__(__scanf__,2,3)));
It is also possible to diagnose the same string for different format styles. All styles are diagnosed.
void my_fn(const char* a, const char* b, ...)
__attribute__((__format__(__printf__,2,3),
__format__(__strftime__,2,0),
__format__(__scanf__,2,3)));
来源:http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Ffn_attrib_format.htm
本文介绍了GCC编译器中的格式检查属性,通过示例详细解释了如何根据不同版本的GCC编译器启用对printf风格格式字符串的类型检查。此外,还讨论了如何指定格式字符串参数及其检查规则。

1687

被折叠的 条评论
为什么被折叠?



