Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / cxx2c-placeholder-vars.cpp
blob5cf66b48784e917cdf6109cb3a80b0f0483e8021
1 // RUN: %clang -cc1 -fsyntax-only -verify -std=c++2c -Wunused-parameter -Wunused -Wpre-c++26-compat %s
3 void static_var() {
4 static int _; // expected-note {{previous definition is here}} \
5 // expected-note {{candidate}}
6 static int _; // expected-error {{redefinition of '_'}}
7 int _; // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
8 // expected-note {{candidate}}
9 _++; // expected-error{{reference to '_' is ambiguous}}
12 void static_var_2() {
13 int _; // expected-note {{previous definition is here}}
14 static int _; // expected-error {{redefinition of '_'}}
17 void bindings() {
18 int arr[4] = {0, 1, 2, 3};
19 auto [_, _, _, _] = arr; // expected-warning 3{{placeholder variables are incompatible with C++ standards before C++2c}} \
20 // expected-note 4{{placeholder declared here}}
21 _ == 42; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
23 // no extension warning as we only introduce a single placeholder.
24 auto [_, a, b, c] = arr; // expected-warning {{unused variable '[_, a, b, c]'}}
27 auto [_, _, b, c] = arr; // expected-warning {{unused variable '[_, _, b, c]'}} \
28 // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}}
31 // There are only 3 extension warnings because the first
32 // introduction of `_` is valid in all C++ standards
33 auto [_, _, _, _] = arr; // expected-warning 3{{placeholder variables are incompatible with C++ standards before C++2c}}
37 namespace StaticBindings {
39 int arr[2] = {0, 1};
40 static auto [_, _] = arr; // expected-error {{redefinition of '_'}} \
41 // expected-note {{previous definition is here}}
43 void f() {
44 int arr[2] = {0, 1};
45 static auto [_, _] = arr; // expected-error {{redefinition of '_'}} \
46 // expected-note {{previous definition is here}}
51 void lambda() {
52 (void)[_ = 0, _ = 1] { // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
53 // expected-note 4{{placeholder declared here}}
54 (void)_++; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
58 int _ = 12;
59 (void)[_ = 0]{}; // no warning (different scope)
63 namespace global_var {
64 int _; // expected-note {{previous definition is here}}
65 int _; // expected-error {{redefinition of '_'}}
68 namespace {
69 int _; // expected-note {{previous definition is here}}
70 int _; // expected-error {{redefinition of '_'}}
74 namespace global_fun {
75 void _();
76 void _();
78 void _() {} // expected-note {{previous definition is here}}
79 void _() {} // expected-error {{redefinition of '_'}}
80 void _(int){}
83 typedef int _;
84 typedef int _; // Type redeclaration, nothing to do with placeholders
86 void extern_test() {
87 extern int _;
88 extern int _; // expected-note {{candidate}}
89 int _; //expected-note {{candidate}}
90 _++; // expected-error {{reference to '_' is ambiguous}}
94 struct Members {
95 int _; // expected-note 2{{placeholder declared here}}
96 int _; // expected-warning{{placeholder variables are incompatible with C++ standards before C++2c}} \
97 // expected-note 2{{placeholder declared here}}
98 void f() {
99 _++; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
101 void attributes() __attribute__((diagnose_if(_ != 0, "oh no!", "warning"))); // expected-error{{ambiguous reference to placeholder '_', which is defined multiple times}}
104 namespace using_ {
105 int _; // expected-note {{target of using declaration}}
106 void f() {
107 int _; // expected-note {{conflicting declaration}}
108 _ = 0;
109 using using_::_; // expected-error {{target of using declaration conflicts with declaration already in scope}}
114 void call(int);
115 void test_param(int _) {}
116 void test_params(int _, int _); // expected-error {{redefinition of parameter '_'}} \
117 // expected-note {{previous declaration is here}}
119 template <auto _, auto _> // expected-error {{declaration of '_' shadows template parameter}} \
120 // expected-note {{template parameter is declared here}}
121 auto i = 0;
123 template <typename T>
124 concept C = requires(T _, T _) { // expected-error {{redefinition of parameter '_'}} \
125 // expected-note {{previous declaration is here}}
126 T{};
129 struct S {
130 int a;
133 void f(S a, S _) { // expected-warning {{unused parameter 'a'}}
137 void unused_warning() {
138 int _ = 12; // placeholder variable, no unused-but-set warning
139 int x = 12; // expected-warning {{unused variable 'x'}}
140 int _ = 12; // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}}
143 struct ShadowMembers {
144 int _;
145 void f() {
146 int _;
147 _ = 12; // Ok, access the local variable
148 (void)({ int _ = 12; _;}); // Ok, inside a different scope
152 struct MemberPtrs {
153 int _, _; // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
154 // expected-note 4{{placeholder declared here}}
156 constexpr int oh_no = __builtin_offsetof(MemberPtrs, _); // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
157 int MemberPtrs::* ref = &MemberPtrs::_; // expected-error{{ambiguous reference to placeholder '_', which is defined multiple times}}
160 struct MemberInitializer {
161 MemberInitializer() : _(0) {} // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
162 int _, _; // expected-note 2{{placeholder declared here}} \
163 // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}}
166 struct MemberAndUnion {
167 int _; // expected-note {{placeholder declared here}}
168 union { int _; int _; }; // expected-note 2 {{placeholder declared here}} \
169 // expected-warning 2{{placeholder variables are incompatible with C++ standards before C++2c}}
172 MemberAndUnion() : _(0) {} // expected-error {{ambiguous reference to placeholder '_', which is defined multiple time}}
175 struct Union { union { int _, _, _; }; }; // expected-note 3{{placeholder declared here}} \
176 // expected-warning 2{{placeholder variables are incompatible with C++ standards before C++2c}}
178 void TestUnion() {
179 Union c;
180 c._ = 0; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
183 void AnonymousLocals() {
184 union {int _, _;}; // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
185 // expected-note 2{{placeholder declared here}}
186 union {int _, _;}; // expected-warning 2{{placeholder variables are incompatible with C++ standards before C++2c}} \
187 // expected-note 2{{placeholder declared here}}
188 _. = 0; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
191 namespace StaticUnions {
193 static union { int _ = 42; }; // expected-note {{previous declaration is here}}
194 static union { int _ = 43; }; // expected-error {{member of anonymous union redeclares '_'}}
196 inline void StaticUnion() {
197 static union { int _{}; }; // expected-note {{previous declaration is here}}
198 static union { int _{}; }; // expected-error {{member of anonymous union redeclares '_'}}
203 namespace TagVariables {
205 [[maybe_unused]] struct {
206 int _, _, _; // expected-warning 2{{placeholder variables are incompatible with C++ standards before C++2c}}
207 } a;
209 [[maybe_unused]] union {
210 int _, _, _; // expected-warning 2{{placeholder variables are incompatible with C++ standards before C++2c}}
211 } b;
215 namespace MemberLookupTests {
217 struct S {
218 int _, _; // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
219 // expected-note 8{{placeholder declared here}}
221 void f() {
222 _ ++ ; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
226 struct T : S {
230 void Test() {
231 S s{._ =0}; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
232 S{}._; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
233 T{}._; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
238 namespace Bases {
239 struct S {
240 int _, _; // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
241 // expected-note 2{{placeholder declared here}}
242 int a;
244 struct T : S{
245 int _, _; // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
246 // expected-note 2{{placeholder declared here}}
247 int a;
248 void f() {
249 _; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
250 S::_; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}} \
251 // expected-error {{a type specifier is required for all declarations}}