Home | History | Annotate | Download | only in class.virtual
      1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
      2 
      3 namespace Test1 {
      4 
      5 struct B {
      6   virtual void f(int);
      7 };
      8 
      9 struct D : B {
     10   virtual void f(long) override; // expected-error {{'f' marked 'override' but does not override any member functions}}
     11   void f(int) override;
     12 };
     13 }
     14 
     15 namespace Test2 {
     16 
     17 struct A {
     18   virtual void f(int, char, int);
     19 };
     20 
     21 template<typename T>
     22 struct B : A {
     23   // FIXME: Diagnose this.
     24   virtual void f(T) override;
     25 };
     26 
     27 template<typename T>
     28 struct C : A {
     29   virtual void f(int) override; // expected-error {{does not override}}
     30 };
     31 
     32 }
     33 
     34 namespace Test3 {
     35 
     36 struct A {
     37   virtual void f(int, char, int);
     38 };
     39 
     40 template<typename... Args>
     41 struct B : A {
     42   virtual void f(Args...) override; // expected-error {{'f' marked 'override' but does not override any member functions}}
     43 };
     44 
     45 template struct B<int, char, int>;
     46 template struct B<int>; // expected-note {{in instantiation of template class 'Test3::B<int>' requested here}}
     47 
     48 }
     49 
     50 namespace Test4 {
     51 struct B {
     52   virtual void f() const final; // expected-note {{overridden virtual function is here}}
     53 };
     54 
     55 struct D : B {
     56   void f() const; // expected-error {{declaration of 'f' overrides a 'final' function}}
     57 };
     58 
     59 }
     60 
     61 namespace PR13499 {
     62   struct X {
     63     virtual void f();
     64     virtual void h();
     65   };
     66   template<typename T> struct A : X {
     67     void f() override;
     68     void h() final;
     69   };
     70   template<typename T> struct B : X {
     71     void g() override; // expected-error {{only virtual member functions can be marked 'override'}}
     72     void i() final; // expected-error {{only virtual member functions can be marked 'final'}}
     73   };
     74   B<int> b; // no-note
     75   template<typename T> struct C : T {
     76     void g() override;
     77     void i() final;
     78   };
     79   template<typename T> struct D : X {
     80     virtual void g() override; // expected-error {{does not override}}
     81     virtual void i() final;
     82   };
     83   template<typename...T> struct E : X {
     84     void f(T...) override;
     85     void g(T...) override; // expected-error {{only virtual member functions can be marked 'override'}}
     86     void h(T...) final;
     87     void i(T...) final; // expected-error {{only virtual member functions can be marked 'final'}}
     88   };
     89   // FIXME: Diagnose these in the template definition, not in the instantiation.
     90   E<> e; // expected-note {{in instantiation of}}
     91 
     92   template<typename T> struct Y : T {
     93     void f() override;
     94     void h() final;
     95   };
     96   template<typename T> struct Z : T {
     97     void g() override; // expected-error {{only virtual member functions can be marked 'override'}}
     98     void i() final; // expected-error {{only virtual member functions can be marked 'final'}}
     99   };
    100   Y<X> y;
    101   Z<X> z; // expected-note {{in instantiation of}}
    102 }
    103 
    104 namespace MemberOfUnknownSpecialization {
    105   template<typename T> struct A {
    106     struct B {};
    107     struct C : B {
    108       void f() override;
    109     };
    110   };
    111 
    112   template<> struct A<int>::B {
    113     virtual void f();
    114   };
    115   // ok
    116   A<int>::C c1;
    117 
    118   template<> struct A<char>::B {
    119     void f();
    120   };
    121   // expected-error@-13 {{only virtual member functions can be marked 'override'}}
    122   // expected-note@+1 {{in instantiation of}}
    123   A<char>::C c2;
    124 
    125   template<> struct A<double>::B {
    126     virtual void f() final;
    127   };
    128   // expected-error@-20 {{declaration of 'f' overrides a 'final' function}}
    129   // expected-note@-3 {{here}}
    130   // expected-note@+1 {{in instantiation of}}
    131   A<double>::C c3;
    132 }
    133 
    134 namespace DiagnosticsQOI {
    135   struct X {
    136     virtual ~X();
    137     virtual void foo(int x); // expected-note {{hidden overloaded virtual function}}
    138     virtual void bar(int x); // expected-note 2 {{hidden overloaded virtual function}}
    139     virtual void bar(float x); // expected-note 2 {{hidden overloaded virtual function}}
    140   };
    141 
    142   struct Y : X {
    143     void foo(int x, int y) override; // expected-error {{non-virtual member function marked 'override' hides virtual member function}}
    144     void bar(double) override; // expected-error {{non-virtual member function marked 'override' hides virtual member functions}}
    145     void bar(long double) final; // expected-error {{non-virtual member function marked 'final' hides virtual member functions}}
    146   };
    147 
    148   template<typename T>
    149   struct Z : T {
    150     static void foo() override; // expected-error {{only virtual member functions can be marked 'override'}}
    151   };
    152 }
    153