blob: 1c892fd23a5a645b9ff501fce2eea0aa50c60031 [file] [log] [blame]
Chris Lattner5ce933f2009-02-09 08:46:11 +00001<html>
2<head>
3<title>Clang Language Extensions</title>
4<link type="text/css" rel="stylesheet" href="../menu.css" />
5<link type="text/css" rel="stylesheet" href="../content.css" />
6<style type="text/css">
7td {
8 vertical-align: top;
9}
10</style>
11</head>
12<body>
13
14<!--#include virtual="../menu.html.incl"-->
15
16<div id="content">
17
18<h1>Clang Language Extensions</h1>
19
20<ul>
21<li><a href="#intro">Introduction</a></li>
Chris Lattner148772a2009-06-13 07:13:28 +000022<li><a href="#feature_check">Feature Checking Macros</a></li>
John Thompson92bd8c72009-11-02 22:28:12 +000023<li><a href="#has_include">Include File Checking Macros</a></li>
Chris Lattner81edc9f2009-04-13 02:45:46 +000024<li><a href="#builtinmacros">Builtin Macros</a></li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000025<li><a href="#vectors">Vectors and Extended Vectors</a></li>
26<li><a href="#blocks">Blocks</a></li>
Douglas Gregorcb54d432009-02-13 00:57:04 +000027<li><a href="#overloading-in-c">Function Overloading in C</a></li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000028<li><a href="#builtins">Builtin Functions</a>
29 <ul>
Chris Lattner5ce933f2009-02-09 08:46:11 +000030 <li><a href="#__builtin_shufflevector">__builtin_shufflevector</a></li>
Chris Lattner21190d52009-09-21 03:09:59 +000031 <li><a href="#__builtin_unreachable">__builtin_unreachable</a></li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000032 </ul>
33</li>
Chris Lattner1177f912009-04-09 19:58:15 +000034<li><a href="#targetspecific">Target-Specific Extensions</a>
35 <ul>
36 <li><a href="#x86-specific">X86/X86-64 Language Extensions</a></li>
37 </ul>
38</li>
Ted Kremeneked869312009-04-10 05:03:33 +000039<li><a href="#analyzerspecific">Static Analysis-Specific Extensions</a>
40 <ul>
41 <li><a href="#analyzerattributes">Analyzer Attributes</a></li>
42 </ul>
43</li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000044</ul>
45
Chris Lattner5ce933f2009-02-09 08:46:11 +000046<!-- ======================================================================= -->
47<h2 id="intro">Introduction</h2>
48<!-- ======================================================================= -->
49
50<p>This document describes the language extensions provided by Clang. In
Chris Lattner148772a2009-06-13 07:13:28 +000051addition to the language extensions listed here, Clang aims to support a broad
Chris Lattner5ce933f2009-02-09 08:46:11 +000052range of GCC extensions. Please see the <a
53href="http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html">GCC manual</a> for
54more information on these extensions.</p>
55
56<!-- ======================================================================= -->
Chris Lattner148772a2009-06-13 07:13:28 +000057<h2 id="feature_check">Feature Checking Macros</h2>
58<!-- ======================================================================= -->
59
60<p>Language extensions can be very useful, but only if you know you can depend
61on them. In order to allow fine-grain features checks, we support two builtin
62function-like macros. This allows you to directly test for a feature in your
63code without having to resort to something like autoconf or fragile "compiler
64version checks".</p>
65
66<!-- ======================================================================= -->
67<h3 id="__has_builtin">__has_builtin</h3>
68<!-- ======================================================================= -->
69
70<p>This function-like macro takes a single identifier argument that is the name
71of a builtin function. It evaluates to 1 if the builtin is supported or 0 if
72not. It can be used like this:</p>
73
74<blockquote>
75<pre>
76#ifndef __has_builtin // Optional of course.
77 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
78#endif
79
80...
81#if __has_builtin(__builtin_trap)
82 __builtin_trap();
83#else
84 abort();
85#endif
86...
87</pre>
88</blockquote>
89
90
91<!-- ======================================================================= -->
92<h3 id="__has_feature">__has_feature</h3>
93<!-- ======================================================================= -->
94
95<p>This function-like macro takes a single identifier argument that is the name
96of a feature. It evaluates to 1 if the feature is supported or 0 if not. It
97can be used like this:</p>
98
99<blockquote>
100<pre>
101#ifndef __has_feature // Optional of course.
102 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
103#endif
104
105...
106#if __has_feature(attribute_overloadable) || \
107 __has_feature(blocks)
108...
109#endif
110...
111</pre>
112</blockquote>
113
114<p>The feature tag is described along with the language feature below.</p>
115
John Thompson92bd8c72009-11-02 22:28:12 +0000116<!-- ======================================================================= -->
117<h2 id="has_include">Include File Checking Macros</h2>
118<!-- ======================================================================= -->
119
120<p>Not all developments systems have the same include files.
121The <a href="#__has_include">__has_include</a> and
122<a href="#__has_include_next">__has_include_next</a> macros allow you to
123check for the existence of an include file before doing
124a possibly failing #include directive.</p>
125
126<!-- ======================================================================= -->
127<h3 id="__has_include">__has_include</h3>
128<!-- ======================================================================= -->
129
130<p>This function-like macro takes a single file name string argument that
131is the name of an include file. It evaluates to 1 if the file can
132be found using the include paths, or 0 otherwise:</p>
133
134<blockquote>
135<pre>
136// Note the two possible file name string formats.
137#if __has_include("myinclude.h") && __has_include(&lt;stdint.h&gt;)
138# include "myinclude.h"
139#endif
140
141// To avoid problem with non-clang compilers not having this macro.
142#if defined(__has_include) && __has_include("myinclude.h")
143# include "myinclude.h"
144#endif
145</pre>
146</blockquote>
147
148<p>To test for this feature, use #if defined(__has_include).</p>
149
150<!-- ======================================================================= -->
151<h3 id="__has_include_next">__has_include_next</h3>
152<!-- ======================================================================= -->
153
154<p>This function-like macro takes a single file name string argument that
155is the name of an include file. It is like __has_include except that it
156looks for the second instance of the given file found in the include
157paths. It evaluates to 1 if the second instance of the file can
158be found using the include paths, or 0 otherwise:</p>
159
160<blockquote>
161<pre>
162// Note the two possible file name string formats.
163#if __has_include_next("myinclude.h") && __has_include_next(&lt;stdint.h&gt;)
164# include_next "myinclude.h"
165#endif
166
167// To avoid problem with non-clang compilers not having this macro.
168#if defined(__has_include_next) && __has_include_next("myinclude.h")
169# include_next "myinclude.h"
170#endif
171</pre>
172</blockquote>
173
174<p>Note that __has_include_next, like the GNU extension
175#include_next directive, is intended for use in headers only,
176and will issue a warning if used in the top-level compilation
177file. A warning will also be issued if an absolute path
178is used in the file argument.</p>
Chris Lattner148772a2009-06-13 07:13:28 +0000179
180<!-- ======================================================================= -->
Chris Lattner81edc9f2009-04-13 02:45:46 +0000181<h2 id="builtinmacros">Builtin Macros</h2>
182<!-- ======================================================================= -->
183
184<p>__BASE_FILE__, __INCLUDE_LEVEL__, __TIMESTAMP__, __COUNTER__</p>
185
186<!-- ======================================================================= -->
Chris Lattner5ce933f2009-02-09 08:46:11 +0000187<h2 id="vectors">Vectors and Extended Vectors</h2>
188<!-- ======================================================================= -->
189
190<p>Supports the GCC vector extensions, plus some stuff like V[1]. ext_vector
191with V.xyzw syntax and other tidbits. See also <a
192href="#__builtin_shufflevector">__builtin_shufflevector</a>.</p>
193
Chris Lattner148772a2009-06-13 07:13:28 +0000194<p>Query for this feature with __has_feature(attribute_ext_vector_type).</p>
195
Chris Lattner5ce933f2009-02-09 08:46:11 +0000196<!-- ======================================================================= -->
197<h2 id="blocks">Blocks</h2>
198<!-- ======================================================================= -->
199
Chris Lattnera7dbdf52009-03-09 07:03:22 +0000200<p>The syntax and high level language feature description is in <a
201href="BlockLanguageSpec.txt">BlockLanguageSpec.txt</a>. Implementation and ABI
202details for the clang implementation are in <a
203href="BlockImplementation.txt">BlockImplementation.txt</a>.</p>
Chris Lattner5ce933f2009-02-09 08:46:11 +0000204
Chris Lattner148772a2009-06-13 07:13:28 +0000205
206<p>Query for this feature with __has_feature(blocks).</p>
207
Chris Lattner5ce933f2009-02-09 08:46:11 +0000208<!-- ======================================================================= -->
Douglas Gregorcb54d432009-02-13 00:57:04 +0000209<h2 id="overloading-in-c">Function Overloading in C</h2>
210<!-- ======================================================================= -->
211
Chris Lattnerf161d412009-02-13 21:51:45 +0000212<p>Clang provides support for C++ function overloading in C. Function
213overloading in C is introduced using the <tt>overloadable</tt> attribute. For
214example, one might provide several overloaded versions of a <tt>tgsin</tt>
215function that invokes the appropriate standard function computing the sine of a
216value with <tt>float</tt>, <tt>double</tt>, or <tt>long double</tt>
217precision:</p>
Douglas Gregorcb54d432009-02-13 00:57:04 +0000218
219<blockquote>
220<pre>
221#include &lt;math.h&gt;
222float <b>__attribute__((overloadable))</b> tgsin(float x) { return sinf(x); }
223double <b>__attribute__((overloadable))</b> tgsin(double x) { return sin(x); }
224long double <b>__attribute__((overloadable))</b> tgsin(long double x) { return sinl(x); }
225</pre>
226</blockquote>
227
228<p>Given these declarations, one can call <tt>tgsin</tt> with a
229<tt>float</tt> value to receive a <tt>float</tt> result, with a
230<tt>double</tt> to receive a <tt>double</tt> result, etc. Function
231overloading in C follows the rules of C++ function overloading to pick
232the best overload given the call arguments, with a few C-specific
233semantics:</p>
234<ul>
235 <li>Conversion from <tt>float</tt> or <tt>double</tt> to <tt>long
236 double</tt> is ranked as a floating-point promotion (per C99) rather
237 than as a floating-point conversion (as in C++).</li>
238
239 <li>A conversion from a pointer of type <tt>T*</tt> to a pointer of type
240 <tt>U*</tt> is considered a pointer conversion (with conversion
241 rank) if <tt>T</tt> and <tt>U</tt> are compatible types.</li>
242
243 <li>A conversion from type <tt>T</tt> to a value of type <tt>U</tt>
244 is permitted if <tt>T</tt> and <tt>U</tt> are compatible types. This
245 conversion is given "conversion" rank.</li>
246</ul>
247
248<p>The declaration of <tt>overloadable</tt> functions is restricted to
249function declarations and definitions. Most importantly, if any
250function with a given name is given the <tt>overloadable</tt>
251attribute, then all function declarations and definitions with that
252name (and in that scope) must have the <tt>overloadable</tt>
Chris Lattnerf161d412009-02-13 21:51:45 +0000253attribute. This rule even applies to redeclarations of functions whose original
254declaration had the <tt>overloadable</tt> attribute, e.g.,</p>
Douglas Gregorcb54d432009-02-13 00:57:04 +0000255
256<blockquote>
257<pre>
258int f(int) __attribute__((overloadable));
259float f(float); <i>// error: declaration of "f" must have the "overloadable" attribute</i>
260
261int g(int) __attribute__((overloadable));
262int g(int) { } <i>// error: redeclaration of "g" must also have the "overloadable" attribute</i>
263</pre>
264</blockquote>
265
Douglas Gregor965acbb2009-02-18 07:07:28 +0000266<p>Functions marked <tt>overloadable</tt> must have
267prototypes. Therefore, the following code is ill-formed:</p>
268
269<blockquote>
270<pre>
271int h() __attribute__((overloadable)); <i>// error: h does not have a prototype</i>
272</pre>
273</blockquote>
274
275<p>However, <tt>overloadable</tt> functions are allowed to use a
276ellipsis even if there are no named parameters (as is permitted in C++). This feature is particularly useful when combined with the <tt>unavailable</tt> attribute:</p>
277
278<blockquote>
279<pre>
Chris Lattner02246802009-02-18 22:27:46 +0000280void honeypot(...) __attribute__((overloadable, unavailable)); <i>// calling me is an error</i>
Douglas Gregor965acbb2009-02-18 07:07:28 +0000281</pre>
282</blockquote>
283
Douglas Gregorcb54d432009-02-13 00:57:04 +0000284<p>Functions declared with the <tt>overloadable</tt> attribute have
285their names mangled according to the same rules as C++ function
286names. For example, the three <tt>tgsin</tt> functions in our
287motivating example get the mangled names <tt>_Z5tgsinf</tt>,
288<tt>_Z5tgsind</tt>, and <tt>Z5tgsine</tt>, respectively. There are two
289caveats to this use of name mangling:</p>
290
291<ul>
292
293 <li>Future versions of Clang may change the name mangling of
294 functions overloaded in C, so you should not depend on an specific
295 mangling. To be completely safe, we strongly urge the use of
296 <tt>static inline</tt> with <tt>overloadable</tt> functions.</li>
297
298 <li>The <tt>overloadable</tt> attribute has almost no meaning when
299 used in C++, because names will already be mangled and functions are
300 already overloadable. However, when an <tt>overloadable</tt>
301 function occurs within an <tt>extern "C"</tt> linkage specification,
302 it's name <i>will</i> be mangled in the same way as it would in
303 C.</li>
304</ul>
305
Chris Lattner148772a2009-06-13 07:13:28 +0000306<p>Query for this feature with __has_feature(attribute_overloadable).</p>
307
308
Douglas Gregorcb54d432009-02-13 00:57:04 +0000309<!-- ======================================================================= -->
Chris Lattner5ce933f2009-02-09 08:46:11 +0000310<h2 id="builtins">Builtin Functions</h2>
311<!-- ======================================================================= -->
312
313<p>Clang supports a number of builtin library functions with the same syntax as
314GCC, including things like <tt>__builtin_nan</tt>,
315<tt>__builtin_constant_p</tt>, <tt>__builtin_choose_expr</tt>,
316<tt>__builtin_types_compatible_p</tt>, <tt>__sync_fetch_and_add</tt>, etc. In
317addition to the GCC builtins, Clang supports a number of builtins that GCC does
318not, which are listed here.</p>
319
320<p>Please note that Clang does not and will not support all of the GCC builtins
321for vector operations. Instead of using builtins, you should use the functions
322defined in target-specific header files like <tt>&lt;xmmintrin.h&gt;</tt>, which
323define portable wrappers for these. Many of the Clang versions of these
324functions are implemented directly in terms of <a href="#vectors">extended
325vector support</a> instead of builtins, in order to reduce the number of
326builtins that we need to implement.</p>
327
Chris Lattner5ce933f2009-02-09 08:46:11 +0000328<!-- ======================================================================= -->
Chris Lattner6f72da52009-02-13 20:00:20 +0000329<h3 id="__builtin_shufflevector">__builtin_shufflevector</h3>
Chris Lattner5ce933f2009-02-09 08:46:11 +0000330<!-- ======================================================================= -->
331
Chris Lattneraad826b2009-09-16 18:56:12 +0000332<p><tt>__builtin_shufflevector</tt> is used to express generic vector
Chris Lattner6f72da52009-02-13 20:00:20 +0000333permutation/shuffle/swizzle operations. This builtin is also very important for
334the implementation of various target-specific header files like
335<tt>&lt;xmmintrin.h&gt;</tt>.
Chris Lattner5ce933f2009-02-09 08:46:11 +0000336</p>
337
338<p><b>Syntax:</b></p>
339
340<pre>
Chris Lattner6f72da52009-02-13 20:00:20 +0000341__builtin_shufflevector(vec1, vec2, index1, index2, ...)
Chris Lattner5ce933f2009-02-09 08:46:11 +0000342</pre>
343
344<p><b>Examples:</b></p>
345
346<pre>
Chris Lattner6f72da52009-02-13 20:00:20 +0000347 // Identity operation - return 4-element vector V1.
348 __builtin_shufflevector(V1, V1, 0, 1, 2, 3)
349
350 // "Splat" element 0 of V1 into a 4-element result.
351 __builtin_shufflevector(V1, V1, 0, 0, 0, 0)
352
353 // Reverse 4-element vector V1.
354 __builtin_shufflevector(V1, V1, 3, 2, 1, 0)
355
356 // Concatenate every other element of 4-element vectors V1 and V2.
357 __builtin_shufflevector(V1, V2, 0, 2, 4, 6)
358
359 // Concatenate every other element of 8-element vectors V1 and V2.
360 __builtin_shufflevector(V1, V2, 0, 2, 4, 6, 8, 10, 12, 14)
Chris Lattner5ce933f2009-02-09 08:46:11 +0000361</pre>
362
363<p><b>Description:</b></p>
364
Chris Lattner6f72da52009-02-13 20:00:20 +0000365<p>The first two arguments to __builtin_shufflevector are vectors that have the
366same element type. The remaining arguments are a list of integers that specify
367the elements indices of the first two vectors that should be extracted and
368returned in a new vector. These element indices are numbered sequentially
369starting with the first vector, continuing into the second vector. Thus, if
370vec1 is a 4-element vector, index 5 would refer to the second element of vec2.
Chris Lattner5ce933f2009-02-09 08:46:11 +0000371</p>
372
Chris Lattner6f72da52009-02-13 20:00:20 +0000373<p>The result of __builtin_shufflevector is a vector
374with the same element type as vec1/vec2 but that has an element count equal to
375the number of indices specified.
376</p>
Chris Lattner5ce933f2009-02-09 08:46:11 +0000377
Chris Lattner21190d52009-09-21 03:09:59 +0000378<p>Query for this feature with __has_builtin(__builtin_shufflevector).</p>
379
380<!-- ======================================================================= -->
381<h3 id="__builtin_unreachable">__builtin_unreachable</h3>
382<!-- ======================================================================= -->
383
384<p><tt>__builtin_unreachable</tt> is used to indicate that a specific point in
385the program cannot be reached, even if the compiler might otherwise think it
386can. This is useful to improve optimization and eliminates certain warnings.
387For example, without the <tt>__builtin_unreachable</tt> in the example below,
388the compiler assumes that the inline asm can fall through and prints a "function
389declared 'noreturn' should not return" warning.
390</p>
391
392<p><b>Syntax:</b></p>
393
394<pre>
395__builtin_unreachable()
396</pre>
397
398<p><b>Example of Use:</b></p>
399
400<pre>
401void myabort(void) __attribute__((noreturn));
402void myabort(void) {
403 asm("int3");
404 __builtin_unreachable();
405}
406</pre>
407
408<p><b>Description:</b></p>
409
410<p>The __builtin_unreachable() builtin has completely undefined behavior. Since
411it has undefined behavior, it is a statement that it is never reached and the
412optimizer can take advantage of this to produce better code. This builtin takes
413no arguments and produces a void result.
414</p>
415
416<p>Query for this feature with __has_builtin(__builtin_unreachable).</p>
417
418
Chris Lattner1177f912009-04-09 19:58:15 +0000419<!-- ======================================================================= -->
420<h2 id="targetspecific">Target-Specific Extensions</h2>
421<!-- ======================================================================= -->
422
423<p>Clang supports some language features conditionally on some targets.</p>
424
425<!-- ======================================================================= -->
426<h3 id="x86-specific">X86/X86-64 Language Extensions</h3>
427<!-- ======================================================================= -->
428
429<p>The X86 backend has these language extensions:</p>
430
431<!-- ======================================================================= -->
432<h4 id="x86-gs-segment">Memory references off the GS segment</h4>
433<!-- ======================================================================= -->
434
435<p>Annotating a pointer with address space #256 causes it to be code generated
Chris Lattnera021e7c2009-05-05 18:54:47 +0000436relative to the X86 GS segment register, and address space #257 causes it to be
437relative to the X86 FS segment. Note that this is a very very low-level
438feature that should only be used if you know what you're doing (for example in
439an OS kernel).</p>
Chris Lattner1177f912009-04-09 19:58:15 +0000440
441<p>Here is an example:</p>
442
443<pre>
444#define GS_RELATIVE __attribute__((address_space(256)))
445int foo(int GS_RELATIVE *P) {
446 return *P;
447}
448</pre>
449
450<p>Which compiles to (on X86-32):</p>
451
452<pre>
453_foo:
454 movl 4(%esp), %eax
455 movl %gs:(%eax), %eax
456 ret
457</pre>
458
Ted Kremeneked869312009-04-10 05:03:33 +0000459<!-- ======================================================================= -->
460<h2 id="analyzerspecific">Static Analysis-Specific Extensions</h2>
461<!-- ======================================================================= -->
462
463<p>Clang supports additional attributes that are useful for documenting program
464invariants and rules for static analysis tools. The extensions documented here
465are used by the <a
466href="http://clang.llvm.org/StaticAnalysis.html">path-sensitive static analyzer
467engine</a> that is part of Clang's Analysis library.</p>
468
469<!-- ======================================================================= -->
470<h3 id="analyzerattributes">Analyzer Attributes</h3>
471<!-- ======================================================================= -->
472
473<h4 id="attr_analyzer_noreturn"><tt>analyzer_noreturn</tt></h4>
474
475<p>Clang's static analysis engine understands the standard <tt>noreturn</tt>
Ted Kremenek4df21142009-04-10 05:04:22 +0000476attribute. This attribute, which is typically affixed to a function prototype,
477indicates that a call to a given function never returns. Function prototypes for
478common functions like <tt>exit</tt> are typically annotated with this attribute,
479as well as a variety of common assertion handlers. Users can educate the static
480analyzer about their own custom assertion handles (thus cutting down on false
481positives due to false paths) by marking their own &quot;panic&quot; functions
482with this attribute.</p>
Ted Kremeneked869312009-04-10 05:03:33 +0000483
484<p>While useful, <tt>noreturn</tt> is not applicable in all cases. Sometimes
Nick Lewycky625b5862009-06-14 04:08:08 +0000485there are special functions that for all intents and purposes should be
486considered panic functions (i.e., they are only called when an internal program
487error occurs) but may actually return so that the program can fail gracefully.
488The <tt>analyzer_noreturn</tt> attribute allows one to annotate such functions
489as being interpreted as &quot;no return&quot; functions by the analyzer (thus
Chris Lattner28935892009-04-10 05:54:56 +0000490pruning bogus paths) but will not affect compilation (as in the case of
Ted Kremeneked869312009-04-10 05:03:33 +0000491<tt>noreturn</tt>).</p>
492
493<p><b>Usage</b>: The <tt>analyzer_noreturn</tt> attribute can be placed in the
Chris Lattner28935892009-04-10 05:54:56 +0000494same places where the <tt>noreturn</tt> attribute can be placed. It is commonly
Ted Kremeneked869312009-04-10 05:03:33 +0000495placed at the end of function prototypes:</p>
496
497<pre>
498 void foo() <b>__attribute__((analyzer_noreturn))</b>;
Chris Lattner148772a2009-06-13 07:13:28 +0000499</pre>
500
501<p>Query for this feature with __has_feature(attribute_analyzer_noreturn).</p>
502
Ted Kremeneked869312009-04-10 05:03:33 +0000503
Chris Lattner5ce933f2009-02-09 08:46:11 +0000504</div>
505</body>
506</html>