Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

333 rindas
17KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Attribute Syntax (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Attribute Syntax (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Attribute Syntax (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Function-Prototypes.html#Function-Prototypes" rel="next" title="Function Prototypes">
  31. <link href="Statement-Attributes.html#Statement-Attributes" rel="prev" title="Statement Attributes">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Attribute-Syntax"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Function-Prototypes.html#Function-Prototypes" accesskey="n" rel="next">Function Prototypes</a>, Previous: <a href="Statement-Attributes.html#Statement-Attributes" accesskey="p" rel="prev">Statement Attributes</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Attribute-Syntax-1"></a>
  68. <h3 class="section">6.39 Attribute Syntax</h3>
  69. <a name="index-attribute-syntax"></a>
  70. <p>This section describes the syntax with which <code>__attribute__</code> may be
  71. used, and the constructs to which attribute specifiers bind, for the C
  72. language. Some details may vary for C++ and Objective-C. Because of
  73. infelicities in the grammar for attributes, some forms described here
  74. may not be successfully parsed in all cases.
  75. </p>
  76. <p>There are some problems with the semantics of attributes in C++. For
  77. example, there are no manglings for attributes, although they may affect
  78. code generation, so problems may arise when attributed types are used in
  79. conjunction with templates or overloading. Similarly, <code>typeid</code>
  80. does not distinguish between types with different attributes. Support
  81. for attributes in C++ may be restricted in future to attributes on
  82. declarations only, but not on nested declarators.
  83. </p>
  84. <p>See <a href="Function-Attributes.html#Function-Attributes">Function Attributes</a>, for details of the semantics of attributes
  85. applying to functions. See <a href="Variable-Attributes.html#Variable-Attributes">Variable Attributes</a>, for details of the
  86. semantics of attributes applying to variables. See <a href="Type-Attributes.html#Type-Attributes">Type Attributes</a>,
  87. for details of the semantics of attributes applying to structure, union
  88. and enumerated types.
  89. See <a href="Label-Attributes.html#Label-Attributes">Label Attributes</a>, for details of the semantics of attributes
  90. applying to labels.
  91. See <a href="Enumerator-Attributes.html#Enumerator-Attributes">Enumerator Attributes</a>, for details of the semantics of attributes
  92. applying to enumerators.
  93. See <a href="Statement-Attributes.html#Statement-Attributes">Statement Attributes</a>, for details of the semantics of attributes
  94. applying to statements.
  95. </p>
  96. <p>An <em>attribute specifier</em> is of the form
  97. <code>__attribute__ ((<var>attribute-list</var>))</code>. An <em>attribute list</em>
  98. is a possibly empty comma-separated sequence of <em>attributes</em>, where
  99. each attribute is one of the following:
  100. </p>
  101. <ul>
  102. <li> Empty. Empty attributes are ignored.
  103. </li><li> An attribute name
  104. (which may be an identifier such as <code>unused</code>, or a reserved
  105. word such as <code>const</code>).
  106. </li><li> An attribute name followed by a parenthesized list of
  107. parameters for the attribute.
  108. These parameters take one of the following forms:
  109. <ul>
  110. <li> An identifier. For example, <code>mode</code> attributes use this form.
  111. </li><li> An identifier followed by a comma and a non-empty comma-separated list
  112. of expressions. For example, <code>format</code> attributes use this form.
  113. </li><li> A possibly empty comma-separated list of expressions. For example,
  114. <code>format_arg</code> attributes use this form with the list being a single
  115. integer constant expression, and <code>alias</code> attributes use this form
  116. with the list being a single string constant.
  117. </li></ul>
  118. </li></ul>
  119. <p>An <em>attribute specifier list</em> is a sequence of one or more attribute
  120. specifiers, not separated by any other tokens.
  121. </p>
  122. <p>You may optionally specify attribute names with &lsquo;<samp>__</samp>&rsquo;
  123. preceding and following the name.
  124. This allows you to use them in header files without
  125. being concerned about a possible macro of the same name. For example,
  126. you may use the attribute name <code>__noreturn__</code> instead of <code>noreturn</code>.
  127. </p>
  128. <a name="Label-Attributes-2"></a>
  129. <h4 class="subsubheading">Label Attributes</h4>
  130. <p>In GNU C, an attribute specifier list may appear after the colon following a
  131. label, other than a <code>case</code> or <code>default</code> label. GNU C++ only permits
  132. attributes on labels if the attribute specifier is immediately
  133. followed by a semicolon (i.e., the label applies to an empty
  134. statement). If the semicolon is missing, C++ label attributes are
  135. ambiguous, as it is permissible for a declaration, which could begin
  136. with an attribute list, to be labelled in C++. Declarations cannot be
  137. labelled in C90 or C99, so the ambiguity does not arise there.
  138. </p>
  139. <a name="Enumerator-Attributes-2"></a>
  140. <h4 class="subsubheading">Enumerator Attributes</h4>
  141. <p>In GNU C, an attribute specifier list may appear as part of an enumerator.
  142. The attribute goes after the enumeration constant, before <code>=</code>, if
  143. present. The optional attribute in the enumerator appertains to the
  144. enumeration constant. It is not possible to place the attribute after
  145. the constant expression, if present.
  146. </p>
  147. <a name="Statement-Attributes-2"></a>
  148. <h4 class="subsubheading">Statement Attributes</h4>
  149. <p>In GNU C, an attribute specifier list may appear as part of a null
  150. statement. The attribute goes before the semicolon.
  151. </p>
  152. <a name="Type-Attributes-1"></a>
  153. <h4 class="subsubheading">Type Attributes</h4>
  154. <p>An attribute specifier list may appear as part of a <code>struct</code>,
  155. <code>union</code> or <code>enum</code> specifier. It may go either immediately
  156. after the <code>struct</code>, <code>union</code> or <code>enum</code> keyword, or after
  157. the closing brace. The former syntax is preferred.
  158. Where attribute specifiers follow the closing brace, they are considered
  159. to relate to the structure, union or enumerated type defined, not to any
  160. enclosing declaration the type specifier appears in, and the type
  161. defined is not complete until after the attribute specifiers.
  162. </p>
  163. <a name="All-other-attributes"></a>
  164. <h4 class="subsubheading">All other attributes</h4>
  165. <p>Otherwise, an attribute specifier appears as part of a declaration,
  166. counting declarations of unnamed parameters and type names, and relates
  167. to that declaration (which may be nested in another declaration, for
  168. example in the case of a parameter declaration), or to a particular declarator
  169. within a declaration. Where an
  170. attribute specifier is applied to a parameter declared as a function or
  171. an array, it should apply to the function or array rather than the
  172. pointer to which the parameter is implicitly converted, but this is not
  173. yet correctly implemented.
  174. </p>
  175. <p>Any list of specifiers and qualifiers at the start of a declaration may
  176. contain attribute specifiers, whether or not such a list may in that
  177. context contain storage class specifiers. (Some attributes, however,
  178. are essentially in the nature of storage class specifiers, and only make
  179. sense where storage class specifiers may be used; for example,
  180. <code>section</code>.) There is one necessary limitation to this syntax: the
  181. first old-style parameter declaration in a function definition cannot
  182. begin with an attribute specifier, because such an attribute applies to
  183. the function instead by syntax described below (which, however, is not
  184. yet implemented in this case). In some other cases, attribute
  185. specifiers are permitted by this grammar but not yet supported by the
  186. compiler. All attribute specifiers in this place relate to the
  187. declaration as a whole. In the obsolescent usage where a type of
  188. <code>int</code> is implied by the absence of type specifiers, such a list of
  189. specifiers and qualifiers may be an attribute specifier list with no
  190. other specifiers or qualifiers.
  191. </p>
  192. <p>At present, the first parameter in a function prototype must have some
  193. type specifier that is not an attribute specifier; this resolves an
  194. ambiguity in the interpretation of <code>void f(int
  195. (__attribute__((foo)) x))</code>, but is subject to change. At present, if
  196. the parentheses of a function declarator contain only attributes then
  197. those attributes are ignored, rather than yielding an error or warning
  198. or implying a single parameter of type int, but this is subject to
  199. change.
  200. </p>
  201. <p>An attribute specifier list may appear immediately before a declarator
  202. (other than the first) in a comma-separated list of declarators in a
  203. declaration of more than one identifier using a single list of
  204. specifiers and qualifiers. Such attribute specifiers apply
  205. only to the identifier before whose declarator they appear. For
  206. example, in
  207. </p>
  208. <div class="smallexample">
  209. <pre class="smallexample">__attribute__((noreturn)) void d0 (void),
  210. __attribute__((format(printf, 1, 2))) d1 (const char *, ...),
  211. d2 (void);
  212. </pre></div>
  213. <p>the <code>noreturn</code> attribute applies to all the functions
  214. declared; the <code>format</code> attribute only applies to <code>d1</code>.
  215. </p>
  216. <p>An attribute specifier list may appear immediately before the comma,
  217. <code>=</code> or semicolon terminating the declaration of an identifier other
  218. than a function definition. Such attribute specifiers apply
  219. to the declared object or function. Where an
  220. assembler 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>
  221. specification.
  222. </p>
  223. <p>An attribute specifier list may, in future, be permitted to appear after
  224. the declarator in a function definition (before any old-style parameter
  225. declarations or the function body).
  226. </p>
  227. <p>Attribute specifiers may be mixed with type qualifiers appearing inside
  228. the <code>[]</code> of a parameter array declarator, in the C99 construct by
  229. which such qualifiers are applied to the pointer to which the array is
  230. implicitly converted. Such attribute specifiers apply to the pointer,
  231. not to the array, but at present this is not implemented and they are
  232. ignored.
  233. </p>
  234. <p>An attribute specifier list may appear at the start of a nested
  235. declarator. At present, there are some limitations in this usage: the
  236. attributes correctly apply to the declarator, but for most individual
  237. attributes the semantics this implies are not implemented.
  238. When attribute specifiers follow the <code>*</code> of a pointer
  239. declarator, they may be mixed with any type qualifiers present.
  240. The following describes the formal semantics of this syntax. It makes the
  241. most sense if you are familiar with the formal specification of
  242. declarators in the ISO C standard.
  243. </p>
  244. <p>Consider (as in C99 subclause 6.7.5 paragraph 4) a declaration <code>T
  245. D1</code>, where <code>T</code> contains declaration specifiers that specify a type
  246. <var>Type</var> (such as <code>int</code>) and <code>D1</code> is a declarator that
  247. contains an identifier <var>ident</var>. The type specified for <var>ident</var>
  248. for derived declarators whose type does not include an attribute
  249. specifier is as in the ISO C standard.
  250. </p>
  251. <p>If <code>D1</code> has the form <code>( <var>attribute-specifier-list</var> D )</code>,
  252. and the declaration <code>T D</code> specifies the type
  253. &ldquo;<var>derived-declarator-type-list</var> <var>Type</var>&rdquo; for <var>ident</var>, then
  254. <code>T D1</code> specifies the type &ldquo;<var>derived-declarator-type-list</var>
  255. <var>attribute-specifier-list</var> <var>Type</var>&rdquo; for <var>ident</var>.
  256. </p>
  257. <p>If <code>D1</code> has the form <code>*
  258. <var>type-qualifier-and-attribute-specifier-list</var> D</code>, and the
  259. declaration <code>T D</code> specifies the type
  260. &ldquo;<var>derived-declarator-type-list</var> <var>Type</var>&rdquo; for <var>ident</var>, then
  261. <code>T D1</code> specifies the type &ldquo;<var>derived-declarator-type-list</var>
  262. <var>type-qualifier-and-attribute-specifier-list</var> pointer to <var>Type</var>&rdquo; for
  263. <var>ident</var>.
  264. </p>
  265. <p>For example,
  266. </p>
  267. <div class="smallexample">
  268. <pre class="smallexample">void (__attribute__((noreturn)) ****f) (void);
  269. </pre></div>
  270. <p>specifies the type &ldquo;pointer to pointer to pointer to pointer to
  271. non-returning function returning <code>void</code>&rdquo;. As another example,
  272. </p>
  273. <div class="smallexample">
  274. <pre class="smallexample">char *__attribute__((aligned(8))) *f;
  275. </pre></div>
  276. <p>specifies the type &ldquo;pointer to 8-byte-aligned pointer to <code>char</code>&rdquo;.
  277. Note again that this does not work with most attributes; for example,
  278. the usage of &lsquo;<samp>aligned</samp>&rsquo; and &lsquo;<samp>noreturn</samp>&rsquo; attributes given above
  279. is not yet supported.
  280. </p>
  281. <p>For compatibility with existing code written for compiler versions that
  282. did not implement attributes on nested declarators, some laxity is
  283. allowed in the placing of attributes. If an attribute that only applies
  284. to types is applied to a declaration, it is treated as applying to
  285. the type of that declaration. If an attribute that only applies to
  286. declarations is applied to the type of a declaration, it is treated
  287. as applying to that declaration; and, for compatibility with code
  288. placing the attributes immediately before the identifier declared, such
  289. an attribute applied to a function return type is treated as
  290. applying to the function type, and such an attribute applied to an array
  291. element type is treated as applying to the array type. If an
  292. attribute that only applies to function types is applied to a
  293. pointer-to-function type, it is treated as applying to the pointer
  294. target type; if such an attribute is applied to a function return type
  295. that is not a pointer-to-function type, it is treated as applying
  296. to the function type.
  297. </p>
  298. <hr>
  299. <div class="header">
  300. <p>
  301. Next: <a href="Function-Prototypes.html#Function-Prototypes" accesskey="n" rel="next">Function Prototypes</a>, Previous: <a href="Statement-Attributes.html#Statement-Attributes" accesskey="p" rel="prev">Statement Attributes</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  302. </div>
  303. </body>
  304. </html>