blob: faf97df2166a7a282ee60bc0f32f57247c04d262 [file] [log] [blame]
David Majnemerfc256c32014-03-02 18:17:08 +00001..
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===================
8Attributes in Clang
9===================
10.. contents::
11 :local:
12
13Introduction
14============
15
16This page lists the attributes currently supported by Clang.
17
18Function Attributes
19===================
20
21
22interrupt
23---------
24.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +000025 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +000026
Douglas Gregor1a3ae832015-06-22 17:06:56 +000027 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +000028
29Clang supports the GNU style ``__attribute__((interrupt("TYPE")))`` attribute on
30ARM targets. This attribute may be attached to a function definition and
31instructs the backend to generate appropriate function entry/exit code so that
32it can be used directly as an interrupt service routine.
33
34The parameter passed to the interrupt attribute is optional, but if
35provided it must be a string literal with one of the following values: "IRQ",
36"FIQ", "SWI", "ABORT", "UNDEF".
37
38The semantics are as follows:
39
40- If the function is AAPCS, Clang instructs the backend to realign the stack to
41 8 bytes on entry. This is a general requirement of the AAPCS at public
42 interfaces, but may not hold when an exception is taken. Doing this allows
43 other AAPCS functions to be called.
44- If the CPU is M-class this is all that needs to be done since the architecture
45 itself is designed in such a way that functions obeying the normal AAPCS ABI
46 constraints are valid exception handlers.
47- If the CPU is not M-class, the prologue and epilogue are modified to save all
48 non-banked registers that are used, so that upon return the user-mode state
49 will not be corrupted. Note that to avoid unnecessary overhead, only
50 general-purpose (integer) registers are saved in this way. If VFP operations
51 are needed, that state must be saved manually.
52
53 Specifically, interrupt kinds other than "FIQ" will save all core registers
54 except "lr" and "sp". "FIQ" interrupts will save r0-r7.
55- If the CPU is not M-class, the return instruction is changed to one of the
56 canonical sequences permitted by the architecture for exception return. Where
57 possible the function itself will make the necessary "lr" adjustments so that
58 the "preferred return address" is selected.
59
60 Unfortunately the compiler is unable to make this guarantee for an "UNDEF"
61 handler, where the offset from "lr" to the preferred return address depends on
62 the execution state of the code which generated the exception. In this case
63 a sequence equivalent to "movs pc, lr" will be used.
64
65
66acquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability)
67-----------------------------------------------------------------------------------------------------------
68.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +000069 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +000070
Douglas Gregor1a3ae832015-06-22 17:06:56 +000071 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +000072
73Marks a function as acquiring a capability.
74
75
76assert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability)
77-------------------------------------------------------------------------------------------------------
78.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +000079 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +000080
Douglas Gregor1a3ae832015-06-22 17:06:56 +000081 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +000082
83Marks a function that dynamically tests whether a capability is held, and halts
84the program if it is not held.
85
86
Douglas Gregor1a3ae832015-06-22 17:06:56 +000087assume_aligned (gnu::assume_aligned)
88------------------------------------
89.. csv-table:: Supported Syntaxes
90 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
91
92 "X","X","","", ""
93
94Use ``__attribute__((assume_aligned(<alignment>[,<offset>]))`` on a function
95declaration to specify that the return value of the function (which must be a
96pointer type) has the specified offset, in bytes, from an address with the
97specified alignment. The offset is taken to be zero if omitted.
98
99.. code-block:: c++
100
101 // The returned pointer value has 32-byte alignment.
102 void *a() __attribute__((assume_aligned (32)));
103
104 // The returned pointer value is 4 bytes greater than an address having
105 // 32-byte alignment.
106 void *b() __attribute__((assume_aligned (32, 4)));
107
108Note that this attribute provides information to the compiler regarding a
109condition that the code already ensures is true. It does not cause the compiler
110to enforce the provided alignment assumption.
111
112
David Majnemerfc256c32014-03-02 18:17:08 +0000113availability
114------------
115.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000116 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000117
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000118 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000119
120The ``availability`` attribute can be placed on declarations to describe the
121lifecycle of that declaration relative to operating system versions. Consider
122the function declaration for a hypothetical function ``f``:
123
124.. code-block:: c++
125
126 void f(void) __attribute__((availability(macosx,introduced=10.4,deprecated=10.6,obsoleted=10.7)));
127
128The availability attribute states that ``f`` was introduced in Mac OS X 10.4,
129deprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7. This information
130is used by Clang to determine when it is safe to use ``f``: for example, if
131Clang is instructed to compile code for Mac OS X 10.5, a call to ``f()``
132succeeds. If Clang is instructed to compile code for Mac OS X 10.6, the call
133succeeds but Clang emits a warning specifying that the function is deprecated.
134Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call
135fails because ``f()`` is no longer available.
136
137The availability attribute is a comma-separated list starting with the
138platform name and then including clauses specifying important milestones in the
139declaration's lifetime (in any order) along with additional information. Those
140clauses can be:
141
142introduced=\ *version*
143 The first version in which this declaration was introduced.
144
145deprecated=\ *version*
146 The first version in which this declaration was deprecated, meaning that
147 users should migrate away from this API.
148
149obsoleted=\ *version*
150 The first version in which this declaration was obsoleted, meaning that it
151 was removed completely and can no longer be used.
152
153unavailable
154 This declaration is never available on this platform.
155
156message=\ *string-literal*
157 Additional message text that Clang will provide when emitting a warning or
158 error about use of a deprecated or obsoleted declaration. Useful to direct
159 users to replacement APIs.
160
161Multiple availability attributes can be placed on a declaration, which may
162correspond to different platforms. Only the availability attribute with the
163platform corresponding to the target platform will be used; any others will be
164ignored. If no availability attribute specifies availability for the current
165target platform, the availability attributes are ignored. Supported platforms
166are:
167
168``ios``
169 Apple's iOS operating system. The minimum deployment target is specified by
170 the ``-mios-version-min=*version*`` or ``-miphoneos-version-min=*version*``
171 command-line arguments.
172
173``macosx``
174 Apple's Mac OS X operating system. The minimum deployment target is
175 specified by the ``-mmacosx-version-min=*version*`` command-line argument.
176
177A declaration can be used even when deploying back to a platform version prior
178to 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>`_,
181as if the ``weak_import`` attribute were added to the declaration. A
182weakly-linked declaration may or may not be present a run-time, and a program
183can determine whether the declaration is present by checking whether the
184address of that declaration is non-NULL.
185
186If there are multiple declarations of the same entity, the availability
187attributes must either match on a per-platform basis or later
188declarations must not have availability attributes for that
189platform. For example:
190
191.. code-block:: c
192
193 void g(void) __attribute__((availability(macosx,introduced=10.4)));
194 void g(void) __attribute__((availability(macosx,introduced=10.4))); // okay, matches
195 void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform
196 void g(void); // okay, inherits both macosx and ios availability from above.
197 void g(void) __attribute__((availability(macosx,introduced=10.5))); // error: mismatch
198
199When one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,:
200
201.. code-block:: objc
202
203 @interface A
204 - (id)method __attribute__((availability(macosx,introduced=10.4)));
205 - (id)method2 __attribute__((availability(macosx,introduced=10.4)));
206 @end
207
208 @interface B : A
209 - (id)method __attribute__((availability(macosx,introduced=10.3))); // okay: method moved into base class later
210 - (id)method __attribute__((availability(macosx,introduced=10.5))); // error: this method was available via the base class in 10.4
211 @end
212
213
214_Noreturn
215---------
216.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000217 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000218
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000219 "","","","X", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000220
221A function declared as ``_Noreturn`` shall not return to its caller. The
222compiler will generate a diagnostic for a function declared as ``_Noreturn``
223that appears to be capable of returning to its caller.
224
225
226noreturn
227--------
228.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000229 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000230
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000231 "","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000232
233A function declared as ``[[noreturn]]`` shall not return to its caller. The
234compiler will generate a diagnostic for a function declared as ``[[noreturn]]``
235that appears to be capable of returning to its caller.
236
237
238carries_dependency
239------------------
240.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000241 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000242
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000243 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000244
245The ``carries_dependency`` attribute specifies dependency propagation into and
246out of functions.
247
Aaron Ballman60e466f2014-06-11 19:11:24 +0000248When specified on a function or Objective-C method, the ``carries_dependency``
David Majnemerfc256c32014-03-02 18:17:08 +0000249attribute means that the return value carries a dependency out of the function,
250so that the implementation need not constrain ordering upon return from that
251function. Implementations of the function and its caller may choose to preserve
252dependencies instead of emitting memory ordering instructions such as fences.
253
254Note, this attribute does not change the meaning of the program, but may result
Aaron Ballman60e466f2014-06-11 19:11:24 +0000255in generation of more efficient code.
David Majnemerfc256c32014-03-02 18:17:08 +0000256
257
258enable_if
259---------
260.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000261 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000262
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000263 "X","","","", ""
Nick Lewyckye9a77402014-08-16 02:09:42 +0000264
David Majnemerfc256c32014-03-02 18:17:08 +0000265The ``enable_if`` attribute can be placed on function declarations to control
266which overload is selected based on the values of the function's arguments.
267When combined with the ``overloadable`` attribute, this feature is also
268available in C.
269
270.. code-block:: c++
271
272 int isdigit(int c);
273 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")));
274
275 void foo(char c) {
276 isdigit(c);
277 isdigit(10);
278 isdigit(-10); // results in a compile-time error.
279 }
280
281The enable_if attribute takes two arguments, the first is an expression written
282in terms of the function parameters, the second is a string explaining why this
283overload candidate could not be selected to be displayed in diagnostics. The
284expression is part of the function signature for the purposes of determining
285whether it is a redeclaration (following the rules used when determining
286whether a C++ template specialization is ODR-equivalent), but is not part of
287the type.
288
289The enable_if expression is evaluated as if it were the body of a
290bool-returning constexpr function declared with the arguments of the function
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000291it is being applied to, then called with the parameters at the call site. If the
David Majnemerfc256c32014-03-02 18:17:08 +0000292result is false or could not be determined through constant expression
293evaluation, then this overload will not be chosen and the provided string may
294be used in a diagnostic if the compile fails as a result.
295
296Because the enable_if expression is an unevaluated context, there are no global
297state changes, nor the ability to pass information from the enable_if
298expression to the function body. For example, suppose we want calls to
299strnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of
300strbuf) only if the size of strbuf can be determined:
301
302.. code-block:: c++
303
304 __attribute__((always_inline))
305 static inline size_t strnlen(const char *s, size_t maxlen)
306 __attribute__((overloadable))
307 __attribute__((enable_if(__builtin_object_size(s, 0) != -1))),
308 "chosen when the buffer size is known but 'maxlen' is not")))
309 {
310 return strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
311 }
312
313Multiple enable_if attributes may be applied to a single declaration. In this
314case, the enable_if expressions are evaluated from left to right in the
315following manner. First, the candidates whose enable_if expressions evaluate to
316false or cannot be evaluated are discarded. If the remaining candidates do not
317share ODR-equivalent enable_if expressions, the overload resolution is
318ambiguous. Otherwise, enable_if overload resolution continues with the next
319enable_if attribute on the candidates that have not been discarded and have
320remaining enable_if attributes. In this way, we pick the most specific
321overload out of a number of viable overloads using enable_if.
322
323.. code-block:: c++
324
325 void f() __attribute__((enable_if(true, ""))); // #1
326 void f() __attribute__((enable_if(true, ""))) __attribute__((enable_if(true, ""))); // #2
327
328 void g(int i, int j) __attribute__((enable_if(i, ""))); // #1
329 void g(int i, int j) __attribute__((enable_if(j, ""))) __attribute__((enable_if(true))); // #2
330
331In this example, a call to f() is always resolved to #2, as the first enable_if
332expression is ODR-equivalent for both declarations, but #1 does not have another
333enable_if expression to continue evaluating, so the next round of evaluation has
334only a single candidate. In a call to g(1, 1), the call is ambiguous even though
335#2 has more enable_if attributes, because the first enable_if expressions are
336not ODR-equivalent.
337
338Query for this feature with ``__has_attribute(enable_if)``.
339
340
Aaron Ballman60e466f2014-06-11 19:11:24 +0000341flatten (gnu::flatten)
342----------------------
343.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000344 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
Aaron Ballman60e466f2014-06-11 19:11:24 +0000345
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000346 "X","X","","", ""
Aaron Ballman60e466f2014-06-11 19:11:24 +0000347
348The ``flatten`` attribute causes calls within the attributed function to
349be inlined unless it is impossible to do so, for example if the body of the
350callee is unavailable or if the callee has the ``noinline`` attribute.
351
352
David Majnemerfc256c32014-03-02 18:17:08 +0000353format (gnu::format)
354--------------------
355.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000356 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000357
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000358 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000359
360Clang supports the ``format`` attribute, which indicates that the function
361accepts a ``printf`` or ``scanf``-like format string and corresponding
362arguments or a ``va_list`` that contains these arguments.
363
364Please see `GCC documentation about format attribute
365<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details
366about attribute syntax.
367
368Clang implements two kinds of checks with this attribute.
369
370#. Clang checks that the function with the ``format`` attribute is called with
371 a format string that uses format specifiers that are allowed, and that
372 arguments match the format string. This is the ``-Wformat`` warning, it is
373 on by default.
374
375#. Clang checks that the format string argument is a literal string. This is
376 the ``-Wformat-nonliteral`` warning, it is off by default.
377
378 Clang implements this mostly the same way as GCC, but there is a difference
379 for functions that accept a ``va_list`` argument (for example, ``vprintf``).
380 GCC does not emit ``-Wformat-nonliteral`` warning for calls to such
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000381 functions. Clang does not warn if the format string comes from a function
David Majnemerfc256c32014-03-02 18:17:08 +0000382 parameter, where the function is annotated with a compatible attribute,
383 otherwise it warns. For example:
384
385 .. code-block:: c
386
387 __attribute__((__format__ (__scanf__, 1, 3)))
388 void foo(const char* s, char *buf, ...) {
389 va_list ap;
390 va_start(ap, buf);
391
392 vprintf(s, ap); // warning: format string is not a string literal
393 }
394
395 In this case we warn because ``s`` contains a format string for a
396 ``scanf``-like function, but it is passed to a ``printf``-like function.
397
398 If the attribute is removed, clang still warns, because the format string is
399 not a string literal.
400
401 Another example:
402
403 .. code-block:: c
404
405 __attribute__((__format__ (__printf__, 1, 3)))
406 void foo(const char* s, char *buf, ...) {
407 va_list ap;
408 va_start(ap, buf);
409
410 vprintf(s, ap); // warning
411 }
412
413 In this case Clang does not warn because the format string ``s`` and
414 the corresponding arguments are annotated. If the arguments are
415 incorrect, the caller of ``foo`` will receive a warning.
416
417
418noduplicate (clang::noduplicate)
419--------------------------------
420.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000421 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000422
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000423 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000424
425The ``noduplicate`` attribute can be placed on function declarations to control
426whether function calls to this function can be duplicated or not as a result of
427optimizations. This is required for the implementation of functions with
428certain special requirements, like the OpenCL "barrier" function, that might
429need to be run concurrently by all the threads that are executing in lockstep
430on the hardware. For example this attribute applied on the function
431"nodupfunc" in the code below avoids that:
432
433.. code-block:: c
434
435 void nodupfunc() __attribute__((noduplicate));
436 // Setting it as a C++11 attribute is also valid
437 // void nodupfunc() [[clang::noduplicate]];
438 void foo();
439 void bar();
440
441 nodupfunc();
442 if (a > n) {
443 foo();
444 } else {
445 bar();
446 }
447
448gets possibly modified by some optimizations into code similar to this:
449
450.. code-block:: c
451
452 if (a > n) {
453 nodupfunc();
454 foo();
455 } else {
456 nodupfunc();
457 bar();
458 }
459
460where the call to "nodupfunc" is duplicated and sunk into the two branches
461of the condition.
462
463
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000464no_sanitize (clang::no_sanitize)
465--------------------------------
466.. csv-table:: Supported Syntaxes
467 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
468
469 "X","X","","", ""
470
471Use the ``no_sanitize`` attribute on a function declaration to specify
472that a particular instrumentation or set of instrumentations should not be
473applied to that function. The attribute takes a list of string literals,
474which have the same meaning as values accepted by the ``-fno-sanitize=``
475flag. For example, ``__attribute__((no_sanitize("address", "thread")))``
476specifies that AddressSanitizer and ThreadSanitizer should not be applied
477to the function.
478
479See :ref:`Controlling Code Generation <controlling-code-generation>` for a
480full list of supported sanitizer flags.
481
482
David Majnemerfc256c32014-03-02 18:17:08 +0000483no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address)
484-----------------------------------------------------------------------------------------------------------
485.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000486 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000487
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000488 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000489
490.. _langext-address_sanitizer:
491
492Use ``__attribute__((no_sanitize_address))`` on a function declaration to
493specify that address safety instrumentation (e.g. AddressSanitizer) should
494not be applied to that function.
495
496
David Majnemerfc256c32014-03-02 18:17:08 +0000497no_sanitize_thread
498------------------
499.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000500 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000501
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000502 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000503
504.. _langext-thread_sanitizer:
505
506Use ``__attribute__((no_sanitize_thread))`` on a function declaration to
507specify that checks for data races on plain (non-atomic) memory accesses should
508not be inserted by ThreadSanitizer. The function is still instrumented by the
509tool to avoid false positives and provide meaningful stack traces.
510
511
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000512no_sanitize_memory
513------------------
514.. csv-table:: Supported Syntaxes
515 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
516
517 "X","X","","", ""
518
519.. _langext-memory_sanitizer:
520
521Use ``__attribute__((no_sanitize_memory))`` on a function declaration to
522specify that checks for uninitialized memory should not be inserted
523(e.g. by MemorySanitizer). The function may still be instrumented by the tool
524to avoid false positives in other places.
525
526
Aaron Ballman60e466f2014-06-11 19:11:24 +0000527no_split_stack (gnu::no_split_stack)
528------------------------------------
529.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000530 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
Aaron Ballman60e466f2014-06-11 19:11:24 +0000531
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000532 "X","X","","", ""
Aaron Ballman60e466f2014-06-11 19:11:24 +0000533
534The ``no_split_stack`` attribute disables the emission of the split stack
535preamble for a particular function. It has no effect if ``-fsplit-stack``
536is not specified.
537
538
David Majnemerfc256c32014-03-02 18:17:08 +0000539objc_method_family
540------------------
541.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000542 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000543
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000544 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000545
546Many methods in Objective-C have conventional meanings determined by their
547selectors. It is sometimes useful to be able to mark a method as having a
548particular conventional meaning despite not having the right selector, or as
549not having the conventional meaning that its selector would suggest. For these
550use cases, we provide an attribute to specifically describe the "method family"
551that a method belongs to.
552
553**Usage**: ``__attribute__((objc_method_family(X)))``, where ``X`` is one of
554``none``, ``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``. This
555attribute can only be placed at the end of a method declaration:
556
557.. code-block:: objc
558
559 - (NSString *)initMyStringValue __attribute__((objc_method_family(none)));
560
561Users who do not wish to change the conventional meaning of a method, and who
562merely want to document its non-standard retain and release semantics, should
563use the retaining behavior attributes (``ns_returns_retained``,
564``ns_returns_not_retained``, etc).
565
566Query for this feature with ``__has_attribute(objc_method_family)``.
567
568
569objc_requires_super
570-------------------
571.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000572 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000573
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000574 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000575
576Some Objective-C classes allow a subclass to override a particular method in a
577parent class but expect that the overriding method also calls the overridden
578method in the parent class. For these cases, we provide an attribute to
579designate that a method requires a "call to ``super``" in the overriding
580method in the subclass.
581
582**Usage**: ``__attribute__((objc_requires_super))``. This attribute can only
583be placed at the end of a method declaration:
584
585.. code-block:: objc
586
587 - (void)foo __attribute__((objc_requires_super));
588
589This attribute can only be applied the method declarations within a class, and
590not a protocol. Currently this attribute does not enforce any placement of
591where the call occurs in the overriding method (such as in the case of
592``-dealloc`` where the call must appear at the end). It checks only that it
593exists.
594
595Note that on both OS X and iOS that the Foundation framework provides a
596convenience macro ``NS_REQUIRES_SUPER`` that provides syntactic sugar for this
597attribute:
598
599.. code-block:: objc
600
601 - (void)foo NS_REQUIRES_SUPER;
602
603This macro is conditionally defined depending on the compiler's support for
604this attribute. If the compiler does not support the attribute the macro
605expands to nothing.
606
607Operationally, when a method has this annotation the compiler will warn if the
608implementation of an override in a subclass does not call super. For example:
609
610.. code-block:: objc
611
612 warning: method possibly missing a [super AnnotMeth] call
613 - (void) AnnotMeth{};
614 ^
615
616
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000617objc_runtime_name
618-----------------
619.. csv-table:: Supported Syntaxes
620 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
621
622 "X","","","", ""
623
624By default, the Objective-C interface or protocol identifier is used
625in the metadata name for that object. The `objc_runtime_name`
626attribute allows annotated interfaces or protocols to use the
627specified string argument in the object's metadata name instead of the
628default name.
629
630**Usage**: ``__attribute__((objc_runtime_name("MyLocalName")))``. This attribute
631can only be placed before an @protocol or @interface declaration:
632
633.. code-block:: objc
634
635 __attribute__((objc_runtime_name("MyLocalName")))
636 @interface Message
637 @end
638
639
Aaron Ballman60e466f2014-06-11 19:11:24 +0000640optnone (clang::optnone)
641------------------------
642.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000643 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
Aaron Ballman60e466f2014-06-11 19:11:24 +0000644
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000645 "X","X","","", ""
Aaron Ballman60e466f2014-06-11 19:11:24 +0000646
647The ``optnone`` attribute suppresses essentially all optimizations
648on a function or method, regardless of the optimization level applied to
649the compilation unit as a whole. This is particularly useful when you
650need to debug a particular function, but it is infeasible to build the
651entire application without optimization. Avoiding optimization on the
652specified function can improve the quality of the debugging information
653for that function.
654
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000655This attribute is incompatible with the ``always_inline`` and ``minsize``
656attributes.
Aaron Ballman60e466f2014-06-11 19:11:24 +0000657
658
David Majnemerfc256c32014-03-02 18:17:08 +0000659overloadable
660------------
661.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000662 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000663
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000664 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000665
666Clang provides support for C++ function overloading in C. Function overloading
667in C is introduced using the ``overloadable`` attribute. For example, one
668might provide several overloaded versions of a ``tgsin`` function that invokes
669the appropriate standard function computing the sine of a value with ``float``,
670``double``, or ``long double`` precision:
671
672.. code-block:: c
673
674 #include <math.h>
675 float __attribute__((overloadable)) tgsin(float x) { return sinf(x); }
676 double __attribute__((overloadable)) tgsin(double x) { return sin(x); }
677 long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); }
678
679Given these declarations, one can call ``tgsin`` with a ``float`` value to
680receive a ``float`` result, with a ``double`` to receive a ``double`` result,
681etc. Function overloading in C follows the rules of C++ function overloading
682to pick the best overload given the call arguments, with a few C-specific
683semantics:
684
685* Conversion from ``float`` or ``double`` to ``long double`` is ranked as a
686 floating-point promotion (per C99) rather than as a floating-point conversion
687 (as in C++).
688
689* A conversion from a pointer of type ``T*`` to a pointer of type ``U*`` is
690 considered a pointer conversion (with conversion rank) if ``T`` and ``U`` are
691 compatible types.
692
693* A conversion from type ``T`` to a value of type ``U`` is permitted if ``T``
694 and ``U`` are compatible types. This conversion is given "conversion" rank.
695
696The declaration of ``overloadable`` functions is restricted to function
697declarations and definitions. Most importantly, if any function with a given
698name is given the ``overloadable`` attribute, then all function declarations
699and definitions with that name (and in that scope) must have the
700``overloadable`` attribute. This rule even applies to redeclarations of
701functions whose original declaration had the ``overloadable`` attribute, e.g.,
702
703.. code-block:: c
704
705 int f(int) __attribute__((overloadable));
706 float f(float); // error: declaration of "f" must have the "overloadable" attribute
707
708 int g(int) __attribute__((overloadable));
709 int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute
710
711Functions marked ``overloadable`` must have prototypes. Therefore, the
712following code is ill-formed:
713
714.. code-block:: c
715
716 int h() __attribute__((overloadable)); // error: h does not have a prototype
717
718However, ``overloadable`` functions are allowed to use a ellipsis even if there
719are no named parameters (as is permitted in C++). This feature is particularly
720useful when combined with the ``unavailable`` attribute:
721
722.. code-block:: c++
723
724 void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error
725
726Functions declared with the ``overloadable`` attribute have their names mangled
727according to the same rules as C++ function names. For example, the three
728``tgsin`` functions in our motivating example get the mangled names
729``_Z5tgsinf``, ``_Z5tgsind``, and ``_Z5tgsine``, respectively. There are two
730caveats to this use of name mangling:
731
732* Future versions of Clang may change the name mangling of functions overloaded
733 in C, so you should not depend on an specific mangling. To be completely
734 safe, we strongly urge the use of ``static inline`` with ``overloadable``
735 functions.
736
737* The ``overloadable`` attribute has almost no meaning when used in C++,
738 because names will already be mangled and functions are already overloadable.
739 However, when an ``overloadable`` function occurs within an ``extern "C"``
740 linkage specification, it's name *will* be mangled in the same way as it
741 would in C.
742
743Query for this feature with ``__has_extension(attribute_overloadable)``.
744
745
746release_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability)
747-----------------------------------------------------------------------------------------------------------
748.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000749 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000750
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000751 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000752
753Marks a function as releasing a capability.
754
755
756try_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability)
757---------------------------------------------------------------------------------------------------------------------------
758.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000759 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000760
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000761 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000762
763Marks a function that attempts to acquire a capability. This function may fail to
764actually acquire the capability; they accept a Boolean value determining
765whether acquiring the capability means success (true), or failing to acquire
766the capability means success (false).
767
768
769Variable Attributes
770===================
771
772
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000773init_seg
774--------
775.. csv-table:: Supported Syntaxes
776 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
777
778 "","","","", "X"
779
780The attribute applied by ``pragma init_seg()`` controls the section into
781which global initialization function pointers are emitted. It is only
782available with ``-fms-extensions``. Typically, this function pointer is
783emitted into ``.CRT$XCU`` on Windows. The user can change the order of
784initialization by using a different section name with the same
785``.CRT$XC`` prefix and a suffix that sorts lexicographically before or
786after the standard ``.CRT$XCU`` sections. See the init_seg_
787documentation on MSDN for more information.
788
789.. _init_seg: http://msdn.microsoft.com/en-us/library/7977wcck(v=vs.110).aspx
790
791
Aaron Ballman60e466f2014-06-11 19:11:24 +0000792section (gnu::section, __declspec(allocate))
793--------------------------------------------
794.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000795 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
Aaron Ballman60e466f2014-06-11 19:11:24 +0000796
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000797 "X","X","X","", ""
Aaron Ballman60e466f2014-06-11 19:11:24 +0000798
799The ``section`` attribute allows you to specify a specific section a
800global variable or function should be in after translation.
801
802
David Majnemerfc256c32014-03-02 18:17:08 +0000803tls_model (gnu::tls_model)
804--------------------------
805.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000806 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000807
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000808 "X","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000809
810The ``tls_model`` attribute allows you to specify which thread-local storage
811model to use. It accepts the following strings:
812
813* global-dynamic
814* local-dynamic
815* initial-exec
816* local-exec
817
818TLS models are mutually exclusive.
819
820
Aaron Ballman60e466f2014-06-11 19:11:24 +0000821thread
822------
823.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000824 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
Aaron Ballman60e466f2014-06-11 19:11:24 +0000825
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000826 "","","X","", ""
Aaron Ballman60e466f2014-06-11 19:11:24 +0000827
828The ``__declspec(thread)`` attribute declares a variable with thread local
829storage. It is available under the ``-fms-extensions`` flag for MSVC
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000830compatibility. See the documentation for `__declspec(thread)`_ on MSDN.
Aaron Ballman60e466f2014-06-11 19:11:24 +0000831
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000832.. _`__declspec(thread)`: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx
Aaron Ballman60e466f2014-06-11 19:11:24 +0000833
834In Clang, ``__declspec(thread)`` is generally equivalent in functionality to the
835GNU ``__thread`` keyword. The variable must not have a destructor and must have
836a constant initializer, if any. The attribute only applies to variables
837declared with static storage duration, such as globals, class static data
838members, and static locals.
839
840
David Majnemerfc256c32014-03-02 18:17:08 +0000841Type Attributes
842===============
843
844
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000845align_value
846-----------
847.. csv-table:: Supported Syntaxes
848 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
849
850 "X","","","", ""
851
852The align_value attribute can be added to the typedef of a pointer type or the
853declaration of a variable of pointer or reference type. It specifies that the
854pointer will point to, or the reference will bind to, only objects with at
855least the provided alignment. This alignment value must be some positive power
856of 2.
857
858 .. code-block:: c
859
860 typedef double * aligned_double_ptr __attribute__((align_value(64)));
861 void foo(double & x __attribute__((align_value(128)),
862 aligned_double_ptr y) { ... }
863
864If the pointer value does not have the specified alignment at runtime, the
865behavior of the program is undefined.
866
867
868flag_enum
869---------
870.. csv-table:: Supported Syntaxes
871 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
872
873 "X","","","", ""
874
875This attribute can be added to an enumerator to signal to the compiler that it
876is intended to be used as a flag type. This will cause the compiler to assume
877that the range of the type includes all of the values that you can get by
878manipulating bits of the enumerator when issuing warnings.
879
880
David Majnemerfc256c32014-03-02 18:17:08 +0000881__single_inhertiance, __multiple_inheritance, __virtual_inheritance
882-------------------------------------------------------------------
883.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000884 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000885
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000886 "","","","X", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000887
888This collection of keywords is enabled under ``-fms-extensions`` and controls
889the pointer-to-member representation used on ``*-*-win32`` targets.
890
891The ``*-*-win32`` targets utilize a pointer-to-member representation which
892varies in size and alignment depending on the definition of the underlying
893class.
894
895However, this is problematic when a forward declaration is only available and
896no definition has been made yet. In such cases, Clang is forced to utilize the
897most general representation that is available to it.
898
899These keywords make it possible to use a pointer-to-member representation other
900than the most general one regardless of whether or not the definition will ever
901be present in the current translation unit.
902
903This family of keywords belong between the ``class-key`` and ``class-name``:
904
905.. code-block:: c++
906
907 struct __single_inheritance S;
908 int S::*i;
909 struct S {};
910
911This keyword can be applied to class templates but only has an effect when used
912on full specializations:
913
914.. code-block:: c++
915
916 template <typename T, typename U> struct __single_inheritance A; // warning: inheritance model ignored on primary template
917 template <typename T> struct __multiple_inheritance A<T, T>; // warning: inheritance model ignored on partial specialization
918 template <> struct __single_inheritance A<int, float>;
919
920Note that choosing an inheritance model less general than strictly necessary is
921an error:
922
923.. code-block:: c++
924
925 struct __multiple_inheritance S; // error: inheritance model does not match definition
926 int S::*i;
927 struct S {};
928
929
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000930novtable
931--------
932.. csv-table:: Supported Syntaxes
933 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
934
935 "","","X","", ""
936
937This attribute can be added to a class declaration or definition to signal to
938the compiler that constructors and destructors will not reference the virtual
939function table.
940
941
David Majnemerfc256c32014-03-02 18:17:08 +0000942Statement Attributes
943====================
944
945
946fallthrough (clang::fallthrough)
947--------------------------------
948.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000949 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +0000950
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000951 "","X","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +0000952
953The ``clang::fallthrough`` attribute is used along with the
954``-Wimplicit-fallthrough`` argument to annotate intentional fall-through
955between switch labels. It can only be applied to a null statement placed at a
956point of execution between any statement and the next switch label. It is
957common to mark these places with a specific comment, but this attribute is
958meant to replace comments with a more strict annotation, which can be checked
959by the compiler. This attribute doesn't change semantics of the code and can
960be used wherever an intended fall-through occurs. It is designed to mimic
961control-flow statements like ``break;``, so it can be placed in most places
962where ``break;`` can, but only if there are no statements on the execution path
963between it and the next switch label.
964
965Here is an example:
966
967.. code-block:: c++
968
969 // compile with -Wimplicit-fallthrough
970 switch (n) {
971 case 22:
972 case 33: // no warning: no statements between case labels
973 f();
974 case 44: // warning: unannotated fall-through
975 g();
976 [[clang::fallthrough]];
977 case 55: // no warning
978 if (x) {
979 h();
980 break;
981 }
982 else {
983 i();
984 [[clang::fallthrough]];
985 }
986 case 66: // no warning
987 p();
988 [[clang::fallthrough]]; // warning: fallthrough annotation does not
989 // directly precede case label
990 q();
991 case 77: // warning: unannotated fall-through
992 r();
993 }
994
995
Douglas Gregor1a3ae832015-06-22 17:06:56 +0000996#pragma clang loop
997------------------
998.. csv-table:: Supported Syntaxes
999 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1000
1001 "","","","", "X"
1002
1003The ``#pragma clang loop`` directive allows loop optimization hints to be
1004specified for the subsequent loop. The directive allows vectorization,
1005interleaving, and unrolling to be enabled or disabled. Vector width as well
1006as interleave and unrolling count can be manually specified. See
1007`language extensions
1008<http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_
1009for details.
1010
1011
1012#pragma unroll, #pragma nounroll
1013--------------------------------
1014.. csv-table:: Supported Syntaxes
1015 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1016
1017 "","","","", "X"
1018
1019Loop unrolling optimization hints can be specified with ``#pragma unroll`` and
1020``#pragma nounroll``. The pragma is placed immediately before a for, while,
1021do-while, or c++11 range-based for loop.
1022
1023Specifying ``#pragma unroll`` without a parameter directs the loop unroller to
1024attempt to fully unroll the loop if the trip count is known at compile time:
1025
1026.. code-block:: c++
1027
1028 #pragma unroll
1029 for (...) {
1030 ...
1031 }
1032
1033Specifying the optional parameter, ``#pragma unroll _value_``, directs the
1034unroller to unroll the loop ``_value_`` times. The parameter may optionally be
1035enclosed in parentheses:
1036
1037.. code-block:: c++
1038
1039 #pragma unroll 16
1040 for (...) {
1041 ...
1042 }
1043
1044 #pragma unroll(16)
1045 for (...) {
1046 ...
1047 }
1048
1049Specifying ``#pragma nounroll`` indicates that the loop should not be unrolled:
1050
1051.. code-block:: c++
1052
1053 #pragma nounroll
1054 for (...) {
1055 ...
1056 }
1057
1058``#pragma unroll`` and ``#pragma unroll _value_`` have identical semantics to
1059``#pragma clang loop unroll(full)`` and
1060``#pragma clang loop unroll_count(_value_)`` respectively. ``#pragma nounroll``
1061is equivalent to ``#pragma clang loop unroll(disable)``. See
1062`language extensions
1063<http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_
1064for further details including limitations of the unroll hints.
1065
1066
1067AMD GPU Register Attributes
1068===========================
1069Clang supports attributes for controlling register usage on AMD GPU
1070targets. These attributes may be attached to a kernel function
1071definition and is an optimization hint to the backend for the maximum
1072number of registers to use. This is useful in cases where register
1073limited occupancy is known to be an important factor for the
1074performance for the kernel.
1075
1076The semantics are as follows:
1077
1078- The backend will attempt to limit the number of used registers to
1079 the specified value, but the exact number used is not
1080 guaranteed. The number used may be rounded up to satisfy the
1081 allocation requirements or ABI constraints of the subtarget. For
1082 example, on Southern Islands VGPRs may only be allocated in
1083 increments of 4, so requesting a limit of 39 VGPRs will really
1084 attempt to use up to 40. Requesting more registers than the
1085 subtarget supports will truncate to the maximum allowed. The backend
1086 may also use fewer registers than requested whenever possible.
1087
1088- 0 implies the default no limit on register usage.
1089
1090- Ignored on older VLIW subtargets which did not have separate scalar
1091 and vector registers, R600 through Northern Islands.
1092
1093amdgpu_num_sgpr
1094---------------
1095.. csv-table:: Supported Syntaxes
1096 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1097
1098 "X","","","", ""
1099
1100Clang supports the
1101``__attribute__((amdgpu_num_sgpr(<num_registers>)))`` attribute on AMD
1102Southern Islands GPUs and later for controlling the number of scalar
1103registers. A typical value would be between 8 and 104 in increments of
11048.
1105
1106Due to common instruction constraints, an additional 2-4 SGPRs are
1107typically required for internal use depending on features used. This
1108value is a hint for the total number of SGPRs to use, and not the
1109number of user SGPRs, so no special consideration needs to be given
1110for these.
1111
1112
1113amdgpu_num_vgpr
1114---------------
1115.. csv-table:: Supported Syntaxes
1116 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1117
1118 "X","","","", ""
1119
1120Clang supports the
1121``__attribute__((amdgpu_num_vgpr(<num_registers>)))`` attribute on AMD
1122Southern Islands GPUs and later for controlling the number of vector
1123registers. A typical value would be between 4 and 256 in increments
1124of 4.
1125
1126
1127Calling Conventions
1128===================
1129Clang supports several different calling conventions, depending on the target
1130platform and architecture. The calling convention used for a function determines
1131how parameters are passed, how results are returned to the caller, and other
1132low-level details of calling a function.
1133
1134fastcall (gnu::fastcall, __fastcall, _fastcall)
1135-----------------------------------------------
1136.. csv-table:: Supported Syntaxes
1137 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1138
1139 "X","X","","X", ""
1140
1141On 32-bit x86 targets, this attribute changes the calling convention of a
1142function to use ECX and EDX as register parameters and clear parameters off of
1143the stack on return. This convention does not support variadic calls or
1144unprototyped functions in C, and has no effect on x86_64 targets. This calling
1145convention is supported primarily for compatibility with existing code. Users
1146seeking register parameters should use the ``regparm`` attribute, which does
1147not require callee-cleanup. See the documentation for `__fastcall`_ on MSDN.
1148
1149.. _`__fastcall`: http://msdn.microsoft.com/en-us/library/6xa169sk.aspx
1150
1151
1152ms_abi (gnu::ms_abi)
1153--------------------
1154.. csv-table:: Supported Syntaxes
1155 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1156
1157 "X","X","","", ""
1158
1159On non-Windows x86_64 targets, this attribute changes the calling convention of
1160a function to match the default convention used on Windows x86_64. This
1161attribute has no effect on Windows targets or non-x86_64 targets.
1162
1163
1164pcs (gnu::pcs)
1165--------------
1166.. csv-table:: Supported Syntaxes
1167 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1168
1169 "X","X","","", ""
1170
1171On ARM targets, this attribute can be used to select calling conventions
1172similar to ``stdcall`` on x86. Valid parameter values are "aapcs" and
1173"aapcs-vfp".
1174
1175
1176regparm (gnu::regparm)
1177----------------------
1178.. csv-table:: Supported Syntaxes
1179 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1180
1181 "X","X","","", ""
1182
1183On 32-bit x86 targets, the regparm attribute causes the compiler to pass
1184the first three integer parameters in EAX, EDX, and ECX instead of on the
1185stack. This attribute has no effect on variadic functions, and all parameters
1186are passed via the stack as normal.
1187
1188
1189stdcall (gnu::stdcall, __stdcall, _stdcall)
1190-------------------------------------------
1191.. csv-table:: Supported Syntaxes
1192 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1193
1194 "X","X","","X", ""
1195
1196On 32-bit x86 targets, this attribute changes the calling convention of a
1197function to clear parameters off of the stack on return. This convention does
1198not support variadic calls or unprototyped functions in C, and has no effect on
1199x86_64 targets. This calling convention is used widely by the Windows API and
1200COM applications. See the documentation for `__stdcall`_ on MSDN.
1201
1202.. _`__stdcall`: http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx
1203
1204
1205thiscall (gnu::thiscall, __thiscall, _thiscall)
1206-----------------------------------------------
1207.. csv-table:: Supported Syntaxes
1208 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1209
1210 "X","X","","X", ""
1211
1212On 32-bit x86 targets, this attribute changes the calling convention of a
1213function to use ECX for the first parameter (typically the implicit ``this``
1214parameter of C++ methods) and clear parameters off of the stack on return. This
1215convention does not support variadic calls or unprototyped functions in C, and
1216has no effect on x86_64 targets. See the documentation for `__thiscall`_ on
1217MSDN.
1218
1219.. _`__thiscall`: http://msdn.microsoft.com/en-us/library/ek8tkfbw.aspx
1220
1221
1222vectorcall (__vectorcall, _vectorcall)
1223--------------------------------------
1224.. csv-table:: Supported Syntaxes
1225 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1226
1227 "X","","","X", ""
1228
1229On 32-bit x86 *and* x86_64 targets, this attribute changes the calling
1230convention of a function to pass vector parameters in SSE registers.
1231
1232On 32-bit x86 targets, this calling convention is similar to ``__fastcall``.
1233The first two integer parameters are passed in ECX and EDX. Subsequent integer
1234parameters are passed in memory, and callee clears the stack. On x86_64
1235targets, the callee does *not* clear the stack, and integer parameters are
1236passed in RCX, RDX, R8, and R9 as is done for the default Windows x64 calling
1237convention.
1238
1239On both 32-bit x86 and x86_64 targets, vector and floating point arguments are
1240passed in XMM0-XMM5. Homogenous vector aggregates of up to four elements are
1241passed in sequential SSE registers if enough are available. If AVX is enabled,
1242256 bit vectors are passed in YMM0-YMM5. Any vector or aggregate type that
1243cannot be passed in registers for any reason is passed by reference, which
1244allows the caller to align the parameter memory.
1245
1246See the documentation for `__vectorcall`_ on MSDN for more details.
1247
1248.. _`__vectorcall`: http://msdn.microsoft.com/en-us/library/dn375768.aspx
1249
1250
David Majnemerfc256c32014-03-02 18:17:08 +00001251Consumed Annotation Checking
1252============================
1253Clang supports additional attributes for checking basic resource management
1254properties, specifically for unique objects that have a single owning reference.
1255The following attributes are currently supported, although **the implementation
1256for these annotations is currently in development and are subject to change.**
1257
1258callable_when
1259-------------
1260.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001261 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001262
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001263 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001264
1265Use ``__attribute__((callable_when(...)))`` to indicate what states a method
1266may be called in. Valid states are unconsumed, consumed, or unknown. Each
1267argument to this attribute must be a quoted string. E.g.:
1268
1269``__attribute__((callable_when("unconsumed", "unknown")))``
1270
1271
1272consumable
1273----------
1274.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001275 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001276
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001277 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001278
1279Each ``class`` that uses any of the typestate annotations must first be marked
1280using the ``consumable`` attribute. Failure to do so will result in a warning.
1281
1282This attribute accepts a single parameter that must be one of the following:
1283``unknown``, ``consumed``, or ``unconsumed``.
1284
1285
1286param_typestate
1287---------------
1288.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001289 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001290
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001291 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001292
1293This attribute specifies expectations about function parameters. Calls to an
1294function with annotated parameters will issue a warning if the corresponding
1295argument isn't in the expected state. The attribute is also used to set the
1296initial state of the parameter when analyzing the function's body.
1297
1298
1299return_typestate
1300----------------
1301.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001302 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001303
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001304 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001305
1306The ``return_typestate`` attribute can be applied to functions or parameters.
1307When applied to a function the attribute specifies the state of the returned
1308value. The function's body is checked to ensure that it always returns a value
1309in the specified state. On the caller side, values returned by the annotated
1310function are initialized to the given state.
1311
1312When applied to a function parameter it modifies the state of an argument after
1313a call to the function returns. The function's body is checked to ensure that
1314the parameter is in the expected state before returning.
1315
1316
1317set_typestate
1318-------------
1319.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001320 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001321
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001322 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001323
1324Annotate methods that transition an object into a new state with
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001325``__attribute__((set_typestate(new_state)))``. The new state must be
David Majnemerfc256c32014-03-02 18:17:08 +00001326unconsumed, consumed, or unknown.
1327
1328
1329test_typestate
1330--------------
1331.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001332 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001333
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001334 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001335
1336Use ``__attribute__((test_typestate(tested_state)))`` to indicate that a method
1337returns true if the object is in the specified state..
1338
1339
1340Type Safety Checking
1341====================
1342Clang supports additional attributes to enable checking type safety properties
1343that can't be enforced by the C type system. Use cases include:
1344
1345* MPI library implementations, where these attributes enable checking that
1346 the buffer type matches the passed ``MPI_Datatype``;
1347* for HDF5 library there is a similar use case to MPI;
1348* checking types of variadic functions' arguments for functions like
1349 ``fcntl()`` and ``ioctl()``.
1350
1351You can detect support for these attributes with ``__has_attribute()``. For
1352example:
1353
1354.. code-block:: c++
1355
1356 #if defined(__has_attribute)
1357 # if __has_attribute(argument_with_type_tag) && \
1358 __has_attribute(pointer_with_type_tag) && \
1359 __has_attribute(type_tag_for_datatype)
1360 # define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx)))
1361 /* ... other macros ... */
1362 # endif
1363 #endif
1364
1365 #if !defined(ATTR_MPI_PWT)
1366 # define ATTR_MPI_PWT(buffer_idx, type_idx)
1367 #endif
1368
1369 int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */)
1370 ATTR_MPI_PWT(1,3);
1371
1372argument_with_type_tag
1373----------------------
1374.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001375 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001376
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001377 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001378
1379Use ``__attribute__((argument_with_type_tag(arg_kind, arg_idx,
1380type_tag_idx)))`` on a function declaration to specify that the function
1381accepts a type tag that determines the type of some other argument.
1382``arg_kind`` is an identifier that should be used when annotating all
1383applicable type tags.
1384
1385This attribute is primarily useful for checking arguments of variadic functions
1386(``pointer_with_type_tag`` can be used in most non-variadic cases).
1387
1388For example:
1389
1390.. code-block:: c++
1391
1392 int fcntl(int fd, int cmd, ...)
1393 __attribute__(( argument_with_type_tag(fcntl,3,2) ));
1394
1395
1396pointer_with_type_tag
1397---------------------
1398.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001399 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001400
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001401 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001402
1403Use ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))``
1404on a function declaration to specify that the function accepts a type tag that
1405determines the pointee type of some other pointer argument.
1406
1407For example:
1408
1409.. code-block:: c++
1410
1411 int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */)
1412 __attribute__(( pointer_with_type_tag(mpi,1,3) ));
1413
1414
1415type_tag_for_datatype
1416---------------------
1417.. csv-table:: Supported Syntaxes
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001418 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
David Majnemerfc256c32014-03-02 18:17:08 +00001419
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001420 "X","","","", ""
David Majnemerfc256c32014-03-02 18:17:08 +00001421
1422Clang supports annotating type tags of two forms.
1423
1424* **Type tag that is an expression containing a reference to some declared
1425 identifier.** Use ``__attribute__((type_tag_for_datatype(kind, type)))`` on a
1426 declaration with that identifier:
1427
1428 .. code-block:: c++
1429
1430 extern struct mpi_datatype mpi_datatype_int
1431 __attribute__(( type_tag_for_datatype(mpi,int) ));
1432 #define MPI_INT ((MPI_Datatype) &mpi_datatype_int)
1433
1434* **Type tag that is an integral literal.** Introduce a ``static const``
1435 variable with a corresponding initializer value and attach
1436 ``__attribute__((type_tag_for_datatype(kind, type)))`` on that declaration,
1437 for example:
1438
1439 .. code-block:: c++
1440
1441 #define MPI_INT ((MPI_Datatype) 42)
1442 static const MPI_Datatype mpi_datatype_int
1443 __attribute__(( type_tag_for_datatype(mpi,int) )) = 42
1444
1445The attribute also accepts an optional third argument that determines how the
1446expression is compared to the type tag. There are two supported flags:
1447
1448* ``layout_compatible`` will cause types to be compared according to
1449 layout-compatibility rules (C++11 [class.mem] p 17, 18). This is
1450 implemented to support annotating types like ``MPI_DOUBLE_INT``.
1451
1452 For example:
1453
1454 .. code-block:: c++
1455
1456 /* In mpi.h */
1457 struct internal_mpi_double_int { double d; int i; };
1458 extern struct mpi_datatype mpi_datatype_double_int
1459 __attribute__(( type_tag_for_datatype(mpi, struct internal_mpi_double_int, layout_compatible) ));
1460
1461 #define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int)
1462
1463 /* In user code */
1464 struct my_pair { double a; int b; };
1465 struct my_pair *buffer;
1466 MPI_Send(buffer, 1, MPI_DOUBLE_INT /*, ... */); // no warning
1467
1468 struct my_int_pair { int a; int b; }
1469 struct my_int_pair *buffer2;
1470 MPI_Send(buffer2, 1, MPI_DOUBLE_INT /*, ... */); // warning: actual buffer element
1471 // type 'struct my_int_pair'
1472 // doesn't match specified MPI_Datatype
1473
1474* ``must_be_null`` specifies that the expression should be a null pointer
1475 constant, for example:
1476
1477 .. code-block:: c++
1478
1479 /* In mpi.h */
1480 extern struct mpi_datatype mpi_datatype_null
1481 __attribute__(( type_tag_for_datatype(mpi, void, must_be_null) ));
1482
1483 #define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null)
1484
1485 /* In user code */
1486 MPI_Send(buffer, 1, MPI_DATATYPE_NULL /*, ... */); // warning: MPI_DATATYPE_NULL
1487 // was specified but buffer
1488 // is not a null pointer
1489
1490
Douglas Gregor1a3ae832015-06-22 17:06:56 +00001491OpenCL Address Spaces
1492=====================
1493The address space qualifier may be used to specify the region of memory that is
1494used to allocate the object. OpenCL supports the following address spaces:
1495__generic(generic), __global(global), __local(local), __private(private),
1496__constant(constant).
1497
1498 .. code-block:: c
1499
1500 __constant int c = ...;
1501
1502 __generic int* foo(global int* g) {
1503 __local int* l;
1504 private int p;
1505 ...
1506 return l;
1507 }
1508
1509More details can be found in the OpenCL C language Spec v2.0, Section 6.5.
1510
1511__constant(constant)
1512--------------------
1513.. csv-table:: Supported Syntaxes
1514 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1515
1516 "","","","X", ""
1517
1518The constant address space attribute signals that an object is located in
1519a constant (non-modifiable) memory region. It is available to all work items.
1520Any type can be annotated with the constant address space attribute. Objects
1521with the constant address space qualifier can be declared in any scope and must
1522have an initializer.
1523
1524
1525__generic(generic)
1526------------------
1527.. csv-table:: Supported Syntaxes
1528 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1529
1530 "","","","X", ""
1531
1532The generic address space attribute is only available with OpenCL v2.0 and later.
1533It can be used with pointer types. Variables in global and local scope and
1534function parameters in non-kernel functions can have the generic address space
1535type attribute. It is intended to be a placeholder for any other address space
1536except for '__constant' in OpenCL code which can be used with multiple address
1537spaces.
1538
1539
1540__global(global)
1541----------------
1542.. csv-table:: Supported Syntaxes
1543 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1544
1545 "","","","X", ""
1546
1547The global address space attribute specifies that an object is allocated in
1548global memory, which is accessible by all work items. The content stored in this
1549memory area persists between kernel executions. Pointer types to the global
1550address space are allowed as function parameters or local variables. Starting
1551with OpenCL v2.0, the global address space can be used with global (program
1552scope) variables and static local variable as well.
1553
1554
1555__local(local)
1556--------------
1557.. csv-table:: Supported Syntaxes
1558 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1559
1560 "","","","X", ""
1561
1562The local address space specifies that an object is allocated in the local (work
1563group) memory area, which is accessible to all work items in the same work
1564group. The content stored in this memory region is not accessible after
1565the kernel execution ends. In a kernel function scope, any variable can be in
1566the local address space. In other scopes, only pointer types to the local address
1567space are allowed. Local address space variables cannot have an initializer.
1568
1569
1570__private(private)
1571------------------
1572.. csv-table:: Supported Syntaxes
1573 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1574
1575 "","","","X", ""
1576
1577The private address space specifies that an object is allocated in the private
1578(work item) memory. Other work items cannot access the same memory area and its
1579content is destroyed after work item execution ends. Local variables can be
1580declared in the private address space. Function arguments are always in the
1581private address space. Kernel function arguments of a pointer or an array type
1582cannot point to the private address space.
1583
1584
1585Nullability Attributes
1586======================
1587Whether 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``).
1588
1589The 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:
1590
1591 .. code-block:: c
1592
1593 // No meaningful result when 'ptr' is null (here, it happens to be undefined behavior).
1594 int fetch(int * __nonnull ptr) { return *ptr; }
1595
1596 // 'ptr' may be null.
1597 int fetch_or_zero(int * __nullable ptr) {
1598 return ptr ? *ptr : 0;
1599 }
1600
1601 // A nullable pointer to non-null pointers to const characters.
1602 const char *join_strings(const char * __nonnull * __nullable strings, unsigned n);
1603
1604In 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:
1605
1606 .. code-block:: objective-c
1607
1608 @interface NSView : NSResponder
1609 - (nullable NSView *)ancestorSharedWithView:(nonnull NSView *)aView;
1610 @property (assign, nullable) NSView *superview;
1611 @property (readonly, nonnull) NSArray *subviews;
1612 @end
1613
1614nonnull
1615-------
1616.. csv-table:: Supported Syntaxes
1617 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1618
1619 "X","X","","", ""
1620
1621The ``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:
1622
1623 .. code-block:: c
1624
1625 extern void * my_memcpy (void *dest, const void *src, size_t len)
1626 __attribute__((nonnull (1, 2)));
1627
1628Here, the ``nonnull`` attribute indicates that parameters 1 and 2
1629cannot have a null value. Omitting the parenthesized list of parameter indices means that all parameters of pointer type cannot be null:
1630
1631 .. code-block:: c
1632
1633 extern void * my_memcpy (void *dest, const void *src, size_t len)
1634 __attribute__((nonnull));
1635
1636Clang 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:
1637
1638 .. code-block:: c
1639
1640 extern void * my_memcpy (void *dest __attribute__((nonnull)),
1641 const void *src __attribute__((nonnull)), size_t len);
1642
1643Note 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.
1644
1645
1646returns_nonnull
1647---------------
1648.. csv-table:: Supported Syntaxes
1649 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1650
1651 "X","X","","", ""
1652
1653The ``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:
1654
1655 .. code-block:: c
1656
1657 extern void * malloc (size_t size) __attribute__((returns_nonnull));
1658
1659The ``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
1660
1661
1662__nonnull
1663---------
1664.. csv-table:: Supported Syntaxes
1665 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1666
1667 "","","","X", ""
1668
1669The ``__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:
1670
1671 .. code-block:: c
1672
1673 int fetch(int * __nonnull ptr);
1674
1675a 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.
1676
1677
1678__null_unspecified
1679------------------
1680.. csv-table:: Supported Syntaxes
1681 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1682
1683 "","","","X", ""
1684
1685The ``__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.
1686
1687
1688__nullable
1689----------
1690.. csv-table:: Supported Syntaxes
1691 :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
1692
1693 "","","","X", ""
1694
1695The ``__nullable`` nullability qualifier indicates that a value of the ``__nullable`` pointer type can be null. For example, given:
1696
1697 .. code-block:: c
1698
1699 int fetch_or_zero(int * __nullable ptr);
1700
1701a caller of ``fetch_or_zero`` can provide null.
1702
1703