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