Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1 | ========================= |
| 2 | Clang Language Extensions |
| 3 | ========================= |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
Sean Silva | 55d3f94 | 2013-01-02 21:09:58 +0000 | [diff] [blame] | 7 | :depth: 1 |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 8 | |
Sean Silva | a0c392d | 2013-01-02 21:03:11 +0000 | [diff] [blame] | 9 | .. toctree:: |
| 10 | :hidden: |
| 11 | |
| 12 | ObjectiveCLiterals |
| 13 | BlockLanguageSpec |
Michael Gottesman | a65e076 | 2013-01-07 22:24:45 +0000 | [diff] [blame] | 14 | Block-ABI-Apple |
| 15 | AutomaticReferenceCounting |
Sean Silva | a0c392d | 2013-01-02 21:03:11 +0000 | [diff] [blame] | 16 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 17 | Introduction |
| 18 | ============ |
| 19 | |
| 20 | This document describes the language extensions provided by Clang. In addition |
| 21 | to the language extensions listed here, Clang aims to support a broad range of |
| 22 | GCC extensions. Please see the `GCC manual |
| 23 | <http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html>`_ for more information on |
| 24 | these extensions. |
| 25 | |
| 26 | .. _langext-feature_check: |
| 27 | |
| 28 | Feature Checking Macros |
| 29 | ======================= |
| 30 | |
| 31 | Language extensions can be very useful, but only if you know you can depend on |
| 32 | them. In order to allow fine-grain features checks, we support three builtin |
| 33 | function-like macros. This allows you to directly test for a feature in your |
| 34 | code without having to resort to something like autoconf or fragile "compiler |
| 35 | version checks". |
| 36 | |
| 37 | ``__has_builtin`` |
| 38 | ----------------- |
| 39 | |
| 40 | This function-like macro takes a single identifier argument that is the name of |
| 41 | a builtin function. It evaluates to 1 if the builtin is supported or 0 if not. |
| 42 | It can be used like this: |
| 43 | |
| 44 | .. code-block:: c++ |
| 45 | |
| 46 | #ifndef __has_builtin // Optional of course. |
| 47 | #define __has_builtin(x) 0 // Compatibility with non-clang compilers. |
| 48 | #endif |
| 49 | |
| 50 | ... |
| 51 | #if __has_builtin(__builtin_trap) |
| 52 | __builtin_trap(); |
| 53 | #else |
| 54 | abort(); |
| 55 | #endif |
| 56 | ... |
| 57 | |
| 58 | .. _langext-__has_feature-__has_extension: |
| 59 | |
| 60 | ``__has_feature`` and ``__has_extension`` |
| 61 | ----------------------------------------- |
| 62 | |
| 63 | These function-like macros take a single identifier argument that is the name |
| 64 | of a feature. ``__has_feature`` evaluates to 1 if the feature is both |
| 65 | supported by Clang and standardized in the current language standard or 0 if |
| 66 | not (but see :ref:`below <langext-has-feature-back-compat>`), while |
| 67 | ``__has_extension`` evaluates to 1 if the feature is supported by Clang in the |
| 68 | current language (either as a language extension or a standard language |
| 69 | feature) or 0 if not. They can be used like this: |
| 70 | |
| 71 | .. code-block:: c++ |
| 72 | |
| 73 | #ifndef __has_feature // Optional of course. |
| 74 | #define __has_feature(x) 0 // Compatibility with non-clang compilers. |
| 75 | #endif |
| 76 | #ifndef __has_extension |
| 77 | #define __has_extension __has_feature // Compatibility with pre-3.0 compilers. |
| 78 | #endif |
| 79 | |
| 80 | ... |
| 81 | #if __has_feature(cxx_rvalue_references) |
| 82 | // This code will only be compiled with the -std=c++11 and -std=gnu++11 |
| 83 | // options, because rvalue references are only standardized in C++11. |
| 84 | #endif |
| 85 | |
| 86 | #if __has_extension(cxx_rvalue_references) |
| 87 | // This code will be compiled with the -std=c++11, -std=gnu++11, -std=c++98 |
| 88 | // and -std=gnu++98 options, because rvalue references are supported as a |
| 89 | // language extension in C++98. |
| 90 | #endif |
| 91 | |
| 92 | .. _langext-has-feature-back-compat: |
| 93 | |
| 94 | For backwards compatibility reasons, ``__has_feature`` can also be used to test |
| 95 | for support for non-standardized features, i.e. features not prefixed ``c_``, |
| 96 | ``cxx_`` or ``objc_``. |
| 97 | |
| 98 | Another use of ``__has_feature`` is to check for compiler features not related |
Sean Silva | 159cc9e | 2013-01-02 13:07:47 +0000 | [diff] [blame] | 99 | to the language standard, such as e.g. :doc:`AddressSanitizer |
| 100 | <AddressSanitizer>`. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 101 | |
| 102 | If the ``-pedantic-errors`` option is given, ``__has_extension`` is equivalent |
| 103 | to ``__has_feature``. |
| 104 | |
| 105 | The feature tag is described along with the language feature below. |
| 106 | |
| 107 | The feature name or extension name can also be specified with a preceding and |
| 108 | following ``__`` (double underscore) to avoid interference from a macro with |
| 109 | the same name. For instance, ``__cxx_rvalue_references__`` can be used instead |
| 110 | of ``cxx_rvalue_references``. |
| 111 | |
| 112 | ``__has_attribute`` |
| 113 | ------------------- |
| 114 | |
| 115 | This function-like macro takes a single identifier argument that is the name of |
| 116 | an attribute. It evaluates to 1 if the attribute is supported or 0 if not. It |
| 117 | can be used like this: |
| 118 | |
| 119 | .. code-block:: c++ |
| 120 | |
| 121 | #ifndef __has_attribute // Optional of course. |
| 122 | #define __has_attribute(x) 0 // Compatibility with non-clang compilers. |
| 123 | #endif |
| 124 | |
| 125 | ... |
| 126 | #if __has_attribute(always_inline) |
| 127 | #define ALWAYS_INLINE __attribute__((always_inline)) |
| 128 | #else |
| 129 | #define ALWAYS_INLINE |
| 130 | #endif |
| 131 | ... |
| 132 | |
| 133 | The attribute name can also be specified with a preceding and following ``__`` |
| 134 | (double underscore) to avoid interference from a macro with the same name. For |
| 135 | instance, ``__always_inline__`` can be used instead of ``always_inline``. |
| 136 | |
| 137 | Include File Checking Macros |
| 138 | ============================ |
| 139 | |
| 140 | Not all developments systems have the same include files. The |
| 141 | :ref:`langext-__has_include` and :ref:`langext-__has_include_next` macros allow |
| 142 | you to check for the existence of an include file before doing a possibly |
Dmitri Gribenko | 21937c6 | 2013-01-17 17:04:54 +0000 | [diff] [blame] | 143 | failing ``#include`` directive. Include file checking macros must be used |
| 144 | as expressions in ``#if`` or ``#elif`` preprocessing directives. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 145 | |
| 146 | .. _langext-__has_include: |
| 147 | |
| 148 | ``__has_include`` |
| 149 | ----------------- |
| 150 | |
| 151 | This function-like macro takes a single file name string argument that is the |
| 152 | name of an include file. It evaluates to 1 if the file can be found using the |
| 153 | include paths, or 0 otherwise: |
| 154 | |
| 155 | .. code-block:: c++ |
| 156 | |
| 157 | // Note the two possible file name string formats. |
| 158 | #if __has_include("myinclude.h") && __has_include(<stdint.h>) |
| 159 | # include "myinclude.h" |
| 160 | #endif |
| 161 | |
| 162 | // To avoid problem with non-clang compilers not having this macro. |
| 163 | #if defined(__has_include) && __has_include("myinclude.h") |
| 164 | # include "myinclude.h" |
| 165 | #endif |
| 166 | |
| 167 | To test for this feature, use ``#if defined(__has_include)``. |
| 168 | |
| 169 | .. _langext-__has_include_next: |
| 170 | |
| 171 | ``__has_include_next`` |
| 172 | ---------------------- |
| 173 | |
| 174 | This function-like macro takes a single file name string argument that is the |
| 175 | name of an include file. It is like ``__has_include`` except that it looks for |
| 176 | the second instance of the given file found in the include paths. It evaluates |
| 177 | to 1 if the second instance of the file can be found using the include paths, |
| 178 | or 0 otherwise: |
| 179 | |
| 180 | .. code-block:: c++ |
| 181 | |
| 182 | // Note the two possible file name string formats. |
| 183 | #if __has_include_next("myinclude.h") && __has_include_next(<stdint.h>) |
| 184 | # include_next "myinclude.h" |
| 185 | #endif |
| 186 | |
| 187 | // To avoid problem with non-clang compilers not having this macro. |
| 188 | #if defined(__has_include_next) && __has_include_next("myinclude.h") |
| 189 | # include_next "myinclude.h" |
| 190 | #endif |
| 191 | |
| 192 | Note that ``__has_include_next``, like the GNU extension ``#include_next`` |
| 193 | directive, is intended for use in headers only, and will issue a warning if |
| 194 | used in the top-level compilation file. A warning will also be issued if an |
| 195 | absolute path is used in the file argument. |
| 196 | |
| 197 | ``__has_warning`` |
| 198 | ----------------- |
| 199 | |
| 200 | This function-like macro takes a string literal that represents a command line |
| 201 | option for a warning and returns true if that is a valid warning option. |
| 202 | |
| 203 | .. code-block:: c++ |
| 204 | |
| 205 | #if __has_warning("-Wformat") |
| 206 | ... |
| 207 | #endif |
| 208 | |
| 209 | Builtin Macros |
| 210 | ============== |
| 211 | |
| 212 | ``__BASE_FILE__`` |
| 213 | Defined to a string that contains the name of the main input file passed to |
| 214 | Clang. |
| 215 | |
| 216 | ``__COUNTER__`` |
| 217 | Defined to an integer value that starts at zero and is incremented each time |
| 218 | the ``__COUNTER__`` macro is expanded. |
| 219 | |
| 220 | ``__INCLUDE_LEVEL__`` |
| 221 | Defined to an integral value that is the include depth of the file currently |
| 222 | being translated. For the main file, this value is zero. |
| 223 | |
| 224 | ``__TIMESTAMP__`` |
| 225 | Defined to the date and time of the last modification of the current source |
| 226 | file. |
| 227 | |
| 228 | ``__clang__`` |
| 229 | Defined when compiling with Clang |
| 230 | |
| 231 | ``__clang_major__`` |
| 232 | Defined to the major marketing version number of Clang (e.g., the 2 in |
| 233 | 2.0.1). Note that marketing version numbers should not be used to check for |
| 234 | language features, as different vendors use different numbering schemes. |
| 235 | Instead, use the :ref:`langext-feature_check`. |
| 236 | |
| 237 | ``__clang_minor__`` |
| 238 | Defined to the minor version number of Clang (e.g., the 0 in 2.0.1). Note |
| 239 | that marketing version numbers should not be used to check for language |
| 240 | features, as different vendors use different numbering schemes. Instead, use |
| 241 | the :ref:`langext-feature_check`. |
| 242 | |
| 243 | ``__clang_patchlevel__`` |
| 244 | Defined to the marketing patch level of Clang (e.g., the 1 in 2.0.1). |
| 245 | |
| 246 | ``__clang_version__`` |
| 247 | Defined to a string that captures the Clang marketing version, including the |
| 248 | Subversion tag or revision number, e.g., "``1.5 (trunk 102332)``". |
| 249 | |
| 250 | .. _langext-vectors: |
| 251 | |
| 252 | Vectors and Extended Vectors |
| 253 | ============================ |
| 254 | |
| 255 | Supports the GCC, OpenCL, AltiVec and NEON vector extensions. |
| 256 | |
| 257 | OpenCL vector types are created using ``ext_vector_type`` attribute. It |
| 258 | support for ``V.xyzw`` syntax and other tidbits as seen in OpenCL. An example |
| 259 | is: |
| 260 | |
| 261 | .. code-block:: c++ |
| 262 | |
| 263 | typedef float float4 __attribute__((ext_vector_type(4))); |
| 264 | typedef float float2 __attribute__((ext_vector_type(2))); |
| 265 | |
| 266 | float4 foo(float2 a, float2 b) { |
| 267 | float4 c; |
| 268 | c.xz = a; |
| 269 | c.yw = b; |
| 270 | return c; |
| 271 | } |
| 272 | |
| 273 | Query for this feature with ``__has_extension(attribute_ext_vector_type)``. |
| 274 | |
| 275 | Giving ``-faltivec`` option to clang enables support for AltiVec vector syntax |
| 276 | and functions. For example: |
| 277 | |
| 278 | .. code-block:: c++ |
| 279 | |
| 280 | vector float foo(vector int a) { |
| 281 | vector int b; |
| 282 | b = vec_add(a, a) + a; |
| 283 | return (vector float)b; |
| 284 | } |
| 285 | |
| 286 | NEON vector types are created using ``neon_vector_type`` and |
| 287 | ``neon_polyvector_type`` attributes. For example: |
| 288 | |
| 289 | .. code-block:: c++ |
| 290 | |
| 291 | typedef __attribute__((neon_vector_type(8))) int8_t int8x8_t; |
| 292 | typedef __attribute__((neon_polyvector_type(16))) poly8_t poly8x16_t; |
| 293 | |
| 294 | int8x8_t foo(int8x8_t a) { |
| 295 | int8x8_t v; |
| 296 | v = a; |
| 297 | return v; |
| 298 | } |
| 299 | |
| 300 | Vector Literals |
| 301 | --------------- |
| 302 | |
| 303 | Vector literals can be used to create vectors from a set of scalars, or |
| 304 | vectors. Either parentheses or braces form can be used. In the parentheses |
| 305 | form the number of literal values specified must be one, i.e. referring to a |
| 306 | scalar value, or must match the size of the vector type being created. If a |
| 307 | single scalar literal value is specified, the scalar literal value will be |
| 308 | replicated to all the components of the vector type. In the brackets form any |
| 309 | number of literals can be specified. For example: |
| 310 | |
| 311 | .. code-block:: c++ |
| 312 | |
| 313 | typedef int v4si __attribute__((__vector_size__(16))); |
| 314 | typedef float float4 __attribute__((ext_vector_type(4))); |
| 315 | typedef float float2 __attribute__((ext_vector_type(2))); |
| 316 | |
| 317 | v4si vsi = (v4si){1, 2, 3, 4}; |
| 318 | float4 vf = (float4)(1.0f, 2.0f, 3.0f, 4.0f); |
| 319 | vector int vi1 = (vector int)(1); // vi1 will be (1, 1, 1, 1). |
| 320 | vector int vi2 = (vector int){1}; // vi2 will be (1, 0, 0, 0). |
| 321 | vector int vi3 = (vector int)(1, 2); // error |
| 322 | vector int vi4 = (vector int){1, 2}; // vi4 will be (1, 2, 0, 0). |
| 323 | vector int vi5 = (vector int)(1, 2, 3, 4); |
| 324 | float4 vf = (float4)((float2)(1.0f, 2.0f), (float2)(3.0f, 4.0f)); |
| 325 | |
| 326 | Vector Operations |
| 327 | ----------------- |
| 328 | |
| 329 | The table below shows the support for each operation by vector extension. A |
| 330 | dash indicates that an operation is not accepted according to a corresponding |
| 331 | specification. |
| 332 | |
| 333 | ============================== ====== ======= === ==== |
| 334 | Opeator OpenCL AltiVec GCC NEON |
| 335 | ============================== ====== ======= === ==== |
| 336 | [] yes yes yes -- |
| 337 | unary operators +, -- yes yes yes -- |
| 338 | ++, -- -- yes yes yes -- |
| 339 | +,--,*,/,% yes yes yes -- |
| 340 | bitwise operators &,|,^,~ yes yes yes -- |
| 341 | >>,<< yes yes yes -- |
| 342 | !, &&, || no -- -- -- |
| 343 | ==, !=, >, <, >=, <= yes yes -- -- |
| 344 | = yes yes yes yes |
| 345 | :? yes -- -- -- |
| 346 | sizeof yes yes yes yes |
| 347 | ============================== ====== ======= === ==== |
| 348 | |
| 349 | See also :ref:`langext-__builtin_shufflevector`. |
| 350 | |
| 351 | Messages on ``deprecated`` and ``unavailable`` Attributes |
| 352 | ========================================================= |
| 353 | |
| 354 | An optional string message can be added to the ``deprecated`` and |
| 355 | ``unavailable`` attributes. For example: |
| 356 | |
| 357 | .. code-block:: c++ |
| 358 | |
| 359 | void explode(void) __attribute__((deprecated("extremely unsafe, use 'combust' instead!!!"))); |
| 360 | |
| 361 | If the deprecated or unavailable declaration is used, the message will be |
| 362 | incorporated into the appropriate diagnostic: |
| 363 | |
| 364 | .. code-block:: c++ |
| 365 | |
| 366 | harmless.c:4:3: warning: 'explode' is deprecated: extremely unsafe, use 'combust' instead!!! |
| 367 | [-Wdeprecated-declarations] |
| 368 | explode(); |
| 369 | ^ |
| 370 | |
| 371 | Query for this feature with |
| 372 | ``__has_extension(attribute_deprecated_with_message)`` and |
| 373 | ``__has_extension(attribute_unavailable_with_message)``. |
| 374 | |
| 375 | Attributes on Enumerators |
| 376 | ========================= |
| 377 | |
| 378 | Clang allows attributes to be written on individual enumerators. This allows |
| 379 | enumerators to be deprecated, made unavailable, etc. The attribute must appear |
| 380 | after the enumerator name and before any initializer, like so: |
| 381 | |
| 382 | .. code-block:: c++ |
| 383 | |
| 384 | enum OperationMode { |
| 385 | OM_Invalid, |
| 386 | OM_Normal, |
| 387 | OM_Terrified __attribute__((deprecated)), |
| 388 | OM_AbortOnError __attribute__((deprecated)) = 4 |
| 389 | }; |
| 390 | |
| 391 | Attributes on the ``enum`` declaration do not apply to individual enumerators. |
| 392 | |
| 393 | Query for this feature with ``__has_extension(enumerator_attributes)``. |
| 394 | |
| 395 | 'User-Specified' System Frameworks |
| 396 | ================================== |
| 397 | |
| 398 | Clang provides a mechanism by which frameworks can be built in such a way that |
| 399 | they will always be treated as being "system frameworks", even if they are not |
| 400 | present in a system framework directory. This can be useful to system |
| 401 | framework developers who want to be able to test building other applications |
| 402 | with development builds of their framework, including the manner in which the |
| 403 | compiler changes warning behavior for system headers. |
| 404 | |
| 405 | Framework developers can opt-in to this mechanism by creating a |
| 406 | "``.system_framework``" file at the top-level of their framework. That is, the |
| 407 | framework should have contents like: |
| 408 | |
| 409 | .. code-block:: none |
| 410 | |
| 411 | .../TestFramework.framework |
| 412 | .../TestFramework.framework/.system_framework |
| 413 | .../TestFramework.framework/Headers |
| 414 | .../TestFramework.framework/Headers/TestFramework.h |
| 415 | ... |
| 416 | |
| 417 | Clang will treat the presence of this file as an indicator that the framework |
| 418 | should be treated as a system framework, regardless of how it was found in the |
| 419 | framework search path. For consistency, we recommend that such files never be |
| 420 | included in installed versions of the framework. |
| 421 | |
| 422 | Availability attribute |
| 423 | ====================== |
| 424 | |
| 425 | Clang introduces the ``availability`` attribute, which can be placed on |
| 426 | declarations to describe the lifecycle of that declaration relative to |
| 427 | operating system versions. Consider the function declaration for a |
| 428 | hypothetical function ``f``: |
| 429 | |
| 430 | .. code-block:: c++ |
| 431 | |
| 432 | void f(void) __attribute__((availability(macosx,introduced=10.4,deprecated=10.6,obsoleted=10.7))); |
| 433 | |
| 434 | The availability attribute states that ``f`` was introduced in Mac OS X 10.4, |
| 435 | deprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7. This information |
| 436 | is used by Clang to determine when it is safe to use ``f``: for example, if |
| 437 | Clang is instructed to compile code for Mac OS X 10.5, a call to ``f()`` |
| 438 | succeeds. If Clang is instructed to compile code for Mac OS X 10.6, the call |
| 439 | succeeds but Clang emits a warning specifying that the function is deprecated. |
| 440 | Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call |
| 441 | fails because ``f()`` is no longer available. |
| 442 | |
Douglas Gregor | 52fb37a | 2013-01-16 01:12:31 +0000 | [diff] [blame] | 443 | The availability attribute is a comma-separated list starting with the |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 444 | platform name and then including clauses specifying important milestones in the |
| 445 | declaration's lifetime (in any order) along with additional information. Those |
| 446 | clauses can be: |
| 447 | |
| 448 | introduced=\ *version* |
| 449 | The first version in which this declaration was introduced. |
| 450 | |
| 451 | deprecated=\ *version* |
| 452 | The first version in which this declaration was deprecated, meaning that |
| 453 | users should migrate away from this API. |
| 454 | |
| 455 | obsoleted=\ *version* |
| 456 | The first version in which this declaration was obsoleted, meaning that it |
| 457 | was removed completely and can no longer be used. |
| 458 | |
| 459 | unavailable |
| 460 | This declaration is never available on this platform. |
| 461 | |
| 462 | message=\ *string-literal* |
| 463 | Additional message text that Clang will provide when emitting a warning or |
| 464 | error about use of a deprecated or obsoleted declaration. Useful to direct |
| 465 | users to replacement APIs. |
| 466 | |
| 467 | Multiple availability attributes can be placed on a declaration, which may |
| 468 | correspond to different platforms. Only the availability attribute with the |
| 469 | platform corresponding to the target platform will be used; any others will be |
| 470 | ignored. If no availability attribute specifies availability for the current |
| 471 | target platform, the availability attributes are ignored. Supported platforms |
| 472 | are: |
| 473 | |
| 474 | ``ios`` |
| 475 | Apple's iOS operating system. The minimum deployment target is specified by |
| 476 | the ``-mios-version-min=*version*`` or ``-miphoneos-version-min=*version*`` |
| 477 | command-line arguments. |
| 478 | |
| 479 | ``macosx`` |
| 480 | Apple's Mac OS X operating system. The minimum deployment target is |
| 481 | specified by the ``-mmacosx-version-min=*version*`` command-line argument. |
| 482 | |
| 483 | A declaration can be used even when deploying back to a platform version prior |
| 484 | to when the declaration was introduced. When this happens, the declaration is |
| 485 | `weakly linked |
| 486 | <https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html>`_, |
| 487 | as if the ``weak_import`` attribute were added to the declaration. A |
| 488 | weakly-linked declaration may or may not be present a run-time, and a program |
| 489 | can determine whether the declaration is present by checking whether the |
| 490 | address of that declaration is non-NULL. |
| 491 | |
Dmitri Gribenko | 44a1efe | 2013-01-16 01:17:05 +0000 | [diff] [blame] | 492 | If there are multiple declarations of the same entity, the availability |
Douglas Gregor | 52fb37a | 2013-01-16 01:12:31 +0000 | [diff] [blame] | 493 | attributes must either match on a per-platform basis or later |
| 494 | declarations must not have availability attributes for that |
| 495 | platform. For example: |
| 496 | |
| 497 | .. code-block:: c |
| 498 | |
| 499 | void g(void) __attribute__((availability(macosx,introduced=10.4))); |
| 500 | void g(void) __attribute__((availability(macosx,introduced=10.4))); // okay, matches |
| 501 | void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform |
| 502 | void g(void); // okay, inherits both macosx and ios availability from above. |
| 503 | void g(void) __attribute__((availability(macosx,introduced=10.5))); // error: mismatch |
| 504 | |
| 505 | When one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,: |
| 506 | |
| 507 | .. code-block:: objc |
| 508 | |
| 509 | @interface A |
| 510 | - (id)method __attribute__((availability(macosx,introduced=10.4))); |
| 511 | - (id)method2 __attribute__((availability(macosx,introduced=10.4))); |
| 512 | @end |
| 513 | |
| 514 | @interface B : A |
| 515 | - (id)method __attribute__((availability(macosx,introduced=10.3))); // okay: method moved into base class later |
| 516 | - (id)method __attribute__((availability(macosx,introduced=10.5))); // error: this method was available via the base class in 10.4 |
| 517 | @end |
| 518 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 519 | Checks for Standard Language Features |
| 520 | ===================================== |
| 521 | |
| 522 | The ``__has_feature`` macro can be used to query if certain standard language |
| 523 | features are enabled. The ``__has_extension`` macro can be used to query if |
| 524 | language features are available as an extension when compiling for a standard |
| 525 | which does not provide them. The features which can be tested are listed here. |
| 526 | |
| 527 | C++98 |
| 528 | ----- |
| 529 | |
| 530 | The features listed below are part of the C++98 standard. These features are |
| 531 | enabled by default when compiling C++ code. |
| 532 | |
| 533 | C++ exceptions |
| 534 | ^^^^^^^^^^^^^^ |
| 535 | |
| 536 | Use ``__has_feature(cxx_exceptions)`` to determine if C++ exceptions have been |
| 537 | enabled. For example, compiling code with ``-fno-exceptions`` disables C++ |
| 538 | exceptions. |
| 539 | |
| 540 | C++ RTTI |
| 541 | ^^^^^^^^ |
| 542 | |
| 543 | Use ``__has_feature(cxx_rtti)`` to determine if C++ RTTI has been enabled. For |
| 544 | example, compiling code with ``-fno-rtti`` disables the use of RTTI. |
| 545 | |
| 546 | C++11 |
| 547 | ----- |
| 548 | |
| 549 | The features listed below are part of the C++11 standard. As a result, all |
| 550 | these features are enabled with the ``-std=c++11`` or ``-std=gnu++11`` option |
| 551 | when compiling C++ code. |
| 552 | |
| 553 | C++11 SFINAE includes access control |
| 554 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 555 | |
| 556 | Use ``__has_feature(cxx_access_control_sfinae)`` or |
| 557 | ``__has_extension(cxx_access_control_sfinae)`` to determine whether |
| 558 | access-control errors (e.g., calling a private constructor) are considered to |
| 559 | be template argument deduction errors (aka SFINAE errors), per `C++ DR1170 |
| 560 | <http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1170>`_. |
| 561 | |
| 562 | C++11 alias templates |
| 563 | ^^^^^^^^^^^^^^^^^^^^^ |
| 564 | |
| 565 | Use ``__has_feature(cxx_alias_templates)`` or |
| 566 | ``__has_extension(cxx_alias_templates)`` to determine if support for C++11's |
| 567 | alias declarations and alias templates is enabled. |
| 568 | |
| 569 | C++11 alignment specifiers |
| 570 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 571 | |
| 572 | Use ``__has_feature(cxx_alignas)`` or ``__has_extension(cxx_alignas)`` to |
| 573 | determine if support for alignment specifiers using ``alignas`` is enabled. |
| 574 | |
| 575 | C++11 attributes |
| 576 | ^^^^^^^^^^^^^^^^ |
| 577 | |
| 578 | Use ``__has_feature(cxx_attributes)`` or ``__has_extension(cxx_attributes)`` to |
| 579 | determine if support for attribute parsing with C++11's square bracket notation |
| 580 | is enabled. |
| 581 | |
| 582 | C++11 generalized constant expressions |
| 583 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 584 | |
| 585 | Use ``__has_feature(cxx_constexpr)`` to determine if support for generalized |
| 586 | constant expressions (e.g., ``constexpr``) is enabled. |
| 587 | |
| 588 | C++11 ``decltype()`` |
| 589 | ^^^^^^^^^^^^^^^^^^^^ |
| 590 | |
| 591 | Use ``__has_feature(cxx_decltype)`` or ``__has_extension(cxx_decltype)`` to |
| 592 | determine if support for the ``decltype()`` specifier is enabled. C++11's |
| 593 | ``decltype`` does not require type-completeness of a function call expression. |
| 594 | Use ``__has_feature(cxx_decltype_incomplete_return_types)`` or |
| 595 | ``__has_extension(cxx_decltype_incomplete_return_types)`` to determine if |
| 596 | support for this feature is enabled. |
| 597 | |
| 598 | C++11 default template arguments in function templates |
| 599 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 600 | |
| 601 | Use ``__has_feature(cxx_default_function_template_args)`` or |
| 602 | ``__has_extension(cxx_default_function_template_args)`` to determine if support |
| 603 | for default template arguments in function templates is enabled. |
| 604 | |
| 605 | C++11 ``default``\ ed functions |
| 606 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 607 | |
| 608 | Use ``__has_feature(cxx_defaulted_functions)`` or |
| 609 | ``__has_extension(cxx_defaulted_functions)`` to determine if support for |
| 610 | defaulted function definitions (with ``= default``) is enabled. |
| 611 | |
| 612 | C++11 delegating constructors |
| 613 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 614 | |
| 615 | Use ``__has_feature(cxx_delegating_constructors)`` to determine if support for |
| 616 | delegating constructors is enabled. |
| 617 | |
| 618 | C++11 ``deleted`` functions |
| 619 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 620 | |
| 621 | Use ``__has_feature(cxx_deleted_functions)`` or |
| 622 | ``__has_extension(cxx_deleted_functions)`` to determine if support for deleted |
| 623 | function definitions (with ``= delete``) is enabled. |
| 624 | |
| 625 | C++11 explicit conversion functions |
| 626 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 627 | |
| 628 | Use ``__has_feature(cxx_explicit_conversions)`` to determine if support for |
| 629 | ``explicit`` conversion functions is enabled. |
| 630 | |
| 631 | C++11 generalized initializers |
| 632 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 633 | |
| 634 | Use ``__has_feature(cxx_generalized_initializers)`` to determine if support for |
| 635 | generalized initializers (using braced lists and ``std::initializer_list``) is |
| 636 | enabled. |
| 637 | |
| 638 | C++11 implicit move constructors/assignment operators |
| 639 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 640 | |
| 641 | Use ``__has_feature(cxx_implicit_moves)`` to determine if Clang will implicitly |
| 642 | generate move constructors and move assignment operators where needed. |
| 643 | |
| 644 | C++11 inheriting constructors |
| 645 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 646 | |
| 647 | Use ``__has_feature(cxx_inheriting_constructors)`` to determine if support for |
Richard Smith | e6e68b5 | 2013-04-19 17:00:31 +0000 | [diff] [blame] | 648 | inheriting constructors is enabled. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 649 | |
| 650 | C++11 inline namespaces |
| 651 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 652 | |
| 653 | Use ``__has_feature(cxx_inline_namespaces)`` or |
| 654 | ``__has_extension(cxx_inline_namespaces)`` to determine if support for inline |
| 655 | namespaces is enabled. |
| 656 | |
| 657 | C++11 lambdas |
| 658 | ^^^^^^^^^^^^^ |
| 659 | |
| 660 | Use ``__has_feature(cxx_lambdas)`` or ``__has_extension(cxx_lambdas)`` to |
| 661 | determine if support for lambdas is enabled. |
| 662 | |
| 663 | C++11 local and unnamed types as template arguments |
| 664 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 665 | |
| 666 | Use ``__has_feature(cxx_local_type_template_args)`` or |
| 667 | ``__has_extension(cxx_local_type_template_args)`` to determine if support for |
| 668 | local and unnamed types as template arguments is enabled. |
| 669 | |
| 670 | C++11 noexcept |
| 671 | ^^^^^^^^^^^^^^ |
| 672 | |
| 673 | Use ``__has_feature(cxx_noexcept)`` or ``__has_extension(cxx_noexcept)`` to |
| 674 | determine if support for noexcept exception specifications is enabled. |
| 675 | |
| 676 | C++11 in-class non-static data member initialization |
| 677 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 678 | |
| 679 | Use ``__has_feature(cxx_nonstatic_member_init)`` to determine whether in-class |
| 680 | initialization of non-static data members is enabled. |
| 681 | |
| 682 | C++11 ``nullptr`` |
| 683 | ^^^^^^^^^^^^^^^^^ |
| 684 | |
| 685 | Use ``__has_feature(cxx_nullptr)`` or ``__has_extension(cxx_nullptr)`` to |
| 686 | determine if support for ``nullptr`` is enabled. |
| 687 | |
| 688 | C++11 ``override control`` |
| 689 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 690 | |
| 691 | Use ``__has_feature(cxx_override_control)`` or |
| 692 | ``__has_extension(cxx_override_control)`` to determine if support for the |
| 693 | override control keywords is enabled. |
| 694 | |
| 695 | C++11 reference-qualified functions |
| 696 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 697 | |
| 698 | Use ``__has_feature(cxx_reference_qualified_functions)`` or |
| 699 | ``__has_extension(cxx_reference_qualified_functions)`` to determine if support |
| 700 | for reference-qualified functions (e.g., member functions with ``&`` or ``&&`` |
| 701 | applied to ``*this``) is enabled. |
| 702 | |
| 703 | C++11 range-based ``for`` loop |
| 704 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 705 | |
| 706 | Use ``__has_feature(cxx_range_for)`` or ``__has_extension(cxx_range_for)`` to |
| 707 | determine if support for the range-based for loop is enabled. |
| 708 | |
| 709 | C++11 raw string literals |
| 710 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 711 | |
| 712 | Use ``__has_feature(cxx_raw_string_literals)`` to determine if support for raw |
| 713 | string literals (e.g., ``R"x(foo\bar)x"``) is enabled. |
| 714 | |
| 715 | C++11 rvalue references |
| 716 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 717 | |
| 718 | Use ``__has_feature(cxx_rvalue_references)`` or |
| 719 | ``__has_extension(cxx_rvalue_references)`` to determine if support for rvalue |
| 720 | references is enabled. |
| 721 | |
| 722 | C++11 ``static_assert()`` |
| 723 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 724 | |
| 725 | Use ``__has_feature(cxx_static_assert)`` or |
| 726 | ``__has_extension(cxx_static_assert)`` to determine if support for compile-time |
| 727 | assertions using ``static_assert`` is enabled. |
| 728 | |
Richard Smith | e6e68b5 | 2013-04-19 17:00:31 +0000 | [diff] [blame] | 729 | C++11 ``thread_local`` |
| 730 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 731 | |
| 732 | Use ``__has_feature(cxx_thread_local)`` to determine if support for |
| 733 | ``thread_local`` variables is enabled. |
| 734 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 735 | C++11 type inference |
| 736 | ^^^^^^^^^^^^^^^^^^^^ |
| 737 | |
| 738 | Use ``__has_feature(cxx_auto_type)`` or ``__has_extension(cxx_auto_type)`` to |
| 739 | determine C++11 type inference is supported using the ``auto`` specifier. If |
| 740 | this is disabled, ``auto`` will instead be a storage class specifier, as in C |
| 741 | or C++98. |
| 742 | |
| 743 | C++11 strongly typed enumerations |
| 744 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 745 | |
| 746 | Use ``__has_feature(cxx_strong_enums)`` or |
| 747 | ``__has_extension(cxx_strong_enums)`` to determine if support for strongly |
| 748 | typed, scoped enumerations is enabled. |
| 749 | |
| 750 | C++11 trailing return type |
| 751 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 752 | |
| 753 | Use ``__has_feature(cxx_trailing_return)`` or |
| 754 | ``__has_extension(cxx_trailing_return)`` to determine if support for the |
| 755 | alternate function declaration syntax with trailing return type is enabled. |
| 756 | |
| 757 | C++11 Unicode string literals |
| 758 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 759 | |
| 760 | Use ``__has_feature(cxx_unicode_literals)`` to determine if support for Unicode |
| 761 | string literals is enabled. |
| 762 | |
| 763 | C++11 unrestricted unions |
| 764 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 765 | |
| 766 | Use ``__has_feature(cxx_unrestricted_unions)`` to determine if support for |
| 767 | unrestricted unions is enabled. |
| 768 | |
| 769 | C++11 user-defined literals |
| 770 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 771 | |
| 772 | Use ``__has_feature(cxx_user_literals)`` to determine if support for |
| 773 | user-defined literals is enabled. |
| 774 | |
| 775 | C++11 variadic templates |
| 776 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 777 | |
| 778 | Use ``__has_feature(cxx_variadic_templates)`` or |
| 779 | ``__has_extension(cxx_variadic_templates)`` to determine if support for |
| 780 | variadic templates is enabled. |
| 781 | |
Richard Smith | 7f0ffb3 | 2013-05-07 19:32:56 +0000 | [diff] [blame] | 782 | C++1y |
| 783 | ----- |
| 784 | |
| 785 | The features listed below are part of the committee draft for the C++1y |
| 786 | standard. As a result, all these features are enabled with the ``-std=c++1y`` |
| 787 | or ``-std=gnu++1y`` option when compiling C++ code. |
| 788 | |
| 789 | C++1y binary literals |
| 790 | ^^^^^^^^^^^^^^^^^^^^^ |
| 791 | |
| 792 | Use ``__has_feature(cxx_binary_literals)`` or |
| 793 | ``__has_extension(cxx_binary_literals)`` to determine whether |
| 794 | binary literals (for instance, ``0b10010``) are recognized. Clang supports this |
| 795 | feature as an extension in all language modes. |
| 796 | |
| 797 | C++1y contextual conversions |
| 798 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 799 | |
| 800 | Use ``__has_feature(cxx_contextual_conversions)`` or |
| 801 | ``__has_extension(cxx_contextual_conversions)`` to determine if the C++1y rules |
| 802 | are used when performing an implicit conversion for an array bound in a |
| 803 | *new-expression*, the operand of a *delete-expression*, an integral constant |
| 804 | expression, or a condition in a ``switch`` statement. Clang does not yet |
| 805 | support this feature. |
| 806 | |
| 807 | C++1y decltype(auto) |
| 808 | ^^^^^^^^^^^^^^^^^^^^ |
| 809 | |
| 810 | Use ``__has_feature(cxx_decltype_auto)`` or |
| 811 | ``__has_extension(cxx_decltype_auto)`` to determine if support |
| 812 | for the ``decltype(auto)`` placeholder type is enabled. |
| 813 | |
| 814 | C++1y default initializers for aggregates |
| 815 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 816 | |
| 817 | Use ``__has_feature(cxx_aggregate_nsdmi)`` or |
| 818 | ``__has_extension(cxx_aggregate_nsdmi)`` to determine if support |
| 819 | for default initializers in aggregate members is enabled. |
| 820 | |
| 821 | C++1y generalized lambda capture |
| 822 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 823 | |
| 824 | Use ``__has_feature(cxx_generalized_capture)`` or |
| 825 | ``__has_extension(cxx_generalized_capture`` to determine if support for |
| 826 | generalized lambda captures is enabled |
| 827 | (for instance, ``[n(0)] { return ++n; }``). |
| 828 | Clang does not yet support this feature. |
| 829 | |
| 830 | C++1y generic lambdas |
| 831 | ^^^^^^^^^^^^^^^^^^^^^ |
| 832 | |
| 833 | Use ``__has_feature(cxx_generic_lambda)`` or |
| 834 | ``__has_extension(cxx_generic_lambda)`` to determine if support for generic |
| 835 | (polymorphic) lambdas is enabled |
| 836 | (for instance, ``[] (auto x) { return x + 1; }``). |
| 837 | Clang does not yet support this feature. |
| 838 | |
| 839 | C++1y relaxed constexpr |
| 840 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 841 | |
| 842 | Use ``__has_feature(cxx_relaxed_constexpr)`` or |
| 843 | ``__has_extension(cxx_relaxed_constexpr)`` to determine if variable |
| 844 | declarations, local variable modification, and control flow constructs |
| 845 | are permitted in ``constexpr`` functions. |
| 846 | Clang's implementation of this feature is incomplete. |
| 847 | |
| 848 | C++1y return type deduction |
| 849 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 850 | |
| 851 | Use ``__has_feature(cxx_return_type_deduction)`` or |
| 852 | ``__has_extension(cxx_return_type_deduction)`` to determine if support |
| 853 | for return type deduction for functions (using ``auto`` as a return type) |
| 854 | is enabled. |
Richard Smith | 7f0ffb3 | 2013-05-07 19:32:56 +0000 | [diff] [blame] | 855 | |
| 856 | C++1y runtime-sized arrays |
| 857 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 858 | |
| 859 | Use ``__has_feature(cxx_runtime_array)`` or |
| 860 | ``__has_extension(cxx_runtime_array)`` to determine if support |
| 861 | for arrays of runtime bound (a restricted form of variable-length arrays) |
| 862 | is enabled. |
| 863 | Clang's implementation of this feature is incomplete. |
| 864 | |
| 865 | C++1y variable templates |
| 866 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 867 | |
| 868 | Use ``__has_feature(cxx_variable_templates)`` or |
| 869 | ``__has_extension(cxx_variable_templates)`` to determine if support for |
| 870 | templated variable declarations is enabled. |
| 871 | Clang does not yet support this feature. |
| 872 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 873 | C11 |
| 874 | --- |
| 875 | |
| 876 | The features listed below are part of the C11 standard. As a result, all these |
| 877 | features are enabled with the ``-std=c11`` or ``-std=gnu11`` option when |
| 878 | compiling C code. Additionally, because these features are all |
| 879 | backward-compatible, they are available as extensions in all language modes. |
| 880 | |
| 881 | C11 alignment specifiers |
| 882 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 883 | |
| 884 | Use ``__has_feature(c_alignas)`` or ``__has_extension(c_alignas)`` to determine |
| 885 | if support for alignment specifiers using ``_Alignas`` is enabled. |
| 886 | |
| 887 | C11 atomic operations |
| 888 | ^^^^^^^^^^^^^^^^^^^^^ |
| 889 | |
| 890 | Use ``__has_feature(c_atomic)`` or ``__has_extension(c_atomic)`` to determine |
| 891 | if support for atomic types using ``_Atomic`` is enabled. Clang also provides |
| 892 | :ref:`a set of builtins <langext-__c11_atomic>` which can be used to implement |
| 893 | the ``<stdatomic.h>`` operations on ``_Atomic`` types. |
| 894 | |
| 895 | C11 generic selections |
| 896 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 897 | |
| 898 | Use ``__has_feature(c_generic_selections)`` or |
| 899 | ``__has_extension(c_generic_selections)`` to determine if support for generic |
| 900 | selections is enabled. |
| 901 | |
| 902 | As an extension, the C11 generic selection expression is available in all |
| 903 | languages supported by Clang. The syntax is the same as that given in the C11 |
| 904 | standard. |
| 905 | |
| 906 | In C, type compatibility is decided according to the rules given in the |
| 907 | appropriate standard, but in C++, which lacks the type compatibility rules used |
| 908 | in C, types are considered compatible only if they are equivalent. |
| 909 | |
| 910 | C11 ``_Static_assert()`` |
| 911 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 912 | |
| 913 | Use ``__has_feature(c_static_assert)`` or ``__has_extension(c_static_assert)`` |
| 914 | to determine if support for compile-time assertions using ``_Static_assert`` is |
| 915 | enabled. |
| 916 | |
Richard Smith | e6e68b5 | 2013-04-19 17:00:31 +0000 | [diff] [blame] | 917 | C11 ``_Thread_local`` |
| 918 | ^^^^^^^^^^^^^^^^^^^^^ |
| 919 | |
| 920 | Use ``__has_feature(c_thread_local)`` to determine if support for |
| 921 | ``_Thread_local`` variables is enabled. |
| 922 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 923 | Checks for Type Traits |
| 924 | ====================== |
| 925 | |
| 926 | Clang supports the `GNU C++ type traits |
| 927 | <http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html>`_ and a subset of the |
| 928 | `Microsoft Visual C++ Type traits |
| 929 | <http://msdn.microsoft.com/en-us/library/ms177194(v=VS.100).aspx>`_. For each |
| 930 | supported type trait ``__X``, ``__has_extension(X)`` indicates the presence of |
| 931 | the type trait. For example: |
| 932 | |
| 933 | .. code-block:: c++ |
| 934 | |
| 935 | #if __has_extension(is_convertible_to) |
| 936 | template<typename From, typename To> |
| 937 | struct is_convertible_to { |
| 938 | static const bool value = __is_convertible_to(From, To); |
| 939 | }; |
| 940 | #else |
| 941 | // Emulate type trait |
| 942 | #endif |
| 943 | |
| 944 | The following type traits are supported by Clang: |
| 945 | |
| 946 | * ``__has_nothrow_assign`` (GNU, Microsoft) |
| 947 | * ``__has_nothrow_copy`` (GNU, Microsoft) |
| 948 | * ``__has_nothrow_constructor`` (GNU, Microsoft) |
| 949 | * ``__has_trivial_assign`` (GNU, Microsoft) |
| 950 | * ``__has_trivial_copy`` (GNU, Microsoft) |
| 951 | * ``__has_trivial_constructor`` (GNU, Microsoft) |
| 952 | * ``__has_trivial_destructor`` (GNU, Microsoft) |
| 953 | * ``__has_virtual_destructor`` (GNU, Microsoft) |
| 954 | * ``__is_abstract`` (GNU, Microsoft) |
| 955 | * ``__is_base_of`` (GNU, Microsoft) |
| 956 | * ``__is_class`` (GNU, Microsoft) |
| 957 | * ``__is_convertible_to`` (Microsoft) |
| 958 | * ``__is_empty`` (GNU, Microsoft) |
| 959 | * ``__is_enum`` (GNU, Microsoft) |
| 960 | * ``__is_interface_class`` (Microsoft) |
| 961 | * ``__is_pod`` (GNU, Microsoft) |
| 962 | * ``__is_polymorphic`` (GNU, Microsoft) |
| 963 | * ``__is_union`` (GNU, Microsoft) |
| 964 | * ``__is_literal(type)``: Determines whether the given type is a literal type |
| 965 | * ``__is_final``: Determines whether the given type is declared with a |
| 966 | ``final`` class-virt-specifier. |
| 967 | * ``__underlying_type(type)``: Retrieves the underlying type for a given |
| 968 | ``enum`` type. This trait is required to implement the C++11 standard |
| 969 | library. |
| 970 | * ``__is_trivially_assignable(totype, fromtype)``: Determines whether a value |
| 971 | of type ``totype`` can be assigned to from a value of type ``fromtype`` such |
| 972 | that no non-trivial functions are called as part of that assignment. This |
| 973 | trait is required to implement the C++11 standard library. |
| 974 | * ``__is_trivially_constructible(type, argtypes...)``: Determines whether a |
| 975 | value of type ``type`` can be direct-initialized with arguments of types |
| 976 | ``argtypes...`` such that no non-trivial functions are called as part of |
| 977 | that initialization. This trait is required to implement the C++11 standard |
| 978 | library. |
| 979 | |
| 980 | Blocks |
| 981 | ====== |
| 982 | |
| 983 | The syntax and high level language feature description is in |
Michael Gottesman | a65e076 | 2013-01-07 22:24:45 +0000 | [diff] [blame] | 984 | :doc:`BlockLanguageSpec<BlockLanguageSpec>`. Implementation and ABI details for |
| 985 | the clang implementation are in :doc:`Block-ABI-Apple<Block-ABI-Apple>`. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 986 | |
| 987 | Query for this feature with ``__has_extension(blocks)``. |
| 988 | |
| 989 | Objective-C Features |
| 990 | ==================== |
| 991 | |
| 992 | Related result types |
| 993 | -------------------- |
| 994 | |
| 995 | According to Cocoa conventions, Objective-C methods with certain names |
| 996 | ("``init``", "``alloc``", etc.) always return objects that are an instance of |
| 997 | the receiving class's type. Such methods are said to have a "related result |
| 998 | type", meaning that a message send to one of these methods will have the same |
| 999 | static type as an instance of the receiver class. For example, given the |
| 1000 | following classes: |
| 1001 | |
| 1002 | .. code-block:: objc |
| 1003 | |
| 1004 | @interface NSObject |
| 1005 | + (id)alloc; |
| 1006 | - (id)init; |
| 1007 | @end |
| 1008 | |
| 1009 | @interface NSArray : NSObject |
| 1010 | @end |
| 1011 | |
| 1012 | and this common initialization pattern |
| 1013 | |
| 1014 | .. code-block:: objc |
| 1015 | |
| 1016 | NSArray *array = [[NSArray alloc] init]; |
| 1017 | |
| 1018 | the type of the expression ``[NSArray alloc]`` is ``NSArray*`` because |
| 1019 | ``alloc`` implicitly has a related result type. Similarly, the type of the |
| 1020 | expression ``[[NSArray alloc] init]`` is ``NSArray*``, since ``init`` has a |
| 1021 | related result type and its receiver is known to have the type ``NSArray *``. |
| 1022 | If neither ``alloc`` nor ``init`` had a related result type, the expressions |
| 1023 | would have had type ``id``, as declared in the method signature. |
| 1024 | |
| 1025 | A method with a related result type can be declared by using the type |
| 1026 | ``instancetype`` as its result type. ``instancetype`` is a contextual keyword |
| 1027 | that is only permitted in the result type of an Objective-C method, e.g. |
| 1028 | |
| 1029 | .. code-block:: objc |
| 1030 | |
| 1031 | @interface A |
| 1032 | + (instancetype)constructAnA; |
| 1033 | @end |
| 1034 | |
| 1035 | The related result type can also be inferred for some methods. To determine |
| 1036 | whether a method has an inferred related result type, the first word in the |
| 1037 | camel-case selector (e.g., "``init``" in "``initWithObjects``") is considered, |
| 1038 | and the method will have a related result type if its return type is compatible |
| 1039 | with the type of its class and if: |
| 1040 | |
| 1041 | * the first word is "``alloc``" or "``new``", and the method is a class method, |
| 1042 | or |
| 1043 | |
| 1044 | * the first word is "``autorelease``", "``init``", "``retain``", or "``self``", |
| 1045 | and the method is an instance method. |
| 1046 | |
| 1047 | If a method with a related result type is overridden by a subclass method, the |
| 1048 | subclass method must also return a type that is compatible with the subclass |
| 1049 | type. For example: |
| 1050 | |
| 1051 | .. code-block:: objc |
| 1052 | |
| 1053 | @interface NSString : NSObject |
| 1054 | - (NSUnrelated *)init; // incorrect usage: NSUnrelated is not NSString or a superclass of NSString |
| 1055 | @end |
| 1056 | |
| 1057 | Related result types only affect the type of a message send or property access |
| 1058 | via the given method. In all other respects, a method with a related result |
| 1059 | type is treated the same way as method that returns ``id``. |
| 1060 | |
| 1061 | Use ``__has_feature(objc_instancetype)`` to determine whether the |
| 1062 | ``instancetype`` contextual keyword is available. |
| 1063 | |
| 1064 | Automatic reference counting |
| 1065 | ---------------------------- |
| 1066 | |
Sean Silva | 159cc9e | 2013-01-02 13:07:47 +0000 | [diff] [blame] | 1067 | Clang provides support for :doc:`automated reference counting |
| 1068 | <AutomaticReferenceCounting>` in Objective-C, which eliminates the need |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1069 | for manual ``retain``/``release``/``autorelease`` message sends. There are two |
| 1070 | feature macros associated with automatic reference counting: |
| 1071 | ``__has_feature(objc_arc)`` indicates the availability of automated reference |
| 1072 | counting in general, while ``__has_feature(objc_arc_weak)`` indicates that |
| 1073 | automated reference counting also includes support for ``__weak`` pointers to |
| 1074 | Objective-C objects. |
| 1075 | |
Sean Silva | 159cc9e | 2013-01-02 13:07:47 +0000 | [diff] [blame] | 1076 | .. _objc-fixed-enum: |
| 1077 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1078 | Enumerations with a fixed underlying type |
| 1079 | ----------------------------------------- |
| 1080 | |
| 1081 | Clang provides support for C++11 enumerations with a fixed underlying type |
| 1082 | within Objective-C. For example, one can write an enumeration type as: |
| 1083 | |
| 1084 | .. code-block:: c++ |
| 1085 | |
| 1086 | typedef enum : unsigned char { Red, Green, Blue } Color; |
| 1087 | |
| 1088 | This specifies that the underlying type, which is used to store the enumeration |
| 1089 | value, is ``unsigned char``. |
| 1090 | |
| 1091 | Use ``__has_feature(objc_fixed_enum)`` to determine whether support for fixed |
| 1092 | underlying types is available in Objective-C. |
| 1093 | |
| 1094 | Interoperability with C++11 lambdas |
| 1095 | ----------------------------------- |
| 1096 | |
| 1097 | Clang provides interoperability between C++11 lambdas and blocks-based APIs, by |
| 1098 | permitting a lambda to be implicitly converted to a block pointer with the |
| 1099 | corresponding signature. For example, consider an API such as ``NSArray``'s |
| 1100 | array-sorting method: |
| 1101 | |
| 1102 | .. code-block:: objc |
| 1103 | |
| 1104 | - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr; |
| 1105 | |
| 1106 | ``NSComparator`` is simply a typedef for the block pointer ``NSComparisonResult |
| 1107 | (^)(id, id)``, and parameters of this type are generally provided with block |
| 1108 | literals as arguments. However, one can also use a C++11 lambda so long as it |
| 1109 | provides the same signature (in this case, accepting two parameters of type |
| 1110 | ``id`` and returning an ``NSComparisonResult``): |
| 1111 | |
| 1112 | .. code-block:: objc |
| 1113 | |
| 1114 | NSArray *array = @[@"string 1", @"string 21", @"string 12", @"String 11", |
| 1115 | @"String 02"]; |
| 1116 | const NSStringCompareOptions comparisonOptions |
| 1117 | = NSCaseInsensitiveSearch | NSNumericSearch | |
| 1118 | NSWidthInsensitiveSearch | NSForcedOrderingSearch; |
| 1119 | NSLocale *currentLocale = [NSLocale currentLocale]; |
| 1120 | NSArray *sorted |
| 1121 | = [array sortedArrayUsingComparator:[=](id s1, id s2) -> NSComparisonResult { |
| 1122 | NSRange string1Range = NSMakeRange(0, [s1 length]); |
| 1123 | return [s1 compare:s2 options:comparisonOptions |
| 1124 | range:string1Range locale:currentLocale]; |
| 1125 | }]; |
| 1126 | NSLog(@"sorted: %@", sorted); |
| 1127 | |
| 1128 | This code relies on an implicit conversion from the type of the lambda |
| 1129 | expression (an unnamed, local class type called the *closure type*) to the |
| 1130 | corresponding block pointer type. The conversion itself is expressed by a |
| 1131 | conversion operator in that closure type that produces a block pointer with the |
| 1132 | same signature as the lambda itself, e.g., |
| 1133 | |
| 1134 | .. code-block:: objc |
| 1135 | |
| 1136 | operator NSComparisonResult (^)(id, id)() const; |
| 1137 | |
| 1138 | This conversion function returns a new block that simply forwards the two |
| 1139 | parameters to the lambda object (which it captures by copy), then returns the |
| 1140 | result. The returned block is first copied (with ``Block_copy``) and then |
| 1141 | autoreleased. As an optimization, if a lambda expression is immediately |
| 1142 | converted to a block pointer (as in the first example, above), then the block |
| 1143 | is not copied and autoreleased: rather, it is given the same lifetime as a |
| 1144 | block literal written at that point in the program, which avoids the overhead |
| 1145 | of copying a block to the heap in the common case. |
| 1146 | |
| 1147 | The conversion from a lambda to a block pointer is only available in |
| 1148 | Objective-C++, and not in C++ with blocks, due to its use of Objective-C memory |
| 1149 | management (autorelease). |
| 1150 | |
| 1151 | Object Literals and Subscripting |
| 1152 | -------------------------------- |
| 1153 | |
Sean Silva | 159cc9e | 2013-01-02 13:07:47 +0000 | [diff] [blame] | 1154 | Clang provides support for :doc:`Object Literals and Subscripting |
| 1155 | <ObjectiveCLiterals>` in Objective-C, which simplifies common Objective-C |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1156 | programming patterns, makes programs more concise, and improves the safety of |
| 1157 | container creation. There are several feature macros associated with object |
| 1158 | literals and subscripting: ``__has_feature(objc_array_literals)`` tests the |
| 1159 | availability of array literals; ``__has_feature(objc_dictionary_literals)`` |
| 1160 | tests the availability of dictionary literals; |
| 1161 | ``__has_feature(objc_subscripting)`` tests the availability of object |
| 1162 | subscripting. |
| 1163 | |
| 1164 | Objective-C Autosynthesis of Properties |
| 1165 | --------------------------------------- |
| 1166 | |
| 1167 | Clang provides support for autosynthesis of declared properties. Using this |
| 1168 | feature, clang provides default synthesis of those properties not declared |
| 1169 | @dynamic and not having user provided backing getter and setter methods. |
| 1170 | ``__has_feature(objc_default_synthesize_properties)`` checks for availability |
| 1171 | of this feature in version of clang being used. |
| 1172 | |
Jordan Rose | 3115f5b6 | 2012-12-15 00:37:01 +0000 | [diff] [blame] | 1173 | .. _langext-objc_method_family: |
| 1174 | |
| 1175 | The ``objc_method_family`` attribute |
| 1176 | ------------------------------------ |
| 1177 | |
| 1178 | Many methods in Objective-C have conventional meanings determined by their |
| 1179 | selectors. It is sometimes useful to be able to mark a method as having a |
| 1180 | particular conventional meaning despite not having the right selector, or as |
| 1181 | not having the conventional meaning that its selector would suggest. For these |
| 1182 | use cases, we provide an attribute to specifically describe the "method family" |
| 1183 | that a method belongs to. |
| 1184 | |
| 1185 | **Usage**: ``__attribute__((objc_method_family(X)))``, where ``X`` is one of |
| 1186 | ``none``, ``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``. This |
| 1187 | attribute can only be placed at the end of a method declaration: |
| 1188 | |
| 1189 | .. code-block:: objc |
| 1190 | |
| 1191 | - (NSString *)initMyStringValue __attribute__((objc_method_family(none))); |
| 1192 | |
| 1193 | Users who do not wish to change the conventional meaning of a method, and who |
| 1194 | merely want to document its non-standard retain and release semantics, should |
| 1195 | use the :ref:`retaining behavior attributes <langext-objc-retain-release>` |
| 1196 | described below. |
| 1197 | |
| 1198 | Query for this feature with ``__has_attribute(objc_method_family)``. |
| 1199 | |
| 1200 | .. _langext-objc-retain-release: |
| 1201 | |
| 1202 | Objective-C retaining behavior attributes |
| 1203 | ----------------------------------------- |
| 1204 | |
| 1205 | In Objective-C, functions and methods are generally assumed to follow the |
| 1206 | `Cocoa Memory Management |
| 1207 | <http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html>`_ |
| 1208 | conventions for ownership of object arguments and |
| 1209 | return values. However, there are exceptions, and so Clang provides attributes |
| 1210 | to allow these exceptions to be documented. This are used by ARC and the |
| 1211 | `static analyzer <http://clang-analyzer.llvm.org>`_ Some exceptions may be |
| 1212 | better described using the :ref:`objc_method_family |
| 1213 | <langext-objc_method_family>` attribute instead. |
| 1214 | |
| 1215 | **Usage**: The ``ns_returns_retained``, ``ns_returns_not_retained``, |
| 1216 | ``ns_returns_autoreleased``, ``cf_returns_retained``, and |
| 1217 | ``cf_returns_not_retained`` attributes can be placed on methods and functions |
| 1218 | that return Objective-C or CoreFoundation objects. They are commonly placed at |
| 1219 | the end of a function prototype or method declaration: |
| 1220 | |
| 1221 | .. code-block:: objc |
| 1222 | |
| 1223 | id foo() __attribute__((ns_returns_retained)); |
| 1224 | |
| 1225 | - (NSString *)bar:(int)x __attribute__((ns_returns_retained)); |
| 1226 | |
| 1227 | The ``*_returns_retained`` attributes specify that the returned object has a +1 |
| 1228 | retain count. The ``*_returns_not_retained`` attributes specify that the return |
| 1229 | object has a +0 retain count, even if the normal convention for its selector |
| 1230 | would be +1. ``ns_returns_autoreleased`` specifies that the returned object is |
| 1231 | +0, but is guaranteed to live at least as long as the next flush of an |
| 1232 | autorelease pool. |
| 1233 | |
| 1234 | **Usage**: The ``ns_consumed`` and ``cf_consumed`` attributes can be placed on |
| 1235 | an parameter declaration; they specify that the argument is expected to have a |
| 1236 | +1 retain count, which will be balanced in some way by the function or method. |
| 1237 | The ``ns_consumes_self`` attribute can only be placed on an Objective-C |
| 1238 | method; it specifies that the method expects its ``self`` parameter to have a |
| 1239 | +1 retain count, which it will balance in some way. |
| 1240 | |
| 1241 | .. code-block:: objc |
| 1242 | |
| 1243 | void foo(__attribute__((ns_consumed)) NSString *string); |
| 1244 | |
| 1245 | - (void) bar __attribute__((ns_consumes_self)); |
| 1246 | - (void) baz:(id) __attribute__((ns_consumed)) x; |
| 1247 | |
| 1248 | Further examples of these attributes are available in the static analyzer's `list of annotations for analysis |
| 1249 | <http://clang-analyzer.llvm.org/annotations.html#cocoa_mem>`_. |
| 1250 | |
| 1251 | Query for these features with ``__has_attribute(ns_consumed)``, |
| 1252 | ``__has_attribute(ns_returns_retained)``, etc. |
| 1253 | |
| 1254 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1255 | Function Overloading in C |
| 1256 | ========================= |
| 1257 | |
| 1258 | Clang provides support for C++ function overloading in C. Function overloading |
| 1259 | in C is introduced using the ``overloadable`` attribute. For example, one |
| 1260 | might provide several overloaded versions of a ``tgsin`` function that invokes |
| 1261 | the appropriate standard function computing the sine of a value with ``float``, |
| 1262 | ``double``, or ``long double`` precision: |
| 1263 | |
| 1264 | .. code-block:: c |
| 1265 | |
| 1266 | #include <math.h> |
| 1267 | float __attribute__((overloadable)) tgsin(float x) { return sinf(x); } |
| 1268 | double __attribute__((overloadable)) tgsin(double x) { return sin(x); } |
| 1269 | long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); } |
| 1270 | |
| 1271 | Given these declarations, one can call ``tgsin`` with a ``float`` value to |
| 1272 | receive a ``float`` result, with a ``double`` to receive a ``double`` result, |
| 1273 | etc. Function overloading in C follows the rules of C++ function overloading |
| 1274 | to pick the best overload given the call arguments, with a few C-specific |
| 1275 | semantics: |
| 1276 | |
| 1277 | * Conversion from ``float`` or ``double`` to ``long double`` is ranked as a |
| 1278 | floating-point promotion (per C99) rather than as a floating-point conversion |
| 1279 | (as in C++). |
| 1280 | |
| 1281 | * A conversion from a pointer of type ``T*`` to a pointer of type ``U*`` is |
| 1282 | considered a pointer conversion (with conversion rank) if ``T`` and ``U`` are |
| 1283 | compatible types. |
| 1284 | |
| 1285 | * A conversion from type ``T`` to a value of type ``U`` is permitted if ``T`` |
| 1286 | and ``U`` are compatible types. This conversion is given "conversion" rank. |
| 1287 | |
| 1288 | The declaration of ``overloadable`` functions is restricted to function |
| 1289 | declarations and definitions. Most importantly, if any function with a given |
| 1290 | name is given the ``overloadable`` attribute, then all function declarations |
| 1291 | and definitions with that name (and in that scope) must have the |
| 1292 | ``overloadable`` attribute. This rule even applies to redeclarations of |
| 1293 | functions whose original declaration had the ``overloadable`` attribute, e.g., |
| 1294 | |
| 1295 | .. code-block:: c |
| 1296 | |
| 1297 | int f(int) __attribute__((overloadable)); |
| 1298 | float f(float); // error: declaration of "f" must have the "overloadable" attribute |
| 1299 | |
| 1300 | int g(int) __attribute__((overloadable)); |
| 1301 | int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute |
| 1302 | |
| 1303 | Functions marked ``overloadable`` must have prototypes. Therefore, the |
| 1304 | following code is ill-formed: |
| 1305 | |
| 1306 | .. code-block:: c |
| 1307 | |
| 1308 | int h() __attribute__((overloadable)); // error: h does not have a prototype |
| 1309 | |
| 1310 | However, ``overloadable`` functions are allowed to use a ellipsis even if there |
| 1311 | are no named parameters (as is permitted in C++). This feature is particularly |
| 1312 | useful when combined with the ``unavailable`` attribute: |
| 1313 | |
| 1314 | .. code-block:: c++ |
| 1315 | |
| 1316 | void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error |
| 1317 | |
| 1318 | Functions declared with the ``overloadable`` attribute have their names mangled |
| 1319 | according to the same rules as C++ function names. For example, the three |
| 1320 | ``tgsin`` functions in our motivating example get the mangled names |
| 1321 | ``_Z5tgsinf``, ``_Z5tgsind``, and ``_Z5tgsine``, respectively. There are two |
| 1322 | caveats to this use of name mangling: |
| 1323 | |
| 1324 | * Future versions of Clang may change the name mangling of functions overloaded |
| 1325 | in C, so you should not depend on an specific mangling. To be completely |
| 1326 | safe, we strongly urge the use of ``static inline`` with ``overloadable`` |
| 1327 | functions. |
| 1328 | |
| 1329 | * The ``overloadable`` attribute has almost no meaning when used in C++, |
| 1330 | because names will already be mangled and functions are already overloadable. |
| 1331 | However, when an ``overloadable`` function occurs within an ``extern "C"`` |
| 1332 | linkage specification, it's name *will* be mangled in the same way as it |
| 1333 | would in C. |
| 1334 | |
| 1335 | Query for this feature with ``__has_extension(attribute_overloadable)``. |
| 1336 | |
| 1337 | Initializer lists for complex numbers in C |
| 1338 | ========================================== |
| 1339 | |
| 1340 | clang supports an extension which allows the following in C: |
| 1341 | |
| 1342 | .. code-block:: c++ |
| 1343 | |
| 1344 | #include <math.h> |
| 1345 | #include <complex.h> |
| 1346 | complex float x = { 1.0f, INFINITY }; // Init to (1, Inf) |
| 1347 | |
| 1348 | This construct is useful because there is no way to separately initialize the |
| 1349 | real and imaginary parts of a complex variable in standard C, given that clang |
| 1350 | does not support ``_Imaginary``. (Clang also supports the ``__real__`` and |
| 1351 | ``__imag__`` extensions from gcc, which help in some cases, but are not usable |
| 1352 | in static initializers.) |
| 1353 | |
| 1354 | Note that this extension does not allow eliding the braces; the meaning of the |
| 1355 | following two lines is different: |
| 1356 | |
| 1357 | .. code-block:: c++ |
| 1358 | |
| 1359 | complex float x[] = { { 1.0f, 1.0f } }; // [0] = (1, 1) |
| 1360 | complex float x[] = { 1.0f, 1.0f }; // [0] = (1, 0), [1] = (1, 0) |
| 1361 | |
| 1362 | This extension also works in C++ mode, as far as that goes, but does not apply |
| 1363 | to the C++ ``std::complex``. (In C++11, list initialization allows the same |
| 1364 | syntax to be used with ``std::complex`` with the same meaning.) |
| 1365 | |
| 1366 | Builtin Functions |
| 1367 | ================= |
| 1368 | |
| 1369 | Clang supports a number of builtin library functions with the same syntax as |
| 1370 | GCC, including things like ``__builtin_nan``, ``__builtin_constant_p``, |
| 1371 | ``__builtin_choose_expr``, ``__builtin_types_compatible_p``, |
| 1372 | ``__sync_fetch_and_add``, etc. In addition to the GCC builtins, Clang supports |
| 1373 | a number of builtins that GCC does not, which are listed here. |
| 1374 | |
| 1375 | Please note that Clang does not and will not support all of the GCC builtins |
| 1376 | for vector operations. Instead of using builtins, you should use the functions |
| 1377 | defined in target-specific header files like ``<xmmintrin.h>``, which define |
| 1378 | portable wrappers for these. Many of the Clang versions of these functions are |
| 1379 | implemented directly in terms of :ref:`extended vector support |
| 1380 | <langext-vectors>` instead of builtins, in order to reduce the number of |
| 1381 | builtins that we need to implement. |
| 1382 | |
| 1383 | ``__builtin_readcyclecounter`` |
| 1384 | ------------------------------ |
| 1385 | |
| 1386 | ``__builtin_readcyclecounter`` is used to access the cycle counter register (or |
| 1387 | a similar low-latency, high-accuracy clock) on those targets that support it. |
| 1388 | |
| 1389 | **Syntax**: |
| 1390 | |
| 1391 | .. code-block:: c++ |
| 1392 | |
| 1393 | __builtin_readcyclecounter() |
| 1394 | |
| 1395 | **Example of Use**: |
| 1396 | |
| 1397 | .. code-block:: c++ |
| 1398 | |
| 1399 | unsigned long long t0 = __builtin_readcyclecounter(); |
| 1400 | do_something(); |
| 1401 | unsigned long long t1 = __builtin_readcyclecounter(); |
| 1402 | unsigned long long cycles_to_do_something = t1 - t0; // assuming no overflow |
| 1403 | |
| 1404 | **Description**: |
| 1405 | |
| 1406 | The ``__builtin_readcyclecounter()`` builtin returns the cycle counter value, |
| 1407 | which may be either global or process/thread-specific depending on the target. |
| 1408 | As the backing counters often overflow quickly (on the order of seconds) this |
| 1409 | should only be used for timing small intervals. When not supported by the |
| 1410 | target, the return value is always zero. This builtin takes no arguments and |
| 1411 | produces an unsigned long long result. |
| 1412 | |
| 1413 | Query for this feature with ``__has_builtin(__builtin_readcyclecounter)``. |
| 1414 | |
| 1415 | .. _langext-__builtin_shufflevector: |
| 1416 | |
| 1417 | ``__builtin_shufflevector`` |
| 1418 | --------------------------- |
| 1419 | |
| 1420 | ``__builtin_shufflevector`` is used to express generic vector |
| 1421 | permutation/shuffle/swizzle operations. This builtin is also very important |
| 1422 | for the implementation of various target-specific header files like |
| 1423 | ``<xmmintrin.h>``. |
| 1424 | |
| 1425 | **Syntax**: |
| 1426 | |
| 1427 | .. code-block:: c++ |
| 1428 | |
| 1429 | __builtin_shufflevector(vec1, vec2, index1, index2, ...) |
| 1430 | |
| 1431 | **Examples**: |
| 1432 | |
| 1433 | .. code-block:: c++ |
| 1434 | |
| 1435 | // Identity operation - return 4-element vector V1. |
| 1436 | __builtin_shufflevector(V1, V1, 0, 1, 2, 3) |
| 1437 | |
| 1438 | // "Splat" element 0 of V1 into a 4-element result. |
| 1439 | __builtin_shufflevector(V1, V1, 0, 0, 0, 0) |
| 1440 | |
| 1441 | // Reverse 4-element vector V1. |
| 1442 | __builtin_shufflevector(V1, V1, 3, 2, 1, 0) |
| 1443 | |
| 1444 | // Concatenate every other element of 4-element vectors V1 and V2. |
| 1445 | __builtin_shufflevector(V1, V2, 0, 2, 4, 6) |
| 1446 | |
| 1447 | // Concatenate every other element of 8-element vectors V1 and V2. |
| 1448 | __builtin_shufflevector(V1, V2, 0, 2, 4, 6, 8, 10, 12, 14) |
| 1449 | |
| 1450 | **Description**: |
| 1451 | |
| 1452 | The first two arguments to ``__builtin_shufflevector`` are vectors that have |
| 1453 | the same element type. The remaining arguments are a list of integers that |
| 1454 | specify the elements indices of the first two vectors that should be extracted |
| 1455 | and returned in a new vector. These element indices are numbered sequentially |
| 1456 | starting with the first vector, continuing into the second vector. Thus, if |
| 1457 | ``vec1`` is a 4-element vector, index 5 would refer to the second element of |
| 1458 | ``vec2``. |
| 1459 | |
| 1460 | The result of ``__builtin_shufflevector`` is a vector with the same element |
| 1461 | type as ``vec1``/``vec2`` but that has an element count equal to the number of |
| 1462 | indices specified. |
| 1463 | |
| 1464 | Query for this feature with ``__has_builtin(__builtin_shufflevector)``. |
| 1465 | |
| 1466 | ``__builtin_unreachable`` |
| 1467 | ------------------------- |
| 1468 | |
| 1469 | ``__builtin_unreachable`` is used to indicate that a specific point in the |
| 1470 | program cannot be reached, even if the compiler might otherwise think it can. |
| 1471 | This is useful to improve optimization and eliminates certain warnings. For |
| 1472 | example, without the ``__builtin_unreachable`` in the example below, the |
| 1473 | compiler assumes that the inline asm can fall through and prints a "function |
| 1474 | declared '``noreturn``' should not return" warning. |
| 1475 | |
| 1476 | **Syntax**: |
| 1477 | |
| 1478 | .. code-block:: c++ |
| 1479 | |
| 1480 | __builtin_unreachable() |
| 1481 | |
| 1482 | **Example of use**: |
| 1483 | |
| 1484 | .. code-block:: c++ |
| 1485 | |
| 1486 | void myabort(void) __attribute__((noreturn)); |
| 1487 | void myabort(void) { |
| 1488 | asm("int3"); |
| 1489 | __builtin_unreachable(); |
| 1490 | } |
| 1491 | |
| 1492 | **Description**: |
| 1493 | |
| 1494 | The ``__builtin_unreachable()`` builtin has completely undefined behavior. |
| 1495 | Since it has undefined behavior, it is a statement that it is never reached and |
| 1496 | the optimizer can take advantage of this to produce better code. This builtin |
| 1497 | takes no arguments and produces a void result. |
| 1498 | |
| 1499 | Query for this feature with ``__has_builtin(__builtin_unreachable)``. |
| 1500 | |
| 1501 | ``__sync_swap`` |
| 1502 | --------------- |
| 1503 | |
| 1504 | ``__sync_swap`` is used to atomically swap integers or pointers in memory. |
| 1505 | |
| 1506 | **Syntax**: |
| 1507 | |
| 1508 | .. code-block:: c++ |
| 1509 | |
| 1510 | type __sync_swap(type *ptr, type value, ...) |
| 1511 | |
| 1512 | **Example of Use**: |
| 1513 | |
| 1514 | .. code-block:: c++ |
| 1515 | |
| 1516 | int old_value = __sync_swap(&value, new_value); |
| 1517 | |
| 1518 | **Description**: |
| 1519 | |
| 1520 | The ``__sync_swap()`` builtin extends the existing ``__sync_*()`` family of |
| 1521 | atomic intrinsics to allow code to atomically swap the current value with the |
| 1522 | new value. More importantly, it helps developers write more efficient and |
| 1523 | correct code by avoiding expensive loops around |
| 1524 | ``__sync_bool_compare_and_swap()`` or relying on the platform specific |
| 1525 | implementation details of ``__sync_lock_test_and_set()``. The |
| 1526 | ``__sync_swap()`` builtin is a full barrier. |
| 1527 | |
Michael Gottesman | 377b8c6 | 2013-01-13 04:35:31 +0000 | [diff] [blame] | 1528 | Multiprecision Arithmetic Builtins |
| 1529 | ---------------------------------- |
| 1530 | |
| 1531 | Clang provides a set of builtins which expose multiprecision arithmetic in a |
| 1532 | manner amenable to C. They all have the following form: |
| 1533 | |
| 1534 | .. code-block:: c |
| 1535 | |
| 1536 | unsigned x = ..., y = ..., carryin = ..., carryout; |
| 1537 | unsigned sum = __builtin_addc(x, y, carryin, &carryout); |
| 1538 | |
| 1539 | Thus one can form a multiprecision addition chain in the following manner: |
| 1540 | |
| 1541 | .. code-block:: c |
| 1542 | |
| 1543 | unsigned *x, *y, *z, carryin=0, carryout; |
| 1544 | z[0] = __builtin_addc(x[0], y[0], carryin, &carryout); |
| 1545 | carryin = carryout; |
| 1546 | z[1] = __builtin_addc(x[1], y[1], carryin, &carryout); |
| 1547 | carryin = carryout; |
| 1548 | z[2] = __builtin_addc(x[2], y[2], carryin, &carryout); |
| 1549 | carryin = carryout; |
| 1550 | z[3] = __builtin_addc(x[3], y[3], carryin, &carryout); |
| 1551 | |
| 1552 | The complete list of builtins are: |
| 1553 | |
| 1554 | .. code-block:: c |
| 1555 | |
| 1556 | unsigned short __builtin_addcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout); |
| 1557 | unsigned __builtin_addc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout); |
| 1558 | unsigned long __builtin_addcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout); |
| 1559 | unsigned long long __builtin_addcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout); |
| 1560 | unsigned short __builtin_subcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout); |
| 1561 | unsigned __builtin_subc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout); |
| 1562 | unsigned long __builtin_subcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout); |
| 1563 | unsigned long long __builtin_subcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout); |
| 1564 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1565 | .. _langext-__c11_atomic: |
| 1566 | |
| 1567 | __c11_atomic builtins |
| 1568 | --------------------- |
| 1569 | |
| 1570 | Clang provides a set of builtins which are intended to be used to implement |
| 1571 | C11's ``<stdatomic.h>`` header. These builtins provide the semantics of the |
| 1572 | ``_explicit`` form of the corresponding C11 operation, and are named with a |
| 1573 | ``__c11_`` prefix. The supported operations are: |
| 1574 | |
| 1575 | * ``__c11_atomic_init`` |
| 1576 | * ``__c11_atomic_thread_fence`` |
| 1577 | * ``__c11_atomic_signal_fence`` |
| 1578 | * ``__c11_atomic_is_lock_free`` |
| 1579 | * ``__c11_atomic_store`` |
| 1580 | * ``__c11_atomic_load`` |
| 1581 | * ``__c11_atomic_exchange`` |
| 1582 | * ``__c11_atomic_compare_exchange_strong`` |
| 1583 | * ``__c11_atomic_compare_exchange_weak`` |
| 1584 | * ``__c11_atomic_fetch_add`` |
| 1585 | * ``__c11_atomic_fetch_sub`` |
| 1586 | * ``__c11_atomic_fetch_and`` |
| 1587 | * ``__c11_atomic_fetch_or`` |
| 1588 | * ``__c11_atomic_fetch_xor`` |
| 1589 | |
| 1590 | Non-standard C++11 Attributes |
| 1591 | ============================= |
| 1592 | |
Richard Smith | 6f48819 | 2013-02-14 00:13:34 +0000 | [diff] [blame] | 1593 | Clang's non-standard C++11 attributes live in the ``clang`` attribute |
| 1594 | namespace. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1595 | |
| 1596 | The ``clang::fallthrough`` attribute |
| 1597 | ------------------------------------ |
| 1598 | |
| 1599 | The ``clang::fallthrough`` attribute is used along with the |
| 1600 | ``-Wimplicit-fallthrough`` argument to annotate intentional fall-through |
| 1601 | between switch labels. It can only be applied to a null statement placed at a |
| 1602 | point of execution between any statement and the next switch label. It is |
| 1603 | common to mark these places with a specific comment, but this attribute is |
| 1604 | meant to replace comments with a more strict annotation, which can be checked |
| 1605 | by the compiler. This attribute doesn't change semantics of the code and can |
| 1606 | be used wherever an intended fall-through occurs. It is designed to mimic |
| 1607 | control-flow statements like ``break;``, so it can be placed in most places |
| 1608 | where ``break;`` can, but only if there are no statements on the execution path |
| 1609 | between it and the next switch label. |
| 1610 | |
| 1611 | Here is an example: |
| 1612 | |
| 1613 | .. code-block:: c++ |
| 1614 | |
| 1615 | // compile with -Wimplicit-fallthrough |
| 1616 | switch (n) { |
| 1617 | case 22: |
| 1618 | case 33: // no warning: no statements between case labels |
| 1619 | f(); |
| 1620 | case 44: // warning: unannotated fall-through |
| 1621 | g(); |
| 1622 | [[clang::fallthrough]]; |
| 1623 | case 55: // no warning |
| 1624 | if (x) { |
| 1625 | h(); |
| 1626 | break; |
| 1627 | } |
| 1628 | else { |
| 1629 | i(); |
| 1630 | [[clang::fallthrough]]; |
| 1631 | } |
| 1632 | case 66: // no warning |
| 1633 | p(); |
| 1634 | [[clang::fallthrough]]; // warning: fallthrough annotation does not |
| 1635 | // directly precede case label |
| 1636 | q(); |
| 1637 | case 77: // warning: unannotated fall-through |
| 1638 | r(); |
| 1639 | } |
| 1640 | |
Richard Smith | 6f48819 | 2013-02-14 00:13:34 +0000 | [diff] [blame] | 1641 | ``gnu::`` attributes |
| 1642 | -------------------- |
| 1643 | |
| 1644 | Clang also supports GCC's ``gnu`` attribute namespace. All GCC attributes which |
| 1645 | are accepted with the ``__attribute__((foo))`` syntax are also accepted as |
| 1646 | ``[[gnu::foo]]``. This only extends to attributes which are specified by GCC |
| 1647 | (see the list of `GCC function attributes |
| 1648 | <http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_, `GCC variable |
| 1649 | attributes <http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html>`_, and |
| 1650 | `GCC type attributes |
| 1651 | <http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html>`_. As with the GCC |
| 1652 | implementation, these attributes must appertain to the *declarator-id* in a |
| 1653 | declaration, which means they must go either at the start of the declaration or |
| 1654 | immediately after the name being declared. |
| 1655 | |
| 1656 | For example, this applies the GNU ``unused`` attribute to ``a`` and ``f``, and |
| 1657 | also applies the GNU ``noreturn`` attribute to ``f``. |
| 1658 | |
| 1659 | .. code-block:: c++ |
| 1660 | |
| 1661 | [[gnu::unused]] int a, f [[gnu::noreturn]] (); |
| 1662 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1663 | Target-Specific Extensions |
| 1664 | ========================== |
| 1665 | |
| 1666 | Clang supports some language features conditionally on some targets. |
| 1667 | |
| 1668 | X86/X86-64 Language Extensions |
| 1669 | ------------------------------ |
| 1670 | |
| 1671 | The X86 backend has these language extensions: |
| 1672 | |
| 1673 | Memory references off the GS segment |
| 1674 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1675 | |
| 1676 | Annotating a pointer with address space #256 causes it to be code generated |
| 1677 | relative to the X86 GS segment register, and address space #257 causes it to be |
| 1678 | relative to the X86 FS segment. Note that this is a very very low-level |
| 1679 | feature that should only be used if you know what you're doing (for example in |
| 1680 | an OS kernel). |
| 1681 | |
| 1682 | Here is an example: |
| 1683 | |
| 1684 | .. code-block:: c++ |
| 1685 | |
| 1686 | #define GS_RELATIVE __attribute__((address_space(256))) |
| 1687 | int foo(int GS_RELATIVE *P) { |
| 1688 | return *P; |
| 1689 | } |
| 1690 | |
| 1691 | Which compiles to (on X86-32): |
| 1692 | |
| 1693 | .. code-block:: gas |
| 1694 | |
| 1695 | _foo: |
| 1696 | movl 4(%esp), %eax |
| 1697 | movl %gs:(%eax), %eax |
| 1698 | ret |
| 1699 | |
Jordan Rose | 3115f5b6 | 2012-12-15 00:37:01 +0000 | [diff] [blame] | 1700 | Extensions for Static Analysis |
Dmitri Gribenko | 1228d66 | 2012-12-15 14:25:25 +0000 | [diff] [blame] | 1701 | ============================== |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1702 | |
| 1703 | Clang supports additional attributes that are useful for documenting program |
Jordan Rose | 3115f5b6 | 2012-12-15 00:37:01 +0000 | [diff] [blame] | 1704 | invariants and rules for static analysis tools, such as the `Clang Static |
| 1705 | Analyzer <http://clang-analyzer.llvm.org/>`_. These attributes are documented |
| 1706 | in the analyzer's `list of source-level annotations |
| 1707 | <http://clang-analyzer.llvm.org/annotations.html>`_. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1708 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1709 | |
Jordan Rose | 3115f5b6 | 2012-12-15 00:37:01 +0000 | [diff] [blame] | 1710 | Extensions for Dynamic Analysis |
Dmitri Gribenko | 1228d66 | 2012-12-15 14:25:25 +0000 | [diff] [blame] | 1711 | =============================== |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1712 | |
| 1713 | .. _langext-address_sanitizer: |
| 1714 | |
| 1715 | AddressSanitizer |
| 1716 | ---------------- |
| 1717 | |
| 1718 | Use ``__has_feature(address_sanitizer)`` to check if the code is being built |
Dmitri Gribenko | 1228d66 | 2012-12-15 14:25:25 +0000 | [diff] [blame] | 1719 | with :doc:`AddressSanitizer`. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1720 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 1721 | Use ``__attribute__((no_sanitize_address))`` |
| 1722 | on a function declaration |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1723 | to specify that address safety instrumentation (e.g. AddressSanitizer) should |
| 1724 | not be applied to that function. |
| 1725 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 1726 | .. _langext-thread_sanitizer: |
| 1727 | |
| 1728 | ThreadSanitizer |
| 1729 | ---------------- |
| 1730 | |
| 1731 | Use ``__has_feature(thread_sanitizer)`` to check if the code is being built |
| 1732 | with :doc:`ThreadSanitizer`. |
| 1733 | |
| 1734 | Use ``__attribute__((no_sanitize_thread))`` on a function declaration |
| 1735 | to specify that checks for data races on plain (non-atomic) memory accesses |
| 1736 | should not be inserted by ThreadSanitizer. |
| 1737 | The function may still be instrumented by the tool |
| 1738 | to avoid false positives in other places. |
| 1739 | |
| 1740 | .. _langext-memory_sanitizer: |
| 1741 | |
| 1742 | MemorySanitizer |
| 1743 | ---------------- |
| 1744 | Use ``__has_feature(memory_sanitizer)`` to check if the code is being built |
| 1745 | with :doc:`MemorySanitizer`. |
| 1746 | |
| 1747 | Use ``__attribute__((no_sanitize_memory))`` on a function declaration |
| 1748 | to specify that checks for uninitialized memory should not be inserted |
| 1749 | (e.g. by MemorySanitizer). The function may still be instrumented by the tool |
| 1750 | to avoid false positives in other places. |
| 1751 | |
| 1752 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1753 | Thread-Safety Annotation Checking |
| 1754 | ================================= |
| 1755 | |
| 1756 | Clang supports additional attributes for checking basic locking policies in |
| 1757 | multithreaded programs. Clang currently parses the following list of |
| 1758 | attributes, although **the implementation for these annotations is currently in |
| 1759 | development.** For more details, see the `GCC implementation |
| 1760 | <http://gcc.gnu.org/wiki/ThreadSafetyAnnotation>`_. |
| 1761 | |
| 1762 | ``no_thread_safety_analysis`` |
| 1763 | ----------------------------- |
| 1764 | |
| 1765 | Use ``__attribute__((no_thread_safety_analysis))`` on a function declaration to |
| 1766 | specify that the thread safety analysis should not be run on that function. |
| 1767 | This attribute provides an escape hatch (e.g. for situations when it is |
| 1768 | difficult to annotate the locking policy). |
| 1769 | |
| 1770 | ``lockable`` |
| 1771 | ------------ |
| 1772 | |
| 1773 | Use ``__attribute__((lockable))`` on a class definition to specify that it has |
| 1774 | a lockable type (e.g. a Mutex class). This annotation is primarily used to |
| 1775 | check consistency. |
| 1776 | |
| 1777 | ``scoped_lockable`` |
| 1778 | ------------------- |
| 1779 | |
| 1780 | Use ``__attribute__((scoped_lockable))`` on a class definition to specify that |
| 1781 | it has a "scoped" lockable type. Objects of this type will acquire the lock |
| 1782 | upon construction and release it upon going out of scope. This annotation is |
| 1783 | primarily used to check consistency. |
| 1784 | |
| 1785 | ``guarded_var`` |
| 1786 | --------------- |
| 1787 | |
| 1788 | Use ``__attribute__((guarded_var))`` on a variable declaration to specify that |
| 1789 | the variable must be accessed while holding some lock. |
| 1790 | |
| 1791 | ``pt_guarded_var`` |
| 1792 | ------------------ |
| 1793 | |
| 1794 | Use ``__attribute__((pt_guarded_var))`` on a pointer declaration to specify |
| 1795 | that the pointer must be dereferenced while holding some lock. |
| 1796 | |
| 1797 | ``guarded_by(l)`` |
| 1798 | ----------------- |
| 1799 | |
| 1800 | Use ``__attribute__((guarded_by(l)))`` on a variable declaration to specify |
| 1801 | that the variable must be accessed while holding lock ``l``. |
| 1802 | |
| 1803 | ``pt_guarded_by(l)`` |
| 1804 | -------------------- |
| 1805 | |
| 1806 | Use ``__attribute__((pt_guarded_by(l)))`` on a pointer declaration to specify |
| 1807 | that the pointer must be dereferenced while holding lock ``l``. |
| 1808 | |
| 1809 | ``acquired_before(...)`` |
| 1810 | ------------------------ |
| 1811 | |
| 1812 | Use ``__attribute__((acquired_before(...)))`` on a declaration of a lockable |
| 1813 | variable to specify that the lock must be acquired before all attribute |
| 1814 | arguments. Arguments must be lockable type, and there must be at least one |
| 1815 | argument. |
| 1816 | |
| 1817 | ``acquired_after(...)`` |
| 1818 | ----------------------- |
| 1819 | |
| 1820 | Use ``__attribute__((acquired_after(...)))`` on a declaration of a lockable |
| 1821 | variable to specify that the lock must be acquired after all attribute |
| 1822 | arguments. Arguments must be lockable type, and there must be at least one |
| 1823 | argument. |
| 1824 | |
| 1825 | ``exclusive_lock_function(...)`` |
| 1826 | -------------------------------- |
| 1827 | |
| 1828 | Use ``__attribute__((exclusive_lock_function(...)))`` on a function declaration |
| 1829 | to specify that the function acquires all listed locks exclusively. This |
| 1830 | attribute takes zero or more arguments: either of lockable type or integers |
| 1831 | indexing into function parameters of lockable type. If no arguments are given, |
| 1832 | the acquired lock is implicitly ``this`` of the enclosing object. |
| 1833 | |
| 1834 | ``shared_lock_function(...)`` |
| 1835 | ----------------------------- |
| 1836 | |
| 1837 | Use ``__attribute__((shared_lock_function(...)))`` on a function declaration to |
| 1838 | specify that the function acquires all listed locks, although the locks may be |
| 1839 | shared (e.g. read locks). This attribute takes zero or more arguments: either |
| 1840 | of lockable type or integers indexing into function parameters of lockable |
| 1841 | type. If no arguments are given, the acquired lock is implicitly ``this`` of |
| 1842 | the enclosing object. |
| 1843 | |
| 1844 | ``exclusive_trylock_function(...)`` |
| 1845 | ----------------------------------- |
| 1846 | |
| 1847 | Use ``__attribute__((exclusive_lock_function(...)))`` on a function declaration |
| 1848 | to specify that the function will try (without blocking) to acquire all listed |
| 1849 | locks exclusively. This attribute takes one or more arguments. The first |
| 1850 | argument is an integer or boolean value specifying the return value of a |
| 1851 | successful lock acquisition. The remaining arugments are either of lockable |
| 1852 | type or integers indexing into function parameters of lockable type. If only |
| 1853 | one argument is given, the acquired lock is implicitly ``this`` of the |
| 1854 | enclosing object. |
| 1855 | |
| 1856 | ``shared_trylock_function(...)`` |
| 1857 | -------------------------------- |
| 1858 | |
| 1859 | Use ``__attribute__((shared_lock_function(...)))`` on a function declaration to |
| 1860 | specify that the function will try (without blocking) to acquire all listed |
| 1861 | locks, although the locks may be shared (e.g. read locks). This attribute |
| 1862 | takes one or more arguments. The first argument is an integer or boolean value |
| 1863 | specifying the return value of a successful lock acquisition. The remaining |
| 1864 | arugments are either of lockable type or integers indexing into function |
| 1865 | parameters of lockable type. If only one argument is given, the acquired lock |
| 1866 | is implicitly ``this`` of the enclosing object. |
| 1867 | |
| 1868 | ``unlock_function(...)`` |
| 1869 | ------------------------ |
| 1870 | |
| 1871 | Use ``__attribute__((unlock_function(...)))`` on a function declaration to |
| 1872 | specify that the function release all listed locks. This attribute takes zero |
| 1873 | or more arguments: either of lockable type or integers indexing into function |
| 1874 | parameters of lockable type. If no arguments are given, the acquired lock is |
| 1875 | implicitly ``this`` of the enclosing object. |
| 1876 | |
| 1877 | ``lock_returned(l)`` |
| 1878 | -------------------- |
| 1879 | |
| 1880 | Use ``__attribute__((lock_returned(l)))`` on a function declaration to specify |
| 1881 | that the function returns lock ``l`` (``l`` must be of lockable type). This |
| 1882 | annotation is used to aid in resolving lock expressions. |
| 1883 | |
| 1884 | ``locks_excluded(...)`` |
| 1885 | ----------------------- |
| 1886 | |
| 1887 | Use ``__attribute__((locks_excluded(...)))`` on a function declaration to |
| 1888 | specify that the function must not be called with the listed locks. Arguments |
| 1889 | must be lockable type, and there must be at least one argument. |
| 1890 | |
| 1891 | ``exclusive_locks_required(...)`` |
| 1892 | --------------------------------- |
| 1893 | |
| 1894 | Use ``__attribute__((exclusive_locks_required(...)))`` on a function |
| 1895 | declaration to specify that the function must be called while holding the |
| 1896 | listed exclusive locks. Arguments must be lockable type, and there must be at |
| 1897 | least one argument. |
| 1898 | |
| 1899 | ``shared_locks_required(...)`` |
| 1900 | ------------------------------ |
| 1901 | |
| 1902 | Use ``__attribute__((shared_locks_required(...)))`` on a function declaration |
| 1903 | to specify that the function must be called while holding the listed shared |
| 1904 | locks. Arguments must be lockable type, and there must be at least one |
| 1905 | argument. |
| 1906 | |
| 1907 | Type Safety Checking |
| 1908 | ==================== |
| 1909 | |
| 1910 | Clang supports additional attributes to enable checking type safety properties |
| 1911 | that can't be enforced by C type system. Usecases include: |
| 1912 | |
| 1913 | * MPI library implementations, where these attributes enable checking that |
| 1914 | buffer type matches the passed ``MPI_Datatype``; |
| 1915 | * for HDF5 library there is a similar usecase as MPI; |
| 1916 | * checking types of variadic functions' arguments for functions like |
| 1917 | ``fcntl()`` and ``ioctl()``. |
| 1918 | |
| 1919 | You can detect support for these attributes with ``__has_attribute()``. For |
| 1920 | example: |
| 1921 | |
| 1922 | .. code-block:: c++ |
| 1923 | |
| 1924 | #if defined(__has_attribute) |
| 1925 | # if __has_attribute(argument_with_type_tag) && \ |
| 1926 | __has_attribute(pointer_with_type_tag) && \ |
| 1927 | __has_attribute(type_tag_for_datatype) |
| 1928 | # define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx))) |
| 1929 | /* ... other macros ... */ |
| 1930 | # endif |
| 1931 | #endif |
| 1932 | |
| 1933 | #if !defined(ATTR_MPI_PWT) |
| 1934 | # define ATTR_MPI_PWT(buffer_idx, type_idx) |
| 1935 | #endif |
| 1936 | |
| 1937 | int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) |
| 1938 | ATTR_MPI_PWT(1,3); |
| 1939 | |
| 1940 | ``argument_with_type_tag(...)`` |
| 1941 | ------------------------------- |
| 1942 | |
| 1943 | Use ``__attribute__((argument_with_type_tag(arg_kind, arg_idx, |
| 1944 | type_tag_idx)))`` on a function declaration to specify that the function |
| 1945 | accepts a type tag that determines the type of some other argument. |
| 1946 | ``arg_kind`` is an identifier that should be used when annotating all |
| 1947 | applicable type tags. |
| 1948 | |
| 1949 | This attribute is primarily useful for checking arguments of variadic functions |
| 1950 | (``pointer_with_type_tag`` can be used in most of non-variadic cases). |
| 1951 | |
| 1952 | For example: |
| 1953 | |
| 1954 | .. code-block:: c++ |
| 1955 | |
| 1956 | int fcntl(int fd, int cmd, ...) |
| 1957 | __attribute__(( argument_with_type_tag(fcntl,3,2) )); |
| 1958 | |
| 1959 | ``pointer_with_type_tag(...)`` |
| 1960 | ------------------------------ |
| 1961 | |
| 1962 | Use ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))`` |
| 1963 | on a function declaration to specify that the function accepts a type tag that |
| 1964 | determines the pointee type of some other pointer argument. |
| 1965 | |
| 1966 | For example: |
| 1967 | |
| 1968 | .. code-block:: c++ |
| 1969 | |
| 1970 | int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) |
| 1971 | __attribute__(( pointer_with_type_tag(mpi,1,3) )); |
| 1972 | |
| 1973 | ``type_tag_for_datatype(...)`` |
| 1974 | ------------------------------ |
| 1975 | |
| 1976 | Clang supports annotating type tags of two forms. |
| 1977 | |
| 1978 | * **Type tag that is an expression containing a reference to some declared |
| 1979 | identifier.** Use ``__attribute__((type_tag_for_datatype(kind, type)))`` on a |
| 1980 | declaration with that identifier: |
| 1981 | |
| 1982 | .. code-block:: c++ |
| 1983 | |
| 1984 | extern struct mpi_datatype mpi_datatype_int |
| 1985 | __attribute__(( type_tag_for_datatype(mpi,int) )); |
| 1986 | #define MPI_INT ((MPI_Datatype) &mpi_datatype_int) |
| 1987 | |
| 1988 | * **Type tag that is an integral literal.** Introduce a ``static const`` |
| 1989 | variable with a corresponding initializer value and attach |
| 1990 | ``__attribute__((type_tag_for_datatype(kind, type)))`` on that declaration, |
| 1991 | for example: |
| 1992 | |
| 1993 | .. code-block:: c++ |
| 1994 | |
| 1995 | #define MPI_INT ((MPI_Datatype) 42) |
| 1996 | static const MPI_Datatype mpi_datatype_int |
| 1997 | __attribute__(( type_tag_for_datatype(mpi,int) )) = 42 |
| 1998 | |
| 1999 | The attribute also accepts an optional third argument that determines how the |
| 2000 | expression is compared to the type tag. There are two supported flags: |
| 2001 | |
| 2002 | * ``layout_compatible`` will cause types to be compared according to |
| 2003 | layout-compatibility rules (C++11 [class.mem] p 17, 18). This is |
| 2004 | implemented to support annotating types like ``MPI_DOUBLE_INT``. |
| 2005 | |
| 2006 | For example: |
| 2007 | |
| 2008 | .. code-block:: c++ |
| 2009 | |
| 2010 | /* In mpi.h */ |
| 2011 | struct internal_mpi_double_int { double d; int i; }; |
| 2012 | extern struct mpi_datatype mpi_datatype_double_int |
| 2013 | __attribute__(( type_tag_for_datatype(mpi, struct internal_mpi_double_int, layout_compatible) )); |
| 2014 | |
| 2015 | #define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int) |
| 2016 | |
| 2017 | /* In user code */ |
| 2018 | struct my_pair { double a; int b; }; |
| 2019 | struct my_pair *buffer; |
| 2020 | MPI_Send(buffer, 1, MPI_DOUBLE_INT /*, ... */); // no warning |
| 2021 | |
| 2022 | struct my_int_pair { int a; int b; } |
| 2023 | struct my_int_pair *buffer2; |
| 2024 | MPI_Send(buffer2, 1, MPI_DOUBLE_INT /*, ... */); // warning: actual buffer element |
| 2025 | // type 'struct my_int_pair' |
| 2026 | // doesn't match specified MPI_Datatype |
| 2027 | |
| 2028 | * ``must_be_null`` specifies that the expression should be a null pointer |
| 2029 | constant, for example: |
| 2030 | |
| 2031 | .. code-block:: c++ |
| 2032 | |
| 2033 | /* In mpi.h */ |
| 2034 | extern struct mpi_datatype mpi_datatype_null |
| 2035 | __attribute__(( type_tag_for_datatype(mpi, void, must_be_null) )); |
| 2036 | |
| 2037 | #define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null) |
| 2038 | |
| 2039 | /* In user code */ |
| 2040 | MPI_Send(buffer, 1, MPI_DATATYPE_NULL /*, ... */); // warning: MPI_DATATYPE_NULL |
| 2041 | // was specified but buffer |
| 2042 | // is not a null pointer |
| 2043 | |
Dmitri Gribenko | b4b970f | 2013-01-13 16:37:18 +0000 | [diff] [blame] | 2044 | Format String Checking |
| 2045 | ====================== |
| 2046 | |
| 2047 | Clang supports the ``format`` attribute, which indicates that the function |
| 2048 | accepts a ``printf`` or ``scanf``-like format string and corresponding |
| 2049 | arguments or a ``va_list`` that contains these arguments. |
| 2050 | |
| 2051 | Please see `GCC documentation about format attribute |
| 2052 | <http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details |
| 2053 | about attribute syntax. |
| 2054 | |
| 2055 | Clang implements two kinds of checks with this attribute. |
| 2056 | |
| 2057 | #. Clang checks that the function with the ``format`` attribute is called with |
| 2058 | a format string that uses format specifiers that are allowed, and that |
| 2059 | arguments match the format string. This is the ``-Wformat`` warning, it is |
| 2060 | on by default. |
| 2061 | |
| 2062 | #. Clang checks that the format string argument is a literal string. This is |
| 2063 | the ``-Wformat-nonliteral`` warning, it is off by default. |
| 2064 | |
| 2065 | Clang implements this mostly the same way as GCC, but there is a difference |
| 2066 | for functions that accept a ``va_list`` argument (for example, ``vprintf``). |
| 2067 | GCC does not emit ``-Wformat-nonliteral`` warning for calls to such |
| 2068 | fuctions. Clang does not warn if the format string comes from a function |
Richard Smith | 7cdc311 | 2013-02-14 00:22:00 +0000 | [diff] [blame] | 2069 | parameter, where the function is annotated with a compatible attribute, |
Dmitri Gribenko | b4b970f | 2013-01-13 16:37:18 +0000 | [diff] [blame] | 2070 | otherwise it warns. For example: |
| 2071 | |
| 2072 | .. code-block:: c |
| 2073 | |
| 2074 | __attribute__((__format__ (__scanf__, 1, 3))) |
| 2075 | void foo(const char* s, char *buf, ...) { |
| 2076 | va_list ap; |
| 2077 | va_start(ap, buf); |
| 2078 | |
| 2079 | vprintf(s, ap); // warning: format string is not a string literal |
| 2080 | } |
| 2081 | |
| 2082 | In this case we warn because ``s`` contains a format string for a |
Richard Smith | 7cdc311 | 2013-02-14 00:22:00 +0000 | [diff] [blame] | 2083 | ``scanf``-like function, but it is passed to a ``printf``-like function. |
Dmitri Gribenko | b4b970f | 2013-01-13 16:37:18 +0000 | [diff] [blame] | 2084 | |
| 2085 | If the attribute is removed, clang still warns, because the format string is |
| 2086 | not a string literal. |
| 2087 | |
Richard Smith | 7cdc311 | 2013-02-14 00:22:00 +0000 | [diff] [blame] | 2088 | Another example: |
Dmitri Gribenko | b4b970f | 2013-01-13 16:37:18 +0000 | [diff] [blame] | 2089 | |
Richard Smith | 3a09444 | 2013-02-14 00:23:04 +0000 | [diff] [blame] | 2090 | .. code-block:: c |
Dmitri Gribenko | b4b970f | 2013-01-13 16:37:18 +0000 | [diff] [blame] | 2091 | |
| 2092 | __attribute__((__format__ (__printf__, 1, 3))) |
| 2093 | void foo(const char* s, char *buf, ...) { |
| 2094 | va_list ap; |
| 2095 | va_start(ap, buf); |
| 2096 | |
| 2097 | vprintf(s, ap); // warning |
| 2098 | } |
| 2099 | |
Richard Smith | 7cdc311 | 2013-02-14 00:22:00 +0000 | [diff] [blame] | 2100 | In this case Clang does not warn because the format string ``s`` and |
| 2101 | the corresponding arguments are annotated. If the arguments are |
| 2102 | incorrect, the caller of ``foo`` will receive a warning. |