aboutsummaryrefslogtreecommitdiff
path: root/docs/LanguageExtensions.html
blob: 838b65f27b81da517358e1f1460a47b1ffdb84a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<html>
<head>
<title>Clang Language Extensions</title>
<link type="text/css" rel="stylesheet" href="../menu.css" />
<link type="text/css" rel="stylesheet" href="../content.css" />
<style type="text/css">
td {
	vertical-align: top;
}
</style>
</head>
<body>

<!--#include virtual="../menu.html.incl"-->

<div id="content">

<h1>Clang Language Extensions</h1>

<ul>
<li><a href="#intro">Introduction</a></li>
<li><a href="#feature_check">Feature Checking Macros</a></li>
<li><a href="#has_include">Include File Checking Macros</a></li>
<li><a href="#builtinmacros">Builtin Macros</a></li>
<li><a href="#vectors">Vectors and Extended Vectors</a></li>
<li><a href="#checking_language_features">Checks for Standard Language Features</a></li>
  <ul>
  <li><a href="#cxx_exceptions">C++ exceptions</a></li>
  <li><a href="#cxx_rtti">C++ RTTI</a></li>
  </ul>
<li><a href="#checking_upcoming_features">Checks for Upcoming Standard Language Features</a></li>
  <ul>
  <li><a href="#cxx_attributes">C++0x attributes</a></li>
  <li><a href="#cxx_decltype">C++0x <tt>decltype()</tt></a></li>
  <li><a href="#cxx_deleted_functions">C++0x deleted functions</a></li>
  <li><a href="#cxx_concepts">C++ TR concepts</a></li>
  <li><a href="#cxx_lambdas">C++0x lambdas</a></li>
  <li><a href="#cxx_nullptr">C++0x nullptr</a></li>
  <li><a href="#cxx_rvalue_references">C++0x rvalue references</a></li>
  <li><a href="#cxx_static_assert">C++0x <tt>static_assert()</tt></a></li>
  <li><a href="#cxx_auto_type">C++0x type inference</a></li>
  <li><a href="#cxx_variadic_templates">C++0x variadic templates</a></li>
  </ul>
<li><a href="#blocks">Blocks</a></li>
<li><a href="#overloading-in-c">Function Overloading in C</a></li>
<li><a href="#builtins">Builtin Functions</a>
  <ul>
  <li><a href="#__builtin_shufflevector">__builtin_shufflevector</a></li>
  <li><a href="#__builtin_unreachable">__builtin_unreachable</a></li>
  </ul>
</li>
<li><a href="#targetspecific">Target-Specific Extensions</a>
  <ul>
  <li><a href="#x86-specific">X86/X86-64 Language Extensions</a></li>
  </ul>
</li>
<li><a href="#analyzerspecific">Static Analysis-Specific Extensions</a>
  <ul>
    <li><a href="#analyzerattributes">Analyzer Attributes</a></li>
  </ul>
</li>
</ul>

<!-- ======================================================================= -->
<h2 id="intro">Introduction</h2>
<!-- ======================================================================= -->

<p>This document describes the language extensions provided by Clang.  In
addition to the language extensions listed here, Clang aims to support a broad
range of GCC extensions.  Please see the <a 
href="http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html">GCC manual</a> for
more information on these extensions.</p>

<!-- ======================================================================= -->
<h2 id="feature_check">Feature Checking Macros</h2>
<!-- ======================================================================= -->

<p>Language extensions can be very useful, but only if you know you can depend
on them.  In order to allow fine-grain features checks, we support two builtin
function-like macros.  This allows you to directly test for a feature in your
code without having to resort to something like autoconf or fragile "compiler
version checks".</p>

<!-- ======================================================================= -->
<h3 id="__has_builtin">__has_builtin</h3>
<!-- ======================================================================= -->

<p>This function-like macro takes a single identifier argument that is the name
of a builtin function.  It evaluates to 1 if the builtin is supported or 0 if
not.  It can be used like this:</p>

<blockquote>
<pre>
#ifndef __has_builtin         // Optional of course.
  #define __has_builtin(x) 0  // Compatibility with non-clang compilers.
#endif

...
#if __has_builtin(__builtin_trap)
  __builtin_trap();
#else
  abort();
#endif
...
</pre>
</blockquote>


<!-- ======================================================================= -->
<h3 id="__has_feature">__has_feature</h3>
<!-- ======================================================================= -->

<p>This function-like macro takes a single identifier argument that is the name
of a feature.  It evaluates to 1 if the feature is supported or 0 if not.  It
can be used like this:</p>

<blockquote>
<pre>
#ifndef __has_feature         // Optional of course.
  #define __has_feature(x) 0  // Compatibility with non-clang compilers.
#endif

...
#if __has_feature(attribute_overloadable) || \
    __has_feature(blocks)
...
#endif
...
</pre>
</blockquote>

