[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / AST / ast-dump-attr.cpp
blob25cfa6820b37240540ce233fa396cd27826d39b9
1 // 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
12 int TestLocation
13 __attribute__((unused));
14 // CHECK: VarDecl{{.*}}TestLocation
15 // CHECK-NEXT: UnusedAttr 0x{{[^ ]*}} <line:[[@LINE-2]]:16>
17 int TestIndent
18 __attribute__((unused));
19 // CHECK: {{^}}VarDecl{{.*TestIndent[^()]*$}}
20 // CHECK-NEXT: {{^}}`-UnusedAttr{{[^()]*$}}
22 void TestAttributedStmt() {
23 switch (1) {
24 case 1:
25 [[clang::fallthrough]];
26 case 2:
30 // CHECK: FunctionDecl{{.*}}TestAttributedStmt
31 // CHECK: AttributedStmt
32 // CHECK-NEXT: FallThroughAttr
33 // CHECK-NEXT: NullStmt
35 [[clang::warn_unused_result]] int TestCXX11DeclAttr();
36 // CHECK: FunctionDecl{{.*}}TestCXX11DeclAttr
37 // CHECK-NEXT: WarnUnusedResultAttr
39 int TestAlignedNull __attribute__((aligned));
40 // CHECK: VarDecl{{.*}}TestAlignedNull
41 // CHECK-NEXT: AlignedAttr {{.*}} aligned
42 // CHECK-NEXT: <<<NULL>>>
44 int TestAlignedExpr __attribute__((aligned(4)));
45 // CHECK: VarDecl{{.*}}TestAlignedExpr
46 // CHECK-NEXT: AlignedAttr {{.*}} aligned
47 // CHECK-NEXT: ConstantExpr
48 // CHECK-NEXT: value: Int 4
49 // CHECK-NEXT: IntegerLiteral
51 int TestEnum __attribute__((visibility("default")));
52 // CHECK: VarDecl{{.*}}TestEnum
53 // CHECK-NEXT: VisibilityAttr{{.*}} Default
55 class __attribute__((lockable)) Mutex {
56 } mu1, mu2;
57 int TestExpr __attribute__((guarded_by(mu1)));
58 // CHECK: VarDecl{{.*}}TestExpr
59 // CHECK-NEXT: GuardedByAttr
60 // CHECK-NEXT: DeclRefExpr{{.*}}mu1
62 class Mutex TestVariadicExpr __attribute__((acquired_after(mu1, mu2)));
63 // CHECK: VarDecl{{.*}}TestVariadicExpr
64 // CHECK: AcquiredAfterAttr
65 // CHECK-NEXT: DeclRefExpr{{.*}}mu1
66 // CHECK-NEXT: DeclRefExpr{{.*}}mu2
68 void function1(void *) {
69 int TestFunction __attribute__((cleanup(function1)));
71 // CHECK: VarDecl{{.*}}TestFunction
72 // CHECK-NEXT: CleanupAttr{{.*}} Function{{.*}}function1
74 void TestIdentifier(void *, int)
75 __attribute__((pointer_with_type_tag(ident1,1,2)));
76 // CHECK: FunctionDecl{{.*}}TestIdentifier
77 // CHECK: ArgumentWithTypeTagAttr{{.*}} pointer_with_type_tag ident1
79 void TestBool(void *, int)
80 __attribute__((pointer_with_type_tag(bool1,1,2)));
81 // CHECK: FunctionDecl{{.*}}TestBool
82 // CHECK: ArgumentWithTypeTagAttr{{.*}}pointer_with_type_tag bool1 1 2 IsPointer
84 void TestUnsigned(void *, int)
85 __attribute__((pointer_with_type_tag(unsigned1,1,2)));
86 // CHECK: FunctionDecl{{.*}}TestUnsigned
87 // CHECK: ArgumentWithTypeTagAttr{{.*}} pointer_with_type_tag unsigned1 1 2
89 void TestInt(void) __attribute__((constructor(123)));
90 // CHECK: FunctionDecl{{.*}}TestInt
91 // CHECK-NEXT: ConstructorAttr{{.*}} 123
93 static int TestString __attribute__((alias("alias1")));
94 // CHECK: VarDecl{{.*}}TestString
95 // CHECK-NEXT: AliasAttr{{.*}} "alias1"
97 extern struct s1 TestType
98 __attribute__((type_tag_for_datatype(ident1,int)));
99 // CHECK: VarDecl{{.*}}TestType
100 // CHECK-NEXT: TypeTagForDatatypeAttr{{.*}} int
102 void TestLabel() {
103 L: __attribute__((unused)) int i;
104 // CHECK: LabelStmt{{.*}}'L'
105 // CHECK: VarDecl{{.*}}i 'int'
106 // CHECK-NEXT: UnusedAttr{{.*}}
108 M: __attribute(()) int j;
109 // CHECK: LabelStmt {{.*}} 'M'
110 // CHECK-NEXT: DeclStmt
111 // CHECK-NEXT: VarDecl {{.*}} j 'int'
113 N: __attribute(()) ;
114 // CHECK: LabelStmt {{.*}} 'N'
115 // CHECK-NEXT: NullStmt
118 namespace Test {
119 extern "C" int printf(const char *format, ...);
120 // CHECK: FunctionDecl{{.*}}printf
121 // CHECK-NEXT: ParmVarDecl{{.*}}format{{.*}}'const char *'
122 // CHECK-NEXT: BuiltinAttr{{.*}}Implicit
123 // CHECK-NEXT: FormatAttr{{.*}}Implicit printf 1 2
125 alignas(8) extern int x;
126 extern int x;
127 // CHECK: VarDecl{{.*}} x 'int'
128 // CHECK: VarDecl{{.*}} x 'int'
129 // CHECK-NEXT: AlignedAttr{{.*}} Inherited
132 int __attribute__((cdecl)) TestOne(void), TestTwo(void);
133 // CHECK: FunctionDecl{{.*}}TestOne{{.*}}__attribute__((cdecl))
134 // CHECK: FunctionDecl{{.*}}TestTwo{{.*}}__attribute__((cdecl))
136 void func() {
137 auto Test = []() __attribute__((no_thread_safety_analysis)) {};
138 // CHECK: CXXMethodDecl{{.*}}operator() 'void () const'
139 // CHECK: NoThreadSafetyAnalysisAttr
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))
151 namespace PR20930 {
152 struct S {
153 struct { int Test __attribute__((deprecated)); };
154 // CHECK: FieldDecl{{.*}}Test 'int'
155 // CHECK-NEXT: DeprecatedAttr
158 void f() {
159 S s;
160 s.Test = 1;
161 // CHECK: IndirectFieldDecl{{.*}}Test 'int'
162 // CHECK: DeprecatedAttr
166 struct __attribute__((objc_bridge_related(NSParagraphStyle,,))) TestBridgedRef;
167 // CHECK: CXXRecordDecl{{.*}} struct TestBridgedRef
168 // CHECK-NEXT: ObjCBridgeRelatedAttr{{.*}} NSParagraphStyle
170 void TestExternalSourceSymbolAttr1()
171 __attribute__((external_source_symbol(language="Swift", defined_in="module", generated_declaration)));
172 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr1
173 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module" GeneratedDeclaration
175 void TestExternalSourceSymbolAttr2()
176 __attribute__((external_source_symbol(defined_in="module", language="Swift")));
177 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr2
178 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module"{{$}}
180 void 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
185 void 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
190 void TestExternalSourceSymbolAttr5()
191 __attribute__((external_source_symbol(generated_declaration, defined_in="module", language="Swift")));
192 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr5
193 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module" GeneratedDeclaration
195 namespace 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
203 namespace 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
211 // CHECK-NEXT: SuppressAttr{{.*}} on-decl
213 void f() {
214 int *i;
216 [[gsl::suppress("on-stmt")]] {
217 // CHECK: AttributedStmt
218 // CHECK-NEXT: SuppressAttr{{.*}} on-stmt
219 // CHECK-NEXT: CompoundStmt
220 i = reinterpret_cast<int*>(7);
225 namespace TestLifetimeCategories {
226 class [[gsl::Owner(int)]] AOwner{};
227 // CHECK: CXXRecordDecl{{.*}} class AOwner
228 // CHECK: OwnerAttr {{.*}} int
229 class [[gsl::Pointer(int)]] APointer{};
230 // CHECK: CXXRecordDecl{{.*}} class APointer
231 // CHECK: PointerAttr {{.*}} int
233 class [[gsl::Pointer]] PointerWithoutArgument{};
234 // CHECK: CXXRecordDecl{{.*}} class PointerWithoutArgument
235 // CHECK: PointerAttr
237 class [[gsl::Owner]] OwnerWithoutArgument{};
238 // CHECK: CXXRecordDecl{{.*}} class OwnerWithoutArgument
239 // CHECK: OwnerAttr
240 } // namespace TestLifetimeCategories
242 // Verify the order of attributes in the Ast. It must reflect the order
243 // in the parsed source.
244 int mergeAttrTest() __attribute__((deprecated)) __attribute__((warn_unused_result));
245 int mergeAttrTest() __attribute__((annotate("test")));
246 int mergeAttrTest() __attribute__((unused,no_thread_safety_analysis));
247 // CHECK: FunctionDecl{{.*}} mergeAttrTest
248 // CHECK-NEXT: DeprecatedAttr
249 // CHECK-NEXT: WarnUnusedResultAttr
251 // CHECK: FunctionDecl{{.*}} mergeAttrTest
252 // CHECK-NEXT: DeprecatedAttr{{.*}} Inherited
253 // CHECK-NEXT: WarnUnusedResultAttr{{.*}} Inherited
254 // CHECK-NEXT: AnnotateAttr{{.*}}
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