dbus宏定义解读

本文介绍了GCC编译器中的格式检查属性,通过示例详细解释了如何根据不同版本的GCC编译器启用对printf风格格式字符串的类型检查。此外,还讨论了如何指定格式字符串参数及其检查规则。

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 printfscanfstrftime, and strfmon for errors.

Read syntax diagramSkip visual syntax diagramformat 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.  C++ only 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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值