xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-shadow.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -verify -fsyntax-only -Wshadow %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc namespace {
4*f4a2713aSLionel Sambuc   int i; // expected-note {{previous declaration is here}}
5*f4a2713aSLionel Sambuc }
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc namespace one {
8*f4a2713aSLionel Sambuc namespace two {
9*f4a2713aSLionel Sambuc   int j; // expected-note {{previous declaration is here}}
10*f4a2713aSLionel Sambuc }
11*f4a2713aSLionel Sambuc }
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc namespace xx {
14*f4a2713aSLionel Sambuc   int m;
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc namespace yy {
17*f4a2713aSLionel Sambuc   int m;
18*f4a2713aSLionel Sambuc }
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc using namespace one::two;
21*f4a2713aSLionel Sambuc using namespace xx;
22*f4a2713aSLionel Sambuc using namespace yy;
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc void foo() {
25*f4a2713aSLionel Sambuc   int i; // expected-warning {{declaration shadows a variable in namespace '<anonymous>'}}
26*f4a2713aSLionel Sambuc   int j; // expected-warning {{declaration shadows a variable in namespace 'one::two'}}
27*f4a2713aSLionel Sambuc   int m;
28*f4a2713aSLionel Sambuc }
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc class A {
31*f4a2713aSLionel Sambuc   static int data; // expected-note {{previous declaration}}
32*f4a2713aSLionel Sambuc   int field; // expected-note {{previous declaration}}
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc   void test() {
35*f4a2713aSLionel Sambuc     char *field; // expected-warning {{declaration shadows a field of 'A'}}
36*f4a2713aSLionel Sambuc     char *data; // expected-warning {{declaration shadows a static data member of 'A'}}
37*f4a2713aSLionel Sambuc   }
38*f4a2713aSLionel Sambuc };
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc // TODO: this should warn, <rdar://problem/5018057>
41*f4a2713aSLionel Sambuc class B : A {
42*f4a2713aSLionel Sambuc   int data;
43*f4a2713aSLionel Sambuc   static int field;
44*f4a2713aSLionel Sambuc };
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc // rdar://8900456
47*f4a2713aSLionel Sambuc namespace rdar8900456 {
48*f4a2713aSLionel Sambuc struct Foo {
49*f4a2713aSLionel Sambuc   static void Baz();
50*f4a2713aSLionel Sambuc private:
51*f4a2713aSLionel Sambuc   int Bar;
52*f4a2713aSLionel Sambuc };
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc void Foo::Baz() {
55*f4a2713aSLionel Sambuc   double Bar = 12; // Don't warn.
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc }
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc // http://llvm.org/PR9160
60*f4a2713aSLionel Sambuc namespace PR9160 {
61*f4a2713aSLionel Sambuc struct V {
62*f4a2713aSLionel Sambuc   V(int);
63*f4a2713aSLionel Sambuc };
64*f4a2713aSLionel Sambuc struct S {
65*f4a2713aSLionel Sambuc   V v;
66*f4a2713aSLionel Sambuc   static void m() {
67*f4a2713aSLionel Sambuc     if (1) {
68*f4a2713aSLionel Sambuc       V v(0);
69*f4a2713aSLionel Sambuc     }
70*f4a2713aSLionel Sambuc   }
71*f4a2713aSLionel Sambuc };
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc extern int bob; // expected-note {{previous declaration is here}}
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc // rdar://8883302
77*f4a2713aSLionel Sambuc void rdar8883302() {
78*f4a2713aSLionel Sambuc   extern int bob; // don't warn for shadowing.
79*f4a2713aSLionel Sambuc }
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc void test8() {
82*f4a2713aSLionel Sambuc   int bob; // expected-warning {{declaration shadows a variable in the global namespace}}
83*f4a2713aSLionel Sambuc }
84