Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1626 linhas
79KB

  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>Common Function Attributes (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Common Function Attributes (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Common Function Attributes (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="Function-Attributes.html#Function-Attributes" rel="up" title="Function Attributes">
  30. <link href="AArch64-Function-Attributes.html#AArch64-Function-Attributes" rel="next" title="AArch64 Function Attributes">
  31. <link href="Function-Attributes.html#Function-Attributes" rel="prev" title="Function 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="Common-Function-Attributes"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="AArch64-Function-Attributes.html#AArch64-Function-Attributes" accesskey="n" rel="next">AArch64 Function Attributes</a>, Up: <a href="Function-Attributes.html#Function-Attributes" accesskey="u" rel="up">Function Attributes</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="Common-Function-Attributes-1"></a>
  68. <h4 class="subsection">6.33.1 Common Function Attributes</h4>
  69. <p>The following attributes are supported on most targets.
  70. </p>
  71. <dl compact="compact">
  72. <dt><code>access</code></dt>
  73. <dt><code>access (<var>access-mode</var>, <var>ref-index</var>)</code></dt>
  74. <dt><code>access (<var>access-mode</var>, <var>ref-index</var>, <var>size-index</var>)</code></dt>
  75. <dd>
  76. <p>The <code>access</code> attribute enables the detection of invalid or unsafe
  77. accesses by functions to which they apply or their callers, as well as
  78. write-only accesses to objects that are never read from. Such accesses
  79. may be diagnosed by warnings such as <samp>-Wstringop-overflow</samp>,
  80. <samp>-Wuninitialized</samp>, <samp>-Wunused</samp>, and others.
  81. </p>
  82. <p>The <code>access</code> attribute specifies that a function to whose by-reference
  83. arguments the attribute applies accesses the referenced object according to
  84. <var>access-mode</var>. The <var>access-mode</var> argument is required and must be
  85. one of three names: <code>read_only</code>, <code>read_write</code>, or <code>write_only</code>.
  86. The remaining two are positional arguments.
  87. </p>
  88. <p>The required <var>ref-index</var> positional argument denotes a function
  89. argument of pointer (or in C++, reference) type that is subject to
  90. the access. The same pointer argument can be referenced by at most one
  91. distinct <code>access</code> attribute.
  92. </p>
  93. <p>The optional <var>size-index</var> positional argument denotes a function
  94. argument of integer type that specifies the maximum size of the access.
  95. The size is the number of elements of the type referenced by <var>ref-index</var>,
  96. or the number of bytes when the pointer type is <code>void*</code>. When no
  97. <var>size-index</var> argument is specified, the pointer argument must be either
  98. null or point to a space that is suitably aligned and large for at least one
  99. object of the referenced type (this implies that a past-the-end pointer is
  100. not a valid argument). The actual size of the access may be less but it
  101. must not be more.
  102. </p>
  103. <p>The <code>read_only</code> access mode specifies that the pointer to which it
  104. applies is used to read the referenced object but not write to it. Unless
  105. the argument specifying the size of the access denoted by <var>size-index</var>
  106. is zero, the referenced object must be initialized. The mode implies
  107. a stronger guarantee than the <code>const</code> qualifier which, when cast away
  108. from a pointer, does not prevent the pointed-to object from being modified.
  109. Examples of the use of the <code>read_only</code> access mode is the argument to
  110. the <code>puts</code> function, or the second and third arguments to
  111. the <code>memcpy</code> function.
  112. </p>
  113. <div class="smallexample">
  114. <pre class="smallexample">__attribute__ ((access (read_only, 1))) int puts (const char*);
  115. __attribute__ ((access (read_only, 1, 2))) void* memcpy (void*, const void*, size_t);
  116. </pre></div>
  117. <p>The <code>read_write</code> access mode applies to arguments of pointer types
  118. without the <code>const</code> qualifier. It specifies that the pointer to which
  119. it applies is used to both read and write the referenced object. Unless
  120. the argument specifying the size of the access denoted by <var>size-index</var>
  121. is zero, the object referenced by the pointer must be initialized. An example
  122. of the use of the <code>read_write</code> access mode is the first argument to
  123. the <code>strcat</code> function.
  124. </p>
  125. <div class="smallexample">
  126. <pre class="smallexample">__attribute__ ((access (read_write, 1), access (read_only, 2))) char* strcat (char*, const char*);
  127. </pre></div>
  128. <p>The <code>write_only</code> access mode applies to arguments of pointer types
  129. without the <code>const</code> qualifier. It specifies that the pointer to which
  130. it applies is used to write to the referenced object but not read from it.
  131. The object referenced by the pointer need not be initialized. An example
  132. of the use of the <code>write_only</code> access mode is the first argument to
  133. the <code>strcpy</code> function, or the first two arguments to the <code>fgets</code>
  134. function.
  135. </p>
  136. <div class="smallexample">
  137. <pre class="smallexample">__attribute__ ((access (write_only, 1), access (read_only, 2))) char* strcpy (char*, const char*);
  138. __attribute__ ((access (write_only, 1, 2), access (read_write, 3))) int fgets (char*, int, FILE*);
  139. </pre></div>
  140. </dd>
  141. <dt><code>alias (&quot;<var>target</var>&quot;)</code></dt>
  142. <dd><a name="index-alias-function-attribute"></a>
  143. <p>The <code>alias</code> attribute causes the declaration to be emitted as an alias
  144. for another symbol, which must have been previously declared with the same
  145. type, and for variables, also the same size and alignment. Declaring an alias
  146. with a different type than the target is undefined and may be diagnosed. As
  147. an example, the following declarations:
  148. </p>
  149. <div class="smallexample">
  150. <pre class="smallexample">void __f () { /* <span class="roman">Do something.</span> */; }
  151. void f () __attribute__ ((weak, alias (&quot;__f&quot;)));
  152. </pre></div>
  153. <p>define &lsquo;<samp>f</samp>&rsquo; to be a weak alias for &lsquo;<samp>__f</samp>&rsquo;. In C++, the mangled name
  154. for the target must be used. It is an error if &lsquo;<samp>__f</samp>&rsquo; is not defined in
  155. the same translation unit.
  156. </p>
  157. <p>This attribute requires assembler and object file support,
  158. and may not be available on all targets.
  159. </p>
  160. </dd>
  161. <dt><code>aligned</code></dt>
  162. <dt><code>aligned (<var>alignment</var>)</code></dt>
  163. <dd><a name="index-aligned-function-attribute"></a>
  164. <p>The <code>aligned</code> attribute specifies a minimum alignment for
  165. the first instruction of the function, measured in bytes. When specified,
  166. <var>alignment</var> must be an integer constant power of 2. Specifying no
  167. <var>alignment</var> argument implies the ideal alignment for the target.
  168. The <code>__alignof__</code> operator can be used to determine what that is
  169. (see <a href="Alignment.html#Alignment">Alignment</a>). The attribute has no effect when a definition for
  170. the function is not provided in the same translation unit.
  171. </p>
  172. <p>The attribute cannot be used to decrease the alignment of a function
  173. previously declared with a more restrictive alignment; only to increase
  174. it. Attempts to do otherwise are diagnosed. Some targets specify
  175. a minimum default alignment for functions that is greater than 1. On
  176. such targets, specifying a less restrictive alignment is silently ignored.
  177. Using the attribute overrides the effect of the <samp>-falign-functions</samp>
  178. (see <a href="Optimize-Options.html#Optimize-Options">Optimize Options</a>) option for this function.
  179. </p>
  180. <p>Note that the effectiveness of <code>aligned</code> attributes may be
  181. limited by inherent limitations in the system linker
  182. and/or object file format. On some systems, the
  183. linker is only able to arrange for functions to be aligned up to a
  184. certain maximum alignment. (For some linkers, the maximum supported
  185. alignment may be very very small.) See your linker documentation for
  186. further information.
  187. </p>
  188. <p>The <code>aligned</code> attribute can also be used for variables and fields
  189. (see <a href="Variable-Attributes.html#Variable-Attributes">Variable Attributes</a>.)
  190. </p>
  191. </dd>
  192. <dt><code>alloc_align (<var>position</var>)</code></dt>
  193. <dd><a name="index-alloc_005falign-function-attribute"></a>
  194. <p>The <code>alloc_align</code> attribute may be applied to a function that
  195. returns a pointer and takes at least one argument of an integer or
  196. enumerated type.
  197. It indicates that the returned pointer is aligned on a boundary given
  198. by the function argument at <var>position</var>. Meaningful alignments are
  199. powers of 2 greater than one. GCC uses this information to improve
  200. pointer alignment analysis.
  201. </p>
  202. <p>The function parameter denoting the allocated alignment is specified by
  203. one constant integer argument whose number is the argument of the attribute.
  204. Argument numbering starts at one.
  205. </p>
  206. <p>For instance,
  207. </p>
  208. <div class="smallexample">
  209. <pre class="smallexample">void* my_memalign (size_t, size_t) __attribute__ ((alloc_align (1)));
  210. </pre></div>
  211. <p>declares that <code>my_memalign</code> returns memory with minimum alignment
  212. given by parameter 1.
  213. </p>
  214. </dd>
  215. <dt><code>alloc_size (<var>position</var>)</code></dt>
  216. <dt><code>alloc_size (<var>position-1</var>, <var>position-2</var>)</code></dt>
  217. <dd><a name="index-alloc_005fsize-function-attribute"></a>
  218. <p>The <code>alloc_size</code> attribute may be applied to a function that
  219. returns a pointer and takes at least one argument of an integer or
  220. enumerated type.
  221. It indicates that the returned pointer points to memory whose size is
  222. given by the function argument at <var>position-1</var>, or by the product
  223. of the arguments at <var>position-1</var> and <var>position-2</var>. Meaningful
  224. sizes are positive values less than <code>PTRDIFF_MAX</code>. GCC uses this
  225. information to improve the results of <code>__builtin_object_size</code>.
  226. </p>
  227. <p>The function parameter(s) denoting the allocated size are specified by
  228. one or two integer arguments supplied to the attribute. The allocated size
  229. is either the value of the single function argument specified or the product
  230. of the two function arguments specified. Argument numbering starts at
  231. one for ordinary functions, and at two for C++ non-static member functions.
  232. </p>
  233. <p>For instance,
  234. </p>
  235. <div class="smallexample">
  236. <pre class="smallexample">void* my_calloc (size_t, size_t) __attribute__ ((alloc_size (1, 2)));
  237. void* my_realloc (void*, size_t) __attribute__ ((alloc_size (2)));
  238. </pre></div>
  239. <p>declares that <code>my_calloc</code> returns memory of the size given by
  240. the product of parameter 1 and 2 and that <code>my_realloc</code> returns memory
  241. of the size given by parameter 2.
  242. </p>
  243. </dd>
  244. <dt><code>always_inline</code></dt>
  245. <dd><a name="index-always_005finline-function-attribute"></a>
  246. <p>Generally, functions are not inlined unless optimization is specified.
  247. For functions declared inline, this attribute inlines the function
  248. independent of any restrictions that otherwise apply to inlining.
  249. Failure to inline such a function is diagnosed as an error.
  250. Note that if such a function is called indirectly the compiler may
  251. or may not inline it depending on optimization level and a failure
  252. to inline an indirect call may or may not be diagnosed.
  253. </p>
  254. </dd>
  255. <dt><code>artificial</code></dt>
  256. <dd><a name="index-artificial-function-attribute"></a>
  257. <p>This attribute is useful for small inline wrappers that if possible
  258. should appear during debugging as a unit. Depending on the debug
  259. info format it either means marking the function as artificial
  260. or using the caller location for all instructions within the inlined
  261. body.
  262. </p>
  263. </dd>
  264. <dt><code>assume_aligned (<var>alignment</var>)</code></dt>
  265. <dt><code>assume_aligned (<var>alignment</var>, <var>offset</var>)</code></dt>
  266. <dd><a name="index-assume_005faligned-function-attribute"></a>
  267. <p>The <code>assume_aligned</code> attribute may be applied to a function that
  268. returns a pointer. It indicates that the returned pointer is aligned
  269. on a boundary given by <var>alignment</var>. If the attribute has two
  270. arguments, the second argument is misalignment <var>offset</var>. Meaningful
  271. values of <var>alignment</var> are powers of 2 greater than one. Meaningful
  272. values of <var>offset</var> are greater than zero and less than <var>alignment</var>.
  273. </p>
  274. <p>For instance
  275. </p>
  276. <div class="smallexample">
  277. <pre class="smallexample">void* my_alloc1 (size_t) __attribute__((assume_aligned (16)));
  278. void* my_alloc2 (size_t) __attribute__((assume_aligned (32, 8)));
  279. </pre></div>
  280. <p>declares that <code>my_alloc1</code> returns 16-byte aligned pointers and
  281. that <code>my_alloc2</code> returns a pointer whose value modulo 32 is equal
  282. to 8.
  283. </p>
  284. </dd>
  285. <dt><code>cold</code></dt>
  286. <dd><a name="index-cold-function-attribute"></a>
  287. <p>The <code>cold</code> attribute on functions is used to inform the compiler that
  288. the function is unlikely to be executed. The function is optimized for
  289. size rather than speed and on many targets it is placed into a special
  290. subsection of the text section so all cold functions appear close together,
  291. improving code locality of non-cold parts of program. The paths leading
  292. to calls of cold functions within code are marked as unlikely by the branch
  293. prediction mechanism. It is thus useful to mark functions used to handle
  294. unlikely conditions, such as <code>perror</code>, as cold to improve optimization
  295. of hot functions that do call marked functions in rare occasions.
  296. </p>
  297. <p>When profile feedback is available, via <samp>-fprofile-use</samp>, cold functions
  298. are automatically detected and this attribute is ignored.
  299. </p>
  300. </dd>
  301. <dt><code>const</code></dt>
  302. <dd><a name="index-const-function-attribute"></a>
  303. <a name="index-functions-that-have-no-side-effects"></a>
  304. <p>Calls to functions whose return value is not affected by changes to
  305. the observable state of the program and that have no observable effects
  306. on such state other than to return a value may lend themselves to
  307. optimizations such as common subexpression elimination. Declaring such
  308. functions with the <code>const</code> attribute allows GCC to avoid emitting
  309. some calls in repeated invocations of the function with the same argument
  310. values.
  311. </p>
  312. <p>For example,
  313. </p>
  314. <div class="smallexample">
  315. <pre class="smallexample">int square (int) __attribute__ ((const));
  316. </pre></div>
  317. <p>tells GCC that subsequent calls to function <code>square</code> with the same
  318. argument value can be replaced by the result of the first call regardless
  319. of the statements in between.
  320. </p>
  321. <p>The <code>const</code> attribute prohibits a function from reading objects
  322. that affect its return value between successive invocations. However,
  323. functions declared with the attribute can safely read objects that do
  324. not change their return value, such as non-volatile constants.
  325. </p>
  326. <p>The <code>const</code> attribute imposes greater restrictions on a function&rsquo;s
  327. definition than the similar <code>pure</code> attribute. Declaring the same
  328. function with both the <code>const</code> and the <code>pure</code> attribute is
  329. diagnosed. Because a const function cannot have any observable side
  330. effects it does not make sense for it to return <code>void</code>. Declaring
  331. such a function is diagnosed.
  332. </p>
  333. <a name="index-pointer-arguments"></a>
  334. <p>Note that a function that has pointer arguments and examines the data
  335. pointed to must <em>not</em> be declared <code>const</code> if the pointed-to
  336. data might change between successive invocations of the function. In
  337. general, since a function cannot distinguish data that might change
  338. from data that cannot, const functions should never take pointer or,
  339. in C++, reference arguments. Likewise, a function that calls a non-const
  340. function usually must not be const itself.
  341. </p>
  342. </dd>
  343. <dt><code>constructor</code></dt>
  344. <dt><code>destructor</code></dt>
  345. <dt><code>constructor (<var>priority</var>)</code></dt>
  346. <dt><code>destructor (<var>priority</var>)</code></dt>
  347. <dd><a name="index-constructor-function-attribute"></a>
  348. <a name="index-destructor-function-attribute"></a>
  349. <p>The <code>constructor</code> attribute causes the function to be called
  350. automatically before execution enters <code>main ()</code>. Similarly, the
  351. <code>destructor</code> attribute causes the function to be called
  352. automatically after <code>main ()</code> completes or <code>exit ()</code> is
  353. called. Functions with these attributes are useful for
  354. initializing data that is used implicitly during the execution of
  355. the program.
  356. </p>
  357. <p>On some targets the attributes also accept an integer argument to
  358. specify a priority to control the order in which constructor and
  359. destructor functions are run. A constructor
  360. with a smaller priority number runs before a constructor with a larger
  361. priority number; the opposite relationship holds for destructors. So,
  362. if you have a constructor that allocates a resource and a destructor
  363. that deallocates the same resource, both functions typically have the
  364. same priority. The priorities for constructor and destructor
  365. functions are the same as those specified for namespace-scope C++
  366. objects (see <a href="C_002b_002b-Attributes.html#C_002b_002b-Attributes">C++ Attributes</a>). However, at present, the order in which
  367. constructors for C++ objects with static storage duration and functions
  368. decorated with attribute <code>constructor</code> are invoked is unspecified.
  369. In mixed declarations, attribute <code>init_priority</code> can be used to
  370. impose a specific ordering.
  371. </p>
  372. <p>Using the argument forms of the <code>constructor</code> and <code>destructor</code>
  373. attributes on targets where the feature is not supported is rejected with
  374. an error.
  375. </p>
  376. </dd>
  377. <dt><code>copy</code></dt>
  378. <dt><code>copy (<var>function</var>)</code></dt>
  379. <dd><a name="index-copy-function-attribute"></a>
  380. <p>The <code>copy</code> attribute applies the set of attributes with which
  381. <var>function</var> has been declared to the declaration of the function
  382. to which the attribute is applied. The attribute is designed for
  383. libraries that define aliases or function resolvers that are expected
  384. to specify the same set of attributes as their targets. The <code>copy</code>
  385. attribute can be used with functions, variables, or types. However,
  386. the kind of symbol to which the attribute is applied (either function
  387. or variable) must match the kind of symbol to which the argument refers.
  388. The <code>copy</code> attribute copies only syntactic and semantic attributes
  389. but not attributes that affect a symbol&rsquo;s linkage or visibility such as
  390. <code>alias</code>, <code>visibility</code>, or <code>weak</code>. The <code>deprecated</code>
  391. and <code>target_clones</code> attribute are also not copied.
  392. See <a href="Common-Type-Attributes.html#Common-Type-Attributes">Common Type Attributes</a>.
  393. See <a href="Common-Variable-Attributes.html#Common-Variable-Attributes">Common Variable Attributes</a>.
  394. </p>
  395. <p>For example, the <var>StrongAlias</var> macro below makes use of the <code>alias</code>
  396. and <code>copy</code> attributes to define an alias named <var>alloc</var> for function
  397. <var>allocate</var> declared with attributes <var>alloc_size</var>, <var>malloc</var>, and
  398. <var>nothrow</var>. Thanks to the <code>__typeof__</code> operator the alias has
  399. the same type as the target function. As a result of the <code>copy</code>
  400. attribute the alias also shares the same attributes as the target.
  401. </p>
  402. <div class="smallexample">
  403. <pre class="smallexample">#define StrongAlias(TargetFunc, AliasDecl) \
  404. extern __typeof__ (TargetFunc) AliasDecl \
  405. __attribute__ ((alias (#TargetFunc), copy (TargetFunc)));
  406. extern __attribute__ ((alloc_size (1), malloc, nothrow))
  407. void* allocate (size_t);
  408. StrongAlias (allocate, alloc);
  409. </pre></div>
  410. </dd>
  411. <dt><code>deprecated</code></dt>
  412. <dt><code>deprecated (<var>msg</var>)</code></dt>
  413. <dd><a name="index-deprecated-function-attribute"></a>
  414. <p>The <code>deprecated</code> attribute results in a warning if the function
  415. is used anywhere in the source file. This is useful when identifying
  416. functions that are expected to be removed in a future version of a
  417. program. The warning also includes the location of the declaration
  418. of the deprecated function, to enable users to easily find further
  419. information about why the function is deprecated, or what they should
  420. do instead. Note that the warnings only occurs for uses:
  421. </p>
  422. <div class="smallexample">
  423. <pre class="smallexample">int old_fn () __attribute__ ((deprecated));
  424. int old_fn ();
  425. int (*fn_ptr)() = old_fn;
  426. </pre></div>
  427. <p>results in a warning on line 3 but not line 2. The optional <var>msg</var>
  428. argument, which must be a string, is printed in the warning if
  429. present.
  430. </p>
  431. <p>The <code>deprecated</code> attribute can also be used for variables and
  432. types (see <a href="Variable-Attributes.html#Variable-Attributes">Variable Attributes</a>, see <a href="Type-Attributes.html#Type-Attributes">Type Attributes</a>.)
  433. </p>
  434. <p>The message attached to the attribute is affected by the setting of
  435. the <samp>-fmessage-length</samp> option.
  436. </p>
  437. </dd>
  438. <dt><code>error (&quot;<var>message</var>&quot;)</code></dt>
  439. <dt><code>warning (&quot;<var>message</var>&quot;)</code></dt>
  440. <dd><a name="index-error-function-attribute"></a>
  441. <a name="index-warning-function-attribute"></a>
  442. <p>If the <code>error</code> or <code>warning</code> attribute
  443. is used on a function declaration and a call to such a function
  444. is not eliminated through dead code elimination or other optimizations,
  445. an error or warning (respectively) that includes <var>message</var> is diagnosed.
  446. This is useful
  447. for compile-time checking, especially together with <code>__builtin_constant_p</code>
  448. and inline functions where checking the inline function arguments is not
  449. possible through <code>extern char [(condition) ? 1 : -1];</code> tricks.
  450. </p>
  451. <p>While it is possible to leave the function undefined and thus invoke
  452. a link failure (to define the function with
  453. a message in <code>.gnu.warning*</code> section),
  454. when using these attributes the problem is diagnosed
  455. earlier and with exact location of the call even in presence of inline
  456. functions or when not emitting debugging information.
  457. </p>
  458. </dd>
  459. <dt><code>externally_visible</code></dt>
  460. <dd><a name="index-externally_005fvisible-function-attribute"></a>
  461. <p>This attribute, attached to a global variable or function, nullifies
  462. the effect of the <samp>-fwhole-program</samp> command-line option, so the
  463. object remains visible outside the current compilation unit.
  464. </p>
  465. <p>If <samp>-fwhole-program</samp> is used together with <samp>-flto</samp> and
  466. <code>gold</code> is used as the linker plugin,
  467. <code>externally_visible</code> attributes are automatically added to functions
  468. (not variable yet due to a current <code>gold</code> issue)
  469. that are accessed outside of LTO objects according to resolution file
  470. produced by <code>gold</code>.
  471. For other linkers that cannot generate resolution file,
  472. explicit <code>externally_visible</code> attributes are still necessary.
  473. </p>
  474. </dd>
  475. <dt><code>flatten</code></dt>
  476. <dd><a name="index-flatten-function-attribute"></a>
  477. <p>Generally, inlining into a function is limited. For a function marked with
  478. this attribute, every call inside this function is inlined, if possible.
  479. Functions declared with attribute <code>noinline</code> and similar are not
  480. inlined. Whether the function itself is considered for inlining depends
  481. on its size and the current inlining parameters.
  482. </p>
  483. </dd>
  484. <dt><code>format (<var>archetype</var>, <var>string-index</var>, <var>first-to-check</var>)</code></dt>
  485. <dd><a name="index-format-function-attribute"></a>
  486. <a name="index-functions-with-printf_002c-scanf_002c-strftime-or-strfmon-style-arguments"></a>
  487. <a name="index-Wformat-3"></a>
  488. <p>The <code>format</code> attribute specifies that a function takes <code>printf</code>,
  489. <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> style arguments that
  490. should be type-checked against a format string. For example, the
  491. declaration:
  492. </p>
  493. <div class="smallexample">
  494. <pre class="smallexample">extern int
  495. my_printf (void *my_object, const char *my_format, ...)
  496. __attribute__ ((format (printf, 2, 3)));
  497. </pre></div>
  498. <p>causes the compiler to check the arguments in calls to <code>my_printf</code>
  499. for consistency with the <code>printf</code> style format string argument
  500. <code>my_format</code>.
  501. </p>
  502. <p>The parameter <var>archetype</var> determines how the format string is
  503. interpreted, and should be <code>printf</code>, <code>scanf</code>, <code>strftime</code>,
  504. <code>gnu_printf</code>, <code>gnu_scanf</code>, <code>gnu_strftime</code> or
  505. <code>strfmon</code>. (You can also use <code>__printf__</code>,
  506. <code>__scanf__</code>, <code>__strftime__</code> or <code>__strfmon__</code>.) On
  507. MinGW targets, <code>ms_printf</code>, <code>ms_scanf</code>, and
  508. <code>ms_strftime</code> are also present.
  509. <var>archetype</var> values such as <code>printf</code> refer to the formats accepted
  510. by the system&rsquo;s C runtime library,
  511. while values prefixed with &lsquo;<samp>gnu_</samp>&rsquo; always refer
  512. to the formats accepted by the GNU C Library. On Microsoft Windows
  513. targets, values prefixed with &lsquo;<samp>ms_</samp>&rsquo; refer to the formats accepted by the
  514. <samp>msvcrt.dll</samp> library.
  515. The parameter <var>string-index</var>
  516. specifies which argument is the format string argument (starting
  517. from 1), while <var>first-to-check</var> is the number of the first
  518. argument to check against the format string. For functions
  519. where the arguments are not available to be checked (such as
  520. <code>vprintf</code>), specify the third parameter as zero. In this case the
  521. compiler only checks the format string for consistency. For
  522. <code>strftime</code> formats, the third parameter is required to be zero.
  523. Since non-static C++ methods have an implicit <code>this</code> argument, the
  524. arguments of such methods should be counted from two, not one, when
  525. giving values for <var>string-index</var> and <var>first-to-check</var>.
  526. </p>
  527. <p>In the example above, the format string (<code>my_format</code>) is the second
  528. argument of the function <code>my_print</code>, and the arguments to check
  529. start with the third argument, so the correct parameters for the format
  530. attribute are 2 and 3.
  531. </p>
  532. <a name="index-ffreestanding-3"></a>
  533. <a name="index-fno_002dbuiltin-2"></a>
  534. <p>The <code>format</code> attribute allows you to identify your own functions
  535. that take format strings as arguments, so that GCC can check the
  536. calls to these functions for errors. The compiler always (unless
  537. <samp>-ffreestanding</samp> or <samp>-fno-builtin</samp> is used) checks formats
  538. for the standard library functions <code>printf</code>, <code>fprintf</code>,
  539. <code>sprintf</code>, <code>scanf</code>, <code>fscanf</code>, <code>sscanf</code>, <code>strftime</code>,
  540. <code>vprintf</code>, <code>vfprintf</code> and <code>vsprintf</code> whenever such
  541. warnings are requested (using <samp>-Wformat</samp>), so there is no need to
  542. modify the header file <samp>stdio.h</samp>. In C99 mode, the functions
  543. <code>snprintf</code>, <code>vsnprintf</code>, <code>vscanf</code>, <code>vfscanf</code> and
  544. <code>vsscanf</code> are also checked. Except in strictly conforming C
  545. standard modes, the X/Open function <code>strfmon</code> is also checked as
  546. are <code>printf_unlocked</code> and <code>fprintf_unlocked</code>.
  547. See <a href="C-Dialect-Options.html#C-Dialect-Options">Options Controlling C Dialect</a>.
  548. </p>
  549. <p>For Objective-C dialects, <code>NSString</code> (or <code>__NSString__</code>) is
  550. recognized in the same context. Declarations including these format attributes
  551. are parsed for correct syntax, however the result of checking of such format
  552. strings is not yet defined, and is not carried out by this version of the
  553. compiler.
  554. </p>
  555. <p>The target may also provide additional types of format checks.
  556. See <a href="Target-Format-Checks.html#Target-Format-Checks">Format Checks Specific to Particular
  557. Target Machines</a>.
  558. </p>
  559. </dd>
  560. <dt><code>format_arg (<var>string-index</var>)</code></dt>
  561. <dd><a name="index-format_005farg-function-attribute"></a>
  562. <a name="index-Wformat_002dnonliteral-1"></a>
  563. <p>The <code>format_arg</code> attribute specifies that a function takes one or
  564. more format strings for a <code>printf</code>, <code>scanf</code>, <code>strftime</code> or
  565. <code>strfmon</code> style function and modifies it (for example, to translate
  566. it into another language), so the result can be passed to a
  567. <code>printf</code>, <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> style
  568. function (with the remaining arguments to the format function the same
  569. as they would have been for the unmodified string). Multiple
  570. <code>format_arg</code> attributes may be applied to the same function, each
  571. designating a distinct parameter as a format string. For example, the
  572. declaration:
  573. </p>
  574. <div class="smallexample">
  575. <pre class="smallexample">extern char *
  576. my_dgettext (char *my_domain, const char *my_format)
  577. __attribute__ ((format_arg (2)));
  578. </pre></div>
  579. <p>causes the compiler to check the arguments in calls to a <code>printf</code>,
  580. <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> type function, whose
  581. format string argument is a call to the <code>my_dgettext</code> function, for
  582. consistency with the format string argument <code>my_format</code>. If the
  583. <code>format_arg</code> attribute had not been specified, all the compiler
  584. could tell in such calls to format functions would be that the format
  585. string argument is not constant; this would generate a warning when
  586. <samp>-Wformat-nonliteral</samp> is used, but the calls could not be checked
  587. without the attribute.
  588. </p>
  589. <p>In calls to a function declared with more than one <code>format_arg</code>
  590. attribute, each with a distinct argument value, the corresponding
  591. actual function arguments are checked against all format strings
  592. designated by the attributes. This capability is designed to support
  593. the GNU <code>ngettext</code> family of functions.
  594. </p>
  595. <p>The parameter <var>string-index</var> specifies which argument is the format
  596. string argument (starting from one). Since non-static C++ methods have
  597. an implicit <code>this</code> argument, the arguments of such methods should
  598. be counted from two.
  599. </p>
  600. <p>The <code>format_arg</code> attribute allows you to identify your own
  601. functions that modify format strings, so that GCC can check the
  602. calls to <code>printf</code>, <code>scanf</code>, <code>strftime</code> or <code>strfmon</code>
  603. type function whose operands are a call to one of your own function.
  604. The compiler always treats <code>gettext</code>, <code>dgettext</code>, and
  605. <code>dcgettext</code> in this manner except when strict ISO C support is
  606. requested by <samp>-ansi</samp> or an appropriate <samp>-std</samp> option, or
  607. <samp>-ffreestanding</samp> or <samp>-fno-builtin</samp>
  608. is used. See <a href="C-Dialect-Options.html#C-Dialect-Options">Options
  609. Controlling C Dialect</a>.
  610. </p>
  611. <p>For Objective-C dialects, the <code>format-arg</code> attribute may refer to an
  612. <code>NSString</code> reference for compatibility with the <code>format</code> attribute
  613. above.
  614. </p>
  615. <p>The target may also allow additional types in <code>format-arg</code> attributes.
  616. See <a href="Target-Format-Checks.html#Target-Format-Checks">Format Checks Specific to Particular
  617. Target Machines</a>.
  618. </p>
  619. </dd>
  620. <dt><code>gnu_inline</code></dt>
  621. <dd><a name="index-gnu_005finline-function-attribute"></a>
  622. <p>This attribute should be used with a function that is also declared
  623. with the <code>inline</code> keyword. It directs GCC to treat the function
  624. as if it were defined in gnu90 mode even when compiling in C99 or
  625. gnu99 mode.
  626. </p>
  627. <p>If the function is declared <code>extern</code>, then this definition of the
  628. function is used only for inlining. In no case is the function
  629. compiled as a standalone function, not even if you take its address
  630. explicitly. Such an address becomes an external reference, as if you
  631. had only declared the function, and had not defined it. This has
  632. almost the effect of a macro. The way to use this is to put a
  633. function definition in a header file with this attribute, and put
  634. another copy of the function, without <code>extern</code>, in a library
  635. file. The definition in the header file causes most calls to the
  636. function to be inlined. If any uses of the function remain, they
  637. refer to the single copy in the library. Note that the two
  638. definitions of the functions need not be precisely the same, although
  639. if they do not have the same effect your program may behave oddly.
  640. </p>
  641. <p>In C, if the function is neither <code>extern</code> nor <code>static</code>, then
  642. the function is compiled as a standalone function, as well as being
  643. inlined where possible.
  644. </p>
  645. <p>This is how GCC traditionally handled functions declared
  646. <code>inline</code>. Since ISO C99 specifies a different semantics for
  647. <code>inline</code>, this function attribute is provided as a transition
  648. measure and as a useful feature in its own right. This attribute is
  649. available in GCC 4.1.3 and later. It is available if either of the
  650. preprocessor macros <code>__GNUC_GNU_INLINE__</code> or
  651. <code>__GNUC_STDC_INLINE__</code> are defined. See <a href="Inline.html#Inline">An Inline
  652. Function is As Fast As a Macro</a>.
  653. </p>
  654. <p>In C++, this attribute does not depend on <code>extern</code> in any way,
  655. but it still requires the <code>inline</code> keyword to enable its special
  656. behavior.
  657. </p>
  658. </dd>
  659. <dt><code>hot</code></dt>
  660. <dd><a name="index-hot-function-attribute"></a>
  661. <p>The <code>hot</code> attribute on a function is used to inform the compiler that
  662. the function is a hot spot of the compiled program. The function is
  663. optimized more aggressively and on many targets it is placed into a special
  664. subsection of the text section so all hot functions appear close together,
  665. improving locality.
  666. </p>
  667. <p>When profile feedback is available, via <samp>-fprofile-use</samp>, hot functions
  668. are automatically detected and this attribute is ignored.
  669. </p>
  670. </dd>
  671. <dt><code>ifunc (&quot;<var>resolver</var>&quot;)</code></dt>
  672. <dd><a name="index-ifunc-function-attribute"></a>
  673. <a name="index-indirect-functions"></a>
  674. <a name="index-functions-that-are-dynamically-resolved"></a>
  675. <p>The <code>ifunc</code> attribute is used to mark a function as an indirect
  676. function using the STT_GNU_IFUNC symbol type extension to the ELF
  677. standard. This allows the resolution of the symbol value to be
  678. determined dynamically at load time, and an optimized version of the
  679. routine to be selected for the particular processor or other system
  680. characteristics determined then. To use this attribute, first define
  681. the implementation functions available, and a resolver function that
  682. returns a pointer to the selected implementation function. The
  683. implementation functions&rsquo; declarations must match the API of the
  684. function being implemented. The resolver should be declared to
  685. be a function taking no arguments and returning a pointer to
  686. a function of the same type as the implementation. For example:
  687. </p>
  688. <div class="smallexample">
  689. <pre class="smallexample">void *my_memcpy (void *dst, const void *src, size_t len)
  690. {
  691. &hellip;
  692. return dst;
  693. }
  694. static void * (*resolve_memcpy (void))(void *, const void *, size_t)
  695. {
  696. return my_memcpy; // we will just always select this routine
  697. }
  698. </pre></div>
  699. <p>The exported header file declaring the function the user calls would
  700. contain:
  701. </p>
  702. <div class="smallexample">
  703. <pre class="smallexample">extern void *memcpy (void *, const void *, size_t);
  704. </pre></div>
  705. <p>allowing the user to call <code>memcpy</code> as a regular function, unaware of
  706. the actual implementation. Finally, the indirect function needs to be
  707. defined in the same translation unit as the resolver function:
  708. </p>
  709. <div class="smallexample">
  710. <pre class="smallexample">void *memcpy (void *, const void *, size_t)
  711. __attribute__ ((ifunc (&quot;resolve_memcpy&quot;)));
  712. </pre></div>
  713. <p>In C++, the <code>ifunc</code> attribute takes a string that is the mangled name
  714. of the resolver function. A C++ resolver for a non-static member function
  715. of class <code>C</code> should be declared to return a pointer to a non-member
  716. function taking pointer to <code>C</code> as the first argument, followed by
  717. the same arguments as of the implementation function. G++ checks
  718. the signatures of the two functions and issues
  719. a <samp>-Wattribute-alias</samp> warning for mismatches. To suppress a warning
  720. for the necessary cast from a pointer to the implementation member function
  721. to the type of the corresponding non-member function use
  722. the <samp>-Wno-pmf-conversions</samp> option. For example:
  723. </p>
  724. <div class="smallexample">
  725. <pre class="smallexample">class S
  726. {
  727. private:
  728. int debug_impl (int);
  729. int optimized_impl (int);
  730. typedef int Func (S*, int);
  731. static Func* resolver ();
  732. public:
  733. int interface (int);
  734. };
  735. int S::debug_impl (int) { /* <span class="roman">&hellip;</span> */ }
  736. int S::optimized_impl (int) { /* <span class="roman">&hellip;</span> */ }
  737. S::Func* S::resolver ()
  738. {
  739. int (S::*pimpl) (int)
  740. = getenv (&quot;DEBUG&quot;) ? &amp;S::debug_impl : &amp;S::optimized_impl;
  741. // Cast triggers -Wno-pmf-conversions.
  742. return reinterpret_cast&lt;Func*&gt;(pimpl);
  743. }
  744. int S::interface (int) __attribute__ ((ifunc (&quot;_ZN1S8resolverEv&quot;)));
  745. </pre></div>
  746. <p>Indirect functions cannot be weak. Binutils version 2.20.1 or higher
  747. and GNU C Library version 2.11.1 are required to use this feature.
  748. </p>
  749. </dd>
  750. <dt><code>interrupt</code></dt>
  751. <dt><code>interrupt_handler</code></dt>
  752. <dd><p>Many GCC back ends support attributes to indicate that a function is
  753. an interrupt handler, which tells the compiler to generate function
  754. entry and exit sequences that differ from those from regular
  755. functions. The exact syntax and behavior are target-specific;
  756. refer to the following subsections for details.
  757. </p>
  758. </dd>
  759. <dt><code>leaf</code></dt>
  760. <dd><a name="index-leaf-function-attribute"></a>
  761. <p>Calls to external functions with this attribute must return to the
  762. current compilation unit only by return or by exception handling. In
  763. particular, a leaf function is not allowed to invoke callback functions
  764. passed to it from the current compilation unit, directly call functions
  765. exported by the unit, or <code>longjmp</code> into the unit. Leaf functions
  766. might still call functions from other compilation units and thus they
  767. are not necessarily leaf in the sense that they contain no function
  768. calls at all.
  769. </p>
  770. <p>The attribute is intended for library functions to improve dataflow
  771. analysis. The compiler takes the hint that any data not escaping the
  772. current compilation unit cannot be used or modified by the leaf
  773. function. For example, the <code>sin</code> function is a leaf function, but
  774. <code>qsort</code> is not.
  775. </p>
  776. <p>Note that leaf functions might indirectly run a signal handler defined
  777. in the current compilation unit that uses static variables. Similarly,
  778. when lazy symbol resolution is in effect, leaf functions might invoke
  779. indirect functions whose resolver function or implementation function is
  780. defined in the current compilation unit and uses static variables. There
  781. is no standard-compliant way to write such a signal handler, resolver
  782. function, or implementation function, and the best that you can do is to
  783. remove the <code>leaf</code> attribute or mark all such static variables
  784. <code>volatile</code>. Lastly, for ELF-based systems that support symbol
  785. interposition, care should be taken that functions defined in the
  786. current compilation unit do not unexpectedly interpose other symbols
  787. based on the defined standards mode and defined feature test macros;
  788. otherwise an inadvertent callback would be added.
  789. </p>
  790. <p>The attribute has no effect on functions defined within the current
  791. compilation unit. This is to allow easy merging of multiple compilation
  792. units into one, for example, by using the link-time optimization. For
  793. this reason the attribute is not allowed on types to annotate indirect
  794. calls.
  795. </p>
  796. </dd>
  797. <dt><code>malloc</code></dt>
  798. <dd><a name="index-malloc-function-attribute"></a>
  799. <a name="index-functions-that-behave-like-malloc"></a>
  800. <p>This tells the compiler that a function is <code>malloc</code>-like, i.e.,
  801. that the pointer <var>P</var> returned by the function cannot alias any
  802. other pointer valid when the function returns, and moreover no
  803. pointers to valid objects occur in any storage addressed by <var>P</var>.
  804. </p>
  805. <p>Using this attribute can improve optimization. Compiler predicts
  806. that a function with the attribute returns non-null in most cases.
  807. Functions like
  808. <code>malloc</code> and <code>calloc</code> have this property because they return
  809. a pointer to uninitialized or zeroed-out storage. However, functions
  810. like <code>realloc</code> do not have this property, as they can return a
  811. pointer to storage containing pointers.
  812. </p>
  813. </dd>
  814. <dt><code>no_icf</code></dt>
  815. <dd><a name="index-no_005ficf-function-attribute"></a>
  816. <p>This function attribute prevents a functions from being merged with another
  817. semantically equivalent function.
  818. </p>
  819. </dd>
  820. <dt><code>no_instrument_function</code></dt>
  821. <dd><a name="index-no_005finstrument_005ffunction-function-attribute"></a>
  822. <a name="index-finstrument_002dfunctions-1"></a>
  823. <a name="index-p-1"></a>
  824. <a name="index-pg-1"></a>
  825. <p>If any of <samp>-finstrument-functions</samp>, <samp>-p</samp>, or <samp>-pg</samp> are
  826. given, profiling function calls are
  827. generated at entry and exit of most user-compiled functions.
  828. Functions with this attribute are not so instrumented.
  829. </p>
  830. </dd>
  831. <dt><code>no_profile_instrument_function</code></dt>
  832. <dd><a name="index-no_005fprofile_005finstrument_005ffunction-function-attribute"></a>
  833. <p>The <code>no_profile_instrument_function</code> attribute on functions is used
  834. to inform the compiler that it should not process any profile feedback based
  835. optimization code instrumentation.
  836. </p>
  837. </dd>
  838. <dt><code>no_reorder</code></dt>
  839. <dd><a name="index-no_005freorder-function-attribute"></a>
  840. <p>Do not reorder functions or variables marked <code>no_reorder</code>
  841. against each other or top level assembler statements the executable.
  842. The actual order in the program will depend on the linker command
  843. line. Static variables marked like this are also not removed.
  844. This has a similar effect
  845. as the <samp>-fno-toplevel-reorder</samp> option, but only applies to the
  846. marked symbols.
  847. </p>
  848. </dd>
  849. <dt><code>no_sanitize (&quot;<var>sanitize_option</var>&quot;)</code></dt>
  850. <dd><a name="index-no_005fsanitize-function-attribute"></a>
  851. <p>The <code>no_sanitize</code> attribute on functions is used
  852. to inform the compiler that it should not do sanitization of any option
  853. mentioned in <var>sanitize_option</var>. A list of values acceptable by
  854. the <samp>-fsanitize</samp> option can be provided.
  855. </p>
  856. <div class="smallexample">
  857. <pre class="smallexample">void __attribute__ ((no_sanitize (&quot;alignment&quot;, &quot;object-size&quot;)))
  858. f () { /* <span class="roman">Do something.</span> */; }
  859. void __attribute__ ((no_sanitize (&quot;alignment,object-size&quot;)))
  860. g () { /* <span class="roman">Do something.</span> */; }
  861. </pre></div>
  862. </dd>
  863. <dt><code>no_sanitize_address</code></dt>
  864. <dt><code>no_address_safety_analysis</code></dt>
  865. <dd><a name="index-no_005fsanitize_005faddress-function-attribute"></a>
  866. <p>The <code>no_sanitize_address</code> attribute on functions is used
  867. to inform the compiler that it should not instrument memory accesses
  868. in the function when compiling with the <samp>-fsanitize=address</samp> option.
  869. The <code>no_address_safety_analysis</code> is a deprecated alias of the
  870. <code>no_sanitize_address</code> attribute, new code should use
  871. <code>no_sanitize_address</code>.
  872. </p>
  873. </dd>
  874. <dt><code>no_sanitize_thread</code></dt>
  875. <dd><a name="index-no_005fsanitize_005fthread-function-attribute"></a>
  876. <p>The <code>no_sanitize_thread</code> attribute on functions is used
  877. to inform the compiler that it should not instrument memory accesses
  878. in the function when compiling with the <samp>-fsanitize=thread</samp> option.
  879. </p>
  880. </dd>
  881. <dt><code>no_sanitize_undefined</code></dt>
  882. <dd><a name="index-no_005fsanitize_005fundefined-function-attribute"></a>
  883. <p>The <code>no_sanitize_undefined</code> attribute on functions is used
  884. to inform the compiler that it should not check for undefined behavior
  885. in the function when compiling with the <samp>-fsanitize=undefined</samp> option.
  886. </p>
  887. </dd>
  888. <dt><code>no_split_stack</code></dt>
  889. <dd><a name="index-no_005fsplit_005fstack-function-attribute"></a>
  890. <a name="index-fsplit_002dstack-1"></a>
  891. <p>If <samp>-fsplit-stack</samp> is given, functions have a small
  892. prologue which decides whether to split the stack. Functions with the
  893. <code>no_split_stack</code> attribute do not have that prologue, and thus
  894. may run with only a small amount of stack space available.
  895. </p>
  896. </dd>
  897. <dt><code>no_stack_limit</code></dt>
  898. <dd><a name="index-no_005fstack_005flimit-function-attribute"></a>
  899. <p>This attribute locally overrides the <samp>-fstack-limit-register</samp>
  900. and <samp>-fstack-limit-symbol</samp> command-line options; it has the effect
  901. of disabling stack limit checking in the function it applies to.
  902. </p>
  903. </dd>
  904. <dt><code>noclone</code></dt>
  905. <dd><a name="index-noclone-function-attribute"></a>
  906. <p>This function attribute prevents a function from being considered for
  907. cloning&mdash;a mechanism that produces specialized copies of functions
  908. and which is (currently) performed by interprocedural constant
  909. propagation.
  910. </p>
  911. </dd>
  912. <dt><code>noinline</code></dt>
  913. <dd><a name="index-noinline-function-attribute"></a>
  914. <p>This function attribute prevents a function from being considered for
  915. inlining.
  916. If the function does not have side effects, there are optimizations
  917. other than inlining that cause function calls to be optimized away,
  918. although the function call is live. To keep such calls from being
  919. optimized away, put
  920. </p><div class="smallexample">
  921. <pre class="smallexample">asm (&quot;&quot;);
  922. </pre></div>
  923. <p>(see <a href="Extended-Asm.html#Extended-Asm">Extended Asm</a>) in the called function, to serve as a special
  924. side effect.
  925. </p>
  926. </dd>
  927. <dt><code>noipa</code></dt>
  928. <dd><a name="index-noipa-function-attribute"></a>
  929. <p>Disable interprocedural optimizations between the function with this
  930. attribute and its callers, as if the body of the function is not available
  931. when optimizing callers and the callers are unavailable when optimizing
  932. the body. This attribute implies <code>noinline</code>, <code>noclone</code> and
  933. <code>no_icf</code> attributes. However, this attribute is not equivalent
  934. to a combination of other attributes, because its purpose is to suppress
  935. existing and future optimizations employing interprocedural analysis,
  936. including those that do not have an attribute suitable for disabling
  937. them individually. This attribute is supported mainly for the purpose
  938. of testing the compiler.
  939. </p>
  940. </dd>
  941. <dt><code>nonnull</code></dt>
  942. <dt><code>nonnull (<var>arg-index</var>, &hellip;)</code></dt>
  943. <dd><a name="index-nonnull-function-attribute"></a>
  944. <a name="index-functions-with-non_002dnull-pointer-arguments"></a>
  945. <p>The <code>nonnull</code> attribute may be applied to a function that takes at
  946. least one argument of a pointer type. It indicates that the referenced
  947. arguments must be non-null pointers. For instance, the declaration:
  948. </p>
  949. <div class="smallexample">
  950. <pre class="smallexample">extern void *
  951. my_memcpy (void *dest, const void *src, size_t len)
  952. __attribute__((nonnull (1, 2)));
  953. </pre></div>
  954. <p>causes the compiler to check that, in calls to <code>my_memcpy</code>,
  955. arguments <var>dest</var> and <var>src</var> are non-null. If the compiler
  956. determines that a null pointer is passed in an argument slot marked
  957. as non-null, and the <samp>-Wnonnull</samp> option is enabled, a warning
  958. is issued. See <a href="Warning-Options.html#Warning-Options">Warning Options</a>. Unless disabled by
  959. the <samp>-fno-delete-null-pointer-checks</samp> option the compiler may
  960. also perform optimizations based on the knowledge that certain function
  961. arguments cannot be null. In addition,
  962. the <samp>-fisolate-erroneous-paths-attribute</samp> option can be specified
  963. to have GCC transform calls with null arguments to non-null functions
  964. into traps. See <a href="Optimize-Options.html#Optimize-Options">Optimize Options</a>.
  965. </p>
  966. <p>If no <var>arg-index</var> is given to the <code>nonnull</code> attribute,
  967. all pointer arguments are marked as non-null. To illustrate, the
  968. following declaration is equivalent to the previous example:
  969. </p>
  970. <div class="smallexample">
  971. <pre class="smallexample">extern void *
  972. my_memcpy (void *dest, const void *src, size_t len)
  973. __attribute__((nonnull));
  974. </pre></div>
  975. </dd>
  976. <dt><code>noplt</code></dt>
  977. <dd><a name="index-noplt-function-attribute"></a>
  978. <p>The <code>noplt</code> attribute is the counterpart to option <samp>-fno-plt</samp>.
  979. Calls to functions marked with this attribute in position-independent code
  980. do not use the PLT.
  981. </p>
  982. <div class="smallexample">
  983. <pre class="smallexample">/* Externally defined function foo. */
  984. int foo () __attribute__ ((noplt));
  985. int
  986. main (/* <span class="roman">&hellip;</span> */)
  987. {
  988. /* <span class="roman">&hellip;</span> */
  989. foo ();
  990. /* <span class="roman">&hellip;</span> */
  991. }
  992. </pre></div>
  993. <p>The <code>noplt</code> attribute on function <code>foo</code>
  994. tells the compiler to assume that
  995. the function <code>foo</code> is externally defined and that the call to
  996. <code>foo</code> must avoid the PLT
  997. in position-independent code.
  998. </p>
  999. <p>In position-dependent code, a few targets also convert calls to
  1000. functions that are marked to not use the PLT to use the GOT instead.
  1001. </p>
  1002. </dd>
  1003. <dt><code>noreturn</code></dt>
  1004. <dd><a name="index-noreturn-function-attribute"></a>
  1005. <a name="index-functions-that-never-return"></a>
  1006. <p>A few standard library functions, such as <code>abort</code> and <code>exit</code>,
  1007. cannot return. GCC knows this automatically. Some programs define
  1008. their own functions that never return. You can declare them
  1009. <code>noreturn</code> to tell the compiler this fact. For example,
  1010. </p>
  1011. <div class="smallexample">
  1012. <pre class="smallexample">void fatal () __attribute__ ((noreturn));
  1013. void
  1014. fatal (/* <span class="roman">&hellip;</span> */)
  1015. {
  1016. /* <span class="roman">&hellip;</span> */ /* <span class="roman">Print error message.</span> */ /* <span class="roman">&hellip;</span> */
  1017. exit (1);
  1018. }
  1019. </pre></div>
  1020. <p>The <code>noreturn</code> keyword tells the compiler to assume that
  1021. <code>fatal</code> cannot return. It can then optimize without regard to what
  1022. would happen if <code>fatal</code> ever did return. This makes slightly
  1023. better code. More importantly, it helps avoid spurious warnings of
  1024. uninitialized variables.
  1025. </p>
  1026. <p>The <code>noreturn</code> keyword does not affect the exceptional path when that
  1027. applies: a <code>noreturn</code>-marked function may still return to the caller
  1028. by throwing an exception or calling <code>longjmp</code>.
  1029. </p>
  1030. <p>In order to preserve backtraces, GCC will never turn calls to
  1031. <code>noreturn</code> functions into tail calls.
  1032. </p>
  1033. <p>Do not assume that registers saved by the calling function are
  1034. restored before calling the <code>noreturn</code> function.
  1035. </p>
  1036. <p>It does not make sense for a <code>noreturn</code> function to have a return
  1037. type other than <code>void</code>.
  1038. </p>
  1039. </dd>
  1040. <dt><code>nothrow</code></dt>
  1041. <dd><a name="index-nothrow-function-attribute"></a>
  1042. <p>The <code>nothrow</code> attribute is used to inform the compiler that a
  1043. function cannot throw an exception. For example, most functions in
  1044. the standard C library can be guaranteed not to throw an exception
  1045. with the notable exceptions of <code>qsort</code> and <code>bsearch</code> that
  1046. take function pointer arguments.
  1047. </p>
  1048. </dd>
  1049. <dt><code>optimize (<var>level</var>, &hellip;)</code></dt>
  1050. <dt><code>optimize (<var>string</var>, &hellip;)</code></dt>
  1051. <dd><a name="index-optimize-function-attribute"></a>
  1052. <p>The <code>optimize</code> attribute is used to specify that a function is to
  1053. be compiled with different optimization options than specified on the
  1054. command line. Valid arguments are constant non-negative integers and
  1055. strings. Each numeric argument specifies an optimization <var>level</var>.
  1056. Each <var>string</var> argument consists of one or more comma-separated
  1057. substrings. Each substring that begins with the letter <code>O</code> refers
  1058. to an optimization option such as <samp>-O0</samp> or <samp>-Os</samp>. Other
  1059. substrings are taken as suffixes to the <code>-f</code> prefix jointly
  1060. forming the name of an optimization option. See <a href="Optimize-Options.html#Optimize-Options">Optimize Options</a>.
  1061. </p>
  1062. <p>&lsquo;<samp>#pragma GCC optimize</samp>&rsquo; can be used to set optimization options
  1063. for more than one function. See <a href="Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas">Function Specific Option Pragmas</a>,
  1064. for details about the pragma.
  1065. </p>
  1066. <p>Providing multiple strings as arguments separated by commas to specify
  1067. multiple options is equivalent to separating the option suffixes with
  1068. a comma (&lsquo;<samp>,</samp>&rsquo;) within a single string. Spaces are not permitted
  1069. within the strings.
  1070. </p>
  1071. <p>Not every optimization option that starts with the <var>-f</var> prefix
  1072. specified by the attribute necessarily has an effect on the function.
  1073. The <code>optimize</code> attribute should be used for debugging purposes only.
  1074. It is not suitable in production code.
  1075. </p>
  1076. </dd>
  1077. <dt><code>patchable_function_entry</code></dt>
  1078. <dd><a name="index-patchable_005ffunction_005fentry-function-attribute"></a>
  1079. <a name="index-extra-NOP-instructions-at-the-function-entry-point"></a>
  1080. <p>In case the target&rsquo;s text segment can be made writable at run time by
  1081. any means, padding the function entry with a number of NOPs can be
  1082. used to provide a universal tool for instrumentation.
  1083. </p>
  1084. <p>The <code>patchable_function_entry</code> function attribute can be used to
  1085. change the number of NOPs to any desired value. The two-value syntax
  1086. is the same as for the command-line switch
  1087. <samp>-fpatchable-function-entry=N,M</samp>, generating <var>N</var> NOPs, with
  1088. the function entry point before the <var>M</var>th NOP instruction.
  1089. <var>M</var> defaults to 0 if omitted e.g. function entry point is before
  1090. the first NOP.
  1091. </p>
  1092. <p>If patchable function entries are enabled globally using the command-line
  1093. option <samp>-fpatchable-function-entry=N,M</samp>, then you must disable
  1094. instrumentation on all functions that are part of the instrumentation
  1095. framework with the attribute <code>patchable_function_entry (0)</code>
  1096. to prevent recursion.
  1097. </p>
  1098. </dd>
  1099. <dt><code>pure</code></dt>
  1100. <dd><a name="index-pure-function-attribute"></a>
  1101. <a name="index-functions-that-have-no-side-effects-1"></a>
  1102. <p>Calls to functions that have no observable effects on the state of
  1103. the program other than to return a value may lend themselves to optimizations
  1104. such as common subexpression elimination. Declaring such functions with
  1105. the <code>pure</code> attribute allows GCC to avoid emitting some calls in repeated
  1106. invocations of the function with the same argument values.
  1107. </p>
  1108. <p>The <code>pure</code> attribute prohibits a function from modifying the state
  1109. of the program that is observable by means other than inspecting
  1110. the function&rsquo;s return value. However, functions declared with the <code>pure</code>
  1111. attribute can safely read any non-volatile objects, and modify the value of
  1112. objects in a way that does not affect their return value or the observable
  1113. state of the program.
  1114. </p>
  1115. <p>For example,
  1116. </p>
  1117. <div class="smallexample">
  1118. <pre class="smallexample">int hash (char *) __attribute__ ((pure));
  1119. </pre></div>
  1120. <p>tells GCC that subsequent calls to the function <code>hash</code> with the same
  1121. string can be replaced by the result of the first call provided the state
  1122. of the program observable by <code>hash</code>, including the contents of the array
  1123. itself, does not change in between. Even though <code>hash</code> takes a non-const
  1124. pointer argument it must not modify the array it points to, or any other object
  1125. whose value the rest of the program may depend on. However, the caller may
  1126. safely change the contents of the array between successive calls to
  1127. the function (doing so disables the optimization). The restriction also
  1128. applies to member objects referenced by the <code>this</code> pointer in C++
  1129. non-static member functions.
  1130. </p>
  1131. <p>Some common examples of pure functions are <code>strlen</code> or <code>memcmp</code>.
  1132. Interesting non-pure functions are functions with infinite loops or those
  1133. depending on volatile memory or other system resource, that may change between
  1134. consecutive calls (such as the standard C <code>feof</code> function in
  1135. a multithreading environment).
  1136. </p>
  1137. <p>The <code>pure</code> attribute imposes similar but looser restrictions on
  1138. a function&rsquo;s definition than the <code>const</code> attribute: <code>pure</code>
  1139. allows the function to read any non-volatile memory, even if it changes
  1140. in between successive invocations of the function. Declaring the same
  1141. function with both the <code>pure</code> and the <code>const</code> attribute is
  1142. diagnosed. Because a pure function cannot have any observable side
  1143. effects it does not make sense for such a function to return <code>void</code>.
  1144. Declaring such a function is diagnosed.
  1145. </p>
  1146. </dd>
  1147. <dt><code>returns_nonnull</code></dt>
  1148. <dd><a name="index-returns_005fnonnull-function-attribute"></a>
  1149. <p>The <code>returns_nonnull</code> attribute specifies that the function
  1150. return value should be a non-null pointer. For instance, the declaration:
  1151. </p>
  1152. <div class="smallexample">
  1153. <pre class="smallexample">extern void *
  1154. mymalloc (size_t len) __attribute__((returns_nonnull));
  1155. </pre></div>
  1156. <p>lets the compiler optimize callers based on the knowledge
  1157. that the return value will never be null.
  1158. </p>
  1159. </dd>
  1160. <dt><code>returns_twice</code></dt>
  1161. <dd><a name="index-returns_005ftwice-function-attribute"></a>
  1162. <a name="index-functions-that-return-more-than-once"></a>
  1163. <p>The <code>returns_twice</code> attribute tells the compiler that a function may
  1164. return more than one time. The compiler ensures that all registers
  1165. are dead before calling such a function and emits a warning about
  1166. the variables that may be clobbered after the second return from the
  1167. function. Examples of such functions are <code>setjmp</code> and <code>vfork</code>.
  1168. The <code>longjmp</code>-like counterpart of such function, if any, might need
  1169. to be marked with the <code>noreturn</code> attribute.
  1170. </p>
  1171. </dd>
  1172. <dt><code>section (&quot;<var>section-name</var>&quot;)</code></dt>
  1173. <dd><a name="index-section-function-attribute"></a>
  1174. <a name="index-functions-in-arbitrary-sections"></a>
  1175. <p>Normally, the compiler places the code it generates in the <code>text</code> section.
  1176. Sometimes, however, you need additional sections, or you need certain
  1177. particular functions to appear in special sections. The <code>section</code>
  1178. attribute specifies that a function lives in a particular section.
  1179. For example, the declaration:
  1180. </p>
  1181. <div class="smallexample">
  1182. <pre class="smallexample">extern void foobar (void) __attribute__ ((section (&quot;bar&quot;)));
  1183. </pre></div>
  1184. <p>puts the function <code>foobar</code> in the <code>bar</code> section.
  1185. </p>
  1186. <p>Some file formats do not support arbitrary sections so the <code>section</code>
  1187. attribute is not available on all platforms.
  1188. If you need to map the entire contents of a module to a particular
  1189. section, consider using the facilities of the linker instead.
  1190. </p>
  1191. </dd>
  1192. <dt><code>sentinel</code></dt>
  1193. <dt><code>sentinel (<var>position</var>)</code></dt>
  1194. <dd><a name="index-sentinel-function-attribute"></a>
  1195. <p>This function attribute indicates that an argument in a call to the function
  1196. is expected to be an explicit <code>NULL</code>. The attribute is only valid on
  1197. variadic functions. By default, the sentinel is expected to be the last
  1198. argument of the function call. If the optional <var>position</var> argument
  1199. is specified to the attribute, the sentinel must be located at
  1200. <var>position</var> counting backwards from the end of the argument list.
  1201. </p>
  1202. <div class="smallexample">
  1203. <pre class="smallexample">__attribute__ ((sentinel))
  1204. is equivalent to
  1205. __attribute__ ((sentinel(0)))
  1206. </pre></div>
  1207. <p>The attribute is automatically set with a position of 0 for the built-in
  1208. functions <code>execl</code> and <code>execlp</code>. The built-in function
  1209. <code>execle</code> has the attribute set with a position of 1.
  1210. </p>
  1211. <p>A valid <code>NULL</code> in this context is defined as zero with any object
  1212. pointer type. If your system defines the <code>NULL</code> macro with
  1213. an integer type then you need to add an explicit cast. During
  1214. installation GCC replaces the system <code>&lt;stddef.h&gt;</code> header with
  1215. a copy that redefines NULL appropriately.
  1216. </p>
  1217. <p>The warnings for missing or incorrect sentinels are enabled with
  1218. <samp>-Wformat</samp>.
  1219. </p>
  1220. </dd>
  1221. <dt><code>simd</code></dt>
  1222. <dt><code>simd(&quot;<var>mask</var>&quot;)</code></dt>
  1223. <dd><a name="index-simd-function-attribute"></a>
  1224. <p>This attribute enables creation of one or more function versions that
  1225. can process multiple arguments using SIMD instructions from a
  1226. single invocation. Specifying this attribute allows compiler to
  1227. assume that such versions are available at link time (provided
  1228. in the same or another translation unit). Generated versions are
  1229. target-dependent and described in the corresponding Vector ABI document. For
  1230. x86_64 target this document can be found
  1231. <a href="https://sourceware.org/glibc/wiki/libmvec?action=AttachFile&amp;do=view&amp;target=VectorABI.txt">here</a><!-- /@w -->.
  1232. </p>
  1233. <p>The optional argument <var>mask</var> may have the value
  1234. <code>notinbranch</code> or <code>inbranch</code>,
  1235. and instructs the compiler to generate non-masked or masked
  1236. clones correspondingly. By default, all clones are generated.
  1237. </p>
  1238. <p>If the attribute is specified and <code>#pragma omp declare simd</code> is
  1239. present on a declaration and the <samp>-fopenmp</samp> or <samp>-fopenmp-simd</samp>
  1240. switch is specified, then the attribute is ignored.
  1241. </p>
  1242. </dd>
  1243. <dt><code>stack_protect</code></dt>
  1244. <dd><a name="index-stack_005fprotect-function-attribute"></a>
  1245. <p>This attribute adds stack protection code to the function if
  1246. flags <samp>-fstack-protector</samp>, <samp>-fstack-protector-strong</samp>
  1247. or <samp>-fstack-protector-explicit</samp> are set.
  1248. </p>
  1249. </dd>
  1250. <dt><code>target (<var>string</var>, &hellip;)</code></dt>
  1251. <dd><a name="index-target-function-attribute"></a>
  1252. <p>Multiple target back ends implement the <code>target</code> attribute
  1253. to specify that a function is to
  1254. be compiled with different target options than specified on the
  1255. command line. One or more strings can be provided as arguments.
  1256. Each string consists of one or more comma-separated suffixes to
  1257. the <code>-m</code> prefix jointly forming the name of a machine-dependent
  1258. option. See <a href="Submodel-Options.html#Submodel-Options">Machine-Dependent Options</a>.
  1259. </p>
  1260. <p>The <code>target</code> attribute can be used for instance to have a function
  1261. compiled with a different ISA (instruction set architecture) than the
  1262. default. &lsquo;<samp>#pragma GCC target</samp>&rsquo; can be used to specify target-specific
  1263. options for more than one function. See <a href="Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas">Function Specific Option Pragmas</a>,
  1264. for details about the pragma.
  1265. </p>
  1266. <p>For instance, on an x86, you could declare one function with the
  1267. <code>target(&quot;sse4.1,arch=core2&quot;)</code> attribute and another with
  1268. <code>target(&quot;sse4a,arch=amdfam10&quot;)</code>. This is equivalent to
  1269. compiling the first function with <samp>-msse4.1</samp> and
  1270. <samp>-march=core2</samp> options, and the second function with
  1271. <samp>-msse4a</samp> and <samp>-march=amdfam10</samp> options. It is up to you
  1272. to make sure that a function is only invoked on a machine that
  1273. supports the particular ISA it is compiled for (for example by using
  1274. <code>cpuid</code> on x86 to determine what feature bits and architecture
  1275. family are used).
  1276. </p>
  1277. <div class="smallexample">
  1278. <pre class="smallexample">int core2_func (void) __attribute__ ((__target__ (&quot;arch=core2&quot;)));
  1279. int sse3_func (void) __attribute__ ((__target__ (&quot;sse3&quot;)));
  1280. </pre></div>
  1281. <p>Providing multiple strings as arguments separated by commas to specify
  1282. multiple options is equivalent to separating the option suffixes with
  1283. a comma (&lsquo;<samp>,</samp>&rsquo;) within a single string. Spaces are not permitted
  1284. within the strings.
  1285. </p>
  1286. <p>The options supported are specific to each target; refer to <a href="x86-Function-Attributes.html#x86-Function-Attributes">x86 Function Attributes</a>, <a href="PowerPC-Function-Attributes.html#PowerPC-Function-Attributes">PowerPC Function Attributes</a>,
  1287. <a href="ARM-Function-Attributes.html#ARM-Function-Attributes">ARM Function Attributes</a>, <a href="AArch64-Function-Attributes.html#AArch64-Function-Attributes">AArch64 Function Attributes</a>,
  1288. <a href="Nios-II-Function-Attributes.html#Nios-II-Function-Attributes">Nios II Function Attributes</a>, and <a href="S_002f390-Function-Attributes.html#S_002f390-Function-Attributes">S/390 Function Attributes</a>
  1289. for details.
  1290. </p>
  1291. </dd>
  1292. <dt><code>symver (&quot;<var>name2</var>@<var>nodename</var>&quot;)</code></dt>
  1293. <dd><a name="index-symver-function-attribute"></a>
  1294. <p>On ELF targets this attribute creates a symbol version. The <var>name2</var> part
  1295. of the parameter is the actual name of the symbol by which it will be
  1296. externally referenced. The <code>nodename</code> portion should be the name of a
  1297. node specified in the version script supplied to the linker when building a
  1298. shared library. Versioned symbol must be defined and must be exported with
  1299. default visibility.
  1300. </p>
  1301. <div class="smallexample">
  1302. <pre class="smallexample">__attribute__ ((__symver__ (&quot;foo@VERS_1&quot;))) int
  1303. foo_v1 (void)
  1304. {
  1305. }
  1306. </pre></div>
  1307. <p>Will produce a <code>.symver foo_v1, foo@VERS_1</code> directive in the assembler
  1308. output.
  1309. </p>
  1310. <p>It&rsquo;s an error to define multiple version of a given symbol. In such case
  1311. an alias can be used.
  1312. </p>
  1313. <div class="smallexample">
  1314. <pre class="smallexample">__attribute__ ((__symver__ (&quot;foo@VERS_2&quot;)))
  1315. __attribute__ ((alias (&quot;foo_v1&quot;)))
  1316. int symver_foo_v1 (void);
  1317. </pre></div>
  1318. <p>This example creates an alias of <code>foo_v1</code> with symbol name
  1319. <code>symver_foo_v1</code> which will be version <code>VERS_2</code> of <code>foo</code>.
  1320. </p>
  1321. <p>Finally if the parameter is <code>&quot;<var>name2</var>@@<var>nodename</var>&quot;</code> then in
  1322. addition to creating a symbol version (as if
  1323. <code>&quot;<var>name2</var>@<var>nodename</var>&quot;</code> was used) the version will be also used
  1324. to resolve <var>name2</var> by the linker.
  1325. </p>
  1326. </dd>
  1327. <dt><code>target_clones (<var>options</var>)</code></dt>
  1328. <dd><a name="index-target_005fclones-function-attribute"></a>
  1329. <p>The <code>target_clones</code> attribute is used to specify that a function
  1330. be cloned into multiple versions compiled with different target options
  1331. than specified on the command line. The supported options and restrictions
  1332. are the same as for <code>target</code> attribute.
  1333. </p>
  1334. <p>For instance, on an x86, you could compile a function with
  1335. <code>target_clones(&quot;sse4.1,avx&quot;)</code>. GCC creates two function clones,
  1336. one compiled with <samp>-msse4.1</samp> and another with <samp>-mavx</samp>.
  1337. </p>
  1338. <p>On a PowerPC, you can compile a function with
  1339. <code>target_clones(&quot;cpu=power9,default&quot;)</code>. GCC will create two
  1340. function clones, one compiled with <samp>-mcpu=power9</samp> and another
  1341. with the default options. GCC must be configured to use GLIBC 2.23 or
  1342. newer in order to use the <code>target_clones</code> attribute.
  1343. </p>
  1344. <p>It also creates a resolver function (see
  1345. the <code>ifunc</code> attribute above) that dynamically selects a clone
  1346. suitable for current architecture. The resolver is created only if there
  1347. is a usage of a function with <code>target_clones</code> attribute.
  1348. </p>
  1349. <p>Note that any subsequent call of a function without <code>target_clone</code>
  1350. from a <code>target_clone</code> caller will not lead to copying
  1351. (target clone) of the called function.
  1352. If you want to enforce such behaviour,
  1353. we recommend declaring the calling function with the <code>flatten</code> attribute?
  1354. </p>
  1355. </dd>
  1356. <dt><code>unused</code></dt>
  1357. <dd><a name="index-unused-function-attribute"></a>
  1358. <p>This attribute, attached to a function, means that the function is meant
  1359. to be possibly unused. GCC does not produce a warning for this
  1360. function.
  1361. </p>
  1362. </dd>
  1363. <dt><code>used</code></dt>
  1364. <dd><a name="index-used-function-attribute"></a>
  1365. <p>This attribute, attached to a function, means that code must be emitted
  1366. for the function even if it appears that the function is not referenced.
  1367. This is useful, for example, when the function is referenced only in
  1368. inline assembly.
  1369. </p>
  1370. <p>When applied to a member function of a C++ class template, the
  1371. attribute also means that the function is instantiated if the
  1372. class itself is instantiated.
  1373. </p>
  1374. </dd>
  1375. <dt><code>visibility (&quot;<var>visibility_type</var>&quot;)</code></dt>
  1376. <dd><a name="index-visibility-function-attribute"></a>
  1377. <p>This attribute affects the linkage of the declaration to which it is attached.
  1378. It can be applied to variables (see <a href="Common-Variable-Attributes.html#Common-Variable-Attributes">Common Variable Attributes</a>) and types
  1379. (see <a href="Common-Type-Attributes.html#Common-Type-Attributes">Common Type Attributes</a>) as well as functions.
  1380. </p>
  1381. <p>There are four supported <var>visibility_type</var> values: default,
  1382. hidden, protected or internal visibility.
  1383. </p>
  1384. <div class="smallexample">
  1385. <pre class="smallexample">void __attribute__ ((visibility (&quot;protected&quot;)))
  1386. f () { /* <span class="roman">Do something.</span> */; }
  1387. int i __attribute__ ((visibility (&quot;hidden&quot;)));
  1388. </pre></div>
  1389. <p>The possible values of <var>visibility_type</var> correspond to the
  1390. visibility settings in the ELF gABI.
  1391. </p>
  1392. <dl compact="compact">
  1393. <dt><code>default</code></dt>
  1394. <dd><p>Default visibility is the normal case for the object file format.
  1395. This value is available for the visibility attribute to override other
  1396. options that may change the assumed visibility of entities.
  1397. </p>
  1398. <p>On ELF, default visibility means that the declaration is visible to other
  1399. modules and, in shared libraries, means that the declared entity may be
  1400. overridden.
  1401. </p>
  1402. <p>On Darwin, default visibility means that the declaration is visible to
  1403. other modules.
  1404. </p>
  1405. <p>Default visibility corresponds to &ldquo;external linkage&rdquo; in the language.
  1406. </p>
  1407. </dd>
  1408. <dt><code>hidden</code></dt>
  1409. <dd><p>Hidden visibility indicates that the entity declared has a new
  1410. form of linkage, which we call &ldquo;hidden linkage&rdquo;. Two
  1411. declarations of an object with hidden linkage refer to the same object
  1412. if they are in the same shared object.
  1413. </p>
  1414. </dd>
  1415. <dt><code>internal</code></dt>
  1416. <dd><p>Internal visibility is like hidden visibility, but with additional
  1417. processor specific semantics. Unless otherwise specified by the
  1418. psABI, GCC defines internal visibility to mean that a function is
  1419. <em>never</em> called from another module. Compare this with hidden
  1420. functions which, while they cannot be referenced directly by other
  1421. modules, can be referenced indirectly via function pointers. By
  1422. indicating that a function cannot be called from outside the module,
  1423. GCC may for instance omit the load of a PIC register since it is known
  1424. that the calling function loaded the correct value.
  1425. </p>
  1426. </dd>
  1427. <dt><code>protected</code></dt>
  1428. <dd><p>Protected visibility is like default visibility except that it
  1429. indicates that references within the defining module bind to the
  1430. definition in that module. That is, the declared entity cannot be
  1431. overridden by another module.
  1432. </p>
  1433. </dd>
  1434. </dl>
  1435. <p>All visibilities are supported on many, but not all, ELF targets
  1436. (supported when the assembler supports the &lsquo;<samp>.visibility</samp>&rsquo;
  1437. pseudo-op). Default visibility is supported everywhere. Hidden
  1438. visibility is supported on Darwin targets.
  1439. </p>
  1440. <p>The visibility attribute should be applied only to declarations that
  1441. would otherwise have external linkage. The attribute should be applied
  1442. consistently, so that the same entity should not be declared with
  1443. different settings of the attribute.
  1444. </p>
  1445. <p>In C++, the visibility attribute applies to types as well as functions
  1446. and objects, because in C++ types have linkage. A class must not have
  1447. greater visibility than its non-static data member types and bases,
  1448. and class members default to the visibility of their class. Also, a
  1449. declaration without explicit visibility is limited to the visibility
  1450. of its type.
  1451. </p>
  1452. <p>In C++, you can mark member functions and static member variables of a
  1453. class with the visibility attribute. This is useful if you know a
  1454. particular method or static member variable should only be used from
  1455. one shared object; then you can mark it hidden while the rest of the
  1456. class has default visibility. Care must be taken to avoid breaking
  1457. the One Definition Rule; for example, it is usually not useful to mark
  1458. an inline method as hidden without marking the whole class as hidden.
  1459. </p>
  1460. <p>A C++ namespace declaration can also have the visibility attribute.
  1461. </p>
  1462. <div class="smallexample">
  1463. <pre class="smallexample">namespace nspace1 __attribute__ ((visibility (&quot;protected&quot;)))
  1464. { /* <span class="roman">Do something.</span> */; }
  1465. </pre></div>
  1466. <p>This attribute applies only to the particular namespace body, not to
  1467. other definitions of the same namespace; it is equivalent to using
  1468. &lsquo;<samp>#pragma GCC visibility</samp>&rsquo; before and after the namespace
  1469. definition (see <a href="Visibility-Pragmas.html#Visibility-Pragmas">Visibility Pragmas</a>).
  1470. </p>
  1471. <p>In C++, if a template argument has limited visibility, this
  1472. restriction is implicitly propagated to the template instantiation.
  1473. Otherwise, template instantiations and specializations default to the
  1474. visibility of their template.
  1475. </p>
  1476. <p>If both the template and enclosing class have explicit visibility, the
  1477. visibility from the template is used.
  1478. </p>
  1479. </dd>
  1480. <dt><code>warn_unused_result</code></dt>
  1481. <dd><a name="index-warn_005funused_005fresult-function-attribute"></a>
  1482. <p>The <code>warn_unused_result</code> attribute causes a warning to be emitted
  1483. if a caller of the function with this attribute does not use its
  1484. return value. This is useful for functions where not checking
  1485. the result is either a security problem or always a bug, such as
  1486. <code>realloc</code>.
  1487. </p>
  1488. <div class="smallexample">
  1489. <pre class="smallexample">int fn () __attribute__ ((warn_unused_result));
  1490. int foo ()
  1491. {
  1492. if (fn () &lt; 0) return -1;
  1493. fn ();
  1494. return 0;
  1495. }
  1496. </pre></div>
  1497. <p>results in warning on line 5.
  1498. </p>
  1499. </dd>
  1500. <dt><code>weak</code></dt>
  1501. <dd><a name="index-weak-function-attribute"></a>
  1502. <p>The <code>weak</code> attribute causes a declaration of an external symbol
  1503. to be emitted as a weak symbol rather than a global. This is primarily
  1504. useful in defining library functions that can be overridden in user code,
  1505. though it can also be used with non-function declarations. The overriding
  1506. symbol must have the same type as the weak symbol. In addition, if it
  1507. designates a variable it must also have the same size and alignment as
  1508. the weak symbol. Weak symbols are supported for ELF targets, and also
  1509. for a.out targets when using the GNU assembler and linker.
  1510. </p>
  1511. </dd>
  1512. <dt><code>weakref</code></dt>
  1513. <dt><code>weakref (&quot;<var>target</var>&quot;)</code></dt>
  1514. <dd><a name="index-weakref-function-attribute"></a>
  1515. <p>The <code>weakref</code> attribute marks a declaration as a weak reference.
  1516. Without arguments, it should be accompanied by an <code>alias</code> attribute
  1517. naming the target symbol. Alternatively, <var>target</var> may be given as
  1518. an argument to <code>weakref</code> itself, naming the target definition of
  1519. the alias. The <var>target</var> must have the same type as the declaration.
  1520. In addition, if it designates a variable it must also have the same size
  1521. and alignment as the declaration. In either form of the declaration
  1522. <code>weakref</code> implicitly marks the declared symbol as <code>weak</code>. Without
  1523. a <var>target</var> given as an argument to <code>weakref</code> or to <code>alias</code>,
  1524. <code>weakref</code> is equivalent to <code>weak</code> (in that case the declaration
  1525. may be <code>extern</code>).
  1526. </p>
  1527. <div class="smallexample">
  1528. <pre class="smallexample">/* Given the declaration: */
  1529. extern int y (void);
  1530. /* the following... */
  1531. static int x (void) __attribute__ ((weakref (&quot;y&quot;)));
  1532. /* is equivalent to... */
  1533. static int x (void) __attribute__ ((weakref, alias (&quot;y&quot;)));
  1534. /* or, alternatively, to... */
  1535. static int x (void) __attribute__ ((weakref));
  1536. static int x (void) __attribute__ ((alias (&quot;y&quot;)));
  1537. </pre></div>
  1538. <p>A weak reference is an alias that does not by itself require a
  1539. definition to be given for the target symbol. If the target symbol is
  1540. only referenced through weak references, then it becomes a <code>weak</code>
  1541. undefined symbol. If it is directly referenced, however, then such
  1542. strong references prevail, and a definition is required for the
  1543. symbol, not necessarily in the same translation unit.
  1544. </p>
  1545. <p>The effect is equivalent to moving all references to the alias to a
  1546. separate translation unit, renaming the alias to the aliased symbol,
  1547. declaring it as weak, compiling the two separate translation units and
  1548. performing a link with relocatable output (i.e. <code>ld -r</code>) on them.
  1549. </p>
  1550. <p>A declaration to which <code>weakref</code> is attached and that is associated
  1551. with a named <code>target</code> must be <code>static</code>.
  1552. </p>
  1553. </dd>
  1554. </dl>
  1555. <hr>
  1556. <div class="header">
  1557. <p>
  1558. Next: <a href="AArch64-Function-Attributes.html#AArch64-Function-Attributes" accesskey="n" rel="next">AArch64 Function Attributes</a>, Up: <a href="Function-Attributes.html#Function-Attributes" accesskey="u" rel="up">Function Attributes</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>
  1559. </div>
  1560. </body>
  1561. </html>