Debian Codegraph @

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

raw
1 // RUN: %clang_cc1 -fsyntax-only -fblocks -fcxx-exceptions -verify -Wfunction-effects -Wfunction-effect-redeclarations %s
2 // RUN: %clang_cc1 -fsyntax-only -fblocks -verify -x c -std=c23 -Wfunction-effects -Wfunction-effect-redeclarations %s
3
4 #if !__has_attribute(nonblocking)
5 #error "the 'nonblocking' attribute is not available"
6 #endif
7
8 // --- ATTRIBUTE SYNTAX: SUBJECTS ---
9
10 int nl_var [[clang::nonblocking]]; // expected-warning {{'nonblocking' only applies to function types; type here is 'int'}}
11 struct nl_struct {} [[clang::nonblocking]]; // expected-warning {{attribute 'clang::nonblocking' is ignored, place it after "struct" to apply attribute to type declaration}}
12 struct [[clang::nonblocking]] nl_struct2 {}; // expected-error {{'clang::nonblocking' attribute cannot be applied to a declaration}}
13
14 // Positive case
15 typedef void (*fo)() [[clang::nonblocking]];
16 void (*read_me_and_weep(
17 int val, void (*func)(int) [[clang::nonblocking]])
18 [[clang::nonblocking]]) (int)
19 [[clang::nonblocking]];
20
21 // --- ATTRIBUTE SYNTAX: ARGUMENT COUNT ---
22 void nargs_1() [[clang::nonblocking(1, 2)]]; // expected-error {{'clang::nonblocking' attribute takes no more than 1 argument}}
23 void nargs_2() [[clang::nonallocating(1, 2)]]; // expected-error {{'clang::nonallocating' attribute takes no more than 1 argument}}
24 void nargs_3() [[clang::blocking(1)]]; // expected-error {{'clang::blocking' attribute takes no arguments}}
25 void nargs_4() [[clang::allocating(1)]]; // expected-error {{'clang::allocating' attribute takes no arguments}}
26
27 // --- ATTRIBUTE SYNTAX: COMBINATIONS ---
28 // Check invalid combinations of nonblocking/nonallocating attributes
29
30 void nl_true_false_1() [[clang::nonblocking(true)]] [[clang::blocking]]; // expected-error {{'blocking' and 'nonblocking' attributes are not compatible}}
31 void nl_true_false_2() [[clang::blocking]] [[clang::nonblocking(true)]]; // expected-error {{'nonblocking' and 'blocking' attributes are not compatible}}
32
33 void nl_true_false_3() [[clang::nonblocking, clang::blocking]]; // expected-error {{'blocking' and 'nonblocking' attributes are not compatible}}
34 void nl_true_false_4() [[clang::blocking, clang::nonblocking]]; // expected-error {{'nonblocking' and 'blocking' attributes are not compatible}}
35
36 void na_true_false_1() [[clang::nonallocating(true)]] [[clang::allocating]]; // expected-error {{'allocating' and 'nonallocating' attributes are not compatible}}
37 void na_true_false_2() [[clang::allocating]] [[clang::nonallocating(true)]]; // expected-error {{'nonallocating' and 'allocating' attributes are not compatible}}
38
39 void na_true_false_3() [[clang::nonallocating, clang::allocating]]; // expected-error {{'allocating' and 'nonallocating' attributes are not compatible}}
40 void na_true_false_4() [[clang::allocating, clang::nonallocating]]; // expected-error {{'nonallocating' and 'allocating' attributes are not compatible}}
41
42 void nl_true_na_true_1() [[clang::nonblocking]] [[clang::nonallocating]];
43 void nl_true_na_true_2() [[clang::nonallocating]] [[clang::nonblocking]];
44
45 void nl_true_na_false_1() [[clang::nonblocking]] [[clang::allocating]]; // expected-error {{'allocating' and 'nonblocking' attributes are not compatible}}
46 void nl_true_na_false_2() [[clang::allocating]] [[clang::nonblocking]]; // expected-error {{'nonblocking' and 'allocating' attributes are not compatible}}
47
48 void nl_false_na_true_1() [[clang::blocking]] [[clang::nonallocating]];
49 void nl_false_na_true_2() [[clang::nonallocating]] [[clang::blocking]];
50
51 void nl_false_na_false_1() [[clang::blocking]] [[clang::allocating]];
52 void nl_false_na_false_2() [[clang::allocating]] [[clang::blocking]];
53
54 // --- TYPE CONVERSIONS ---
55
56 void unannotated();
57 void nonblocking() [[clang::nonblocking]];
58 void nonallocating() [[clang::nonallocating]];
59 void type_conversions()
60 {
61 // It's fine to remove a performance constraint.
62 void (*fp_plain)();
63
64 fp_plain = nullptr;
65 fp_plain = unannotated;
66 fp_plain = nonblocking;
67 fp_plain = nonallocating;
68
69 // Adding/spoofing nonblocking is unsafe.
70 void (*fp_nonblocking)() [[clang::nonblocking]];
71 fp_nonblocking = nullptr;
72 fp_nonblocking = nonblocking;
73 fp_nonblocking = unannotated; // expected-warning {{attribute 'nonblocking' should not be added via type conversion}}
74 fp_nonblocking = nonallocating; // expected-warning {{attribute 'nonblocking' should not be added via type conversion}}
75
76 // Adding/spoofing nonallocating is unsafe.
77 void (*fp_nonallocating)() [[clang::nonallocating]];
78 fp_nonallocating = nullptr;
79 fp_nonallocating = nonallocating;
80 fp_nonallocating = nonblocking; // no warning because nonblocking includes nonallocating
81 fp_nonallocating = unannotated; // expected-warning {{attribute 'nonallocating' should not be added via type conversion}}
82 }
83
84 #ifdef __cplusplus
85 struct PTMF {
86 void unannotated();
87 void nonblocking() [[clang::nonblocking]];
88 void nonallocating() [[clang::nonallocating]];
89 };
90
91 void type_conversions_ptmf()
92 {
93 // It's fine to remove a performance constraint.
94 void (PTMF::*ptmf_plain)() = nullptr;
95
96 ptmf_plain = &PTMF::unannotated;
97 ptmf_plain = &PTMF::nonblocking;
98 ptmf_plain = &PTMF::nonallocating;
99
100 // Adding/spoofing nonblocking is unsafe.
101 void (PTMF::*fp_nonblocking)() [[clang::nonblocking]] = nullptr;
102 fp_nonblocking = &PTMF::nonblocking;
103 fp_nonblocking = &PTMF::unannotated; // expected-warning {{attribute 'nonblocking' should not be added via type conversion}}
104 fp_nonblocking = &PTMF::nonallocating; // expected-warning {{attribute 'nonblocking' should not be added via type conversion}}
105
106 // Adding/spoofing nonallocating is unsafe.
107 void (PTMF::*fp_nonallocating)() [[clang::nonallocating]] = nullptr;
108 fp_nonallocating = &PTMF::nonallocating;
109 fp_nonallocating = &PTMF::nonblocking; // no warning because nonblocking includes nonallocating fp_nonallocating = unannotated;
110 fp_nonallocating = &PTMF::unannotated; // expected-warning {{attribute 'nonallocating' should not be added via type conversion}}
111 }
112
113 // There was a bug: noexcept and nonblocking could be individually removed in conversion, but not both
114 void type_conversions_2()
115 {
116 auto receives_fp = [](void (*fp)()) {
117 };
118
119 auto ne = +[]() noexcept {};
120 auto nl = +[]() [[clang::nonblocking]] {};
121 auto nl_ne = +[]() noexcept [[clang::nonblocking]] {};
122
123 receives_fp(ne);
124 receives_fp(nl);
125 receives_fp(nl_ne);
126 }
127 #endif
128
129 // --- VIRTUAL METHODS ---
130 // Attributes propagate to overridden methods.
131 // Check this in the syntax tests too.
132 #ifdef __cplusplus
133 struct Base {
134 virtual void f1();
135 virtual void nonblocking() noexcept [[clang::nonblocking]]; // expected-note {{overridden virtual function is here}}
136 virtual void nonallocating() noexcept [[clang::nonallocating]]; // expected-note {{overridden virtual function is here}}
137 virtual void f2() [[clang::nonallocating]]; // expected-note {{previous declaration is here}}
138 virtual void f3() [[clang::nonblocking]]; // expected-note {{overridden virtual function is here}}
139 };
140
141 struct Derived : public Base {
142 void f1() [[clang::nonblocking]] override;
143 void nonblocking() noexcept override; // expected-warning {{overriding function is missing 'nonblocking' attribute from base declaration}}
144 void nonallocating() noexcept override; // expected-warning {{overriding function is missing 'nonallocating' attribute from base declaration}}
145 void f2() [[clang::allocating]] override; // expected-warning {{effects conflict when merging declarations; kept 'allocating', discarded 'nonallocating'}}
146 };
147
148 template <bool B>
149 struct TDerived : public Base {
150 void f3() [[clang::nonblocking(B)]] override; // expected-warning {{attribute 'nonblocking' on overriding function conflicts with base declaration}}
151 };
152 #endif // __cplusplus
153
154 // --- REDECLARATIONS ---
155
156 void f2();
157 void f2() [[clang::nonblocking]]; // expected-note {{previous declaration is here}}
158 void f2(); // expected-warning {{redeclaration is missing 'nonblocking' attribute from previous declaration}}
159 // Note: we verify that the attribute is actually seen during the constraints tests.
160
161 void f3() [[clang::blocking]]; // expected-note {{previous declaration is here}}
162 void f3() [[clang::nonblocking]]; // expected-warning {{effects conflict when merging declarations; kept 'blocking', discarded 'nonblocking'}}
163
164 // --- OVERLOADS ---
165 #ifdef __cplusplus
166 struct S {
167 void foo(); // expected-note {{previous declaration is here}}
168 void foo() [[clang::nonblocking]]; // expected-error {{class member cannot be redeclared}}
169 };
170 #endif // __cplusplus
171
172 // --- COMPUTED NONBLOCKING ---
173 void f4() [[clang::nonblocking(__builtin_memset)]] {} // expected-error {{nonblocking attribute requires an integer constant}}
174
175 #ifdef __cplusplus
176 // Unexpanded parameter pack
177 template <bool ...val>
178 void f5() [[clang::nonblocking(val /* NO ... here */)]] {} // expected-error {{expression contains unexpanded parameter pack 'val'}}
179
180 void f6() { f5<true, false>(); }
181
182 template <bool B>
183 void ambiguous() [[clang::nonblocking(B)]] [[clang::blocking]]; // expected-note {{candidate template ignored: substitution failure [with B = true]: 'blocking' and 'nonblocking' attributes are not compatible}}
184
185 void f7() {
186 ambiguous<true>(); // expected-error {{no matching function for call to 'ambiguous'}}
187 ambiguous<false>();
188 }
189 #endif // __cplusplus