[Main]
Title=Attribute Syntax

[Top]
This section describes the syntax with which <CODE>__attribute__</CODE> may be
used, and the constructs to which attribute specifiers bind, for the C
language.  Because of
infelicities in the grammar for attributes, some forms described here
may not be successfully parsed in all cases.
<BR><BR>
See <A HREF="$$LINK(SEC85)">Function Attributes</A> for details of the semantics of attributes
applying to functions.  See <A HREF="$$LINK(SEC91)">Variable Attributes</A> for details of the
semantics of attributes applying to variables.  See <A HREF="$$LINK(SEC92)">Type Attributes</A>
for details of the semantics of attributes applying to structure, union
and enumerated types.
<BR><BR>
An <U>attribute specifier</U> is of the form
<CODE>__attribute__&nbsp;((<I>attribute-list</I>))</CODE>.  An <U>attribute list</U>
is a possibly empty comma-separated sequence of <U>attributes</U>, where
each attribute is one of the following:
<UL>
<LI>
Empty.  Empty attributes are ignored.
<BR><BR>
</LI>
<LI>
A word (which may be an identifier such as <CODE>unused</CODE>, or a reserved
word such as <CODE>const</CODE>).
<BR><BR>
</LI>
<LI>
A word, followed by, in parentheses, parameters for the attribute.
These parameters take one of the following forms:
<UL>
<LI>
An identifier.  For example, <CODE>mode</CODE> attributes use this form.
<BR><BR>
</LI>
<LI>
An identifier followed by a comma and a non-empty comma-separated list
of expressions.  For example, <CODE>format</CODE> attributes use this form.
<BR><BR>
</LI>
<LI>
A possibly empty comma-separated list of expressions.  For example,
<CODE>format_arg</CODE> attributes use this form with the list being a single
integer constant expression, and <CODE>alias</CODE> attributes use this form
with the list being a single string constant.
</LI>
</UL>
</LI>
</UL>
An <U>attribute specifier list</U> is a sequence of one or more attribute
specifiers, not separated by any other tokens.
<BR><BR>
An attribute specifier list may appear after the colon following a
label, other than a <CODE>case</CODE> or <CODE>default</CODE> label.  The only
attribute it makes sense to use after a label is <CODE>unused</CODE>.  This
feature is intended for code generated by programs which contains labels
that may be unused but which is compiled with <B>'-Wall'</B>.  It would
not normally be appropriate to use in it human-written code, though it
could be useful in cases where the code that jumps to the label is
contained within an <CODE>#ifdef</CODE> conditional.
<BR><BR>
An attribute specifier list may appear as part of a <CODE>struct</CODE>,
<CODE>union</CODE> or <CODE>enum</CODE> specifier.  It may go either immediately
after the <CODE>struct</CODE>, <CODE>union</CODE> or <CODE>enum</CODE> keyword, or after
the closing brace.  It is ignored if the content of the structure, union
or enumerated type is not defined in the specifier in which the
attribute specifier list is used - that is, in usages such as
<CODE>struct&nbsp;__attribute__((foo))&nbsp;bar</CODE> with no following opening brace.
Where attribute specifiers follow the closing brace, they are considered
to relate to the structure, union or enumerated type defined, not to any
enclosing declaration the type specifier appears in, and the type
defined is not complete until after the attribute specifiers.
<BR><BR>
Otherwise, an attribute specifier appears as part of a declaration,
counting declarations of unnamed parameters and type names, and relates
to that declaration (which may be nested in another declaration, for
example in the case of a parameter declaration), or to a particular declarator
within a declaration.  Where an
attribute specifier is applied to a parameter declared as a function or
an array, it should apply to the function or array rather than the
pointer to which the parameter is implicitly converted, but this is not
yet correctly implemented.
<BR><BR>
Any list of specifiers and qualifiers at the start of a declaration may
contain attribute specifiers, whether or not such a list may in that
context contain storage class specifiers.  (Some attributes, however,
are essentially in the nature of storage class specifiers, and only make
sense where storage class specifiers may be used; for example,
<CODE>section</CODE>.)  There is one necessary limitation to this syntax: the
first old-style parameter declaration in a function definition cannot
begin with an attribute specifier, because such an attribute applies to
the function instead by syntax described below (which, however, is not
yet implemented in this case).  In some other cases, attribute
specifiers are permitted by this grammar but not yet supported by the
compiler.  All attribute specifiers in this place relate to the
declaration as a whole.  In the obsolescent usage where a type of
<CODE>int</CODE> is implied by the absence of type specifiers, such a list of
specifiers and qualifiers may be an attribute specifier list with no
other specifiers or qualifiers.
<BR><BR>
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
<PRE>__attribute__((noreturn)) void d0 (void),
    __attribute__((format(printf, 1, 2))) d1 (const char *, ...),
     d2 (void)
