xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/cfg.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=debug.DumpCFG -triple x86_64-apple-darwin12 -std=c++11 %s 2>&1 | FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // CHECK: ENTRY
4*f4a2713aSLionel Sambuc // CHECK-NEXT: Succs (1): B1
5*f4a2713aSLionel Sambuc // CHECK: [B1]
6*f4a2713aSLionel Sambuc // CHECK: Succs (21): B2 B3 B4 B5 B6 B7 B8 B9
7*f4a2713aSLionel Sambuc // CHECK: B10 B11 B12 B13 B14 B15 B16 B17 B18 B19
8*f4a2713aSLionel Sambuc // CHECK: B20 B21 B0
9*f4a2713aSLionel Sambuc // CHECK: [B0 (EXIT)]
10*f4a2713aSLionel Sambuc // CHECK-NEXT: Preds (21): B2 B3 B4 B5 B6 B7 B8 B9
11*f4a2713aSLionel Sambuc // CHECK-NEXT: B10 B11 B12 B13 B14 B15 B16 B17 B18 B19
12*f4a2713aSLionel Sambuc // CHECK-NEXT: B20 B21 B1
13*f4a2713aSLionel Sambuc void checkWrap(int i) {
14*f4a2713aSLionel Sambuc   switch(i) {
15*f4a2713aSLionel Sambuc     case 0: break;
16*f4a2713aSLionel Sambuc     case 1: break;
17*f4a2713aSLionel Sambuc     case 2: break;
18*f4a2713aSLionel Sambuc     case 3: break;
19*f4a2713aSLionel Sambuc     case 4: break;
20*f4a2713aSLionel Sambuc     case 5: break;
21*f4a2713aSLionel Sambuc     case 6: break;
22*f4a2713aSLionel Sambuc     case 7: break;
23*f4a2713aSLionel Sambuc     case 8: break;
24*f4a2713aSLionel Sambuc     case 9: break;
25*f4a2713aSLionel Sambuc     case 10: break;
26*f4a2713aSLionel Sambuc     case 11: break;
27*f4a2713aSLionel Sambuc     case 12: break;
28*f4a2713aSLionel Sambuc     case 13: break;
29*f4a2713aSLionel Sambuc     case 14: break;
30*f4a2713aSLionel Sambuc     case 15: break;
31*f4a2713aSLionel Sambuc     case 16: break;
32*f4a2713aSLionel Sambuc     case 17: break;
33*f4a2713aSLionel Sambuc     case 18: break;
34*f4a2713aSLionel Sambuc     case 19: break;
35*f4a2713aSLionel Sambuc   }
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc // CHECK: ENTRY
39*f4a2713aSLionel Sambuc // CHECK-NEXT: Succs (1): B1
40*f4a2713aSLionel Sambuc // CHECK: [B1]
41*f4a2713aSLionel Sambuc // CHECK-NEXT:   1: int i;
42*f4a2713aSLionel Sambuc // CHECK-NEXT:   2: int j;
43*f4a2713aSLionel Sambuc // CHECK-NEXT:   3: 1
44*f4a2713aSLionel Sambuc // CHECK-NEXT:   4: int k = 1;
45*f4a2713aSLionel Sambuc // CHECK-NEXT:   5: int l;
46*f4a2713aSLionel Sambuc // CHECK-NEXT:   6: 2
47*f4a2713aSLionel Sambuc // CHECK-NEXT:   7: int m = 2;
48*f4a2713aSLionel Sambuc // CHECK-NEXT: CXXConstructExpr
49*f4a2713aSLionel Sambuc // CHECK-NEXT:   9: struct standalone myStandalone;
50*f4a2713aSLionel Sambuc // CHECK-NEXT: CXXConstructExpr
51*f4a2713aSLionel Sambuc // CHECK-NEXT:  11: struct <anonymous struct at {{.*}}> myAnon;
52*f4a2713aSLionel Sambuc // CHECK-NEXT: CXXConstructExpr
53*f4a2713aSLionel Sambuc // CHECK-NEXT:  13: struct named myNamed;
54*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B2
55*f4a2713aSLionel Sambuc // CHECK-NEXT:   Succs (1): B0
56*f4a2713aSLionel Sambuc void checkDeclStmts() {
57*f4a2713aSLionel Sambuc   int i, j;
58*f4a2713aSLionel Sambuc   int k = 1, l, m = 2;
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   struct standalone { int x, y; };
61*f4a2713aSLionel Sambuc   struct standalone myStandalone;
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   struct { int x, y; } myAnon;
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc   struct named { int x, y; } myNamed;
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc   static_assert(1, "abc");
68*f4a2713aSLionel Sambuc }
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc // CHECK: ENTRY
71*f4a2713aSLionel Sambuc // CHECK-NEXT: Succs (1): B1
72*f4a2713aSLionel Sambuc // CHECK: [B1]
73*f4a2713aSLionel Sambuc // CHECK-NEXT:   1: e
74*f4a2713aSLionel Sambuc // CHECK-NEXT:   2: [B1.1] (ImplicitCastExpr, LValueToRValue, enum EmptyE)
75*f4a2713aSLionel Sambuc // CHECK-NEXT:   3: [B1.2] (ImplicitCastExpr, IntegralCast, int)
76*f4a2713aSLionel Sambuc // CHECK-NEXT:   T: switch [B1.3]
77*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B2
78*f4a2713aSLionel Sambuc // CHECK-NEXT:   Succs (1): B0
79*f4a2713aSLionel Sambuc // CHECK: [B0 (EXIT)]
80*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B1
81*f4a2713aSLionel Sambuc enum EmptyE {};
82*f4a2713aSLionel Sambuc void F(EmptyE e) {
83*f4a2713aSLionel Sambuc   switch (e) {}
84*f4a2713aSLionel Sambuc }
85*f4a2713aSLionel Sambuc 
86*f4a2713aSLionel Sambuc // CHECK: ENTRY
87*f4a2713aSLionel Sambuc // CHECK-NEXT: Succs (1): B1
88*f4a2713aSLionel Sambuc // CHECK: [B1]
89*f4a2713aSLionel Sambuc // CHECK-NEXT:   1: __builtin_object_size
90*f4a2713aSLionel Sambuc // CHECK-NEXT:   2: [B1.1] (ImplicitCastExpr, BuiltinFnToFnPtr, unsigned long (*)(const void *, int))
91*f4a2713aSLionel Sambuc // CHECK-NEXT:   3: [B1.2](dummy(), 0)
92*f4a2713aSLionel Sambuc // CHECK-NEXT:   4: (void)[B1.3] (CStyleCastExpr, ToVoid, void)
93*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B2
94*f4a2713aSLionel Sambuc // CHECK-NEXT:   Succs (1): B0
95*f4a2713aSLionel Sambuc // CHECK: [B0 (EXIT)]
96*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B1
97*f4a2713aSLionel Sambuc void testBuiltinSize() {
98*f4a2713aSLionel Sambuc   extern int *dummy();
99*f4a2713aSLionel Sambuc   (void)__builtin_object_size(dummy(), 0);
100*f4a2713aSLionel Sambuc }
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc class A {
104*f4a2713aSLionel Sambuc public:
105*f4a2713aSLionel Sambuc   A() {}
106*f4a2713aSLionel Sambuc   ~A() {}
107*f4a2713aSLionel Sambuc };
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc // CHECK: [B2 (ENTRY)]
110*f4a2713aSLionel Sambuc // CHECK-NEXT:   Succs (1): B1
111*f4a2713aSLionel Sambuc // CHECK: [B1]
112*f4a2713aSLionel Sambuc // CHECK-NEXT:   1:  (CXXConstructExpr, class A)
113*f4a2713aSLionel Sambuc // CHECK-NEXT:   2: new A([B1.1])
114*f4a2713aSLionel Sambuc // CHECK-NEXT:   3: A *a = new A();
115*f4a2713aSLionel Sambuc // CHECK-NEXT:   4: a
116*f4a2713aSLionel Sambuc // CHECK-NEXT:   5: [B1.4] (ImplicitCastExpr, LValueToRValue, class A *)
117*f4a2713aSLionel Sambuc // CHECK-NEXT:   6: [B1.5]->~A() (Implicit destructor)
118*f4a2713aSLionel Sambuc // CHECK-NEXT:   7: delete [B1.5]
119*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B2
120*f4a2713aSLionel Sambuc // CHECK-NEXT:   Succs (1): B0
121*f4a2713aSLionel Sambuc // CHECK: [B0 (EXIT)]
122*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B1
123*f4a2713aSLionel Sambuc void test_deletedtor() {
124*f4a2713aSLionel Sambuc   A *a = new A();
125*f4a2713aSLionel Sambuc   delete a;
126*f4a2713aSLionel Sambuc }
127*f4a2713aSLionel Sambuc 
128*f4a2713aSLionel Sambuc // CHECK: [B2 (ENTRY)]
129*f4a2713aSLionel Sambuc // CHECK-NEXT:   Succs (1): B1
130*f4a2713aSLionel Sambuc // CHECK: [B1]
131*f4a2713aSLionel Sambuc // CHECK-NEXT:   1: 5
132*f4a2713aSLionel Sambuc // CHECK-NEXT:   2:  (CXXConstructExpr, class A)
133*f4a2713aSLionel Sambuc // CHECK-NEXT:   3: new A {{\[\[}}B1.1]]
134*f4a2713aSLionel Sambuc // CHECK-NEXT:   4: A *a = new A [5];
135*f4a2713aSLionel Sambuc // CHECK-NEXT:   5: a
136*f4a2713aSLionel Sambuc // CHECK-NEXT:   6: [B1.5] (ImplicitCastExpr, LValueToRValue, class A *)
137*f4a2713aSLionel Sambuc // CHECK-NEXT:   7: [B1.6]->~A() (Implicit destructor)
138*f4a2713aSLionel Sambuc // CHECK-NEXT:   8: delete [] [B1.6]
139*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B2
140*f4a2713aSLionel Sambuc // CHECK-NEXT:   Succs (1): B0
141*f4a2713aSLionel Sambuc // CHECK: [B0 (EXIT)]
142*f4a2713aSLionel Sambuc // CHECK-NEXT:   Preds (1): B1
143*f4a2713aSLionel Sambuc void test_deleteArraydtor() {
144*f4a2713aSLionel Sambuc   A *a = new A[5];
145*f4a2713aSLionel Sambuc   delete[] a;
146*f4a2713aSLionel Sambuc }
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc 
149*f4a2713aSLionel Sambuc namespace NoReturnSingleSuccessor {
150*f4a2713aSLionel Sambuc   struct A {
151*f4a2713aSLionel Sambuc     A();
152*f4a2713aSLionel Sambuc     ~A();
153*f4a2713aSLionel Sambuc   };
154*f4a2713aSLionel Sambuc 
155*f4a2713aSLionel Sambuc   struct B : public A {
156*f4a2713aSLionel Sambuc     B();
157*f4a2713aSLionel Sambuc     ~B() __attribute__((noreturn));
158*f4a2713aSLionel Sambuc   };
159*f4a2713aSLionel Sambuc 
160*f4a2713aSLionel Sambuc // CHECK: ENTRY
161*f4a2713aSLionel Sambuc // CHECK: 1: 1
162*f4a2713aSLionel Sambuc // CHECK-NEXT: 2: return
163*f4a2713aSLionel Sambuc // CHECK-NEXT: ~B() (Implicit destructor)
164*f4a2713aSLionel Sambuc // CHECK-NEXT: Preds (1)
165*f4a2713aSLionel Sambuc // CHECK-NEXT: Succs (1): B0
166*f4a2713aSLionel Sambuc   int test1(int *x) {
167*f4a2713aSLionel Sambuc     B b;
168*f4a2713aSLionel Sambuc     if (x)
169*f4a2713aSLionel Sambuc       return 1;
170*f4a2713aSLionel Sambuc   }
171*f4a2713aSLionel Sambuc 
172*f4a2713aSLionel Sambuc // CHECK: ENTRY
173*f4a2713aSLionel Sambuc // CHECK: 1: 1
174*f4a2713aSLionel Sambuc // CHECK-NEXT: 2: return
175*f4a2713aSLionel Sambuc // CHECK-NEXT: destructor
176*f4a2713aSLionel Sambuc // CHECK-NEXT: Preds (1)
177*f4a2713aSLionel Sambuc // CHECK-NEXT: Succs (1): B0
178*f4a2713aSLionel Sambuc   int test2(int *x) {
179*f4a2713aSLionel Sambuc     const A& a = B();
180*f4a2713aSLionel Sambuc     if (x)
181*f4a2713aSLionel Sambuc       return 1;
182*f4a2713aSLionel Sambuc   }
183*f4a2713aSLionel Sambuc }
184