xref: /llvm-project/clang/test/Sema/warn-shadow.c (revision 29dc47a9eeeb2e080170109e3e2fb3cd5aad58d2)
1 // RUN: %clang_cc1 -verify -fsyntax-only -fblocks -Wshadow %s
2 
3 int i;          // expected-note 4 {{previous declaration is here}}
4 static int s;   // expected-note 2 {{previous declaration is here}}
5 
6 void foo(void) {
7   int pass1;
8   int i;        // expected-warning {{declaration shadows a variable in the global scope}} \
9                 // expected-note {{previous declaration is here}}
10   int s;        // expected-warning {{declaration shadows a variable in the global scope}} \
11                 // expected-note {{previous declaration is here}}
12   {
13     int pass2;
14     int i;      // expected-warning {{declaration shadows a local variable}} \
15                 // expected-note {{previous declaration is here}}
16     int s;      // expected-warning {{declaration shadows a local variable}} \
17                 // expected-note {{previous declaration is here}}
18     {
19       int pass3;
20       int i;    // expected-warning {{declaration shadows a local variable}}
21       int s;    // expected-warning {{declaration shadows a local variable}}
22     }
23   }
24 
25   int sin; // okay; 'sin' has not been declared, even though it's a builtin.
26 }
27 
28 // <rdar://problem/7677531>
29 void (^test1)(int) = ^(int i) { // expected-warning {{declaration shadows a variable in the global scope}} \
30                                  // expected-note{{previous declaration is here}}
31   {
32     int i; // expected-warning {{declaration shadows a local variable}} \
33            // expected-note{{previous declaration is here}}
34 
35     (^(int i) { return i; })(i); //expected-warning {{declaration shadows a local variable}}
36   }
37 };
38 
39 
40 struct test2 {
41   int i;
42 };
43 
44 void test3(void) {
45   struct test4 {
46     int i;
47   };
48 }
49 
50 void test4(int i) { // expected-warning {{declaration shadows a variable in the global scope}}
51 }
52 
53 // Don't warn about shadowing for function declarations.
54 void test5(int i);
55 void test6(void (*f)(int i)) {}
56 void test7(void *context, void (*callback)(void *context)) {}
57 
58 extern int bob; // expected-note {{previous declaration is here}}
59 
60 // rdar://8883302
61 void rdar8883302(void) {
62   extern int bob; // don't warn for shadowing.
63 }
64 
65 void test8(void) {
66   int bob; // expected-warning {{declaration shadows a variable in the global scope}}
67 }
68 
69 enum PR24718_1{pr24718}; // expected-note {{previous declaration is here}}
70 void PR24718(void) {
71   enum PR24718_2{pr24718}; // expected-warning {{declaration shadows a variable in the global scope}}
72 }
73 
74 struct PR24718_3;
75 struct PR24718_4 {
76   enum {
77     PR24718_3 // Does not shadow a type.
78   };
79 };
80 
81 void static_locals() {
82   static int i; // expected-warning {{declaration shadows a variable in the global scope}}
83                 // expected-note@-1 {{previous definition is here}}
84                 // expected-note@-2 {{previous declaration is here}}
85   int i;        // expected-error {{non-static declaration of 'i' follows static declaration}}
86   static int foo; // expected-note {{previous declaration is here}}
87   static int hoge; // expected-note {{previous declaration is here}}
88   int s; // expected-warning {{declaration shadows a variable in the global scope}}
89   {
90     static int foo; // expected-warning {{declaration shadows a local variable}}
91                     // expected-note@-1 {{previous declaration is here}}
92     static int i; // expected-warning {{declaration shadows a local variable}}
93                   // expected-note@-1 {{previous declaration is here}}
94     int hoge; // expected-warning {{declaration shadows a local variable}}
95     {
96       static int foo; // expected-warning {{declaration shadows a local variable}}
97       int i; // expected-warning {{declaration shadows a local variable}}
98       extern int hoge;
99     }
100   }
101 }
102