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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Language Compatibility</title>
<link type="text/css" rel="stylesheet" href="menu.css" />
<link type="text/css" rel="stylesheet" href="content.css" />
<style type="text/css">
</style>
</head>
<body>
<!--#include virtual="menu.html.incl"-->
<div id="content">
<!-- ======================================================================= -->
<h1>Language Compatibility</h1>
<!-- ======================================================================= -->
<p>Clang strives to both conform to current language standards (C99,
C++98) and also to implement many widely-used extensions available
in other compilers, so that most correct code will "just work" when
compiler with Clang. However, Clang is more strict than other
popular compilers, and may reject incorrect code that other
compilers allow. This page documents common compatibility and
portability issues with Clang to help you understand and fix the
problem in your code when Clang emits an error message.</p>
<ul>
<li><a href="#c">C compatibility</a>
<ul>
<li><a href="#inline">C99 inline functions</a></li>
<li><a href="#vector_builtins">"missing" vector __builtin functions</a></li>
<li><a href="#lvalue-cast">Lvalue casts</a></li>
<li><a href="#blocks-in-protected-scope">Jumps to within <tt>__block</tt> variable scope</a></li>
</ul>
</li>
<li><a href="#objective-c">Objective-C compatibility</a>
<ul>
<li><a href="#super-cast">Cast of super</a></li>
<li><a href="#sizeof-interface">Size of interfaces</a></li>
<li><a href="#objc_objs-cast">Internal Objective-C types</a></li>
<li><a href="#c_variables-class">C variables in @class or @protocol</a></li>
</ul>
</li>
<li><a href="#c++">C++ compatibility</a>
<ul>
<li><a href="#vla">Variable-length arrays</a></li>
<li><a href="#dep_lookup">Unqualified lookup in templates</a></li>
<li><a href="#dep_lookup_bases">Unqualified lookup into dependent bases of class templates</a></li>
<li><a href="#undep_incomplete">Incomplete types in templates</a></li>
<li><a href="#bad_templates">Templates with no valid instantiations</a></li>
<li><a href="#default_init_const">Default initialization of const
variable of a class type requires user-defined default
constructor</a></li>
</ul>
</li>
<li><a href="#objective-c++">Objective-C++ compatibility</a>
<ul>
<li><a href="#implicit-downcasts">Implicit downcasts</a></li>
</ul>
<ul>
<li><a href="#Use of class as method name">Use of class as method name</a></li>
</ul>
</li>
</ul>
<!-- ======================================================================= -->
<h2 id="c">C compatibility</h3>
<!-- ======================================================================= -->
<!-- ======================================================================= -->
<h3 id="inline">C99 inline functions</h3>
<!-- ======================================================================= -->
<p>By default, Clang builds C code according to the C99 standard,
which provides different inlining semantics than GCC's default
behavior. For example, when compiling the following code with no optimization:</p>
<pre>
inline int add(int i, int j) { return i + j; }
int main() {
int i = add(4, 5);
return i;
}
</pre>
<p>In C99, this is an incomplete (incorrect) program because there is
no external definition of the <code>add</code> function: the inline
definition is only used for optimization, if the compiler decides to
perform inlining. Therefore, we will get a (correct) link-time error
with Clang, e.g.:</p>
<pre>
Undefined symbols:
"_add", referenced from:
_main in cc-y1jXIr.o
</pre>
<p>There are several ways to fix this problem:</p>
<ul>
<li>Change <code>add</code> to a <code>static inline</code>
function. Static inline functions are always resolved within the
translation unit, so you won't have to add an external, non-inline
definition of the function elsewhere in your program.</li>
<li>Provide an external (non-inline) definition of <code>add</code>
somewhere in your program.</li>
<li>Compile with the GNU89 dialect by adding
<code>-std=gnu89</code> to the set of Clang options. This option is
only recommended if the program source cannot be changed or if the
program also relies on additional C89-specific behavior that cannot
be changed.</li>
</ul>
<!-- ======================================================================= -->
<h3 id="vector_builtins">"missing" vector __builtin functions</h3>
<!-- ======================================================================= -->
<p>The Intel and AMD manuals document a number "<tt><*mmintrin.h></tt>"
header files, which define a standardized API for accessing vector operations
on X86 CPUs. These functions have names like <tt>_mm_xor_ps</tt> and
<tt>_mm256_addsub_pd</tt>. Compilers have leeway to implement these functions
however they want. Since Clang supports an excellent set of <a
href="../docs/LanguageExtensions.html#vectors">native vector operations</a>,
the Clang headers implement these interfaces in terms of the native vector
operations.
</p>
<p>In contrast, GCC implements these functions mostly as a 1-to-1 mapping to
builtin function calls, like <tt>__builtin_ia32_paddw128</tt>. These builtin
functions are an internal implementation detail of GCC, and are not portable to
the Intel compiler, the Microsoft compiler, or Clang. If you get build errors
mentioning these, the fix is simple: switch to the *mmintrin.h functions.</p>
<p>The same issue occurs for NEON and Altivec for the ARM and PowerPC
architectures respectively. For these, make sure to use the <arm_neon.h>
and <altivec.h> headers.</p>
<!-- ======================================================================= -->
<h3 id="lvalue-cast">Lvalue casts</h3>
<!-- ======================================================================= -->
<p>Old versions of GCC permit casting the left-hand side of an assignment to a
different type. Clang produces an error on similar code, e.g.,</p>
<pre>
lvalue.c:2:3: error: assignment to cast is illegal, lvalue casts are not
supported
(int*)addr = val;
^~~~~~~~~~ ~
</pre>
<p>To fix this problem, move the cast to the right-hand side. In this
example, one could use:</p>
<pre>
addr = (float *)val;
</pre>
<!-- ======================================================================= -->
<h3 id="blocks-in-protected-scope">Jumps to within <tt>__block</tt> variable scope</h3>
<!-- ======================================================================= -->
<p>Clang disallows jumps into the scope of a <tt>__block</tt> variable, similar
to the manner in which both GCC and Clang disallow jumps into the scope of
variables which have user defined constructors (in C++).</p>
<p>Variables marked with <tt>__block</tt> require special runtime initialization
before they can be used. A jump into the scope of a <tt>__block</tt> variable
would bypass this initialization and therefore the variable cannot safely be
used.</p>
<p>For example, consider the following code fragment:</p>
<pre>
int f0(int c) {
if (c)
goto error;
__block int x;
x = 1;
return x;
error:
x = 0;
return x;
}
</pre>
<p>GCC accepts this code, but it will crash at runtime along the error path,
because the runtime setup for the storage backing the <tt>x</tt> variable will
not have been initialized. Clang rejects this code with a hard error:</p>
<pre>
t.c:3:5: error: goto into protected scope
goto error;
^
t.c:5:15: note: jump bypasses setup of __block variable
__block int x;
^
</pre>
<p>Some instances of this construct may be safe if the variable is never used
after the jump target, however the protected scope checker does not check the
uses of the variable, only the scopes in which it is visible. You should rewrite
your code to put the <tt>__block</tt> variables in a scope which is only visible
where they are used.</p>
<!-- ======================================================================= -->
<h2 id="objective-c">Objective-C compatibility</h3>
<!-- ======================================================================= -->
<!-- ======================================================================= -->
<h3 id="super-cast">Cast of super</h3>
<!-- ======================================================================= -->
<p>GCC treats the <code>super</code> identifier as an expression that
can, among other things, be cast to a different type. Clang treats
<code>super</code> as a context-sensitive keyword, and will reject a
type-cast of <code>super</code>:</p>
<pre>
super.m:11:12: error: cannot cast 'super' (it isn't an expression)
[(Super*)super add:4];
~~~~~~~~^
</pre>
<p>To fix this problem, remove the type cast, e.g.</p>
<pre>
[super add:4];
</pre>
<!-- ======================================================================= -->
<h3 id="sizeof-interface">Size of interfaces</h3>
<!-- ======================================================================= -->
<p>When using the "non-fragile" Objective-C ABI in use, the size of an
Objective-C class may change over time as instance variables are added
(or removed). For this reason, Clang rejects the application of the
<code>sizeof</code> operator to an Objective-C class when using this
ABI:</p>
<pre>
sizeof.m:4:14: error: invalid application of 'sizeof' to interface 'NSArray' in
non-fragile ABI
int size = sizeof(NSArray);
^ ~~~~~~~~~
</pre>
<p>Code that relies on the size of an Objective-C class is likely to
be broken anyway, since that size is not actually constant. To address
this problem, use the Objective-C runtime API function
<code>class_getInstanceSize()</code>:</p>
<pre>
class_getInstanceSize([NSArray class])
</pre>
<!-- ======================================================================= -->
<h3 id="objc_objs-cast">Internal Objective-C types</h3>
<!-- ======================================================================= -->
<p>GCC allows using pointers to internal Objective-C objects, <tt>struct objc_object*</tt>,
<tt>struct objc_selector*</tt>, and <tt>struct objc_class*</tt> in place of the types
<tt>id</tt>, <tt>SEL</tt>, and <tt>Class</tt> respectively. Clang treats the
internal Objective-C structures as implementation detail and won't do implicit conversions:
<pre>
t.mm:11:2: error: no matching function for call to 'f'
f((struct objc_object *)p);
^
t.mm:5:6: note: candidate function not viable: no known conversion from 'struct objc_object *' to 'id' for 1st argument
void f(id x);
^
</pre>
<p>Code s
|