</PRE>
the <CODE>noreturn</CODE> attribute applies to all the functions
declared; the <CODE>format</CODE> attribute only applies to <CODE>d1</CODE>.
<BR><BR>
An attribute specifier list may appear immediately before the comma,
<CODE>=</CODE> or semicolon terminating the declaration of an identifier other
than a function definition.  At present, such attribute specifiers apply
to the declared object or function, but in future they may attach to the
outermost adjacent declarator.  In simple cases there is no difference,
but, for example, in
<PRE>void (****f)(void) __attribute__((noreturn));
</PRE>
at present the <CODE>noreturn</CODE> attribute applies to <CODE>f</CODE>, which
causes a warning since <CODE>f</CODE> is not a function, but in future it may
apply to the function <CODE>****f</CODE>.  The precise semantics of what
attributes in such cases will apply to are not yet specified.  Where an
assembler name for an object or function is specified (see <A HREF="$$LINK(SEC96)">Asm
Labels</A>), at present the attribute must follow the <CODE>asm</CODE>
specification; in future, attributes before the <CODE>asm</CODE> specification
may apply to the adjacent declarator, and those after it to the declared
object or function.
<BR><BR>
An attribute specifier list may, in future, be permitted to appear after
the declarator in a function definition (before any old-style parameter
declarations or the function body).
<BR><BR>
Attribute specifiers may be mixed with type qualifiers appearing inside
the <CODE>[]</CODE> of a parameter array declarator, in the C99 construct by
which such qualifiers are applied to the pointer to which the array is
implicitly converted.  Such attribute specifiers apply to the pointer,
not to the array, but at present this is not implemented and they are
ignored.
<BR><BR>
An attribute specifier list may appear at the start of a nested
declarator.  At present, there are some limitations in this usage: the
attributes correctly apply to the declarator, but for most individual
attributes the semantics this implies are not implemented.
When attribute specifiers follow the <CODE>*</CODE> of a pointer
declarator, they may be mixed with any type qualifiers present.
The following describes the formal semantics of this syntax.  It will make the
most sense if you are familiar with the formal specification of
declarators in the ISO C standard.
<BR><BR>
Consider (as in C99 subclause 6.7.5 paragraph 4) a declaration <CODE>T
D1</CODE>, where <CODE>T</CODE> contains declaration specifiers that specify a type
<I>Type</I> (such as <CODE>int</CODE>) and <CODE>D1</CODE> is a declarator that
contains an identifier <I>ident</I>.  The type specified for <I>ident</I>
for derived declarators whose type does not include an attribute
specifier is as in the ISO C standard.
<BR><BR>
If <CODE>D1</CODE> has the form <CODE>(&nbsp;<I>attribute-specifier-list</I>&nbsp;D&nbsp;)</CODE>,
and the declaration <CODE>T&nbsp;D</CODE> specifies the type
&quot;<I>derived-declarator-type-list</I> <I>Type</I>&quot; for <I>ident</I>, then
<CODE>T&nbsp;D1</CODE> specifies the type &quot;<I>derived-declarator-type-list</I>
<I>attribute-specifier-list</I> <I>Type</I>&quot; for <I>ident</I>.
<BR><BR>
If <CODE>D1</CODE> has the form <CODE>*
<I>type-qualifier-and-attribute-specifier-list</I>&nbsp;D</CODE>, and the
declaration <CODE>T&nbsp;D</CODE> specifies the type
&quot;<I>derived-declarator-type-list</I> <I>Type</I>&quot; for <I>ident</I>, then
<CODE>T&nbsp;D1</CODE> specifies the type &quot;<I>derived-declarator-type-list</I>
<I>type-qualifier-and-attribute-specifier-list</I> <I>Type</I>&quot; for
<I>ident</I>.
<BR><BR>
For example,
<PRE>void (__attribute__((noreturn)) ****f) (void);
</PRE>
specifies the type &quot;pointer to pointer to pointer to pointer to
non-returning function returning <CODE>void</CODE>&quot;.  As another example,
<PRE>char *__attribute__((aligned(8))) *f;
</PRE>
specifies the type &quot;pointer to 8-byte-aligned pointer to <CODE>char</CODE>&quot;.
Note again that this does not work with most attributes; for example,
the usage of <CODE>aligned</CODE> and <CODE>noreturn</CODE> attributes given above
is not yet supported.
<BR><BR>
For compatibility with existing code written for compiler versions that
did not implement attributes on nested declarators, some laxity is
allowed in the placing of attributes.  If an attribute that only applies
to types is applied to a declaration, it will be treated as applying to
the type of that declaration.  If an attribute that only applies to
declarations is applied to the type of a declaration, it will be treated
as applying to that declaration; and, for compatibility with code
placing the attributes immediately before the identifier declared, such
an attribute applied to a function return type will be treated as
applying to the function type, and such an attribute applied to an array
element type will be treated as applying to the array type.  If an
attribute that only applies to function types is applied to a
pointer-to-function type, it will be treated as applying to the pointer
target type; if such an attribute is applied to a function return type
that is not a pointer-to-function type, it will be treated as applying
to the function type.
