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