1 // RUN: %clang_analyze_cc1 -analyzer-checker=debug.DumpCFG -triple x86_64-apple-darwin12 -fheinous-gnu-extensions -std=c++11 -analyzer-config cfg-rich-constructors=false %s > %t 2>&1
2 // RUN: FileCheck --input-file=%t -check-prefixes=CHECK,WARNINGS %s
3 // RUN: %clang_analyze_cc1 -analyzer-checker=debug.DumpCFG -triple x86_64-apple-darwin12 -fheinous-gnu-extensions -std=c++11 -analyzer-config cfg-rich-constructors=true %s > %t 2>&1
4 // RUN: FileCheck --input-file=%t -check-prefixes=CHECK,ANALYZER %s
6 // This file tests how we construct two different flavors of the Clang CFG -
7 // the CFG used by the Sema analysis-based warnings and the CFG used by the
8 // static analyzer. The difference in the behavior is checked via FileCheck
9 // prefixes (WARNINGS and ANALYZER respectively). When introducing new analyzer
10 // flags, no new run lines should be added - just these flags would go to the
11 // respective line depending on where is it turned on and where is it turned
12 // off. Feel free to add tests that test only one of the CFG flavors if you're
13 // not sure how the other flavor is supposed to work in your case.
15 // CHECK-LABEL: void checkDeclStmts()
17 // CHECK-NEXT: Succs (1): B1
19 // CHECK-NEXT: 1: int i;
20 // CHECK-NEXT: 2: int j;
22 // CHECK-NEXT: 4: int k = 1;
23 // CHECK-NEXT: 5: int l;
25 // CHECK-NEXT: 7: int m = 2;
26 // WARNINGS-NEXT: (CXXConstructExpr, struct standalone)
27 // ANALYZER-NEXT: (CXXConstructExpr, [B1.9], struct standalone)
28 // CHECK-NEXT: 9: struct standalone myStandalone;
29 // WARNINGS-NEXT: (CXXConstructExpr, struct (unnamed struct at {{.*}}))
30 // ANALYZER-NEXT: (CXXConstructExpr, [B1.11], struct (unnamed struct at {{.*}}))
31 // CHECK-NEXT: 11: struct (unnamed struct at {{.*}}) myAnon;
32 // WARNINGS-NEXT: (CXXConstructExpr, struct named)
33 // ANALYZER-NEXT: (CXXConstructExpr, [B1.13], struct named)
34 // CHECK-NEXT: 13: struct named myNamed;
35 // CHECK-NEXT: Preds (1): B2
36 // CHECK-NEXT: Succs (1): B0
37 void checkDeclStmts() {
41 struct standalone
{ int x
, y
; };
42 struct standalone myStandalone
;
44 struct { int x
, y
; } myAnon
;
46 struct named
{ int x
, y
; } myNamed
;
48 static_assert(1, "abc");
51 // CHECK-LABEL: void F(EmptyE e)
53 // CHECK-NEXT: Succs (1): B1
56 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, enum EmptyE)
57 // CHECK-NEXT: 3: [B1.2] (ImplicitCastExpr, IntegralCast, int)
58 // CHECK-NEXT: T: switch [B1.3]
59 // CHECK-NEXT: Preds (1): B2
60 // CHECK-NEXT: Succs (1): B0
62 // CHECK-NEXT: Preds (1): B1
68 // CHECK-LABEL: void testBuiltinSize()
70 // CHECK-NEXT: Succs (1): B1
72 // CHECK-NEXT: 1: __builtin_object_size
73 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, BuiltinFnToFnPtr, unsigned long (*)(const void *, int) noexcept)
74 // CHECK-NEXT: 3: [B1.2](dummy(), 0)
75 // CHECK-NEXT: 4: (void)[B1.3] (CStyleCastExpr, ToVoid, void)
76 // CHECK-NEXT: Preds (1): B2
77 // CHECK-NEXT: Succs (1): B0
79 // CHECK-NEXT: Preds (1): B1
80 void testBuiltinSize() {
82 (void)__builtin_object_size(dummy(), 0);
91 // CHECK-LABEL: void test_deletedtor()
92 // CHECK: [B2 (ENTRY)]
93 // CHECK-NEXT: Succs (1): B1
95 // CHECK-NEXT: 1: CFGNewAllocator(A *)
96 // WARNINGS-NEXT: 2: (CXXConstructExpr, class A)
97 // ANALYZER-NEXT: 2: (CXXConstructExpr, [B1.3], class A)
98 // CHECK-NEXT: 3: new A([B1.2])
99 // CHECK-NEXT: 4: A *a = new A();
101 // CHECK-NEXT: 6: [B1.5] (ImplicitCastExpr, LValueToRValue, class A *)
102 // CHECK-NEXT: 7: [B1.6]->~A() (Implicit destructor)
103 // CHECK-NEXT: 8: delete [B1.6]
104 // CHECK-NEXT: Preds (1): B2
105 // CHECK-NEXT: Succs (1): B0
106 // CHECK: [B0 (EXIT)]
107 // CHECK-NEXT: Preds (1): B1
108 void test_deletedtor() {
113 // CHECK-LABEL: void test_deleteArraydtor()
114 // CHECK: [B2 (ENTRY)]
115 // CHECK-NEXT: Succs (1): B1
118 // CHECK-NEXT: 2: CFGNewAllocator(A *)
119 // WARNINGS-NEXT: 3: (CXXConstructExpr, class A[5])
120 // ANALYZER-NEXT: 3: (CXXConstructExpr, [B1.4], class A[5])
121 // CHECK-NEXT: 4: new A {{\[\[}}B1.1]]
122 // CHECK-NEXT: 5: A *a = new A [5];
124 // CHECK-NEXT: 7: [B1.6] (ImplicitCastExpr, LValueToRValue, class A *)
125 // CHECK-NEXT: 8: [B1.7]->~A() (Implicit destructor)
126 // CHECK-NEXT: 9: delete [] [B1.7]
127 // CHECK-NEXT: Preds (1): B2
128 // CHECK-NEXT: Succs (1): B0
129 // CHECK: [B0 (EXIT)]
130 // CHECK-NEXT: Preds (1): B1
131 void test_deleteArraydtor() {
137 namespace NoReturnSingleSuccessor
{
143 struct B
: public A
{
145 ~B() __attribute__((noreturn
));
148 // CHECK-LABEL: int test1(int *x)
150 // CHECK-NEXT: 2: return
151 // CHECK-NEXT: ~NoReturnSingleSuccessor::B() (Implicit destructor)
152 // CHECK-NEXT: Preds (1)
153 // CHECK-NEXT: Succs (1): B0
160 // CHECK-LABEL: int test2(int *x)
162 // CHECK-NEXT: 2: return
163 // CHECK-NEXT: destructor
164 // CHECK-NEXT: Preds (1)
165 // CHECK-NEXT: Succs (1): B0
173 // Test CFG support for "extending" an enum.
174 // CHECK-LABEL: int test_enum_with_extension(enum MyEnum value)
175 // CHECK: [B7 (ENTRY)]
176 // CHECK-NEXT: Succs (1): B2
179 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
180 // CHECK-NEXT: 3: return [B1.2];
181 // CHECK-NEXT: Preds (5): B3 B4 B5 B6 B2(Unreachable)
182 // CHECK-NEXT: Succs (1): B0
185 // CHECK-NEXT: 2: int x = 0;
186 // CHECK-NEXT: 3: value
187 // CHECK-NEXT: 4: [B2.3] (ImplicitCastExpr, LValueToRValue, enum MyEnum)
188 // CHECK-NEXT: 5: [B2.4] (ImplicitCastExpr, IntegralCast, int)
189 // CHECK-NEXT: T: switch [B2.5]
190 // CHECK-NEXT: Preds (1): B7
191 // CHECK-NEXT: Succs (5): B3 B4 B5 B6 B1(Unreachable)
193 // CHECK-NEXT: case D:
196 // CHECK-NEXT: 3: [B3.2] = [B3.1]
197 // CHECK-NEXT: T: break;
198 // CHECK-NEXT: Preds (1): B2
199 // CHECK-NEXT: Succs (1): B1
201 // CHECK-NEXT: case C:
204 // CHECK-NEXT: 3: [B4.2] = [B4.1]
205 // CHECK-NEXT: T: break;
206 // CHECK-NEXT: Preds (1): B2
207 // CHECK-NEXT: Succs (1): B1
209 // CHECK-NEXT: case B:
212 // CHECK-NEXT: 3: [B5.2] = [B5.1]
213 // CHECK-NEXT: T: break;
214 // CHECK-NEXT: Preds (1): B2
215 // CHECK-NEXT: Succs (1): B1
217 // CHECK-NEXT: case A:
220 // CHECK-NEXT: 3: [B6.2] = [B6.1]
221 // CHECK-NEXT: T: break;
222 // CHECK-NEXT: Preds (1): B2
223 // CHECK-NEXT: Succs (1): B1
224 // CHECK: [B0 (EXIT)]
225 // CHECK-NEXT: Preds (1): B1
226 enum MyEnum
{ A
, B
, C
};
227 static const enum MyEnum D
= (enum MyEnum
) 32;
229 int test_enum_with_extension(enum MyEnum value
) {
232 case A
: x
= 1; break;
233 case B
: x
= 2; break;
234 case C
: x
= 3; break;
235 case D
: x
= 4; break;
240 // CHECK-LABEL: int test_enum_with_extension_default(enum MyEnum value)
241 // CHECK: [B7 (ENTRY)]
242 // CHECK-NEXT: Succs (1): B2
245 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
246 // CHECK-NEXT: 3: return [B1.2];
247 // CHECK-NEXT: Preds (4): B3 B4 B5 B6
248 // CHECK-NEXT: Succs (1): B0
251 // CHECK-NEXT: 2: int x = 0;
252 // CHECK-NEXT: 3: value
253 // CHECK-NEXT: 4: [B2.3] (ImplicitCastExpr, LValueToRValue, enum MyEnum)
254 // CHECK-NEXT: 5: [B2.4] (ImplicitCastExpr, IntegralCast, int)
255 // CHECK-NEXT: T: switch [B2.5]
256 // CHECK-NEXT: Preds (1): B7
257 // CHECK-NEXT: Succs (4): B4 B5 B6 B3(Unreachable)
259 // CHECK-NEXT: default:
262 // CHECK-NEXT: 3: [B3.2] = [B3.1]
263 // CHECK-NEXT: T: break;
264 // CHECK-NEXT: Preds (1): B2(Unreachable)
265 // CHECK-NEXT: Succs (1): B1
267 // CHECK-NEXT: case C:
270 // CHECK-NEXT: 3: [B4.2] = [B4.1]
271 // CHECK-NEXT: T: break;
272 // CHECK-NEXT: Preds (1): B2
273 // CHECK-NEXT: Succs (1): B1
275 // CHECK-NEXT: case B:
278 // CHECK-NEXT: 3: [B5.2] = [B5.1]
279 // CHECK-NEXT: T: break;
280 // CHECK-NEXT: Preds (1): B2
281 // CHECK-NEXT: Succs (1): B1
283 // CHECK-NEXT: case A:
286 // CHECK-NEXT: 3: [B6.2] = [B6.1]
287 // CHECK-NEXT: T: break;
288 // CHECK-NEXT: Preds (1): B2
289 // CHECK-NEXT: Succs (1): B1
290 // CHECK: [B0 (EXIT)]
291 // CHECK-NEXT: Preds (1): B1
292 int test_enum_with_extension_default(enum MyEnum value
) {
295 case A
: x
= 1; break;
296 case B
: x
= 2; break;
297 case C
: x
= 3; break;
298 default: x
= 4; break;
303 // CHECK-LABEL: void test_placement_new()
304 // CHECK: [B2 (ENTRY)]
305 // CHECK-NEXT: Succs (1): B1
307 // CHECK-NEXT: 1: int buffer[16];
308 // CHECK-NEXT: 2: buffer
309 // CHECK-NEXT: 3: [B1.2] (ImplicitCastExpr, ArrayToPointerDecay, int *)
310 // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, BitCast, void *)
311 // CHECK-NEXT: 5: CFGNewAllocator(MyClass *)
312 // WARNINGS-NEXT: 6: (CXXConstructExpr, class MyClass)
313 // ANALYZER-NEXT: 6: (CXXConstructExpr, [B1.7], class MyClass)
314 // CHECK-NEXT: 7: new ([B1.4]) MyClass([B1.6])
315 // CHECK-NEXT: 8: MyClass *obj = new (buffer) MyClass();
316 // CHECK-NEXT: Preds (1): B2
317 // CHECK-NEXT: Succs (1): B0
318 // CHECK: [B0 (EXIT)]
319 // CHECK-NEXT: Preds (1): B1
321 extern void* operator new (unsigned long sz
, void* v
);
322 extern void* operator new[] (unsigned long sz
, void* ptr
);
330 void test_placement_new() {
332 MyClass
* obj
= new (buffer
) MyClass();
335 // CHECK-LABEL: void test_placement_new_array()
336 // CHECK: [B2 (ENTRY)]
337 // CHECK-NEXT: Succs (1): B1
339 // CHECK-NEXT: 1: int buffer[16];
340 // CHECK-NEXT: 2: buffer
341 // CHECK-NEXT: 3: [B1.2] (ImplicitCastExpr, ArrayToPointerDecay, int *)
342 // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, BitCast, void *)
344 // CHECK-NEXT: 6: CFGNewAllocator(MyClass *)
345 // WARNINGS-NEXT: 7: (CXXConstructExpr, class MyClass[5])
346 // ANALYZER-NEXT: 7: (CXXConstructExpr, [B1.8], class MyClass[5])
347 // CHECK-NEXT: 8: new ([B1.4]) MyClass {{\[\[}}B1.5]]
348 // CHECK-NEXT: 9: MyClass *obj = new (buffer) MyClass [5];
349 // CHECK-NEXT: Preds (1): B2
350 // CHECK-NEXT: Succs (1): B0
351 // CHECK: [B0 (EXIT)]
352 // CHECK-NEXT: Preds (1): B1
354 void test_placement_new_array() {
356 MyClass
* obj
= new (buffer
) MyClass
[5];
360 // CHECK-LABEL: void test_lifetime_extended_temporaries()
362 struct LifetimeExtend
{ LifetimeExtend(int); ~LifetimeExtend(); };
363 struct Aggregate
{ const LifetimeExtend a
; const LifetimeExtend b
; };
364 struct AggregateRef
{ const LifetimeExtend
&a
; const LifetimeExtend
&b
; };
365 void test_lifetime_extended_temporaries() {
366 // CHECK: LifetimeExtend(1);
368 // CHECK-NEXT: ~LifetimeExtend()
369 // CHECK-NOT: ~LifetimeExtend()
371 const LifetimeExtend
&l
= LifetimeExtend(1);
374 // CHECK: LifetimeExtend(2)
375 // CHECK-NEXT: ~LifetimeExtend()
377 // CHECK-NOT: ~LifetimeExtend()
379 // No life-time extension.
380 const int &l
= (LifetimeExtend(2), 2);
383 // CHECK: LifetimeExtend(3)
385 // CHECK-NEXT: ~LifetimeExtend()
386 // CHECK-NOT: ~LifetimeExtend()
388 // The last one is lifetime extended.
389 const LifetimeExtend
&l
= (3, LifetimeExtend(3));
392 // CHECK: LifetimeExtend(4)
393 // CHECK-NEXT: ~LifetimeExtend()
394 // CHECK-NEXT: ~LifetimeExtend()
396 // CHECK-NOT: ~LifetimeExtend()
398 Aggregate a
{LifetimeExtend(4), LifetimeExtend(4)};
401 // CHECK: LifetimeExtend(5)
403 // FIXME: We want to emit the destructors of the lifetime
404 // extended variables here.
405 // CHECK-NOT: ~LifetimeExtend()
407 AggregateRef a
{LifetimeExtend(5), LifetimeExtend(5)};
410 // FIXME: Add tests for lifetime extension via subobject
411 // references (LifetimeExtend().some_member).
415 // FIXME: The destructor for 'a' shouldn't be there because it's deleted
417 // CHECK-LABEL: void foo()
418 // CHECK: [B2 (ENTRY)]
419 // CHECK-NEXT: Succs (1): B1
421 // WARNINGS-NEXT: 1: (CXXConstructExpr, struct pr37688_deleted_union_destructor::A)
422 // ANALYZER-NEXT: 1: (CXXConstructExpr, [B1.2], struct pr37688_deleted_union_destructor::A)
423 // CHECK-NEXT: 2: pr37688_deleted_union_destructor::A a;
424 // CHECK-NEXT: 3: [B1.2].~pr37688_deleted_union_destructor::A() (Implicit destructor)
425 // CHECK-NEXT: Preds (1): B2
426 // CHECK-NEXT: Succs (1): B0
427 // CHECK: [B0 (EXIT)]
428 // CHECK-NEXT: Preds (1): B1
430 namespace pr37688_deleted_union_destructor
{
443 } // end namespace pr37688_deleted_union_destructor
446 namespace return_statement_expression
{
449 // CHECK-LABEL: int foo()
450 // CHECK: [B6 (ENTRY)]
451 // CHECK-NEXT: Succs (1): B5
454 // CHECK-NEXT: 2: return [B1.1];
455 // CHECK-NEXT: Preds (1): B5
456 // CHECK-NEXT: Succs (1): B0
459 // CHECK-NEXT: 2: ({ ... ; [B2.1] })
460 // CHECK-NEXT: 3: return [B2.2];
461 // CHECK-NEXT: Preds (1): B4
462 // CHECK-NEXT: Succs (1): B0
463 // FIXME: Why do we have [B3] at all?
465 // CHECK-NEXT: Succs (1): B4
468 // CHECK-NEXT: 2: [B4.1] (ImplicitCastExpr, IntegralToBoolean, _Bool)
469 // CHECK-NEXT: T: while [B4.2]
470 // CHECK-NEXT: Preds (2): B3 B5
471 // CHECK-NEXT: Succs (2): NULL B2
473 // CHECK-NEXT: 1: unknown
474 // CHECK-NEXT: 2: [B5.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void))
475 // CHECK-NEXT: 3: [B5.2]()
476 // CHECK-NEXT: 4: [B5.3] (ImplicitCastExpr, IntegralToBoolean, _Bool)
477 // CHECK-NEXT: T: if [B5.4]
478 // CHECK-NEXT: Preds (1): B6
479 // CHECK-NEXT: Succs (2): B4 B1
480 // CHECK: [B0 (EXIT)]
481 // CHECK-NEXT: Preds (2): B1 B2
492 } // namespace statement_expression_in_return
494 // CHECK-LABEL: void vla_simple(int x)
497 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
498 // CHECK-NEXT: 3: int vla[x];
499 void vla_simple(int x
) {
503 // CHECK-LABEL: void vla_typedef(int x)
506 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
507 // CHECK-NEXT: 3: typedef int VLA[x];
508 void vla_typedef(int x
) {
512 // CHECK-LABEL: void vla_typealias(int x)
515 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
516 // CHECK-NEXT: 3: using VLA = int[x];
517 void vla_typealias(int x
) {
521 // CHECK-LABEL: void vla_typedef_multi(int x, int y)
524 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
526 // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, LValueToRValue, int)
527 // CHECK-NEXT: 5: typedef int VLA[x][y];
528 void vla_typedef_multi(int x
, int y
) {
529 typedef int VLA
[x
][y
];
532 // CHECK-LABEL: void vla_typedefname_multi(int x, int y)
535 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
536 // CHECK-NEXT: 3: typedef int VLA[x];
538 // CHECK-NEXT: 5: [B1.4] (ImplicitCastExpr, LValueToRValue, int)
539 // CHECK-NEXT: 6: typedef VLA VLA1[y];
541 // CHECK-NEXT: 8: using VLA2 = VLA1[3];
543 // CHECK-NEXT: 10: VLA2 vla[4];
544 void vla_typedefname_multi(int x
, int y
) {
547 using VLA2
= VLA1
[3];
551 // CHECK-LABEL: int vla_evaluate(int x)
554 // CHECK-NEXT: 2: ++[B1.1]
555 // CHECK-NEXT: 3: [B1.2] (ImplicitCastExpr, LValueToRValue, int)
556 // CHECK-NEXT: 4: typedef int VLA[++x];
558 // CHECK-NEXT: 6: ++[B1.5]
559 // CHECK-NEXT: 7: [B1.6] (ImplicitCastExpr, LValueToRValue, int)
560 // CHECK-NEXT: 8: sizeof(int[++x])
561 // CHECK-NEXT: 9: alignof(int[++x])
564 // CHECK-NEXT: 12: [B1.11] (ImplicitCastExpr, LValueToRValue, int)
565 // CHECK-NEXT: 13: return [B1.12];
566 int vla_evaluate(int x
) {
568 typedef int VLA
[++x
];
571 // Do not evaluate the ++x
573 _Generic((int(*)[++x
])0, default : 0);
578 // CHECK-LABEL: void CommaTemp::f()
580 // CHECK-NEXT: 1: CommaTemp::A() (CXXConstructExpr,
581 // CHECK-NEXT: 2: [B1.1] (BindTemporary)
582 // CHECK-NEXT: 3: CommaTemp::B() (CXXConstructExpr,
583 // CHECK-NEXT: 4: [B1.3] (BindTemporary)
584 // CHECK-NEXT: 5: ... , [B1.4]
585 // CHECK-NEXT: 6: ~CommaTemp::B() (Temporary object destructor)
586 // CHECK-NEXT: 7: ~CommaTemp::A() (Temporary object destructor)
587 namespace CommaTemp
{
592 void CommaTemp::f() {
596 // CHECK-LABEL: template<> int *PR18472<int>()
597 // CHECK: [B2 (ENTRY)]
598 // CHECK-NEXT: Succs (1): B1
601 // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, NullToPointer, PR18472_t)
602 // CHECK-NEXT: 3: (PR18472_t)[B1.2] (CStyleCastExpr, NoOp, PR18472_t)
603 // CHECK-NEXT: 4: CFGNewAllocator(int *)
604 // CHECK-NEXT: 5: new (([B1.3])) int
605 // CHECK-NEXT: 6: return [B1.5];
606 // CHECK-NEXT: Preds (1): B2
607 // CHECK-NEXT: Succs (1): B0
608 // CHECK: [B0 (EXIT)]
609 // CHECK-NEXT: Preds (1): B1
611 extern "C" typedef int *PR18472_t
;
612 void *operator new (unsigned long, PR18472_t
);
613 template <class T
> T
*PR18472() {
614 return new (((PR18472_t
) 0)) T
;
616 void PR18472_helper() {