xref: /llvm-project/clang/test/Sema/warn-shadow.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
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 
foo(void)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 void (^test1)(int) = ^(int i) { // expected-warning {{declaration shadows a variable in the global scope}} \
29                                  // expected-note{{previous declaration is here}}
30   {
31     int i; // expected-warning {{declaration shadows a local variable}} \
32            // expected-note{{previous declaration is here}}
33 
34     (^(int i) { return i; })(i); //expected-warning {{declaration shadows a local variable}}
35   }
36 };
37 
38 
39 struct test2 {
40   int i;
41 };
42 
test3(void)43 void test3(void) {
44   struct test4 {
45     int i;
46   };
47 }
48 
test4(int i)49 void test4(int i) { // expected-warning {{declaration shadows a variable in the global scope}}
50 }
51 
52 // Don't warn about shadowing for function declarations.
53 void test5(int i);
test6(void (* f)(int i))54 void test6(void (*f)(int i)) {}
test7(void * context,void (* callback)(void * context))55 void test7(void *context, void (*callback)(void *context)) {}
56 
57 extern int bob; // expected-note {{previous declaration is here}}
58 
rdar8883302(void)59 void rdar8883302(void) {
60   extern int bob; // don't warn for shadowing.
61 }
62 
test8(void)63 void test8(void) {
64   int bob; // expected-warning {{declaration shadows a variable in the global scope}}
65 }
66 
67 enum PR24718_1{pr24718}; // expected-note {{previous declaration is here}}
PR24718(void)68 void PR24718(void) {
69   enum PR24718_2{pr24718}; // expected-warning {{declaration shadows a variable in the global scope}}
70 }
71 
72 struct PR24718_3;
73 struct PR24718_4 {
74   enum {
75     PR24718_3 // Does not shadow a type.
76   };
77 };
78 
static_locals()79 void static_locals() {
80   static int i; // expected-warning {{declaration shadows a variable in the global scope}}
81                 // expected-note@-1 {{previous definition is here}}
82                 // expected-note@-2 {{previous declaration is here}}
83   int i;        // expected-error {{non-static declaration of 'i' follows static declaration}}
84   static int foo; // expected-note {{previous declaration is here}}
85   static int hoge; // expected-note {{previous declaration is here}}
86   int s; // expected-warning {{declaration shadows a variable in the global scope}}
87   {
88     static int foo; // expected-warning {{declaration shadows a local variable}}
89                     // expected-note@-1 {{previous declaration is here}}
90     static int i; // expected-warning {{declaration shadows a local variable}}
91                   // expected-note@-1 {{previous declaration is here}}
92     int hoge; // expected-warning {{declaration shadows a local variable}}
93     {
94       static int foo; // expected-warning {{declaration shadows a local variable}}
95       int i; // expected-warning {{declaration shadows a local variable}}
96       extern int hoge;
97     }
98   }
99 }
100