1 // Test without serialization:
2 // RUN: %clang_cc1 -std=c++2a -triple x86_64-linux-gnu -fcxx-exceptions -ast-dump %s \
3 // RUN: | FileCheck -strict-whitespace %s
4 //
5 // Test with serialization:
6 // RUN: %clang_cc1 -std=c++2a -triple x86_64-linux-gnu -fcxx-exceptions -emit-pch -o %t %s
7 // RUN: %clang_cc1 -x c++ -std=c++2a -triple x86_64-linux-gnu -fcxx-exceptions -include-pch %t -ast-dump-all /dev/null \
8 // RUN: | sed -e "s/ <undeserialized declarations>//" -e "s/ imported//" \
9 // RUN: | FileCheck -strict-whitespace %s
10
11 namespace n {
function()12 void function() {}
13 int Variable;
14 }
15 using n::function;
16 using n::Variable;
TestFunction()17 void TestFunction() {
18 void (*f)() = &function;
19 // CHECK: DeclRefExpr{{.*}} (UsingShadow{{.*}}function
20 Variable = 4;
21 // CHECK: DeclRefExpr{{.*}} (UsingShadow{{.*}}Variable
22 }
23
24 // CHECK: FunctionDecl {{.*}} TestCatch1
TestCatch1()25 void TestCatch1() {
26 // CHECK: CXXTryStmt
27 // CHECK-NEXT: CompoundStmt
28 try {
29 }
30 // CHECK-NEXT: CXXCatchStmt
31 // CHECK-NEXT: VarDecl {{.*}} x
32 // CHECK-NEXT: CompoundStmt
33 catch (int x) {
34 }
35 }
36
37 // CHECK: FunctionDecl {{.*}} TestCatch2
TestCatch2()38 void TestCatch2() {
39 // CHECK: CXXTryStmt
40 // CHECK-NEXT: CompoundStmt
41 try {
42 }
43 // CHECK-NEXT: CXXCatchStmt
44 // CHECK-NEXT: NULL
45 // CHECK-NEXT: CompoundStmt
46 catch (...) {
47 }
48 }
49
TestAllocationExprs()50 void TestAllocationExprs() {
51 int *p;
52 p = new int;
53 delete p;
54 p = new int[2];
55 delete[] p;
56 p = ::new int;
57 ::delete p;
58 }
59 // CHECK: FunctionDecl {{.*}} TestAllocationExprs
60 // CHECK: CXXNewExpr {{.*}} 'int *' Function {{.*}} 'operator new'
61 // CHECK: CXXDeleteExpr {{.*}} 'void' Function {{.*}} 'operator delete'
62 // CHECK: CXXNewExpr {{.*}} 'int *' array Function {{.*}} 'operator new[]'
63 // CHECK: CXXDeleteExpr {{.*}} 'void' array Function {{.*}} 'operator delete[]'
64 // CHECK: CXXNewExpr {{.*}} 'int *' global Function {{.*}} 'operator new'
65 // CHECK: CXXDeleteExpr {{.*}} 'void' global Function {{.*}} 'operator delete'
66
67 // Don't crash on dependent exprs that haven't been resolved yet.
68 template <typename T>
TestDependentAllocationExpr()69 void TestDependentAllocationExpr() {
70 T *p = new T;
71 delete p;
72 }
73 // CHECK: FunctionTemplateDecl {{.*}} TestDependentAllocationExpr
74 // CHECK: CXXNewExpr {{.*'T \*'$}}
75 // CHECK: CXXDeleteExpr {{.*'void'$}}
76
77 template <typename T>
78 class DependentScopeMemberExprWrapper {
79 T member;
80 };
81
82 template <typename T>
TestDependentScopeMemberExpr()83 void TestDependentScopeMemberExpr() {
84 DependentScopeMemberExprWrapper<T> obj;
85 obj.member = T();
86 (&obj)->member = T();
87 }
88 // CHECK: FunctionTemplateDecl {{.*}} TestDependentScopeMemberExpr
89 // CHECK: CXXDependentScopeMemberExpr {{.*}} lvalue .member
90 // CHECK: CXXDependentScopeMemberExpr {{.*}} lvalue ->member
91
92 union U {
93 int i;
94 long l;
95 };
96
TestUnionInitList()97 void TestUnionInitList()
98 {
99 U us[3] = {1};
100 // CHECK: VarDecl {{.+}} <col:3, col:15> col:5 us 'U [3]' cinit
101 // CHECK-NEXT: `-InitListExpr {{.+}} <col:13, col:15> 'U [3]'
102 // CHECK-NEXT: |-array_filler: InitListExpr {{.+}} <col:15> 'U' field Field {{.+}} 'i' 'int'
103 // CHECK-NEXT: `-InitListExpr {{.+}} <col:14> 'U' field Field {{.+}} 'i' 'int'
104 // CHECK-NEXT: `-IntegerLiteral {{.+}} <col:14> 'int' 1
105 }
106
TestSwitch(int i)107 void TestSwitch(int i) {
108 switch (int a; i)
109 ;
110 // CHECK: SwitchStmt 0x{{[^ ]*}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:5> has_init
111 // CHECK-NEXT: DeclStmt
112 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:11, col:15> col:15 a 'int'
113 // CHECK-NEXT: ImplicitCastExpr
114 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:18> 'int' lvalue ParmVar 0x{{[^ ]*}} 'i' 'int'
115 // CHECK-NEXT: NullStmt
116 }
117
TestIf(bool b)118 void TestIf(bool b) {
119 if (int i = 12; b)
120 ;
121 // CHECK: IfStmt 0x{{[^ ]*}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:5> has_init
122 // CHECK-NEXT: DeclStmt
123 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:7, col:15> col:11 i 'int' cinit
124 // CHECK-NEXT: IntegerLiteral 0x{{[^ ]*}} <col:15> 'int' 12
125 // CHECK-NEXT: ImplicitCastExpr
126 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:19> 'bool' lvalue ParmVar 0x{{[^ ]*}} 'b' 'bool'
127 // CHECK-NEXT: NullStmt
128
129 if constexpr (sizeof(b) == 1)
130 ;
131 // CHECK: IfStmt 0x{{[^ ]*}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:5>
132 // CHECK-NEXT: ConstantExpr 0x{{[^ ]*}} <line:[[@LINE-3]]:17, col:30> 'bool'
133 // CHECK-NEXT: value: Int 1
134 // CHECK-NEXT: BinaryOperator
135 // CHECK-NEXT: UnaryExprOrTypeTraitExpr
136 // CHECK-NEXT: ParenExpr
137 // CHECK-NEXT: DeclRefExpr
138 // CHECK-NEXT: ImplicitCastExpr
139 // CHECK-NEXT: IntegerLiteral
140 // CHECK-NEXT: NullStmt
141
142 if constexpr (sizeof(b) == 1)
143 ;
144 else
145 ;
146 // CHECK: IfStmt 0x{{[^ ]*}} <line:[[@LINE-4]]:3, line:[[@LINE-1]]:5> has_else
147 // CHECK-NEXT: ConstantExpr 0x{{[^ ]*}} <line:[[@LINE-5]]:17, col:30> 'bool'
148 // CHECK-NEXT: value: Int 1
149 // CHECK-NEXT: BinaryOperator
150 // CHECK-NEXT: UnaryExprOrTypeTraitExpr
151 // CHECK-NEXT: ParenExpr
152 // CHECK-NEXT: DeclRefExpr
153 // CHECK-NEXT: ImplicitCastExpr
154 // CHECK-NEXT: IntegerLiteral
155 // CHECK-NEXT: NullStmt
156 // CHECK-NEXT: NullStmt
157 }
158
159 struct Container {
160 int *begin() const;
161 int *end() const;
162 };
163
TestIteration()164 void TestIteration() {
165 for (int i = 0; int j = i; ++i)
166 ;
167 // CHECK: ForStmt 0x{{[^ ]*}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:5>
168 // CHECK-NEXT: DeclStmt
169 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:8, col:16> col:12 used i 'int' cinit
170 // CHECK-NEXT: IntegerLiteral 0x{{[^ ]*}} <col:16> 'int' 0
171 // CHECK-NEXT: DeclStmt
172 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:19, col:27> col:23 used j 'int' cinit
173 // CHECK-NEXT: ImplicitCastExpr
174 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:27> 'int' lvalue Var 0x{{[^ ]*}} 'i' 'int'
175 // CHECK-NEXT: ImplicitCastExpr 0x{{[^ ]*}} <col:23> 'bool' <IntegralToBoolean>
176 // CHECK-NEXT: ImplicitCastExpr 0x{{[^ ]*}} <col:23> 'int' <LValueToRValue>
177 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:23> 'int' lvalue Var 0x{{[^ ]*}} 'j' 'int'
178 // CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:30, col:32> 'int' lvalue prefix '++'
179 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:32> 'int' lvalue Var 0x{{[^ ]*}} 'i' 'int'
180 // CHECK-NEXT: NullStmt
181
182 int vals[10];
183 for (int v : vals)
184 ;
185 // CHECK: CXXForRangeStmt 0x{{[^ ]*}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:5>
186 // CHECK-NEXT: <<<NULL>>>
187 // CHECK-NEXT: DeclStmt
188 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:16> col:16 implicit used __range1 'int (&)[10]' cinit
189 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:16> 'int [10]' lvalue Var 0x{{[^ ]*}} 'vals' 'int [10]'
190 // CHECK-NEXT: DeclStmt
191 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:14> col:14 implicit used __begin1 'int *':'int *' cinit
192 // CHECK-NEXT: ImplicitCastExpr
193 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int [10]' lvalue Var 0x{{[^ ]*}} '__range1' 'int (&)[10]'
194 // CHECK-NEXT: DeclStmt
195 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:14, col:16> col:14 implicit used __end1 'int *':'int *' cinit
196 // CHECK-NEXT: BinaryOperator 0x{{[^ ]*}} <col:14, col:16> 'int *' '+'
197 // CHECK-NEXT: ImplicitCastExpr
198 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int [10]' lvalue Var 0x{{[^ ]*}} '__range1' 'int (&)[10]'
199 // CHECK-NEXT: IntegerLiteral 0x{{[^ ]*}} <col:16> 'long' 10
200 // CHECK-NEXT: BinaryOperator 0x{{[^ ]*}} <col:14> 'bool' '!='
201 // CHECK-NEXT: ImplicitCastExpr
202 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
203 // CHECK-NEXT: ImplicitCastExpr
204 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__end1' 'int *':'int *'
205 // CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue prefix '++'
206 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
207 // CHECK-NEXT: DeclStmt
208 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:8, col:14> col:12 v 'int' cinit
209 // CHECK-NEXT: ImplicitCastExpr
210 // CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:14> 'int' lvalue prefix '*' cannot overflow
211 // CHECK-NEXT: ImplicitCastExpr
212 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
213 // CHECK-NEXT: NullStmt
214
215 Container C;
216 for (int v : C)
217 ;
218 // CHECK: CXXForRangeStmt 0x{{[^ ]*}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:5>
219 // CHECK-NEXT: <<<NULL>>>
220 // CHECK-NEXT: DeclStmt
221 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:16> col:16 implicit used __range1 'Container &' cinit
222 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:16> 'Container' lvalue Var 0x{{[^ ]*}} 'C' 'Container'
223 // CHECK-NEXT: DeclStmt
224 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:14> col:14 implicit used __begin1 'int *':'int *' cinit
225 // CHECK-NEXT: CXXMemberCallExpr 0x{{[^ ]*}} <col:14> 'int *'
226 // CHECK-NEXT: MemberExpr 0x{{[^ ]*}} <col:14> '<bound member function type>' .begin 0x{{[^ ]*}}
227 // CHECK-NEXT: ImplicitCastExpr
228 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'Container' lvalue Var 0x{{[^ ]*}} '__range1' 'Container &'
229 // CHECK-NEXT: DeclStmt
230 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:14> col:14 implicit used __end1 'int *':'int *' cinit
231 // CHECK-NEXT: CXXMemberCallExpr 0x{{[^ ]*}} <col:14> 'int *'
232 // CHECK-NEXT: MemberExpr 0x{{[^ ]*}} <col:14> '<bound member function type>' .end 0x{{[^ ]*}}
233 // CHECK-NEXT: ImplicitCastExpr
234 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'Container' lvalue Var 0x{{[^ ]*}} '__range1' 'Container &'
235 // CHECK-NEXT: BinaryOperator 0x{{[^ ]*}} <col:14> 'bool' '!='
236 // CHECK-NEXT: ImplicitCastExpr
237 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
238 // CHECK-NEXT: ImplicitCastExpr
239 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__end1' 'int *':'int *'
240 // CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue prefix '++'
241 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
242 // CHECK-NEXT: DeclStmt
243 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:8, col:14> col:12 v 'int' cinit
244 // CHECK-NEXT: ImplicitCastExpr
245 // CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:14> 'int' lvalue prefix '*' cannot overflow
246 // CHECK-NEXT: ImplicitCastExpr
247 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:14> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
248 // CHECK-NEXT: NullStmt
249
250 for (int a; int v : vals)
251 ;
252 // CHECK: CXXForRangeStmt 0x{{[^ ]*}} <line:[[@LINE-2]]:3, line:[[@LINE-1]]:5>
253 // CHECK-NEXT: DeclStmt
254 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:8, col:12> col:12 a 'int'
255 // CHECK-NEXT: DeclStmt
256 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:23> col:23 implicit used __range1 'int (&)[10]' cinit
257 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:23> 'int [10]' lvalue Var 0x{{[^ ]*}} 'vals' 'int [10]'
258 // CHECK-NEXT: DeclStmt
259 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:21> col:21 implicit used __begin1 'int *':'int *' cinit
260 // CHECK-NEXT: ImplicitCastExpr
261 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:21> 'int [10]' lvalue Var 0x{{[^ ]*}} '__range1' 'int (&)[10]'
262 // CHECK-NEXT: DeclStmt
263 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:21, col:23> col:21 implicit used __end1 'int *':'int *' cinit
264 // CHECK-NEXT: BinaryOperator 0x{{[^ ]*}} <col:21, col:23> 'int *' '+'
265 // CHECK-NEXT: ImplicitCastExpr
266 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:21> 'int [10]' lvalue Var 0x{{[^ ]*}} '__range1' 'int (&)[10]'
267 // CHECK-NEXT: IntegerLiteral 0x{{[^ ]*}} <col:23> 'long' 10
268 // CHECK-NEXT: BinaryOperator 0x{{[^ ]*}} <col:21> 'bool' '!='
269 // CHECK-NEXT: ImplicitCastExpr
270 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:21> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
271 // CHECK-NEXT: ImplicitCastExpr
272 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:21> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__end1' 'int *':'int *'
273 // CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:21> 'int *':'int *' lvalue prefix '++'
274 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:21> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
275 // CHECK-NEXT: DeclStmt
276 // CHECK-NEXT: VarDecl 0x{{[^ ]*}} <col:15, col:21> col:19 v 'int' cinit
277 // CHECK-NEXT: ImplicitCastExpr
278 // CHECK-NEXT: UnaryOperator 0x{{[^ ]*}} <col:21> 'int' lvalue prefix '*' cannot overflow
279 // CHECK-NEXT: ImplicitCastExpr
280 // CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:21> 'int *':'int *' lvalue Var 0x{{[^ ]*}} '__begin1' 'int *':'int *'
281 // CHECK-NEXT: NullStmt
282 }
283