GCC attribute

GCC attribute


用在function, type, variable 在編譯的時候進行一些檢查 例如 attribute((noreturn)) 宣告函數不會有回傳值就離開 EX: ------------ static void usage(void) attribute((noreturn)); //宣告變數不會return static void usage(void) { /*something*/ exit(0); } ------------ attribute((format(printf,1,2))) attribute((packed)) 用來解決 struture word alignment 問題的 gcc extension struct alignment 是指 struct 中的資料 compiler 會自動加入一些 padding 的字元,讓整個 struct 的大小會是 4byte 的倍數,有些 cpu 允許資料沒有 padding,像是 x86 cpu 就可以,而有些不行,但是在網路傳輸資料的時候,是不能加入這個 padding 字元的,會造網路連線另一端解釋宇元的時候,發生錯誤。所以會使用 GNU compiler extension 的 attribute ((packed)) 符號,讓 compiler 不要加入 padding 字元(ref: GD's Blog C/C++)


"noreturn"A few standard library functions, such as "abort" and "exit", cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them "noreturn" to tell the compiler this fact. For example,

void fatal () attribute ((noreturn));

void fatal (/* ... */)

The "noreturn" keyword tells the compiler to assume that "fatal" cannot return. It can then optimize without regard to what would happen if "fatal" ever did return. This makes slightly better code. More importantly, it helps avoid spurious warnings of uninitialized variables.

The "noreturn" keyword does not affect the exceptional path when that applies: a "noreturn"-marked function may still return to the caller by throwing an exception or calling "longjmp".

Do not assume that registers saved by the calling function are restored before calling the "noreturn" function.

It does not make sense for a "noreturn" function to have a return type other than "void".

The attribute "noreturn" is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function does not return, which works in the current version and in some older versions, is as follows:

typedef void voidfn ();

volatile voidfn fatal;

This approach does not work in GNU C++.

"format ("archetype", "string-index", "first-to-check")"The "format" attribute specifies that a function takes "printf", "scanf", "strftime" or "strfmon" style arguments which 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" or "strfmon". (You can also use "printf", "scanf", "strftime" or "strfmon".) 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 which 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.

The target may provide additional types of format checks. See Format Checks Specific to Particular Target Machines.

更多關於attribute 1. 5.24 Declaring Attributes of Functions 2. 5.31 Specifying Attributes of Variables 3. 5.32 Specifying Attributes of Types

convert from Thomas blog post id 55 old convert log: ./153400/tag%3E2007%2002%20linux)

@2007 @02 @linux