xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-overloaded-virtual.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Woverloaded-virtual -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc struct B1 {
4f4a2713aSLionel Sambuc   virtual void foo(int); // expected-note {{declared here}}
5f4a2713aSLionel Sambuc   virtual void foo(); // expected-note {{declared here}}
6f4a2713aSLionel Sambuc };
7f4a2713aSLionel Sambuc 
8f4a2713aSLionel Sambuc struct S1 : public B1 {
9f4a2713aSLionel Sambuc   void foo(float); // expected-warning {{hides overloaded virtual functions}}
10f4a2713aSLionel Sambuc };
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc struct S2 : public B1 {
13f4a2713aSLionel Sambuc   void foo(); // expected-note {{declared here}}
14f4a2713aSLionel Sambuc };
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc struct B2 {
17f4a2713aSLionel Sambuc   virtual void foo(void*); // expected-note {{declared here}}
18f4a2713aSLionel Sambuc };
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc struct MS1 : public S2, public B2 {
21f4a2713aSLionel Sambuc    virtual void foo(int); // expected-warning {{hides overloaded virtual functions}}
22f4a2713aSLionel Sambuc };
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc struct B3 {
25f4a2713aSLionel Sambuc   virtual void foo(int);
26f4a2713aSLionel Sambuc   virtual void foo();
27f4a2713aSLionel Sambuc };
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc struct S3 : public B3 {
30f4a2713aSLionel Sambuc   using B3::foo;
31f4a2713aSLionel Sambuc   void foo(float);
32f4a2713aSLionel Sambuc };
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc struct B4 {
35f4a2713aSLionel Sambuc   virtual void foo();
36f4a2713aSLionel Sambuc };
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc struct S4 : public B4 {
39f4a2713aSLionel Sambuc   void foo(float);
40f4a2713aSLionel Sambuc   void foo();
41f4a2713aSLionel Sambuc };
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc namespace PR9182 {
44f4a2713aSLionel Sambuc struct Base {
45f4a2713aSLionel Sambuc   virtual void foo(int);
46f4a2713aSLionel Sambuc };
47f4a2713aSLionel Sambuc 
foo(int)48f4a2713aSLionel Sambuc void Base::foo(int) { }
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc struct Derived : public Base {
51f4a2713aSLionel Sambuc   virtual void foo(int);
52f4a2713aSLionel Sambuc   void foo(int, int);
53f4a2713aSLionel Sambuc };
54f4a2713aSLionel Sambuc }
55f4a2713aSLionel Sambuc 
56f4a2713aSLionel Sambuc namespace PR9396 {
57f4a2713aSLionel Sambuc class A {
58f4a2713aSLionel Sambuc public:
f(int)59f4a2713aSLionel Sambuc   virtual void f(int) {}
60f4a2713aSLionel Sambuc };
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc class B : public A {
63f4a2713aSLionel Sambuc public:
f()64f4a2713aSLionel Sambuc   static void f() {}
65f4a2713aSLionel Sambuc };
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc namespace ThreeLayer {
69f4a2713aSLionel Sambuc struct A {
70f4a2713aSLionel Sambuc   virtual void f();
71f4a2713aSLionel Sambuc };
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc struct B: A {
74f4a2713aSLionel Sambuc   void f();
75f4a2713aSLionel Sambuc   void f(int);
76f4a2713aSLionel Sambuc };
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc struct C: B {
79f4a2713aSLionel Sambuc   void f(int);
80f4a2713aSLionel Sambuc   using A::f;
81f4a2713aSLionel Sambuc };
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc namespace UnbalancedVirtual {
85f4a2713aSLionel Sambuc struct Base {
86f4a2713aSLionel Sambuc   virtual void func();
87f4a2713aSLionel Sambuc };
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc struct Derived1: virtual Base {
90f4a2713aSLionel Sambuc   virtual void func();
91f4a2713aSLionel Sambuc };
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc struct Derived2: virtual Base {
94f4a2713aSLionel Sambuc };
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc struct MostDerived: Derived1, Derived2 {
97f4a2713aSLionel Sambuc   void func(int);
98f4a2713aSLionel Sambuc   void func();
99f4a2713aSLionel Sambuc };
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc namespace UnbalancedVirtual2 {
103f4a2713aSLionel Sambuc struct Base {
104f4a2713aSLionel Sambuc   virtual void func();
105f4a2713aSLionel Sambuc };
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc struct Derived1: virtual Base {
108f4a2713aSLionel Sambuc   virtual void func();
109f4a2713aSLionel Sambuc };
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc struct Derived2: virtual Base {
112f4a2713aSLionel Sambuc };
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc struct Derived3: Derived1 {
115f4a2713aSLionel Sambuc   virtual void func();
116f4a2713aSLionel Sambuc };
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc struct MostDerived: Derived3, Derived2 {
119f4a2713aSLionel Sambuc   void func(int);
120f4a2713aSLionel Sambuc   void func();
121f4a2713aSLionel Sambuc };
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc namespace {
125f4a2713aSLionel Sambuc   class A {
126f4a2713aSLionel Sambuc     virtual int foo(bool) const;
127f4a2713aSLionel Sambuc     // expected-note@-1{{type mismatch at 1st parameter ('bool' vs 'int')}}
128f4a2713aSLionel Sambuc     virtual int foo(int, int) const;
129f4a2713aSLionel Sambuc     // expected-note@-1{{different number of parameters (2 vs 1)}}
130f4a2713aSLionel Sambuc     virtual int foo(int*) const;
131f4a2713aSLionel Sambuc     // expected-note@-1{{type mismatch at 1st parameter ('int *' vs 'int')}}
132f4a2713aSLionel Sambuc     virtual int foo(int) volatile;
133f4a2713aSLionel Sambuc     // expected-note@-1{{different qualifiers (volatile vs const)}}
134f4a2713aSLionel Sambuc   };
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc   class B : public A {
137f4a2713aSLionel Sambuc     virtual int foo(int) const;
138f4a2713aSLionel Sambuc     // expected-warning@-1{{hides overloaded virtual functions}}
139f4a2713aSLionel Sambuc   };
140*0a6a1f1dSLionel Sambuc }
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc namespace {
143*0a6a1f1dSLionel Sambuc struct base {
f__anon0ff973fc0211::base144*0a6a1f1dSLionel Sambuc   void f(char) {}
145*0a6a1f1dSLionel Sambuc };
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc struct derived : base {
f__anon0ff973fc0211::derived148*0a6a1f1dSLionel Sambuc   void f(int) {}
149*0a6a1f1dSLionel Sambuc };
150*0a6a1f1dSLionel Sambuc 
foo(derived & d)151*0a6a1f1dSLionel Sambuc void foo(derived &d) {
152*0a6a1f1dSLionel Sambuc   d.f('1'); // FIXME: this should warn about calling (anonymous namespace)::derived::f(int)
153*0a6a1f1dSLionel Sambuc             // instead of (anonymous namespace)::base::f(char).
154*0a6a1f1dSLionel Sambuc             // Note: this should be under a new diagnostic flag and eventually moved to a
155*0a6a1f1dSLionel Sambuc             // new test case since it's not strictly related to virtual functions.
156*0a6a1f1dSLionel Sambuc   d.f(12);  // This should not warn.
157*0a6a1f1dSLionel Sambuc }
158f4a2713aSLionel Sambuc }
159