1*67e74705SXin Li========================= 2*67e74705SXin LiClang Language Extensions 3*67e74705SXin Li========================= 4*67e74705SXin Li 5*67e74705SXin Li.. contents:: 6*67e74705SXin Li :local: 7*67e74705SXin Li :depth: 1 8*67e74705SXin Li 9*67e74705SXin Li.. toctree:: 10*67e74705SXin Li :hidden: 11*67e74705SXin Li 12*67e74705SXin Li ObjectiveCLiterals 13*67e74705SXin Li BlockLanguageSpec 14*67e74705SXin Li Block-ABI-Apple 15*67e74705SXin Li AutomaticReferenceCounting 16*67e74705SXin Li 17*67e74705SXin LiIntroduction 18*67e74705SXin Li============ 19*67e74705SXin Li 20*67e74705SXin LiThis document describes the language extensions provided by Clang. In addition 21*67e74705SXin Lito the language extensions listed here, Clang aims to support a broad range of 22*67e74705SXin LiGCC extensions. Please see the `GCC manual 23*67e74705SXin Li<http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html>`_ for more information on 24*67e74705SXin Lithese extensions. 25*67e74705SXin Li 26*67e74705SXin Li.. _langext-feature_check: 27*67e74705SXin Li 28*67e74705SXin LiFeature Checking Macros 29*67e74705SXin Li======================= 30*67e74705SXin Li 31*67e74705SXin LiLanguage extensions can be very useful, but only if you know you can depend on 32*67e74705SXin Lithem. In order to allow fine-grain features checks, we support three builtin 33*67e74705SXin Lifunction-like macros. This allows you to directly test for a feature in your 34*67e74705SXin Licode without having to resort to something like autoconf or fragile "compiler 35*67e74705SXin Liversion checks". 36*67e74705SXin Li 37*67e74705SXin Li``__has_builtin`` 38*67e74705SXin Li----------------- 39*67e74705SXin Li 40*67e74705SXin LiThis function-like macro takes a single identifier argument that is the name of 41*67e74705SXin Lia builtin function. It evaluates to 1 if the builtin is supported or 0 if not. 42*67e74705SXin LiIt can be used like this: 43*67e74705SXin Li 44*67e74705SXin Li.. code-block:: c++ 45*67e74705SXin Li 46*67e74705SXin Li #ifndef __has_builtin // Optional of course. 47*67e74705SXin Li #define __has_builtin(x) 0 // Compatibility with non-clang compilers. 48*67e74705SXin Li #endif 49*67e74705SXin Li 50*67e74705SXin Li ... 51*67e74705SXin Li #if __has_builtin(__builtin_trap) 52*67e74705SXin Li __builtin_trap(); 53*67e74705SXin Li #else 54*67e74705SXin Li abort(); 55*67e74705SXin Li #endif 56*67e74705SXin Li ... 57*67e74705SXin Li 58*67e74705SXin Li.. _langext-__has_feature-__has_extension: 59*67e74705SXin Li 60*67e74705SXin Li``__has_feature`` and ``__has_extension`` 61*67e74705SXin Li----------------------------------------- 62*67e74705SXin Li 63*67e74705SXin LiThese function-like macros take a single identifier argument that is the name 64*67e74705SXin Liof a feature. ``__has_feature`` evaluates to 1 if the feature is both 65*67e74705SXin Lisupported by Clang and standardized in the current language standard or 0 if 66*67e74705SXin Linot (but see :ref:`below <langext-has-feature-back-compat>`), while 67*67e74705SXin Li``__has_extension`` evaluates to 1 if the feature is supported by Clang in the 68*67e74705SXin Licurrent language (either as a language extension or a standard language 69*67e74705SXin Lifeature) or 0 if not. They can be used like this: 70*67e74705SXin Li 71*67e74705SXin Li.. code-block:: c++ 72*67e74705SXin Li 73*67e74705SXin Li #ifndef __has_feature // Optional of course. 74*67e74705SXin Li #define __has_feature(x) 0 // Compatibility with non-clang compilers. 75*67e74705SXin Li #endif 76*67e74705SXin Li #ifndef __has_extension 77*67e74705SXin Li #define __has_extension __has_feature // Compatibility with pre-3.0 compilers. 78*67e74705SXin Li #endif 79*67e74705SXin Li 80*67e74705SXin Li ... 81*67e74705SXin Li #if __has_feature(cxx_rvalue_references) 82*67e74705SXin Li // This code will only be compiled with the -std=c++11 and -std=gnu++11 83*67e74705SXin Li // options, because rvalue references are only standardized in C++11. 84*67e74705SXin Li #endif 85*67e74705SXin Li 86*67e74705SXin Li #if __has_extension(cxx_rvalue_references) 87*67e74705SXin Li // This code will be compiled with the -std=c++11, -std=gnu++11, -std=c++98 88*67e74705SXin Li // and -std=gnu++98 options, because rvalue references are supported as a 89*67e74705SXin Li // language extension in C++98. 90*67e74705SXin Li #endif 91*67e74705SXin Li 92*67e74705SXin Li.. _langext-has-feature-back-compat: 93*67e74705SXin Li 94*67e74705SXin LiFor backward compatibility, ``__has_feature`` can also be used to test 95*67e74705SXin Lifor support for non-standardized features, i.e. features not prefixed ``c_``, 96*67e74705SXin Li``cxx_`` or ``objc_``. 97*67e74705SXin Li 98*67e74705SXin LiAnother use of ``__has_feature`` is to check for compiler features not related 99*67e74705SXin Lito the language standard, such as e.g. :doc:`AddressSanitizer 100*67e74705SXin Li<AddressSanitizer>`. 101*67e74705SXin Li 102*67e74705SXin LiIf the ``-pedantic-errors`` option is given, ``__has_extension`` is equivalent 103*67e74705SXin Lito ``__has_feature``. 104*67e74705SXin Li 105*67e74705SXin LiThe feature tag is described along with the language feature below. 106*67e74705SXin Li 107*67e74705SXin LiThe feature name or extension name can also be specified with a preceding and 108*67e74705SXin Lifollowing ``__`` (double underscore) to avoid interference from a macro with 109*67e74705SXin Lithe same name. For instance, ``__cxx_rvalue_references__`` can be used instead 110*67e74705SXin Liof ``cxx_rvalue_references``. 111*67e74705SXin Li 112*67e74705SXin Li``__has_cpp_attribute`` 113*67e74705SXin Li----------------------- 114*67e74705SXin Li 115*67e74705SXin LiThis function-like macro takes a single argument that is the name of a 116*67e74705SXin LiC++11-style attribute. The argument can either be a single identifier, or a 117*67e74705SXin Liscoped identifier. If the attribute is supported, a nonzero value is returned. 118*67e74705SXin LiIf the attribute is a standards-based attribute, this macro returns a nonzero 119*67e74705SXin Livalue based on the year and month in which the attribute was voted into the 120*67e74705SXin Liworking draft. If the attribute is not supported by the current compliation 121*67e74705SXin Litarget, this macro evaluates to 0. It can be used like this: 122*67e74705SXin Li 123*67e74705SXin Li.. code-block:: c++ 124*67e74705SXin Li 125*67e74705SXin Li #ifndef __has_cpp_attribute // Optional of course. 126*67e74705SXin Li #define __has_cpp_attribute(x) 0 // Compatibility with non-clang compilers. 127*67e74705SXin Li #endif 128*67e74705SXin Li 129*67e74705SXin Li ... 130*67e74705SXin Li #if __has_cpp_attribute(clang::fallthrough) 131*67e74705SXin Li #define FALLTHROUGH [[clang::fallthrough]] 132*67e74705SXin Li #else 133*67e74705SXin Li #define FALLTHROUGH 134*67e74705SXin Li #endif 135*67e74705SXin Li ... 136*67e74705SXin Li 137*67e74705SXin LiThe attribute identifier (but not scope) can also be specified with a preceding 138*67e74705SXin Liand following ``__`` (double underscore) to avoid interference from a macro with 139*67e74705SXin Lithe same name. For instance, ``gnu::__const__`` can be used instead of 140*67e74705SXin Li``gnu::const``. 141*67e74705SXin Li 142*67e74705SXin Li``__has_attribute`` 143*67e74705SXin Li------------------- 144*67e74705SXin Li 145*67e74705SXin LiThis function-like macro takes a single identifier argument that is the name of 146*67e74705SXin Lia GNU-style attribute. It evaluates to 1 if the attribute is supported by the 147*67e74705SXin Licurrent compilation target, or 0 if not. It can be used like this: 148*67e74705SXin Li 149*67e74705SXin Li.. code-block:: c++ 150*67e74705SXin Li 151*67e74705SXin Li #ifndef __has_attribute // Optional of course. 152*67e74705SXin Li #define __has_attribute(x) 0 // Compatibility with non-clang compilers. 153*67e74705SXin Li #endif 154*67e74705SXin Li 155*67e74705SXin Li ... 156*67e74705SXin Li #if __has_attribute(always_inline) 157*67e74705SXin Li #define ALWAYS_INLINE __attribute__((always_inline)) 158*67e74705SXin Li #else 159*67e74705SXin Li #define ALWAYS_INLINE 160*67e74705SXin Li #endif 161*67e74705SXin Li ... 162*67e74705SXin Li 163*67e74705SXin LiThe attribute name can also be specified with a preceding and following ``__`` 164*67e74705SXin Li(double underscore) to avoid interference from a macro with the same name. For 165*67e74705SXin Liinstance, ``__always_inline__`` can be used instead of ``always_inline``. 166*67e74705SXin Li 167*67e74705SXin Li 168*67e74705SXin Li``__has_declspec_attribute`` 169*67e74705SXin Li---------------------------- 170*67e74705SXin Li 171*67e74705SXin LiThis function-like macro takes a single identifier argument that is the name of 172*67e74705SXin Lian attribute implemented as a Microsoft-style ``__declspec`` attribute. It 173*67e74705SXin Lievaluates to 1 if the attribute is supported by the current compilation target, 174*67e74705SXin Lior 0 if not. It can be used like this: 175*67e74705SXin Li 176*67e74705SXin Li.. code-block:: c++ 177*67e74705SXin Li 178*67e74705SXin Li #ifndef __has_declspec_attribute // Optional of course. 179*67e74705SXin Li #define __has_declspec_attribute(x) 0 // Compatibility with non-clang compilers. 180*67e74705SXin Li #endif 181*67e74705SXin Li 182*67e74705SXin Li ... 183*67e74705SXin Li #if __has_declspec_attribute(dllexport) 184*67e74705SXin Li #define DLLEXPORT __declspec(dllexport) 185*67e74705SXin Li #else 186*67e74705SXin Li #define DLLEXPORT 187*67e74705SXin Li #endif 188*67e74705SXin Li ... 189*67e74705SXin Li 190*67e74705SXin LiThe attribute name can also be specified with a preceding and following ``__`` 191*67e74705SXin Li(double underscore) to avoid interference from a macro with the same name. For 192*67e74705SXin Liinstance, ``__dllexport__`` can be used instead of ``dllexport``. 193*67e74705SXin Li 194*67e74705SXin Li``__is_identifier`` 195*67e74705SXin Li------------------- 196*67e74705SXin Li 197*67e74705SXin LiThis function-like macro takes a single identifier argument that might be either 198*67e74705SXin Lia reserved word or a regular identifier. It evaluates to 1 if the argument is just 199*67e74705SXin Lia regular identifier and not a reserved word, in the sense that it can then be 200*67e74705SXin Liused as the name of a user-defined function or variable. Otherwise it evaluates 201*67e74705SXin Lito 0. It can be used like this: 202*67e74705SXin Li 203*67e74705SXin Li.. code-block:: c++ 204*67e74705SXin Li 205*67e74705SXin Li ... 206*67e74705SXin Li #ifdef __is_identifier // Compatibility with non-clang compilers. 207*67e74705SXin Li #if __is_identifier(__wchar_t) 208*67e74705SXin Li typedef wchar_t __wchar_t; 209*67e74705SXin Li #endif 210*67e74705SXin Li #endif 211*67e74705SXin Li 212*67e74705SXin Li __wchar_t WideCharacter; 213*67e74705SXin Li ... 214*67e74705SXin Li 215*67e74705SXin LiInclude File Checking Macros 216*67e74705SXin Li============================ 217*67e74705SXin Li 218*67e74705SXin LiNot all developments systems have the same include files. The 219*67e74705SXin Li:ref:`langext-__has_include` and :ref:`langext-__has_include_next` macros allow 220*67e74705SXin Liyou to check for the existence of an include file before doing a possibly 221*67e74705SXin Lifailing ``#include`` directive. Include file checking macros must be used 222*67e74705SXin Lias expressions in ``#if`` or ``#elif`` preprocessing directives. 223*67e74705SXin Li 224*67e74705SXin Li.. _langext-__has_include: 225*67e74705SXin Li 226*67e74705SXin Li``__has_include`` 227*67e74705SXin Li----------------- 228*67e74705SXin Li 229*67e74705SXin LiThis function-like macro takes a single file name string argument that is the 230*67e74705SXin Liname of an include file. It evaluates to 1 if the file can be found using the 231*67e74705SXin Liinclude paths, or 0 otherwise: 232*67e74705SXin Li 233*67e74705SXin Li.. code-block:: c++ 234*67e74705SXin Li 235*67e74705SXin Li // Note the two possible file name string formats. 236*67e74705SXin Li #if __has_include("myinclude.h") && __has_include(<stdint.h>) 237*67e74705SXin Li # include "myinclude.h" 238*67e74705SXin Li #endif 239*67e74705SXin Li 240*67e74705SXin LiTo test for this feature, use ``#if defined(__has_include)``: 241*67e74705SXin Li 242*67e74705SXin Li.. code-block:: c++ 243*67e74705SXin Li 244*67e74705SXin Li // To avoid problem with non-clang compilers not having this macro. 245*67e74705SXin Li #if defined(__has_include) 246*67e74705SXin Li #if __has_include("myinclude.h") 247*67e74705SXin Li # include "myinclude.h" 248*67e74705SXin Li #endif 249*67e74705SXin Li #endif 250*67e74705SXin Li 251*67e74705SXin Li.. _langext-__has_include_next: 252*67e74705SXin Li 253*67e74705SXin Li``__has_include_next`` 254*67e74705SXin Li---------------------- 255*67e74705SXin Li 256*67e74705SXin LiThis function-like macro takes a single file name string argument that is the 257*67e74705SXin Liname of an include file. It is like ``__has_include`` except that it looks for 258*67e74705SXin Lithe second instance of the given file found in the include paths. It evaluates 259*67e74705SXin Lito 1 if the second instance of the file can be found using the include paths, 260*67e74705SXin Lior 0 otherwise: 261*67e74705SXin Li 262*67e74705SXin Li.. code-block:: c++ 263*67e74705SXin Li 264*67e74705SXin Li // Note the two possible file name string formats. 265*67e74705SXin Li #if __has_include_next("myinclude.h") && __has_include_next(<stdint.h>) 266*67e74705SXin Li # include_next "myinclude.h" 267*67e74705SXin Li #endif 268*67e74705SXin Li 269*67e74705SXin Li // To avoid problem with non-clang compilers not having this macro. 270*67e74705SXin Li #if defined(__has_include_next) 271*67e74705SXin Li #if __has_include_next("myinclude.h") 272*67e74705SXin Li # include_next "myinclude.h" 273*67e74705SXin Li #endif 274*67e74705SXin Li #endif 275*67e74705SXin Li 276*67e74705SXin LiNote that ``__has_include_next``, like the GNU extension ``#include_next`` 277*67e74705SXin Lidirective, is intended for use in headers only, and will issue a warning if 278*67e74705SXin Liused in the top-level compilation file. A warning will also be issued if an 279*67e74705SXin Liabsolute path is used in the file argument. 280*67e74705SXin Li 281*67e74705SXin Li``__has_warning`` 282*67e74705SXin Li----------------- 283*67e74705SXin Li 284*67e74705SXin LiThis function-like macro takes a string literal that represents a command line 285*67e74705SXin Lioption for a warning and returns true if that is a valid warning option. 286*67e74705SXin Li 287*67e74705SXin Li.. code-block:: c++ 288*67e74705SXin Li 289*67e74705SXin Li #if __has_warning("-Wformat") 290*67e74705SXin Li ... 291*67e74705SXin Li #endif 292*67e74705SXin Li 293*67e74705SXin LiBuiltin Macros 294*67e74705SXin Li============== 295*67e74705SXin Li 296*67e74705SXin Li``__BASE_FILE__`` 297*67e74705SXin Li Defined to a string that contains the name of the main input file passed to 298*67e74705SXin Li Clang. 299*67e74705SXin Li 300*67e74705SXin Li``__COUNTER__`` 301*67e74705SXin Li Defined to an integer value that starts at zero and is incremented each time 302*67e74705SXin Li the ``__COUNTER__`` macro is expanded. 303*67e74705SXin Li 304*67e74705SXin Li``__INCLUDE_LEVEL__`` 305*67e74705SXin Li Defined to an integral value that is the include depth of the file currently 306*67e74705SXin Li being translated. For the main file, this value is zero. 307*67e74705SXin Li 308*67e74705SXin Li``__TIMESTAMP__`` 309*67e74705SXin Li Defined to the date and time of the last modification of the current source 310*67e74705SXin Li file. 311*67e74705SXin Li 312*67e74705SXin Li``__clang__`` 313*67e74705SXin Li Defined when compiling with Clang 314*67e74705SXin Li 315*67e74705SXin Li``__clang_major__`` 316*67e74705SXin Li Defined to the major marketing version number of Clang (e.g., the 2 in 317*67e74705SXin Li 2.0.1). Note that marketing version numbers should not be used to check for 318*67e74705SXin Li language features, as different vendors use different numbering schemes. 319*67e74705SXin Li Instead, use the :ref:`langext-feature_check`. 320*67e74705SXin Li 321*67e74705SXin Li``__clang_minor__`` 322*67e74705SXin Li Defined to the minor version number of Clang (e.g., the 0 in 2.0.1). Note 323*67e74705SXin Li that marketing version numbers should not be used to check for language 324*67e74705SXin Li features, as different vendors use different numbering schemes. Instead, use 325*67e74705SXin Li the :ref:`langext-feature_check`. 326*67e74705SXin Li 327*67e74705SXin Li``__clang_patchlevel__`` 328*67e74705SXin Li Defined to the marketing patch level of Clang (e.g., the 1 in 2.0.1). 329*67e74705SXin Li 330*67e74705SXin Li``__clang_version__`` 331*67e74705SXin Li Defined to a string that captures the Clang marketing version, including the 332*67e74705SXin Li Subversion tag or revision number, e.g., "``1.5 (trunk 102332)``". 333*67e74705SXin Li 334*67e74705SXin Li.. _langext-vectors: 335*67e74705SXin Li 336*67e74705SXin LiVectors and Extended Vectors 337*67e74705SXin Li============================ 338*67e74705SXin Li 339*67e74705SXin LiSupports the GCC, OpenCL, AltiVec and NEON vector extensions. 340*67e74705SXin Li 341*67e74705SXin LiOpenCL vector types are created using ``ext_vector_type`` attribute. It 342*67e74705SXin Lisupport for ``V.xyzw`` syntax and other tidbits as seen in OpenCL. An example 343*67e74705SXin Liis: 344*67e74705SXin Li 345*67e74705SXin Li.. code-block:: c++ 346*67e74705SXin Li 347*67e74705SXin Li typedef float float4 __attribute__((ext_vector_type(4))); 348*67e74705SXin Li typedef float float2 __attribute__((ext_vector_type(2))); 349*67e74705SXin Li 350*67e74705SXin Li float4 foo(float2 a, float2 b) { 351*67e74705SXin Li float4 c; 352*67e74705SXin Li c.xz = a; 353*67e74705SXin Li c.yw = b; 354*67e74705SXin Li return c; 355*67e74705SXin Li } 356*67e74705SXin Li 357*67e74705SXin LiQuery for this feature with ``__has_extension(attribute_ext_vector_type)``. 358*67e74705SXin Li 359*67e74705SXin LiGiving ``-faltivec`` option to clang enables support for AltiVec vector syntax 360*67e74705SXin Liand functions. For example: 361*67e74705SXin Li 362*67e74705SXin Li.. code-block:: c++ 363*67e74705SXin Li 364*67e74705SXin Li vector float foo(vector int a) { 365*67e74705SXin Li vector int b; 366*67e74705SXin Li b = vec_add(a, a) + a; 367*67e74705SXin Li return (vector float)b; 368*67e74705SXin Li } 369*67e74705SXin Li 370*67e74705SXin LiNEON vector types are created using ``neon_vector_type`` and 371*67e74705SXin Li``neon_polyvector_type`` attributes. For example: 372*67e74705SXin Li 373*67e74705SXin Li.. code-block:: c++ 374*67e74705SXin Li 375*67e74705SXin Li typedef __attribute__((neon_vector_type(8))) int8_t int8x8_t; 376*67e74705SXin Li typedef __attribute__((neon_polyvector_type(16))) poly8_t poly8x16_t; 377*67e74705SXin Li 378*67e74705SXin Li int8x8_t foo(int8x8_t a) { 379*67e74705SXin Li int8x8_t v; 380*67e74705SXin Li v = a; 381*67e74705SXin Li return v; 382*67e74705SXin Li } 383*67e74705SXin Li 384*67e74705SXin LiVector Literals 385*67e74705SXin Li--------------- 386*67e74705SXin Li 387*67e74705SXin LiVector literals can be used to create vectors from a set of scalars, or 388*67e74705SXin Livectors. Either parentheses or braces form can be used. In the parentheses 389*67e74705SXin Liform the number of literal values specified must be one, i.e. referring to a 390*67e74705SXin Liscalar value, or must match the size of the vector type being created. If a 391*67e74705SXin Lisingle scalar literal value is specified, the scalar literal value will be 392*67e74705SXin Lireplicated to all the components of the vector type. In the brackets form any 393*67e74705SXin Linumber of literals can be specified. For example: 394*67e74705SXin Li 395*67e74705SXin Li.. code-block:: c++ 396*67e74705SXin Li 397*67e74705SXin Li typedef int v4si __attribute__((__vector_size__(16))); 398*67e74705SXin Li typedef float float4 __attribute__((ext_vector_type(4))); 399*67e74705SXin Li typedef float float2 __attribute__((ext_vector_type(2))); 400*67e74705SXin Li 401*67e74705SXin Li v4si vsi = (v4si){1, 2, 3, 4}; 402*67e74705SXin Li float4 vf = (float4)(1.0f, 2.0f, 3.0f, 4.0f); 403*67e74705SXin Li vector int vi1 = (vector int)(1); // vi1 will be (1, 1, 1, 1). 404*67e74705SXin Li vector int vi2 = (vector int){1}; // vi2 will be (1, 0, 0, 0). 405*67e74705SXin Li vector int vi3 = (vector int)(1, 2); // error 406*67e74705SXin Li vector int vi4 = (vector int){1, 2}; // vi4 will be (1, 2, 0, 0). 407*67e74705SXin Li vector int vi5 = (vector int)(1, 2, 3, 4); 408*67e74705SXin Li float4 vf = (float4)((float2)(1.0f, 2.0f), (float2)(3.0f, 4.0f)); 409*67e74705SXin Li 410*67e74705SXin LiVector Operations 411*67e74705SXin Li----------------- 412*67e74705SXin Li 413*67e74705SXin LiThe table below shows the support for each operation by vector extension. A 414*67e74705SXin Lidash indicates that an operation is not accepted according to a corresponding 415*67e74705SXin Lispecification. 416*67e74705SXin Li 417*67e74705SXin Li============================== ======= ======= ======= ======= 418*67e74705SXin Li Operator OpenCL AltiVec GCC NEON 419*67e74705SXin Li============================== ======= ======= ======= ======= 420*67e74705SXin Li[] yes yes yes -- 421*67e74705SXin Liunary operators +, -- yes yes yes -- 422*67e74705SXin Li++, -- -- yes yes yes -- 423*67e74705SXin Li+,--,*,/,% yes yes yes -- 424*67e74705SXin Libitwise operators &,|,^,~ yes yes yes -- 425*67e74705SXin Li>>,<< yes yes yes -- 426*67e74705SXin Li!, &&, || yes -- -- -- 427*67e74705SXin Li==, !=, >, <, >=, <= yes yes -- -- 428*67e74705SXin Li= yes yes yes yes 429*67e74705SXin Li:? yes -- -- -- 430*67e74705SXin Lisizeof yes yes yes yes 431*67e74705SXin LiC-style cast yes yes yes no 432*67e74705SXin Lireinterpret_cast yes no yes no 433*67e74705SXin Listatic_cast yes no yes no 434*67e74705SXin Liconst_cast no no no no 435*67e74705SXin Li============================== ======= ======= ======= ======= 436*67e74705SXin Li 437*67e74705SXin LiSee also :ref:`langext-__builtin_shufflevector`, :ref:`langext-__builtin_convertvector`. 438*67e74705SXin Li 439*67e74705SXin LiMessages on ``deprecated`` and ``unavailable`` Attributes 440*67e74705SXin Li========================================================= 441*67e74705SXin Li 442*67e74705SXin LiAn optional string message can be added to the ``deprecated`` and 443*67e74705SXin Li``unavailable`` attributes. For example: 444*67e74705SXin Li 445*67e74705SXin Li.. code-block:: c++ 446*67e74705SXin Li 447*67e74705SXin Li void explode(void) __attribute__((deprecated("extremely unsafe, use 'combust' instead!!!"))); 448*67e74705SXin Li 449*67e74705SXin LiIf the deprecated or unavailable declaration is used, the message will be 450*67e74705SXin Liincorporated into the appropriate diagnostic: 451*67e74705SXin Li 452*67e74705SXin Li.. code-block:: none 453*67e74705SXin Li 454*67e74705SXin Li harmless.c:4:3: warning: 'explode' is deprecated: extremely unsafe, use 'combust' instead!!! 455*67e74705SXin Li [-Wdeprecated-declarations] 456*67e74705SXin Li explode(); 457*67e74705SXin Li ^ 458*67e74705SXin Li 459*67e74705SXin LiQuery for this feature with 460*67e74705SXin Li``__has_extension(attribute_deprecated_with_message)`` and 461*67e74705SXin Li``__has_extension(attribute_unavailable_with_message)``. 462*67e74705SXin Li 463*67e74705SXin LiAttributes on Enumerators 464*67e74705SXin Li========================= 465*67e74705SXin Li 466*67e74705SXin LiClang allows attributes to be written on individual enumerators. This allows 467*67e74705SXin Lienumerators to be deprecated, made unavailable, etc. The attribute must appear 468*67e74705SXin Liafter the enumerator name and before any initializer, like so: 469*67e74705SXin Li 470*67e74705SXin Li.. code-block:: c++ 471*67e74705SXin Li 472*67e74705SXin Li enum OperationMode { 473*67e74705SXin Li OM_Invalid, 474*67e74705SXin Li OM_Normal, 475*67e74705SXin Li OM_Terrified __attribute__((deprecated)), 476*67e74705SXin Li OM_AbortOnError __attribute__((deprecated)) = 4 477*67e74705SXin Li }; 478*67e74705SXin Li 479*67e74705SXin LiAttributes on the ``enum`` declaration do not apply to individual enumerators. 480*67e74705SXin Li 481*67e74705SXin LiQuery for this feature with ``__has_extension(enumerator_attributes)``. 482*67e74705SXin Li 483*67e74705SXin Li'User-Specified' System Frameworks 484*67e74705SXin Li================================== 485*67e74705SXin Li 486*67e74705SXin LiClang provides a mechanism by which frameworks can be built in such a way that 487*67e74705SXin Lithey will always be treated as being "system frameworks", even if they are not 488*67e74705SXin Lipresent in a system framework directory. This can be useful to system 489*67e74705SXin Liframework developers who want to be able to test building other applications 490*67e74705SXin Liwith development builds of their framework, including the manner in which the 491*67e74705SXin Licompiler changes warning behavior for system headers. 492*67e74705SXin Li 493*67e74705SXin LiFramework developers can opt-in to this mechanism by creating a 494*67e74705SXin Li"``.system_framework``" file at the top-level of their framework. That is, the 495*67e74705SXin Liframework should have contents like: 496*67e74705SXin Li 497*67e74705SXin Li.. code-block:: none 498*67e74705SXin Li 499*67e74705SXin Li .../TestFramework.framework 500*67e74705SXin Li .../TestFramework.framework/.system_framework 501*67e74705SXin Li .../TestFramework.framework/Headers 502*67e74705SXin Li .../TestFramework.framework/Headers/TestFramework.h 503*67e74705SXin Li ... 504*67e74705SXin Li 505*67e74705SXin LiClang will treat the presence of this file as an indicator that the framework 506*67e74705SXin Lishould be treated as a system framework, regardless of how it was found in the 507*67e74705SXin Liframework search path. For consistency, we recommend that such files never be 508*67e74705SXin Liincluded in installed versions of the framework. 509*67e74705SXin Li 510*67e74705SXin LiChecks for Standard Language Features 511*67e74705SXin Li===================================== 512*67e74705SXin Li 513*67e74705SXin LiThe ``__has_feature`` macro can be used to query if certain standard language 514*67e74705SXin Lifeatures are enabled. The ``__has_extension`` macro can be used to query if 515*67e74705SXin Lilanguage features are available as an extension when compiling for a standard 516*67e74705SXin Liwhich does not provide them. The features which can be tested are listed here. 517*67e74705SXin Li 518*67e74705SXin LiSince Clang 3.4, the C++ SD-6 feature test macros are also supported. 519*67e74705SXin LiThese are macros with names of the form ``__cpp_<feature_name>``, and are 520*67e74705SXin Liintended to be a portable way to query the supported features of the compiler. 521*67e74705SXin LiSee `the C++ status page <http://clang.llvm.org/cxx_status.html#ts>`_ for 522*67e74705SXin Liinformation on the version of SD-6 supported by each Clang release, and the 523*67e74705SXin Limacros provided by that revision of the recommendations. 524*67e74705SXin Li 525*67e74705SXin LiC++98 526*67e74705SXin Li----- 527*67e74705SXin Li 528*67e74705SXin LiThe features listed below are part of the C++98 standard. These features are 529*67e74705SXin Lienabled by default when compiling C++ code. 530*67e74705SXin Li 531*67e74705SXin LiC++ exceptions 532*67e74705SXin Li^^^^^^^^^^^^^^ 533*67e74705SXin Li 534*67e74705SXin LiUse ``__has_feature(cxx_exceptions)`` to determine if C++ exceptions have been 535*67e74705SXin Lienabled. For example, compiling code with ``-fno-exceptions`` disables C++ 536*67e74705SXin Liexceptions. 537*67e74705SXin Li 538*67e74705SXin LiC++ RTTI 539*67e74705SXin Li^^^^^^^^ 540*67e74705SXin Li 541*67e74705SXin LiUse ``__has_feature(cxx_rtti)`` to determine if C++ RTTI has been enabled. For 542*67e74705SXin Liexample, compiling code with ``-fno-rtti`` disables the use of RTTI. 543*67e74705SXin Li 544*67e74705SXin LiC++11 545*67e74705SXin Li----- 546*67e74705SXin Li 547*67e74705SXin LiThe features listed below are part of the C++11 standard. As a result, all 548*67e74705SXin Lithese features are enabled with the ``-std=c++11`` or ``-std=gnu++11`` option 549*67e74705SXin Liwhen compiling C++ code. 550*67e74705SXin Li 551*67e74705SXin LiC++11 SFINAE includes access control 552*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 553*67e74705SXin Li 554*67e74705SXin LiUse ``__has_feature(cxx_access_control_sfinae)`` or 555*67e74705SXin Li``__has_extension(cxx_access_control_sfinae)`` to determine whether 556*67e74705SXin Liaccess-control errors (e.g., calling a private constructor) are considered to 557*67e74705SXin Libe template argument deduction errors (aka SFINAE errors), per `C++ DR1170 558*67e74705SXin Li<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1170>`_. 559*67e74705SXin Li 560*67e74705SXin LiC++11 alias templates 561*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 562*67e74705SXin Li 563*67e74705SXin LiUse ``__has_feature(cxx_alias_templates)`` or 564*67e74705SXin Li``__has_extension(cxx_alias_templates)`` to determine if support for C++11's 565*67e74705SXin Lialias declarations and alias templates is enabled. 566*67e74705SXin Li 567*67e74705SXin LiC++11 alignment specifiers 568*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^ 569*67e74705SXin Li 570*67e74705SXin LiUse ``__has_feature(cxx_alignas)`` or ``__has_extension(cxx_alignas)`` to 571*67e74705SXin Lidetermine if support for alignment specifiers using ``alignas`` is enabled. 572*67e74705SXin Li 573*67e74705SXin LiUse ``__has_feature(cxx_alignof)`` or ``__has_extension(cxx_alignof)`` to 574*67e74705SXin Lidetermine if support for the ``alignof`` keyword is enabled. 575*67e74705SXin Li 576*67e74705SXin LiC++11 attributes 577*67e74705SXin Li^^^^^^^^^^^^^^^^ 578*67e74705SXin Li 579*67e74705SXin LiUse ``__has_feature(cxx_attributes)`` or ``__has_extension(cxx_attributes)`` to 580*67e74705SXin Lidetermine if support for attribute parsing with C++11's square bracket notation 581*67e74705SXin Liis enabled. 582*67e74705SXin Li 583*67e74705SXin LiC++11 generalized constant expressions 584*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 585*67e74705SXin Li 586*67e74705SXin LiUse ``__has_feature(cxx_constexpr)`` to determine if support for generalized 587*67e74705SXin Liconstant expressions (e.g., ``constexpr``) is enabled. 588*67e74705SXin Li 589*67e74705SXin LiC++11 ``decltype()`` 590*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^ 591*67e74705SXin Li 592*67e74705SXin LiUse ``__has_feature(cxx_decltype)`` or ``__has_extension(cxx_decltype)`` to 593*67e74705SXin Lidetermine if support for the ``decltype()`` specifier is enabled. C++11's 594*67e74705SXin Li``decltype`` does not require type-completeness of a function call expression. 595*67e74705SXin LiUse ``__has_feature(cxx_decltype_incomplete_return_types)`` or 596*67e74705SXin Li``__has_extension(cxx_decltype_incomplete_return_types)`` to determine if 597*67e74705SXin Lisupport for this feature is enabled. 598*67e74705SXin Li 599*67e74705SXin LiC++11 default template arguments in function templates 600*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 601*67e74705SXin Li 602*67e74705SXin LiUse ``__has_feature(cxx_default_function_template_args)`` or 603*67e74705SXin Li``__has_extension(cxx_default_function_template_args)`` to determine if support 604*67e74705SXin Lifor default template arguments in function templates is enabled. 605*67e74705SXin Li 606*67e74705SXin LiC++11 ``default``\ ed functions 607*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 608*67e74705SXin Li 609*67e74705SXin LiUse ``__has_feature(cxx_defaulted_functions)`` or 610*67e74705SXin Li``__has_extension(cxx_defaulted_functions)`` to determine if support for 611*67e74705SXin Lidefaulted function definitions (with ``= default``) is enabled. 612*67e74705SXin Li 613*67e74705SXin LiC++11 delegating constructors 614*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 615*67e74705SXin Li 616*67e74705SXin LiUse ``__has_feature(cxx_delegating_constructors)`` to determine if support for 617*67e74705SXin Lidelegating constructors is enabled. 618*67e74705SXin Li 619*67e74705SXin LiC++11 ``deleted`` functions 620*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^ 621*67e74705SXin Li 622*67e74705SXin LiUse ``__has_feature(cxx_deleted_functions)`` or 623*67e74705SXin Li``__has_extension(cxx_deleted_functions)`` to determine if support for deleted 624*67e74705SXin Lifunction definitions (with ``= delete``) is enabled. 625*67e74705SXin Li 626*67e74705SXin LiC++11 explicit conversion functions 627*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 628*67e74705SXin Li 629*67e74705SXin LiUse ``__has_feature(cxx_explicit_conversions)`` to determine if support for 630*67e74705SXin Li``explicit`` conversion functions is enabled. 631*67e74705SXin Li 632*67e74705SXin LiC++11 generalized initializers 633*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 634*67e74705SXin Li 635*67e74705SXin LiUse ``__has_feature(cxx_generalized_initializers)`` to determine if support for 636*67e74705SXin Ligeneralized initializers (using braced lists and ``std::initializer_list``) is 637*67e74705SXin Lienabled. 638*67e74705SXin Li 639*67e74705SXin LiC++11 implicit move constructors/assignment operators 640*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 641*67e74705SXin Li 642*67e74705SXin LiUse ``__has_feature(cxx_implicit_moves)`` to determine if Clang will implicitly 643*67e74705SXin Ligenerate move constructors and move assignment operators where needed. 644*67e74705SXin Li 645*67e74705SXin LiC++11 inheriting constructors 646*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 647*67e74705SXin Li 648*67e74705SXin LiUse ``__has_feature(cxx_inheriting_constructors)`` to determine if support for 649*67e74705SXin Liinheriting constructors is enabled. 650*67e74705SXin Li 651*67e74705SXin LiC++11 inline namespaces 652*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^ 653*67e74705SXin Li 654*67e74705SXin LiUse ``__has_feature(cxx_inline_namespaces)`` or 655*67e74705SXin Li``__has_extension(cxx_inline_namespaces)`` to determine if support for inline 656*67e74705SXin Linamespaces is enabled. 657*67e74705SXin Li 658*67e74705SXin LiC++11 lambdas 659*67e74705SXin Li^^^^^^^^^^^^^ 660*67e74705SXin Li 661*67e74705SXin LiUse ``__has_feature(cxx_lambdas)`` or ``__has_extension(cxx_lambdas)`` to 662*67e74705SXin Lidetermine if support for lambdas is enabled. 663*67e74705SXin Li 664*67e74705SXin LiC++11 local and unnamed types as template arguments 665*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 666*67e74705SXin Li 667*67e74705SXin LiUse ``__has_feature(cxx_local_type_template_args)`` or 668*67e74705SXin Li``__has_extension(cxx_local_type_template_args)`` to determine if support for 669*67e74705SXin Lilocal and unnamed types as template arguments is enabled. 670*67e74705SXin Li 671*67e74705SXin LiC++11 noexcept 672*67e74705SXin Li^^^^^^^^^^^^^^ 673*67e74705SXin Li 674*67e74705SXin LiUse ``__has_feature(cxx_noexcept)`` or ``__has_extension(cxx_noexcept)`` to 675*67e74705SXin Lidetermine if support for noexcept exception specifications is enabled. 676*67e74705SXin Li 677*67e74705SXin LiC++11 in-class non-static data member initialization 678*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 679*67e74705SXin Li 680*67e74705SXin LiUse ``__has_feature(cxx_nonstatic_member_init)`` to determine whether in-class 681*67e74705SXin Liinitialization of non-static data members is enabled. 682*67e74705SXin Li 683*67e74705SXin LiC++11 ``nullptr`` 684*67e74705SXin Li^^^^^^^^^^^^^^^^^ 685*67e74705SXin Li 686*67e74705SXin LiUse ``__has_feature(cxx_nullptr)`` or ``__has_extension(cxx_nullptr)`` to 687*67e74705SXin Lidetermine if support for ``nullptr`` is enabled. 688*67e74705SXin Li 689*67e74705SXin LiC++11 ``override control`` 690*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^ 691*67e74705SXin Li 692*67e74705SXin LiUse ``__has_feature(cxx_override_control)`` or 693*67e74705SXin Li``__has_extension(cxx_override_control)`` to determine if support for the 694*67e74705SXin Lioverride control keywords is enabled. 695*67e74705SXin Li 696*67e74705SXin LiC++11 reference-qualified functions 697*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 698*67e74705SXin Li 699*67e74705SXin LiUse ``__has_feature(cxx_reference_qualified_functions)`` or 700*67e74705SXin Li``__has_extension(cxx_reference_qualified_functions)`` to determine if support 701*67e74705SXin Lifor reference-qualified functions (e.g., member functions with ``&`` or ``&&`` 702*67e74705SXin Liapplied to ``*this``) is enabled. 703*67e74705SXin Li 704*67e74705SXin LiC++11 range-based ``for`` loop 705*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 706*67e74705SXin Li 707*67e74705SXin LiUse ``__has_feature(cxx_range_for)`` or ``__has_extension(cxx_range_for)`` to 708*67e74705SXin Lidetermine if support for the range-based for loop is enabled. 709*67e74705SXin Li 710*67e74705SXin LiC++11 raw string literals 711*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^ 712*67e74705SXin Li 713*67e74705SXin LiUse ``__has_feature(cxx_raw_string_literals)`` to determine if support for raw 714*67e74705SXin Listring literals (e.g., ``R"x(foo\bar)x"``) is enabled. 715*67e74705SXin Li 716*67e74705SXin LiC++11 rvalue references 717*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^ 718*67e74705SXin Li 719*67e74705SXin LiUse ``__has_feature(cxx_rvalue_references)`` or 720*67e74705SXin Li``__has_extension(cxx_rvalue_references)`` to determine if support for rvalue 721*67e74705SXin Lireferences is enabled. 722*67e74705SXin Li 723*67e74705SXin LiC++11 ``static_assert()`` 724*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^ 725*67e74705SXin Li 726*67e74705SXin LiUse ``__has_feature(cxx_static_assert)`` or 727*67e74705SXin Li``__has_extension(cxx_static_assert)`` to determine if support for compile-time 728*67e74705SXin Liassertions using ``static_assert`` is enabled. 729*67e74705SXin Li 730*67e74705SXin LiC++11 ``thread_local`` 731*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^ 732*67e74705SXin Li 733*67e74705SXin LiUse ``__has_feature(cxx_thread_local)`` to determine if support for 734*67e74705SXin Li``thread_local`` variables is enabled. 735*67e74705SXin Li 736*67e74705SXin LiC++11 type inference 737*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^ 738*67e74705SXin Li 739*67e74705SXin LiUse ``__has_feature(cxx_auto_type)`` or ``__has_extension(cxx_auto_type)`` to 740*67e74705SXin Lidetermine C++11 type inference is supported using the ``auto`` specifier. If 741*67e74705SXin Lithis is disabled, ``auto`` will instead be a storage class specifier, as in C 742*67e74705SXin Lior C++98. 743*67e74705SXin Li 744*67e74705SXin LiC++11 strongly typed enumerations 745*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 746*67e74705SXin Li 747*67e74705SXin LiUse ``__has_feature(cxx_strong_enums)`` or 748*67e74705SXin Li``__has_extension(cxx_strong_enums)`` to determine if support for strongly 749*67e74705SXin Lityped, scoped enumerations is enabled. 750*67e74705SXin Li 751*67e74705SXin LiC++11 trailing return type 752*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^ 753*67e74705SXin Li 754*67e74705SXin LiUse ``__has_feature(cxx_trailing_return)`` or 755*67e74705SXin Li``__has_extension(cxx_trailing_return)`` to determine if support for the 756*67e74705SXin Lialternate function declaration syntax with trailing return type is enabled. 757*67e74705SXin Li 758*67e74705SXin LiC++11 Unicode string literals 759*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 760*67e74705SXin Li 761*67e74705SXin LiUse ``__has_feature(cxx_unicode_literals)`` to determine if support for Unicode 762*67e74705SXin Listring literals is enabled. 763*67e74705SXin Li 764*67e74705SXin LiC++11 unrestricted unions 765*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^ 766*67e74705SXin Li 767*67e74705SXin LiUse ``__has_feature(cxx_unrestricted_unions)`` to determine if support for 768*67e74705SXin Liunrestricted unions is enabled. 769*67e74705SXin Li 770*67e74705SXin LiC++11 user-defined literals 771*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^ 772*67e74705SXin Li 773*67e74705SXin LiUse ``__has_feature(cxx_user_literals)`` to determine if support for 774*67e74705SXin Liuser-defined literals is enabled. 775*67e74705SXin Li 776*67e74705SXin LiC++11 variadic templates 777*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^ 778*67e74705SXin Li 779*67e74705SXin LiUse ``__has_feature(cxx_variadic_templates)`` or 780*67e74705SXin Li``__has_extension(cxx_variadic_templates)`` to determine if support for 781*67e74705SXin Livariadic templates is enabled. 782*67e74705SXin Li 783*67e74705SXin LiC++1y 784*67e74705SXin Li----- 785*67e74705SXin Li 786*67e74705SXin LiThe features listed below are part of the committee draft for the C++1y 787*67e74705SXin Listandard. As a result, all these features are enabled with the ``-std=c++1y`` 788*67e74705SXin Lior ``-std=gnu++1y`` option when compiling C++ code. 789*67e74705SXin Li 790*67e74705SXin LiC++1y binary literals 791*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 792*67e74705SXin Li 793*67e74705SXin LiUse ``__has_feature(cxx_binary_literals)`` or 794*67e74705SXin Li``__has_extension(cxx_binary_literals)`` to determine whether 795*67e74705SXin Libinary literals (for instance, ``0b10010``) are recognized. Clang supports this 796*67e74705SXin Lifeature as an extension in all language modes. 797*67e74705SXin Li 798*67e74705SXin LiC++1y contextual conversions 799*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 800*67e74705SXin Li 801*67e74705SXin LiUse ``__has_feature(cxx_contextual_conversions)`` or 802*67e74705SXin Li``__has_extension(cxx_contextual_conversions)`` to determine if the C++1y rules 803*67e74705SXin Liare used when performing an implicit conversion for an array bound in a 804*67e74705SXin Li*new-expression*, the operand of a *delete-expression*, an integral constant 805*67e74705SXin Liexpression, or a condition in a ``switch`` statement. 806*67e74705SXin Li 807*67e74705SXin LiC++1y decltype(auto) 808*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^ 809*67e74705SXin Li 810*67e74705SXin LiUse ``__has_feature(cxx_decltype_auto)`` or 811*67e74705SXin Li``__has_extension(cxx_decltype_auto)`` to determine if support 812*67e74705SXin Lifor the ``decltype(auto)`` placeholder type is enabled. 813*67e74705SXin Li 814*67e74705SXin LiC++1y default initializers for aggregates 815*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 816*67e74705SXin Li 817*67e74705SXin LiUse ``__has_feature(cxx_aggregate_nsdmi)`` or 818*67e74705SXin Li``__has_extension(cxx_aggregate_nsdmi)`` to determine if support 819*67e74705SXin Lifor default initializers in aggregate members is enabled. 820*67e74705SXin Li 821*67e74705SXin LiC++1y digit separators 822*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^ 823*67e74705SXin Li 824*67e74705SXin LiUse ``__cpp_digit_separators`` to determine if support for digit separators 825*67e74705SXin Liusing single quotes (for instance, ``10'000``) is enabled. At this time, there 826*67e74705SXin Liis no corresponding ``__has_feature`` name 827*67e74705SXin Li 828*67e74705SXin LiC++1y generalized lambda capture 829*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 830*67e74705SXin Li 831*67e74705SXin LiUse ``__has_feature(cxx_init_captures)`` or 832*67e74705SXin Li``__has_extension(cxx_init_captures)`` to determine if support for 833*67e74705SXin Lilambda captures with explicit initializers is enabled 834*67e74705SXin Li(for instance, ``[n(0)] { return ++n; }``). 835*67e74705SXin Li 836*67e74705SXin LiC++1y generic lambdas 837*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 838*67e74705SXin Li 839*67e74705SXin LiUse ``__has_feature(cxx_generic_lambdas)`` or 840*67e74705SXin Li``__has_extension(cxx_generic_lambdas)`` to determine if support for generic 841*67e74705SXin Li(polymorphic) lambdas is enabled 842*67e74705SXin Li(for instance, ``[] (auto x) { return x + 1; }``). 843*67e74705SXin Li 844*67e74705SXin LiC++1y relaxed constexpr 845*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^ 846*67e74705SXin Li 847*67e74705SXin LiUse ``__has_feature(cxx_relaxed_constexpr)`` or 848*67e74705SXin Li``__has_extension(cxx_relaxed_constexpr)`` to determine if variable 849*67e74705SXin Lideclarations, local variable modification, and control flow constructs 850*67e74705SXin Liare permitted in ``constexpr`` functions. 851*67e74705SXin Li 852*67e74705SXin LiC++1y return type deduction 853*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^ 854*67e74705SXin Li 855*67e74705SXin LiUse ``__has_feature(cxx_return_type_deduction)`` or 856*67e74705SXin Li``__has_extension(cxx_return_type_deduction)`` to determine if support 857*67e74705SXin Lifor return type deduction for functions (using ``auto`` as a return type) 858*67e74705SXin Liis enabled. 859*67e74705SXin Li 860*67e74705SXin LiC++1y runtime-sized arrays 861*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^ 862*67e74705SXin Li 863*67e74705SXin LiUse ``__has_feature(cxx_runtime_array)`` or 864*67e74705SXin Li``__has_extension(cxx_runtime_array)`` to determine if support 865*67e74705SXin Lifor arrays of runtime bound (a restricted form of variable-length arrays) 866*67e74705SXin Liis enabled. 867*67e74705SXin LiClang's implementation of this feature is incomplete. 868*67e74705SXin Li 869*67e74705SXin LiC++1y variable templates 870*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^ 871*67e74705SXin Li 872*67e74705SXin LiUse ``__has_feature(cxx_variable_templates)`` or 873*67e74705SXin Li``__has_extension(cxx_variable_templates)`` to determine if support for 874*67e74705SXin Litemplated variable declarations is enabled. 875*67e74705SXin Li 876*67e74705SXin LiC11 877*67e74705SXin Li--- 878*67e74705SXin Li 879*67e74705SXin LiThe features listed below are part of the C11 standard. As a result, all these 880*67e74705SXin Lifeatures are enabled with the ``-std=c11`` or ``-std=gnu11`` option when 881*67e74705SXin Licompiling C code. Additionally, because these features are all 882*67e74705SXin Libackward-compatible, they are available as extensions in all language modes. 883*67e74705SXin Li 884*67e74705SXin LiC11 alignment specifiers 885*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^ 886*67e74705SXin Li 887*67e74705SXin LiUse ``__has_feature(c_alignas)`` or ``__has_extension(c_alignas)`` to determine 888*67e74705SXin Liif support for alignment specifiers using ``_Alignas`` is enabled. 889*67e74705SXin Li 890*67e74705SXin LiUse ``__has_feature(c_alignof)`` or ``__has_extension(c_alignof)`` to determine 891*67e74705SXin Liif support for the ``_Alignof`` keyword is enabled. 892*67e74705SXin Li 893*67e74705SXin LiC11 atomic operations 894*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 895*67e74705SXin Li 896*67e74705SXin LiUse ``__has_feature(c_atomic)`` or ``__has_extension(c_atomic)`` to determine 897*67e74705SXin Liif support for atomic types using ``_Atomic`` is enabled. Clang also provides 898*67e74705SXin Li:ref:`a set of builtins <langext-__c11_atomic>` which can be used to implement 899*67e74705SXin Lithe ``<stdatomic.h>`` operations on ``_Atomic`` types. Use 900*67e74705SXin Li``__has_include(<stdatomic.h>)`` to determine if C11's ``<stdatomic.h>`` header 901*67e74705SXin Liis available. 902*67e74705SXin Li 903*67e74705SXin LiClang will use the system's ``<stdatomic.h>`` header when one is available, and 904*67e74705SXin Liwill otherwise use its own. When using its own, implementations of the atomic 905*67e74705SXin Lioperations are provided as macros. In the cases where C11 also requires a real 906*67e74705SXin Lifunction, this header provides only the declaration of that function (along 907*67e74705SXin Liwith a shadowing macro implementation), and you must link to a library which 908*67e74705SXin Liprovides a definition of the function if you use it instead of the macro. 909*67e74705SXin Li 910*67e74705SXin LiC11 generic selections 911*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^ 912*67e74705SXin Li 913*67e74705SXin LiUse ``__has_feature(c_generic_selections)`` or 914*67e74705SXin Li``__has_extension(c_generic_selections)`` to determine if support for generic 915*67e74705SXin Liselections is enabled. 916*67e74705SXin Li 917*67e74705SXin LiAs an extension, the C11 generic selection expression is available in all 918*67e74705SXin Lilanguages supported by Clang. The syntax is the same as that given in the C11 919*67e74705SXin Listandard. 920*67e74705SXin Li 921*67e74705SXin LiIn C, type compatibility is decided according to the rules given in the 922*67e74705SXin Liappropriate standard, but in C++, which lacks the type compatibility rules used 923*67e74705SXin Liin C, types are considered compatible only if they are equivalent. 924*67e74705SXin Li 925*67e74705SXin LiC11 ``_Static_assert()`` 926*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^ 927*67e74705SXin Li 928*67e74705SXin LiUse ``__has_feature(c_static_assert)`` or ``__has_extension(c_static_assert)`` 929*67e74705SXin Lito determine if support for compile-time assertions using ``_Static_assert`` is 930*67e74705SXin Lienabled. 931*67e74705SXin Li 932*67e74705SXin LiC11 ``_Thread_local`` 933*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^ 934*67e74705SXin Li 935*67e74705SXin LiUse ``__has_feature(c_thread_local)`` or ``__has_extension(c_thread_local)`` 936*67e74705SXin Lito determine if support for ``_Thread_local`` variables is enabled. 937*67e74705SXin Li 938*67e74705SXin LiModules 939*67e74705SXin Li------- 940*67e74705SXin Li 941*67e74705SXin LiUse ``__has_feature(modules)`` to determine if Modules have been enabled. 942*67e74705SXin LiFor example, compiling code with ``-fmodules`` enables the use of Modules. 943*67e74705SXin Li 944*67e74705SXin LiMore information could be found `here <http://clang.llvm.org/docs/Modules.html>`_. 945*67e74705SXin Li 946*67e74705SXin LiChecks for Type Trait Primitives 947*67e74705SXin Li================================ 948*67e74705SXin Li 949*67e74705SXin LiType trait primitives are special builtin constant expressions that can be used 950*67e74705SXin Liby the standard C++ library to facilitate or simplify the implementation of 951*67e74705SXin Liuser-facing type traits in the <type_traits> header. 952*67e74705SXin Li 953*67e74705SXin LiThey are not intended to be used directly by user code because they are 954*67e74705SXin Liimplementation-defined and subject to change -- as such they're tied closely to 955*67e74705SXin Lithe supported set of system headers, currently: 956*67e74705SXin Li 957*67e74705SXin Li* LLVM's own libc++ 958*67e74705SXin Li* GNU libstdc++ 959*67e74705SXin Li* The Microsoft standard C++ library 960*67e74705SXin Li 961*67e74705SXin LiClang supports the `GNU C++ type traits 962*67e74705SXin Li<http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html>`_ and a subset of the 963*67e74705SXin Li`Microsoft Visual C++ Type traits 964*67e74705SXin Li<http://msdn.microsoft.com/en-us/library/ms177194(v=VS.100).aspx>`_. 965*67e74705SXin Li 966*67e74705SXin LiFeature detection is supported only for some of the primitives at present. User 967*67e74705SXin Licode should not use these checks because they bear no direct relation to the 968*67e74705SXin Liactual set of type traits supported by the C++ standard library. 969*67e74705SXin Li 970*67e74705SXin LiFor type trait ``__X``, ``__has_extension(X)`` indicates the presence of the 971*67e74705SXin Litype trait primitive in the compiler. A simplistic usage example as might be 972*67e74705SXin Liseen in standard C++ headers follows: 973*67e74705SXin Li 974*67e74705SXin Li.. code-block:: c++ 975*67e74705SXin Li 976*67e74705SXin Li #if __has_extension(is_convertible_to) 977*67e74705SXin Li template<typename From, typename To> 978*67e74705SXin Li struct is_convertible_to { 979*67e74705SXin Li static const bool value = __is_convertible_to(From, To); 980*67e74705SXin Li }; 981*67e74705SXin Li #else 982*67e74705SXin Li // Emulate type trait for compatibility with other compilers. 983*67e74705SXin Li #endif 984*67e74705SXin Li 985*67e74705SXin LiThe following type trait primitives are supported by Clang: 986*67e74705SXin Li 987*67e74705SXin Li* ``__has_nothrow_assign`` (GNU, Microsoft) 988*67e74705SXin Li* ``__has_nothrow_copy`` (GNU, Microsoft) 989*67e74705SXin Li* ``__has_nothrow_constructor`` (GNU, Microsoft) 990*67e74705SXin Li* ``__has_trivial_assign`` (GNU, Microsoft) 991*67e74705SXin Li* ``__has_trivial_copy`` (GNU, Microsoft) 992*67e74705SXin Li* ``__has_trivial_constructor`` (GNU, Microsoft) 993*67e74705SXin Li* ``__has_trivial_destructor`` (GNU, Microsoft) 994*67e74705SXin Li* ``__has_virtual_destructor`` (GNU, Microsoft) 995*67e74705SXin Li* ``__is_abstract`` (GNU, Microsoft) 996*67e74705SXin Li* ``__is_base_of`` (GNU, Microsoft) 997*67e74705SXin Li* ``__is_class`` (GNU, Microsoft) 998*67e74705SXin Li* ``__is_convertible_to`` (Microsoft) 999*67e74705SXin Li* ``__is_empty`` (GNU, Microsoft) 1000*67e74705SXin Li* ``__is_enum`` (GNU, Microsoft) 1001*67e74705SXin Li* ``__is_interface_class`` (Microsoft) 1002*67e74705SXin Li* ``__is_pod`` (GNU, Microsoft) 1003*67e74705SXin Li* ``__is_polymorphic`` (GNU, Microsoft) 1004*67e74705SXin Li* ``__is_union`` (GNU, Microsoft) 1005*67e74705SXin Li* ``__is_literal(type)``: Determines whether the given type is a literal type 1006*67e74705SXin Li* ``__is_final``: Determines whether the given type is declared with a 1007*67e74705SXin Li ``final`` class-virt-specifier. 1008*67e74705SXin Li* ``__underlying_type(type)``: Retrieves the underlying type for a given 1009*67e74705SXin Li ``enum`` type. This trait is required to implement the C++11 standard 1010*67e74705SXin Li library. 1011*67e74705SXin Li* ``__is_trivially_assignable(totype, fromtype)``: Determines whether a value 1012*67e74705SXin Li of type ``totype`` can be assigned to from a value of type ``fromtype`` such 1013*67e74705SXin Li that no non-trivial functions are called as part of that assignment. This 1014*67e74705SXin Li trait is required to implement the C++11 standard library. 1015*67e74705SXin Li* ``__is_trivially_constructible(type, argtypes...)``: Determines whether a 1016*67e74705SXin Li value of type ``type`` can be direct-initialized with arguments of types 1017*67e74705SXin Li ``argtypes...`` such that no non-trivial functions are called as part of 1018*67e74705SXin Li that initialization. This trait is required to implement the C++11 standard 1019*67e74705SXin Li library. 1020*67e74705SXin Li* ``__is_destructible`` (MSVC 2013) 1021*67e74705SXin Li* ``__is_nothrow_destructible`` (MSVC 2013) 1022*67e74705SXin Li* ``__is_nothrow_assignable`` (MSVC 2013, clang) 1023*67e74705SXin Li* ``__is_constructible`` (MSVC 2013, clang) 1024*67e74705SXin Li* ``__is_nothrow_constructible`` (MSVC 2013, clang) 1025*67e74705SXin Li* ``__is_assignable`` (MSVC 2015, clang) 1026*67e74705SXin Li 1027*67e74705SXin LiBlocks 1028*67e74705SXin Li====== 1029*67e74705SXin Li 1030*67e74705SXin LiThe syntax and high level language feature description is in 1031*67e74705SXin Li:doc:`BlockLanguageSpec<BlockLanguageSpec>`. Implementation and ABI details for 1032*67e74705SXin Lithe clang implementation are in :doc:`Block-ABI-Apple<Block-ABI-Apple>`. 1033*67e74705SXin Li 1034*67e74705SXin LiQuery for this feature with ``__has_extension(blocks)``. 1035*67e74705SXin Li 1036*67e74705SXin LiObjective-C Features 1037*67e74705SXin Li==================== 1038*67e74705SXin Li 1039*67e74705SXin LiRelated result types 1040*67e74705SXin Li-------------------- 1041*67e74705SXin Li 1042*67e74705SXin LiAccording to Cocoa conventions, Objective-C methods with certain names 1043*67e74705SXin Li("``init``", "``alloc``", etc.) always return objects that are an instance of 1044*67e74705SXin Lithe receiving class's type. Such methods are said to have a "related result 1045*67e74705SXin Litype", meaning that a message send to one of these methods will have the same 1046*67e74705SXin Listatic type as an instance of the receiver class. For example, given the 1047*67e74705SXin Lifollowing classes: 1048*67e74705SXin Li 1049*67e74705SXin Li.. code-block:: objc 1050*67e74705SXin Li 1051*67e74705SXin Li @interface NSObject 1052*67e74705SXin Li + (id)alloc; 1053*67e74705SXin Li - (id)init; 1054*67e74705SXin Li @end 1055*67e74705SXin Li 1056*67e74705SXin Li @interface NSArray : NSObject 1057*67e74705SXin Li @end 1058*67e74705SXin Li 1059*67e74705SXin Liand this common initialization pattern 1060*67e74705SXin Li 1061*67e74705SXin Li.. code-block:: objc 1062*67e74705SXin Li 1063*67e74705SXin Li NSArray *array = [[NSArray alloc] init]; 1064*67e74705SXin Li 1065*67e74705SXin Lithe type of the expression ``[NSArray alloc]`` is ``NSArray*`` because 1066*67e74705SXin Li``alloc`` implicitly has a related result type. Similarly, the type of the 1067*67e74705SXin Liexpression ``[[NSArray alloc] init]`` is ``NSArray*``, since ``init`` has a 1068*67e74705SXin Lirelated result type and its receiver is known to have the type ``NSArray *``. 1069*67e74705SXin LiIf neither ``alloc`` nor ``init`` had a related result type, the expressions 1070*67e74705SXin Liwould have had type ``id``, as declared in the method signature. 1071*67e74705SXin Li 1072*67e74705SXin LiA method with a related result type can be declared by using the type 1073*67e74705SXin Li``instancetype`` as its result type. ``instancetype`` is a contextual keyword 1074*67e74705SXin Lithat is only permitted in the result type of an Objective-C method, e.g. 1075*67e74705SXin Li 1076*67e74705SXin Li.. code-block:: objc 1077*67e74705SXin Li 1078*67e74705SXin Li @interface A 1079*67e74705SXin Li + (instancetype)constructAnA; 1080*67e74705SXin Li @end 1081*67e74705SXin Li 1082*67e74705SXin LiThe related result type can also be inferred for some methods. To determine 1083*67e74705SXin Liwhether a method has an inferred related result type, the first word in the 1084*67e74705SXin Licamel-case selector (e.g., "``init``" in "``initWithObjects``") is considered, 1085*67e74705SXin Liand the method will have a related result type if its return type is compatible 1086*67e74705SXin Liwith the type of its class and if: 1087*67e74705SXin Li 1088*67e74705SXin Li* the first word is "``alloc``" or "``new``", and the method is a class method, 1089*67e74705SXin Li or 1090*67e74705SXin Li 1091*67e74705SXin Li* the first word is "``autorelease``", "``init``", "``retain``", or "``self``", 1092*67e74705SXin Li and the method is an instance method. 1093*67e74705SXin Li 1094*67e74705SXin LiIf a method with a related result type is overridden by a subclass method, the 1095*67e74705SXin Lisubclass method must also return a type that is compatible with the subclass 1096*67e74705SXin Litype. For example: 1097*67e74705SXin Li 1098*67e74705SXin Li.. code-block:: objc 1099*67e74705SXin Li 1100*67e74705SXin Li @interface NSString : NSObject 1101*67e74705SXin Li - (NSUnrelated *)init; // incorrect usage: NSUnrelated is not NSString or a superclass of NSString 1102*67e74705SXin Li @end 1103*67e74705SXin Li 1104*67e74705SXin LiRelated result types only affect the type of a message send or property access 1105*67e74705SXin Livia the given method. In all other respects, a method with a related result 1106*67e74705SXin Litype is treated the same way as method that returns ``id``. 1107*67e74705SXin Li 1108*67e74705SXin LiUse ``__has_feature(objc_instancetype)`` to determine whether the 1109*67e74705SXin Li``instancetype`` contextual keyword is available. 1110*67e74705SXin Li 1111*67e74705SXin LiAutomatic reference counting 1112*67e74705SXin Li---------------------------- 1113*67e74705SXin Li 1114*67e74705SXin LiClang provides support for :doc:`automated reference counting 1115*67e74705SXin Li<AutomaticReferenceCounting>` in Objective-C, which eliminates the need 1116*67e74705SXin Lifor manual ``retain``/``release``/``autorelease`` message sends. There are two 1117*67e74705SXin Lifeature macros associated with automatic reference counting: 1118*67e74705SXin Li``__has_feature(objc_arc)`` indicates the availability of automated reference 1119*67e74705SXin Licounting in general, while ``__has_feature(objc_arc_weak)`` indicates that 1120*67e74705SXin Liautomated reference counting also includes support for ``__weak`` pointers to 1121*67e74705SXin LiObjective-C objects. 1122*67e74705SXin Li 1123*67e74705SXin Li.. _objc-fixed-enum: 1124*67e74705SXin Li 1125*67e74705SXin LiEnumerations with a fixed underlying type 1126*67e74705SXin Li----------------------------------------- 1127*67e74705SXin Li 1128*67e74705SXin LiClang provides support for C++11 enumerations with a fixed underlying type 1129*67e74705SXin Liwithin Objective-C. For example, one can write an enumeration type as: 1130*67e74705SXin Li 1131*67e74705SXin Li.. code-block:: c++ 1132*67e74705SXin Li 1133*67e74705SXin Li typedef enum : unsigned char { Red, Green, Blue } Color; 1134*67e74705SXin Li 1135*67e74705SXin LiThis specifies that the underlying type, which is used to store the enumeration 1136*67e74705SXin Livalue, is ``unsigned char``. 1137*67e74705SXin Li 1138*67e74705SXin LiUse ``__has_feature(objc_fixed_enum)`` to determine whether support for fixed 1139*67e74705SXin Liunderlying types is available in Objective-C. 1140*67e74705SXin Li 1141*67e74705SXin LiInteroperability with C++11 lambdas 1142*67e74705SXin Li----------------------------------- 1143*67e74705SXin Li 1144*67e74705SXin LiClang provides interoperability between C++11 lambdas and blocks-based APIs, by 1145*67e74705SXin Lipermitting a lambda to be implicitly converted to a block pointer with the 1146*67e74705SXin Licorresponding signature. For example, consider an API such as ``NSArray``'s 1147*67e74705SXin Liarray-sorting method: 1148*67e74705SXin Li 1149*67e74705SXin Li.. code-block:: objc 1150*67e74705SXin Li 1151*67e74705SXin Li - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr; 1152*67e74705SXin Li 1153*67e74705SXin Li``NSComparator`` is simply a typedef for the block pointer ``NSComparisonResult 1154*67e74705SXin Li(^)(id, id)``, and parameters of this type are generally provided with block 1155*67e74705SXin Liliterals as arguments. However, one can also use a C++11 lambda so long as it 1156*67e74705SXin Liprovides the same signature (in this case, accepting two parameters of type 1157*67e74705SXin Li``id`` and returning an ``NSComparisonResult``): 1158*67e74705SXin Li 1159*67e74705SXin Li.. code-block:: objc 1160*67e74705SXin Li 1161*67e74705SXin Li NSArray *array = @[@"string 1", @"string 21", @"string 12", @"String 11", 1162*67e74705SXin Li @"String 02"]; 1163*67e74705SXin Li const NSStringCompareOptions comparisonOptions 1164*67e74705SXin Li = NSCaseInsensitiveSearch | NSNumericSearch | 1165*67e74705SXin Li NSWidthInsensitiveSearch | NSForcedOrderingSearch; 1166*67e74705SXin Li NSLocale *currentLocale = [NSLocale currentLocale]; 1167*67e74705SXin Li NSArray *sorted 1168*67e74705SXin Li = [array sortedArrayUsingComparator:[=](id s1, id s2) -> NSComparisonResult { 1169*67e74705SXin Li NSRange string1Range = NSMakeRange(0, [s1 length]); 1170*67e74705SXin Li return [s1 compare:s2 options:comparisonOptions 1171*67e74705SXin Li range:string1Range locale:currentLocale]; 1172*67e74705SXin Li }]; 1173*67e74705SXin Li NSLog(@"sorted: %@", sorted); 1174*67e74705SXin Li 1175*67e74705SXin LiThis code relies on an implicit conversion from the type of the lambda 1176*67e74705SXin Liexpression (an unnamed, local class type called the *closure type*) to the 1177*67e74705SXin Licorresponding block pointer type. The conversion itself is expressed by a 1178*67e74705SXin Liconversion operator in that closure type that produces a block pointer with the 1179*67e74705SXin Lisame signature as the lambda itself, e.g., 1180*67e74705SXin Li 1181*67e74705SXin Li.. code-block:: objc 1182*67e74705SXin Li 1183*67e74705SXin Li operator NSComparisonResult (^)(id, id)() const; 1184*67e74705SXin Li 1185*67e74705SXin LiThis conversion function returns a new block that simply forwards the two 1186*67e74705SXin Liparameters to the lambda object (which it captures by copy), then returns the 1187*67e74705SXin Liresult. The returned block is first copied (with ``Block_copy``) and then 1188*67e74705SXin Liautoreleased. As an optimization, if a lambda expression is immediately 1189*67e74705SXin Liconverted to a block pointer (as in the first example, above), then the block 1190*67e74705SXin Liis not copied and autoreleased: rather, it is given the same lifetime as a 1191*67e74705SXin Liblock literal written at that point in the program, which avoids the overhead 1192*67e74705SXin Liof copying a block to the heap in the common case. 1193*67e74705SXin Li 1194*67e74705SXin LiThe conversion from a lambda to a block pointer is only available in 1195*67e74705SXin LiObjective-C++, and not in C++ with blocks, due to its use of Objective-C memory 1196*67e74705SXin Limanagement (autorelease). 1197*67e74705SXin Li 1198*67e74705SXin LiObject Literals and Subscripting 1199*67e74705SXin Li-------------------------------- 1200*67e74705SXin Li 1201*67e74705SXin LiClang provides support for :doc:`Object Literals and Subscripting 1202*67e74705SXin Li<ObjectiveCLiterals>` in Objective-C, which simplifies common Objective-C 1203*67e74705SXin Liprogramming patterns, makes programs more concise, and improves the safety of 1204*67e74705SXin Licontainer creation. There are several feature macros associated with object 1205*67e74705SXin Liliterals and subscripting: ``__has_feature(objc_array_literals)`` tests the 1206*67e74705SXin Liavailability of array literals; ``__has_feature(objc_dictionary_literals)`` 1207*67e74705SXin Litests the availability of dictionary literals; 1208*67e74705SXin Li``__has_feature(objc_subscripting)`` tests the availability of object 1209*67e74705SXin Lisubscripting. 1210*67e74705SXin Li 1211*67e74705SXin LiObjective-C Autosynthesis of Properties 1212*67e74705SXin Li--------------------------------------- 1213*67e74705SXin Li 1214*67e74705SXin LiClang provides support for autosynthesis of declared properties. Using this 1215*67e74705SXin Lifeature, clang provides default synthesis of those properties not declared 1216*67e74705SXin Li@dynamic and not having user provided backing getter and setter methods. 1217*67e74705SXin Li``__has_feature(objc_default_synthesize_properties)`` checks for availability 1218*67e74705SXin Liof this feature in version of clang being used. 1219*67e74705SXin Li 1220*67e74705SXin Li.. _langext-objc-retain-release: 1221*67e74705SXin Li 1222*67e74705SXin LiObjective-C retaining behavior attributes 1223*67e74705SXin Li----------------------------------------- 1224*67e74705SXin Li 1225*67e74705SXin LiIn Objective-C, functions and methods are generally assumed to follow the 1226*67e74705SXin Li`Cocoa Memory Management 1227*67e74705SXin Li<http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html>`_ 1228*67e74705SXin Liconventions for ownership of object arguments and 1229*67e74705SXin Lireturn values. However, there are exceptions, and so Clang provides attributes 1230*67e74705SXin Lito allow these exceptions to be documented. This are used by ARC and the 1231*67e74705SXin Li`static analyzer <http://clang-analyzer.llvm.org>`_ Some exceptions may be 1232*67e74705SXin Libetter described using the ``objc_method_family`` attribute instead. 1233*67e74705SXin Li 1234*67e74705SXin Li**Usage**: The ``ns_returns_retained``, ``ns_returns_not_retained``, 1235*67e74705SXin Li``ns_returns_autoreleased``, ``cf_returns_retained``, and 1236*67e74705SXin Li``cf_returns_not_retained`` attributes can be placed on methods and functions 1237*67e74705SXin Lithat return Objective-C or CoreFoundation objects. They are commonly placed at 1238*67e74705SXin Lithe end of a function prototype or method declaration: 1239*67e74705SXin Li 1240*67e74705SXin Li.. code-block:: objc 1241*67e74705SXin Li 1242*67e74705SXin Li id foo() __attribute__((ns_returns_retained)); 1243*67e74705SXin Li 1244*67e74705SXin Li - (NSString *)bar:(int)x __attribute__((ns_returns_retained)); 1245*67e74705SXin Li 1246*67e74705SXin LiThe ``*_returns_retained`` attributes specify that the returned object has a +1 1247*67e74705SXin Liretain count. The ``*_returns_not_retained`` attributes specify that the return 1248*67e74705SXin Liobject has a +0 retain count, even if the normal convention for its selector 1249*67e74705SXin Liwould be +1. ``ns_returns_autoreleased`` specifies that the returned object is 1250*67e74705SXin Li+0, but is guaranteed to live at least as long as the next flush of an 1251*67e74705SXin Liautorelease pool. 1252*67e74705SXin Li 1253*67e74705SXin Li**Usage**: The ``ns_consumed`` and ``cf_consumed`` attributes can be placed on 1254*67e74705SXin Lian parameter declaration; they specify that the argument is expected to have a 1255*67e74705SXin Li+1 retain count, which will be balanced in some way by the function or method. 1256*67e74705SXin LiThe ``ns_consumes_self`` attribute can only be placed on an Objective-C 1257*67e74705SXin Limethod; it specifies that the method expects its ``self`` parameter to have a 1258*67e74705SXin Li+1 retain count, which it will balance in some way. 1259*67e74705SXin Li 1260*67e74705SXin Li.. code-block:: objc 1261*67e74705SXin Li 1262*67e74705SXin Li void foo(__attribute__((ns_consumed)) NSString *string); 1263*67e74705SXin Li 1264*67e74705SXin Li - (void) bar __attribute__((ns_consumes_self)); 1265*67e74705SXin Li - (void) baz:(id) __attribute__((ns_consumed)) x; 1266*67e74705SXin Li 1267*67e74705SXin LiFurther examples of these attributes are available in the static analyzer's `list of annotations for analysis 1268*67e74705SXin Li<http://clang-analyzer.llvm.org/annotations.html#cocoa_mem>`_. 1269*67e74705SXin Li 1270*67e74705SXin LiQuery for these features with ``__has_attribute(ns_consumed)``, 1271*67e74705SXin Li``__has_attribute(ns_returns_retained)``, etc. 1272*67e74705SXin Li 1273*67e74705SXin Li 1274*67e74705SXin LiObjective-C++ ABI: protocol-qualifier mangling of parameters 1275*67e74705SXin Li------------------------------------------------------------ 1276*67e74705SXin Li 1277*67e74705SXin LiStarting with LLVM 3.4, Clang produces a new mangling for parameters whose 1278*67e74705SXin Litype is a qualified-``id`` (e.g., ``id<Foo>``). This mangling allows such 1279*67e74705SXin Liparameters to be differentiated from those with the regular unqualified ``id`` 1280*67e74705SXin Litype. 1281*67e74705SXin Li 1282*67e74705SXin LiThis was a non-backward compatible mangling change to the ABI. This change 1283*67e74705SXin Liallows proper overloading, and also prevents mangling conflicts with template 1284*67e74705SXin Liparameters of protocol-qualified type. 1285*67e74705SXin Li 1286*67e74705SXin LiQuery the presence of this new mangling with 1287*67e74705SXin Li``__has_feature(objc_protocol_qualifier_mangling)``. 1288*67e74705SXin Li 1289*67e74705SXin Li.. _langext-overloading: 1290*67e74705SXin Li 1291*67e74705SXin LiInitializer lists for complex numbers in C 1292*67e74705SXin Li========================================== 1293*67e74705SXin Li 1294*67e74705SXin Liclang supports an extension which allows the following in C: 1295*67e74705SXin Li 1296*67e74705SXin Li.. code-block:: c++ 1297*67e74705SXin Li 1298*67e74705SXin Li #include <math.h> 1299*67e74705SXin Li #include <complex.h> 1300*67e74705SXin Li complex float x = { 1.0f, INFINITY }; // Init to (1, Inf) 1301*67e74705SXin Li 1302*67e74705SXin LiThis construct is useful because there is no way to separately initialize the 1303*67e74705SXin Lireal and imaginary parts of a complex variable in standard C, given that clang 1304*67e74705SXin Lidoes not support ``_Imaginary``. (Clang also supports the ``__real__`` and 1305*67e74705SXin Li``__imag__`` extensions from gcc, which help in some cases, but are not usable 1306*67e74705SXin Liin static initializers.) 1307*67e74705SXin Li 1308*67e74705SXin LiNote that this extension does not allow eliding the braces; the meaning of the 1309*67e74705SXin Lifollowing two lines is different: 1310*67e74705SXin Li 1311*67e74705SXin Li.. code-block:: c++ 1312*67e74705SXin Li 1313*67e74705SXin Li complex float x[] = { { 1.0f, 1.0f } }; // [0] = (1, 1) 1314*67e74705SXin Li complex float x[] = { 1.0f, 1.0f }; // [0] = (1, 0), [1] = (1, 0) 1315*67e74705SXin Li 1316*67e74705SXin LiThis extension also works in C++ mode, as far as that goes, but does not apply 1317*67e74705SXin Lito the C++ ``std::complex``. (In C++11, list initialization allows the same 1318*67e74705SXin Lisyntax to be used with ``std::complex`` with the same meaning.) 1319*67e74705SXin Li 1320*67e74705SXin LiBuiltin Functions 1321*67e74705SXin Li================= 1322*67e74705SXin Li 1323*67e74705SXin LiClang supports a number of builtin library functions with the same syntax as 1324*67e74705SXin LiGCC, including things like ``__builtin_nan``, ``__builtin_constant_p``, 1325*67e74705SXin Li``__builtin_choose_expr``, ``__builtin_types_compatible_p``, 1326*67e74705SXin Li``__builtin_assume_aligned``, ``__sync_fetch_and_add``, etc. In addition to 1327*67e74705SXin Lithe GCC builtins, Clang supports a number of builtins that GCC does not, which 1328*67e74705SXin Liare listed here. 1329*67e74705SXin Li 1330*67e74705SXin LiPlease note that Clang does not and will not support all of the GCC builtins 1331*67e74705SXin Lifor vector operations. Instead of using builtins, you should use the functions 1332*67e74705SXin Lidefined in target-specific header files like ``<xmmintrin.h>``, which define 1333*67e74705SXin Liportable wrappers for these. Many of the Clang versions of these functions are 1334*67e74705SXin Liimplemented directly in terms of :ref:`extended vector support 1335*67e74705SXin Li<langext-vectors>` instead of builtins, in order to reduce the number of 1336*67e74705SXin Libuiltins that we need to implement. 1337*67e74705SXin Li 1338*67e74705SXin Li``__builtin_assume`` 1339*67e74705SXin Li------------------------------ 1340*67e74705SXin Li 1341*67e74705SXin Li``__builtin_assume`` is used to provide the optimizer with a boolean 1342*67e74705SXin Liinvariant that is defined to be true. 1343*67e74705SXin Li 1344*67e74705SXin Li**Syntax**: 1345*67e74705SXin Li 1346*67e74705SXin Li.. code-block:: c++ 1347*67e74705SXin Li 1348*67e74705SXin Li __builtin_assume(bool) 1349*67e74705SXin Li 1350*67e74705SXin Li**Example of Use**: 1351*67e74705SXin Li 1352*67e74705SXin Li.. code-block:: c++ 1353*67e74705SXin Li 1354*67e74705SXin Li int foo(int x) { 1355*67e74705SXin Li __builtin_assume(x != 0); 1356*67e74705SXin Li 1357*67e74705SXin Li // The optimizer may short-circuit this check using the invariant. 1358*67e74705SXin Li if (x == 0) 1359*67e74705SXin Li return do_something(); 1360*67e74705SXin Li 1361*67e74705SXin Li return do_something_else(); 1362*67e74705SXin Li } 1363*67e74705SXin Li 1364*67e74705SXin Li**Description**: 1365*67e74705SXin Li 1366*67e74705SXin LiThe boolean argument to this function is defined to be true. The optimizer may 1367*67e74705SXin Lianalyze the form of the expression provided as the argument and deduce from 1368*67e74705SXin Lithat information used to optimize the program. If the condition is violated 1369*67e74705SXin Liduring execution, the behavior is undefined. The argument itself is never 1370*67e74705SXin Lievaluated, so any side effects of the expression will be discarded. 1371*67e74705SXin Li 1372*67e74705SXin LiQuery for this feature with ``__has_builtin(__builtin_assume)``. 1373*67e74705SXin Li 1374*67e74705SXin Li``__builtin_readcyclecounter`` 1375*67e74705SXin Li------------------------------ 1376*67e74705SXin Li 1377*67e74705SXin Li``__builtin_readcyclecounter`` is used to access the cycle counter register (or 1378*67e74705SXin Lia similar low-latency, high-accuracy clock) on those targets that support it. 1379*67e74705SXin Li 1380*67e74705SXin Li**Syntax**: 1381*67e74705SXin Li 1382*67e74705SXin Li.. code-block:: c++ 1383*67e74705SXin Li 1384*67e74705SXin Li __builtin_readcyclecounter() 1385*67e74705SXin Li 1386*67e74705SXin Li**Example of Use**: 1387*67e74705SXin Li 1388*67e74705SXin Li.. code-block:: c++ 1389*67e74705SXin Li 1390*67e74705SXin Li unsigned long long t0 = __builtin_readcyclecounter(); 1391*67e74705SXin Li do_something(); 1392*67e74705SXin Li unsigned long long t1 = __builtin_readcyclecounter(); 1393*67e74705SXin Li unsigned long long cycles_to_do_something = t1 - t0; // assuming no overflow 1394*67e74705SXin Li 1395*67e74705SXin Li**Description**: 1396*67e74705SXin Li 1397*67e74705SXin LiThe ``__builtin_readcyclecounter()`` builtin returns the cycle counter value, 1398*67e74705SXin Liwhich may be either global or process/thread-specific depending on the target. 1399*67e74705SXin LiAs the backing counters often overflow quickly (on the order of seconds) this 1400*67e74705SXin Lishould only be used for timing small intervals. When not supported by the 1401*67e74705SXin Litarget, the return value is always zero. This builtin takes no arguments and 1402*67e74705SXin Liproduces an unsigned long long result. 1403*67e74705SXin Li 1404*67e74705SXin LiQuery for this feature with ``__has_builtin(__builtin_readcyclecounter)``. Note 1405*67e74705SXin Lithat even if present, its use may depend on run-time privilege or other OS 1406*67e74705SXin Licontrolled state. 1407*67e74705SXin Li 1408*67e74705SXin Li.. _langext-__builtin_shufflevector: 1409*67e74705SXin Li 1410*67e74705SXin Li``__builtin_shufflevector`` 1411*67e74705SXin Li--------------------------- 1412*67e74705SXin Li 1413*67e74705SXin Li``__builtin_shufflevector`` is used to express generic vector 1414*67e74705SXin Lipermutation/shuffle/swizzle operations. This builtin is also very important 1415*67e74705SXin Lifor the implementation of various target-specific header files like 1416*67e74705SXin Li``<xmmintrin.h>``. 1417*67e74705SXin Li 1418*67e74705SXin Li**Syntax**: 1419*67e74705SXin Li 1420*67e74705SXin Li.. code-block:: c++ 1421*67e74705SXin Li 1422*67e74705SXin Li __builtin_shufflevector(vec1, vec2, index1, index2, ...) 1423*67e74705SXin Li 1424*67e74705SXin Li**Examples**: 1425*67e74705SXin Li 1426*67e74705SXin Li.. code-block:: c++ 1427*67e74705SXin Li 1428*67e74705SXin Li // identity operation - return 4-element vector v1. 1429*67e74705SXin Li __builtin_shufflevector(v1, v1, 0, 1, 2, 3) 1430*67e74705SXin Li 1431*67e74705SXin Li // "Splat" element 0 of V1 into a 4-element result. 1432*67e74705SXin Li __builtin_shufflevector(V1, V1, 0, 0, 0, 0) 1433*67e74705SXin Li 1434*67e74705SXin Li // Reverse 4-element vector V1. 1435*67e74705SXin Li __builtin_shufflevector(V1, V1, 3, 2, 1, 0) 1436*67e74705SXin Li 1437*67e74705SXin Li // Concatenate every other element of 4-element vectors V1 and V2. 1438*67e74705SXin Li __builtin_shufflevector(V1, V2, 0, 2, 4, 6) 1439*67e74705SXin Li 1440*67e74705SXin Li // Concatenate every other element of 8-element vectors V1 and V2. 1441*67e74705SXin Li __builtin_shufflevector(V1, V2, 0, 2, 4, 6, 8, 10, 12, 14) 1442*67e74705SXin Li 1443*67e74705SXin Li // Shuffle v1 with some elements being undefined 1444*67e74705SXin Li __builtin_shufflevector(v1, v1, 3, -1, 1, -1) 1445*67e74705SXin Li 1446*67e74705SXin Li**Description**: 1447*67e74705SXin Li 1448*67e74705SXin LiThe first two arguments to ``__builtin_shufflevector`` are vectors that have 1449*67e74705SXin Lithe same element type. The remaining arguments are a list of integers that 1450*67e74705SXin Lispecify the elements indices of the first two vectors that should be extracted 1451*67e74705SXin Liand returned in a new vector. These element indices are numbered sequentially 1452*67e74705SXin Listarting with the first vector, continuing into the second vector. Thus, if 1453*67e74705SXin Li``vec1`` is a 4-element vector, index 5 would refer to the second element of 1454*67e74705SXin Li``vec2``. An index of -1 can be used to indicate that the corresponding element 1455*67e74705SXin Liin the returned vector is a don't care and can be optimized by the backend. 1456*67e74705SXin Li 1457*67e74705SXin LiThe result of ``__builtin_shufflevector`` is a vector with the same element 1458*67e74705SXin Litype as ``vec1``/``vec2`` but that has an element count equal to the number of 1459*67e74705SXin Liindices specified. 1460*67e74705SXin Li 1461*67e74705SXin LiQuery for this feature with ``__has_builtin(__builtin_shufflevector)``. 1462*67e74705SXin Li 1463*67e74705SXin Li.. _langext-__builtin_convertvector: 1464*67e74705SXin Li 1465*67e74705SXin Li``__builtin_convertvector`` 1466*67e74705SXin Li--------------------------- 1467*67e74705SXin Li 1468*67e74705SXin Li``__builtin_convertvector`` is used to express generic vector 1469*67e74705SXin Litype-conversion operations. The input vector and the output vector 1470*67e74705SXin Litype must have the same number of elements. 1471*67e74705SXin Li 1472*67e74705SXin Li**Syntax**: 1473*67e74705SXin Li 1474*67e74705SXin Li.. code-block:: c++ 1475*67e74705SXin Li 1476*67e74705SXin Li __builtin_convertvector(src_vec, dst_vec_type) 1477*67e74705SXin Li 1478*67e74705SXin Li**Examples**: 1479*67e74705SXin Li 1480*67e74705SXin Li.. code-block:: c++ 1481*67e74705SXin Li 1482*67e74705SXin Li typedef double vector4double __attribute__((__vector_size__(32))); 1483*67e74705SXin Li typedef float vector4float __attribute__((__vector_size__(16))); 1484*67e74705SXin Li typedef short vector4short __attribute__((__vector_size__(8))); 1485*67e74705SXin Li vector4float vf; vector4short vs; 1486*67e74705SXin Li 1487*67e74705SXin Li // convert from a vector of 4 floats to a vector of 4 doubles. 1488*67e74705SXin Li __builtin_convertvector(vf, vector4double) 1489*67e74705SXin Li // equivalent to: 1490*67e74705SXin Li (vector4double) { (double) vf[0], (double) vf[1], (double) vf[2], (double) vf[3] } 1491*67e74705SXin Li 1492*67e74705SXin Li // convert from a vector of 4 shorts to a vector of 4 floats. 1493*67e74705SXin Li __builtin_convertvector(vs, vector4float) 1494*67e74705SXin Li // equivalent to: 1495*67e74705SXin Li (vector4float) { (float) vs[0], (float) vs[1], (float) vs[2], (float) vs[3] } 1496*67e74705SXin Li 1497*67e74705SXin Li**Description**: 1498*67e74705SXin Li 1499*67e74705SXin LiThe first argument to ``__builtin_convertvector`` is a vector, and the second 1500*67e74705SXin Liargument is a vector type with the same number of elements as the first 1501*67e74705SXin Liargument. 1502*67e74705SXin Li 1503*67e74705SXin LiThe result of ``__builtin_convertvector`` is a vector with the same element 1504*67e74705SXin Litype as the second argument, with a value defined in terms of the action of a 1505*67e74705SXin LiC-style cast applied to each element of the first argument. 1506*67e74705SXin Li 1507*67e74705SXin LiQuery for this feature with ``__has_builtin(__builtin_convertvector)``. 1508*67e74705SXin Li 1509*67e74705SXin Li``__builtin_bitreverse`` 1510*67e74705SXin Li------------------------ 1511*67e74705SXin Li 1512*67e74705SXin Li* ``__builtin_bitreverse8`` 1513*67e74705SXin Li* ``__builtin_bitreverse16`` 1514*67e74705SXin Li* ``__builtin_bitreverse32`` 1515*67e74705SXin Li* ``__builtin_bitreverse64`` 1516*67e74705SXin Li 1517*67e74705SXin Li**Syntax**: 1518*67e74705SXin Li 1519*67e74705SXin Li.. code-block:: c++ 1520*67e74705SXin Li 1521*67e74705SXin Li __builtin_bitreverse32(x) 1522*67e74705SXin Li 1523*67e74705SXin Li**Examples**: 1524*67e74705SXin Li 1525*67e74705SXin Li.. code-block:: c++ 1526*67e74705SXin Li 1527*67e74705SXin Li uint8_t rev_x = __builtin_bitreverse8(x); 1528*67e74705SXin Li uint16_t rev_x = __builtin_bitreverse16(x); 1529*67e74705SXin Li uint32_t rev_y = __builtin_bitreverse32(y); 1530*67e74705SXin Li uint64_t rev_z = __builtin_bitreverse64(z); 1531*67e74705SXin Li 1532*67e74705SXin Li**Description**: 1533*67e74705SXin Li 1534*67e74705SXin LiThe '``__builtin_bitreverse``' family of builtins is used to reverse 1535*67e74705SXin Lithe bitpattern of an integer value; for example ``0b10110110`` becomes 1536*67e74705SXin Li``0b01101101``. 1537*67e74705SXin Li 1538*67e74705SXin Li``__builtin_unreachable`` 1539*67e74705SXin Li------------------------- 1540*67e74705SXin Li 1541*67e74705SXin Li``__builtin_unreachable`` is used to indicate that a specific point in the 1542*67e74705SXin Liprogram cannot be reached, even if the compiler might otherwise think it can. 1543*67e74705SXin LiThis is useful to improve optimization and eliminates certain warnings. For 1544*67e74705SXin Liexample, without the ``__builtin_unreachable`` in the example below, the 1545*67e74705SXin Licompiler assumes that the inline asm can fall through and prints a "function 1546*67e74705SXin Lideclared '``noreturn``' should not return" warning. 1547*67e74705SXin Li 1548*67e74705SXin Li**Syntax**: 1549*67e74705SXin Li 1550*67e74705SXin Li.. code-block:: c++ 1551*67e74705SXin Li 1552*67e74705SXin Li __builtin_unreachable() 1553*67e74705SXin Li 1554*67e74705SXin Li**Example of use**: 1555*67e74705SXin Li 1556*67e74705SXin Li.. code-block:: c++ 1557*67e74705SXin Li 1558*67e74705SXin Li void myabort(void) __attribute__((noreturn)); 1559*67e74705SXin Li void myabort(void) { 1560*67e74705SXin Li asm("int3"); 1561*67e74705SXin Li __builtin_unreachable(); 1562*67e74705SXin Li } 1563*67e74705SXin Li 1564*67e74705SXin Li**Description**: 1565*67e74705SXin Li 1566*67e74705SXin LiThe ``__builtin_unreachable()`` builtin has completely undefined behavior. 1567*67e74705SXin LiSince it has undefined behavior, it is a statement that it is never reached and 1568*67e74705SXin Lithe optimizer can take advantage of this to produce better code. This builtin 1569*67e74705SXin Litakes no arguments and produces a void result. 1570*67e74705SXin Li 1571*67e74705SXin LiQuery for this feature with ``__has_builtin(__builtin_unreachable)``. 1572*67e74705SXin Li 1573*67e74705SXin Li``__builtin_unpredictable`` 1574*67e74705SXin Li--------------------------- 1575*67e74705SXin Li 1576*67e74705SXin Li``__builtin_unpredictable`` is used to indicate that a branch condition is 1577*67e74705SXin Liunpredictable by hardware mechanisms such as branch prediction logic. 1578*67e74705SXin Li 1579*67e74705SXin Li**Syntax**: 1580*67e74705SXin Li 1581*67e74705SXin Li.. code-block:: c++ 1582*67e74705SXin Li 1583*67e74705SXin Li __builtin_unpredictable(long long) 1584*67e74705SXin Li 1585*67e74705SXin Li**Example of use**: 1586*67e74705SXin Li 1587*67e74705SXin Li.. code-block:: c++ 1588*67e74705SXin Li 1589*67e74705SXin Li if (__builtin_unpredictable(x > 0)) { 1590*67e74705SXin Li foo(); 1591*67e74705SXin Li } 1592*67e74705SXin Li 1593*67e74705SXin Li**Description**: 1594*67e74705SXin Li 1595*67e74705SXin LiThe ``__builtin_unpredictable()`` builtin is expected to be used with control 1596*67e74705SXin Liflow conditions such as in ``if`` and ``switch`` statements. 1597*67e74705SXin Li 1598*67e74705SXin LiQuery for this feature with ``__has_builtin(__builtin_unpredictable)``. 1599*67e74705SXin Li 1600*67e74705SXin Li``__sync_swap`` 1601*67e74705SXin Li--------------- 1602*67e74705SXin Li 1603*67e74705SXin Li``__sync_swap`` is used to atomically swap integers or pointers in memory. 1604*67e74705SXin Li 1605*67e74705SXin Li**Syntax**: 1606*67e74705SXin Li 1607*67e74705SXin Li.. code-block:: c++ 1608*67e74705SXin Li 1609*67e74705SXin Li type __sync_swap(type *ptr, type value, ...) 1610*67e74705SXin Li 1611*67e74705SXin Li**Example of Use**: 1612*67e74705SXin Li 1613*67e74705SXin Li.. code-block:: c++ 1614*67e74705SXin Li 1615*67e74705SXin Li int old_value = __sync_swap(&value, new_value); 1616*67e74705SXin Li 1617*67e74705SXin Li**Description**: 1618*67e74705SXin Li 1619*67e74705SXin LiThe ``__sync_swap()`` builtin extends the existing ``__sync_*()`` family of 1620*67e74705SXin Liatomic intrinsics to allow code to atomically swap the current value with the 1621*67e74705SXin Linew value. More importantly, it helps developers write more efficient and 1622*67e74705SXin Licorrect code by avoiding expensive loops around 1623*67e74705SXin Li``__sync_bool_compare_and_swap()`` or relying on the platform specific 1624*67e74705SXin Liimplementation details of ``__sync_lock_test_and_set()``. The 1625*67e74705SXin Li``__sync_swap()`` builtin is a full barrier. 1626*67e74705SXin Li 1627*67e74705SXin Li``__builtin_addressof`` 1628*67e74705SXin Li----------------------- 1629*67e74705SXin Li 1630*67e74705SXin Li``__builtin_addressof`` performs the functionality of the built-in ``&`` 1631*67e74705SXin Lioperator, ignoring any ``operator&`` overload. This is useful in constant 1632*67e74705SXin Liexpressions in C++11, where there is no other way to take the address of an 1633*67e74705SXin Liobject that overloads ``operator&``. 1634*67e74705SXin Li 1635*67e74705SXin Li**Example of use**: 1636*67e74705SXin Li 1637*67e74705SXin Li.. code-block:: c++ 1638*67e74705SXin Li 1639*67e74705SXin Li template<typename T> constexpr T *addressof(T &value) { 1640*67e74705SXin Li return __builtin_addressof(value); 1641*67e74705SXin Li } 1642*67e74705SXin Li 1643*67e74705SXin Li``__builtin_operator_new`` and ``__builtin_operator_delete`` 1644*67e74705SXin Li------------------------------------------------------------ 1645*67e74705SXin Li 1646*67e74705SXin Li``__builtin_operator_new`` allocates memory just like a non-placement non-class 1647*67e74705SXin Li*new-expression*. This is exactly like directly calling the normal 1648*67e74705SXin Linon-placement ``::operator new``, except that it allows certain optimizations 1649*67e74705SXin Lithat the C++ standard does not permit for a direct function call to 1650*67e74705SXin Li``::operator new`` (in particular, removing ``new`` / ``delete`` pairs and 1651*67e74705SXin Limerging allocations). 1652*67e74705SXin Li 1653*67e74705SXin LiLikewise, ``__builtin_operator_delete`` deallocates memory just like a 1654*67e74705SXin Linon-class *delete-expression*, and is exactly like directly calling the normal 1655*67e74705SXin Li``::operator delete``, except that it permits optimizations. Only the unsized 1656*67e74705SXin Liform of ``__builtin_operator_delete`` is currently available. 1657*67e74705SXin Li 1658*67e74705SXin LiThese builtins are intended for use in the implementation of ``std::allocator`` 1659*67e74705SXin Liand other similar allocation libraries, and are only available in C++. 1660*67e74705SXin Li 1661*67e74705SXin LiMultiprecision Arithmetic Builtins 1662*67e74705SXin Li---------------------------------- 1663*67e74705SXin Li 1664*67e74705SXin LiClang provides a set of builtins which expose multiprecision arithmetic in a 1665*67e74705SXin Limanner amenable to C. They all have the following form: 1666*67e74705SXin Li 1667*67e74705SXin Li.. code-block:: c 1668*67e74705SXin Li 1669*67e74705SXin Li unsigned x = ..., y = ..., carryin = ..., carryout; 1670*67e74705SXin Li unsigned sum = __builtin_addc(x, y, carryin, &carryout); 1671*67e74705SXin Li 1672*67e74705SXin LiThus one can form a multiprecision addition chain in the following manner: 1673*67e74705SXin Li 1674*67e74705SXin Li.. code-block:: c 1675*67e74705SXin Li 1676*67e74705SXin Li unsigned *x, *y, *z, carryin=0, carryout; 1677*67e74705SXin Li z[0] = __builtin_addc(x[0], y[0], carryin, &carryout); 1678*67e74705SXin Li carryin = carryout; 1679*67e74705SXin Li z[1] = __builtin_addc(x[1], y[1], carryin, &carryout); 1680*67e74705SXin Li carryin = carryout; 1681*67e74705SXin Li z[2] = __builtin_addc(x[2], y[2], carryin, &carryout); 1682*67e74705SXin Li carryin = carryout; 1683*67e74705SXin Li z[3] = __builtin_addc(x[3], y[3], carryin, &carryout); 1684*67e74705SXin Li 1685*67e74705SXin LiThe complete list of builtins are: 1686*67e74705SXin Li 1687*67e74705SXin Li.. code-block:: c 1688*67e74705SXin Li 1689*67e74705SXin Li unsigned char __builtin_addcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout); 1690*67e74705SXin Li unsigned short __builtin_addcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout); 1691*67e74705SXin Li unsigned __builtin_addc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout); 1692*67e74705SXin Li unsigned long __builtin_addcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout); 1693*67e74705SXin Li unsigned long long __builtin_addcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout); 1694*67e74705SXin Li unsigned char __builtin_subcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout); 1695*67e74705SXin Li unsigned short __builtin_subcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout); 1696*67e74705SXin Li unsigned __builtin_subc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout); 1697*67e74705SXin Li unsigned long __builtin_subcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout); 1698*67e74705SXin Li unsigned long long __builtin_subcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout); 1699*67e74705SXin Li 1700*67e74705SXin LiChecked Arithmetic Builtins 1701*67e74705SXin Li--------------------------- 1702*67e74705SXin Li 1703*67e74705SXin LiClang provides a set of builtins that implement checked arithmetic for security 1704*67e74705SXin Licritical applications in a manner that is fast and easily expressable in C. As 1705*67e74705SXin Lian example of their usage: 1706*67e74705SXin Li 1707*67e74705SXin Li.. code-block:: c 1708*67e74705SXin Li 1709*67e74705SXin Li errorcode_t security_critical_application(...) { 1710*67e74705SXin Li unsigned x, y, result; 1711*67e74705SXin Li ... 1712*67e74705SXin Li if (__builtin_mul_overflow(x, y, &result)) 1713*67e74705SXin Li return kErrorCodeHackers; 1714*67e74705SXin Li ... 1715*67e74705SXin Li use_multiply(result); 1716*67e74705SXin Li ... 1717*67e74705SXin Li } 1718*67e74705SXin Li 1719*67e74705SXin LiClang provides the following checked arithmetic builtins: 1720*67e74705SXin Li 1721*67e74705SXin Li.. code-block:: c 1722*67e74705SXin Li 1723*67e74705SXin Li bool __builtin_add_overflow (type1 x, type2 y, type3 *sum); 1724*67e74705SXin Li bool __builtin_sub_overflow (type1 x, type2 y, type3 *diff); 1725*67e74705SXin Li bool __builtin_mul_overflow (type1 x, type2 y, type3 *prod); 1726*67e74705SXin Li bool __builtin_uadd_overflow (unsigned x, unsigned y, unsigned *sum); 1727*67e74705SXin Li bool __builtin_uaddl_overflow (unsigned long x, unsigned long y, unsigned long *sum); 1728*67e74705SXin Li bool __builtin_uaddll_overflow(unsigned long long x, unsigned long long y, unsigned long long *sum); 1729*67e74705SXin Li bool __builtin_usub_overflow (unsigned x, unsigned y, unsigned *diff); 1730*67e74705SXin Li bool __builtin_usubl_overflow (unsigned long x, unsigned long y, unsigned long *diff); 1731*67e74705SXin Li bool __builtin_usubll_overflow(unsigned long long x, unsigned long long y, unsigned long long *diff); 1732*67e74705SXin Li bool __builtin_umul_overflow (unsigned x, unsigned y, unsigned *prod); 1733*67e74705SXin Li bool __builtin_umull_overflow (unsigned long x, unsigned long y, unsigned long *prod); 1734*67e74705SXin Li bool __builtin_umulll_overflow(unsigned long long x, unsigned long long y, unsigned long long *prod); 1735*67e74705SXin Li bool __builtin_sadd_overflow (int x, int y, int *sum); 1736*67e74705SXin Li bool __builtin_saddl_overflow (long x, long y, long *sum); 1737*67e74705SXin Li bool __builtin_saddll_overflow(long long x, long long y, long long *sum); 1738*67e74705SXin Li bool __builtin_ssub_overflow (int x, int y, int *diff); 1739*67e74705SXin Li bool __builtin_ssubl_overflow (long x, long y, long *diff); 1740*67e74705SXin Li bool __builtin_ssubll_overflow(long long x, long long y, long long *diff); 1741*67e74705SXin Li bool __builtin_smul_overflow (int x, int y, int *prod); 1742*67e74705SXin Li bool __builtin_smull_overflow (long x, long y, long *prod); 1743*67e74705SXin Li bool __builtin_smulll_overflow(long long x, long long y, long long *prod); 1744*67e74705SXin Li 1745*67e74705SXin LiEach builtin performs the specified mathematical operation on the 1746*67e74705SXin Lifirst two arguments and stores the result in the third argument. If 1747*67e74705SXin Lipossible, the result will be equal to mathematically-correct result 1748*67e74705SXin Liand the builtin will return 0. Otherwise, the builtin will return 1749*67e74705SXin Li1 and the result will be equal to the unique value that is equivalent 1750*67e74705SXin Lito the mathematically-correct result modulo two raised to the *k* 1751*67e74705SXin Lipower, where *k* is the number of bits in the result type. The 1752*67e74705SXin Libehavior of these builtins is well-defined for all argument values. 1753*67e74705SXin Li 1754*67e74705SXin LiThe first three builtins work generically for operands of any integer type, 1755*67e74705SXin Liincluding boolean types. The operands need not have the same type as each 1756*67e74705SXin Liother, or as the result. The other builtins may implicitly promote or 1757*67e74705SXin Liconvert their operands before performing the operation. 1758*67e74705SXin Li 1759*67e74705SXin LiQuery for this feature with ``__has_builtin(__builtin_add_overflow)``, etc. 1760*67e74705SXin Li 1761*67e74705SXin LiFloating point builtins 1762*67e74705SXin Li--------------------------------------- 1763*67e74705SXin Li 1764*67e74705SXin Li``__builtin_canonicalize`` 1765*67e74705SXin Li-------------------------- 1766*67e74705SXin Li 1767*67e74705SXin Li.. code-block:: c 1768*67e74705SXin Li 1769*67e74705SXin Li double __builtin_canonicalize(double); 1770*67e74705SXin Li float __builtin_canonicalizef(float); 1771*67e74705SXin Li long double__builtin_canonicalizel(long double); 1772*67e74705SXin Li 1773*67e74705SXin LiReturns the platform specific canonical encoding of a floating point 1774*67e74705SXin Linumber. This canonicalization is useful for implementing certain 1775*67e74705SXin Linumeric primitives such as frexp. See `LLVM canonicalize intrinsic 1776*67e74705SXin Li<http://llvm.org/docs/LangRef.html#llvm-canonicalize-intrinsic>`_ for 1777*67e74705SXin Limore information on the semantics. 1778*67e74705SXin Li 1779*67e74705SXin Li.. _langext-__c11_atomic: 1780*67e74705SXin Li 1781*67e74705SXin Li__c11_atomic builtins 1782*67e74705SXin Li--------------------- 1783*67e74705SXin Li 1784*67e74705SXin LiClang provides a set of builtins which are intended to be used to implement 1785*67e74705SXin LiC11's ``<stdatomic.h>`` header. These builtins provide the semantics of the 1786*67e74705SXin Li``_explicit`` form of the corresponding C11 operation, and are named with a 1787*67e74705SXin Li``__c11_`` prefix. The supported operations, and the differences from 1788*67e74705SXin Lithe corresponding C11 operations, are: 1789*67e74705SXin Li 1790*67e74705SXin Li* ``__c11_atomic_init`` 1791*67e74705SXin Li* ``__c11_atomic_thread_fence`` 1792*67e74705SXin Li* ``__c11_atomic_signal_fence`` 1793*67e74705SXin Li* ``__c11_atomic_is_lock_free`` (The argument is the size of the 1794*67e74705SXin Li ``_Atomic(...)`` object, instead of its address) 1795*67e74705SXin Li* ``__c11_atomic_store`` 1796*67e74705SXin Li* ``__c11_atomic_load`` 1797*67e74705SXin Li* ``__c11_atomic_exchange`` 1798*67e74705SXin Li* ``__c11_atomic_compare_exchange_strong`` 1799*67e74705SXin Li* ``__c11_atomic_compare_exchange_weak`` 1800*67e74705SXin Li* ``__c11_atomic_fetch_add`` 1801*67e74705SXin Li* ``__c11_atomic_fetch_sub`` 1802*67e74705SXin Li* ``__c11_atomic_fetch_and`` 1803*67e74705SXin Li* ``__c11_atomic_fetch_or`` 1804*67e74705SXin Li* ``__c11_atomic_fetch_xor`` 1805*67e74705SXin Li 1806*67e74705SXin LiThe macros ``__ATOMIC_RELAXED``, ``__ATOMIC_CONSUME``, ``__ATOMIC_ACQUIRE``, 1807*67e74705SXin Li``__ATOMIC_RELEASE``, ``__ATOMIC_ACQ_REL``, and ``__ATOMIC_SEQ_CST`` are 1808*67e74705SXin Liprovided, with values corresponding to the enumerators of C11's 1809*67e74705SXin Li``memory_order`` enumeration. 1810*67e74705SXin Li 1811*67e74705SXin Li(Note that Clang additionally provides GCC-compatible ``__atomic_*`` 1812*67e74705SXin Libuiltins) 1813*67e74705SXin Li 1814*67e74705SXin LiLow-level ARM exclusive memory builtins 1815*67e74705SXin Li--------------------------------------- 1816*67e74705SXin Li 1817*67e74705SXin LiClang provides overloaded builtins giving direct access to the three key ARM 1818*67e74705SXin Liinstructions for implementing atomic operations. 1819*67e74705SXin Li 1820*67e74705SXin Li.. code-block:: c 1821*67e74705SXin Li 1822*67e74705SXin Li T __builtin_arm_ldrex(const volatile T *addr); 1823*67e74705SXin Li T __builtin_arm_ldaex(const volatile T *addr); 1824*67e74705SXin Li int __builtin_arm_strex(T val, volatile T *addr); 1825*67e74705SXin Li int __builtin_arm_stlex(T val, volatile T *addr); 1826*67e74705SXin Li void __builtin_arm_clrex(void); 1827*67e74705SXin Li 1828*67e74705SXin LiThe types ``T`` currently supported are: 1829*67e74705SXin Li 1830*67e74705SXin Li* Integer types with width at most 64 bits (or 128 bits on AArch64). 1831*67e74705SXin Li* Floating-point types 1832*67e74705SXin Li* Pointer types. 1833*67e74705SXin Li 1834*67e74705SXin LiNote that the compiler does not guarantee it will not insert stores which clear 1835*67e74705SXin Lithe exclusive monitor in between an ``ldrex`` type operation and its paired 1836*67e74705SXin Li``strex``. In practice this is only usually a risk when the extra store is on 1837*67e74705SXin Lithe same cache line as the variable being modified and Clang will only insert 1838*67e74705SXin Listack stores on its own, so it is best not to use these operations on variables 1839*67e74705SXin Liwith automatic storage duration. 1840*67e74705SXin Li 1841*67e74705SXin LiAlso, loads and stores may be implicit in code written between the ``ldrex`` and 1842*67e74705SXin Li``strex``. Clang will not necessarily mitigate the effects of these either, so 1843*67e74705SXin Licare should be exercised. 1844*67e74705SXin Li 1845*67e74705SXin LiFor these reasons the higher level atomic primitives should be preferred where 1846*67e74705SXin Lipossible. 1847*67e74705SXin Li 1848*67e74705SXin LiNon-temporal load/store builtins 1849*67e74705SXin Li-------------------------------- 1850*67e74705SXin Li 1851*67e74705SXin LiClang provides overloaded builtins allowing generation of non-temporal memory 1852*67e74705SXin Liaccesses. 1853*67e74705SXin Li 1854*67e74705SXin Li.. code-block:: c 1855*67e74705SXin Li 1856*67e74705SXin Li T __builtin_nontemporal_load(T *addr); 1857*67e74705SXin Li void __builtin_nontemporal_store(T value, T *addr); 1858*67e74705SXin Li 1859*67e74705SXin LiThe types ``T`` currently supported are: 1860*67e74705SXin Li 1861*67e74705SXin Li* Integer types. 1862*67e74705SXin Li* Floating-point types. 1863*67e74705SXin Li* Vector types. 1864*67e74705SXin Li 1865*67e74705SXin LiNote that the compiler does not guarantee that non-temporal loads or stores 1866*67e74705SXin Liwill be used. 1867*67e74705SXin Li 1868*67e74705SXin LiNon-standard C++11 Attributes 1869*67e74705SXin Li============================= 1870*67e74705SXin Li 1871*67e74705SXin LiClang's non-standard C++11 attributes live in the ``clang`` attribute 1872*67e74705SXin Linamespace. 1873*67e74705SXin Li 1874*67e74705SXin LiClang supports GCC's ``gnu`` attribute namespace. All GCC attributes which 1875*67e74705SXin Liare accepted with the ``__attribute__((foo))`` syntax are also accepted as 1876*67e74705SXin Li``[[gnu::foo]]``. This only extends to attributes which are specified by GCC 1877*67e74705SXin Li(see the list of `GCC function attributes 1878*67e74705SXin Li<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_, `GCC variable 1879*67e74705SXin Liattributes <http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html>`_, and 1880*67e74705SXin Li`GCC type attributes 1881*67e74705SXin Li<http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html>`_). As with the GCC 1882*67e74705SXin Liimplementation, these attributes must appertain to the *declarator-id* in a 1883*67e74705SXin Lideclaration, which means they must go either at the start of the declaration or 1884*67e74705SXin Liimmediately after the name being declared. 1885*67e74705SXin Li 1886*67e74705SXin LiFor example, this applies the GNU ``unused`` attribute to ``a`` and ``f``, and 1887*67e74705SXin Lialso applies the GNU ``noreturn`` attribute to ``f``. 1888*67e74705SXin Li 1889*67e74705SXin Li.. code-block:: c++ 1890*67e74705SXin Li 1891*67e74705SXin Li [[gnu::unused]] int a, f [[gnu::noreturn]] (); 1892*67e74705SXin Li 1893*67e74705SXin LiTarget-Specific Extensions 1894*67e74705SXin Li========================== 1895*67e74705SXin Li 1896*67e74705SXin LiClang supports some language features conditionally on some targets. 1897*67e74705SXin Li 1898*67e74705SXin LiARM/AArch64 Language Extensions 1899*67e74705SXin Li------------------------------- 1900*67e74705SXin Li 1901*67e74705SXin LiMemory Barrier Intrinsics 1902*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^ 1903*67e74705SXin LiClang implements the ``__dmb``, ``__dsb`` and ``__isb`` intrinsics as defined 1904*67e74705SXin Liin the `ARM C Language Extensions Release 2.0 1905*67e74705SXin Li<http://infocenter.arm.com/help/topic/com.arm.doc.ihi0053c/IHI0053C_acle_2_0.pdf>`_. 1906*67e74705SXin LiNote that these intrinsics are implemented as motion barriers that block 1907*67e74705SXin Lireordering of memory accesses and side effect instructions. Other instructions 1908*67e74705SXin Lilike simple arithmetic may be reordered around the intrinsic. If you expect to 1909*67e74705SXin Lihave no reordering at all, use inline assembly instead. 1910*67e74705SXin Li 1911*67e74705SXin LiX86/X86-64 Language Extensions 1912*67e74705SXin Li------------------------------ 1913*67e74705SXin Li 1914*67e74705SXin LiThe X86 backend has these language extensions: 1915*67e74705SXin Li 1916*67e74705SXin LiMemory references to specified segments 1917*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1918*67e74705SXin Li 1919*67e74705SXin LiAnnotating a pointer with address space #256 causes it to be code generated 1920*67e74705SXin Lirelative to the X86 GS segment register, address space #257 causes it to be 1921*67e74705SXin Lirelative to the X86 FS segment, and address space #258 causes it to be 1922*67e74705SXin Lirelative to the X86 SS segment. Note that this is a very very low-level 1923*67e74705SXin Lifeature that should only be used if you know what you're doing (for example in 1924*67e74705SXin Lian OS kernel). 1925*67e74705SXin Li 1926*67e74705SXin LiHere is an example: 1927*67e74705SXin Li 1928*67e74705SXin Li.. code-block:: c++ 1929*67e74705SXin Li 1930*67e74705SXin Li #define GS_RELATIVE __attribute__((address_space(256))) 1931*67e74705SXin Li int foo(int GS_RELATIVE *P) { 1932*67e74705SXin Li return *P; 1933*67e74705SXin Li } 1934*67e74705SXin Li 1935*67e74705SXin LiWhich compiles to (on X86-32): 1936*67e74705SXin Li 1937*67e74705SXin Li.. code-block:: gas 1938*67e74705SXin Li 1939*67e74705SXin Li _foo: 1940*67e74705SXin Li movl 4(%esp), %eax 1941*67e74705SXin Li movl %gs:(%eax), %eax 1942*67e74705SXin Li ret 1943*67e74705SXin Li 1944*67e74705SXin LiExtensions for Static Analysis 1945*67e74705SXin Li============================== 1946*67e74705SXin Li 1947*67e74705SXin LiClang supports additional attributes that are useful for documenting program 1948*67e74705SXin Liinvariants and rules for static analysis tools, such as the `Clang Static 1949*67e74705SXin LiAnalyzer <http://clang-analyzer.llvm.org/>`_. These attributes are documented 1950*67e74705SXin Liin the analyzer's `list of source-level annotations 1951*67e74705SXin Li<http://clang-analyzer.llvm.org/annotations.html>`_. 1952*67e74705SXin Li 1953*67e74705SXin Li 1954*67e74705SXin LiExtensions for Dynamic Analysis 1955*67e74705SXin Li=============================== 1956*67e74705SXin Li 1957*67e74705SXin LiUse ``__has_feature(address_sanitizer)`` to check if the code is being built 1958*67e74705SXin Liwith :doc:`AddressSanitizer`. 1959*67e74705SXin Li 1960*67e74705SXin LiUse ``__has_feature(thread_sanitizer)`` to check if the code is being built 1961*67e74705SXin Liwith :doc:`ThreadSanitizer`. 1962*67e74705SXin Li 1963*67e74705SXin LiUse ``__has_feature(memory_sanitizer)`` to check if the code is being built 1964*67e74705SXin Liwith :doc:`MemorySanitizer`. 1965*67e74705SXin Li 1966*67e74705SXin LiUse ``__has_feature(safe_stack)`` to check if the code is being built 1967*67e74705SXin Liwith :doc:`SafeStack`. 1968*67e74705SXin Li 1969*67e74705SXin Li 1970*67e74705SXin LiExtensions for selectively disabling optimization 1971*67e74705SXin Li================================================= 1972*67e74705SXin Li 1973*67e74705SXin LiClang provides a mechanism for selectively disabling optimizations in functions 1974*67e74705SXin Liand methods. 1975*67e74705SXin Li 1976*67e74705SXin LiTo disable optimizations in a single function definition, the GNU-style or C++11 1977*67e74705SXin Linon-standard attribute ``optnone`` can be used. 1978*67e74705SXin Li 1979*67e74705SXin Li.. code-block:: c++ 1980*67e74705SXin Li 1981*67e74705SXin Li // The following functions will not be optimized. 1982*67e74705SXin Li // GNU-style attribute 1983*67e74705SXin Li __attribute__((optnone)) int foo() { 1984*67e74705SXin Li // ... code 1985*67e74705SXin Li } 1986*67e74705SXin Li // C++11 attribute 1987*67e74705SXin Li [[clang::optnone]] int bar() { 1988*67e74705SXin Li // ... code 1989*67e74705SXin Li } 1990*67e74705SXin Li 1991*67e74705SXin LiTo facilitate disabling optimization for a range of function definitions, a 1992*67e74705SXin Lirange-based pragma is provided. Its syntax is ``#pragma clang optimize`` 1993*67e74705SXin Lifollowed by ``off`` or ``on``. 1994*67e74705SXin Li 1995*67e74705SXin LiAll function definitions in the region between an ``off`` and the following 1996*67e74705SXin Li``on`` will be decorated with the ``optnone`` attribute unless doing so would 1997*67e74705SXin Liconflict with explicit attributes already present on the function (e.g. the 1998*67e74705SXin Liones that control inlining). 1999*67e74705SXin Li 2000*67e74705SXin Li.. code-block:: c++ 2001*67e74705SXin Li 2002*67e74705SXin Li #pragma clang optimize off 2003*67e74705SXin Li // This function will be decorated with optnone. 2004*67e74705SXin Li int foo() { 2005*67e74705SXin Li // ... code 2006*67e74705SXin Li } 2007*67e74705SXin Li 2008*67e74705SXin Li // optnone conflicts with always_inline, so bar() will not be decorated. 2009*67e74705SXin Li __attribute__((always_inline)) int bar() { 2010*67e74705SXin Li // ... code 2011*67e74705SXin Li } 2012*67e74705SXin Li #pragma clang optimize on 2013*67e74705SXin Li 2014*67e74705SXin LiIf no ``on`` is found to close an ``off`` region, the end of the region is the 2015*67e74705SXin Liend of the compilation unit. 2016*67e74705SXin Li 2017*67e74705SXin LiNote that a stray ``#pragma clang optimize on`` does not selectively enable 2018*67e74705SXin Liadditional optimizations when compiling at low optimization levels. This feature 2019*67e74705SXin Lican only be used to selectively disable optimizations. 2020*67e74705SXin Li 2021*67e74705SXin LiThe pragma has an effect on functions only at the point of their definition; for 2022*67e74705SXin Lifunction templates, this means that the state of the pragma at the point of an 2023*67e74705SXin Liinstantiation is not necessarily relevant. Consider the following example: 2024*67e74705SXin Li 2025*67e74705SXin Li.. code-block:: c++ 2026*67e74705SXin Li 2027*67e74705SXin Li template<typename T> T twice(T t) { 2028*67e74705SXin Li return 2 * t; 2029*67e74705SXin Li } 2030*67e74705SXin Li 2031*67e74705SXin Li #pragma clang optimize off 2032*67e74705SXin Li template<typename T> T thrice(T t) { 2033*67e74705SXin Li return 3 * t; 2034*67e74705SXin Li } 2035*67e74705SXin Li 2036*67e74705SXin Li int container(int a, int b) { 2037*67e74705SXin Li return twice(a) + thrice(b); 2038*67e74705SXin Li } 2039*67e74705SXin Li #pragma clang optimize on 2040*67e74705SXin Li 2041*67e74705SXin LiIn this example, the definition of the template function ``twice`` is outside 2042*67e74705SXin Lithe pragma region, whereas the definition of ``thrice`` is inside the region. 2043*67e74705SXin LiThe ``container`` function is also in the region and will not be optimized, but 2044*67e74705SXin Liit causes the instantiation of ``twice`` and ``thrice`` with an ``int`` type; of 2045*67e74705SXin Lithese two instantiations, ``twice`` will be optimized (because its definition 2046*67e74705SXin Liwas outside the region) and ``thrice`` will not be optimized. 2047*67e74705SXin Li 2048*67e74705SXin LiExtensions for loop hint optimizations 2049*67e74705SXin Li====================================== 2050*67e74705SXin Li 2051*67e74705SXin LiThe ``#pragma clang loop`` directive is used to specify hints for optimizing the 2052*67e74705SXin Lisubsequent for, while, do-while, or c++11 range-based for loop. The directive 2053*67e74705SXin Liprovides options for vectorization, interleaving, unrolling and 2054*67e74705SXin Lidistribution. Loop hints can be specified before any loop and will be ignored if 2055*67e74705SXin Lithe optimization is not safe to apply. 2056*67e74705SXin Li 2057*67e74705SXin LiVectorization and Interleaving 2058*67e74705SXin Li------------------------------ 2059*67e74705SXin Li 2060*67e74705SXin LiA vectorized loop performs multiple iterations of the original loop 2061*67e74705SXin Liin parallel using vector instructions. The instruction set of the target 2062*67e74705SXin Liprocessor determines which vector instructions are available and their vector 2063*67e74705SXin Liwidths. This restricts the types of loops that can be vectorized. The vectorizer 2064*67e74705SXin Liautomatically determines if the loop is safe and profitable to vectorize. A 2065*67e74705SXin Livector instruction cost model is used to select the vector width. 2066*67e74705SXin Li 2067*67e74705SXin LiInterleaving multiple loop iterations allows modern processors to further 2068*67e74705SXin Liimprove instruction-level parallelism (ILP) using advanced hardware features, 2069*67e74705SXin Lisuch as multiple execution units and out-of-order execution. The vectorizer uses 2070*67e74705SXin Lia cost model that depends on the register pressure and generated code size to 2071*67e74705SXin Liselect the interleaving count. 2072*67e74705SXin Li 2073*67e74705SXin LiVectorization is enabled by ``vectorize(enable)`` and interleaving is enabled 2074*67e74705SXin Liby ``interleave(enable)``. This is useful when compiling with ``-Os`` to 2075*67e74705SXin Limanually enable vectorization or interleaving. 2076*67e74705SXin Li 2077*67e74705SXin Li.. code-block:: c++ 2078*67e74705SXin Li 2079*67e74705SXin Li #pragma clang loop vectorize(enable) 2080*67e74705SXin Li #pragma clang loop interleave(enable) 2081*67e74705SXin Li for(...) { 2082*67e74705SXin Li ... 2083*67e74705SXin Li } 2084*67e74705SXin Li 2085*67e74705SXin LiThe vector width is specified by ``vectorize_width(_value_)`` and the interleave 2086*67e74705SXin Licount is specified by ``interleave_count(_value_)``, where 2087*67e74705SXin Li_value_ is a positive integer. This is useful for specifying the optimal 2088*67e74705SXin Liwidth/count of the set of target architectures supported by your application. 2089*67e74705SXin Li 2090*67e74705SXin Li.. code-block:: c++ 2091*67e74705SXin Li 2092*67e74705SXin Li #pragma clang loop vectorize_width(2) 2093*67e74705SXin Li #pragma clang loop interleave_count(2) 2094*67e74705SXin Li for(...) { 2095*67e74705SXin Li ... 2096*67e74705SXin Li } 2097*67e74705SXin Li 2098*67e74705SXin LiSpecifying a width/count of 1 disables the optimization, and is equivalent to 2099*67e74705SXin Li``vectorize(disable)`` or ``interleave(disable)``. 2100*67e74705SXin Li 2101*67e74705SXin LiLoop Unrolling 2102*67e74705SXin Li-------------- 2103*67e74705SXin Li 2104*67e74705SXin LiUnrolling a loop reduces the loop control overhead and exposes more 2105*67e74705SXin Liopportunities for ILP. Loops can be fully or partially unrolled. Full unrolling 2106*67e74705SXin Lieliminates the loop and replaces it with an enumerated sequence of loop 2107*67e74705SXin Liiterations. Full unrolling is only possible if the loop trip count is known at 2108*67e74705SXin Licompile time. Partial unrolling replicates the loop body within the loop and 2109*67e74705SXin Lireduces the trip count. 2110*67e74705SXin Li 2111*67e74705SXin LiIf ``unroll(enable)`` is specified the unroller will attempt to fully unroll the 2112*67e74705SXin Liloop if the trip count is known at compile time. If the fully unrolled code size 2113*67e74705SXin Liis greater than an internal limit the loop will be partially unrolled up to this 2114*67e74705SXin Lilimit. If the trip count is not known at compile time the loop will be partially 2115*67e74705SXin Liunrolled with a heuristically chosen unroll factor. 2116*67e74705SXin Li 2117*67e74705SXin Li.. code-block:: c++ 2118*67e74705SXin Li 2119*67e74705SXin Li #pragma clang loop unroll(enable) 2120*67e74705SXin Li for(...) { 2121*67e74705SXin Li ... 2122*67e74705SXin Li } 2123*67e74705SXin Li 2124*67e74705SXin LiIf ``unroll(full)`` is specified the unroller will attempt to fully unroll the 2125*67e74705SXin Liloop if the trip count is known at compile time identically to 2126*67e74705SXin Li``unroll(enable)``. However, with ``unroll(full)`` the loop will not be unrolled 2127*67e74705SXin Liif the loop count is not known at compile time. 2128*67e74705SXin Li 2129*67e74705SXin Li.. code-block:: c++ 2130*67e74705SXin Li 2131*67e74705SXin Li #pragma clang loop unroll(full) 2132*67e74705SXin Li for(...) { 2133*67e74705SXin Li ... 2134*67e74705SXin Li } 2135*67e74705SXin Li 2136*67e74705SXin LiThe unroll count can be specified explicitly with ``unroll_count(_value_)`` where 2137*67e74705SXin Li_value_ is a positive integer. If this value is greater than the trip count the 2138*67e74705SXin Liloop will be fully unrolled. Otherwise the loop is partially unrolled subject 2139*67e74705SXin Lito the same code size limit as with ``unroll(enable)``. 2140*67e74705SXin Li 2141*67e74705SXin Li.. code-block:: c++ 2142*67e74705SXin Li 2143*67e74705SXin Li #pragma clang loop unroll_count(8) 2144*67e74705SXin Li for(...) { 2145*67e74705SXin Li ... 2146*67e74705SXin Li } 2147*67e74705SXin Li 2148*67e74705SXin LiUnrolling of a loop can be prevented by specifying ``unroll(disable)``. 2149*67e74705SXin Li 2150*67e74705SXin LiLoop Distribution 2151*67e74705SXin Li----------------- 2152*67e74705SXin Li 2153*67e74705SXin LiLoop Distribution allows splitting a loop into multiple loops. This is 2154*67e74705SXin Libeneficial for example when the entire loop cannot be vectorized but some of the 2155*67e74705SXin Liresulting loops can. 2156*67e74705SXin Li 2157*67e74705SXin LiIf ``distribute(enable))`` is specified and the loop has memory dependencies 2158*67e74705SXin Lithat inhibit vectorization, the compiler will attempt to isolate the offending 2159*67e74705SXin Lioperations into a new loop. This optimization is not enabled by default, only 2160*67e74705SXin Liloops marked with the pragma are considered. 2161*67e74705SXin Li 2162*67e74705SXin Li.. code-block:: c++ 2163*67e74705SXin Li 2164*67e74705SXin Li #pragma clang loop distribute(enable) 2165*67e74705SXin Li for (i = 0; i < N; ++i) { 2166*67e74705SXin Li S1: A[i + 1] = A[i] + B[i]; 2167*67e74705SXin Li S2: C[i] = D[i] * E[i]; 2168*67e74705SXin Li } 2169*67e74705SXin Li 2170*67e74705SXin LiThis loop will be split into two loops between statements S1 and S2. The 2171*67e74705SXin Lisecond loop containing S2 will be vectorized. 2172*67e74705SXin Li 2173*67e74705SXin LiLoop Distribution is currently not enabled by default in the optimizer because 2174*67e74705SXin Liit can hurt performance in some cases. For example, instruction-level 2175*67e74705SXin Liparallelism could be reduced by sequentializing the execution of the 2176*67e74705SXin Listatements S1 and S2 above. 2177*67e74705SXin Li 2178*67e74705SXin LiIf Loop Distribution is turned on globally with 2179*67e74705SXin Li``-mllvm -enable-loop-distribution``, specifying ``distribute(disable)`` can 2180*67e74705SXin Libe used the disable it on a per-loop basis. 2181*67e74705SXin Li 2182*67e74705SXin LiAdditional Information 2183*67e74705SXin Li---------------------- 2184*67e74705SXin Li 2185*67e74705SXin LiFor convenience multiple loop hints can be specified on a single line. 2186*67e74705SXin Li 2187*67e74705SXin Li.. code-block:: c++ 2188*67e74705SXin Li 2189*67e74705SXin Li #pragma clang loop vectorize_width(4) interleave_count(8) 2190*67e74705SXin Li for(...) { 2191*67e74705SXin Li ... 2192*67e74705SXin Li } 2193*67e74705SXin Li 2194*67e74705SXin LiIf an optimization cannot be applied any hints that apply to it will be ignored. 2195*67e74705SXin LiFor example, the hint ``vectorize_width(4)`` is ignored if the loop is not 2196*67e74705SXin Liproven safe to vectorize. To identify and diagnose optimization issues use 2197*67e74705SXin Li`-Rpass`, `-Rpass-missed`, and `-Rpass-analysis` command line options. See the 2198*67e74705SXin Liuser guide for details. 2199