Function attributes (IBM extension)

Function attributes are extensions implemented to enhance the portability of programs developed with GNU C. Specifiable attributes for functions provide explicit ways to help the compiler optimize function calls and to instruct it to check more aspects of the code. Others provide additional functionality.

IBM C and C++ implement a subset of the GNU C function attributes. If a particular function attribute is not implemented, its specification is accepted and the semantics are ignored. These language features are collectively available when compiling in any of the extended language levels.

A function attribute is specified with the keyword __attribute__ followed by the attribute name and any additional arguments the attribute name requires. A function __attribute__ specification is included in the declaration or definition of a function. The syntax takes the following forms:
Read syntax diagramSkip visual syntax diagram
Function attribute syntax: function declaration

>>-function declarator--__attribute__--------------------------->

       .-,--------------------------.          
       V                            |          
>--((----+-attribute_name---------+-+--))--;-------------------><
         '-__--attribute_name--__-'            

Read syntax diagramSkip visual syntax diagram
Function attribute syntax: function definition (C only)

                      .-,--------------------------.       
                      V                            |       
>>-__attribute__--((----+-attribute name---------+-+--))-------->
                        '-__--attribute_name--__-'         

>--function_declarator--{--function body--}--------------------><

Read syntax diagramSkip visual syntax diagram
Function attribute syntax: function definition (C++ only)

>>-function declarator--__attribute__--------------------------->

       .-,--------------------------.          
       V                            |          
>--((----+-attribute_name---------+-+--))--;-------------------><
         '-__--attribute_name--__-'            

The function attribute in a function declaration is always placed after the declarator, including the parenthesized parameter declaration:
   /* Specify the attribute on a function prototype declaration */
   void f(int i, int j) __attribute__((individual_attribute_name));
   void f(int i, int j) { }

C++ only begins In C++, the attribute specification must also follow any exception declaration that may be present for the function.C++ only ends

C only begins Due to ambiguities in parsing old-style parameter declarations, a function definition must have the attribute specification precede the declarator:
int __attribute__((individual_attribute_name)) foo(int i) { }

You can specify attribute_name with or without leading and trailing double underscore characters; however, using the double underscore characters reduces the likelihood of name conflicts with macros of the same name.C only ends

IBM extension beginsThe following function attributes are supported:
  • alias
  • always_inline
  • format
  • format_arg
  • noinline
  • noreturn
  • pure
  • weak
IBM extension ends