1*0a6a1f1dSLionel Sambuc.. 2*0a6a1f1dSLionel Sambuc ------------------------------------------------------------------- 3*0a6a1f1dSLionel Sambuc NOTE: This file is automatically generated by running clang-tblgen 4*0a6a1f1dSLionel Sambuc -gen-attr-docs. Do not edit this file by hand!! 5*0a6a1f1dSLionel Sambuc ------------------------------------------------------------------- 6*0a6a1f1dSLionel Sambuc 7*0a6a1f1dSLionel Sambuc=================== 8*0a6a1f1dSLionel SambucAttributes in Clang 9*0a6a1f1dSLionel Sambuc=================== 10*0a6a1f1dSLionel Sambuc.. contents:: 11*0a6a1f1dSLionel Sambuc :local: 12*0a6a1f1dSLionel Sambuc 13*0a6a1f1dSLionel SambucIntroduction 14*0a6a1f1dSLionel Sambuc============ 15*0a6a1f1dSLionel Sambuc 16*0a6a1f1dSLionel SambucThis page lists the attributes currently supported by Clang. 17*0a6a1f1dSLionel Sambuc 18*0a6a1f1dSLionel SambucFunction Attributes 19*0a6a1f1dSLionel Sambuc=================== 20*0a6a1f1dSLionel Sambuc 21*0a6a1f1dSLionel Sambuc 22*0a6a1f1dSLionel Sambucinterrupt 23*0a6a1f1dSLionel Sambuc--------- 24*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 25*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 26*0a6a1f1dSLionel Sambuc 27*0a6a1f1dSLionel Sambuc "X","","","" 28*0a6a1f1dSLionel Sambuc 29*0a6a1f1dSLionel SambucClang supports the GNU style ``__attribute__((interrupt("TYPE")))`` attribute on 30*0a6a1f1dSLionel SambucARM targets. This attribute may be attached to a function definition and 31*0a6a1f1dSLionel Sambucinstructs the backend to generate appropriate function entry/exit code so that 32*0a6a1f1dSLionel Sambucit can be used directly as an interrupt service routine. 33*0a6a1f1dSLionel Sambuc 34*0a6a1f1dSLionel SambucThe parameter passed to the interrupt attribute is optional, but if 35*0a6a1f1dSLionel Sambucprovided it must be a string literal with one of the following values: "IRQ", 36*0a6a1f1dSLionel Sambuc"FIQ", "SWI", "ABORT", "UNDEF". 37*0a6a1f1dSLionel Sambuc 38*0a6a1f1dSLionel SambucThe semantics are as follows: 39*0a6a1f1dSLionel Sambuc 40*0a6a1f1dSLionel Sambuc- If the function is AAPCS, Clang instructs the backend to realign the stack to 41*0a6a1f1dSLionel Sambuc 8 bytes on entry. This is a general requirement of the AAPCS at public 42*0a6a1f1dSLionel Sambuc interfaces, but may not hold when an exception is taken. Doing this allows 43*0a6a1f1dSLionel Sambuc other AAPCS functions to be called. 44*0a6a1f1dSLionel Sambuc- If the CPU is M-class this is all that needs to be done since the architecture 45*0a6a1f1dSLionel Sambuc itself is designed in such a way that functions obeying the normal AAPCS ABI 46*0a6a1f1dSLionel Sambuc constraints are valid exception handlers. 47*0a6a1f1dSLionel Sambuc- If the CPU is not M-class, the prologue and epilogue are modified to save all 48*0a6a1f1dSLionel Sambuc non-banked registers that are used, so that upon return the user-mode state 49*0a6a1f1dSLionel Sambuc will not be corrupted. Note that to avoid unnecessary overhead, only 50*0a6a1f1dSLionel Sambuc general-purpose (integer) registers are saved in this way. If VFP operations 51*0a6a1f1dSLionel Sambuc are needed, that state must be saved manually. 52*0a6a1f1dSLionel Sambuc 53*0a6a1f1dSLionel Sambuc Specifically, interrupt kinds other than "FIQ" will save all core registers 54*0a6a1f1dSLionel Sambuc except "lr" and "sp". "FIQ" interrupts will save r0-r7. 55*0a6a1f1dSLionel Sambuc- If the CPU is not M-class, the return instruction is changed to one of the 56*0a6a1f1dSLionel Sambuc canonical sequences permitted by the architecture for exception return. Where 57*0a6a1f1dSLionel Sambuc possible the function itself will make the necessary "lr" adjustments so that 58*0a6a1f1dSLionel Sambuc the "preferred return address" is selected. 59*0a6a1f1dSLionel Sambuc 60*0a6a1f1dSLionel Sambuc Unfortunately the compiler is unable to make this guarantee for an "UNDEF" 61*0a6a1f1dSLionel Sambuc handler, where the offset from "lr" to the preferred return address depends on 62*0a6a1f1dSLionel Sambuc the execution state of the code which generated the exception. In this case 63*0a6a1f1dSLionel Sambuc a sequence equivalent to "movs pc, lr" will be used. 64*0a6a1f1dSLionel Sambuc 65*0a6a1f1dSLionel Sambuc 66*0a6a1f1dSLionel Sambucacquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability) 67*0a6a1f1dSLionel Sambuc----------------------------------------------------------------------------------------------------------- 68*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 69*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 70*0a6a1f1dSLionel Sambuc 71*0a6a1f1dSLionel Sambuc "X","X","","" 72*0a6a1f1dSLionel Sambuc 73*0a6a1f1dSLionel SambucMarks a function as acquiring a capability. 74*0a6a1f1dSLionel Sambuc 75*0a6a1f1dSLionel Sambuc 76*0a6a1f1dSLionel Sambucassert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability) 77*0a6a1f1dSLionel Sambuc------------------------------------------------------------------------------------------------------- 78*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 79*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 80*0a6a1f1dSLionel Sambuc 81*0a6a1f1dSLionel Sambuc "X","X","","" 82*0a6a1f1dSLionel Sambuc 83*0a6a1f1dSLionel SambucMarks a function that dynamically tests whether a capability is held, and halts 84*0a6a1f1dSLionel Sambucthe program if it is not held. 85*0a6a1f1dSLionel Sambuc 86*0a6a1f1dSLionel Sambuc 87*0a6a1f1dSLionel Sambucavailability 88*0a6a1f1dSLionel Sambuc------------ 89*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 90*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 91*0a6a1f1dSLionel Sambuc 92*0a6a1f1dSLionel Sambuc "X","","","" 93*0a6a1f1dSLionel Sambuc 94*0a6a1f1dSLionel SambucThe ``availability`` attribute can be placed on declarations to describe the 95*0a6a1f1dSLionel Sambuclifecycle of that declaration relative to operating system versions. Consider 96*0a6a1f1dSLionel Sambucthe function declaration for a hypothetical function ``f``: 97*0a6a1f1dSLionel Sambuc 98*0a6a1f1dSLionel Sambuc.. code-block:: c++ 99*0a6a1f1dSLionel Sambuc 100*0a6a1f1dSLionel Sambuc void f(void) __attribute__((availability(macosx,introduced=10.4,deprecated=10.6,obsoleted=10.7))); 101*0a6a1f1dSLionel Sambuc 102*0a6a1f1dSLionel SambucThe availability attribute states that ``f`` was introduced in Mac OS X 10.4, 103*0a6a1f1dSLionel Sambucdeprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7. This information 104*0a6a1f1dSLionel Sambucis used by Clang to determine when it is safe to use ``f``: for example, if 105*0a6a1f1dSLionel SambucClang is instructed to compile code for Mac OS X 10.5, a call to ``f()`` 106*0a6a1f1dSLionel Sambucsucceeds. If Clang is instructed to compile code for Mac OS X 10.6, the call 107*0a6a1f1dSLionel Sambucsucceeds but Clang emits a warning specifying that the function is deprecated. 108*0a6a1f1dSLionel SambucFinally, if Clang is instructed to compile code for Mac OS X 10.7, the call 109*0a6a1f1dSLionel Sambucfails because ``f()`` is no longer available. 110*0a6a1f1dSLionel Sambuc 111*0a6a1f1dSLionel SambucThe availability attribute is a comma-separated list starting with the 112*0a6a1f1dSLionel Sambucplatform name and then including clauses specifying important milestones in the 113*0a6a1f1dSLionel Sambucdeclaration's lifetime (in any order) along with additional information. Those 114*0a6a1f1dSLionel Sambucclauses can be: 115*0a6a1f1dSLionel Sambuc 116*0a6a1f1dSLionel Sambucintroduced=\ *version* 117*0a6a1f1dSLionel Sambuc The first version in which this declaration was introduced. 118*0a6a1f1dSLionel Sambuc 119*0a6a1f1dSLionel Sambucdeprecated=\ *version* 120*0a6a1f1dSLionel Sambuc The first version in which this declaration was deprecated, meaning that 121*0a6a1f1dSLionel Sambuc users should migrate away from this API. 122*0a6a1f1dSLionel Sambuc 123*0a6a1f1dSLionel Sambucobsoleted=\ *version* 124*0a6a1f1dSLionel Sambuc The first version in which this declaration was obsoleted, meaning that it 125*0a6a1f1dSLionel Sambuc was removed completely and can no longer be used. 126*0a6a1f1dSLionel Sambuc 127*0a6a1f1dSLionel Sambucunavailable 128*0a6a1f1dSLionel Sambuc This declaration is never available on this platform. 129*0a6a1f1dSLionel Sambuc 130*0a6a1f1dSLionel Sambucmessage=\ *string-literal* 131*0a6a1f1dSLionel Sambuc Additional message text that Clang will provide when emitting a warning or 132*0a6a1f1dSLionel Sambuc error about use of a deprecated or obsoleted declaration. Useful to direct 133*0a6a1f1dSLionel Sambuc users to replacement APIs. 134*0a6a1f1dSLionel Sambuc 135*0a6a1f1dSLionel SambucMultiple availability attributes can be placed on a declaration, which may 136*0a6a1f1dSLionel Sambuccorrespond to different platforms. Only the availability attribute with the 137*0a6a1f1dSLionel Sambucplatform corresponding to the target platform will be used; any others will be 138*0a6a1f1dSLionel Sambucignored. If no availability attribute specifies availability for the current 139*0a6a1f1dSLionel Sambuctarget platform, the availability attributes are ignored. Supported platforms 140*0a6a1f1dSLionel Sambucare: 141*0a6a1f1dSLionel Sambuc 142*0a6a1f1dSLionel Sambuc``ios`` 143*0a6a1f1dSLionel Sambuc Apple's iOS operating system. The minimum deployment target is specified by 144*0a6a1f1dSLionel Sambuc the ``-mios-version-min=*version*`` or ``-miphoneos-version-min=*version*`` 145*0a6a1f1dSLionel Sambuc command-line arguments. 146*0a6a1f1dSLionel Sambuc 147*0a6a1f1dSLionel Sambuc``macosx`` 148*0a6a1f1dSLionel Sambuc Apple's Mac OS X operating system. The minimum deployment target is 149*0a6a1f1dSLionel Sambuc specified by the ``-mmacosx-version-min=*version*`` command-line argument. 150*0a6a1f1dSLionel Sambuc 151*0a6a1f1dSLionel SambucA declaration can be used even when deploying back to a platform version prior 152*0a6a1f1dSLionel Sambucto when the declaration was introduced. When this happens, the declaration is 153*0a6a1f1dSLionel Sambuc`weakly linked 154*0a6a1f1dSLionel Sambuc<https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html>`_, 155*0a6a1f1dSLionel Sambucas if the ``weak_import`` attribute were added to the declaration. A 156*0a6a1f1dSLionel Sambucweakly-linked declaration may or may not be present a run-time, and a program 157*0a6a1f1dSLionel Sambuccan determine whether the declaration is present by checking whether the 158*0a6a1f1dSLionel Sambucaddress of that declaration is non-NULL. 159*0a6a1f1dSLionel Sambuc 160*0a6a1f1dSLionel SambucIf there are multiple declarations of the same entity, the availability 161*0a6a1f1dSLionel Sambucattributes must either match on a per-platform basis or later 162*0a6a1f1dSLionel Sambucdeclarations must not have availability attributes for that 163*0a6a1f1dSLionel Sambucplatform. For example: 164*0a6a1f1dSLionel Sambuc 165*0a6a1f1dSLionel Sambuc.. code-block:: c 166*0a6a1f1dSLionel Sambuc 167*0a6a1f1dSLionel Sambuc void g(void) __attribute__((availability(macosx,introduced=10.4))); 168*0a6a1f1dSLionel Sambuc void g(void) __attribute__((availability(macosx,introduced=10.4))); // okay, matches 169*0a6a1f1dSLionel Sambuc void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform 170*0a6a1f1dSLionel Sambuc void g(void); // okay, inherits both macosx and ios availability from above. 171*0a6a1f1dSLionel Sambuc void g(void) __attribute__((availability(macosx,introduced=10.5))); // error: mismatch 172*0a6a1f1dSLionel Sambuc 173*0a6a1f1dSLionel SambucWhen one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,: 174*0a6a1f1dSLionel Sambuc 175*0a6a1f1dSLionel Sambuc.. code-block:: objc 176*0a6a1f1dSLionel Sambuc 177*0a6a1f1dSLionel Sambuc @interface A 178*0a6a1f1dSLionel Sambuc - (id)method __attribute__((availability(macosx,introduced=10.4))); 179*0a6a1f1dSLionel Sambuc - (id)method2 __attribute__((availability(macosx,introduced=10.4))); 180*0a6a1f1dSLionel Sambuc @end 181*0a6a1f1dSLionel Sambuc 182*0a6a1f1dSLionel Sambuc @interface B : A 183*0a6a1f1dSLionel Sambuc - (id)method __attribute__((availability(macosx,introduced=10.3))); // okay: method moved into base class later 184*0a6a1f1dSLionel Sambuc - (id)method __attribute__((availability(macosx,introduced=10.5))); // error: this method was available via the base class in 10.4 185*0a6a1f1dSLionel Sambuc @end 186*0a6a1f1dSLionel Sambuc 187*0a6a1f1dSLionel Sambuc 188*0a6a1f1dSLionel Sambuc_Noreturn 189*0a6a1f1dSLionel Sambuc--------- 190*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 191*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 192*0a6a1f1dSLionel Sambuc 193*0a6a1f1dSLionel Sambuc "","","","X" 194*0a6a1f1dSLionel Sambuc 195*0a6a1f1dSLionel SambucA function declared as ``_Noreturn`` shall not return to its caller. The 196*0a6a1f1dSLionel Sambuccompiler will generate a diagnostic for a function declared as ``_Noreturn`` 197*0a6a1f1dSLionel Sambucthat appears to be capable of returning to its caller. 198*0a6a1f1dSLionel Sambuc 199*0a6a1f1dSLionel Sambuc 200*0a6a1f1dSLionel Sambucnoreturn 201*0a6a1f1dSLionel Sambuc-------- 202*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 203*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 204*0a6a1f1dSLionel Sambuc 205*0a6a1f1dSLionel Sambuc "","X","","" 206*0a6a1f1dSLionel Sambuc 207*0a6a1f1dSLionel SambucA function declared as ``[[noreturn]]`` shall not return to its caller. The 208*0a6a1f1dSLionel Sambuccompiler will generate a diagnostic for a function declared as ``[[noreturn]]`` 209*0a6a1f1dSLionel Sambucthat appears to be capable of returning to its caller. 210*0a6a1f1dSLionel Sambuc 211*0a6a1f1dSLionel Sambuc 212*0a6a1f1dSLionel Sambuccarries_dependency 213*0a6a1f1dSLionel Sambuc------------------ 214*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 215*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 216*0a6a1f1dSLionel Sambuc 217*0a6a1f1dSLionel Sambuc "X","X","","" 218*0a6a1f1dSLionel Sambuc 219*0a6a1f1dSLionel SambucThe ``carries_dependency`` attribute specifies dependency propagation into and 220*0a6a1f1dSLionel Sambucout of functions. 221*0a6a1f1dSLionel Sambuc 222*0a6a1f1dSLionel SambucWhen specified on a function or Objective-C method, the ``carries_dependency`` 223*0a6a1f1dSLionel Sambucattribute means that the return value carries a dependency out of the function, 224*0a6a1f1dSLionel Sambucso that the implementation need not constrain ordering upon return from that 225*0a6a1f1dSLionel Sambucfunction. Implementations of the function and its caller may choose to preserve 226*0a6a1f1dSLionel Sambucdependencies instead of emitting memory ordering instructions such as fences. 227*0a6a1f1dSLionel Sambuc 228*0a6a1f1dSLionel SambucNote, this attribute does not change the meaning of the program, but may result 229*0a6a1f1dSLionel Sambucin generation of more efficient code. 230*0a6a1f1dSLionel Sambuc 231*0a6a1f1dSLionel Sambuc 232*0a6a1f1dSLionel Sambucenable_if 233*0a6a1f1dSLionel Sambuc--------- 234*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 235*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 236*0a6a1f1dSLionel Sambuc 237*0a6a1f1dSLionel Sambuc "X","","","" 238*0a6a1f1dSLionel Sambuc 239*0a6a1f1dSLionel Sambuc.. Note:: Some features of this attribute are experimental. The meaning of 240*0a6a1f1dSLionel Sambuc multiple enable_if attributes on a single declaration is subject to change in 241*0a6a1f1dSLionel Sambuc a future version of clang. Also, the ABI is not standardized and the name 242*0a6a1f1dSLionel Sambuc mangling may change in future versions. To avoid that, use asm labels. 243*0a6a1f1dSLionel Sambuc 244*0a6a1f1dSLionel SambucThe ``enable_if`` attribute can be placed on function declarations to control 245*0a6a1f1dSLionel Sambucwhich overload is selected based on the values of the function's arguments. 246*0a6a1f1dSLionel SambucWhen combined with the ``overloadable`` attribute, this feature is also 247*0a6a1f1dSLionel Sambucavailable in C. 248*0a6a1f1dSLionel Sambuc 249*0a6a1f1dSLionel Sambuc.. code-block:: c++ 250*0a6a1f1dSLionel Sambuc 251*0a6a1f1dSLionel Sambuc int isdigit(int c); 252*0a6a1f1dSLionel Sambuc 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"))); 253*0a6a1f1dSLionel Sambuc 254*0a6a1f1dSLionel Sambuc void foo(char c) { 255*0a6a1f1dSLionel Sambuc isdigit(c); 256*0a6a1f1dSLionel Sambuc isdigit(10); 257*0a6a1f1dSLionel Sambuc isdigit(-10); // results in a compile-time error. 258*0a6a1f1dSLionel Sambuc } 259*0a6a1f1dSLionel Sambuc 260*0a6a1f1dSLionel SambucThe enable_if attribute takes two arguments, the first is an expression written 261*0a6a1f1dSLionel Sambucin terms of the function parameters, the second is a string explaining why this 262*0a6a1f1dSLionel Sambucoverload candidate could not be selected to be displayed in diagnostics. The 263*0a6a1f1dSLionel Sambucexpression is part of the function signature for the purposes of determining 264*0a6a1f1dSLionel Sambucwhether it is a redeclaration (following the rules used when determining 265*0a6a1f1dSLionel Sambucwhether a C++ template specialization is ODR-equivalent), but is not part of 266*0a6a1f1dSLionel Sambucthe type. 267*0a6a1f1dSLionel Sambuc 268*0a6a1f1dSLionel SambucThe enable_if expression is evaluated as if it were the body of a 269*0a6a1f1dSLionel Sambucbool-returning constexpr function declared with the arguments of the function 270*0a6a1f1dSLionel Sambucit is being applied to, then called with the parameters at the callsite. If the 271*0a6a1f1dSLionel Sambucresult is false or could not be determined through constant expression 272*0a6a1f1dSLionel Sambucevaluation, then this overload will not be chosen and the provided string may 273*0a6a1f1dSLionel Sambucbe used in a diagnostic if the compile fails as a result. 274*0a6a1f1dSLionel Sambuc 275*0a6a1f1dSLionel SambucBecause the enable_if expression is an unevaluated context, there are no global 276*0a6a1f1dSLionel Sambucstate changes, nor the ability to pass information from the enable_if 277*0a6a1f1dSLionel Sambucexpression to the function body. For example, suppose we want calls to 278*0a6a1f1dSLionel Sambucstrnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of 279*0a6a1f1dSLionel Sambucstrbuf) only if the size of strbuf can be determined: 280*0a6a1f1dSLionel Sambuc 281*0a6a1f1dSLionel Sambuc.. code-block:: c++ 282*0a6a1f1dSLionel Sambuc 283*0a6a1f1dSLionel Sambuc __attribute__((always_inline)) 284*0a6a1f1dSLionel Sambuc static inline size_t strnlen(const char *s, size_t maxlen) 285*0a6a1f1dSLionel Sambuc __attribute__((overloadable)) 286*0a6a1f1dSLionel Sambuc __attribute__((enable_if(__builtin_object_size(s, 0) != -1))), 287*0a6a1f1dSLionel Sambuc "chosen when the buffer size is known but 'maxlen' is not"))) 288*0a6a1f1dSLionel Sambuc { 289*0a6a1f1dSLionel Sambuc return strnlen_chk(s, maxlen, __builtin_object_size(s, 0)); 290*0a6a1f1dSLionel Sambuc } 291*0a6a1f1dSLionel Sambuc 292*0a6a1f1dSLionel SambucMultiple enable_if attributes may be applied to a single declaration. In this 293*0a6a1f1dSLionel Sambuccase, the enable_if expressions are evaluated from left to right in the 294*0a6a1f1dSLionel Sambucfollowing manner. First, the candidates whose enable_if expressions evaluate to 295*0a6a1f1dSLionel Sambucfalse or cannot be evaluated are discarded. If the remaining candidates do not 296*0a6a1f1dSLionel Sambucshare ODR-equivalent enable_if expressions, the overload resolution is 297*0a6a1f1dSLionel Sambucambiguous. Otherwise, enable_if overload resolution continues with the next 298*0a6a1f1dSLionel Sambucenable_if attribute on the candidates that have not been discarded and have 299*0a6a1f1dSLionel Sambucremaining enable_if attributes. In this way, we pick the most specific 300*0a6a1f1dSLionel Sambucoverload out of a number of viable overloads using enable_if. 301*0a6a1f1dSLionel Sambuc 302*0a6a1f1dSLionel Sambuc.. code-block:: c++ 303*0a6a1f1dSLionel Sambuc 304*0a6a1f1dSLionel Sambuc void f() __attribute__((enable_if(true, ""))); // #1 305*0a6a1f1dSLionel Sambuc void f() __attribute__((enable_if(true, ""))) __attribute__((enable_if(true, ""))); // #2 306*0a6a1f1dSLionel Sambuc 307*0a6a1f1dSLionel Sambuc void g(int i, int j) __attribute__((enable_if(i, ""))); // #1 308*0a6a1f1dSLionel Sambuc void g(int i, int j) __attribute__((enable_if(j, ""))) __attribute__((enable_if(true))); // #2 309*0a6a1f1dSLionel Sambuc 310*0a6a1f1dSLionel SambucIn this example, a call to f() is always resolved to #2, as the first enable_if 311*0a6a1f1dSLionel Sambucexpression is ODR-equivalent for both declarations, but #1 does not have another 312*0a6a1f1dSLionel Sambucenable_if expression to continue evaluating, so the next round of evaluation has 313*0a6a1f1dSLionel Sambuconly a single candidate. In a call to g(1, 1), the call is ambiguous even though 314*0a6a1f1dSLionel Sambuc#2 has more enable_if attributes, because the first enable_if expressions are 315*0a6a1f1dSLionel Sambucnot ODR-equivalent. 316*0a6a1f1dSLionel Sambuc 317*0a6a1f1dSLionel SambucQuery for this feature with ``__has_attribute(enable_if)``. 318*0a6a1f1dSLionel Sambuc 319*0a6a1f1dSLionel Sambuc 320*0a6a1f1dSLionel Sambucflatten (gnu::flatten) 321*0a6a1f1dSLionel Sambuc---------------------- 322*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 323*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 324*0a6a1f1dSLionel Sambuc 325*0a6a1f1dSLionel Sambuc "X","X","","" 326*0a6a1f1dSLionel Sambuc 327*0a6a1f1dSLionel SambucThe ``flatten`` attribute causes calls within the attributed function to 328*0a6a1f1dSLionel Sambucbe inlined unless it is impossible to do so, for example if the body of the 329*0a6a1f1dSLionel Sambuccallee is unavailable or if the callee has the ``noinline`` attribute. 330*0a6a1f1dSLionel Sambuc 331*0a6a1f1dSLionel Sambuc 332*0a6a1f1dSLionel Sambucformat (gnu::format) 333*0a6a1f1dSLionel Sambuc-------------------- 334*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 335*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 336*0a6a1f1dSLionel Sambuc 337*0a6a1f1dSLionel Sambuc "X","X","","" 338*0a6a1f1dSLionel Sambuc 339*0a6a1f1dSLionel SambucClang supports the ``format`` attribute, which indicates that the function 340*0a6a1f1dSLionel Sambucaccepts a ``printf`` or ``scanf``-like format string and corresponding 341*0a6a1f1dSLionel Sambucarguments or a ``va_list`` that contains these arguments. 342*0a6a1f1dSLionel Sambuc 343*0a6a1f1dSLionel SambucPlease see `GCC documentation about format attribute 344*0a6a1f1dSLionel Sambuc<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details 345*0a6a1f1dSLionel Sambucabout attribute syntax. 346*0a6a1f1dSLionel Sambuc 347*0a6a1f1dSLionel SambucClang implements two kinds of checks with this attribute. 348*0a6a1f1dSLionel Sambuc 349*0a6a1f1dSLionel Sambuc#. Clang checks that the function with the ``format`` attribute is called with 350*0a6a1f1dSLionel Sambuc a format string that uses format specifiers that are allowed, and that 351*0a6a1f1dSLionel Sambuc arguments match the format string. This is the ``-Wformat`` warning, it is 352*0a6a1f1dSLionel Sambuc on by default. 353*0a6a1f1dSLionel Sambuc 354*0a6a1f1dSLionel Sambuc#. Clang checks that the format string argument is a literal string. This is 355*0a6a1f1dSLionel Sambuc the ``-Wformat-nonliteral`` warning, it is off by default. 356*0a6a1f1dSLionel Sambuc 357*0a6a1f1dSLionel Sambuc Clang implements this mostly the same way as GCC, but there is a difference 358*0a6a1f1dSLionel Sambuc for functions that accept a ``va_list`` argument (for example, ``vprintf``). 359*0a6a1f1dSLionel Sambuc GCC does not emit ``-Wformat-nonliteral`` warning for calls to such 360*0a6a1f1dSLionel Sambuc fuctions. Clang does not warn if the format string comes from a function 361*0a6a1f1dSLionel Sambuc parameter, where the function is annotated with a compatible attribute, 362*0a6a1f1dSLionel Sambuc otherwise it warns. For example: 363*0a6a1f1dSLionel Sambuc 364*0a6a1f1dSLionel Sambuc .. code-block:: c 365*0a6a1f1dSLionel Sambuc 366*0a6a1f1dSLionel Sambuc __attribute__((__format__ (__scanf__, 1, 3))) 367*0a6a1f1dSLionel Sambuc void foo(const char* s, char *buf, ...) { 368*0a6a1f1dSLionel Sambuc va_list ap; 369*0a6a1f1dSLionel Sambuc va_start(ap, buf); 370*0a6a1f1dSLionel Sambuc 371*0a6a1f1dSLionel Sambuc vprintf(s, ap); // warning: format string is not a string literal 372*0a6a1f1dSLionel Sambuc } 373*0a6a1f1dSLionel Sambuc 374*0a6a1f1dSLionel Sambuc In this case we warn because ``s`` contains a format string for a 375*0a6a1f1dSLionel Sambuc ``scanf``-like function, but it is passed to a ``printf``-like function. 376*0a6a1f1dSLionel Sambuc 377*0a6a1f1dSLionel Sambuc If the attribute is removed, clang still warns, because the format string is 378*0a6a1f1dSLionel Sambuc not a string literal. 379*0a6a1f1dSLionel Sambuc 380*0a6a1f1dSLionel Sambuc Another example: 381*0a6a1f1dSLionel Sambuc 382*0a6a1f1dSLionel Sambuc .. code-block:: c 383*0a6a1f1dSLionel Sambuc 384*0a6a1f1dSLionel Sambuc __attribute__((__format__ (__printf__, 1, 3))) 385*0a6a1f1dSLionel Sambuc void foo(const char* s, char *buf, ...) { 386*0a6a1f1dSLionel Sambuc va_list ap; 387*0a6a1f1dSLionel Sambuc va_start(ap, buf); 388*0a6a1f1dSLionel Sambuc 389*0a6a1f1dSLionel Sambuc vprintf(s, ap); // warning 390*0a6a1f1dSLionel Sambuc } 391*0a6a1f1dSLionel Sambuc 392*0a6a1f1dSLionel Sambuc In this case Clang does not warn because the format string ``s`` and 393*0a6a1f1dSLionel Sambuc the corresponding arguments are annotated. If the arguments are 394*0a6a1f1dSLionel Sambuc incorrect, the caller of ``foo`` will receive a warning. 395*0a6a1f1dSLionel Sambuc 396*0a6a1f1dSLionel Sambuc 397*0a6a1f1dSLionel Sambucnoduplicate (clang::noduplicate) 398*0a6a1f1dSLionel Sambuc-------------------------------- 399*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 400*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 401*0a6a1f1dSLionel Sambuc 402*0a6a1f1dSLionel Sambuc "X","X","","" 403*0a6a1f1dSLionel Sambuc 404*0a6a1f1dSLionel SambucThe ``noduplicate`` attribute can be placed on function declarations to control 405*0a6a1f1dSLionel Sambucwhether function calls to this function can be duplicated or not as a result of 406*0a6a1f1dSLionel Sambucoptimizations. This is required for the implementation of functions with 407*0a6a1f1dSLionel Sambuccertain special requirements, like the OpenCL "barrier" function, that might 408*0a6a1f1dSLionel Sambucneed to be run concurrently by all the threads that are executing in lockstep 409*0a6a1f1dSLionel Sambucon the hardware. For example this attribute applied on the function 410*0a6a1f1dSLionel Sambuc"nodupfunc" in the code below avoids that: 411*0a6a1f1dSLionel Sambuc 412*0a6a1f1dSLionel Sambuc.. code-block:: c 413*0a6a1f1dSLionel Sambuc 414*0a6a1f1dSLionel Sambuc void nodupfunc() __attribute__((noduplicate)); 415*0a6a1f1dSLionel Sambuc // Setting it as a C++11 attribute is also valid 416*0a6a1f1dSLionel Sambuc // void nodupfunc() [[clang::noduplicate]]; 417*0a6a1f1dSLionel Sambuc void foo(); 418*0a6a1f1dSLionel Sambuc void bar(); 419*0a6a1f1dSLionel Sambuc 420*0a6a1f1dSLionel Sambuc nodupfunc(); 421*0a6a1f1dSLionel Sambuc if (a > n) { 422*0a6a1f1dSLionel Sambuc foo(); 423*0a6a1f1dSLionel Sambuc } else { 424*0a6a1f1dSLionel Sambuc bar(); 425*0a6a1f1dSLionel Sambuc } 426*0a6a1f1dSLionel Sambuc 427*0a6a1f1dSLionel Sambucgets possibly modified by some optimizations into code similar to this: 428*0a6a1f1dSLionel Sambuc 429*0a6a1f1dSLionel Sambuc.. code-block:: c 430*0a6a1f1dSLionel Sambuc 431*0a6a1f1dSLionel Sambuc if (a > n) { 432*0a6a1f1dSLionel Sambuc nodupfunc(); 433*0a6a1f1dSLionel Sambuc foo(); 434*0a6a1f1dSLionel Sambuc } else { 435*0a6a1f1dSLionel Sambuc nodupfunc(); 436*0a6a1f1dSLionel Sambuc bar(); 437*0a6a1f1dSLionel Sambuc } 438*0a6a1f1dSLionel Sambuc 439*0a6a1f1dSLionel Sambucwhere the call to "nodupfunc" is duplicated and sunk into the two branches 440*0a6a1f1dSLionel Sambucof the condition. 441*0a6a1f1dSLionel Sambuc 442*0a6a1f1dSLionel Sambuc 443*0a6a1f1dSLionel Sambucno_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address) 444*0a6a1f1dSLionel Sambuc----------------------------------------------------------------------------------------------------------- 445*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 446*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 447*0a6a1f1dSLionel Sambuc 448*0a6a1f1dSLionel Sambuc "X","X","","" 449*0a6a1f1dSLionel Sambuc 450*0a6a1f1dSLionel Sambuc.. _langext-address_sanitizer: 451*0a6a1f1dSLionel Sambuc 452*0a6a1f1dSLionel SambucUse ``__attribute__((no_sanitize_address))`` on a function declaration to 453*0a6a1f1dSLionel Sambucspecify that address safety instrumentation (e.g. AddressSanitizer) should 454*0a6a1f1dSLionel Sambucnot be applied to that function. 455*0a6a1f1dSLionel Sambuc 456*0a6a1f1dSLionel Sambuc 457*0a6a1f1dSLionel Sambucno_sanitize_memory 458*0a6a1f1dSLionel Sambuc------------------ 459*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 460*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 461*0a6a1f1dSLionel Sambuc 462*0a6a1f1dSLionel Sambuc "X","","","" 463*0a6a1f1dSLionel Sambuc 464*0a6a1f1dSLionel Sambuc.. _langext-memory_sanitizer: 465*0a6a1f1dSLionel Sambuc 466*0a6a1f1dSLionel SambucUse ``__attribute__((no_sanitize_memory))`` on a function declaration to 467*0a6a1f1dSLionel Sambucspecify that checks for uninitialized memory should not be inserted 468*0a6a1f1dSLionel Sambuc(e.g. by MemorySanitizer). The function may still be instrumented by the tool 469*0a6a1f1dSLionel Sambucto avoid false positives in other places. 470*0a6a1f1dSLionel Sambuc 471*0a6a1f1dSLionel Sambuc 472*0a6a1f1dSLionel Sambucno_sanitize_thread 473*0a6a1f1dSLionel Sambuc------------------ 474*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 475*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 476*0a6a1f1dSLionel Sambuc 477*0a6a1f1dSLionel Sambuc "X","","","" 478*0a6a1f1dSLionel Sambuc 479*0a6a1f1dSLionel Sambuc.. _langext-thread_sanitizer: 480*0a6a1f1dSLionel Sambuc 481*0a6a1f1dSLionel SambucUse ``__attribute__((no_sanitize_thread))`` on a function declaration to 482*0a6a1f1dSLionel Sambucspecify that checks for data races on plain (non-atomic) memory accesses should 483*0a6a1f1dSLionel Sambucnot be inserted by ThreadSanitizer. The function is still instrumented by the 484*0a6a1f1dSLionel Sambuctool to avoid false positives and provide meaningful stack traces. 485*0a6a1f1dSLionel Sambuc 486*0a6a1f1dSLionel Sambuc 487*0a6a1f1dSLionel Sambucno_split_stack (gnu::no_split_stack) 488*0a6a1f1dSLionel Sambuc------------------------------------ 489*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 490*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 491*0a6a1f1dSLionel Sambuc 492*0a6a1f1dSLionel Sambuc "X","X","","" 493*0a6a1f1dSLionel Sambuc 494*0a6a1f1dSLionel SambucThe ``no_split_stack`` attribute disables the emission of the split stack 495*0a6a1f1dSLionel Sambucpreamble for a particular function. It has no effect if ``-fsplit-stack`` 496*0a6a1f1dSLionel Sambucis not specified. 497*0a6a1f1dSLionel Sambuc 498*0a6a1f1dSLionel Sambuc 499*0a6a1f1dSLionel Sambucobjc_method_family 500*0a6a1f1dSLionel Sambuc------------------ 501*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 502*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 503*0a6a1f1dSLionel Sambuc 504*0a6a1f1dSLionel Sambuc "X","","","" 505*0a6a1f1dSLionel Sambuc 506*0a6a1f1dSLionel SambucMany methods in Objective-C have conventional meanings determined by their 507*0a6a1f1dSLionel Sambucselectors. It is sometimes useful to be able to mark a method as having a 508*0a6a1f1dSLionel Sambucparticular conventional meaning despite not having the right selector, or as 509*0a6a1f1dSLionel Sambucnot having the conventional meaning that its selector would suggest. For these 510*0a6a1f1dSLionel Sambucuse cases, we provide an attribute to specifically describe the "method family" 511*0a6a1f1dSLionel Sambucthat a method belongs to. 512*0a6a1f1dSLionel Sambuc 513*0a6a1f1dSLionel Sambuc**Usage**: ``__attribute__((objc_method_family(X)))``, where ``X`` is one of 514*0a6a1f1dSLionel Sambuc``none``, ``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``. This 515*0a6a1f1dSLionel Sambucattribute can only be placed at the end of a method declaration: 516*0a6a1f1dSLionel Sambuc 517*0a6a1f1dSLionel Sambuc.. code-block:: objc 518*0a6a1f1dSLionel Sambuc 519*0a6a1f1dSLionel Sambuc - (NSString *)initMyStringValue __attribute__((objc_method_family(none))); 520*0a6a1f1dSLionel Sambuc 521*0a6a1f1dSLionel SambucUsers who do not wish to change the conventional meaning of a method, and who 522*0a6a1f1dSLionel Sambucmerely want to document its non-standard retain and release semantics, should 523*0a6a1f1dSLionel Sambucuse the retaining behavior attributes (``ns_returns_retained``, 524*0a6a1f1dSLionel Sambuc``ns_returns_not_retained``, etc). 525*0a6a1f1dSLionel Sambuc 526*0a6a1f1dSLionel SambucQuery for this feature with ``__has_attribute(objc_method_family)``. 527*0a6a1f1dSLionel Sambuc 528*0a6a1f1dSLionel Sambuc 529*0a6a1f1dSLionel Sambucobjc_requires_super 530*0a6a1f1dSLionel Sambuc------------------- 531*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 532*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 533*0a6a1f1dSLionel Sambuc 534*0a6a1f1dSLionel Sambuc "X","","","" 535*0a6a1f1dSLionel Sambuc 536*0a6a1f1dSLionel SambucSome Objective-C classes allow a subclass to override a particular method in a 537*0a6a1f1dSLionel Sambucparent class but expect that the overriding method also calls the overridden 538*0a6a1f1dSLionel Sambucmethod in the parent class. For these cases, we provide an attribute to 539*0a6a1f1dSLionel Sambucdesignate that a method requires a "call to ``super``" in the overriding 540*0a6a1f1dSLionel Sambucmethod in the subclass. 541*0a6a1f1dSLionel Sambuc 542*0a6a1f1dSLionel Sambuc**Usage**: ``__attribute__((objc_requires_super))``. This attribute can only 543*0a6a1f1dSLionel Sambucbe placed at the end of a method declaration: 544*0a6a1f1dSLionel Sambuc 545*0a6a1f1dSLionel Sambuc.. code-block:: objc 546*0a6a1f1dSLionel Sambuc 547*0a6a1f1dSLionel Sambuc - (void)foo __attribute__((objc_requires_super)); 548*0a6a1f1dSLionel Sambuc 549*0a6a1f1dSLionel SambucThis attribute can only be applied the method declarations within a class, and 550*0a6a1f1dSLionel Sambucnot a protocol. Currently this attribute does not enforce any placement of 551*0a6a1f1dSLionel Sambucwhere the call occurs in the overriding method (such as in the case of 552*0a6a1f1dSLionel Sambuc``-dealloc`` where the call must appear at the end). It checks only that it 553*0a6a1f1dSLionel Sambucexists. 554*0a6a1f1dSLionel Sambuc 555*0a6a1f1dSLionel SambucNote that on both OS X and iOS that the Foundation framework provides a 556*0a6a1f1dSLionel Sambucconvenience macro ``NS_REQUIRES_SUPER`` that provides syntactic sugar for this 557*0a6a1f1dSLionel Sambucattribute: 558*0a6a1f1dSLionel Sambuc 559*0a6a1f1dSLionel Sambuc.. code-block:: objc 560*0a6a1f1dSLionel Sambuc 561*0a6a1f1dSLionel Sambuc - (void)foo NS_REQUIRES_SUPER; 562*0a6a1f1dSLionel Sambuc 563*0a6a1f1dSLionel SambucThis macro is conditionally defined depending on the compiler's support for 564*0a6a1f1dSLionel Sambucthis attribute. If the compiler does not support the attribute the macro 565*0a6a1f1dSLionel Sambucexpands to nothing. 566*0a6a1f1dSLionel Sambuc 567*0a6a1f1dSLionel SambucOperationally, when a method has this annotation the compiler will warn if the 568*0a6a1f1dSLionel Sambucimplementation of an override in a subclass does not call super. For example: 569*0a6a1f1dSLionel Sambuc 570*0a6a1f1dSLionel Sambuc.. code-block:: objc 571*0a6a1f1dSLionel Sambuc 572*0a6a1f1dSLionel Sambuc warning: method possibly missing a [super AnnotMeth] call 573*0a6a1f1dSLionel Sambuc - (void) AnnotMeth{}; 574*0a6a1f1dSLionel Sambuc ^ 575*0a6a1f1dSLionel Sambuc 576*0a6a1f1dSLionel Sambuc 577*0a6a1f1dSLionel Sambucoptnone (clang::optnone) 578*0a6a1f1dSLionel Sambuc------------------------ 579*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 580*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 581*0a6a1f1dSLionel Sambuc 582*0a6a1f1dSLionel Sambuc "X","X","","" 583*0a6a1f1dSLionel Sambuc 584*0a6a1f1dSLionel SambucThe ``optnone`` attribute suppresses essentially all optimizations 585*0a6a1f1dSLionel Sambucon a function or method, regardless of the optimization level applied to 586*0a6a1f1dSLionel Sambucthe compilation unit as a whole. This is particularly useful when you 587*0a6a1f1dSLionel Sambucneed to debug a particular function, but it is infeasible to build the 588*0a6a1f1dSLionel Sambucentire application without optimization. Avoiding optimization on the 589*0a6a1f1dSLionel Sambucspecified function can improve the quality of the debugging information 590*0a6a1f1dSLionel Sambucfor that function. 591*0a6a1f1dSLionel Sambuc 592*0a6a1f1dSLionel SambucThis attribute is incompatible with the ``always_inline`` attribute. 593*0a6a1f1dSLionel Sambuc 594*0a6a1f1dSLionel Sambuc 595*0a6a1f1dSLionel Sambucoverloadable 596*0a6a1f1dSLionel Sambuc------------ 597*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 598*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 599*0a6a1f1dSLionel Sambuc 600*0a6a1f1dSLionel Sambuc "X","","","" 601*0a6a1f1dSLionel Sambuc 602*0a6a1f1dSLionel SambucClang provides support for C++ function overloading in C. Function overloading 603*0a6a1f1dSLionel Sambucin C is introduced using the ``overloadable`` attribute. For example, one 604*0a6a1f1dSLionel Sambucmight provide several overloaded versions of a ``tgsin`` function that invokes 605*0a6a1f1dSLionel Sambucthe appropriate standard function computing the sine of a value with ``float``, 606*0a6a1f1dSLionel Sambuc``double``, or ``long double`` precision: 607*0a6a1f1dSLionel Sambuc 608*0a6a1f1dSLionel Sambuc.. code-block:: c 609*0a6a1f1dSLionel Sambuc 610*0a6a1f1dSLionel Sambuc #include <math.h> 611*0a6a1f1dSLionel Sambuc float __attribute__((overloadable)) tgsin(float x) { return sinf(x); } 612*0a6a1f1dSLionel Sambuc double __attribute__((overloadable)) tgsin(double x) { return sin(x); } 613*0a6a1f1dSLionel Sambuc long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); } 614*0a6a1f1dSLionel Sambuc 615*0a6a1f1dSLionel SambucGiven these declarations, one can call ``tgsin`` with a ``float`` value to 616*0a6a1f1dSLionel Sambucreceive a ``float`` result, with a ``double`` to receive a ``double`` result, 617*0a6a1f1dSLionel Sambucetc. Function overloading in C follows the rules of C++ function overloading 618*0a6a1f1dSLionel Sambucto pick the best overload given the call arguments, with a few C-specific 619*0a6a1f1dSLionel Sambucsemantics: 620*0a6a1f1dSLionel Sambuc 621*0a6a1f1dSLionel Sambuc* Conversion from ``float`` or ``double`` to ``long double`` is ranked as a 622*0a6a1f1dSLionel Sambuc floating-point promotion (per C99) rather than as a floating-point conversion 623*0a6a1f1dSLionel Sambuc (as in C++). 624*0a6a1f1dSLionel Sambuc 625*0a6a1f1dSLionel Sambuc* A conversion from a pointer of type ``T*`` to a pointer of type ``U*`` is 626*0a6a1f1dSLionel Sambuc considered a pointer conversion (with conversion rank) if ``T`` and ``U`` are 627*0a6a1f1dSLionel Sambuc compatible types. 628*0a6a1f1dSLionel Sambuc 629*0a6a1f1dSLionel Sambuc* A conversion from type ``T`` to a value of type ``U`` is permitted if ``T`` 630*0a6a1f1dSLionel Sambuc and ``U`` are compatible types. This conversion is given "conversion" rank. 631*0a6a1f1dSLionel Sambuc 632*0a6a1f1dSLionel SambucThe declaration of ``overloadable`` functions is restricted to function 633*0a6a1f1dSLionel Sambucdeclarations and definitions. Most importantly, if any function with a given 634*0a6a1f1dSLionel Sambucname is given the ``overloadable`` attribute, then all function declarations 635*0a6a1f1dSLionel Sambucand definitions with that name (and in that scope) must have the 636*0a6a1f1dSLionel Sambuc``overloadable`` attribute. This rule even applies to redeclarations of 637*0a6a1f1dSLionel Sambucfunctions whose original declaration had the ``overloadable`` attribute, e.g., 638*0a6a1f1dSLionel Sambuc 639*0a6a1f1dSLionel Sambuc.. code-block:: c 640*0a6a1f1dSLionel Sambuc 641*0a6a1f1dSLionel Sambuc int f(int) __attribute__((overloadable)); 642*0a6a1f1dSLionel Sambuc float f(float); // error: declaration of "f" must have the "overloadable" attribute 643*0a6a1f1dSLionel Sambuc 644*0a6a1f1dSLionel Sambuc int g(int) __attribute__((overloadable)); 645*0a6a1f1dSLionel Sambuc int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute 646*0a6a1f1dSLionel Sambuc 647*0a6a1f1dSLionel SambucFunctions marked ``overloadable`` must have prototypes. Therefore, the 648*0a6a1f1dSLionel Sambucfollowing code is ill-formed: 649*0a6a1f1dSLionel Sambuc 650*0a6a1f1dSLionel Sambuc.. code-block:: c 651*0a6a1f1dSLionel Sambuc 652*0a6a1f1dSLionel Sambuc int h() __attribute__((overloadable)); // error: h does not have a prototype 653*0a6a1f1dSLionel Sambuc 654*0a6a1f1dSLionel SambucHowever, ``overloadable`` functions are allowed to use a ellipsis even if there 655*0a6a1f1dSLionel Sambucare no named parameters (as is permitted in C++). This feature is particularly 656*0a6a1f1dSLionel Sambucuseful when combined with the ``unavailable`` attribute: 657*0a6a1f1dSLionel Sambuc 658*0a6a1f1dSLionel Sambuc.. code-block:: c++ 659*0a6a1f1dSLionel Sambuc 660*0a6a1f1dSLionel Sambuc void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error 661*0a6a1f1dSLionel Sambuc 662*0a6a1f1dSLionel SambucFunctions declared with the ``overloadable`` attribute have their names mangled 663*0a6a1f1dSLionel Sambucaccording to the same rules as C++ function names. For example, the three 664*0a6a1f1dSLionel Sambuc``tgsin`` functions in our motivating example get the mangled names 665*0a6a1f1dSLionel Sambuc``_Z5tgsinf``, ``_Z5tgsind``, and ``_Z5tgsine``, respectively. There are two 666*0a6a1f1dSLionel Sambuccaveats to this use of name mangling: 667*0a6a1f1dSLionel Sambuc 668*0a6a1f1dSLionel Sambuc* Future versions of Clang may change the name mangling of functions overloaded 669*0a6a1f1dSLionel Sambuc in C, so you should not depend on an specific mangling. To be completely 670*0a6a1f1dSLionel Sambuc safe, we strongly urge the use of ``static inline`` with ``overloadable`` 671*0a6a1f1dSLionel Sambuc functions. 672*0a6a1f1dSLionel Sambuc 673*0a6a1f1dSLionel Sambuc* The ``overloadable`` attribute has almost no meaning when used in C++, 674*0a6a1f1dSLionel Sambuc because names will already be mangled and functions are already overloadable. 675*0a6a1f1dSLionel Sambuc However, when an ``overloadable`` function occurs within an ``extern "C"`` 676*0a6a1f1dSLionel Sambuc linkage specification, it's name *will* be mangled in the same way as it 677*0a6a1f1dSLionel Sambuc would in C. 678*0a6a1f1dSLionel Sambuc 679*0a6a1f1dSLionel SambucQuery for this feature with ``__has_extension(attribute_overloadable)``. 680*0a6a1f1dSLionel Sambuc 681*0a6a1f1dSLionel Sambuc 682*0a6a1f1dSLionel Sambucpcs (gnu::pcs) 683*0a6a1f1dSLionel Sambuc-------------- 684*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 685*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 686*0a6a1f1dSLionel Sambuc 687*0a6a1f1dSLionel Sambuc "X","X","","" 688*0a6a1f1dSLionel Sambuc 689*0a6a1f1dSLionel SambucOn ARM targets, this can attribute can be used to select calling conventions, 690*0a6a1f1dSLionel Sambucsimilar to ``stdcall`` on x86. Valid parameter values are "aapcs" and 691*0a6a1f1dSLionel Sambuc"aapcs-vfp". 692*0a6a1f1dSLionel Sambuc 693*0a6a1f1dSLionel Sambuc 694*0a6a1f1dSLionel Sambucrelease_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability) 695*0a6a1f1dSLionel Sambuc----------------------------------------------------------------------------------------------------------- 696*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 697*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 698*0a6a1f1dSLionel Sambuc 699*0a6a1f1dSLionel Sambuc "X","X","","" 700*0a6a1f1dSLionel Sambuc 701*0a6a1f1dSLionel SambucMarks a function as releasing a capability. 702*0a6a1f1dSLionel Sambuc 703*0a6a1f1dSLionel Sambuc 704*0a6a1f1dSLionel Sambuctry_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability) 705*0a6a1f1dSLionel Sambuc--------------------------------------------------------------------------------------------------------------------------- 706*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 707*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 708*0a6a1f1dSLionel Sambuc 709*0a6a1f1dSLionel Sambuc "X","X","","" 710*0a6a1f1dSLionel Sambuc 711*0a6a1f1dSLionel SambucMarks a function that attempts to acquire a capability. This function may fail to 712*0a6a1f1dSLionel Sambucactually acquire the capability; they accept a Boolean value determining 713*0a6a1f1dSLionel Sambucwhether acquiring the capability means success (true), or failing to acquire 714*0a6a1f1dSLionel Sambucthe capability means success (false). 715*0a6a1f1dSLionel Sambuc 716*0a6a1f1dSLionel Sambuc 717*0a6a1f1dSLionel SambucVariable Attributes 718*0a6a1f1dSLionel Sambuc=================== 719*0a6a1f1dSLionel Sambuc 720*0a6a1f1dSLionel Sambuc 721*0a6a1f1dSLionel Sambucsection (gnu::section, __declspec(allocate)) 722*0a6a1f1dSLionel Sambuc-------------------------------------------- 723*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 724*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 725*0a6a1f1dSLionel Sambuc 726*0a6a1f1dSLionel Sambuc "X","X","X","" 727*0a6a1f1dSLionel Sambuc 728*0a6a1f1dSLionel SambucThe ``section`` attribute allows you to specify a specific section a 729*0a6a1f1dSLionel Sambucglobal variable or function should be in after translation. 730*0a6a1f1dSLionel Sambuc 731*0a6a1f1dSLionel Sambuc 732*0a6a1f1dSLionel Sambuctls_model (gnu::tls_model) 733*0a6a1f1dSLionel Sambuc-------------------------- 734*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 735*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 736*0a6a1f1dSLionel Sambuc 737*0a6a1f1dSLionel Sambuc "X","X","","" 738*0a6a1f1dSLionel Sambuc 739*0a6a1f1dSLionel SambucThe ``tls_model`` attribute allows you to specify which thread-local storage 740*0a6a1f1dSLionel Sambucmodel to use. It accepts the following strings: 741*0a6a1f1dSLionel Sambuc 742*0a6a1f1dSLionel Sambuc* global-dynamic 743*0a6a1f1dSLionel Sambuc* local-dynamic 744*0a6a1f1dSLionel Sambuc* initial-exec 745*0a6a1f1dSLionel Sambuc* local-exec 746*0a6a1f1dSLionel Sambuc 747*0a6a1f1dSLionel SambucTLS models are mutually exclusive. 748*0a6a1f1dSLionel Sambuc 749*0a6a1f1dSLionel Sambuc 750*0a6a1f1dSLionel Sambucthread 751*0a6a1f1dSLionel Sambuc------ 752*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 753*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 754*0a6a1f1dSLionel Sambuc 755*0a6a1f1dSLionel Sambuc "","","X","" 756*0a6a1f1dSLionel Sambuc 757*0a6a1f1dSLionel SambucThe ``__declspec(thread)`` attribute declares a variable with thread local 758*0a6a1f1dSLionel Sambucstorage. It is available under the ``-fms-extensions`` flag for MSVC 759*0a6a1f1dSLionel Sambuccompatibility. Documentation for the Visual C++ attribute is available on MSDN_. 760*0a6a1f1dSLionel Sambuc 761*0a6a1f1dSLionel Sambuc.. _MSDN: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx 762*0a6a1f1dSLionel Sambuc 763*0a6a1f1dSLionel SambucIn Clang, ``__declspec(thread)`` is generally equivalent in functionality to the 764*0a6a1f1dSLionel SambucGNU ``__thread`` keyword. The variable must not have a destructor and must have 765*0a6a1f1dSLionel Sambuca constant initializer, if any. The attribute only applies to variables 766*0a6a1f1dSLionel Sambucdeclared with static storage duration, such as globals, class static data 767*0a6a1f1dSLionel Sambucmembers, and static locals. 768*0a6a1f1dSLionel Sambuc 769*0a6a1f1dSLionel Sambuc 770*0a6a1f1dSLionel SambucType Attributes 771*0a6a1f1dSLionel Sambuc=============== 772*0a6a1f1dSLionel Sambuc 773*0a6a1f1dSLionel Sambuc 774*0a6a1f1dSLionel Sambuc__single_inhertiance, __multiple_inheritance, __virtual_inheritance 775*0a6a1f1dSLionel Sambuc------------------------------------------------------------------- 776*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 777*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 778*0a6a1f1dSLionel Sambuc 779*0a6a1f1dSLionel Sambuc "","","","X" 780*0a6a1f1dSLionel Sambuc 781*0a6a1f1dSLionel SambucThis collection of keywords is enabled under ``-fms-extensions`` and controls 782*0a6a1f1dSLionel Sambucthe pointer-to-member representation used on ``*-*-win32`` targets. 783*0a6a1f1dSLionel Sambuc 784*0a6a1f1dSLionel SambucThe ``*-*-win32`` targets utilize a pointer-to-member representation which 785*0a6a1f1dSLionel Sambucvaries in size and alignment depending on the definition of the underlying 786*0a6a1f1dSLionel Sambucclass. 787*0a6a1f1dSLionel Sambuc 788*0a6a1f1dSLionel SambucHowever, this is problematic when a forward declaration is only available and 789*0a6a1f1dSLionel Sambucno definition has been made yet. In such cases, Clang is forced to utilize the 790*0a6a1f1dSLionel Sambucmost general representation that is available to it. 791*0a6a1f1dSLionel Sambuc 792*0a6a1f1dSLionel SambucThese keywords make it possible to use a pointer-to-member representation other 793*0a6a1f1dSLionel Sambucthan the most general one regardless of whether or not the definition will ever 794*0a6a1f1dSLionel Sambucbe present in the current translation unit. 795*0a6a1f1dSLionel Sambuc 796*0a6a1f1dSLionel SambucThis family of keywords belong between the ``class-key`` and ``class-name``: 797*0a6a1f1dSLionel Sambuc 798*0a6a1f1dSLionel Sambuc.. code-block:: c++ 799*0a6a1f1dSLionel Sambuc 800*0a6a1f1dSLionel Sambuc struct __single_inheritance S; 801*0a6a1f1dSLionel Sambuc int S::*i; 802*0a6a1f1dSLionel Sambuc struct S {}; 803*0a6a1f1dSLionel Sambuc 804*0a6a1f1dSLionel SambucThis keyword can be applied to class templates but only has an effect when used 805*0a6a1f1dSLionel Sambucon full specializations: 806*0a6a1f1dSLionel Sambuc 807*0a6a1f1dSLionel Sambuc.. code-block:: c++ 808*0a6a1f1dSLionel Sambuc 809*0a6a1f1dSLionel Sambuc template <typename T, typename U> struct __single_inheritance A; // warning: inheritance model ignored on primary template 810*0a6a1f1dSLionel Sambuc template <typename T> struct __multiple_inheritance A<T, T>; // warning: inheritance model ignored on partial specialization 811*0a6a1f1dSLionel Sambuc template <> struct __single_inheritance A<int, float>; 812*0a6a1f1dSLionel Sambuc 813*0a6a1f1dSLionel SambucNote that choosing an inheritance model less general than strictly necessary is 814*0a6a1f1dSLionel Sambucan error: 815*0a6a1f1dSLionel Sambuc 816*0a6a1f1dSLionel Sambuc.. code-block:: c++ 817*0a6a1f1dSLionel Sambuc 818*0a6a1f1dSLionel Sambuc struct __multiple_inheritance S; // error: inheritance model does not match definition 819*0a6a1f1dSLionel Sambuc int S::*i; 820*0a6a1f1dSLionel Sambuc struct S {}; 821*0a6a1f1dSLionel Sambuc 822*0a6a1f1dSLionel Sambuc 823*0a6a1f1dSLionel SambucStatement Attributes 824*0a6a1f1dSLionel Sambuc==================== 825*0a6a1f1dSLionel Sambuc 826*0a6a1f1dSLionel Sambuc 827*0a6a1f1dSLionel Sambucfallthrough (clang::fallthrough) 828*0a6a1f1dSLionel Sambuc-------------------------------- 829*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 830*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 831*0a6a1f1dSLionel Sambuc 832*0a6a1f1dSLionel Sambuc "","X","","" 833*0a6a1f1dSLionel Sambuc 834*0a6a1f1dSLionel SambucThe ``clang::fallthrough`` attribute is used along with the 835*0a6a1f1dSLionel Sambuc``-Wimplicit-fallthrough`` argument to annotate intentional fall-through 836*0a6a1f1dSLionel Sambucbetween switch labels. It can only be applied to a null statement placed at a 837*0a6a1f1dSLionel Sambucpoint of execution between any statement and the next switch label. It is 838*0a6a1f1dSLionel Sambuccommon to mark these places with a specific comment, but this attribute is 839*0a6a1f1dSLionel Sambucmeant to replace comments with a more strict annotation, which can be checked 840*0a6a1f1dSLionel Sambucby the compiler. This attribute doesn't change semantics of the code and can 841*0a6a1f1dSLionel Sambucbe used wherever an intended fall-through occurs. It is designed to mimic 842*0a6a1f1dSLionel Sambuccontrol-flow statements like ``break;``, so it can be placed in most places 843*0a6a1f1dSLionel Sambucwhere ``break;`` can, but only if there are no statements on the execution path 844*0a6a1f1dSLionel Sambucbetween it and the next switch label. 845*0a6a1f1dSLionel Sambuc 846*0a6a1f1dSLionel SambucHere is an example: 847*0a6a1f1dSLionel Sambuc 848*0a6a1f1dSLionel Sambuc.. code-block:: c++ 849*0a6a1f1dSLionel Sambuc 850*0a6a1f1dSLionel Sambuc // compile with -Wimplicit-fallthrough 851*0a6a1f1dSLionel Sambuc switch (n) { 852*0a6a1f1dSLionel Sambuc case 22: 853*0a6a1f1dSLionel Sambuc case 33: // no warning: no statements between case labels 854*0a6a1f1dSLionel Sambuc f(); 855*0a6a1f1dSLionel Sambuc case 44: // warning: unannotated fall-through 856*0a6a1f1dSLionel Sambuc g(); 857*0a6a1f1dSLionel Sambuc [[clang::fallthrough]]; 858*0a6a1f1dSLionel Sambuc case 55: // no warning 859*0a6a1f1dSLionel Sambuc if (x) { 860*0a6a1f1dSLionel Sambuc h(); 861*0a6a1f1dSLionel Sambuc break; 862*0a6a1f1dSLionel Sambuc } 863*0a6a1f1dSLionel Sambuc else { 864*0a6a1f1dSLionel Sambuc i(); 865*0a6a1f1dSLionel Sambuc [[clang::fallthrough]]; 866*0a6a1f1dSLionel Sambuc } 867*0a6a1f1dSLionel Sambuc case 66: // no warning 868*0a6a1f1dSLionel Sambuc p(); 869*0a6a1f1dSLionel Sambuc [[clang::fallthrough]]; // warning: fallthrough annotation does not 870*0a6a1f1dSLionel Sambuc // directly precede case label 871*0a6a1f1dSLionel Sambuc q(); 872*0a6a1f1dSLionel Sambuc case 77: // warning: unannotated fall-through 873*0a6a1f1dSLionel Sambuc r(); 874*0a6a1f1dSLionel Sambuc } 875*0a6a1f1dSLionel Sambuc 876*0a6a1f1dSLionel Sambuc 877*0a6a1f1dSLionel SambucConsumed Annotation Checking 878*0a6a1f1dSLionel Sambuc============================ 879*0a6a1f1dSLionel SambucClang supports additional attributes for checking basic resource management 880*0a6a1f1dSLionel Sambucproperties, specifically for unique objects that have a single owning reference. 881*0a6a1f1dSLionel SambucThe following attributes are currently supported, although **the implementation 882*0a6a1f1dSLionel Sambucfor these annotations is currently in development and are subject to change.** 883*0a6a1f1dSLionel Sambuc 884*0a6a1f1dSLionel Sambuccallable_when 885*0a6a1f1dSLionel Sambuc------------- 886*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 887*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 888*0a6a1f1dSLionel Sambuc 889*0a6a1f1dSLionel Sambuc "X","","","" 890*0a6a1f1dSLionel Sambuc 891*0a6a1f1dSLionel SambucUse ``__attribute__((callable_when(...)))`` to indicate what states a method 892*0a6a1f1dSLionel Sambucmay be called in. Valid states are unconsumed, consumed, or unknown. Each 893*0a6a1f1dSLionel Sambucargument to this attribute must be a quoted string. E.g.: 894*0a6a1f1dSLionel Sambuc 895*0a6a1f1dSLionel Sambuc``__attribute__((callable_when("unconsumed", "unknown")))`` 896*0a6a1f1dSLionel Sambuc 897*0a6a1f1dSLionel Sambuc 898*0a6a1f1dSLionel Sambucconsumable 899*0a6a1f1dSLionel Sambuc---------- 900*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 901*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 902*0a6a1f1dSLionel Sambuc 903*0a6a1f1dSLionel Sambuc "X","","","" 904*0a6a1f1dSLionel Sambuc 905*0a6a1f1dSLionel SambucEach ``class`` that uses any of the typestate annotations must first be marked 906*0a6a1f1dSLionel Sambucusing the ``consumable`` attribute. Failure to do so will result in a warning. 907*0a6a1f1dSLionel Sambuc 908*0a6a1f1dSLionel SambucThis attribute accepts a single parameter that must be one of the following: 909*0a6a1f1dSLionel Sambuc``unknown``, ``consumed``, or ``unconsumed``. 910*0a6a1f1dSLionel Sambuc 911*0a6a1f1dSLionel Sambuc 912*0a6a1f1dSLionel Sambucparam_typestate 913*0a6a1f1dSLionel Sambuc--------------- 914*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 915*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 916*0a6a1f1dSLionel Sambuc 917*0a6a1f1dSLionel Sambuc "X","","","" 918*0a6a1f1dSLionel Sambuc 919*0a6a1f1dSLionel SambucThis attribute specifies expectations about function parameters. Calls to an 920*0a6a1f1dSLionel Sambucfunction with annotated parameters will issue a warning if the corresponding 921*0a6a1f1dSLionel Sambucargument isn't in the expected state. The attribute is also used to set the 922*0a6a1f1dSLionel Sambucinitial state of the parameter when analyzing the function's body. 923*0a6a1f1dSLionel Sambuc 924*0a6a1f1dSLionel Sambuc 925*0a6a1f1dSLionel Sambucreturn_typestate 926*0a6a1f1dSLionel Sambuc---------------- 927*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 928*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 929*0a6a1f1dSLionel Sambuc 930*0a6a1f1dSLionel Sambuc "X","","","" 931*0a6a1f1dSLionel Sambuc 932*0a6a1f1dSLionel SambucThe ``return_typestate`` attribute can be applied to functions or parameters. 933*0a6a1f1dSLionel SambucWhen applied to a function the attribute specifies the state of the returned 934*0a6a1f1dSLionel Sambucvalue. The function's body is checked to ensure that it always returns a value 935*0a6a1f1dSLionel Sambucin the specified state. On the caller side, values returned by the annotated 936*0a6a1f1dSLionel Sambucfunction are initialized to the given state. 937*0a6a1f1dSLionel Sambuc 938*0a6a1f1dSLionel SambucWhen applied to a function parameter it modifies the state of an argument after 939*0a6a1f1dSLionel Sambuca call to the function returns. The function's body is checked to ensure that 940*0a6a1f1dSLionel Sambucthe parameter is in the expected state before returning. 941*0a6a1f1dSLionel Sambuc 942*0a6a1f1dSLionel Sambuc 943*0a6a1f1dSLionel Sambucset_typestate 944*0a6a1f1dSLionel Sambuc------------- 945*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 946*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 947*0a6a1f1dSLionel Sambuc 948*0a6a1f1dSLionel Sambuc "X","","","" 949*0a6a1f1dSLionel Sambuc 950*0a6a1f1dSLionel SambucAnnotate methods that transition an object into a new state with 951*0a6a1f1dSLionel Sambuc``__attribute__((set_typestate(new_state)))``. The new new state must be 952*0a6a1f1dSLionel Sambucunconsumed, consumed, or unknown. 953*0a6a1f1dSLionel Sambuc 954*0a6a1f1dSLionel Sambuc 955*0a6a1f1dSLionel Sambuctest_typestate 956*0a6a1f1dSLionel Sambuc-------------- 957*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 958*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 959*0a6a1f1dSLionel Sambuc 960*0a6a1f1dSLionel Sambuc "X","","","" 961*0a6a1f1dSLionel Sambuc 962*0a6a1f1dSLionel SambucUse ``__attribute__((test_typestate(tested_state)))`` to indicate that a method 963*0a6a1f1dSLionel Sambucreturns true if the object is in the specified state.. 964*0a6a1f1dSLionel Sambuc 965*0a6a1f1dSLionel Sambuc 966*0a6a1f1dSLionel SambucType Safety Checking 967*0a6a1f1dSLionel Sambuc==================== 968*0a6a1f1dSLionel SambucClang supports additional attributes to enable checking type safety properties 969*0a6a1f1dSLionel Sambucthat can't be enforced by the C type system. Use cases include: 970*0a6a1f1dSLionel Sambuc 971*0a6a1f1dSLionel Sambuc* MPI library implementations, where these attributes enable checking that 972*0a6a1f1dSLionel Sambuc the buffer type matches the passed ``MPI_Datatype``; 973*0a6a1f1dSLionel Sambuc* for HDF5 library there is a similar use case to MPI; 974*0a6a1f1dSLionel Sambuc* checking types of variadic functions' arguments for functions like 975*0a6a1f1dSLionel Sambuc ``fcntl()`` and ``ioctl()``. 976*0a6a1f1dSLionel Sambuc 977*0a6a1f1dSLionel SambucYou can detect support for these attributes with ``__has_attribute()``. For 978*0a6a1f1dSLionel Sambucexample: 979*0a6a1f1dSLionel Sambuc 980*0a6a1f1dSLionel Sambuc.. code-block:: c++ 981*0a6a1f1dSLionel Sambuc 982*0a6a1f1dSLionel Sambuc #if defined(__has_attribute) 983*0a6a1f1dSLionel Sambuc # if __has_attribute(argument_with_type_tag) && \ 984*0a6a1f1dSLionel Sambuc __has_attribute(pointer_with_type_tag) && \ 985*0a6a1f1dSLionel Sambuc __has_attribute(type_tag_for_datatype) 986*0a6a1f1dSLionel Sambuc # define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx))) 987*0a6a1f1dSLionel Sambuc /* ... other macros ... */ 988*0a6a1f1dSLionel Sambuc # endif 989*0a6a1f1dSLionel Sambuc #endif 990*0a6a1f1dSLionel Sambuc 991*0a6a1f1dSLionel Sambuc #if !defined(ATTR_MPI_PWT) 992*0a6a1f1dSLionel Sambuc # define ATTR_MPI_PWT(buffer_idx, type_idx) 993*0a6a1f1dSLionel Sambuc #endif 994*0a6a1f1dSLionel Sambuc 995*0a6a1f1dSLionel Sambuc int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) 996*0a6a1f1dSLionel Sambuc ATTR_MPI_PWT(1,3); 997*0a6a1f1dSLionel Sambuc 998*0a6a1f1dSLionel Sambucargument_with_type_tag 999*0a6a1f1dSLionel Sambuc---------------------- 1000*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 1001*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 1002*0a6a1f1dSLionel Sambuc 1003*0a6a1f1dSLionel Sambuc "X","","","" 1004*0a6a1f1dSLionel Sambuc 1005*0a6a1f1dSLionel SambucUse ``__attribute__((argument_with_type_tag(arg_kind, arg_idx, 1006*0a6a1f1dSLionel Sambuctype_tag_idx)))`` on a function declaration to specify that the function 1007*0a6a1f1dSLionel Sambucaccepts a type tag that determines the type of some other argument. 1008*0a6a1f1dSLionel Sambuc``arg_kind`` is an identifier that should be used when annotating all 1009*0a6a1f1dSLionel Sambucapplicable type tags. 1010*0a6a1f1dSLionel Sambuc 1011*0a6a1f1dSLionel SambucThis attribute is primarily useful for checking arguments of variadic functions 1012*0a6a1f1dSLionel Sambuc(``pointer_with_type_tag`` can be used in most non-variadic cases). 1013*0a6a1f1dSLionel Sambuc 1014*0a6a1f1dSLionel SambucFor example: 1015*0a6a1f1dSLionel Sambuc 1016*0a6a1f1dSLionel Sambuc.. code-block:: c++ 1017*0a6a1f1dSLionel Sambuc 1018*0a6a1f1dSLionel Sambuc int fcntl(int fd, int cmd, ...) 1019*0a6a1f1dSLionel Sambuc __attribute__(( argument_with_type_tag(fcntl,3,2) )); 1020*0a6a1f1dSLionel Sambuc 1021*0a6a1f1dSLionel Sambuc 1022*0a6a1f1dSLionel Sambucpointer_with_type_tag 1023*0a6a1f1dSLionel Sambuc--------------------- 1024*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 1025*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 1026*0a6a1f1dSLionel Sambuc 1027*0a6a1f1dSLionel Sambuc "X","","","" 1028*0a6a1f1dSLionel Sambuc 1029*0a6a1f1dSLionel SambucUse ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))`` 1030*0a6a1f1dSLionel Sambucon a function declaration to specify that the function accepts a type tag that 1031*0a6a1f1dSLionel Sambucdetermines the pointee type of some other pointer argument. 1032*0a6a1f1dSLionel Sambuc 1033*0a6a1f1dSLionel SambucFor example: 1034*0a6a1f1dSLionel Sambuc 1035*0a6a1f1dSLionel Sambuc.. code-block:: c++ 1036*0a6a1f1dSLionel Sambuc 1037*0a6a1f1dSLionel Sambuc int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) 1038*0a6a1f1dSLionel Sambuc __attribute__(( pointer_with_type_tag(mpi,1,3) )); 1039*0a6a1f1dSLionel Sambuc 1040*0a6a1f1dSLionel Sambuc 1041*0a6a1f1dSLionel Sambuctype_tag_for_datatype 1042*0a6a1f1dSLionel Sambuc--------------------- 1043*0a6a1f1dSLionel Sambuc.. csv-table:: Supported Syntaxes 1044*0a6a1f1dSLionel Sambuc :header: "GNU", "C++11", "__declspec", "Keyword" 1045*0a6a1f1dSLionel Sambuc 1046*0a6a1f1dSLionel Sambuc "X","","","" 1047*0a6a1f1dSLionel Sambuc 1048*0a6a1f1dSLionel SambucClang supports annotating type tags of two forms. 1049*0a6a1f1dSLionel Sambuc 1050*0a6a1f1dSLionel Sambuc* **Type tag that is an expression containing a reference to some declared 1051*0a6a1f1dSLionel Sambuc identifier.** Use ``__attribute__((type_tag_for_datatype(kind, type)))`` on a 1052*0a6a1f1dSLionel Sambuc declaration with that identifier: 1053*0a6a1f1dSLionel Sambuc 1054*0a6a1f1dSLionel Sambuc .. code-block:: c++ 1055*0a6a1f1dSLionel Sambuc 1056*0a6a1f1dSLionel Sambuc extern struct mpi_datatype mpi_datatype_int 1057*0a6a1f1dSLionel Sambuc __attribute__(( type_tag_for_datatype(mpi,int) )); 1058*0a6a1f1dSLionel Sambuc #define MPI_INT ((MPI_Datatype) &mpi_datatype_int) 1059*0a6a1f1dSLionel Sambuc 1060*0a6a1f1dSLionel Sambuc* **Type tag that is an integral literal.** Introduce a ``static const`` 1061*0a6a1f1dSLionel Sambuc variable with a corresponding initializer value and attach 1062*0a6a1f1dSLionel Sambuc ``__attribute__((type_tag_for_datatype(kind, type)))`` on that declaration, 1063*0a6a1f1dSLionel Sambuc for example: 1064*0a6a1f1dSLionel Sambuc 1065*0a6a1f1dSLionel Sambuc .. code-block:: c++ 1066*0a6a1f1dSLionel Sambuc 1067*0a6a1f1dSLionel Sambuc #define MPI_INT ((MPI_Datatype) 42) 1068*0a6a1f1dSLionel Sambuc static const MPI_Datatype mpi_datatype_int 1069*0a6a1f1dSLionel Sambuc __attribute__(( type_tag_for_datatype(mpi,int) )) = 42 1070*0a6a1f1dSLionel Sambuc 1071*0a6a1f1dSLionel SambucThe attribute also accepts an optional third argument that determines how the 1072*0a6a1f1dSLionel Sambucexpression is compared to the type tag. There are two supported flags: 1073*0a6a1f1dSLionel Sambuc 1074*0a6a1f1dSLionel Sambuc* ``layout_compatible`` will cause types to be compared according to 1075*0a6a1f1dSLionel Sambuc layout-compatibility rules (C++11 [class.mem] p 17, 18). This is 1076*0a6a1f1dSLionel Sambuc implemented to support annotating types like ``MPI_DOUBLE_INT``. 1077*0a6a1f1dSLionel Sambuc 1078*0a6a1f1dSLionel Sambuc For example: 1079*0a6a1f1dSLionel Sambuc 1080*0a6a1f1dSLionel Sambuc .. code-block:: c++ 1081*0a6a1f1dSLionel Sambuc 1082*0a6a1f1dSLionel Sambuc /* In mpi.h */ 1083*0a6a1f1dSLionel Sambuc struct internal_mpi_double_int { double d; int i; }; 1084*0a6a1f1dSLionel Sambuc extern struct mpi_datatype mpi_datatype_double_int 1085*0a6a1f1dSLionel Sambuc __attribute__(( type_tag_for_datatype(mpi, struct internal_mpi_double_int, layout_compatible) )); 1086*0a6a1f1dSLionel Sambuc 1087*0a6a1f1dSLionel Sambuc #define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int) 1088*0a6a1f1dSLionel Sambuc 1089*0a6a1f1dSLionel Sambuc /* In user code */ 1090*0a6a1f1dSLionel Sambuc struct my_pair { double a; int b; }; 1091*0a6a1f1dSLionel Sambuc struct my_pair *buffer; 1092*0a6a1f1dSLionel Sambuc MPI_Send(buffer, 1, MPI_DOUBLE_INT /*, ... */); // no warning 1093*0a6a1f1dSLionel Sambuc 1094*0a6a1f1dSLionel Sambuc struct my_int_pair { int a; int b; } 1095*0a6a1f1dSLionel Sambuc struct my_int_pair *buffer2; 1096*0a6a1f1dSLionel Sambuc MPI_Send(buffer2, 1, MPI_DOUBLE_INT /*, ... */); // warning: actual buffer element 1097*0a6a1f1dSLionel Sambuc // type 'struct my_int_pair' 1098*0a6a1f1dSLionel Sambuc // doesn't match specified MPI_Datatype 1099*0a6a1f1dSLionel Sambuc 1100*0a6a1f1dSLionel Sambuc* ``must_be_null`` specifies that the expression should be a null pointer 1101*0a6a1f1dSLionel Sambuc constant, for example: 1102*0a6a1f1dSLionel Sambuc 1103*0a6a1f1dSLionel Sambuc .. code-block:: c++ 1104*0a6a1f1dSLionel Sambuc 1105*0a6a1f1dSLionel Sambuc /* In mpi.h */ 1106*0a6a1f1dSLionel Sambuc extern struct mpi_datatype mpi_datatype_null 1107*0a6a1f1dSLionel Sambuc __attribute__(( type_tag_for_datatype(mpi, void, must_be_null) )); 1108*0a6a1f1dSLionel Sambuc 1109*0a6a1f1dSLionel Sambuc #define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null) 1110*0a6a1f1dSLionel Sambuc 1111*0a6a1f1dSLionel Sambuc /* In user code */ 1112*0a6a1f1dSLionel Sambuc MPI_Send(buffer, 1, MPI_DATATYPE_NULL /*, ... */); // warning: MPI_DATATYPE_NULL 1113*0a6a1f1dSLionel Sambuc // was specified but buffer 1114*0a6a1f1dSLionel Sambuc // is not a null pointer 1115*0a6a1f1dSLionel Sambuc 1116*0a6a1f1dSLionel Sambuc 1117