2.7.1. Attribute Syntax

There are three different kinds of attributes supported by the tiarmclang compiler:

2.7.1.1. General Syntax

In general, an attribute can be applied to a function, variable, or type in the following ways:

attribute-specifier <function, variable, or type>

<function, variable, or type> attribute=specifier

where an attribute-specifier consists of the following parts:

__attribute__((attribute-list))

An attribute-list consists of zero or more comma-separated attributes where each attribute can be:

  • an attribute name that takes no arguments (like noinit or persistent),

  • an attribute name that expects a list of arguments enclosed in parentheses (like aligned or section). Further details about argument requirements are provided in the descriptions of those attributes that take arguments, or

  • empty, in which case the attribute-specifier is ignored.

2.7.1.2. Examples

  • An attribute-specifier can precede a variable definition:

    __attribute__((section("my_sect"))) int my_var;
    
  • An attribute-specifier can be specified at the end of an uninitialized variable declaration:

    int my_var __attribute__((section("my_sect")));
    
  • An attribute-specifier can be applied to an initialized variable:

    int my_var __attribute__((section("my_sect"))) = 5;
    

    The attribute-specifier in this case must precede the initializer.

  • An attribute-specifier can be applied to a structure member:

    struct {
      char m1;
      int  m2 __attribute__((packed));
      int  m3;
    } packed_struct = { 10, 20, 30 };
    

    In this case, struct member m2 is aligned on a 1-byte boundary relative to the beginning of the struct due to the packed attribute, but m3 is aligned on a 4-byte boundary relative to the beginning of the struct.

  • An attribute-specifier applied to a struct type can apply to all members of the struct:

    struct __attribute__((packed)) {
      char m1;
      int  m2;
      int  m3;
    } packed_struct = { 10, 20, 30 };
    

    In this case, all members of the struct are aligned on a 1-byte boundary relative to the beginning of the struct due to the packed attribute.

  • Multiple attributes can be applied in a single attribute-specifier:

    __attribute__((noinit,location(0x100))) int noinit_location_global;
    
  • An attribute-specifier that precedes a list of function declarations applied to all of the declarations in the same statement:

    _attribute__((noreturn)) void d0 (void),
       __attribute__((format(printf, 1, 2))) d1 (const char *, ...),
       d2 (void);
    

    In this case, the noreturn attribute applies to all the declared functions, but the format attribute only applies to d1.