In GNU C and C++, you can use function attributes to specify certain
function properties that may help the compiler optimize calls or
check code more carefully for correctness. For example, you
can use attributes to specify that a function never returns
(noreturn
), returns a value depending only on the values of
its arguments (const
), or has printf
-style arguments
(format
).
You can also use attributes to control memory placement, code generation options or call/return conventions within the function being annotated. Many of these attributes are target-specific. For example, many targets support attributes for defining interrupt handler functions, which typically must follow special register usage and return conventions. Such attributes are described in the subsection for each target. However, a considerable number of attributes are supported by most, if not all targets. Those are described in the Common Function Attributes section.
GCC provides two different ways to specify attributes: the traditional GNU syntax using ‘__attribute__ ((...))’ annotations, and the newer standard C and C++ syntax using ‘[[...]]’ with the ‘gnu::’ prefix on attribute names. Note that the exact rules for placement of attributes in your source code are different depending on which syntax you use. See Attribute Syntax, for details.
Compatible attribute specifications on distinct declarations of the same function are merged. An attribute specification that is not compatible with attributes already applied to a declaration of the same function is ignored with a warning.
Some function attributes take one or more arguments that refer to
the function’s parameters by their positions within the function parameter
list. Such attribute arguments are referred to as positional arguments.
Unless specified otherwise, positional arguments that specify properties
of parameters with pointer types can also specify the same properties of
the implicit C++ this
argument in non-static member functions, and
of parameters of reference to a pointer type. For ordinary functions,
position one refers to the first parameter on the list. In C++ non-static
member functions, position one refers to the implicit this
pointer.
The same restrictions and effects apply to function attributes used with
ordinary functions or C++ member functions.
GCC also supports attributes on
variable declarations (see Specifying Attributes of Variables),
labels (see Label Attributes),
enumerators (see Enumerator Attributes),
statements (see Statement Attributes),
types (see Specifying Attributes of Types),
and on field declarations (for tainted_args
).
There is some overlap between the purposes of attributes and pragmas
(see Pragmas Accepted by GCC). It has been
found convenient to use __attribute__
to achieve a natural
attachment of attributes to their corresponding declarations, whereas
#pragma
is of use for compatibility with other compilers
or constructs that do not naturally form part of the grammar.
In addition to the attributes documented here, GCC plugins may provide their own attributes.