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
11 
12 int TestLocation
13 __attribute__((unused));
14 // CHECK:      VarDecl{{.*}}TestLocation
15 // CHECK-NEXT:   UnusedAttr 0x{{[^ ]*}} <line:[[@LINE-2]]:16>
16 
17 int TestIndent
18 __attribute__((unused));
19 // CHECK:      {{^}}VarDecl{{.*TestIndent[^()]*$}}
20 // CHECK-NEXT: {{^}}`-UnusedAttr{{[^()]*$}}
21 
22 void 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 
39 int TestAlignedNull __attribute__((aligned));
40 // CHECK:      VarDecl{{.*}}TestAlignedNull
41 // CHECK-NEXT:   AlignedAttr {{.*}} aligned
42 // CHECK-NEXT:     <<<NULL>>>
43 
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
50 
51 int TestEnum __attribute__((visibility("default")));
52 // CHECK:      VarDecl{{.*}}TestEnum
53 // CHECK-NEXT:   VisibilityAttr{{.*}} Default
54 
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
61 
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
67 
68 void function1(void *) {
69   int TestFunction __attribute__((cleanup(function1)));
70 }
71 // CHECK:      VarDecl{{.*}}TestFunction
72 // CHECK-NEXT:   CleanupAttr{{.*}} Function{{.*}}function1
73 
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
78 
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
83 
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
88 
89 void TestInt(void) __attribute__((constructor(123)));
90 // CHECK:      FunctionDecl{{.*}}TestInt
91 // CHECK-NEXT:   ConstructorAttr{{.*}} 123
92 
93 static int TestString __attribute__((alias("alias1")));
94 // CHECK:      VarDecl{{.*}}TestString
95 // CHECK-NEXT:   AliasAttr{{.*}} "alias1"
96 
97 extern struct s1 TestType
98 __attribute__((type_tag_for_datatype(ident1,int)));
99 // CHECK:      VarDecl{{.*}}TestType
100 // CHECK-NEXT:   TypeTagForDatatypeAttr{{.*}} int
101 
102 void TestLabel() {
103 L: __attribute__((unused)) int i;
104 // CHECK: LabelStmt{{.*}}'L'
105 // CHECK: VarDecl{{.*}}i 'int'
106 // CHECK-NEXT: UnusedAttr{{.*}}
107 
108 M: __attribute(()) int j;
109 // CHECK: LabelStmt {{.*}} 'M'
110 // CHECK-NEXT: DeclStmt
111 // CHECK-NEXT: VarDecl {{.*}} j 'int'
112 
113 N: __attribute(()) ;
114 // CHECK: LabelStmt {{.*}} 'N'
115 // CHECK-NEXT: NullStmt
116 }
117 
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: FormatAttr{{.*}}Implicit printf 1 2
123 
124 alignas(8) extern int x;
125 extern int x;
126 // CHECK: VarDecl{{.*}} x 'int'
127 // CHECK: VarDecl{{.*}} x 'int'
128 // CHECK-NEXT: AlignedAttr{{.*}} Inherited
129 }
130 
131 int __attribute__((cdecl)) TestOne(void), TestTwo(void);
132 // CHECK: FunctionDecl{{.*}}TestOne{{.*}}__attribute__((cdecl))
133 // CHECK: FunctionDecl{{.*}}TestTwo{{.*}}__attribute__((cdecl))
134 
135 void func() {
136   auto Test = []() __attribute__((no_thread_safety_analysis)) {};
137   // CHECK: CXXMethodDecl{{.*}}operator() 'void () const'
138   // CHECK: NoThreadSafetyAnalysisAttr
139 
140   // Because GNU's noreturn applies to the function type, and this lambda does
141   // not have a capture list, the call operator and the function pointer
142   // conversion should both be noreturn, but the method should not contain a
143   // NoReturnAttr because the attribute applied to the type.
144   auto Test2 = []() __attribute__((noreturn)) { while(1); };
145   // CHECK: CXXMethodDecl{{.*}}operator() 'void () __attribute__((noreturn)) const'
146   // CHECK-NOT: NoReturnAttr
147   // CHECK: CXXConversionDecl{{.*}}operator void (*)() __attribute__((noreturn))
148 }
149 
150 namespace PR20930 {
151 struct S {
152   struct { int Test __attribute__((deprecated)); };
153   // CHECK: FieldDecl{{.*}}Test 'int'
154   // CHECK-NEXT: DeprecatedAttr
155 };
156 
157 void f() {
158   S s;
159   s.Test = 1;
160   // CHECK: IndirectFieldDecl{{.*}}Test 'int'
161   // CHECK: DeprecatedAttr
162 }
163 }
164 
165 struct __attribute__((objc_bridge_related(NSParagraphStyle,,))) TestBridgedRef;
166 // CHECK: CXXRecordDecl{{.*}} struct TestBridgedRef
167 // CHECK-NEXT: ObjCBridgeRelatedAttr{{.*}} NSParagraphStyle
168 
169 void TestExternalSourceSymbolAttr1()
170 __attribute__((external_source_symbol(language="Swift", defined_in="module", generated_declaration)));
171 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr1
172 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module" GeneratedDeclaration
173 
174 void TestExternalSourceSymbolAttr2()
175 __attribute__((external_source_symbol(defined_in="module", language="Swift")));
176 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr2
177 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module"{{$}}
178 
179 void TestExternalSourceSymbolAttr3()
180 __attribute__((external_source_symbol(generated_declaration, language="Objective-C++", defined_in="module")));
181 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr3
182 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Objective-C++" "module" GeneratedDeclaration
183 
184 void TestExternalSourceSymbolAttr4()
185 __attribute__((external_source_symbol(defined_in="Some external file.cs", generated_declaration, language="C Sharp")));
186 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr4
187 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "C Sharp" "Some external file.cs" GeneratedDeclaration
188 
189 void TestExternalSourceSymbolAttr5()
190 __attribute__((external_source_symbol(generated_declaration, defined_in="module", language="Swift")));
191 // CHECK: FunctionDecl{{.*}} TestExternalSourceSymbolAttr5
192 // CHECK-NEXT: ExternalSourceSymbolAttr{{.*}} "Swift" "module" GeneratedDeclaration
193 
194 namespace TestNoEscape {
195   void noescapeFunc(int *p0, __attribute__((noescape)) int *p1) {}
196   // CHECK: `-FunctionDecl{{.*}} noescapeFunc 'void (int *, __attribute__((noescape)) int *)'
197   // CHECK-NEXT: ParmVarDecl
198   // CHECK-NEXT: ParmVarDecl
199   // CHECK-NEXT: NoEscapeAttr
200 }
201 
202 namespace TestSuppress {
203   [[gsl::suppress("at-namespace")]];
204   // CHECK: NamespaceDecl{{.*}} TestSuppress
205   // CHECK-NEXT: EmptyDecl{{.*}}
206   // CHECK-NEXT: SuppressAttr{{.*}} at-namespace
207   [[gsl::suppress("on-decl")]]
208   void TestSuppressFunction();
209   // CHECK: FunctionDecl{{.*}} TestSuppressFunction
210   // CHECK-NEXT: SuppressAttr{{.*}} on-decl
211 
212   void f() {
213       int *i;
214 
215       [[gsl::suppress("on-stmt")]] {
216       // CHECK: AttributedStmt
217       // CHECK-NEXT: SuppressAttr{{.*}} on-stmt
218       // CHECK-NEXT: CompoundStmt
219         i = reinterpret_cast<int*>(7);
220       }
221     }
222 }
223 
224 namespace TestLifetimeCategories {
225 class [[gsl::Owner(int)]] AOwner{};
226 // CHECK: CXXRecordDecl{{.*}} class AOwner
227 // CHECK: OwnerAttr {{.*}} int
228 class [[gsl::Pointer(int)]] APointer{};
229 // CHECK: CXXRecordDecl{{.*}} class APointer
230 // CHECK: PointerAttr {{.*}} int
231 
232 class [[gsl::Pointer]] PointerWithoutArgument{};
233 // CHECK: CXXRecordDecl{{.*}} class PointerWithoutArgument
234 // CHECK: PointerAttr
235 
236 class [[gsl::Owner]] OwnerWithoutArgument{};
237 // CHECK: CXXRecordDecl{{.*}} class OwnerWithoutArgument
238 // CHECK: OwnerAttr
239 } // namespace TestLifetimeCategories
240 
241 // Verify the order of attributes in the Ast. It must reflect the order
242 // in the parsed source.
243 int mergeAttrTest() __attribute__((deprecated)) __attribute__((warn_unused_result));
244 int mergeAttrTest() __attribute__((annotate("test")));
245 int mergeAttrTest() __attribute__((unused,no_thread_safety_analysis));
246 // CHECK: FunctionDecl{{.*}} mergeAttrTest
247 // CHECK-NEXT: DeprecatedAttr
248 // CHECK-NEXT: WarnUnusedResultAttr
249 
250 // CHECK: FunctionDecl{{.*}} mergeAttrTest
251 // CHECK-NEXT: DeprecatedAttr{{.*}} Inherited
252 // CHECK-NEXT: WarnUnusedResultAttr{{.*}} Inherited
253 // CHECK-NEXT: AnnotateAttr{{.*}}
254 
255 // CHECK: FunctionDecl{{.*}} mergeAttrTest
256 // CHECK-NEXT: DeprecatedAttr{{.*}} Inherited
257 // CHECK-NEXT: WarnUnusedResultAttr{{.*}} Inherited
258 // CHECK-NEXT: AnnotateAttr{{.*}} Inherited
259 // CHECK-NEXT: UnusedAttr
260 // CHECK-NEXT: NoThreadSafetyAnalysisAttr
261