class.cpp revision 293279ae4351f4f17ce44aa4f72861d0bc74c918
1a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM// RUN: %clang_cc1 -fsyntax-only -verify %s
2a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMclass C {
3a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMpublic:
4a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  auto int errx; // expected-error {{error: storage class specified for a member declaration}}
5a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  register int erry; // expected-error {{error: storage class specified for a member declaration}}
6a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  extern int errz; // expected-error {{error: storage class specified for a member declaration}}
7a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
8a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static void sm() {
9a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    sx = 0;
10a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    this->x = 0; // expected-error {{error: invalid use of 'this' outside of a nonstatic member function}}
11a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    x = 0; // expected-error {{error: invalid use of member 'x' in static member function}}
12a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  }
13a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
14a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  class NestedC {
15a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    void m() {
16a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      sx = 0;
17a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM      x = 0; // expected-error {{error: invalid use of nonstatic data member 'x'}}
18a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    }
19a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  };
20a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
21a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  int b : 1, w : 2;
22a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  int : 1, : 2;
23a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}}
24a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static int sb : 1; // expected-error {{error: static member 'sb' cannot be a bit-field}}
25a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static int vs;
26a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
27a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  typedef int func();
28a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  func tm;
29a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  func *ptm;
30a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  func btm : 1; // expected-error {{bit-field 'btm' has non-integral type}}
31a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  NestedC bc : 1; // expected-error {{bit-field 'bc' has non-integral type}}
32a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
33a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  enum E1 { en1, en2 };
34a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
35a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  int i = 0; // expected-error {{error: 'i' can only be initialized if it is a static const integral data member}}
36a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static int si = 0; // expected-error {{error: 'si' can only be initialized if it is a static const integral data member}}
37a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static const NestedC ci = 0; // expected-error {{error: 'ci' can only be initialized if it is a static const integral data member}}
38a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static const int nci = vs; // expected-error {{in-class initializer is not an integral constant expression}}
39a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static const int vi = 0;
40a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static const E evi = 0;
41a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
42a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  void m() {
43a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    sx = 0;
44a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    this->x = 0;
45a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    y = 0;
46a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    this = 0; // expected-error {{error: expression is not assignable}}
47a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  }
48a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
49a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  int f1(int p) {
50a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    A z = 6;
51a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM    return p + x + this->y + z;
52a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  }
53a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
54a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  typedef int A;
55a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
56a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}}
57a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  virtual static int vsif(); // expected-error {{error: 'virtual' can only appear on non-static member functions}}
58a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  virtual int vif();
59a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
60a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMprivate:
61a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  int x,y;
62a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static int sx;
63a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
64a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  mutable int mi;
65a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  mutable int &mir; // expected-error {{error: 'mutable' cannot be applied to references}}
66a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  mutable void mfn(); // expected-error {{error: 'mutable' cannot be applied to functions}}
67a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  mutable const int mci; // expected-error {{error: 'mutable' and 'const' cannot be mixed}}
68a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
69a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static const int number = 50;
70a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  static int arr[number];
71a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM};
72a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM
73a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLMclass C2 {
74a8bfde77e0e275070791138d60b75d1cc293daf0Johan RUDHOLM  void f() {
7527c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner    static int lx;
7627c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner    class LC1 {
7727c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner      int m() { return lx; }
7827c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner    };
7927c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner    class LC2 {
8027c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner      int m() { return lx; }
8127c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner    };
8227c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner  }
8327c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner};
8427c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner
8527c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardinerstruct C3 {
8627c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner  int i;
8727c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner  mutable int j;
8827c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner};
8927c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardinervoid f()
9027c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner{
9127c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner  const C3 c3 = { 1, 2 };
9227c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner  (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'int const *' to 'int *' is not allowed}}
9327c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner  // but no error here
94b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  (void)static_cast<int*>(&c3.j);
95b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball}
96b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
97b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball// Play with mutable a bit more, to make sure it doesn't crash anything.
98b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballmutable int gi; // expected-error {{error: 'mutable' can only be applied to member variables}}
99b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballmutable void gfn(); // expected-error {{illegal storage class on function}}
100b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballvoid ogfn()
101b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball{
102b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  mutable int ml; // expected-error {{error: 'mutable' can only be applied to member variables}}
103b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
104b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // PR3020: This used to crash due to double ownership of C4.
105b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  struct C4;
106b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  C4; // expected-warning {{declaration does not declare anything}}
107b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball}
108b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
109b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballstruct C4 {
110b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  void f(); // expected-note{{previous declaration is here}}
111b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  int f; // expected-error{{duplicate member 'f'}}
112b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball};
113b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
114b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball// PR5415 - don't hang!
115b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballstruct S
116b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball{
117b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  void f(); // expected-note 1 {{previous declaration}}
118b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  void S::f() {} // expected-error {{class member cannot be redeclared}} expected-note {{previous declaration}} expected-note {{previous definition}}
119b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  void f() {} // expected-error {{class member cannot be redeclared}} expected-error {{redefinition}}
120b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball};
121b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
122b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball// Don't crash on this bogus code.
123b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballnamespace pr6629 {
124b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  // TODO: most of these errors are spurious
125b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  template<class T1, class T2> struct foo :
126b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}} \
127b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball                       // BOGUS expected-error {{expected '{' after base class list}} \
128b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball                       // BOGUS expected-error {{expected ';' after struct}} \
129b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball                       // BOGUS expected-error {{expected unqualified-id}} \
130b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  { };
131b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
132b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  template<> struct foo<unknown,unknown> { // why isn't there an error here?
133b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    template <typename U1, typename U2> struct bar {
1348ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ball      typedef bar type;
1358ba4466a4ad458618282f8bdcc2706025856a9f2Chris Ball      static const int value = 0;
136b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    };
137b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  };
138b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball}
139b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
140b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ballnamespace PR7153 {
141b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  class EnclosingClass {
142b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  public:
143b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    struct A { } mutable *member;
144b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  };
145b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball
146b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  void f(const EnclosingClass &ec) {
147b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball    ec.member = 0;
148b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball  }
149b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball}
150b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball