xref: /llvm-project/clang/test/SemaCXX/warn-shadow.cpp (revision 738a047ed6380efdfd4b64968881675347d7f915)
1 // RUN: %clang_cc1 -verify -fsyntax-only -std=c++17 -Wshadow-all %s
2 
3 namespace {
4   int i; // expected-note {{previous declaration is here}}
5   static int s; // expected-note {{previous declaration is here}}
6 }
7 
8 namespace one {
9 namespace two {
10   int j; // expected-note {{previous declaration is here}}
11   typedef int jj; // expected-note 2 {{previous declaration is here}}
12   using jjj=int; // expected-note 2 {{previous declaration is here}}
13 }
14 }
15 
16 namespace xx {
17   int m;
18   typedef int mm;
19   using mmm=int;
20 
21 }
22 namespace yy {
23   int m;
24   typedef char mm;
25   using mmm=char;
26 }
27 
28 using namespace one::two;
29 using namespace xx;
30 using namespace yy;
31 
32 void foo() {
33   int i; // expected-warning {{declaration shadows a variable in namespace '(anonymous)'}}
34   int j; // expected-warning {{declaration shadows a variable in namespace 'one::two'}}
35   static int s; // expected-warning {{declaration shadows a variable in namespace '(anonymous)'}}
36   int m;
37   int mm;
38   int mmm;
39 }
40 
41 class A {
42   static int data; // expected-note 1 {{previous declaration}}
43   // expected-note@+1 1 {{previous declaration}}
44   int field;
45   int f1, f2, f3, f4; // expected-note 9 {{previous declaration is here}}
46 
47   typedef int a1; // expected-note 2 {{previous declaration}}
48   using a2=int; // expected-note 2 {{previous declaration}}
49 
50   // The initialization is safe, but the modifications are not.
51   A(int f1, int f2, int f3, int f4) // expected-note-re 4 {{variable 'f{{[0-4]}}' is declared here}}
52 	  : f1(f1) {
53     f1 = 3; // expected-warning {{modifying constructor parameter 'f1' that shadows a field of 'A'}}
54     f1 = 4; // one warning per shadow
55     f2++; // expected-warning {{modifying constructor parameter 'f2' that shadows a field of 'A'}}
56     --f3; // expected-warning {{modifying constructor parameter 'f3' that shadows a field of 'A'}}
57     f4 += 2; // expected-warning {{modifying constructor parameter 'f4' that shadows a field of 'A'}}
58   }
59 
60   // The initialization is safe, but the modifications are not.
61   // expected-warning-re@+1 4 {{constructor parameter 'f{{[0-4]}}' shadows the field 'f{{[0-9]}}' of 'A'}}
62   A(int f1, int f2, int f3, int f4, double overload_dummy) {}
63 
64   void test() {
65     char *field; // expected-warning {{declaration shadows a field of 'A'}}
66     char *data; // expected-warning {{declaration shadows a static data member of 'A'}}
67     char *a1; // no warning
68     char *a2; // no warning
69     char *jj; // no warning
70     char *jjj; // no warning
71     static char *f1; // expected-warning {{declaration shadows a field of 'A'}}
72   }
73 
74   void test2() {
75     typedef char field; // no warning
76     typedef char data; // no warning
77     typedef char a1; // expected-warning {{declaration shadows a typedef in 'A'}}
78     typedef char a2; // expected-warning {{declaration shadows a type alias in 'A'}}
79     typedef char jj; // expected-warning {{declaration shadows a typedef in namespace 'one::two'}}
80     typedef char jjj; // expected-warning {{declaration shadows a type alias in namespace 'one::two'}}
81   }
82 
83   void test3() {
84     using field=char; // no warning
85     using data=char; // no warning
86     using a1=char; // expected-warning {{declaration shadows a typedef in 'A'}}
87     using a2=char; // expected-warning {{declaration shadows a type alias in 'A'}}
88     using jj=char; // expected-warning {{declaration shadows a typedef in namespace 'one::two'}}
89     using jjj=char; // expected-warning {{declaration shadows a type alias in namespace 'one::two'}}
90   }
91 };
92 
93 struct path {
94   using value_type = char;
95   typedef char value_type2;
96   struct iterator {
97     using value_type = path; // no warning
98     typedef path value_type2; // no warning
99   };
100 };
101 
102 
103 // TODO: this should warn
104 class B : A {
105   int data;
106   static int field;
107 };
108 
109 namespace rdar8900456 {
110 struct Foo {
111   static void Baz();
112   static void Baz1();
113   static void Baz2();
114 private:
115   int Bar;
116 };
117 
118 void Foo::Baz() {
119   double Bar = 12; // Don't warn.
120 }
121 
122 void Foo::Baz1() {
123   typedef int Bar; // Don't warn.
124 }
125 
126 void Foo::Baz2() {
127   using Bar=int; // Don't warn.
128 }
129 }
130 
131 // http://llvm.org/PR9160
132 namespace PR9160 {
133 struct V {
134   V(int);
135 };
136 struct S {
137   V v;
138   static void m() {
139     if (1) {
140       V v(0);
141     }
142   }
143 };
144 }
145 
146 extern int bob; // expected-note 1 {{previous declaration is here}}
147 typedef int bob1; // expected-note 2 {{previous declaration is here}}
148 using bob2=int; // expected-note 2 {{previous declaration is here}}
149 
150 void rdar8883302() {
151   extern int bob; // don't warn for shadowing.
152 }
153 
154 void test8() {
155   int bob; // expected-warning {{declaration shadows a variable in the global namespace}}
156   int bob1; //no warning
157   int bob2; // no warning
158 }
159 
160 void test9() {
161   typedef int bob; // no warning
162   typedef int bob1; // expected-warning {{declaration shadows a typedef in the global namespace}}
163   typedef int bob2; // expected-warning {{declaration shadows a type alias in the global namespace}}
164 }
165 
166 void test10() {
167   using bob=int; // no warning
168   using bob1=int; // expected-warning {{declaration shadows a typedef in the global namespace}}
169   using bob2=int; // expected-warning {{declaration shadows a type alias in the global namespace}}
170 }
171 
172 namespace rdar29067894 {
173 
174 void avoidWarningWhenRedefining(int b) { // expected-note {{previous definition is here}}
175   int a = 0; // expected-note {{previous definition is here}}
176   int a = 1; // expected-error {{redefinition of 'a'}}
177   int b = 2; // expected-error {{redefinition of 'b'}}
178 
179   using c=char; // expected-note {{previous definition is here}}
180   using c=int; // expected-error {{type alias redefinition with different types ('int' vs 'char')}}
181 
182   typedef char d; // expected-note {{previous definition is here}}
183   typedef int d; // expected-error {{typedef redefinition with different types ('int' vs 'char')}}
184 
185   using e=char; // expected-note {{previous definition is here}}
186   typedef int e; // expected-error {{type alias redefinition with different types ('int' vs 'char')}}
187 
188   int f; // expected-note {{previous definition is here}}
189   using f=int; // expected-error {{redefinition of 'f'}}
190 
191   using g=int; // expected-note {{previous definition is here}}
192   int g; // expected-error {{redefinition of 'g'}}
193 
194   typedef int h; // expected-note {{previous definition is here}}
195   int h; // expected-error {{redefinition of 'h'}}
196 
197   int k; // expected-note {{previous definition is here}}
198   typedef int k; // expected-error {{redefinition of 'k'}}
199 
200   using l=char; // no warning or error.
201   using l=char; // no warning or error.
202   typedef char l; // no warning or error.
203 
204   typedef char n; // no warning or error.
205   typedef char n; // no warning or error.
206   using n=char; // no warning or error.
207 }
208 
209 }
210 
211 extern "C" {
212 typedef int externC; // expected-note {{previous declaration is here}}
213 }
214 void handleLinkageSpec() {
215   typedef void externC; // expected-warning {{declaration shadows a typedef in the global namespace}}
216 }
217 
218 namespace PR33947 {
219 void f(int a) {
220   struct A {
221     void g(int a) {}
222     A() { int a; }
223   };
224 }
225 }
226 
227 namespace PR34120 {
228 struct A {
229   int B; // expected-note 2 {{declared here}}
230 };
231 
232 class C : public A {
233   void D(int B) {} // expected-warning {{parameter 'B' shadows member inherited from type 'A'}}
234   void E() {
235     extern void f(int B); // Ok
236   }
237   void F(int B); // Ok, declaration; not definition.
238   void G(int B);
239 };
240 
241 void C::G(int B) { // expected-warning {{parameter 'B' shadows member inherited from type 'A'}}
242 }
243 
244 class Private {
245   int B;
246 };
247 class Derived : Private {
248   void D(int B) {} // Ok
249 };
250 
251 struct Static {
252   static int B;
253 };
254 
255 struct Derived2 : Static {
256   void D(int B) {}
257 };
258 }
259 
260 int PR24718;
261 enum class X { PR24718 }; // Ok, not shadowing
262 
263 struct PR24718_1;
264 struct PR24718_2 {
265   enum {
266     PR24718_1 // Does not shadow a type.
267   };
268 };
269 
270 namespace structured_binding_tests {
271 int x; // expected-note {{previous declaration is here}}
272 int y; // expected-note {{previous declaration is here}}
273 struct S {
274   int a, b;
275 };
276 
277 void test1() {
278   const auto [x, y] = S(); // expected-warning 2 {{declaration shadows a variable in namespace 'structured_binding_tests'}}
279 }
280 
281 void test2() {
282   int a; // expected-note {{previous declaration is here}}
283   bool b; // expected-note {{previous declaration is here}}
284   {
285     auto [a, b] = S(); // expected-warning 2 {{declaration shadows a local variable}}
286   }
287 }
288 
289 class A
290 {
291   int m_a; // expected-note {{previous declaration is here}}
292   int m_b; // expected-note {{previous declaration is here}}
293 
294   void test3() {
295     auto [m_a, m_b] = S(); // expected-warning 2 {{declaration shadows a field of 'structured_binding_tests::A'}}
296   }
297 };
298 
299 void test4() {
300   const auto [a, b] = S(); // expected-note 3 {{previous declaration is here}}
301   {
302     int a = 4; // expected-warning {{declaration shadows a structured binding}}
303   }
304   {
305     const auto [a, b] = S(); // expected-warning 2 {{declaration shadows a structured binding}}
306   }
307 }
308 
309 }; // namespace structured_binding_tests
310 
311 namespace GH62588 {
312 class Outer {
313 public:
314   char *foo();          // expected-note {{previous declaration is here}} \
315                         // expected-note {{previous definition is here}}
316   enum Outer_E { foo }; // expected-error {{redefinition of 'foo'}} \
317                         // expected-warning {{declaration shadows a static data member of 'GH62588::Outer'}}
318   class Inner {
319   public:
320     enum Inner_E { foo }; // ok
321   };
322 };
323 } // namespace GH62588
324