1<html lang="en"> 2<head> 3<title>Attribute Syntax - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Function-Attributes.html#Function-Attributes" title="Function Attributes"> 10<link rel="next" href="Function-Prototypes.html#Function-Prototypes" title="Function Prototypes"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Attribute-Syntax"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Function-Prototypes.html#Function-Prototypes">Function Prototypes</a>, 53Previous: <a rel="previous" accesskey="p" href="Function-Attributes.html#Function-Attributes">Function Attributes</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.31 Attribute Syntax</h3> 59 60<p><a name="index-attribute-syntax-2561"></a> 61This section describes the syntax with which <code>__attribute__</code> may be 62used, and the constructs to which attribute specifiers bind, for the C 63language. Some details may vary for C++ and Objective-C. Because of 64infelicities in the grammar for attributes, some forms described here 65may not be successfully parsed in all cases. 66 67 <p>There are some problems with the semantics of attributes in C++. For 68example, there are no manglings for attributes, although they may affect 69code generation, so problems may arise when attributed types are used in 70conjunction with templates or overloading. Similarly, <code>typeid</code> 71does not distinguish between types with different attributes. Support 72for attributes in C++ may be restricted in future to attributes on 73declarations only, but not on nested declarators. 74 75 <p>See <a href="Function-Attributes.html#Function-Attributes">Function Attributes</a>, for details of the semantics of attributes 76applying to functions. See <a href="Variable-Attributes.html#Variable-Attributes">Variable Attributes</a>, for details of the 77semantics of attributes applying to variables. See <a href="Type-Attributes.html#Type-Attributes">Type Attributes</a>, 78for details of the semantics of attributes applying to structure, union 79and enumerated types. 80 81 <p>An <dfn>attribute specifier</dfn> is of the form 82<code>__attribute__ ((</code><var>attribute-list</var><code>))</code>. An <dfn>attribute list</dfn> 83is a possibly empty comma-separated sequence of <dfn>attributes</dfn>, where 84each attribute is one of the following: 85 86 <ul> 87<li>Empty. Empty attributes are ignored. 88 89 <li>A word (which may be an identifier such as <code>unused</code>, or a reserved 90word such as <code>const</code>). 91 92 <li>A word, followed by, in parentheses, parameters for the attribute. 93These parameters take one of the following forms: 94 95 <ul> 96<li>An identifier. For example, <code>mode</code> attributes use this form. 97 98 <li>An identifier followed by a comma and a non-empty comma-separated list 99of expressions. For example, <code>format</code> attributes use this form. 100 101 <li>A possibly empty comma-separated list of expressions. For example, 102<code>format_arg</code> attributes use this form with the list being a single 103integer constant expression, and <code>alias</code> attributes use this form 104with the list being a single string constant. 105</ul> 106 </ul> 107 108 <p>An <dfn>attribute specifier list</dfn> is a sequence of one or more attribute 109specifiers, not separated by any other tokens. 110 111 <p>In GNU C, an attribute specifier list may appear after the colon following a 112label, other than a <code>case</code> or <code>default</code> label. The only 113attribute it makes sense to use after a label is <code>unused</code>. This 114feature is intended for code generated by programs which contains labels 115that may be unused but which is compiled with <samp><span class="option">-Wall</span></samp>. It would 116not normally be appropriate to use in it human-written code, though it 117could be useful in cases where the code that jumps to the label is 118contained within an <code>#ifdef</code> conditional. GNU C++ only permits 119attributes on labels if the attribute specifier is immediately 120followed by a semicolon (i.e., the label applies to an empty 121statement). If the semicolon is missing, C++ label attributes are 122ambiguous, as it is permissible for a declaration, which could begin 123with an attribute list, to be labelled in C++. Declarations cannot be 124labelled in C90 or C99, so the ambiguity does not arise there. 125 126 <p>An attribute specifier list may appear as part of a <code>struct</code>, 127<code>union</code> or <code>enum</code> specifier. It may go either immediately 128after the <code>struct</code>, <code>union</code> or <code>enum</code> keyword, or after 129the closing brace. The former syntax is preferred. 130Where attribute specifiers follow the closing brace, they are considered 131to relate to the structure, union or enumerated type defined, not to any 132enclosing declaration the type specifier appears in, and the type 133defined is not complete until after the attribute specifiers. 134<!-- Otherwise, there would be the following problems: a shift/reduce --> 135<!-- conflict between attributes binding the struct/union/enum and --> 136<!-- binding to the list of specifiers/qualifiers; and "aligned" --> 137<!-- attributes could use sizeof for the structure, but the size could be --> 138<!-- changed later by "packed" attributes. --> 139 140 <p>Otherwise, an attribute specifier appears as part of a declaration, 141counting declarations of unnamed parameters and type names, and relates 142to that declaration (which may be nested in another declaration, for 143example in the case of a parameter declaration), or to a particular declarator 144within a declaration. Where an 145attribute specifier is applied to a parameter declared as a function or 146an array, it should apply to the function or array rather than the 147pointer to which the parameter is implicitly converted, but this is not 148yet correctly implemented. 149 150 <p>Any list of specifiers and qualifiers at the start of a declaration may 151contain attribute specifiers, whether or not such a list may in that 152context contain storage class specifiers. (Some attributes, however, 153are essentially in the nature of storage class specifiers, and only make 154sense where storage class specifiers may be used; for example, 155<code>section</code>.) There is one necessary limitation to this syntax: the 156first old-style parameter declaration in a function definition cannot 157begin with an attribute specifier, because such an attribute applies to 158the function instead by syntax described below (which, however, is not 159yet implemented in this case). In some other cases, attribute 160specifiers are permitted by this grammar but not yet supported by the 161compiler. All attribute specifiers in this place relate to the 162declaration as a whole. In the obsolescent usage where a type of 163<code>int</code> is implied by the absence of type specifiers, such a list of 164specifiers and qualifiers may be an attribute specifier list with no 165other specifiers or qualifiers. 166 167 <p>At present, the first parameter in a function prototype must have some 168type specifier which is not an attribute specifier; this resolves an 169ambiguity in the interpretation of <code>void f(int 170(__attribute__((foo)) x))</code>, but is subject to change. At present, if 171the parentheses of a function declarator contain only attributes then 172those attributes are ignored, rather than yielding an error or warning 173or implying a single parameter of type int, but this is subject to 174change. 175 176 <p>An attribute specifier list may appear immediately before a declarator 177(other than the first) in a comma-separated list of declarators in a 178declaration of more than one identifier using a single list of 179specifiers and qualifiers. Such attribute specifiers apply 180only to the identifier before whose declarator they appear. For 181example, in 182 183<pre class="smallexample"> __attribute__((noreturn)) void d0 (void), 184 __attribute__((format(printf, 1, 2))) d1 (const char *, ...), 185 d2 (void) 186</pre> 187 <p class="noindent">the <code>noreturn</code> attribute applies to all the functions 188declared; the <code>format</code> attribute only applies to <code>d1</code>. 189 190 <p>An attribute specifier list may appear immediately before the comma, 191<code>=</code> or semicolon terminating the declaration of an identifier other 192than a function definition. Such attribute specifiers apply 193to the declared object or function. Where an 194assembler name for an object or function is specified (see <a href="Asm-Labels.html#Asm-Labels">Asm Labels</a>), the attribute must follow the <code>asm</code> 195specification. 196 197 <p>An attribute specifier list may, in future, be permitted to appear after 198the declarator in a function definition (before any old-style parameter 199declarations or the function body). 200 201 <p>Attribute specifiers may be mixed with type qualifiers appearing inside 202the <code>[]</code> of a parameter array declarator, in the C99 construct by 203which such qualifiers are applied to the pointer to which the array is 204implicitly converted. Such attribute specifiers apply to the pointer, 205not to the array, but at present this is not implemented and they are 206ignored. 207 208 <p>An attribute specifier list may appear at the start of a nested 209declarator. At present, there are some limitations in this usage: the 210attributes correctly apply to the declarator, but for most individual 211attributes the semantics this implies are not implemented. 212When attribute specifiers follow the <code>*</code> of a pointer 213declarator, they may be mixed with any type qualifiers present. 214The following describes the formal semantics of this syntax. It will make the 215most sense if you are familiar with the formal specification of 216declarators in the ISO C standard. 217 218 <p>Consider (as in C99 subclause 6.7.5 paragraph 4) a declaration <code>T 219D1</code>, where <code>T</code> contains declaration specifiers that specify a type 220<var>Type</var> (such as <code>int</code>) and <code>D1</code> is a declarator that 221contains an identifier <var>ident</var>. The type specified for <var>ident</var> 222for derived declarators whose type does not include an attribute 223specifier is as in the ISO C standard. 224 225 <p>If <code>D1</code> has the form <code>( </code><var>attribute-specifier-list</var><code> D )</code>, 226and the declaration <code>T D</code> specifies the type 227“<var>derived-declarator-type-list</var> <var>Type</var>” for <var>ident</var>, then 228<code>T D1</code> specifies the type “<var>derived-declarator-type-list</var> 229<var>attribute-specifier-list</var> <var>Type</var>” for <var>ident</var>. 230 231 <p>If <code>D1</code> has the form <code>* 232</code><var>type-qualifier-and-attribute-specifier-list</var><code> D</code>, and the 233declaration <code>T D</code> specifies the type 234“<var>derived-declarator-type-list</var> <var>Type</var>” for <var>ident</var>, then 235<code>T D1</code> specifies the type “<var>derived-declarator-type-list</var> 236<var>type-qualifier-and-attribute-specifier-list</var> pointer to <var>Type</var>” for 237<var>ident</var>. 238 239 <p>For example, 240 241<pre class="smallexample"> void (__attribute__((noreturn)) ****f) (void); 242</pre> 243 <p class="noindent">specifies the type “pointer to pointer to pointer to pointer to 244non-returning function returning <code>void</code>”. As another example, 245 246<pre class="smallexample"> char *__attribute__((aligned(8))) *f; 247</pre> 248 <p class="noindent">specifies the type “pointer to 8-byte-aligned pointer to <code>char</code>”. 249Note again that this does not work with most attributes; for example, 250the usage of ‘<samp><span class="samp">aligned</span></samp>’ and ‘<samp><span class="samp">noreturn</span></samp>’ attributes given above 251is not yet supported. 252 253 <p>For compatibility with existing code written for compiler versions that 254did not implement attributes on nested declarators, some laxity is 255allowed in the placing of attributes. If an attribute that only applies 256to types is applied to a declaration, it will be treated as applying to 257the type of that declaration. If an attribute that only applies to 258declarations is applied to the type of a declaration, it will be treated 259as applying to that declaration; and, for compatibility with code 260placing the attributes immediately before the identifier declared, such 261an attribute applied to a function return type will be treated as 262applying to the function type, and such an attribute applied to an array 263element type will be treated as applying to the array type. If an 264attribute that only applies to function types is applied to a 265pointer-to-function type, it will be treated as applying to the pointer 266target type; if such an attribute is applied to a function return type 267that is not a pointer-to-function type, it will be treated as applying 268to the function type. 269 270 </body></html> 271 272