Does __attribute__ apply to all the variables in a declaration?

From GCC: Attribute-Syntax:

An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in

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

the noreturn attribute applies to all the functions declared; the format attribute only applies to d1.


Correction: As the comment points out, my previous conclusion is incorrect. I didn't notice the other than the first part.

Modified conclusion:

In both

int __attribute__((used)) a, b, c;

and

__attribute__((used)) int a, b, c;

The attribute applies to all a, b, and c.

But if it were:

int a, __attribute__((used)) b, c;

The attribute would apply to b only.


gcc documentation (6.36 Attribute Syntax) says it only applies to the identifier before whose declarator they appear:

An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in

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

So in your example:

int __attribute__((used)) a, b, c;

the attribute only applies to a.


Referring the GCC document,

The keyword __attribute__ allows you to specify special attributes when making a declaration. This keyword is followed by an attribute specification inside double parentheses. Nine attributes, noreturn, const, format, no_instrument_function, section, constructor, destructor, unused and weak are currently defined for functions. Other attributes, including section are supported for variables declarations (see section 4.29 Specifying Attributes of Variables) and for types (see section 4.30 Specifying Attributes of Types).

Section 4.29: Attributes of Variables

unused:
This attribute, attached to a variable, means that the variable is meant to be possibly unused. GNU CC will not produce a warning for this variable.

Section 4.30: Attributes of Types

unused:
When attached to a type (including a union or a struct), this attribute means that variables of that type are meant to appear possibly unused. GNU CC will not produce a warning for any variables of that type, even if the variable appears to do nothing. This is often the case with lock or thread classes, which are usually defined and then not referenced, but contain constructors and destructors that have nontrivial bookkeeping functions

Tags:

C

Gcc