Debian Codegraph @

clang/test/Sema/attr-nonblocking-syntax.cpp

raw
1 // RUN: %clang_cc1 %s -ast-dump -fblocks | FileCheck %s
2
3 // Make sure that the attribute gets parsed and attached to the correct AST elements.
4
5 #pragma clang diagnostic ignored "-Wunused-variable"
6
7 // =========================================================================================
8 // Square brackets, true
9
10 namespace square_brackets {
11
12 // 1. On the type of the FunctionDecl
13 void nl_function() [[clang::nonblocking]];
14 // CHECK: FunctionDecl {{.*}} nl_function 'void () __attribute__((nonblocking))'
15
16 // 2. On the type of the VarDecl holding a function pointer
17 void (*nl_func_a)() [[clang::nonblocking]];
18 // CHECK: VarDecl {{.*}} nl_func_a 'void (*)() __attribute__((nonblocking))'
19
20 // 3. On the type of the ParmVarDecl of a function parameter
21 static void nlReceiver(void (*nl_func)() [[clang::nonblocking]]);
22 // CHECK: ParmVarDecl {{.*}} nl_func 'void (*)() __attribute__((nonblocking))'
23
24 // 4. As an AttributedType within the nested types of a typedef
25 typedef void (*nl_fp_type)() [[clang::nonblocking]];
26 // CHECK: TypedefDecl {{.*}} nl_fp_type 'void (*)() __attribute__((nonblocking))'
27 using nl_fp_talias = void (*)() [[clang::nonblocking]];
28 // CHECK: TypeAliasDecl {{.*}} nl_fp_talias 'void (*)() __attribute__((nonblocking))'
29
30 // 5. From a typedef or typealias, on a VarDecl
31 nl_fp_type nl_fp_var1;
32 // CHECK: VarDecl {{.*}} nl_fp_var1 'nl_fp_type':'void (*)() __attribute__((nonblocking))'
33 nl_fp_talias nl_fp_var2;
34 // CHECK: VarDecl {{.*}} nl_fp_var2 'nl_fp_talias':'void (*)() __attribute__((nonblocking))'
35
36 // 6. On type of a FieldDecl
37 struct Struct {
38 void (*nl_func_field)() [[clang::nonblocking]];
39 // CHECK: FieldDecl {{.*}} nl_func_field 'void (*)() __attribute__((nonblocking))'
40 };
41
42 // nonallocating should NOT be subsumed into nonblocking
43 void nl1() [[clang::nonblocking]] [[clang::nonallocating]];
44 // CHECK: FunctionDecl {{.*}} nl1 'void () __attribute__((nonblocking)) __attribute__((nonallocating))'
45
46 void nl2() [[clang::nonallocating]] [[clang::nonblocking]];
47 // CHECK: FunctionDecl {{.*}} nl2 'void () __attribute__((nonblocking)) __attribute__((nonallocating))'
48
49 decltype(nl1) nl3;
50 // CHECK: FunctionDecl {{.*}} nl3 'decltype(nl1)':'void () __attribute__((nonblocking)) __attribute__((nonallocating))'
51
52 // Attribute propagates from base class virtual method to overrides.
53 struct Base {
54 virtual void nb_method() [[clang::nonblocking]];
55 };
56 struct Derived : public Base {
57 void nb_method() override;
58 // CHECK: CXXMethodDecl {{.*}} nb_method 'void () __attribute__((nonblocking))'
59 };
60
61 // Dependent expression
62 template <bool V>
63 struct Dependent {
64 void nb_method2() [[clang::nonblocking(V)]];
65 // CHECK: CXXMethodDecl {{.*}} nb_method2 'void () __attribute__((nonblocking(V)))'
66 };
67
68 // --- Blocks ---
69
70 // On the type of the VarDecl holding a BlockDecl
71 void (^nl_block1)() [[clang::nonblocking]] = ^() [[clang::nonblocking]] {};
72 // CHECK: VarDecl {{.*}} nl_block1 'void (^)() __attribute__((nonblocking))'
73
74 int (^nl_block2)() [[clang::nonblocking]] = ^() [[clang::nonblocking]] { return 0; };
75 // CHECK: VarDecl {{.*}} nl_block2 'int (^)() __attribute__((nonblocking))'
76
77 // The operand of the CallExpr is an ImplicitCastExpr of a DeclRefExpr -> nl_block which hold the attribute
78 static void blockCaller() { nl_block1(); }
79 // CHECK: DeclRefExpr {{.*}} 'nl_block1' 'void (^)() __attribute__((nonblocking))'
80
81 // --- Lambdas ---
82
83 // On the operator() of a lambda's CXXMethodDecl
84 auto nl_lambda = []() [[clang::nonblocking]] {};
85 // CHECK: CXXMethodDecl {{.*}} operator() 'void () const __attribute__((nonblocking))' inline
86
87 // =========================================================================================
88 // Square brackets, false
89
90 void nl_func_false() [[clang::blocking]];
91 // CHECK: FunctionDecl {{.*}} nl_func_false 'void () __attribute__((blocking))'
92
93 auto nl_lambda_false = []() [[clang::blocking]] {};
94 // CHECK: CXXMethodDecl {{.*}} operator() 'void () const __attribute__((blocking))'
95
96 } // namespace square_brackets
97
98 // =========================================================================================
99 // GNU-style attribute, true
100
101 namespace gnu_style {
102
103 // 1. On the type of the FunctionDecl
104 void nl_function() __attribute__((nonblocking));
105 // CHECK: FunctionDecl {{.*}} nl_function 'void () __attribute__((nonblocking))'
106
107 // 1a. Alternate placement on the FunctionDecl
108 __attribute__((nonblocking)) void nl_function();
109 // CHECK: FunctionDecl {{.*}} nl_function 'void () __attribute__((nonblocking))'
110
111 // 2. On the type of the VarDecl holding a function pointer
112 void (*nl_func_a)() __attribute__((nonblocking));
113 // CHECK: VarDecl {{.*}} nl_func_a 'void (*)() __attribute__((nonblocking))'
114
115 // 2a. Alternate attribute placement on VarDecl
116 __attribute__((nonblocking)) void (*nl_func_b)();
117 // CHECK: VarDecl {{.*}} nl_func_b 'void (*)() __attribute__((nonblocking))'
118
119 // 3. On the type of the ParmVarDecl of a function parameter
120 static void nlReceiver(void (*nl_func)() __attribute__((nonblocking)));
121 // CHECK: ParmVarDecl {{.*}} nl_func 'void (*)() __attribute__((nonblocking))'
122
123 // 4. As an AttributedType within the nested types of a typedef
124 // Note different placement from square brackets for the typealias.
125 typedef void (*nl_fp_type)() __attribute__((nonblocking));
126 // CHECK: TypedefDecl {{.*}} nl_fp_type 'void (*)() __attribute__((nonblocking))'
127 using nl_fp_talias = __attribute__((nonblocking)) void (*)();
128 // CHECK: TypeAliasDecl {{.*}} nl_fp_talias 'void (*)() __attribute__((nonblocking))'
129
130 // 5. From a typedef or typealias, on a VarDecl
131 nl_fp_type nl_fp_var1;
132 // CHECK: VarDecl {{.*}} nl_fp_var1 'nl_fp_type':'void (*)() __attribute__((nonblocking))'
133 nl_fp_talias nl_fp_var2;
134 // CHECK: VarDecl {{.*}} nl_fp_var2 'nl_fp_talias':'void (*)() __attribute__((nonblocking))'
135
136 // 6. On type of a FieldDecl
137 struct Struct {
138 void (*nl_func_field)() __attribute__((nonblocking));
139 // CHECK: FieldDecl {{.*}} nl_func_field 'void (*)() __attribute__((nonblocking))'
140 };
141
142 } // namespace gnu_style
143
144 // =========================================================================================
145 // nonallocating and allocating - quick checks because the code paths are generally
146 // identical after parsing.
147
148 void na_function() [[clang::nonallocating]];
149 // CHECK: FunctionDecl {{.*}} na_function 'void () __attribute__((nonallocating))'
150
151 void na_true_function() [[clang::nonallocating(true)]];
152 // CHECK: FunctionDecl {{.*}} na_true_function 'void () __attribute__((nonallocating))'
153
154 void na_false_function() [[clang::nonallocating(false)]];
155 // CHECK: FunctionDecl {{.*}} na_false_function 'void () __attribute__((allocating))'
156
157 void alloc_function() [[clang::allocating]];
158 // CHECK: FunctionDecl {{.*}} alloc_function 'void () __attribute__((allocating))'
159
160
161 // =========================================================================================
162 // Non-blocking with an expression parameter
163
164 void t0() [[clang::nonblocking(1 - 1)]];
165 // CHECK: FunctionDecl {{.*}} t0 'void () __attribute__((blocking))'
166 void t1() [[clang::nonblocking(1 + 1)]];
167 // CHECK: FunctionDecl {{.*}} t1 'void () __attribute__((nonblocking))'
168
169 template <bool V>
170 struct ValueDependent {
171 void nb_method() [[clang::nonblocking(V)]];
172 };
173
174 void t3() [[clang::nonblocking]]
175 {
176 ValueDependent<false> x1;
177 x1.nb_method();
178 // CHECK: ClassTemplateSpecializationDecl {{.*}} ValueDependent
179 // CHECK: TemplateArgument integral 'false'
180 // CHECK: CXXMethodDecl {{.*}} nb_method 'void () __attribute__((blocking))'
181
182 ValueDependent<true> x2;
183 x2.nb_method();
184 // CHECK: ClassTemplateSpecializationDecl {{.*}} ValueDependent
185 // CHECK: TemplateArgument integral 'true'
186 // CHECK: CXXMethodDecl {{.*}} nb_method 'void () __attribute__((nonblocking))'
187 }
188
189 template <typename X>
190 struct TypeDependent {
191 void td_method() [[clang::nonblocking(X::is_nb)]];
192 };
193
194 struct NBPolicyTrue {
195 static constexpr bool is_nb = true;
196 };
197
198 struct NBPolicyFalse {
199 static constexpr bool is_nb = false;
200 };
201
202 void t4()
203 {
204 TypeDependent<NBPolicyFalse> x1;
205 x1.td_method();
206 // CHECK: ClassTemplateSpecializationDecl {{.*}} TypeDependent
207 // CHECK: TemplateArgument type 'NBPolicyFalse'
208 // CHECK: CXXMethodDecl {{.*}} td_method 'void () __attribute__((blocking))'
209
210 TypeDependent<NBPolicyTrue> x2;
211 x2.td_method();
212 // CHECK: ClassTemplateSpecializationDecl {{.*}} TypeDependent
213 // CHECK: TemplateArgument type 'NBPolicyTrue'
214 // CHECK: CXXMethodDecl {{.*}} td_method 'void () __attribute__((nonblocking))'
215 }
216