]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/doc/gcc/extensions-to-the-c-language-family/attribute-syntax.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / extensions-to-the-c-language-family / attribute-syntax.rst
CommitLineData
c63539ff
ML
1..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6.. index:: attribute syntax
7
8.. _attribute-syntax:
9
10Attribute Syntax
11****************
12
13This section describes the syntax with which ``__attribute__`` may be
14used, and the constructs to which attribute specifiers bind, for the C
15language. Some details may vary for C++ and Objective-C. Because of
16infelicities in the grammar for attributes, some forms described here
17may not be successfully parsed in all cases.
18
19There are some problems with the semantics of attributes in C++. For
20example, there are no manglings for attributes, although they may affect
21code generation, so problems may arise when attributed types are used in
22conjunction with templates or overloading. Similarly, ``typeid``
23does not distinguish between types with different attributes. Support
24for attributes in C++ may be restricted in future to attributes on
25declarations only, but not on nested declarators.
26
27See :ref:`function-attributes`, for details of the semantics of attributes
28applying to functions. See :ref:`variable-attributes`, for details of the
29semantics of attributes applying to variables. See :ref:`type-attributes`,
30for details of the semantics of attributes applying to structure, union
31and enumerated types.
32See :ref:`label-attributes`, for details of the semantics of attributes
33applying to labels.
34See :ref:`enumerator-attributes`, for details of the semantics of attributes
35applying to enumerators.
36See :ref:`statement-attributes`, for details of the semantics of attributes
37applying to statements.
38
39An :dfn:`attribute specifier` is of the form
40``__attribute__ ((attribute-list))``. An :dfn:`attribute list`
41is a possibly empty comma-separated sequence of :dfn:`attributes`, where
42each attribute is one of the following:
43
44* Empty. Empty attributes are ignored.
45
46* An attribute name
47 (which may be an identifier such as :fn-attr:`unused`, or a reserved
48 word such as :fn-attr:`const`).
49
50* An attribute name followed by a parenthesized list of
51 parameters for the attribute.
52 These parameters take one of the following forms:
53
54 * An identifier. For example, ``mode`` attributes use this form.
55
56 * An identifier followed by a comma and a non-empty comma-separated list
57 of expressions. For example, ``format`` attributes use this form.
58
59 * A possibly empty comma-separated list of expressions. For example,
60 ``format_arg`` attributes use this form with the list being a single
61 integer constant expression, and ``alias`` attributes use this form
62 with the list being a single string constant.
63
64An :dfn:`attribute specifier list` is a sequence of one or more attribute
65specifiers, not separated by any other tokens.
66
67You may optionally specify attribute names with :samp:`__`
68preceding and following the name.
69This allows you to use them in header files without
70being concerned about a possible macro of the same name. For example,
71you may use the attribute name ``__noreturn__`` instead of :fn-attr:`noreturn`.
72
73Label Attributes
74^^^^^^^^^^^^^^^^
75
76In GNU C, an attribute specifier list may appear after the colon following a
77label, other than a ``case`` or ``default`` label. GNU C++ only permits
78attributes on labels if the attribute specifier is immediately
79followed by a semicolon (i.e., the label applies to an empty
80statement). If the semicolon is missing, C++ label attributes are
81ambiguous, as it is permissible for a declaration, which could begin
82with an attribute list, to be labelled in C++. Declarations cannot be
83labelled in C90 or C99, so the ambiguity does not arise there.
84
85Enumerator Attributes
86^^^^^^^^^^^^^^^^^^^^^
87
88In GNU C, an attribute specifier list may appear as part of an enumerator.
89The attribute goes after the enumeration constant, before ``=``, if
90present. The optional attribute in the enumerator appertains to the
91enumeration constant. It is not possible to place the attribute after
92the constant expression, if present.
93
94Statement Attributes
95^^^^^^^^^^^^^^^^^^^^
96
97In GNU C, an attribute specifier list may appear as part of a null
98statement. The attribute goes before the semicolon.
99
100Type Attributes
101^^^^^^^^^^^^^^^
102
103An attribute specifier list may appear as part of a ``struct``,
104``union`` or ``enum`` specifier. It may go either immediately
105after the ``struct``, ``union`` or ``enum`` keyword, or after
106the closing brace. The former syntax is preferred.
107Where attribute specifiers follow the closing brace, they are considered
108to relate to the structure, union or enumerated type defined, not to any
109enclosing declaration the type specifier appears in, and the type
110defined is not complete until after the attribute specifiers.
111
112.. Otherwise, there would be the following problems: a shift/reduce
113
114.. conflict between attributes binding the struct/union/enum and
115
116.. binding to the list of specifiers/qualifiers; and "aligned"
117
118.. attributes could use sizeof for the structure, but the size could be
119
120.. changed later by "packed" attributes.
121
122All other attributes
123^^^^^^^^^^^^^^^^^^^^
124
125Otherwise, an attribute specifier appears as part of a declaration,
126counting declarations of unnamed parameters and type names, and relates
127to that declaration (which may be nested in another declaration, for
128example in the case of a parameter declaration), or to a particular declarator
129within a declaration. Where an
130attribute specifier is applied to a parameter declared as a function or
131an array, it should apply to the function or array rather than the
132pointer to which the parameter is implicitly converted, but this is not
133yet correctly implemented.
134
135Any list of specifiers and qualifiers at the start of a declaration may
136contain attribute specifiers, whether or not such a list may in that
137context contain storage class specifiers. (Some attributes, however,
138are essentially in the nature of storage class specifiers, and only make
139sense where storage class specifiers may be used; for example,
140``section``.) There is one necessary limitation to this syntax: the
141first old-style parameter declaration in a function definition cannot
142begin with an attribute specifier, because such an attribute applies to
143the function instead by syntax described below (which, however, is not
144yet implemented in this case). In some other cases, attribute
145specifiers are permitted by this grammar but not yet supported by the
146compiler. All attribute specifiers in this place relate to the
147declaration as a whole. In the obsolescent usage where a type of
148``int`` is implied by the absence of type specifiers, such a list of
149specifiers and qualifiers may be an attribute specifier list with no
150other specifiers or qualifiers.
151
152At present, the first parameter in a function prototype must have some
153type specifier that is not an attribute specifier; this resolves an
154ambiguity in the interpretation of ``void f(int
155(__attribute__((foo)) x))``, but is subject to change. At present, if
156the parentheses of a function declarator contain only attributes then
157those attributes are ignored, rather than yielding an error or warning
158or implying a single parameter of type int, but this is subject to
159change.
160
161An attribute specifier list may appear immediately before a declarator
162(other than the first) in a comma-separated list of declarators in a
163declaration of more than one identifier using a single list of
164specifiers and qualifiers. Such attribute specifiers apply
165only to the identifier before whose declarator they appear. For
166example, in
167
168.. code-block:: c++
169
170 __attribute__((noreturn)) void d0 (void),
171 __attribute__((format(printf, 1, 2))) d1 (const char *, ...),
172 d2 (void);
173
174the :fn-attr:`noreturn` attribute applies to all the functions
175declared; the ``format`` attribute only applies to ``d1``.
176
177An attribute specifier list may appear immediately before the comma,
178``=`` or semicolon terminating the declaration of an identifier other
179than a function definition. Such attribute specifiers apply
180to the declared object or function. Where an
181assembler name for an object or function is specified (see :ref:`asm-labels`), the attribute must follow the ``asm``
182specification.
183
184An attribute specifier list may, in future, be permitted to appear after
185the declarator in a function definition (before any old-style parameter
186declarations or the function body).
187
188Attribute specifiers may be mixed with type qualifiers appearing inside
189the ``[]`` of a parameter array declarator, in the C99 construct by
190which such qualifiers are applied to the pointer to which the array is
191implicitly converted. Such attribute specifiers apply to the pointer,
192not to the array, but at present this is not implemented and they are
193ignored.
194
195An attribute specifier list may appear at the start of a nested
196declarator. At present, there are some limitations in this usage: the
197attributes correctly apply to the declarator, but for most individual
198attributes the semantics this implies are not implemented.
199When attribute specifiers follow the ``*`` of a pointer
200declarator, they may be mixed with any type qualifiers present.
201The following describes the formal semantics of this syntax. It makes the
202most sense if you are familiar with the formal specification of
203declarators in the ISO C standard.
204
205Consider (as in C99 subclause 6.7.5 paragraph 4) a declaration ``T
206D1``, where ``T`` contains declaration specifiers that specify a type
207:samp:`{Type}` (such as ``int``) and ``D1`` is a declarator that
208contains an identifier :samp:`{ident}`. The type specified for :samp:`{ident}`
209for derived declarators whose type does not include an attribute
210specifier is as in the ISO C standard.
211
212If ``D1`` has the form ``( attribute-specifier-list D )``,
213and the declaration ``T D`` specifies the type
214' :samp:`{derived-declarator-type-list}` :samp:`{Type}` ' for :samp:`{ident}`, then
215``T D1`` specifies the type ' :samp:`{derived-declarator-type-list}`
216:samp:`{attribute-specifier-list}` :samp:`{Type}` ' for :samp:`{ident}`.
217
218If ``D1`` has the form ``*
219type-qualifier-and-attribute-specifier-list D``, and the
220declaration ``T D`` specifies the type
221' :samp:`{derived-declarator-type-list}` :samp:`{Type}` ' for :samp:`{ident}`, then
222``T D1`` specifies the type ' :samp:`{derived-declarator-type-list}`
223:samp:`{type-qualifier-and-attribute-specifier-list}` pointer to :samp:`{Type}` ' for
224:samp:`{ident}`.
225
226For example,
227
228.. code-block:: c++
229
230 void (__attribute__((noreturn)) ****f) (void);
231
232specifies the type 'pointer to pointer to pointer to pointer to
233non-returning function returning ``void`` '. As another example,
234
235.. code-block:: c++
236
237 char *__attribute__((aligned(8))) *f;
238
239specifies the type 'pointer to 8-byte-aligned pointer to ``char`` '.
240Note again that this does not work with most attributes; for example,
241the usage of :samp:`aligned` and :samp:`noreturn` attributes given above
242is not yet supported.
243
244For compatibility with existing code written for compiler versions that
245did not implement attributes on nested declarators, some laxity is
246allowed in the placing of attributes. If an attribute that only applies
247to types is applied to a declaration, it is treated as applying to
248the type of that declaration. If an attribute that only applies to
249declarations is applied to the type of a declaration, it is treated
250as applying to that declaration; and, for compatibility with code
251placing the attributes immediately before the identifier declared, such
252an attribute applied to a function return type is treated as
253applying to the function type, and such an attribute applied to an array
254element type is treated as applying to the array type. If an
255attribute that only applies to function types is applied to a
256pointer-to-function type, it is treated as applying to the pointer
257target type; if such an attribute is applied to a function return type
258that is not a pointer-to-function type, it is treated as applying
3ed1b4ce 259to the function type.