2
0

AttributeReference.rst 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. ..
  2. -------------------------------------------------------------------
  3. NOTE: This file is automatically generated by running clang-tblgen
  4. -gen-attr-docs. Do not edit this file by hand!!
  5. -------------------------------------------------------------------
  6. ===================
  7. Attributes in Clang
  8. ===================
  9. .. contents::
  10. :local:
  11. Introduction
  12. ============
  13. This page lists the attributes currently supported by Clang.
  14. AMD GPU Register Attributes
  15. ===========================
  16. Clang supports attributes for controlling register usage on AMD GPU
  17. targets. These attributes may be attached to a kernel function
  18. definition and is an optimization hint to the backend for the maximum
  19. number of registers to use. This is useful in cases where register
  20. limited occupancy is known to be an important factor for the
  21. performance for the kernel.
  22. The semantics are as follows:
  23. - The backend will attempt to limit the number of used registers to
  24. the specified value, but the exact number used is not
  25. guaranteed. The number used may be rounded up to satisfy the
  26. allocation requirements or ABI constraints of the subtarget. For
  27. example, on Southern Islands VGPRs may only be allocated in
  28. increments of 4, so requesting a limit of 39 VGPRs will really
  29. attempt to use up to 40. Requesting more registers than the
  30. subtarget supports will truncate to the maximum allowed. The backend
  31. may also use fewer registers than requested whenever possible.
  32. - 0 implies the default no limit on register usage.
  33. - Ignored on older VLIW subtargets which did not have separate scalar
  34. and vector registers, R600 through Northern Islands.
  35. amdgpu_num_sgpr
  36. ---------------
  37. .. csv-table:: Supported Syntaxes
  38. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  39. "X","","","", ""
  40. Clang supports the
  41. ``__attribute__((amdgpu_num_sgpr(<num_registers>)))`` attribute on AMD
  42. Southern Islands GPUs and later for controlling the number of scalar
  43. registers. A typical value would be between 8 and 104 in increments of
  44. 8.
  45. Due to common instruction constraints, an additional 2-4 SGPRs are
  46. typically required for internal use depending on features used. This
  47. value is a hint for the total number of SGPRs to use, and not the
  48. number of user SGPRs, so no special consideration needs to be given
  49. for these.
  50. amdgpu_num_vgpr
  51. ---------------
  52. .. csv-table:: Supported Syntaxes
  53. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  54. "X","","","", ""
  55. Clang supports the
  56. ``__attribute__((amdgpu_num_vgpr(<num_registers>)))`` attribute on AMD
  57. Southern Islands GPUs and later for controlling the number of vector
  58. registers. A typical value would be between 4 and 256 in increments
  59. of 4.
  60. Function Attributes
  61. ===================
  62. interrupt
  63. ---------
  64. .. csv-table:: Supported Syntaxes
  65. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  66. "X","","","", ""
  67. Clang supports the GNU style ``__attribute__((interrupt("TYPE")))`` attribute on
  68. ARM targets. This attribute may be attached to a function definition and
  69. instructs the backend to generate appropriate function entry/exit code so that
  70. it can be used directly as an interrupt service routine.
  71. The parameter passed to the interrupt attribute is optional, but if
  72. provided it must be a string literal with one of the following values: "IRQ",
  73. "FIQ", "SWI", "ABORT", "UNDEF".
  74. The semantics are as follows:
  75. - If the function is AAPCS, Clang instructs the backend to realign the stack to
  76. 8 bytes on entry. This is a general requirement of the AAPCS at public
  77. interfaces, but may not hold when an exception is taken. Doing this allows
  78. other AAPCS functions to be called.
  79. - If the CPU is M-class this is all that needs to be done since the architecture
  80. itself is designed in such a way that functions obeying the normal AAPCS ABI
  81. constraints are valid exception handlers.
  82. - If the CPU is not M-class, the prologue and epilogue are modified to save all
  83. non-banked registers that are used, so that upon return the user-mode state
  84. will not be corrupted. Note that to avoid unnecessary overhead, only
  85. general-purpose (integer) registers are saved in this way. If VFP operations
  86. are needed, that state must be saved manually.
  87. Specifically, interrupt kinds other than "FIQ" will save all core registers
  88. except "lr" and "sp". "FIQ" interrupts will save r0-r7.
  89. - If the CPU is not M-class, the return instruction is changed to one of the
  90. canonical sequences permitted by the architecture for exception return. Where
  91. possible the function itself will make the necessary "lr" adjustments so that
  92. the "preferred return address" is selected.
  93. Unfortunately the compiler is unable to make this guarantee for an "UNDEF"
  94. handler, where the offset from "lr" to the preferred return address depends on
  95. the execution state of the code which generated the exception. In this case
  96. a sequence equivalent to "movs pc, lr" will be used.
  97. acquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability)
  98. -----------------------------------------------------------------------------------------------------------
  99. .. csv-table:: Supported Syntaxes
  100. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  101. "X","X","","", ""
  102. Marks a function as acquiring a capability.
  103. assert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability)
  104. -------------------------------------------------------------------------------------------------------
  105. .. csv-table:: Supported Syntaxes
  106. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  107. "X","X","","", ""
  108. Marks a function that dynamically tests whether a capability is held, and halts
  109. the program if it is not held.
  110. assume_aligned (gnu::assume_aligned)
  111. ------------------------------------
  112. .. csv-table:: Supported Syntaxes
  113. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  114. "X","X","","", ""
  115. Use ``__attribute__((assume_aligned(<alignment>[,<offset>]))`` on a function
  116. declaration to specify that the return value of the function (which must be a
  117. pointer type) has the specified offset, in bytes, from an address with the
  118. specified alignment. The offset is taken to be zero if omitted.
  119. .. code-block:: c++
  120. // The returned pointer value has 32-byte alignment.
  121. void *a() __attribute__((assume_aligned (32)));
  122. // The returned pointer value is 4 bytes greater than an address having
  123. // 32-byte alignment.
  124. void *b() __attribute__((assume_aligned (32, 4)));
  125. Note that this attribute provides information to the compiler regarding a
  126. condition that the code already ensures is true. It does not cause the compiler
  127. to enforce the provided alignment assumption.
  128. availability
  129. ------------
  130. .. csv-table:: Supported Syntaxes
  131. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  132. "X","","","", ""
  133. The ``availability`` attribute can be placed on declarations to describe the
  134. lifecycle of that declaration relative to operating system versions. Consider
  135. the function declaration for a hypothetical function ``f``:
  136. .. code-block:: c++
  137. void f(void) __attribute__((availability(macosx,introduced=10.4,deprecated=10.6,obsoleted=10.7)));
  138. The availability attribute states that ``f`` was introduced in Mac OS X 10.4,
  139. deprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7. This information
  140. is used by Clang to determine when it is safe to use ``f``: for example, if
  141. Clang is instructed to compile code for Mac OS X 10.5, a call to ``f()``
  142. succeeds. If Clang is instructed to compile code for Mac OS X 10.6, the call
  143. succeeds but Clang emits a warning specifying that the function is deprecated.
  144. Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call
  145. fails because ``f()`` is no longer available.
  146. The availability attribute is a comma-separated list starting with the
  147. platform name and then including clauses specifying important milestones in the
  148. declaration's lifetime (in any order) along with additional information. Those
  149. clauses can be:
  150. introduced=\ *version*
  151. The first version in which this declaration was introduced.
  152. deprecated=\ *version*
  153. The first version in which this declaration was deprecated, meaning that
  154. users should migrate away from this API.
  155. obsoleted=\ *version*
  156. The first version in which this declaration was obsoleted, meaning that it
  157. was removed completely and can no longer be used.
  158. unavailable
  159. This declaration is never available on this platform.
  160. message=\ *string-literal*
  161. Additional message text that Clang will provide when emitting a warning or
  162. error about use of a deprecated or obsoleted declaration. Useful to direct
  163. users to replacement APIs.
  164. Multiple availability attributes can be placed on a declaration, which may
  165. correspond to different platforms. Only the availability attribute with the
  166. platform corresponding to the target platform will be used; any others will be
  167. ignored. If no availability attribute specifies availability for the current
  168. target platform, the availability attributes are ignored. Supported platforms
  169. are:
  170. ``ios``
  171. Apple's iOS operating system. The minimum deployment target is specified by
  172. the ``-mios-version-min=*version*`` or ``-miphoneos-version-min=*version*``
  173. command-line arguments.
  174. ``macosx``
  175. Apple's Mac OS X operating system. The minimum deployment target is
  176. specified by the ``-mmacosx-version-min=*version*`` command-line argument.
  177. A declaration can be used even when deploying back to a platform version prior
  178. to when the declaration was introduced. When this happens, the declaration is
  179. `weakly linked
  180. <https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html>`_,
  181. as if the ``weak_import`` attribute were added to the declaration. A
  182. weakly-linked declaration may or may not be present a run-time, and a program
  183. can determine whether the declaration is present by checking whether the
  184. address of that declaration is non-NULL.
  185. If there are multiple declarations of the same entity, the availability
  186. attributes must either match on a per-platform basis or later
  187. declarations must not have availability attributes for that
  188. platform. For example:
  189. .. code-block:: c
  190. void g(void) __attribute__((availability(macosx,introduced=10.4)));
  191. void g(void) __attribute__((availability(macosx,introduced=10.4))); // okay, matches
  192. void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform
  193. void g(void); // okay, inherits both macosx and ios availability from above.
  194. void g(void) __attribute__((availability(macosx,introduced=10.5))); // error: mismatch
  195. When one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,:
  196. .. code-block:: objc
  197. @interface A
  198. - (id)method __attribute__((availability(macosx,introduced=10.4)));
  199. - (id)method2 __attribute__((availability(macosx,introduced=10.4)));
  200. @end
  201. @interface B : A
  202. - (id)method __attribute__((availability(macosx,introduced=10.3))); // okay: method moved into base class later
  203. - (id)method __attribute__((availability(macosx,introduced=10.5))); // error: this method was available via the base class in 10.4
  204. @end
  205. _Noreturn
  206. ---------
  207. .. csv-table:: Supported Syntaxes
  208. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  209. "","","","X", ""
  210. A function declared as ``_Noreturn`` shall not return to its caller. The
  211. compiler will generate a diagnostic for a function declared as ``_Noreturn``
  212. that appears to be capable of returning to its caller.
  213. noreturn
  214. --------
  215. .. csv-table:: Supported Syntaxes
  216. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  217. "","X","","", ""
  218. A function declared as ``[[noreturn]]`` shall not return to its caller. The
  219. compiler will generate a diagnostic for a function declared as ``[[noreturn]]``
  220. that appears to be capable of returning to its caller.
  221. carries_dependency
  222. ------------------
  223. .. csv-table:: Supported Syntaxes
  224. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  225. "X","X","","", ""
  226. The ``carries_dependency`` attribute specifies dependency propagation into and
  227. out of functions.
  228. When specified on a function or Objective-C method, the ``carries_dependency``
  229. attribute means that the return value carries a dependency out of the function,
  230. so that the implementation need not constrain ordering upon return from that
  231. function. Implementations of the function and its caller may choose to preserve
  232. dependencies instead of emitting memory ordering instructions such as fences.
  233. Note, this attribute does not change the meaning of the program, but may result
  234. in generation of more efficient code.
  235. enable_if
  236. ---------
  237. .. csv-table:: Supported Syntaxes
  238. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  239. "X","","","", ""
  240. .. Note:: Some features of this attribute are experimental. The meaning of
  241. multiple enable_if attributes on a single declaration is subject to change in
  242. a future version of clang. Also, the ABI is not standardized and the name
  243. mangling may change in future versions. To avoid that, use asm labels.
  244. The ``enable_if`` attribute can be placed on function declarations to control
  245. which overload is selected based on the values of the function's arguments.
  246. When combined with the ``overloadable`` attribute, this feature is also
  247. available in C.
  248. .. code-block:: c++
  249. int isdigit(int c);
  250. int isdigit(int c) __attribute__((enable_if(c <= -1 || c > 255, "chosen when 'c' is out of range"))) __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
  251. void foo(char c) {
  252. isdigit(c);
  253. isdigit(10);
  254. isdigit(-10); // results in a compile-time error.
  255. }
  256. The enable_if attribute takes two arguments, the first is an expression written
  257. in terms of the function parameters, the second is a string explaining why this
  258. overload candidate could not be selected to be displayed in diagnostics. The
  259. expression is part of the function signature for the purposes of determining
  260. whether it is a redeclaration (following the rules used when determining
  261. whether a C++ template specialization is ODR-equivalent), but is not part of
  262. the type.
  263. The enable_if expression is evaluated as if it were the body of a
  264. bool-returning constexpr function declared with the arguments of the function
  265. it is being applied to, then called with the parameters at the call site. If the
  266. result is false or could not be determined through constant expression
  267. evaluation, then this overload will not be chosen and the provided string may
  268. be used in a diagnostic if the compile fails as a result.
  269. Because the enable_if expression is an unevaluated context, there are no global
  270. state changes, nor the ability to pass information from the enable_if
  271. expression to the function body. For example, suppose we want calls to
  272. strnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of
  273. strbuf) only if the size of strbuf can be determined:
  274. .. code-block:: c++
  275. __attribute__((always_inline))
  276. static inline size_t strnlen(const char *s, size_t maxlen)
  277. __attribute__((overloadable))
  278. __attribute__((enable_if(__builtin_object_size(s, 0) != -1))),
  279. "chosen when the buffer size is known but 'maxlen' is not")))
  280. {
  281. return strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
  282. }
  283. Multiple enable_if attributes may be applied to a single declaration. In this
  284. case, the enable_if expressions are evaluated from left to right in the
  285. following manner. First, the candidates whose enable_if expressions evaluate to
  286. false or cannot be evaluated are discarded. If the remaining candidates do not
  287. share ODR-equivalent enable_if expressions, the overload resolution is
  288. ambiguous. Otherwise, enable_if overload resolution continues with the next
  289. enable_if attribute on the candidates that have not been discarded and have
  290. remaining enable_if attributes. In this way, we pick the most specific
  291. overload out of a number of viable overloads using enable_if.
  292. .. code-block:: c++
  293. void f() __attribute__((enable_if(true, ""))); // #1
  294. void f() __attribute__((enable_if(true, ""))) __attribute__((enable_if(true, ""))); // #2
  295. void g(int i, int j) __attribute__((enable_if(i, ""))); // #1
  296. void g(int i, int j) __attribute__((enable_if(j, ""))) __attribute__((enable_if(true))); // #2
  297. In this example, a call to f() is always resolved to #2, as the first enable_if
  298. expression is ODR-equivalent for both declarations, but #1 does not have another
  299. enable_if expression to continue evaluating, so the next round of evaluation has
  300. only a single candidate. In a call to g(1, 1), the call is ambiguous even though
  301. #2 has more enable_if attributes, because the first enable_if expressions are
  302. not ODR-equivalent.
  303. Query for this feature with ``__has_attribute(enable_if)``.
  304. flatten (gnu::flatten)
  305. ----------------------
  306. .. csv-table:: Supported Syntaxes
  307. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  308. "X","X","","", ""
  309. The ``flatten`` attribute causes calls within the attributed function to
  310. be inlined unless it is impossible to do so, for example if the body of the
  311. callee is unavailable or if the callee has the ``noinline`` attribute.
  312. format (gnu::format)
  313. --------------------
  314. .. csv-table:: Supported Syntaxes
  315. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  316. "X","X","","", ""
  317. Clang supports the ``format`` attribute, which indicates that the function
  318. accepts a ``printf`` or ``scanf``-like format string and corresponding
  319. arguments or a ``va_list`` that contains these arguments.
  320. Please see `GCC documentation about format attribute
  321. <http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details
  322. about attribute syntax.
  323. Clang implements two kinds of checks with this attribute.
  324. #. Clang checks that the function with the ``format`` attribute is called with
  325. a format string that uses format specifiers that are allowed, and that
  326. arguments match the format string. This is the ``-Wformat`` warning, it is
  327. on by default.
  328. #. Clang checks that the format string argument is a literal string. This is
  329. the ``-Wformat-nonliteral`` warning, it is off by default.
  330. Clang implements this mostly the same way as GCC, but there is a difference
  331. for functions that accept a ``va_list`` argument (for example, ``vprintf``).
  332. GCC does not emit ``-Wformat-nonliteral`` warning for calls to such
  333. functions. Clang does not warn if the format string comes from a function
  334. parameter, where the function is annotated with a compatible attribute,
  335. otherwise it warns. For example:
  336. .. code-block:: c
  337. __attribute__((__format__ (__scanf__, 1, 3)))
  338. void foo(const char* s, char *buf, ...) {
  339. va_list ap;
  340. va_start(ap, buf);
  341. vprintf(s, ap); // warning: format string is not a string literal
  342. }
  343. In this case we warn because ``s`` contains a format string for a
  344. ``scanf``-like function, but it is passed to a ``printf``-like function.
  345. If the attribute is removed, clang still warns, because the format string is
  346. not a string literal.
  347. Another example:
  348. .. code-block:: c
  349. __attribute__((__format__ (__printf__, 1, 3)))
  350. void foo(const char* s, char *buf, ...) {
  351. va_list ap;
  352. va_start(ap, buf);
  353. vprintf(s, ap); // warning
  354. }
  355. In this case Clang does not warn because the format string ``s`` and
  356. the corresponding arguments are annotated. If the arguments are
  357. incorrect, the caller of ``foo`` will receive a warning.
  358. noduplicate (clang::noduplicate)
  359. --------------------------------
  360. .. csv-table:: Supported Syntaxes
  361. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  362. "X","X","","", ""
  363. The ``noduplicate`` attribute can be placed on function declarations to control
  364. whether function calls to this function can be duplicated or not as a result of
  365. optimizations. This is required for the implementation of functions with
  366. certain special requirements, like the OpenCL "barrier" function, that might
  367. need to be run concurrently by all the threads that are executing in lockstep
  368. on the hardware. For example this attribute applied on the function
  369. "nodupfunc" in the code below avoids that:
  370. .. code-block:: c
  371. void nodupfunc() __attribute__((noduplicate));
  372. // Setting it as a C++11 attribute is also valid
  373. // void nodupfunc() [[clang::noduplicate]];
  374. void foo();
  375. void bar();
  376. nodupfunc();
  377. if (a > n) {
  378. foo();
  379. } else {
  380. bar();
  381. }
  382. gets possibly modified by some optimizations into code similar to this:
  383. .. code-block:: c
  384. if (a > n) {
  385. nodupfunc();
  386. foo();
  387. } else {
  388. nodupfunc();
  389. bar();
  390. }
  391. where the call to "nodupfunc" is duplicated and sunk into the two branches
  392. of the condition.
  393. no_sanitize (clang::no_sanitize)
  394. --------------------------------
  395. .. csv-table:: Supported Syntaxes
  396. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  397. "X","X","","", ""
  398. Use the ``no_sanitize`` attribute on a function declaration to specify
  399. that a particular instrumentation or set of instrumentations should not be
  400. applied to that function. The attribute takes a list of string literals,
  401. which have the same meaning as values accepted by the ``-fno-sanitize=``
  402. flag. For example, ``__attribute__((no_sanitize("address", "thread")))``
  403. specifies that AddressSanitizer and ThreadSanitizer should not be applied
  404. to the function.
  405. See :ref:`Controlling Code Generation <controlling-code-generation>` for a
  406. full list of supported sanitizer flags.
  407. no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address)
  408. -----------------------------------------------------------------------------------------------------------
  409. .. csv-table:: Supported Syntaxes
  410. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  411. "X","X","","", ""
  412. .. _langext-address_sanitizer:
  413. Use ``__attribute__((no_sanitize_address))`` on a function declaration to
  414. specify that address safety instrumentation (e.g. AddressSanitizer) should
  415. not be applied to that function.
  416. no_sanitize_thread
  417. ------------------
  418. .. csv-table:: Supported Syntaxes
  419. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  420. "X","X","","", ""
  421. .. _langext-thread_sanitizer:
  422. Use ``__attribute__((no_sanitize_thread))`` on a function declaration to
  423. specify that checks for data races on plain (non-atomic) memory accesses should
  424. not be inserted by ThreadSanitizer. The function is still instrumented by the
  425. tool to avoid false positives and provide meaningful stack traces.
  426. no_sanitize_memory
  427. ------------------
  428. .. csv-table:: Supported Syntaxes
  429. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  430. "X","X","","", ""
  431. .. _langext-memory_sanitizer:
  432. Use ``__attribute__((no_sanitize_memory))`` on a function declaration to
  433. specify that checks for uninitialized memory should not be inserted
  434. (e.g. by MemorySanitizer). The function may still be instrumented by the tool
  435. to avoid false positives in other places.
  436. no_split_stack (gnu::no_split_stack)
  437. ------------------------------------
  438. .. csv-table:: Supported Syntaxes
  439. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  440. "X","X","","", ""
  441. The ``no_split_stack`` attribute disables the emission of the split stack
  442. preamble for a particular function. It has no effect if ``-fsplit-stack``
  443. is not specified.
  444. objc_boxable
  445. ------------
  446. .. csv-table:: Supported Syntaxes
  447. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  448. "X","","","", ""
  449. Structs and unions marked with the ``objc_boxable`` attribute can be used
  450. with the Objective-C boxed expression syntax, ``@(...)``.
  451. **Usage**: ``__attribute__((objc_boxable))``. This attribute
  452. can only be placed on a declaration of a trivially-copyable struct or union:
  453. .. code-block:: objc
  454. struct __attribute__((objc_boxable)) some_struct {
  455. int i;
  456. };
  457. union __attribute__((objc_boxable)) some_union {
  458. int i;
  459. float f;
  460. };
  461. typedef struct __attribute__((objc_boxable)) _some_struct some_struct;
  462. // ...
  463. some_struct ss;
  464. NSValue *boxed = @(ss);
  465. objc_method_family
  466. ------------------
  467. .. csv-table:: Supported Syntaxes
  468. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  469. "X","","","", ""
  470. Many methods in Objective-C have conventional meanings determined by their
  471. selectors. It is sometimes useful to be able to mark a method as having a
  472. particular conventional meaning despite not having the right selector, or as
  473. not having the conventional meaning that its selector would suggest. For these
  474. use cases, we provide an attribute to specifically describe the "method family"
  475. that a method belongs to.
  476. **Usage**: ``__attribute__((objc_method_family(X)))``, where ``X`` is one of
  477. ``none``, ``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``. This
  478. attribute can only be placed at the end of a method declaration:
  479. .. code-block:: objc
  480. - (NSString *)initMyStringValue __attribute__((objc_method_family(none)));
  481. Users who do not wish to change the conventional meaning of a method, and who
  482. merely want to document its non-standard retain and release semantics, should
  483. use the retaining behavior attributes (``ns_returns_retained``,
  484. ``ns_returns_not_retained``, etc).
  485. Query for this feature with ``__has_attribute(objc_method_family)``.
  486. objc_requires_super
  487. -------------------
  488. .. csv-table:: Supported Syntaxes
  489. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  490. "X","","","", ""
  491. Some Objective-C classes allow a subclass to override a particular method in a
  492. parent class but expect that the overriding method also calls the overridden
  493. method in the parent class. For these cases, we provide an attribute to
  494. designate that a method requires a "call to ``super``" in the overriding
  495. method in the subclass.
  496. **Usage**: ``__attribute__((objc_requires_super))``. This attribute can only
  497. be placed at the end of a method declaration:
  498. .. code-block:: objc
  499. - (void)foo __attribute__((objc_requires_super));
  500. This attribute can only be applied the method declarations within a class, and
  501. not a protocol. Currently this attribute does not enforce any placement of
  502. where the call occurs in the overriding method (such as in the case of
  503. ``-dealloc`` where the call must appear at the end). It checks only that it
  504. exists.
  505. Note that on both OS X and iOS that the Foundation framework provides a
  506. convenience macro ``NS_REQUIRES_SUPER`` that provides syntactic sugar for this
  507. attribute:
  508. .. code-block:: objc
  509. - (void)foo NS_REQUIRES_SUPER;
  510. This macro is conditionally defined depending on the compiler's support for
  511. this attribute. If the compiler does not support the attribute the macro
  512. expands to nothing.
  513. Operationally, when a method has this annotation the compiler will warn if the
  514. implementation of an override in a subclass does not call super. For example:
  515. .. code-block:: objc
  516. warning: method possibly missing a [super AnnotMeth] call
  517. - (void) AnnotMeth{};
  518. ^
  519. objc_runtime_name
  520. -----------------
  521. .. csv-table:: Supported Syntaxes
  522. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  523. "X","","","", ""
  524. By default, the Objective-C interface or protocol identifier is used
  525. in the metadata name for that object. The `objc_runtime_name`
  526. attribute allows annotated interfaces or protocols to use the
  527. specified string argument in the object's metadata name instead of the
  528. default name.
  529. **Usage**: ``__attribute__((objc_runtime_name("MyLocalName")))``. This attribute
  530. can only be placed before an @protocol or @interface declaration:
  531. .. code-block:: objc
  532. __attribute__((objc_runtime_name("MyLocalName")))
  533. @interface Message
  534. @end
  535. optnone (clang::optnone)
  536. ------------------------
  537. .. csv-table:: Supported Syntaxes
  538. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  539. "X","X","","", ""
  540. The ``optnone`` attribute suppresses essentially all optimizations
  541. on a function or method, regardless of the optimization level applied to
  542. the compilation unit as a whole. This is particularly useful when you
  543. need to debug a particular function, but it is infeasible to build the
  544. entire application without optimization. Avoiding optimization on the
  545. specified function can improve the quality of the debugging information
  546. for that function.
  547. This attribute is incompatible with the ``always_inline`` and ``minsize``
  548. attributes.
  549. overloadable
  550. ------------
  551. .. csv-table:: Supported Syntaxes
  552. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  553. "X","","","", ""
  554. Clang provides support for C++ function overloading in C. Function overloading
  555. in C is introduced using the ``overloadable`` attribute. For example, one
  556. might provide several overloaded versions of a ``tgsin`` function that invokes
  557. the appropriate standard function computing the sine of a value with ``float``,
  558. ``double``, or ``long double`` precision:
  559. .. code-block:: c
  560. #include <math.h>
  561. float __attribute__((overloadable)) tgsin(float x) { return sinf(x); }
  562. double __attribute__((overloadable)) tgsin(double x) { return sin(x); }
  563. long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); }
  564. Given these declarations, one can call ``tgsin`` with a ``float`` value to
  565. receive a ``float`` result, with a ``double`` to receive a ``double`` result,
  566. etc. Function overloading in C follows the rules of C++ function overloading
  567. to pick the best overload given the call arguments, with a few C-specific
  568. semantics:
  569. * Conversion from ``float`` or ``double`` to ``long double`` is ranked as a
  570. floating-point promotion (per C99) rather than as a floating-point conversion
  571. (as in C++).
  572. * A conversion from a pointer of type ``T*`` to a pointer of type ``U*`` is
  573. considered a pointer conversion (with conversion rank) if ``T`` and ``U`` are
  574. compatible types.
  575. * A conversion from type ``T`` to a value of type ``U`` is permitted if ``T``
  576. and ``U`` are compatible types. This conversion is given "conversion" rank.
  577. The declaration of ``overloadable`` functions is restricted to function
  578. declarations and definitions. Most importantly, if any function with a given
  579. name is given the ``overloadable`` attribute, then all function declarations
  580. and definitions with that name (and in that scope) must have the
  581. ``overloadable`` attribute. This rule even applies to redeclarations of
  582. functions whose original declaration had the ``overloadable`` attribute, e.g.,
  583. .. code-block:: c
  584. int f(int) __attribute__((overloadable));
  585. float f(float); // error: declaration of "f" must have the "overloadable" attribute
  586. int g(int) __attribute__((overloadable));
  587. int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute
  588. Functions marked ``overloadable`` must have prototypes. Therefore, the
  589. following code is ill-formed:
  590. .. code-block:: c
  591. int h() __attribute__((overloadable)); // error: h does not have a prototype
  592. However, ``overloadable`` functions are allowed to use a ellipsis even if there
  593. are no named parameters (as is permitted in C++). This feature is particularly
  594. useful when combined with the ``unavailable`` attribute:
  595. .. code-block:: c++
  596. void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error
  597. Functions declared with the ``overloadable`` attribute have their names mangled
  598. according to the same rules as C++ function names. For example, the three
  599. ``tgsin`` functions in our motivating example get the mangled names
  600. ``_Z5tgsinf``, ``_Z5tgsind``, and ``_Z5tgsine``, respectively. There are two
  601. caveats to this use of name mangling:
  602. * Future versions of Clang may change the name mangling of functions overloaded
  603. in C, so you should not depend on an specific mangling. To be completely
  604. safe, we strongly urge the use of ``static inline`` with ``overloadable``
  605. functions.
  606. * The ``overloadable`` attribute has almost no meaning when used in C++,
  607. because names will already be mangled and functions are already overloadable.
  608. However, when an ``overloadable`` function occurs within an ``extern "C"``
  609. linkage specification, it's name *will* be mangled in the same way as it
  610. would in C.
  611. Query for this feature with ``__has_extension(attribute_overloadable)``.
  612. release_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability)
  613. -----------------------------------------------------------------------------------------------------------
  614. .. csv-table:: Supported Syntaxes
  615. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  616. "X","X","","", ""
  617. Marks a function as releasing a capability.
  618. target (gnu::target)
  619. --------------------
  620. .. csv-table:: Supported Syntaxes
  621. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  622. "X","X","","", ""
  623. Clang supports the GNU style ``__attribute__((target("OPTIONS")))`` attribute.
  624. This attribute may be attached to a function definition and instructs
  625. the backend to use different code generation options than were passed on the
  626. command line.
  627. The current set of options correspond to the existing "subtarget features" for
  628. the target with or without a "-mno-" in front corresponding to the absence
  629. of the feature, as well as ``arch="CPU"`` which will change the default "CPU"
  630. for the function.
  631. Example "subtarget features" from the x86 backend include: "mmx", "sse", "sse4.2",
  632. "avx", "xop" and largely correspond to the machine specific options handled by
  633. the front end.
  634. try_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability)
  635. ---------------------------------------------------------------------------------------------------------------------------
  636. .. csv-table:: Supported Syntaxes
  637. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  638. "X","X","","", ""
  639. Marks a function that attempts to acquire a capability. This function may fail to
  640. actually acquire the capability; they accept a Boolean value determining
  641. whether acquiring the capability means success (true), or failing to acquire
  642. the capability means success (false).
  643. Variable Attributes
  644. ===================
  645. init_seg
  646. --------
  647. .. csv-table:: Supported Syntaxes
  648. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  649. "","","","", "X"
  650. The attribute applied by ``pragma init_seg()`` controls the section into
  651. which global initialization function pointers are emitted. It is only
  652. available with ``-fms-extensions``. Typically, this function pointer is
  653. emitted into ``.CRT$XCU`` on Windows. The user can change the order of
  654. initialization by using a different section name with the same
  655. ``.CRT$XC`` prefix and a suffix that sorts lexicographically before or
  656. after the standard ``.CRT$XCU`` sections. See the init_seg_
  657. documentation on MSDN for more information.
  658. .. _init_seg: http://msdn.microsoft.com/en-us/library/7977wcck(v=vs.110).aspx
  659. section (gnu::section, __declspec(allocate))
  660. --------------------------------------------
  661. .. csv-table:: Supported Syntaxes
  662. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  663. "X","X","X","", ""
  664. The ``section`` attribute allows you to specify a specific section a
  665. global variable or function should be in after translation.
  666. tls_model (gnu::tls_model)
  667. --------------------------
  668. .. csv-table:: Supported Syntaxes
  669. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  670. "X","X","","", ""
  671. The ``tls_model`` attribute allows you to specify which thread-local storage
  672. model to use. It accepts the following strings:
  673. * global-dynamic
  674. * local-dynamic
  675. * initial-exec
  676. * local-exec
  677. TLS models are mutually exclusive.
  678. thread
  679. ------
  680. .. csv-table:: Supported Syntaxes
  681. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  682. "","","X","", ""
  683. The ``__declspec(thread)`` attribute declares a variable with thread local
  684. storage. It is available under the ``-fms-extensions`` flag for MSVC
  685. compatibility. See the documentation for `__declspec(thread)`_ on MSDN.
  686. .. _`__declspec(thread)`: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx
  687. In Clang, ``__declspec(thread)`` is generally equivalent in functionality to the
  688. GNU ``__thread`` keyword. The variable must not have a destructor and must have
  689. a constant initializer, if any. The attribute only applies to variables
  690. declared with static storage duration, such as globals, class static data
  691. members, and static locals.
  692. Type Attributes
  693. ===============
  694. align_value
  695. -----------
  696. .. csv-table:: Supported Syntaxes
  697. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  698. "X","","","", ""
  699. The align_value attribute can be added to the typedef of a pointer type or the
  700. declaration of a variable of pointer or reference type. It specifies that the
  701. pointer will point to, or the reference will bind to, only objects with at
  702. least the provided alignment. This alignment value must be some positive power
  703. of 2.
  704. .. code-block:: c
  705. typedef double * aligned_double_ptr __attribute__((align_value(64)));
  706. void foo(double & x __attribute__((align_value(128)),
  707. aligned_double_ptr y) { ... }
  708. If the pointer value does not have the specified alignment at runtime, the
  709. behavior of the program is undefined.
  710. flag_enum
  711. ---------
  712. .. csv-table:: Supported Syntaxes
  713. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  714. "X","","","", ""
  715. This attribute can be added to an enumerator to signal to the compiler that it
  716. is intended to be used as a flag type. This will cause the compiler to assume
  717. that the range of the type includes all of the values that you can get by
  718. manipulating bits of the enumerator when issuing warnings.
  719. __single_inhertiance, __multiple_inheritance, __virtual_inheritance
  720. -------------------------------------------------------------------
  721. .. csv-table:: Supported Syntaxes
  722. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  723. "","","","X", ""
  724. This collection of keywords is enabled under ``-fms-extensions`` and controls
  725. the pointer-to-member representation used on ``*-*-win32`` targets.
  726. The ``*-*-win32`` targets utilize a pointer-to-member representation which
  727. varies in size and alignment depending on the definition of the underlying
  728. class.
  729. However, this is problematic when a forward declaration is only available and
  730. no definition has been made yet. In such cases, Clang is forced to utilize the
  731. most general representation that is available to it.
  732. These keywords make it possible to use a pointer-to-member representation other
  733. than the most general one regardless of whether or not the definition will ever
  734. be present in the current translation unit.
  735. This family of keywords belong between the ``class-key`` and ``class-name``:
  736. .. code-block:: c++
  737. struct __single_inheritance S;
  738. int S::*i;
  739. struct S {};
  740. This keyword can be applied to class templates but only has an effect when used
  741. on full specializations:
  742. .. code-block:: c++
  743. template <typename T, typename U> struct __single_inheritance A; // warning: inheritance model ignored on primary template
  744. template <typename T> struct __multiple_inheritance A<T, T>; // warning: inheritance model ignored on partial specialization
  745. template <> struct __single_inheritance A<int, float>;
  746. Note that choosing an inheritance model less general than strictly necessary is
  747. an error:
  748. .. code-block:: c++
  749. struct __multiple_inheritance S; // error: inheritance model does not match definition
  750. int S::*i;
  751. struct S {};
  752. novtable
  753. --------
  754. .. csv-table:: Supported Syntaxes
  755. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  756. "","","X","", ""
  757. This attribute can be added to a class declaration or definition to signal to
  758. the compiler that constructors and destructors will not reference the virtual
  759. function table.
  760. Statement Attributes
  761. ====================
  762. fallthrough (clang::fallthrough)
  763. --------------------------------
  764. .. csv-table:: Supported Syntaxes
  765. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  766. "","X","","", ""
  767. The ``clang::fallthrough`` attribute is used along with the
  768. ``-Wimplicit-fallthrough`` argument to annotate intentional fall-through
  769. between switch labels. It can only be applied to a null statement placed at a
  770. point of execution between any statement and the next switch label. It is
  771. common to mark these places with a specific comment, but this attribute is
  772. meant to replace comments with a more strict annotation, which can be checked
  773. by the compiler. This attribute doesn't change semantics of the code and can
  774. be used wherever an intended fall-through occurs. It is designed to mimic
  775. control-flow statements like ``break;``, so it can be placed in most places
  776. where ``break;`` can, but only if there are no statements on the execution path
  777. between it and the next switch label.
  778. Here is an example:
  779. .. code-block:: c++
  780. // compile with -Wimplicit-fallthrough
  781. switch (n) {
  782. case 22:
  783. case 33: // no warning: no statements between case labels
  784. f();
  785. case 44: // warning: unannotated fall-through
  786. g();
  787. [[clang::fallthrough]];
  788. case 55: // no warning
  789. if (x) {
  790. h();
  791. break;
  792. }
  793. else {
  794. i();
  795. [[clang::fallthrough]];
  796. }
  797. case 66: // no warning
  798. p();
  799. [[clang::fallthrough]]; // warning: fallthrough annotation does not
  800. // directly precede case label
  801. q();
  802. case 77: // warning: unannotated fall-through
  803. r();
  804. }
  805. #pragma clang loop
  806. ------------------
  807. .. csv-table:: Supported Syntaxes
  808. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  809. "","","","", "X"
  810. The ``#pragma clang loop`` directive allows loop optimization hints to be
  811. specified for the subsequent loop. The directive allows vectorization,
  812. interleaving, and unrolling to be enabled or disabled. Vector width as well
  813. as interleave and unrolling count can be manually specified. See
  814. `language extensions
  815. <http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_
  816. for details.
  817. #pragma unroll, #pragma nounroll
  818. --------------------------------
  819. .. csv-table:: Supported Syntaxes
  820. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  821. "","","","", "X"
  822. Loop unrolling optimization hints can be specified with ``#pragma unroll`` and
  823. ``#pragma nounroll``. The pragma is placed immediately before a for, while,
  824. do-while, or c++11 range-based for loop.
  825. Specifying ``#pragma unroll`` without a parameter directs the loop unroller to
  826. attempt to fully unroll the loop if the trip count is known at compile time:
  827. .. code-block:: c++
  828. #pragma unroll
  829. for (...) {
  830. ...
  831. }
  832. Specifying the optional parameter, ``#pragma unroll _value_``, directs the
  833. unroller to unroll the loop ``_value_`` times. The parameter may optionally be
  834. enclosed in parentheses:
  835. .. code-block:: c++
  836. #pragma unroll 16
  837. for (...) {
  838. ...
  839. }
  840. #pragma unroll(16)
  841. for (...) {
  842. ...
  843. }
  844. Specifying ``#pragma nounroll`` indicates that the loop should not be unrolled:
  845. .. code-block:: c++
  846. #pragma nounroll
  847. for (...) {
  848. ...
  849. }
  850. ``#pragma unroll`` and ``#pragma unroll _value_`` have identical semantics to
  851. ``#pragma clang loop unroll(full)`` and
  852. ``#pragma clang loop unroll_count(_value_)`` respectively. ``#pragma nounroll``
  853. is equivalent to ``#pragma clang loop unroll(disable)``. See
  854. `language extensions
  855. <http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_
  856. for further details including limitations of the unroll hints.
  857. Calling Conventions
  858. ===================
  859. Clang supports several different calling conventions, depending on the target
  860. platform and architecture. The calling convention used for a function determines
  861. how parameters are passed, how results are returned to the caller, and other
  862. low-level details of calling a function.
  863. fastcall (gnu::fastcall, __fastcall, _fastcall)
  864. -----------------------------------------------
  865. .. csv-table:: Supported Syntaxes
  866. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  867. "X","X","","X", ""
  868. On 32-bit x86 targets, this attribute changes the calling convention of a
  869. function to use ECX and EDX as register parameters and clear parameters off of
  870. the stack on return. This convention does not support variadic calls or
  871. unprototyped functions in C, and has no effect on x86_64 targets. This calling
  872. convention is supported primarily for compatibility with existing code. Users
  873. seeking register parameters should use the ``regparm`` attribute, which does
  874. not require callee-cleanup. See the documentation for `__fastcall`_ on MSDN.
  875. .. _`__fastcall`: http://msdn.microsoft.com/en-us/library/6xa169sk.aspx
  876. ms_abi (gnu::ms_abi)
  877. --------------------
  878. .. csv-table:: Supported Syntaxes
  879. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  880. "X","X","","", ""
  881. On non-Windows x86_64 targets, this attribute changes the calling convention of
  882. a function to match the default convention used on Windows x86_64. This
  883. attribute has no effect on Windows targets or non-x86_64 targets.
  884. pcs (gnu::pcs)
  885. --------------
  886. .. csv-table:: Supported Syntaxes
  887. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  888. "X","X","","", ""
  889. On ARM targets, this attribute can be used to select calling conventions
  890. similar to ``stdcall`` on x86. Valid parameter values are "aapcs" and
  891. "aapcs-vfp".
  892. regparm (gnu::regparm)
  893. ----------------------
  894. .. csv-table:: Supported Syntaxes
  895. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  896. "X","X","","", ""
  897. On 32-bit x86 targets, the regparm attribute causes the compiler to pass
  898. the first three integer parameters in EAX, EDX, and ECX instead of on the
  899. stack. This attribute has no effect on variadic functions, and all parameters
  900. are passed via the stack as normal.
  901. stdcall (gnu::stdcall, __stdcall, _stdcall)
  902. -------------------------------------------
  903. .. csv-table:: Supported Syntaxes
  904. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  905. "X","X","","X", ""
  906. On 32-bit x86 targets, this attribute changes the calling convention of a
  907. function to clear parameters off of the stack on return. This convention does
  908. not support variadic calls or unprototyped functions in C, and has no effect on
  909. x86_64 targets. This calling convention is used widely by the Windows API and
  910. COM applications. See the documentation for `__stdcall`_ on MSDN.
  911. .. _`__stdcall`: http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx
  912. thiscall (gnu::thiscall, __thiscall, _thiscall)
  913. -----------------------------------------------
  914. .. csv-table:: Supported Syntaxes
  915. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  916. "X","X","","X", ""
  917. On 32-bit x86 targets, this attribute changes the calling convention of a
  918. function to use ECX for the first parameter (typically the implicit ``this``
  919. parameter of C++ methods) and clear parameters off of the stack on return. This
  920. convention does not support variadic calls or unprototyped functions in C, and
  921. has no effect on x86_64 targets. See the documentation for `__thiscall`_ on
  922. MSDN.
  923. .. _`__thiscall`: http://msdn.microsoft.com/en-us/library/ek8tkfbw.aspx
  924. vectorcall (__vectorcall, _vectorcall)
  925. --------------------------------------
  926. .. csv-table:: Supported Syntaxes
  927. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  928. "X","","","X", ""
  929. On 32-bit x86 *and* x86_64 targets, this attribute changes the calling
  930. convention of a function to pass vector parameters in SSE registers.
  931. On 32-bit x86 targets, this calling convention is similar to ``__fastcall``.
  932. The first two integer parameters are passed in ECX and EDX. Subsequent integer
  933. parameters are passed in memory, and callee clears the stack. On x86_64
  934. targets, the callee does *not* clear the stack, and integer parameters are
  935. passed in RCX, RDX, R8, and R9 as is done for the default Windows x64 calling
  936. convention.
  937. On both 32-bit x86 and x86_64 targets, vector and floating point arguments are
  938. passed in XMM0-XMM5. Homogenous vector aggregates of up to four elements are
  939. passed in sequential SSE registers if enough are available. If AVX is enabled,
  940. 256 bit vectors are passed in YMM0-YMM5. Any vector or aggregate type that
  941. cannot be passed in registers for any reason is passed by reference, which
  942. allows the caller to align the parameter memory.
  943. See the documentation for `__vectorcall`_ on MSDN for more details.
  944. .. _`__vectorcall`: http://msdn.microsoft.com/en-us/library/dn375768.aspx
  945. Consumed Annotation Checking
  946. ============================
  947. Clang supports additional attributes for checking basic resource management
  948. properties, specifically for unique objects that have a single owning reference.
  949. The following attributes are currently supported, although **the implementation
  950. for these annotations is currently in development and are subject to change.**
  951. callable_when
  952. -------------
  953. .. csv-table:: Supported Syntaxes
  954. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  955. "X","","","", ""
  956. Use ``__attribute__((callable_when(...)))`` to indicate what states a method
  957. may be called in. Valid states are unconsumed, consumed, or unknown. Each
  958. argument to this attribute must be a quoted string. E.g.:
  959. ``__attribute__((callable_when("unconsumed", "unknown")))``
  960. consumable
  961. ----------
  962. .. csv-table:: Supported Syntaxes
  963. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  964. "X","","","", ""
  965. Each ``class`` that uses any of the typestate annotations must first be marked
  966. using the ``consumable`` attribute. Failure to do so will result in a warning.
  967. This attribute accepts a single parameter that must be one of the following:
  968. ``unknown``, ``consumed``, or ``unconsumed``.
  969. param_typestate
  970. ---------------
  971. .. csv-table:: Supported Syntaxes
  972. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  973. "X","","","", ""
  974. This attribute specifies expectations about function parameters. Calls to an
  975. function with annotated parameters will issue a warning if the corresponding
  976. argument isn't in the expected state. The attribute is also used to set the
  977. initial state of the parameter when analyzing the function's body.
  978. return_typestate
  979. ----------------
  980. .. csv-table:: Supported Syntaxes
  981. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  982. "X","","","", ""
  983. The ``return_typestate`` attribute can be applied to functions or parameters.
  984. When applied to a function the attribute specifies the state of the returned
  985. value. The function's body is checked to ensure that it always returns a value
  986. in the specified state. On the caller side, values returned by the annotated
  987. function are initialized to the given state.
  988. When applied to a function parameter it modifies the state of an argument after
  989. a call to the function returns. The function's body is checked to ensure that
  990. the parameter is in the expected state before returning.
  991. set_typestate
  992. -------------
  993. .. csv-table:: Supported Syntaxes
  994. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  995. "X","","","", ""
  996. Annotate methods that transition an object into a new state with
  997. ``__attribute__((set_typestate(new_state)))``. The new state must be
  998. unconsumed, consumed, or unknown.
  999. test_typestate
  1000. --------------
  1001. .. csv-table:: Supported Syntaxes
  1002. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1003. "X","","","", ""
  1004. Use ``__attribute__((test_typestate(tested_state)))`` to indicate that a method
  1005. returns true if the object is in the specified state..
  1006. Type Safety Checking
  1007. ====================
  1008. Clang supports additional attributes to enable checking type safety properties
  1009. that can't be enforced by the C type system. Use cases include:
  1010. * MPI library implementations, where these attributes enable checking that
  1011. the buffer type matches the passed ``MPI_Datatype``;
  1012. * for HDF5 library there is a similar use case to MPI;
  1013. * checking types of variadic functions' arguments for functions like
  1014. ``fcntl()`` and ``ioctl()``.
  1015. You can detect support for these attributes with ``__has_attribute()``. For
  1016. example:
  1017. .. code-block:: c++
  1018. #if defined(__has_attribute)
  1019. # if __has_attribute(argument_with_type_tag) && \
  1020. __has_attribute(pointer_with_type_tag) && \
  1021. __has_attribute(type_tag_for_datatype)
  1022. # define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx)))
  1023. /* ... other macros ... */
  1024. # endif
  1025. #endif
  1026. #if !defined(ATTR_MPI_PWT)
  1027. # define ATTR_MPI_PWT(buffer_idx, type_idx)
  1028. #endif
  1029. int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */)
  1030. ATTR_MPI_PWT(1,3);
  1031. argument_with_type_tag
  1032. ----------------------
  1033. .. csv-table:: Supported Syntaxes
  1034. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1035. "X","","","", ""
  1036. Use ``__attribute__((argument_with_type_tag(arg_kind, arg_idx,
  1037. type_tag_idx)))`` on a function declaration to specify that the function
  1038. accepts a type tag that determines the type of some other argument.
  1039. ``arg_kind`` is an identifier that should be used when annotating all
  1040. applicable type tags.
  1041. This attribute is primarily useful for checking arguments of variadic functions
  1042. (``pointer_with_type_tag`` can be used in most non-variadic cases).
  1043. For example:
  1044. .. code-block:: c++
  1045. int fcntl(int fd, int cmd, ...)
  1046. __attribute__(( argument_with_type_tag(fcntl,3,2) ));
  1047. pointer_with_type_tag
  1048. ---------------------
  1049. .. csv-table:: Supported Syntaxes
  1050. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1051. "X","","","", ""
  1052. Use ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))``
  1053. on a function declaration to specify that the function accepts a type tag that
  1054. determines the pointee type of some other pointer argument.
  1055. For example:
  1056. .. code-block:: c++
  1057. int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */)
  1058. __attribute__(( pointer_with_type_tag(mpi,1,3) ));
  1059. type_tag_for_datatype
  1060. ---------------------
  1061. .. csv-table:: Supported Syntaxes
  1062. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1063. "X","","","", ""
  1064. Clang supports annotating type tags of two forms.
  1065. * **Type tag that is an expression containing a reference to some declared
  1066. identifier.** Use ``__attribute__((type_tag_for_datatype(kind, type)))`` on a
  1067. declaration with that identifier:
  1068. .. code-block:: c++
  1069. extern struct mpi_datatype mpi_datatype_int
  1070. __attribute__(( type_tag_for_datatype(mpi,int) ));
  1071. #define MPI_INT ((MPI_Datatype) &mpi_datatype_int)
  1072. * **Type tag that is an integral literal.** Introduce a ``static const``
  1073. variable with a corresponding initializer value and attach
  1074. ``__attribute__((type_tag_for_datatype(kind, type)))`` on that declaration,
  1075. for example:
  1076. .. code-block:: c++
  1077. #define MPI_INT ((MPI_Datatype) 42)
  1078. static const MPI_Datatype mpi_datatype_int
  1079. __attribute__(( type_tag_for_datatype(mpi,int) )) = 42
  1080. The attribute also accepts an optional third argument that determines how the
  1081. expression is compared to the type tag. There are two supported flags:
  1082. * ``layout_compatible`` will cause types to be compared according to
  1083. layout-compatibility rules (C++11 [class.mem] p 17, 18). This is
  1084. implemented to support annotating types like ``MPI_DOUBLE_INT``.
  1085. For example:
  1086. .. code-block:: c++
  1087. /* In mpi.h */
  1088. struct internal_mpi_double_int { double d; int i; };
  1089. extern struct mpi_datatype mpi_datatype_double_int
  1090. __attribute__(( type_tag_for_datatype(mpi, struct internal_mpi_double_int, layout_compatible) ));
  1091. #define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int)
  1092. /* In user code */
  1093. struct my_pair { double a; int b; };
  1094. struct my_pair *buffer;
  1095. MPI_Send(buffer, 1, MPI_DOUBLE_INT /*, ... */); // no warning
  1096. struct my_int_pair { int a; int b; }
  1097. struct my_int_pair *buffer2;
  1098. MPI_Send(buffer2, 1, MPI_DOUBLE_INT /*, ... */); // warning: actual buffer element
  1099. // type 'struct my_int_pair'
  1100. // doesn't match specified MPI_Datatype
  1101. * ``must_be_null`` specifies that the expression should be a null pointer
  1102. constant, for example:
  1103. .. code-block:: c++
  1104. /* In mpi.h */
  1105. extern struct mpi_datatype mpi_datatype_null
  1106. __attribute__(( type_tag_for_datatype(mpi, void, must_be_null) ));
  1107. #define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null)
  1108. /* In user code */
  1109. MPI_Send(buffer, 1, MPI_DATATYPE_NULL /*, ... */); // warning: MPI_DATATYPE_NULL
  1110. // was specified but buffer
  1111. // is not a null pointer
  1112. OpenCL Address Spaces
  1113. =====================
  1114. The address space qualifier may be used to specify the region of memory that is
  1115. used to allocate the object. OpenCL supports the following address spaces:
  1116. __generic(generic), __global(global), __local(local), __private(private),
  1117. __constant(constant).
  1118. .. code-block:: c
  1119. __constant int c = ...;
  1120. __generic int* foo(global int* g) {
  1121. __local int* l;
  1122. private int p;
  1123. ...
  1124. return l;
  1125. }
  1126. More details can be found in the OpenCL C language Spec v2.0, Section 6.5.
  1127. __constant(constant)
  1128. --------------------
  1129. .. csv-table:: Supported Syntaxes
  1130. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1131. "","","","X", ""
  1132. The constant address space attribute signals that an object is located in
  1133. a constant (non-modifiable) memory region. It is available to all work items.
  1134. Any type can be annotated with the constant address space attribute. Objects
  1135. with the constant address space qualifier can be declared in any scope and must
  1136. have an initializer.
  1137. __generic(generic)
  1138. ------------------
  1139. .. csv-table:: Supported Syntaxes
  1140. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1141. "","","","X", ""
  1142. The generic address space attribute is only available with OpenCL v2.0 and later.
  1143. It can be used with pointer types. Variables in global and local scope and
  1144. function parameters in non-kernel functions can have the generic address space
  1145. type attribute. It is intended to be a placeholder for any other address space
  1146. except for '__constant' in OpenCL code which can be used with multiple address
  1147. spaces.
  1148. __global(global)
  1149. ----------------
  1150. .. csv-table:: Supported Syntaxes
  1151. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1152. "","","","X", ""
  1153. The global address space attribute specifies that an object is allocated in
  1154. global memory, which is accessible by all work items. The content stored in this
  1155. memory area persists between kernel executions. Pointer types to the global
  1156. address space are allowed as function parameters or local variables. Starting
  1157. with OpenCL v2.0, the global address space can be used with global (program
  1158. scope) variables and static local variable as well.
  1159. __local(local)
  1160. --------------
  1161. .. csv-table:: Supported Syntaxes
  1162. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1163. "","","","X", ""
  1164. The local address space specifies that an object is allocated in the local (work
  1165. group) memory area, which is accessible to all work items in the same work
  1166. group. The content stored in this memory region is not accessible after
  1167. the kernel execution ends. In a kernel function scope, any variable can be in
  1168. the local address space. In other scopes, only pointer types to the local address
  1169. space are allowed. Local address space variables cannot have an initializer.
  1170. __private(private)
  1171. ------------------
  1172. .. csv-table:: Supported Syntaxes
  1173. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1174. "","","","X", ""
  1175. The private address space specifies that an object is allocated in the private
  1176. (work item) memory. Other work items cannot access the same memory area and its
  1177. content is destroyed after work item execution ends. Local variables can be
  1178. declared in the private address space. Function arguments are always in the
  1179. private address space. Kernel function arguments of a pointer or an array type
  1180. cannot point to the private address space.
  1181. Nullability Attributes
  1182. ======================
  1183. Whether a particular pointer may be "null" is an important concern when working with pointers in the C family of languages. The various nullability attributes indicate whether a particular pointer can be null or not, which makes APIs more expressive and can help static analysis tools identify bugs involving null pointers. Clang supports several kinds of nullability attributes: the ``nonnull`` and ``returns_nonnull`` attributes indicate which function or method parameters and result types can never be null, while nullability type qualifiers indicate which pointer types can be null (``_Nullable``) or cannot be null (``_Nonnull``).
  1184. The nullability (type) qualifiers express whether a value of a given pointer type can be null (the ``_Nullable`` qualifier), doesn't have a defined meaning for null (the ``_Nonnull`` qualifier), or for which the purpose of null is unclear (the ``_Null_unspecified`` qualifier). Because nullability qualifiers are expressed within the type system, they are more general than the ``nonnull`` and ``returns_nonnull`` attributes, allowing one to express (for example) a nullable pointer to an array of nonnull pointers. Nullability qualifiers are written to the right of the pointer to which they apply. For example:
  1185. .. code-block:: c
  1186. // No meaningful result when 'ptr' is null (here, it happens to be undefined behavior).
  1187. int fetch(int * _Nonnull ptr) { return *ptr; }
  1188. // 'ptr' may be null.
  1189. int fetch_or_zero(int * _Nullable ptr) {
  1190. return ptr ? *ptr : 0;
  1191. }
  1192. // A nullable pointer to non-null pointers to const characters.
  1193. const char *join_strings(const char * _Nonnull * _Nullable strings, unsigned n);
  1194. In Objective-C, there is an alternate spelling for the nullability qualifiers that can be used in Objective-C methods and properties using context-sensitive, non-underscored keywords. For example:
  1195. .. code-block:: objective-c
  1196. @interface NSView : NSResponder
  1197. - (nullable NSView *)ancestorSharedWithView:(nonnull NSView *)aView;
  1198. @property (assign, nullable) NSView *superview;
  1199. @property (readonly, nonnull) NSArray *subviews;
  1200. @end
  1201. nonnull
  1202. -------
  1203. .. csv-table:: Supported Syntaxes
  1204. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1205. "X","X","","", ""
  1206. The ``nonnull`` attribute indicates that some function parameters must not be null, and can be used in several different ways. It's original usage (`from GCC <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes>`_) is as a function (or Objective-C method) attribute that specifies which parameters of the function are nonnull in a comma-separated list. For example:
  1207. .. code-block:: c
  1208. extern void * my_memcpy (void *dest, const void *src, size_t len)
  1209. __attribute__((nonnull (1, 2)));
  1210. Here, the ``nonnull`` attribute indicates that parameters 1 and 2
  1211. cannot have a null value. Omitting the parenthesized list of parameter indices means that all parameters of pointer type cannot be null:
  1212. .. code-block:: c
  1213. extern void * my_memcpy (void *dest, const void *src, size_t len)
  1214. __attribute__((nonnull));
  1215. Clang also allows the ``nonnull`` attribute to be placed directly on a function (or Objective-C method) parameter, eliminating the need to specify the parameter index ahead of type. For example:
  1216. .. code-block:: c
  1217. extern void * my_memcpy (void *dest __attribute__((nonnull)),
  1218. const void *src __attribute__((nonnull)), size_t len);
  1219. Note that the ``nonnull`` attribute indicates that passing null to a non-null parameter is undefined behavior, which the optimizer may take advantage of to, e.g., remove null checks. The ``_Nonnull`` type qualifier indicates that a pointer cannot be null in a more general manner (because it is part of the type system) and does not imply undefined behavior, making it more widely applicable.
  1220. returns_nonnull
  1221. ---------------
  1222. .. csv-table:: Supported Syntaxes
  1223. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1224. "X","X","","", ""
  1225. The ``returns_nonnull`` attribute indicates that a particular function (or Objective-C method) always returns a non-null pointer. For example, a particular system ``malloc`` might be defined to terminate a process when memory is not available rather than returning a null pointer:
  1226. .. code-block:: c
  1227. extern void * malloc (size_t size) __attribute__((returns_nonnull));
  1228. The ``returns_nonnull`` attribute implies that returning a null pointer is undefined behavior, which the optimizer may take advantage of. The ``_Nonnull`` type qualifier indicates that a pointer cannot be null in a more general manner (because it is part of the type system) and does not imply undefined behavior, making it more widely applicable
  1229. _Nonnull
  1230. --------
  1231. .. csv-table:: Supported Syntaxes
  1232. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1233. "","","","X", ""
  1234. The ``_Nonnull`` nullability qualifier indicates that null is not a meaningful value for a value of the ``_Nonnull`` pointer type. For example, given a declaration such as:
  1235. .. code-block:: c
  1236. int fetch(int * _Nonnull ptr);
  1237. a caller of ``fetch`` should not provide a null value, and the compiler will produce a warning if it sees a literal null value passed to ``fetch``. Note that, unlike the declaration attribute ``nonnull``, the presence of ``_Nonnull`` does not imply that passing null is undefined behavior: ``fetch`` is free to consider null undefined behavior or (perhaps for backward-compatibility reasons) defensively handle null.
  1238. _Null_unspecified
  1239. -----------------
  1240. .. csv-table:: Supported Syntaxes
  1241. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1242. "","","","X", ""
  1243. The ``_Null_unspecified`` nullability qualifier indicates that neither the ``_Nonnull`` nor ``_Nullable`` qualifiers make sense for a particular pointer type. It is used primarily to indicate that the role of null with specific pointers in a nullability-annotated header is unclear, e.g., due to overly-complex implementations or historical factors with a long-lived API.
  1244. _Nullable
  1245. ---------
  1246. .. csv-table:: Supported Syntaxes
  1247. :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
  1248. "","","","X", ""
  1249. The ``_Nullable`` nullability qualifier indicates that a value of the ``_Nullable`` pointer type can be null. For example, given:
  1250. .. code-block:: c
  1251. int fetch_or_zero(int * _Nullable ptr);
  1252. a caller of ``fetch_or_zero`` can provide null.