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