<p>The feature tag is described along with the language feature below.</p>

<!-- ======================================================================= -->
<h2 id="has_include">Include File Checking Macros</h2>
<!-- ======================================================================= -->

<p>Not all developments systems have the same include files.
The <a href="#__has_include">__has_include</a> and
<a href="#__has_include_next">__has_include_next</a> macros allow you to
check for the existence of an include file before doing
a possibly failing #include directive.</p>

<!-- ======================================================================= -->
<h3 id="__has_include">__has_include</h3>
<!-- ======================================================================= -->

<p>This function-like macro takes a single file name string argument that
is the name of an include file.  It evaluates to 1 if the file can
be found using the include paths, or 0 otherwise:</p>

<blockquote>
<pre>
// Note the two possible file name string formats.
#if __has_include("myinclude.h") && __has_include(&lt;stdint.h&gt;)
# include "myinclude.h"
#endif

// To avoid problem with non-clang compilers not having this macro.
#if defined(__has_include) && __has_include("myinclude.h")
# include "myinclude.h"
#endif
</pre>
</blockquote>

<p>To test for this feature, use #if defined(__has_include).</p>

<!-- ======================================================================= -->
<h3 id="__has_include_next">__has_include_next</h3>
<!-- ======================================================================= -->

<p>This function-like macro takes a single file name string argument that
is the name of an include file.  It is like __has_include except that it
looks for the second instance of the given file found in the include
paths.  It evaluates to 1 if the second instance of the file can
be found using the include paths, or 0 otherwise:</p>

<blockquote>
<pre>
// Note the two possible file name string formats.
#if __has_include_next("myinclude.h") && __has_include_next(&lt;stdint.h&gt;)
# include_next "myinclude.h"
#endif

// To avoid problem with non-clang compilers not having this macro.
#if defined(__has_include_next) && __has_include_next("myinclude.h")
# include_next "myinclude.h"
#endif
</pre>
</blockquote>

<p>Note that __has_include_next, like the GNU extension
#include_next directive, is intended for use in headers only,
and will issue a warning if used in the top-level compilation
file.  A warning will also be issued if an absolute path
is used in the file argument.</p>

<!-- ======================================================================= -->
<h2 id="builtinmacros">Builtin Macros</h2>
<!-- ======================================================================= -->

<dl>
  <dt><code>__BASE_FILE__</code></dt>
  <dd>Defined to a string that contains the name of the main input
  file passed to Clang.</dd> 

  <dt><code>__COUNTER__</code></dt>
  <dd>Defined to an integer value that starts at zero and is
  incremented each time the <code>__COUNTER__</code> macro is
  expanded.</dd> 
    
  <dt><code>__INCLUDE_LEVEL__</code></dt>
  <dd>Defined to an integral value that is the include depth of the
  file currently being translated. For the main file, this value is
  zero.</dd> 

  <dt><code>__TIMESTAMP__</code></dt>
  <dd>Defined to the date and time of the last modification of the
  current source file.</dd> 
    
  <dt><code>__clang__</code></dt>
  <dd>Defined when compiling with Clang</dd>

  <dt><code>__clang_major__</code></dt>
  <dd>Defined to the major version number of Clang (e.g., the 2 in
  2.0.1).</dd> 

  <dt><code>__clang_minor__</code></dt>
  <dd>Defined to the minor version number of Clang (e.g., the 0 in
  2.0.1).</dd> 

  <dt><code>__clang_patchlevel__</code></dt>
  <dd>Defined to the patch level of Clang (e.g., the 1 in 2.0.1).</dd>

  <dt><code>__clang_version__</code></dt>
  <dd>Defined to a string that captures the Clang version, including
  the Subversion tag or revision number, e.g., "1.5 (trunk
  102332)".</dd> 
</dl>

<!-- ======================================================================= -->
<h2 id="vectors">Vectors and Extended Vectors</h2>
<!-- ======================================================================= -->

<p>Supports the GCC vector extensions, plus some stuff like V[1].</p>

<p>Also supports <tt>ext_vector</tt>, which additionally support for V.xyzw
syntax and other tidbits as seen in OpenCL. An example is:</p>

<blockquote>
<pre>
typedef float float4 <b>__attribute__((ext_vector_type(4)))</b>;
typedef float float2 <b>__attribute__((ext_vector_type(2)))</b>;

float4 foo(float2 a, float2 b) {
  float4 c;
  c.xz = a;
  c.yw = b;
  return c;
}
</blockquote>

<p>Query for this feature with __has_feature(attribute_ext_vector_type).</p>

<p>See also <a href="#__builtin_shufflevector">__builtin_shufflevector</a>.</p>

<!-- ======================================================================= -->
<h2 id="checking_language_features">Checks for Standard Language Features</h2>
<!-- ======================================================================= -->

<p>The <tt>__has_feature</tt> macro can be