Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 class C {
      3 public:
      4   auto int errx; // expected-error {{error: storage class specified for a member declaration}} expected-warning {{'auto' storage class specifier is redundant}}
      5   register int erry; // expected-error {{error: storage class specified for a member declaration}}
      6   extern int errz; // expected-error {{error: storage class specified for a member declaration}}
      7 
      8   static void sm() {
      9     sx = 0;
     10     this->x = 0; // expected-error {{error: invalid use of 'this' outside of a nonstatic member function}}
     11     x = 0; // expected-error {{error: invalid use of member 'x' in static member function}}
     12   }
     13 
     14   class NestedC {
     15   public:
     16     NestedC(int);
     17     void m() {
     18       sx = 0;
     19       x = 0; // expected-error {{invalid use of nonstatic data member 'x'}}
     20     }
     21   };
     22 
     23   int b : 1, w : 2;
     24   int : 1, : 2;
     25   typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}}
     26   static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}}
     27   static int vs;
     28 
     29   typedef int func();
     30   func tm;
     31   func *ptm;
     32   func btm : 1; // expected-error {{bit-field 'btm' has non-integral type}}
     33   NestedC bc : 1; // expected-error {{bit-field 'bc' has non-integral type}}
     34 
     35   enum E1 { en1, en2 };
     36 
     37   int i = 0; // expected-warning {{in-class initialization of non-static data member accepted as a C++11 extension}}
     38   static int si = 0; // expected-error {{non-const static data member must be initialized out of line}}
     39   static const NestedC ci = 0; // expected-error {{static data member of type 'const C::NestedC' must be initialized out of line}}
     40   static const int nci = vs; // expected-error {{in-class initializer is not a constant expression}}
     41   static const int vi = 0;
     42   static const volatile int cvi = 0; // ok, illegal in C++11
     43   static const E evi = 0;
     44 
     45   void m() {
     46     sx = 0;
     47     this->x = 0;
     48     y = 0;
     49     this = 0; // expected-error {{error: expression is not assignable}}
     50   }
     51 
     52   int f1(int p) {
     53     A z = 6;
     54     return p + x + this->y + z;
     55   }
     56 
     57   typedef int A;
     58 
     59   virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}}
     60   virtual static int vsif(); // expected-error {{error: 'virtual' can only appear on non-static member functions}}
     61   virtual int vif();
     62 
     63 private:
     64   int x,y;
     65   static int sx;
     66 
     67   mutable int mi;
     68   mutable int &mir; // expected-error {{error: 'mutable' cannot be applied to references}}
     69   mutable void mfn(); // expected-error {{error: 'mutable' cannot be applied to functions}}
     70   mutable const int mci; // expected-error {{error: 'mutable' and 'const' cannot be mixed}}
     71 
     72   static const int number = 50;
     73   static int arr[number];
     74 };
     75 
     76 class C2 {
     77   void f() {
     78     static int lx;
     79     class LC1 {
     80       int m() { return lx; }
     81     };
     82     class LC2 {
     83       int m() { return lx; }
     84     };
     85   }
     86 };
     87 
     88 struct C3 {
     89   int i;
     90   mutable int j;
     91 };
     92 void f()
     93 {
     94   const C3 c3 = { 1, 2 };
     95   (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}}
     96   // but no error here
     97   (void)static_cast<int*>(&c3.j);
     98 }
     99 
    100 // Play with mutable a bit more, to make sure it doesn't crash anything.
    101 mutable int gi; // expected-error {{error: 'mutable' can only be applied to member variables}}
    102 mutable void gfn(); // expected-error {{illegal storage class on function}}
    103 void ogfn()
    104 {
    105   mutable int ml; // expected-error {{error: 'mutable' can only be applied to member variables}}
    106 
    107   // PR3020: This used to crash due to double ownership of C4.
    108   struct C4;
    109   C4; // expected-warning {{declaration does not declare anything}}
    110 }
    111 
    112 struct C4 {
    113   void f(); // expected-note{{previous declaration is here}}
    114   int f; // expected-error{{duplicate member 'f'}}
    115 };
    116 
    117 // PR5415 - don't hang!
    118 struct S
    119 {
    120   void f(); // expected-note 1 {{previous declaration}}
    121   void S::f() {} // expected-warning {{extra qualification on member}} expected-error {{class member cannot be redeclared}} expected-note {{previous declaration}} expected-note {{previous definition}}
    122   void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}}
    123 };
    124 
    125 // Don't crash on this bogus code.
    126 namespace pr6629 {
    127   // TODO: most of these errors are spurious
    128   template<class T1, class T2> struct foo :
    129     bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}} \
    130                        // BOGUS expected-error {{expected '{' after base class list}} \
    131                        // BOGUS expected-error {{expected ';' after struct}} \
    132                        // BOGUS expected-error {{expected unqualified-id}} \
    133   { };
    134 
    135   template<> struct foo<unknown,unknown> { // why isn't there an error here?
    136     template <typename U1, typename U2> struct bar {
    137       typedef bar type;
    138       static const int value = 0;
    139     };
    140   };
    141 }
    142 
    143 namespace PR7153 {
    144   class EnclosingClass {
    145   public:
    146     struct A { } mutable *member;
    147   };
    148 
    149   void f(const EnclosingClass &ec) {
    150     ec.member = 0;
    151   }
    152 }
    153 
    154 namespace PR7196 {
    155   struct A {
    156     int a;
    157 
    158     void f() {
    159       char i[sizeof(a)];
    160       enum { x = sizeof(i) };
    161       enum { y = sizeof(a) };
    162     }
    163   };
    164 }
    165 
    166 namespace rdar8066414 {
    167   class C {
    168     C() {}
    169   } // expected-error{{expected ';' after class}}
    170 }
    171 
    172 namespace rdar8367341 {
    173   float foo();
    174 
    175   struct A {
    176     static const float x = 5.0f; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}}
    177     static const float y = foo(); // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}} expected-error {{in-class initializer is not a constant expression}}
    178   };
    179 }
    180 
    181 namespace with_anon {
    182 struct S {
    183   union {
    184     char c;
    185   };
    186 };
    187 
    188 void f() {
    189     S::c; // expected-error {{invalid use of nonstatic data member}}
    190 }
    191 }
    192 
    193 struct PR9989 {
    194   static int const PR9989_Member = sizeof PR9989_Member;
    195 };
    196