blob: c2bd768dc2adfc316cea6273dd202dffc5ee1722 [file] [log] [blame]
Bruno Ricciecbf2f52020-06-21 13:35:15 +01001// Test without serialization:
2// RUN: %clang_cc1 -triple x86_64-pc-linux -std=c++11 -Wno-deprecated-declarations -ast-dump -ast-dump-filter Test %s \
3// RUN: | FileCheck --strict-whitespace %s
4//
5// Test with serialization:
6// RUN: %clang_cc1 -triple x86_64-pc-linux -std=c++11 -Wno-deprecated-declarations -emit-pch -o %t %s
7// RUN: %clang_cc1 -x c++ -triple x86_64-pc-linux -std=c++11 -Wno-deprecated-declarations \
8// RUN: -include-pch %t -ast-dump-all -ast-dump-filter Test /dev/null \
9// RUN: | sed -e "s/ <undeserialized declarations>//" -e "s/ imported//" \
10// RUN: | FileCheck --strict-whitespace %s
Aaron Ballman8c208282017-12-21 21:42:42 +000011
12int TestLocation
13__attribute__((unused));
14// CHECK: VarDecl{{.*}}TestLocation
15// CHECK-NEXT: UnusedAttr 0x{{[^ ]*}} <line:[[@LINE-2]]:16>
16
17int TestIndent
18__attribute__((unused));
19// CHECK: {{^}}VarDecl{{.*TestIndent[^()]*$}}
20// CHECK-NEXT: {{^}}`-UnusedAttr{{[^()]*$}}
21
22void TestAttributedStmt() {
23 switch (1) {
24 case 1:
25 [[clang::fallthrough]];
26 case 2:
27 ;
28 }
29}
30// CHECK: FunctionDecl{{.*}}TestAttributedStmt
31// CHECK: AttributedStmt
32// CHECK-NEXT: FallThroughAttr
33// CHECK-NEXT: NullStmt
34
35[[clang::warn_unused_result]] int TestCXX11DeclAttr();
36// CHECK: FunctionDecl{{.*}}TestCXX11DeclAttr
37// CHECK-NEXT: WarnUnusedResultAttr
38
39int TestAlignedNull __attribute__((aligned));
40// CHECK: VarDecl{{.*}}TestAlignedNull
41// CHECK-NEXT: AlignedAttr {{.*}} aligned
42// CHECK-NEXT: <<<NULL>>>
43
44int TestAlignedExpr __attribute__((aligned(4)));
45// CHECK: VarDecl{{.*}}TestAlignedExpr
46// CHECK-NEXT: AlignedAttr {{.*}} aligned
Bill Wendling8003edc2018-11-09 00:41:36 +000047// CHECK-NEXT: ConstantExpr
Bruno Riccif63e3ea2020-07-06 21:50:23 +010048// CHECK-NEXT: value: Int 4
Bill Wendling8003edc2018-11-09 00:41:36 +000049// CHECK-NEXT: IntegerLiteral
Aaron Ballman8c208282017-12-21 21:42:42 +000050
51int TestEnum __attribute__((visibility("default")));
52// CHECK: VarDecl{{.*}}TestEnum
53// CHECK-NEXT: VisibilityAttr{{.*}} Default
54
55class __attribute__((lockable)) Mutex {
56} mu1, mu2;
57int TestExpr __attribute__((guarded_by(mu1)));
58// CHECK: VarDecl{{.*}}TestExpr
59// CHECK-NEXT: GuardedByAttr
60// CHECK-NEXT: DeclRefExpr{{.*}}mu1
61
62class Mutex TestVariadicExpr __attribute__((acquired_after(mu1, mu2)));
63// CHECK: VarDecl{{.*}}TestVariadicExpr
64// CHECK: AcquiredAfterAttr
65// CHECK-NEXT: DeclRefExpr{{.*}}mu1
66// CHECK-NEXT: DeclRefExpr{{.*}}mu2
67
68void function1(void *) {
69 int TestFunction __attribute__((cleanup(function1)));
70}
71// CHECK: VarDecl{{.*}}TestFunction
72// CHECK-NEXT: CleanupAttr{{.*}} Function{{.*}}function1
73
74void TestIdentifier(void *, int)
75__attribute__((pointer_with_type_tag(ident1,1,2)));
76// CHECK: FunctionDecl{{.*}}TestIdentifier
77// CHECK: ArgumentWithTypeTagAttr{{.*}} pointer_with_type_tag ident1
78
79void TestBool(void *, int)
80__attribute__((pointer_with_type_tag(bool1,1,2)));
81// CHECK: FunctionDecl{{.*}}TestBool
Joel E. Denny81508102018-03-13 14:51:22 +000082// CHECK: ArgumentWithTypeTagAttr{{.*}}pointer_with_type_tag bool1 1 2 IsPointer
Aaron Ballman8c208282017-12-21 21:42:42 +000083
84void TestUnsigned(void *, int)
85__attribute__((pointer_with_type_tag(unsigned1,1,2)));
86// CHECK: FunctionDecl{{.*}}TestUnsigned
Joel E. Denny81508102018-03-13 14:51:22 +000087// CHECK: ArgumentWithTypeTagAttr{{.*}} pointer_with_type_tag unsigned1 1 2
Aaron Ballman8c208282017-12-21 21:42:42 +000088
89void TestInt(void) __attribute__((constructor(123)));
90// CHECK: FunctionDecl{{.*}}TestInt
91// CHECK-NEXT: ConstructorAttr{{.*}} 123
92
93static int TestString __attribute__((alias("alias1")));
94// CHECK: VarDecl{{.*}}TestString
95// CHECK-NEXT: AliasAttr{{.*}} "alias1"
96
97extern struct s1 TestType
98__attribute__((type_tag_for_datatype(ident1,int)));
99// CHECK: VarDecl{{.*}}TestType
100// CHECK-NEXT: TypeTagForDatatypeAttr{{.*}} int
101
102void TestLabel() {
103L: __attribute__((unused)) int i;
104// CHECK: LabelStmt{{.*}}'L'
105// CHECK: VarDecl{{.*}}i 'int'
106// CHECK-NEXT: UnusedAttr{{.*}}
107
108M: __attribute(()) int j;
109// CHECK: LabelStmt {{.*}} 'M'
110// CHECK-NEXT: DeclStmt
111// CHECK-NEXT: VarDecl {{.*}} j 'int'
112
113N: __attribute(()) ;
114// CHECK: LabelStmt {{.*}} 'N'
115// CHECK-NEXT: NullStmt
116}
117
118namespace Test {
119extern "C" int printf(const char *format, ...);
120// CHECK: FunctionDecl{{.*}}printf
121// CHECK-NEXT: ParmVarDecl{{.*}}format{{.*}}'const char *'
Raul Tambree09107a2020-09-04 19:10:09 +0300122// CHECK-NEXT: BuiltinAttr{{.*}}Implicit
Aaron Ballman8c208282017-12-21 21:42:42 +0000123// CHECK-NEXT: FormatAttr{{.*}}Implicit printf 1 2
124
125alignas(8) extern int x;
126extern int x;
127// CHECK: VarDecl{{.*}} x 'int'
128// CHECK: VarDecl{{.*}} x 'int'
129// CHECK-NEXT: AlignedAttr{{.*}} Inherited
130}
131
132int __attribute__((cdecl)) TestOne(void), TestTwo(void);
133// CHECK: FunctionDecl{{.*}}TestOne{{.*}}__attribute__((cdecl))
134// CHECK: FunctionDecl{{.*}}TestTwo{{.*}}__attribute__((cdecl))
135
136void func() {
137 auto Test = []() __attribute__((no_thread_safety_analysis)) {};
138 // CHECK: CXXMethodDecl{{.*}}operator() 'void () const'
139 // CHECK: NoThreadSafetyAnalysisAttr
140
141 // Because GNU's noreturn applies to the function type, and this lambda does
142 // not have a capture list, the call operator and the function pointer
143 // conversion should both be noreturn, but the method should not contain a
144 // NoReturnAttr because the attribute applied to the type.
145 auto Test2 = []() __attribute__((noreturn)) { while(1); };
146 // CHECK: CXXMethodDecl{{.*}}operator() 'void () __attribute__((noreturn)) const'
147 // CHECK-NOT: NoReturnAttr
148 // CHECK: CXXConversionDecl{{.*}}operator void (*)() __attribute__((noreturn))
149}
150
151namespace PR20930 {
152struct S {
153 struct { int Test __attribute__((deprecated)); };
154 // CHECK: FieldDecl{{.*}}Test 'int'
155 // CHECK-NEXT: DeprecatedAttr
156};
157
158void f() {
159 S s;
160 s.Test = 1;
161 // CHECK: IndirectFieldDecl{{.*}}Test 'int'
162 // CHECK: DeprecatedAttr
163}
164}
165
166struct __attribute__((objc_bridge_related(NSParagraphStyle,,))) TestBridgedRef;
167// CHECK: CXXRecordDecl{{.*}} struct TestBridgedRef
168// CHECK-NEXT: ObjCBridgeRelatedAttr{{.*}} NSParagraphStyle
169
170void TestExternalSourceSymbolAttr1()
171__attribute__((external_source_symbol(language="Swift", defined_in="module", generated_declaration)));
172// CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr1
173// CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module" GeneratedDeclaration
174
175void TestExternalSourceSymbolAttr2()
176__attribute__((external_source_symbol(defined_in="module", language="Swift")));
177// CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr2
178// CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module"{{$}}
179
180void TestExternalSourceSymbolAttr3()
181__attribute__((external_source_symbol(generated_declaration, language="Objective-C++", defined_in="module")));
182// CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr3
183// CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Objective-C++" "module" GeneratedDeclaration
184
185void TestExternalSourceSymbolAttr4()
186__attribute__((external_source_symbol(defined_in="Some external file.cs", generated_declaration, language="C Sharp")));
187// CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr4
188// CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "C Sharp" "Some external file.cs" GeneratedDeclaration
189
190void TestExternalSourceSymbolAttr5()
191__attribute__((external_source_symbol(generated_declaration, defined_in="module", language="Swift")));
192// CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr5
193// CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module" GeneratedDeclaration
194
195namespace TestNoEscape {
196 void noescapeFunc(int *p0, __attribute__((noescape)) int *p1) {}
197 // CHECK: `-FunctionDecl{{.*}} noescapeFunc 'void (int *, __attribute__((noescape)) int *)'
198 // CHECK-NEXT: ParmVarDecl
199 // CHECK-NEXT: ParmVarDecl
200 // CHECK-NEXT: NoEscapeAttr
201}
202
203namespace TestSuppress {
204 [[gsl::suppress("at-namespace")]];
205 // CHECK: NamespaceDecl{{.*}} TestSuppress
206 // CHECK-NEXT: EmptyDecl{{.*}}
207 // CHECK-NEXT: SuppressAttr{{.*}} at-namespace
208 [[gsl::suppress("on-decl")]]
209 void TestSuppressFunction();
210 // CHECK: FunctionDecl{{.*}} TestSuppressFunction
Nico Weber346f1e92019-02-11 20:33:22 +0000211 // CHECK-NEXT: SuppressAttr{{.*}} on-decl
Aaron Ballman8c208282017-12-21 21:42:42 +0000212
213 void f() {
214 int *i;
215
216 [[gsl::suppress("on-stmt")]] {
217 // CHECK: AttributedStmt
218 // CHECK-NEXT: SuppressAttr{{.*}} on-stmt
219 // CHECK-NEXT: CompoundStmt
220 i = reinterpret_cast<int*>(7);
221 }
222 }
223}
Michael Kruse4c99f5f2018-09-24 06:31:37 +0000224
Matthias Gehred293cbd2019-07-25 17:50:51 +0000225namespace TestLifetimeCategories {
226class [[gsl::Owner(int)]] AOwner{};
227// CHECK: CXXRecordDecl{{.*}} class AOwner
228// CHECK: OwnerAttr {{.*}} int
229class [[gsl::Pointer(int)]] APointer{};
230// CHECK: CXXRecordDecl{{.*}} class APointer
231// CHECK: PointerAttr {{.*}} int
232
233class [[gsl::Pointer]] PointerWithoutArgument{};
234// CHECK: CXXRecordDecl{{.*}} class PointerWithoutArgument
235// CHECK: PointerAttr
236
237class [[gsl::Owner]] OwnerWithoutArgument{};
238// CHECK: CXXRecordDecl{{.*}} class OwnerWithoutArgument
239// CHECK: OwnerAttr
240} // namespace TestLifetimeCategories
241
Michael Kruse4c99f5f2018-09-24 06:31:37 +0000242// Verify the order of attributes in the Ast. It must reflect the order
243// in the parsed source.
244int mergeAttrTest() __attribute__((deprecated)) __attribute__((warn_unused_result));
245int mergeAttrTest() __attribute__((annotate("test")));
246int mergeAttrTest() __attribute__((unused,no_thread_safety_analysis));
247// CHECK: FunctionDecl{{.*}} mergeAttrTest
248// CHECK-NEXT: DeprecatedAttr
249// CHECK-NEXT: WarnUnusedResultAttr
250
251// CHECK: FunctionDecl{{.*}} mergeAttrTest
252// CHECK-NEXT: DeprecatedAttr{{.*}} Inherited
253// CHECK-NEXT: WarnUnusedResultAttr{{.*}} Inherited
254// CHECK-NEXT: AnnotateAttr{{.*}}
255
256// CHECK: FunctionDecl{{.*}} mergeAttrTest
257// CHECK-NEXT: DeprecatedAttr{{.*}} Inherited
258// CHECK-NEXT: WarnUnusedResultAttr{{.*}} Inherited
259// CHECK-NEXT: AnnotateAttr{{.*}} Inherited
260// CHECK-NEXT: UnusedAttr
261// CHECK-NEXT: NoThreadSafetyAnalysisAttr