From here below, we will meet FUNCTION_DECL and FUNCTION_TYPE here and there. As we know, in C, a function is associated with a type. For example, int a (int A); and int b (int B), both functions are of the same type (they can be referred by type int(*)(int)), but they are two different declarations of the type and can behave total differently. You can say that type somewhat gives the interface, and declaration sounds different implementation under the interface.
In later, we will see that some attributes can be applied to types, and some should be used by declaration, this variance just due to the different between type and declaration. Keep this in mind.
Here first a series macros create the FUNCTION_TYPE for declarations in builtin-types.def. See that the created type is saved into corresponding slot of builtin_types. And for FUNCTION_TYPE, if the last node in the list of argument values is `void_list_node', then functions of this type do not take variable arguments. Otherwise, they do take a variable number of arguments.
c_common_nodes_and_builtins (continue)
3336 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) /
3337 builtin_types[(int) ENUM] = VALUE;
3338 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) /
3339 builtin_types[(int) ENUM] /
3340 = build_function_type (builtin_types[(int) RETURN], /
3341 void_list_node);
3342 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) /
3343 builtin_types[(int) ENUM] /
3344 = build_function_type (builtin_types[(int) RETURN], /
3345 tree_cons (NULL_TREE, /
3346 builtin_types[(int) ARG1], /
3347 void_list_node));
3348 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) /
3349 builtin_types[(int) ENUM] /
3350 = build_function_type /
3351 (builtin_types[(int) RETURN], /
3352 tree_cons (NULL_TREE, /
3353 builtin_types[(int) ARG1], /
3354 tree_cons (NULL_TREE, /
3355 builtin_types[(int) ARG2], /
3356 void_list_node)));
3357 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) /
3358 builtin_types[(int) ENUM] /
3359 = build_function_type /
3360 (builtin_types[(int) RETURN], /
3361 tree_cons (NULL_TREE, /
3362 builtin_types[(int) ARG1], /
3363 tree_cons (NULL_TREE, /
3364 builtin_types[(int) ARG2], /
3365 tree_cons (NULL_TREE, /
3366 builtin_types[(int) ARG3], /
3367 void_list_node))));
3368 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) /
3369 builtin_types[(int) ENUM] /
3370 = build_function_type /
3371 (builtin_types[(int) RETURN], /
3372 tree_cons (NULL_TREE, /
3373 builtin_types[(int) ARG1], /
3374 tree_cons (NULL_TREE, /
3375 builtin_types[(int) ARG2], /
3376 tree_cons /
3377 (NULL_TREE, /
3378 builtin_types[(int) ARG3], /
3379 tree_cons (NULL_TREE, /
3380 builtin_types[(int) ARG4], /
3381 void_list_node)))));
3382 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) /
3383 builtin_types[(int) ENUM] /
3384 = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
3385 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) /
3386 builtin_types[(int) ENUM] /
3387 = build_function_type (builtin_types[(int) RETURN], /
3388 tree_cons (NULL_TREE, /
3389 builtin_types[(int) ARG1], /
3390 NULL_TREE));
3391
3392 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) /
3393 builtin_types[(int) ENUM] /
3394 = build_function_type /
3395 (builtin_types[(int) RETURN], /
3396 tree_cons (NULL_TREE, /
3397 builtin_types[(int) ARG1], /
3398 tree_cons (NULL_TREE, /
3399 builtin_types[(int) ARG2], /
3400 NULL_TREE)));
3401
3402 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) /
3403 builtin_types[(int) ENUM] /
3404 = build_function_type /
3405 (builtin_types[(int) RETURN], /
3406 tree_cons (NULL_TREE, /
3407 builtin_types[(int) ARG1], /
3408 tree_cons (NULL_TREE, /
3409 builtin_types[(int) ARG2], /
3410 tree_cons (NULL_TREE, /
3411 builtin_types[(int) ARG3], /
3412 NULL_TREE))));
3413
3414 #define DEF_POINTER_TYPE(ENUM, TYPE) /
3415 builtin_types[(int) ENUM] /
3416 = build_function_type (builtin_types[(int) TYPE]);
3417 #include "builtin-types.def"
3418 #undef DEF_PRIMITIVE_TYPE
3419 #undef DEF_FUNCTION_TYPE_1
3420 #undef DEF_FUNCTION_TYPE_2
3421 #undef DEF_FUNCTION_TYPE_3
3422 #undef DEF_FUNCTION_TYPE_4
3423 #undef DEF_FUNCTION_TYPE_VAR_0
3424 #undef DEF_FUNCTION_TYPE_VAR_1
3425 #undef DEF_FUNCTION_TYPE_VAR_2
3426 #undef DEF_FUNCTION_TYPE_VAR_3
3427 #undef DEF_POINTER_TYPE
In GNU C, you declare certain things about functions called in your program which help the compiler optimize function calls and check your code more carefully.
The keyword __attribute__ allows you to specify special attributes when making a declaration. This keyword is followed by an attribute specification inside double parentheses. The following attributes are currently defined for functions on all targets: aligned, alloc_size, noreturn, returns_twice, noinline, always_inline, flatten, pure, const, nothrow, sentinel, format, format_arg, no_instrument_function, section, constructor, destructor, used, unused, deprecated, weak, malloc, alias, warn_unused_result, nonnull, gnu_inline, externally_visible, hot, cold, artificial, error and warning.
The builtin functions provided by GCC, no different than other normal functions in view inside internal, also need some attributes though not all. Same as other part of declaration, when processed in internal, attribute for the declaration should also be in form of tree node. So before create tree nodes for builtin functions, nodes for attributes should be ready, and now we are going to create them.
c_common_nodes_and_builtins (continue)
3429 c_init_attributes ();
Below built_in_attributes are identifiers array indexed by built_in_attribute which is definied as following:
3036 enum built_in_attribute in c-common.c
3037 {
3038 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3039 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3040 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3041 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3042 #include "builtin-attrs.def"
3043 #undef DEF_ATTR_NULL_TREE
3044 #undef DEF_ATTR_INT
3045 #undef DEF_ATTR_IDENT
3046 #undef DEF_ATTR_TREE_LIST
3047 ATTR_LAST
3048 };
Then in c_init_attributes, the macros are redefined so that:
DEF_ATTR_NULL_TREE (ENUM) constructs a NULL_TREE.
DEF_ATTR_INT (ENUM, VALUE) constructs an INTEGER_CST with value VALUE (an integer representable in HOST_WIDE_INT).
DEF_ATTR_IDENT (ENUM, STRING) constructs an IDENTIFIER_NODE for STRING.
DEF_ATTR_TREE_LIST (ENUM, PURPOSE, VALUE, CHAIN) constructs a TREE_LIST with given PURPOSE, VALUE and CHAIN (given as previous ENUM names).
4258 static void
4259 c_init_attributes (void) in c-common.c
4260 {
4261 /* Fill in the built_in_attributes array. */
4262 #define DEF_ATTR_NULL_TREE(ENUM) /
4263 built_in_attributes[(int) ENUM] = NULL_TREE;
4264 #define DEF_ATTR_INT(ENUM, VALUE) /
4265 built_in_attributes[(int) ENUM] = build_int_2 (VALUE, VALUE < 0 ? -1 : 0);
4266 #define DEF_ATTR_IDENT(ENUM, STRING) /
4267 built_in_attributes[(int) ENUM] = get_identifier (STRING);
4268 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) /
4269 built_in_attributes[(int) ENUM] /
4270 = tree_cons (built_in_attributes[(int) PURPOSE], /
4271 built_in_attributes[(int) VALUE], /
4272 built_in_attributes[(int) CHAIN]);
4273 #include "builtin-attrs.def"
4274 #undef DEF_ATTR_NULL_TREE
4275 #undef DEF_ATTR_INT
4276 #undef DEF_ATTR_IDENT
4277 #undef DEF_ATTR_TREE_LIST
4278 }
The macros looks dazzling, it better let GCC to expand the macros for us. It is the result in below. The code generates attributes for builtin functions.
built_in_attributes[(int) ATTR_0] = build_int_2 (0, 0 < 0 ? -1 : 0);
built_in_attributes[(int) ATTR_LIST_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_0], built_in_attributes[(int) ATTR_NULL]);
built_in_attributes[(int) ATTR_1] = build_int_2 (1, 1 < 0 ? -1 : 0);
built_in_attributes[(int) ATTR_LIST_1] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_1], built_in_attributes[(int) ATTR_NULL]);
built_in_attributes[(int) ATTR_2] = build_int_2 (2, 2 < 0 ? -1 : 0);
built_in_attributes[(int) ATTR_LIST_2] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_2], built_in_attributes[(int) ATTR_NULL]);
built_in_attributes[(int) ATTR_3] = build_int_2 (3, 3 < 0 ? -1 : 0);
built_in_attributes[(int) ATTR_LIST_3] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_3], built_in_attributes[(int) ATTR_NULL]);
built_in_attributes[(int) ATTR_4] = build_int_2 (4, 4 < 0 ? -1 : 0);
built_in_attributes[(int) ATTR_LIST_4] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_4], built_in_attributes[(int) ATTR_NULL]);
built_in_attributes[(int) ATTR_LIST_1_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_1], built_in_attributes[(int) ATTR_LIST_0]);
built_in_attributes[(int) ATTR_LIST_1_2] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_1], built_in_attributes[(int) ATTR_LIST_2]);
built_in_attributes[(int) ATTR_LIST_2_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_2], built_in_attributes[(int) ATTR_LIST_0]);
built_in_attributes[(int) ATTR_LIST_2_3] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_2], built_in_attributes[(int) ATTR_LIST_3]);
built_in_attributes[(int) ATTR_LIST_3_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_3], built_in_attributes[(int) ATTR_LIST_0]);
built_in_attributes[(int) ATTR_LIST_3_4] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_3], built_in_attributes[(int) ATTR_LIST_4]);
built_in_attributes[(int) ATTR_CONST] = get_identifier ("const"); built_in_attributes[(int) ATTR_FORMAT] = get_identifier ("format"); built_in_attributes[(int) ATTR_FORMAT_ARG] = get_identifier ("format_arg"); built_in_attributes[(int) ATTR_MALLOC] = get_identifier ("malloc"); built_in_attributes[(int) ATTR_NONNULL] = get_identifier ("nonnull"); built_in_attributes[(int) ATTR_NORETURN] = get_identifier ("noreturn"); built_in_attributes[(int) ATTR_NOTHROW] = get_identifier ("nothrow"); built_in_attributes[(int) ATTR_PRINTF] = get_identifier ("printf"); built_in_attributes[(int) ATTR_ASM_FPRINTF] = get_identifier ("asm_fprintf"); built_in_attributes[(int) ATTR_GCC_DIAG] = get_identifier ("gcc_diag"); built_in_attributes[(int) ATTR_GCC_CDIAG] = get_identifier ("gcc_cdiag"); built_in_attributes[(int) ATTR_GCC_CXXDIAG] = get_identifier ("gcc_cxxdiag"); built_in_attributes[(int) ATTR_PURE] = get_identifier ("pure"); built_in_attributes[(int) ATTR_SCANF] = get_identifier ("scanf"); built_in_attributes[(int) ATTR_STRFMON] = get_identifier ("strfmon"); built_in_attributes[(int) ATTR_STRFTIME] = get_identifier ("strftime");
built_in_attributes[(int) ATTR_NOTHROW_LIST] = tree_cons (built_in_attributes[(int) ATTR_NOTHROW], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NULL]);
built_in_attributes[(int) ATTR_CONST_NOTHROW_LIST] = tree_cons (built_in_attributes[(int) ATTR_CONST], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_LIST]);
built_in_attributes[(int) ATTR_PURE_NOTHROW_LIST] = tree_cons (built_in_attributes[(int) ATTR_PURE], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_LIST]);
built_in_attributes[(int) ATTR_NORETURN_NOTHROW_LIST] = tree_cons (built_in_attributes[(int) ATTR_NORETURN], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_LIST]);
built_in_attributes[(int) ATTR_MALLOC_NOTHROW_LIST] = tree_cons (built_in_attributes[(int) ATTR_MALLOC], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_LIST]);
built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1] = tree_cons (built_in_attributes[(int) ATTR_NONNULL], built_in_attributes[(int) ATTR_LIST_1], built_in_attributes[(int) ATTR_NOTHROW_LIST]);
built_in_attributes[(int) ATTR_NOTHROW_NONNULL_2] = tree_cons (built_in_attributes[(int) ATTR_NONNULL], built_in_attributes[(int) ATTR_LIST_2], built_in_attributes[(int) ATTR_NOTHROW_LIST]);
built_in_attributes[(int) ATTR_NOTHROW_NONNULL_3] = tree_cons (built_in_attributes[(int) ATTR_NONNULL], built_in_attributes[(int) ATTR_LIST_3], built_in_attributes[(int) ATTR_NOTHROW_LIST]);
built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1_2] = tree_cons (built_in_attributes[(int) ATTR_NONNULL], built_in_attributes[(int) ATTR_LIST_2], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1_4] = tree_cons (built_in_attributes[(int) ATTR_NONNULL], built_in_attributes[(int) ATTR_LIST_4], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_CONST_NOTHROW_NONNULL_1] = tree_cons (built_in_attributes[(int) ATTR_CONST], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_PURE_NOTHROW_NONNULL_1] = tree_cons (built_in_attributes[(int) ATTR_PURE], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_PURE_NOTHROW_NONNULL_1_2] = tree_cons (built_in_attributes[(int) ATTR_PURE], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1_2]);
built_in_attributes[(int) ATTR_MALLOC_NOTHROW_NONNULL_1] = tree_cons (built_in_attributes[(int) ATTR_MALLOC], built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_PRINTF_1_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_PRINTF], built_in_attributes[(int) ATTR_LIST_1_0]);
built_in_attributes[(int) ATTR_FORMAT_PRINTF_1_0] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_PRINTF_1_0], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_PRINTF_1_2] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_PRINTF], built_in_attributes[(int) ATTR_LIST_1_2]);
built_in_attributes[(int) ATTR_FORMAT_PRINTF_1_2] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_PRINTF_1_2], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_PRINTF_2_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_PRINTF], built_in_attributes[(int) ATTR_LIST_2_0]);
built_in_attributes[(int) ATTR_FORMAT_PRINTF_2_0] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_PRINTF_2_0], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_2]);
built_in_attributes[(int) ATTR_PRINTF_2_3] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_PRINTF], built_in_attributes[(int) ATTR_LIST_2_3]);
built_in_attributes[(int) ATTR_FORMAT_PRINTF_2_3] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_PRINTF_2_3], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_2]);
built_in_attributes[(int) ATTR_PRINTF_3_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_PRINTF], built_in_attributes[(int) ATTR_LIST_3_0]);
built_in_attributes[(int) ATTR_FORMAT_PRINTF_3_0] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_PRINTF_3_0], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_3]);
built_in_attributes[(int) ATTR_PRINTF_3_4] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_PRINTF], built_in_attributes[(int) ATTR_LIST_3_4]);
built_in_attributes[(int) ATTR_FORMAT_PRINTF_3_4] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_PRINTF_3_4], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_3]);
built_in_attributes[(int) ATTR_SCANF_1_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_SCANF], built_in_attributes[(int) ATTR_LIST_1_0]);
built_in_attributes[(int) ATTR_FORMAT_SCANF_1_0] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_SCANF_1_0], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_SCANF_1_2] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_SCANF], built_in_attributes[(int) ATTR_LIST_1_2]);
built_in_attributes[(int) ATTR_FORMAT_SCANF_1_2] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_SCANF_1_2], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_SCANF_2_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_SCANF], built_in_attributes[(int) ATTR_LIST_2_0]);
built_in_attributes[(int) ATTR_FORMAT_SCANF_2_0] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_SCANF_2_0], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_2]);
built_in_attributes[(int) ATTR_SCANF_2_3] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_SCANF], built_in_attributes[(int) ATTR_LIST_2_3]);
built_in_attributes[(int) ATTR_FORMAT_SCANF_2_3] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_SCANF_2_3], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_2]);
built_in_attributes[(int) ATTR_STRFTIME_3_0] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_STRFTIME], built_in_attributes[(int) ATTR_LIST_3_0]);
built_in_attributes[(int) ATTR_FORMAT_STRFTIME_3_0] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_STRFTIME_3_0], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_3]);
built_in_attributes[(int) ATTR_STRFMON_3_4] = tree_cons (built_in_attributes[(int) ATTR_NULL], built_in_attributes[(int) ATTR_STRFMON], built_in_attributes[(int) ATTR_LIST_3_4]);
built_in_attributes[(int) ATTR_FORMAT_STRFMON_3_4] = tree_cons (built_in_attributes[(int) ATTR_FORMAT], built_in_attributes[(int) ATTR_STRFMON_3_4], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_3]);
built_in_attributes[(int) ATTR_FORMAT_ARG_1] = tree_cons (built_in_attributes[(int) ATTR_FORMAT_ARG], built_in_attributes[(int) ATTR_LIST_1], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_1]);
built_in_attributes[(int) ATTR_FORMAT_ARG_2] = tree_cons (built_in_attributes[(int) ATTR_FORMAT_ARG], built_in_attributes[(int) ATTR_LIST_2], built_in_attributes[(int) ATTR_NOTHROW_NONNULL_2]); |
However, it is still amazing to see that there need so much codes to generate nodes for ATTR_FORMAT_FUNC_NUM_NUM. To see the nodes more clearly, we show some of nodes in below figure, we mark nodes relate to ATTR_FORMAT_PRINTF_1_0 in read.
Figure 37: builtin function attributes
Figure 37: builtin function attributes
4.3.1.7.5.5.1. Detail about attributes of builtin functions
To better understand those builtin functions, detail description about documented attributes in used are extracted from [6] in below:
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(1)), 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. The target may provide additional types of format checks.
Note (1): The declaration of vprintf is: int vprintf(const char *restrict format, va_list ap); |
format_arg (string-index) The format_arg attribute specifies that a function takes a format string for a printf, scanf, strftime or strfmon style function and modifies it (for example, to translate it into another language), so the result can be passed to a printf, scanf, strftime or strfmon 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, scanf, strftime or strfmon type function, whose format string argument is 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 one). Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two. The format-arg attribute allows you to identify your own functions which modify format strings, so that GCC can check the calls to printf, scanf, strftime or strfmon type function whose operands are a call to one of your own function. 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’ or ‘-fno-builtin’ is used. |
malloc The malloc attribute is used to tell the compiler that a function may be treated as if any non-NULL pointer it returns cannot alias any other pointer valid when the function returns. This will often improve optimization. Standard functions with this property include malloc and calloc. realloc-like functions have this property as long as the old pointer is never referred to (including comparing it to the new pointer) after the function returns a non-NULL value. |
nonnull (arg-index, ...) The nonnull attribute specifies that some function parameters should be nonnull pointers. For instance, the declaration: extern void * my_memcpy (void *dest, const void *src, size_t len) __attribute__((nonnull (1, 2))); causes the compiler to check that, in calls to my_memcpy, arguments dest and src are non-null. If the compiler determines that a null pointer is passed in an argument slot marked as non-null, and the ‘-Wnonnull’ option is enabled, a warning is issued. The compiler may also choose to make optimizations based on the knowledge that certain function arguments will not be null. If no argument index list is given to the nonnull attribute, all pointer arguments are marked as non-null. To illustrate, the following declaration is equivalent to the previous example: extern void * my_memcpy (void *dest, const void *src, size_t len) __attribute__((nonnull)); |
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 (/* . . . */) { /* . . . */ /* Print error message. */ /* . . . */ exit (1); } 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++. |
nothrow The nothrow attribute is used to inform the compiler that a function cannot throw an exception. For example, most functions in the standard C library can be guaranteed not to throw an exception with the notable exceptions of qsort and bsearch that take function pointer arguments. The nothrow attribute is not implemented in GCC versions earlier than 3.3. |
pure Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure. For example, int square (int) __attribute__ ((pure)); says that the hypothetical function square is safe to call fewer times than the program says. Some of common examples of pure functions are strlen or memcmp. Interesting non-pure functions are functions with infinite loops or those depending on volatile memory or other system resource, that may change between two consecutive calls (such as feof in a multithreading environment). The attribute pure is not implemented in GCC versions earlier than 2.96. |
const Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute, since function is not allowed to read global memory. Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void. The attribute const is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function has no side effects, which works in the current version and in some older versions, is as follows: typedef int intfn (); extern const intfn square; This approach does not work in GNU C++ from 2.6.0 on, since the language specifies that the ‘const’ must be attached to the return value. |