1e5dd7070Spatrick//==--- AttrDocs.td - Attribute documentation ----------------------------===// 2e5dd7070Spatrick// 3e5dd7070Spatrick// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e5dd7070Spatrick// See https://llvm.org/LICENSE.txt for license information. 5e5dd7070Spatrick// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e5dd7070Spatrick// 7e5dd7070Spatrick//===---------------------------------------------------------------------===// 8e5dd7070Spatrick 9e5dd7070Spatrick// To test that the documentation builds cleanly, you must run clang-tblgen to 10e5dd7070Spatrick// convert the .td file into a .rst file, and then run sphinx to convert the 11e5dd7070Spatrick// .rst file into an HTML file. After completing testing, you should revert the 12e5dd7070Spatrick// generated .rst file so that the modified version does not get checked in to 13e5dd7070Spatrick// version control. 14e5dd7070Spatrick// 15e5dd7070Spatrick// To run clang-tblgen to generate the .rst file: 16e5dd7070Spatrick// clang-tblgen -gen-attr-docs -I <root>/llvm/tools/clang/include 17e5dd7070Spatrick// <root>/llvm/tools/clang/include/clang/Basic/Attr.td -o 18e5dd7070Spatrick// <root>/llvm/tools/clang/docs/AttributeReference.rst 19e5dd7070Spatrick// 20e5dd7070Spatrick// To run sphinx to generate the .html files (note that sphinx-build must be 21e5dd7070Spatrick// available on the PATH): 22e5dd7070Spatrick// Windows (from within the clang\docs directory): 23e5dd7070Spatrick// make.bat html 24e5dd7070Spatrick// Non-Windows (from within the clang\docs directory): 25*12c85518Srobert// sphinx-build -b html _build/html 26e5dd7070Spatrick 27e5dd7070Spatrickdef GlobalDocumentation { 28e5dd7070Spatrick code Intro =[{.. 29e5dd7070Spatrick ------------------------------------------------------------------- 30e5dd7070Spatrick NOTE: This file is automatically generated by running clang-tblgen 31e5dd7070Spatrick -gen-attr-docs. Do not edit this file by hand!! 32e5dd7070Spatrick ------------------------------------------------------------------- 33e5dd7070Spatrick 34e5dd7070Spatrick=================== 35e5dd7070SpatrickAttributes in Clang 36e5dd7070Spatrick=================== 37e5dd7070Spatrick.. contents:: 38e5dd7070Spatrick :local: 39e5dd7070Spatrick 40e5dd7070Spatrick.. |br| raw:: html 41e5dd7070Spatrick 42e5dd7070Spatrick <br/> 43e5dd7070Spatrick 44e5dd7070SpatrickIntroduction 45e5dd7070Spatrick============ 46e5dd7070Spatrick 47e5dd7070SpatrickThis page lists the attributes currently supported by Clang. 48e5dd7070Spatrick}]; 49e5dd7070Spatrick} 50e5dd7070Spatrick 51e5dd7070Spatrickdef SectionDocs : Documentation { 52e5dd7070Spatrick let Category = DocCatVariable; 53e5dd7070Spatrick let Content = [{ 54e5dd7070SpatrickThe ``section`` attribute allows you to specify a specific section a 55e5dd7070Spatrickglobal variable or function should be in after translation. 56e5dd7070Spatrick }]; 57e5dd7070Spatrick let Heading = "section, __declspec(allocate)"; 58e5dd7070Spatrick} 59e5dd7070Spatrick 60a9ac8606Spatrickdef UsedDocs : Documentation { 61a9ac8606Spatrick let Category = DocCatFunction; 62a9ac8606Spatrick let Content = [{ 63a9ac8606SpatrickThis attribute, when attached to a function or variable definition, indicates 64a9ac8606Spatrickthat there may be references to the entity which are not apparent in the source 65a9ac8606Spatrickcode. For example, it may be referenced from inline ``asm``, or it may be 66a9ac8606Spatrickfound through a dynamic symbol or section lookup. 67a9ac8606Spatrick 68a9ac8606SpatrickThe compiler must emit the definition even if it appears to be unused, and it 69a9ac8606Spatrickmust not apply optimizations which depend on fully understanding how the entity 70a9ac8606Spatrickis used. 71a9ac8606Spatrick 72a9ac8606SpatrickWhether this attribute has any effect on the linker depends on the target and 73a9ac8606Spatrickthe linker. Most linkers support the feature of section garbage collection 74a9ac8606Spatrick(``--gc-sections``), also known as "dead stripping" (``ld64 -dead_strip``) or 75a9ac8606Spatrickdiscarding unreferenced sections (``link.exe /OPT:REF``). On COFF and Mach-O 76a9ac8606Spatricktargets (Windows and Apple platforms), the `used` attribute prevents symbols 77a9ac8606Spatrickfrom being removed by linker section GC. On ELF targets, it has no effect on its 78a9ac8606Spatrickown, and the linker may remove the definition if it is not otherwise referenced. 79a9ac8606SpatrickThis linker GC can be avoided by also adding the ``retain`` attribute. Note 80a9ac8606Spatrickthat ``retain`` requires special support from the linker; see that attribute's 81a9ac8606Spatrickdocumentation for further information. 82a9ac8606Spatrick }]; 83a9ac8606Spatrick} 84a9ac8606Spatrick 85a9ac8606Spatrickdef RetainDocs : Documentation { 86a9ac8606Spatrick let Category = DocCatFunction; 87a9ac8606Spatrick let Content = [{ 88a9ac8606SpatrickThis attribute, when attached to a function or variable definition, prevents 89a9ac8606Spatricksection garbage collection in the linker. It does not prevent other discard 90a9ac8606Spatrickmechanisms, such as archive member selection, and COMDAT group resolution. 91a9ac8606Spatrick 92a9ac8606SpatrickIf the compiler does not emit the definition, e.g. because it was not used in 93a9ac8606Spatrickthe translation unit or the compiler was able to eliminate all of the uses, 94a9ac8606Spatrickthis attribute has no effect. This attribute is typically combined with the 95a9ac8606Spatrick``used`` attribute to force the definition to be emitted and preserved into the 96a9ac8606Spatrickfinal linked image. 97a9ac8606Spatrick 98a9ac8606SpatrickThis attribute is only necessary on ELF targets; other targets prevent section 99a9ac8606Spatrickgarbage collection by the linker when using the ``used`` attribute alone. 100a9ac8606SpatrickUsing the attributes together should result in consistent behavior across 101a9ac8606Spatricktargets. 102a9ac8606Spatrick 103a9ac8606SpatrickThis attribute requires the linker to support the ``SHF_GNU_RETAIN`` extension. 104a9ac8606SpatrickThis support is available in GNU ``ld`` and ``gold`` as of binutils 2.36, as 105a9ac8606Spatrickwell as in ``ld.lld`` 13. 106a9ac8606Spatrick }]; 107a9ac8606Spatrick} 108a9ac8606Spatrick 109ec727ea7Spatrickdef InitPriorityDocs : Documentation { 110ec727ea7Spatrick let Category = DocCatVariable; 111ec727ea7Spatrick let Content = [{ 112ec727ea7SpatrickIn C++, the order in which global variables are initialized across translation 113ec727ea7Spatrickunits is unspecified, unlike the ordering within a single translation unit. The 114ec727ea7Spatrick``init_priority`` attribute allows you to specify a relative ordering for the 115ec727ea7Spatrickinitialization of objects declared at namespace scope in C++. The priority is 116ec727ea7Spatrickgiven as an integer constant expression between 101 and 65535 (inclusive). 117ec727ea7SpatrickPriorities outside of that range are reserved for use by the implementation. A 118ec727ea7Spatricklower value indicates a higher priority of initialization. Note that only the 119ec727ea7Spatrickrelative ordering of values is important. For example: 120ec727ea7Spatrick 121ec727ea7Spatrick.. code-block:: c++ 122ec727ea7Spatrick 123ec727ea7Spatrick struct SomeType { SomeType(); }; 124ec727ea7Spatrick __attribute__((init_priority(200))) SomeType Obj1; 125ec727ea7Spatrick __attribute__((init_priority(101))) SomeType Obj2; 126ec727ea7Spatrick 127a9ac8606Spatrick``Obj2`` will be initialized *before* ``Obj1`` despite the usual order of 128ec727ea7Spatrickinitialization being the opposite. 129ec727ea7Spatrick 130*12c85518SrobertOn Windows, ``init_seg(compiler)`` is represented with a priority of 200 and 131*12c85518Srobert``init_seg(library)`` is represented with a priority of 400. ``init_seg(user)`` 132*12c85518Srobertuses the default 65535 priority. 133*12c85518Srobert 134ec727ea7SpatrickThis attribute is only supported for C++ and Objective-C++ and is ignored in 135a9ac8606Spatrickother language modes. Currently, this attribute is not implemented on z/OS. 136ec727ea7Spatrick }]; 137ec727ea7Spatrick} 138ec727ea7Spatrick 139e5dd7070Spatrickdef InitSegDocs : Documentation { 140e5dd7070Spatrick let Category = DocCatVariable; 141e5dd7070Spatrick let Content = [{ 142e5dd7070SpatrickThe attribute applied by ``pragma init_seg()`` controls the section into 143e5dd7070Spatrickwhich global initialization function pointers are emitted. It is only 144e5dd7070Spatrickavailable with ``-fms-extensions``. Typically, this function pointer is 145e5dd7070Spatrickemitted into ``.CRT$XCU`` on Windows. The user can change the order of 146e5dd7070Spatrickinitialization by using a different section name with the same 147e5dd7070Spatrick``.CRT$XC`` prefix and a suffix that sorts lexicographically before or 148e5dd7070Spatrickafter the standard ``.CRT$XCU`` sections. See the init_seg_ 149e5dd7070Spatrickdocumentation on MSDN for more information. 150e5dd7070Spatrick 151e5dd7070Spatrick.. _init_seg: http://msdn.microsoft.com/en-us/library/7977wcck(v=vs.110).aspx 152e5dd7070Spatrick }]; 153e5dd7070Spatrick} 154e5dd7070Spatrick 155e5dd7070Spatrickdef TLSModelDocs : Documentation { 156e5dd7070Spatrick let Category = DocCatVariable; 157e5dd7070Spatrick let Content = [{ 158e5dd7070SpatrickThe ``tls_model`` attribute allows you to specify which thread-local storage 159e5dd7070Spatrickmodel to use. It accepts the following strings: 160e5dd7070Spatrick 161e5dd7070Spatrick* global-dynamic 162e5dd7070Spatrick* local-dynamic 163e5dd7070Spatrick* initial-exec 164e5dd7070Spatrick* local-exec 165e5dd7070Spatrick 166e5dd7070SpatrickTLS models are mutually exclusive. 167e5dd7070Spatrick }]; 168e5dd7070Spatrick} 169e5dd7070Spatrick 170e5dd7070Spatrickdef DLLExportDocs : Documentation { 171e5dd7070Spatrick let Category = DocCatVariable; 172e5dd7070Spatrick let Content = [{ 173e5dd7070SpatrickThe ``__declspec(dllexport)`` attribute declares a variable, function, or 174e5dd7070SpatrickObjective-C interface to be exported from the module. It is available under the 175e5dd7070Spatrick``-fdeclspec`` flag for compatibility with various compilers. The primary use 176e5dd7070Spatrickis for COFF object files which explicitly specify what interfaces are available 177e5dd7070Spatrickfor external use. See the dllexport_ documentation on MSDN for more 178e5dd7070Spatrickinformation. 179e5dd7070Spatrick 180e5dd7070Spatrick.. _dllexport: https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx 181e5dd7070Spatrick }]; 182e5dd7070Spatrick} 183e5dd7070Spatrick 184e5dd7070Spatrickdef DLLImportDocs : Documentation { 185e5dd7070Spatrick let Category = DocCatVariable; 186e5dd7070Spatrick let Content = [{ 187e5dd7070SpatrickThe ``__declspec(dllimport)`` attribute declares a variable, function, or 188e5dd7070SpatrickObjective-C interface to be imported from an external module. It is available 189e5dd7070Spatrickunder the ``-fdeclspec`` flag for compatibility with various compilers. The 190e5dd7070Spatrickprimary use is for COFF object files which explicitly specify what interfaces 191e5dd7070Spatrickare imported from external modules. See the dllimport_ documentation on MSDN 192e5dd7070Spatrickfor more information. 193e5dd7070Spatrick 194*12c85518SrobertNote that a dllimport function may still be inlined, if its definition is 195*12c85518Srobertavailable and it doesn't reference any non-dllimport functions or global 196*12c85518Srobertvariables. 197*12c85518Srobert 198e5dd7070Spatrick.. _dllimport: https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx 199e5dd7070Spatrick }]; 200e5dd7070Spatrick} 201e5dd7070Spatrick 202e5dd7070Spatrickdef ThreadDocs : Documentation { 203e5dd7070Spatrick let Category = DocCatVariable; 204e5dd7070Spatrick let Content = [{ 205e5dd7070SpatrickThe ``__declspec(thread)`` attribute declares a variable with thread local 206e5dd7070Spatrickstorage. It is available under the ``-fms-extensions`` flag for MSVC 207e5dd7070Spatrickcompatibility. See the documentation for `__declspec(thread)`_ on MSDN. 208e5dd7070Spatrick 209e5dd7070Spatrick.. _`__declspec(thread)`: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx 210e5dd7070Spatrick 211e5dd7070SpatrickIn Clang, ``__declspec(thread)`` is generally equivalent in functionality to the 212e5dd7070SpatrickGNU ``__thread`` keyword. The variable must not have a destructor and must have 213e5dd7070Spatricka constant initializer, if any. The attribute only applies to variables 214e5dd7070Spatrickdeclared with static storage duration, such as globals, class static data 215e5dd7070Spatrickmembers, and static locals. 216e5dd7070Spatrick }]; 217e5dd7070Spatrick} 218e5dd7070Spatrick 219e5dd7070Spatrickdef NoEscapeDocs : Documentation { 220e5dd7070Spatrick let Category = DocCatVariable; 221e5dd7070Spatrick let Content = [{ 222e5dd7070Spatrick``noescape`` placed on a function parameter of a pointer type is used to inform 223e5dd7070Spatrickthe compiler that the pointer cannot escape: that is, no reference to the object 224e5dd7070Spatrickthe pointer points to that is derived from the parameter value will survive 225e5dd7070Spatrickafter the function returns. Users are responsible for making sure parameters 226ec727ea7Spatrickannotated with ``noescape`` do not actually escape. Calling ``free()`` on such 227ec727ea7Spatricka parameter does not constitute an escape. 228e5dd7070Spatrick 229e5dd7070SpatrickFor example: 230e5dd7070Spatrick 231e5dd7070Spatrick.. code-block:: c 232e5dd7070Spatrick 233e5dd7070Spatrick int *gp; 234e5dd7070Spatrick 235e5dd7070Spatrick void nonescapingFunc(__attribute__((noescape)) int *p) { 236e5dd7070Spatrick *p += 100; // OK. 237e5dd7070Spatrick } 238e5dd7070Spatrick 239e5dd7070Spatrick void escapingFunc(__attribute__((noescape)) int *p) { 240e5dd7070Spatrick gp = p; // Not OK. 241e5dd7070Spatrick } 242e5dd7070Spatrick 243e5dd7070SpatrickAdditionally, when the parameter is a `block pointer 244e5dd7070Spatrick<https://clang.llvm.org/docs/BlockLanguageSpec.html>`, the same restriction 245e5dd7070Spatrickapplies to copies of the block. For example: 246e5dd7070Spatrick 247e5dd7070Spatrick.. code-block:: c 248e5dd7070Spatrick 249e5dd7070Spatrick typedef void (^BlockTy)(); 250e5dd7070Spatrick BlockTy g0, g1; 251e5dd7070Spatrick 252e5dd7070Spatrick void nonescapingFunc(__attribute__((noescape)) BlockTy block) { 253e5dd7070Spatrick block(); // OK. 254e5dd7070Spatrick } 255e5dd7070Spatrick 256e5dd7070Spatrick void escapingFunc(__attribute__((noescape)) BlockTy block) { 257e5dd7070Spatrick g0 = block; // Not OK. 258e5dd7070Spatrick g1 = Block_copy(block); // Not OK either. 259e5dd7070Spatrick } 260e5dd7070Spatrick 261e5dd7070Spatrick }]; 262e5dd7070Spatrick} 263e5dd7070Spatrick 264*12c85518Srobertdef MaybeUndefDocs : Documentation { 265*12c85518Srobert let Category = DocCatVariable; 266*12c85518Srobert let Content = [{ 267*12c85518SrobertThe ``maybe_undef`` attribute can be placed on a function parameter. It indicates 268*12c85518Srobertthat the parameter is allowed to use undef values. It informs the compiler 269*12c85518Srobertto insert a freeze LLVM IR instruction on the function parameter. 270*12c85518SrobertPlease note that this is an attribute that is used as an internal 271*12c85518Srobertimplementation detail and not intended to be used by external users. 272*12c85518Srobert 273*12c85518SrobertIn languages HIP, CUDA etc., some functions have multi-threaded semantics and 274*12c85518Srobertit is enough for only one or some threads to provide defined arguments. 275*12c85518SrobertDepending on semantics, undef arguments in some threads don't produce 276*12c85518Srobertundefined results in the function call. Since, these functions accept undefined 277*12c85518Srobertarguments, ``maybe_undef`` attribute can be placed. 278*12c85518Srobert 279*12c85518SrobertSample usage: 280*12c85518Srobert.. code-block:: c 281*12c85518Srobert 282*12c85518Srobert void maybeundeffunc(int __attribute__((maybe_undef))param); 283*12c85518Srobert }]; 284*12c85518Srobert} 285*12c85518Srobert 286e5dd7070Spatrickdef CarriesDependencyDocs : Documentation { 287e5dd7070Spatrick let Category = DocCatFunction; 288e5dd7070Spatrick let Content = [{ 289e5dd7070SpatrickThe ``carries_dependency`` attribute specifies dependency propagation into and 290e5dd7070Spatrickout of functions. 291e5dd7070Spatrick 292e5dd7070SpatrickWhen specified on a function or Objective-C method, the ``carries_dependency`` 293e5dd7070Spatrickattribute means that the return value carries a dependency out of the function, 294e5dd7070Spatrickso that the implementation need not constrain ordering upon return from that 295e5dd7070Spatrickfunction. Implementations of the function and its caller may choose to preserve 296e5dd7070Spatrickdependencies instead of emitting memory ordering instructions such as fences. 297e5dd7070Spatrick 298e5dd7070SpatrickNote, this attribute does not change the meaning of the program, but may result 299e5dd7070Spatrickin generation of more efficient code. 300e5dd7070Spatrick }]; 301e5dd7070Spatrick} 302e5dd7070Spatrick 303e5dd7070Spatrickdef CPUSpecificCPUDispatchDocs : Documentation { 304e5dd7070Spatrick let Category = DocCatFunction; 305e5dd7070Spatrick let Content = [{ 306e5dd7070SpatrickThe ``cpu_specific`` and ``cpu_dispatch`` attributes are used to define and 307e5dd7070Spatrickresolve multiversioned functions. This form of multiversioning provides a 308e5dd7070Spatrickmechanism for declaring versions across translation units and manually 309e5dd7070Spatrickspecifying the resolved function list. A specified CPU defines a set of minimum 310e5dd7070Spatrickfeatures that are required for the function to be called. The result of this is 311e5dd7070Spatrickthat future processors execute the most restrictive version of the function the 312e5dd7070Spatricknew processor can execute. 313e5dd7070Spatrick 314*12c85518SrobertIn addition, unlike the ICC implementation of this feature, the selection of the 315*12c85518Srobertversion does not consider the manufacturer or microarchitecture of the processor. 316*12c85518SrobertIt tests solely the list of features that are both supported by the specified 317*12c85518Srobertprocessor and present in the compiler-rt library. This can be surprising at times, 318*12c85518Srobertas the runtime processor may be from a completely different manufacturer, as long 319*12c85518Srobertas it supports the same feature set. 320*12c85518Srobert 321*12c85518SrobertThis can additionally be surprising, as some processors are indistringuishable from 322*12c85518Srobertothers based on the list of testable features. When this happens, the variant 323*12c85518Srobertis selected in an unspecified manner. 324*12c85518Srobert 325e5dd7070SpatrickFunction versions are defined with ``cpu_specific``, which takes one or more CPU 326e5dd7070Spatricknames as a parameter. For example: 327e5dd7070Spatrick 328e5dd7070Spatrick.. code-block:: c 329e5dd7070Spatrick 330e5dd7070Spatrick // Declares and defines the ivybridge version of single_cpu. 331e5dd7070Spatrick __attribute__((cpu_specific(ivybridge))) 332e5dd7070Spatrick void single_cpu(void){} 333e5dd7070Spatrick 334e5dd7070Spatrick // Declares and defines the atom version of single_cpu. 335e5dd7070Spatrick __attribute__((cpu_specific(atom))) 336e5dd7070Spatrick void single_cpu(void){} 337e5dd7070Spatrick 338e5dd7070Spatrick // Declares and defines both the ivybridge and atom version of multi_cpu. 339e5dd7070Spatrick __attribute__((cpu_specific(ivybridge, atom))) 340e5dd7070Spatrick void multi_cpu(void){} 341e5dd7070Spatrick 342e5dd7070SpatrickA dispatching (or resolving) function can be declared anywhere in a project's 343e5dd7070Spatricksource code with ``cpu_dispatch``. This attribute takes one or more CPU names 344e5dd7070Spatrickas a parameter (like ``cpu_specific``). Functions marked with ``cpu_dispatch`` 345e5dd7070Spatrickare not expected to be defined, only declared. If such a marked function has a 346e5dd7070Spatrickdefinition, any side effects of the function are ignored; trivial function 347e5dd7070Spatrickbodies are permissible for ICC compatibility. 348e5dd7070Spatrick 349e5dd7070Spatrick.. code-block:: c 350e5dd7070Spatrick 351e5dd7070Spatrick // Creates a resolver for single_cpu above. 352e5dd7070Spatrick __attribute__((cpu_dispatch(ivybridge, atom))) 353e5dd7070Spatrick void single_cpu(void){} 354e5dd7070Spatrick 355e5dd7070Spatrick // Creates a resolver for multi_cpu, but adds a 3rd version defined in another 356e5dd7070Spatrick // translation unit. 357e5dd7070Spatrick __attribute__((cpu_dispatch(ivybridge, atom, sandybridge))) 358e5dd7070Spatrick void multi_cpu(void){} 359e5dd7070Spatrick 360e5dd7070SpatrickNote that it is possible to have a resolving function that dispatches based on 361e5dd7070Spatrickmore or fewer options than are present in the program. Specifying fewer will 362e5dd7070Spatrickresult in the omitted options not being considered during resolution. Specifying 363e5dd7070Spatricka version for resolution that isn't defined in the program will result in a 364e5dd7070Spatricklinking failure. 365e5dd7070Spatrick 366e5dd7070SpatrickIt is also possible to specify a CPU name of ``generic`` which will be resolved 367e5dd7070Spatrickif the executing processor doesn't satisfy the features required in the CPU 368e5dd7070Spatrickname. The behavior of a program executing on a processor that doesn't satisfy 369e5dd7070Spatrickany option of a multiversioned function is undefined. 370e5dd7070Spatrick }]; 371e5dd7070Spatrick} 372e5dd7070Spatrick 373e5dd7070Spatrickdef SYCLKernelDocs : Documentation { 374e5dd7070Spatrick let Category = DocCatFunction; 375e5dd7070Spatrick let Content = [{ 376e5dd7070SpatrickThe ``sycl_kernel`` attribute specifies that a function template will be used 377e5dd7070Spatrickto outline device code and to generate an OpenCL kernel. 378e5dd7070SpatrickHere is a code example of the SYCL program, which demonstrates the compiler's 379e5dd7070Spatrickoutlining job: 380ec727ea7Spatrick 381e5dd7070Spatrick.. code-block:: c++ 382e5dd7070Spatrick 383e5dd7070Spatrick int foo(int x) { return ++x; } 384e5dd7070Spatrick 385e5dd7070Spatrick using namespace cl::sycl; 386e5dd7070Spatrick queue Q; 387e5dd7070Spatrick buffer<int, 1> a(range<1>{1024}); 388e5dd7070Spatrick Q.submit([&](handler& cgh) { 389e5dd7070Spatrick auto A = a.get_access<access::mode::write>(cgh); 390e5dd7070Spatrick cgh.parallel_for<init_a>(range<1>{1024}, [=](id<1> index) { 391e5dd7070Spatrick A[index] = index[0] + foo(42); 392e5dd7070Spatrick }); 393e5dd7070Spatrick } 394e5dd7070Spatrick 395e5dd7070SpatrickA C++ function object passed to the ``parallel_for`` is called a "SYCL kernel". 396e5dd7070SpatrickA SYCL kernel defines the entry point to the "device part" of the code. The 397e5dd7070Spatrickcompiler will emit all symbols accessible from a "kernel". In this code 398e5dd7070Spatrickexample, the compiler will emit "foo" function. More details about the 399e5dd7070Spatrickcompilation of functions for the device part can be found in the SYCL 1.2.1 400e5dd7070Spatrickspecification Section 6.4. 401e5dd7070SpatrickTo show to the compiler entry point to the "device part" of the code, the SYCL 402e5dd7070Spatrickruntime can use the ``sycl_kernel`` attribute in the following way: 403ec727ea7Spatrick 404e5dd7070Spatrick.. code-block:: c++ 405ec727ea7Spatrick 406e5dd7070Spatrick namespace cl { 407e5dd7070Spatrick namespace sycl { 408e5dd7070Spatrick class handler { 409e5dd7070Spatrick template <typename KernelName, typename KernelType/*, ...*/> 410e5dd7070Spatrick __attribute__((sycl_kernel)) void sycl_kernel_function(KernelType KernelFuncObj) { 411e5dd7070Spatrick // ... 412e5dd7070Spatrick KernelFuncObj(); 413e5dd7070Spatrick } 414e5dd7070Spatrick 415e5dd7070Spatrick template <typename KernelName, typename KernelType, int Dims> 416e5dd7070Spatrick void parallel_for(range<Dims> NumWorkItems, KernelType KernelFunc) { 417e5dd7070Spatrick #ifdef __SYCL_DEVICE_ONLY__ 418e5dd7070Spatrick sycl_kernel_function<KernelName, KernelType, Dims>(KernelFunc); 419e5dd7070Spatrick #else 420e5dd7070Spatrick // Host implementation 421e5dd7070Spatrick #endif 422e5dd7070Spatrick } 423e5dd7070Spatrick }; 424e5dd7070Spatrick } // namespace sycl 425e5dd7070Spatrick } // namespace cl 426e5dd7070Spatrick 427e5dd7070SpatrickThe compiler will also generate an OpenCL kernel using the function marked with 428e5dd7070Spatrickthe ``sycl_kernel`` attribute. 429e5dd7070SpatrickHere is the list of SYCL device compiler expectations with regard to the 430e5dd7070Spatrickfunction marked with the ``sycl_kernel`` attribute: 431e5dd7070Spatrick 432e5dd7070Spatrick- The function must be a template with at least two type template parameters. 433e5dd7070Spatrick The compiler generates an OpenCL kernel and uses the first template parameter 434e5dd7070Spatrick as a unique name for the generated OpenCL kernel. The host application uses 435e5dd7070Spatrick this unique name to invoke the OpenCL kernel generated for the SYCL kernel 436e5dd7070Spatrick specialized by this name and second template parameter ``KernelType`` (which 437e5dd7070Spatrick might be an unnamed function object type). 438e5dd7070Spatrick- The function must have at least one parameter. The first parameter is 439e5dd7070Spatrick required to be a function object type (named or unnamed i.e. lambda). The 440e5dd7070Spatrick compiler uses function object type fields to generate OpenCL kernel 441e5dd7070Spatrick parameters. 442e5dd7070Spatrick- The function must return void. The compiler reuses the body of marked functions to 443ec727ea7Spatrick generate the OpenCL kernel body, and the OpenCL kernel must return ``void``. 444e5dd7070Spatrick 445e5dd7070SpatrickThe SYCL kernel in the previous code sample meets these expectations. 446e5dd7070Spatrick }]; 447e5dd7070Spatrick} 448e5dd7070Spatrick 449*12c85518Srobertdef SYCLSpecialClassDocs : Documentation { 450*12c85518Srobert let Category = DocCatStmt; 451*12c85518Srobert let Content = [{ 452*12c85518SrobertSYCL defines some special classes (accessor, sampler, and stream) which require 453*12c85518Srobertspecific handling during the generation of the SPIR entry point. 454*12c85518SrobertThe ``__attribute__((sycl_special_class))`` attribute is used in SYCL 455*12c85518Srobertheaders to indicate that a class or a struct needs a specific handling when 456*12c85518Srobertit is passed from host to device. 457*12c85518SrobertSpecial classes will have a mandatory ``__init`` method and an optional 458*12c85518Srobert``__finalize`` method (the ``__finalize`` method is used only with the 459*12c85518Srobert``stream`` type). Kernel parameters types are extract from the ``__init`` method 460*12c85518Srobertparameters. The kernel function arguments list is derived from the 461*12c85518Srobertarguments of the ``__init`` method. The arguments of the ``__init`` method are 462*12c85518Srobertcopied into the kernel function argument list and the ``__init`` and 463*12c85518Srobert``__finalize`` methods are called at the beginning and the end of the kernel, 464*12c85518Srobertrespectively. 465*12c85518SrobertThe ``__init`` and ``__finalize`` methods must be defined inside the 466*12c85518Srobertspecial class. 467*12c85518SrobertPlease note that this is an attribute that is used as an internal 468*12c85518Srobertimplementation detail and not intended to be used by external users. 469*12c85518Srobert 470*12c85518SrobertThe syntax of the attribute is as follows: 471*12c85518Srobert 472*12c85518Srobert.. code-block:: text 473*12c85518Srobert 474*12c85518Srobert class __attribute__((sycl_special_class)) accessor {}; 475*12c85518Srobert class [[clang::sycl_special_class]] accessor {}; 476*12c85518Srobert 477*12c85518SrobertThis is a code example that illustrates the use of the attribute: 478*12c85518Srobert 479*12c85518Srobert.. code-block:: c++ 480*12c85518Srobert 481*12c85518Srobert class __attribute__((sycl_special_class)) SpecialType { 482*12c85518Srobert int F1; 483*12c85518Srobert int F2; 484*12c85518Srobert void __init(int f1) { 485*12c85518Srobert F1 = f1; 486*12c85518Srobert F2 = f1; 487*12c85518Srobert } 488*12c85518Srobert void __finalize() {} 489*12c85518Srobert public: 490*12c85518Srobert SpecialType() = default; 491*12c85518Srobert int getF2() const { return F2; } 492*12c85518Srobert }; 493*12c85518Srobert 494*12c85518Srobert int main () { 495*12c85518Srobert SpecialType T; 496*12c85518Srobert cgh.single_task([=] { 497*12c85518Srobert T.getF2(); 498*12c85518Srobert }); 499*12c85518Srobert } 500*12c85518Srobert 501*12c85518SrobertThis would trigger the following kernel entry point in the AST: 502*12c85518Srobert 503*12c85518Srobert.. code-block:: c++ 504*12c85518Srobert 505*12c85518Srobert void __sycl_kernel(int f1) { 506*12c85518Srobert SpecialType T; 507*12c85518Srobert T.__init(f1); 508*12c85518Srobert ... 509*12c85518Srobert T.__finalize() 510*12c85518Srobert } 511*12c85518Srobert }]; 512*12c85518Srobert} 513*12c85518Srobert 514e5dd7070Spatrickdef C11NoReturnDocs : Documentation { 515e5dd7070Spatrick let Category = DocCatFunction; 516e5dd7070Spatrick let Content = [{ 517e5dd7070SpatrickA function declared as ``_Noreturn`` shall not return to its caller. The 518e5dd7070Spatrickcompiler will generate a diagnostic for a function declared as ``_Noreturn`` 519e5dd7070Spatrickthat appears to be capable of returning to its caller. Despite being a type 520e5dd7070Spatrickspecifier, the ``_Noreturn`` attribute cannot be specified on a function 521e5dd7070Spatrickpointer type. 522e5dd7070Spatrick }]; 523e5dd7070Spatrick} 524e5dd7070Spatrick 525e5dd7070Spatrickdef CXX11NoReturnDocs : Documentation { 526e5dd7070Spatrick let Category = DocCatFunction; 527*12c85518Srobert let Heading = "noreturn, _Noreturn"; 528e5dd7070Spatrick let Content = [{ 529e5dd7070SpatrickA function declared as ``[[noreturn]]`` shall not return to its caller. The 530e5dd7070Spatrickcompiler will generate a diagnostic for a function declared as ``[[noreturn]]`` 531e5dd7070Spatrickthat appears to be capable of returning to its caller. 532*12c85518Srobert 533*12c85518SrobertThe ``[[_Noreturn]]`` spelling is deprecated and only exists to ease code 534*12c85518Srobertmigration for code using ``[[noreturn]]`` after including ``<stdnoreturn.h>``. 535e5dd7070Spatrick }]; 536e5dd7070Spatrick} 537e5dd7070Spatrick 538ec727ea7Spatrickdef NoMergeDocs : Documentation { 539a9ac8606Spatrick let Category = DocCatStmt; 540ec727ea7Spatrick let Content = [{ 541a9ac8606SpatrickIf a statement is marked ``nomerge`` and contains call expressions, those call 542ec727ea7Spatrickexpressions inside the statement will not be merged during optimization. This 543ec727ea7Spatrickattribute can be used to prevent the optimizer from obscuring the source 544ec727ea7Spatricklocation of certain calls. For example, it will prevent tail merging otherwise 545ec727ea7Spatrickidentical code sequences that raise an exception or terminate the program. Tail 546ec727ea7Spatrickmerging normally reduces the precision of source location information, making 547ec727ea7Spatrickstack traces less useful for debugging. This attribute gives the user control 548ec727ea7Spatrickover the tradeoff between code size and debug information precision. 549a9ac8606Spatrick 550a9ac8606Spatrick``nomerge`` attribute can also be used as function attribute to prevent all 551a9ac8606Spatrickcalls to the specified function from merging. It has no effect on indirect 552a9ac8606Spatrickcalls. 553a9ac8606Spatrick }]; 554a9ac8606Spatrick} 555a9ac8606Spatrick 556*12c85518Srobertdef NoInlineDocs : Documentation { 557*12c85518Srobert let Category = DocCatFunction; 558*12c85518Srobert let Content = [{ 559*12c85518SrobertThis function attribute suppresses the inlining of a function at the call sites 560*12c85518Srobertof the function. 561*12c85518Srobert 562*12c85518Srobert``[[clang::noinline]]`` spelling can be used as a statement attribute; other 563*12c85518Srobertspellings of the attribute are not supported on statements. If a statement is 564*12c85518Srobertmarked ``[[clang::noinline]]`` and contains calls, those calls inside the 565*12c85518Srobertstatement will not be inlined by the compiler. 566*12c85518Srobert 567*12c85518Srobert``__noinline__`` can be used as a keyword in CUDA/HIP languages. This is to 568*12c85518Srobertavoid diagnostics due to usage of ``__attribute__((__noinline__))`` 569*12c85518Srobertwith ``__noinline__`` defined as a macro as ``__attribute__((noinline))``. 570*12c85518Srobert 571*12c85518Srobert.. code-block:: c 572*12c85518Srobert 573*12c85518Srobert int example(void) { 574*12c85518Srobert int r; 575*12c85518Srobert [[clang::noinline]] foo(); 576*12c85518Srobert [[clang::noinline]] r = bar(); 577*12c85518Srobert return r; 578*12c85518Srobert } 579*12c85518Srobert 580*12c85518Srobert }]; 581*12c85518Srobert} 582*12c85518Srobert 583a9ac8606Spatrickdef MustTailDocs : Documentation { 584a9ac8606Spatrick let Category = DocCatStmt; 585a9ac8606Spatrick let Content = [{ 586a9ac8606SpatrickIf a ``return`` statement is marked ``musttail``, this indicates that the 587a9ac8606Spatrickcompiler must generate a tail call for the program to be correct, even when 588a9ac8606Spatrickoptimizations are disabled. This guarantees that the call will not cause 589a9ac8606Spatrickunbounded stack growth if it is part of a recursive cycle in the call graph. 590a9ac8606Spatrick 591a9ac8606SpatrickIf the callee is a virtual function that is implemented by a thunk, there is 592a9ac8606Spatrickno guarantee in general that the thunk tail-calls the implementation of the 593a9ac8606Spatrickvirtual function, so such a call in a recursive cycle can still result in 594a9ac8606Spatrickunbounded stack growth. 595a9ac8606Spatrick 596a9ac8606Spatrick``clang::musttail`` can only be applied to a ``return`` statement whose value 597a9ac8606Spatrickis the result of a function call (even functions returning void must use 598a9ac8606Spatrick``return``, although no value is returned). The target function must have the 599a9ac8606Spatricksame number of arguments as the caller. The types of the return value and all 600a9ac8606Spatrickarguments must be similar according to C++ rules (differing only in cv 601a9ac8606Spatrickqualifiers or array size), including the implicit "this" argument, if any. 602a9ac8606SpatrickAny variables in scope, including all arguments to the function and the 603a9ac8606Spatrickreturn value must be trivially destructible. The calling convention of the 604a9ac8606Spatrickcaller and callee must match, and they must not be variadic functions or have 605a9ac8606Spatrickold style K&R C function declarations. 606ec727ea7Spatrick }]; 607ec727ea7Spatrick} 608ec727ea7Spatrick 609e5dd7070Spatrickdef AssertCapabilityDocs : Documentation { 610e5dd7070Spatrick let Category = DocCatFunction; 611e5dd7070Spatrick let Heading = "assert_capability, assert_shared_capability"; 612e5dd7070Spatrick let Content = [{ 613e5dd7070SpatrickMarks a function that dynamically tests whether a capability is held, and halts 614e5dd7070Spatrickthe program if it is not held. 615e5dd7070Spatrick }]; 616e5dd7070Spatrick} 617e5dd7070Spatrick 618e5dd7070Spatrickdef AcquireCapabilityDocs : Documentation { 619e5dd7070Spatrick let Category = DocCatFunction; 620e5dd7070Spatrick let Heading = "acquire_capability, acquire_shared_capability"; 621e5dd7070Spatrick let Content = [{ 622e5dd7070SpatrickMarks a function as acquiring a capability. 623e5dd7070Spatrick }]; 624e5dd7070Spatrick} 625e5dd7070Spatrick 626e5dd7070Spatrickdef TryAcquireCapabilityDocs : Documentation { 627e5dd7070Spatrick let Category = DocCatFunction; 628e5dd7070Spatrick let Heading = "try_acquire_capability, try_acquire_shared_capability"; 629e5dd7070Spatrick let Content = [{ 630e5dd7070SpatrickMarks a function that attempts to acquire a capability. This function may fail to 631e5dd7070Spatrickactually acquire the capability; they accept a Boolean value determining 632e5dd7070Spatrickwhether acquiring the capability means success (true), or failing to acquire 633e5dd7070Spatrickthe capability means success (false). 634e5dd7070Spatrick }]; 635e5dd7070Spatrick} 636e5dd7070Spatrick 637e5dd7070Spatrickdef ReleaseCapabilityDocs : Documentation { 638e5dd7070Spatrick let Category = DocCatFunction; 639e5dd7070Spatrick let Heading = "release_capability, release_shared_capability"; 640e5dd7070Spatrick let Content = [{ 641e5dd7070SpatrickMarks a function as releasing a capability. 642e5dd7070Spatrick }]; 643e5dd7070Spatrick} 644e5dd7070Spatrick 645e5dd7070Spatrickdef AssumeAlignedDocs : Documentation { 646e5dd7070Spatrick let Category = DocCatFunction; 647e5dd7070Spatrick let Content = [{ 648e5dd7070SpatrickUse ``__attribute__((assume_aligned(<alignment>[,<offset>]))`` on a function 649e5dd7070Spatrickdeclaration to specify that the return value of the function (which must be a 650e5dd7070Spatrickpointer type) has the specified offset, in bytes, from an address with the 651e5dd7070Spatrickspecified alignment. The offset is taken to be zero if omitted. 652e5dd7070Spatrick 653e5dd7070Spatrick.. code-block:: c++ 654e5dd7070Spatrick 655e5dd7070Spatrick // The returned pointer value has 32-byte alignment. 656e5dd7070Spatrick void *a() __attribute__((assume_aligned (32))); 657e5dd7070Spatrick 658e5dd7070Spatrick // The returned pointer value is 4 bytes greater than an address having 659e5dd7070Spatrick // 32-byte alignment. 660e5dd7070Spatrick void *b() __attribute__((assume_aligned (32, 4))); 661e5dd7070Spatrick 662e5dd7070SpatrickNote that this attribute provides information to the compiler regarding a 663e5dd7070Spatrickcondition that the code already ensures is true. It does not cause the compiler 664e5dd7070Spatrickto enforce the provided alignment assumption. 665e5dd7070Spatrick }]; 666e5dd7070Spatrick} 667e5dd7070Spatrick 668e5dd7070Spatrickdef AllocSizeDocs : Documentation { 669e5dd7070Spatrick let Category = DocCatFunction; 670e5dd7070Spatrick let Content = [{ 671e5dd7070SpatrickThe ``alloc_size`` attribute can be placed on functions that return pointers in 672e5dd7070Spatrickorder to hint to the compiler how many bytes of memory will be available at the 673e5dd7070Spatrickreturned pointer. ``alloc_size`` takes one or two arguments. 674e5dd7070Spatrick 675e5dd7070Spatrick- ``alloc_size(N)`` implies that argument number N equals the number of 676e5dd7070Spatrick available bytes at the returned pointer. 677e5dd7070Spatrick- ``alloc_size(N, M)`` implies that the product of argument number N and 678e5dd7070Spatrick argument number M equals the number of available bytes at the returned 679e5dd7070Spatrick pointer. 680e5dd7070Spatrick 681e5dd7070SpatrickArgument numbers are 1-based. 682e5dd7070Spatrick 683e5dd7070SpatrickAn example of how to use ``alloc_size`` 684e5dd7070Spatrick 685e5dd7070Spatrick.. code-block:: c 686e5dd7070Spatrick 687e5dd7070Spatrick void *my_malloc(int a) __attribute__((alloc_size(1))); 688e5dd7070Spatrick void *my_calloc(int a, int b) __attribute__((alloc_size(1, 2))); 689e5dd7070Spatrick 690e5dd7070Spatrick int main() { 691e5dd7070Spatrick void *const p = my_malloc(100); 692e5dd7070Spatrick assert(__builtin_object_size(p, 0) == 100); 693e5dd7070Spatrick void *const a = my_calloc(20, 5); 694e5dd7070Spatrick assert(__builtin_object_size(a, 0) == 100); 695e5dd7070Spatrick } 696e5dd7070Spatrick 697e5dd7070Spatrick.. Note:: This attribute works differently in clang than it does in GCC. 698e5dd7070Spatrick Specifically, clang will only trace ``const`` pointers (as above); we give up 699e5dd7070Spatrick on pointers that are not marked as ``const``. In the vast majority of cases, 700e5dd7070Spatrick this is unimportant, because LLVM has support for the ``alloc_size`` 701e5dd7070Spatrick attribute. However, this may cause mildly unintuitive behavior when used with 702e5dd7070Spatrick other attributes, such as ``enable_if``. 703e5dd7070Spatrick }]; 704e5dd7070Spatrick} 705e5dd7070Spatrick 706e5dd7070Spatrickdef CodeSegDocs : Documentation { 707e5dd7070Spatrick let Category = DocCatFunction; 708e5dd7070Spatrick let Content = [{ 709e5dd7070SpatrickThe ``__declspec(code_seg)`` attribute enables the placement of code into separate 710e5dd7070Spatricknamed segments that can be paged or locked in memory individually. This attribute 711e5dd7070Spatrickis used to control the placement of instantiated templates and compiler-generated 712e5dd7070Spatrickcode. See the documentation for `__declspec(code_seg)`_ on MSDN. 713e5dd7070Spatrick 714e5dd7070Spatrick.. _`__declspec(code_seg)`: http://msdn.microsoft.com/en-us/library/dn636922.aspx 715e5dd7070Spatrick }]; 716e5dd7070Spatrick} 717e5dd7070Spatrick 718e5dd7070Spatrickdef AllocAlignDocs : Documentation { 719e5dd7070Spatrick let Category = DocCatFunction; 720e5dd7070Spatrick let Content = [{ 721e5dd7070SpatrickUse ``__attribute__((alloc_align(<alignment>))`` on a function 722e5dd7070Spatrickdeclaration to specify that the return value of the function (which must be a 723e5dd7070Spatrickpointer type) is at least as aligned as the value of the indicated parameter. The 724e5dd7070Spatrickparameter is given by its index in the list of formal parameters; the first 725e5dd7070Spatrickparameter has index 1 unless the function is a C++ non-static member function, 726e5dd7070Spatrickin which case the first parameter has index 2 to account for the implicit ``this`` 727e5dd7070Spatrickparameter. 728e5dd7070Spatrick 729e5dd7070Spatrick.. code-block:: c++ 730e5dd7070Spatrick 731e5dd7070Spatrick // The returned pointer has the alignment specified by the first parameter. 732e5dd7070Spatrick void *a(size_t align) __attribute__((alloc_align(1))); 733e5dd7070Spatrick 734e5dd7070Spatrick // The returned pointer has the alignment specified by the second parameter. 735e5dd7070Spatrick void *b(void *v, size_t align) __attribute__((alloc_align(2))); 736e5dd7070Spatrick 737e5dd7070Spatrick // The returned pointer has the alignment specified by the second visible 738e5dd7070Spatrick // parameter, however it must be adjusted for the implicit 'this' parameter. 739e5dd7070Spatrick void *Foo::b(void *v, size_t align) __attribute__((alloc_align(3))); 740e5dd7070Spatrick 741e5dd7070SpatrickNote that this attribute merely informs the compiler that a function always 742e5dd7070Spatrickreturns a sufficiently aligned pointer. It does not cause the compiler to 743e5dd7070Spatrickemit code to enforce that alignment. The behavior is undefined if the returned 744ec727ea7Spatrickpointer is not sufficiently aligned. 745e5dd7070Spatrick }]; 746e5dd7070Spatrick} 747e5dd7070Spatrick 748e5dd7070Spatrickdef EnableIfDocs : Documentation { 749e5dd7070Spatrick let Category = DocCatFunction; 750e5dd7070Spatrick let Content = [{ 751e5dd7070Spatrick.. Note:: Some features of this attribute are experimental. The meaning of 752e5dd7070Spatrick multiple enable_if attributes on a single declaration is subject to change in 753e5dd7070Spatrick a future version of clang. Also, the ABI is not standardized and the name 754e5dd7070Spatrick mangling may change in future versions. To avoid that, use asm labels. 755e5dd7070Spatrick 756e5dd7070SpatrickThe ``enable_if`` attribute can be placed on function declarations to control 757e5dd7070Spatrickwhich overload is selected based on the values of the function's arguments. 758e5dd7070SpatrickWhen combined with the ``overloadable`` attribute, this feature is also 759e5dd7070Spatrickavailable in C. 760e5dd7070Spatrick 761e5dd7070Spatrick.. code-block:: c++ 762e5dd7070Spatrick 763e5dd7070Spatrick int isdigit(int c); 764e5dd7070Spatrick 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"))); 765e5dd7070Spatrick 766e5dd7070Spatrick void foo(char c) { 767e5dd7070Spatrick isdigit(c); 768e5dd7070Spatrick isdigit(10); 769e5dd7070Spatrick isdigit(-10); // results in a compile-time error. 770e5dd7070Spatrick } 771e5dd7070Spatrick 772e5dd7070SpatrickThe enable_if attribute takes two arguments, the first is an expression written 773e5dd7070Spatrickin terms of the function parameters, the second is a string explaining why this 774e5dd7070Spatrickoverload candidate could not be selected to be displayed in diagnostics. The 775e5dd7070Spatrickexpression is part of the function signature for the purposes of determining 776e5dd7070Spatrickwhether it is a redeclaration (following the rules used when determining 777e5dd7070Spatrickwhether a C++ template specialization is ODR-equivalent), but is not part of 778e5dd7070Spatrickthe type. 779e5dd7070Spatrick 780e5dd7070SpatrickThe enable_if expression is evaluated as if it were the body of a 781e5dd7070Spatrickbool-returning constexpr function declared with the arguments of the function 782e5dd7070Spatrickit is being applied to, then called with the parameters at the call site. If the 783e5dd7070Spatrickresult is false or could not be determined through constant expression 784e5dd7070Spatrickevaluation, then this overload will not be chosen and the provided string may 785e5dd7070Spatrickbe used in a diagnostic if the compile fails as a result. 786e5dd7070Spatrick 787e5dd7070SpatrickBecause the enable_if expression is an unevaluated context, there are no global 788e5dd7070Spatrickstate changes, nor the ability to pass information from the enable_if 789e5dd7070Spatrickexpression to the function body. For example, suppose we want calls to 790e5dd7070Spatrickstrnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of 791e5dd7070Spatrickstrbuf) only if the size of strbuf can be determined: 792e5dd7070Spatrick 793e5dd7070Spatrick.. code-block:: c++ 794e5dd7070Spatrick 795e5dd7070Spatrick __attribute__((always_inline)) 796e5dd7070Spatrick static inline size_t strnlen(const char *s, size_t maxlen) 797e5dd7070Spatrick __attribute__((overloadable)) 798e5dd7070Spatrick __attribute__((enable_if(__builtin_object_size(s, 0) != -1))), 799e5dd7070Spatrick "chosen when the buffer size is known but 'maxlen' is not"))) 800e5dd7070Spatrick { 801e5dd7070Spatrick return strnlen_chk(s, maxlen, __builtin_object_size(s, 0)); 802e5dd7070Spatrick } 803e5dd7070Spatrick 804e5dd7070SpatrickMultiple enable_if attributes may be applied to a single declaration. In this 805e5dd7070Spatrickcase, the enable_if expressions are evaluated from left to right in the 806e5dd7070Spatrickfollowing manner. First, the candidates whose enable_if expressions evaluate to 807e5dd7070Spatrickfalse or cannot be evaluated are discarded. If the remaining candidates do not 808e5dd7070Spatrickshare ODR-equivalent enable_if expressions, the overload resolution is 809e5dd7070Spatrickambiguous. Otherwise, enable_if overload resolution continues with the next 810e5dd7070Spatrickenable_if attribute on the candidates that have not been discarded and have 811e5dd7070Spatrickremaining enable_if attributes. In this way, we pick the most specific 812e5dd7070Spatrickoverload out of a number of viable overloads using enable_if. 813e5dd7070Spatrick 814e5dd7070Spatrick.. code-block:: c++ 815e5dd7070Spatrick 816e5dd7070Spatrick void f() __attribute__((enable_if(true, ""))); // #1 817e5dd7070Spatrick void f() __attribute__((enable_if(true, ""))) __attribute__((enable_if(true, ""))); // #2 818e5dd7070Spatrick 819e5dd7070Spatrick void g(int i, int j) __attribute__((enable_if(i, ""))); // #1 820e5dd7070Spatrick void g(int i, int j) __attribute__((enable_if(j, ""))) __attribute__((enable_if(true))); // #2 821e5dd7070Spatrick 822e5dd7070SpatrickIn this example, a call to f() is always resolved to #2, as the first enable_if 823e5dd7070Spatrickexpression is ODR-equivalent for both declarations, but #1 does not have another 824e5dd7070Spatrickenable_if expression to continue evaluating, so the next round of evaluation has 825e5dd7070Spatrickonly a single candidate. In a call to g(1, 1), the call is ambiguous even though 826e5dd7070Spatrick#2 has more enable_if attributes, because the first enable_if expressions are 827e5dd7070Spatricknot ODR-equivalent. 828e5dd7070Spatrick 829e5dd7070SpatrickQuery for this feature with ``__has_attribute(enable_if)``. 830e5dd7070Spatrick 831e5dd7070SpatrickNote that functions with one or more ``enable_if`` attributes may not have 832e5dd7070Spatricktheir address taken, unless all of the conditions specified by said 833e5dd7070Spatrick``enable_if`` are constants that evaluate to ``true``. For example: 834e5dd7070Spatrick 835e5dd7070Spatrick.. code-block:: c 836e5dd7070Spatrick 837e5dd7070Spatrick const int TrueConstant = 1; 838e5dd7070Spatrick const int FalseConstant = 0; 839e5dd7070Spatrick int f(int a) __attribute__((enable_if(a > 0, ""))); 840e5dd7070Spatrick int g(int a) __attribute__((enable_if(a == 0 || a != 0, ""))); 841e5dd7070Spatrick int h(int a) __attribute__((enable_if(1, ""))); 842e5dd7070Spatrick int i(int a) __attribute__((enable_if(TrueConstant, ""))); 843e5dd7070Spatrick int j(int a) __attribute__((enable_if(FalseConstant, ""))); 844e5dd7070Spatrick 845e5dd7070Spatrick void fn() { 846e5dd7070Spatrick int (*ptr)(int); 847e5dd7070Spatrick ptr = &f; // error: 'a > 0' is not always true 848e5dd7070Spatrick ptr = &g; // error: 'a == 0 || a != 0' is not a truthy constant 849e5dd7070Spatrick ptr = &h; // OK: 1 is a truthy constant 850e5dd7070Spatrick ptr = &i; // OK: 'TrueConstant' is a truthy constant 851e5dd7070Spatrick ptr = &j; // error: 'FalseConstant' is a constant, but not truthy 852e5dd7070Spatrick } 853e5dd7070Spatrick 854e5dd7070SpatrickBecause ``enable_if`` evaluation happens during overload resolution, 855e5dd7070Spatrick``enable_if`` may give unintuitive results when used with templates, depending 856e5dd7070Spatrickon when overloads are resolved. In the example below, clang will emit a 857e5dd7070Spatrickdiagnostic about no viable overloads for ``foo`` in ``bar``, but not in ``baz``: 858e5dd7070Spatrick 859e5dd7070Spatrick.. code-block:: c++ 860e5dd7070Spatrick 861e5dd7070Spatrick double foo(int i) __attribute__((enable_if(i > 0, ""))); 862e5dd7070Spatrick void *foo(int i) __attribute__((enable_if(i <= 0, ""))); 863e5dd7070Spatrick template <int I> 864e5dd7070Spatrick auto bar() { return foo(I); } 865e5dd7070Spatrick 866e5dd7070Spatrick template <typename T> 867e5dd7070Spatrick auto baz() { return foo(T::number); } 868e5dd7070Spatrick 869e5dd7070Spatrick struct WithNumber { constexpr static int number = 1; }; 870e5dd7070Spatrick void callThem() { 871e5dd7070Spatrick bar<sizeof(WithNumber)>(); 872e5dd7070Spatrick baz<WithNumber>(); 873e5dd7070Spatrick } 874e5dd7070Spatrick 875e5dd7070SpatrickThis is because, in ``bar``, ``foo`` is resolved prior to template 876e5dd7070Spatrickinstantiation, so the value for ``I`` isn't known (thus, both ``enable_if`` 877e5dd7070Spatrickconditions for ``foo`` fail). However, in ``baz``, ``foo`` is resolved during 878e5dd7070Spatricktemplate instantiation, so the value for ``T::number`` is known. 879e5dd7070Spatrick }]; 880e5dd7070Spatrick} 881e5dd7070Spatrick 882e5dd7070Spatrickdef DiagnoseIfDocs : Documentation { 883e5dd7070Spatrick let Category = DocCatFunction; 884e5dd7070Spatrick let Content = [{ 885e5dd7070SpatrickThe ``diagnose_if`` attribute can be placed on function declarations to emit 886e5dd7070Spatrickwarnings or errors at compile-time if calls to the attributed function meet 887e5dd7070Spatrickcertain user-defined criteria. For example: 888e5dd7070Spatrick 889e5dd7070Spatrick.. code-block:: c 890e5dd7070Spatrick 891e5dd7070Spatrick int abs(int a) 892e5dd7070Spatrick __attribute__((diagnose_if(a >= 0, "Redundant abs call", "warning"))); 893e5dd7070Spatrick int must_abs(int a) 894e5dd7070Spatrick __attribute__((diagnose_if(a >= 0, "Redundant abs call", "error"))); 895e5dd7070Spatrick 896e5dd7070Spatrick int val = abs(1); // warning: Redundant abs call 897e5dd7070Spatrick int val2 = must_abs(1); // error: Redundant abs call 898e5dd7070Spatrick int val3 = abs(val); 899e5dd7070Spatrick int val4 = must_abs(val); // Because run-time checks are not emitted for 900e5dd7070Spatrick // diagnose_if attributes, this executes without 901e5dd7070Spatrick // issue. 902e5dd7070Spatrick 903e5dd7070Spatrick 904e5dd7070Spatrick``diagnose_if`` is closely related to ``enable_if``, with a few key differences: 905e5dd7070Spatrick 906e5dd7070Spatrick* Overload resolution is not aware of ``diagnose_if`` attributes: they're 907e5dd7070Spatrick considered only after we select the best candidate from a given candidate set. 908e5dd7070Spatrick* Function declarations that differ only in their ``diagnose_if`` attributes are 909e5dd7070Spatrick considered to be redeclarations of the same function (not overloads). 910e5dd7070Spatrick* If the condition provided to ``diagnose_if`` cannot be evaluated, no 911e5dd7070Spatrick diagnostic will be emitted. 912e5dd7070Spatrick 913e5dd7070SpatrickOtherwise, ``diagnose_if`` is essentially the logical negation of ``enable_if``. 914e5dd7070Spatrick 915e5dd7070SpatrickAs a result of bullet number two, ``diagnose_if`` attributes will stack on the 916e5dd7070Spatricksame function. For example: 917e5dd7070Spatrick 918e5dd7070Spatrick.. code-block:: c 919e5dd7070Spatrick 920e5dd7070Spatrick int foo() __attribute__((diagnose_if(1, "diag1", "warning"))); 921e5dd7070Spatrick int foo() __attribute__((diagnose_if(1, "diag2", "warning"))); 922e5dd7070Spatrick 923e5dd7070Spatrick int bar = foo(); // warning: diag1 924e5dd7070Spatrick // warning: diag2 925e5dd7070Spatrick int (*fooptr)(void) = foo; // warning: diag1 926e5dd7070Spatrick // warning: diag2 927e5dd7070Spatrick 928e5dd7070Spatrick constexpr int supportsAPILevel(int N) { return N < 5; } 929e5dd7070Spatrick int baz(int a) 930e5dd7070Spatrick __attribute__((diagnose_if(!supportsAPILevel(10), 931e5dd7070Spatrick "Upgrade to API level 10 to use baz", "error"))); 932e5dd7070Spatrick int baz(int a) 933e5dd7070Spatrick __attribute__((diagnose_if(!a, "0 is not recommended.", "warning"))); 934e5dd7070Spatrick 935e5dd7070Spatrick int (*bazptr)(int) = baz; // error: Upgrade to API level 10 to use baz 936e5dd7070Spatrick int v = baz(0); // error: Upgrade to API level 10 to use baz 937e5dd7070Spatrick 938e5dd7070SpatrickQuery for this feature with ``__has_attribute(diagnose_if)``. 939e5dd7070Spatrick }]; 940e5dd7070Spatrick} 941e5dd7070Spatrick 942e5dd7070Spatrickdef PassObjectSizeDocs : Documentation { 943e5dd7070Spatrick let Category = DocCatVariable; // Technically it's a parameter doc, but eh. 944e5dd7070Spatrick let Heading = "pass_object_size, pass_dynamic_object_size"; 945e5dd7070Spatrick let Content = [{ 946e5dd7070Spatrick.. Note:: The mangling of functions with parameters that are annotated with 947e5dd7070Spatrick ``pass_object_size`` is subject to change. You can get around this by 948e5dd7070Spatrick using ``__asm__("foo")`` to explicitly name your functions, thus preserving 949e5dd7070Spatrick your ABI; also, non-overloadable C functions with ``pass_object_size`` are 950e5dd7070Spatrick not mangled. 951e5dd7070Spatrick 952e5dd7070SpatrickThe ``pass_object_size(Type)`` attribute can be placed on function parameters to 953e5dd7070Spatrickinstruct clang to call ``__builtin_object_size(param, Type)`` at each callsite 954e5dd7070Spatrickof said function, and implicitly pass the result of this call in as an invisible 955e5dd7070Spatrickargument of type ``size_t`` directly after the parameter annotated with 956e5dd7070Spatrick``pass_object_size``. Clang will also replace any calls to 957e5dd7070Spatrick``__builtin_object_size(param, Type)`` in the function by said implicit 958e5dd7070Spatrickparameter. 959e5dd7070Spatrick 960e5dd7070SpatrickExample usage: 961e5dd7070Spatrick 962e5dd7070Spatrick.. code-block:: c 963e5dd7070Spatrick 964e5dd7070Spatrick int bzero1(char *const p __attribute__((pass_object_size(0)))) 965e5dd7070Spatrick __attribute__((noinline)) { 966e5dd7070Spatrick int i = 0; 967e5dd7070Spatrick for (/**/; i < (int)__builtin_object_size(p, 0); ++i) { 968e5dd7070Spatrick p[i] = 0; 969e5dd7070Spatrick } 970e5dd7070Spatrick return i; 971e5dd7070Spatrick } 972e5dd7070Spatrick 973e5dd7070Spatrick int main() { 974e5dd7070Spatrick char chars[100]; 975e5dd7070Spatrick int n = bzero1(&chars[0]); 976e5dd7070Spatrick assert(n == sizeof(chars)); 977e5dd7070Spatrick return 0; 978e5dd7070Spatrick } 979e5dd7070Spatrick 980e5dd7070SpatrickIf successfully evaluating ``__builtin_object_size(param, Type)`` at the 981e5dd7070Spatrickcallsite is not possible, then the "failed" value is passed in. So, using the 982e5dd7070Spatrickdefinition of ``bzero1`` from above, the following code would exit cleanly: 983e5dd7070Spatrick 984e5dd7070Spatrick.. code-block:: c 985e5dd7070Spatrick 986e5dd7070Spatrick int main2(int argc, char *argv[]) { 987e5dd7070Spatrick int n = bzero1(argv); 988e5dd7070Spatrick assert(n == -1); 989e5dd7070Spatrick return 0; 990e5dd7070Spatrick } 991e5dd7070Spatrick 992e5dd7070Spatrick``pass_object_size`` plays a part in overload resolution. If two overload 993e5dd7070Spatrickcandidates are otherwise equally good, then the overload with one or more 994e5dd7070Spatrickparameters with ``pass_object_size`` is preferred. This implies that the choice 995e5dd7070Spatrickbetween two identical overloads both with ``pass_object_size`` on one or more 996e5dd7070Spatrickparameters will always be ambiguous; for this reason, having two such overloads 997e5dd7070Spatrickis illegal. For example: 998e5dd7070Spatrick 999e5dd7070Spatrick.. code-block:: c++ 1000e5dd7070Spatrick 1001e5dd7070Spatrick #define PS(N) __attribute__((pass_object_size(N))) 1002e5dd7070Spatrick // OK 1003e5dd7070Spatrick void Foo(char *a, char *b); // Overload A 1004e5dd7070Spatrick // OK -- overload A has no parameters with pass_object_size. 1005e5dd7070Spatrick void Foo(char *a PS(0), char *b PS(0)); // Overload B 1006e5dd7070Spatrick // Error -- Same signature (sans pass_object_size) as overload B, and both 1007e5dd7070Spatrick // overloads have one or more parameters with the pass_object_size attribute. 1008e5dd7070Spatrick void Foo(void *a PS(0), void *b); 1009e5dd7070Spatrick 1010e5dd7070Spatrick // OK 1011e5dd7070Spatrick void Bar(void *a PS(0)); // Overload C 1012e5dd7070Spatrick // OK 1013e5dd7070Spatrick void Bar(char *c PS(1)); // Overload D 1014e5dd7070Spatrick 1015e5dd7070Spatrick void main() { 1016e5dd7070Spatrick char known[10], *unknown; 1017e5dd7070Spatrick Foo(unknown, unknown); // Calls overload B 1018e5dd7070Spatrick Foo(known, unknown); // Calls overload B 1019e5dd7070Spatrick Foo(unknown, known); // Calls overload B 1020e5dd7070Spatrick Foo(known, known); // Calls overload B 1021e5dd7070Spatrick 1022e5dd7070Spatrick Bar(known); // Calls overload D 1023e5dd7070Spatrick Bar(unknown); // Calls overload D 1024e5dd7070Spatrick } 1025e5dd7070Spatrick 1026e5dd7070SpatrickCurrently, ``pass_object_size`` is a bit restricted in terms of its usage: 1027e5dd7070Spatrick 1028e5dd7070Spatrick* Only one use of ``pass_object_size`` is allowed per parameter. 1029e5dd7070Spatrick 1030e5dd7070Spatrick* It is an error to take the address of a function with ``pass_object_size`` on 1031e5dd7070Spatrick any of its parameters. If you wish to do this, you can create an overload 1032e5dd7070Spatrick without ``pass_object_size`` on any parameters. 1033e5dd7070Spatrick 1034e5dd7070Spatrick* It is an error to apply the ``pass_object_size`` attribute to parameters that 1035e5dd7070Spatrick are not pointers. Additionally, any parameter that ``pass_object_size`` is 1036e5dd7070Spatrick applied to must be marked ``const`` at its function's definition. 1037e5dd7070Spatrick 1038e5dd7070SpatrickClang also supports the ``pass_dynamic_object_size`` attribute, which behaves 1039e5dd7070Spatrickidentically to ``pass_object_size``, but evaluates a call to 1040e5dd7070Spatrick``__builtin_dynamic_object_size`` at the callee instead of 1041e5dd7070Spatrick``__builtin_object_size``. ``__builtin_dynamic_object_size`` provides some extra 1042e5dd7070Spatrickruntime checks when the object size can't be determined at compile-time. You can 1043e5dd7070Spatrickread more about ``__builtin_dynamic_object_size`` `here 1044e5dd7070Spatrick<https://clang.llvm.org/docs/LanguageExtensions.html#evaluating-object-size-dynamically>`_. 1045e5dd7070Spatrick 1046e5dd7070Spatrick }]; 1047e5dd7070Spatrick} 1048e5dd7070Spatrick 1049e5dd7070Spatrickdef OverloadableDocs : Documentation { 1050e5dd7070Spatrick let Category = DocCatFunction; 1051e5dd7070Spatrick let Content = [{ 1052e5dd7070SpatrickClang provides support for C++ function overloading in C. Function overloading 1053e5dd7070Spatrickin C is introduced using the ``overloadable`` attribute. For example, one 1054e5dd7070Spatrickmight provide several overloaded versions of a ``tgsin`` function that invokes 1055e5dd7070Spatrickthe appropriate standard function computing the sine of a value with ``float``, 1056e5dd7070Spatrick``double``, or ``long double`` precision: 1057e5dd7070Spatrick 1058e5dd7070Spatrick.. code-block:: c 1059e5dd7070Spatrick 1060e5dd7070Spatrick #include <math.h> 1061e5dd7070Spatrick float __attribute__((overloadable)) tgsin(float x) { return sinf(x); } 1062e5dd7070Spatrick double __attribute__((overloadable)) tgsin(double x) { return sin(x); } 1063e5dd7070Spatrick long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); } 1064e5dd7070Spatrick 1065e5dd7070SpatrickGiven these declarations, one can call ``tgsin`` with a ``float`` value to 1066e5dd7070Spatrickreceive a ``float`` result, with a ``double`` to receive a ``double`` result, 1067e5dd7070Spatricketc. Function overloading in C follows the rules of C++ function overloading 1068e5dd7070Spatrickto pick the best overload given the call arguments, with a few C-specific 1069e5dd7070Spatricksemantics: 1070e5dd7070Spatrick 1071e5dd7070Spatrick* Conversion from ``float`` or ``double`` to ``long double`` is ranked as a 1072e5dd7070Spatrick floating-point promotion (per C99) rather than as a floating-point conversion 1073e5dd7070Spatrick (as in C++). 1074e5dd7070Spatrick 1075e5dd7070Spatrick* A conversion from a pointer of type ``T*`` to a pointer of type ``U*`` is 1076e5dd7070Spatrick considered a pointer conversion (with conversion rank) if ``T`` and ``U`` are 1077e5dd7070Spatrick compatible types. 1078e5dd7070Spatrick 1079e5dd7070Spatrick* A conversion from type ``T`` to a value of type ``U`` is permitted if ``T`` 1080e5dd7070Spatrick and ``U`` are compatible types. This conversion is given "conversion" rank. 1081e5dd7070Spatrick 1082e5dd7070Spatrick* If no viable candidates are otherwise available, we allow a conversion from a 1083e5dd7070Spatrick pointer of type ``T*`` to a pointer of type ``U*``, where ``T`` and ``U`` are 1084e5dd7070Spatrick incompatible. This conversion is ranked below all other types of conversions. 1085e5dd7070Spatrick Please note: ``U`` lacking qualifiers that are present on ``T`` is sufficient 1086e5dd7070Spatrick for ``T`` and ``U`` to be incompatible. 1087e5dd7070Spatrick 1088e5dd7070SpatrickThe declaration of ``overloadable`` functions is restricted to function 1089e5dd7070Spatrickdeclarations and definitions. If a function is marked with the ``overloadable`` 1090e5dd7070Spatrickattribute, then all declarations and definitions of functions with that name, 1091e5dd7070Spatrickexcept for at most one (see the note below about unmarked overloads), must have 1092e5dd7070Spatrickthe ``overloadable`` attribute. In addition, redeclarations of a function with 1093e5dd7070Spatrickthe ``overloadable`` attribute must have the ``overloadable`` attribute, and 1094e5dd7070Spatrickredeclarations of a function without the ``overloadable`` attribute must *not* 1095e5dd7070Spatrickhave the ``overloadable`` attribute. e.g., 1096e5dd7070Spatrick 1097e5dd7070Spatrick.. code-block:: c 1098e5dd7070Spatrick 1099e5dd7070Spatrick int f(int) __attribute__((overloadable)); 1100e5dd7070Spatrick float f(float); // error: declaration of "f" must have the "overloadable" attribute 1101e5dd7070Spatrick int f(int); // error: redeclaration of "f" must have the "overloadable" attribute 1102e5dd7070Spatrick 1103e5dd7070Spatrick int g(int) __attribute__((overloadable)); 1104e5dd7070Spatrick int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute 1105e5dd7070Spatrick 1106e5dd7070Spatrick int h(int); 1107e5dd7070Spatrick int h(int) __attribute__((overloadable)); // error: declaration of "h" must not 1108e5dd7070Spatrick // have the "overloadable" attribute 1109e5dd7070Spatrick 1110e5dd7070SpatrickFunctions marked ``overloadable`` must have prototypes. Therefore, the 1111e5dd7070Spatrickfollowing code is ill-formed: 1112e5dd7070Spatrick 1113e5dd7070Spatrick.. code-block:: c 1114e5dd7070Spatrick 1115e5dd7070Spatrick int h() __attribute__((overloadable)); // error: h does not have a prototype 1116e5dd7070Spatrick 1117e5dd7070SpatrickHowever, ``overloadable`` functions are allowed to use a ellipsis even if there 1118e5dd7070Spatrickare no named parameters (as is permitted in C++). This feature is particularly 1119e5dd7070Spatrickuseful when combined with the ``unavailable`` attribute: 1120e5dd7070Spatrick 1121e5dd7070Spatrick.. code-block:: c++ 1122e5dd7070Spatrick 1123e5dd7070Spatrick void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error 1124e5dd7070Spatrick 1125e5dd7070SpatrickFunctions declared with the ``overloadable`` attribute have their names mangled 1126e5dd7070Spatrickaccording to the same rules as C++ function names. For example, the three 1127e5dd7070Spatrick``tgsin`` functions in our motivating example get the mangled names 1128e5dd7070Spatrick``_Z5tgsinf``, ``_Z5tgsind``, and ``_Z5tgsine``, respectively. There are two 1129e5dd7070Spatrickcaveats to this use of name mangling: 1130e5dd7070Spatrick 1131e5dd7070Spatrick* Future versions of Clang may change the name mangling of functions overloaded 1132e5dd7070Spatrick in C, so you should not depend on an specific mangling. To be completely 1133e5dd7070Spatrick safe, we strongly urge the use of ``static inline`` with ``overloadable`` 1134e5dd7070Spatrick functions. 1135e5dd7070Spatrick 1136e5dd7070Spatrick* The ``overloadable`` attribute has almost no meaning when used in C++, 1137e5dd7070Spatrick because names will already be mangled and functions are already overloadable. 1138e5dd7070Spatrick However, when an ``overloadable`` function occurs within an ``extern "C"`` 1139e5dd7070Spatrick linkage specification, it's name *will* be mangled in the same way as it 1140e5dd7070Spatrick would in C. 1141e5dd7070Spatrick 1142e5dd7070SpatrickFor the purpose of backwards compatibility, at most one function with the same 1143e5dd7070Spatrickname as other ``overloadable`` functions may omit the ``overloadable`` 1144e5dd7070Spatrickattribute. In this case, the function without the ``overloadable`` attribute 1145e5dd7070Spatrickwill not have its name mangled. 1146e5dd7070Spatrick 1147e5dd7070SpatrickFor example: 1148e5dd7070Spatrick 1149e5dd7070Spatrick.. code-block:: c 1150e5dd7070Spatrick 1151e5dd7070Spatrick // Notes with mangled names assume Itanium mangling. 1152e5dd7070Spatrick int f(int); 1153e5dd7070Spatrick int f(double) __attribute__((overloadable)); 1154e5dd7070Spatrick void foo() { 1155e5dd7070Spatrick f(5); // Emits a call to f (not _Z1fi, as it would with an overload that 1156e5dd7070Spatrick // was marked with overloadable). 1157e5dd7070Spatrick f(1.0); // Emits a call to _Z1fd. 1158e5dd7070Spatrick } 1159e5dd7070Spatrick 1160e5dd7070SpatrickSupport for unmarked overloads is not present in some versions of clang. You may 1161e5dd7070Spatrickquery for it using ``__has_extension(overloadable_unmarked)``. 1162e5dd7070Spatrick 1163e5dd7070SpatrickQuery for this attribute with ``__has_attribute(overloadable)``. 1164e5dd7070Spatrick }]; 1165e5dd7070Spatrick} 1166e5dd7070Spatrick 1167e5dd7070Spatrickdef ObjCMethodFamilyDocs : Documentation { 1168e5dd7070Spatrick let Category = DocCatFunction; 1169e5dd7070Spatrick let Content = [{ 1170e5dd7070SpatrickMany methods in Objective-C have conventional meanings determined by their 1171e5dd7070Spatrickselectors. It is sometimes useful to be able to mark a method as having a 1172e5dd7070Spatrickparticular conventional meaning despite not having the right selector, or as 1173e5dd7070Spatricknot having the conventional meaning that its selector would suggest. For these 1174e5dd7070Spatrickuse cases, we provide an attribute to specifically describe the "method family" 1175e5dd7070Spatrickthat a method belongs to. 1176e5dd7070Spatrick 1177e5dd7070Spatrick**Usage**: ``__attribute__((objc_method_family(X)))``, where ``X`` is one of 1178e5dd7070Spatrick``none``, ``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``. This 1179e5dd7070Spatrickattribute can only be placed at the end of a method declaration: 1180e5dd7070Spatrick 1181e5dd7070Spatrick.. code-block:: objc 1182e5dd7070Spatrick 1183e5dd7070Spatrick - (NSString *)initMyStringValue __attribute__((objc_method_family(none))); 1184e5dd7070Spatrick 1185e5dd7070SpatrickUsers who do not wish to change the conventional meaning of a method, and who 1186e5dd7070Spatrickmerely want to document its non-standard retain and release semantics, should 1187e5dd7070Spatrickuse the retaining behavior attributes (``ns_returns_retained``, 1188e5dd7070Spatrick``ns_returns_not_retained``, etc). 1189e5dd7070Spatrick 1190e5dd7070SpatrickQuery for this feature with ``__has_attribute(objc_method_family)``. 1191e5dd7070Spatrick }]; 1192e5dd7070Spatrick} 1193e5dd7070Spatrick 1194e5dd7070Spatrickdef RetainBehaviorDocs : Documentation { 1195e5dd7070Spatrick let Category = DocCatFunction; 1196e5dd7070Spatrick let Content = [{ 1197e5dd7070SpatrickThe behavior of a function with respect to reference counting for Foundation 1198e5dd7070Spatrick(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming 1199e5dd7070Spatrickconvention (e.g. functions starting with "get" are assumed to return at 1200e5dd7070Spatrick``+0``). 1201e5dd7070Spatrick 1202ec727ea7SpatrickIt can be overridden using a family of the following attributes. In 1203e5dd7070SpatrickObjective-C, the annotation ``__attribute__((ns_returns_retained))`` applied to 1204e5dd7070Spatricka function communicates that the object is returned at ``+1``, and the caller 1205e5dd7070Spatrickis responsible for freeing it. 1206ec727ea7SpatrickSimilarly, the annotation ``__attribute__((ns_returns_not_retained))`` 1207e5dd7070Spatrickspecifies that the object is returned at ``+0`` and the ownership remains with 1208e5dd7070Spatrickthe callee. 1209e5dd7070SpatrickThe annotation ``__attribute__((ns_consumes_self))`` specifies that 1210e5dd7070Spatrickthe Objective-C method call consumes the reference to ``self``, e.g. by 1211e5dd7070Spatrickattaching it to a supplied parameter. 1212e5dd7070SpatrickAdditionally, parameters can have an annotation 1213e5dd7070Spatrick``__attribute__((ns_consumed))``, which specifies that passing an owned object 1214e5dd7070Spatrickas that parameter effectively transfers the ownership, and the caller is no 1215e5dd7070Spatricklonger responsible for it. 1216e5dd7070SpatrickThese attributes affect code generation when interacting with ARC code, and 1217e5dd7070Spatrickthey are used by the Clang Static Analyzer. 1218e5dd7070Spatrick 1219e5dd7070SpatrickIn C programs using CoreFoundation, a similar set of attributes: 1220e5dd7070Spatrick``__attribute__((cf_returns_not_retained))``, 1221e5dd7070Spatrick``__attribute__((cf_returns_retained))`` and ``__attribute__((cf_consumed))`` 1222e5dd7070Spatrickhave the same respective semantics when applied to CoreFoundation objects. 1223e5dd7070SpatrickThese attributes affect code generation when interacting with ARC code, and 1224e5dd7070Spatrickthey are used by the Clang Static Analyzer. 1225e5dd7070Spatrick 1226e5dd7070SpatrickFinally, in C++ interacting with XNU kernel (objects inheriting from OSObject), 1227e5dd7070Spatrickthe same attribute family is present: 1228e5dd7070Spatrick``__attribute__((os_returns_not_retained))``, 1229e5dd7070Spatrick``__attribute__((os_returns_retained))`` and ``__attribute__((os_consumed))``, 1230e5dd7070Spatrickwith the same respective semantics. 1231e5dd7070SpatrickSimilar to ``__attribute__((ns_consumes_self))``, 1232e5dd7070Spatrick``__attribute__((os_consumes_this))`` specifies that the method call consumes 1233e5dd7070Spatrickthe reference to "this" (e.g., when attaching it to a different object supplied 1234e5dd7070Spatrickas a parameter). 1235e5dd7070SpatrickOut parameters (parameters the function is meant to write into, 1236e5dd7070Spatrickeither via pointers-to-pointers or references-to-pointers) 1237e5dd7070Spatrickmay be annotated with ``__attribute__((os_returns_retained))`` 1238e5dd7070Spatrickor ``__attribute__((os_returns_not_retained))`` which specifies that the object 1239e5dd7070Spatrickwritten into the out parameter should (or respectively should not) be released 1240e5dd7070Spatrickafter use. 1241e5dd7070SpatrickSince often out parameters may or may not be written depending on the exit 1242e5dd7070Spatrickcode of the function, 1243e5dd7070Spatrickannotations ``__attribute__((os_returns_retained_on_zero))`` 1244e5dd7070Spatrickand ``__attribute__((os_returns_retained_on_non_zero))`` specify that 1245e5dd7070Spatrickan out parameter at ``+1`` is written if and only if the function returns a zero 1246e5dd7070Spatrick(respectively non-zero) error code. 1247e5dd7070SpatrickObserve that return-code-dependent out parameter annotations are only 1248e5dd7070Spatrickavailable for retained out parameters, as non-retained object do not have to be 1249e5dd7070Spatrickreleased by the callee. 1250e5dd7070SpatrickThese attributes are only used by the Clang Static Analyzer. 1251e5dd7070Spatrick 1252e5dd7070SpatrickThe family of attributes ``X_returns_X_retained`` can be added to functions, 1253e5dd7070SpatrickC++ methods, and Objective-C methods and properties. 1254e5dd7070SpatrickAttributes ``X_consumed`` can be added to parameters of methods, functions, 1255e5dd7070Spatrickand Objective-C methods. 1256e5dd7070Spatrick }]; 1257e5dd7070Spatrick} 1258e5dd7070Spatrick 1259e5dd7070Spatrickdef NoDebugDocs : Documentation { 1260e5dd7070Spatrick let Category = DocCatVariable; 1261e5dd7070Spatrick let Content = [{ 1262e5dd7070SpatrickThe ``nodebug`` attribute allows you to suppress debugging information for a 1263a9ac8606Spatrickfunction or method, for a variable that is not a parameter or a non-static 1264a9ac8606Spatrickdata member, or for a typedef or using declaration. 1265a9ac8606Spatrick }]; 1266a9ac8606Spatrick} 1267a9ac8606Spatrick 1268a9ac8606Spatrickdef StandaloneDebugDocs : Documentation { 1269a9ac8606Spatrick let Category = DocCatVariable; 1270a9ac8606Spatrick let Content = [{ 1271a9ac8606SpatrickThe ``standalone_debug`` attribute causes debug info to be emitted for a record 1272a9ac8606Spatricktype regardless of the debug info optimizations that are enabled with 1273a9ac8606Spatrick-fno-standalone-debug. This attribute only has an effect when debug info 1274a9ac8606Spatrickoptimizations are enabled (e.g. with -fno-standalone-debug), and is C++-only. 1275e5dd7070Spatrick }]; 1276e5dd7070Spatrick} 1277e5dd7070Spatrick 1278e5dd7070Spatrickdef NoDuplicateDocs : Documentation { 1279e5dd7070Spatrick let Category = DocCatFunction; 1280e5dd7070Spatrick let Content = [{ 1281e5dd7070SpatrickThe ``noduplicate`` attribute can be placed on function declarations to control 1282e5dd7070Spatrickwhether function calls to this function can be duplicated or not as a result of 1283e5dd7070Spatrickoptimizations. This is required for the implementation of functions with 1284e5dd7070Spatrickcertain special requirements, like the OpenCL "barrier" function, that might 1285e5dd7070Spatrickneed to be run concurrently by all the threads that are executing in lockstep 1286e5dd7070Spatrickon the hardware. For example this attribute applied on the function 1287e5dd7070Spatrick"nodupfunc" in the code below avoids that: 1288e5dd7070Spatrick 1289e5dd7070Spatrick.. code-block:: c 1290e5dd7070Spatrick 1291e5dd7070Spatrick void nodupfunc() __attribute__((noduplicate)); 1292e5dd7070Spatrick // Setting it as a C++11 attribute is also valid 1293e5dd7070Spatrick // void nodupfunc() [[clang::noduplicate]]; 1294e5dd7070Spatrick void foo(); 1295e5dd7070Spatrick void bar(); 1296e5dd7070Spatrick 1297e5dd7070Spatrick nodupfunc(); 1298e5dd7070Spatrick if (a > n) { 1299e5dd7070Spatrick foo(); 1300e5dd7070Spatrick } else { 1301e5dd7070Spatrick bar(); 1302e5dd7070Spatrick } 1303e5dd7070Spatrick 1304e5dd7070Spatrickgets possibly modified by some optimizations into code similar to this: 1305e5dd7070Spatrick 1306e5dd7070Spatrick.. code-block:: c 1307e5dd7070Spatrick 1308e5dd7070Spatrick if (a > n) { 1309e5dd7070Spatrick nodupfunc(); 1310e5dd7070Spatrick foo(); 1311e5dd7070Spatrick } else { 1312e5dd7070Spatrick nodupfunc(); 1313e5dd7070Spatrick bar(); 1314e5dd7070Spatrick } 1315e5dd7070Spatrick 1316e5dd7070Spatrickwhere the call to "nodupfunc" is duplicated and sunk into the two branches 1317e5dd7070Spatrickof the condition. 1318e5dd7070Spatrick }]; 1319e5dd7070Spatrick} 1320e5dd7070Spatrick 1321e5dd7070Spatrickdef ConvergentDocs : Documentation { 1322e5dd7070Spatrick let Category = DocCatFunction; 1323e5dd7070Spatrick let Content = [{ 1324e5dd7070SpatrickThe ``convergent`` attribute can be placed on a function declaration. It is 1325e5dd7070Spatricktranslated into the LLVM ``convergent`` attribute, which indicates that the call 1326e5dd7070Spatrickinstructions of a function with this attribute cannot be made control-dependent 1327e5dd7070Spatrickon any additional values. 1328e5dd7070Spatrick 1329e5dd7070SpatrickIn languages designed for SPMD/SIMT programming model, e.g. OpenCL or CUDA, 1330e5dd7070Spatrickthe call instructions of a function with this attribute must be executed by 1331e5dd7070Spatrickall work items or threads in a work group or sub group. 1332e5dd7070Spatrick 1333e5dd7070SpatrickThis attribute is different from ``noduplicate`` because it allows duplicating 1334e5dd7070Spatrickfunction calls if it can be proved that the duplicated function calls are 1335e5dd7070Spatricknot made control-dependent on any additional values, e.g., unrolling a loop 1336e5dd7070Spatrickexecuted by all work items. 1337e5dd7070Spatrick 1338e5dd7070SpatrickSample usage: 1339a9ac8606Spatrick 1340e5dd7070Spatrick.. code-block:: c 1341e5dd7070Spatrick 1342e5dd7070Spatrick void convfunc(void) __attribute__((convergent)); 1343e5dd7070Spatrick // Setting it as a C++11 attribute is also valid in a C++ program. 1344e5dd7070Spatrick // void convfunc(void) [[clang::convergent]]; 1345e5dd7070Spatrick 1346e5dd7070Spatrick }]; 1347e5dd7070Spatrick} 1348e5dd7070Spatrick 1349e5dd7070Spatrickdef NoSplitStackDocs : Documentation { 1350e5dd7070Spatrick let Category = DocCatFunction; 1351e5dd7070Spatrick let Content = [{ 1352e5dd7070SpatrickThe ``no_split_stack`` attribute disables the emission of the split stack 1353e5dd7070Spatrickpreamble for a particular function. It has no effect if ``-fsplit-stack`` 1354e5dd7070Spatrickis not specified. 1355e5dd7070Spatrick }]; 1356e5dd7070Spatrick} 1357e5dd7070Spatrick 1358e5dd7070Spatrickdef NoUniqueAddressDocs : Documentation { 1359e5dd7070Spatrick let Category = DocCatField; 1360e5dd7070Spatrick let Content = [{ 1361e5dd7070SpatrickThe ``no_unique_address`` attribute allows tail padding in a non-static data 1362e5dd7070Spatrickmember to overlap other members of the enclosing class (and in the special 1363e5dd7070Spatrickcase when the type is empty, permits it to fully overlap other members). 1364e5dd7070SpatrickThe field is laid out as if a base class were encountered at the corresponding 1365e5dd7070Spatrickpoint within the class (except that it does not share a vptr with the enclosing 1366e5dd7070Spatrickobject). 1367e5dd7070Spatrick 1368e5dd7070SpatrickExample usage: 1369e5dd7070Spatrick 1370e5dd7070Spatrick.. code-block:: c++ 1371e5dd7070Spatrick 1372e5dd7070Spatrick template<typename T, typename Alloc> struct my_vector { 1373e5dd7070Spatrick T *p; 1374e5dd7070Spatrick [[no_unique_address]] Alloc alloc; 1375e5dd7070Spatrick // ... 1376e5dd7070Spatrick }; 1377e5dd7070Spatrick static_assert(sizeof(my_vector<int, std::allocator<int>>) == sizeof(int*)); 1378e5dd7070Spatrick 1379e5dd7070Spatrick``[[no_unique_address]]`` is a standard C++20 attribute. Clang supports its use 1380e5dd7070Spatrickin C++11 onwards. 1381e5dd7070Spatrick }]; 1382e5dd7070Spatrick} 1383e5dd7070Spatrick 1384e5dd7070Spatrickdef ObjCRequiresSuperDocs : Documentation { 1385e5dd7070Spatrick let Category = DocCatFunction; 1386e5dd7070Spatrick let Content = [{ 1387e5dd7070SpatrickSome Objective-C classes allow a subclass to override a particular method in a 1388e5dd7070Spatrickparent class but expect that the overriding method also calls the overridden 1389e5dd7070Spatrickmethod in the parent class. For these cases, we provide an attribute to 1390e5dd7070Spatrickdesignate that a method requires a "call to ``super``" in the overriding 1391e5dd7070Spatrickmethod in the subclass. 1392e5dd7070Spatrick 1393e5dd7070Spatrick**Usage**: ``__attribute__((objc_requires_super))``. This attribute can only 1394e5dd7070Spatrickbe placed at the end of a method declaration: 1395e5dd7070Spatrick 1396e5dd7070Spatrick.. code-block:: objc 1397e5dd7070Spatrick 1398e5dd7070Spatrick - (void)foo __attribute__((objc_requires_super)); 1399e5dd7070Spatrick 1400e5dd7070SpatrickThis attribute can only be applied the method declarations within a class, and 1401e5dd7070Spatricknot a protocol. Currently this attribute does not enforce any placement of 1402e5dd7070Spatrickwhere the call occurs in the overriding method (such as in the case of 1403e5dd7070Spatrick``-dealloc`` where the call must appear at the end). It checks only that it 1404e5dd7070Spatrickexists. 1405e5dd7070Spatrick 1406e5dd7070SpatrickNote that on both OS X and iOS that the Foundation framework provides a 1407e5dd7070Spatrickconvenience macro ``NS_REQUIRES_SUPER`` that provides syntactic sugar for this 1408e5dd7070Spatrickattribute: 1409e5dd7070Spatrick 1410e5dd7070Spatrick.. code-block:: objc 1411e5dd7070Spatrick 1412e5dd7070Spatrick - (void)foo NS_REQUIRES_SUPER; 1413e5dd7070Spatrick 1414e5dd7070SpatrickThis macro is conditionally defined depending on the compiler's support for 1415e5dd7070Spatrickthis attribute. If the compiler does not support the attribute the macro 1416e5dd7070Spatrickexpands to nothing. 1417e5dd7070Spatrick 1418e5dd7070SpatrickOperationally, when a method has this annotation the compiler will warn if the 1419e5dd7070Spatrickimplementation of an override in a subclass does not call super. For example: 1420e5dd7070Spatrick 1421e5dd7070Spatrick.. code-block:: objc 1422e5dd7070Spatrick 1423e5dd7070Spatrick warning: method possibly missing a [super AnnotMeth] call 1424e5dd7070Spatrick - (void) AnnotMeth{}; 1425e5dd7070Spatrick ^ 1426e5dd7070Spatrick }]; 1427e5dd7070Spatrick} 1428e5dd7070Spatrick 1429e5dd7070Spatrickdef ObjCRuntimeNameDocs : Documentation { 1430e5dd7070Spatrick let Category = DocCatDecl; 1431e5dd7070Spatrick let Content = [{ 1432e5dd7070SpatrickBy default, the Objective-C interface or protocol identifier is used 1433ec727ea7Spatrickin the metadata name for that object. The ``objc_runtime_name`` 1434e5dd7070Spatrickattribute allows annotated interfaces or protocols to use the 1435e5dd7070Spatrickspecified string argument in the object's metadata name instead of the 1436e5dd7070Spatrickdefault name. 1437e5dd7070Spatrick 1438e5dd7070Spatrick**Usage**: ``__attribute__((objc_runtime_name("MyLocalName")))``. This attribute 1439e5dd7070Spatrickcan only be placed before an @protocol or @interface declaration: 1440e5dd7070Spatrick 1441e5dd7070Spatrick.. code-block:: objc 1442e5dd7070Spatrick 1443e5dd7070Spatrick __attribute__((objc_runtime_name("MyLocalName"))) 1444e5dd7070Spatrick @interface Message 1445e5dd7070Spatrick @end 1446e5dd7070Spatrick 1447e5dd7070Spatrick }]; 1448e5dd7070Spatrick} 1449e5dd7070Spatrick 1450e5dd7070Spatrickdef ObjCRuntimeVisibleDocs : Documentation { 1451e5dd7070Spatrick let Category = DocCatDecl; 1452e5dd7070Spatrick let Content = [{ 1453e5dd7070SpatrickThis attribute specifies that the Objective-C class to which it applies is 1454e5dd7070Spatrickvisible to the Objective-C runtime but not to the linker. Classes annotated 1455e5dd7070Spatrickwith this attribute cannot be subclassed and cannot have categories defined for 1456e5dd7070Spatrickthem. 1457e5dd7070Spatrick }]; 1458e5dd7070Spatrick} 1459e5dd7070Spatrick 1460e5dd7070Spatrickdef ObjCClassStubDocs : Documentation { 1461e5dd7070Spatrick let Category = DocCatType; 1462e5dd7070Spatrick let Content = [{ 1463e5dd7070SpatrickThis attribute specifies that the Objective-C class to which it applies is 1464e5dd7070Spatrickinstantiated at runtime. 1465e5dd7070Spatrick 1466e5dd7070SpatrickUnlike ``__attribute__((objc_runtime_visible))``, a class having this attribute 1467e5dd7070Spatrickstill has a "class stub" that is visible to the linker. This allows categories 1468e5dd7070Spatrickto be defined. Static message sends with the class as a receiver use a special 1469e5dd7070Spatrickaccess pattern to ensure the class is lazily instantiated from the class stub. 1470e5dd7070Spatrick 1471e5dd7070SpatrickClasses annotated with this attribute cannot be subclassed and cannot have 1472e5dd7070Spatrickimplementations defined for them. This attribute is intended for use in 1473e5dd7070SpatrickSwift-generated headers for classes defined in Swift. 1474e5dd7070Spatrick 1475e5dd7070SpatrickAdding or removing this attribute to a class is an ABI-breaking change. 1476e5dd7070Spatrick }]; 1477e5dd7070Spatrick} 1478e5dd7070Spatrick 1479e5dd7070Spatrickdef ObjCBoxableDocs : Documentation { 1480e5dd7070Spatrick let Category = DocCatDecl; 1481e5dd7070Spatrick let Content = [{ 1482e5dd7070SpatrickStructs and unions marked with the ``objc_boxable`` attribute can be used 1483e5dd7070Spatrickwith the Objective-C boxed expression syntax, ``@(...)``. 1484e5dd7070Spatrick 1485e5dd7070Spatrick**Usage**: ``__attribute__((objc_boxable))``. This attribute 1486e5dd7070Spatrickcan only be placed on a declaration of a trivially-copyable struct or union: 1487e5dd7070Spatrick 1488e5dd7070Spatrick.. code-block:: objc 1489e5dd7070Spatrick 1490e5dd7070Spatrick struct __attribute__((objc_boxable)) some_struct { 1491e5dd7070Spatrick int i; 1492e5dd7070Spatrick }; 1493e5dd7070Spatrick union __attribute__((objc_boxable)) some_union { 1494e5dd7070Spatrick int i; 1495e5dd7070Spatrick float f; 1496e5dd7070Spatrick }; 1497e5dd7070Spatrick typedef struct __attribute__((objc_boxable)) _some_struct some_struct; 1498e5dd7070Spatrick 1499e5dd7070Spatrick // ... 1500e5dd7070Spatrick 1501e5dd7070Spatrick some_struct ss; 1502e5dd7070Spatrick NSValue *boxed = @(ss); 1503e5dd7070Spatrick 1504e5dd7070Spatrick }]; 1505e5dd7070Spatrick} 1506e5dd7070Spatrick 1507e5dd7070Spatrickdef AvailabilityDocs : Documentation { 1508e5dd7070Spatrick let Category = DocCatFunction; 1509e5dd7070Spatrick let Content = [{ 1510e5dd7070SpatrickThe ``availability`` attribute can be placed on declarations to describe the 1511e5dd7070Spatricklifecycle of that declaration relative to operating system versions. Consider 1512e5dd7070Spatrickthe function declaration for a hypothetical function ``f``: 1513e5dd7070Spatrick 1514e5dd7070Spatrick.. code-block:: c++ 1515e5dd7070Spatrick 1516e5dd7070Spatrick void f(void) __attribute__((availability(macos,introduced=10.4,deprecated=10.6,obsoleted=10.7))); 1517e5dd7070Spatrick 1518e5dd7070SpatrickThe availability attribute states that ``f`` was introduced in macOS 10.4, 1519e5dd7070Spatrickdeprecated in macOS 10.6, and obsoleted in macOS 10.7. This information 1520e5dd7070Spatrickis used by Clang to determine when it is safe to use ``f``: for example, if 1521e5dd7070SpatrickClang is instructed to compile code for macOS 10.5, a call to ``f()`` 1522e5dd7070Spatricksucceeds. If Clang is instructed to compile code for macOS 10.6, the call 1523e5dd7070Spatricksucceeds but Clang emits a warning specifying that the function is deprecated. 1524e5dd7070SpatrickFinally, if Clang is instructed to compile code for macOS 10.7, the call 1525e5dd7070Spatrickfails because ``f()`` is no longer available. 1526e5dd7070Spatrick 1527e5dd7070SpatrickThe availability attribute is a comma-separated list starting with the 1528e5dd7070Spatrickplatform name and then including clauses specifying important milestones in the 1529e5dd7070Spatrickdeclaration's lifetime (in any order) along with additional information. Those 1530e5dd7070Spatrickclauses can be: 1531e5dd7070Spatrick 1532e5dd7070Spatrickintroduced=\ *version* 1533e5dd7070Spatrick The first version in which this declaration was introduced. 1534e5dd7070Spatrick 1535e5dd7070Spatrickdeprecated=\ *version* 1536e5dd7070Spatrick The first version in which this declaration was deprecated, meaning that 1537e5dd7070Spatrick users should migrate away from this API. 1538e5dd7070Spatrick 1539e5dd7070Spatrickobsoleted=\ *version* 1540e5dd7070Spatrick The first version in which this declaration was obsoleted, meaning that it 1541e5dd7070Spatrick was removed completely and can no longer be used. 1542e5dd7070Spatrick 1543e5dd7070Spatrickunavailable 1544e5dd7070Spatrick This declaration is never available on this platform. 1545e5dd7070Spatrick 1546e5dd7070Spatrickmessage=\ *string-literal* 1547e5dd7070Spatrick Additional message text that Clang will provide when emitting a warning or 1548e5dd7070Spatrick error about use of a deprecated or obsoleted declaration. Useful to direct 1549e5dd7070Spatrick users to replacement APIs. 1550e5dd7070Spatrick 1551e5dd7070Spatrickreplacement=\ *string-literal* 1552e5dd7070Spatrick Additional message text that Clang will use to provide Fix-It when emitting 1553e5dd7070Spatrick a warning about use of a deprecated declaration. The Fix-It will replace 1554e5dd7070Spatrick the deprecated declaration with the new declaration specified. 1555e5dd7070Spatrick 1556e5dd7070SpatrickMultiple availability attributes can be placed on a declaration, which may 1557e5dd7070Spatrickcorrespond to different platforms. For most platforms, the availability 1558e5dd7070Spatrickattribute with the platform corresponding to the target platform will be used; 1559e5dd7070Spatrickany others will be ignored. However, the availability for ``watchOS`` and 1560e5dd7070Spatrick``tvOS`` can be implicitly inferred from an ``iOS`` availability attribute. 1561ec727ea7SpatrickAny explicit availability attributes for those platforms are still preferred over 1562e5dd7070Spatrickthe implicitly inferred availability attributes. If no availability attribute 1563e5dd7070Spatrickspecifies availability for the current target platform, the availability 1564e5dd7070Spatrickattributes are ignored. Supported platforms are: 1565e5dd7070Spatrick 1566e5dd7070Spatrick``ios`` 1567e5dd7070Spatrick Apple's iOS operating system. The minimum deployment target is specified by 1568e5dd7070Spatrick the ``-mios-version-min=*version*`` or ``-miphoneos-version-min=*version*`` 1569e5dd7070Spatrick command-line arguments. 1570e5dd7070Spatrick 1571e5dd7070Spatrick``macos`` 1572e5dd7070Spatrick Apple's macOS operating system. The minimum deployment target is 1573e5dd7070Spatrick specified by the ``-mmacosx-version-min=*version*`` command-line argument. 1574e5dd7070Spatrick ``macosx`` is supported for backward-compatibility reasons, but it is 1575e5dd7070Spatrick deprecated. 1576e5dd7070Spatrick 1577e5dd7070Spatrick``tvos`` 1578e5dd7070Spatrick Apple's tvOS operating system. The minimum deployment target is specified by 1579e5dd7070Spatrick the ``-mtvos-version-min=*version*`` command-line argument. 1580e5dd7070Spatrick 1581e5dd7070Spatrick``watchos`` 1582e5dd7070Spatrick Apple's watchOS operating system. The minimum deployment target is specified by 1583e5dd7070Spatrick the ``-mwatchos-version-min=*version*`` command-line argument. 1584e5dd7070Spatrick 1585*12c85518Srobert``driverkit`` 1586*12c85518Srobert Apple's DriverKit userspace kernel extensions. The minimum deployment target 1587*12c85518Srobert is specified as part of the triple. 1588*12c85518Srobert 1589e5dd7070SpatrickA declaration can typically be used even when deploying back to a platform 1590e5dd7070Spatrickversion prior to when the declaration was introduced. When this happens, the 1591e5dd7070Spatrickdeclaration is `weakly linked 1592e5dd7070Spatrick<https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html>`_, 1593e5dd7070Spatrickas if the ``weak_import`` attribute were added to the declaration. A 1594e5dd7070Spatrickweakly-linked declaration may or may not be present a run-time, and a program 1595e5dd7070Spatrickcan determine whether the declaration is present by checking whether the 1596e5dd7070Spatrickaddress of that declaration is non-NULL. 1597e5dd7070Spatrick 1598e5dd7070SpatrickThe flag ``strict`` disallows using API when deploying back to a 1599e5dd7070Spatrickplatform version prior to when the declaration was introduced. An 1600e5dd7070Spatrickattempt to use such API before its introduction causes a hard error. 1601e5dd7070SpatrickWeakly-linking is almost always a better API choice, since it allows 1602e5dd7070Spatrickusers to query availability at runtime. 1603e5dd7070Spatrick 1604e5dd7070SpatrickIf there are multiple declarations of the same entity, the availability 1605e5dd7070Spatrickattributes must either match on a per-platform basis or later 1606e5dd7070Spatrickdeclarations must not have availability attributes for that 1607e5dd7070Spatrickplatform. For example: 1608e5dd7070Spatrick 1609e5dd7070Spatrick.. code-block:: c 1610e5dd7070Spatrick 1611e5dd7070Spatrick void g(void) __attribute__((availability(macos,introduced=10.4))); 1612e5dd7070Spatrick void g(void) __attribute__((availability(macos,introduced=10.4))); // okay, matches 1613e5dd7070Spatrick void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform 1614e5dd7070Spatrick void g(void); // okay, inherits both macos and ios availability from above. 1615e5dd7070Spatrick void g(void) __attribute__((availability(macos,introduced=10.5))); // error: mismatch 1616e5dd7070Spatrick 1617e5dd7070SpatrickWhen one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,: 1618e5dd7070Spatrick 1619e5dd7070Spatrick.. code-block:: objc 1620e5dd7070Spatrick 1621e5dd7070Spatrick @interface A 1622e5dd7070Spatrick - (id)method __attribute__((availability(macos,introduced=10.4))); 1623e5dd7070Spatrick - (id)method2 __attribute__((availability(macos,introduced=10.4))); 1624e5dd7070Spatrick @end 1625e5dd7070Spatrick 1626e5dd7070Spatrick @interface B : A 1627e5dd7070Spatrick - (id)method __attribute__((availability(macos,introduced=10.3))); // okay: method moved into base class later 1628e5dd7070Spatrick - (id)method __attribute__((availability(macos,introduced=10.5))); // error: this method was available via the base class in 10.4 1629e5dd7070Spatrick @end 1630e5dd7070Spatrick 1631e5dd7070SpatrickStarting with the macOS 10.12 SDK, the ``API_AVAILABLE`` macro from 1632e5dd7070Spatrick``<os/availability.h>`` can simplify the spelling: 1633e5dd7070Spatrick 1634e5dd7070Spatrick.. code-block:: objc 1635e5dd7070Spatrick 1636e5dd7070Spatrick @interface A 1637e5dd7070Spatrick - (id)method API_AVAILABLE(macos(10.11))); 1638e5dd7070Spatrick - (id)otherMethod API_AVAILABLE(macos(10.11), ios(11.0)); 1639e5dd7070Spatrick @end 1640e5dd7070Spatrick 1641e5dd7070SpatrickAvailability attributes can also be applied using a ``#pragma clang attribute``. 1642e5dd7070SpatrickAny explicit availability attribute whose platform corresponds to the target 1643e5dd7070Spatrickplatform is applied to a declaration regardless of the availability attributes 1644e5dd7070Spatrickspecified in the pragma. For example, in the code below, 1645e5dd7070Spatrick``hasExplicitAvailabilityAttribute`` will use the ``macOS`` availability 1646e5dd7070Spatrickattribute that is specified with the declaration, whereas 1647e5dd7070Spatrick``getsThePragmaAvailabilityAttribute`` will use the ``macOS`` availability 1648e5dd7070Spatrickattribute that is applied by the pragma. 1649e5dd7070Spatrick 1650e5dd7070Spatrick.. code-block:: c 1651e5dd7070Spatrick 1652e5dd7070Spatrick #pragma clang attribute push (__attribute__((availability(macOS, introduced=10.12))), apply_to=function) 1653e5dd7070Spatrick void getsThePragmaAvailabilityAttribute(void); 1654e5dd7070Spatrick void hasExplicitAvailabilityAttribute(void) __attribute__((availability(macos,introduced=10.4))); 1655e5dd7070Spatrick #pragma clang attribute pop 1656e5dd7070Spatrick 1657e5dd7070SpatrickFor platforms like ``watchOS`` and ``tvOS``, whose availability attributes can 1658e5dd7070Spatrickbe implicitly inferred from an ``iOS`` availability attribute, the logic is 1659e5dd7070Spatrickslightly more complex. The explicit and the pragma-applied availability 1660e5dd7070Spatrickattributes whose platform corresponds to the target platform are applied as 1661e5dd7070Spatrickdescribed in the previous paragraph. However, the implicitly inferred attributes 1662e5dd7070Spatrickare applied to a declaration only when there is no explicit or pragma-applied 1663e5dd7070Spatrickavailability attribute whose platform corresponds to the target platform. For 1664e5dd7070Spatrickexample, the function below will receive the ``tvOS`` availability from the 1665e5dd7070Spatrickpragma rather than using the inferred ``iOS`` availability from the declaration: 1666e5dd7070Spatrick 1667e5dd7070Spatrick.. code-block:: c 1668e5dd7070Spatrick 1669e5dd7070Spatrick #pragma clang attribute push (__attribute__((availability(tvOS, introduced=12.0))), apply_to=function) 1670e5dd7070Spatrick void getsThePragmaTVOSAvailabilityAttribute(void) __attribute__((availability(iOS,introduced=11.0))); 1671e5dd7070Spatrick #pragma clang attribute pop 1672e5dd7070Spatrick 1673ec727ea7SpatrickThe compiler is also able to apply implicitly inferred attributes from a pragma 1674e5dd7070Spatrickas well. For example, when targeting ``tvOS``, the function below will receive 1675e5dd7070Spatricka ``tvOS`` availability attribute that is implicitly inferred from the ``iOS`` 1676e5dd7070Spatrickavailability attribute applied by the pragma: 1677e5dd7070Spatrick 1678e5dd7070Spatrick.. code-block:: c 1679e5dd7070Spatrick 1680e5dd7070Spatrick #pragma clang attribute push (__attribute__((availability(iOS, introduced=12.0))), apply_to=function) 1681e5dd7070Spatrick void infersTVOSAvailabilityFromPragma(void); 1682e5dd7070Spatrick #pragma clang attribute pop 1683e5dd7070Spatrick 1684e5dd7070SpatrickThe implicit attributes that are inferred from explicitly specified attributes 1685e5dd7070Spatrickwhose platform corresponds to the target platform are applied to the declaration 1686e5dd7070Spatrickeven if there is an availability attribute that can be inferred from a pragma. 1687e5dd7070SpatrickFor example, the function below will receive the ``tvOS, introduced=11.0`` 1688e5dd7070Spatrickavailability that is inferred from the attribute on the declaration rather than 1689e5dd7070Spatrickinferring availability from the pragma: 1690e5dd7070Spatrick 1691e5dd7070Spatrick.. code-block:: c 1692e5dd7070Spatrick 1693e5dd7070Spatrick #pragma clang attribute push (__attribute__((availability(iOS, unavailable))), apply_to=function) 1694e5dd7070Spatrick void infersTVOSAvailabilityFromAttributeNextToDeclaration(void) 1695e5dd7070Spatrick __attribute__((availability(iOS,introduced=11.0))); 1696e5dd7070Spatrick #pragma clang attribute pop 1697e5dd7070Spatrick 1698e5dd7070SpatrickAlso see the documentation for `@available 1699e5dd7070Spatrick<http://clang.llvm.org/docs/LanguageExtensions.html#objective-c-available>`_ 1700e5dd7070Spatrick }]; 1701e5dd7070Spatrick} 1702e5dd7070Spatrick 1703e5dd7070Spatrickdef ExternalSourceSymbolDocs : Documentation { 1704e5dd7070Spatrick let Category = DocCatDecl; 1705e5dd7070Spatrick let Content = [{ 1706e5dd7070SpatrickThe ``external_source_symbol`` attribute specifies that a declaration originates 1707e5dd7070Spatrickfrom an external source and describes the nature of that source. 1708e5dd7070Spatrick 1709e5dd7070SpatrickThe fact that Clang is capable of recognizing declarations that were defined 1710e5dd7070Spatrickexternally can be used to provide better tooling support for mixed-language 1711e5dd7070Spatrickprojects or projects that rely on auto-generated code. For instance, an IDE that 1712e5dd7070Spatrickuses Clang and that supports mixed-language projects can use this attribute to 1713e5dd7070Spatrickprovide a correct 'jump-to-definition' feature. For a concrete example, 1714e5dd7070Spatrickconsider a protocol that's defined in a Swift file: 1715e5dd7070Spatrick 1716e5dd7070Spatrick.. code-block:: swift 1717e5dd7070Spatrick 1718e5dd7070Spatrick @objc public protocol SwiftProtocol { 1719e5dd7070Spatrick func method() 1720e5dd7070Spatrick } 1721e5dd7070Spatrick 1722e5dd7070SpatrickThis protocol can be used from Objective-C code by including a header file that 1723e5dd7070Spatrickwas generated by the Swift compiler. The declarations in that header can use 1724e5dd7070Spatrickthe ``external_source_symbol`` attribute to make Clang aware of the fact 1725e5dd7070Spatrickthat ``SwiftProtocol`` actually originates from a Swift module: 1726e5dd7070Spatrick 1727e5dd7070Spatrick.. code-block:: objc 1728e5dd7070Spatrick 1729e5dd7070Spatrick __attribute__((external_source_symbol(language="Swift",defined_in="module"))) 1730e5dd7070Spatrick @protocol SwiftProtocol 1731e5dd7070Spatrick @required 1732e5dd7070Spatrick - (void) method; 1733e5dd7070Spatrick @end 1734e5dd7070Spatrick 1735e5dd7070SpatrickConsequently, when 'jump-to-definition' is performed at a location that 1736e5dd7070Spatrickreferences ``SwiftProtocol``, the IDE can jump to the original definition in 1737e5dd7070Spatrickthe Swift source file rather than jumping to the Objective-C declaration in the 1738e5dd7070Spatrickauto-generated header file. 1739e5dd7070Spatrick 1740e5dd7070SpatrickThe ``external_source_symbol`` attribute is a comma-separated list that includes 1741e5dd7070Spatrickclauses that describe the origin and the nature of the particular declaration. 1742e5dd7070SpatrickThose clauses can be: 1743e5dd7070Spatrick 1744e5dd7070Spatricklanguage=\ *string-literal* 1745e5dd7070Spatrick The name of the source language in which this declaration was defined. 1746e5dd7070Spatrick 1747e5dd7070Spatrickdefined_in=\ *string-literal* 1748e5dd7070Spatrick The name of the source container in which the declaration was defined. The 1749e5dd7070Spatrick exact definition of source container is language-specific, e.g. Swift's 1750e5dd7070Spatrick source containers are modules, so ``defined_in`` should specify the Swift 1751e5dd7070Spatrick module name. 1752e5dd7070Spatrick 1753e5dd7070Spatrickgenerated_declaration 1754e5dd7070Spatrick This declaration was automatically generated by some tool. 1755e5dd7070Spatrick 1756e5dd7070SpatrickThe clauses can be specified in any order. The clauses that are listed above are 1757e5dd7070Spatrickall optional, but the attribute has to have at least one clause. 1758e5dd7070Spatrick }]; 1759e5dd7070Spatrick} 1760e5dd7070Spatrick 1761e5dd7070Spatrickdef ConstInitDocs : Documentation { 1762e5dd7070Spatrick let Category = DocCatVariable; 1763e5dd7070Spatrick let Heading = "require_constant_initialization, constinit (C++20)"; 1764e5dd7070Spatrick let Content = [{ 1765e5dd7070SpatrickThis attribute specifies that the variable to which it is attached is intended 1766e5dd7070Spatrickto have a `constant initializer <http://en.cppreference.com/w/cpp/language/constant_initialization>`_ 1767e5dd7070Spatrickaccording to the rules of [basic.start.static]. The variable is required to 1768e5dd7070Spatrickhave static or thread storage duration. If the initialization of the variable 1769e5dd7070Spatrickis not a constant initializer an error will be produced. This attribute may 1770e5dd7070Spatrickonly be used in C++; the ``constinit`` spelling is only accepted in C++20 1771e5dd7070Spatrickonwards. 1772e5dd7070Spatrick 1773e5dd7070SpatrickNote that in C++03 strict constant expression checking is not done. Instead 1774e5dd7070Spatrickthe attribute reports if Clang can emit the variable as a constant, even if it's 1775e5dd7070Spatricknot technically a 'constant initializer'. This behavior is non-portable. 1776e5dd7070Spatrick 1777e5dd7070SpatrickStatic storage duration variables with constant initializers avoid hard-to-find 1778e5dd7070Spatrickbugs caused by the indeterminate order of dynamic initialization. They can also 1779e5dd7070Spatrickbe safely used during dynamic initialization across translation units. 1780e5dd7070Spatrick 1781e5dd7070SpatrickThis attribute acts as a compile time assertion that the requirements 1782e5dd7070Spatrickfor constant initialization have been met. Since these requirements change 1783e5dd7070Spatrickbetween dialects and have subtle pitfalls it's important to fail fast instead 1784e5dd7070Spatrickof silently falling back on dynamic initialization. 1785e5dd7070Spatrick 1786e5dd7070SpatrickThe first use of the attribute on a variable must be part of, or precede, the 1787e5dd7070Spatrickinitializing declaration of the variable. C++20 requires the ``constinit`` 1788e5dd7070Spatrickspelling of the attribute to be present on the initializing declaration if it 1789e5dd7070Spatrickis used anywhere. The other spellings can be specified on a forward declaration 1790e5dd7070Spatrickand omitted on a later initializing declaration. 1791e5dd7070Spatrick 1792e5dd7070Spatrick.. code-block:: c++ 1793e5dd7070Spatrick 1794e5dd7070Spatrick // -std=c++14 1795e5dd7070Spatrick #define SAFE_STATIC [[clang::require_constant_initialization]] 1796e5dd7070Spatrick struct T { 1797e5dd7070Spatrick constexpr T(int) {} 1798e5dd7070Spatrick ~T(); // non-trivial 1799e5dd7070Spatrick }; 1800e5dd7070Spatrick SAFE_STATIC T x = {42}; // Initialization OK. Doesn't check destructor. 1801e5dd7070Spatrick SAFE_STATIC T y = 42; // error: variable does not have a constant initializer 1802e5dd7070Spatrick // copy initialization is not a constant expression on a non-literal type. 1803e5dd7070Spatrick }]; 1804e5dd7070Spatrick} 1805e5dd7070Spatrick 1806e5dd7070Spatrickdef WarnMaybeUnusedDocs : Documentation { 1807e5dd7070Spatrick let Category = DocCatVariable; 1808e5dd7070Spatrick let Heading = "maybe_unused, unused"; 1809e5dd7070Spatrick let Content = [{ 1810e5dd7070SpatrickWhen passing the ``-Wunused`` flag to Clang, entities that are unused by the 1811e5dd7070Spatrickprogram may be diagnosed. The ``[[maybe_unused]]`` (or 1812e5dd7070Spatrick``__attribute__((unused))``) attribute can be used to silence such diagnostics 1813e5dd7070Spatrickwhen the entity cannot be removed. For instance, a local variable may exist 1814e5dd7070Spatricksolely for use in an ``assert()`` statement, which makes the local variable 1815e5dd7070Spatrickunused when ``NDEBUG`` is defined. 1816e5dd7070Spatrick 1817e5dd7070SpatrickThe attribute may be applied to the declaration of a class, a typedef, a 1818e5dd7070Spatrickvariable, a function or method, a function parameter, an enumeration, an 1819e5dd7070Spatrickenumerator, a non-static data member, or a label. 1820e5dd7070Spatrick 1821e5dd7070Spatrick.. code-block: c++ 1822e5dd7070Spatrick #include <cassert> 1823e5dd7070Spatrick 1824e5dd7070Spatrick [[maybe_unused]] void f([[maybe_unused]] bool thing1, 1825e5dd7070Spatrick [[maybe_unused]] bool thing2) { 1826e5dd7070Spatrick [[maybe_unused]] bool b = thing1 && thing2; 1827e5dd7070Spatrick assert(b); 1828e5dd7070Spatrick } 1829e5dd7070Spatrick }]; 1830e5dd7070Spatrick} 1831e5dd7070Spatrick 1832e5dd7070Spatrickdef WarnUnusedResultsDocs : Documentation { 1833e5dd7070Spatrick let Category = DocCatFunction; 1834e5dd7070Spatrick let Heading = "nodiscard, warn_unused_result"; 1835e5dd7070Spatrick let Content = [{ 1836e5dd7070SpatrickClang supports the ability to diagnose when the results of a function call 1837e5dd7070Spatrickexpression are discarded under suspicious circumstances. A diagnostic is 1838e5dd7070Spatrickgenerated when a function or its return type is marked with ``[[nodiscard]]`` 1839e5dd7070Spatrick(or ``__attribute__((warn_unused_result))``) and the function call appears as a 1840e5dd7070Spatrickpotentially-evaluated discarded-value expression that is not explicitly cast to 1841ec727ea7Spatrick``void``. 1842e5dd7070Spatrick 1843e5dd7070SpatrickA string literal may optionally be provided to the attribute, which will be 1844e5dd7070Spatrickreproduced in any resulting diagnostics. Redeclarations using different forms 1845e5dd7070Spatrickof the attribute (with or without the string literal or with different string 1846e5dd7070Spatrickliteral contents) are allowed. If there are redeclarations of the entity with 1847e5dd7070Spatrickdiffering string literals, it is unspecified which one will be used by Clang 1848e5dd7070Spatrickin any resulting diagnostics. 1849e5dd7070Spatrick 1850e5dd7070Spatrick.. code-block: c++ 1851e5dd7070Spatrick struct [[nodiscard]] error_info { /*...*/ }; 1852e5dd7070Spatrick error_info enable_missile_safety_mode(); 1853e5dd7070Spatrick 1854e5dd7070Spatrick void launch_missiles(); 1855e5dd7070Spatrick void test_missiles() { 1856e5dd7070Spatrick enable_missile_safety_mode(); // diagnoses 1857e5dd7070Spatrick launch_missiles(); 1858e5dd7070Spatrick } 1859e5dd7070Spatrick error_info &foo(); 1860e5dd7070Spatrick void f() { foo(); } // Does not diagnose, error_info is a reference. 1861e5dd7070Spatrick 1862e5dd7070SpatrickAdditionally, discarded temporaries resulting from a call to a constructor 1863e5dd7070Spatrickmarked with ``[[nodiscard]]`` or a constructor of a type marked 1864e5dd7070Spatrick``[[nodiscard]]`` will also diagnose. This also applies to type conversions that 1865e5dd7070Spatrickuse the annotated ``[[nodiscard]]`` constructor or result in an annotated type. 1866e5dd7070Spatrick 1867e5dd7070Spatrick.. code-block: c++ 1868e5dd7070Spatrick struct [[nodiscard]] marked_type {/*..*/ }; 1869e5dd7070Spatrick struct marked_ctor { 1870e5dd7070Spatrick [[nodiscard]] marked_ctor(); 1871e5dd7070Spatrick marked_ctor(int); 1872e5dd7070Spatrick }; 1873e5dd7070Spatrick 1874e5dd7070Spatrick struct S { 1875e5dd7070Spatrick operator marked_type() const; 1876e5dd7070Spatrick [[nodiscard]] operator int() const; 1877e5dd7070Spatrick }; 1878e5dd7070Spatrick 1879e5dd7070Spatrick void usages() { 1880e5dd7070Spatrick marked_type(); // diagnoses. 1881e5dd7070Spatrick marked_ctor(); // diagnoses. 1882e5dd7070Spatrick marked_ctor(3); // Does not diagnose, int constructor isn't marked nodiscard. 1883e5dd7070Spatrick 1884e5dd7070Spatrick S s; 1885e5dd7070Spatrick static_cast<marked_type>(s); // diagnoses 1886e5dd7070Spatrick (int)s; // diagnoses 1887e5dd7070Spatrick } 1888e5dd7070Spatrick }]; 1889e5dd7070Spatrick} 1890e5dd7070Spatrick 1891e5dd7070Spatrickdef FallthroughDocs : Documentation { 1892e5dd7070Spatrick let Category = DocCatStmt; 1893e5dd7070Spatrick let Heading = "fallthrough"; 1894e5dd7070Spatrick let Content = [{ 1895e5dd7070SpatrickThe ``fallthrough`` (or ``clang::fallthrough``) attribute is used 1896e5dd7070Spatrickto annotate intentional fall-through 1897e5dd7070Spatrickbetween switch labels. It can only be applied to a null statement placed at a 1898e5dd7070Spatrickpoint of execution between any statement and the next switch label. It is 1899e5dd7070Spatrickcommon to mark these places with a specific comment, but this attribute is 1900e5dd7070Spatrickmeant to replace comments with a more strict annotation, which can be checked 1901e5dd7070Spatrickby the compiler. This attribute doesn't change semantics of the code and can 1902e5dd7070Spatrickbe used wherever an intended fall-through occurs. It is designed to mimic 1903e5dd7070Spatrickcontrol-flow statements like ``break;``, so it can be placed in most places 1904e5dd7070Spatrickwhere ``break;`` can, but only if there are no statements on the execution path 1905e5dd7070Spatrickbetween it and the next switch label. 1906e5dd7070Spatrick 1907e5dd7070SpatrickBy default, Clang does not warn on unannotated fallthrough from one ``switch`` 1908e5dd7070Spatrickcase to another. Diagnostics on fallthrough without a corresponding annotation 1909e5dd7070Spatrickcan be enabled with the ``-Wimplicit-fallthrough`` argument. 1910e5dd7070Spatrick 1911e5dd7070SpatrickHere is an example: 1912e5dd7070Spatrick 1913e5dd7070Spatrick.. code-block:: c++ 1914e5dd7070Spatrick 1915e5dd7070Spatrick // compile with -Wimplicit-fallthrough 1916e5dd7070Spatrick switch (n) { 1917e5dd7070Spatrick case 22: 1918e5dd7070Spatrick case 33: // no warning: no statements between case labels 1919e5dd7070Spatrick f(); 1920e5dd7070Spatrick case 44: // warning: unannotated fall-through 1921e5dd7070Spatrick g(); 1922e5dd7070Spatrick [[clang::fallthrough]]; 1923e5dd7070Spatrick case 55: // no warning 1924e5dd7070Spatrick if (x) { 1925e5dd7070Spatrick h(); 1926e5dd7070Spatrick break; 1927e5dd7070Spatrick } 1928e5dd7070Spatrick else { 1929e5dd7070Spatrick i(); 1930e5dd7070Spatrick [[clang::fallthrough]]; 1931e5dd7070Spatrick } 1932e5dd7070Spatrick case 66: // no warning 1933e5dd7070Spatrick p(); 1934e5dd7070Spatrick [[clang::fallthrough]]; // warning: fallthrough annotation does not 1935e5dd7070Spatrick // directly precede case label 1936e5dd7070Spatrick q(); 1937e5dd7070Spatrick case 77: // warning: unannotated fall-through 1938e5dd7070Spatrick r(); 1939e5dd7070Spatrick } 1940e5dd7070Spatrick }]; 1941e5dd7070Spatrick} 1942e5dd7070Spatrick 1943a9ac8606Spatrickdef LikelihoodDocs : Documentation { 1944a9ac8606Spatrick let Category = DocCatStmt; 1945a9ac8606Spatrick let Heading = "likely and unlikely"; 1946a9ac8606Spatrick let Content = [{ 1947a9ac8606SpatrickThe ``likely`` and ``unlikely`` attributes are used as compiler hints. 1948a9ac8606SpatrickThe attributes are used to aid the compiler to determine which branch is 1949a9ac8606Spatricklikely or unlikely to be taken. This is done by marking the branch substatement 1950a9ac8606Spatrickwith one of the two attributes. 1951a9ac8606Spatrick 1952a9ac8606SpatrickIt isn't allowed to annotate a single statement with both ``likely`` and 1953a9ac8606Spatrick``unlikely``. Annotating the ``true`` and ``false`` branch of an ``if`` 1954a9ac8606Spatrickstatement with the same likelihood attribute will result in a diagnostic and 1955a9ac8606Spatrickthe attributes are ignored on both branches. 1956a9ac8606Spatrick 1957a9ac8606SpatrickIn a ``switch`` statement it's allowed to annotate multiple ``case`` labels 1958a9ac8606Spatrickor the ``default`` label with the same likelihood attribute. This makes 1959a9ac8606Spatrick* all labels without an attribute have a neutral likelihood, 1960a9ac8606Spatrick* all labels marked ``[[likely]]`` have an equally positive likelihood, and 1961a9ac8606Spatrick* all labels marked ``[[unlikely]]`` have an equally negative likelihood. 1962a9ac8606SpatrickThe neutral likelihood is the more likely of path execution than the negative 1963a9ac8606Spatricklikelihood. The positive likelihood is the more likely of path of execution 1964a9ac8606Spatrickthan the neutral likelihood. 1965a9ac8606Spatrick 1966a9ac8606SpatrickThese attributes have no effect on the generated code when using 1967a9ac8606SpatrickPGO (Profile-Guided Optimization) or at optimization level 0. 1968a9ac8606Spatrick 1969a9ac8606SpatrickIn Clang, the attributes will be ignored if they're not placed on 1970a9ac8606Spatrick* the ``case`` or ``default`` label of a ``switch`` statement, 1971a9ac8606Spatrick* or on the substatement of an ``if`` or ``else`` statement, 1972a9ac8606Spatrick* or on the substatement of an ``for`` or ``while`` statement. 1973a9ac8606SpatrickThe C++ Standard recommends to honor them on every statement in the 1974a9ac8606Spatrickpath of execution, but that can be confusing: 1975a9ac8606Spatrick 1976a9ac8606Spatrick.. code-block:: c++ 1977a9ac8606Spatrick 1978a9ac8606Spatrick if (b) { 1979a9ac8606Spatrick [[unlikely]] --b; // In the path of execution, 1980a9ac8606Spatrick // this branch is considered unlikely. 1981a9ac8606Spatrick } 1982a9ac8606Spatrick 1983a9ac8606Spatrick if (b) { 1984a9ac8606Spatrick --b; 1985a9ac8606Spatrick if(b) 1986a9ac8606Spatrick return; 1987a9ac8606Spatrick [[unlikely]] --b; // Not in the path of execution, 1988a9ac8606Spatrick } // the branch has no likelihood information. 1989a9ac8606Spatrick 1990a9ac8606Spatrick if (b) { 1991a9ac8606Spatrick --b; 1992a9ac8606Spatrick foo(b); 1993a9ac8606Spatrick // Whether or not the next statement is in the path of execution depends 1994a9ac8606Spatrick // on the declaration of foo(): 1995a9ac8606Spatrick // In the path of execution: void foo(int); 1996a9ac8606Spatrick // Not in the path of execution: [[noreturn]] void foo(int); 1997a9ac8606Spatrick // This means the likelihood of the branch depends on the declaration 1998a9ac8606Spatrick // of foo(). 1999a9ac8606Spatrick [[unlikely]] --b; 2000a9ac8606Spatrick } 2001a9ac8606Spatrick 2002a9ac8606Spatrick 2003a9ac8606SpatrickBelow are some example usages of the likelihood attributes and their effects: 2004a9ac8606Spatrick 2005a9ac8606Spatrick.. code-block:: c++ 2006a9ac8606Spatrick 2007a9ac8606Spatrick if (b) [[likely]] { // Placement on the first statement in the branch. 2008a9ac8606Spatrick // The compiler will optimize to execute the code here. 2009a9ac8606Spatrick } else { 2010a9ac8606Spatrick } 2011a9ac8606Spatrick 2012a9ac8606Spatrick if (b) 2013a9ac8606Spatrick [[unlikely]] b++; // Placement on the first statement in the branch. 2014a9ac8606Spatrick else { 2015a9ac8606Spatrick // The compiler will optimize to execute the code here. 2016a9ac8606Spatrick } 2017a9ac8606Spatrick 2018a9ac8606Spatrick if (b) { 2019a9ac8606Spatrick [[unlikely]] b++; // Placement on the second statement in the branch. 2020a9ac8606Spatrick } // The attribute will be ignored. 2021a9ac8606Spatrick 2022a9ac8606Spatrick if (b) [[likely]] { 2023a9ac8606Spatrick [[unlikely]] b++; // No contradiction since the second attribute 2024a9ac8606Spatrick } // is ignored. 2025a9ac8606Spatrick 2026a9ac8606Spatrick if (b) 2027a9ac8606Spatrick ; 2028a9ac8606Spatrick else [[likely]] { 2029a9ac8606Spatrick // The compiler will optimize to execute the code here. 2030a9ac8606Spatrick } 2031a9ac8606Spatrick 2032a9ac8606Spatrick if (b) 2033a9ac8606Spatrick ; 2034a9ac8606Spatrick else 2035a9ac8606Spatrick // The compiler will optimize to execute the next statement. 2036a9ac8606Spatrick [[likely]] b = f(); 2037a9ac8606Spatrick 2038a9ac8606Spatrick if (b) [[likely]]; // Both branches are likely. A diagnostic is issued 2039a9ac8606Spatrick else [[likely]]; // and the attributes are ignored. 2040a9ac8606Spatrick 2041a9ac8606Spatrick if (b) 2042a9ac8606Spatrick [[likely]] int i = 5; // Issues a diagnostic since the attribute 2043a9ac8606Spatrick // isn't allowed on a declaration. 2044a9ac8606Spatrick 2045a9ac8606Spatrick switch (i) { 2046a9ac8606Spatrick [[likely]] case 1: // This value is likely 2047a9ac8606Spatrick ... 2048a9ac8606Spatrick break; 2049a9ac8606Spatrick 2050a9ac8606Spatrick [[unlikely]] case 2: // This value is unlikely 2051a9ac8606Spatrick ... 2052a9ac8606Spatrick [[fallthrough]]; 2053a9ac8606Spatrick 2054a9ac8606Spatrick case 3: // No likelihood attribute 2055a9ac8606Spatrick ... 2056a9ac8606Spatrick [[likely]] break; // No effect 2057a9ac8606Spatrick 2058a9ac8606Spatrick case 4: [[likely]] { // attribute on substatement has no effect 2059a9ac8606Spatrick ... 2060a9ac8606Spatrick break; 2061a9ac8606Spatrick } 2062a9ac8606Spatrick 2063a9ac8606Spatrick [[unlikely]] default: // All other values are unlikely 2064a9ac8606Spatrick ... 2065a9ac8606Spatrick break; 2066a9ac8606Spatrick } 2067a9ac8606Spatrick 2068a9ac8606Spatrick switch (i) { 2069a9ac8606Spatrick [[likely]] case 0: // This value and code path is likely 2070a9ac8606Spatrick ... 2071a9ac8606Spatrick [[fallthrough]]; 2072a9ac8606Spatrick 2073a9ac8606Spatrick case 1: // No likelihood attribute, code path is neutral 2074a9ac8606Spatrick break; // falling through has no effect on the likelihood 2075a9ac8606Spatrick 2076a9ac8606Spatrick case 2: // No likelihood attribute, code path is neutral 2077a9ac8606Spatrick [[fallthrough]]; 2078a9ac8606Spatrick 2079a9ac8606Spatrick [[unlikely]] default: // This value and code path are both unlikely 2080a9ac8606Spatrick break; 2081a9ac8606Spatrick } 2082a9ac8606Spatrick 2083a9ac8606Spatrick for(int i = 0; i != size; ++i) [[likely]] { 2084a9ac8606Spatrick ... // The loop is the likely path of execution 2085a9ac8606Spatrick } 2086a9ac8606Spatrick 2087a9ac8606Spatrick for(const auto &E : Elements) [[likely]] { 2088a9ac8606Spatrick ... // The loop is the likely path of execution 2089a9ac8606Spatrick } 2090a9ac8606Spatrick 2091a9ac8606Spatrick while(i != size) [[unlikely]] { 2092a9ac8606Spatrick ... // The loop is the unlikely path of execution 2093a9ac8606Spatrick } // The generated code will optimize to skip the loop body 2094a9ac8606Spatrick 2095a9ac8606Spatrick while(true) [[unlikely]] { 2096a9ac8606Spatrick ... // The attribute has no effect 2097a9ac8606Spatrick } // Clang elides the comparison and generates an infinite 2098a9ac8606Spatrick // loop 2099a9ac8606Spatrick 2100a9ac8606Spatrick }]; 2101a9ac8606Spatrick} 2102a9ac8606Spatrick 2103e5dd7070Spatrickdef ARMInterruptDocs : Documentation { 2104e5dd7070Spatrick let Category = DocCatFunction; 2105e5dd7070Spatrick let Heading = "interrupt (ARM)"; 2106e5dd7070Spatrick let Content = [{ 2107e5dd7070SpatrickClang supports the GNU style ``__attribute__((interrupt("TYPE")))`` attribute on 2108e5dd7070SpatrickARM targets. This attribute may be attached to a function definition and 2109e5dd7070Spatrickinstructs the backend to generate appropriate function entry/exit code so that 2110e5dd7070Spatrickit can be used directly as an interrupt service routine. 2111e5dd7070Spatrick 2112e5dd7070SpatrickThe parameter passed to the interrupt attribute is optional, but if 2113e5dd7070Spatrickprovided it must be a string literal with one of the following values: "IRQ", 2114e5dd7070Spatrick"FIQ", "SWI", "ABORT", "UNDEF". 2115e5dd7070Spatrick 2116e5dd7070SpatrickThe semantics are as follows: 2117e5dd7070Spatrick 2118e5dd7070Spatrick- If the function is AAPCS, Clang instructs the backend to realign the stack to 2119e5dd7070Spatrick 8 bytes on entry. This is a general requirement of the AAPCS at public 2120e5dd7070Spatrick interfaces, but may not hold when an exception is taken. Doing this allows 2121e5dd7070Spatrick other AAPCS functions to be called. 2122e5dd7070Spatrick- If the CPU is M-class this is all that needs to be done since the architecture 2123e5dd7070Spatrick itself is designed in such a way that functions obeying the normal AAPCS ABI 2124e5dd7070Spatrick constraints are valid exception handlers. 2125e5dd7070Spatrick- If the CPU is not M-class, the prologue and epilogue are modified to save all 2126e5dd7070Spatrick non-banked registers that are used, so that upon return the user-mode state 2127e5dd7070Spatrick will not be corrupted. Note that to avoid unnecessary overhead, only 2128e5dd7070Spatrick general-purpose (integer) registers are saved in this way. If VFP operations 2129e5dd7070Spatrick are needed, that state must be saved manually. 2130e5dd7070Spatrick 2131e5dd7070Spatrick Specifically, interrupt kinds other than "FIQ" will save all core registers 2132e5dd7070Spatrick except "lr" and "sp". "FIQ" interrupts will save r0-r7. 2133e5dd7070Spatrick- If the CPU is not M-class, the return instruction is changed to one of the 2134e5dd7070Spatrick canonical sequences permitted by the architecture for exception return. Where 2135e5dd7070Spatrick possible the function itself will make the necessary "lr" adjustments so that 2136e5dd7070Spatrick the "preferred return address" is selected. 2137e5dd7070Spatrick 2138e5dd7070Spatrick Unfortunately the compiler is unable to make this guarantee for an "UNDEF" 2139e5dd7070Spatrick handler, where the offset from "lr" to the preferred return address depends on 2140e5dd7070Spatrick the execution state of the code which generated the exception. In this case 2141e5dd7070Spatrick a sequence equivalent to "movs pc, lr" will be used. 2142e5dd7070Spatrick }]; 2143e5dd7070Spatrick} 2144e5dd7070Spatrick 2145e5dd7070Spatrickdef BPFPreserveAccessIndexDocs : Documentation { 2146e5dd7070Spatrick let Category = DocCatFunction; 2147e5dd7070Spatrick let Content = [{ 2148e5dd7070SpatrickClang supports the ``__attribute__((preserve_access_index))`` 2149e5dd7070Spatrickattribute for the BPF target. This attribute may be attached to a 2150e5dd7070Spatrickstruct or union declaration, where if -g is specified, it enables 2151ec727ea7Spatrickpreserving struct or union member access debuginfo indices of this 2152a9ac8606Spatrickstruct or union, similar to clang ``__builtin_preserve_access_index()``. 2153e5dd7070Spatrick }]; 2154e5dd7070Spatrick} 2155*12c85518Srobertdef BTFDeclTagDocs : Documentation { 2156*12c85518Srobert let Category = DocCatFunction; 2157*12c85518Srobert let Content = [{ 2158*12c85518SrobertClang supports the ``__attribute__((btf_decl_tag("ARGUMENT")))`` attribute for 2159*12c85518Srobertall targets. This attribute may be attached to a struct/union, struct/union 2160*12c85518Srobertfield, function, function parameter, variable or typedef declaration. If -g is 2161*12c85518Srobertspecified, the ``ARGUMENT`` info will be preserved in IR and be emitted to 2162*12c85518Srobertdwarf. For BPF targets, the ``ARGUMENT`` info will be emitted to .BTF ELF 2163*12c85518Srobertsection too. 2164*12c85518Srobert }]; 2165*12c85518Srobert} 2166*12c85518Srobert 2167*12c85518Srobertdef BTFTypeTagDocs : Documentation { 2168*12c85518Srobert let Category = DocCatType; 2169*12c85518Srobert let Content = [{ 2170*12c85518SrobertClang supports the ``__attribute__((btf_type_tag("ARGUMENT")))`` attribute for 2171*12c85518Srobertall targets. It only has effect when ``-g`` is specified on the command line and 2172*12c85518Srobertis currently silently ignored when not applied to a pointer type (note: this 2173*12c85518Srobertscenario may be diagnosed in the future). 2174*12c85518Srobert 2175*12c85518SrobertThe ``ARGUMENT`` string will be preserved in IR and emitted to DWARF for the 2176*12c85518Sroberttypes used in variable declarations, function declarations, or typedef 2177*12c85518Srobertdeclarations. 2178*12c85518Srobert 2179*12c85518SrobertFor BPF targets, the ``ARGUMENT`` string will also be emitted to .BTF ELF 2180*12c85518Srobertsection. 2181*12c85518Srobert }]; 2182*12c85518Srobert} 2183e5dd7070Spatrick 2184e5dd7070Spatrickdef MipsInterruptDocs : Documentation { 2185e5dd7070Spatrick let Category = DocCatFunction; 2186e5dd7070Spatrick let Heading = "interrupt (MIPS)"; 2187e5dd7070Spatrick let Content = [{ 2188e5dd7070SpatrickClang supports the GNU style ``__attribute__((interrupt("ARGUMENT")))`` attribute on 2189e5dd7070SpatrickMIPS targets. This attribute may be attached to a function definition and instructs 2190e5dd7070Spatrickthe backend to generate appropriate function entry/exit code so that it can be used 2191e5dd7070Spatrickdirectly as an interrupt service routine. 2192e5dd7070Spatrick 2193e5dd7070SpatrickBy default, the compiler will produce a function prologue and epilogue suitable for 2194e5dd7070Spatrickan interrupt service routine that handles an External Interrupt Controller (eic) 2195ec727ea7Spatrickgenerated interrupt. This behavior can be explicitly requested with the "eic" 2196e5dd7070Spatrickargument. 2197e5dd7070Spatrick 2198e5dd7070SpatrickOtherwise, for use with vectored interrupt mode, the argument passed should be 2199e5dd7070Spatrickof the form "vector=LEVEL" where LEVEL is one of the following values: 2200e5dd7070Spatrick"sw0", "sw1", "hw0", "hw1", "hw2", "hw3", "hw4", "hw5". The compiler will 2201e5dd7070Spatrickthen set the interrupt mask to the corresponding level which will mask all 2202e5dd7070Spatrickinterrupts up to and including the argument. 2203e5dd7070Spatrick 2204e5dd7070SpatrickThe semantics are as follows: 2205e5dd7070Spatrick 2206e5dd7070Spatrick- The prologue is modified so that the Exception Program Counter (EPC) and 2207e5dd7070Spatrick Status coprocessor registers are saved to the stack. The interrupt mask is 2208e5dd7070Spatrick set so that the function can only be interrupted by a higher priority 2209e5dd7070Spatrick interrupt. The epilogue will restore the previous values of EPC and Status. 2210e5dd7070Spatrick 2211e5dd7070Spatrick- The prologue and epilogue are modified to save and restore all non-kernel 2212e5dd7070Spatrick registers as necessary. 2213e5dd7070Spatrick 2214e5dd7070Spatrick- The FPU is disabled in the prologue, as the floating pointer registers are not 2215e5dd7070Spatrick spilled to the stack. 2216e5dd7070Spatrick 2217e5dd7070Spatrick- The function return sequence is changed to use an exception return instruction. 2218e5dd7070Spatrick 2219e5dd7070Spatrick- The parameter sets the interrupt mask for the function corresponding to the 2220e5dd7070Spatrick interrupt level specified. If no mask is specified the interrupt mask 2221e5dd7070Spatrick defaults to "eic". 2222e5dd7070Spatrick }]; 2223e5dd7070Spatrick} 2224e5dd7070Spatrick 2225e5dd7070Spatrickdef MicroMipsDocs : Documentation { 2226e5dd7070Spatrick let Category = DocCatFunction; 2227e5dd7070Spatrick let Content = [{ 2228e5dd7070SpatrickClang supports the GNU style ``__attribute__((micromips))`` and 2229e5dd7070Spatrick``__attribute__((nomicromips))`` attributes on MIPS targets. These attributes 2230e5dd7070Spatrickmay be attached to a function definition and instructs the backend to generate 2231e5dd7070Spatrickor not to generate microMIPS code for that function. 2232e5dd7070Spatrick 2233ec727ea7SpatrickThese attributes override the ``-mmicromips`` and ``-mno-micromips`` options 2234e5dd7070Spatrickon the command line. 2235e5dd7070Spatrick }]; 2236e5dd7070Spatrick} 2237e5dd7070Spatrick 2238e5dd7070Spatrickdef MipsLongCallStyleDocs : Documentation { 2239e5dd7070Spatrick let Category = DocCatFunction; 2240e5dd7070Spatrick let Heading = "long_call, far"; 2241e5dd7070Spatrick let Content = [{ 2242e5dd7070SpatrickClang supports the ``__attribute__((long_call))``, ``__attribute__((far))``, 2243e5dd7070Spatrickand ``__attribute__((near))`` attributes on MIPS targets. These attributes may 2244e5dd7070Spatrickonly be added to function declarations and change the code generated 2245e5dd7070Spatrickby the compiler when directly calling the function. The ``near`` attribute 2246e5dd7070Spatrickallows calls to the function to be made using the ``jal`` instruction, which 2247e5dd7070Spatrickrequires the function to be located in the same naturally aligned 256MB 2248e5dd7070Spatricksegment as the caller. The ``long_call`` and ``far`` attributes are synonyms 2249e5dd7070Spatrickand require the use of a different call sequence that works regardless 2250e5dd7070Spatrickof the distance between the functions. 2251e5dd7070Spatrick 2252e5dd7070SpatrickThese attributes have no effect for position-independent code. 2253e5dd7070Spatrick 2254e5dd7070SpatrickThese attributes take priority over command line switches such 2255e5dd7070Spatrickas ``-mlong-calls`` and ``-mno-long-calls``. 2256e5dd7070Spatrick }]; 2257e5dd7070Spatrick} 2258e5dd7070Spatrick 2259e5dd7070Spatrickdef MipsShortCallStyleDocs : Documentation { 2260e5dd7070Spatrick let Category = DocCatFunction; 2261e5dd7070Spatrick let Heading = "short_call, near"; 2262e5dd7070Spatrick let Content = [{ 2263e5dd7070SpatrickClang supports the ``__attribute__((long_call))``, ``__attribute__((far))``, 2264e5dd7070Spatrick``__attribute__((short__call))``, and ``__attribute__((near))`` attributes 2265e5dd7070Spatrickon MIPS targets. These attributes may only be added to function declarations 2266e5dd7070Spatrickand change the code generated by the compiler when directly calling 2267e5dd7070Spatrickthe function. The ``short_call`` and ``near`` attributes are synonyms and 2268e5dd7070Spatrickallow calls to the function to be made using the ``jal`` instruction, which 2269e5dd7070Spatrickrequires the function to be located in the same naturally aligned 256MB segment 2270e5dd7070Spatrickas the caller. The ``long_call`` and ``far`` attributes are synonyms and 2271e5dd7070Spatrickrequire the use of a different call sequence that works regardless 2272e5dd7070Spatrickof the distance between the functions. 2273e5dd7070Spatrick 2274e5dd7070SpatrickThese attributes have no effect for position-independent code. 2275e5dd7070Spatrick 2276e5dd7070SpatrickThese attributes take priority over command line switches such 2277e5dd7070Spatrickas ``-mlong-calls`` and ``-mno-long-calls``. 2278e5dd7070Spatrick }]; 2279e5dd7070Spatrick} 2280e5dd7070Spatrick 2281e5dd7070Spatrickdef RISCVInterruptDocs : Documentation { 2282e5dd7070Spatrick let Category = DocCatFunction; 2283e5dd7070Spatrick let Heading = "interrupt (RISCV)"; 2284e5dd7070Spatrick let Content = [{ 2285e5dd7070SpatrickClang supports the GNU style ``__attribute__((interrupt))`` attribute on RISCV 2286e5dd7070Spatricktargets. This attribute may be attached to a function definition and instructs 2287e5dd7070Spatrickthe backend to generate appropriate function entry/exit code so that it can be 2288e5dd7070Spatrickused directly as an interrupt service routine. 2289e5dd7070Spatrick 2290e5dd7070SpatrickPermissible values for this parameter are ``user``, ``supervisor``, 2291e5dd7070Spatrickand ``machine``. If there is no parameter, then it defaults to machine. 2292e5dd7070Spatrick 2293e5dd7070SpatrickRepeated interrupt attribute on the same declaration will cause a warning 2294e5dd7070Spatrickto be emitted. In case of repeated declarations, the last one prevails. 2295e5dd7070Spatrick 2296e5dd7070SpatrickRefer to: 2297e5dd7070Spatrickhttps://gcc.gnu.org/onlinedocs/gcc/RISC-V-Function-Attributes.html 2298e5dd7070Spatrickhttps://riscv.org/specifications/privileged-isa/ 2299e5dd7070SpatrickThe RISC-V Instruction Set Manual Volume II: Privileged Architecture 2300e5dd7070SpatrickVersion 1.10. 2301e5dd7070Spatrick }]; 2302e5dd7070Spatrick} 2303e5dd7070Spatrick 2304e5dd7070Spatrickdef AVRInterruptDocs : Documentation { 2305e5dd7070Spatrick let Category = DocCatFunction; 2306e5dd7070Spatrick let Heading = "interrupt (AVR)"; 2307e5dd7070Spatrick let Content = [{ 2308e5dd7070SpatrickClang supports the GNU style ``__attribute__((interrupt))`` attribute on 2309e5dd7070SpatrickAVR targets. This attribute may be attached to a function definition and instructs 2310e5dd7070Spatrickthe backend to generate appropriate function entry/exit code so that it can be used 2311e5dd7070Spatrickdirectly as an interrupt service routine. 2312e5dd7070Spatrick 2313e5dd7070SpatrickOn the AVR, the hardware globally disables interrupts when an interrupt is executed. 2314e5dd7070SpatrickThe first instruction of an interrupt handler declared with this attribute is a SEI 2315e5dd7070Spatrickinstruction to re-enable interrupts. See also the signal attribute that 2316e5dd7070Spatrickdoes not insert a SEI instruction. 2317e5dd7070Spatrick }]; 2318e5dd7070Spatrick} 2319e5dd7070Spatrick 2320e5dd7070Spatrickdef AVRSignalDocs : Documentation { 2321e5dd7070Spatrick let Category = DocCatFunction; 2322e5dd7070Spatrick let Content = [{ 2323e5dd7070SpatrickClang supports the GNU style ``__attribute__((signal))`` attribute on 2324e5dd7070SpatrickAVR targets. This attribute may be attached to a function definition and instructs 2325e5dd7070Spatrickthe backend to generate appropriate function entry/exit code so that it can be used 2326e5dd7070Spatrickdirectly as an interrupt service routine. 2327e5dd7070Spatrick 2328e5dd7070SpatrickInterrupt handler functions defined with the signal attribute do not re-enable interrupts. 2329e5dd7070Spatrick}]; 2330e5dd7070Spatrick} 2331e5dd7070Spatrick 2332e5dd7070Spatrickdef TargetDocs : Documentation { 2333e5dd7070Spatrick let Category = DocCatFunction; 2334e5dd7070Spatrick let Content = [{ 2335e5dd7070SpatrickClang supports the GNU style ``__attribute__((target("OPTIONS")))`` attribute. 2336e5dd7070SpatrickThis attribute may be attached to a function definition and instructs 2337e5dd7070Spatrickthe backend to use different code generation options than were passed on the 2338e5dd7070Spatrickcommand line. 2339e5dd7070Spatrick 2340e5dd7070SpatrickThe current set of options correspond to the existing "subtarget features" for 2341e5dd7070Spatrickthe target with or without a "-mno-" in front corresponding to the absence 2342e5dd7070Spatrickof the feature, as well as ``arch="CPU"`` which will change the default "CPU" 2343e5dd7070Spatrickfor the function. 2344e5dd7070Spatrick 2345a9ac8606SpatrickFor X86, the attribute also allows ``tune="CPU"`` to optimize the generated 2346a9ac8606Spatrickcode for the given CPU without changing the available instructions. 2347a9ac8606Spatrick 2348*12c85518SrobertFor AArch64, ``arch="Arch"`` will set the architecture, similar to the -march 2349*12c85518Srobertcommand line options. ``cpu="CPU"`` can be used to select a specific cpu, 2350*12c85518Srobertas per the ``-mcpu`` option, similarly for ``tune=``. The attribute also allows the 2351*12c85518Srobert"branch-protection=<args>" option, where the permissible arguments and their 2352*12c85518Sroberteffect on code generation are the same as for the command-line option 2353*12c85518Srobert``-mbranch-protection``. 2354e5dd7070Spatrick 2355e5dd7070SpatrickExample "subtarget features" from the x86 backend include: "mmx", "sse", "sse4.2", 2356e5dd7070Spatrick"avx", "xop" and largely correspond to the machine specific options handled by 2357e5dd7070Spatrickthe front end. 2358e5dd7070Spatrick 2359e5dd7070SpatrickAdditionally, this attribute supports function multiversioning for ELF based 2360e5dd7070Spatrickx86/x86-64 targets, which can be used to create multiple implementations of the 2361e5dd7070Spatricksame function that will be resolved at runtime based on the priority of their 2362e5dd7070Spatrick``target`` attribute strings. A function is considered a multiversioned function 2363e5dd7070Spatrickif either two declarations of the function have different ``target`` attribute 2364e5dd7070Spatrickstrings, or if it has a ``target`` attribute string of ``default``. For 2365e5dd7070Spatrickexample: 2366e5dd7070Spatrick 2367e5dd7070Spatrick .. code-block:: c++ 2368e5dd7070Spatrick 2369e5dd7070Spatrick __attribute__((target("arch=atom"))) 2370e5dd7070Spatrick void foo() {} // will be called on 'atom' processors. 2371e5dd7070Spatrick __attribute__((target("default"))) 2372e5dd7070Spatrick void foo() {} // will be called on any other processors. 2373e5dd7070Spatrick 2374e5dd7070SpatrickAll multiversioned functions must contain a ``default`` (fallback) 2375e5dd7070Spatrickimplementation, otherwise usages of the function are considered invalid. 2376e5dd7070SpatrickAdditionally, a function may not become multiversioned after its first use. 2377e5dd7070Spatrick}]; 2378e5dd7070Spatrick} 2379e5dd7070Spatrick 2380*12c85518Srobertdef TargetVersionDocs : Documentation { 2381*12c85518Srobert let Category = DocCatFunction; 2382*12c85518Srobert let Content = [{ 2383*12c85518SrobertFor AArch64 target clang supports function multiversioning by 2384*12c85518Srobert``__attribute__((target_version("OPTIONS")))`` attribute. When applied to a 2385*12c85518Srobertfunction it instructs compiler to emit multiple function versions based on 2386*12c85518Srobert``target_version`` attribute strings, which resolved at runtime depend on their 2387*12c85518Srobertpriority and target features availability. One of the versions is always 2388*12c85518Srobert( implicitly or explicitly ) the ``default`` (fallback). Attribute strings can 2389*12c85518Srobertcontain dependent features names joined by the "+" sign. 2390*12c85518Srobert}]; 2391*12c85518Srobert} 2392*12c85518Srobert 2393*12c85518Srobertdef TargetClonesDocs : Documentation { 2394*12c85518Srobert let Category = DocCatFunction; 2395*12c85518Srobert let Content = [{ 2396*12c85518SrobertClang supports the ``target_clones("OPTIONS")`` attribute. This attribute may be 2397*12c85518Srobertattached to a function declaration and causes function multiversioning, where 2398*12c85518Srobertmultiple versions of the function will be emitted with different code 2399*12c85518Srobertgeneration options. Additionally, these versions will be resolved at runtime 2400*12c85518Srobertbased on the priority of their attribute options. All ``target_clone`` functions 2401*12c85518Srobertare considered multiversioned functions. 2402*12c85518Srobert 2403*12c85518SrobertFor AArch64 target: 2404*12c85518SrobertThe attribute contains comma-separated strings of target features joined by "+" 2405*12c85518Srobertsign. For example: 2406*12c85518Srobert 2407*12c85518Srobert .. code-block:: c++ 2408*12c85518Srobert 2409*12c85518Srobert __attribute__((target_clones("sha2+memtag2", "fcma+sve2-pmull128"))) 2410*12c85518Srobert void foo() {} 2411*12c85518Srobert 2412*12c85518SrobertFor every multiversioned function a ``default`` (fallback) implementation 2413*12c85518Srobertalways generated if not specified directly. 2414*12c85518Srobert 2415*12c85518SrobertFor x86/x86-64 targets: 2416*12c85518SrobertAll multiversioned functions must contain a ``default`` (fallback) 2417*12c85518Srobertimplementation, otherwise usages of the function are considered invalid. 2418*12c85518SrobertAdditionally, a function may not become multiversioned after its first use. 2419*12c85518Srobert 2420*12c85518SrobertThe options to ``target_clones`` can either be a target-specific architecture 2421*12c85518Srobert(specified as ``arch=CPU``), or one of a list of subtarget features. 2422*12c85518Srobert 2423*12c85518SrobertExample "subtarget features" from the x86 backend include: "mmx", "sse", "sse4.2", 2424*12c85518Srobert"avx", "xop" and largely correspond to the machine specific options handled by 2425*12c85518Srobertthe front end. 2426*12c85518Srobert 2427*12c85518SrobertThe versions can either be listed as a comma-separated sequence of string 2428*12c85518Srobertliterals or as a single string literal containing a comma-separated list of 2429*12c85518Srobertversions. For compatibility with GCC, the two formats can be mixed. For 2430*12c85518Srobertexample, the following will emit 4 versions of the function: 2431*12c85518Srobert 2432*12c85518Srobert .. code-block:: c++ 2433*12c85518Srobert 2434*12c85518Srobert __attribute__((target_clones("arch=atom,avx2","arch=ivybridge","default"))) 2435*12c85518Srobert void foo() {} 2436*12c85518Srobert 2437*12c85518Srobert}]; 2438*12c85518Srobert} 2439*12c85518Srobert 2440e5dd7070Spatrickdef MinVectorWidthDocs : Documentation { 2441e5dd7070Spatrick let Category = DocCatFunction; 2442e5dd7070Spatrick let Content = [{ 2443e5dd7070SpatrickClang supports the ``__attribute__((min_vector_width(width)))`` attribute. This 2444e5dd7070Spatrickattribute may be attached to a function and informs the backend that this 2445e5dd7070Spatrickfunction desires vectors of at least this width to be generated. Target-specific 2446e5dd7070Spatrickmaximum vector widths still apply. This means even if you ask for something 2447e5dd7070Spatricklarger than the target supports, you will only get what the target supports. 2448e5dd7070SpatrickThis attribute is meant to be a hint to control target heuristics that may 2449e5dd7070Spatrickgenerate narrower vectors than what the target hardware supports. 2450e5dd7070Spatrick 2451e5dd7070SpatrickThis is currently used by the X86 target to allow some CPUs that support 512-bit 2452e5dd7070Spatrickvectors to be limited to using 256-bit vectors to avoid frequency penalties. 2453e5dd7070SpatrickThis is currently enabled with the ``-prefer-vector-width=256`` command line 2454e5dd7070Spatrickoption. The ``min_vector_width`` attribute can be used to prevent the backend 2455e5dd7070Spatrickfrom trying to split vector operations to match the ``prefer-vector-width``. All 2456e5dd7070SpatrickX86 vector intrinsics from x86intrin.h already set this attribute. Additionally, 2457e5dd7070Spatrickuse of any of the X86-specific vector builtins will implicitly set this 2458e5dd7070Spatrickattribute on the calling function. The intent is that explicitly writing vector 2459e5dd7070Spatrickcode using the X86 intrinsics will prevent ``prefer-vector-width`` from 2460e5dd7070Spatrickaffecting the code. 2461e5dd7070Spatrick}]; 2462e5dd7070Spatrick} 2463e5dd7070Spatrick 2464e5dd7070Spatrickdef DocCatAMDGPUAttributes : DocumentationCategory<"AMD GPU Attributes">; 2465e5dd7070Spatrick 2466e5dd7070Spatrickdef AMDGPUFlatWorkGroupSizeDocs : Documentation { 2467e5dd7070Spatrick let Category = DocCatAMDGPUAttributes; 2468e5dd7070Spatrick let Content = [{ 2469e5dd7070SpatrickThe flat work-group size is the number of work-items in the work-group size 2470e5dd7070Spatrickspecified when the kernel is dispatched. It is the product of the sizes of the 2471e5dd7070Spatrickx, y, and z dimension of the work-group. 2472e5dd7070Spatrick 2473e5dd7070SpatrickClang supports the 2474e5dd7070Spatrick``__attribute__((amdgpu_flat_work_group_size(<min>, <max>)))`` attribute for the 2475e5dd7070SpatrickAMDGPU target. This attribute may be attached to a kernel function definition 2476e5dd7070Spatrickand is an optimization hint. 2477e5dd7070Spatrick 2478e5dd7070Spatrick``<min>`` parameter specifies the minimum flat work-group size, and ``<max>`` 2479e5dd7070Spatrickparameter specifies the maximum flat work-group size (must be greater than 2480e5dd7070Spatrick``<min>``) to which all dispatches of the kernel will conform. Passing ``0, 0`` 2481e5dd7070Spatrickas ``<min>, <max>`` implies the default behavior (``128, 256``). 2482e5dd7070Spatrick 2483e5dd7070SpatrickIf specified, the AMDGPU target backend might be able to produce better machine 2484e5dd7070Spatrickcode for barriers and perform scratch promotion by estimating available group 2485e5dd7070Spatricksegment size. 2486e5dd7070Spatrick 2487e5dd7070SpatrickAn error will be given if: 2488e5dd7070Spatrick - Specified values violate subtarget specifications; 2489e5dd7070Spatrick - Specified values are not compatible with values provided through other 2490e5dd7070Spatrick attributes. 2491e5dd7070Spatrick }]; 2492e5dd7070Spatrick} 2493e5dd7070Spatrick 2494e5dd7070Spatrickdef AMDGPUWavesPerEUDocs : Documentation { 2495e5dd7070Spatrick let Category = DocCatAMDGPUAttributes; 2496e5dd7070Spatrick let Content = [{ 2497e5dd7070SpatrickA compute unit (CU) is responsible for executing the wavefronts of a work-group. 2498e5dd7070SpatrickIt is composed of one or more execution units (EU), which are responsible for 2499e5dd7070Spatrickexecuting the wavefronts. An EU can have enough resources to maintain the state 2500e5dd7070Spatrickof more than one executing wavefront. This allows an EU to hide latency by 2501e5dd7070Spatrickswitching between wavefronts in a similar way to symmetric multithreading on a 2502e5dd7070SpatrickCPU. In order to allow the state for multiple wavefronts to fit on an EU, the 2503e5dd7070Spatrickresources used by a single wavefront have to be limited. For example, the number 2504e5dd7070Spatrickof SGPRs and VGPRs. Limiting such resources can allow greater latency hiding, 2505e5dd7070Spatrickbut can result in having to spill some register state to memory. 2506e5dd7070Spatrick 2507e5dd7070SpatrickClang supports the ``__attribute__((amdgpu_waves_per_eu(<min>[, <max>])))`` 2508e5dd7070Spatrickattribute for the AMDGPU target. This attribute may be attached to a kernel 2509e5dd7070Spatrickfunction definition and is an optimization hint. 2510e5dd7070Spatrick 2511e5dd7070Spatrick``<min>`` parameter specifies the requested minimum number of waves per EU, and 2512e5dd7070Spatrick*optional* ``<max>`` parameter specifies the requested maximum number of waves 2513e5dd7070Spatrickper EU (must be greater than ``<min>`` if specified). If ``<max>`` is omitted, 2514e5dd7070Spatrickthen there is no restriction on the maximum number of waves per EU other than 2515e5dd7070Spatrickthe one dictated by the hardware for which the kernel is compiled. Passing 2516e5dd7070Spatrick``0, 0`` as ``<min>, <max>`` implies the default behavior (no limits). 2517e5dd7070Spatrick 2518e5dd7070SpatrickIf specified, this attribute allows an advanced developer to tune the number of 2519e5dd7070Spatrickwavefronts that are capable of fitting within the resources of an EU. The AMDGPU 2520e5dd7070Spatricktarget backend can use this information to limit resources, such as number of 2521e5dd7070SpatrickSGPRs, number of VGPRs, size of available group and private memory segments, in 2522e5dd7070Spatricksuch a way that guarantees that at least ``<min>`` wavefronts and at most 2523e5dd7070Spatrick``<max>`` wavefronts are able to fit within the resources of an EU. Requesting 2524e5dd7070Spatrickmore wavefronts can hide memory latency but limits available registers which 2525e5dd7070Spatrickcan result in spilling. Requesting fewer wavefronts can help reduce cache 2526e5dd7070Spatrickthrashing, but can reduce memory latency hiding. 2527e5dd7070Spatrick 2528e5dd7070SpatrickThis attribute controls the machine code generated by the AMDGPU target backend 2529e5dd7070Spatrickto ensure it is capable of meeting the requested values. However, when the 2530e5dd7070Spatrickkernel is executed, there may be other reasons that prevent meeting the request, 2531e5dd7070Spatrickfor example, there may be wavefronts from other kernels executing on the EU. 2532e5dd7070Spatrick 2533e5dd7070SpatrickAn error will be given if: 2534e5dd7070Spatrick - Specified values violate subtarget specifications; 2535e5dd7070Spatrick - Specified values are not compatible with values provided through other 2536e5dd7070Spatrick attributes; 2537e5dd7070Spatrick - The AMDGPU target backend is unable to create machine code that can meet the 2538e5dd7070Spatrick request. 2539e5dd7070Spatrick }]; 2540e5dd7070Spatrick} 2541e5dd7070Spatrick 2542e5dd7070Spatrickdef AMDGPUNumSGPRNumVGPRDocs : Documentation { 2543e5dd7070Spatrick let Category = DocCatAMDGPUAttributes; 2544e5dd7070Spatrick let Content = [{ 2545e5dd7070SpatrickClang supports the ``__attribute__((amdgpu_num_sgpr(<num_sgpr>)))`` and 2546e5dd7070Spatrick``__attribute__((amdgpu_num_vgpr(<num_vgpr>)))`` attributes for the AMDGPU 2547e5dd7070Spatricktarget. These attributes may be attached to a kernel function definition and are 2548e5dd7070Spatrickan optimization hint. 2549e5dd7070Spatrick 2550e5dd7070SpatrickIf these attributes are specified, then the AMDGPU target backend will attempt 2551e5dd7070Spatrickto limit the number of SGPRs and/or VGPRs used to the specified value(s). The 2552e5dd7070Spatricknumber of used SGPRs and/or VGPRs may further be rounded up to satisfy the 2553e5dd7070Spatrickallocation requirements or constraints of the subtarget. Passing ``0`` as 2554e5dd7070Spatrick``num_sgpr`` and/or ``num_vgpr`` implies the default behavior (no limits). 2555e5dd7070Spatrick 2556e5dd7070SpatrickThese attributes can be used to test the AMDGPU target backend. It is 2557e5dd7070Spatrickrecommended that the ``amdgpu_waves_per_eu`` attribute be used to control 2558e5dd7070Spatrickresources such as SGPRs and VGPRs since it is aware of the limits for different 2559e5dd7070Spatricksubtargets. 2560e5dd7070Spatrick 2561e5dd7070SpatrickAn error will be given if: 2562e5dd7070Spatrick - Specified values violate subtarget specifications; 2563e5dd7070Spatrick - Specified values are not compatible with values provided through other 2564e5dd7070Spatrick attributes; 2565e5dd7070Spatrick - The AMDGPU target backend is unable to create machine code that can meet the 2566e5dd7070Spatrick request. 2567e5dd7070Spatrick }]; 2568e5dd7070Spatrick} 2569e5dd7070Spatrick 2570e5dd7070Spatrickdef DocCatCallingConvs : DocumentationCategory<"Calling Conventions"> { 2571e5dd7070Spatrick let Content = [{ 2572e5dd7070SpatrickClang supports several different calling conventions, depending on the target 2573e5dd7070Spatrickplatform and architecture. The calling convention used for a function determines 2574e5dd7070Spatrickhow parameters are passed, how results are returned to the caller, and other 2575e5dd7070Spatricklow-level details of calling a function. 2576e5dd7070Spatrick }]; 2577e5dd7070Spatrick} 2578e5dd7070Spatrick 2579e5dd7070Spatrickdef PcsDocs : Documentation { 2580e5dd7070Spatrick let Category = DocCatCallingConvs; 2581e5dd7070Spatrick let Content = [{ 2582e5dd7070SpatrickOn ARM targets, this attribute can be used to select calling conventions 2583e5dd7070Spatricksimilar to ``stdcall`` on x86. Valid parameter values are "aapcs" and 2584e5dd7070Spatrick"aapcs-vfp". 2585e5dd7070Spatrick }]; 2586e5dd7070Spatrick} 2587e5dd7070Spatrick 2588e5dd7070Spatrickdef AArch64VectorPcsDocs : Documentation { 2589e5dd7070Spatrick let Category = DocCatCallingConvs; 2590e5dd7070Spatrick let Content = [{ 2591e5dd7070SpatrickOn AArch64 targets, this attribute changes the calling convention of a 2592e5dd7070Spatrickfunction to preserve additional floating-point and Advanced SIMD registers 2593e5dd7070Spatrickrelative to the default calling convention used for AArch64. 2594e5dd7070Spatrick 2595e5dd7070SpatrickThis means it is more efficient to call such functions from code that performs 2596e5dd7070Spatrickextensive floating-point and vector calculations, because fewer live SIMD and FP 2597e5dd7070Spatrickregisters need to be saved. This property makes it well-suited for e.g. 2598e5dd7070Spatrickfloating-point or vector math library functions, which are typically leaf 2599e5dd7070Spatrickfunctions that require a small number of registers. 2600e5dd7070Spatrick 2601e5dd7070SpatrickHowever, using this attribute also means that it is more expensive to call 2602e5dd7070Spatricka function that adheres to the default calling convention from within such 2603e5dd7070Spatricka function. Therefore, it is recommended that this attribute is only used 2604e5dd7070Spatrickfor leaf functions. 2605e5dd7070Spatrick 2606e5dd7070SpatrickFor more information, see the documentation for `aarch64_vector_pcs`_ on 2607e5dd7070Spatrickthe Arm Developer website. 2608e5dd7070Spatrick 2609e5dd7070Spatrick.. _`aarch64_vector_pcs`: https://developer.arm.com/products/software-development-tools/hpc/arm-compiler-for-hpc/vector-function-abi 2610e5dd7070Spatrick }]; 2611e5dd7070Spatrick} 2612e5dd7070Spatrick 2613*12c85518Srobertdef AArch64SVEPcsDocs : Documentation { 2614*12c85518Srobert let Category = DocCatCallingConvs; 2615*12c85518Srobert let Content = [{ 2616*12c85518SrobertOn AArch64 targets, this attribute changes the calling convention of a 2617*12c85518Srobertfunction to preserve additional Scalable Vector registers and Scalable 2618*12c85518SrobertPredicate registers relative to the default calling convention used for 2619*12c85518SrobertAArch64. 2620*12c85518Srobert 2621*12c85518SrobertThis means it is more efficient to call such functions from code that performs 2622*12c85518Srobertextensive scalable vector and scalable predicate calculations, because fewer 2623*12c85518Srobertlive SVE registers need to be saved. This property makes it well-suited for SVE 2624*12c85518Srobertmath library functions, which are typically leaf functions that require a small 2625*12c85518Srobertnumber of registers. 2626*12c85518Srobert 2627*12c85518SrobertHowever, using this attribute also means that it is more expensive to call 2628*12c85518Sroberta function that adheres to the default calling convention from within such 2629*12c85518Sroberta function. Therefore, it is recommended that this attribute is only used 2630*12c85518Srobertfor leaf functions. 2631*12c85518Srobert 2632*12c85518SrobertFor more information, see the documentation for `aarch64_sve_pcs` in the 2633*12c85518SrobertARM C Language Extension (ACLE) documentation. 2634*12c85518Srobert 2635*12c85518Srobert.. _`aarch64_sve_pcs`: https://github.com/ARM-software/acle/blob/main/main/acle.md#scalable-vector-extension-procedure-call-standard-attribute 2636*12c85518Srobert }]; 2637*12c85518Srobert} 2638*12c85518Srobert 2639e5dd7070Spatrickdef RegparmDocs : Documentation { 2640e5dd7070Spatrick let Category = DocCatCallingConvs; 2641e5dd7070Spatrick let Content = [{ 2642e5dd7070SpatrickOn 32-bit x86 targets, the regparm attribute causes the compiler to pass 2643e5dd7070Spatrickthe first three integer parameters in EAX, EDX, and ECX instead of on the 2644e5dd7070Spatrickstack. This attribute has no effect on variadic functions, and all parameters 2645e5dd7070Spatrickare passed via the stack as normal. 2646e5dd7070Spatrick }]; 2647e5dd7070Spatrick} 2648e5dd7070Spatrick 2649e5dd7070Spatrickdef SysVABIDocs : Documentation { 2650e5dd7070Spatrick let Category = DocCatCallingConvs; 2651e5dd7070Spatrick let Content = [{ 2652e5dd7070SpatrickOn Windows x86_64 targets, this attribute changes the calling convention of a 2653e5dd7070Spatrickfunction to match the default convention used on Sys V targets such as Linux, 2654e5dd7070SpatrickMac, and BSD. This attribute has no effect on other targets. 2655e5dd7070Spatrick }]; 2656e5dd7070Spatrick} 2657e5dd7070Spatrick 2658e5dd7070Spatrickdef MSABIDocs : Documentation { 2659e5dd7070Spatrick let Category = DocCatCallingConvs; 2660e5dd7070Spatrick let Content = [{ 2661e5dd7070SpatrickOn non-Windows x86_64 targets, this attribute changes the calling convention of 2662e5dd7070Spatricka function to match the default convention used on Windows x86_64. This 2663e5dd7070Spatrickattribute has no effect on Windows targets or non-x86_64 targets. 2664e5dd7070Spatrick }]; 2665e5dd7070Spatrick} 2666e5dd7070Spatrick 2667e5dd7070Spatrickdef StdCallDocs : Documentation { 2668e5dd7070Spatrick let Category = DocCatCallingConvs; 2669e5dd7070Spatrick let Content = [{ 2670e5dd7070SpatrickOn 32-bit x86 targets, this attribute changes the calling convention of a 2671e5dd7070Spatrickfunction to clear parameters off of the stack on return. This convention does 2672e5dd7070Spatricknot support variadic calls or unprototyped functions in C, and has no effect on 2673e5dd7070Spatrickx86_64 targets. This calling convention is used widely by the Windows API and 2674e5dd7070SpatrickCOM applications. See the documentation for `__stdcall`_ on MSDN. 2675e5dd7070Spatrick 2676e5dd7070Spatrick.. _`__stdcall`: http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx 2677e5dd7070Spatrick }]; 2678e5dd7070Spatrick} 2679e5dd7070Spatrick 2680e5dd7070Spatrickdef FastCallDocs : Documentation { 2681e5dd7070Spatrick let Category = DocCatCallingConvs; 2682e5dd7070Spatrick let Content = [{ 2683e5dd7070SpatrickOn 32-bit x86 targets, this attribute changes the calling convention of a 2684e5dd7070Spatrickfunction to use ECX and EDX as register parameters and clear parameters off of 2685e5dd7070Spatrickthe stack on return. This convention does not support variadic calls or 2686e5dd7070Spatrickunprototyped functions in C, and has no effect on x86_64 targets. This calling 2687e5dd7070Spatrickconvention is supported primarily for compatibility with existing code. Users 2688e5dd7070Spatrickseeking register parameters should use the ``regparm`` attribute, which does 2689e5dd7070Spatricknot require callee-cleanup. See the documentation for `__fastcall`_ on MSDN. 2690e5dd7070Spatrick 2691e5dd7070Spatrick.. _`__fastcall`: http://msdn.microsoft.com/en-us/library/6xa169sk.aspx 2692e5dd7070Spatrick }]; 2693e5dd7070Spatrick} 2694e5dd7070Spatrick 2695e5dd7070Spatrickdef RegCallDocs : Documentation { 2696e5dd7070Spatrick let Category = DocCatCallingConvs; 2697e5dd7070Spatrick let Content = [{ 2698e5dd7070SpatrickOn x86 targets, this attribute changes the calling convention to 2699e5dd7070Spatrick`__regcall`_ convention. This convention aims to pass as many arguments 2700e5dd7070Spatrickas possible in registers. It also tries to utilize registers for the 2701e5dd7070Spatrickreturn value whenever it is possible. 2702e5dd7070Spatrick 2703e5dd7070Spatrick.. _`__regcall`: https://software.intel.com/en-us/node/693069 2704e5dd7070Spatrick }]; 2705e5dd7070Spatrick} 2706e5dd7070Spatrick 2707e5dd7070Spatrickdef ThisCallDocs : Documentation { 2708e5dd7070Spatrick let Category = DocCatCallingConvs; 2709e5dd7070Spatrick let Content = [{ 2710e5dd7070SpatrickOn 32-bit x86 targets, this attribute changes the calling convention of a 2711e5dd7070Spatrickfunction to use ECX for the first parameter (typically the implicit ``this`` 2712e5dd7070Spatrickparameter of C++ methods) and clear parameters off of the stack on return. This 2713e5dd7070Spatrickconvention does not support variadic calls or unprototyped functions in C, and 2714e5dd7070Spatrickhas no effect on x86_64 targets. See the documentation for `__thiscall`_ on 2715e5dd7070SpatrickMSDN. 2716e5dd7070Spatrick 2717e5dd7070Spatrick.. _`__thiscall`: http://msdn.microsoft.com/en-us/library/ek8tkfbw.aspx 2718e5dd7070Spatrick }]; 2719e5dd7070Spatrick} 2720e5dd7070Spatrick 2721e5dd7070Spatrickdef VectorCallDocs : Documentation { 2722e5dd7070Spatrick let Category = DocCatCallingConvs; 2723e5dd7070Spatrick let Content = [{ 2724e5dd7070SpatrickOn 32-bit x86 *and* x86_64 targets, this attribute changes the calling 2725e5dd7070Spatrickconvention of a function to pass vector parameters in SSE registers. 2726e5dd7070Spatrick 2727e5dd7070SpatrickOn 32-bit x86 targets, this calling convention is similar to ``__fastcall``. 2728e5dd7070SpatrickThe first two integer parameters are passed in ECX and EDX. Subsequent integer 2729e5dd7070Spatrickparameters are passed in memory, and callee clears the stack. On x86_64 2730e5dd7070Spatricktargets, the callee does *not* clear the stack, and integer parameters are 2731e5dd7070Spatrickpassed in RCX, RDX, R8, and R9 as is done for the default Windows x64 calling 2732e5dd7070Spatrickconvention. 2733e5dd7070Spatrick 2734e5dd7070SpatrickOn both 32-bit x86 and x86_64 targets, vector and floating point arguments are 2735e5dd7070Spatrickpassed in XMM0-XMM5. Homogeneous vector aggregates of up to four elements are 2736e5dd7070Spatrickpassed in sequential SSE registers if enough are available. If AVX is enabled, 2737e5dd7070Spatrick256 bit vectors are passed in YMM0-YMM5. Any vector or aggregate type that 2738e5dd7070Spatrickcannot be passed in registers for any reason is passed by reference, which 2739e5dd7070Spatrickallows the caller to align the parameter memory. 2740e5dd7070Spatrick 2741e5dd7070SpatrickSee the documentation for `__vectorcall`_ on MSDN for more details. 2742e5dd7070Spatrick 2743e5dd7070Spatrick.. _`__vectorcall`: http://msdn.microsoft.com/en-us/library/dn375768.aspx 2744e5dd7070Spatrick }]; 2745e5dd7070Spatrick} 2746e5dd7070Spatrick 2747e5dd7070Spatrickdef DocCatConsumed : DocumentationCategory<"Consumed Annotation Checking"> { 2748e5dd7070Spatrick let Content = [{ 2749e5dd7070SpatrickClang supports additional attributes for checking basic resource management 2750e5dd7070Spatrickproperties, specifically for unique objects that have a single owning reference. 2751e5dd7070SpatrickThe following attributes are currently supported, although **the implementation 2752e5dd7070Spatrickfor these annotations is currently in development and are subject to change.** 2753e5dd7070Spatrick }]; 2754e5dd7070Spatrick} 2755e5dd7070Spatrick 2756e5dd7070Spatrickdef SetTypestateDocs : Documentation { 2757e5dd7070Spatrick let Category = DocCatConsumed; 2758e5dd7070Spatrick let Content = [{ 2759e5dd7070SpatrickAnnotate methods that transition an object into a new state with 2760e5dd7070Spatrick``__attribute__((set_typestate(new_state)))``. The new state must be 2761e5dd7070Spatrickunconsumed, consumed, or unknown. 2762e5dd7070Spatrick }]; 2763e5dd7070Spatrick} 2764e5dd7070Spatrick 2765e5dd7070Spatrickdef CallableWhenDocs : Documentation { 2766e5dd7070Spatrick let Category = DocCatConsumed; 2767e5dd7070Spatrick let Content = [{ 2768e5dd7070SpatrickUse ``__attribute__((callable_when(...)))`` to indicate what states a method 2769e5dd7070Spatrickmay be called in. Valid states are unconsumed, consumed, or unknown. Each 2770e5dd7070Spatrickargument to this attribute must be a quoted string. E.g.: 2771e5dd7070Spatrick 2772e5dd7070Spatrick``__attribute__((callable_when("unconsumed", "unknown")))`` 2773e5dd7070Spatrick }]; 2774e5dd7070Spatrick} 2775e5dd7070Spatrick 2776e5dd7070Spatrickdef TestTypestateDocs : Documentation { 2777e5dd7070Spatrick let Category = DocCatConsumed; 2778e5dd7070Spatrick let Content = [{ 2779e5dd7070SpatrickUse ``__attribute__((test_typestate(tested_state)))`` to indicate that a method 2780e5dd7070Spatrickreturns true if the object is in the specified state.. 2781e5dd7070Spatrick }]; 2782e5dd7070Spatrick} 2783e5dd7070Spatrick 2784e5dd7070Spatrickdef ParamTypestateDocs : Documentation { 2785e5dd7070Spatrick let Category = DocCatConsumed; 2786e5dd7070Spatrick let Content = [{ 2787e5dd7070SpatrickThis attribute specifies expectations about function parameters. Calls to an 2788e5dd7070Spatrickfunction with annotated parameters will issue a warning if the corresponding 2789e5dd7070Spatrickargument isn't in the expected state. The attribute is also used to set the 2790e5dd7070Spatrickinitial state of the parameter when analyzing the function's body. 2791e5dd7070Spatrick }]; 2792e5dd7070Spatrick} 2793e5dd7070Spatrick 2794e5dd7070Spatrickdef ReturnTypestateDocs : Documentation { 2795e5dd7070Spatrick let Category = DocCatConsumed; 2796e5dd7070Spatrick let Content = [{ 2797e5dd7070SpatrickThe ``return_typestate`` attribute can be applied to functions or parameters. 2798e5dd7070SpatrickWhen applied to a function the attribute specifies the state of the returned 2799e5dd7070Spatrickvalue. The function's body is checked to ensure that it always returns a value 2800e5dd7070Spatrickin the specified state. On the caller side, values returned by the annotated 2801e5dd7070Spatrickfunction are initialized to the given state. 2802e5dd7070Spatrick 2803e5dd7070SpatrickWhen applied to a function parameter it modifies the state of an argument after 2804e5dd7070Spatricka call to the function returns. The function's body is checked to ensure that 2805e5dd7070Spatrickthe parameter is in the expected state before returning. 2806e5dd7070Spatrick }]; 2807e5dd7070Spatrick} 2808e5dd7070Spatrick 2809e5dd7070Spatrickdef ConsumableDocs : Documentation { 2810e5dd7070Spatrick let Category = DocCatConsumed; 2811e5dd7070Spatrick let Content = [{ 2812e5dd7070SpatrickEach ``class`` that uses any of the typestate annotations must first be marked 2813e5dd7070Spatrickusing the ``consumable`` attribute. Failure to do so will result in a warning. 2814e5dd7070Spatrick 2815e5dd7070SpatrickThis attribute accepts a single parameter that must be one of the following: 2816e5dd7070Spatrick``unknown``, ``consumed``, or ``unconsumed``. 2817e5dd7070Spatrick }]; 2818e5dd7070Spatrick} 2819e5dd7070Spatrick 2820a9ac8606Spatrickdef NoProfileInstrumentFunctionDocs : Documentation { 2821a9ac8606Spatrick let Category = DocCatFunction; 2822a9ac8606Spatrick let Content = [{ 2823a9ac8606SpatrickUse the ``no_profile_instrument_function`` attribute on a function declaration 2824a9ac8606Spatrickto denote that the compiler should not instrument the function with 2825a9ac8606Spatrickprofile-related instrumentation, such as via the 2826a9ac8606Spatrick``-fprofile-generate`` / ``-fprofile-instr-generate`` / 2827a9ac8606Spatrick``-fcs-profile-generate`` / ``-fprofile-arcs`` flags. 2828a9ac8606Spatrick}]; 2829a9ac8606Spatrick} 2830a9ac8606Spatrick 2831e5dd7070Spatrickdef NoSanitizeDocs : Documentation { 2832e5dd7070Spatrick let Category = DocCatFunction; 2833e5dd7070Spatrick let Content = [{ 2834e5dd7070SpatrickUse the ``no_sanitize`` attribute on a function or a global variable 2835e5dd7070Spatrickdeclaration to specify that a particular instrumentation or set of 2836a9ac8606Spatrickinstrumentations should not be applied. 2837a9ac8606Spatrick 2838a9ac8606SpatrickThe attribute takes a list of string literals with the following accepted 2839a9ac8606Spatrickvalues: 2840a9ac8606Spatrick* all values accepted by ``-fno-sanitize=``; 2841a9ac8606Spatrick* ``coverage``, to disable SanitizerCoverage instrumentation. 2842a9ac8606Spatrick 2843a9ac8606SpatrickFor example, ``__attribute__((no_sanitize("address", "thread")))`` specifies 2844a9ac8606Spatrickthat AddressSanitizer and ThreadSanitizer should not be applied to the function 2845a9ac8606Spatrickor variable. Using ``__attribute__((no_sanitize("coverage")))`` specifies that 2846a9ac8606SpatrickSanitizerCoverage should not be applied to the function. 2847e5dd7070Spatrick 2848e5dd7070SpatrickSee :ref:`Controlling Code Generation <controlling-code-generation>` for a 2849e5dd7070Spatrickfull list of supported sanitizer flags. 2850e5dd7070Spatrick }]; 2851e5dd7070Spatrick} 2852e5dd7070Spatrick 2853*12c85518Srobertdef DisableSanitizerInstrumentationDocs : Documentation { 2854*12c85518Srobert let Category = DocCatFunction; 2855*12c85518Srobert let Content = [{ 2856*12c85518SrobertUse the ``disable_sanitizer_instrumentation`` attribute on a function, 2857*12c85518SrobertObjective-C method, or global variable, to specify that no sanitizer 2858*12c85518Srobertinstrumentation should be applied. 2859*12c85518Srobert 2860*12c85518SrobertThis is not the same as ``__attribute__((no_sanitize(...)))``, which depending 2861*12c85518Sroberton the tool may still insert instrumentation to prevent false positive reports. 2862*12c85518Srobert }]; 2863*12c85518Srobert} 2864*12c85518Srobert 2865e5dd7070Spatrickdef NoSanitizeAddressDocs : Documentation { 2866e5dd7070Spatrick let Category = DocCatFunction; 2867e5dd7070Spatrick // This function has multiple distinct spellings, and so it requires a custom 2868e5dd7070Spatrick // heading to be specified. The most common spelling is sufficient. 2869e5dd7070Spatrick let Heading = "no_sanitize_address, no_address_safety_analysis"; 2870e5dd7070Spatrick let Content = [{ 2871e5dd7070Spatrick.. _langext-address_sanitizer: 2872e5dd7070Spatrick 2873e5dd7070SpatrickUse ``__attribute__((no_sanitize_address))`` on a function or a global 2874e5dd7070Spatrickvariable declaration to specify that address safety instrumentation 2875e5dd7070Spatrick(e.g. AddressSanitizer) should not be applied. 2876e5dd7070Spatrick }]; 2877e5dd7070Spatrick} 2878e5dd7070Spatrick 2879e5dd7070Spatrickdef NoSanitizeThreadDocs : Documentation { 2880e5dd7070Spatrick let Category = DocCatFunction; 2881e5dd7070Spatrick let Heading = "no_sanitize_thread"; 2882e5dd7070Spatrick let Content = [{ 2883e5dd7070Spatrick.. _langext-thread_sanitizer: 2884e5dd7070Spatrick 2885e5dd7070SpatrickUse ``__attribute__((no_sanitize_thread))`` on a function declaration to 2886e5dd7070Spatrickspecify that checks for data races on plain (non-atomic) memory accesses should 2887e5dd7070Spatricknot be inserted by ThreadSanitizer. The function is still instrumented by the 2888e5dd7070Spatricktool to avoid false positives and provide meaningful stack traces. 2889e5dd7070Spatrick }]; 2890e5dd7070Spatrick} 2891e5dd7070Spatrick 2892e5dd7070Spatrickdef NoSanitizeMemoryDocs : Documentation { 2893e5dd7070Spatrick let Category = DocCatFunction; 2894e5dd7070Spatrick let Heading = "no_sanitize_memory"; 2895e5dd7070Spatrick let Content = [{ 2896e5dd7070Spatrick.. _langext-memory_sanitizer: 2897e5dd7070Spatrick 2898e5dd7070SpatrickUse ``__attribute__((no_sanitize_memory))`` on a function declaration to 2899e5dd7070Spatrickspecify that checks for uninitialized memory should not be inserted 2900e5dd7070Spatrick(e.g. by MemorySanitizer). The function may still be instrumented by the tool 2901e5dd7070Spatrickto avoid false positives in other places. 2902e5dd7070Spatrick }]; 2903e5dd7070Spatrick} 2904e5dd7070Spatrick 2905e5dd7070Spatrickdef CFICanonicalJumpTableDocs : Documentation { 2906e5dd7070Spatrick let Category = DocCatFunction; 2907e5dd7070Spatrick let Heading = "cfi_canonical_jump_table"; 2908e5dd7070Spatrick let Content = [{ 2909e5dd7070Spatrick.. _langext-cfi_canonical_jump_table: 2910e5dd7070Spatrick 2911e5dd7070SpatrickUse ``__attribute__((cfi_canonical_jump_table))`` on a function declaration to 2912e5dd7070Spatrickmake the function's CFI jump table canonical. See :ref:`the CFI documentation 2913e5dd7070Spatrick<cfi-canonical-jump-tables>` for more details. 2914e5dd7070Spatrick }]; 2915e5dd7070Spatrick} 2916e5dd7070Spatrick 2917e5dd7070Spatrickdef DocCatTypeSafety : DocumentationCategory<"Type Safety Checking"> { 2918e5dd7070Spatrick let Content = [{ 2919e5dd7070SpatrickClang supports additional attributes to enable checking type safety properties 2920e5dd7070Spatrickthat can't be enforced by the C type system. To see warnings produced by these 2921e5dd7070Spatrickchecks, ensure that -Wtype-safety is enabled. Use cases include: 2922e5dd7070Spatrick 2923e5dd7070Spatrick* MPI library implementations, where these attributes enable checking that 2924e5dd7070Spatrick the buffer type matches the passed ``MPI_Datatype``; 2925e5dd7070Spatrick* for HDF5 library there is a similar use case to MPI; 2926e5dd7070Spatrick* checking types of variadic functions' arguments for functions like 2927e5dd7070Spatrick ``fcntl()`` and ``ioctl()``. 2928e5dd7070Spatrick 2929e5dd7070SpatrickYou can detect support for these attributes with ``__has_attribute()``. For 2930e5dd7070Spatrickexample: 2931e5dd7070Spatrick 2932e5dd7070Spatrick.. code-block:: c++ 2933e5dd7070Spatrick 2934e5dd7070Spatrick #if defined(__has_attribute) 2935e5dd7070Spatrick # if __has_attribute(argument_with_type_tag) && \ 2936e5dd7070Spatrick __has_attribute(pointer_with_type_tag) && \ 2937e5dd7070Spatrick __has_attribute(type_tag_for_datatype) 2938e5dd7070Spatrick # define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx))) 2939e5dd7070Spatrick /* ... other macros ... */ 2940e5dd7070Spatrick # endif 2941e5dd7070Spatrick #endif 2942e5dd7070Spatrick 2943e5dd7070Spatrick #if !defined(ATTR_MPI_PWT) 2944e5dd7070Spatrick # define ATTR_MPI_PWT(buffer_idx, type_idx) 2945e5dd7070Spatrick #endif 2946e5dd7070Spatrick 2947e5dd7070Spatrick int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) 2948e5dd7070Spatrick ATTR_MPI_PWT(1,3); 2949e5dd7070Spatrick }]; 2950e5dd7070Spatrick} 2951e5dd7070Spatrick 2952e5dd7070Spatrickdef ArgumentWithTypeTagDocs : Documentation { 2953e5dd7070Spatrick let Category = DocCatTypeSafety; 2954e5dd7070Spatrick let Heading = "argument_with_type_tag"; 2955e5dd7070Spatrick let Content = [{ 2956e5dd7070SpatrickUse ``__attribute__((argument_with_type_tag(arg_kind, arg_idx, 2957e5dd7070Spatricktype_tag_idx)))`` on a function declaration to specify that the function 2958e5dd7070Spatrickaccepts a type tag that determines the type of some other argument. 2959e5dd7070Spatrick 2960e5dd7070SpatrickThis attribute is primarily useful for checking arguments of variadic functions 2961e5dd7070Spatrick(``pointer_with_type_tag`` can be used in most non-variadic cases). 2962e5dd7070Spatrick 2963e5dd7070SpatrickIn the attribute prototype above: 2964e5dd7070Spatrick * ``arg_kind`` is an identifier that should be used when annotating all 2965e5dd7070Spatrick applicable type tags. 2966e5dd7070Spatrick * ``arg_idx`` provides the position of a function argument. The expected type of 2967e5dd7070Spatrick this function argument will be determined by the function argument specified 2968e5dd7070Spatrick by ``type_tag_idx``. In the code example below, "3" means that the type of the 2969e5dd7070Spatrick function's third argument will be determined by ``type_tag_idx``. 2970e5dd7070Spatrick * ``type_tag_idx`` provides the position of a function argument. This function 2971e5dd7070Spatrick argument will be a type tag. The type tag will determine the expected type of 2972e5dd7070Spatrick the argument specified by ``arg_idx``. In the code example below, "2" means 2973e5dd7070Spatrick that the type tag associated with the function's second argument should agree 2974e5dd7070Spatrick with the type of the argument specified by ``arg_idx``. 2975e5dd7070Spatrick 2976e5dd7070SpatrickFor example: 2977e5dd7070Spatrick 2978e5dd7070Spatrick.. code-block:: c++ 2979e5dd7070Spatrick 2980e5dd7070Spatrick int fcntl(int fd, int cmd, ...) 2981e5dd7070Spatrick __attribute__(( argument_with_type_tag(fcntl,3,2) )); 2982e5dd7070Spatrick // The function's second argument will be a type tag; this type tag will 2983e5dd7070Spatrick // determine the expected type of the function's third argument. 2984e5dd7070Spatrick }]; 2985e5dd7070Spatrick} 2986e5dd7070Spatrick 2987e5dd7070Spatrickdef PointerWithTypeTagDocs : Documentation { 2988e5dd7070Spatrick let Category = DocCatTypeSafety; 2989e5dd7070Spatrick let Heading = "pointer_with_type_tag"; 2990e5dd7070Spatrick let Content = [{ 2991e5dd7070SpatrickUse ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))`` 2992e5dd7070Spatrickon a function declaration to specify that the function accepts a type tag that 2993e5dd7070Spatrickdetermines the pointee type of some other pointer argument. 2994e5dd7070Spatrick 2995e5dd7070SpatrickIn the attribute prototype above: 2996e5dd7070Spatrick * ``ptr_kind`` is an identifier that should be used when annotating all 2997e5dd7070Spatrick applicable type tags. 2998e5dd7070Spatrick * ``ptr_idx`` provides the position of a function argument; this function 2999e5dd7070Spatrick argument will have a pointer type. The expected pointee type of this pointer 3000e5dd7070Spatrick type will be determined by the function argument specified by 3001e5dd7070Spatrick ``type_tag_idx``. In the code example below, "1" means that the pointee type 3002e5dd7070Spatrick of the function's first argument will be determined by ``type_tag_idx``. 3003e5dd7070Spatrick * ``type_tag_idx`` provides the position of a function argument; this function 3004e5dd7070Spatrick argument will be a type tag. The type tag will determine the expected pointee 3005e5dd7070Spatrick type of the pointer argument specified by ``ptr_idx``. In the code example 3006e5dd7070Spatrick below, "3" means that the type tag associated with the function's third 3007e5dd7070Spatrick argument should agree with the pointee type of the pointer argument specified 3008e5dd7070Spatrick by ``ptr_idx``. 3009e5dd7070Spatrick 3010e5dd7070SpatrickFor example: 3011e5dd7070Spatrick 3012e5dd7070Spatrick.. code-block:: c++ 3013e5dd7070Spatrick 3014e5dd7070Spatrick typedef int MPI_Datatype; 3015e5dd7070Spatrick int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) 3016e5dd7070Spatrick __attribute__(( pointer_with_type_tag(mpi,1,3) )); 3017e5dd7070Spatrick // The function's 3rd argument will be a type tag; this type tag will 3018e5dd7070Spatrick // determine the expected pointee type of the function's 1st argument. 3019e5dd7070Spatrick }]; 3020e5dd7070Spatrick} 3021e5dd7070Spatrick 3022e5dd7070Spatrickdef TypeTagForDatatypeDocs : Documentation { 3023e5dd7070Spatrick let Category = DocCatTypeSafety; 3024e5dd7070Spatrick let Content = [{ 3025e5dd7070SpatrickWhen declaring a variable, use 3026e5dd7070Spatrick``__attribute__((type_tag_for_datatype(kind, type)))`` to create a type tag that 3027e5dd7070Spatrickis tied to the ``type`` argument given to the attribute. 3028e5dd7070Spatrick 3029e5dd7070SpatrickIn the attribute prototype above: 3030e5dd7070Spatrick * ``kind`` is an identifier that should be used when annotating all applicable 3031e5dd7070Spatrick type tags. 3032e5dd7070Spatrick * ``type`` indicates the name of the type. 3033e5dd7070Spatrick 3034e5dd7070SpatrickClang supports annotating type tags of two forms. 3035e5dd7070Spatrick 3036e5dd7070Spatrick * **Type tag that is a reference to a declared identifier.** 3037e5dd7070Spatrick Use ``__attribute__((type_tag_for_datatype(kind, type)))`` when declaring that 3038e5dd7070Spatrick identifier: 3039e5dd7070Spatrick 3040e5dd7070Spatrick .. code-block:: c++ 3041e5dd7070Spatrick 3042e5dd7070Spatrick typedef int MPI_Datatype; 3043e5dd7070Spatrick extern struct mpi_datatype mpi_datatype_int 3044e5dd7070Spatrick __attribute__(( type_tag_for_datatype(mpi,int) )); 3045e5dd7070Spatrick #define MPI_INT ((MPI_Datatype) &mpi_datatype_int) 3046e5dd7070Spatrick // &mpi_datatype_int is a type tag. It is tied to type "int". 3047e5dd7070Spatrick 3048e5dd7070Spatrick * **Type tag that is an integral literal.** 3049e5dd7070Spatrick Declare a ``static const`` variable with an initializer value and attach 3050e5dd7070Spatrick ``__attribute__((type_tag_for_datatype(kind, type)))`` on that declaration: 3051e5dd7070Spatrick 3052e5dd7070Spatrick .. code-block:: c++ 3053e5dd7070Spatrick 3054e5dd7070Spatrick typedef int MPI_Datatype; 3055e5dd7070Spatrick static const MPI_Datatype mpi_datatype_int 3056e5dd7070Spatrick __attribute__(( type_tag_for_datatype(mpi,int) )) = 42; 3057e5dd7070Spatrick #define MPI_INT ((MPI_Datatype) 42) 3058e5dd7070Spatrick // The number 42 is a type tag. It is tied to type "int". 3059e5dd7070Spatrick 3060e5dd7070Spatrick 3061e5dd7070SpatrickThe ``type_tag_for_datatype`` attribute also accepts an optional third argument 3062e5dd7070Spatrickthat determines how the type of the function argument specified by either 3063e5dd7070Spatrick``arg_idx`` or ``ptr_idx`` is compared against the type associated with the type 3064e5dd7070Spatricktag. (Recall that for the ``argument_with_type_tag`` attribute, the type of the 3065e5dd7070Spatrickfunction argument specified by ``arg_idx`` is compared against the type 3066e5dd7070Spatrickassociated with the type tag. Also recall that for the ``pointer_with_type_tag`` 3067e5dd7070Spatrickattribute, the pointee type of the function argument specified by ``ptr_idx`` is 3068e5dd7070Spatrickcompared against the type associated with the type tag.) There are two supported 3069e5dd7070Spatrickvalues for this optional third argument: 3070e5dd7070Spatrick 3071e5dd7070Spatrick * ``layout_compatible`` will cause types to be compared according to 3072e5dd7070Spatrick layout-compatibility rules (In C++11 [class.mem] p 17, 18, see the 3073e5dd7070Spatrick layout-compatibility rules for two standard-layout struct types and for two 3074e5dd7070Spatrick standard-layout union types). This is useful when creating a type tag 3075e5dd7070Spatrick associated with a struct or union type. For example: 3076e5dd7070Spatrick 3077e5dd7070Spatrick .. code-block:: c++ 3078e5dd7070Spatrick 3079e5dd7070Spatrick /* In mpi.h */ 3080e5dd7070Spatrick typedef int MPI_Datatype; 3081e5dd7070Spatrick struct internal_mpi_double_int { double d; int i; }; 3082e5dd7070Spatrick extern struct mpi_datatype mpi_datatype_double_int 3083e5dd7070Spatrick __attribute__(( type_tag_for_datatype(mpi, 3084e5dd7070Spatrick struct internal_mpi_double_int, layout_compatible) )); 3085e5dd7070Spatrick 3086e5dd7070Spatrick #define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int) 3087e5dd7070Spatrick 3088e5dd7070Spatrick int MPI_Send(void *buf, int count, MPI_Datatype datatype, ...) 3089e5dd7070Spatrick __attribute__(( pointer_with_type_tag(mpi,1,3) )); 3090e5dd7070Spatrick 3091e5dd7070Spatrick /* In user code */ 3092e5dd7070Spatrick struct my_pair { double a; int b; }; 3093e5dd7070Spatrick struct my_pair *buffer; 3094e5dd7070Spatrick MPI_Send(buffer, 1, MPI_DOUBLE_INT /*, ... */); // no warning because the 3095e5dd7070Spatrick // layout of my_pair is 3096e5dd7070Spatrick // compatible with that of 3097e5dd7070Spatrick // internal_mpi_double_int 3098e5dd7070Spatrick 3099e5dd7070Spatrick struct my_int_pair { int a; int b; } 3100e5dd7070Spatrick struct my_int_pair *buffer2; 3101e5dd7070Spatrick MPI_Send(buffer2, 1, MPI_DOUBLE_INT /*, ... */); // warning because the 3102e5dd7070Spatrick // layout of my_int_pair 3103e5dd7070Spatrick // does not match that of 3104e5dd7070Spatrick // internal_mpi_double_int 3105e5dd7070Spatrick 3106e5dd7070Spatrick * ``must_be_null`` specifies that the function argument specified by either 3107e5dd7070Spatrick ``arg_idx`` (for the ``argument_with_type_tag`` attribute) or ``ptr_idx`` (for 3108e5dd7070Spatrick the ``pointer_with_type_tag`` attribute) should be a null pointer constant. 3109e5dd7070Spatrick The second argument to the ``type_tag_for_datatype`` attribute is ignored. For 3110e5dd7070Spatrick example: 3111e5dd7070Spatrick 3112e5dd7070Spatrick .. code-block:: c++ 3113e5dd7070Spatrick 3114e5dd7070Spatrick /* In mpi.h */ 3115e5dd7070Spatrick typedef int MPI_Datatype; 3116e5dd7070Spatrick extern struct mpi_datatype mpi_datatype_null 3117e5dd7070Spatrick __attribute__(( type_tag_for_datatype(mpi, void, must_be_null) )); 3118e5dd7070Spatrick 3119e5dd7070Spatrick #define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null) 3120e5dd7070Spatrick int MPI_Send(void *buf, int count, MPI_Datatype datatype, ...) 3121e5dd7070Spatrick __attribute__(( pointer_with_type_tag(mpi,1,3) )); 3122e5dd7070Spatrick 3123e5dd7070Spatrick /* In user code */ 3124e5dd7070Spatrick struct my_pair { double a; int b; }; 3125e5dd7070Spatrick struct my_pair *buffer; 3126e5dd7070Spatrick MPI_Send(buffer, 1, MPI_DATATYPE_NULL /*, ... */); // warning: MPI_DATATYPE_NULL 3127e5dd7070Spatrick // was specified but buffer 3128e5dd7070Spatrick // is not a null pointer 3129e5dd7070Spatrick }]; 3130e5dd7070Spatrick} 3131e5dd7070Spatrick 3132e5dd7070Spatrickdef FlattenDocs : Documentation { 3133e5dd7070Spatrick let Category = DocCatFunction; 3134e5dd7070Spatrick let Content = [{ 3135e5dd7070SpatrickThe ``flatten`` attribute causes calls within the attributed function to 3136e5dd7070Spatrickbe inlined unless it is impossible to do so, for example if the body of the 3137e5dd7070Spatrickcallee is unavailable or if the callee has the ``noinline`` attribute. 3138e5dd7070Spatrick }]; 3139e5dd7070Spatrick} 3140e5dd7070Spatrick 3141e5dd7070Spatrickdef FormatDocs : Documentation { 3142e5dd7070Spatrick let Category = DocCatFunction; 3143e5dd7070Spatrick let Content = [{ 3144e5dd7070Spatrick 3145e5dd7070SpatrickClang supports the ``format`` attribute, which indicates that the function 3146*12c85518Srobertaccepts (among other possibilities) a ``printf`` or ``scanf``-like format string 3147*12c85518Srobertand corresponding arguments or a ``va_list`` that contains these arguments. 3148e5dd7070Spatrick 3149e5dd7070SpatrickPlease see `GCC documentation about format attribute 3150e5dd7070Spatrick<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details 3151e5dd7070Spatrickabout attribute syntax. 3152e5dd7070Spatrick 3153e5dd7070SpatrickClang implements two kinds of checks with this attribute. 3154e5dd7070Spatrick 3155e5dd7070Spatrick#. Clang checks that the function with the ``format`` attribute is called with 3156e5dd7070Spatrick a format string that uses format specifiers that are allowed, and that 3157e5dd7070Spatrick arguments match the format string. This is the ``-Wformat`` warning, it is 3158e5dd7070Spatrick on by default. 3159e5dd7070Spatrick 3160e5dd7070Spatrick#. Clang checks that the format string argument is a literal string. This is 3161e5dd7070Spatrick the ``-Wformat-nonliteral`` warning, it is off by default. 3162e5dd7070Spatrick 3163e5dd7070Spatrick Clang implements this mostly the same way as GCC, but there is a difference 3164e5dd7070Spatrick for functions that accept a ``va_list`` argument (for example, ``vprintf``). 3165e5dd7070Spatrick GCC does not emit ``-Wformat-nonliteral`` warning for calls to such 3166e5dd7070Spatrick functions. Clang does not warn if the format string comes from a function 3167e5dd7070Spatrick parameter, where the function is annotated with a compatible attribute, 3168e5dd7070Spatrick otherwise it warns. For example: 3169e5dd7070Spatrick 3170e5dd7070Spatrick .. code-block:: c 3171e5dd7070Spatrick 3172e5dd7070Spatrick __attribute__((__format__ (__scanf__, 1, 3))) 3173e5dd7070Spatrick void foo(const char* s, char *buf, ...) { 3174e5dd7070Spatrick va_list ap; 3175e5dd7070Spatrick va_start(ap, buf); 3176e5dd7070Spatrick 3177e5dd7070Spatrick vprintf(s, ap); // warning: format string is not a string literal 3178e5dd7070Spatrick } 3179e5dd7070Spatrick 3180e5dd7070Spatrick In this case we warn because ``s`` contains a format string for a 3181e5dd7070Spatrick ``scanf``-like function, but it is passed to a ``printf``-like function. 3182e5dd7070Spatrick 3183e5dd7070Spatrick If the attribute is removed, clang still warns, because the format string is 3184e5dd7070Spatrick not a string literal. 3185e5dd7070Spatrick 3186e5dd7070Spatrick Another example: 3187e5dd7070Spatrick 3188e5dd7070Spatrick .. code-block:: c 3189e5dd7070Spatrick 3190e5dd7070Spatrick __attribute__((__format__ (__printf__, 1, 3))) 3191e5dd7070Spatrick void foo(const char* s, char *buf, ...) { 3192e5dd7070Spatrick va_list ap; 3193e5dd7070Spatrick va_start(ap, buf); 3194e5dd7070Spatrick 3195e5dd7070Spatrick vprintf(s, ap); // warning 3196e5dd7070Spatrick } 3197e5dd7070Spatrick 3198e5dd7070Spatrick In this case Clang does not warn because the format string ``s`` and 3199e5dd7070Spatrick the corresponding arguments are annotated. If the arguments are 3200e5dd7070Spatrick incorrect, the caller of ``foo`` will receive a warning. 3201*12c85518Srobert 3202*12c85518SrobertAs an extension to GCC's behavior, Clang accepts the ``format`` attribute on 3203*12c85518Srobertnon-variadic functions. Clang checks non-variadic format functions for the same 3204*12c85518Srobertclasses of issues that can be found on variadic functions, as controlled by the 3205*12c85518Srobertsame warning flags, except that the types of formatted arguments is forced by 3206*12c85518Srobertthe function signature. For example: 3207*12c85518Srobert 3208*12c85518Srobert.. code-block:: c 3209*12c85518Srobert 3210*12c85518Srobert __attribute__((__format__(__printf__, 1, 2))) 3211*12c85518Srobert void fmt(const char *s, const char *a, int b); 3212*12c85518Srobert 3213*12c85518Srobert void bar(void) { 3214*12c85518Srobert fmt("%s %i", "hello", 123); // OK 3215*12c85518Srobert fmt("%i %g", "hello", 123); // warning: arguments don't match format 3216*12c85518Srobert extern const char *fmt; 3217*12c85518Srobert fmt(fmt, "hello", 123); // warning: format string is not a string literal 3218*12c85518Srobert } 3219*12c85518Srobert 3220*12c85518SrobertWhen using the format attribute on a variadic function, the first data parameter 3221*12c85518Srobert_must_ be the index of the ellipsis in the parameter list. Clang will generate 3222*12c85518Sroberta diagnostic otherwise, as it wouldn't be possible to forward that argument list 3223*12c85518Srobertto `printf`-family functions. For instance, this is an error: 3224*12c85518Srobert 3225*12c85518Srobert.. code-block:: c 3226*12c85518Srobert 3227*12c85518Srobert __attribute__((__format__(__printf__, 1, 2))) 3228*12c85518Srobert void fmt(const char *s, int b, ...); 3229*12c85518Srobert // ^ error: format attribute parameter 3 is out of bounds 3230*12c85518Srobert // (must be __printf__, 1, 3) 3231*12c85518Srobert 3232*12c85518SrobertUsing the ``format`` attribute on a non-variadic function emits a GCC 3233*12c85518Srobertcompatibility diagnostic. 3234e5dd7070Spatrick }]; 3235e5dd7070Spatrick} 3236e5dd7070Spatrick 3237e5dd7070Spatrickdef AlignValueDocs : Documentation { 3238e5dd7070Spatrick let Category = DocCatType; 3239e5dd7070Spatrick let Content = [{ 3240e5dd7070SpatrickThe align_value attribute can be added to the typedef of a pointer type or the 3241e5dd7070Spatrickdeclaration of a variable of pointer or reference type. It specifies that the 3242e5dd7070Spatrickpointer will point to, or the reference will bind to, only objects with at 3243e5dd7070Spatrickleast the provided alignment. This alignment value must be some positive power 3244e5dd7070Spatrickof 2. 3245e5dd7070Spatrick 3246e5dd7070Spatrick .. code-block:: c 3247e5dd7070Spatrick 3248e5dd7070Spatrick typedef double * aligned_double_ptr __attribute__((align_value(64))); 3249e5dd7070Spatrick void foo(double & x __attribute__((align_value(128)), 3250e5dd7070Spatrick aligned_double_ptr y) { ... } 3251e5dd7070Spatrick 3252e5dd7070SpatrickIf the pointer value does not have the specified alignment at runtime, the 3253e5dd7070Spatrickbehavior of the program is undefined. 3254e5dd7070Spatrick }]; 3255e5dd7070Spatrick} 3256e5dd7070Spatrick 3257e5dd7070Spatrickdef FlagEnumDocs : Documentation { 3258e5dd7070Spatrick let Category = DocCatDecl; 3259e5dd7070Spatrick let Content = [{ 3260e5dd7070SpatrickThis attribute can be added to an enumerator to signal to the compiler that it 3261e5dd7070Spatrickis intended to be used as a flag type. This will cause the compiler to assume 3262e5dd7070Spatrickthat the range of the type includes all of the values that you can get by 3263e5dd7070Spatrickmanipulating bits of the enumerator when issuing warnings. 3264e5dd7070Spatrick }]; 3265e5dd7070Spatrick} 3266e5dd7070Spatrick 3267e5dd7070Spatrickdef AsmLabelDocs : Documentation { 3268e5dd7070Spatrick let Category = DocCatDecl; 3269e5dd7070Spatrick let Content = [{ 3270e5dd7070SpatrickThis attribute can be used on a function or variable to specify its symbol name. 3271e5dd7070Spatrick 3272a9ac8606SpatrickOn some targets, all C symbols are prefixed by default with a single character, 3273a9ac8606Spatricktypically ``_``. This was done historically to distinguish them from symbols 3274a9ac8606Spatrickused by other languages. (This prefix is also added to the standard Itanium 3275a9ac8606SpatrickC++ ABI prefix on "mangled" symbol names, so that e.g. on such targets the true 3276a9ac8606Spatricksymbol name for a C++ variable declared as ``int cppvar;`` would be 3277a9ac8606Spatrick``__Z6cppvar``; note the two underscores.) This prefix is *not* added to the 3278a9ac8606Spatricksymbol names specified by the ``asm`` attribute; programmers wishing to match a 3279a9ac8606SpatrickC symbol name must compensate for this. 3280e5dd7070Spatrick 3281e5dd7070SpatrickFor example, consider the following C code: 3282e5dd7070Spatrick 3283e5dd7070Spatrick.. code-block:: c 3284e5dd7070Spatrick 3285e5dd7070Spatrick int var1 asm("altvar") = 1; // "altvar" in symbol table. 3286e5dd7070Spatrick int var2 = 1; // "_var2" in symbol table. 3287e5dd7070Spatrick 3288e5dd7070Spatrick void func1(void) asm("altfunc"); 3289e5dd7070Spatrick void func1(void) {} // "altfunc" in symbol table. 3290e5dd7070Spatrick void func2(void) {} // "_func2" in symbol table. 3291e5dd7070Spatrick 3292e5dd7070SpatrickClang's implementation of this attribute is compatible with GCC's, `documented here <https://gcc.gnu.org/onlinedocs/gcc/Asm-Labels.html>`_. 3293e5dd7070Spatrick 3294a9ac8606SpatrickWhile it is possible to use this attribute to name a special symbol used 3295a9ac8606Spatrickinternally by the compiler, such as an LLVM intrinsic, this is neither 3296a9ac8606Spatrickrecommended nor supported and may cause the compiler to crash or miscompile. 3297a9ac8606SpatrickUsers who wish to gain access to intrinsic behavior are strongly encouraged to 3298a9ac8606Spatrickrequest new builtin functions. 3299e5dd7070Spatrick }]; 3300e5dd7070Spatrick} 3301e5dd7070Spatrick 3302e5dd7070Spatrickdef EnumExtensibilityDocs : Documentation { 3303e5dd7070Spatrick let Category = DocCatDecl; 3304e5dd7070Spatrick let Content = [{ 3305e5dd7070SpatrickAttribute ``enum_extensibility`` is used to distinguish between enum definitions 3306e5dd7070Spatrickthat are extensible and those that are not. The attribute can take either 3307e5dd7070Spatrick``closed`` or ``open`` as an argument. ``closed`` indicates a variable of the 3308e5dd7070Spatrickenum type takes a value that corresponds to one of the enumerators listed in the 3309e5dd7070Spatrickenum definition or, when the enum is annotated with ``flag_enum``, a value that 3310e5dd7070Spatrickcan be constructed using values corresponding to the enumerators. ``open`` 3311e5dd7070Spatrickindicates a variable of the enum type can take any values allowed by the 3312e5dd7070Spatrickstandard and instructs clang to be more lenient when issuing warnings. 3313e5dd7070Spatrick 3314e5dd7070Spatrick.. code-block:: c 3315e5dd7070Spatrick 3316e5dd7070Spatrick enum __attribute__((enum_extensibility(closed))) ClosedEnum { 3317e5dd7070Spatrick A0, A1 3318e5dd7070Spatrick }; 3319e5dd7070Spatrick 3320e5dd7070Spatrick enum __attribute__((enum_extensibility(open))) OpenEnum { 3321e5dd7070Spatrick B0, B1 3322e5dd7070Spatrick }; 3323e5dd7070Spatrick 3324e5dd7070Spatrick enum __attribute__((enum_extensibility(closed),flag_enum)) ClosedFlagEnum { 3325e5dd7070Spatrick C0 = 1 << 0, C1 = 1 << 1 3326e5dd7070Spatrick }; 3327e5dd7070Spatrick 3328e5dd7070Spatrick enum __attribute__((enum_extensibility(open),flag_enum)) OpenFlagEnum { 3329e5dd7070Spatrick D0 = 1 << 0, D1 = 1 << 1 3330e5dd7070Spatrick }; 3331e5dd7070Spatrick 3332e5dd7070Spatrick void foo1() { 3333e5dd7070Spatrick enum ClosedEnum ce; 3334e5dd7070Spatrick enum OpenEnum oe; 3335e5dd7070Spatrick enum ClosedFlagEnum cfe; 3336e5dd7070Spatrick enum OpenFlagEnum ofe; 3337e5dd7070Spatrick 3338e5dd7070Spatrick ce = A1; // no warnings 3339e5dd7070Spatrick ce = 100; // warning issued 3340e5dd7070Spatrick oe = B1; // no warnings 3341e5dd7070Spatrick oe = 100; // no warnings 3342e5dd7070Spatrick cfe = C0 | C1; // no warnings 3343e5dd7070Spatrick cfe = C0 | C1 | 4; // warning issued 3344e5dd7070Spatrick ofe = D0 | D1; // no warnings 3345e5dd7070Spatrick ofe = D0 | D1 | 4; // no warnings 3346e5dd7070Spatrick } 3347e5dd7070Spatrick 3348e5dd7070Spatrick }]; 3349e5dd7070Spatrick} 3350e5dd7070Spatrick 3351e5dd7070Spatrickdef EmptyBasesDocs : Documentation { 3352e5dd7070Spatrick let Category = DocCatDecl; 3353e5dd7070Spatrick let Content = [{ 3354e5dd7070SpatrickThe empty_bases attribute permits the compiler to utilize the 3355e5dd7070Spatrickempty-base-optimization more frequently. 3356e5dd7070SpatrickThis attribute only applies to struct, class, and union types. 3357e5dd7070SpatrickIt is only supported when using the Microsoft C++ ABI. 3358e5dd7070Spatrick }]; 3359e5dd7070Spatrick} 3360e5dd7070Spatrick 3361e5dd7070Spatrickdef LayoutVersionDocs : Documentation { 3362e5dd7070Spatrick let Category = DocCatDecl; 3363e5dd7070Spatrick let Content = [{ 3364e5dd7070SpatrickThe layout_version attribute requests that the compiler utilize the class 3365e5dd7070Spatricklayout rules of a particular compiler version. 3366e5dd7070SpatrickThis attribute only applies to struct, class, and union types. 3367e5dd7070SpatrickIt is only supported when using the Microsoft C++ ABI. 3368e5dd7070Spatrick }]; 3369e5dd7070Spatrick} 3370e5dd7070Spatrick 3371e5dd7070Spatrickdef LifetimeBoundDocs : Documentation { 3372e5dd7070Spatrick let Category = DocCatFunction; 3373e5dd7070Spatrick let Content = [{ 3374a9ac8606SpatrickThe ``lifetimebound`` attribute on a function parameter or implicit object 3375a9ac8606Spatrickparameter indicates that objects that are referred to by that parameter may 3376a9ac8606Spatrickalso be referred to by the return value of the annotated function (or, for a 3377a9ac8606Spatrickparameter of a constructor, by the value of the constructed object). It is only 3378a9ac8606Spatricksupported in C++. 3379e5dd7070Spatrick 3380a9ac8606SpatrickBy default, a reference is considered to refer to its referenced object, a 3381a9ac8606Spatrickpointer is considered to refer to its pointee, a ``std::initializer_list<T>`` 3382a9ac8606Spatrickis considered to refer to its underlying array, and aggregates (arrays and 3383a9ac8606Spatricksimple ``struct``\s) are considered to refer to all objects that their 3384a9ac8606Spatricktransitive subobjects refer to. 3385a9ac8606Spatrick 3386a9ac8606SpatrickClang warns if it is able to detect that an object or reference refers to 3387a9ac8606Spatrickanother object with a shorter lifetime. For example, Clang will warn if a 3388a9ac8606Spatrickfunction returns a reference to a local variable, or if a reference is bound to 3389a9ac8606Spatricka temporary object whose lifetime is not extended. By using the 3390a9ac8606Spatrick``lifetimebound`` attribute, this determination can be extended to look through 3391a9ac8606Spatrickuser-declared functions. For example: 3392a9ac8606Spatrick 3393a9ac8606Spatrick.. code-block:: c++ 3394a9ac8606Spatrick 3395a9ac8606Spatrick // Returns m[key] if key is present, or default_value if not. 3396a9ac8606Spatrick template<typename T, typename U> 3397a9ac8606Spatrick const U &get_or_default(const std::map<T, U> &m [[clang::lifetimebound]], 3398a9ac8606Spatrick const T &key, /* note, not lifetimebound */ 3399a9ac8606Spatrick const U &default_value [[clang::lifetimebound]]); 3400a9ac8606Spatrick 3401a9ac8606Spatrick std::map<std::string, std::string> m; 3402a9ac8606Spatrick // warning: temporary "bar"s that might be bound to local reference 'val' 3403a9ac8606Spatrick // will be destroyed at the end of the full-expression 3404a9ac8606Spatrick const std::string &val = get_or_default(m, "foo"s, "bar"s); 3405a9ac8606Spatrick 3406a9ac8606Spatrick // No warning in this case. 3407a9ac8606Spatrick std::string def_val = "bar"s; 3408a9ac8606Spatrick const std::string &val = get_or_default(m, "foo"s, def_val); 3409a9ac8606Spatrick 3410a9ac8606SpatrickThe attribute can be applied to the implicit ``this`` parameter of a member 3411a9ac8606Spatrickfunction by writing the attribute after the function type: 3412a9ac8606Spatrick 3413a9ac8606Spatrick.. code-block:: c++ 3414a9ac8606Spatrick 3415a9ac8606Spatrick struct string { 3416a9ac8606Spatrick // The returned pointer should not outlive ``*this``. 3417a9ac8606Spatrick const char *data() const [[clang::lifetimebound]]; 3418a9ac8606Spatrick }; 3419a9ac8606Spatrick 3420a9ac8606SpatrickThis attribute is inspired by the C++ committee paper `P0936R0 3421a9ac8606Spatrick<http://wg21.link/p0936r0>`_, but does not affect whether temporary objects 3422a9ac8606Spatrickhave their lifetimes extended. 3423e5dd7070Spatrick }]; 3424e5dd7070Spatrick} 3425e5dd7070Spatrick 3426e5dd7070Spatrickdef TrivialABIDocs : Documentation { 3427e5dd7070Spatrick let Category = DocCatDecl; 3428e5dd7070Spatrick let Content = [{ 3429e5dd7070SpatrickThe ``trivial_abi`` attribute can be applied to a C++ class, struct, or union. 3430e5dd7070SpatrickIt instructs the compiler to pass and return the type using the C ABI for the 3431e5dd7070Spatrickunderlying type when the type would otherwise be considered non-trivial for the 3432e5dd7070Spatrickpurpose of calls. 3433ec727ea7SpatrickA class annotated with ``trivial_abi`` can have non-trivial destructors or 3434ec727ea7Spatrickcopy/move constructors without automatically becoming non-trivial for the 3435ec727ea7Spatrickpurposes of calls. For example: 3436e5dd7070Spatrick 3437e5dd7070Spatrick .. code-block:: c++ 3438e5dd7070Spatrick 3439ec727ea7Spatrick // A is trivial for the purposes of calls because ``trivial_abi`` makes the 3440e5dd7070Spatrick // user-provided special functions trivial. 3441e5dd7070Spatrick struct __attribute__((trivial_abi)) A { 3442e5dd7070Spatrick ~A(); 3443e5dd7070Spatrick A(const A &); 3444e5dd7070Spatrick A(A &&); 3445e5dd7070Spatrick int x; 3446e5dd7070Spatrick }; 3447e5dd7070Spatrick 3448e5dd7070Spatrick // B's destructor and copy/move constructor are considered trivial for the 3449e5dd7070Spatrick // purpose of calls because A is trivial. 3450e5dd7070Spatrick struct B { 3451e5dd7070Spatrick A a; 3452e5dd7070Spatrick }; 3453e5dd7070Spatrick 3454e5dd7070SpatrickIf a type is trivial for the purposes of calls, has a non-trivial destructor, 3455e5dd7070Spatrickand is passed as an argument by value, the convention is that the callee will 3456e5dd7070Spatrickdestroy the object before returning. 3457e5dd7070Spatrick 3458*12c85518SrobertIf a type is trivial for the purpose of calls, it is assumed to be trivially 3459*12c85518Srobertrelocatable for the purpose of ``__is_trivially_relocatable``. 3460*12c85518Srobert 3461e5dd7070SpatrickAttribute ``trivial_abi`` has no effect in the following cases: 3462e5dd7070Spatrick 3463e5dd7070Spatrick- The class directly declares a virtual base or virtual methods. 3464ec727ea7Spatrick- Copy constructors and move constructors of the class are all deleted. 3465e5dd7070Spatrick- The class has a base class that is non-trivial for the purposes of calls. 3466a9ac8606Spatrick- The class has a non-static data member whose type is non-trivial for the 3467a9ac8606Spatrick purposes of calls, which includes: 3468e5dd7070Spatrick 3469e5dd7070Spatrick - classes that are non-trivial for the purposes of calls 3470e5dd7070Spatrick - __weak-qualified types in Objective-C++ 3471e5dd7070Spatrick - arrays of any of the above 3472e5dd7070Spatrick }]; 3473e5dd7070Spatrick} 3474e5dd7070Spatrick 3475e5dd7070Spatrickdef MSInheritanceDocs : Documentation { 3476e5dd7070Spatrick let Category = DocCatDecl; 3477e5dd7070Spatrick let Heading = "__single_inhertiance, __multiple_inheritance, __virtual_inheritance"; 3478e5dd7070Spatrick let Content = [{ 3479e5dd7070SpatrickThis collection of keywords is enabled under ``-fms-extensions`` and controls 3480e5dd7070Spatrickthe pointer-to-member representation used on ``*-*-win32`` targets. 3481e5dd7070Spatrick 3482e5dd7070SpatrickThe ``*-*-win32`` targets utilize a pointer-to-member representation which 3483e5dd7070Spatrickvaries in size and alignment depending on the definition of the underlying 3484e5dd7070Spatrickclass. 3485e5dd7070Spatrick 3486e5dd7070SpatrickHowever, this is problematic when a forward declaration is only available and 3487e5dd7070Spatrickno definition has been made yet. In such cases, Clang is forced to utilize the 3488e5dd7070Spatrickmost general representation that is available to it. 3489e5dd7070Spatrick 3490e5dd7070SpatrickThese keywords make it possible to use a pointer-to-member representation other 3491e5dd7070Spatrickthan the most general one regardless of whether or not the definition will ever 3492e5dd7070Spatrickbe present in the current translation unit. 3493e5dd7070Spatrick 3494e5dd7070SpatrickThis family of keywords belong between the ``class-key`` and ``class-name``: 3495e5dd7070Spatrick 3496e5dd7070Spatrick.. code-block:: c++ 3497e5dd7070Spatrick 3498e5dd7070Spatrick struct __single_inheritance S; 3499e5dd7070Spatrick int S::*i; 3500e5dd7070Spatrick struct S {}; 3501e5dd7070Spatrick 3502e5dd7070SpatrickThis keyword can be applied to class templates but only has an effect when used 3503e5dd7070Spatrickon full specializations: 3504e5dd7070Spatrick 3505e5dd7070Spatrick.. code-block:: c++ 3506e5dd7070Spatrick 3507e5dd7070Spatrick template <typename T, typename U> struct __single_inheritance A; // warning: inheritance model ignored on primary template 3508e5dd7070Spatrick template <typename T> struct __multiple_inheritance A<T, T>; // warning: inheritance model ignored on partial specialization 3509e5dd7070Spatrick template <> struct __single_inheritance A<int, float>; 3510e5dd7070Spatrick 3511e5dd7070SpatrickNote that choosing an inheritance model less general than strictly necessary is 3512e5dd7070Spatrickan error: 3513e5dd7070Spatrick 3514e5dd7070Spatrick.. code-block:: c++ 3515e5dd7070Spatrick 3516e5dd7070Spatrick struct __multiple_inheritance S; // error: inheritance model does not match definition 3517e5dd7070Spatrick int S::*i; 3518e5dd7070Spatrick struct S {}; 3519e5dd7070Spatrick}]; 3520e5dd7070Spatrick} 3521e5dd7070Spatrick 3522e5dd7070Spatrickdef MSNoVTableDocs : Documentation { 3523e5dd7070Spatrick let Category = DocCatDecl; 3524e5dd7070Spatrick let Content = [{ 3525e5dd7070SpatrickThis attribute can be added to a class declaration or definition to signal to 3526e5dd7070Spatrickthe compiler that constructors and destructors will not reference the virtual 3527e5dd7070Spatrickfunction table. It is only supported when using the Microsoft C++ ABI. 3528e5dd7070Spatrick }]; 3529e5dd7070Spatrick} 3530e5dd7070Spatrick 3531e5dd7070Spatrickdef OptnoneDocs : Documentation { 3532e5dd7070Spatrick let Category = DocCatFunction; 3533e5dd7070Spatrick let Content = [{ 3534e5dd7070SpatrickThe ``optnone`` attribute suppresses essentially all optimizations 3535e5dd7070Spatrickon a function or method, regardless of the optimization level applied to 3536e5dd7070Spatrickthe compilation unit as a whole. This is particularly useful when you 3537e5dd7070Spatrickneed to debug a particular function, but it is infeasible to build the 3538e5dd7070Spatrickentire application without optimization. Avoiding optimization on the 3539e5dd7070Spatrickspecified function can improve the quality of the debugging information 3540e5dd7070Spatrickfor that function. 3541e5dd7070Spatrick 3542e5dd7070SpatrickThis attribute is incompatible with the ``always_inline`` and ``minsize`` 3543e5dd7070Spatrickattributes. 3544e5dd7070Spatrick }]; 3545e5dd7070Spatrick} 3546e5dd7070Spatrick 3547e5dd7070Spatrickdef LoopHintDocs : Documentation { 3548e5dd7070Spatrick let Category = DocCatStmt; 3549e5dd7070Spatrick let Heading = "#pragma clang loop"; 3550e5dd7070Spatrick let Content = [{ 3551e5dd7070SpatrickThe ``#pragma clang loop`` directive allows loop optimization hints to be 3552e5dd7070Spatrickspecified for the subsequent loop. The directive allows pipelining to be 3553e5dd7070Spatrickdisabled, or vectorization, vector predication, interleaving, and unrolling to 3554e5dd7070Spatrickbe enabled or disabled. Vector width, vector predication, interleave count, 3555e5dd7070Spatrickunrolling count, and the initiation interval for pipelining can be explicitly 3556e5dd7070Spatrickspecified. See `language extensions 3557e5dd7070Spatrick<http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_ 3558e5dd7070Spatrickfor details. 3559e5dd7070Spatrick }]; 3560e5dd7070Spatrick} 3561e5dd7070Spatrick 3562e5dd7070Spatrickdef UnrollHintDocs : Documentation { 3563e5dd7070Spatrick let Category = DocCatStmt; 3564e5dd7070Spatrick let Heading = "#pragma unroll, #pragma nounroll"; 3565e5dd7070Spatrick let Content = [{ 3566e5dd7070SpatrickLoop unrolling optimization hints can be specified with ``#pragma unroll`` and 3567e5dd7070Spatrick``#pragma nounroll``. The pragma is placed immediately before a for, while, 3568a9ac8606Spatrickdo-while, or c++11 range-based for loop. GCC's loop unrolling hints 3569a9ac8606Spatrick``#pragma GCC unroll`` and ``#pragma GCC nounroll`` are also supported and have 3570a9ac8606Spatrickidentical semantics to ``#pragma unroll`` and ``#pragma nounroll``. 3571e5dd7070Spatrick 3572e5dd7070SpatrickSpecifying ``#pragma unroll`` without a parameter directs the loop unroller to 3573e5dd7070Spatrickattempt to fully unroll the loop if the trip count is known at compile time and 3574e5dd7070Spatrickattempt to partially unroll the loop if the trip count is not known at compile 3575e5dd7070Spatricktime: 3576e5dd7070Spatrick 3577e5dd7070Spatrick.. code-block:: c++ 3578e5dd7070Spatrick 3579e5dd7070Spatrick #pragma unroll 3580e5dd7070Spatrick for (...) { 3581e5dd7070Spatrick ... 3582e5dd7070Spatrick } 3583e5dd7070Spatrick 3584e5dd7070SpatrickSpecifying the optional parameter, ``#pragma unroll _value_``, directs the 3585e5dd7070Spatrickunroller to unroll the loop ``_value_`` times. The parameter may optionally be 3586e5dd7070Spatrickenclosed in parentheses: 3587e5dd7070Spatrick 3588e5dd7070Spatrick.. code-block:: c++ 3589e5dd7070Spatrick 3590e5dd7070Spatrick #pragma unroll 16 3591e5dd7070Spatrick for (...) { 3592e5dd7070Spatrick ... 3593e5dd7070Spatrick } 3594e5dd7070Spatrick 3595e5dd7070Spatrick #pragma unroll(16) 3596e5dd7070Spatrick for (...) { 3597e5dd7070Spatrick ... 3598e5dd7070Spatrick } 3599e5dd7070Spatrick 3600e5dd7070SpatrickSpecifying ``#pragma nounroll`` indicates that the loop should not be unrolled: 3601e5dd7070Spatrick 3602e5dd7070Spatrick.. code-block:: c++ 3603e5dd7070Spatrick 3604e5dd7070Spatrick #pragma nounroll 3605e5dd7070Spatrick for (...) { 3606e5dd7070Spatrick ... 3607e5dd7070Spatrick } 3608e5dd7070Spatrick 3609e5dd7070Spatrick``#pragma unroll`` and ``#pragma unroll _value_`` have identical semantics to 3610*12c85518Srobert``#pragma clang loop unroll(enable)`` and 3611e5dd7070Spatrick``#pragma clang loop unroll_count(_value_)`` respectively. ``#pragma nounroll`` 3612e5dd7070Spatrickis equivalent to ``#pragma clang loop unroll(disable)``. See 3613e5dd7070Spatrick`language extensions 3614e5dd7070Spatrick<http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_ 3615e5dd7070Spatrickfor further details including limitations of the unroll hints. 3616e5dd7070Spatrick }]; 3617e5dd7070Spatrick} 3618e5dd7070Spatrick 3619e5dd7070Spatrickdef PipelineHintDocs : Documentation { 3620e5dd7070Spatrick let Category = DocCatStmt; 3621e5dd7070Spatrick let Heading = "#pragma clang loop pipeline, #pragma clang loop pipeline_initiation_interval"; 3622e5dd7070Spatrick let Content = [{ 3623e5dd7070Spatrick Software Pipelining optimization is a technique used to optimize loops by 3624e5dd7070Spatrick utilizing instruction-level parallelism. It reorders loop instructions to 3625e5dd7070Spatrick overlap iterations. As a result, the next iteration starts before the previous 3626e5dd7070Spatrick iteration has finished. The module scheduling technique creates a schedule for 3627e5dd7070Spatrick one iteration such that when repeating at regular intervals, no inter-iteration 3628e5dd7070Spatrick dependencies are violated. This constant interval(in cycles) between the start 3629e5dd7070Spatrick of iterations is called the initiation interval. i.e. The initiation interval 3630e5dd7070Spatrick is the number of cycles between two iterations of an unoptimized loop in the 3631e5dd7070Spatrick newly created schedule. A new, optimized loop is created such that a single iteration 3632e5dd7070Spatrick of the loop executes in the same number of cycles as the initiation interval. 3633e5dd7070Spatrick For further details see <https://llvm.org/pubs/2005-06-17-LattnerMSThesis-book.pdf>. 3634e5dd7070Spatrick 3635e5dd7070Spatrick ``#pragma clang loop pipeline and #pragma loop pipeline_initiation_interval`` 3636e5dd7070Spatrick could be used as hints for the software pipelining optimization. The pragma is 3637e5dd7070Spatrick placed immediately before a for, while, do-while, or a C++11 range-based for 3638e5dd7070Spatrick loop. 3639e5dd7070Spatrick 3640e5dd7070Spatrick Using ``#pragma clang loop pipeline(disable)`` avoids the software pipelining 3641e5dd7070Spatrick optimization. The disable state can only be specified: 3642e5dd7070Spatrick 3643e5dd7070Spatrick .. code-block:: c++ 3644e5dd7070Spatrick 3645e5dd7070Spatrick #pragma clang loop pipeline(disable) 3646e5dd7070Spatrick for (...) { 3647e5dd7070Spatrick ... 3648e5dd7070Spatrick } 3649e5dd7070Spatrick 3650e5dd7070Spatrick Using ``#pragma loop pipeline_initiation_interval`` instructs 3651e5dd7070Spatrick the software pipeliner to try the specified initiation interval. 3652e5dd7070Spatrick If a schedule was found then the resulting loop iteration would have 3653e5dd7070Spatrick the specified cycle count. If a schedule was not found then loop 3654e5dd7070Spatrick remains unchanged. The initiation interval must be a positive number 3655e5dd7070Spatrick greater than zero: 3656e5dd7070Spatrick 3657e5dd7070Spatrick .. code-block:: c++ 3658e5dd7070Spatrick 3659e5dd7070Spatrick #pragma loop pipeline_initiation_interval(10) 3660e5dd7070Spatrick for (...) { 3661e5dd7070Spatrick ... 3662e5dd7070Spatrick } 3663e5dd7070Spatrick 3664e5dd7070Spatrick }]; 3665e5dd7070Spatrick} 3666e5dd7070Spatrick 3667e5dd7070Spatrickdef OpenCLUnrollHintDocs : Documentation { 3668e5dd7070Spatrick let Category = DocCatStmt; 3669e5dd7070Spatrick let Content = [{ 3670e5dd7070SpatrickThe opencl_unroll_hint attribute qualifier can be used to specify that a loop 3671e5dd7070Spatrick(for, while and do loops) can be unrolled. This attribute qualifier can be 3672e5dd7070Spatrickused to specify full unrolling or partial unrolling by a specified amount. 3673e5dd7070SpatrickThis is a compiler hint and the compiler may ignore this directive. See 3674e5dd7070Spatrick`OpenCL v2.0 <https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf>`_ 3675e5dd7070Spatricks6.11.5 for details. 3676e5dd7070Spatrick }]; 3677e5dd7070Spatrick} 3678e5dd7070Spatrick 3679e5dd7070Spatrickdef OpenCLIntelReqdSubGroupSizeDocs : Documentation { 3680e5dd7070Spatrick let Category = DocCatStmt; 3681e5dd7070Spatrick let Content = [{ 3682e5dd7070SpatrickThe optional attribute intel_reqd_sub_group_size can be used to indicate that 3683e5dd7070Spatrickthe kernel must be compiled and executed with the specified subgroup size. When 3684e5dd7070Spatrickthis attribute is present, get_max_sub_group_size() is guaranteed to return the 3685e5dd7070Spatrickspecified integer value. This is important for the correctness of many subgroup 3686e5dd7070Spatrickalgorithms, and in some cases may be used by the compiler to generate more optimal 3687e5dd7070Spatrickcode. See `cl_intel_required_subgroup_size 3688e5dd7070Spatrick<https://www.khronos.org/registry/OpenCL/extensions/intel/cl_intel_required_subgroup_size.txt>` 3689e5dd7070Spatrickfor details. 3690e5dd7070Spatrick }]; 3691e5dd7070Spatrick} 3692e5dd7070Spatrick 3693e5dd7070Spatrickdef OpenCLAccessDocs : Documentation { 3694e5dd7070Spatrick let Category = DocCatStmt; 3695e5dd7070Spatrick let Heading = "__read_only, __write_only, __read_write (read_only, write_only, read_write)"; 3696e5dd7070Spatrick let Content = [{ 3697e5dd7070SpatrickThe access qualifiers must be used with image object arguments or pipe arguments 3698e5dd7070Spatrickto declare if they are being read or written by a kernel or function. 3699e5dd7070Spatrick 3700e5dd7070SpatrickThe read_only/__read_only, write_only/__write_only and read_write/__read_write 3701e5dd7070Spatricknames are reserved for use as access qualifiers and shall not be used otherwise. 3702e5dd7070Spatrick 3703e5dd7070Spatrick.. code-block:: c 3704e5dd7070Spatrick 3705e5dd7070Spatrick kernel void 3706e5dd7070Spatrick foo (read_only image2d_t imageA, 3707e5dd7070Spatrick write_only image2d_t imageB) { 3708e5dd7070Spatrick ... 3709e5dd7070Spatrick } 3710e5dd7070Spatrick 3711e5dd7070SpatrickIn the above example imageA is a read-only 2D image object, and imageB is a 3712e5dd7070Spatrickwrite-only 2D image object. 3713e5dd7070Spatrick 3714e5dd7070SpatrickThe read_write (or __read_write) qualifier can not be used with pipe. 3715e5dd7070Spatrick 3716e5dd7070SpatrickMore details can be found in the OpenCL C language Spec v2.0, Section 6.6. 3717e5dd7070Spatrick }]; 3718e5dd7070Spatrick} 3719e5dd7070Spatrick 3720e5dd7070Spatrickdef DocOpenCLAddressSpaces : DocumentationCategory<"OpenCL Address Spaces"> { 3721e5dd7070Spatrick let Content = [{ 3722e5dd7070SpatrickThe address space qualifier may be used to specify the region of memory that is 3723e5dd7070Spatrickused to allocate the object. OpenCL supports the following address spaces: 3724e5dd7070Spatrick__generic(generic), __global(global), __local(local), __private(private), 3725e5dd7070Spatrick__constant(constant). 3726e5dd7070Spatrick 3727e5dd7070Spatrick .. code-block:: c 3728e5dd7070Spatrick 3729e5dd7070Spatrick __constant int c = ...; 3730e5dd7070Spatrick 3731e5dd7070Spatrick __generic int* foo(global int* g) { 3732e5dd7070Spatrick __local int* l; 3733e5dd7070Spatrick private int p; 3734e5dd7070Spatrick ... 3735e5dd7070Spatrick return l; 3736e5dd7070Spatrick } 3737e5dd7070Spatrick 3738e5dd7070SpatrickMore details can be found in the OpenCL C language Spec v2.0, Section 6.5. 3739e5dd7070Spatrick }]; 3740e5dd7070Spatrick} 3741e5dd7070Spatrick 3742e5dd7070Spatrickdef OpenCLAddressSpaceGenericDocs : Documentation { 3743e5dd7070Spatrick let Category = DocOpenCLAddressSpaces; 3744e5dd7070Spatrick let Heading = "__generic, generic, [[clang::opencl_generic]]"; 3745e5dd7070Spatrick let Content = [{ 3746e5dd7070SpatrickThe generic address space attribute is only available with OpenCL v2.0 and later. 3747e5dd7070SpatrickIt can be used with pointer types. Variables in global and local scope and 3748e5dd7070Spatrickfunction parameters in non-kernel functions can have the generic address space 3749e5dd7070Spatricktype attribute. It is intended to be a placeholder for any other address space 3750e5dd7070Spatrickexcept for '__constant' in OpenCL code which can be used with multiple address 3751e5dd7070Spatrickspaces. 3752e5dd7070Spatrick }]; 3753e5dd7070Spatrick} 3754e5dd7070Spatrick 3755e5dd7070Spatrickdef OpenCLAddressSpaceConstantDocs : Documentation { 3756e5dd7070Spatrick let Category = DocOpenCLAddressSpaces; 3757e5dd7070Spatrick let Heading = "__constant, constant, [[clang::opencl_constant]]"; 3758e5dd7070Spatrick let Content = [{ 3759e5dd7070SpatrickThe constant address space attribute signals that an object is located in 3760e5dd7070Spatricka constant (non-modifiable) memory region. It is available to all work items. 3761e5dd7070SpatrickAny type can be annotated with the constant address space attribute. Objects 3762e5dd7070Spatrickwith the constant address space qualifier can be declared in any scope and must 3763e5dd7070Spatrickhave an initializer. 3764e5dd7070Spatrick }]; 3765e5dd7070Spatrick} 3766e5dd7070Spatrick 3767e5dd7070Spatrickdef OpenCLAddressSpaceGlobalDocs : Documentation { 3768e5dd7070Spatrick let Category = DocOpenCLAddressSpaces; 3769e5dd7070Spatrick let Heading = "__global, global, [[clang::opencl_global]]"; 3770e5dd7070Spatrick let Content = [{ 3771e5dd7070SpatrickThe global address space attribute specifies that an object is allocated in 3772e5dd7070Spatrickglobal memory, which is accessible by all work items. The content stored in this 3773e5dd7070Spatrickmemory area persists between kernel executions. Pointer types to the global 3774e5dd7070Spatrickaddress space are allowed as function parameters or local variables. Starting 3775e5dd7070Spatrickwith OpenCL v2.0, the global address space can be used with global (program 3776e5dd7070Spatrickscope) variables and static local variable as well. 3777e5dd7070Spatrick }]; 3778e5dd7070Spatrick} 3779e5dd7070Spatrick 3780a9ac8606Spatrickdef OpenCLAddressSpaceGlobalExtDocs : Documentation { 3781a9ac8606Spatrick let Category = DocOpenCLAddressSpaces; 3782a9ac8606Spatrick let Heading = "[[clang::opencl_global_device]], [[clang::opencl_global_host]]"; 3783a9ac8606Spatrick let Content = [{ 3784a9ac8606SpatrickThe ``global_device`` and ``global_host`` address space attributes specify that 3785a9ac8606Spatrickan object is allocated in global memory on the device/host. It helps to 3786a9ac8606Spatrickdistinguish USM (Unified Shared Memory) pointers that access global device 3787a9ac8606Spatrickmemory from those that access global host memory. These new address spaces are 3788a9ac8606Spatricka subset of the ``__global/opencl_global`` address space, the full address space 3789a9ac8606Spatrickset model for OpenCL 2.0 with the extension looks as follows: 3790a9ac8606Spatrick 3791a9ac8606Spatrick | generic->global->host 3792a9ac8606Spatrick | ->device 3793a9ac8606Spatrick | ->private 3794a9ac8606Spatrick | ->local 3795a9ac8606Spatrick | constant 3796a9ac8606Spatrick 3797a9ac8606SpatrickAs ``global_device`` and ``global_host`` are a subset of 3798a9ac8606Spatrick``__global/opencl_global`` address spaces it is allowed to convert 3799a9ac8606Spatrick``global_device`` and ``global_host`` address spaces to 3800a9ac8606Spatrick``__global/opencl_global`` address spaces (following ISO/IEC TR 18037 5.1.3 3801a9ac8606Spatrick"Address space nesting and rules for pointers"). 3802a9ac8606Spatrick }]; 3803a9ac8606Spatrick} 3804a9ac8606Spatrick 3805e5dd7070Spatrickdef OpenCLAddressSpaceLocalDocs : Documentation { 3806e5dd7070Spatrick let Category = DocOpenCLAddressSpaces; 3807e5dd7070Spatrick let Heading = "__local, local, [[clang::opencl_local]]"; 3808e5dd7070Spatrick let Content = [{ 3809e5dd7070SpatrickThe local address space specifies that an object is allocated in the local (work 3810e5dd7070Spatrickgroup) memory area, which is accessible to all work items in the same work 3811e5dd7070Spatrickgroup. The content stored in this memory region is not accessible after 3812e5dd7070Spatrickthe kernel execution ends. In a kernel function scope, any variable can be in 3813e5dd7070Spatrickthe local address space. In other scopes, only pointer types to the local address 3814e5dd7070Spatrickspace are allowed. Local address space variables cannot have an initializer. 3815e5dd7070Spatrick }]; 3816e5dd7070Spatrick} 3817e5dd7070Spatrick 3818e5dd7070Spatrickdef OpenCLAddressSpacePrivateDocs : Documentation { 3819e5dd7070Spatrick let Category = DocOpenCLAddressSpaces; 3820e5dd7070Spatrick let Heading = "__private, private, [[clang::opencl_private]]"; 3821e5dd7070Spatrick let Content = [{ 3822e5dd7070SpatrickThe private address space specifies that an object is allocated in the private 3823e5dd7070Spatrick(work item) memory. Other work items cannot access the same memory area and its 3824e5dd7070Spatrickcontent is destroyed after work item execution ends. Local variables can be 3825e5dd7070Spatrickdeclared in the private address space. Function arguments are always in the 3826e5dd7070Spatrickprivate address space. Kernel function arguments of a pointer or an array type 3827e5dd7070Spatrickcannot point to the private address space. 3828e5dd7070Spatrick }]; 3829e5dd7070Spatrick} 3830e5dd7070Spatrick 3831e5dd7070Spatrickdef OpenCLNoSVMDocs : Documentation { 3832e5dd7070Spatrick let Category = DocCatVariable; 3833e5dd7070Spatrick let Content = [{ 3834e5dd7070SpatrickOpenCL 2.0 supports the optional ``__attribute__((nosvm))`` qualifier for 3835e5dd7070Spatrickpointer variable. It informs the compiler that the pointer does not refer 3836e5dd7070Spatrickto a shared virtual memory region. See OpenCL v2.0 s6.7.2 for details. 3837e5dd7070Spatrick 3838e5dd7070SpatrickSince it is not widely used and has been removed from OpenCL 2.1, it is ignored 3839e5dd7070Spatrickby Clang. 3840e5dd7070Spatrick }]; 3841e5dd7070Spatrick} 3842e5dd7070Spatrick 3843e5dd7070Spatrickdef Ptr32Docs : Documentation { 3844e5dd7070Spatrick let Category = DocCatType; 3845e5dd7070Spatrick let Content = [{ 3846e5dd7070SpatrickThe ``__ptr32`` qualifier represents a native pointer on a 32-bit system. On a 3847e5dd7070Spatrick64-bit system, a pointer with ``__ptr32`` is extended to a 64-bit pointer. The 3848e5dd7070Spatrick``__sptr`` and ``__uptr`` qualifiers can be used to specify whether the pointer 3849e5dd7070Spatrickis sign extended or zero extended. This qualifier is enabled under 3850e5dd7070Spatrick``-fms-extensions``. 3851e5dd7070Spatrick }]; 3852e5dd7070Spatrick} 3853e5dd7070Spatrick 3854e5dd7070Spatrickdef Ptr64Docs : Documentation { 3855e5dd7070Spatrick let Category = DocCatType; 3856e5dd7070Spatrick let Content = [{ 3857e5dd7070SpatrickThe ``__ptr64`` qualifier represents a native pointer on a 64-bit system. On a 3858e5dd7070Spatrick32-bit system, a ``__ptr64`` pointer is truncated to a 32-bit pointer. This 3859e5dd7070Spatrickqualifier is enabled under ``-fms-extensions``. 3860e5dd7070Spatrick }]; 3861e5dd7070Spatrick} 3862e5dd7070Spatrick 3863e5dd7070Spatrickdef SPtrDocs : Documentation { 3864e5dd7070Spatrick let Category = DocCatType; 3865e5dd7070Spatrick let Content = [{ 3866e5dd7070SpatrickThe ``__sptr`` qualifier specifies that a 32-bit pointer should be sign 3867e5dd7070Spatrickextended when converted to a 64-bit pointer. 3868e5dd7070Spatrick }]; 3869e5dd7070Spatrick} 3870e5dd7070Spatrick 3871e5dd7070Spatrickdef UPtrDocs : Documentation { 3872e5dd7070Spatrick let Category = DocCatType; 3873e5dd7070Spatrick let Content = [{ 3874e5dd7070SpatrickThe ``__uptr`` qualifier specifies that a 32-bit pointer should be zero 3875e5dd7070Spatrickextended when converted to a 64-bit pointer. 3876e5dd7070Spatrick }]; 3877e5dd7070Spatrick} 3878e5dd7070Spatrick 3879e5dd7070Spatrick 3880e5dd7070Spatrickdef NullabilityDocs : DocumentationCategory<"Nullability Attributes"> { 3881e5dd7070Spatrick let Content = [{ 3882a9ac8606SpatrickWhether a particular pointer may be "null" is an important concern when working 3883a9ac8606Spatrickwith pointers in the C family of languages. The various nullability attributes 3884a9ac8606Spatrickindicate whether a particular pointer can be null or not, which makes APIs more 3885a9ac8606Spatrickexpressive and can help static analysis tools identify bugs involving null 3886a9ac8606Spatrickpointers. Clang supports several kinds of nullability attributes: the 3887a9ac8606Spatrick``nonnull`` and ``returns_nonnull`` attributes indicate which function or 3888a9ac8606Spatrickmethod parameters and result types can never be null, while nullability type 3889a9ac8606Spatrickqualifiers indicate which pointer types can be null (``_Nullable``) or cannot 3890a9ac8606Spatrickbe null (``_Nonnull``). 3891e5dd7070Spatrick 3892a9ac8606SpatrickThe nullability (type) qualifiers express whether a value of a given pointer 3893a9ac8606Spatricktype can be null (the ``_Nullable`` qualifier), doesn't have a defined meaning 3894a9ac8606Spatrickfor null (the ``_Nonnull`` qualifier), or for which the purpose of null is 3895a9ac8606Spatrickunclear (the ``_Null_unspecified`` qualifier). Because nullability qualifiers 3896a9ac8606Spatrickare expressed within the type system, they are more general than the 3897a9ac8606Spatrick``nonnull`` and ``returns_nonnull`` attributes, allowing one to express (for 3898a9ac8606Spatrickexample) a nullable pointer to an array of nonnull pointers. Nullability 3899a9ac8606Spatrickqualifiers are written to the right of the pointer to which they apply. For 3900a9ac8606Spatrickexample: 3901e5dd7070Spatrick 3902e5dd7070Spatrick .. code-block:: c 3903e5dd7070Spatrick 3904e5dd7070Spatrick // No meaningful result when 'ptr' is null (here, it happens to be undefined behavior). 3905e5dd7070Spatrick int fetch(int * _Nonnull ptr) { return *ptr; } 3906e5dd7070Spatrick 3907e5dd7070Spatrick // 'ptr' may be null. 3908e5dd7070Spatrick int fetch_or_zero(int * _Nullable ptr) { 3909e5dd7070Spatrick return ptr ? *ptr : 0; 3910e5dd7070Spatrick } 3911e5dd7070Spatrick 3912e5dd7070Spatrick // A nullable pointer to non-null pointers to const characters. 3913e5dd7070Spatrick const char *join_strings(const char * _Nonnull * _Nullable strings, unsigned n); 3914e5dd7070Spatrick 3915a9ac8606SpatrickIn Objective-C, there is an alternate spelling for the nullability qualifiers 3916a9ac8606Spatrickthat can be used in Objective-C methods and properties using context-sensitive, 3917a9ac8606Spatricknon-underscored keywords. For example: 3918e5dd7070Spatrick 3919e5dd7070Spatrick .. code-block:: objective-c 3920e5dd7070Spatrick 3921e5dd7070Spatrick @interface NSView : NSResponder 3922e5dd7070Spatrick - (nullable NSView *)ancestorSharedWithView:(nonnull NSView *)aView; 3923e5dd7070Spatrick @property (assign, nullable) NSView *superview; 3924e5dd7070Spatrick @property (readonly, nonnull) NSArray *subviews; 3925e5dd7070Spatrick @end 3926e5dd7070Spatrick }]; 3927e5dd7070Spatrick} 3928e5dd7070Spatrick 3929e5dd7070Spatrickdef TypeNonNullDocs : Documentation { 3930e5dd7070Spatrick let Category = NullabilityDocs; 3931e5dd7070Spatrick let Content = [{ 3932a9ac8606SpatrickThe ``_Nonnull`` nullability qualifier indicates that null is not a meaningful 3933a9ac8606Spatrickvalue for a value of the ``_Nonnull`` pointer type. For example, given a 3934a9ac8606Spatrickdeclaration such as: 3935e5dd7070Spatrick 3936e5dd7070Spatrick .. code-block:: c 3937e5dd7070Spatrick 3938e5dd7070Spatrick int fetch(int * _Nonnull ptr); 3939e5dd7070Spatrick 3940a9ac8606Spatricka caller of ``fetch`` should not provide a null value, and the compiler will 3941a9ac8606Spatrickproduce a warning if it sees a literal null value passed to ``fetch``. Note 3942a9ac8606Spatrickthat, unlike the declaration attribute ``nonnull``, the presence of 3943a9ac8606Spatrick``_Nonnull`` does not imply that passing null is undefined behavior: ``fetch`` 3944a9ac8606Spatrickis free to consider null undefined behavior or (perhaps for 3945a9ac8606Spatrickbackward-compatibility reasons) defensively handle null. 3946e5dd7070Spatrick }]; 3947e5dd7070Spatrick} 3948e5dd7070Spatrick 3949e5dd7070Spatrickdef TypeNullableDocs : Documentation { 3950e5dd7070Spatrick let Category = NullabilityDocs; 3951e5dd7070Spatrick let Content = [{ 3952a9ac8606SpatrickThe ``_Nullable`` nullability qualifier indicates that a value of the 3953a9ac8606Spatrick``_Nullable`` pointer type can be null. For example, given: 3954e5dd7070Spatrick 3955e5dd7070Spatrick .. code-block:: c 3956e5dd7070Spatrick 3957e5dd7070Spatrick int fetch_or_zero(int * _Nullable ptr); 3958e5dd7070Spatrick 3959e5dd7070Spatricka caller of ``fetch_or_zero`` can provide null. 3960e5dd7070Spatrick }]; 3961e5dd7070Spatrick} 3962e5dd7070Spatrick 3963a9ac8606Spatrickdef TypeNullableResultDocs : Documentation { 3964a9ac8606Spatrick let Category = NullabilityDocs; 3965a9ac8606Spatrick let Content = [{ 3966a9ac8606SpatrickThe ``_Nullable_result`` nullability qualifier means that a value of the 3967a9ac8606Spatrick``_Nullable_result`` pointer can be ``nil``, just like ``_Nullable``. Where this 3968a9ac8606Spatrickattribute differs from ``_Nullable`` is when it's used on a parameter to a 3969a9ac8606Spatrickcompletion handler in a Swift async method. For instance, here: 3970a9ac8606Spatrick 3971a9ac8606Spatrick .. code-block:: objc 3972a9ac8606Spatrick 3973a9ac8606Spatrick -(void)fetchSomeDataWithID:(int)identifier 3974a9ac8606Spatrick completionHandler:(void (^)(Data *_Nullable_result result, NSError *error))completionHandler; 3975a9ac8606Spatrick 3976a9ac8606SpatrickThis method asynchronously calls ``completionHandler`` when the data is 3977a9ac8606Spatrickavailable, or calls it with an error. ``_Nullable_result`` indicates to the 3978a9ac8606SpatrickSwift importer that this is the uncommon case where ``result`` can get ``nil`` 3979*12c85518Sroberteven if no error has occurred, and will therefore import it as a Swift optional 3980a9ac8606Spatricktype. Otherwise, if ``result`` was annotated with ``_Nullable``, the Swift 3981a9ac8606Spatrickimporter will assume that ``result`` will always be non-nil unless an error 3982*12c85518Srobertoccurred. 3983a9ac8606Spatrick}]; 3984a9ac8606Spatrick} 3985a9ac8606Spatrick 3986e5dd7070Spatrickdef TypeNullUnspecifiedDocs : Documentation { 3987e5dd7070Spatrick let Category = NullabilityDocs; 3988e5dd7070Spatrick let Content = [{ 3989a9ac8606SpatrickThe ``_Null_unspecified`` nullability qualifier indicates that neither the 3990a9ac8606Spatrick``_Nonnull`` nor ``_Nullable`` qualifiers make sense for a particular pointer 3991a9ac8606Spatricktype. It is used primarily to indicate that the role of null with specific 3992a9ac8606Spatrickpointers in a nullability-annotated header is unclear, e.g., due to 3993a9ac8606Spatrickoverly-complex implementations or historical factors with a long-lived API. 3994e5dd7070Spatrick }]; 3995e5dd7070Spatrick} 3996e5dd7070Spatrick 3997e5dd7070Spatrickdef NonNullDocs : Documentation { 3998e5dd7070Spatrick let Category = NullabilityDocs; 3999e5dd7070Spatrick let Content = [{ 4000a9ac8606SpatrickThe ``nonnull`` attribute indicates that some function parameters must not be 4001a9ac8606Spatricknull, and can be used in several different ways. It's original usage 4002a9ac8606Spatrick(`from GCC <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes>`_) 4003a9ac8606Spatrickis as a function (or Objective-C method) attribute that specifies which 4004a9ac8606Spatrickparameters of the function are nonnull in a comma-separated list. For example: 4005e5dd7070Spatrick 4006e5dd7070Spatrick .. code-block:: c 4007e5dd7070Spatrick 4008e5dd7070Spatrick extern void * my_memcpy (void *dest, const void *src, size_t len) 4009e5dd7070Spatrick __attribute__((nonnull (1, 2))); 4010e5dd7070Spatrick 4011e5dd7070SpatrickHere, the ``nonnull`` attribute indicates that parameters 1 and 2 4012a9ac8606Spatrickcannot have a null value. Omitting the parenthesized list of parameter indices 4013a9ac8606Spatrickmeans that all parameters of pointer type cannot be null: 4014e5dd7070Spatrick 4015e5dd7070Spatrick .. code-block:: c 4016e5dd7070Spatrick 4017e5dd7070Spatrick extern void * my_memcpy (void *dest, const void *src, size_t len) 4018e5dd7070Spatrick __attribute__((nonnull)); 4019e5dd7070Spatrick 4020a9ac8606SpatrickClang also allows the ``nonnull`` attribute to be placed directly on a function 4021a9ac8606Spatrick(or Objective-C method) parameter, eliminating the need to specify the 4022a9ac8606Spatrickparameter index ahead of type. For example: 4023e5dd7070Spatrick 4024e5dd7070Spatrick .. code-block:: c 4025e5dd7070Spatrick 4026e5dd7070Spatrick extern void * my_memcpy (void *dest __attribute__((nonnull)), 4027e5dd7070Spatrick const void *src __attribute__((nonnull)), size_t len); 4028e5dd7070Spatrick 4029a9ac8606SpatrickNote that the ``nonnull`` attribute indicates that passing null to a non-null 4030a9ac8606Spatrickparameter is undefined behavior, which the optimizer may take advantage of to, 4031a9ac8606Spatricke.g., remove null checks. The ``_Nonnull`` type qualifier indicates that a 4032a9ac8606Spatrickpointer cannot be null in a more general manner (because it is part of the type 4033a9ac8606Spatricksystem) and does not imply undefined behavior, making it more widely applicable. 4034a9ac8606Spatrick }]; 4035a9ac8606Spatrick} 4036a9ac8606Spatrick 4037a9ac8606Spatrickdef RestrictDocs : Documentation { 4038a9ac8606Spatrick let Category = DocCatFunction; 4039a9ac8606Spatrick let Heading = "malloc"; 4040a9ac8606Spatrick let Content = [{ 4041a9ac8606SpatrickThe ``malloc`` attribute indicates that the function acts like a system memory 4042a9ac8606Spatrickallocation function, returning a pointer to allocated storage disjoint from the 4043a9ac8606Spatrickstorage for any other object accessible to the caller. 4044e5dd7070Spatrick }]; 4045e5dd7070Spatrick} 4046e5dd7070Spatrick 4047e5dd7070Spatrickdef ReturnsNonNullDocs : Documentation { 4048e5dd7070Spatrick let Category = NullabilityDocs; 4049e5dd7070Spatrick let Content = [{ 4050a9ac8606SpatrickThe ``returns_nonnull`` attribute indicates that a particular function (or 4051a9ac8606SpatrickObjective-C method) always returns a non-null pointer. For example, a 4052a9ac8606Spatrickparticular system ``malloc`` might be defined to terminate a process when 4053a9ac8606Spatrickmemory is not available rather than returning a null pointer: 4054e5dd7070Spatrick 4055e5dd7070Spatrick .. code-block:: c 4056e5dd7070Spatrick 4057e5dd7070Spatrick extern void * malloc (size_t size) __attribute__((returns_nonnull)); 4058e5dd7070Spatrick 4059a9ac8606SpatrickThe ``returns_nonnull`` attribute implies that returning a null pointer is 4060a9ac8606Spatrickundefined behavior, which the optimizer may take advantage of. The ``_Nonnull`` 4061a9ac8606Spatricktype qualifier indicates that a pointer cannot be null in a more general manner 4062a9ac8606Spatrick(because it is part of the type system) and does not imply undefined behavior, 4063a9ac8606Spatrickmaking it more widely applicable 4064e5dd7070Spatrick}]; 4065e5dd7070Spatrick} 4066e5dd7070Spatrick 4067e5dd7070Spatrickdef NoAliasDocs : Documentation { 4068e5dd7070Spatrick let Category = DocCatFunction; 4069e5dd7070Spatrick let Content = [{ 4070e5dd7070SpatrickThe ``noalias`` attribute indicates that the only memory accesses inside 4071e5dd7070Spatrickfunction are loads and stores from objects pointed to by its pointer-typed 4072e5dd7070Spatrickarguments, with arbitrary offsets. 4073e5dd7070Spatrick }]; 4074e5dd7070Spatrick} 4075e5dd7070Spatrick 4076a9ac8606Spatrickdef NSErrorDomainDocs : Documentation { 4077a9ac8606Spatrick let Category = DocCatDecl; 4078a9ac8606Spatrick let Content = [{ 4079a9ac8606SpatrickIn Cocoa frameworks in Objective-C, one can group related error codes in enums 4080a9ac8606Spatrickand categorize these enums with error domains. 4081a9ac8606Spatrick 4082a9ac8606SpatrickThe ``ns_error_domain`` attribute indicates a global ``NSString`` or 4083a9ac8606Spatrick``CFString`` constant representing the error domain that an error code belongs 4084a9ac8606Spatrickto. For pointer uniqueness and code size this is a constant symbol, not a 4085a9ac8606Spatrickliteral. 4086a9ac8606Spatrick 4087a9ac8606SpatrickThe domain and error code need to be used together. The ``ns_error_domain`` 4088a9ac8606Spatrickattribute links error codes to their domain at the source level. 4089a9ac8606Spatrick 4090a9ac8606SpatrickThis metadata is useful for documentation purposes, for static analysis, and for 4091a9ac8606Spatrickimproving interoperability between Objective-C and Swift. It is not used for 4092a9ac8606Spatrickcode generation in Objective-C. 4093a9ac8606Spatrick 4094a9ac8606SpatrickFor example: 4095a9ac8606Spatrick 4096a9ac8606Spatrick .. code-block:: objc 4097a9ac8606Spatrick 4098a9ac8606Spatrick #define NS_ERROR_ENUM(_type, _name, _domain) \ 4099a9ac8606Spatrick enum _name : _type _name; enum __attribute__((ns_error_domain(_domain))) _name : _type 4100a9ac8606Spatrick 4101a9ac8606Spatrick extern NSString *const MyErrorDomain; 4102a9ac8606Spatrick typedef NS_ERROR_ENUM(unsigned char, MyErrorEnum, MyErrorDomain) { 4103a9ac8606Spatrick MyErrFirst, 4104a9ac8606Spatrick MyErrSecond, 4105a9ac8606Spatrick }; 4106a9ac8606Spatrick }]; 4107a9ac8606Spatrick} 4108a9ac8606Spatrick 4109a9ac8606Spatrickdef SwiftDocs : DocumentationCategory<"Customizing Swift Import"> { 4110a9ac8606Spatrick let Content = [{ 4111a9ac8606SpatrickClang supports additional attributes for customizing how APIs are imported into 4112a9ac8606SpatrickSwift. 4113a9ac8606Spatrick }]; 4114a9ac8606Spatrick} 4115a9ac8606Spatrick 4116a9ac8606Spatrickdef SwiftAsyncNameDocs : Documentation { 4117a9ac8606Spatrick let Category = SwiftDocs; 4118a9ac8606Spatrick let Heading = "swift_async_name"; 4119a9ac8606Spatrick let Content = [{ 4120a9ac8606SpatrickThe ``swift_async_name`` attribute provides the name of the ``async`` overload for 4121a9ac8606Spatrickthe given declaration in Swift. If this attribute is absent, the name is 4122a9ac8606Spatricktransformed according to the algorithm built into the Swift compiler. 4123a9ac8606Spatrick 4124a9ac8606SpatrickThe argument is a string literal that contains the Swift name of the function or 4125a9ac8606Spatrickmethod. The name may be a compound Swift name. The function or method with such 4126a9ac8606Spatrickan attribute must have more than zero parameters, as its last parameter is 4127a9ac8606Spatrickassumed to be a callback that's eliminated in the Swift ``async`` name. 4128a9ac8606Spatrick 4129a9ac8606Spatrick .. code-block:: objc 4130a9ac8606Spatrick 4131a9ac8606Spatrick @interface URL 4132a9ac8606Spatrick + (void) loadContentsFrom:(URL *)url callback:(void (^)(NSData *))data __attribute__((__swift_async_name__("URL.loadContentsFrom(_:)"))) 4133a9ac8606Spatrick @end 4134a9ac8606Spatrick }]; 4135a9ac8606Spatrick} 4136a9ac8606Spatrick 4137a9ac8606Spatrickdef SwiftAttrDocs : Documentation { 4138a9ac8606Spatrick let Category = SwiftDocs; 4139a9ac8606Spatrick let Heading = "swift_attr"; 4140a9ac8606Spatrick let Content = [{ 4141a9ac8606SpatrickThe ``swift_attr`` provides a Swift-specific annotation for the declaration 4142a9ac8606Spatrickto which the attribute appertains to. It can be used on any declaration 4143a9ac8606Spatrickin Clang. This kind of annotation is ignored by Clang as it doesn't have any 4144a9ac8606Spatricksemantic meaning in languages supported by Clang. The Swift compiler can 4145a9ac8606Spatrickinterpret these annotations according to its own rules when importing C or 4146a9ac8606SpatrickObjective-C declarations. 4147a9ac8606Spatrick}]; 4148a9ac8606Spatrick} 4149a9ac8606Spatrick 4150a9ac8606Spatrickdef SwiftBridgeDocs : Documentation { 4151a9ac8606Spatrick let Category = SwiftDocs; 4152a9ac8606Spatrick let Heading = "swift_bridge"; 4153a9ac8606Spatrick let Content = [{ 4154a9ac8606SpatrickThe ``swift_bridge`` attribute indicates that the declaration to which the 4155a9ac8606Spatrickattribute appertains is bridged to the named Swift type. 4156a9ac8606Spatrick 4157a9ac8606Spatrick .. code-block:: objc 4158a9ac8606Spatrick 4159a9ac8606Spatrick __attribute__((__objc_root__)) 4160a9ac8606Spatrick @interface Base 4161a9ac8606Spatrick - (instancetype)init; 4162a9ac8606Spatrick @end 4163a9ac8606Spatrick 4164a9ac8606Spatrick __attribute__((__swift_bridge__("BridgedI"))) 4165a9ac8606Spatrick @interface I : Base 4166a9ac8606Spatrick @end 4167a9ac8606Spatrick 4168a9ac8606SpatrickIn this example, the Objective-C interface ``I`` will be made available to Swift 4169a9ac8606Spatrickwith the name ``BridgedI``. It would be possible for the compiler to refer to 4170a9ac8606Spatrick``I`` still in order to bridge the type back to Objective-C. 4171a9ac8606Spatrick }]; 4172a9ac8606Spatrick} 4173a9ac8606Spatrick 4174a9ac8606Spatrickdef SwiftBridgedTypedefDocs : Documentation { 4175a9ac8606Spatrick let Category = SwiftDocs; 4176a9ac8606Spatrick let Heading = "swift_bridged"; 4177a9ac8606Spatrick let Content = [{ 4178a9ac8606SpatrickThe ``swift_bridged_typedef`` attribute indicates that when the typedef to which 4179a9ac8606Spatrickthe attribute appertains is imported into Swift, it should refer to the bridged 4180a9ac8606SpatrickSwift type (e.g. Swift's ``String``) rather than the Objective-C type as written 4181a9ac8606Spatrick(e.g. ``NSString``). 4182a9ac8606Spatrick 4183a9ac8606Spatrick .. code-block:: objc 4184a9ac8606Spatrick 4185a9ac8606Spatrick @interface NSString; 4186a9ac8606Spatrick typedef NSString *AliasedString __attribute__((__swift_bridged_typedef__)); 4187a9ac8606Spatrick 4188a9ac8606Spatrick extern void acceptsAliasedString(AliasedString _Nonnull parameter); 4189a9ac8606Spatrick 4190a9ac8606SpatrickIn this case, the function ``acceptsAliasedString`` will be imported into Swift 4191a9ac8606Spatrickas a function which accepts a ``String`` type parameter. 4192a9ac8606Spatrick }]; 4193a9ac8606Spatrick} 4194a9ac8606Spatrick 4195a9ac8606Spatrickdef SwiftObjCMembersDocs : Documentation { 4196a9ac8606Spatrick let Category = SwiftDocs; 4197a9ac8606Spatrick let Heading = "swift_objc_members"; 4198a9ac8606Spatrick let Content = [{ 4199a9ac8606SpatrickThis attribute indicates that Swift subclasses and members of Swift extensions 4200a9ac8606Spatrickof this class will be implicitly marked with the ``@objcMembers`` Swift 4201a9ac8606Spatrickattribute, exposing them back to Objective-C. 4202a9ac8606Spatrick }]; 4203a9ac8606Spatrick} 4204a9ac8606Spatrick 4205a9ac8606Spatrickdef SwiftErrorDocs : Documentation { 4206a9ac8606Spatrick let Category = SwiftDocs; 4207a9ac8606Spatrick let Heading = "swift_error"; 4208a9ac8606Spatrick let Content = [{ 4209a9ac8606SpatrickThe ``swift_error`` attribute controls whether a particular function (or 4210a9ac8606SpatrickObjective-C method) is imported into Swift as a throwing function, and if so, 4211a9ac8606Spatrickwhich dynamic convention it uses. 4212a9ac8606Spatrick 4213a9ac8606SpatrickAll of these conventions except ``none`` require the function to have an error 4214a9ac8606Spatrickparameter. Currently, the error parameter is always the last parameter of type 4215a9ac8606Spatrick``NSError**`` or ``CFErrorRef*``. Swift will remove the error parameter from 4216a9ac8606Spatrickthe imported API. When calling the API, Swift will always pass a valid address 4217a9ac8606Spatrickinitialized to a null pointer. 4218a9ac8606Spatrick 4219a9ac8606Spatrick* ``swift_error(none)`` means that the function should not be imported as 4220a9ac8606Spatrick throwing. The error parameter and result type will be imported normally. 4221a9ac8606Spatrick 4222a9ac8606Spatrick* ``swift_error(null_result)`` means that calls to the function should be 4223a9ac8606Spatrick considered to have thrown if they return a null value. The return type must be 4224a9ac8606Spatrick a pointer type, and it will be imported into Swift with a non-optional type. 4225a9ac8606Spatrick This is the default error convention for Objective-C methods that return 4226a9ac8606Spatrick pointers. 4227a9ac8606Spatrick 4228a9ac8606Spatrick* ``swift_error(zero_result)`` means that calls to the function should be 4229a9ac8606Spatrick considered to have thrown if they return a zero result. The return type must be 4230a9ac8606Spatrick an integral type. If the return type would have been imported as ``Bool``, it 4231a9ac8606Spatrick is instead imported as ``Void``. This is the default error convention for 4232a9ac8606Spatrick Objective-C methods that return a type that would be imported as ``Bool``. 4233a9ac8606Spatrick 4234a9ac8606Spatrick* ``swift_error(nonzero_result)`` means that calls to the function should be 4235a9ac8606Spatrick considered to have thrown if they return a non-zero result. The return type must 4236a9ac8606Spatrick be an integral type. If the return type would have been imported as ``Bool``, 4237a9ac8606Spatrick it is instead imported as ``Void``. 4238a9ac8606Spatrick 4239a9ac8606Spatrick* ``swift_error(nonnull_error)`` means that calls to the function should be 4240a9ac8606Spatrick considered to have thrown if they leave a non-null error in the error parameter. 4241a9ac8606Spatrick The return type is left unmodified. 4242a9ac8606Spatrick 4243a9ac8606Spatrick }]; 4244a9ac8606Spatrick} 4245a9ac8606Spatrick 4246a9ac8606Spatrickdef SwiftNameDocs : Documentation { 4247a9ac8606Spatrick let Category = SwiftDocs; 4248a9ac8606Spatrick let Heading = "swift_name"; 4249a9ac8606Spatrick let Content = [{ 4250a9ac8606SpatrickThe ``swift_name`` attribute provides the name of the declaration in Swift. If 4251a9ac8606Spatrickthis attribute is absent, the name is transformed according to the algorithm 4252a9ac8606Spatrickbuilt into the Swift compiler. 4253a9ac8606Spatrick 4254a9ac8606SpatrickThe argument is a string literal that contains the Swift name of the function, 4255a9ac8606Spatrickvariable, or type. When renaming a function, the name may be a compound Swift 4256a9ac8606Spatrickname. For a type, enum constant, property, or variable declaration, the name 4257a9ac8606Spatrickmust be a simple or qualified identifier. 4258a9ac8606Spatrick 4259a9ac8606Spatrick .. code-block:: objc 4260a9ac8606Spatrick 4261a9ac8606Spatrick @interface URL 4262a9ac8606Spatrick - (void) initWithString:(NSString *)s __attribute__((__swift_name__("URL.init(_:)"))) 4263a9ac8606Spatrick @end 4264a9ac8606Spatrick 4265a9ac8606Spatrick void __attribute__((__swift_name__("squareRoot()"))) sqrt(double v) { 4266a9ac8606Spatrick } 4267a9ac8606Spatrick }]; 4268a9ac8606Spatrick} 4269a9ac8606Spatrick 4270a9ac8606Spatrickdef SwiftNewTypeDocs : Documentation { 4271a9ac8606Spatrick let Category = SwiftDocs; 4272a9ac8606Spatrick let Heading = "swift_newtype"; 4273a9ac8606Spatrick let Content = [{ 4274a9ac8606SpatrickThe ``swift_newtype`` attribute indicates that the typedef to which the 4275a9ac8606Spatrickattribute appertains is imported as a new Swift type of the typedef's name. 4276a9ac8606SpatrickPreviously, the attribute was spelt ``swift_wrapper``. While the behaviour of 4277a9ac8606Spatrickthe attribute is identical with either spelling, ``swift_wrapper`` is 4278a9ac8606Spatrickdeprecated, only exists for compatibility purposes, and should not be used in 4279a9ac8606Spatricknew code. 4280a9ac8606Spatrick 4281a9ac8606Spatrick* ``swift_newtype(struct)`` means that a Swift struct will be created for this 4282a9ac8606Spatrick typedef. 4283a9ac8606Spatrick 4284a9ac8606Spatrick* ``swift_newtype(enum)`` means that a Swift enum will be created for this 4285a9ac8606Spatrick typedef. 4286a9ac8606Spatrick 4287a9ac8606Spatrick .. code-block:: c 4288a9ac8606Spatrick 4289a9ac8606Spatrick // Import UIFontTextStyle as an enum type, with enumerated values being 4290a9ac8606Spatrick // constants. 4291a9ac8606Spatrick typedef NSString * UIFontTextStyle __attribute__((__swift_newtype__(enum))); 4292a9ac8606Spatrick 4293a9ac8606Spatrick // Import UIFontDescriptorFeatureKey as a structure type, with enumerated 4294a9ac8606Spatrick // values being members of the type structure. 4295a9ac8606Spatrick typedef NSString * UIFontDescriptorFeatureKey __attribute__((__swift_newtype__(struct))); 4296a9ac8606Spatrick 4297a9ac8606Spatrick }]; 4298a9ac8606Spatrick} 4299a9ac8606Spatrick 4300a9ac8606Spatrickdef SwiftPrivateDocs : Documentation { 4301a9ac8606Spatrick let Category = SwiftDocs; 4302a9ac8606Spatrick let Heading = "swift_private"; 4303a9ac8606Spatrick let Content = [{ 4304a9ac8606SpatrickDeclarations marked with the ``swift_private`` attribute are hidden from the 4305a9ac8606Spatrickframework client but are still made available for use within the framework or 4306a9ac8606SpatrickSwift SDK overlay. 4307a9ac8606Spatrick 4308a9ac8606SpatrickThe purpose of this attribute is to permit a more idomatic implementation of 4309a9ac8606Spatrickdeclarations in Swift while hiding the non-idiomatic one. 4310a9ac8606Spatrick }]; 4311a9ac8606Spatrick} 4312a9ac8606Spatrick 4313e5dd7070Spatrickdef OMPDeclareSimdDocs : Documentation { 4314e5dd7070Spatrick let Category = DocCatFunction; 4315e5dd7070Spatrick let Heading = "#pragma omp declare simd"; 4316e5dd7070Spatrick let Content = [{ 4317ec727ea7SpatrickThe ``declare simd`` construct can be applied to a function to enable the creation 4318e5dd7070Spatrickof one or more versions that can process multiple arguments using SIMD 4319ec727ea7Spatrickinstructions from a single invocation in a SIMD loop. The ``declare simd`` 4320ec727ea7Spatrickdirective is a declarative directive. There may be multiple ``declare simd`` 4321ec727ea7Spatrickdirectives for a function. The use of a ``declare simd`` construct on a function 4322e5dd7070Spatrickenables the creation of SIMD versions of the associated function that can be 4323e5dd7070Spatrickused to process multiple arguments from a single invocation from a SIMD loop 4324e5dd7070Spatrickconcurrently. 4325ec727ea7SpatrickThe syntax of the ``declare simd`` construct is as follows: 4326e5dd7070Spatrick 4327e5dd7070Spatrick .. code-block:: none 4328e5dd7070Spatrick 4329e5dd7070Spatrick #pragma omp declare simd [clause[[,] clause] ...] new-line 4330e5dd7070Spatrick [#pragma omp declare simd [clause[[,] clause] ...] new-line] 4331e5dd7070Spatrick [...] 4332e5dd7070Spatrick function definition or declaration 4333e5dd7070Spatrick 4334e5dd7070Spatrickwhere clause is one of the following: 4335e5dd7070Spatrick 4336e5dd7070Spatrick .. code-block:: none 4337e5dd7070Spatrick 4338e5dd7070Spatrick simdlen(length) 4339e5dd7070Spatrick linear(argument-list[:constant-linear-step]) 4340e5dd7070Spatrick aligned(argument-list[:alignment]) 4341e5dd7070Spatrick uniform(argument-list) 4342e5dd7070Spatrick inbranch 4343e5dd7070Spatrick notinbranch 4344e5dd7070Spatrick 4345e5dd7070Spatrick }]; 4346e5dd7070Spatrick} 4347e5dd7070Spatrick 4348e5dd7070Spatrickdef OMPDeclareTargetDocs : Documentation { 4349e5dd7070Spatrick let Category = DocCatFunction; 4350e5dd7070Spatrick let Heading = "#pragma omp declare target"; 4351e5dd7070Spatrick let Content = [{ 4352ec727ea7SpatrickThe ``declare target`` directive specifies that variables and functions are mapped 4353e5dd7070Spatrickto a device for OpenMP offload mechanism. 4354e5dd7070Spatrick 4355e5dd7070SpatrickThe syntax of the declare target directive is as follows: 4356e5dd7070Spatrick 4357e5dd7070Spatrick .. code-block:: c 4358e5dd7070Spatrick 4359e5dd7070Spatrick #pragma omp declare target new-line 4360e5dd7070Spatrick declarations-definition-seq 4361e5dd7070Spatrick #pragma omp end declare target new-line 4362e5dd7070Spatrick 4363e5dd7070Spatrickor 4364e5dd7070Spatrick 4365e5dd7070Spatrick .. code-block:: c 4366e5dd7070Spatrick 4367e5dd7070Spatrick #pragma omp declare target (extended-list) new-line 4368e5dd7070Spatrick 4369e5dd7070Spatrickor 4370e5dd7070Spatrick 4371e5dd7070Spatrick .. code-block:: c 4372e5dd7070Spatrick 4373e5dd7070Spatrick #pragma omp declare target clause[ [,] clause ... ] new-line 4374e5dd7070Spatrick 4375e5dd7070Spatrickwhere clause is one of the following: 4376e5dd7070Spatrick 4377e5dd7070Spatrick 4378e5dd7070Spatrick .. code-block:: c 4379e5dd7070Spatrick 4380e5dd7070Spatrick to(extended-list) 4381e5dd7070Spatrick link(list) 4382e5dd7070Spatrick device_type(host | nohost | any) 4383e5dd7070Spatrick }]; 4384e5dd7070Spatrick} 4385e5dd7070Spatrick 4386e5dd7070Spatrickdef OMPDeclareVariantDocs : Documentation { 4387e5dd7070Spatrick let Category = DocCatFunction; 4388e5dd7070Spatrick let Heading = "#pragma omp declare variant"; 4389e5dd7070Spatrick let Content = [{ 4390ec727ea7SpatrickThe ``declare variant`` directive declares a specialized variant of a base 4391e5dd7070Spatrickfunction and specifies the context in which that specialized variant is used. 4392e5dd7070SpatrickThe declare variant directive is a declarative directive. 4393ec727ea7SpatrickThe syntax of the ``declare variant`` construct is as follows: 4394e5dd7070Spatrick 4395e5dd7070Spatrick .. code-block:: none 4396e5dd7070Spatrick 4397e5dd7070Spatrick #pragma omp declare variant(variant-func-id) clause new-line 4398e5dd7070Spatrick [#pragma omp declare variant(variant-func-id) clause new-line] 4399e5dd7070Spatrick [...] 4400e5dd7070Spatrick function definition or declaration 4401e5dd7070Spatrick 4402e5dd7070Spatrickwhere clause is one of the following: 4403e5dd7070Spatrick 4404e5dd7070Spatrick .. code-block:: none 4405e5dd7070Spatrick 4406e5dd7070Spatrick match(context-selector-specification) 4407e5dd7070Spatrick 4408ec727ea7Spatrickand where ``variant-func-id`` is the name of a function variant that is either a 4409e5dd7070Spatrickbase language identifier or, for C++, a template-id. 4410e5dd7070Spatrick 4411ec727ea7SpatrickClang provides the following context selector extensions, used via 4412ec727ea7Spatrick``implementation={extension(EXTENSION)}``: 4413ec727ea7Spatrick 4414ec727ea7Spatrick .. code-block:: none 4415ec727ea7Spatrick 4416ec727ea7Spatrick match_all 4417ec727ea7Spatrick match_any 4418ec727ea7Spatrick match_none 4419a9ac8606Spatrick disable_implicit_base 4420a9ac8606Spatrick allow_templates 4421*12c85518Srobert bind_to_declaration 4422ec727ea7Spatrick 4423ec727ea7SpatrickThe match extensions change when the *entire* context selector is considered a 4424ec727ea7Spatrickmatch for an OpenMP context. The default is ``all``, with ``none`` no trait in the 4425ec727ea7Spatrickselector is allowed to be in the OpenMP context, with ``any`` a single trait in 4426ec727ea7Spatrickboth the selector and OpenMP context is sufficient. Only a single match 4427ec727ea7Spatrickextension trait is allowed per context selector. 4428a9ac8606SpatrickThe disable extensions remove default effects of the ``begin declare variant`` 4429a9ac8606Spatrickapplied to a definition. If ``disable_implicit_base`` is given, we will not 4430a9ac8606Spatrickintroduce an implicit base function for a variant if no base function was 4431a9ac8606Spatrickfound. The variant is still generated but will never be called, due to the 4432a9ac8606Spatrickabsence of a base function and consequently calls to a base function. 4433a9ac8606SpatrickThe allow extensions change when the ``begin declare variant`` effect is 4434a9ac8606Spatrickapplied to a definition. If ``allow_templates`` is given, template function 4435a9ac8606Spatrickdefinitions are considered as specializations of existing or assumed template 4436a9ac8606Spatrickdeclarations with the same name. The template parameters for the base functions 4437*12c85518Srobertare used to instantiate the specialization. If ``bind_to_declaration`` is given, 4438*12c85518Srobertapply the same variant rules to function declarations. This allows the user to 4439*12c85518Srobertoverride declarations with only a function declaration. 4440a9ac8606Spatrick }]; 4441a9ac8606Spatrick} 4442a9ac8606Spatrick 4443a9ac8606Spatrickdef LeafDocs : Documentation { 4444a9ac8606Spatrick let Category = DocCatVariable; 4445a9ac8606Spatrick let Content = [{ 4446a9ac8606Spatrick 4447a9ac8606SpatrickThe ``leaf`` attribute is used as a compiler hint to improve dataflow analysis 4448a9ac8606Spatrickin library functions. Functions marked with the ``leaf`` attribute are not allowed 4449a9ac8606Spatrickto jump back into the caller's translation unit, whether through invoking a 4450a9ac8606Spatrickcallback function, an external function call, use of ``longjmp``, or other means. 4451a9ac8606SpatrickTherefore, they cannot use or modify any data that does not escape the caller function's 4452a9ac8606Spatrickcompilation unit. 4453a9ac8606Spatrick 4454a9ac8606SpatrickFor more information see 4455a9ac8606Spatrick`gcc documentation <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html>` 4456a9ac8606Spatrick}]; 4457a9ac8606Spatrick} 4458a9ac8606Spatrick 4459a9ac8606Spatrickdef AssumptionDocs : Documentation { 4460a9ac8606Spatrick let Category = DocCatFunction; 4461a9ac8606Spatrick let Heading = "assume"; 4462a9ac8606Spatrick let Content = [{ 4463a9ac8606SpatrickClang supports the ``__attribute__((assume("assumption")))`` attribute to 4464a9ac8606Spatrickprovide additional information to the optimizer. The string-literal, here 4465a9ac8606Spatrick"assumption", will be attached to the function declaration such that later 4466a9ac8606Spatrickanalysis and optimization passes can assume the "assumption" to hold. 4467a9ac8606SpatrickThis is similar to :ref:`__builtin_assume <langext-__builtin_assume>` but 4468a9ac8606Spatrickinstead of an expression that can be assumed to be non-zero, the assumption is 4469a9ac8606Spatrickexpressed as a string and it holds for the entire function. 4470a9ac8606Spatrick 4471a9ac8606SpatrickA function can have multiple assume attributes and they propagate from prior 4472a9ac8606Spatrickdeclarations to later definitions. Multiple assumptions are aggregated into a 4473a9ac8606Spatricksingle comma separated string. Thus, one can provide multiple assumptions via 4474a9ac8606Spatricka comma separated string, i.a., 4475a9ac8606Spatrick``__attribute__((assume("assumption1,assumption2")))``. 4476a9ac8606Spatrick 4477a9ac8606SpatrickWhile LLVM plugins might provide more assumption strings, the default LLVM 4478a9ac8606Spatrickoptimization passes are aware of the following assumptions: 4479a9ac8606Spatrick 4480a9ac8606Spatrick .. code-block:: none 4481a9ac8606Spatrick 4482a9ac8606Spatrick "omp_no_openmp" 4483a9ac8606Spatrick "omp_no_openmp_routines" 4484a9ac8606Spatrick "omp_no_parallelism" 4485a9ac8606Spatrick 4486a9ac8606SpatrickThe OpenMP standard defines the meaning of OpenMP assumptions ("omp_XYZ" is 4487a9ac8606Spatrickspelled "XYZ" in the `OpenMP 5.1 Standard`_). 4488a9ac8606Spatrick 4489a9ac8606Spatrick.. _`OpenMP 5.1 Standard`: https://www.openmp.org/spec-html/5.1/openmpsu37.html#x56-560002.5.2 4490ec727ea7Spatrick 4491e5dd7070Spatrick}]; 4492e5dd7070Spatrick} 4493e5dd7070Spatrick 4494e5dd7070Spatrickdef NoStackProtectorDocs : Documentation { 4495e5dd7070Spatrick let Category = DocCatFunction; 4496*12c85518Srobert let Heading = "no_stack_protector, safebuffers"; 4497e5dd7070Spatrick let Content = [{ 4498*12c85518SrobertClang supports the GNU style ``__attribute__((no_stack_protector))`` and Microsoft 4499*12c85518Srobertstyle ``__declspec(safebuffers)`` attribute which disables 4500e5dd7070Spatrickthe stack protector on the specified function. This attribute is useful for 4501e5dd7070Spatrickselectively disabling the stack protector on some functions when building with 4502e5dd7070Spatrick``-fstack-protector`` compiler option. 4503e5dd7070Spatrick 4504e5dd7070SpatrickFor example, it disables the stack protector for the function ``foo`` but function 4505e5dd7070Spatrick``bar`` will still be built with the stack protector with the ``-fstack-protector`` 4506e5dd7070Spatrickoption. 4507e5dd7070Spatrick 4508e5dd7070Spatrick.. code-block:: c 4509e5dd7070Spatrick 4510e5dd7070Spatrick int __attribute__((no_stack_protector)) 4511e5dd7070Spatrick foo (int x); // stack protection will be disabled for foo. 4512e5dd7070Spatrick 4513e5dd7070Spatrick int bar(int y); // bar can be built with the stack protector. 4514e5dd7070Spatrick 4515e5dd7070Spatrick }]; 4516e5dd7070Spatrick} 4517e5dd7070Spatrick 4518*12c85518Srobertdef StrictGuardStackCheckDocs : Documentation { 4519*12c85518Srobert let Category = DocCatFunction; 4520*12c85518Srobert let Content = [{ 4521*12c85518SrobertClang supports the Microsoft style ``__declspec((strict_gs_check))`` attribute 4522*12c85518Srobertwhich upgrades the stack protector check from ``-fstack-protector`` to 4523*12c85518Srobert``-fstack-protector-strong``. 4524*12c85518Srobert 4525*12c85518SrobertFor example, it upgrades the stack protector for the function ``foo`` to 4526*12c85518Srobert``-fstack-protector-strong`` but function ``bar`` will still be built with the 4527*12c85518Srobertstack protector with the ``-fstack-protector`` option. 4528*12c85518Srobert 4529*12c85518Srobert.. code-block:: c 4530*12c85518Srobert 4531*12c85518Srobert __declspec((strict_gs_check)) 4532*12c85518Srobert int foo(int x); // stack protection will be upgraded for foo. 4533*12c85518Srobert 4534*12c85518Srobert int bar(int y); // bar can be built with the standard stack protector checks. 4535*12c85518Srobert 4536*12c85518Srobert }]; 4537*12c85518Srobert} 4538*12c85518Srobert 4539e5dd7070Spatrickdef NotTailCalledDocs : Documentation { 4540e5dd7070Spatrick let Category = DocCatFunction; 4541e5dd7070Spatrick let Content = [{ 4542a9ac8606SpatrickThe ``not_tail_called`` attribute prevents tail-call optimization on statically 4543a9ac8606Spatrickbound calls. Objective-c methods, and functions marked as ``always_inline`` 4544a9ac8606Spatrickcannot be marked as ``not_tail_called``. 4545e5dd7070Spatrick 4546e5dd7070SpatrickFor example, it prevents tail-call optimization in the following case: 4547e5dd7070Spatrick 4548e5dd7070Spatrick .. code-block:: c 4549e5dd7070Spatrick 4550e5dd7070Spatrick int __attribute__((not_tail_called)) foo1(int); 4551e5dd7070Spatrick 4552e5dd7070Spatrick int foo2(int a) { 4553e5dd7070Spatrick return foo1(a); // No tail-call optimization on direct calls. 4554e5dd7070Spatrick } 4555e5dd7070Spatrick 4556e5dd7070SpatrickHowever, it doesn't prevent tail-call optimization in this case: 4557e5dd7070Spatrick 4558e5dd7070Spatrick .. code-block:: c 4559e5dd7070Spatrick 4560e5dd7070Spatrick int __attribute__((not_tail_called)) foo1(int); 4561e5dd7070Spatrick 4562e5dd7070Spatrick int foo2(int a) { 4563e5dd7070Spatrick int (*fn)(int) = &foo1; 4564e5dd7070Spatrick 4565a9ac8606Spatrick // not_tail_called has no effect on an indirect call even if the call can 4566a9ac8606Spatrick // be resolved at compile time. 4567e5dd7070Spatrick return (*fn)(a); 4568e5dd7070Spatrick } 4569e5dd7070Spatrick 4570a9ac8606SpatrickGenerally, marking an overriding virtual function as ``not_tail_called`` is 4571a9ac8606Spatricknot useful, because this attribute is a property of the static type. Calls 4572a9ac8606Spatrickmade through a pointer or reference to the base class type will respect 4573a9ac8606Spatrickthe ``not_tail_called`` attribute of the base class's member function, 4574a9ac8606Spatrickregardless of the runtime destination of the call: 4575e5dd7070Spatrick 4576e5dd7070Spatrick .. code-block:: c++ 4577e5dd7070Spatrick 4578a9ac8606Spatrick struct Foo { virtual void f(); }; 4579a9ac8606Spatrick struct Bar : Foo { 4580a9ac8606Spatrick [[clang::not_tail_called]] void f() override; 4581e5dd7070Spatrick }; 4582a9ac8606Spatrick void callera(Bar& bar) { 4583a9ac8606Spatrick Foo& foo = bar; 4584a9ac8606Spatrick // not_tail_called has no effect on here, even though the 4585a9ac8606Spatrick // underlying method is f from Bar. 4586a9ac8606Spatrick foo.f(); 4587a9ac8606Spatrick bar.f(); // No tail-call optimization on here. 4588a9ac8606Spatrick } 4589e5dd7070Spatrick }]; 4590e5dd7070Spatrick} 4591e5dd7070Spatrick 4592e5dd7070Spatrickdef NoThrowDocs : Documentation { 4593e5dd7070Spatrick let Category = DocCatFunction; 4594e5dd7070Spatrick let Content = [{ 4595e5dd7070SpatrickClang supports the GNU style ``__attribute__((nothrow))`` and Microsoft style 4596ec727ea7Spatrick``__declspec(nothrow)`` attribute as an equivalent of ``noexcept`` on function 4597e5dd7070Spatrickdeclarations. This attribute informs the compiler that the annotated function 4598e5dd7070Spatrickdoes not throw an exception. This prevents exception-unwinding. This attribute 4599e5dd7070Spatrickis particularly useful on functions in the C Standard Library that are 4600e5dd7070Spatrickguaranteed to not throw an exception. 4601e5dd7070Spatrick }]; 4602e5dd7070Spatrick} 4603e5dd7070Spatrick 4604*12c85518Srobertdef NoUwtableDocs : Documentation { 4605*12c85518Srobert let Category = DocCatFunction; 4606*12c85518Srobert let Content = [{ 4607*12c85518SrobertClang supports the ``nouwtable`` attribute which skips emitting 4608*12c85518Srobertthe unwind table entry for the specified function. This attribute is useful for 4609*12c85518Srobertselectively emitting the unwind table entry on some functions when building with 4610*12c85518Srobert``-funwind-tables`` compiler option. 4611*12c85518Srobert }]; 4612*12c85518Srobert} 4613*12c85518Srobert 4614e5dd7070Spatrickdef InternalLinkageDocs : Documentation { 4615e5dd7070Spatrick let Category = DocCatFunction; 4616e5dd7070Spatrick let Content = [{ 4617a9ac8606SpatrickThe ``internal_linkage`` attribute changes the linkage type of the declaration 4618a9ac8606Spatrickto internal. This is similar to C-style ``static``, but can be used on classes 4619a9ac8606Spatrickand class methods. When applied to a class definition, this attribute affects 4620a9ac8606Spatrickall methods and static data members of that class. This can be used to contain 4621a9ac8606Spatrickthe ABI of a C++ library by excluding unwanted class methods from the export 4622a9ac8606Spatricktables. 4623e5dd7070Spatrick }]; 4624e5dd7070Spatrick} 4625e5dd7070Spatrick 4626e5dd7070Spatrickdef ExcludeFromExplicitInstantiationDocs : Documentation { 4627e5dd7070Spatrick let Category = DocCatFunction; 4628e5dd7070Spatrick let Content = [{ 4629e5dd7070SpatrickThe ``exclude_from_explicit_instantiation`` attribute opts-out a member of a 4630e5dd7070Spatrickclass template from being part of explicit template instantiations of that 4631e5dd7070Spatrickclass template. This means that an explicit instantiation will not instantiate 4632e5dd7070Spatrickmembers of the class template marked with the attribute, but also that code 4633e5dd7070Spatrickwhere an extern template declaration of the enclosing class template is visible 4634e5dd7070Spatrickwill not take for granted that an external instantiation of the class template 4635e5dd7070Spatrickwould provide those members (which would otherwise be a link error, since the 4636e5dd7070Spatrickexplicit instantiation won't provide those members). For example, let's say we 4637e5dd7070Spatrickdon't want the ``data()`` method to be part of libc++'s ABI. To make sure it 4638e5dd7070Spatrickis not exported from the dylib, we give it hidden visibility: 4639e5dd7070Spatrick 4640e5dd7070Spatrick .. code-block:: c++ 4641e5dd7070Spatrick 4642e5dd7070Spatrick // in <string> 4643e5dd7070Spatrick template <class CharT> 4644e5dd7070Spatrick class basic_string { 4645e5dd7070Spatrick public: 4646e5dd7070Spatrick __attribute__((__visibility__("hidden"))) 4647e5dd7070Spatrick const value_type* data() const noexcept { ... } 4648e5dd7070Spatrick }; 4649e5dd7070Spatrick 4650e5dd7070Spatrick template class basic_string<char>; 4651e5dd7070Spatrick 4652e5dd7070SpatrickSince an explicit template instantiation declaration for ``basic_string<char>`` 4653e5dd7070Spatrickis provided, the compiler is free to assume that ``basic_string<char>::data()`` 4654e5dd7070Spatrickwill be provided by another translation unit, and it is free to produce an 4655e5dd7070Spatrickexternal call to this function. However, since ``data()`` has hidden visibility 4656e5dd7070Spatrickand the explicit template instantiation is provided in a shared library (as 4657e5dd7070Spatrickopposed to simply another translation unit), ``basic_string<char>::data()`` 4658e5dd7070Spatrickwon't be found and a link error will ensue. This happens because the compiler 4659e5dd7070Spatrickassumes that ``basic_string<char>::data()`` is part of the explicit template 4660e5dd7070Spatrickinstantiation declaration, when it really isn't. To tell the compiler that 4661e5dd7070Spatrick``data()`` is not part of the explicit template instantiation declaration, the 4662e5dd7070Spatrick``exclude_from_explicit_instantiation`` attribute can be used: 4663e5dd7070Spatrick 4664e5dd7070Spatrick .. code-block:: c++ 4665e5dd7070Spatrick 4666e5dd7070Spatrick // in <string> 4667e5dd7070Spatrick template <class CharT> 4668e5dd7070Spatrick class basic_string { 4669e5dd7070Spatrick public: 4670e5dd7070Spatrick __attribute__((__visibility__("hidden"))) 4671e5dd7070Spatrick __attribute__((exclude_from_explicit_instantiation)) 4672e5dd7070Spatrick const value_type* data() const noexcept { ... } 4673e5dd7070Spatrick }; 4674e5dd7070Spatrick 4675e5dd7070Spatrick template class basic_string<char>; 4676e5dd7070Spatrick 4677e5dd7070SpatrickNow, the compiler won't assume that ``basic_string<char>::data()`` is provided 4678e5dd7070Spatrickexternally despite there being an explicit template instantiation declaration: 4679e5dd7070Spatrickthe compiler will implicitly instantiate ``basic_string<char>::data()`` in the 4680e5dd7070SpatrickTUs where it is used. 4681e5dd7070Spatrick 4682e5dd7070SpatrickThis attribute can be used on static and non-static member functions of class 4683e5dd7070Spatricktemplates, static data members of class templates and member classes of class 4684e5dd7070Spatricktemplates. 4685e5dd7070Spatrick }]; 4686e5dd7070Spatrick} 4687e5dd7070Spatrick 4688e5dd7070Spatrickdef DisableTailCallsDocs : Documentation { 4689e5dd7070Spatrick let Category = DocCatFunction; 4690e5dd7070Spatrick let Content = [{ 4691a9ac8606SpatrickThe ``disable_tail_calls`` attribute instructs the backend to not perform tail 4692a9ac8606Spatrickcall optimization inside the marked function. 4693e5dd7070Spatrick 4694e5dd7070SpatrickFor example: 4695e5dd7070Spatrick 4696e5dd7070Spatrick .. code-block:: c 4697e5dd7070Spatrick 4698e5dd7070Spatrick int callee(int); 4699e5dd7070Spatrick 4700e5dd7070Spatrick int foo(int a) __attribute__((disable_tail_calls)) { 4701e5dd7070Spatrick return callee(a); // This call is not tail-call optimized. 4702e5dd7070Spatrick } 4703e5dd7070Spatrick 4704e5dd7070SpatrickMarking virtual functions as ``disable_tail_calls`` is legal. 4705e5dd7070Spatrick 4706e5dd7070Spatrick .. code-block:: c++ 4707e5dd7070Spatrick 4708e5dd7070Spatrick int callee(int); 4709e5dd7070Spatrick 4710e5dd7070Spatrick class Base { 4711e5dd7070Spatrick public: 4712e5dd7070Spatrick [[clang::disable_tail_calls]] virtual int foo1() { 4713e5dd7070Spatrick return callee(); // This call is not tail-call optimized. 4714e5dd7070Spatrick } 4715e5dd7070Spatrick }; 4716e5dd7070Spatrick 4717e5dd7070Spatrick class Derived1 : public Base { 4718e5dd7070Spatrick public: 4719e5dd7070Spatrick int foo1() override { 4720e5dd7070Spatrick return callee(); // This call is tail-call optimized. 4721e5dd7070Spatrick } 4722e5dd7070Spatrick }; 4723e5dd7070Spatrick 4724e5dd7070Spatrick }]; 4725e5dd7070Spatrick} 4726e5dd7070Spatrick 4727e5dd7070Spatrickdef AnyX86NoCallerSavedRegistersDocs : Documentation { 4728e5dd7070Spatrick let Category = DocCatFunction; 4729e5dd7070Spatrick let Content = [{ 4730e5dd7070SpatrickUse this attribute to indicate that the specified function has no 4731e5dd7070Spatrickcaller-saved registers. That is, all registers are callee-saved except for 4732e5dd7070Spatrickregisters used for passing parameters to the function or returning parameters 4733e5dd7070Spatrickfrom the function. 4734e5dd7070SpatrickThe compiler saves and restores any modified registers that were not used for 4735e5dd7070Spatrickpassing or returning arguments to the function. 4736e5dd7070Spatrick 4737e5dd7070SpatrickThe user can call functions specified with the 'no_caller_saved_registers' 4738e5dd7070Spatrickattribute from an interrupt handler without saving and restoring all 4739e5dd7070Spatrickcall-clobbered registers. 4740e5dd7070Spatrick 4741e5dd7070SpatrickNote that 'no_caller_saved_registers' attribute is not a calling convention. 4742e5dd7070SpatrickIn fact, it only overrides the decision of which registers should be saved by 4743e5dd7070Spatrickthe caller, but not how the parameters are passed from the caller to the callee. 4744e5dd7070Spatrick 4745e5dd7070SpatrickFor example: 4746e5dd7070Spatrick 4747e5dd7070Spatrick .. code-block:: c 4748e5dd7070Spatrick 4749e5dd7070Spatrick __attribute__ ((no_caller_saved_registers, fastcall)) 4750e5dd7070Spatrick void f (int arg1, int arg2) { 4751e5dd7070Spatrick ... 4752e5dd7070Spatrick } 4753e5dd7070Spatrick 4754e5dd7070Spatrick In this case parameters 'arg1' and 'arg2' will be passed in registers. 4755e5dd7070Spatrick In this case, on 32-bit x86 targets, the function 'f' will use ECX and EDX as 4756e5dd7070Spatrick register parameters. However, it will not assume any scratch registers and 4757e5dd7070Spatrick should save and restore any modified registers except for ECX and EDX. 4758e5dd7070Spatrick }]; 4759e5dd7070Spatrick} 4760e5dd7070Spatrick 4761e5dd7070Spatrickdef X86ForceAlignArgPointerDocs : Documentation { 4762e5dd7070Spatrick let Category = DocCatFunction; 4763e5dd7070Spatrick let Content = [{ 4764e5dd7070SpatrickUse this attribute to force stack alignment. 4765e5dd7070Spatrick 4766e5dd7070SpatrickLegacy x86 code uses 4-byte stack alignment. Newer aligned SSE instructions 4767e5dd7070Spatrick(like 'movaps') that work with the stack require operands to be 16-byte aligned. 4768e5dd7070SpatrickThis attribute realigns the stack in the function prologue to make sure the 4769e5dd7070Spatrickstack can be used with SSE instructions. 4770e5dd7070Spatrick 4771e5dd7070SpatrickNote that the x86_64 ABI forces 16-byte stack alignment at the call site. 4772e5dd7070SpatrickBecause of this, 'force_align_arg_pointer' is not needed on x86_64, except in 4773e5dd7070Spatrickrare cases where the caller does not align the stack properly (e.g. flow 4774e5dd7070Spatrickjumps from i386 arch code). 4775e5dd7070Spatrick 4776e5dd7070Spatrick .. code-block:: c 4777e5dd7070Spatrick 4778e5dd7070Spatrick __attribute__ ((force_align_arg_pointer)) 4779e5dd7070Spatrick void f () { 4780e5dd7070Spatrick ... 4781e5dd7070Spatrick } 4782e5dd7070Spatrick 4783e5dd7070Spatrick }]; 4784e5dd7070Spatrick} 4785e5dd7070Spatrick 4786e5dd7070Spatrickdef AnyX86NoCfCheckDocs : Documentation { 4787e5dd7070Spatrick let Category = DocCatFunction; 4788e5dd7070Spatrick let Content = [{ 4789e5dd7070SpatrickJump Oriented Programming attacks rely on tampering with addresses used by 4790e5dd7070Spatrickindirect call / jmp, e.g. redirect control-flow to non-programmer 4791e5dd7070Spatrickintended bytes in the binary. 4792e5dd7070SpatrickX86 Supports Indirect Branch Tracking (IBT) as part of Control-Flow 4793e5dd7070SpatrickEnforcement Technology (CET). IBT instruments ENDBR instructions used to 4794e5dd7070Spatrickspecify valid targets of indirect call / jmp. 4795e5dd7070SpatrickThe ``nocf_check`` attribute has two roles: 4796e5dd7070Spatrick1. Appertains to a function - do not add ENDBR instruction at the beginning of 4797e5dd7070Spatrickthe function. 4798e5dd7070Spatrick2. Appertains to a function pointer - do not track the target function of this 4799e5dd7070Spatrickpointer (by adding nocf_check prefix to the indirect-call instruction). 4800e5dd7070Spatrick}]; 4801e5dd7070Spatrick} 4802e5dd7070Spatrick 4803e5dd7070Spatrickdef SwiftCallDocs : Documentation { 4804e5dd7070Spatrick let Category = DocCatVariable; 4805e5dd7070Spatrick let Content = [{ 4806e5dd7070SpatrickThe ``swiftcall`` attribute indicates that a function should be called 4807e5dd7070Spatrickusing the Swift calling convention for a function or function pointer. 4808e5dd7070Spatrick 4809e5dd7070SpatrickThe lowering for the Swift calling convention, as described by the Swift 4810e5dd7070SpatrickABI documentation, occurs in multiple phases. The first, "high-level" 4811e5dd7070Spatrickphase breaks down the formal parameters and results into innately direct 4812ec727ea7Spatrickand indirect components, adds implicit parameters for the generic 4813e5dd7070Spatricksignature, and assigns the context and error ABI treatments to parameters 4814e5dd7070Spatrickwhere applicable. The second phase breaks down the direct parameters 4815e5dd7070Spatrickand results from the first phase and assigns them to registers or the 4816e5dd7070Spatrickstack. The ``swiftcall`` convention only handles this second phase of 4817e5dd7070Spatricklowering; the C function type must accurately reflect the results 4818e5dd7070Spatrickof the first phase, as follows: 4819e5dd7070Spatrick 4820e5dd7070Spatrick- Results classified as indirect by high-level lowering should be 4821e5dd7070Spatrick represented as parameters with the ``swift_indirect_result`` attribute. 4822e5dd7070Spatrick 4823e5dd7070Spatrick- Results classified as direct by high-level lowering should be represented 4824e5dd7070Spatrick as follows: 4825e5dd7070Spatrick 4826e5dd7070Spatrick - First, remove any empty direct results. 4827e5dd7070Spatrick 4828e5dd7070Spatrick - If there are no direct results, the C result type should be ``void``. 4829e5dd7070Spatrick 4830e5dd7070Spatrick - If there is one direct result, the C result type should be a type with 4831e5dd7070Spatrick the exact layout of that result type. 4832e5dd7070Spatrick 4833e5dd7070Spatrick - If there are a multiple direct results, the C result type should be 4834e5dd7070Spatrick a struct type with the exact layout of a tuple of those results. 4835e5dd7070Spatrick 4836e5dd7070Spatrick- Parameters classified as indirect by high-level lowering should be 4837e5dd7070Spatrick represented as parameters of pointer type. 4838e5dd7070Spatrick 4839e5dd7070Spatrick- Parameters classified as direct by high-level lowering should be 4840e5dd7070Spatrick omitted if they are empty types; otherwise, they should be represented 4841e5dd7070Spatrick as a parameter type with a layout exactly matching the layout of the 4842e5dd7070Spatrick Swift parameter type. 4843e5dd7070Spatrick 4844e5dd7070Spatrick- The context parameter, if present, should be represented as a trailing 4845e5dd7070Spatrick parameter with the ``swift_context`` attribute. 4846e5dd7070Spatrick 4847e5dd7070Spatrick- The error result parameter, if present, should be represented as a 4848e5dd7070Spatrick trailing parameter (always following a context parameter) with the 4849e5dd7070Spatrick ``swift_error_result`` attribute. 4850e5dd7070Spatrick 4851e5dd7070Spatrick``swiftcall`` does not support variadic arguments or unprototyped functions. 4852e5dd7070Spatrick 4853e5dd7070SpatrickThe parameter ABI treatment attributes are aspects of the function type. 4854ec727ea7SpatrickA function type which applies an ABI treatment attribute to a 4855e5dd7070Spatrickparameter is a different type from an otherwise-identical function type 4856e5dd7070Spatrickthat does not. A single parameter may not have multiple ABI treatment 4857e5dd7070Spatrickattributes. 4858e5dd7070Spatrick 4859e5dd7070SpatrickSupport for this feature is target-dependent, although it should be 4860e5dd7070Spatricksupported on every target that Swift supports. Query for this support 4861e5dd7070Spatrickwith ``__has_attribute(swiftcall)``. This implies support for the 4862e5dd7070Spatrick``swift_context``, ``swift_error_result``, and ``swift_indirect_result`` 4863e5dd7070Spatrickattributes. 4864e5dd7070Spatrick }]; 4865e5dd7070Spatrick} 4866e5dd7070Spatrick 4867e5dd7070Spatrickdef SwiftContextDocs : Documentation { 4868e5dd7070Spatrick let Category = DocCatVariable; 4869e5dd7070Spatrick let Content = [{ 4870e5dd7070SpatrickThe ``swift_context`` attribute marks a parameter of a ``swiftcall`` 4871a9ac8606Spatrickor ``swiftasynccall`` function as having the special context-parameter 4872a9ac8606SpatrickABI treatment. 4873e5dd7070Spatrick 4874e5dd7070SpatrickThis treatment generally passes the context value in a special register 4875e5dd7070Spatrickwhich is normally callee-preserved. 4876e5dd7070Spatrick 4877e5dd7070SpatrickA ``swift_context`` parameter must either be the last parameter or must be 4878e5dd7070Spatrickfollowed by a ``swift_error_result`` parameter (which itself must always be 4879e5dd7070Spatrickthe last parameter). 4880e5dd7070Spatrick 4881e5dd7070SpatrickA context parameter must have pointer or reference type. 4882e5dd7070Spatrick }]; 4883e5dd7070Spatrick} 4884e5dd7070Spatrick 4885a9ac8606Spatrickdef SwiftAsyncCallDocs : Documentation { 4886a9ac8606Spatrick let Category = DocCatVariable; 4887a9ac8606Spatrick let Content = [{ 4888a9ac8606SpatrickThe ``swiftasynccall`` attribute indicates that a function is 4889a9ac8606Spatrickcompatible with the low-level conventions of Swift async functions, 4890a9ac8606Spatrickprovided it declares the right formal arguments. 4891a9ac8606Spatrick 4892a9ac8606SpatrickIn most respects, this is similar to the ``swiftcall`` attribute, except for 4893a9ac8606Spatrickthe following: 4894a9ac8606Spatrick 4895a9ac8606Spatrick- A parameter may be marked ``swift_async_context``, ``swift_context`` 4896a9ac8606Spatrick or ``swift_indirect_result`` (with the same restrictions on parameter 4897a9ac8606Spatrick ordering as ``swiftcall``) but the parameter attribute 4898a9ac8606Spatrick ``swift_error_result`` is not permitted. 4899a9ac8606Spatrick 4900a9ac8606Spatrick- A ``swiftasynccall`` function must have return type ``void``. 4901a9ac8606Spatrick 4902a9ac8606Spatrick- Within a ``swiftasynccall`` function, a call to a ``swiftasynccall`` 4903a9ac8606Spatrick function that is the immediate operand of a ``return`` statement is 4904a9ac8606Spatrick guaranteed to be performed as a tail call. This syntax is allowed even 4905a9ac8606Spatrick in C as an extension (a call to a void-returning function cannot be a 4906a9ac8606Spatrick return operand in standard C). If something in the calling function would 4907a9ac8606Spatrick semantically be performed after a guaranteed tail call, such as the 4908a9ac8606Spatrick non-trivial destruction of a local variable or temporary, 4909a9ac8606Spatrick then the program is ill-formed. 4910a9ac8606Spatrick }]; 4911a9ac8606Spatrick} 4912a9ac8606Spatrick 4913a9ac8606Spatrickdef SwiftAsyncContextDocs : Documentation { 4914a9ac8606Spatrick let Category = DocCatVariable; 4915a9ac8606Spatrick let Content = [{ 4916a9ac8606SpatrickThe ``swift_async_context`` attribute marks a parameter of a ``swiftasynccall`` 4917a9ac8606Spatrickfunction as having the special asynchronous context-parameter ABI treatment. 4918a9ac8606Spatrick 4919a9ac8606SpatrickIf the function is not ``swiftasynccall``, this attribute only generates 4920a9ac8606Spatrickextended frame information. 4921a9ac8606Spatrick 4922a9ac8606SpatrickA context parameter must have pointer or reference type. 4923a9ac8606Spatrick }]; 4924a9ac8606Spatrick} 4925a9ac8606Spatrick 4926e5dd7070Spatrickdef SwiftErrorResultDocs : Documentation { 4927e5dd7070Spatrick let Category = DocCatVariable; 4928e5dd7070Spatrick let Content = [{ 4929e5dd7070SpatrickThe ``swift_error_result`` attribute marks a parameter of a ``swiftcall`` 4930e5dd7070Spatrickfunction as having the special error-result ABI treatment. 4931e5dd7070Spatrick 4932e5dd7070SpatrickThis treatment generally passes the underlying error value in and out of 4933e5dd7070Spatrickthe function through a special register which is normally callee-preserved. 4934e5dd7070SpatrickThis is modeled in C by pretending that the register is addressable memory: 4935e5dd7070Spatrick 4936e5dd7070Spatrick- The caller appears to pass the address of a variable of pointer type. 4937e5dd7070Spatrick The current value of this variable is copied into the register before 4938e5dd7070Spatrick the call; if the call returns normally, the value is copied back into the 4939e5dd7070Spatrick variable. 4940e5dd7070Spatrick 4941e5dd7070Spatrick- The callee appears to receive the address of a variable. This address 4942e5dd7070Spatrick is actually a hidden location in its own stack, initialized with the 4943e5dd7070Spatrick value of the register upon entry. When the function returns normally, 4944e5dd7070Spatrick the value in that hidden location is written back to the register. 4945e5dd7070Spatrick 4946e5dd7070SpatrickA ``swift_error_result`` parameter must be the last parameter, and it must be 4947e5dd7070Spatrickpreceded by a ``swift_context`` parameter. 4948e5dd7070Spatrick 4949e5dd7070SpatrickA ``swift_error_result`` parameter must have type ``T**`` or ``T*&`` for some 4950e5dd7070Spatricktype T. Note that no qualifiers are permitted on the intermediate level. 4951e5dd7070Spatrick 4952e5dd7070SpatrickIt is undefined behavior if the caller does not pass a pointer or 4953e5dd7070Spatrickreference to a valid object. 4954e5dd7070Spatrick 4955e5dd7070SpatrickThe standard convention is that the error value itself (that is, the 4956e5dd7070Spatrickvalue stored in the apparent argument) will be null upon function entry, 4957e5dd7070Spatrickbut this is not enforced by the ABI. 4958e5dd7070Spatrick }]; 4959e5dd7070Spatrick} 4960e5dd7070Spatrick 4961e5dd7070Spatrickdef SwiftIndirectResultDocs : Documentation { 4962e5dd7070Spatrick let Category = DocCatVariable; 4963e5dd7070Spatrick let Content = [{ 4964e5dd7070SpatrickThe ``swift_indirect_result`` attribute marks a parameter of a ``swiftcall`` 4965a9ac8606Spatrickor ``swiftasynccall`` function as having the special indirect-result ABI 4966a9ac8606Spatricktreatment. 4967e5dd7070Spatrick 4968e5dd7070SpatrickThis treatment gives the parameter the target's normal indirect-result 4969e5dd7070SpatrickABI treatment, which may involve passing it differently from an ordinary 4970e5dd7070Spatrickparameter. However, only the first indirect result will receive this 4971e5dd7070Spatricktreatment. Furthermore, low-level lowering may decide that a direct result 4972e5dd7070Spatrickmust be returned indirectly; if so, this will take priority over the 4973e5dd7070Spatrick``swift_indirect_result`` parameters. 4974e5dd7070Spatrick 4975e5dd7070SpatrickA ``swift_indirect_result`` parameter must either be the first parameter or 4976e5dd7070Spatrickfollow another ``swift_indirect_result`` parameter. 4977e5dd7070Spatrick 4978e5dd7070SpatrickA ``swift_indirect_result`` parameter must have type ``T*`` or ``T&`` for 4979e5dd7070Spatricksome object type ``T``. If ``T`` is a complete type at the point of 4980e5dd7070Spatrickdefinition of a function, it is undefined behavior if the argument 4981e5dd7070Spatrickvalue does not point to storage of adequate size and alignment for a 4982e5dd7070Spatrickvalue of type ``T``. 4983e5dd7070Spatrick 4984e5dd7070SpatrickMaking indirect results explicit in the signature allows C functions to 4985e5dd7070Spatrickdirectly construct objects into them without relying on language 4986e5dd7070Spatrickoptimizations like C++'s named return value optimization (NRVO). 4987e5dd7070Spatrick }]; 4988e5dd7070Spatrick} 4989e5dd7070Spatrick 4990a9ac8606Spatrickdef SwiftAsyncDocs : Documentation { 4991a9ac8606Spatrick let Category = SwiftDocs; 4992a9ac8606Spatrick let Heading = "swift_async"; 4993a9ac8606Spatrick let Content = [{ 4994a9ac8606SpatrickThe ``swift_async`` attribute specifies if and how a particular function or 4995a9ac8606SpatrickObjective-C method is imported into a swift async method. For instance: 4996a9ac8606Spatrick 4997a9ac8606Spatrick.. code-block:: objc 4998a9ac8606Spatrick 4999a9ac8606Spatrick @interface MyClass : NSObject 5000a9ac8606Spatrick -(void)notActuallyAsync:(int)p1 withCompletionHandler:(void (^)())handler 5001a9ac8606Spatrick __attribute__((swift_async(none))); 5002a9ac8606Spatrick 5003a9ac8606Spatrick -(void)actuallyAsync:(int)p1 callThisAsync:(void (^)())fun 5004a9ac8606Spatrick __attribute__((swift_async(swift_private, 1))); 5005a9ac8606Spatrick @end 5006a9ac8606Spatrick 5007a9ac8606SpatrickHere, ``notActuallyAsync:withCompletionHandler`` would have been imported as 5008a9ac8606Spatrick``async`` (because it's last parameter's selector piece is 5009a9ac8606Spatrick``withCompletionHandler``) if not for the ``swift_async(none)`` attribute. 5010a9ac8606SpatrickConversely, ``actuallyAsync:callThisAsync`` wouldn't have been imported as 5011a9ac8606Spatrick``async`` if not for the ``swift_async`` attribute because it doesn't match the 5012a9ac8606Spatricknaming convention. 5013a9ac8606Spatrick 5014a9ac8606SpatrickWhen using ``swift_async`` to enable importing, the first argument to the 5015a9ac8606Spatrickattribute is either ``swift_private`` or ``not_swift_private`` to indicate 5016a9ac8606Spatrickwhether the function/method is private to the current framework, and the second 5017a9ac8606Spatrickargument is the index of the completion handler parameter. 5018a9ac8606Spatrick }]; 5019a9ac8606Spatrick} 5020a9ac8606Spatrick 5021a9ac8606Spatrickdef SwiftAsyncErrorDocs : Documentation { 5022a9ac8606Spatrick let Category = SwiftDocs; 5023a9ac8606Spatrick let Heading = "swift_async_error"; 5024a9ac8606Spatrick let Content = [{ 5025a9ac8606SpatrickThe ``swift_async_error`` attribute specifies how an error state will be 5026a9ac8606Spatrickrepresented in a swift async method. It's a bit analogous to the ``swift_error`` 5027a9ac8606Spatrickattribute for the generated async method. The ``swift_async_error`` attribute 5028a9ac8606Spatrickcan indicate a variety of different ways of representing an error. 5029a9ac8606Spatrick 5030a9ac8606Spatrick- ``__attribute__((swift_async_error(zero_argument, N)))``, specifies that the 5031a9ac8606Spatrick async method is considered to have failed if the Nth argument to the 5032a9ac8606Spatrick completion handler is zero. 5033a9ac8606Spatrick 5034a9ac8606Spatrick- ``__attribute__((swift_async_error(nonzero_argument, N)))``, specifies that 5035a9ac8606Spatrick the async method is considered to have failed if the Nth argument to the 5036a9ac8606Spatrick completion handler is non-zero. 5037a9ac8606Spatrick 5038a9ac8606Spatrick- ``__attribute__((swift_async_error(nonnull_error)))``, specifies that the 5039a9ac8606Spatrick async method is considered to have failed if the ``NSError *`` argument to the 5040a9ac8606Spatrick completion handler is non-null. 5041a9ac8606Spatrick 5042a9ac8606Spatrick- ``__attribute__((swift_async_error(none)))``, specifies that the async method 5043a9ac8606Spatrick cannot fail. 5044a9ac8606Spatrick 5045a9ac8606Spatrick 5046a9ac8606SpatrickFor instance: 5047a9ac8606Spatrick 5048a9ac8606Spatrick.. code-block:: objc 5049a9ac8606Spatrick 5050a9ac8606Spatrick @interface MyClass : NSObject 5051a9ac8606Spatrick -(void)asyncMethod:(void (^)(char, int, float))handler 5052a9ac8606Spatrick __attribute__((swift_async(swift_private, 1))) 5053a9ac8606Spatrick __attribute__((swift_async_error(zero_argument, 2))); 5054a9ac8606Spatrick @end 5055a9ac8606Spatrick 5056a9ac8606SpatrickHere, the ``swift_async`` attribute specifies that ``handler`` is the completion 5057a9ac8606Spatrickhandler for this method, and the ``swift_async_error`` attribute specifies that 5058a9ac8606Spatrickthe ``int`` parameter is the one that represents the error. 5059a9ac8606Spatrick}]; 5060a9ac8606Spatrick} 5061a9ac8606Spatrick 5062e5dd7070Spatrickdef SuppressDocs : Documentation { 5063e5dd7070Spatrick let Category = DocCatStmt; 5064e5dd7070Spatrick let Content = [{ 5065e5dd7070SpatrickThe ``[[gsl::suppress]]`` attribute suppresses specific 5066e5dd7070Spatrickclang-tidy diagnostics for rules of the `C++ Core Guidelines`_ in a portable 5067e5dd7070Spatrickway. The attribute can be attached to declarations, statements, and at 5068e5dd7070Spatricknamespace scope. 5069e5dd7070Spatrick 5070e5dd7070Spatrick.. code-block:: c++ 5071e5dd7070Spatrick 5072e5dd7070Spatrick [[gsl::suppress("Rh-public")]] 5073e5dd7070Spatrick void f_() { 5074e5dd7070Spatrick int *p; 5075e5dd7070Spatrick [[gsl::suppress("type")]] { 5076e5dd7070Spatrick p = reinterpret_cast<int*>(7); 5077e5dd7070Spatrick } 5078e5dd7070Spatrick } 5079e5dd7070Spatrick namespace N { 5080e5dd7070Spatrick [[clang::suppress("type", "bounds")]]; 5081e5dd7070Spatrick ... 5082e5dd7070Spatrick } 5083e5dd7070Spatrick 5084e5dd7070Spatrick.. _`C++ Core Guidelines`: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#inforce-enforcement 5085e5dd7070Spatrick }]; 5086e5dd7070Spatrick} 5087e5dd7070Spatrick 5088e5dd7070Spatrickdef AbiTagsDocs : Documentation { 5089e5dd7070Spatrick let Category = DocCatFunction; 5090e5dd7070Spatrick let Content = [{ 5091e5dd7070SpatrickThe ``abi_tag`` attribute can be applied to a function, variable, class or 5092e5dd7070Spatrickinline namespace declaration to modify the mangled name of the entity. It gives 5093e5dd7070Spatrickthe ability to distinguish between different versions of the same entity but 5094e5dd7070Spatrickwith different ABI versions supported. For example, a newer version of a class 5095e5dd7070Spatrickcould have a different set of data members and thus have a different size. Using 5096e5dd7070Spatrickthe ``abi_tag`` attribute, it is possible to have different mangled names for 5097e5dd7070Spatricka global variable of the class type. Therefore, the old code could keep using 5098ec727ea7Spatrickthe old mangled name and the new code will use the new mangled name with tags. 5099e5dd7070Spatrick }]; 5100e5dd7070Spatrick} 5101e5dd7070Spatrick 5102a9ac8606Spatrickdef BuiltinAliasDocs : Documentation { 5103a9ac8606Spatrick let Category = DocCatFunction; 5104a9ac8606Spatrick let Heading = "clang::builtin_alias, clang_builtin_alias"; 5105a9ac8606Spatrick let Content = [{ 5106a9ac8606SpatrickThis attribute is used in the implementation of the C intrinsics. 5107a9ac8606SpatrickIt allows the C intrinsic functions to be declared using the names defined 5108a9ac8606Spatrickin target builtins, and still be recognized as clang builtins equivalent to the 5109a9ac8606Spatrickunderlying name. For example, ``riscv_vector.h`` declares the function ``vadd`` 5110a9ac8606Spatrickwith ``__attribute__((clang_builtin_alias(__builtin_rvv_vadd_vv_i8m1)))``. 5111a9ac8606SpatrickThis ensures that both functions are recognized as that clang builtin, 5112a9ac8606Spatrickand in the latter case, the choice of which builtin to identify the 5113a9ac8606Spatrickfunction as can be deferred until after overload resolution. 5114a9ac8606Spatrick 5115a9ac8606SpatrickThis attribute can only be used to set up the aliases for certain ARM/RISC-V 5116a9ac8606SpatrickC intrinsic functions; it is intended for use only inside ``arm_*.h`` and 5117a9ac8606Spatrick``riscv_*.h`` and is not a general mechanism for declaring arbitrary aliases 5118a9ac8606Spatrickfor clang builtin functions. 5119a9ac8606Spatrick }]; 5120a9ac8606Spatrick} 5121a9ac8606Spatrick 5122a9ac8606Spatrickdef PreferredNameDocs : Documentation { 5123a9ac8606Spatrick let Category = DocCatDecl; 5124a9ac8606Spatrick let Content = [{ 5125a9ac8606SpatrickThe ``preferred_name`` attribute can be applied to a class template, and 5126a9ac8606Spatrickspecifies a preferred way of naming a specialization of the template. The 5127a9ac8606Spatrickpreferred name will be used whenever the corresponding template specialization 5128a9ac8606Spatrickwould otherwise be printed in a diagnostic or similar context. 5129a9ac8606Spatrick 5130a9ac8606SpatrickThe preferred name must be a typedef or type alias declaration that refers to a 5131a9ac8606Spatrickspecialization of the class template (not including any type qualifiers). In 5132a9ac8606Spatrickgeneral this requires the template to be declared at least twice. For example: 5133a9ac8606Spatrick 5134a9ac8606Spatrick.. code-block:: c++ 5135a9ac8606Spatrick 5136a9ac8606Spatrick template<typename T> struct basic_string; 5137a9ac8606Spatrick using string = basic_string<char>; 5138a9ac8606Spatrick using wstring = basic_string<wchar_t>; 5139a9ac8606Spatrick template<typename T> struct [[clang::preferred_name(string), 5140a9ac8606Spatrick clang::preferred_name(wstring)]] basic_string { 5141a9ac8606Spatrick // ... 5142a9ac8606Spatrick }; 5143*12c85518Srobert 5144*12c85518Srobert 5145*12c85518SrobertNote that the ``preferred_name`` attribute will be ignored when the compiler 5146*12c85518Srobertwrites a C++20 Module interface now. This is due to a compiler issue 5147*12c85518Srobert(https://github.com/llvm/llvm-project/issues/56490) that blocks users to modularize 5148*12c85518Srobertdeclarations with `preferred_name`. This is intended to be fixed in the future. 5149a9ac8606Spatrick }]; 5150a9ac8606Spatrick} 5151a9ac8606Spatrick 5152e5dd7070Spatrickdef PreserveMostDocs : Documentation { 5153e5dd7070Spatrick let Category = DocCatCallingConvs; 5154e5dd7070Spatrick let Content = [{ 5155e5dd7070SpatrickOn X86-64 and AArch64 targets, this attribute changes the calling convention of 5156e5dd7070Spatricka function. The ``preserve_most`` calling convention attempts to make the code 5157e5dd7070Spatrickin the caller as unintrusive as possible. This convention behaves identically 5158e5dd7070Spatrickto the ``C`` calling convention on how arguments and return values are passed, 5159e5dd7070Spatrickbut it uses a different set of caller/callee-saved registers. This alleviates 5160e5dd7070Spatrickthe burden of saving and recovering a large register set before and after the 5161e5dd7070Spatrickcall in the caller. If the arguments are passed in callee-saved registers, 5162e5dd7070Spatrickthen they will be preserved by the callee across the call. This doesn't 5163e5dd7070Spatrickapply for values returned in callee-saved registers. 5164e5dd7070Spatrick 5165e5dd7070Spatrick- On X86-64 the callee preserves all general purpose registers, except for 5166e5dd7070Spatrick R11. R11 can be used as a scratch register. Floating-point registers 5167e5dd7070Spatrick (XMMs/YMMs) are not preserved and need to be saved by the caller. 5168e5dd7070Spatrick 5169e5dd7070SpatrickThe idea behind this convention is to support calls to runtime functions 5170e5dd7070Spatrickthat have a hot path and a cold path. The hot path is usually a small piece 5171e5dd7070Spatrickof code that doesn't use many registers. The cold path might need to call out to 5172e5dd7070Spatrickanother function and therefore only needs to preserve the caller-saved 5173e5dd7070Spatrickregisters, which haven't already been saved by the caller. The 5174ec727ea7Spatrick``preserve_most`` calling convention is very similar to the ``cold`` calling 5175e5dd7070Spatrickconvention in terms of caller/callee-saved registers, but they are used for 5176e5dd7070Spatrickdifferent types of function calls. ``coldcc`` is for function calls that are 5177ec727ea7Spatrickrarely executed, whereas ``preserve_most`` function calls are intended to be 5178e5dd7070Spatrickon the hot path and definitely executed a lot. Furthermore ``preserve_most`` 5179e5dd7070Spatrickdoesn't prevent the inliner from inlining the function call. 5180e5dd7070Spatrick 5181e5dd7070SpatrickThis calling convention will be used by a future version of the Objective-C 5182e5dd7070Spatrickruntime and should therefore still be considered experimental at this time. 5183e5dd7070SpatrickAlthough this convention was created to optimize certain runtime calls to 5184e5dd7070Spatrickthe Objective-C runtime, it is not limited to this runtime and might be used 5185e5dd7070Spatrickby other runtimes in the future too. The current implementation only 5186e5dd7070Spatricksupports X86-64 and AArch64, but the intention is to support more architectures 5187e5dd7070Spatrickin the future. 5188e5dd7070Spatrick }]; 5189e5dd7070Spatrick} 5190e5dd7070Spatrick 5191e5dd7070Spatrickdef PreserveAllDocs : Documentation { 5192e5dd7070Spatrick let Category = DocCatCallingConvs; 5193e5dd7070Spatrick let Content = [{ 5194e5dd7070SpatrickOn X86-64 and AArch64 targets, this attribute changes the calling convention of 5195e5dd7070Spatricka function. The ``preserve_all`` calling convention attempts to make the code 5196e5dd7070Spatrickin the caller even less intrusive than the ``preserve_most`` calling convention. 5197e5dd7070SpatrickThis calling convention also behaves identical to the ``C`` calling convention 5198e5dd7070Spatrickon how arguments and return values are passed, but it uses a different set of 5199e5dd7070Spatrickcaller/callee-saved registers. This removes the burden of saving and 5200e5dd7070Spatrickrecovering a large register set before and after the call in the caller. If 5201e5dd7070Spatrickthe arguments are passed in callee-saved registers, then they will be 5202e5dd7070Spatrickpreserved by the callee across the call. This doesn't apply for values 5203e5dd7070Spatrickreturned in callee-saved registers. 5204e5dd7070Spatrick 5205e5dd7070Spatrick- On X86-64 the callee preserves all general purpose registers, except for 5206e5dd7070Spatrick R11. R11 can be used as a scratch register. Furthermore it also preserves 5207e5dd7070Spatrick all floating-point registers (XMMs/YMMs). 5208e5dd7070Spatrick 5209e5dd7070SpatrickThe idea behind this convention is to support calls to runtime functions 5210e5dd7070Spatrickthat don't need to call out to any other functions. 5211e5dd7070Spatrick 5212e5dd7070SpatrickThis calling convention, like the ``preserve_most`` calling convention, will be 5213e5dd7070Spatrickused by a future version of the Objective-C runtime and should be considered 5214e5dd7070Spatrickexperimental at this time. 5215e5dd7070Spatrick }]; 5216e5dd7070Spatrick} 5217e5dd7070Spatrick 5218e5dd7070Spatrickdef DeprecatedDocs : Documentation { 5219e5dd7070Spatrick let Category = DocCatDecl; 5220e5dd7070Spatrick let Content = [{ 5221e5dd7070SpatrickThe ``deprecated`` attribute can be applied to a function, a variable, or a 5222e5dd7070Spatricktype. This is useful when identifying functions, variables, or types that are 5223e5dd7070Spatrickexpected to be removed in a future version of a program. 5224e5dd7070Spatrick 5225e5dd7070SpatrickConsider the function declaration for a hypothetical function ``f``: 5226e5dd7070Spatrick 5227e5dd7070Spatrick.. code-block:: c++ 5228e5dd7070Spatrick 5229e5dd7070Spatrick void f(void) __attribute__((deprecated("message", "replacement"))); 5230e5dd7070Spatrick 5231ec727ea7SpatrickWhen spelled as ``__attribute__((deprecated))``, the deprecated attribute can have 5232e5dd7070Spatricktwo optional string arguments. The first one is the message to display when 5233e5dd7070Spatrickemitting the warning; the second one enables the compiler to provide a Fix-It 5234e5dd7070Spatrickto replace the deprecated name with a new name. Otherwise, when spelled as 5235ec727ea7Spatrick``[[gnu::deprecated]]`` or ``[[deprecated]]``, the attribute can have one optional 5236e5dd7070Spatrickstring argument which is the message to display when emitting the warning. 5237e5dd7070Spatrick }]; 5238e5dd7070Spatrick} 5239e5dd7070Spatrick 5240e5dd7070Spatrickdef IFuncDocs : Documentation { 5241e5dd7070Spatrick let Category = DocCatFunction; 5242e5dd7070Spatrick let Content = [{ 5243a9ac8606Spatrick``__attribute__((ifunc("resolver")))`` is used to mark that the address of a 5244a9ac8606Spatrickdeclaration should be resolved at runtime by calling a resolver function. 5245e5dd7070Spatrick 5246a9ac8606SpatrickThe symbol name of the resolver function is given in quotes. A function with 5247a9ac8606Spatrickthis name (after mangling) must be defined in the current translation unit; it 5248a9ac8606Spatrickmay be ``static``. The resolver function should return a pointer. 5249e5dd7070Spatrick 5250a9ac8606SpatrickThe ``ifunc`` attribute may only be used on a function declaration. A function 5251a9ac8606Spatrickdeclaration with an ``ifunc`` attribute is considered to be a definition of the 5252a9ac8606Spatrickdeclared entity. The entity must not have weak linkage; for example, in C++, 5253a9ac8606Spatrickit cannot be applied to a declaration if a definition at that location would be 5254a9ac8606Spatrickconsidered inline. 5255e5dd7070Spatrick 5256a9ac8606SpatrickNot all targets support this attribute. ELF target support depends on both the 5257a9ac8606Spatricklinker and runtime linker, and is available in at least lld 4.0 and later, 5258a9ac8606Spatrickbinutils 2.20.1 and later, glibc v2.11.1 and later, and FreeBSD 9.1 and later. 5259a9ac8606SpatrickNon-ELF targets currently do not support this attribute. 5260e5dd7070Spatrick }]; 5261e5dd7070Spatrick} 5262e5dd7070Spatrick 5263e5dd7070Spatrickdef LTOVisibilityDocs : Documentation { 5264e5dd7070Spatrick let Category = DocCatDecl; 5265e5dd7070Spatrick let Content = [{ 5266e5dd7070SpatrickSee :doc:`LTOVisibility`. 5267e5dd7070Spatrick }]; 5268e5dd7070Spatrick} 5269e5dd7070Spatrick 5270e5dd7070Spatrickdef RenderScriptKernelAttributeDocs : Documentation { 5271e5dd7070Spatrick let Category = DocCatFunction; 5272e5dd7070Spatrick let Content = [{ 5273e5dd7070Spatrick``__attribute__((kernel))`` is used to mark a ``kernel`` function in 5274e5dd7070SpatrickRenderScript. 5275e5dd7070Spatrick 5276e5dd7070SpatrickIn RenderScript, ``kernel`` functions are used to express data-parallel 5277e5dd7070Spatrickcomputations. The RenderScript runtime efficiently parallelizes ``kernel`` 5278e5dd7070Spatrickfunctions to run on computational resources such as multi-core CPUs and GPUs. 5279e5dd7070SpatrickSee the RenderScript_ documentation for more information. 5280e5dd7070Spatrick 5281e5dd7070Spatrick.. _RenderScript: https://developer.android.com/guide/topics/renderscript/compute.html 5282e5dd7070Spatrick }]; 5283e5dd7070Spatrick} 5284e5dd7070Spatrick 5285e5dd7070Spatrickdef XRayDocs : Documentation { 5286e5dd7070Spatrick let Category = DocCatFunction; 5287e5dd7070Spatrick let Heading = "xray_always_instrument, xray_never_instrument, xray_log_args"; 5288e5dd7070Spatrick let Content = [{ 5289a9ac8606Spatrick``__attribute__((xray_always_instrument))`` or 5290a9ac8606Spatrick``[[clang::xray_always_instrument]]`` is used to mark member functions (in C++), 5291a9ac8606Spatrickmethods (in Objective C), and free functions (in C, C++, and Objective C) to be 5292a9ac8606Spatrickinstrumented with XRay. This will cause the function to always have space at 5293a9ac8606Spatrickthe beginning and exit points to allow for runtime patching. 5294e5dd7070Spatrick 5295a9ac8606SpatrickConversely, ``__attribute__((xray_never_instrument))`` or 5296a9ac8606Spatrick``[[clang::xray_never_instrument]]`` will inhibit the insertion of these 5297a9ac8606Spatrickinstrumentation points. 5298e5dd7070Spatrick 5299a9ac8606SpatrickIf a function has neither of these attributes, they become subject to the XRay 5300a9ac8606Spatrickheuristics used to determine whether a function should be instrumented or 5301a9ac8606Spatrickotherwise. 5302e5dd7070Spatrick 5303a9ac8606Spatrick``__attribute__((xray_log_args(N)))`` or ``[[clang::xray_log_args(N)]]`` is 5304a9ac8606Spatrickused to preserve N function arguments for the logging function. Currently, 5305a9ac8606Spatrickonly N==1 is supported. 5306e5dd7070Spatrick }]; 5307e5dd7070Spatrick} 5308e5dd7070Spatrick 5309e5dd7070Spatrickdef PatchableFunctionEntryDocs : Documentation { 5310e5dd7070Spatrick let Category = DocCatFunction; 5311e5dd7070Spatrick let Content = [{ 5312e5dd7070Spatrick``__attribute__((patchable_function_entry(N,M)))`` is used to generate M NOPs 5313e5dd7070Spatrickbefore the function entry and N-M NOPs after the function entry. This attribute 5314e5dd7070Spatricktakes precedence over the command line option ``-fpatchable-function-entry=N,M``. 5315e5dd7070Spatrick``M`` defaults to 0 if omitted. 5316a9ac8606Spatrick 5317a9ac8606SpatrickThis attribute is only supported on 5318a9ac8606Spatrickaarch64/aarch64-be/riscv32/riscv64/i386/x86-64 targets. 5319e5dd7070Spatrick}]; 5320e5dd7070Spatrick} 5321e5dd7070Spatrick 5322*12c85518Srobertdef HotFunctionEntryDocs : Documentation { 5323*12c85518Srobert let Category = DocCatFunction; 5324*12c85518Srobert let Content = [{ 5325*12c85518Srobert``__attribute__((hot))`` marks a function as hot, as a manual alternative to PGO hotness data. 5326*12c85518SrobertIf PGO data is available, the annotation ``__attribute__((hot))`` overrides the profile count based hotness (unlike ``__attribute__((cold))``). 5327*12c85518Srobert}]; 5328*12c85518Srobert} 5329*12c85518Srobert 5330*12c85518Srobertdef ColdFunctionEntryDocs : Documentation { 5331*12c85518Srobert let Category = DocCatFunction; 5332*12c85518Srobert let Content = [{ 5333*12c85518Srobert``__attribute__((cold))`` marks a function as cold, as a manual alternative to PGO hotness data. 5334*12c85518SrobertIf PGO data is available, the profile count based hotness overrides the ``__attribute__((cold))`` annotation (unlike ``__attribute__((hot))``). 5335*12c85518Srobert}]; 5336*12c85518Srobert} 5337e5dd7070Spatrickdef TransparentUnionDocs : Documentation { 5338e5dd7070Spatrick let Category = DocCatDecl; 5339e5dd7070Spatrick let Content = [{ 5340ec727ea7SpatrickThis attribute can be applied to a union to change the behavior of calls to 5341e5dd7070Spatrickfunctions that have an argument with a transparent union type. The compiler 5342ec727ea7Spatrickbehavior is changed in the following manner: 5343e5dd7070Spatrick 5344e5dd7070Spatrick- A value whose type is any member of the transparent union can be passed as an 5345e5dd7070Spatrick argument without the need to cast that value. 5346e5dd7070Spatrick 5347e5dd7070Spatrick- The argument is passed to the function using the calling convention of the 5348e5dd7070Spatrick first member of the transparent union. Consequently, all the members of the 5349e5dd7070Spatrick transparent union should have the same calling convention as its first member. 5350e5dd7070Spatrick 5351e5dd7070SpatrickTransparent unions are not supported in C++. 5352e5dd7070Spatrick }]; 5353e5dd7070Spatrick} 5354e5dd7070Spatrick 5355e5dd7070Spatrickdef ObjCSubclassingRestrictedDocs : Documentation { 5356e5dd7070Spatrick let Category = DocCatDecl; 5357e5dd7070Spatrick let Content = [{ 5358e5dd7070SpatrickThis attribute can be added to an Objective-C ``@interface`` declaration to 5359e5dd7070Spatrickensure that this class cannot be subclassed. 5360e5dd7070Spatrick }]; 5361e5dd7070Spatrick} 5362e5dd7070Spatrick 5363e5dd7070Spatrickdef ObjCNonLazyClassDocs : Documentation { 5364e5dd7070Spatrick let Category = DocCatDecl; 5365e5dd7070Spatrick let Content = [{ 5366e5dd7070SpatrickThis attribute can be added to an Objective-C ``@interface`` or 5367e5dd7070Spatrick``@implementation`` declaration to add the class to the list of non-lazily 5368e5dd7070Spatrickinitialized classes. A non-lazy class will be initialized eagerly when the 5369e5dd7070SpatrickObjective-C runtime is loaded. This is required for certain system classes which 5370e5dd7070Spatrickhave instances allocated in non-standard ways, such as the classes for blocks 5371e5dd7070Spatrickand constant strings. Adding this attribute is essentially equivalent to 5372ec727ea7Spatrickproviding a trivial ``+load`` method but avoids the (fairly small) load-time 5373e5dd7070Spatrickoverheads associated with defining and calling such a method. 5374e5dd7070Spatrick }]; 5375e5dd7070Spatrick} 5376e5dd7070Spatrick 5377e5dd7070Spatrickdef ObjCDirectDocs : Documentation { 5378e5dd7070Spatrick let Category = DocCatDecl; 5379e5dd7070Spatrick let Content = [{ 5380e5dd7070SpatrickThe ``objc_direct`` attribute can be used to mark an Objective-C method as 5381e5dd7070Spatrickbeing *direct*. A direct method is treated statically like an ordinary method, 5382e5dd7070Spatrickbut dynamically it behaves more like a C function. This lowers some of the costs 5383e5dd7070Spatrickassociated with the method but also sacrifices some of the ordinary capabilities 5384e5dd7070Spatrickof Objective-C methods. 5385e5dd7070Spatrick 5386e5dd7070SpatrickA message send of a direct method calls the implementation directly, as if it 5387e5dd7070Spatrickwere a C function, rather than using ordinary Objective-C method dispatch. This 5388e5dd7070Spatrickis substantially faster and potentially allows the implementation to be inlined, 5389e5dd7070Spatrickbut it also means the method cannot be overridden in subclasses or replaced 5390e5dd7070Spatrickdynamically, as ordinary Objective-C methods can. 5391e5dd7070Spatrick 5392e5dd7070SpatrickFurthermore, a direct method is not listed in the class's method lists. This 5393e5dd7070Spatricksubstantially reduces the code-size overhead of the method but also means it 5394e5dd7070Spatrickcannot be called dynamically using ordinary Objective-C method dispatch at all; 5395e5dd7070Spatrickin particular, this means that it cannot override a superclass method or satisfy 5396e5dd7070Spatricka protocol requirement. 5397e5dd7070Spatrick 5398e5dd7070SpatrickBecause a direct method cannot be overridden, it is an error to perform 5399e5dd7070Spatricka ``super`` message send of one. 5400e5dd7070Spatrick 5401e5dd7070SpatrickAlthough a message send of a direct method causes the method to be called 5402e5dd7070Spatrickdirectly as if it were a C function, it still obeys Objective-C semantics in other 5403e5dd7070Spatrickways: 5404e5dd7070Spatrick 5405e5dd7070Spatrick- If the receiver is ``nil``, the message send does nothing and returns the zero value 5406e5dd7070Spatrick for the return type. 5407e5dd7070Spatrick 5408e5dd7070Spatrick- A message send of a direct class method will cause the class to be initialized, 5409e5dd7070Spatrick including calling the ``+initialize`` method if present. 5410e5dd7070Spatrick 5411e5dd7070Spatrick- The implicit ``_cmd`` parameter containing the method's selector is still defined. 5412e5dd7070Spatrick In order to minimize code-size costs, the implementation will not emit a reference 5413e5dd7070Spatrick to the selector if the parameter is unused within the method. 5414e5dd7070Spatrick 5415e5dd7070SpatrickSymbols for direct method implementations are implicitly given hidden 5416e5dd7070Spatrickvisibility, meaning that they can only be called within the same linkage unit. 5417e5dd7070Spatrick 5418e5dd7070SpatrickIt is an error to do any of the following: 5419e5dd7070Spatrick 5420e5dd7070Spatrick- declare a direct method in a protocol, 5421e5dd7070Spatrick- declare an override of a direct method with a method in a subclass, 5422e5dd7070Spatrick- declare an override of a non-direct method with a direct method in a subclass, 5423e5dd7070Spatrick- declare a method with different directness in different class interfaces, or 5424e5dd7070Spatrick- implement a non-direct method (as declared in any class interface) with a direct method. 5425e5dd7070Spatrick 5426e5dd7070SpatrickIf any of these rules would be violated if every method defined in an 5427e5dd7070Spatrick``@implementation`` within a single linkage unit were declared in an 5428e5dd7070Spatrickappropriate class interface, the program is ill-formed with no diagnostic 5429e5dd7070Spatrickrequired. If a violation of this rule is not diagnosed, behavior remains 5430e5dd7070Spatrickwell-defined; this paragraph is simply reserving the right to diagnose such 5431e5dd7070Spatrickconflicts in the future, not to treat them as undefined behavior. 5432e5dd7070Spatrick 5433e5dd7070SpatrickAdditionally, Clang will warn about any ``@selector`` expression that 5434e5dd7070Spatricknames a selector that is only known to be used for direct methods. 5435e5dd7070Spatrick 5436e5dd7070SpatrickFor the purpose of these rules, a "class interface" includes a class's primary 5437e5dd7070Spatrick``@interface`` block, its class extensions, its categories, its declared protocols, 5438e5dd7070Spatrickand all the class interfaces of its superclasses. 5439e5dd7070Spatrick 5440e5dd7070SpatrickAn Objective-C property can be declared with the ``direct`` property 5441e5dd7070Spatrickattribute. If a direct property declaration causes an implicit declaration of 5442e5dd7070Spatricka getter or setter method (that is, if the given method is not explicitly 5443e5dd7070Spatrickdeclared elsewhere), the method is declared to be direct. 5444e5dd7070Spatrick 5445e5dd7070SpatrickSome programmers may wish to make many methods direct at once. In order 5446e5dd7070Spatrickto simplify this, the ``objc_direct_members`` attribute is provided; see its 5447e5dd7070Spatrickdocumentation for more information. 5448e5dd7070Spatrick }]; 5449e5dd7070Spatrick} 5450e5dd7070Spatrick 5451e5dd7070Spatrickdef ObjCDirectMembersDocs : Documentation { 5452e5dd7070Spatrick let Category = DocCatDecl; 5453e5dd7070Spatrick let Content = [{ 5454e5dd7070SpatrickThe ``objc_direct_members`` attribute can be placed on an Objective-C 5455e5dd7070Spatrick``@interface`` or ``@implementation`` to mark that methods declared 5456e5dd7070Spatricktherein should be considered direct by default. See the documentation 5457e5dd7070Spatrickfor ``objc_direct`` for more information about direct methods. 5458e5dd7070Spatrick 5459e5dd7070SpatrickWhen ``objc_direct_members`` is placed on an ``@interface`` block, every 5460e5dd7070Spatrickmethod in the block is considered to be declared as direct. This includes any 5461e5dd7070Spatrickimplicit method declarations introduced by property declarations. If the method 5462e5dd7070Spatrickredeclares a non-direct method, the declaration is ill-formed, exactly as if the 5463ec727ea7Spatrickmethod was annotated with the ``objc_direct`` attribute. 5464e5dd7070Spatrick 5465e5dd7070SpatrickWhen ``objc_direct_members`` is placed on an ``@implementation`` block, 5466e5dd7070Spatrickmethods defined in the block are considered to be declared as direct unless 5467e5dd7070Spatrickthey have been previously declared as non-direct in any interface of the class. 5468e5dd7070SpatrickThis includes the implicit method definitions introduced by synthesized 5469e5dd7070Spatrickproperties, including auto-synthesized properties. 5470e5dd7070Spatrick }]; 5471e5dd7070Spatrick} 5472e5dd7070Spatrick 5473a9ac8606Spatrickdef ObjCNonRuntimeProtocolDocs : Documentation { 5474a9ac8606Spatrick let Category = DocCatDecl; 5475a9ac8606Spatrick let Content = [{ 5476a9ac8606SpatrickThe ``objc_non_runtime_protocol`` attribute can be used to mark that an 5477a9ac8606SpatrickObjective-C protocol is only used during static type-checking and doesn't need 5478a9ac8606Spatrickto be represented dynamically. This avoids several small code-size and run-time 5479a9ac8606Spatrickoverheads associated with handling the protocol's metadata. A non-runtime 5480a9ac8606Spatrickprotocol cannot be used as the operand of a ``@protocol`` expression, and 5481a9ac8606Spatrickdynamic attempts to find it with ``objc_getProtocol`` will fail. 5482a9ac8606Spatrick 5483a9ac8606SpatrickIf a non-runtime protocol inherits from any ordinary protocols, classes and 5484a9ac8606Spatrickderived protocols that declare conformance to the non-runtime protocol will 5485a9ac8606Spatrickdynamically list their conformance to those bare protocols. 5486a9ac8606Spatrick }]; 5487a9ac8606Spatrick} 5488a9ac8606Spatrick 5489e5dd7070Spatrickdef SelectAnyDocs : Documentation { 5490e5dd7070Spatrick let Category = DocCatDecl; 5491e5dd7070Spatrick let Content = [{ 5492e5dd7070SpatrickThis attribute appertains to a global symbol, causing it to have a weak 5493e5dd7070Spatrickdefinition ( 5494e5dd7070Spatrick`linkonce <https://llvm.org/docs/LangRef.html#linkage-types>`_ 5495e5dd7070Spatrick), allowing the linker to select any definition. 5496e5dd7070Spatrick 5497e5dd7070SpatrickFor more information see 5498e5dd7070Spatrick`gcc documentation <https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Microsoft-Windows-Variable-Attributes.html>`_ 5499e5dd7070Spatrickor `msvc documentation <https://docs.microsoft.com/pl-pl/cpp/cpp/selectany>`_. 5500e5dd7070Spatrick}]; } 5501e5dd7070Spatrick 5502e5dd7070Spatrickdef WebAssemblyExportNameDocs : Documentation { 5503e5dd7070Spatrick let Category = DocCatFunction; 5504e5dd7070Spatrick let Content = [{ 5505e5dd7070SpatrickClang supports the ``__attribute__((export_name(<name>)))`` 5506e5dd7070Spatrickattribute for the WebAssembly target. This attribute may be attached to a 5507e5dd7070Spatrickfunction declaration, where it modifies how the symbol is to be exported 5508e5dd7070Spatrickfrom the linked WebAssembly. 5509e5dd7070Spatrick 5510e5dd7070SpatrickWebAssembly functions are exported via string name. By default when a symbol 5511e5dd7070Spatrickis exported, the export name for C/C++ symbols are the same as their C/C++ 5512e5dd7070Spatricksymbol names. This attribute can be used to override the default behavior, and 5513e5dd7070Spatrickrequest a specific string name be used instead. 5514e5dd7070Spatrick }]; 5515e5dd7070Spatrick} 5516e5dd7070Spatrick 5517e5dd7070Spatrickdef WebAssemblyImportModuleDocs : Documentation { 5518e5dd7070Spatrick let Category = DocCatFunction; 5519e5dd7070Spatrick let Content = [{ 5520e5dd7070SpatrickClang supports the ``__attribute__((import_module(<module_name>)))`` 5521e5dd7070Spatrickattribute for the WebAssembly target. This attribute may be attached to a 5522e5dd7070Spatrickfunction declaration, where it modifies how the symbol is to be imported 5523e5dd7070Spatrickwithin the WebAssembly linking environment. 5524e5dd7070Spatrick 5525e5dd7070SpatrickWebAssembly imports use a two-level namespace scheme, consisting of a module 5526e5dd7070Spatrickname, which typically identifies a module from which to import, and a field 5527e5dd7070Spatrickname, which typically identifies a field from that module to import. By 5528e5dd7070Spatrickdefault, module names for C/C++ symbols are assigned automatically by the 5529e5dd7070Spatricklinker. This attribute can be used to override the default behavior, and 5530e5dd7070Spatrickrequest a specific module name be used instead. 5531e5dd7070Spatrick }]; 5532e5dd7070Spatrick} 5533e5dd7070Spatrick 5534e5dd7070Spatrickdef WebAssemblyImportNameDocs : Documentation { 5535e5dd7070Spatrick let Category = DocCatFunction; 5536e5dd7070Spatrick let Content = [{ 5537e5dd7070SpatrickClang supports the ``__attribute__((import_name(<name>)))`` 5538e5dd7070Spatrickattribute for the WebAssembly target. This attribute may be attached to a 5539e5dd7070Spatrickfunction declaration, where it modifies how the symbol is to be imported 5540e5dd7070Spatrickwithin the WebAssembly linking environment. 5541e5dd7070Spatrick 5542e5dd7070SpatrickWebAssembly imports use a two-level namespace scheme, consisting of a module 5543e5dd7070Spatrickname, which typically identifies a module from which to import, and a field 5544e5dd7070Spatrickname, which typically identifies a field from that module to import. By 5545e5dd7070Spatrickdefault, field names for C/C++ symbols are the same as their C/C++ symbol 5546e5dd7070Spatricknames. This attribute can be used to override the default behavior, and 5547e5dd7070Spatrickrequest a specific field name be used instead. 5548e5dd7070Spatrick }]; 5549e5dd7070Spatrick} 5550e5dd7070Spatrick 5551e5dd7070Spatrickdef ArtificialDocs : Documentation { 5552e5dd7070Spatrick let Category = DocCatFunction; 5553e5dd7070Spatrick let Content = [{ 5554e5dd7070SpatrickThe ``artificial`` attribute can be applied to an inline function. If such a 5555e5dd7070Spatrickfunction is inlined, the attribute indicates that debuggers should associate 5556e5dd7070Spatrickthe resulting instructions with the call site, rather than with the 5557e5dd7070Spatrickcorresponding line within the inlined callee. 5558e5dd7070Spatrick }]; 5559e5dd7070Spatrick} 5560e5dd7070Spatrick 5561e5dd7070Spatrickdef NoDerefDocs : Documentation { 5562e5dd7070Spatrick let Category = DocCatType; 5563e5dd7070Spatrick let Content = [{ 5564e5dd7070SpatrickThe ``noderef`` attribute causes clang to diagnose dereferences of annotated pointer types. 5565e5dd7070SpatrickThis is ideally used with pointers that point to special memory which cannot be read 5566e5dd7070Spatrickfrom or written to, but allowing for the pointer to be used in pointer arithmetic. 5567e5dd7070SpatrickThe following are examples of valid expressions where dereferences are diagnosed: 5568e5dd7070Spatrick 5569e5dd7070Spatrick.. code-block:: c 5570e5dd7070Spatrick 5571e5dd7070Spatrick int __attribute__((noderef)) *p; 5572e5dd7070Spatrick int x = *p; // warning 5573e5dd7070Spatrick 5574e5dd7070Spatrick int __attribute__((noderef)) **p2; 5575e5dd7070Spatrick x = **p2; // warning 5576e5dd7070Spatrick 5577e5dd7070Spatrick int * __attribute__((noderef)) *p3; 5578e5dd7070Spatrick p = *p3; // warning 5579e5dd7070Spatrick 5580e5dd7070Spatrick struct S { 5581e5dd7070Spatrick int a; 5582e5dd7070Spatrick }; 5583e5dd7070Spatrick struct S __attribute__((noderef)) *s; 5584e5dd7070Spatrick x = s->a; // warning 5585e5dd7070Spatrick x = (*s).a; // warning 5586e5dd7070Spatrick 5587e5dd7070SpatrickNot all dereferences may diagnose a warning if the value directed by the pointer may not be 5588e5dd7070Spatrickaccessed. The following are examples of valid expressions where may not be diagnosed: 5589e5dd7070Spatrick 5590e5dd7070Spatrick.. code-block:: c 5591e5dd7070Spatrick 5592e5dd7070Spatrick int *q; 5593e5dd7070Spatrick int __attribute__((noderef)) *p; 5594e5dd7070Spatrick q = &*p; 5595e5dd7070Spatrick q = *&p; 5596e5dd7070Spatrick 5597e5dd7070Spatrick struct S { 5598e5dd7070Spatrick int a; 5599e5dd7070Spatrick }; 5600e5dd7070Spatrick struct S __attribute__((noderef)) *s; 5601e5dd7070Spatrick p = &s->a; 5602e5dd7070Spatrick p = &(*s).a; 5603e5dd7070Spatrick 5604a9ac8606Spatrick``noderef`` is currently only supported for pointers and arrays and not usable 5605a9ac8606Spatrickfor references or Objective-C object pointers. 5606e5dd7070Spatrick 5607e5dd7070Spatrick.. code-block: c++ 5608e5dd7070Spatrick 5609e5dd7070Spatrick int x = 2; 5610e5dd7070Spatrick int __attribute__((noderef)) &y = x; // warning: 'noderef' can only be used on an array or pointer type 5611e5dd7070Spatrick 5612e5dd7070Spatrick.. code-block: objc 5613e5dd7070Spatrick 5614e5dd7070Spatrick id __attribute__((noderef)) obj = [NSObject new]; // warning: 'noderef' can only be used on an array or pointer type 5615e5dd7070Spatrick}]; 5616e5dd7070Spatrick} 5617e5dd7070Spatrick 5618e5dd7070Spatrickdef ReinitializesDocs : Documentation { 5619e5dd7070Spatrick let Category = DocCatFunction; 5620e5dd7070Spatrick let Content = [{ 5621e5dd7070SpatrickThe ``reinitializes`` attribute can be applied to a non-static, non-const C++ 5622e5dd7070Spatrickmember function to indicate that this member function reinitializes the entire 5623e5dd7070Spatrickobject to a known state, independent of the previous state of the object. 5624e5dd7070Spatrick 5625e5dd7070SpatrickThis attribute can be interpreted by static analyzers that warn about uses of an 5626e5dd7070Spatrickobject that has been left in an indeterminate state by a move operation. If a 5627e5dd7070Spatrickmember function marked with the ``reinitializes`` attribute is called on a 5628e5dd7070Spatrickmoved-from object, the analyzer can conclude that the object is no longer in an 5629e5dd7070Spatrickindeterminate state. 5630e5dd7070Spatrick 5631e5dd7070SpatrickA typical example where this attribute would be used is on functions that clear 5632e5dd7070Spatricka container class: 5633e5dd7070Spatrick 5634e5dd7070Spatrick.. code-block:: c++ 5635e5dd7070Spatrick 5636e5dd7070Spatrick template <class T> 5637e5dd7070Spatrick class Container { 5638e5dd7070Spatrick public: 5639e5dd7070Spatrick ... 5640e5dd7070Spatrick [[clang::reinitializes]] void Clear(); 5641e5dd7070Spatrick ... 5642e5dd7070Spatrick }; 5643e5dd7070Spatrick }]; 5644e5dd7070Spatrick} 5645e5dd7070Spatrick 5646e5dd7070Spatrickdef AlwaysDestroyDocs : Documentation { 5647e5dd7070Spatrick let Category = DocCatVariable; 5648e5dd7070Spatrick let Content = [{ 5649e5dd7070SpatrickThe ``always_destroy`` attribute specifies that a variable with static or thread 5650e5dd7070Spatrickstorage duration should have its exit-time destructor run. This attribute is the 5651e5dd7070Spatrickdefault unless clang was invoked with -fno-c++-static-destructors. 5652e5dd7070Spatrick }]; 5653e5dd7070Spatrick} 5654e5dd7070Spatrick 5655e5dd7070Spatrickdef NoDestroyDocs : Documentation { 5656e5dd7070Spatrick let Category = DocCatVariable; 5657e5dd7070Spatrick let Content = [{ 5658e5dd7070SpatrickThe ``no_destroy`` attribute specifies that a variable with static or thread 5659e5dd7070Spatrickstorage duration shouldn't have its exit-time destructor run. Annotating every 5660e5dd7070Spatrickstatic and thread duration variable with this attribute is equivalent to 5661e5dd7070Spatrickinvoking clang with -fno-c++-static-destructors. 5662e5dd7070Spatrick 5663e5dd7070SpatrickIf a variable is declared with this attribute, clang doesn't access check or 5664e5dd7070Spatrickgenerate the type's destructor. If you have a type that you only want to be 5665e5dd7070Spatrickannotated with ``no_destroy``, you can therefore declare the destructor private: 5666e5dd7070Spatrick 5667e5dd7070Spatrick.. code-block:: c++ 5668e5dd7070Spatrick 5669e5dd7070Spatrick struct only_no_destroy { 5670e5dd7070Spatrick only_no_destroy(); 5671e5dd7070Spatrick private: 5672e5dd7070Spatrick ~only_no_destroy(); 5673e5dd7070Spatrick }; 5674e5dd7070Spatrick 5675e5dd7070Spatrick [[clang::no_destroy]] only_no_destroy global; // fine! 5676e5dd7070Spatrick 5677e5dd7070SpatrickNote that destructors are still required for subobjects of aggregates annotated 5678e5dd7070Spatrickwith this attribute. This is because previously constructed subobjects need to 5679e5dd7070Spatrickbe destroyed if an exception gets thrown before the initialization of the 5680e5dd7070Spatrickcomplete object is complete. For instance: 5681e5dd7070Spatrick 5682e5dd7070Spatrick.. code-block:: c++ 5683e5dd7070Spatrick 5684e5dd7070Spatrick void f() { 5685e5dd7070Spatrick try { 5686e5dd7070Spatrick [[clang::no_destroy]] 5687e5dd7070Spatrick static only_no_destroy array[10]; // error, only_no_destroy has a private destructor. 5688e5dd7070Spatrick } catch (...) { 5689e5dd7070Spatrick // Handle the error 5690e5dd7070Spatrick } 5691e5dd7070Spatrick } 5692e5dd7070Spatrick 5693ec727ea7SpatrickHere, if the construction of ``array[9]`` fails with an exception, ``array[0..8]`` 5694e5dd7070Spatrickwill be destroyed, so the element's destructor needs to be accessible. 5695e5dd7070Spatrick }]; 5696e5dd7070Spatrick} 5697e5dd7070Spatrick 5698e5dd7070Spatrickdef UninitializedDocs : Documentation { 5699e5dd7070Spatrick let Category = DocCatVariable; 5700e5dd7070Spatrick let Content = [{ 5701e5dd7070SpatrickThe command-line parameter ``-ftrivial-auto-var-init=*`` can be used to 5702e5dd7070Spatrickinitialize trivial automatic stack variables. By default, trivial automatic 5703e5dd7070Spatrickstack variables are uninitialized. This attribute is used to override the 5704e5dd7070Spatrickcommand-line parameter, forcing variables to remain uninitialized. It has no 5705e5dd7070Spatricksemantic meaning in that using uninitialized values is undefined behavior, 5706e5dd7070Spatrickit rather documents the programmer's intent. 5707e5dd7070Spatrick }]; 5708e5dd7070Spatrick} 5709e5dd7070Spatrick 5710ec727ea7Spatrickdef LoaderUninitializedDocs : Documentation { 5711ec727ea7Spatrick let Category = DocCatVariable; 5712ec727ea7Spatrick let Content = [{ 5713ec727ea7SpatrickThe ``loader_uninitialized`` attribute can be placed on global variables to 5714ec727ea7Spatrickindicate that the variable does not need to be zero initialized by the loader. 5715ec727ea7SpatrickOn most targets, zero-initialization does not incur any additional cost. 5716ec727ea7SpatrickFor example, most general purpose operating systems deliberately ensure 5717ec727ea7Spatrickthat all memory is properly initialized in order to avoid leaking privileged 5718ec727ea7Spatrickinformation from the kernel or other programs. However, some targets 5719ec727ea7Spatrickdo not make this guarantee, and on these targets, avoiding an unnecessary 5720ec727ea7Spatrickzero-initialization can have a significant impact on load times and/or code 5721ec727ea7Spatricksize. 5722ec727ea7Spatrick 5723ec727ea7SpatrickA declaration with this attribute is a non-tentative definition just as if it 5724ec727ea7Spatrickprovided an initializer. Variables with this attribute are considered to be 5725ec727ea7Spatrickuninitialized in the same sense as a local variable, and the programs must 5726ec727ea7Spatrickwrite to them before reading from them. If the variable's type is a C++ class 5727ec727ea7Spatricktype with a non-trivial default constructor, or an array thereof, this attribute 5728ec727ea7Spatrickonly suppresses the static zero-initialization of the variable, not the dynamic 5729ec727ea7Spatrickinitialization provided by executing the default constructor. 5730ec727ea7Spatrick }]; 5731ec727ea7Spatrick} 5732ec727ea7Spatrick 5733e5dd7070Spatrickdef CallbackDocs : Documentation { 5734e5dd7070Spatrick let Category = DocCatFunction; 5735e5dd7070Spatrick let Content = [{ 5736e5dd7070SpatrickThe ``callback`` attribute specifies that the annotated function may invoke the 5737e5dd7070Spatrickspecified callback zero or more times. The callback, as well as the passed 5738e5dd7070Spatrickarguments, are identified by their parameter name or position (starting with 5739e5dd7070Spatrick1!) in the annotated function. The first position in the attribute identifies 5740e5dd7070Spatrickthe callback callee, the following positions declare describe its arguments. 5741e5dd7070SpatrickThe callback callee is required to be callable with the number, and order, of 5742ec727ea7Spatrickthe specified arguments. The index ``0``, or the identifier ``this``, is used to 5743e5dd7070Spatrickrepresent an implicit "this" pointer in class methods. If there is no implicit 5744e5dd7070Spatrick"this" pointer it shall not be referenced. The index '-1', or the name "__", 5745e5dd7070Spatrickrepresents an unknown callback callee argument. This can be a value which is 5746e5dd7070Spatricknot present in the declared parameter list, or one that is, but is potentially 5747e5dd7070Spatrickinspected, captured, or modified. Parameter names and indices can be mixed in 5748e5dd7070Spatrickthe callback attribute. 5749e5dd7070Spatrick 5750e5dd7070SpatrickThe ``callback`` attribute, which is directly translated to ``callback`` 5751e5dd7070Spatrickmetadata <http://llvm.org/docs/LangRef.html#callback-metadata>, make the 5752e5dd7070Spatrickconnection between the call to the annotated function and the callback callee. 5753e5dd7070SpatrickThis can enable interprocedural optimizations which were otherwise impossible. 5754e5dd7070SpatrickIf a function parameter is mentioned in the ``callback`` attribute, through its 5755e5dd7070Spatrickposition, it is undefined if that parameter is used for anything other than the 5756e5dd7070Spatrickactual callback. Inspected, captured, or modified parameters shall not be 5757e5dd7070Spatricklisted in the ``callback`` metadata. 5758e5dd7070Spatrick 5759ec727ea7SpatrickExample encodings for the callback performed by ``pthread_create`` are shown 5760e5dd7070Spatrickbelow. The explicit attribute annotation indicates that the third parameter 5761ec727ea7Spatrick(``start_routine``) is called zero or more times by the ``pthread_create`` function, 5762ec727ea7Spatrickand that the fourth parameter (``arg``) is passed along. Note that the callback 5763ec727ea7Spatrickbehavior of ``pthread_create`` is automatically recognized by Clang. In addition, 5764ec727ea7Spatrickthe declarations of ``__kmpc_fork_teams`` and ``__kmpc_fork_call``, generated for 5765ec727ea7Spatrick``#pragma omp target teams`` and ``#pragma omp parallel``, respectively, are also 5766e5dd7070Spatrickautomatically recognized as broker functions. Further functions might be added 5767e5dd7070Spatrickin the future. 5768e5dd7070Spatrick 5769e5dd7070Spatrick .. code-block:: c 5770e5dd7070Spatrick 5771e5dd7070Spatrick __attribute__((callback (start_routine, arg))) 5772e5dd7070Spatrick int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 5773e5dd7070Spatrick void *(*start_routine) (void *), void *arg); 5774e5dd7070Spatrick 5775e5dd7070Spatrick __attribute__((callback (3, 4))) 5776e5dd7070Spatrick int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 5777e5dd7070Spatrick void *(*start_routine) (void *), void *arg); 5778e5dd7070Spatrick 5779e5dd7070Spatrick }]; 5780e5dd7070Spatrick} 5781e5dd7070Spatrick 5782a9ac8606Spatrickdef CalledOnceDocs : Documentation { 5783a9ac8606Spatrick let Category = DocCatVariable; 5784a9ac8606Spatrick let Content = [{ 5785a9ac8606SpatrickThe ``called_once`` attribute specifies that the annotated function or method 5786a9ac8606Spatrickparameter is invoked exactly once on all execution paths. It only applies 5787a9ac8606Spatrickto parameters with function-like types, i.e. function pointers or blocks. This 5788a9ac8606Spatrickconcept is particularly useful for asynchronous programs. 5789a9ac8606Spatrick 5790a9ac8606SpatrickClang implements a check for ``called_once`` parameters, 5791a9ac8606Spatrick``-Wcalled-once-parameter``. It is on by default and finds the following 5792a9ac8606Spatrickviolations: 5793a9ac8606Spatrick 5794a9ac8606Spatrick* Parameter is not called at all. 5795a9ac8606Spatrick 5796a9ac8606Spatrick* Parameter is called more than once. 5797a9ac8606Spatrick 5798a9ac8606Spatrick* Parameter is not called on one of the execution paths. 5799a9ac8606Spatrick 5800a9ac8606SpatrickIn the latter case, Clang pinpoints the path where parameter is not invoked 5801a9ac8606Spatrickby showing the control-flow statement where the path diverges. 5802a9ac8606Spatrick 5803a9ac8606Spatrick.. code-block:: objc 5804a9ac8606Spatrick 5805a9ac8606Spatrick void fooWithCallback(void (^callback)(void) __attribute__((called_once))) { 5806a9ac8606Spatrick if (somePredicate()) { 5807a9ac8606Spatrick ... 5808a9ac8606Spatrick callback(); 5809*12c85518Srobert } else { 5810a9ac8606Spatrick callback(); // OK: callback is called on every path 5811a9ac8606Spatrick } 5812a9ac8606Spatrick } 5813a9ac8606Spatrick 5814a9ac8606Spatrick void barWithCallback(void (^callback)(void) __attribute__((called_once))) { 5815a9ac8606Spatrick if (somePredicate()) { 5816a9ac8606Spatrick ... 5817a9ac8606Spatrick callback(); // note: previous call is here 5818a9ac8606Spatrick } 5819a9ac8606Spatrick callback(); // warning: callback is called twice 5820a9ac8606Spatrick } 5821a9ac8606Spatrick 5822a9ac8606Spatrick void foobarWithCallback(void (^callback)(void) __attribute__((called_once))) { 5823a9ac8606Spatrick if (somePredicate()) { // warning: callback is not called when condition is false 5824a9ac8606Spatrick ... 5825a9ac8606Spatrick callback(); 5826a9ac8606Spatrick } 5827a9ac8606Spatrick } 5828a9ac8606Spatrick 5829a9ac8606SpatrickThis attribute is useful for API developers who want to double-check if they 5830a9ac8606Spatrickimplemented their method correctly. 5831a9ac8606Spatrick 5832a9ac8606Spatrick }]; 5833a9ac8606Spatrick} 5834a9ac8606Spatrick 5835e5dd7070Spatrickdef GnuInlineDocs : Documentation { 5836e5dd7070Spatrick let Category = DocCatFunction; 5837e5dd7070Spatrick let Content = [{ 5838e5dd7070SpatrickThe ``gnu_inline`` changes the meaning of ``extern inline`` to use GNU inline 5839e5dd7070Spatricksemantics, meaning: 5840e5dd7070Spatrick 5841e5dd7070Spatrick* If any declaration that is declared ``inline`` is not declared ``extern``, 5842e5dd7070Spatrick then the ``inline`` keyword is just a hint. In particular, an out-of-line 5843e5dd7070Spatrick definition is still emitted for a function with external linkage, even if all 5844e5dd7070Spatrick call sites are inlined, unlike in C99 and C++ inline semantics. 5845e5dd7070Spatrick 5846e5dd7070Spatrick* If all declarations that are declared ``inline`` are also declared 5847e5dd7070Spatrick ``extern``, then the function body is present only for inlining and no 5848e5dd7070Spatrick out-of-line version is emitted. 5849e5dd7070Spatrick 5850e5dd7070SpatrickSome important consequences: ``static inline`` emits an out-of-line 5851e5dd7070Spatrickversion if needed, a plain ``inline`` definition emits an out-of-line version 5852e5dd7070Spatrickalways, and an ``extern inline`` definition (in a header) followed by a 5853e5dd7070Spatrick(non-``extern``) ``inline`` declaration in a source file emits an out-of-line 5854e5dd7070Spatrickversion of the function in that source file but provides the function body for 5855e5dd7070Spatrickinlining to all includers of the header. 5856e5dd7070Spatrick 5857e5dd7070SpatrickEither ``__GNUC_GNU_INLINE__`` (GNU inline semantics) or 5858e5dd7070Spatrick``__GNUC_STDC_INLINE__`` (C99 semantics) will be defined (they are mutually 5859e5dd7070Spatrickexclusive). If ``__GNUC_STDC_INLINE__`` is defined, then the ``gnu_inline`` 5860e5dd7070Spatrickfunction attribute can be used to get GNU inline semantics on a per function 5861e5dd7070Spatrickbasis. If ``__GNUC_GNU_INLINE__`` is defined, then the translation unit is 5862e5dd7070Spatrickalready being compiled with GNU inline semantics as the implied default. It is 5863e5dd7070Spatrickunspecified which macro is defined in a C++ compilation. 5864e5dd7070Spatrick 5865e5dd7070SpatrickGNU inline semantics are the default behavior with ``-std=gnu89``, 5866e5dd7070Spatrick``-std=c89``, ``-std=c94``, or ``-fgnu89-inline``. 5867e5dd7070Spatrick }]; 5868e5dd7070Spatrick} 5869e5dd7070Spatrick 5870e5dd7070Spatrickdef SpeculativeLoadHardeningDocs : Documentation { 5871e5dd7070Spatrick let Category = DocCatFunction; 5872e5dd7070Spatrick let Content = [{ 5873e5dd7070Spatrick This attribute can be applied to a function declaration in order to indicate 5874e5dd7070Spatrick that `Speculative Load Hardening <https://llvm.org/docs/SpeculativeLoadHardening.html>`_ 5875e5dd7070Spatrick should be enabled for the function body. This can also be applied to a method 5876e5dd7070Spatrick in Objective C. This attribute will take precedence over the command line flag in 5877e5dd7070Spatrick the case where `-mno-speculative-load-hardening <https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mspeculative-load-hardening>`_ is specified. 5878e5dd7070Spatrick 5879e5dd7070Spatrick Speculative Load Hardening is a best-effort mitigation against 5880e5dd7070Spatrick information leak attacks that make use of control flow 5881e5dd7070Spatrick miss-speculation - specifically miss-speculation of whether a branch 5882e5dd7070Spatrick is taken or not. Typically vulnerabilities enabling such attacks are 5883e5dd7070Spatrick classified as "Spectre variant #1". Notably, this does not attempt to 5884e5dd7070Spatrick mitigate against miss-speculation of branch target, classified as 5885e5dd7070Spatrick "Spectre variant #2" vulnerabilities. 5886e5dd7070Spatrick 5887e5dd7070Spatrick When inlining, the attribute is sticky. Inlining a function that 5888e5dd7070Spatrick carries this attribute will cause the caller to gain the 5889e5dd7070Spatrick attribute. This is intended to provide a maximally conservative model 5890e5dd7070Spatrick where the code in a function annotated with this attribute will always 5891e5dd7070Spatrick (even after inlining) end up hardened. 5892e5dd7070Spatrick }]; 5893e5dd7070Spatrick} 5894e5dd7070Spatrick 5895e5dd7070Spatrickdef NoSpeculativeLoadHardeningDocs : Documentation { 5896e5dd7070Spatrick let Category = DocCatFunction; 5897e5dd7070Spatrick let Content = [{ 5898e5dd7070Spatrick This attribute can be applied to a function declaration in order to indicate 5899e5dd7070Spatrick that `Speculative Load Hardening <https://llvm.org/docs/SpeculativeLoadHardening.html>`_ 5900e5dd7070Spatrick is *not* needed for the function body. This can also be applied to a method 5901e5dd7070Spatrick in Objective C. This attribute will take precedence over the command line flag in 5902e5dd7070Spatrick the case where `-mspeculative-load-hardening <https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mspeculative-load-hardening>`_ is specified. 5903e5dd7070Spatrick 5904e5dd7070Spatrick Warning: This attribute may not prevent Speculative Load Hardening from being 5905e5dd7070Spatrick enabled for a function which inlines a function that has the 5906e5dd7070Spatrick 'speculative_load_hardening' attribute. This is intended to provide a 5907e5dd7070Spatrick maximally conservative model where the code that is marked with the 5908e5dd7070Spatrick 'speculative_load_hardening' attribute will always (even when inlined) 5909e5dd7070Spatrick be hardened. A user of this attribute may want to mark functions called by 5910e5dd7070Spatrick a function they do not want to be hardened with the 'noinline' attribute. 5911e5dd7070Spatrick 5912e5dd7070Spatrick For example: 5913e5dd7070Spatrick 5914e5dd7070Spatrick .. code-block:: c 5915e5dd7070Spatrick 5916e5dd7070Spatrick __attribute__((speculative_load_hardening)) 5917e5dd7070Spatrick int foo(int i) { 5918e5dd7070Spatrick return i; 5919e5dd7070Spatrick } 5920e5dd7070Spatrick 5921e5dd7070Spatrick // Note: bar() may still have speculative load hardening enabled if 5922e5dd7070Spatrick // foo() is inlined into bar(). Mark foo() with __attribute__((noinline)) 5923e5dd7070Spatrick // to avoid this situation. 5924e5dd7070Spatrick __attribute__((no_speculative_load_hardening)) 5925e5dd7070Spatrick int bar(int i) { 5926e5dd7070Spatrick return foo(i); 5927e5dd7070Spatrick } 5928e5dd7070Spatrick }]; 5929e5dd7070Spatrick} 5930e5dd7070Spatrick 5931e5dd7070Spatrickdef ObjCExternallyRetainedDocs : Documentation { 5932e5dd7070Spatrick let Category = DocCatVariable; 5933e5dd7070Spatrick let Content = [{ 5934e5dd7070SpatrickThe ``objc_externally_retained`` attribute can be applied to strong local 5935e5dd7070Spatrickvariables, functions, methods, or blocks to opt into 5936e5dd7070Spatrick`externally-retained semantics 5937e5dd7070Spatrick<https://clang.llvm.org/docs/AutomaticReferenceCounting.html#externally-retained-variables>`_. 5938e5dd7070Spatrick 5939e5dd7070SpatrickWhen applied to the definition of a function, method, or block, every parameter 5940e5dd7070Spatrickof the function with implicit strong retainable object pointer type is 5941e5dd7070Spatrickconsidered externally-retained, and becomes ``const``. By explicitly annotating 5942e5dd7070Spatricka parameter with ``__strong``, you can opt back into the default 5943ec727ea7Spatricknon-externally-retained behavior for that parameter. For instance, 5944e5dd7070Spatrick``first_param`` is externally-retained below, but not ``second_param``: 5945e5dd7070Spatrick 5946e5dd7070Spatrick.. code-block:: objc 5947e5dd7070Spatrick 5948e5dd7070Spatrick __attribute__((objc_externally_retained)) 5949e5dd7070Spatrick void f(NSArray *first_param, __strong NSArray *second_param) { 5950e5dd7070Spatrick // ... 5951e5dd7070Spatrick } 5952e5dd7070Spatrick 5953e5dd7070SpatrickLikewise, when applied to a strong local variable, that variable becomes 5954e5dd7070Spatrick``const`` and is considered externally-retained. 5955e5dd7070Spatrick 5956e5dd7070SpatrickWhen compiled without ``-fobjc-arc``, this attribute is ignored. 5957e5dd7070Spatrick}]; } 5958e5dd7070Spatrick 5959e5dd7070Spatrickdef MIGConventionDocs : Documentation { 5960e5dd7070Spatrick let Category = DocCatFunction; 5961e5dd7070Spatrick let Content = [{ 5962e5dd7070Spatrick The Mach Interface Generator release-on-success convention dictates 5963e5dd7070Spatrickfunctions that follow it to only release arguments passed to them when they 5964e5dd7070Spatrickreturn "success" (a ``kern_return_t`` error code that indicates that 5965ec727ea7Spatrickno errors have occurred). Otherwise the release is performed by the MIG client 5966e5dd7070Spatrickthat called the function. The annotation ``__attribute__((mig_server_routine))`` 5967e5dd7070Spatrickis applied in order to specify which functions are expected to follow the 5968e5dd7070Spatrickconvention. This allows the Static Analyzer to find bugs caused by violations of 5969e5dd7070Spatrickthat convention. The attribute would normally appear on the forward declaration 5970e5dd7070Spatrickof the actual server routine in the MIG server header, but it may also be 5971e5dd7070Spatrickadded to arbitrary functions that need to follow the same convention - for 5972e5dd7070Spatrickexample, a user can add them to auxiliary functions called by the server routine 5973e5dd7070Spatrickthat have their return value of type ``kern_return_t`` unconditionally returned 5974e5dd7070Spatrickfrom the routine. The attribute can be applied to C++ methods, and in this case 5975e5dd7070Spatrickit will be automatically applied to overrides if the method is virtual. The 5976e5dd7070Spatrickattribute can also be written using C++11 syntax: ``[[mig::server_routine]]``. 5977e5dd7070Spatrick}]; 5978e5dd7070Spatrick} 5979e5dd7070Spatrick 5980*12c85518Srobertdef MinSizeDocs : Documentation { 5981*12c85518Srobert let Category = DocCatFunction; 5982*12c85518Srobert let Content = [{ 5983*12c85518SrobertThis function attribute indicates that optimization passes and code generator passes 5984*12c85518Srobertmake choices that keep the function code size as small as possible. Optimizations may 5985*12c85518Srobertalso sacrifice runtime performance in order to minimize the size of the generated code. 5986*12c85518Srobert }]; 5987*12c85518Srobert} 5988*12c85518Srobert 5989e5dd7070Spatrickdef MSAllocatorDocs : Documentation { 5990e5dd7070Spatrick let Category = DocCatFunction; 5991e5dd7070Spatrick let Content = [{ 5992e5dd7070SpatrickThe ``__declspec(allocator)`` attribute is applied to functions that allocate 5993e5dd7070Spatrickmemory, such as operator new in C++. When CodeView debug information is emitted 5994e5dd7070Spatrick(enabled by ``clang -gcodeview`` or ``clang-cl /Z7``), Clang will attempt to 5995e5dd7070Spatrickrecord the code offset of heap allocation call sites in the debug info. It will 5996e5dd7070Spatrickalso record the type being allocated using some local heuristics. The Visual 5997e5dd7070SpatrickStudio debugger uses this information to `profile memory usage`_. 5998e5dd7070Spatrick 5999e5dd7070Spatrick.. _profile memory usage: https://docs.microsoft.com/en-us/visualstudio/profiling/memory-usage 6000e5dd7070Spatrick 6001e5dd7070SpatrickThis attribute does not affect optimizations in any way, unlike GCC's 6002e5dd7070Spatrick``__attribute__((malloc))``. 6003e5dd7070Spatrick}]; 6004e5dd7070Spatrick} 6005e5dd7070Spatrick 6006e5dd7070Spatrickdef CFGuardDocs : Documentation { 6007e5dd7070Spatrick let Category = DocCatFunction; 6008e5dd7070Spatrick let Content = [{ 6009e5dd7070SpatrickCode can indicate CFG checks are not wanted with the ``__declspec(guard(nocf))`` 6010e5dd7070Spatrickattribute. This directs the compiler to not insert any CFG checks for the entire 6011e5dd7070Spatrickfunction. This approach is typically used only sparingly in specific situations 6012e5dd7070Spatrickwhere the programmer has manually inserted "CFG-equivalent" protection. The 6013e5dd7070Spatrickprogrammer knows that they are calling through some read-only function table 6014e5dd7070Spatrickwhose address is obtained through read-only memory references and for which the 6015e5dd7070Spatrickindex is masked to the function table limit. This approach may also be applied 6016e5dd7070Spatrickto small wrapper functions that are not inlined and that do nothing more than 6017e5dd7070Spatrickmake a call through a function pointer. Since incorrect usage of this directive 6018e5dd7070Spatrickcan compromise the security of CFG, the programmer must be very careful using 6019e5dd7070Spatrickthe directive. Typically, this usage is limited to very small functions that 6020e5dd7070Spatrickonly call one function. 6021e5dd7070Spatrick 6022e5dd7070Spatrick`Control Flow Guard documentation <https://docs.microsoft.com/en-us/windows/win32/secbp/pe-metadata>` 6023e5dd7070Spatrick}]; 6024e5dd7070Spatrick} 6025e5dd7070Spatrick 6026ec727ea7Spatrickdef CUDADeviceBuiltinSurfaceTypeDocs : Documentation { 6027e5dd7070Spatrick let Category = DocCatType; 6028e5dd7070Spatrick let Content = [{ 6029ec727ea7SpatrickThe ``device_builtin_surface_type`` attribute can be applied to a class 6030ec727ea7Spatricktemplate when declaring the surface reference. A surface reference variable 6031ec727ea7Spatrickcould be accessed on the host side and, on the device side, might be translated 6032ec727ea7Spatrickinto an internal surface object, which is established through surface bind and 6033ec727ea7Spatrickunbind runtime APIs. 6034ec727ea7Spatrick }]; 6035ec727ea7Spatrick} 6036ec727ea7Spatrick 6037ec727ea7Spatrickdef CUDADeviceBuiltinTextureTypeDocs : Documentation { 6038ec727ea7Spatrick let Category = DocCatType; 6039ec727ea7Spatrick let Content = [{ 6040ec727ea7SpatrickThe ``device_builtin_texture_type`` attribute can be applied to a class 6041ec727ea7Spatricktemplate when declaring the texture reference. A texture reference variable 6042ec727ea7Spatrickcould be accessed on the host side and, on the device side, might be translated 6043ec727ea7Spatrickinto an internal texture object, which is established through texture bind and 6044ec727ea7Spatrickunbind runtime APIs. 6045e5dd7070Spatrick }]; 6046e5dd7070Spatrick} 6047e5dd7070Spatrick 6048a9ac8606Spatrickdef HIPManagedAttrDocs : Documentation { 6049a9ac8606Spatrick let Category = DocCatDecl; 6050a9ac8606Spatrick let Content = [{ 6051a9ac8606SpatrickThe ``__managed__`` attribute can be applied to a global variable declaration in HIP. 6052a9ac8606SpatrickA managed variable is emitted as an undefined global symbol in the device binary and is 6053a9ac8606Spatrickregistered by ``__hipRegisterManagedVariable`` in init functions. The HIP runtime allocates 6054a9ac8606Spatrickmanaged memory and uses it to define the symbol when loading the device binary. 6055a9ac8606SpatrickA managed variable can be accessed in both device and host code. 6056a9ac8606Spatrick }]; 6057a9ac8606Spatrick} 6058a9ac8606Spatrick 6059e5dd7070Spatrickdef LifetimeOwnerDocs : Documentation { 6060e5dd7070Spatrick let Category = DocCatDecl; 6061e5dd7070Spatrick let Content = [{ 6062e5dd7070Spatrick.. Note:: This attribute is experimental and its effect on analysis is subject to change in 6063e5dd7070Spatrick a future version of clang. 6064e5dd7070Spatrick 6065e5dd7070SpatrickThe attribute ``[[gsl::Owner(T)]]`` applies to structs and classes that own an 6066e5dd7070Spatrickobject of type ``T``: 6067e5dd7070Spatrick 6068ec727ea7Spatrick.. code:: 6069e5dd7070Spatrick 6070e5dd7070Spatrick class [[gsl::Owner(int)]] IntOwner { 6071e5dd7070Spatrick private: 6072e5dd7070Spatrick int value; 6073e5dd7070Spatrick public: 6074e5dd7070Spatrick int *getInt() { return &value; } 6075e5dd7070Spatrick }; 6076e5dd7070Spatrick 6077e5dd7070SpatrickThe argument ``T`` is optional and is ignored. 6078e5dd7070SpatrickThis attribute may be used by analysis tools and has no effect on code 6079e5dd7070Spatrickgeneration. A ``void`` argument means that the class can own any type. 6080e5dd7070Spatrick 6081e5dd7070SpatrickSee Pointer_ for an example. 6082e5dd7070Spatrick}]; 6083e5dd7070Spatrick} 6084e5dd7070Spatrick 6085e5dd7070Spatrickdef LifetimePointerDocs : Documentation { 6086e5dd7070Spatrick let Category = DocCatDecl; 6087e5dd7070Spatrick let Content = [{ 6088e5dd7070Spatrick.. Note:: This attribute is experimental and its effect on analysis is subject to change in 6089e5dd7070Spatrick a future version of clang. 6090e5dd7070Spatrick 6091e5dd7070SpatrickThe attribute ``[[gsl::Pointer(T)]]`` applies to structs and classes that behave 6092e5dd7070Spatricklike pointers to an object of type ``T``: 6093e5dd7070Spatrick 6094ec727ea7Spatrick.. code:: 6095e5dd7070Spatrick 6096e5dd7070Spatrick class [[gsl::Pointer(int)]] IntPointer { 6097e5dd7070Spatrick private: 6098e5dd7070Spatrick int *valuePointer; 6099e5dd7070Spatrick public: 6100e5dd7070Spatrick int *getInt() { return &valuePointer; } 6101e5dd7070Spatrick }; 6102e5dd7070Spatrick 6103e5dd7070SpatrickThe argument ``T`` is optional and is ignored. 6104e5dd7070SpatrickThis attribute may be used by analysis tools and has no effect on code 6105e5dd7070Spatrickgeneration. A ``void`` argument means that the pointer can point to any type. 6106e5dd7070Spatrick 6107e5dd7070SpatrickExample: 6108e5dd7070SpatrickWhen constructing an instance of a class annotated like this (a Pointer) from 6109e5dd7070Spatrickan instance of a class annotated with ``[[gsl::Owner]]`` (an Owner), 6110e5dd7070Spatrickthen the analysis will consider the Pointer to point inside the Owner. 6111e5dd7070SpatrickWhen the Owner's lifetime ends, it will consider the Pointer to be dangling. 6112e5dd7070Spatrick 6113e5dd7070Spatrick.. code-block:: c++ 6114e5dd7070Spatrick 6115e5dd7070Spatrick int f() { 6116e5dd7070Spatrick IntPointer P; 6117e5dd7070Spatrick if (true) { 6118e5dd7070Spatrick IntOwner O(7); 6119e5dd7070Spatrick P = IntPointer(O); // P "points into" O 6120e5dd7070Spatrick } // P is dangling 6121e5dd7070Spatrick return P.get(); // error: Using a dangling Pointer. 6122e5dd7070Spatrick } 6123e5dd7070Spatrick 6124e5dd7070Spatrick}]; 6125e5dd7070Spatrick} 6126e5dd7070Spatrick 6127ec727ea7Spatrickdef ArmBuiltinAliasDocs : Documentation { 6128e5dd7070Spatrick let Category = DocCatFunction; 6129e5dd7070Spatrick let Content = [{ 6130ec727ea7SpatrickThis attribute is used in the implementation of the ACLE intrinsics. 6131ec727ea7SpatrickIt allows the intrinsic functions to 6132e5dd7070Spatrickbe declared using the names defined in ACLE, and still be recognized 6133e5dd7070Spatrickas clang builtins equivalent to the underlying name. For example, 6134e5dd7070Spatrick``arm_mve.h`` declares the function ``vaddq_u32`` with 6135e5dd7070Spatrick``__attribute__((__clang_arm_mve_alias(__builtin_arm_mve_vaddq_u32)))``, 6136e5dd7070Spatrickand similarly, one of the type-overloaded declarations of ``vaddq`` 6137e5dd7070Spatrickwill have the same attribute. This ensures that both functions are 6138e5dd7070Spatrickrecognized as that clang builtin, and in the latter case, the choice 6139e5dd7070Spatrickof which builtin to identify the function as can be deferred until 6140e5dd7070Spatrickafter overload resolution. 6141e5dd7070Spatrick 6142ec727ea7SpatrickThis attribute can only be used to set up the aliases for certain Arm 6143ec727ea7Spatrickintrinsic functions; it is intended for use only inside ``arm_*.h`` 6144e5dd7070Spatrickand is not a general mechanism for declaring arbitrary aliases for 6145e5dd7070Spatrickclang builtin functions. 6146a9ac8606Spatrick 6147a9ac8606SpatrickIn order to avoid duplicating the attribute definitions for similar 6148a9ac8606Spatrickpurpose for other architecture, there is a general form for the 6149a9ac8606Spatrickattribute `clang_builtin_alias`. 6150e5dd7070Spatrick }]; 6151e5dd7070Spatrick} 6152e5dd7070Spatrick 6153e5dd7070Spatrickdef NoBuiltinDocs : Documentation { 6154e5dd7070Spatrick let Category = DocCatFunction; 6155e5dd7070Spatrick let Content = [{ 6156e5dd7070SpatrickThe ``__attribute__((no_builtin))`` is similar to the ``-fno-builtin`` flag 6157e5dd7070Spatrickexcept it is specific to the body of a function. The attribute may also be 6158e5dd7070Spatrickapplied to a virtual function but has no effect on the behavior of overriding 6159e5dd7070Spatrickfunctions in a derived class. 6160e5dd7070Spatrick 6161e5dd7070SpatrickIt accepts one or more strings corresponding to the specific names of the 6162e5dd7070Spatrickbuiltins to disable (e.g. "memcpy", "memset"). 6163e5dd7070SpatrickIf the attribute is used without parameters it will disable all buitins at 6164e5dd7070Spatrickonce. 6165e5dd7070Spatrick 6166e5dd7070Spatrick.. code-block:: c++ 6167e5dd7070Spatrick 6168e5dd7070Spatrick // The compiler is not allowed to add any builtin to foo's body. 6169e5dd7070Spatrick void foo(char* data, size_t count) __attribute__((no_builtin)) { 6170e5dd7070Spatrick // The compiler is not allowed to convert the loop into 6171e5dd7070Spatrick // `__builtin_memset(data, 0xFE, count);`. 6172e5dd7070Spatrick for (size_t i = 0; i < count; ++i) 6173e5dd7070Spatrick data[i] = 0xFE; 6174e5dd7070Spatrick } 6175e5dd7070Spatrick 6176e5dd7070Spatrick // The compiler is not allowed to add the `memcpy` builtin to bar's body. 6177e5dd7070Spatrick void bar(char* data, size_t count) __attribute__((no_builtin("memcpy"))) { 6178e5dd7070Spatrick // The compiler is allowed to convert the loop into 6179e5dd7070Spatrick // `__builtin_memset(data, 0xFE, count);` but cannot generate any 6180e5dd7070Spatrick // `__builtin_memcpy` 6181e5dd7070Spatrick for (size_t i = 0; i < count; ++i) 6182e5dd7070Spatrick data[i] = 0xFE; 6183e5dd7070Spatrick } 6184e5dd7070Spatrick }]; 6185e5dd7070Spatrick} 6186e5dd7070Spatrick 6187a9ac8606Spatrickdef UsingIfExistsDocs : Documentation { 6188a9ac8606Spatrick let Category = DocCatDecl; 6189a9ac8606Spatrick let Content = [{ 6190a9ac8606SpatrickThe ``using_if_exists`` attribute applies to a using-declaration. It allows 6191a9ac8606Spatrickprogrammers to import a declaration that potentially does not exist, instead 6192a9ac8606Spatrickdeferring any errors to the point of use. For instance: 6193a9ac8606Spatrick 6194a9ac8606Spatrick.. code-block:: c++ 6195a9ac8606Spatrick 6196a9ac8606Spatrick namespace empty_namespace {}; 6197a9ac8606Spatrick __attribute__((using_if_exists)) 6198a9ac8606Spatrick using empty_namespace::does_not_exist; // no error! 6199a9ac8606Spatrick 6200a9ac8606Spatrick does_not_exist x; // error: use of unresolved 'using_if_exists' 6201a9ac8606Spatrick 6202*12c85518SrobertThe C++ spelling of the attribute (`[[clang::using_if_exists]]`) is also 6203a9ac8606Spatricksupported as a clang extension, since ISO C++ doesn't support attributes in this 6204a9ac8606Spatrickposition. If the entity referred to by the using-declaration is found by name 6205a9ac8606Spatricklookup, the attribute has no effect. This attribute is useful for libraries 6206a9ac8606Spatrick(primarily, libc++) that wish to redeclare a set of declarations in another 6207a9ac8606Spatricknamespace, when the availability of those declarations is difficult or 6208a9ac8606Spatrickimpossible to detect at compile time with the preprocessor. 6209a9ac8606Spatrick }]; 6210a9ac8606Spatrick} 6211a9ac8606Spatrick 6212e5dd7070Spatrickdef HandleDocs : DocumentationCategory<"Handle Attributes"> { 6213e5dd7070Spatrick let Content = [{ 6214e5dd7070SpatrickHandles are a way to identify resources like files, sockets, and processes. 6215e5dd7070SpatrickThey are more opaque than pointers and widely used in system programming. They 6216e5dd7070Spatrickhave similar risks such as never releasing a resource associated with a handle, 6217e5dd7070Spatrickattempting to use a handle that was already released, or trying to release a 6218e5dd7070Spatrickhandle twice. Using the annotations below it is possible to make the ownership 6219e5dd7070Spatrickof the handles clear: whose responsibility is to release them. They can also 6220e5dd7070Spatrickaid static analysis tools to find bugs. 6221e5dd7070Spatrick }]; 6222e5dd7070Spatrick} 6223e5dd7070Spatrick 6224e5dd7070Spatrickdef AcquireHandleDocs : Documentation { 6225e5dd7070Spatrick let Category = HandleDocs; 6226e5dd7070Spatrick let Content = [{ 6227e5dd7070SpatrickIf this annotation is on a function or a function type it is assumed to return 6228e5dd7070Spatricka new handle. In case this annotation is on an output parameter, 6229e5dd7070Spatrickthe function is assumed to fill the corresponding argument with a new 6230*12c85518Sroberthandle. The attribute requires a string literal argument which used to 6231*12c85518Srobertidentify the handle with later uses of ``use_handle`` or 6232*12c85518Srobert``release_handle``. 6233e5dd7070Spatrick 6234e5dd7070Spatrick.. code-block:: c++ 6235e5dd7070Spatrick 6236e5dd7070Spatrick // Output arguments from Zircon. 6237e5dd7070Spatrick zx_status_t zx_socket_create(uint32_t options, 6238*12c85518Srobert zx_handle_t __attribute__((acquire_handle("zircon"))) * out0, 6239*12c85518Srobert zx_handle_t* out1 [[clang::acquire_handle("zircon")]]); 6240e5dd7070Spatrick 6241e5dd7070Spatrick 6242e5dd7070Spatrick // Returned handle. 6243*12c85518Srobert [[clang::acquire_handle("tag")]] int open(const char *path, int oflag, ... ); 6244*12c85518Srobert int open(const char *path, int oflag, ... ) __attribute__((acquire_handle("tag"))); 6245e5dd7070Spatrick }]; 6246e5dd7070Spatrick} 6247e5dd7070Spatrick 6248e5dd7070Spatrickdef UseHandleDocs : Documentation { 6249e5dd7070Spatrick let Category = HandleDocs; 6250e5dd7070Spatrick let Content = [{ 6251e5dd7070SpatrickA function taking a handle by value might close the handle. If a function 6252*12c85518Srobertparameter is annotated with ``use_handle(tag)`` it is assumed to not to change 6253e5dd7070Spatrickthe state of the handle. It is also assumed to require an open handle to work with. 6254*12c85518SrobertThe attribute requires a string literal argument to identify the handle being used. 6255e5dd7070Spatrick 6256e5dd7070Spatrick.. code-block:: c++ 6257e5dd7070Spatrick 6258*12c85518Srobert zx_status_t zx_port_wait(zx_handle_t handle [[clang::use_handle("zircon")]], 6259e5dd7070Spatrick zx_time_t deadline, 6260e5dd7070Spatrick zx_port_packet_t* packet); 6261e5dd7070Spatrick }]; 6262e5dd7070Spatrick} 6263e5dd7070Spatrick 6264e5dd7070Spatrickdef ReleaseHandleDocs : Documentation { 6265e5dd7070Spatrick let Category = HandleDocs; 6266e5dd7070Spatrick let Content = [{ 6267*12c85518SrobertIf a function parameter is annotated with ``release_handle(tag)`` it is assumed to 6268*12c85518Srobertclose the handle. It is also assumed to require an open handle to work with. The 6269*12c85518Srobertattribute requires a string literal argument to identify the handle being released. 6270e5dd7070Spatrick 6271e5dd7070Spatrick.. code-block:: c++ 6272e5dd7070Spatrick 6273*12c85518Srobert zx_status_t zx_handle_close(zx_handle_t handle [[clang::release_handle("tag")]]); 6274*12c85518Srobert }]; 6275*12c85518Srobert} 6276*12c85518Srobert 6277*12c85518Srobertdef DiagnoseAsBuiltinDocs : Documentation { 6278*12c85518Srobert let Category = DocCatFunction; 6279*12c85518Srobert let Content = [{ 6280*12c85518SrobertThe ``diagnose_as_builtin`` attribute indicates that Fortify diagnostics are to 6281*12c85518Srobertbe applied to the declared function as if it were the function specified by the 6282*12c85518Srobertattribute. The builtin function whose diagnostics are to be mimicked should be 6283*12c85518Srobertgiven. In addition, the order in which arguments should be applied must also 6284*12c85518Srobertbe given. 6285*12c85518Srobert 6286*12c85518SrobertFor example, the attribute can be used as follows. 6287*12c85518Srobert 6288*12c85518Srobert.. code-block:: c 6289*12c85518Srobert 6290*12c85518Srobert __attribute__((diagnose_as_builtin(__builtin_memset, 3, 2, 1))) 6291*12c85518Srobert void *mymemset(int n, int c, void *s) { 6292*12c85518Srobert // ... 6293*12c85518Srobert } 6294*12c85518Srobert 6295*12c85518SrobertThis indicates that calls to ``mymemset`` should be diagnosed as if they were 6296*12c85518Srobertcalls to ``__builtin_memset``. The arguments ``3, 2, 1`` indicate by index the 6297*12c85518Srobertorder in which arguments of ``mymemset`` should be applied to 6298*12c85518Srobert``__builtin_memset``. The third argument should be applied first, then the 6299*12c85518Srobertsecond, and then the first. Thus (when Fortify warnings are enabled) the call 6300*12c85518Srobert``mymemset(n, c, s)`` will diagnose overflows as if it were the call 6301*12c85518Srobert``__builtin_memset(s, c, n)``. 6302*12c85518Srobert 6303*12c85518SrobertFor variadic functions, the variadic arguments must come in the same order as 6304*12c85518Srobertthey would to the builtin function, after all normal arguments. For instance, 6305*12c85518Srobertto diagnose a new function as if it were `sscanf`, we can use the attribute as 6306*12c85518Srobertfollows. 6307*12c85518Srobert 6308*12c85518Srobert.. code-block:: c 6309*12c85518Srobert 6310*12c85518Srobert __attribute__((diagnose_as_builtin(sscanf, 1, 2))) 6311*12c85518Srobert int mysscanf(const char *str, const char *format, ...) { 6312*12c85518Srobert // ... 6313*12c85518Srobert } 6314*12c85518Srobert 6315*12c85518SrobertThen the call `mysscanf("abc def", "%4s %4s", buf1, buf2)` will be diagnosed as 6316*12c85518Srobertif it were the call `sscanf("abc def", "%4s %4s", buf1, buf2)`. 6317*12c85518Srobert 6318*12c85518SrobertThis attribute cannot be applied to non-static member functions. 6319e5dd7070Spatrick}]; 6320e5dd7070Spatrick} 6321ec727ea7Spatrick 6322a9ac8606Spatrickdef ArmSveVectorBitsDocs : Documentation { 6323a9ac8606Spatrick let Category = DocCatType; 6324a9ac8606Spatrick let Content = [{ 6325a9ac8606SpatrickThe ``arm_sve_vector_bits(N)`` attribute is defined by the Arm C Language 6326a9ac8606SpatrickExtensions (ACLE) for SVE. It is used to define fixed-length (VLST) variants of 6327a9ac8606Spatricksizeless types (VLAT). 6328a9ac8606Spatrick 6329a9ac8606SpatrickFor example: 6330a9ac8606Spatrick 6331a9ac8606Spatrick.. code-block:: c 6332a9ac8606Spatrick 6333a9ac8606Spatrick #include <arm_sve.h> 6334a9ac8606Spatrick 6335a9ac8606Spatrick #if __ARM_FEATURE_SVE_BITS==512 6336a9ac8606Spatrick typedef svint32_t fixed_svint32_t __attribute__((arm_sve_vector_bits(512))); 6337a9ac8606Spatrick #endif 6338a9ac8606Spatrick 6339a9ac8606SpatrickCreates a type ``fixed_svint32_t`` that is a fixed-length variant of 6340a9ac8606Spatrick``svint32_t`` that contains exactly 512-bits. Unlike ``svint32_t``, this type 6341a9ac8606Spatrickcan be used in globals, structs, unions, and arrays, all of which are 6342a9ac8606Spatrickunsupported for sizeless types. 6343a9ac8606Spatrick 6344a9ac8606SpatrickThe attribute can be attached to a single SVE vector (such as ``svint32_t``) or 6345a9ac8606Spatrickto the SVE predicate type ``svbool_t``, this excludes tuple types such as 6346a9ac8606Spatrick``svint32x4_t``. The behavior of the attribute is undefined unless 6347a9ac8606Spatrick``N==__ARM_FEATURE_SVE_BITS``, the implementation defined feature macro that is 6348a9ac8606Spatrickenabled under the ``-msve-vector-bits`` flag. 6349a9ac8606Spatrick 6350a9ac8606SpatrickFor more information See `Arm C Language Extensions for SVE 6351a9ac8606Spatrick<https://developer.arm.com/documentation/100987/latest>`_ for more information. 6352a9ac8606Spatrick}]; 6353a9ac8606Spatrick} 6354a9ac8606Spatrick 6355ec727ea7Spatrickdef ArmMveStrictPolymorphismDocs : Documentation { 6356ec727ea7Spatrick let Category = DocCatType; 6357ec727ea7Spatrick let Content = [{ 6358ec727ea7SpatrickThis attribute is used in the implementation of the ACLE intrinsics for the Arm 6359ec727ea7SpatrickMVE instruction set. It is used to define the vector types used by the MVE 6360ec727ea7Spatrickintrinsics. 6361ec727ea7Spatrick 6362ec727ea7SpatrickIts effect is to modify the behavior of a vector type with respect to function 6363ec727ea7Spatrickoverloading. If a candidate function for overload resolution has a parameter 6364ec727ea7Spatricktype with this attribute, then the selection of that candidate function will be 6365ec727ea7Spatrickdisallowed if the actual argument can only be converted via a lax vector 6366ec727ea7Spatrickconversion. The aim is to prevent spurious ambiguity in ARM MVE polymorphic 6367ec727ea7Spatrickintrinsics. 6368ec727ea7Spatrick 6369ec727ea7Spatrick.. code-block:: c++ 6370ec727ea7Spatrick 6371ec727ea7Spatrick void overloaded(uint16x8_t vector, uint16_t scalar); 6372ec727ea7Spatrick void overloaded(int32x4_t vector, int32_t scalar); 6373ec727ea7Spatrick uint16x8_t myVector; 6374ec727ea7Spatrick uint16_t myScalar; 6375ec727ea7Spatrick 6376ec727ea7Spatrick // myScalar is promoted to int32_t as a side effect of the addition, 6377ec727ea7Spatrick // so if lax vector conversions are considered for myVector, then 6378ec727ea7Spatrick // the two overloads are equally good (one argument conversion 6379ec727ea7Spatrick // each). But if the vector has the __clang_arm_mve_strict_polymorphism 6380ec727ea7Spatrick // attribute, only the uint16x8_t,uint16_t overload will match. 6381ec727ea7Spatrick overloaded(myVector, myScalar + 1); 6382ec727ea7Spatrick 6383ec727ea7SpatrickHowever, this attribute does not prohibit lax vector conversions in contexts 6384ec727ea7Spatrickother than overloading. 6385ec727ea7Spatrick 6386ec727ea7Spatrick.. code-block:: c++ 6387ec727ea7Spatrick 6388ec727ea7Spatrick uint16x8_t function(); 6389ec727ea7Spatrick 6390ec727ea7Spatrick // This is still permitted with lax vector conversion enabled, even 6391ec727ea7Spatrick // if the vector types have __clang_arm_mve_strict_polymorphism 6392ec727ea7Spatrick int32x4_t result = function(); 6393ec727ea7Spatrick 6394ec727ea7Spatrick }]; 6395ec727ea7Spatrick} 6396ec727ea7Spatrick 6397ec727ea7Spatrickdef ArmCmseNSCallDocs : Documentation { 6398ec727ea7Spatrick let Category = DocCatType; 6399ec727ea7Spatrick let Content = [{ 6400ec727ea7SpatrickThis attribute declares a non-secure function type. When compiling for secure 6401ec727ea7Spatrickstate, a call to such a function would switch from secure to non-secure state. 6402ec727ea7SpatrickAll non-secure function calls must happen only through a function pointer, and 6403ec727ea7Spatricka non-secure function type should only be used as a base type of a pointer. 6404ec727ea7SpatrickSee `ARMv8-M Security Extensions: Requirements on Development 6405ec727ea7SpatrickTools - Engineering Specification Documentation 6406ec727ea7Spatrick<https://developer.arm.com/docs/ecm0359818/latest/>`_ for more information. 6407ec727ea7Spatrick }]; 6408ec727ea7Spatrick} 6409ec727ea7Spatrick 6410ec727ea7Spatrickdef ArmCmseNSEntryDocs : Documentation { 6411ec727ea7Spatrick let Category = DocCatFunction; 6412ec727ea7Spatrick let Content = [{ 6413ec727ea7SpatrickThis attribute declares a function that can be called from non-secure state, or 6414ec727ea7Spatrickfrom secure state. Entering from and returning to non-secure state would switch 6415ec727ea7Spatrickto and from secure state, respectively, and prevent flow of information 6416ec727ea7Spatrickto non-secure state, except via return values. See `ARMv8-M Security Extensions: 6417ec727ea7SpatrickRequirements on Development Tools - Engineering Specification Documentation 6418ec727ea7Spatrick<https://developer.arm.com/docs/ecm0359818/latest/>`_ for more information. 6419ec727ea7Spatrick }]; 6420ec727ea7Spatrick} 6421a9ac8606Spatrick 6422a9ac8606Spatrickdef AlwaysInlineDocs : Documentation { 6423a9ac8606Spatrick let Category = DocCatFunction; 6424a9ac8606Spatrick let Content = [{ 6425a9ac8606SpatrickInlining heuristics are disabled and inlining is always attempted regardless of 6426a9ac8606Spatrickoptimization level. 6427a9ac8606Spatrick 6428*12c85518Srobert``[[clang::always_inline]]`` spelling can be used as a statement attribute; other 6429*12c85518Srobertspellings of the attribute are not supported on statements. If a statement is 6430*12c85518Srobertmarked ``[[clang::always_inline]]`` and contains calls, the compiler attempts 6431*12c85518Srobertto inline those calls. 6432*12c85518Srobert 6433*12c85518Srobert.. code-block:: c 6434*12c85518Srobert 6435*12c85518Srobert int example(void) { 6436*12c85518Srobert int i; 6437*12c85518Srobert [[clang::always_inline]] foo(); // attempts to inline foo 6438*12c85518Srobert [[clang::always_inline]] i = bar(); // attempts to inline bar 6439*12c85518Srobert [[clang::always_inline]] return f(42, baz(bar())); // attempts to inline everything 6440*12c85518Srobert } 6441*12c85518Srobert 6442*12c85518SrobertA declaration statement, which is a statement, is not a statement that can have an 6443*12c85518Srobertattribute associated with it (the attribute applies to the declaration, not the 6444*12c85518Srobertstatement in that case). So this use case will not work: 6445*12c85518Srobert 6446*12c85518Srobert.. code-block:: c 6447*12c85518Srobert 6448*12c85518Srobert int example(void) { 6449*12c85518Srobert [[clang::always_inline]] int i = bar(); 6450*12c85518Srobert return i; 6451*12c85518Srobert } 6452*12c85518Srobert 6453*12c85518SrobertThis attribute does not guarantee that inline substitution actually occurs. 6454*12c85518Srobert 6455*12c85518Srobert<ins>Note: applying this attribute to a coroutine at the `-O0` optimization level 6456*12c85518Sroberthas no effect; other optimization levels may only partially inline and result in a 6457*12c85518Srobertdiagnostic.</ins> 6458a9ac8606Spatrick 6459a9ac8606SpatrickSee also `the Microsoft Docs on Inline Functions`_, `the GCC Common Function 6460a9ac8606SpatrickAttribute docs`_, and `the GCC Inline docs`_. 6461a9ac8606Spatrick 6462a9ac8606Spatrick.. _the Microsoft Docs on Inline Functions: https://docs.microsoft.com/en-us/cpp/cpp/inline-functions-cpp 6463a9ac8606Spatrick.. _the GCC Common Function Attribute docs: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html 6464a9ac8606Spatrick.. _the GCC Inline docs: https://gcc.gnu.org/onlinedocs/gcc/Inline.html 6465a9ac8606Spatrick 6466a9ac8606Spatrick}]; 6467a9ac8606Spatrick let Heading = "always_inline, __force_inline"; 6468a9ac8606Spatrick} 6469a9ac8606Spatrick 6470a9ac8606Spatrickdef EnforceTCBDocs : Documentation { 6471a9ac8606Spatrick let Category = DocCatFunction; 6472a9ac8606Spatrick let Content = [{ 6473a9ac8606Spatrick The ``enforce_tcb`` attribute can be placed on functions to enforce that a 6474a9ac8606Spatrick trusted compute base (TCB) does not call out of the TCB. This generates a 6475a9ac8606Spatrick warning every time a function not marked with an ``enforce_tcb`` attribute is 6476a9ac8606Spatrick called from a function with the ``enforce_tcb`` attribute. A function may be a 6477a9ac8606Spatrick part of multiple TCBs. Invocations through function pointers are currently 6478a9ac8606Spatrick not checked. Builtins are considered to a part of every TCB. 6479a9ac8606Spatrick 6480a9ac8606Spatrick - ``enforce_tcb(Name)`` indicates that this function is a part of the TCB named ``Name`` 6481a9ac8606Spatrick }]; 6482a9ac8606Spatrick} 6483a9ac8606Spatrick 6484a9ac8606Spatrickdef EnforceTCBLeafDocs : Documentation { 6485a9ac8606Spatrick let Category = DocCatFunction; 6486a9ac8606Spatrick let Content = [{ 6487a9ac8606Spatrick The ``enforce_tcb_leaf`` attribute satisfies the requirement enforced by 6488a9ac8606Spatrick ``enforce_tcb`` for the marked function to be in the named TCB but does not 6489a9ac8606Spatrick continue to check the functions called from within the leaf function. 6490a9ac8606Spatrick 6491a9ac8606Spatrick - ``enforce_tcb_leaf(Name)`` indicates that this function is a part of the TCB named ``Name`` 6492a9ac8606Spatrick }]; 6493a9ac8606Spatrick} 6494*12c85518Srobert 6495*12c85518Srobertdef ErrorAttrDocs : Documentation { 6496*12c85518Srobert let Category = DocCatFunction; 6497*12c85518Srobert let Heading = "error, warning"; 6498*12c85518Srobert let Content = [{ 6499*12c85518SrobertThe ``error`` and ``warning`` function attributes can be used to specify a 6500*12c85518Srobertcustom diagnostic to be emitted when a call to such a function is not 6501*12c85518Sroberteliminated via optimizations. This can be used to create compile time 6502*12c85518Srobertassertions that depend on optimizations, while providing diagnostics 6503*12c85518Srobertpointing to precise locations of the call site in the source. 6504*12c85518Srobert 6505*12c85518Srobert.. code-block:: c++ 6506*12c85518Srobert 6507*12c85518Srobert __attribute__((warning("oh no"))) void dontcall(); 6508*12c85518Srobert void foo() { 6509*12c85518Srobert if (someCompileTimeAssertionThatsTrue) 6510*12c85518Srobert dontcall(); // Warning 6511*12c85518Srobert 6512*12c85518Srobert dontcall(); // Warning 6513*12c85518Srobert 6514*12c85518Srobert if (someCompileTimeAssertionThatsFalse) 6515*12c85518Srobert dontcall(); // No Warning 6516*12c85518Srobert sizeof(dontcall()); // No Warning 6517*12c85518Srobert } 6518*12c85518Srobert }]; 6519*12c85518Srobert} 6520*12c85518Srobert 6521*12c85518Srobertdef ZeroCallUsedRegsDocs : Documentation { 6522*12c85518Srobert let Category = DocCatFunction; 6523*12c85518Srobert let Content = [{ 6524*12c85518SrobertThis attribute, when attached to a function, causes the compiler to zero a 6525*12c85518Srobertsubset of all call-used registers before the function returns. It's used to 6526*12c85518Srobertincrease program security by either mitigating `Return-Oriented Programming`_ 6527*12c85518Srobert(ROP) attacks or preventing information leakage through registers. 6528*12c85518Srobert 6529*12c85518SrobertThe term "call-used" means registers which are not guaranteed to be preserved 6530*12c85518Srobertunchanged for the caller by the current calling convention. This could also be 6531*12c85518Srobertdescribed as "caller-saved" or "not callee-saved". 6532*12c85518Srobert 6533*12c85518SrobertThe `choice` parameters gives the programmer flexibility to choose the subset 6534*12c85518Srobertof the call-used registers to be zeroed: 6535*12c85518Srobert 6536*12c85518Srobert- ``skip`` doesn't zero any call-used registers. This choice overrides any 6537*12c85518Srobert command-line arguments. 6538*12c85518Srobert- ``used`` only zeros call-used registers used in the function. By ``used``, we 6539*12c85518Srobert mean a register whose contents have been set or referenced in the function. 6540*12c85518Srobert- ``used-gpr`` only zeros call-used GPR registers used in the function. 6541*12c85518Srobert- ``used-arg`` only zeros call-used registers used to pass arguments to the 6542*12c85518Srobert function. 6543*12c85518Srobert- ``used-gpr-arg`` only zeros call-used GPR registers used to pass arguments to 6544*12c85518Srobert the function. 6545*12c85518Srobert- ``all`` zeros all call-used registers. 6546*12c85518Srobert- ``all-gpr`` zeros all call-used GPR registers. 6547*12c85518Srobert- ``all-arg`` zeros all call-used registers used to pass arguments to the 6548*12c85518Srobert function. 6549*12c85518Srobert- ``all-gpr-arg`` zeros all call-used GPR registers used to pass arguments to 6550*12c85518Srobert the function. 6551*12c85518Srobert 6552*12c85518SrobertThe default for the attribute is controlled by the ``-fzero-call-used-regs`` 6553*12c85518Srobertflag. 6554*12c85518Srobert 6555*12c85518Srobert.. _Return-Oriented Programming: https://en.wikipedia.org/wiki/Return-oriented_programming 6556*12c85518Srobert }]; 6557*12c85518Srobert} 6558*12c85518Srobert 6559*12c85518Srobertdef NumThreadsDocs : Documentation { 6560*12c85518Srobert let Category = DocCatFunction; 6561*12c85518Srobert let Content = [{ 6562*12c85518SrobertThe ``numthreads`` attribute applies to HLSL shaders where explcit thread counts 6563*12c85518Srobertare required. The ``X``, ``Y``, and ``Z`` values provided to the attribute 6564*12c85518Srobertdictate the thread id. Total number of threads executed is ``X * Y * Z``. 6565*12c85518Srobert 6566*12c85518SrobertThe full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-attributes-numthreads 6567*12c85518Srobert }]; 6568*12c85518Srobert} 6569*12c85518Srobert 6570*12c85518Srobertdef HLSLSV_ShaderTypeAttrDocs : Documentation { 6571*12c85518Srobert let Category = DocCatFunction; 6572*12c85518Srobert let Content = [{ 6573*12c85518SrobertThe ``shader`` type attribute applies to HLSL shader entry functions to 6574*12c85518Srobertidentify the shader type for the entry function. 6575*12c85518SrobertThe syntax is: 6576*12c85518Srobert 6577*12c85518Srobert.. code-block:: text 6578*12c85518Srobert 6579*12c85518Srobert ``[shader(string-literal)]`` 6580*12c85518Srobert 6581*12c85518Srobertwhere the string literal is one of: "pixel", "vertex", "geometry", "hull", 6582*12c85518Srobert"domain", "compute", "raygeneration", "intersection", "anyhit", "closesthit", 6583*12c85518Srobert"miss", "callable", "mesh", "amplification". Normally the shader type is set 6584*12c85518Srobertby shader target with the ``-T`` option like ``-Tps_6_1``. When compiling to a 6585*12c85518Srobertlibrary target like ``lib_6_3``, the shader type attribute can help the 6586*12c85518Srobertcompiler to identify the shader type. It is mostly used by Raytracing shaders 6587*12c85518Srobertwhere shaders must be compiled into a library and linked at runtime. 6588*12c85518Srobert }]; 6589*12c85518Srobert} 6590*12c85518Srobert 6591*12c85518Srobertdef ClangRandomizeLayoutDocs : Documentation { 6592*12c85518Srobert let Category = DocCatDecl; 6593*12c85518Srobert let Heading = "randomize_layout, no_randomize_layout"; 6594*12c85518Srobert let Content = [{ 6595*12c85518SrobertThe attribute ``randomize_layout``, when attached to a C structure, selects it 6596*12c85518Srobertfor structure layout field randomization; a compile-time hardening technique. A 6597*12c85518Srobert"seed" value, is specified via the ``-frandomize-layout-seed=`` command line flag. 6598*12c85518SrobertFor example: 6599*12c85518Srobert 6600*12c85518Srobert.. code-block:: bash 6601*12c85518Srobert 6602*12c85518Srobert SEED=`od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n'` 6603*12c85518Srobert make ... CFLAGS="-frandomize-layout-seed=$SEED" ... 6604*12c85518Srobert 6605*12c85518SrobertYou can also supply the seed in a file with ``-frandomize-layout-seed-file=``. 6606*12c85518SrobertFor example: 6607*12c85518Srobert 6608*12c85518Srobert.. code-block:: bash 6609*12c85518Srobert 6610*12c85518Srobert od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n' > /tmp/seed_file.txt 6611*12c85518Srobert make ... CFLAGS="-frandomize-layout-seed-file=/tmp/seed_file.txt" ... 6612*12c85518Srobert 6613*12c85518SrobertThe randomization is deterministic based for a given seed, so the entire 6614*12c85518Srobertprogram should be compiled with the same seed, but keep the seed safe 6615*12c85518Srobertotherwise. 6616*12c85518Srobert 6617*12c85518SrobertThe attribute ``no_randomize_layout``, when attached to a C structure, 6618*12c85518Srobertinstructs the compiler that this structure should not have its field layout 6619*12c85518Srobertrandomized. 6620*12c85518Srobert }]; 6621*12c85518Srobert} 6622*12c85518Srobert 6623*12c85518Srobertdef HLSLSV_GroupIndexDocs : Documentation { 6624*12c85518Srobert let Category = DocCatFunction; 6625*12c85518Srobert let Content = [{ 6626*12c85518SrobertThe ``SV_GroupIndex`` semantic, when applied to an input parameter, specifies a 6627*12c85518Srobertdata binding to map the group index to the specified parameter. This attribute 6628*12c85518Srobertis only supported in compute shaders. 6629*12c85518Srobert 6630*12c85518SrobertThe full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-groupindex 6631*12c85518Srobert }]; 6632*12c85518Srobert} 6633*12c85518Srobert 6634*12c85518Srobertdef HLSLResourceBindingDocs : Documentation { 6635*12c85518Srobert let Category = DocCatFunction; 6636*12c85518Srobert let Content = [{ 6637*12c85518SrobertThe resource binding attribute sets the virtual register and logical register space for a resource. 6638*12c85518SrobertAttribute spelling in HLSL is: ``register(slot [, space])``. 6639*12c85518Srobert``slot`` takes the format ``[type][number]``, 6640*12c85518Srobertwhere ``type`` is a single character specifying the resource type and ``number`` is the virtual register number. 6641*12c85518Srobert 6642*12c85518SrobertRegister types are: 6643*12c85518Srobertt for shader resource views (SRV), 6644*12c85518Sroberts for samplers, 6645*12c85518Srobertu for unordered access views (UAV), 6646*12c85518Srobertb for constant buffer views (CBV). 6647*12c85518Srobert 6648*12c85518SrobertRegister space is specified in the format ``space[number]`` and defaults to ``space0`` if omitted. 6649*12c85518SrobertHere're resource binding examples with and without space: 6650*12c85518Srobert.. code-block:: c++ 6651*12c85518Srobert 6652*12c85518Srobert RWBuffer<float> Uav : register(u3, space1); 6653*12c85518Srobert Buffer<float> Buf : register(t1); 6654*12c85518Srobert 6655*12c85518SrobertThe full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3d12/resource-binding-in-hlsl 6656*12c85518Srobert }]; 6657*12c85518Srobert} 6658*12c85518Srobert 6659*12c85518Srobertdef HLSLSV_DispatchThreadIDDocs : Documentation { 6660*12c85518Srobert let Category = DocCatFunction; 6661*12c85518Srobert let Content = [{ 6662*12c85518SrobertThe ``SV_DispatchThreadID`` semantic, when applied to an input parameter, 6663*12c85518Srobertspecifies a data binding to map the global thread offset within the Dispatch 6664*12c85518Srobertcall (per dimension of the group) to the specified parameter. 6665*12c85518SrobertWhen applied to a field of a struct, the data binding is specified to the field 6666*12c85518Srobertwhen the struct is used as a parameter type. 6667*12c85518SrobertThe semantic on the field is ignored when not used as a parameter. 6668*12c85518SrobertThis attribute is only supported in compute shaders. 6669*12c85518Srobert 6670*12c85518SrobertThe full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-dispatchthreadid 6671*12c85518Srobert }]; 6672*12c85518Srobert} 6673*12c85518Srobert 6674*12c85518Srobertdef HLSLGroupSharedAddressSpaceDocs : Documentation { 6675*12c85518Srobert let Category = DocCatVariable; 6676*12c85518Srobert let Content = [{ 6677*12c85518SrobertHLSL enables threads of a compute shader to exchange values via shared memory. 6678*12c85518SrobertHLSL provides barrier primitives such as GroupMemoryBarrierWithGroupSync, 6679*12c85518Srobertand so on to ensure the correct ordering of reads and writes to shared memory 6680*12c85518Srobertin the shader and to avoid data races. 6681*12c85518SrobertHere's an example to declare a groupshared variable. 6682*12c85518Srobert.. code-block:: c++ 6683*12c85518Srobert 6684*12c85518Srobert groupshared GSData data[5*5*1]; 6685*12c85518Srobert 6686*12c85518SrobertThe full documentation is available here: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-variable-syntax#group-shared 6687*12c85518Srobert }]; 6688*12c85518Srobert} 6689*12c85518Srobert 6690*12c85518Srobertdef AnnotateTypeDocs : Documentation { 6691*12c85518Srobert let Category = DocCatType; 6692*12c85518Srobert let Heading = "annotate_type"; 6693*12c85518Srobert let Content = [{ 6694*12c85518SrobertThis attribute is used to add annotations to types, typically for use by static 6695*12c85518Srobertanalysis tools that are not integrated into the core Clang compiler (e.g., 6696*12c85518SrobertClang-Tidy checks or out-of-tree Clang-based tools). It is a counterpart to the 6697*12c85518Srobert`annotate` attribute, which serves the same purpose, but for declarations. 6698*12c85518Srobert 6699*12c85518SrobertThe attribute takes a mandatory string literal argument specifying the 6700*12c85518Srobertannotation category and an arbitrary number of optional arguments that provide 6701*12c85518Srobertadditional information specific to the annotation category. The optional 6702*12c85518Srobertarguments must be constant expressions of arbitrary type. 6703*12c85518Srobert 6704*12c85518SrobertFor example: 6705*12c85518Srobert 6706*12c85518Srobert.. code-block:: c++ 6707*12c85518Srobert 6708*12c85518Srobert int* [[clang::annotate_type("category1", "foo", 1)]] f(int[[clang::annotate_type("category2")]] *); 6709*12c85518Srobert 6710*12c85518SrobertThe attribute does not have any effect on the semantics of the type system, 6711*12c85518Srobertneither type checking rules, nor runtime semantics. In particular: 6712*12c85518Srobert 6713*12c85518Srobert- ``std::is_same<T, T [[clang::annotate_type("foo")]]>`` is true for all types 6714*12c85518Srobert ``T``. 6715*12c85518Srobert 6716*12c85518Srobert- It is not permissible for overloaded functions or template specializations 6717*12c85518Srobert to differ merely by an ``annotate_type`` attribute. 6718*12c85518Srobert 6719*12c85518Srobert- The presence of an ``annotate_type`` attribute will not affect name 6720*12c85518Srobert mangling. 6721*12c85518Srobert }]; 6722*12c85518Srobert} 6723*12c85518Srobert 6724*12c85518Srobertdef WeakDocs : Documentation { 6725*12c85518Srobert let Category = DocCatDecl; 6726*12c85518Srobert let Content = [{ 6727*12c85518Srobert 6728*12c85518SrobertIn supported output formats the ``weak`` attribute can be used to 6729*12c85518Srobertspecify that a variable or function should be emitted as a symbol with 6730*12c85518Srobert``weak`` (if a definition) or ``extern_weak`` (if a declaration of an 6731*12c85518Srobertexternal symbol) `linkage 6732*12c85518Srobert<https://llvm.org/docs/LangRef.html#linkage-types>`_. 6733*12c85518Srobert 6734*12c85518SrobertIf there is a non-weak definition of the symbol the linker will select 6735*12c85518Srobertthat over the weak. They must have same type and alignment (variables 6736*12c85518Srobertmust also have the same size), but may have a different value. 6737*12c85518Srobert 6738*12c85518SrobertIf there are multiple weak definitions of same symbol, but no non-weak 6739*12c85518Srobertdefinition, they should have same type, size, alignment and value, the 6740*12c85518Srobertlinker will select one of them (see also selectany_ attribute). 6741*12c85518Srobert 6742*12c85518SrobertIf the ``weak`` attribute is applied to a ``const`` qualified variable 6743*12c85518Srobertdefinition that variable is no longer consider a compiletime constant 6744*12c85518Srobertas its value can change during linking (or dynamic linking). This 6745*12c85518Srobertmeans that it can e.g no longer be part of an initializer expression. 6746*12c85518Srobert 6747*12c85518Srobert.. code-block:: c 6748*12c85518Srobert 6749*12c85518Srobert const int ANSWER __attribute__ ((weak)) = 42; 6750*12c85518Srobert 6751*12c85518Srobert /* This function may be replaced link-time */ 6752*12c85518Srobert __attribute__ ((weak)) void debug_log(const char *msg) 6753*12c85518Srobert { 6754*12c85518Srobert fprintf(stderr, "DEBUG: %s\n", msg); 6755*12c85518Srobert } 6756*12c85518Srobert 6757*12c85518Srobert int main(int argc, const char **argv) 6758*12c85518Srobert { 6759*12c85518Srobert debug_log ("Starting up..."); 6760*12c85518Srobert 6761*12c85518Srobert /* This may print something else than "6 * 7 = 42", 6762*12c85518Srobert if there is a non-weak definition of "ANSWER" in 6763*12c85518Srobert an object linked in */ 6764*12c85518Srobert printf("6 * 7 = %d\n", ANSWER); 6765*12c85518Srobert 6766*12c85518Srobert return 0; 6767*12c85518Srobert } 6768*12c85518Srobert 6769*12c85518SrobertIf an external declaration is marked weak and that symbol does not 6770*12c85518Srobertexist during linking (possibly dynamic) the address of the symbol will 6771*12c85518Srobertevaluate to NULL. 6772*12c85518Srobert 6773*12c85518Srobert.. code-block:: c 6774*12c85518Srobert 6775*12c85518Srobert void may_not_exist(void) __attribute__ ((weak)); 6776*12c85518Srobert 6777*12c85518Srobert int main(int argc, const char **argv) 6778*12c85518Srobert { 6779*12c85518Srobert if (may_not_exist) { 6780*12c85518Srobert may_not_exist(); 6781*12c85518Srobert } else { 6782*12c85518Srobert printf("Function did not exist\n"); 6783*12c85518Srobert } 6784*12c85518Srobert return 0; 6785*12c85518Srobert } 6786*12c85518Srobert }]; 6787*12c85518Srobert} 6788*12c85518Srobert 6789*12c85518Srobertdef FunctionReturnThunksDocs : Documentation { 6790*12c85518Srobert let Category = DocCatFunction; 6791*12c85518Srobert let Content = [{ 6792*12c85518SrobertThe attribute ``function_return`` can replace return instructions with jumps to 6793*12c85518Sroberttarget-specific symbols. This attribute supports 2 possible values, 6794*12c85518Srobertcorresponding to the values supported by the ``-mfunction-return=`` command 6795*12c85518Srobertline flag: 6796*12c85518Srobert 6797*12c85518Srobert* ``__attribute__((function_return("keep")))`` to disable related transforms. 6798*12c85518Srobert This is useful for undoing global setting from ``-mfunction-return=`` locally 6799*12c85518Srobert for individual functions. 6800*12c85518Srobert* ``__attribute__((function_return("thunk-extern")))`` to replace returns with 6801*12c85518Srobert jumps, while NOT emitting the thunk. 6802*12c85518Srobert 6803*12c85518SrobertThe values ``thunk`` and ``thunk-inline`` from GCC are not supported. 6804*12c85518Srobert 6805*12c85518SrobertThe symbol used for ``thunk-extern`` is target specific: 6806*12c85518Srobert* X86: ``__x86_return_thunk`` 6807*12c85518Srobert 6808*12c85518SrobertAs such, this function attribute is currently only supported on X86 targets. 6809*12c85518Srobert }]; 6810*12c85518Srobert} 6811*12c85518Srobert 6812*12c85518Srobertdef ReadOnlyPlacementDocs : Documentation { 6813*12c85518Srobert let Category = DocCatType; 6814*12c85518Srobert let Content = [{This attribute is attached to a structure, class or union declaration. 6815*12c85518Srobert When attached to a record declaration/definition, it checks if all instances 6816*12c85518Srobert of this type can be placed in the read-only data segment of the program. If it 6817*12c85518Srobert finds an instance that can not be placed in a read-only segment, the compiler 6818*12c85518Srobert emits a warning at the source location where the type was used. 6819*12c85518Srobert 6820*12c85518Srobert Examples: 6821*12c85518Srobert * ``struct __attribute__((enforce_read_only_placement)) Foo;`` 6822*12c85518Srobert * ``struct __attribute__((enforce_read_only_placement)) Bar { ... };`` 6823*12c85518Srobert 6824*12c85518Srobert Both ``Foo`` and ``Bar`` types have the ``enforce_read_only_placement`` attribute. 6825*12c85518Srobert 6826*12c85518Srobert The goal of introducing this attribute is to assist developers with writing secure 6827*12c85518Srobert code. A ``const``-qualified global is generally placed in the read-only section 6828*12c85518Srobert of the memory that has additional run time protection from malicious writes. By 6829*12c85518Srobert attaching this attribute to a declaration, the developer can express the intent 6830*12c85518Srobert to place all instances of the annotated type in the read-only program memory. 6831*12c85518Srobert 6832*12c85518Srobert Note 1: The attribute doesn't guarantee that the object will be placed in the 6833*12c85518Srobert read-only data segment as it does not instruct the compiler to ensure such 6834*12c85518Srobert a placement. It emits a warning if something in the code can be proven to prevent 6835*12c85518Srobert an instance from being placed in the read-only data segment. 6836*12c85518Srobert 6837*12c85518Srobert Note 2: Currently, clang only checks if all global declarations of a given type 'T' 6838*12c85518Srobert are ``const``-qualified. The following conditions would also prevent the data to be 6839*12c85518Srobert put into read only segment, but the corresponding warnings are not yet implemented. 6840*12c85518Srobert 6841*12c85518Srobert 1. An instance of type ``T`` is allocated on the heap/stack. 6842*12c85518Srobert 2. Type ``T`` defines/inherits a mutable field. 6843*12c85518Srobert 3. Type ``T`` defines/inherits non-constexpr constructor(s) for initialization. 6844*12c85518Srobert 4. A field of type ``T`` is defined by type ``Q``, which does not bear the 6845*12c85518Srobert ``enforce_read_only_placement`` attribute. 6846*12c85518Srobert 5. A type ``Q`` inherits from type ``T`` and it does not have the 6847*12c85518Srobert ``enforce_read_only_placement`` attribute. 6848*12c85518Srobert }]; 6849*12c85518Srobert} 6850