xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/function-redecl.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc int foo(int);
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc namespace N {
f1()5*f4a2713aSLionel Sambuc   void f1() {
6*f4a2713aSLionel Sambuc     void foo(int); // okay
7*f4a2713aSLionel Sambuc     void bar(int); // expected-note 2{{previous declaration is here}}
8*f4a2713aSLionel Sambuc   }
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc   void foo(int); // expected-note 2{{previous declaration is here}}
11*f4a2713aSLionel Sambuc 
f2()12*f4a2713aSLionel Sambuc   void f2() {
13*f4a2713aSLionel Sambuc     int foo(int); // expected-error {{functions that differ only in their return type cannot be overloaded}}
14*f4a2713aSLionel Sambuc     int bar(int); // expected-error {{functions that differ only in their return type cannot be overloaded}}
15*f4a2713aSLionel Sambuc     int baz(int); // expected-note {{previous declaration is here}}
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc     {
18*f4a2713aSLionel Sambuc       int foo;
19*f4a2713aSLionel Sambuc       int bar;
20*f4a2713aSLionel Sambuc       int baz;
21*f4a2713aSLionel Sambuc       {
22*f4a2713aSLionel Sambuc         float foo(int); // expected-error {{functions that differ only in their return type cannot be overloaded}}
23*f4a2713aSLionel Sambuc         float bar(int); // expected-error {{functions that differ only in their return type cannot be overloaded}}
24*f4a2713aSLionel Sambuc         float baz(int); // expected-error {{functions that differ only in their return type cannot be overloaded}}
25*f4a2713aSLionel Sambuc       }
26*f4a2713aSLionel Sambuc     }
27*f4a2713aSLionel Sambuc   }
28*f4a2713aSLionel Sambuc }
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc class A {
31*f4a2713aSLionel Sambuc  void typocorrection(); // expected-note {{'typocorrection' declared here}}
32*f4a2713aSLionel Sambuc };
33*f4a2713aSLionel Sambuc 
Notypocorrection()34*f4a2713aSLionel Sambuc void A::Notypocorrection() { // expected-error {{out-of-line definition of 'Notypocorrection' does not match any declaration in 'A'; did you mean 'typocorrection'}}
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc namespace test0 {
dummy()39*f4a2713aSLionel Sambuc   void dummy() {
40*f4a2713aSLionel Sambuc     void Bar(); // expected-note {{'Bar' declared here}}
41*f4a2713aSLionel Sambuc     class A {
42*f4a2713aSLionel Sambuc       friend void bar(); // expected-error {{no matching function 'bar' found in local scope; did you mean 'Bar'}}
43*f4a2713aSLionel Sambuc     };
44*f4a2713aSLionel Sambuc   }
45*f4a2713aSLionel Sambuc }
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc class B {
49*f4a2713aSLionel Sambuc  void typocorrection(const int); // expected-note {{'typocorrection' declared here}}
50*f4a2713aSLionel Sambuc  void typocorrection(double);
51*f4a2713aSLionel Sambuc };
52*f4a2713aSLionel Sambuc 
Notypocorrection(int)53*f4a2713aSLionel Sambuc void B::Notypocorrection(int) { // expected-error {{out-of-line definition of 'Notypocorrection' does not match any declaration in 'B'; did you mean 'typocorrection'}}
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc struct X { int f(); };
57*f4a2713aSLionel Sambuc struct Y : public X {};
f()58*f4a2713aSLionel Sambuc int Y::f() { return 3; } // expected-error {{out-of-line definition of 'f' does not match any declaration in 'Y'}}
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc namespace test1 {
61*f4a2713aSLionel Sambuc struct Foo {
62*f4a2713aSLionel Sambuc   class Inner { };
63*f4a2713aSLionel Sambuc };
64*f4a2713aSLionel Sambuc }
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc class Bar {
67*f4a2713aSLionel Sambuc   void f(test1::Foo::Inner foo) const; // expected-note {{member declaration does not match because it is const qualified}}
68*f4a2713aSLionel Sambuc };
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc using test1::Foo;
71*f4a2713aSLionel Sambuc 
f(Foo::Inner foo)72*f4a2713aSLionel Sambuc void Bar::f(Foo::Inner foo) { // expected-error {{out-of-line definition of 'f' does not match any declaration in 'Bar'}}
73*f4a2713aSLionel Sambuc   (void)foo;
74*f4a2713aSLionel Sambuc }
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc class Crash {
77*f4a2713aSLionel Sambuc  public:
78*f4a2713aSLionel Sambuc   void GetCart(int count) const;
79*f4a2713aSLionel Sambuc };
80*f4a2713aSLionel Sambuc // This out-of-line definition was fine...
cart(int count) const81*f4a2713aSLionel Sambuc void Crash::cart(int count) const {} // expected-error {{out-of-line definition of 'cart' does not match any declaration in 'Crash'}}
82*f4a2713aSLionel Sambuc // ...while this one crashed clang
chart(int count) const83*f4a2713aSLionel Sambuc void Crash::chart(int count) const {} // expected-error {{out-of-line definition of 'chart' does not match any declaration in 'Crash'}}
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc class TestConst {
86*f4a2713aSLionel Sambuc  public:
87*f4a2713aSLionel Sambuc   int getit() const; // expected-note {{member declaration does not match because it is const qualified}}
88*f4a2713aSLionel Sambuc   void setit(int); // expected-note {{member declaration does not match because it is not const qualified}}
89*f4a2713aSLionel Sambuc };
90*f4a2713aSLionel Sambuc 
getit()91*f4a2713aSLionel Sambuc int TestConst::getit() { // expected-error {{out-of-line definition of 'getit' does not match any declaration in 'TestConst'}}
92*f4a2713aSLionel Sambuc   return 1;
93*f4a2713aSLionel Sambuc }
94*f4a2713aSLionel Sambuc 
setit(int) const95*f4a2713aSLionel Sambuc void TestConst::setit(int) const { // expected-error {{out-of-line definition of 'setit' does not match any declaration in 'TestConst'}}
96*f4a2713aSLionel Sambuc }
97*f4a2713aSLionel Sambuc 
98*f4a2713aSLionel Sambuc struct J { int typo() const; };
typo_()99*f4a2713aSLionel Sambuc int J::typo_() { return 3; } // expected-error {{out-of-line definition of 'typo_' does not match any declaration in 'J'}}
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc // Ensure we correct the redecl of Foo::isGood to Bar::Foo::isGood and not
102*f4a2713aSLionel Sambuc // Foo::IsGood even though Foo::IsGood is technically a closer match since it
103*f4a2713aSLionel Sambuc // already has a body. Also make sure Foo::beEvil is corrected to Foo::BeEvil
104*f4a2713aSLionel Sambuc // since it is a closer match than Bar::Foo::beEvil and neither have a body.
105*f4a2713aSLionel Sambuc namespace redecl_typo {
106*f4a2713aSLionel Sambuc namespace Foo {
IsGood()107*f4a2713aSLionel Sambuc   bool IsGood() { return false; }
108*f4a2713aSLionel Sambuc   void BeEvil(); // expected-note {{'BeEvil' declared here}}
109*f4a2713aSLionel Sambuc }
110*f4a2713aSLionel Sambuc namespace Bar {
111*f4a2713aSLionel Sambuc   namespace Foo {
112*f4a2713aSLionel Sambuc     bool isGood(); // expected-note {{'Bar::Foo::isGood' declared here}}
113*f4a2713aSLionel Sambuc     void beEvil();
114*f4a2713aSLionel Sambuc   }
115*f4a2713aSLionel Sambuc }
isGood()116*f4a2713aSLionel Sambuc bool Foo::isGood() { // expected-error {{out-of-line definition of 'isGood' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'Bar::Foo::isGood'?}}
117*f4a2713aSLionel Sambuc   return true;
118*f4a2713aSLionel Sambuc }
beEvil()119*f4a2713aSLionel Sambuc void Foo::beEvil() {} // expected-error {{out-of-line definition of 'beEvil' does not match any declaration in namespace 'redecl_typo::Foo'; did you mean 'BeEvil'?}}
120*f4a2713aSLionel Sambuc }
121