[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / warn-shadow.cpp
blob4e1a3c393a9dbb919d136cc1fc0525104b026985
1 // RUN: %clang_cc1 -verify -fsyntax-only -std=c++17 -Wshadow-all %s
3 namespace {
4 int i; // expected-note {{previous declaration is here}}
7 namespace one {
8 namespace two {
9 int j; // expected-note {{previous declaration is here}}
10 typedef int jj; // expected-note 2 {{previous declaration is here}}
11 using jjj=int; // expected-note 2 {{previous declaration is here}}
15 namespace xx {
16 int m;
17 typedef int mm;
18 using mmm=int;
21 namespace yy {
22 int m;
23 typedef char mm;
24 using mmm=char;
27 using namespace one::two;
28 using namespace xx;
29 using namespace yy;
31 void foo() {
32 int i; // expected-warning {{declaration shadows a variable in namespace '(anonymous)'}}
33 int j; // expected-warning {{declaration shadows a variable in namespace 'one::two'}}
34 int m;
35 int mm;
36 int mmm;
39 class A {
40 static int data; // expected-note 1 {{previous declaration}}
41 // expected-note@+1 1 {{previous declaration}}
42 int field;
43 int f1, f2, f3, f4; // expected-note 8 {{previous declaration is here}}
45 typedef int a1; // expected-note 2 {{previous declaration}}
46 using a2=int; // expected-note 2 {{previous declaration}}
48 // The initialization is safe, but the modifications are not.
49 A(int f1, int f2, int f3, int f4) // expected-note-re 4 {{variable 'f{{[0-4]}}' is declared here}}
50 : f1(f1) {
51 f1 = 3; // expected-warning {{modifying constructor parameter 'f1' that shadows a field of 'A'}}
52 f1 = 4; // one warning per shadow
53 f2++; // expected-warning {{modifying constructor parameter 'f2' that shadows a field of 'A'}}
54 --f3; // expected-warning {{modifying constructor parameter 'f3' that shadows a field of 'A'}}
55 f4 += 2; // expected-warning {{modifying constructor parameter 'f4' that shadows a field of 'A'}}
58 // The initialization is safe, but the modifications are not.
59 // expected-warning-re@+1 4 {{constructor parameter 'f{{[0-4]}}' shadows the field 'f{{[0-9]}}' of 'A'}}
60 A(int f1, int f2, int f3, int f4, double overload_dummy) {}
62 void test() {
63 char *field; // expected-warning {{declaration shadows a field of 'A'}}
64 char *data; // expected-warning {{declaration shadows a static data member of 'A'}}
65 char *a1; // no warning
66 char *a2; // no warning
67 char *jj; // no warning
68 char *jjj; // no warning
71 void test2() {
72 typedef char field; // no warning
73 typedef char data; // no warning
74 typedef char a1; // expected-warning {{declaration shadows a typedef in 'A'}}
75 typedef char a2; // expected-warning {{declaration shadows a type alias in 'A'}}
76 typedef char jj; // expected-warning {{declaration shadows a typedef in namespace 'one::two'}}
77 typedef char jjj; // expected-warning {{declaration shadows a type alias in namespace 'one::two'}}
80 void test3() {
81 using field=char; // no warning
82 using data=char; // no warning
83 using a1=char; // expected-warning {{declaration shadows a typedef in 'A'}}
84 using a2=char; // expected-warning {{declaration shadows a type alias in 'A'}}
85 using jj=char; // expected-warning {{declaration shadows a typedef in namespace 'one::two'}}
86 using jjj=char; // expected-warning {{declaration shadows a type alias in namespace 'one::two'}}
90 struct path {
91 using value_type = char;
92 typedef char value_type2;
93 struct iterator {
94 using value_type = path; // no warning
95 typedef path value_type2; // no warning
100 // TODO: this should warn, <rdar://problem/5018057>
101 class B : A {
102 int data;
103 static int field;
106 // rdar://8900456
107 namespace rdar8900456 {
108 struct Foo {
109 static void Baz();
110 static void Baz1();
111 static void Baz2();
112 private:
113 int Bar;
116 void Foo::Baz() {
117 double Bar = 12; // Don't warn.
120 void Foo::Baz1() {
121 typedef int Bar; // Don't warn.
124 void Foo::Baz2() {
125 using Bar=int; // Don't warn.
129 // http://llvm.org/PR9160
130 namespace PR9160 {
131 struct V {
132 V(int);
134 struct S {
135 V v;
136 static void m() {
137 if (1) {
138 V v(0);
144 extern int bob; // expected-note 1 {{previous declaration is here}}
145 typedef int bob1; // expected-note 2 {{previous declaration is here}}
146 using bob2=int; // expected-note 2 {{previous declaration is here}}
148 // rdar://8883302
149 void rdar8883302() {
150 extern int bob; // don't warn for shadowing.
153 void test8() {
154 int bob; // expected-warning {{declaration shadows a variable in the global namespace}}
155 int bob1; //no warning
156 int bob2; // no warning
159 void test9() {
160 typedef int bob; // no warning
161 typedef int bob1; // expected-warning {{declaration shadows a typedef in the global namespace}}
162 typedef int bob2; // expected-warning {{declaration shadows a type alias in the global namespace}}
165 void test10() {
166 using bob=int; // no warning
167 using bob1=int; // expected-warning {{declaration shadows a typedef in the global namespace}}
168 using bob2=int; // expected-warning {{declaration shadows a type alias in the global namespace}}
171 namespace rdar29067894 {
173 void avoidWarningWhenRedefining(int b) { // expected-note {{previous definition is here}}
174 int a = 0; // expected-note {{previous definition is here}}
175 int a = 1; // expected-error {{redefinition of 'a'}}
176 int b = 2; // expected-error {{redefinition of 'b'}}
178 using c=char; // expected-note {{previous definition is here}}
179 using c=int; // expected-error {{type alias redefinition with different types ('int' vs 'char')}}
181 typedef char d; // expected-note {{previous definition is here}}
182 typedef int d; // expected-error {{typedef redefinition with different types ('int' vs 'char')}}
184 using e=char; // expected-note {{previous definition is here}}
185 typedef int e; // expected-error {{type alias redefinition with different types ('int' vs 'char')}}
187 int f; // expected-note {{previous definition is here}}
188 using f=int; // expected-error {{redefinition of 'f'}}
190 using g=int; // expected-note {{previous definition is here}}
191 int g; // expected-error {{redefinition of 'g'}}
193 typedef int h; // expected-note {{previous definition is here}}
194 int h; // expected-error {{redefinition of 'h'}}
196 int k; // expected-note {{previous definition is here}}
197 typedef int k; // expected-error {{redefinition of 'k'}}
199 using l=char; // no warning or error.
200 using l=char; // no warning or error.
201 typedef char l; // no warning or error.
203 typedef char n; // no warning or error.
204 typedef char n; // no warning or error.
205 using n=char; // no warning or error.
210 extern "C" {
211 typedef int externC; // expected-note {{previous declaration is here}}
213 void handleLinkageSpec() {
214 typedef void externC; // expected-warning {{declaration shadows a typedef in the global namespace}}
217 namespace PR33947 {
218 void f(int a) {
219 struct A {
220 void g(int a) {}
221 A() { int a; }
226 namespace PR34120 {
227 struct A {
228 int B; // expected-note 2 {{declared here}}
231 class C : public A {
232 void D(int B) {} // expected-warning {{parameter 'B' shadows member inherited from type 'A'}}
233 void E() {
234 extern void f(int B); // Ok
236 void F(int B); // Ok, declaration; not definition.
237 void G(int B);
240 void C::G(int B) { // expected-warning {{parameter 'B' shadows member inherited from type 'A'}}
243 class Private {
244 int B;
246 class Derived : Private {
247 void D(int B) {} // Ok
250 struct Static {
251 static int B;
254 struct Derived2 : Static {
255 void D(int B) {}
259 int PR24718;
260 enum class X { PR24718 }; // Ok, not shadowing
262 struct PR24718_1;
263 struct PR24718_2 {
264 enum {
265 PR24718_1 // Does not shadow a type.
269 namespace structured_binding_tests {
270 int x; // expected-note {{previous declaration is here}}
271 int y; // expected-note {{previous declaration is here}}
272 struct S {
273 int a, b;
276 void test1() {
277 const auto [x, y] = S(); // expected-warning 2 {{declaration shadows a variable in namespace 'structured_binding_tests'}}
280 void test2() {
281 int a; // expected-note {{previous declaration is here}}
282 bool b; // expected-note {{previous declaration is here}}
284 auto [a, b] = S(); // expected-warning 2 {{declaration shadows a local variable}}
288 class A
290 int m_a; // expected-note {{previous declaration is here}}
291 int m_b; // expected-note {{previous declaration is here}}
293 void test3() {
294 auto [m_a, m_b] = S(); // expected-warning 2 {{declaration shadows a field of 'structured_binding_tests::A'}}
298 void test4() {
299 const auto [a, b] = S(); // expected-note 3 {{previous declaration is here}}
301 int a = 4; // expected-warning {{declaration shadows a structured binding}}
304 const auto [a, b] = S(); // expected-warning 2 {{declaration shadows a structured binding}}
308 }; // namespace structured_binding_tests