David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 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 | =================== |
| 8 | Attributes in Clang |
| 9 | =================== |
| 10 | .. contents:: |
| 11 | :local: |
| 12 | |
| 13 | Introduction |
| 14 | ============ |
| 15 | |
| 16 | This page lists the attributes currently supported by Clang. |
| 17 | |
| 18 | Function Attributes |
| 19 | =================== |
| 20 | |
| 21 | |
| 22 | interrupt |
| 23 | --------- |
| 24 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 25 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 27 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 28 | |
| 29 | Clang supports the GNU style ``__attribute__((interrupt("TYPE")))`` attribute on |
| 30 | ARM targets. This attribute may be attached to a function definition and |
| 31 | instructs the backend to generate appropriate function entry/exit code so that |
| 32 | it can be used directly as an interrupt service routine. |
| 33 | |
| 34 | The parameter passed to the interrupt attribute is optional, but if |
| 35 | provided it must be a string literal with one of the following values: "IRQ", |
| 36 | "FIQ", "SWI", "ABORT", "UNDEF". |
| 37 | |
| 38 | The 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 | |
| 66 | acquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability) |
| 67 | ----------------------------------------------------------------------------------------------------------- |
| 68 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 69 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 71 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 72 | |
| 73 | Marks a function as acquiring a capability. |
| 74 | |
| 75 | |
| 76 | assert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability) |
| 77 | ------------------------------------------------------------------------------------------------------- |
| 78 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 79 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 81 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 82 | |
| 83 | Marks a function that dynamically tests whether a capability is held, and halts |
| 84 | the program if it is not held. |
| 85 | |
| 86 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 87 | assume_aligned (gnu::assume_aligned) |
| 88 | ------------------------------------ |
| 89 | .. csv-table:: Supported Syntaxes |
| 90 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 91 | |
| 92 | "X","X","","", "" |
| 93 | |
| 94 | Use ``__attribute__((assume_aligned(<alignment>[,<offset>]))`` on a function |
| 95 | declaration to specify that the return value of the function (which must be a |
| 96 | pointer type) has the specified offset, in bytes, from an address with the |
| 97 | specified 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 | |
| 108 | Note that this attribute provides information to the compiler regarding a |
| 109 | condition that the code already ensures is true. It does not cause the compiler |
| 110 | to enforce the provided alignment assumption. |
| 111 | |
| 112 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 113 | availability |
| 114 | ------------ |
| 115 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 116 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 118 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 119 | |
| 120 | The ``availability`` attribute can be placed on declarations to describe the |
| 121 | lifecycle of that declaration relative to operating system versions. Consider |
| 122 | the 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 | |
| 128 | The availability attribute states that ``f`` was introduced in Mac OS X 10.4, |
| 129 | deprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7. This information |
| 130 | is used by Clang to determine when it is safe to use ``f``: for example, if |
| 131 | Clang is instructed to compile code for Mac OS X 10.5, a call to ``f()`` |
| 132 | succeeds. If Clang is instructed to compile code for Mac OS X 10.6, the call |
| 133 | succeeds but Clang emits a warning specifying that the function is deprecated. |
| 134 | Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call |
| 135 | fails because ``f()`` is no longer available. |
| 136 | |
| 137 | The availability attribute is a comma-separated list starting with the |
| 138 | platform name and then including clauses specifying important milestones in the |
| 139 | declaration's lifetime (in any order) along with additional information. Those |
| 140 | clauses can be: |
| 141 | |
| 142 | introduced=\ *version* |
| 143 | The first version in which this declaration was introduced. |
| 144 | |
| 145 | deprecated=\ *version* |
| 146 | The first version in which this declaration was deprecated, meaning that |
| 147 | users should migrate away from this API. |
| 148 | |
| 149 | obsoleted=\ *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 | |
| 153 | unavailable |
| 154 | This declaration is never available on this platform. |
| 155 | |
| 156 | message=\ *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 | |
| 161 | Multiple availability attributes can be placed on a declaration, which may |
| 162 | correspond to different platforms. Only the availability attribute with the |
| 163 | platform corresponding to the target platform will be used; any others will be |
| 164 | ignored. If no availability attribute specifies availability for the current |
| 165 | target platform, the availability attributes are ignored. Supported platforms |
| 166 | are: |
| 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 | |
| 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 | |
| 186 | If there are multiple declarations of the same entity, the availability |
| 187 | attributes must either match on a per-platform basis or later |
| 188 | declarations must not have availability attributes for that |
| 189 | platform. 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 | |
| 199 | When 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 Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 217 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 218 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 219 | "","","","X", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 220 | |
| 221 | A function declared as ``_Noreturn`` shall not return to its caller. The |
| 222 | compiler will generate a diagnostic for a function declared as ``_Noreturn`` |
| 223 | that appears to be capable of returning to its caller. |
| 224 | |
| 225 | |
| 226 | noreturn |
| 227 | -------- |
| 228 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 229 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 231 | "","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 232 | |
| 233 | A function declared as ``[[noreturn]]`` shall not return to its caller. The |
| 234 | compiler will generate a diagnostic for a function declared as ``[[noreturn]]`` |
| 235 | that appears to be capable of returning to its caller. |
| 236 | |
| 237 | |
| 238 | carries_dependency |
| 239 | ------------------ |
| 240 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 241 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 242 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 243 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 244 | |
| 245 | The ``carries_dependency`` attribute specifies dependency propagation into and |
| 246 | out of functions. |
| 247 | |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 248 | When specified on a function or Objective-C method, the ``carries_dependency`` |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 249 | attribute means that the return value carries a dependency out of the function, |
| 250 | so that the implementation need not constrain ordering upon return from that |
| 251 | function. Implementations of the function and its caller may choose to preserve |
| 252 | dependencies instead of emitting memory ordering instructions such as fences. |
| 253 | |
| 254 | Note, this attribute does not change the meaning of the program, but may result |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 255 | in generation of more efficient code. |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | enable_if |
| 259 | --------- |
| 260 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 261 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 263 | "X","","","", "" |
Nick Lewycky | e9a7740 | 2014-08-16 02:09:42 +0000 | [diff] [blame] | 264 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 265 | The ``enable_if`` attribute can be placed on function declarations to control |
| 266 | which overload is selected based on the values of the function's arguments. |
| 267 | When combined with the ``overloadable`` attribute, this feature is also |
| 268 | available 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 | |
| 281 | The enable_if attribute takes two arguments, the first is an expression written |
| 282 | in terms of the function parameters, the second is a string explaining why this |
| 283 | overload candidate could not be selected to be displayed in diagnostics. The |
| 284 | expression is part of the function signature for the purposes of determining |
| 285 | whether it is a redeclaration (following the rules used when determining |
| 286 | whether a C++ template specialization is ODR-equivalent), but is not part of |
| 287 | the type. |
| 288 | |
| 289 | The enable_if expression is evaluated as if it were the body of a |
| 290 | bool-returning constexpr function declared with the arguments of the function |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 291 | it is being applied to, then called with the parameters at the call site. If the |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 292 | result is false or could not be determined through constant expression |
| 293 | evaluation, then this overload will not be chosen and the provided string may |
| 294 | be used in a diagnostic if the compile fails as a result. |
| 295 | |
| 296 | Because the enable_if expression is an unevaluated context, there are no global |
| 297 | state changes, nor the ability to pass information from the enable_if |
| 298 | expression to the function body. For example, suppose we want calls to |
| 299 | strnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of |
| 300 | strbuf) 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 | |
| 313 | Multiple enable_if attributes may be applied to a single declaration. In this |
| 314 | case, the enable_if expressions are evaluated from left to right in the |
| 315 | following manner. First, the candidates whose enable_if expressions evaluate to |
| 316 | false or cannot be evaluated are discarded. If the remaining candidates do not |
| 317 | share ODR-equivalent enable_if expressions, the overload resolution is |
| 318 | ambiguous. Otherwise, enable_if overload resolution continues with the next |
| 319 | enable_if attribute on the candidates that have not been discarded and have |
| 320 | remaining enable_if attributes. In this way, we pick the most specific |
| 321 | overload 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 | |
| 331 | In this example, a call to f() is always resolved to #2, as the first enable_if |
| 332 | expression is ODR-equivalent for both declarations, but #1 does not have another |
| 333 | enable_if expression to continue evaluating, so the next round of evaluation has |
| 334 | only 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 |
| 336 | not ODR-equivalent. |
| 337 | |
| 338 | Query for this feature with ``__has_attribute(enable_if)``. |
| 339 | |
| 340 | |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 341 | flatten (gnu::flatten) |
| 342 | ---------------------- |
| 343 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 344 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 346 | "X","X","","", "" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 347 | |
| 348 | The ``flatten`` attribute causes calls within the attributed function to |
| 349 | be inlined unless it is impossible to do so, for example if the body of the |
| 350 | callee is unavailable or if the callee has the ``noinline`` attribute. |
| 351 | |
| 352 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 353 | format (gnu::format) |
| 354 | -------------------- |
| 355 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 356 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 358 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 359 | |
| 360 | Clang supports the ``format`` attribute, which indicates that the function |
| 361 | accepts a ``printf`` or ``scanf``-like format string and corresponding |
| 362 | arguments or a ``va_list`` that contains these arguments. |
| 363 | |
| 364 | Please see `GCC documentation about format attribute |
| 365 | <http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details |
| 366 | about attribute syntax. |
| 367 | |
| 368 | Clang 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 Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 381 | functions. Clang does not warn if the format string comes from a function |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 382 | 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 | |
| 418 | noduplicate (clang::noduplicate) |
| 419 | -------------------------------- |
| 420 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 421 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 423 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 424 | |
| 425 | The ``noduplicate`` attribute can be placed on function declarations to control |
| 426 | whether function calls to this function can be duplicated or not as a result of |
| 427 | optimizations. This is required for the implementation of functions with |
| 428 | certain special requirements, like the OpenCL "barrier" function, that might |
| 429 | need to be run concurrently by all the threads that are executing in lockstep |
| 430 | on 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 | |
| 448 | gets 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 | |
| 460 | where the call to "nodupfunc" is duplicated and sunk into the two branches |
| 461 | of the condition. |
| 462 | |
| 463 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 464 | no_sanitize (clang::no_sanitize) |
| 465 | -------------------------------- |
| 466 | .. csv-table:: Supported Syntaxes |
| 467 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 468 | |
| 469 | "X","X","","", "" |
| 470 | |
| 471 | Use the ``no_sanitize`` attribute on a function declaration to specify |
| 472 | that a particular instrumentation or set of instrumentations should not be |
| 473 | applied to that function. The attribute takes a list of string literals, |
| 474 | which have the same meaning as values accepted by the ``-fno-sanitize=`` |
| 475 | flag. For example, ``__attribute__((no_sanitize("address", "thread")))`` |
| 476 | specifies that AddressSanitizer and ThreadSanitizer should not be applied |
| 477 | to the function. |
| 478 | |
| 479 | See :ref:`Controlling Code Generation <controlling-code-generation>` for a |
| 480 | full list of supported sanitizer flags. |
| 481 | |
| 482 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 483 | no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address) |
| 484 | ----------------------------------------------------------------------------------------------------------- |
| 485 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 486 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 488 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 489 | |
| 490 | .. _langext-address_sanitizer: |
| 491 | |
| 492 | Use ``__attribute__((no_sanitize_address))`` on a function declaration to |
| 493 | specify that address safety instrumentation (e.g. AddressSanitizer) should |
| 494 | not be applied to that function. |
| 495 | |
| 496 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 497 | no_sanitize_thread |
| 498 | ------------------ |
| 499 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 500 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 502 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 503 | |
| 504 | .. _langext-thread_sanitizer: |
| 505 | |
| 506 | Use ``__attribute__((no_sanitize_thread))`` on a function declaration to |
| 507 | specify that checks for data races on plain (non-atomic) memory accesses should |
| 508 | not be inserted by ThreadSanitizer. The function is still instrumented by the |
| 509 | tool to avoid false positives and provide meaningful stack traces. |
| 510 | |
| 511 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 512 | no_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 | |
| 521 | Use ``__attribute__((no_sanitize_memory))`` on a function declaration to |
| 522 | specify that checks for uninitialized memory should not be inserted |
| 523 | (e.g. by MemorySanitizer). The function may still be instrumented by the tool |
| 524 | to avoid false positives in other places. |
| 525 | |
| 526 | |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 527 | no_split_stack (gnu::no_split_stack) |
| 528 | ------------------------------------ |
| 529 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 530 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 531 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 532 | "X","X","","", "" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 533 | |
| 534 | The ``no_split_stack`` attribute disables the emission of the split stack |
| 535 | preamble for a particular function. It has no effect if ``-fsplit-stack`` |
| 536 | is not specified. |
| 537 | |
| 538 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 539 | objc_method_family |
| 540 | ------------------ |
| 541 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 542 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 543 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 544 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 545 | |
| 546 | Many methods in Objective-C have conventional meanings determined by their |
| 547 | selectors. It is sometimes useful to be able to mark a method as having a |
| 548 | particular conventional meaning despite not having the right selector, or as |
| 549 | not having the conventional meaning that its selector would suggest. For these |
| 550 | use cases, we provide an attribute to specifically describe the "method family" |
| 551 | that 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 |
| 555 | attribute 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 | |
| 561 | Users who do not wish to change the conventional meaning of a method, and who |
| 562 | merely want to document its non-standard retain and release semantics, should |
| 563 | use the retaining behavior attributes (``ns_returns_retained``, |
| 564 | ``ns_returns_not_retained``, etc). |
| 565 | |
| 566 | Query for this feature with ``__has_attribute(objc_method_family)``. |
| 567 | |
| 568 | |
| 569 | objc_requires_super |
| 570 | ------------------- |
| 571 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 572 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 574 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 575 | |
| 576 | Some Objective-C classes allow a subclass to override a particular method in a |
| 577 | parent class but expect that the overriding method also calls the overridden |
| 578 | method in the parent class. For these cases, we provide an attribute to |
| 579 | designate that a method requires a "call to ``super``" in the overriding |
| 580 | method in the subclass. |
| 581 | |
| 582 | **Usage**: ``__attribute__((objc_requires_super))``. This attribute can only |
| 583 | be placed at the end of a method declaration: |
| 584 | |
| 585 | .. code-block:: objc |
| 586 | |
| 587 | - (void)foo __attribute__((objc_requires_super)); |
| 588 | |
| 589 | This attribute can only be applied the method declarations within a class, and |
| 590 | not a protocol. Currently this attribute does not enforce any placement of |
| 591 | where 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 |
| 593 | exists. |
| 594 | |
| 595 | Note that on both OS X and iOS that the Foundation framework provides a |
| 596 | convenience macro ``NS_REQUIRES_SUPER`` that provides syntactic sugar for this |
| 597 | attribute: |
| 598 | |
| 599 | .. code-block:: objc |
| 600 | |
| 601 | - (void)foo NS_REQUIRES_SUPER; |
| 602 | |
| 603 | This macro is conditionally defined depending on the compiler's support for |
| 604 | this attribute. If the compiler does not support the attribute the macro |
| 605 | expands to nothing. |
| 606 | |
| 607 | Operationally, when a method has this annotation the compiler will warn if the |
| 608 | implementation 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 Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 617 | objc_runtime_name |
| 618 | ----------------- |
| 619 | .. csv-table:: Supported Syntaxes |
| 620 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 621 | |
| 622 | "X","","","", "" |
| 623 | |
| 624 | By default, the Objective-C interface or protocol identifier is used |
| 625 | in the metadata name for that object. The `objc_runtime_name` |
| 626 | attribute allows annotated interfaces or protocols to use the |
| 627 | specified string argument in the object's metadata name instead of the |
| 628 | default name. |
| 629 | |
| 630 | **Usage**: ``__attribute__((objc_runtime_name("MyLocalName")))``. This attribute |
| 631 | can 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 Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 640 | optnone (clang::optnone) |
| 641 | ------------------------ |
| 642 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 643 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 644 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 645 | "X","X","","", "" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 646 | |
| 647 | The ``optnone`` attribute suppresses essentially all optimizations |
| 648 | on a function or method, regardless of the optimization level applied to |
| 649 | the compilation unit as a whole. This is particularly useful when you |
| 650 | need to debug a particular function, but it is infeasible to build the |
| 651 | entire application without optimization. Avoiding optimization on the |
| 652 | specified function can improve the quality of the debugging information |
| 653 | for that function. |
| 654 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 655 | This attribute is incompatible with the ``always_inline`` and ``minsize`` |
| 656 | attributes. |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 657 | |
| 658 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 659 | overloadable |
| 660 | ------------ |
| 661 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 662 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 664 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 665 | |
| 666 | Clang provides support for C++ function overloading in C. Function overloading |
| 667 | in C is introduced using the ``overloadable`` attribute. For example, one |
| 668 | might provide several overloaded versions of a ``tgsin`` function that invokes |
| 669 | the 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 | |
| 679 | Given these declarations, one can call ``tgsin`` with a ``float`` value to |
| 680 | receive a ``float`` result, with a ``double`` to receive a ``double`` result, |
| 681 | etc. Function overloading in C follows the rules of C++ function overloading |
| 682 | to pick the best overload given the call arguments, with a few C-specific |
| 683 | semantics: |
| 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 | |
| 696 | The declaration of ``overloadable`` functions is restricted to function |
| 697 | declarations and definitions. Most importantly, if any function with a given |
| 698 | name is given the ``overloadable`` attribute, then all function declarations |
| 699 | and definitions with that name (and in that scope) must have the |
| 700 | ``overloadable`` attribute. This rule even applies to redeclarations of |
| 701 | functions 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 | |
| 711 | Functions marked ``overloadable`` must have prototypes. Therefore, the |
| 712 | following code is ill-formed: |
| 713 | |
| 714 | .. code-block:: c |
| 715 | |
| 716 | int h() __attribute__((overloadable)); // error: h does not have a prototype |
| 717 | |
| 718 | However, ``overloadable`` functions are allowed to use a ellipsis even if there |
| 719 | are no named parameters (as is permitted in C++). This feature is particularly |
| 720 | useful when combined with the ``unavailable`` attribute: |
| 721 | |
| 722 | .. code-block:: c++ |
| 723 | |
| 724 | void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error |
| 725 | |
| 726 | Functions declared with the ``overloadable`` attribute have their names mangled |
| 727 | according 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 |
| 730 | caveats 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 | |
| 743 | Query for this feature with ``__has_extension(attribute_overloadable)``. |
| 744 | |
| 745 | |
| 746 | release_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability) |
| 747 | ----------------------------------------------------------------------------------------------------------- |
| 748 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 749 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 751 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 752 | |
| 753 | Marks a function as releasing a capability. |
| 754 | |
| 755 | |
| 756 | try_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability) |
| 757 | --------------------------------------------------------------------------------------------------------------------------- |
| 758 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 759 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 760 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 761 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 762 | |
| 763 | Marks a function that attempts to acquire a capability. This function may fail to |
| 764 | actually acquire the capability; they accept a Boolean value determining |
| 765 | whether acquiring the capability means success (true), or failing to acquire |
| 766 | the capability means success (false). |
| 767 | |
| 768 | |
| 769 | Variable Attributes |
| 770 | =================== |
| 771 | |
| 772 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 773 | init_seg |
| 774 | -------- |
| 775 | .. csv-table:: Supported Syntaxes |
| 776 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 777 | |
| 778 | "","","","", "X" |
| 779 | |
| 780 | The attribute applied by ``pragma init_seg()`` controls the section into |
| 781 | which global initialization function pointers are emitted. It is only |
| 782 | available with ``-fms-extensions``. Typically, this function pointer is |
| 783 | emitted into ``.CRT$XCU`` on Windows. The user can change the order of |
| 784 | initialization by using a different section name with the same |
| 785 | ``.CRT$XC`` prefix and a suffix that sorts lexicographically before or |
| 786 | after the standard ``.CRT$XCU`` sections. See the init_seg_ |
| 787 | documentation on MSDN for more information. |
| 788 | |
| 789 | .. _init_seg: http://msdn.microsoft.com/en-us/library/7977wcck(v=vs.110).aspx |
| 790 | |
| 791 | |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 792 | section (gnu::section, __declspec(allocate)) |
| 793 | -------------------------------------------- |
| 794 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 795 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 796 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 797 | "X","X","X","", "" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 798 | |
| 799 | The ``section`` attribute allows you to specify a specific section a |
| 800 | global variable or function should be in after translation. |
| 801 | |
| 802 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 803 | tls_model (gnu::tls_model) |
| 804 | -------------------------- |
| 805 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 806 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 808 | "X","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 809 | |
| 810 | The ``tls_model`` attribute allows you to specify which thread-local storage |
| 811 | model to use. It accepts the following strings: |
| 812 | |
| 813 | * global-dynamic |
| 814 | * local-dynamic |
| 815 | * initial-exec |
| 816 | * local-exec |
| 817 | |
| 818 | TLS models are mutually exclusive. |
| 819 | |
| 820 | |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 821 | thread |
| 822 | ------ |
| 823 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 824 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 826 | "","","X","", "" |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 827 | |
| 828 | The ``__declspec(thread)`` attribute declares a variable with thread local |
| 829 | storage. It is available under the ``-fms-extensions`` flag for MSVC |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 830 | compatibility. See the documentation for `__declspec(thread)`_ on MSDN. |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 832 | .. _`__declspec(thread)`: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx |
Aaron Ballman | 60e466f | 2014-06-11 19:11:24 +0000 | [diff] [blame] | 833 | |
| 834 | In Clang, ``__declspec(thread)`` is generally equivalent in functionality to the |
| 835 | GNU ``__thread`` keyword. The variable must not have a destructor and must have |
| 836 | a constant initializer, if any. The attribute only applies to variables |
| 837 | declared with static storage duration, such as globals, class static data |
| 838 | members, and static locals. |
| 839 | |
| 840 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 841 | Type Attributes |
| 842 | =============== |
| 843 | |
| 844 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 845 | align_value |
| 846 | ----------- |
| 847 | .. csv-table:: Supported Syntaxes |
| 848 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 849 | |
| 850 | "X","","","", "" |
| 851 | |
| 852 | The align_value attribute can be added to the typedef of a pointer type or the |
| 853 | declaration of a variable of pointer or reference type. It specifies that the |
| 854 | pointer will point to, or the reference will bind to, only objects with at |
| 855 | least the provided alignment. This alignment value must be some positive power |
| 856 | of 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 | |
| 864 | If the pointer value does not have the specified alignment at runtime, the |
| 865 | behavior of the program is undefined. |
| 866 | |
| 867 | |
| 868 | flag_enum |
| 869 | --------- |
| 870 | .. csv-table:: Supported Syntaxes |
| 871 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 872 | |
| 873 | "X","","","", "" |
| 874 | |
| 875 | This attribute can be added to an enumerator to signal to the compiler that it |
| 876 | is intended to be used as a flag type. This will cause the compiler to assume |
| 877 | that the range of the type includes all of the values that you can get by |
| 878 | manipulating bits of the enumerator when issuing warnings. |
| 879 | |
| 880 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 881 | __single_inhertiance, __multiple_inheritance, __virtual_inheritance |
| 882 | ------------------------------------------------------------------- |
| 883 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 884 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 886 | "","","","X", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 887 | |
| 888 | This collection of keywords is enabled under ``-fms-extensions`` and controls |
| 889 | the pointer-to-member representation used on ``*-*-win32`` targets. |
| 890 | |
| 891 | The ``*-*-win32`` targets utilize a pointer-to-member representation which |
| 892 | varies in size and alignment depending on the definition of the underlying |
| 893 | class. |
| 894 | |
| 895 | However, this is problematic when a forward declaration is only available and |
| 896 | no definition has been made yet. In such cases, Clang is forced to utilize the |
| 897 | most general representation that is available to it. |
| 898 | |
| 899 | These keywords make it possible to use a pointer-to-member representation other |
| 900 | than the most general one regardless of whether or not the definition will ever |
| 901 | be present in the current translation unit. |
| 902 | |
| 903 | This 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 | |
| 911 | This keyword can be applied to class templates but only has an effect when used |
| 912 | on 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 | |
| 920 | Note that choosing an inheritance model less general than strictly necessary is |
| 921 | an 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 Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 930 | novtable |
| 931 | -------- |
| 932 | .. csv-table:: Supported Syntaxes |
| 933 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 934 | |
| 935 | "","","X","", "" |
| 936 | |
| 937 | This attribute can be added to a class declaration or definition to signal to |
| 938 | the compiler that constructors and destructors will not reference the virtual |
| 939 | function table. |
| 940 | |
| 941 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 942 | Statement Attributes |
| 943 | ==================== |
| 944 | |
| 945 | |
| 946 | fallthrough (clang::fallthrough) |
| 947 | -------------------------------- |
| 948 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 949 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 950 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 951 | "","X","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 952 | |
| 953 | The ``clang::fallthrough`` attribute is used along with the |
| 954 | ``-Wimplicit-fallthrough`` argument to annotate intentional fall-through |
| 955 | between switch labels. It can only be applied to a null statement placed at a |
| 956 | point of execution between any statement and the next switch label. It is |
| 957 | common to mark these places with a specific comment, but this attribute is |
| 958 | meant to replace comments with a more strict annotation, which can be checked |
| 959 | by the compiler. This attribute doesn't change semantics of the code and can |
| 960 | be used wherever an intended fall-through occurs. It is designed to mimic |
| 961 | control-flow statements like ``break;``, so it can be placed in most places |
| 962 | where ``break;`` can, but only if there are no statements on the execution path |
| 963 | between it and the next switch label. |
| 964 | |
| 965 | Here 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 Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 996 | #pragma clang loop |
| 997 | ------------------ |
| 998 | .. csv-table:: Supported Syntaxes |
| 999 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1000 | |
| 1001 | "","","","", "X" |
| 1002 | |
| 1003 | The ``#pragma clang loop`` directive allows loop optimization hints to be |
| 1004 | specified for the subsequent loop. The directive allows vectorization, |
| 1005 | interleaving, and unrolling to be enabled or disabled. Vector width as well |
| 1006 | as 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>`_ |
| 1009 | for 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 | |
| 1019 | Loop unrolling optimization hints can be specified with ``#pragma unroll`` and |
| 1020 | ``#pragma nounroll``. The pragma is placed immediately before a for, while, |
| 1021 | do-while, or c++11 range-based for loop. |
| 1022 | |
| 1023 | Specifying ``#pragma unroll`` without a parameter directs the loop unroller to |
| 1024 | attempt 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 | |
| 1033 | Specifying the optional parameter, ``#pragma unroll _value_``, directs the |
| 1034 | unroller to unroll the loop ``_value_`` times. The parameter may optionally be |
| 1035 | enclosed 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 | |
| 1049 | Specifying ``#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`` |
| 1061 | is equivalent to ``#pragma clang loop unroll(disable)``. See |
| 1062 | `language extensions |
| 1063 | <http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_ |
| 1064 | for further details including limitations of the unroll hints. |
| 1065 | |
| 1066 | |
| 1067 | AMD GPU Register Attributes |
| 1068 | =========================== |
| 1069 | Clang supports attributes for controlling register usage on AMD GPU |
| 1070 | targets. These attributes may be attached to a kernel function |
| 1071 | definition and is an optimization hint to the backend for the maximum |
| 1072 | number of registers to use. This is useful in cases where register |
| 1073 | limited occupancy is known to be an important factor for the |
| 1074 | performance for the kernel. |
| 1075 | |
| 1076 | The 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 | |
| 1093 | amdgpu_num_sgpr |
| 1094 | --------------- |
| 1095 | .. csv-table:: Supported Syntaxes |
| 1096 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1097 | |
| 1098 | "X","","","", "" |
| 1099 | |
| 1100 | Clang supports the |
| 1101 | ``__attribute__((amdgpu_num_sgpr(<num_registers>)))`` attribute on AMD |
| 1102 | Southern Islands GPUs and later for controlling the number of scalar |
| 1103 | registers. A typical value would be between 8 and 104 in increments of |
| 1104 | 8. |
| 1105 | |
| 1106 | Due to common instruction constraints, an additional 2-4 SGPRs are |
| 1107 | typically required for internal use depending on features used. This |
| 1108 | value is a hint for the total number of SGPRs to use, and not the |
| 1109 | number of user SGPRs, so no special consideration needs to be given |
| 1110 | for these. |
| 1111 | |
| 1112 | |
| 1113 | amdgpu_num_vgpr |
| 1114 | --------------- |
| 1115 | .. csv-table:: Supported Syntaxes |
| 1116 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1117 | |
| 1118 | "X","","","", "" |
| 1119 | |
| 1120 | Clang supports the |
| 1121 | ``__attribute__((amdgpu_num_vgpr(<num_registers>)))`` attribute on AMD |
| 1122 | Southern Islands GPUs and later for controlling the number of vector |
| 1123 | registers. A typical value would be between 4 and 256 in increments |
| 1124 | of 4. |
| 1125 | |
| 1126 | |
| 1127 | Calling Conventions |
| 1128 | =================== |
| 1129 | Clang supports several different calling conventions, depending on the target |
| 1130 | platform and architecture. The calling convention used for a function determines |
| 1131 | how parameters are passed, how results are returned to the caller, and other |
| 1132 | low-level details of calling a function. |
| 1133 | |
| 1134 | fastcall (gnu::fastcall, __fastcall, _fastcall) |
| 1135 | ----------------------------------------------- |
| 1136 | .. csv-table:: Supported Syntaxes |
| 1137 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1138 | |
| 1139 | "X","X","","X", "" |
| 1140 | |
| 1141 | On 32-bit x86 targets, this attribute changes the calling convention of a |
| 1142 | function to use ECX and EDX as register parameters and clear parameters off of |
| 1143 | the stack on return. This convention does not support variadic calls or |
| 1144 | unprototyped functions in C, and has no effect on x86_64 targets. This calling |
| 1145 | convention is supported primarily for compatibility with existing code. Users |
| 1146 | seeking register parameters should use the ``regparm`` attribute, which does |
| 1147 | not require callee-cleanup. See the documentation for `__fastcall`_ on MSDN. |
| 1148 | |
| 1149 | .. _`__fastcall`: http://msdn.microsoft.com/en-us/library/6xa169sk.aspx |
| 1150 | |
| 1151 | |
| 1152 | ms_abi (gnu::ms_abi) |
| 1153 | -------------------- |
| 1154 | .. csv-table:: Supported Syntaxes |
| 1155 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1156 | |
| 1157 | "X","X","","", "" |
| 1158 | |
| 1159 | On non-Windows x86_64 targets, this attribute changes the calling convention of |
| 1160 | a function to match the default convention used on Windows x86_64. This |
| 1161 | attribute has no effect on Windows targets or non-x86_64 targets. |
| 1162 | |
| 1163 | |
| 1164 | pcs (gnu::pcs) |
| 1165 | -------------- |
| 1166 | .. csv-table:: Supported Syntaxes |
| 1167 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1168 | |
| 1169 | "X","X","","", "" |
| 1170 | |
| 1171 | On ARM targets, this attribute can be used to select calling conventions |
| 1172 | similar to ``stdcall`` on x86. Valid parameter values are "aapcs" and |
| 1173 | "aapcs-vfp". |
| 1174 | |
| 1175 | |
| 1176 | regparm (gnu::regparm) |
| 1177 | ---------------------- |
| 1178 | .. csv-table:: Supported Syntaxes |
| 1179 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1180 | |
| 1181 | "X","X","","", "" |
| 1182 | |
| 1183 | On 32-bit x86 targets, the regparm attribute causes the compiler to pass |
| 1184 | the first three integer parameters in EAX, EDX, and ECX instead of on the |
| 1185 | stack. This attribute has no effect on variadic functions, and all parameters |
| 1186 | are passed via the stack as normal. |
| 1187 | |
| 1188 | |
| 1189 | stdcall (gnu::stdcall, __stdcall, _stdcall) |
| 1190 | ------------------------------------------- |
| 1191 | .. csv-table:: Supported Syntaxes |
| 1192 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1193 | |
| 1194 | "X","X","","X", "" |
| 1195 | |
| 1196 | On 32-bit x86 targets, this attribute changes the calling convention of a |
| 1197 | function to clear parameters off of the stack on return. This convention does |
| 1198 | not support variadic calls or unprototyped functions in C, and has no effect on |
| 1199 | x86_64 targets. This calling convention is used widely by the Windows API and |
| 1200 | COM applications. See the documentation for `__stdcall`_ on MSDN. |
| 1201 | |
| 1202 | .. _`__stdcall`: http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx |
| 1203 | |
| 1204 | |
| 1205 | thiscall (gnu::thiscall, __thiscall, _thiscall) |
| 1206 | ----------------------------------------------- |
| 1207 | .. csv-table:: Supported Syntaxes |
| 1208 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1209 | |
| 1210 | "X","X","","X", "" |
| 1211 | |
| 1212 | On 32-bit x86 targets, this attribute changes the calling convention of a |
| 1213 | function to use ECX for the first parameter (typically the implicit ``this`` |
| 1214 | parameter of C++ methods) and clear parameters off of the stack on return. This |
| 1215 | convention does not support variadic calls or unprototyped functions in C, and |
| 1216 | has no effect on x86_64 targets. See the documentation for `__thiscall`_ on |
| 1217 | MSDN. |
| 1218 | |
| 1219 | .. _`__thiscall`: http://msdn.microsoft.com/en-us/library/ek8tkfbw.aspx |
| 1220 | |
| 1221 | |
| 1222 | vectorcall (__vectorcall, _vectorcall) |
| 1223 | -------------------------------------- |
| 1224 | .. csv-table:: Supported Syntaxes |
| 1225 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1226 | |
| 1227 | "X","","","X", "" |
| 1228 | |
| 1229 | On 32-bit x86 *and* x86_64 targets, this attribute changes the calling |
| 1230 | convention of a function to pass vector parameters in SSE registers. |
| 1231 | |
| 1232 | On 32-bit x86 targets, this calling convention is similar to ``__fastcall``. |
| 1233 | The first two integer parameters are passed in ECX and EDX. Subsequent integer |
| 1234 | parameters are passed in memory, and callee clears the stack. On x86_64 |
| 1235 | targets, the callee does *not* clear the stack, and integer parameters are |
| 1236 | passed in RCX, RDX, R8, and R9 as is done for the default Windows x64 calling |
| 1237 | convention. |
| 1238 | |
| 1239 | On both 32-bit x86 and x86_64 targets, vector and floating point arguments are |
| 1240 | passed in XMM0-XMM5. Homogenous vector aggregates of up to four elements are |
| 1241 | passed in sequential SSE registers if enough are available. If AVX is enabled, |
| 1242 | 256 bit vectors are passed in YMM0-YMM5. Any vector or aggregate type that |
| 1243 | cannot be passed in registers for any reason is passed by reference, which |
| 1244 | allows the caller to align the parameter memory. |
| 1245 | |
| 1246 | See the documentation for `__vectorcall`_ on MSDN for more details. |
| 1247 | |
| 1248 | .. _`__vectorcall`: http://msdn.microsoft.com/en-us/library/dn375768.aspx |
| 1249 | |
| 1250 | |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1251 | Consumed Annotation Checking |
| 1252 | ============================ |
| 1253 | Clang supports additional attributes for checking basic resource management |
| 1254 | properties, specifically for unique objects that have a single owning reference. |
| 1255 | The following attributes are currently supported, although **the implementation |
| 1256 | for these annotations is currently in development and are subject to change.** |
| 1257 | |
| 1258 | callable_when |
| 1259 | ------------- |
| 1260 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1261 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1262 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1263 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1264 | |
| 1265 | Use ``__attribute__((callable_when(...)))`` to indicate what states a method |
| 1266 | may be called in. Valid states are unconsumed, consumed, or unknown. Each |
| 1267 | argument to this attribute must be a quoted string. E.g.: |
| 1268 | |
| 1269 | ``__attribute__((callable_when("unconsumed", "unknown")))`` |
| 1270 | |
| 1271 | |
| 1272 | consumable |
| 1273 | ---------- |
| 1274 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1275 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1276 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1277 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1278 | |
| 1279 | Each ``class`` that uses any of the typestate annotations must first be marked |
| 1280 | using the ``consumable`` attribute. Failure to do so will result in a warning. |
| 1281 | |
| 1282 | This attribute accepts a single parameter that must be one of the following: |
| 1283 | ``unknown``, ``consumed``, or ``unconsumed``. |
| 1284 | |
| 1285 | |
| 1286 | param_typestate |
| 1287 | --------------- |
| 1288 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1289 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1290 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1291 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1292 | |
| 1293 | This attribute specifies expectations about function parameters. Calls to an |
| 1294 | function with annotated parameters will issue a warning if the corresponding |
| 1295 | argument isn't in the expected state. The attribute is also used to set the |
| 1296 | initial state of the parameter when analyzing the function's body. |
| 1297 | |
| 1298 | |
| 1299 | return_typestate |
| 1300 | ---------------- |
| 1301 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1302 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1304 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1305 | |
| 1306 | The ``return_typestate`` attribute can be applied to functions or parameters. |
| 1307 | When applied to a function the attribute specifies the state of the returned |
| 1308 | value. The function's body is checked to ensure that it always returns a value |
| 1309 | in the specified state. On the caller side, values returned by the annotated |
| 1310 | function are initialized to the given state. |
| 1311 | |
| 1312 | When applied to a function parameter it modifies the state of an argument after |
| 1313 | a call to the function returns. The function's body is checked to ensure that |
| 1314 | the parameter is in the expected state before returning. |
| 1315 | |
| 1316 | |
| 1317 | set_typestate |
| 1318 | ------------- |
| 1319 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1320 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1321 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1322 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1323 | |
| 1324 | Annotate methods that transition an object into a new state with |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1325 | ``__attribute__((set_typestate(new_state)))``. The new state must be |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1326 | unconsumed, consumed, or unknown. |
| 1327 | |
| 1328 | |
| 1329 | test_typestate |
| 1330 | -------------- |
| 1331 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1332 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1333 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1334 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1335 | |
| 1336 | Use ``__attribute__((test_typestate(tested_state)))`` to indicate that a method |
| 1337 | returns true if the object is in the specified state.. |
| 1338 | |
| 1339 | |
| 1340 | Type Safety Checking |
| 1341 | ==================== |
| 1342 | Clang supports additional attributes to enable checking type safety properties |
| 1343 | that 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 | |
| 1351 | You can detect support for these attributes with ``__has_attribute()``. For |
| 1352 | example: |
| 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 | |
| 1372 | argument_with_type_tag |
| 1373 | ---------------------- |
| 1374 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1375 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1377 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1378 | |
| 1379 | Use ``__attribute__((argument_with_type_tag(arg_kind, arg_idx, |
| 1380 | type_tag_idx)))`` on a function declaration to specify that the function |
| 1381 | accepts a type tag that determines the type of some other argument. |
| 1382 | ``arg_kind`` is an identifier that should be used when annotating all |
| 1383 | applicable type tags. |
| 1384 | |
| 1385 | This attribute is primarily useful for checking arguments of variadic functions |
| 1386 | (``pointer_with_type_tag`` can be used in most non-variadic cases). |
| 1387 | |
| 1388 | For 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 | |
| 1396 | pointer_with_type_tag |
| 1397 | --------------------- |
| 1398 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1399 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1400 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1401 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1402 | |
| 1403 | Use ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))`` |
| 1404 | on a function declaration to specify that the function accepts a type tag that |
| 1405 | determines the pointee type of some other pointer argument. |
| 1406 | |
| 1407 | For 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 | |
| 1415 | type_tag_for_datatype |
| 1416 | --------------------- |
| 1417 | .. csv-table:: Supported Syntaxes |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1418 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1420 | "X","","","", "" |
David Majnemer | fc256c3 | 2014-03-02 18:17:08 +0000 | [diff] [blame] | 1421 | |
| 1422 | Clang 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 | |
| 1445 | The attribute also accepts an optional third argument that determines how the |
| 1446 | expression 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 Gregor | 1a3ae83 | 2015-06-22 17:06:56 +0000 | [diff] [blame] | 1491 | OpenCL Address Spaces |
| 1492 | ===================== |
| 1493 | The address space qualifier may be used to specify the region of memory that is |
| 1494 | used 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 | |
| 1509 | More 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 | |
| 1518 | The constant address space attribute signals that an object is located in |
| 1519 | a constant (non-modifiable) memory region. It is available to all work items. |
| 1520 | Any type can be annotated with the constant address space attribute. Objects |
| 1521 | with the constant address space qualifier can be declared in any scope and must |
| 1522 | have 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 | |
| 1532 | The generic address space attribute is only available with OpenCL v2.0 and later. |
| 1533 | It can be used with pointer types. Variables in global and local scope and |
| 1534 | function parameters in non-kernel functions can have the generic address space |
| 1535 | type attribute. It is intended to be a placeholder for any other address space |
| 1536 | except for '__constant' in OpenCL code which can be used with multiple address |
| 1537 | spaces. |
| 1538 | |
| 1539 | |
| 1540 | __global(global) |
| 1541 | ---------------- |
| 1542 | .. csv-table:: Supported Syntaxes |
| 1543 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1544 | |
| 1545 | "","","","X", "" |
| 1546 | |
| 1547 | The global address space attribute specifies that an object is allocated in |
| 1548 | global memory, which is accessible by all work items. The content stored in this |
| 1549 | memory area persists between kernel executions. Pointer types to the global |
| 1550 | address space are allowed as function parameters or local variables. Starting |
| 1551 | with OpenCL v2.0, the global address space can be used with global (program |
| 1552 | scope) 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 | |
| 1562 | The local address space specifies that an object is allocated in the local (work |
| 1563 | group) memory area, which is accessible to all work items in the same work |
| 1564 | group. The content stored in this memory region is not accessible after |
| 1565 | the kernel execution ends. In a kernel function scope, any variable can be in |
| 1566 | the local address space. In other scopes, only pointer types to the local address |
| 1567 | space 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 | |
| 1577 | The 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 |
| 1579 | content is destroyed after work item execution ends. Local variables can be |
| 1580 | declared in the private address space. Function arguments are always in the |
| 1581 | private address space. Kernel function arguments of a pointer or an array type |
| 1582 | cannot point to the private address space. |
| 1583 | |
| 1584 | |
| 1585 | Nullability Attributes |
| 1586 | ====================== |
| 1587 | 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``). |
| 1588 | |
| 1589 | 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: |
| 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 | |
| 1604 | 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: |
| 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 | |
| 1614 | nonnull |
| 1615 | ------- |
| 1616 | .. csv-table:: Supported Syntaxes |
| 1617 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1618 | |
| 1619 | "X","X","","", "" |
| 1620 | |
| 1621 | 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: |
| 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 | |
| 1628 | Here, the ``nonnull`` attribute indicates that parameters 1 and 2 |
| 1629 | cannot 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 | |
| 1636 | 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: |
| 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 | |
| 1643 | 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. |
| 1644 | |
| 1645 | |
| 1646 | returns_nonnull |
| 1647 | --------------- |
| 1648 | .. csv-table:: Supported Syntaxes |
| 1649 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1650 | |
| 1651 | "X","X","","", "" |
| 1652 | |
| 1653 | 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: |
| 1654 | |
| 1655 | .. code-block:: c |
| 1656 | |
| 1657 | extern void * malloc (size_t size) __attribute__((returns_nonnull)); |
| 1658 | |
| 1659 | 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 |
| 1660 | |
| 1661 | |
| 1662 | __nonnull |
| 1663 | --------- |
| 1664 | .. csv-table:: Supported Syntaxes |
| 1665 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1666 | |
| 1667 | "","","","X", "" |
| 1668 | |
| 1669 | 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: |
| 1670 | |
| 1671 | .. code-block:: c |
| 1672 | |
| 1673 | int fetch(int * __nonnull ptr); |
| 1674 | |
| 1675 | 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. |
| 1676 | |
| 1677 | |
| 1678 | __null_unspecified |
| 1679 | ------------------ |
| 1680 | .. csv-table:: Supported Syntaxes |
| 1681 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1682 | |
| 1683 | "","","","X", "" |
| 1684 | |
| 1685 | 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. |
| 1686 | |
| 1687 | |
| 1688 | __nullable |
| 1689 | ---------- |
| 1690 | .. csv-table:: Supported Syntaxes |
| 1691 | :header: "GNU", "C++11", "__declspec", "Keyword", "Pragma" |
| 1692 | |
| 1693 | "","","","X", "" |
| 1694 | |
| 1695 | The ``__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 | |
| 1701 | a caller of ``fetch_or_zero`` can provide null. |
| 1702 | |
| 1703 | |