xref: /minix3/external/bsd/llvm/dist/clang/docs/LanguageExtensions.rst (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc=========================
2f4a2713aSLionel SambucClang Language Extensions
3f4a2713aSLionel Sambuc=========================
4f4a2713aSLionel Sambuc
5f4a2713aSLionel Sambuc.. contents::
6f4a2713aSLionel Sambuc   :local:
7f4a2713aSLionel Sambuc   :depth: 1
8f4a2713aSLionel Sambuc
9f4a2713aSLionel Sambuc.. toctree::
10f4a2713aSLionel Sambuc   :hidden:
11f4a2713aSLionel Sambuc
12f4a2713aSLionel Sambuc   ObjectiveCLiterals
13f4a2713aSLionel Sambuc   BlockLanguageSpec
14f4a2713aSLionel Sambuc   Block-ABI-Apple
15f4a2713aSLionel Sambuc   AutomaticReferenceCounting
16f4a2713aSLionel Sambuc
17f4a2713aSLionel SambucIntroduction
18f4a2713aSLionel Sambuc============
19f4a2713aSLionel Sambuc
20f4a2713aSLionel SambucThis document describes the language extensions provided by Clang.  In addition
21f4a2713aSLionel Sambucto the language extensions listed here, Clang aims to support a broad range of
22f4a2713aSLionel SambucGCC extensions.  Please see the `GCC manual
23f4a2713aSLionel Sambuc<http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html>`_ for more information on
24f4a2713aSLionel Sambucthese extensions.
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc.. _langext-feature_check:
27f4a2713aSLionel Sambuc
28f4a2713aSLionel SambucFeature Checking Macros
29f4a2713aSLionel Sambuc=======================
30f4a2713aSLionel Sambuc
31f4a2713aSLionel SambucLanguage extensions can be very useful, but only if you know you can depend on
32f4a2713aSLionel Sambucthem.  In order to allow fine-grain features checks, we support three builtin
33f4a2713aSLionel Sambucfunction-like macros.  This allows you to directly test for a feature in your
34f4a2713aSLionel Sambuccode without having to resort to something like autoconf or fragile "compiler
35f4a2713aSLionel Sambucversion checks".
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc``__has_builtin``
38f4a2713aSLionel Sambuc-----------------
39f4a2713aSLionel Sambuc
40f4a2713aSLionel SambucThis function-like macro takes a single identifier argument that is the name of
41f4a2713aSLionel Sambuca builtin function.  It evaluates to 1 if the builtin is supported or 0 if not.
42f4a2713aSLionel SambucIt can be used like this:
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc.. code-block:: c++
45f4a2713aSLionel Sambuc
46f4a2713aSLionel Sambuc  #ifndef __has_builtin         // Optional of course.
47f4a2713aSLionel Sambuc    #define __has_builtin(x) 0  // Compatibility with non-clang compilers.
48f4a2713aSLionel Sambuc  #endif
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc  ...
51f4a2713aSLionel Sambuc  #if __has_builtin(__builtin_trap)
52f4a2713aSLionel Sambuc    __builtin_trap();
53f4a2713aSLionel Sambuc  #else
54f4a2713aSLionel Sambuc    abort();
55f4a2713aSLionel Sambuc  #endif
56f4a2713aSLionel Sambuc  ...
57f4a2713aSLionel Sambuc
58f4a2713aSLionel Sambuc.. _langext-__has_feature-__has_extension:
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc``__has_feature`` and ``__has_extension``
61f4a2713aSLionel Sambuc-----------------------------------------
62f4a2713aSLionel Sambuc
63f4a2713aSLionel SambucThese function-like macros take a single identifier argument that is the name
64f4a2713aSLionel Sambucof a feature.  ``__has_feature`` evaluates to 1 if the feature is both
65f4a2713aSLionel Sambucsupported by Clang and standardized in the current language standard or 0 if
66f4a2713aSLionel Sambucnot (but see :ref:`below <langext-has-feature-back-compat>`), while
67f4a2713aSLionel Sambuc``__has_extension`` evaluates to 1 if the feature is supported by Clang in the
68f4a2713aSLionel Sambuccurrent language (either as a language extension or a standard language
69f4a2713aSLionel Sambucfeature) or 0 if not.  They can be used like this:
70f4a2713aSLionel Sambuc
71f4a2713aSLionel Sambuc.. code-block:: c++
72f4a2713aSLionel Sambuc
73f4a2713aSLionel Sambuc  #ifndef __has_feature         // Optional of course.
74f4a2713aSLionel Sambuc    #define __has_feature(x) 0  // Compatibility with non-clang compilers.
75f4a2713aSLionel Sambuc  #endif
76f4a2713aSLionel Sambuc  #ifndef __has_extension
77f4a2713aSLionel Sambuc    #define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
78f4a2713aSLionel Sambuc  #endif
79f4a2713aSLionel Sambuc
80f4a2713aSLionel Sambuc  ...
81f4a2713aSLionel Sambuc  #if __has_feature(cxx_rvalue_references)
82f4a2713aSLionel Sambuc  // This code will only be compiled with the -std=c++11 and -std=gnu++11
83f4a2713aSLionel Sambuc  // options, because rvalue references are only standardized in C++11.
84f4a2713aSLionel Sambuc  #endif
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc  #if __has_extension(cxx_rvalue_references)
87f4a2713aSLionel Sambuc  // This code will be compiled with the -std=c++11, -std=gnu++11, -std=c++98
88f4a2713aSLionel Sambuc  // and -std=gnu++98 options, because rvalue references are supported as a
89f4a2713aSLionel Sambuc  // language extension in C++98.
90f4a2713aSLionel Sambuc  #endif
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc.. _langext-has-feature-back-compat:
93f4a2713aSLionel Sambuc
94*0a6a1f1dSLionel SambucFor backward compatibility, ``__has_feature`` can also be used to test
95f4a2713aSLionel Sambucfor support for non-standardized features, i.e. features not prefixed ``c_``,
96f4a2713aSLionel Sambuc``cxx_`` or ``objc_``.
97f4a2713aSLionel Sambuc
98f4a2713aSLionel SambucAnother use of ``__has_feature`` is to check for compiler features not related
99f4a2713aSLionel Sambucto the language standard, such as e.g. :doc:`AddressSanitizer
100f4a2713aSLionel Sambuc<AddressSanitizer>`.
101f4a2713aSLionel Sambuc
102f4a2713aSLionel SambucIf the ``-pedantic-errors`` option is given, ``__has_extension`` is equivalent
103f4a2713aSLionel Sambucto ``__has_feature``.
104f4a2713aSLionel Sambuc
105f4a2713aSLionel SambucThe feature tag is described along with the language feature below.
106f4a2713aSLionel Sambuc
107f4a2713aSLionel SambucThe feature name or extension name can also be specified with a preceding and
108f4a2713aSLionel Sambucfollowing ``__`` (double underscore) to avoid interference from a macro with
109f4a2713aSLionel Sambucthe same name.  For instance, ``__cxx_rvalue_references__`` can be used instead
110f4a2713aSLionel Sambucof ``cxx_rvalue_references``.
111f4a2713aSLionel Sambuc
112*0a6a1f1dSLionel Sambuc``__has_cpp_attribute``
113*0a6a1f1dSLionel Sambuc-----------------------
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel SambucThis function-like macro takes a single argument that is the name of a
116*0a6a1f1dSLionel SambucC++11-style attribute. The argument can either be a single identifier, or a
117*0a6a1f1dSLionel Sambucscoped identifier. If the attribute is supported, a nonzero value is returned.
118*0a6a1f1dSLionel SambucIf the attribute is a standards-based attribute, this macro returns a nonzero
119*0a6a1f1dSLionel Sambucvalue based on the year and month in which the attribute was voted into the
120*0a6a1f1dSLionel Sambucworking draft. If the attribute is not supported by the current compliation
121*0a6a1f1dSLionel Sambuctarget, this macro evaluates to 0.  It can be used like this:
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc.. code-block:: c++
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc  #ifndef __has_cpp_attribute         // Optional of course.
126*0a6a1f1dSLionel Sambuc    #define __has_cpp_attribute(x) 0  // Compatibility with non-clang compilers.
127*0a6a1f1dSLionel Sambuc  #endif
128*0a6a1f1dSLionel Sambuc
129*0a6a1f1dSLionel Sambuc  ...
130*0a6a1f1dSLionel Sambuc  #if __has_cpp_attribute(clang::fallthrough)
131*0a6a1f1dSLionel Sambuc  #define FALLTHROUGH [[clang::fallthrough]]
132*0a6a1f1dSLionel Sambuc  #else
133*0a6a1f1dSLionel Sambuc  #define FALLTHROUGH
134*0a6a1f1dSLionel Sambuc  #endif
135*0a6a1f1dSLionel Sambuc  ...
136*0a6a1f1dSLionel Sambuc
137*0a6a1f1dSLionel SambucThe attribute identifier (but not scope) can also be specified with a preceding
138*0a6a1f1dSLionel Sambucand following ``__`` (double underscore) to avoid interference from a macro with
139*0a6a1f1dSLionel Sambucthe same name.  For instance, ``gnu::__const__`` can be used instead of
140*0a6a1f1dSLionel Sambuc``gnu::const``.
141*0a6a1f1dSLionel Sambuc
142f4a2713aSLionel Sambuc``__has_attribute``
143f4a2713aSLionel Sambuc-------------------
144f4a2713aSLionel Sambuc
145f4a2713aSLionel SambucThis function-like macro takes a single identifier argument that is the name of
146*0a6a1f1dSLionel Sambuca GNU-style attribute.  It evaluates to 1 if the attribute is supported by the
147*0a6a1f1dSLionel Sambuccurrent compilation target, or 0 if not.  It can be used like this:
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc.. code-block:: c++
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc  #ifndef __has_attribute         // Optional of course.
152f4a2713aSLionel Sambuc    #define __has_attribute(x) 0  // Compatibility with non-clang compilers.
153f4a2713aSLionel Sambuc  #endif
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc  ...
156f4a2713aSLionel Sambuc  #if __has_attribute(always_inline)
157f4a2713aSLionel Sambuc  #define ALWAYS_INLINE __attribute__((always_inline))
158f4a2713aSLionel Sambuc  #else
159f4a2713aSLionel Sambuc  #define ALWAYS_INLINE
160f4a2713aSLionel Sambuc  #endif
161f4a2713aSLionel Sambuc  ...
162f4a2713aSLionel Sambuc
163f4a2713aSLionel SambucThe attribute name can also be specified with a preceding and following ``__``
164f4a2713aSLionel Sambuc(double underscore) to avoid interference from a macro with the same name.  For
165f4a2713aSLionel Sambucinstance, ``__always_inline__`` can be used instead of ``always_inline``.
166f4a2713aSLionel Sambuc
167*0a6a1f1dSLionel Sambuc
168*0a6a1f1dSLionel Sambuc``__has_declspec_attribute``
169*0a6a1f1dSLionel Sambuc----------------------------
170*0a6a1f1dSLionel Sambuc
171*0a6a1f1dSLionel SambucThis function-like macro takes a single identifier argument that is the name of
172*0a6a1f1dSLionel Sambucan attribute implemented as a Microsoft-style ``__declspec`` attribute.  It
173*0a6a1f1dSLionel Sambucevaluates to 1 if the attribute is supported by the current compilation target,
174*0a6a1f1dSLionel Sambucor 0 if not.  It can be used like this:
175*0a6a1f1dSLionel Sambuc
176*0a6a1f1dSLionel Sambuc.. code-block:: c++
177*0a6a1f1dSLionel Sambuc
178*0a6a1f1dSLionel Sambuc  #ifndef __has_declspec_attribute         // Optional of course.
179*0a6a1f1dSLionel Sambuc    #define __has_declspec_attribute(x) 0  // Compatibility with non-clang compilers.
180*0a6a1f1dSLionel Sambuc  #endif
181*0a6a1f1dSLionel Sambuc
182*0a6a1f1dSLionel Sambuc  ...
183*0a6a1f1dSLionel Sambuc  #if __has_declspec_attribute(dllexport)
184*0a6a1f1dSLionel Sambuc  #define DLLEXPORT __declspec(dllexport)
185*0a6a1f1dSLionel Sambuc  #else
186*0a6a1f1dSLionel Sambuc  #define DLLEXPORT
187*0a6a1f1dSLionel Sambuc  #endif
188*0a6a1f1dSLionel Sambuc  ...
189*0a6a1f1dSLionel Sambuc
190*0a6a1f1dSLionel SambucThe attribute name can also be specified with a preceding and following ``__``
191*0a6a1f1dSLionel Sambuc(double underscore) to avoid interference from a macro with the same name.  For
192*0a6a1f1dSLionel Sambucinstance, ``__dllexport__`` can be used instead of ``dllexport``.
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc``__is_identifier``
195*0a6a1f1dSLionel Sambuc-------------------
196*0a6a1f1dSLionel Sambuc
197*0a6a1f1dSLionel SambucThis function-like macro takes a single identifier argument that might be either
198*0a6a1f1dSLionel Sambuca reserved word or a regular identifier. It evaluates to 1 if the argument is just
199*0a6a1f1dSLionel Sambuca regular identifier and not a reserved word, in the sense that it can then be
200*0a6a1f1dSLionel Sambucused as the name of a user-defined function or variable. Otherwise it evaluates
201*0a6a1f1dSLionel Sambucto 0.  It can be used like this:
202*0a6a1f1dSLionel Sambuc
203*0a6a1f1dSLionel Sambuc.. code-block:: c++
204*0a6a1f1dSLionel Sambuc
205*0a6a1f1dSLionel Sambuc  ...
206*0a6a1f1dSLionel Sambuc  #ifdef __is_identifier          // Compatibility with non-clang compilers.
207*0a6a1f1dSLionel Sambuc    #if __is_identifier(__wchar_t)
208*0a6a1f1dSLionel Sambuc      typedef wchar_t __wchar_t;
209*0a6a1f1dSLionel Sambuc    #endif
210*0a6a1f1dSLionel Sambuc  #endif
211*0a6a1f1dSLionel Sambuc
212*0a6a1f1dSLionel Sambuc  __wchar_t WideCharacter;
213*0a6a1f1dSLionel Sambuc  ...
214*0a6a1f1dSLionel Sambuc
215f4a2713aSLionel SambucInclude File Checking Macros
216f4a2713aSLionel Sambuc============================
217f4a2713aSLionel Sambuc
218f4a2713aSLionel SambucNot all developments systems have the same include files.  The
219f4a2713aSLionel Sambuc:ref:`langext-__has_include` and :ref:`langext-__has_include_next` macros allow
220f4a2713aSLionel Sambucyou to check for the existence of an include file before doing a possibly
221f4a2713aSLionel Sambucfailing ``#include`` directive.  Include file checking macros must be used
222f4a2713aSLionel Sambucas expressions in ``#if`` or ``#elif`` preprocessing directives.
223f4a2713aSLionel Sambuc
224f4a2713aSLionel Sambuc.. _langext-__has_include:
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc``__has_include``
227f4a2713aSLionel Sambuc-----------------
228f4a2713aSLionel Sambuc
229f4a2713aSLionel SambucThis function-like macro takes a single file name string argument that is the
230f4a2713aSLionel Sambucname of an include file.  It evaluates to 1 if the file can be found using the
231f4a2713aSLionel Sambucinclude paths, or 0 otherwise:
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc.. code-block:: c++
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc  // Note the two possible file name string formats.
236f4a2713aSLionel Sambuc  #if __has_include("myinclude.h") && __has_include(<stdint.h>)
237f4a2713aSLionel Sambuc  # include "myinclude.h"
238f4a2713aSLionel Sambuc  #endif
239f4a2713aSLionel Sambuc
240f4a2713aSLionel SambucTo test for this feature, use ``#if defined(__has_include)``:
241f4a2713aSLionel Sambuc
242f4a2713aSLionel Sambuc.. code-block:: c++
243f4a2713aSLionel Sambuc
244f4a2713aSLionel Sambuc  // To avoid problem with non-clang compilers not having this macro.
245f4a2713aSLionel Sambuc  #if defined(__has_include)
246f4a2713aSLionel Sambuc  #if __has_include("myinclude.h")
247f4a2713aSLionel Sambuc  # include "myinclude.h"
248f4a2713aSLionel Sambuc  #endif
249f4a2713aSLionel Sambuc  #endif
250f4a2713aSLionel Sambuc
251f4a2713aSLionel Sambuc.. _langext-__has_include_next:
252f4a2713aSLionel Sambuc
253f4a2713aSLionel Sambuc``__has_include_next``
254f4a2713aSLionel Sambuc----------------------
255f4a2713aSLionel Sambuc
256f4a2713aSLionel SambucThis function-like macro takes a single file name string argument that is the
257f4a2713aSLionel Sambucname of an include file.  It is like ``__has_include`` except that it looks for
258f4a2713aSLionel Sambucthe second instance of the given file found in the include paths.  It evaluates
259f4a2713aSLionel Sambucto 1 if the second instance of the file can be found using the include paths,
260f4a2713aSLionel Sambucor 0 otherwise:
261f4a2713aSLionel Sambuc
262f4a2713aSLionel Sambuc.. code-block:: c++
263f4a2713aSLionel Sambuc
264f4a2713aSLionel Sambuc  // Note the two possible file name string formats.
265f4a2713aSLionel Sambuc  #if __has_include_next("myinclude.h") && __has_include_next(<stdint.h>)
266f4a2713aSLionel Sambuc  # include_next "myinclude.h"
267f4a2713aSLionel Sambuc  #endif
268f4a2713aSLionel Sambuc
269f4a2713aSLionel Sambuc  // To avoid problem with non-clang compilers not having this macro.
270f4a2713aSLionel Sambuc  #if defined(__has_include_next)
271f4a2713aSLionel Sambuc  #if __has_include_next("myinclude.h")
272f4a2713aSLionel Sambuc  # include_next "myinclude.h"
273f4a2713aSLionel Sambuc  #endif
274f4a2713aSLionel Sambuc  #endif
275f4a2713aSLionel Sambuc
276f4a2713aSLionel SambucNote that ``__has_include_next``, like the GNU extension ``#include_next``
277f4a2713aSLionel Sambucdirective, is intended for use in headers only, and will issue a warning if
278f4a2713aSLionel Sambucused in the top-level compilation file.  A warning will also be issued if an
279f4a2713aSLionel Sambucabsolute path is used in the file argument.
280f4a2713aSLionel Sambuc
281f4a2713aSLionel Sambuc``__has_warning``
282f4a2713aSLionel Sambuc-----------------
283f4a2713aSLionel Sambuc
284f4a2713aSLionel SambucThis function-like macro takes a string literal that represents a command line
285f4a2713aSLionel Sambucoption for a warning and returns true if that is a valid warning option.
286f4a2713aSLionel Sambuc
287f4a2713aSLionel Sambuc.. code-block:: c++
288f4a2713aSLionel Sambuc
289f4a2713aSLionel Sambuc  #if __has_warning("-Wformat")
290f4a2713aSLionel Sambuc  ...
291f4a2713aSLionel Sambuc  #endif
292f4a2713aSLionel Sambuc
293f4a2713aSLionel SambucBuiltin Macros
294f4a2713aSLionel Sambuc==============
295f4a2713aSLionel Sambuc
296f4a2713aSLionel Sambuc``__BASE_FILE__``
297f4a2713aSLionel Sambuc  Defined to a string that contains the name of the main input file passed to
298f4a2713aSLionel Sambuc  Clang.
299f4a2713aSLionel Sambuc
300f4a2713aSLionel Sambuc``__COUNTER__``
301f4a2713aSLionel Sambuc  Defined to an integer value that starts at zero and is incremented each time
302f4a2713aSLionel Sambuc  the ``__COUNTER__`` macro is expanded.
303f4a2713aSLionel Sambuc
304f4a2713aSLionel Sambuc``__INCLUDE_LEVEL__``
305f4a2713aSLionel Sambuc  Defined to an integral value that is the include depth of the file currently
306f4a2713aSLionel Sambuc  being translated.  For the main file, this value is zero.
307f4a2713aSLionel Sambuc
308f4a2713aSLionel Sambuc``__TIMESTAMP__``
309f4a2713aSLionel Sambuc  Defined to the date and time of the last modification of the current source
310f4a2713aSLionel Sambuc  file.
311f4a2713aSLionel Sambuc
312f4a2713aSLionel Sambuc``__clang__``
313f4a2713aSLionel Sambuc  Defined when compiling with Clang
314f4a2713aSLionel Sambuc
315f4a2713aSLionel Sambuc``__clang_major__``
316f4a2713aSLionel Sambuc  Defined to the major marketing version number of Clang (e.g., the 2 in
317f4a2713aSLionel Sambuc  2.0.1).  Note that marketing version numbers should not be used to check for
318f4a2713aSLionel Sambuc  language features, as different vendors use different numbering schemes.
319f4a2713aSLionel Sambuc  Instead, use the :ref:`langext-feature_check`.
320f4a2713aSLionel Sambuc
321f4a2713aSLionel Sambuc``__clang_minor__``
322f4a2713aSLionel Sambuc  Defined to the minor version number of Clang (e.g., the 0 in 2.0.1).  Note
323f4a2713aSLionel Sambuc  that marketing version numbers should not be used to check for language
324f4a2713aSLionel Sambuc  features, as different vendors use different numbering schemes.  Instead, use
325f4a2713aSLionel Sambuc  the :ref:`langext-feature_check`.
326f4a2713aSLionel Sambuc
327f4a2713aSLionel Sambuc``__clang_patchlevel__``
328f4a2713aSLionel Sambuc  Defined to the marketing patch level of Clang (e.g., the 1 in 2.0.1).
329f4a2713aSLionel Sambuc
330f4a2713aSLionel Sambuc``__clang_version__``
331f4a2713aSLionel Sambuc  Defined to a string that captures the Clang marketing version, including the
332f4a2713aSLionel Sambuc  Subversion tag or revision number, e.g., "``1.5 (trunk 102332)``".
333f4a2713aSLionel Sambuc
334f4a2713aSLionel Sambuc.. _langext-vectors:
335f4a2713aSLionel Sambuc
336f4a2713aSLionel SambucVectors and Extended Vectors
337f4a2713aSLionel Sambuc============================
338f4a2713aSLionel Sambuc
339f4a2713aSLionel SambucSupports the GCC, OpenCL, AltiVec and NEON vector extensions.
340f4a2713aSLionel Sambuc
341f4a2713aSLionel SambucOpenCL vector types are created using ``ext_vector_type`` attribute.  It
342f4a2713aSLionel Sambucsupport for ``V.xyzw`` syntax and other tidbits as seen in OpenCL.  An example
343f4a2713aSLionel Sambucis:
344f4a2713aSLionel Sambuc
345f4a2713aSLionel Sambuc.. code-block:: c++
346f4a2713aSLionel Sambuc
347f4a2713aSLionel Sambuc  typedef float float4 __attribute__((ext_vector_type(4)));
348f4a2713aSLionel Sambuc  typedef float float2 __attribute__((ext_vector_type(2)));
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc  float4 foo(float2 a, float2 b) {
351f4a2713aSLionel Sambuc    float4 c;
352f4a2713aSLionel Sambuc    c.xz = a;
353f4a2713aSLionel Sambuc    c.yw = b;
354f4a2713aSLionel Sambuc    return c;
355f4a2713aSLionel Sambuc  }
356f4a2713aSLionel Sambuc
357f4a2713aSLionel SambucQuery for this feature with ``__has_extension(attribute_ext_vector_type)``.
358f4a2713aSLionel Sambuc
359f4a2713aSLionel SambucGiving ``-faltivec`` option to clang enables support for AltiVec vector syntax
360f4a2713aSLionel Sambucand functions.  For example:
361f4a2713aSLionel Sambuc
362f4a2713aSLionel Sambuc.. code-block:: c++
363f4a2713aSLionel Sambuc
364f4a2713aSLionel Sambuc  vector float foo(vector int a) {
365f4a2713aSLionel Sambuc    vector int b;
366f4a2713aSLionel Sambuc    b = vec_add(a, a) + a;
367f4a2713aSLionel Sambuc    return (vector float)b;
368f4a2713aSLionel Sambuc  }
369f4a2713aSLionel Sambuc
370f4a2713aSLionel SambucNEON vector types are created using ``neon_vector_type`` and
371f4a2713aSLionel Sambuc``neon_polyvector_type`` attributes.  For example:
372f4a2713aSLionel Sambuc
373f4a2713aSLionel Sambuc.. code-block:: c++
374f4a2713aSLionel Sambuc
375f4a2713aSLionel Sambuc  typedef __attribute__((neon_vector_type(8))) int8_t int8x8_t;
376f4a2713aSLionel Sambuc  typedef __attribute__((neon_polyvector_type(16))) poly8_t poly8x16_t;
377f4a2713aSLionel Sambuc
378f4a2713aSLionel Sambuc  int8x8_t foo(int8x8_t a) {
379f4a2713aSLionel Sambuc    int8x8_t v;
380f4a2713aSLionel Sambuc    v = a;
381f4a2713aSLionel Sambuc    return v;
382f4a2713aSLionel Sambuc  }
383f4a2713aSLionel Sambuc
384f4a2713aSLionel SambucVector Literals
385f4a2713aSLionel Sambuc---------------
386f4a2713aSLionel Sambuc
387f4a2713aSLionel SambucVector literals can be used to create vectors from a set of scalars, or
388f4a2713aSLionel Sambucvectors.  Either parentheses or braces form can be used.  In the parentheses
389f4a2713aSLionel Sambucform the number of literal values specified must be one, i.e. referring to a
390f4a2713aSLionel Sambucscalar value, or must match the size of the vector type being created.  If a
391f4a2713aSLionel Sambucsingle scalar literal value is specified, the scalar literal value will be
392f4a2713aSLionel Sambucreplicated to all the components of the vector type.  In the brackets form any
393f4a2713aSLionel Sambucnumber of literals can be specified.  For example:
394f4a2713aSLionel Sambuc
395f4a2713aSLionel Sambuc.. code-block:: c++
396f4a2713aSLionel Sambuc
397f4a2713aSLionel Sambuc  typedef int v4si __attribute__((__vector_size__(16)));
398f4a2713aSLionel Sambuc  typedef float float4 __attribute__((ext_vector_type(4)));
399f4a2713aSLionel Sambuc  typedef float float2 __attribute__((ext_vector_type(2)));
400f4a2713aSLionel Sambuc
401f4a2713aSLionel Sambuc  v4si vsi = (v4si){1, 2, 3, 4};
402f4a2713aSLionel Sambuc  float4 vf = (float4)(1.0f, 2.0f, 3.0f, 4.0f);
403f4a2713aSLionel Sambuc  vector int vi1 = (vector int)(1);    // vi1 will be (1, 1, 1, 1).
404f4a2713aSLionel Sambuc  vector int vi2 = (vector int){1};    // vi2 will be (1, 0, 0, 0).
405f4a2713aSLionel Sambuc  vector int vi3 = (vector int)(1, 2); // error
406f4a2713aSLionel Sambuc  vector int vi4 = (vector int){1, 2}; // vi4 will be (1, 2, 0, 0).
407f4a2713aSLionel Sambuc  vector int vi5 = (vector int)(1, 2, 3, 4);
408f4a2713aSLionel Sambuc  float4 vf = (float4)((float2)(1.0f, 2.0f), (float2)(3.0f, 4.0f));
409f4a2713aSLionel Sambuc
410f4a2713aSLionel SambucVector Operations
411f4a2713aSLionel Sambuc-----------------
412f4a2713aSLionel Sambuc
413f4a2713aSLionel SambucThe table below shows the support for each operation by vector extension.  A
414f4a2713aSLionel Sambucdash indicates that an operation is not accepted according to a corresponding
415f4a2713aSLionel Sambucspecification.
416f4a2713aSLionel Sambuc
417*0a6a1f1dSLionel Sambuc============================== ======= ======= ======= =======
418f4a2713aSLionel Sambuc         Opeator               OpenCL  AltiVec   GCC    NEON
419*0a6a1f1dSLionel Sambuc============================== ======= ======= ======= =======
420f4a2713aSLionel Sambuc[]                               yes     yes     yes     --
421f4a2713aSLionel Sambucunary operators +, --            yes     yes     yes     --
422f4a2713aSLionel Sambuc++, -- --                        yes     yes     yes     --
423f4a2713aSLionel Sambuc+,--,*,/,%                       yes     yes     yes     --
424f4a2713aSLionel Sambucbitwise operators &,|,^,~        yes     yes     yes     --
425f4a2713aSLionel Sambuc>>,<<                            yes     yes     yes     --
426*0a6a1f1dSLionel Sambuc!, &&, ||                        yes     --      --      --
427f4a2713aSLionel Sambuc==, !=, >, <, >=, <=             yes     yes     --      --
428f4a2713aSLionel Sambuc=                                yes     yes     yes     yes
429f4a2713aSLionel Sambuc:?                               yes     --      --      --
430f4a2713aSLionel Sambucsizeof                           yes     yes     yes     yes
431*0a6a1f1dSLionel SambucC-style cast                     yes     yes     yes     no
432*0a6a1f1dSLionel Sambucreinterpret_cast                 yes     no      yes     no
433*0a6a1f1dSLionel Sambucstatic_cast                      yes     no      yes     no
434*0a6a1f1dSLionel Sambucconst_cast                       no      no      no      no
435*0a6a1f1dSLionel Sambuc============================== ======= ======= ======= =======
436f4a2713aSLionel Sambuc
437*0a6a1f1dSLionel SambucSee also :ref:`langext-__builtin_shufflevector`, :ref:`langext-__builtin_convertvector`.
438f4a2713aSLionel Sambuc
439f4a2713aSLionel SambucMessages on ``deprecated`` and ``unavailable`` Attributes
440f4a2713aSLionel Sambuc=========================================================
441f4a2713aSLionel Sambuc
442f4a2713aSLionel SambucAn optional string message can be added to the ``deprecated`` and
443f4a2713aSLionel Sambuc``unavailable`` attributes.  For example:
444f4a2713aSLionel Sambuc
445f4a2713aSLionel Sambuc.. code-block:: c++
446f4a2713aSLionel Sambuc
447f4a2713aSLionel Sambuc  void explode(void) __attribute__((deprecated("extremely unsafe, use 'combust' instead!!!")));
448f4a2713aSLionel Sambuc
449f4a2713aSLionel SambucIf the deprecated or unavailable declaration is used, the message will be
450f4a2713aSLionel Sambucincorporated into the appropriate diagnostic:
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc.. code-block:: c++
453f4a2713aSLionel Sambuc
454f4a2713aSLionel Sambuc  harmless.c:4:3: warning: 'explode' is deprecated: extremely unsafe, use 'combust' instead!!!
455f4a2713aSLionel Sambuc        [-Wdeprecated-declarations]
456f4a2713aSLionel Sambuc    explode();
457f4a2713aSLionel Sambuc    ^
458f4a2713aSLionel Sambuc
459f4a2713aSLionel SambucQuery for this feature with
460f4a2713aSLionel Sambuc``__has_extension(attribute_deprecated_with_message)`` and
461f4a2713aSLionel Sambuc``__has_extension(attribute_unavailable_with_message)``.
462f4a2713aSLionel Sambuc
463f4a2713aSLionel SambucAttributes on Enumerators
464f4a2713aSLionel Sambuc=========================
465f4a2713aSLionel Sambuc
466f4a2713aSLionel SambucClang allows attributes to be written on individual enumerators.  This allows
467f4a2713aSLionel Sambucenumerators to be deprecated, made unavailable, etc.  The attribute must appear
468f4a2713aSLionel Sambucafter the enumerator name and before any initializer, like so:
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc.. code-block:: c++
471f4a2713aSLionel Sambuc
472f4a2713aSLionel Sambuc  enum OperationMode {
473f4a2713aSLionel Sambuc    OM_Invalid,
474f4a2713aSLionel Sambuc    OM_Normal,
475f4a2713aSLionel Sambuc    OM_Terrified __attribute__((deprecated)),
476f4a2713aSLionel Sambuc    OM_AbortOnError __attribute__((deprecated)) = 4
477f4a2713aSLionel Sambuc  };
478f4a2713aSLionel Sambuc
479f4a2713aSLionel SambucAttributes on the ``enum`` declaration do not apply to individual enumerators.
480f4a2713aSLionel Sambuc
481f4a2713aSLionel SambucQuery for this feature with ``__has_extension(enumerator_attributes)``.
482f4a2713aSLionel Sambuc
483f4a2713aSLionel Sambuc'User-Specified' System Frameworks
484f4a2713aSLionel Sambuc==================================
485f4a2713aSLionel Sambuc
486f4a2713aSLionel SambucClang provides a mechanism by which frameworks can be built in such a way that
487f4a2713aSLionel Sambucthey will always be treated as being "system frameworks", even if they are not
488f4a2713aSLionel Sambucpresent in a system framework directory.  This can be useful to system
489f4a2713aSLionel Sambucframework developers who want to be able to test building other applications
490f4a2713aSLionel Sambucwith development builds of their framework, including the manner in which the
491f4a2713aSLionel Sambuccompiler changes warning behavior for system headers.
492f4a2713aSLionel Sambuc
493f4a2713aSLionel SambucFramework developers can opt-in to this mechanism by creating a
494f4a2713aSLionel Sambuc"``.system_framework``" file at the top-level of their framework.  That is, the
495f4a2713aSLionel Sambucframework should have contents like:
496f4a2713aSLionel Sambuc
497f4a2713aSLionel Sambuc.. code-block:: none
498f4a2713aSLionel Sambuc
499f4a2713aSLionel Sambuc  .../TestFramework.framework
500f4a2713aSLionel Sambuc  .../TestFramework.framework/.system_framework
501f4a2713aSLionel Sambuc  .../TestFramework.framework/Headers
502f4a2713aSLionel Sambuc  .../TestFramework.framework/Headers/TestFramework.h
503f4a2713aSLionel Sambuc  ...
504f4a2713aSLionel Sambuc
505f4a2713aSLionel SambucClang will treat the presence of this file as an indicator that the framework
506f4a2713aSLionel Sambucshould be treated as a system framework, regardless of how it was found in the
507f4a2713aSLionel Sambucframework search path.  For consistency, we recommend that such files never be
508f4a2713aSLionel Sambucincluded in installed versions of the framework.
509f4a2713aSLionel Sambuc
510f4a2713aSLionel SambucChecks for Standard Language Features
511f4a2713aSLionel Sambuc=====================================
512f4a2713aSLionel Sambuc
513f4a2713aSLionel SambucThe ``__has_feature`` macro can be used to query if certain standard language
514f4a2713aSLionel Sambucfeatures are enabled.  The ``__has_extension`` macro can be used to query if
515f4a2713aSLionel Sambuclanguage features are available as an extension when compiling for a standard
516f4a2713aSLionel Sambucwhich does not provide them.  The features which can be tested are listed here.
517f4a2713aSLionel Sambuc
518*0a6a1f1dSLionel SambucSince Clang 3.4, the C++ SD-6 feature test macros are also supported.
519*0a6a1f1dSLionel SambucThese are macros with names of the form ``__cpp_<feature_name>``, and are
520*0a6a1f1dSLionel Sambucintended to be a portable way to query the supported features of the compiler.
521*0a6a1f1dSLionel SambucSee `the C++ status page <http://clang.llvm.org/cxx_status.html#ts>`_ for
522*0a6a1f1dSLionel Sambucinformation on the version of SD-6 supported by each Clang release, and the
523*0a6a1f1dSLionel Sambucmacros provided by that revision of the recommendations.
524*0a6a1f1dSLionel Sambuc
525f4a2713aSLionel SambucC++98
526f4a2713aSLionel Sambuc-----
527f4a2713aSLionel Sambuc
528f4a2713aSLionel SambucThe features listed below are part of the C++98 standard.  These features are
529f4a2713aSLionel Sambucenabled by default when compiling C++ code.
530f4a2713aSLionel Sambuc
531f4a2713aSLionel SambucC++ exceptions
532f4a2713aSLionel Sambuc^^^^^^^^^^^^^^
533f4a2713aSLionel Sambuc
534f4a2713aSLionel SambucUse ``__has_feature(cxx_exceptions)`` to determine if C++ exceptions have been
535f4a2713aSLionel Sambucenabled.  For example, compiling code with ``-fno-exceptions`` disables C++
536f4a2713aSLionel Sambucexceptions.
537f4a2713aSLionel Sambuc
538f4a2713aSLionel SambucC++ RTTI
539f4a2713aSLionel Sambuc^^^^^^^^
540f4a2713aSLionel Sambuc
541f4a2713aSLionel SambucUse ``__has_feature(cxx_rtti)`` to determine if C++ RTTI has been enabled.  For
542f4a2713aSLionel Sambucexample, compiling code with ``-fno-rtti`` disables the use of RTTI.
543f4a2713aSLionel Sambuc
544f4a2713aSLionel SambucC++11
545f4a2713aSLionel Sambuc-----
546f4a2713aSLionel Sambuc
547f4a2713aSLionel SambucThe features listed below are part of the C++11 standard.  As a result, all
548f4a2713aSLionel Sambucthese features are enabled with the ``-std=c++11`` or ``-std=gnu++11`` option
549f4a2713aSLionel Sambucwhen compiling C++ code.
550f4a2713aSLionel Sambuc
551f4a2713aSLionel SambucC++11 SFINAE includes access control
552f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
553f4a2713aSLionel Sambuc
554f4a2713aSLionel SambucUse ``__has_feature(cxx_access_control_sfinae)`` or
555f4a2713aSLionel Sambuc``__has_extension(cxx_access_control_sfinae)`` to determine whether
556f4a2713aSLionel Sambucaccess-control errors (e.g., calling a private constructor) are considered to
557f4a2713aSLionel Sambucbe template argument deduction errors (aka SFINAE errors), per `C++ DR1170
558f4a2713aSLionel Sambuc<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1170>`_.
559f4a2713aSLionel Sambuc
560f4a2713aSLionel SambucC++11 alias templates
561f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
562f4a2713aSLionel Sambuc
563f4a2713aSLionel SambucUse ``__has_feature(cxx_alias_templates)`` or
564f4a2713aSLionel Sambuc``__has_extension(cxx_alias_templates)`` to determine if support for C++11's
565f4a2713aSLionel Sambucalias declarations and alias templates is enabled.
566f4a2713aSLionel Sambuc
567f4a2713aSLionel SambucC++11 alignment specifiers
568f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^
569f4a2713aSLionel Sambuc
570f4a2713aSLionel SambucUse ``__has_feature(cxx_alignas)`` or ``__has_extension(cxx_alignas)`` to
571f4a2713aSLionel Sambucdetermine if support for alignment specifiers using ``alignas`` is enabled.
572f4a2713aSLionel Sambuc
573*0a6a1f1dSLionel SambucUse ``__has_feature(cxx_alignof)`` or ``__has_extension(cxx_alignof)`` to
574*0a6a1f1dSLionel Sambucdetermine if support for the ``alignof`` keyword is enabled.
575*0a6a1f1dSLionel Sambuc
576f4a2713aSLionel SambucC++11 attributes
577f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^
578f4a2713aSLionel Sambuc
579f4a2713aSLionel SambucUse ``__has_feature(cxx_attributes)`` or ``__has_extension(cxx_attributes)`` to
580f4a2713aSLionel Sambucdetermine if support for attribute parsing with C++11's square bracket notation
581f4a2713aSLionel Sambucis enabled.
582f4a2713aSLionel Sambuc
583f4a2713aSLionel SambucC++11 generalized constant expressions
584f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
585f4a2713aSLionel Sambuc
586f4a2713aSLionel SambucUse ``__has_feature(cxx_constexpr)`` to determine if support for generalized
587f4a2713aSLionel Sambucconstant expressions (e.g., ``constexpr``) is enabled.
588f4a2713aSLionel Sambuc
589f4a2713aSLionel SambucC++11 ``decltype()``
590f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
591f4a2713aSLionel Sambuc
592f4a2713aSLionel SambucUse ``__has_feature(cxx_decltype)`` or ``__has_extension(cxx_decltype)`` to
593f4a2713aSLionel Sambucdetermine if support for the ``decltype()`` specifier is enabled.  C++11's
594f4a2713aSLionel Sambuc``decltype`` does not require type-completeness of a function call expression.
595f4a2713aSLionel SambucUse ``__has_feature(cxx_decltype_incomplete_return_types)`` or
596f4a2713aSLionel Sambuc``__has_extension(cxx_decltype_incomplete_return_types)`` to determine if
597f4a2713aSLionel Sambucsupport for this feature is enabled.
598f4a2713aSLionel Sambuc
599f4a2713aSLionel SambucC++11 default template arguments in function templates
600f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
601f4a2713aSLionel Sambuc
602f4a2713aSLionel SambucUse ``__has_feature(cxx_default_function_template_args)`` or
603f4a2713aSLionel Sambuc``__has_extension(cxx_default_function_template_args)`` to determine if support
604f4a2713aSLionel Sambucfor default template arguments in function templates is enabled.
605f4a2713aSLionel Sambuc
606f4a2713aSLionel SambucC++11 ``default``\ ed functions
607f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
608f4a2713aSLionel Sambuc
609f4a2713aSLionel SambucUse ``__has_feature(cxx_defaulted_functions)`` or
610f4a2713aSLionel Sambuc``__has_extension(cxx_defaulted_functions)`` to determine if support for
611f4a2713aSLionel Sambucdefaulted function definitions (with ``= default``) is enabled.
612f4a2713aSLionel Sambuc
613f4a2713aSLionel SambucC++11 delegating constructors
614f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
615f4a2713aSLionel Sambuc
616f4a2713aSLionel SambucUse ``__has_feature(cxx_delegating_constructors)`` to determine if support for
617f4a2713aSLionel Sambucdelegating constructors is enabled.
618f4a2713aSLionel Sambuc
619f4a2713aSLionel SambucC++11 ``deleted`` functions
620f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^
621f4a2713aSLionel Sambuc
622f4a2713aSLionel SambucUse ``__has_feature(cxx_deleted_functions)`` or
623f4a2713aSLionel Sambuc``__has_extension(cxx_deleted_functions)`` to determine if support for deleted
624f4a2713aSLionel Sambucfunction definitions (with ``= delete``) is enabled.
625f4a2713aSLionel Sambuc
626f4a2713aSLionel SambucC++11 explicit conversion functions
627f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
628f4a2713aSLionel Sambuc
629f4a2713aSLionel SambucUse ``__has_feature(cxx_explicit_conversions)`` to determine if support for
630f4a2713aSLionel Sambuc``explicit`` conversion functions is enabled.
631f4a2713aSLionel Sambuc
632f4a2713aSLionel SambucC++11 generalized initializers
633f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
634f4a2713aSLionel Sambuc
635f4a2713aSLionel SambucUse ``__has_feature(cxx_generalized_initializers)`` to determine if support for
636f4a2713aSLionel Sambucgeneralized initializers (using braced lists and ``std::initializer_list``) is
637f4a2713aSLionel Sambucenabled.
638f4a2713aSLionel Sambuc
639f4a2713aSLionel SambucC++11 implicit move constructors/assignment operators
640f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
641f4a2713aSLionel Sambuc
642f4a2713aSLionel SambucUse ``__has_feature(cxx_implicit_moves)`` to determine if Clang will implicitly
643f4a2713aSLionel Sambucgenerate move constructors and move assignment operators where needed.
644f4a2713aSLionel Sambuc
645f4a2713aSLionel SambucC++11 inheriting constructors
646f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
647f4a2713aSLionel Sambuc
648f4a2713aSLionel SambucUse ``__has_feature(cxx_inheriting_constructors)`` to determine if support for
649f4a2713aSLionel Sambucinheriting constructors is enabled.
650f4a2713aSLionel Sambuc
651f4a2713aSLionel SambucC++11 inline namespaces
652f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
653f4a2713aSLionel Sambuc
654f4a2713aSLionel SambucUse ``__has_feature(cxx_inline_namespaces)`` or
655f4a2713aSLionel Sambuc``__has_extension(cxx_inline_namespaces)`` to determine if support for inline
656f4a2713aSLionel Sambucnamespaces is enabled.
657f4a2713aSLionel Sambuc
658f4a2713aSLionel SambucC++11 lambdas
659f4a2713aSLionel Sambuc^^^^^^^^^^^^^
660f4a2713aSLionel Sambuc
661f4a2713aSLionel SambucUse ``__has_feature(cxx_lambdas)`` or ``__has_extension(cxx_lambdas)`` to
662f4a2713aSLionel Sambucdetermine if support for lambdas is enabled.
663f4a2713aSLionel Sambuc
664f4a2713aSLionel SambucC++11 local and unnamed types as template arguments
665f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
666f4a2713aSLionel Sambuc
667f4a2713aSLionel SambucUse ``__has_feature(cxx_local_type_template_args)`` or
668f4a2713aSLionel Sambuc``__has_extension(cxx_local_type_template_args)`` to determine if support for
669f4a2713aSLionel Sambuclocal and unnamed types as template arguments is enabled.
670f4a2713aSLionel Sambuc
671f4a2713aSLionel SambucC++11 noexcept
672f4a2713aSLionel Sambuc^^^^^^^^^^^^^^
673f4a2713aSLionel Sambuc
674f4a2713aSLionel SambucUse ``__has_feature(cxx_noexcept)`` or ``__has_extension(cxx_noexcept)`` to
675f4a2713aSLionel Sambucdetermine if support for noexcept exception specifications is enabled.
676f4a2713aSLionel Sambuc
677f4a2713aSLionel SambucC++11 in-class non-static data member initialization
678f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
679f4a2713aSLionel Sambuc
680f4a2713aSLionel SambucUse ``__has_feature(cxx_nonstatic_member_init)`` to determine whether in-class
681f4a2713aSLionel Sambucinitialization of non-static data members is enabled.
682f4a2713aSLionel Sambuc
683f4a2713aSLionel SambucC++11 ``nullptr``
684f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^
685f4a2713aSLionel Sambuc
686f4a2713aSLionel SambucUse ``__has_feature(cxx_nullptr)`` or ``__has_extension(cxx_nullptr)`` to
687f4a2713aSLionel Sambucdetermine if support for ``nullptr`` is enabled.
688f4a2713aSLionel Sambuc
689f4a2713aSLionel SambucC++11 ``override control``
690f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^
691f4a2713aSLionel Sambuc
692f4a2713aSLionel SambucUse ``__has_feature(cxx_override_control)`` or
693f4a2713aSLionel Sambuc``__has_extension(cxx_override_control)`` to determine if support for the
694f4a2713aSLionel Sambucoverride control keywords is enabled.
695f4a2713aSLionel Sambuc
696f4a2713aSLionel SambucC++11 reference-qualified functions
697f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
698f4a2713aSLionel Sambuc
699f4a2713aSLionel SambucUse ``__has_feature(cxx_reference_qualified_functions)`` or
700f4a2713aSLionel Sambuc``__has_extension(cxx_reference_qualified_functions)`` to determine if support
701f4a2713aSLionel Sambucfor reference-qualified functions (e.g., member functions with ``&`` or ``&&``
702f4a2713aSLionel Sambucapplied to ``*this``) is enabled.
703f4a2713aSLionel Sambuc
704f4a2713aSLionel SambucC++11 range-based ``for`` loop
705f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
706f4a2713aSLionel Sambuc
707f4a2713aSLionel SambucUse ``__has_feature(cxx_range_for)`` or ``__has_extension(cxx_range_for)`` to
708f4a2713aSLionel Sambucdetermine if support for the range-based for loop is enabled.
709f4a2713aSLionel Sambuc
710f4a2713aSLionel SambucC++11 raw string literals
711f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^
712f4a2713aSLionel Sambuc
713f4a2713aSLionel SambucUse ``__has_feature(cxx_raw_string_literals)`` to determine if support for raw
714f4a2713aSLionel Sambucstring literals (e.g., ``R"x(foo\bar)x"``) is enabled.
715f4a2713aSLionel Sambuc
716f4a2713aSLionel SambucC++11 rvalue references
717f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
718f4a2713aSLionel Sambuc
719f4a2713aSLionel SambucUse ``__has_feature(cxx_rvalue_references)`` or
720f4a2713aSLionel Sambuc``__has_extension(cxx_rvalue_references)`` to determine if support for rvalue
721f4a2713aSLionel Sambucreferences is enabled.
722f4a2713aSLionel Sambuc
723f4a2713aSLionel SambucC++11 ``static_assert()``
724f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^
725f4a2713aSLionel Sambuc
726f4a2713aSLionel SambucUse ``__has_feature(cxx_static_assert)`` or
727f4a2713aSLionel Sambuc``__has_extension(cxx_static_assert)`` to determine if support for compile-time
728f4a2713aSLionel Sambucassertions using ``static_assert`` is enabled.
729f4a2713aSLionel Sambuc
730f4a2713aSLionel SambucC++11 ``thread_local``
731f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
732f4a2713aSLionel Sambuc
733f4a2713aSLionel SambucUse ``__has_feature(cxx_thread_local)`` to determine if support for
734f4a2713aSLionel Sambuc``thread_local`` variables is enabled.
735f4a2713aSLionel Sambuc
736f4a2713aSLionel SambucC++11 type inference
737f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
738f4a2713aSLionel Sambuc
739f4a2713aSLionel SambucUse ``__has_feature(cxx_auto_type)`` or ``__has_extension(cxx_auto_type)`` to
740f4a2713aSLionel Sambucdetermine C++11 type inference is supported using the ``auto`` specifier.  If
741f4a2713aSLionel Sambucthis is disabled, ``auto`` will instead be a storage class specifier, as in C
742f4a2713aSLionel Sambucor C++98.
743f4a2713aSLionel Sambuc
744f4a2713aSLionel SambucC++11 strongly typed enumerations
745f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
746f4a2713aSLionel Sambuc
747f4a2713aSLionel SambucUse ``__has_feature(cxx_strong_enums)`` or
748f4a2713aSLionel Sambuc``__has_extension(cxx_strong_enums)`` to determine if support for strongly
749f4a2713aSLionel Sambuctyped, scoped enumerations is enabled.
750f4a2713aSLionel Sambuc
751f4a2713aSLionel SambucC++11 trailing return type
752f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^
753f4a2713aSLionel Sambuc
754f4a2713aSLionel SambucUse ``__has_feature(cxx_trailing_return)`` or
755f4a2713aSLionel Sambuc``__has_extension(cxx_trailing_return)`` to determine if support for the
756f4a2713aSLionel Sambucalternate function declaration syntax with trailing return type is enabled.
757f4a2713aSLionel Sambuc
758f4a2713aSLionel SambucC++11 Unicode string literals
759f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
760f4a2713aSLionel Sambuc
761f4a2713aSLionel SambucUse ``__has_feature(cxx_unicode_literals)`` to determine if support for Unicode
762f4a2713aSLionel Sambucstring literals is enabled.
763f4a2713aSLionel Sambuc
764f4a2713aSLionel SambucC++11 unrestricted unions
765f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^
766f4a2713aSLionel Sambuc
767f4a2713aSLionel SambucUse ``__has_feature(cxx_unrestricted_unions)`` to determine if support for
768f4a2713aSLionel Sambucunrestricted unions is enabled.
769f4a2713aSLionel Sambuc
770f4a2713aSLionel SambucC++11 user-defined literals
771f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^
772f4a2713aSLionel Sambuc
773f4a2713aSLionel SambucUse ``__has_feature(cxx_user_literals)`` to determine if support for
774f4a2713aSLionel Sambucuser-defined literals is enabled.
775f4a2713aSLionel Sambuc
776f4a2713aSLionel SambucC++11 variadic templates
777f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
778f4a2713aSLionel Sambuc
779f4a2713aSLionel SambucUse ``__has_feature(cxx_variadic_templates)`` or
780f4a2713aSLionel Sambuc``__has_extension(cxx_variadic_templates)`` to determine if support for
781f4a2713aSLionel Sambucvariadic templates is enabled.
782f4a2713aSLionel Sambuc
783f4a2713aSLionel SambucC++1y
784f4a2713aSLionel Sambuc-----
785f4a2713aSLionel Sambuc
786f4a2713aSLionel SambucThe features listed below are part of the committee draft for the C++1y
787f4a2713aSLionel Sambucstandard.  As a result, all these features are enabled with the ``-std=c++1y``
788f4a2713aSLionel Sambucor ``-std=gnu++1y`` option when compiling C++ code.
789f4a2713aSLionel Sambuc
790f4a2713aSLionel SambucC++1y binary literals
791f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
792f4a2713aSLionel Sambuc
793f4a2713aSLionel SambucUse ``__has_feature(cxx_binary_literals)`` or
794f4a2713aSLionel Sambuc``__has_extension(cxx_binary_literals)`` to determine whether
795f4a2713aSLionel Sambucbinary literals (for instance, ``0b10010``) are recognized. Clang supports this
796f4a2713aSLionel Sambucfeature as an extension in all language modes.
797f4a2713aSLionel Sambuc
798f4a2713aSLionel SambucC++1y contextual conversions
799f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^
800f4a2713aSLionel Sambuc
801f4a2713aSLionel SambucUse ``__has_feature(cxx_contextual_conversions)`` or
802f4a2713aSLionel Sambuc``__has_extension(cxx_contextual_conversions)`` to determine if the C++1y rules
803f4a2713aSLionel Sambucare used when performing an implicit conversion for an array bound in a
804f4a2713aSLionel Sambuc*new-expression*, the operand of a *delete-expression*, an integral constant
805f4a2713aSLionel Sambucexpression, or a condition in a ``switch`` statement.
806f4a2713aSLionel Sambuc
807f4a2713aSLionel SambucC++1y decltype(auto)
808f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
809f4a2713aSLionel Sambuc
810f4a2713aSLionel SambucUse ``__has_feature(cxx_decltype_auto)`` or
811f4a2713aSLionel Sambuc``__has_extension(cxx_decltype_auto)`` to determine if support
812f4a2713aSLionel Sambucfor the ``decltype(auto)`` placeholder type is enabled.
813f4a2713aSLionel Sambuc
814f4a2713aSLionel SambucC++1y default initializers for aggregates
815f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
816f4a2713aSLionel Sambuc
817f4a2713aSLionel SambucUse ``__has_feature(cxx_aggregate_nsdmi)`` or
818f4a2713aSLionel Sambuc``__has_extension(cxx_aggregate_nsdmi)`` to determine if support
819f4a2713aSLionel Sambucfor default initializers in aggregate members is enabled.
820f4a2713aSLionel Sambuc
821*0a6a1f1dSLionel SambucC++1y digit separators
822*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
823*0a6a1f1dSLionel Sambuc
824*0a6a1f1dSLionel SambucUse ``__cpp_digit_separators`` to determine if support for digit separators
825*0a6a1f1dSLionel Sambucusing single quotes (for instance, ``10'000``) is enabled. At this time, there
826*0a6a1f1dSLionel Sambucis no corresponding ``__has_feature`` name
827*0a6a1f1dSLionel Sambuc
828f4a2713aSLionel SambucC++1y generalized lambda capture
829f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
830f4a2713aSLionel Sambuc
831*0a6a1f1dSLionel SambucUse ``__has_feature(cxx_init_captures)`` or
832*0a6a1f1dSLionel Sambuc``__has_extension(cxx_init_captures)`` to determine if support for
833f4a2713aSLionel Sambuclambda captures with explicit initializers is enabled
834f4a2713aSLionel Sambuc(for instance, ``[n(0)] { return ++n; }``).
835f4a2713aSLionel Sambuc
836f4a2713aSLionel SambucC++1y generic lambdas
837f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
838f4a2713aSLionel Sambuc
839*0a6a1f1dSLionel SambucUse ``__has_feature(cxx_generic_lambdas)`` or
840*0a6a1f1dSLionel Sambuc``__has_extension(cxx_generic_lambdas)`` to determine if support for generic
841f4a2713aSLionel Sambuc(polymorphic) lambdas is enabled
842f4a2713aSLionel Sambuc(for instance, ``[] (auto x) { return x + 1; }``).
843f4a2713aSLionel Sambuc
844f4a2713aSLionel SambucC++1y relaxed constexpr
845f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
846f4a2713aSLionel Sambuc
847f4a2713aSLionel SambucUse ``__has_feature(cxx_relaxed_constexpr)`` or
848f4a2713aSLionel Sambuc``__has_extension(cxx_relaxed_constexpr)`` to determine if variable
849f4a2713aSLionel Sambucdeclarations, local variable modification, and control flow constructs
850f4a2713aSLionel Sambucare permitted in ``constexpr`` functions.
851f4a2713aSLionel Sambuc
852f4a2713aSLionel SambucC++1y return type deduction
853f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^
854f4a2713aSLionel Sambuc
855f4a2713aSLionel SambucUse ``__has_feature(cxx_return_type_deduction)`` or
856f4a2713aSLionel Sambuc``__has_extension(cxx_return_type_deduction)`` to determine if support
857f4a2713aSLionel Sambucfor return type deduction for functions (using ``auto`` as a return type)
858f4a2713aSLionel Sambucis enabled.
859f4a2713aSLionel Sambuc
860f4a2713aSLionel SambucC++1y runtime-sized arrays
861f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^
862f4a2713aSLionel Sambuc
863f4a2713aSLionel SambucUse ``__has_feature(cxx_runtime_array)`` or
864f4a2713aSLionel Sambuc``__has_extension(cxx_runtime_array)`` to determine if support
865f4a2713aSLionel Sambucfor arrays of runtime bound (a restricted form of variable-length arrays)
866f4a2713aSLionel Sambucis enabled.
867f4a2713aSLionel SambucClang's implementation of this feature is incomplete.
868f4a2713aSLionel Sambuc
869f4a2713aSLionel SambucC++1y variable templates
870f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
871f4a2713aSLionel Sambuc
872f4a2713aSLionel SambucUse ``__has_feature(cxx_variable_templates)`` or
873f4a2713aSLionel Sambuc``__has_extension(cxx_variable_templates)`` to determine if support for
874f4a2713aSLionel Sambuctemplated variable declarations is enabled.
875f4a2713aSLionel Sambuc
876f4a2713aSLionel SambucC11
877f4a2713aSLionel Sambuc---
878f4a2713aSLionel Sambuc
879f4a2713aSLionel SambucThe features listed below are part of the C11 standard.  As a result, all these
880f4a2713aSLionel Sambucfeatures are enabled with the ``-std=c11`` or ``-std=gnu11`` option when
881f4a2713aSLionel Sambuccompiling C code.  Additionally, because these features are all
882f4a2713aSLionel Sambucbackward-compatible, they are available as extensions in all language modes.
883f4a2713aSLionel Sambuc
884f4a2713aSLionel SambucC11 alignment specifiers
885f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
886f4a2713aSLionel Sambuc
887f4a2713aSLionel SambucUse ``__has_feature(c_alignas)`` or ``__has_extension(c_alignas)`` to determine
888f4a2713aSLionel Sambucif support for alignment specifiers using ``_Alignas`` is enabled.
889f4a2713aSLionel Sambuc
890*0a6a1f1dSLionel SambucUse ``__has_feature(c_alignof)`` or ``__has_extension(c_alignof)`` to determine
891*0a6a1f1dSLionel Sambucif support for the ``_Alignof`` keyword is enabled.
892*0a6a1f1dSLionel Sambuc
893f4a2713aSLionel SambucC11 atomic operations
894f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
895f4a2713aSLionel Sambuc
896f4a2713aSLionel SambucUse ``__has_feature(c_atomic)`` or ``__has_extension(c_atomic)`` to determine
897f4a2713aSLionel Sambucif support for atomic types using ``_Atomic`` is enabled.  Clang also provides
898f4a2713aSLionel Sambuc:ref:`a set of builtins <langext-__c11_atomic>` which can be used to implement
899*0a6a1f1dSLionel Sambucthe ``<stdatomic.h>`` operations on ``_Atomic`` types. Use
900*0a6a1f1dSLionel Sambuc``__has_include(<stdatomic.h>)`` to determine if C11's ``<stdatomic.h>`` header
901*0a6a1f1dSLionel Sambucis available.
902*0a6a1f1dSLionel Sambuc
903*0a6a1f1dSLionel SambucClang will use the system's ``<stdatomic.h>`` header when one is available, and
904*0a6a1f1dSLionel Sambucwill otherwise use its own. When using its own, implementations of the atomic
905*0a6a1f1dSLionel Sambucoperations are provided as macros. In the cases where C11 also requires a real
906*0a6a1f1dSLionel Sambucfunction, this header provides only the declaration of that function (along
907*0a6a1f1dSLionel Sambucwith a shadowing macro implementation), and you must link to a library which
908*0a6a1f1dSLionel Sambucprovides a definition of the function if you use it instead of the macro.
909f4a2713aSLionel Sambuc
910f4a2713aSLionel SambucC11 generic selections
911f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
912f4a2713aSLionel Sambuc
913f4a2713aSLionel SambucUse ``__has_feature(c_generic_selections)`` or
914f4a2713aSLionel Sambuc``__has_extension(c_generic_selections)`` to determine if support for generic
915f4a2713aSLionel Sambucselections is enabled.
916f4a2713aSLionel Sambuc
917f4a2713aSLionel SambucAs an extension, the C11 generic selection expression is available in all
918f4a2713aSLionel Sambuclanguages supported by Clang.  The syntax is the same as that given in the C11
919f4a2713aSLionel Sambucstandard.
920f4a2713aSLionel Sambuc
921f4a2713aSLionel SambucIn C, type compatibility is decided according to the rules given in the
922f4a2713aSLionel Sambucappropriate standard, but in C++, which lacks the type compatibility rules used
923f4a2713aSLionel Sambucin C, types are considered compatible only if they are equivalent.
924f4a2713aSLionel Sambuc
925f4a2713aSLionel SambucC11 ``_Static_assert()``
926f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
927f4a2713aSLionel Sambuc
928f4a2713aSLionel SambucUse ``__has_feature(c_static_assert)`` or ``__has_extension(c_static_assert)``
929f4a2713aSLionel Sambucto determine if support for compile-time assertions using ``_Static_assert`` is
930f4a2713aSLionel Sambucenabled.
931f4a2713aSLionel Sambuc
932f4a2713aSLionel SambucC11 ``_Thread_local``
933f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
934f4a2713aSLionel Sambuc
935f4a2713aSLionel SambucUse ``__has_feature(c_thread_local)`` or ``__has_extension(c_thread_local)``
936f4a2713aSLionel Sambucto determine if support for ``_Thread_local`` variables is enabled.
937f4a2713aSLionel Sambuc
938*0a6a1f1dSLionel SambucChecks for Type Trait Primitives
939*0a6a1f1dSLionel Sambuc================================
940*0a6a1f1dSLionel Sambuc
941*0a6a1f1dSLionel SambucType trait primitives are special builtin constant expressions that can be used
942*0a6a1f1dSLionel Sambucby the standard C++ library to facilitate or simplify the implementation of
943*0a6a1f1dSLionel Sambucuser-facing type traits in the <type_traits> header.
944*0a6a1f1dSLionel Sambuc
945*0a6a1f1dSLionel SambucThey are not intended to be used directly by user code because they are
946*0a6a1f1dSLionel Sambucimplementation-defined and subject to change -- as such they're tied closely to
947*0a6a1f1dSLionel Sambucthe supported set of system headers, currently:
948*0a6a1f1dSLionel Sambuc
949*0a6a1f1dSLionel Sambuc* LLVM's own libc++
950*0a6a1f1dSLionel Sambuc* GNU libstdc++
951*0a6a1f1dSLionel Sambuc* The Microsoft standard C++ library
952f4a2713aSLionel Sambuc
953f4a2713aSLionel SambucClang supports the `GNU C++ type traits
954f4a2713aSLionel Sambuc<http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html>`_ and a subset of the
955f4a2713aSLionel Sambuc`Microsoft Visual C++ Type traits
956*0a6a1f1dSLionel Sambuc<http://msdn.microsoft.com/en-us/library/ms177194(v=VS.100).aspx>`_.
957*0a6a1f1dSLionel Sambuc
958*0a6a1f1dSLionel SambucFeature detection is supported only for some of the primitives at present. User
959*0a6a1f1dSLionel Sambuccode should not use these checks because they bear no direct relation to the
960*0a6a1f1dSLionel Sambucactual set of type traits supported by the C++ standard library.
961*0a6a1f1dSLionel Sambuc
962*0a6a1f1dSLionel SambucFor type trait ``__X``, ``__has_extension(X)`` indicates the presence of the
963*0a6a1f1dSLionel Sambuctype trait primitive in the compiler. A simplistic usage example as might be
964*0a6a1f1dSLionel Sambucseen in standard C++ headers follows:
965f4a2713aSLionel Sambuc
966f4a2713aSLionel Sambuc.. code-block:: c++
967f4a2713aSLionel Sambuc
968f4a2713aSLionel Sambuc  #if __has_extension(is_convertible_to)
969f4a2713aSLionel Sambuc  template<typename From, typename To>
970f4a2713aSLionel Sambuc  struct is_convertible_to {
971f4a2713aSLionel Sambuc    static const bool value = __is_convertible_to(From, To);
972f4a2713aSLionel Sambuc  };
973f4a2713aSLionel Sambuc  #else
974*0a6a1f1dSLionel Sambuc  // Emulate type trait for compatibility with other compilers.
975f4a2713aSLionel Sambuc  #endif
976f4a2713aSLionel Sambuc
977*0a6a1f1dSLionel SambucThe following type trait primitives are supported by Clang:
978f4a2713aSLionel Sambuc
979f4a2713aSLionel Sambuc* ``__has_nothrow_assign`` (GNU, Microsoft)
980f4a2713aSLionel Sambuc* ``__has_nothrow_copy`` (GNU, Microsoft)
981f4a2713aSLionel Sambuc* ``__has_nothrow_constructor`` (GNU, Microsoft)
982f4a2713aSLionel Sambuc* ``__has_trivial_assign`` (GNU, Microsoft)
983f4a2713aSLionel Sambuc* ``__has_trivial_copy`` (GNU, Microsoft)
984f4a2713aSLionel Sambuc* ``__has_trivial_constructor`` (GNU, Microsoft)
985f4a2713aSLionel Sambuc* ``__has_trivial_destructor`` (GNU, Microsoft)
986f4a2713aSLionel Sambuc* ``__has_virtual_destructor`` (GNU, Microsoft)
987f4a2713aSLionel Sambuc* ``__is_abstract`` (GNU, Microsoft)
988f4a2713aSLionel Sambuc* ``__is_base_of`` (GNU, Microsoft)
989f4a2713aSLionel Sambuc* ``__is_class`` (GNU, Microsoft)
990f4a2713aSLionel Sambuc* ``__is_convertible_to`` (Microsoft)
991f4a2713aSLionel Sambuc* ``__is_empty`` (GNU, Microsoft)
992f4a2713aSLionel Sambuc* ``__is_enum`` (GNU, Microsoft)
993f4a2713aSLionel Sambuc* ``__is_interface_class`` (Microsoft)
994f4a2713aSLionel Sambuc* ``__is_pod`` (GNU, Microsoft)
995f4a2713aSLionel Sambuc* ``__is_polymorphic`` (GNU, Microsoft)
996f4a2713aSLionel Sambuc* ``__is_union`` (GNU, Microsoft)
997f4a2713aSLionel Sambuc* ``__is_literal(type)``: Determines whether the given type is a literal type
998f4a2713aSLionel Sambuc* ``__is_final``: Determines whether the given type is declared with a
999f4a2713aSLionel Sambuc  ``final`` class-virt-specifier.
1000f4a2713aSLionel Sambuc* ``__underlying_type(type)``: Retrieves the underlying type for a given
1001f4a2713aSLionel Sambuc  ``enum`` type.  This trait is required to implement the C++11 standard
1002f4a2713aSLionel Sambuc  library.
1003f4a2713aSLionel Sambuc* ``__is_trivially_assignable(totype, fromtype)``: Determines whether a value
1004f4a2713aSLionel Sambuc  of type ``totype`` can be assigned to from a value of type ``fromtype`` such
1005f4a2713aSLionel Sambuc  that no non-trivial functions are called as part of that assignment.  This
1006f4a2713aSLionel Sambuc  trait is required to implement the C++11 standard library.
1007f4a2713aSLionel Sambuc* ``__is_trivially_constructible(type, argtypes...)``: Determines whether a
1008f4a2713aSLionel Sambuc  value of type ``type`` can be direct-initialized with arguments of types
1009f4a2713aSLionel Sambuc  ``argtypes...`` such that no non-trivial functions are called as part of
1010f4a2713aSLionel Sambuc  that initialization.  This trait is required to implement the C++11 standard
1011f4a2713aSLionel Sambuc  library.
1012*0a6a1f1dSLionel Sambuc* ``__is_destructible`` (MSVC 2013): partially implemented
1013*0a6a1f1dSLionel Sambuc* ``__is_nothrow_destructible`` (MSVC 2013): partially implemented
1014*0a6a1f1dSLionel Sambuc* ``__is_nothrow_assignable`` (MSVC 2013, clang)
1015*0a6a1f1dSLionel Sambuc* ``__is_constructible`` (MSVC 2013, clang)
1016*0a6a1f1dSLionel Sambuc* ``__is_nothrow_constructible`` (MSVC 2013, clang)
1017f4a2713aSLionel Sambuc
1018f4a2713aSLionel SambucBlocks
1019f4a2713aSLionel Sambuc======
1020f4a2713aSLionel Sambuc
1021f4a2713aSLionel SambucThe syntax and high level language feature description is in
1022f4a2713aSLionel Sambuc:doc:`BlockLanguageSpec<BlockLanguageSpec>`. Implementation and ABI details for
1023f4a2713aSLionel Sambucthe clang implementation are in :doc:`Block-ABI-Apple<Block-ABI-Apple>`.
1024f4a2713aSLionel Sambuc
1025f4a2713aSLionel SambucQuery for this feature with ``__has_extension(blocks)``.
1026f4a2713aSLionel Sambuc
1027f4a2713aSLionel SambucObjective-C Features
1028f4a2713aSLionel Sambuc====================
1029f4a2713aSLionel Sambuc
1030f4a2713aSLionel SambucRelated result types
1031f4a2713aSLionel Sambuc--------------------
1032f4a2713aSLionel Sambuc
1033f4a2713aSLionel SambucAccording to Cocoa conventions, Objective-C methods with certain names
1034f4a2713aSLionel Sambuc("``init``", "``alloc``", etc.) always return objects that are an instance of
1035f4a2713aSLionel Sambucthe receiving class's type.  Such methods are said to have a "related result
1036f4a2713aSLionel Sambuctype", meaning that a message send to one of these methods will have the same
1037f4a2713aSLionel Sambucstatic type as an instance of the receiver class.  For example, given the
1038f4a2713aSLionel Sambucfollowing classes:
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc.. code-block:: objc
1041f4a2713aSLionel Sambuc
1042f4a2713aSLionel Sambuc  @interface NSObject
1043f4a2713aSLionel Sambuc  + (id)alloc;
1044f4a2713aSLionel Sambuc  - (id)init;
1045f4a2713aSLionel Sambuc  @end
1046f4a2713aSLionel Sambuc
1047f4a2713aSLionel Sambuc  @interface NSArray : NSObject
1048f4a2713aSLionel Sambuc  @end
1049f4a2713aSLionel Sambuc
1050f4a2713aSLionel Sambucand this common initialization pattern
1051f4a2713aSLionel Sambuc
1052f4a2713aSLionel Sambuc.. code-block:: objc
1053f4a2713aSLionel Sambuc
1054f4a2713aSLionel Sambuc  NSArray *array = [[NSArray alloc] init];
1055f4a2713aSLionel Sambuc
1056f4a2713aSLionel Sambucthe type of the expression ``[NSArray alloc]`` is ``NSArray*`` because
1057f4a2713aSLionel Sambuc``alloc`` implicitly has a related result type.  Similarly, the type of the
1058f4a2713aSLionel Sambucexpression ``[[NSArray alloc] init]`` is ``NSArray*``, since ``init`` has a
1059f4a2713aSLionel Sambucrelated result type and its receiver is known to have the type ``NSArray *``.
1060f4a2713aSLionel SambucIf neither ``alloc`` nor ``init`` had a related result type, the expressions
1061f4a2713aSLionel Sambucwould have had type ``id``, as declared in the method signature.
1062f4a2713aSLionel Sambuc
1063f4a2713aSLionel SambucA method with a related result type can be declared by using the type
1064f4a2713aSLionel Sambuc``instancetype`` as its result type.  ``instancetype`` is a contextual keyword
1065f4a2713aSLionel Sambucthat is only permitted in the result type of an Objective-C method, e.g.
1066f4a2713aSLionel Sambuc
1067f4a2713aSLionel Sambuc.. code-block:: objc
1068f4a2713aSLionel Sambuc
1069f4a2713aSLionel Sambuc  @interface A
1070f4a2713aSLionel Sambuc  + (instancetype)constructAnA;
1071f4a2713aSLionel Sambuc  @end
1072f4a2713aSLionel Sambuc
1073f4a2713aSLionel SambucThe related result type can also be inferred for some methods.  To determine
1074f4a2713aSLionel Sambucwhether a method has an inferred related result type, the first word in the
1075f4a2713aSLionel Sambuccamel-case selector (e.g., "``init``" in "``initWithObjects``") is considered,
1076f4a2713aSLionel Sambucand the method will have a related result type if its return type is compatible
1077f4a2713aSLionel Sambucwith the type of its class and if:
1078f4a2713aSLionel Sambuc
1079f4a2713aSLionel Sambuc* the first word is "``alloc``" or "``new``", and the method is a class method,
1080f4a2713aSLionel Sambuc  or
1081f4a2713aSLionel Sambuc
1082f4a2713aSLionel Sambuc* the first word is "``autorelease``", "``init``", "``retain``", or "``self``",
1083f4a2713aSLionel Sambuc  and the method is an instance method.
1084f4a2713aSLionel Sambuc
1085f4a2713aSLionel SambucIf a method with a related result type is overridden by a subclass method, the
1086f4a2713aSLionel Sambucsubclass method must also return a type that is compatible with the subclass
1087f4a2713aSLionel Sambuctype.  For example:
1088f4a2713aSLionel Sambuc
1089f4a2713aSLionel Sambuc.. code-block:: objc
1090f4a2713aSLionel Sambuc
1091f4a2713aSLionel Sambuc  @interface NSString : NSObject
1092f4a2713aSLionel Sambuc  - (NSUnrelated *)init; // incorrect usage: NSUnrelated is not NSString or a superclass of NSString
1093f4a2713aSLionel Sambuc  @end
1094f4a2713aSLionel Sambuc
1095f4a2713aSLionel SambucRelated result types only affect the type of a message send or property access
1096f4a2713aSLionel Sambucvia the given method.  In all other respects, a method with a related result
1097f4a2713aSLionel Sambuctype is treated the same way as method that returns ``id``.
1098f4a2713aSLionel Sambuc
1099f4a2713aSLionel SambucUse ``__has_feature(objc_instancetype)`` to determine whether the
1100f4a2713aSLionel Sambuc``instancetype`` contextual keyword is available.
1101f4a2713aSLionel Sambuc
1102f4a2713aSLionel SambucAutomatic reference counting
1103f4a2713aSLionel Sambuc----------------------------
1104f4a2713aSLionel Sambuc
1105f4a2713aSLionel SambucClang provides support for :doc:`automated reference counting
1106f4a2713aSLionel Sambuc<AutomaticReferenceCounting>` in Objective-C, which eliminates the need
1107f4a2713aSLionel Sambucfor manual ``retain``/``release``/``autorelease`` message sends.  There are two
1108f4a2713aSLionel Sambucfeature macros associated with automatic reference counting:
1109f4a2713aSLionel Sambuc``__has_feature(objc_arc)`` indicates the availability of automated reference
1110f4a2713aSLionel Sambuccounting in general, while ``__has_feature(objc_arc_weak)`` indicates that
1111f4a2713aSLionel Sambucautomated reference counting also includes support for ``__weak`` pointers to
1112f4a2713aSLionel SambucObjective-C objects.
1113f4a2713aSLionel Sambuc
1114f4a2713aSLionel Sambuc.. _objc-fixed-enum:
1115f4a2713aSLionel Sambuc
1116f4a2713aSLionel SambucEnumerations with a fixed underlying type
1117f4a2713aSLionel Sambuc-----------------------------------------
1118f4a2713aSLionel Sambuc
1119f4a2713aSLionel SambucClang provides support for C++11 enumerations with a fixed underlying type
1120f4a2713aSLionel Sambucwithin Objective-C.  For example, one can write an enumeration type as:
1121f4a2713aSLionel Sambuc
1122f4a2713aSLionel Sambuc.. code-block:: c++
1123f4a2713aSLionel Sambuc
1124f4a2713aSLionel Sambuc  typedef enum : unsigned char { Red, Green, Blue } Color;
1125f4a2713aSLionel Sambuc
1126f4a2713aSLionel SambucThis specifies that the underlying type, which is used to store the enumeration
1127f4a2713aSLionel Sambucvalue, is ``unsigned char``.
1128f4a2713aSLionel Sambuc
1129f4a2713aSLionel SambucUse ``__has_feature(objc_fixed_enum)`` to determine whether support for fixed
1130f4a2713aSLionel Sambucunderlying types is available in Objective-C.
1131f4a2713aSLionel Sambuc
1132f4a2713aSLionel SambucInteroperability with C++11 lambdas
1133f4a2713aSLionel Sambuc-----------------------------------
1134f4a2713aSLionel Sambuc
1135f4a2713aSLionel SambucClang provides interoperability between C++11 lambdas and blocks-based APIs, by
1136f4a2713aSLionel Sambucpermitting a lambda to be implicitly converted to a block pointer with the
1137f4a2713aSLionel Sambuccorresponding signature.  For example, consider an API such as ``NSArray``'s
1138f4a2713aSLionel Sambucarray-sorting method:
1139f4a2713aSLionel Sambuc
1140f4a2713aSLionel Sambuc.. code-block:: objc
1141f4a2713aSLionel Sambuc
1142f4a2713aSLionel Sambuc  - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr;
1143f4a2713aSLionel Sambuc
1144f4a2713aSLionel Sambuc``NSComparator`` is simply a typedef for the block pointer ``NSComparisonResult
1145f4a2713aSLionel Sambuc(^)(id, id)``, and parameters of this type are generally provided with block
1146f4a2713aSLionel Sambucliterals as arguments.  However, one can also use a C++11 lambda so long as it
1147f4a2713aSLionel Sambucprovides the same signature (in this case, accepting two parameters of type
1148f4a2713aSLionel Sambuc``id`` and returning an ``NSComparisonResult``):
1149f4a2713aSLionel Sambuc
1150f4a2713aSLionel Sambuc.. code-block:: objc
1151f4a2713aSLionel Sambuc
1152f4a2713aSLionel Sambuc  NSArray *array = @[@"string 1", @"string 21", @"string 12", @"String 11",
1153f4a2713aSLionel Sambuc                     @"String 02"];
1154f4a2713aSLionel Sambuc  const NSStringCompareOptions comparisonOptions
1155f4a2713aSLionel Sambuc    = NSCaseInsensitiveSearch | NSNumericSearch |
1156f4a2713aSLionel Sambuc      NSWidthInsensitiveSearch | NSForcedOrderingSearch;
1157f4a2713aSLionel Sambuc  NSLocale *currentLocale = [NSLocale currentLocale];
1158f4a2713aSLionel Sambuc  NSArray *sorted
1159f4a2713aSLionel Sambuc    = [array sortedArrayUsingComparator:[=](id s1, id s2) -> NSComparisonResult {
1160f4a2713aSLionel Sambuc               NSRange string1Range = NSMakeRange(0, [s1 length]);
1161f4a2713aSLionel Sambuc               return [s1 compare:s2 options:comparisonOptions
1162f4a2713aSLionel Sambuc               range:string1Range locale:currentLocale];
1163f4a2713aSLionel Sambuc       }];
1164f4a2713aSLionel Sambuc  NSLog(@"sorted: %@", sorted);
1165f4a2713aSLionel Sambuc
1166f4a2713aSLionel SambucThis code relies on an implicit conversion from the type of the lambda
1167f4a2713aSLionel Sambucexpression (an unnamed, local class type called the *closure type*) to the
1168f4a2713aSLionel Sambuccorresponding block pointer type.  The conversion itself is expressed by a
1169f4a2713aSLionel Sambucconversion operator in that closure type that produces a block pointer with the
1170f4a2713aSLionel Sambucsame signature as the lambda itself, e.g.,
1171f4a2713aSLionel Sambuc
1172f4a2713aSLionel Sambuc.. code-block:: objc
1173f4a2713aSLionel Sambuc
1174f4a2713aSLionel Sambuc  operator NSComparisonResult (^)(id, id)() const;
1175f4a2713aSLionel Sambuc
1176f4a2713aSLionel SambucThis conversion function returns a new block that simply forwards the two
1177f4a2713aSLionel Sambucparameters to the lambda object (which it captures by copy), then returns the
1178f4a2713aSLionel Sambucresult.  The returned block is first copied (with ``Block_copy``) and then
1179f4a2713aSLionel Sambucautoreleased.  As an optimization, if a lambda expression is immediately
1180f4a2713aSLionel Sambucconverted to a block pointer (as in the first example, above), then the block
1181f4a2713aSLionel Sambucis not copied and autoreleased: rather, it is given the same lifetime as a
1182f4a2713aSLionel Sambucblock literal written at that point in the program, which avoids the overhead
1183f4a2713aSLionel Sambucof copying a block to the heap in the common case.
1184f4a2713aSLionel Sambuc
1185f4a2713aSLionel SambucThe conversion from a lambda to a block pointer is only available in
1186f4a2713aSLionel SambucObjective-C++, and not in C++ with blocks, due to its use of Objective-C memory
1187f4a2713aSLionel Sambucmanagement (autorelease).
1188f4a2713aSLionel Sambuc
1189f4a2713aSLionel SambucObject Literals and Subscripting
1190f4a2713aSLionel Sambuc--------------------------------
1191f4a2713aSLionel Sambuc
1192f4a2713aSLionel SambucClang provides support for :doc:`Object Literals and Subscripting
1193f4a2713aSLionel Sambuc<ObjectiveCLiterals>` in Objective-C, which simplifies common Objective-C
1194f4a2713aSLionel Sambucprogramming patterns, makes programs more concise, and improves the safety of
1195f4a2713aSLionel Sambuccontainer creation.  There are several feature macros associated with object
1196f4a2713aSLionel Sambucliterals and subscripting: ``__has_feature(objc_array_literals)`` tests the
1197f4a2713aSLionel Sambucavailability of array literals; ``__has_feature(objc_dictionary_literals)``
1198f4a2713aSLionel Sambuctests the availability of dictionary literals;
1199f4a2713aSLionel Sambuc``__has_feature(objc_subscripting)`` tests the availability of object
1200f4a2713aSLionel Sambucsubscripting.
1201f4a2713aSLionel Sambuc
1202f4a2713aSLionel SambucObjective-C Autosynthesis of Properties
1203f4a2713aSLionel Sambuc---------------------------------------
1204f4a2713aSLionel Sambuc
1205f4a2713aSLionel SambucClang provides support for autosynthesis of declared properties.  Using this
1206f4a2713aSLionel Sambucfeature, clang provides default synthesis of those properties not declared
1207f4a2713aSLionel Sambuc@dynamic and not having user provided backing getter and setter methods.
1208f4a2713aSLionel Sambuc``__has_feature(objc_default_synthesize_properties)`` checks for availability
1209f4a2713aSLionel Sambucof this feature in version of clang being used.
1210f4a2713aSLionel Sambuc
1211f4a2713aSLionel Sambuc.. _langext-objc-retain-release:
1212f4a2713aSLionel Sambuc
1213f4a2713aSLionel SambucObjective-C retaining behavior attributes
1214f4a2713aSLionel Sambuc-----------------------------------------
1215f4a2713aSLionel Sambuc
1216f4a2713aSLionel SambucIn Objective-C, functions and methods are generally assumed to follow the
1217f4a2713aSLionel Sambuc`Cocoa Memory Management
1218f4a2713aSLionel Sambuc<http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html>`_
1219f4a2713aSLionel Sambucconventions for ownership of object arguments and
1220f4a2713aSLionel Sambucreturn values. However, there are exceptions, and so Clang provides attributes
1221f4a2713aSLionel Sambucto allow these exceptions to be documented. This are used by ARC and the
1222f4a2713aSLionel Sambuc`static analyzer <http://clang-analyzer.llvm.org>`_ Some exceptions may be
1223*0a6a1f1dSLionel Sambucbetter described using the ``objc_method_family`` attribute instead.
1224f4a2713aSLionel Sambuc
1225f4a2713aSLionel Sambuc**Usage**: The ``ns_returns_retained``, ``ns_returns_not_retained``,
1226f4a2713aSLionel Sambuc``ns_returns_autoreleased``, ``cf_returns_retained``, and
1227f4a2713aSLionel Sambuc``cf_returns_not_retained`` attributes can be placed on methods and functions
1228f4a2713aSLionel Sambucthat return Objective-C or CoreFoundation objects. They are commonly placed at
1229f4a2713aSLionel Sambucthe end of a function prototype or method declaration:
1230f4a2713aSLionel Sambuc
1231f4a2713aSLionel Sambuc.. code-block:: objc
1232f4a2713aSLionel Sambuc
1233f4a2713aSLionel Sambuc  id foo() __attribute__((ns_returns_retained));
1234f4a2713aSLionel Sambuc
1235f4a2713aSLionel Sambuc  - (NSString *)bar:(int)x __attribute__((ns_returns_retained));
1236f4a2713aSLionel Sambuc
1237f4a2713aSLionel SambucThe ``*_returns_retained`` attributes specify that the returned object has a +1
1238f4a2713aSLionel Sambucretain count.  The ``*_returns_not_retained`` attributes specify that the return
1239f4a2713aSLionel Sambucobject has a +0 retain count, even if the normal convention for its selector
1240f4a2713aSLionel Sambucwould be +1.  ``ns_returns_autoreleased`` specifies that the returned object is
1241f4a2713aSLionel Sambuc+0, but is guaranteed to live at least as long as the next flush of an
1242f4a2713aSLionel Sambucautorelease pool.
1243f4a2713aSLionel Sambuc
1244f4a2713aSLionel Sambuc**Usage**: The ``ns_consumed`` and ``cf_consumed`` attributes can be placed on
1245f4a2713aSLionel Sambucan parameter declaration; they specify that the argument is expected to have a
1246f4a2713aSLionel Sambuc+1 retain count, which will be balanced in some way by the function or method.
1247f4a2713aSLionel SambucThe ``ns_consumes_self`` attribute can only be placed on an Objective-C
1248f4a2713aSLionel Sambucmethod; it specifies that the method expects its ``self`` parameter to have a
1249f4a2713aSLionel Sambuc+1 retain count, which it will balance in some way.
1250f4a2713aSLionel Sambuc
1251f4a2713aSLionel Sambuc.. code-block:: objc
1252f4a2713aSLionel Sambuc
1253f4a2713aSLionel Sambuc  void foo(__attribute__((ns_consumed)) NSString *string);
1254f4a2713aSLionel Sambuc
1255f4a2713aSLionel Sambuc  - (void) bar __attribute__((ns_consumes_self));
1256f4a2713aSLionel Sambuc  - (void) baz:(id) __attribute__((ns_consumed)) x;
1257f4a2713aSLionel Sambuc
1258f4a2713aSLionel SambucFurther examples of these attributes are available in the static analyzer's `list of annotations for analysis
1259f4a2713aSLionel Sambuc<http://clang-analyzer.llvm.org/annotations.html#cocoa_mem>`_.
1260f4a2713aSLionel Sambuc
1261f4a2713aSLionel SambucQuery for these features with ``__has_attribute(ns_consumed)``,
1262f4a2713aSLionel Sambuc``__has_attribute(ns_returns_retained)``, etc.
1263f4a2713aSLionel Sambuc
1264f4a2713aSLionel Sambuc
1265f4a2713aSLionel SambucObjective-C++ ABI: protocol-qualifier mangling of parameters
1266f4a2713aSLionel Sambuc------------------------------------------------------------
1267f4a2713aSLionel Sambuc
1268f4a2713aSLionel SambucStarting with LLVM 3.4, Clang produces a new mangling for parameters whose
1269f4a2713aSLionel Sambuctype is a qualified-``id`` (e.g., ``id<Foo>``).  This mangling allows such
1270f4a2713aSLionel Sambucparameters to be differentiated from those with the regular unqualified ``id``
1271f4a2713aSLionel Sambuctype.
1272f4a2713aSLionel Sambuc
1273f4a2713aSLionel SambucThis was a non-backward compatible mangling change to the ABI.  This change
1274f4a2713aSLionel Sambucallows proper overloading, and also prevents mangling conflicts with template
1275f4a2713aSLionel Sambucparameters of protocol-qualified type.
1276f4a2713aSLionel Sambuc
1277f4a2713aSLionel SambucQuery the presence of this new mangling with
1278f4a2713aSLionel Sambuc``__has_feature(objc_protocol_qualifier_mangling)``.
1279f4a2713aSLionel Sambuc
1280*0a6a1f1dSLionel Sambuc.. _langext-overloading:
1281f4a2713aSLionel Sambuc
1282f4a2713aSLionel SambucInitializer lists for complex numbers in C
1283f4a2713aSLionel Sambuc==========================================
1284f4a2713aSLionel Sambuc
1285f4a2713aSLionel Sambucclang supports an extension which allows the following in C:
1286f4a2713aSLionel Sambuc
1287f4a2713aSLionel Sambuc.. code-block:: c++
1288f4a2713aSLionel Sambuc
1289f4a2713aSLionel Sambuc  #include <math.h>
1290f4a2713aSLionel Sambuc  #include <complex.h>
1291f4a2713aSLionel Sambuc  complex float x = { 1.0f, INFINITY }; // Init to (1, Inf)
1292f4a2713aSLionel Sambuc
1293f4a2713aSLionel SambucThis construct is useful because there is no way to separately initialize the
1294f4a2713aSLionel Sambucreal and imaginary parts of a complex variable in standard C, given that clang
1295f4a2713aSLionel Sambucdoes not support ``_Imaginary``.  (Clang also supports the ``__real__`` and
1296f4a2713aSLionel Sambuc``__imag__`` extensions from gcc, which help in some cases, but are not usable
1297f4a2713aSLionel Sambucin static initializers.)
1298f4a2713aSLionel Sambuc
1299f4a2713aSLionel SambucNote that this extension does not allow eliding the braces; the meaning of the
1300f4a2713aSLionel Sambucfollowing two lines is different:
1301f4a2713aSLionel Sambuc
1302f4a2713aSLionel Sambuc.. code-block:: c++
1303f4a2713aSLionel Sambuc
1304f4a2713aSLionel Sambuc  complex float x[] = { { 1.0f, 1.0f } }; // [0] = (1, 1)
1305f4a2713aSLionel Sambuc  complex float x[] = { 1.0f, 1.0f }; // [0] = (1, 0), [1] = (1, 0)
1306f4a2713aSLionel Sambuc
1307f4a2713aSLionel SambucThis extension also works in C++ mode, as far as that goes, but does not apply
1308f4a2713aSLionel Sambucto the C++ ``std::complex``.  (In C++11, list initialization allows the same
1309f4a2713aSLionel Sambucsyntax to be used with ``std::complex`` with the same meaning.)
1310f4a2713aSLionel Sambuc
1311f4a2713aSLionel SambucBuiltin Functions
1312f4a2713aSLionel Sambuc=================
1313f4a2713aSLionel Sambuc
1314f4a2713aSLionel SambucClang supports a number of builtin library functions with the same syntax as
1315f4a2713aSLionel SambucGCC, including things like ``__builtin_nan``, ``__builtin_constant_p``,
1316f4a2713aSLionel Sambuc``__builtin_choose_expr``, ``__builtin_types_compatible_p``,
1317*0a6a1f1dSLionel Sambuc``__builtin_assume_aligned``, ``__sync_fetch_and_add``, etc.  In addition to
1318*0a6a1f1dSLionel Sambucthe GCC builtins, Clang supports a number of builtins that GCC does not, which
1319*0a6a1f1dSLionel Sambucare listed here.
1320f4a2713aSLionel Sambuc
1321f4a2713aSLionel SambucPlease note that Clang does not and will not support all of the GCC builtins
1322f4a2713aSLionel Sambucfor vector operations.  Instead of using builtins, you should use the functions
1323f4a2713aSLionel Sambucdefined in target-specific header files like ``<xmmintrin.h>``, which define
1324f4a2713aSLionel Sambucportable wrappers for these.  Many of the Clang versions of these functions are
1325f4a2713aSLionel Sambucimplemented directly in terms of :ref:`extended vector support
1326f4a2713aSLionel Sambuc<langext-vectors>` instead of builtins, in order to reduce the number of
1327f4a2713aSLionel Sambucbuiltins that we need to implement.
1328f4a2713aSLionel Sambuc
1329*0a6a1f1dSLionel Sambuc``__builtin_assume``
1330*0a6a1f1dSLionel Sambuc------------------------------
1331*0a6a1f1dSLionel Sambuc
1332*0a6a1f1dSLionel Sambuc``__builtin_assume`` is used to provide the optimizer with a boolean
1333*0a6a1f1dSLionel Sambucinvariant that is defined to be true.
1334*0a6a1f1dSLionel Sambuc
1335*0a6a1f1dSLionel Sambuc**Syntax**:
1336*0a6a1f1dSLionel Sambuc
1337*0a6a1f1dSLionel Sambuc.. code-block:: c++
1338*0a6a1f1dSLionel Sambuc
1339*0a6a1f1dSLionel Sambuc  __builtin_assume(bool)
1340*0a6a1f1dSLionel Sambuc
1341*0a6a1f1dSLionel Sambuc**Example of Use**:
1342*0a6a1f1dSLionel Sambuc
1343*0a6a1f1dSLionel Sambuc.. code-block:: c++
1344*0a6a1f1dSLionel Sambuc
1345*0a6a1f1dSLionel Sambuc  int foo(int x) {
1346*0a6a1f1dSLionel Sambuc    __builtin_assume(x != 0);
1347*0a6a1f1dSLionel Sambuc
1348*0a6a1f1dSLionel Sambuc    // The optimizer may short-circuit this check using the invariant.
1349*0a6a1f1dSLionel Sambuc    if (x == 0)
1350*0a6a1f1dSLionel Sambuc      return do_something();
1351*0a6a1f1dSLionel Sambuc
1352*0a6a1f1dSLionel Sambuc    return do_something_else();
1353*0a6a1f1dSLionel Sambuc  }
1354*0a6a1f1dSLionel Sambuc
1355*0a6a1f1dSLionel Sambuc**Description**:
1356*0a6a1f1dSLionel Sambuc
1357*0a6a1f1dSLionel SambucThe boolean argument to this function is defined to be true. The optimizer may
1358*0a6a1f1dSLionel Sambucanalyze the form of the expression provided as the argument and deduce from
1359*0a6a1f1dSLionel Sambucthat information used to optimize the program. If the condition is violated
1360*0a6a1f1dSLionel Sambucduring execution, the behavior is undefined. The argument itself is never
1361*0a6a1f1dSLionel Sambucevaluated, so any side effects of the expression will be discarded.
1362*0a6a1f1dSLionel Sambuc
1363*0a6a1f1dSLionel SambucQuery for this feature with ``__has_builtin(__builtin_assume)``.
1364*0a6a1f1dSLionel Sambuc
1365f4a2713aSLionel Sambuc``__builtin_readcyclecounter``
1366f4a2713aSLionel Sambuc------------------------------
1367f4a2713aSLionel Sambuc
1368f4a2713aSLionel Sambuc``__builtin_readcyclecounter`` is used to access the cycle counter register (or
1369f4a2713aSLionel Sambuca similar low-latency, high-accuracy clock) on those targets that support it.
1370f4a2713aSLionel Sambuc
1371f4a2713aSLionel Sambuc**Syntax**:
1372f4a2713aSLionel Sambuc
1373f4a2713aSLionel Sambuc.. code-block:: c++
1374f4a2713aSLionel Sambuc
1375f4a2713aSLionel Sambuc  __builtin_readcyclecounter()
1376f4a2713aSLionel Sambuc
1377f4a2713aSLionel Sambuc**Example of Use**:
1378f4a2713aSLionel Sambuc
1379f4a2713aSLionel Sambuc.. code-block:: c++
1380f4a2713aSLionel Sambuc
1381f4a2713aSLionel Sambuc  unsigned long long t0 = __builtin_readcyclecounter();
1382f4a2713aSLionel Sambuc  do_something();
1383f4a2713aSLionel Sambuc  unsigned long long t1 = __builtin_readcyclecounter();
1384f4a2713aSLionel Sambuc  unsigned long long cycles_to_do_something = t1 - t0; // assuming no overflow
1385f4a2713aSLionel Sambuc
1386f4a2713aSLionel Sambuc**Description**:
1387f4a2713aSLionel Sambuc
1388f4a2713aSLionel SambucThe ``__builtin_readcyclecounter()`` builtin returns the cycle counter value,
1389f4a2713aSLionel Sambucwhich may be either global or process/thread-specific depending on the target.
1390f4a2713aSLionel SambucAs the backing counters often overflow quickly (on the order of seconds) this
1391f4a2713aSLionel Sambucshould only be used for timing small intervals.  When not supported by the
1392f4a2713aSLionel Sambuctarget, the return value is always zero.  This builtin takes no arguments and
1393f4a2713aSLionel Sambucproduces an unsigned long long result.
1394f4a2713aSLionel Sambuc
1395f4a2713aSLionel SambucQuery for this feature with ``__has_builtin(__builtin_readcyclecounter)``. Note
1396f4a2713aSLionel Sambucthat even if present, its use may depend on run-time privilege or other OS
1397f4a2713aSLionel Sambuccontrolled state.
1398f4a2713aSLionel Sambuc
1399f4a2713aSLionel Sambuc.. _langext-__builtin_shufflevector:
1400f4a2713aSLionel Sambuc
1401f4a2713aSLionel Sambuc``__builtin_shufflevector``
1402f4a2713aSLionel Sambuc---------------------------
1403f4a2713aSLionel Sambuc
1404f4a2713aSLionel Sambuc``__builtin_shufflevector`` is used to express generic vector
1405f4a2713aSLionel Sambucpermutation/shuffle/swizzle operations.  This builtin is also very important
1406f4a2713aSLionel Sambucfor the implementation of various target-specific header files like
1407f4a2713aSLionel Sambuc``<xmmintrin.h>``.
1408f4a2713aSLionel Sambuc
1409f4a2713aSLionel Sambuc**Syntax**:
1410f4a2713aSLionel Sambuc
1411f4a2713aSLionel Sambuc.. code-block:: c++
1412f4a2713aSLionel Sambuc
1413f4a2713aSLionel Sambuc  __builtin_shufflevector(vec1, vec2, index1, index2, ...)
1414f4a2713aSLionel Sambuc
1415f4a2713aSLionel Sambuc**Examples**:
1416f4a2713aSLionel Sambuc
1417f4a2713aSLionel Sambuc.. code-block:: c++
1418f4a2713aSLionel Sambuc
1419f4a2713aSLionel Sambuc  // identity operation - return 4-element vector v1.
1420f4a2713aSLionel Sambuc  __builtin_shufflevector(v1, v1, 0, 1, 2, 3)
1421f4a2713aSLionel Sambuc
1422f4a2713aSLionel Sambuc  // "Splat" element 0 of V1 into a 4-element result.
1423f4a2713aSLionel Sambuc  __builtin_shufflevector(V1, V1, 0, 0, 0, 0)
1424f4a2713aSLionel Sambuc
1425f4a2713aSLionel Sambuc  // Reverse 4-element vector V1.
1426f4a2713aSLionel Sambuc  __builtin_shufflevector(V1, V1, 3, 2, 1, 0)
1427f4a2713aSLionel Sambuc
1428f4a2713aSLionel Sambuc  // Concatenate every other element of 4-element vectors V1 and V2.
1429f4a2713aSLionel Sambuc  __builtin_shufflevector(V1, V2, 0, 2, 4, 6)
1430f4a2713aSLionel Sambuc
1431f4a2713aSLionel Sambuc  // Concatenate every other element of 8-element vectors V1 and V2.
1432f4a2713aSLionel Sambuc  __builtin_shufflevector(V1, V2, 0, 2, 4, 6, 8, 10, 12, 14)
1433f4a2713aSLionel Sambuc
1434f4a2713aSLionel Sambuc  // Shuffle v1 with some elements being undefined
1435f4a2713aSLionel Sambuc  __builtin_shufflevector(v1, v1, 3, -1, 1, -1)
1436f4a2713aSLionel Sambuc
1437f4a2713aSLionel Sambuc**Description**:
1438f4a2713aSLionel Sambuc
1439f4a2713aSLionel SambucThe first two arguments to ``__builtin_shufflevector`` are vectors that have
1440f4a2713aSLionel Sambucthe same element type.  The remaining arguments are a list of integers that
1441f4a2713aSLionel Sambucspecify the elements indices of the first two vectors that should be extracted
1442f4a2713aSLionel Sambucand returned in a new vector.  These element indices are numbered sequentially
1443f4a2713aSLionel Sambucstarting with the first vector, continuing into the second vector.  Thus, if
1444f4a2713aSLionel Sambuc``vec1`` is a 4-element vector, index 5 would refer to the second element of
1445f4a2713aSLionel Sambuc``vec2``. An index of -1 can be used to indicate that the corresponding element
1446f4a2713aSLionel Sambucin the returned vector is a don't care and can be optimized by the backend.
1447f4a2713aSLionel Sambuc
1448f4a2713aSLionel SambucThe result of ``__builtin_shufflevector`` is a vector with the same element
1449f4a2713aSLionel Sambuctype as ``vec1``/``vec2`` but that has an element count equal to the number of
1450f4a2713aSLionel Sambucindices specified.
1451f4a2713aSLionel Sambuc
1452f4a2713aSLionel SambucQuery for this feature with ``__has_builtin(__builtin_shufflevector)``.
1453f4a2713aSLionel Sambuc
1454*0a6a1f1dSLionel Sambuc.. _langext-__builtin_convertvector:
1455*0a6a1f1dSLionel Sambuc
1456f4a2713aSLionel Sambuc``__builtin_convertvector``
1457f4a2713aSLionel Sambuc---------------------------
1458f4a2713aSLionel Sambuc
1459f4a2713aSLionel Sambuc``__builtin_convertvector`` is used to express generic vector
1460f4a2713aSLionel Sambuctype-conversion operations. The input vector and the output vector
1461f4a2713aSLionel Sambuctype must have the same number of elements.
1462f4a2713aSLionel Sambuc
1463f4a2713aSLionel Sambuc**Syntax**:
1464f4a2713aSLionel Sambuc
1465f4a2713aSLionel Sambuc.. code-block:: c++
1466f4a2713aSLionel Sambuc
1467f4a2713aSLionel Sambuc  __builtin_convertvector(src_vec, dst_vec_type)
1468f4a2713aSLionel Sambuc
1469f4a2713aSLionel Sambuc**Examples**:
1470f4a2713aSLionel Sambuc
1471f4a2713aSLionel Sambuc.. code-block:: c++
1472f4a2713aSLionel Sambuc
1473f4a2713aSLionel Sambuc  typedef double vector4double __attribute__((__vector_size__(32)));
1474f4a2713aSLionel Sambuc  typedef float  vector4float  __attribute__((__vector_size__(16)));
1475f4a2713aSLionel Sambuc  typedef short  vector4short  __attribute__((__vector_size__(8)));
1476f4a2713aSLionel Sambuc  vector4float vf; vector4short vs;
1477f4a2713aSLionel Sambuc
1478f4a2713aSLionel Sambuc  // convert from a vector of 4 floats to a vector of 4 doubles.
1479f4a2713aSLionel Sambuc  __builtin_convertvector(vf, vector4double)
1480f4a2713aSLionel Sambuc  // equivalent to:
1481f4a2713aSLionel Sambuc  (vector4double) { (double) vf[0], (double) vf[1], (double) vf[2], (double) vf[3] }
1482f4a2713aSLionel Sambuc
1483f4a2713aSLionel Sambuc  // convert from a vector of 4 shorts to a vector of 4 floats.
1484f4a2713aSLionel Sambuc  __builtin_convertvector(vs, vector4float)
1485f4a2713aSLionel Sambuc  // equivalent to:
1486*0a6a1f1dSLionel Sambuc  (vector4float) { (float) vs[0], (float) vs[1], (float) vs[2], (float) vs[3] }
1487f4a2713aSLionel Sambuc
1488f4a2713aSLionel Sambuc**Description**:
1489f4a2713aSLionel Sambuc
1490f4a2713aSLionel SambucThe first argument to ``__builtin_convertvector`` is a vector, and the second
1491f4a2713aSLionel Sambucargument is a vector type with the same number of elements as the first
1492f4a2713aSLionel Sambucargument.
1493f4a2713aSLionel Sambuc
1494f4a2713aSLionel SambucThe result of ``__builtin_convertvector`` is a vector with the same element
1495f4a2713aSLionel Sambuctype as the second argument, with a value defined in terms of the action of a
1496f4a2713aSLionel SambucC-style cast applied to each element of the first argument.
1497f4a2713aSLionel Sambuc
1498f4a2713aSLionel SambucQuery for this feature with ``__has_builtin(__builtin_convertvector)``.
1499f4a2713aSLionel Sambuc
1500f4a2713aSLionel Sambuc``__builtin_unreachable``
1501f4a2713aSLionel Sambuc-------------------------
1502f4a2713aSLionel Sambuc
1503f4a2713aSLionel Sambuc``__builtin_unreachable`` is used to indicate that a specific point in the
1504f4a2713aSLionel Sambucprogram cannot be reached, even if the compiler might otherwise think it can.
1505f4a2713aSLionel SambucThis is useful to improve optimization and eliminates certain warnings.  For
1506f4a2713aSLionel Sambucexample, without the ``__builtin_unreachable`` in the example below, the
1507f4a2713aSLionel Sambuccompiler assumes that the inline asm can fall through and prints a "function
1508f4a2713aSLionel Sambucdeclared '``noreturn``' should not return" warning.
1509f4a2713aSLionel Sambuc
1510f4a2713aSLionel Sambuc**Syntax**:
1511f4a2713aSLionel Sambuc
1512f4a2713aSLionel Sambuc.. code-block:: c++
1513f4a2713aSLionel Sambuc
1514f4a2713aSLionel Sambuc    __builtin_unreachable()
1515f4a2713aSLionel Sambuc
1516f4a2713aSLionel Sambuc**Example of use**:
1517f4a2713aSLionel Sambuc
1518f4a2713aSLionel Sambuc.. code-block:: c++
1519f4a2713aSLionel Sambuc
1520f4a2713aSLionel Sambuc  void myabort(void) __attribute__((noreturn));
1521f4a2713aSLionel Sambuc  void myabort(void) {
1522f4a2713aSLionel Sambuc    asm("int3");
1523f4a2713aSLionel Sambuc    __builtin_unreachable();
1524f4a2713aSLionel Sambuc  }
1525f4a2713aSLionel Sambuc
1526f4a2713aSLionel Sambuc**Description**:
1527f4a2713aSLionel Sambuc
1528f4a2713aSLionel SambucThe ``__builtin_unreachable()`` builtin has completely undefined behavior.
1529f4a2713aSLionel SambucSince it has undefined behavior, it is a statement that it is never reached and
1530f4a2713aSLionel Sambucthe optimizer can take advantage of this to produce better code.  This builtin
1531f4a2713aSLionel Sambuctakes no arguments and produces a void result.
1532f4a2713aSLionel Sambuc
1533f4a2713aSLionel SambucQuery for this feature with ``__has_builtin(__builtin_unreachable)``.
1534f4a2713aSLionel Sambuc
1535f4a2713aSLionel Sambuc``__sync_swap``
1536f4a2713aSLionel Sambuc---------------
1537f4a2713aSLionel Sambuc
1538f4a2713aSLionel Sambuc``__sync_swap`` is used to atomically swap integers or pointers in memory.
1539f4a2713aSLionel Sambuc
1540f4a2713aSLionel Sambuc**Syntax**:
1541f4a2713aSLionel Sambuc
1542f4a2713aSLionel Sambuc.. code-block:: c++
1543f4a2713aSLionel Sambuc
1544f4a2713aSLionel Sambuc  type __sync_swap(type *ptr, type value, ...)
1545f4a2713aSLionel Sambuc
1546f4a2713aSLionel Sambuc**Example of Use**:
1547f4a2713aSLionel Sambuc
1548f4a2713aSLionel Sambuc.. code-block:: c++
1549f4a2713aSLionel Sambuc
1550f4a2713aSLionel Sambuc  int old_value = __sync_swap(&value, new_value);
1551f4a2713aSLionel Sambuc
1552f4a2713aSLionel Sambuc**Description**:
1553f4a2713aSLionel Sambuc
1554f4a2713aSLionel SambucThe ``__sync_swap()`` builtin extends the existing ``__sync_*()`` family of
1555f4a2713aSLionel Sambucatomic intrinsics to allow code to atomically swap the current value with the
1556f4a2713aSLionel Sambucnew value.  More importantly, it helps developers write more efficient and
1557f4a2713aSLionel Sambuccorrect code by avoiding expensive loops around
1558f4a2713aSLionel Sambuc``__sync_bool_compare_and_swap()`` or relying on the platform specific
1559f4a2713aSLionel Sambucimplementation details of ``__sync_lock_test_and_set()``.  The
1560f4a2713aSLionel Sambuc``__sync_swap()`` builtin is a full barrier.
1561f4a2713aSLionel Sambuc
1562f4a2713aSLionel Sambuc``__builtin_addressof``
1563f4a2713aSLionel Sambuc-----------------------
1564f4a2713aSLionel Sambuc
1565f4a2713aSLionel Sambuc``__builtin_addressof`` performs the functionality of the built-in ``&``
1566f4a2713aSLionel Sambucoperator, ignoring any ``operator&`` overload.  This is useful in constant
1567f4a2713aSLionel Sambucexpressions in C++11, where there is no other way to take the address of an
1568f4a2713aSLionel Sambucobject that overloads ``operator&``.
1569f4a2713aSLionel Sambuc
1570f4a2713aSLionel Sambuc**Example of use**:
1571f4a2713aSLionel Sambuc
1572f4a2713aSLionel Sambuc.. code-block:: c++
1573f4a2713aSLionel Sambuc
1574f4a2713aSLionel Sambuc  template<typename T> constexpr T *addressof(T &value) {
1575f4a2713aSLionel Sambuc    return __builtin_addressof(value);
1576f4a2713aSLionel Sambuc  }
1577f4a2713aSLionel Sambuc
1578*0a6a1f1dSLionel Sambuc``__builtin_operator_new`` and ``__builtin_operator_delete``
1579*0a6a1f1dSLionel Sambuc------------------------------------------------------------
1580*0a6a1f1dSLionel Sambuc
1581*0a6a1f1dSLionel Sambuc``__builtin_operator_new`` allocates memory just like a non-placement non-class
1582*0a6a1f1dSLionel Sambuc*new-expression*. This is exactly like directly calling the normal
1583*0a6a1f1dSLionel Sambucnon-placement ``::operator new``, except that it allows certain optimizations
1584*0a6a1f1dSLionel Sambucthat the C++ standard does not permit for a direct function call to
1585*0a6a1f1dSLionel Sambuc``::operator new`` (in particular, removing ``new`` / ``delete`` pairs and
1586*0a6a1f1dSLionel Sambucmerging allocations).
1587*0a6a1f1dSLionel Sambuc
1588*0a6a1f1dSLionel SambucLikewise, ``__builtin_operator_delete`` deallocates memory just like a
1589*0a6a1f1dSLionel Sambucnon-class *delete-expression*, and is exactly like directly calling the normal
1590*0a6a1f1dSLionel Sambuc``::operator delete``, except that it permits optimizations. Only the unsized
1591*0a6a1f1dSLionel Sambucform of ``__builtin_operator_delete`` is currently available.
1592*0a6a1f1dSLionel Sambuc
1593*0a6a1f1dSLionel SambucThese builtins are intended for use in the implementation of ``std::allocator``
1594*0a6a1f1dSLionel Sambucand other similar allocation libraries, and are only available in C++.
1595*0a6a1f1dSLionel Sambuc
1596f4a2713aSLionel SambucMultiprecision Arithmetic Builtins
1597f4a2713aSLionel Sambuc----------------------------------
1598f4a2713aSLionel Sambuc
1599f4a2713aSLionel SambucClang provides a set of builtins which expose multiprecision arithmetic in a
1600f4a2713aSLionel Sambucmanner amenable to C. They all have the following form:
1601f4a2713aSLionel Sambuc
1602f4a2713aSLionel Sambuc.. code-block:: c
1603f4a2713aSLionel Sambuc
1604f4a2713aSLionel Sambuc  unsigned x = ..., y = ..., carryin = ..., carryout;
1605f4a2713aSLionel Sambuc  unsigned sum = __builtin_addc(x, y, carryin, &carryout);
1606f4a2713aSLionel Sambuc
1607f4a2713aSLionel SambucThus one can form a multiprecision addition chain in the following manner:
1608f4a2713aSLionel Sambuc
1609f4a2713aSLionel Sambuc.. code-block:: c
1610f4a2713aSLionel Sambuc
1611f4a2713aSLionel Sambuc  unsigned *x, *y, *z, carryin=0, carryout;
1612f4a2713aSLionel Sambuc  z[0] = __builtin_addc(x[0], y[0], carryin, &carryout);
1613f4a2713aSLionel Sambuc  carryin = carryout;
1614f4a2713aSLionel Sambuc  z[1] = __builtin_addc(x[1], y[1], carryin, &carryout);
1615f4a2713aSLionel Sambuc  carryin = carryout;
1616f4a2713aSLionel Sambuc  z[2] = __builtin_addc(x[2], y[2], carryin, &carryout);
1617f4a2713aSLionel Sambuc  carryin = carryout;
1618f4a2713aSLionel Sambuc  z[3] = __builtin_addc(x[3], y[3], carryin, &carryout);
1619f4a2713aSLionel Sambuc
1620f4a2713aSLionel SambucThe complete list of builtins are:
1621f4a2713aSLionel Sambuc
1622f4a2713aSLionel Sambuc.. code-block:: c
1623f4a2713aSLionel Sambuc
1624f4a2713aSLionel Sambuc  unsigned char      __builtin_addcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout);
1625f4a2713aSLionel Sambuc  unsigned short     __builtin_addcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout);
1626f4a2713aSLionel Sambuc  unsigned           __builtin_addc  (unsigned x, unsigned y, unsigned carryin, unsigned *carryout);
1627f4a2713aSLionel Sambuc  unsigned long      __builtin_addcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout);
1628f4a2713aSLionel Sambuc  unsigned long long __builtin_addcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout);
1629f4a2713aSLionel Sambuc  unsigned char      __builtin_subcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout);
1630f4a2713aSLionel Sambuc  unsigned short     __builtin_subcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout);
1631f4a2713aSLionel Sambuc  unsigned           __builtin_subc  (unsigned x, unsigned y, unsigned carryin, unsigned *carryout);
1632f4a2713aSLionel Sambuc  unsigned long      __builtin_subcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout);
1633f4a2713aSLionel Sambuc  unsigned long long __builtin_subcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout);
1634f4a2713aSLionel Sambuc
1635f4a2713aSLionel SambucChecked Arithmetic Builtins
1636f4a2713aSLionel Sambuc---------------------------
1637f4a2713aSLionel Sambuc
1638f4a2713aSLionel SambucClang provides a set of builtins that implement checked arithmetic for security
1639f4a2713aSLionel Sambuccritical applications in a manner that is fast and easily expressable in C. As
1640f4a2713aSLionel Sambucan example of their usage:
1641f4a2713aSLionel Sambuc
1642f4a2713aSLionel Sambuc.. code-block:: c
1643f4a2713aSLionel Sambuc
1644f4a2713aSLionel Sambuc  errorcode_t security_critical_application(...) {
1645f4a2713aSLionel Sambuc    unsigned x, y, result;
1646f4a2713aSLionel Sambuc    ...
1647f4a2713aSLionel Sambuc    if (__builtin_umul_overflow(x, y, &result))
1648f4a2713aSLionel Sambuc      return kErrorCodeHackers;
1649f4a2713aSLionel Sambuc    ...
1650f4a2713aSLionel Sambuc    use_multiply(result);
1651f4a2713aSLionel Sambuc    ...
1652f4a2713aSLionel Sambuc  }
1653f4a2713aSLionel Sambuc
1654f4a2713aSLionel SambucA complete enumeration of the builtins are:
1655f4a2713aSLionel Sambuc
1656f4a2713aSLionel Sambuc.. code-block:: c
1657f4a2713aSLionel Sambuc
1658f4a2713aSLionel Sambuc  bool __builtin_uadd_overflow  (unsigned x, unsigned y, unsigned *sum);
1659f4a2713aSLionel Sambuc  bool __builtin_uaddl_overflow (unsigned long x, unsigned long y, unsigned long *sum);
1660f4a2713aSLionel Sambuc  bool __builtin_uaddll_overflow(unsigned long long x, unsigned long long y, unsigned long long *sum);
1661f4a2713aSLionel Sambuc  bool __builtin_usub_overflow  (unsigned x, unsigned y, unsigned *diff);
1662f4a2713aSLionel Sambuc  bool __builtin_usubl_overflow (unsigned long x, unsigned long y, unsigned long *diff);
1663f4a2713aSLionel Sambuc  bool __builtin_usubll_overflow(unsigned long long x, unsigned long long y, unsigned long long *diff);
1664f4a2713aSLionel Sambuc  bool __builtin_umul_overflow  (unsigned x, unsigned y, unsigned *prod);
1665f4a2713aSLionel Sambuc  bool __builtin_umull_overflow (unsigned long x, unsigned long y, unsigned long *prod);
1666f4a2713aSLionel Sambuc  bool __builtin_umulll_overflow(unsigned long long x, unsigned long long y, unsigned long long *prod);
1667f4a2713aSLionel Sambuc  bool __builtin_sadd_overflow  (int x, int y, int *sum);
1668f4a2713aSLionel Sambuc  bool __builtin_saddl_overflow (long x, long y, long *sum);
1669f4a2713aSLionel Sambuc  bool __builtin_saddll_overflow(long long x, long long y, long long *sum);
1670f4a2713aSLionel Sambuc  bool __builtin_ssub_overflow  (int x, int y, int *diff);
1671f4a2713aSLionel Sambuc  bool __builtin_ssubl_overflow (long x, long y, long *diff);
1672f4a2713aSLionel Sambuc  bool __builtin_ssubll_overflow(long long x, long long y, long long *diff);
1673f4a2713aSLionel Sambuc  bool __builtin_smul_overflow  (int x, int y, int *prod);
1674f4a2713aSLionel Sambuc  bool __builtin_smull_overflow (long x, long y, long *prod);
1675f4a2713aSLionel Sambuc  bool __builtin_smulll_overflow(long long x, long long y, long long *prod);
1676f4a2713aSLionel Sambuc
1677f4a2713aSLionel Sambuc
1678f4a2713aSLionel Sambuc.. _langext-__c11_atomic:
1679f4a2713aSLionel Sambuc
1680f4a2713aSLionel Sambuc__c11_atomic builtins
1681f4a2713aSLionel Sambuc---------------------
1682f4a2713aSLionel Sambuc
1683f4a2713aSLionel SambucClang provides a set of builtins which are intended to be used to implement
1684f4a2713aSLionel SambucC11's ``<stdatomic.h>`` header.  These builtins provide the semantics of the
1685f4a2713aSLionel Sambuc``_explicit`` form of the corresponding C11 operation, and are named with a
1686*0a6a1f1dSLionel Sambuc``__c11_`` prefix.  The supported operations, and the differences from
1687*0a6a1f1dSLionel Sambucthe corresponding C11 operations, are:
1688f4a2713aSLionel Sambuc
1689f4a2713aSLionel Sambuc* ``__c11_atomic_init``
1690f4a2713aSLionel Sambuc* ``__c11_atomic_thread_fence``
1691f4a2713aSLionel Sambuc* ``__c11_atomic_signal_fence``
1692*0a6a1f1dSLionel Sambuc* ``__c11_atomic_is_lock_free`` (The argument is the size of the
1693*0a6a1f1dSLionel Sambuc  ``_Atomic(...)`` object, instead of its address)
1694f4a2713aSLionel Sambuc* ``__c11_atomic_store``
1695f4a2713aSLionel Sambuc* ``__c11_atomic_load``
1696f4a2713aSLionel Sambuc* ``__c11_atomic_exchange``
1697f4a2713aSLionel Sambuc* ``__c11_atomic_compare_exchange_strong``
1698f4a2713aSLionel Sambuc* ``__c11_atomic_compare_exchange_weak``
1699f4a2713aSLionel Sambuc* ``__c11_atomic_fetch_add``
1700f4a2713aSLionel Sambuc* ``__c11_atomic_fetch_sub``
1701f4a2713aSLionel Sambuc* ``__c11_atomic_fetch_and``
1702f4a2713aSLionel Sambuc* ``__c11_atomic_fetch_or``
1703f4a2713aSLionel Sambuc* ``__c11_atomic_fetch_xor``
1704f4a2713aSLionel Sambuc
1705*0a6a1f1dSLionel SambucThe macros ``__ATOMIC_RELAXED``, ``__ATOMIC_CONSUME``, ``__ATOMIC_ACQUIRE``,
1706*0a6a1f1dSLionel Sambuc``__ATOMIC_RELEASE``, ``__ATOMIC_ACQ_REL``, and ``__ATOMIC_SEQ_CST`` are
1707*0a6a1f1dSLionel Sambucprovided, with values corresponding to the enumerators of C11's
1708*0a6a1f1dSLionel Sambuc``memory_order`` enumeration.
1709*0a6a1f1dSLionel Sambuc
1710f4a2713aSLionel SambucLow-level ARM exclusive memory builtins
1711f4a2713aSLionel Sambuc---------------------------------------
1712f4a2713aSLionel Sambuc
1713f4a2713aSLionel SambucClang provides overloaded builtins giving direct access to the three key ARM
1714f4a2713aSLionel Sambucinstructions for implementing atomic operations.
1715f4a2713aSLionel Sambuc
1716f4a2713aSLionel Sambuc.. code-block:: c
1717f4a2713aSLionel Sambuc
1718f4a2713aSLionel Sambuc  T __builtin_arm_ldrex(const volatile T *addr);
1719*0a6a1f1dSLionel Sambuc  T __builtin_arm_ldaex(const volatile T *addr);
1720f4a2713aSLionel Sambuc  int __builtin_arm_strex(T val, volatile T *addr);
1721*0a6a1f1dSLionel Sambuc  int __builtin_arm_stlex(T val, volatile T *addr);
1722f4a2713aSLionel Sambuc  void __builtin_arm_clrex(void);
1723f4a2713aSLionel Sambuc
1724f4a2713aSLionel SambucThe types ``T`` currently supported are:
1725*0a6a1f1dSLionel Sambuc* Integer types with width at most 64 bits (or 128 bits on AArch64).
1726f4a2713aSLionel Sambuc* Floating-point types
1727f4a2713aSLionel Sambuc* Pointer types.
1728f4a2713aSLionel Sambuc
1729f4a2713aSLionel SambucNote that the compiler does not guarantee it will not insert stores which clear
1730*0a6a1f1dSLionel Sambucthe exclusive monitor in between an ``ldrex`` type operation and its paired
1731*0a6a1f1dSLionel Sambuc``strex``. In practice this is only usually a risk when the extra store is on
1732*0a6a1f1dSLionel Sambucthe same cache line as the variable being modified and Clang will only insert
1733*0a6a1f1dSLionel Sambucstack stores on its own, so it is best not to use these operations on variables
1734*0a6a1f1dSLionel Sambucwith automatic storage duration.
1735f4a2713aSLionel Sambuc
1736f4a2713aSLionel SambucAlso, loads and stores may be implicit in code written between the ``ldrex`` and
1737f4a2713aSLionel Sambuc``strex``. Clang will not necessarily mitigate the effects of these either, so
1738f4a2713aSLionel Sambuccare should be exercised.
1739f4a2713aSLionel Sambuc
1740f4a2713aSLionel SambucFor these reasons the higher level atomic primitives should be preferred where
1741f4a2713aSLionel Sambucpossible.
1742f4a2713aSLionel Sambuc
1743f4a2713aSLionel SambucNon-standard C++11 Attributes
1744f4a2713aSLionel Sambuc=============================
1745f4a2713aSLionel Sambuc
1746f4a2713aSLionel SambucClang's non-standard C++11 attributes live in the ``clang`` attribute
1747f4a2713aSLionel Sambucnamespace.
1748f4a2713aSLionel Sambuc
1749*0a6a1f1dSLionel SambucClang supports GCC's ``gnu`` attribute namespace. All GCC attributes which
1750f4a2713aSLionel Sambucare accepted with the ``__attribute__((foo))`` syntax are also accepted as
1751f4a2713aSLionel Sambuc``[[gnu::foo]]``. This only extends to attributes which are specified by GCC
1752f4a2713aSLionel Sambuc(see the list of `GCC function attributes
1753f4a2713aSLionel Sambuc<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_, `GCC variable
1754f4a2713aSLionel Sambucattributes <http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html>`_, and
1755f4a2713aSLionel Sambuc`GCC type attributes
1756f4a2713aSLionel Sambuc<http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html>`_). As with the GCC
1757f4a2713aSLionel Sambucimplementation, these attributes must appertain to the *declarator-id* in a
1758f4a2713aSLionel Sambucdeclaration, which means they must go either at the start of the declaration or
1759f4a2713aSLionel Sambucimmediately after the name being declared.
1760f4a2713aSLionel Sambuc
1761f4a2713aSLionel SambucFor example, this applies the GNU ``unused`` attribute to ``a`` and ``f``, and
1762f4a2713aSLionel Sambucalso applies the GNU ``noreturn`` attribute to ``f``.
1763f4a2713aSLionel Sambuc
1764f4a2713aSLionel Sambuc.. code-block:: c++
1765f4a2713aSLionel Sambuc
1766f4a2713aSLionel Sambuc  [[gnu::unused]] int a, f [[gnu::noreturn]] ();
1767f4a2713aSLionel Sambuc
1768f4a2713aSLionel SambucTarget-Specific Extensions
1769f4a2713aSLionel Sambuc==========================
1770f4a2713aSLionel Sambuc
1771f4a2713aSLionel SambucClang supports some language features conditionally on some targets.
1772f4a2713aSLionel Sambuc
1773*0a6a1f1dSLionel SambucARM/AArch64 Language Extensions
1774*0a6a1f1dSLionel Sambuc-------------------------------
1775*0a6a1f1dSLionel Sambuc
1776*0a6a1f1dSLionel SambucMemory Barrier Intrinsics
1777*0a6a1f1dSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^
1778*0a6a1f1dSLionel SambucClang implements the ``__dmb``, ``__dsb`` and ``__isb`` intrinsics as defined
1779*0a6a1f1dSLionel Sambucin the `ARM C Language Extensions Release 2.0
1780*0a6a1f1dSLionel Sambuc<http://infocenter.arm.com/help/topic/com.arm.doc.ihi0053c/IHI0053C_acle_2_0.pdf>`_.
1781*0a6a1f1dSLionel SambucNote that these intrinsics are implemented as motion barriers that block
1782*0a6a1f1dSLionel Sambucreordering of memory accesses and side effect instructions. Other instructions
1783*0a6a1f1dSLionel Sambuclike simple arithmatic may be reordered around the intrinsic. If you expect to
1784*0a6a1f1dSLionel Sambuchave no reordering at all, use inline assembly instead.
1785*0a6a1f1dSLionel Sambuc
1786f4a2713aSLionel SambucX86/X86-64 Language Extensions
1787f4a2713aSLionel Sambuc------------------------------
1788f4a2713aSLionel Sambuc
1789f4a2713aSLionel SambucThe X86 backend has these language extensions:
1790f4a2713aSLionel Sambuc
1791f4a2713aSLionel SambucMemory references off the GS segment
1792f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1793f4a2713aSLionel Sambuc
1794f4a2713aSLionel SambucAnnotating a pointer with address space #256 causes it to be code generated
1795f4a2713aSLionel Sambucrelative to the X86 GS segment register, and address space #257 causes it to be
1796f4a2713aSLionel Sambucrelative to the X86 FS segment.  Note that this is a very very low-level
1797f4a2713aSLionel Sambucfeature that should only be used if you know what you're doing (for example in
1798f4a2713aSLionel Sambucan OS kernel).
1799f4a2713aSLionel Sambuc
1800f4a2713aSLionel SambucHere is an example:
1801f4a2713aSLionel Sambuc
1802f4a2713aSLionel Sambuc.. code-block:: c++
1803f4a2713aSLionel Sambuc
1804f4a2713aSLionel Sambuc  #define GS_RELATIVE __attribute__((address_space(256)))
1805f4a2713aSLionel Sambuc  int foo(int GS_RELATIVE *P) {
1806f4a2713aSLionel Sambuc    return *P;
1807f4a2713aSLionel Sambuc  }
1808f4a2713aSLionel Sambuc
1809f4a2713aSLionel SambucWhich compiles to (on X86-32):
1810f4a2713aSLionel Sambuc
1811f4a2713aSLionel Sambuc.. code-block:: gas
1812f4a2713aSLionel Sambuc
1813f4a2713aSLionel Sambuc  _foo:
1814f4a2713aSLionel Sambuc          movl    4(%esp), %eax
1815f4a2713aSLionel Sambuc          movl    %gs:(%eax), %eax
1816f4a2713aSLionel Sambuc          ret
1817f4a2713aSLionel Sambuc
1818f4a2713aSLionel SambucExtensions for Static Analysis
1819f4a2713aSLionel Sambuc==============================
1820f4a2713aSLionel Sambuc
1821f4a2713aSLionel SambucClang supports additional attributes that are useful for documenting program
1822f4a2713aSLionel Sambucinvariants and rules for static analysis tools, such as the `Clang Static
1823f4a2713aSLionel SambucAnalyzer <http://clang-analyzer.llvm.org/>`_. These attributes are documented
1824f4a2713aSLionel Sambucin the analyzer's `list of source-level annotations
1825f4a2713aSLionel Sambuc<http://clang-analyzer.llvm.org/annotations.html>`_.
1826f4a2713aSLionel Sambuc
1827f4a2713aSLionel Sambuc
1828f4a2713aSLionel SambucExtensions for Dynamic Analysis
1829f4a2713aSLionel Sambuc===============================
1830f4a2713aSLionel Sambuc
1831f4a2713aSLionel SambucUse ``__has_feature(address_sanitizer)`` to check if the code is being built
1832f4a2713aSLionel Sambucwith :doc:`AddressSanitizer`.
1833f4a2713aSLionel Sambuc
1834f4a2713aSLionel SambucUse ``__has_feature(thread_sanitizer)`` to check if the code is being built
1835f4a2713aSLionel Sambucwith :doc:`ThreadSanitizer`.
1836f4a2713aSLionel Sambuc
1837f4a2713aSLionel SambucUse ``__has_feature(memory_sanitizer)`` to check if the code is being built
1838f4a2713aSLionel Sambucwith :doc:`MemorySanitizer`.
1839f4a2713aSLionel Sambuc
1840f4a2713aSLionel Sambuc
1841*0a6a1f1dSLionel SambucExtensions for selectively disabling optimization
1842*0a6a1f1dSLionel Sambuc=================================================
1843f4a2713aSLionel Sambuc
1844*0a6a1f1dSLionel SambucClang provides a mechanism for selectively disabling optimizations in functions
1845*0a6a1f1dSLionel Sambucand methods.
1846f4a2713aSLionel Sambuc
1847*0a6a1f1dSLionel SambucTo disable optimizations in a single function definition, the GNU-style or C++11
1848*0a6a1f1dSLionel Sambucnon-standard attribute ``optnone`` can be used.
1849f4a2713aSLionel Sambuc
1850*0a6a1f1dSLionel Sambuc.. code-block:: c++
1851f4a2713aSLionel Sambuc
1852*0a6a1f1dSLionel Sambuc  // The following functions will not be optimized.
1853*0a6a1f1dSLionel Sambuc  // GNU-style attribute
1854*0a6a1f1dSLionel Sambuc  __attribute__((optnone)) int foo() {
1855*0a6a1f1dSLionel Sambuc    // ... code
1856*0a6a1f1dSLionel Sambuc  }
1857*0a6a1f1dSLionel Sambuc  // C++11 attribute
1858*0a6a1f1dSLionel Sambuc  [[clang::optnone]] int bar() {
1859*0a6a1f1dSLionel Sambuc    // ... code
1860*0a6a1f1dSLionel Sambuc  }
1861f4a2713aSLionel Sambuc
1862*0a6a1f1dSLionel SambucTo facilitate disabling optimization for a range of function definitions, a
1863*0a6a1f1dSLionel Sambucrange-based pragma is provided. Its syntax is ``#pragma clang optimize``
1864*0a6a1f1dSLionel Sambucfollowed by ``off`` or ``on``.
1865f4a2713aSLionel Sambuc
1866*0a6a1f1dSLionel SambucAll function definitions in the region between an ``off`` and the following
1867*0a6a1f1dSLionel Sambuc``on`` will be decorated with the ``optnone`` attribute unless doing so would
1868*0a6a1f1dSLionel Sambucconflict with explicit attributes already present on the function (e.g. the
1869*0a6a1f1dSLionel Sambucones that control inlining).
1870f4a2713aSLionel Sambuc
1871*0a6a1f1dSLionel Sambuc.. code-block:: c++
1872f4a2713aSLionel Sambuc
1873*0a6a1f1dSLionel Sambuc  #pragma clang optimize off
1874*0a6a1f1dSLionel Sambuc  // This function will be decorated with optnone.
1875*0a6a1f1dSLionel Sambuc  int foo() {
1876*0a6a1f1dSLionel Sambuc    // ... code
1877*0a6a1f1dSLionel Sambuc  }
1878f4a2713aSLionel Sambuc
1879*0a6a1f1dSLionel Sambuc  // optnone conflicts with always_inline, so bar() will not be decorated.
1880*0a6a1f1dSLionel Sambuc  __attribute__((always_inline)) int bar() {
1881*0a6a1f1dSLionel Sambuc    // ... code
1882*0a6a1f1dSLionel Sambuc  }
1883*0a6a1f1dSLionel Sambuc  #pragma clang optimize on
1884f4a2713aSLionel Sambuc
1885*0a6a1f1dSLionel SambucIf no ``on`` is found to close an ``off`` region, the end of the region is the
1886*0a6a1f1dSLionel Sambucend of the compilation unit.
1887f4a2713aSLionel Sambuc
1888*0a6a1f1dSLionel SambucNote that a stray ``#pragma clang optimize on`` does not selectively enable
1889*0a6a1f1dSLionel Sambucadditional optimizations when compiling at low optimization levels. This feature
1890*0a6a1f1dSLionel Sambuccan only be used to selectively disable optimizations.
1891f4a2713aSLionel Sambuc
1892*0a6a1f1dSLionel SambucThe pragma has an effect on functions only at the point of their definition; for
1893*0a6a1f1dSLionel Sambucfunction templates, this means that the state of the pragma at the point of an
1894*0a6a1f1dSLionel Sambucinstantiation is not necessarily relevant. Consider the following example:
1895f4a2713aSLionel Sambuc
1896*0a6a1f1dSLionel Sambuc.. code-block:: c++
1897f4a2713aSLionel Sambuc
1898*0a6a1f1dSLionel Sambuc  template<typename T> T twice(T t) {
1899*0a6a1f1dSLionel Sambuc    return 2 * t;
1900*0a6a1f1dSLionel Sambuc  }
1901f4a2713aSLionel Sambuc
1902*0a6a1f1dSLionel Sambuc  #pragma clang optimize off
1903*0a6a1f1dSLionel Sambuc  template<typename T> T thrice(T t) {
1904*0a6a1f1dSLionel Sambuc    return 3 * t;
1905*0a6a1f1dSLionel Sambuc  }
1906f4a2713aSLionel Sambuc
1907*0a6a1f1dSLionel Sambuc  int container(int a, int b) {
1908*0a6a1f1dSLionel Sambuc    return twice(a) + thrice(b);
1909*0a6a1f1dSLionel Sambuc  }
1910*0a6a1f1dSLionel Sambuc  #pragma clang optimize on
1911f4a2713aSLionel Sambuc
1912*0a6a1f1dSLionel SambucIn this example, the definition of the template function ``twice`` is outside
1913*0a6a1f1dSLionel Sambucthe pragma region, whereas the definition of ``thrice`` is inside the region.
1914*0a6a1f1dSLionel SambucThe ``container`` function is also in the region and will not be optimized, but
1915*0a6a1f1dSLionel Sambucit causes the instantiation of ``twice`` and ``thrice`` with an ``int`` type; of
1916*0a6a1f1dSLionel Sambucthese two instantiations, ``twice`` will be optimized (because its definition
1917*0a6a1f1dSLionel Sambucwas outside the region) and ``thrice`` will not be optimized.
1918f4a2713aSLionel Sambuc
1919*0a6a1f1dSLionel SambucExtensions for loop hint optimizations
1920*0a6a1f1dSLionel Sambuc======================================
1921f4a2713aSLionel Sambuc
1922*0a6a1f1dSLionel SambucThe ``#pragma clang loop`` directive is used to specify hints for optimizing the
1923*0a6a1f1dSLionel Sambucsubsequent for, while, do-while, or c++11 range-based for loop. The directive
1924*0a6a1f1dSLionel Sambucprovides options for vectorization, interleaving, and unrolling. Loop hints can
1925*0a6a1f1dSLionel Sambucbe specified before any loop and will be ignored if the optimization is not safe
1926*0a6a1f1dSLionel Sambucto apply.
1927f4a2713aSLionel Sambuc
1928*0a6a1f1dSLionel SambucVectorization and Interleaving
1929f4a2713aSLionel Sambuc------------------------------
1930f4a2713aSLionel Sambuc
1931*0a6a1f1dSLionel SambucA vectorized loop performs multiple iterations of the original loop
1932*0a6a1f1dSLionel Sambucin parallel using vector instructions. The instruction set of the target
1933*0a6a1f1dSLionel Sambucprocessor determines which vector instructions are available and their vector
1934*0a6a1f1dSLionel Sambucwidths. This restricts the types of loops that can be vectorized. The vectorizer
1935*0a6a1f1dSLionel Sambucautomatically determines if the loop is safe and profitable to vectorize. A
1936*0a6a1f1dSLionel Sambucvector instruction cost model is used to select the vector width.
1937f4a2713aSLionel Sambuc
1938*0a6a1f1dSLionel SambucInterleaving multiple loop iterations allows modern processors to further
1939*0a6a1f1dSLionel Sambucimprove instruction-level parallelism (ILP) using advanced hardware features,
1940*0a6a1f1dSLionel Sambucsuch as multiple execution units and out-of-order execution. The vectorizer uses
1941*0a6a1f1dSLionel Sambuca cost model that depends on the register pressure and generated code size to
1942*0a6a1f1dSLionel Sambucselect the interleaving count.
1943f4a2713aSLionel Sambuc
1944*0a6a1f1dSLionel SambucVectorization is enabled by ``vectorize(enable)`` and interleaving is enabled
1945*0a6a1f1dSLionel Sambucby ``interleave(enable)``. This is useful when compiling with ``-Os`` to
1946*0a6a1f1dSLionel Sambucmanually enable vectorization or interleaving.
1947f4a2713aSLionel Sambuc
1948*0a6a1f1dSLionel Sambuc.. code-block:: c++
1949*0a6a1f1dSLionel Sambuc
1950*0a6a1f1dSLionel Sambuc  #pragma clang loop vectorize(enable)
1951*0a6a1f1dSLionel Sambuc  #pragma clang loop interleave(enable)
1952*0a6a1f1dSLionel Sambuc  for(...) {
1953*0a6a1f1dSLionel Sambuc    ...
1954*0a6a1f1dSLionel Sambuc  }
1955*0a6a1f1dSLionel Sambuc
1956*0a6a1f1dSLionel SambucThe vector width is specified by ``vectorize_width(_value_)`` and the interleave
1957*0a6a1f1dSLionel Sambuccount is specified by ``interleave_count(_value_)``, where
1958*0a6a1f1dSLionel Sambuc_value_ is a positive integer. This is useful for specifying the optimal
1959*0a6a1f1dSLionel Sambucwidth/count of the set of target architectures supported by your application.
1960*0a6a1f1dSLionel Sambuc
1961*0a6a1f1dSLionel Sambuc.. code-block:: c++
1962*0a6a1f1dSLionel Sambuc
1963*0a6a1f1dSLionel Sambuc  #pragma clang loop vectorize_width(2)
1964*0a6a1f1dSLionel Sambuc  #pragma clang loop interleave_count(2)
1965*0a6a1f1dSLionel Sambuc  for(...) {
1966*0a6a1f1dSLionel Sambuc    ...
1967*0a6a1f1dSLionel Sambuc  }
1968*0a6a1f1dSLionel Sambuc
1969*0a6a1f1dSLionel SambucSpecifying a width/count of 1 disables the optimization, and is equivalent to
1970*0a6a1f1dSLionel Sambuc``vectorize(disable)`` or ``interleave(disable)``.
1971*0a6a1f1dSLionel Sambuc
1972*0a6a1f1dSLionel SambucLoop Unrolling
1973f4a2713aSLionel Sambuc--------------
1974f4a2713aSLionel Sambuc
1975*0a6a1f1dSLionel SambucUnrolling a loop reduces the loop control overhead and exposes more
1976*0a6a1f1dSLionel Sambucopportunities for ILP. Loops can be fully or partially unrolled. Full unrolling
1977*0a6a1f1dSLionel Sambuceliminates the loop and replaces it with an enumerated sequence of loop
1978*0a6a1f1dSLionel Sambuciterations. Full unrolling is only possible if the loop trip count is known at
1979*0a6a1f1dSLionel Sambuccompile time. Partial unrolling replicates the loop body within the loop and
1980*0a6a1f1dSLionel Sambucreduces the trip count.
1981f4a2713aSLionel Sambuc
1982*0a6a1f1dSLionel SambucIf ``unroll(full)`` is specified the unroller will attempt to fully unroll the
1983*0a6a1f1dSLionel Sambucloop if the trip count is known at compile time. If the loop count is not known
1984*0a6a1f1dSLionel Sambucor the fully unrolled code size is greater than the limit specified by the
1985*0a6a1f1dSLionel Sambuc`-pragma-unroll-threshold` command line option the loop will be partially
1986*0a6a1f1dSLionel Sambucunrolled subject to the same limit.
1987f4a2713aSLionel Sambuc
1988*0a6a1f1dSLionel Sambuc.. code-block:: c++
1989f4a2713aSLionel Sambuc
1990*0a6a1f1dSLionel Sambuc  #pragma clang loop unroll(full)
1991*0a6a1f1dSLionel Sambuc  for(...) {
1992*0a6a1f1dSLionel Sambuc    ...
1993*0a6a1f1dSLionel Sambuc  }
1994*0a6a1f1dSLionel Sambuc
1995*0a6a1f1dSLionel SambucThe unroll count can be specified explicitly with ``unroll_count(_value_)`` where
1996*0a6a1f1dSLionel Sambuc_value_ is a positive integer. If this value is greater than the trip count the
1997*0a6a1f1dSLionel Sambucloop will be fully unrolled. Otherwise the loop is partially unrolled subject
1998*0a6a1f1dSLionel Sambucto the `-pragma-unroll-threshold` limit.
1999*0a6a1f1dSLionel Sambuc
2000*0a6a1f1dSLionel Sambuc.. code-block:: c++
2001*0a6a1f1dSLionel Sambuc
2002*0a6a1f1dSLionel Sambuc  #pragma clang loop unroll_count(8)
2003*0a6a1f1dSLionel Sambuc  for(...) {
2004*0a6a1f1dSLionel Sambuc    ...
2005*0a6a1f1dSLionel Sambuc  }
2006*0a6a1f1dSLionel Sambuc
2007*0a6a1f1dSLionel SambucUnrolling of a loop can be prevented by specifying ``unroll(disable)``.
2008*0a6a1f1dSLionel Sambuc
2009*0a6a1f1dSLionel SambucAdditional Information
2010f4a2713aSLionel Sambuc----------------------
2011f4a2713aSLionel Sambuc
2012*0a6a1f1dSLionel SambucFor convenience multiple loop hints can be specified on a single line.
2013f4a2713aSLionel Sambuc
2014f4a2713aSLionel Sambuc.. code-block:: c++
2015f4a2713aSLionel Sambuc
2016*0a6a1f1dSLionel Sambuc  #pragma clang loop vectorize_width(4) interleave_count(8)
2017*0a6a1f1dSLionel Sambuc  for(...) {
2018*0a6a1f1dSLionel Sambuc    ...
2019f4a2713aSLionel Sambuc  }
2020f4a2713aSLionel Sambuc
2021*0a6a1f1dSLionel SambucIf an optimization cannot be applied any hints that apply to it will be ignored.
2022*0a6a1f1dSLionel SambucFor example, the hint ``vectorize_width(4)`` is ignored if the loop is not
2023*0a6a1f1dSLionel Sambucproven safe to vectorize. To identify and diagnose optimization issues use
2024*0a6a1f1dSLionel Sambuc`-Rpass`, `-Rpass-missed`, and `-Rpass-analysis` command line options. See the
2025*0a6a1f1dSLionel Sambucuser guide for details.
2026