blob: 9ac35e1dc2dc23d5d13e2c050a6e0f9bd9ca998b [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>
Chris Lattner81edc9f2009-04-13 02:45:46 +000023<li><a href="#builtinmacros">Builtin Macros</a></li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000024<li><a href="#vectors">Vectors and Extended Vectors</a></li>
25<li><a href="#blocks">Blocks</a></li>
Douglas Gregorcb54d432009-02-13 00:57:04 +000026<li><a href="#overloading-in-c">Function Overloading in C</a></li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000027<li><a href="#builtins">Builtin Functions</a>
28 <ul>
Chris Lattner5ce933f2009-02-09 08:46:11 +000029 <li><a href="#__builtin_shufflevector">__builtin_shufflevector</a></li>
Chris Lattner21190d52009-09-21 03:09:59 +000030 <li><a href="#__builtin_unreachable">__builtin_unreachable</a></li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000031 </ul>
32</li>
Chris Lattner1177f912009-04-09 19:58:15 +000033<li><a href="#targetspecific">Target-Specific Extensions</a>
34 <ul>
35 <li><a href="#x86-specific">X86/X86-64 Language Extensions</a></li>
36 </ul>
37</li>
Ted Kremeneked869312009-04-10 05:03:33 +000038<li><a href="#analyzerspecific">Static Analysis-Specific Extensions</a>
39 <ul>
40 <li><a href="#analyzerattributes">Analyzer Attributes</a></li>
41 </ul>
42</li>
Chris Lattner5ce933f2009-02-09 08:46:11 +000043</ul>
44
Chris Lattner5ce933f2009-02-09 08:46:11 +000045<!-- ======================================================================= -->
46<h2 id="intro">Introduction</h2>
47<!-- ======================================================================= -->
48
49<p>This document describes the language extensions provided by Clang. In
Chris Lattner148772a2009-06-13 07:13:28 +000050addition to the language extensions listed here, Clang aims to support a broad
Chris Lattner5ce933f2009-02-09 08:46:11 +000051range of GCC extensions. Please see the <a
52href="http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html">GCC manual</a> for
53more information on these extensions.</p>
54
55<!-- ======================================================================= -->
Chris Lattner148772a2009-06-13 07:13:28 +000056<h2 id="feature_check">Feature Checking Macros</h2>
57<!-- ======================================================================= -->
58
59<p>Language extensions can be very useful, but only if you know you can depend
60on them. In order to allow fine-grain features checks, we support two builtin
61function-like macros. This allows you to directly test for a feature in your
62code without having to resort to something like autoconf or fragile "compiler
63version checks".</p>
64
65<!-- ======================================================================= -->
66<h3 id="__has_builtin">__has_builtin</h3>
67<!-- ======================================================================= -->
68
69<p>This function-like macro takes a single identifier argument that is the name
70of a builtin function. It evaluates to 1 if the builtin is supported or 0 if
71not. It can be used like this:</p>
72
73<blockquote>
74<pre>
75#ifndef __has_builtin // Optional of course.
76 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
77#endif
78
79...
80#if __has_builtin(__builtin_trap)
81 __builtin_trap();
82#else
83 abort();
84#endif
85...
86</pre>
87</blockquote>
88
89
90<!-- ======================================================================= -->
91<h3 id="__has_feature">__has_feature</h3>
92<!-- ======================================================================= -->
93
94<p>This function-like macro takes a single identifier argument that is the name
95of a feature. It evaluates to 1 if the feature is supported or 0 if not. It
96can be used like this:</p>
97
98<blockquote>
99<pre>
100#ifndef __has_feature // Optional of course.
101 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
102#endif
103
104...
105#if __has_feature(attribute_overloadable) || \
106 __has_feature(blocks)
107...
108#endif
109...
110</pre>
111</blockquote>
112
113<p>The feature tag is described along with the language feature below.</p>
114
115
116<!-- ======================================================================= -->
Chris Lattner81edc9f2009-04-13 02:45:46 +0000117<h2 id="builtinmacros">Builtin Macros</h2>
118<!-- ======================================================================= -->
119
120<p>__BASE_FILE__, __INCLUDE_LEVEL__, __TIMESTAMP__, __COUNTER__</p>
121
122<!-- ======================================================================= -->
Chris Lattner5ce933f2009-02-09 08:46:11 +0000123<h2 id="vectors">Vectors and Extended Vectors</h2>
124<!-- ======================================================================= -->
125
126<p>Supports the GCC vector extensions, plus some stuff like V[1]. ext_vector
127with V.xyzw syntax and other tidbits. See also <a
128href="#__builtin_shufflevector">__builtin_shufflevector</a>.</p>
129
Chris Lattner148772a2009-06-13 07:13:28 +0000130<p>Query for this feature with __has_feature(attribute_ext_vector_type).</p>
131
Chris Lattner5ce933f2009-02-09 08:46:11 +0000132<!-- ======================================================================= -->
133<h2 id="blocks">Blocks</h2>
134<!-- ======================================================================= -->
135
Chris Lattnera7dbdf52009-03-09 07:03:22 +0000136<p>The syntax and high level language feature description is in <a
137href="BlockLanguageSpec.txt">BlockLanguageSpec.txt</a>. Implementation and ABI
138details for the clang implementation are in <a
139href="BlockImplementation.txt">BlockImplementation.txt</a>.</p>
Chris Lattner5ce933f2009-02-09 08:46:11 +0000140
Chris Lattner148772a2009-06-13 07:13:28 +0000141
142<p>Query for this feature with __has_feature(blocks).</p>
143
Chris Lattner5ce933f2009-02-09 08:46:11 +0000144<!-- ======================================================================= -->
Douglas Gregorcb54d432009-02-13 00:57:04 +0000145<h2 id="overloading-in-c">Function Overloading in C</h2>
146<!-- ======================================================================= -->
147
Chris Lattnerf161d412009-02-13 21:51:45 +0000148<p>Clang provides support for C++ function overloading in C. Function
149overloading in C is introduced using the <tt>overloadable</tt> attribute. For
150example, one might provide several overloaded versions of a <tt>tgsin</tt>
151function that invokes the appropriate standard function computing the sine of a
152value with <tt>float</tt>, <tt>double</tt>, or <tt>long double</tt>
153precision:</p>
Douglas Gregorcb54d432009-02-13 00:57:04 +0000154
155<blockquote>
156<pre>
157#include &lt;math.h&gt;
158float <b>__attribute__((overloadable))</b> tgsin(float x) { return sinf(x); }
159double <b>__attribute__((overloadable))</b> tgsin(double x) { return sin(x); }
160long double <b>__attribute__((overloadable))</b> tgsin(long double x) { return sinl(x); }
161</pre>
162</blockquote>
163
164<p>Given these declarations, one can call <tt>tgsin</tt> with a
165<tt>float</tt> value to receive a <tt>float</tt> result, with a
166<tt>double</tt> to receive a <tt>double</tt> result, etc. Function
167overloading in C follows the rules of C++ function overloading to pick
168the best overload given the call arguments, with a few C-specific
169semantics:</p>
170<ul>
171 <li>Conversion from <tt>float</tt> or <tt>double</tt> to <tt>long
172 double</tt> is ranked as a floating-point promotion (per C99) rather
173 than as a floating-point conversion (as in C++).</li>
174
175 <li>A conversion from a pointer of type <tt>T*</tt> to a pointer of type
176 <tt>U*</tt> is considered a pointer conversion (with conversion
177 rank) if <tt>T</tt> and <tt>U</tt> are compatible types.</li>
178
179 <li>A conversion from type <tt>T</tt> to a value of type <tt>U</tt>
180 is permitted if <tt>T</tt> and <tt>U</tt> are compatible types. This
181 conversion is given "conversion" rank.</li>
182</ul>
183
184<p>The declaration of <tt>overloadable</tt> functions is restricted to
185function declarations and definitions. Most importantly, if any
186function with a given name is given the <tt>overloadable</tt>
187attribute, then all function declarations and definitions with that
188name (and in that scope) must have the <tt>overloadable</tt>
Chris Lattnerf161d412009-02-13 21:51:45 +0000189attribute. This rule even applies to redeclarations of functions whose original
190declaration had the <tt>overloadable</tt> attribute, e.g.,</p>
Douglas Gregorcb54d432009-02-13 00:57:04 +0000191
192<blockquote>
193<pre>
194int f(int) __attribute__((overloadable));
195float f(float); <i>// error: declaration of "f" must have the "overloadable" attribute</i>
196
197int g(int) __attribute__((overloadable));
198int g(int) { } <i>// error: redeclaration of "g" must also have the "overloadable" attribute</i>
199</pre>
200</blockquote>
201
Douglas Gregor965acbb2009-02-18 07:07:28 +0000202<p>Functions marked <tt>overloadable</tt> must have
203prototypes. Therefore, the following code is ill-formed:</p>
204
205<blockquote>
206<pre>
207int h() __attribute__((overloadable)); <i>// error: h does not have a prototype</i>
208</pre>
209</blockquote>
210
211<p>However, <tt>overloadable</tt> functions are allowed to use a
212ellipsis 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>
213
214<blockquote>
215<pre>
Chris Lattner02246802009-02-18 22:27:46 +0000216void honeypot(...) __attribute__((overloadable, unavailable)); <i>// calling me is an error</i>
Douglas Gregor965acbb2009-02-18 07:07:28 +0000217</pre>
218</blockquote>
219
Douglas Gregorcb54d432009-02-13 00:57:04 +0000220<p>Functions declared with the <tt>overloadable</tt> attribute have
221their names mangled according to the same rules as C++ function
222names. For example, the three <tt>tgsin</tt> functions in our
223motivating example get the mangled names <tt>_Z5tgsinf</tt>,
224<tt>_Z5tgsind</tt>, and <tt>Z5tgsine</tt>, respectively. There are two
225caveats to this use of name mangling:</p>
226
227<ul>
228
229 <li>Future versions of Clang may change the name mangling of
230 functions overloaded in C, so you should not depend on an specific
231 mangling. To be completely safe, we strongly urge the use of
232 <tt>static inline</tt> with <tt>overloadable</tt> functions.</li>
233
234 <li>The <tt>overloadable</tt> attribute has almost no meaning when
235 used in C++, because names will already be mangled and functions are
236 already overloadable. However, when an <tt>overloadable</tt>
237 function occurs within an <tt>extern "C"</tt> linkage specification,
238 it's name <i>will</i> be mangled in the same way as it would in
239 C.</li>
240</ul>
241
Chris Lattner148772a2009-06-13 07:13:28 +0000242<p>Query for this feature with __has_feature(attribute_overloadable).</p>
243
244
Douglas Gregorcb54d432009-02-13 00:57:04 +0000245<!-- ======================================================================= -->
Chris Lattner5ce933f2009-02-09 08:46:11 +0000246<h2 id="builtins">Builtin Functions</h2>
247<!-- ======================================================================= -->
248
249<p>Clang supports a number of builtin library functions with the same syntax as
250GCC, including things like <tt>__builtin_nan</tt>,
251<tt>__builtin_constant_p</tt>, <tt>__builtin_choose_expr</tt>,
252<tt>__builtin_types_compatible_p</tt>, <tt>__sync_fetch_and_add</tt>, etc. In
253addition to the GCC builtins, Clang supports a number of builtins that GCC does
254not, which are listed here.</p>
255
256<p>Please note that Clang does not and will not support all of the GCC builtins
257for vector operations. Instead of using builtins, you should use the functions
258defined in target-specific header files like <tt>&lt;xmmintrin.h&gt;</tt>, which
259define portable wrappers for these. Many of the Clang versions of these
260functions are implemented directly in terms of <a href="#vectors">extended
261vector support</a> instead of builtins, in order to reduce the number of
262builtins that we need to implement.</p>
263
Chris Lattner5ce933f2009-02-09 08:46:11 +0000264<!-- ======================================================================= -->
Chris Lattner6f72da52009-02-13 20:00:20 +0000265<h3 id="__builtin_shufflevector">__builtin_shufflevector</h3>
Chris Lattner5ce933f2009-02-09 08:46:11 +0000266<!-- ======================================================================= -->
267
Chris Lattneraad826b2009-09-16 18:56:12 +0000268<p><tt>__builtin_shufflevector</tt> is used to express generic vector
Chris Lattner6f72da52009-02-13 20:00:20 +0000269permutation/shuffle/swizzle operations. This builtin is also very important for
270the implementation of various target-specific header files like
271<tt>&lt;xmmintrin.h&gt;</tt>.
Chris Lattner5ce933f2009-02-09 08:46:11 +0000272</p>
273
274<p><b>Syntax:</b></p>
275
276<pre>
Chris Lattner6f72da52009-02-13 20:00:20 +0000277__builtin_shufflevector(vec1, vec2, index1, index2, ...)
Chris Lattner5ce933f2009-02-09 08:46:11 +0000278</pre>
279
280<p><b>Examples:</b></p>
281
282<pre>
Chris Lattner6f72da52009-02-13 20:00:20 +0000283 // Identity operation - return 4-element vector V1.
284 __builtin_shufflevector(V1, V1, 0, 1, 2, 3)
285
286 // "Splat" element 0 of V1 into a 4-element result.
287 __builtin_shufflevector(V1, V1, 0, 0, 0, 0)
288
289 // Reverse 4-element vector V1.
290 __builtin_shufflevector(V1, V1, 3, 2, 1, 0)
291
292 // Concatenate every other element of 4-element vectors V1 and V2.
293 __builtin_shufflevector(V1, V2, 0, 2, 4, 6)
294
295 // Concatenate every other element of 8-element vectors V1 and V2.
296 __builtin_shufflevector(V1, V2, 0, 2, 4, 6, 8, 10, 12, 14)
Chris Lattner5ce933f2009-02-09 08:46:11 +0000297</pre>
298
299<p><b>Description:</b></p>
300
Chris Lattner6f72da52009-02-13 20:00:20 +0000301<p>The first two arguments to __builtin_shufflevector are vectors that have the
302same element type. The remaining arguments are a list of integers that specify
303the elements indices of the first two vectors that should be extracted and
304returned in a new vector. These element indices are numbered sequentially
305starting with the first vector, continuing into the second vector. Thus, if
306vec1 is a 4-element vector, index 5 would refer to the second element of vec2.
Chris Lattner5ce933f2009-02-09 08:46:11 +0000307</p>
308
Chris Lattner6f72da52009-02-13 20:00:20 +0000309<p>The result of __builtin_shufflevector is a vector
310with the same element type as vec1/vec2 but that has an element count equal to
311the number of indices specified.
312</p>
Chris Lattner5ce933f2009-02-09 08:46:11 +0000313
Chris Lattner21190d52009-09-21 03:09:59 +0000314<p>Query for this feature with __has_builtin(__builtin_shufflevector).</p>
315
316<!-- ======================================================================= -->
317<h3 id="__builtin_unreachable">__builtin_unreachable</h3>
318<!-- ======================================================================= -->
319
320<p><tt>__builtin_unreachable</tt> is used to indicate that a specific point in
321the program cannot be reached, even if the compiler might otherwise think it
322can. This is useful to improve optimization and eliminates certain warnings.
323For example, without the <tt>__builtin_unreachable</tt> in the example below,
324the compiler assumes that the inline asm can fall through and prints a "function
325declared 'noreturn' should not return" warning.
326</p>
327
328<p><b>Syntax:</b></p>
329
330<pre>
331__builtin_unreachable()
332</pre>
333
334<p><b>Example of Use:</b></p>
335
336<pre>
337void myabort(void) __attribute__((noreturn));
338void myabort(void) {
339 asm("int3");
340 __builtin_unreachable();
341}
342</pre>
343
344<p><b>Description:</b></p>
345
346<p>The __builtin_unreachable() builtin has completely undefined behavior. Since
347it has undefined behavior, it is a statement that it is never reached and the
348optimizer can take advantage of this to produce better code. This builtin takes
349no arguments and produces a void result.
350</p>
351
352<p>Query for this feature with __has_builtin(__builtin_unreachable).</p>
353
354
Chris Lattner1177f912009-04-09 19:58:15 +0000355<!-- ======================================================================= -->
356<h2 id="targetspecific">Target-Specific Extensions</h2>
357<!-- ======================================================================= -->
358
359<p>Clang supports some language features conditionally on some targets.</p>
360
361<!-- ======================================================================= -->
362<h3 id="x86-specific">X86/X86-64 Language Extensions</h3>
363<!-- ======================================================================= -->
364
365<p>The X86 backend has these language extensions:</p>
366
367<!-- ======================================================================= -->
368<h4 id="x86-gs-segment">Memory references off the GS segment</h4>
369<!-- ======================================================================= -->
370
371<p>Annotating a pointer with address space #256 causes it to be code generated
Chris Lattnera021e7c2009-05-05 18:54:47 +0000372relative to the X86 GS segment register, and address space #257 causes it to be
373relative to the X86 FS segment. Note that this is a very very low-level
374feature that should only be used if you know what you're doing (for example in
375an OS kernel).</p>
Chris Lattner1177f912009-04-09 19:58:15 +0000376
377<p>Here is an example:</p>
378
379<pre>
380#define GS_RELATIVE __attribute__((address_space(256)))
381int foo(int GS_RELATIVE *P) {
382 return *P;
383}
384</pre>
385
386<p>Which compiles to (on X86-32):</p>
387
388<pre>
389_foo:
390 movl 4(%esp), %eax
391 movl %gs:(%eax), %eax
392 ret
393</pre>
394
Ted Kremeneked869312009-04-10 05:03:33 +0000395<!-- ======================================================================= -->
396<h2 id="analyzerspecific">Static Analysis-Specific Extensions</h2>
397<!-- ======================================================================= -->
398
399<p>Clang supports additional attributes that are useful for documenting program
400invariants and rules for static analysis tools. The extensions documented here
401are used by the <a
402href="http://clang.llvm.org/StaticAnalysis.html">path-sensitive static analyzer
403engine</a> that is part of Clang's Analysis library.</p>
404
405<!-- ======================================================================= -->
406<h3 id="analyzerattributes">Analyzer Attributes</h3>
407<!-- ======================================================================= -->
408
409<h4 id="attr_analyzer_noreturn"><tt>analyzer_noreturn</tt></h4>
410
411<p>Clang's static analysis engine understands the standard <tt>noreturn</tt>
Ted Kremenek4df21142009-04-10 05:04:22 +0000412attribute. This attribute, which is typically affixed to a function prototype,
413indicates that a call to a given function never returns. Function prototypes for
414common functions like <tt>exit</tt> are typically annotated with this attribute,
415as well as a variety of common assertion handlers. Users can educate the static
416analyzer about their own custom assertion handles (thus cutting down on false
417positives due to false paths) by marking their own &quot;panic&quot; functions
418with this attribute.</p>
Ted Kremeneked869312009-04-10 05:03:33 +0000419
420<p>While useful, <tt>noreturn</tt> is not applicable in all cases. Sometimes
Nick Lewycky625b5862009-06-14 04:08:08 +0000421there are special functions that for all intents and purposes should be
422considered panic functions (i.e., they are only called when an internal program
423error occurs) but may actually return so that the program can fail gracefully.
424The <tt>analyzer_noreturn</tt> attribute allows one to annotate such functions
425as being interpreted as &quot;no return&quot; functions by the analyzer (thus
Chris Lattner28935892009-04-10 05:54:56 +0000426pruning bogus paths) but will not affect compilation (as in the case of
Ted Kremeneked869312009-04-10 05:03:33 +0000427<tt>noreturn</tt>).</p>
428
429<p><b>Usage</b>: The <tt>analyzer_noreturn</tt> attribute can be placed in the
Chris Lattner28935892009-04-10 05:54:56 +0000430same places where the <tt>noreturn</tt> attribute can be placed. It is commonly
Ted Kremeneked869312009-04-10 05:03:33 +0000431placed at the end of function prototypes:</p>
432
433<pre>
434 void foo() <b>__attribute__((analyzer_noreturn))</b>;
Chris Lattner148772a2009-06-13 07:13:28 +0000435</pre>
436
437<p>Query for this feature with __has_feature(attribute_analyzer_noreturn).</p>
438
Ted Kremeneked869312009-04-10 05:03:33 +0000439
Chris Lattner5ce933f2009-02-09 08:46:11 +0000440</div>
441</body>
442</html>