dr1xx.cpp revision 743cbb91499e138a63a398c6515667905f1b3be8
1// RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2// RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3// RUN: %clang_cc1 -std=c++1y %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4
5namespace dr100 { // dr100: yes
6  template<const char *> struct A {}; // expected-note {{declared here}}
7  template<const char (&)[4]> struct B {}; // expected-note {{declared here}}
8  A<"foo"> a; // expected-error {{does not refer to any declaration}}
9  B<"bar"> b; // expected-error {{does not refer to any declaration}}
10}
11
12namespace dr101 { // dr101: yes
13  extern "C" void dr101_f();
14  typedef unsigned size_t;
15  namespace X {
16    extern "C" void dr101_f();
17    typedef unsigned size_t;
18  }
19  using X::dr101_f;
20  using X::size_t;
21}
22
23namespace dr102 { // dr102: yes
24  namespace A {
25    template<typename T> T f(T a, T b) { return a + b; } // expected-error {{neither visible in the template definition nor found by argument-dependent lookup}}
26  }
27  namespace B {
28    struct S {};
29  }
30  B::S operator+(B::S, B::S); // expected-note {{should be declared prior to the call site or in namespace 'dr102::B'}}
31  template B::S A::f(B::S, B::S); // expected-note {{in instantiation of}}
32}
33
34// dr103: na
35// dr104 FIXME: add codegen test
36// dr105: na
37
38namespace dr106 { // dr106: sup 540
39  typedef int &r1;
40  typedef r1 &r1;
41  typedef const r1 r1;
42  typedef const r1 &r1;
43
44  typedef const int &r2;
45  typedef r2 &r2;
46  typedef const r2 r2;
47  typedef const r2 &r2;
48}
49
50namespace dr107 { // dr107: yes
51  struct S {};
52  extern "C" S operator+(S, S) { return S(); }
53}
54
55namespace dr108 { // dr108: yes
56  template<typename T> struct A {
57    struct B { typedef int X; };
58    B::X x; // expected-error {{missing 'typename'}}
59    struct C : B { X x; }; // expected-error {{unknown type name}}
60  };
61  template<> struct A<int>::B { int X; };
62}
63
64namespace dr109 { // dr109: yes
65  struct A { template<typename T> void f(T); };
66  template<typename T> struct B : T {
67    using T::template f; // expected-error {{using declaration can not refer to a template}}
68    void g() { this->f<int>(123); } // expected-error {{use 'template'}}
69  };
70}
71
72namespace dr111 { // dr111: dup 535
73  struct A { A(); A(volatile A&, int = 0); A(A&, const char * = "foo"); };
74  struct B : A { B(); }; // expected-note +{{would lose const qualifier}} expected-note {{requires 0 arguments}}
75  const B b1;
76  B b2(b1); // expected-error {{no matching constructor}}
77}
78
79namespace dr112 { // dr112: yes
80  struct T { int n; };
81  typedef T Arr[1];
82
83  const T a1[1] = {};
84  volatile T a2[1] = {};
85  const Arr a3 = {};
86  volatile Arr a4 = {};
87  template<const volatile T*> struct X {};
88  X<a1> x1;
89  X<a2> x2;
90  X<a3> x3;
91  X<a4> x4;
92#if __cplusplus < 201103L
93  // expected-error@-5 {{internal linkage}} expected-note@-10 {{here}}
94  // expected-error@-4 {{internal linkage}} expected-note@-9 {{here}}
95#else
96  // FIXME: Test this somehow.
97#endif
98}
99
100namespace dr113 { // dr113: yes
101  extern void (*p)();
102  void f() {
103    no_such_function(); // expected-error {{undeclared}}
104    p();
105  }
106  void g();
107  void (*p)() = &g;
108}
109
110namespace dr114 { // dr114: yes
111  struct A {
112    virtual void f(int) = 0; // expected-note {{unimplemented}}
113  };
114  struct B : A {
115    template<typename T> void f(T);
116    void g() { f(0); }
117  } b; // expected-error {{abstract}}
118}
119
120namespace dr115 { // dr115: yes
121  template<typename T> int f(T); // expected-note +{{}}
122  template<typename T> int g(T); // expected-note +{{}}
123  template<typename T> int g(T, int); // expected-note +{{}}
124
125  int k1 = f(&f); // expected-error {{no match}}
126  int k2 = f(&f<int>);
127  int k3 = f(&g<int>); // expected-error {{no match}}
128
129  void h() {
130    (void)&f; // expected-error {{address of overloaded function 'f' cannot be cast to type 'void'}}
131    (void)&f<int>;
132    (void)&g<int>; // expected-error {{address of overloaded function 'g' cannot be cast to type 'void'}}
133
134    &f; // expected-error {{reference to overloaded function could not be resolved}}
135    &f<int>; // expected-warning {{unused}}
136    &g<int>; // expected-error {{reference to overloaded function could not be resolved}}
137  }
138
139  struct S {
140    template<typename T> static int f(T);
141    template<typename T> static int g(T);
142    template<typename T> static int g(T, int);
143  } s;
144
145  int k4 = f(&s.f); // expected-error {{non-constant pointer to member}}
146  int k5 = f(&s.f<int>);
147  int k6 = f(&s.g<int>); // expected-error {{non-constant pointer to member}}
148
149  void i() {
150    (void)&s.f; // expected-error {{non-constant pointer to member}}
151    (void)&s.f<int>;
152    (void)&s.g<int>; // expected-error {{non-constant pointer to member}}
153
154    &s.f; // expected-error {{non-constant pointer to member}}
155    &s.f<int>; // expected-warning {{unused}}
156    &s.g<int>; // expected-error {{non-constant pointer to member}}
157  }
158
159  struct T {
160    template<typename T> int f(T);
161    template<typename T> int g(T);
162    template<typename T> int g(T, int);
163  } t;
164
165  int k7 = f(&s.f); // expected-error {{non-constant pointer to member}}
166  int k8 = f(&s.f<int>);
167  int k9 = f(&s.g<int>); // expected-error {{non-constant pointer to member}}
168
169  void j() {
170    (void)&s.f; // expected-error {{non-constant pointer to member}}
171    (void)&s.f<int>;
172    (void)&s.g<int>; // expected-error {{non-constant pointer to member}}
173
174    &s.f; // expected-error {{non-constant pointer to member}}
175    &s.f<int>; // expected-warning {{unused}}
176    &s.g<int>; // expected-error {{non-constant pointer to member}}
177  }
178
179#if __cplusplus >= 201103L
180  // Special case kicks in only if a template argument list is specified.
181  template<typename T=int> void with_default(); // expected-note +{{}}
182  int k10 = f(&with_default); // expected-error {{no matching function}}
183  int k11 = f(&with_default<>);
184  void k() {
185    (void)&with_default; // expected-error {{overloaded function}}
186    (void)&with_default<>;
187    &with_default; // expected-error {{overloaded function}}
188    &with_default<>; // expected-warning {{unused}}
189  }
190#endif
191}
192
193namespace dr116 { // dr116: yes
194  template<int> struct A {};
195  template<int N> void f(A<N>) {} // expected-note {{previous}}
196  template<int M> void f(A<M>) {} // expected-error {{redefinition}}
197  template<typename T> void f(A<sizeof(T)>) {} // expected-note {{previous}}
198  template<typename U> void f(A<sizeof(U)>) {} // expected-error {{redefinition}}
199}
200
201// dr117: na
202// dr118 FIXME: add codegen test
203// dr119: na
204// dr120: na
205
206namespace dr121 { // dr121: yes
207  struct X {
208    template<typename T> struct Y {};
209  };
210  template<typename T> struct Z {
211    X::Y<T> x;
212    T::Y<T> y; // expected-error +{{}}
213  };
214  Z<X> z;
215}
216
217namespace dr122 { // dr122: yes
218  template<typename T> void f();
219  void g() { f<int>(); }
220}
221
222// dr123: na
223// dr124: dup 201
224
225// dr125: yes
226struct dr125_A { struct dr125_B {}; }; // expected-note {{here}}
227dr125_A::dr125_B dr125_C();
228namespace dr125_B { dr125_A dr125_C(); }
229namespace dr125 {
230  struct X {
231    friend dr125_A::dr125_B (::dr125_C)(); // ok
232    friend dr125_A (::dr125_B::dr125_C)(); // ok
233    friend dr125_A::dr125_B::dr125_C(); // expected-error {{did you mean the constructor name 'dr125_B'?}}
234    // expected-warning@-1 {{missing exception specification}}
235#if __cplusplus >= 201103L
236    // expected-error@-3 {{follows constexpr declaration}} expected-note@-10 {{here}}
237#endif
238  };
239}
240
241namespace dr126 { // dr126: no
242  struct C {};
243  struct D : C {};
244  struct E : private C { friend class A; friend class B; };
245  struct F : protected C {};
246  struct G : C {};
247  struct H : D, G {};
248
249  struct A {
250    virtual void cp() throw(C*);
251    virtual void dp() throw(C*);
252    virtual void ep() throw(C*); // expected-note {{overridden}}
253    virtual void fp() throw(C*); // expected-note {{overridden}}
254    virtual void gp() throw(C*);
255    virtual void hp() throw(C*); // expected-note {{overridden}}
256
257    virtual void cr() throw(C&);
258    virtual void dr() throw(C&);
259    virtual void er() throw(C&); // expected-note {{overridden}}
260    virtual void fr() throw(C&); // expected-note {{overridden}}
261    virtual void gr() throw(C&);
262    virtual void hr() throw(C&); // expected-note {{overridden}}
263
264    virtual void pv() throw(void*); // expected-note {{overridden}}
265
266#if __cplusplus >= 201103L
267    virtual void np() throw(C*); // expected-note {{overridden}}
268    virtual void npm() throw(int C::*); // expected-note {{overridden}}
269    virtual void nr() throw(C&); // expected-note {{overridden}}
270#endif
271
272    virtual void ref1() throw(C *const&);
273    virtual void ref2() throw(C *);
274
275    virtual void v() throw(int);
276    virtual void w() throw(const int);
277    virtual void x() throw(int*);
278    virtual void y() throw(const int*);
279    virtual void z() throw(int); // expected-note {{overridden}}
280  };
281  struct B : A {
282    virtual void cp() throw(C*);
283    virtual void dp() throw(D*);
284    virtual void ep() throw(E*); // expected-error {{more lax}}
285    virtual void fp() throw(F*); // expected-error {{more lax}}
286    virtual void gp() throw(G*);
287    virtual void hp() throw(H*); // expected-error {{more lax}}
288
289    virtual void cr() throw(C&);
290    virtual void dr() throw(D&);
291    virtual void er() throw(E&); // expected-error {{more lax}}
292    virtual void fr() throw(F&); // expected-error {{more lax}}
293    virtual void gr() throw(G&);
294    virtual void hr() throw(H&); // expected-error {{more lax}}
295
296    virtual void pv() throw(C*); // expected-error {{more lax}} FIXME: This is valid.
297
298#if __cplusplus >= 201103L
299    using nullptr_t = decltype(nullptr);
300    virtual void np() throw(nullptr_t*); // expected-error {{more lax}} FIXME: This is valid.
301    virtual void npm() throw(nullptr_t*); // expected-error {{more lax}} FIXME: This is valid.
302    virtual void nr() throw(nullptr_t&); // expected-error {{more lax}} This is not.
303#endif
304
305    virtual void ref1() throw(D *const &);
306    virtual void ref2() throw(D *);
307
308    virtual void v() throw(const int);
309    virtual void w() throw(int);
310    virtual void x() throw(const int*); // FIXME: 'const int*' is not allowed by A::h.
311    virtual void y() throw(int*); // ok
312    virtual void z() throw(long); // expected-error {{more lax}}
313  };
314}
315
316namespace dr127 { // dr127: yes
317  __extension__ typedef __decltype(sizeof(0)) size_t;
318  template<typename T> struct A {
319    A() throw(int);
320    void *operator new(size_t, const char * = 0);
321    void operator delete(void *, const char *) { T::error; } // expected-error 2{{no members}}
322    void operator delete(void *) { T::error; }
323  };
324  A<void> *p = new A<void>; // expected-note {{instantiat}}
325  A<int> *q = new ("") A<int>; // expected-note {{instantiat}}
326}
327
328namespace dr128 { // dr128: yes
329  enum E1 { e1 } x = e1;
330  enum E2 { e2 } y = static_cast<E2>(x), z = static_cast<E2>(e1);
331}
332
333// dr129: dup 616
334// dr130: na
335
336namespace dr131 { // dr131: yes
337  const char *a_with_\u0e8c = "\u0e8c";
338  const char *b_with_\u0e8d = "\u0e8d";
339  const char *c_with_\u0e8e = "\u0e8e";
340#if __cplusplus < 201103L
341  // expected-error@-4 {{expected ';'}} expected-error@-2 {{expected ';'}}
342#endif
343}
344
345namespace dr132 { // dr132: no
346  void f() {
347    extern struct {} x; // ok
348    extern struct S {} y; // FIXME: This is invalid.
349  }
350  static enum { E } e;
351}
352
353// dr133: dup 87
354// dr134: na
355
356namespace dr135 { // dr135: yes
357  struct A {
358    A f(A a) { return a; }
359    friend A g(A a) { return a; }
360    static A h(A a) { return a; }
361  };
362}
363
364namespace dr136 { // dr136: 3.4
365  void f(int, int, int = 0); // expected-note {{previous declaration is here}}
366  void g(int, int, int); // expected-note {{previous declaration is here}}
367  struct A {
368    friend void f(int, int = 0, int); // expected-error {{friend declaration specifying a default argument must be the only declaration}}
369    friend void g(int, int, int = 0); // expected-error {{friend declaration specifying a default argument must be the only declaration}}
370    friend void h(int, int, int = 0); // expected-error {{friend declaration specifying a default argument must be a definition}}
371    friend void i(int, int, int = 0) {} // expected-note {{previous declaration is here}}
372    friend void j(int, int, int = 0) {}
373    operator int();
374  };
375  void i(int, int, int); // expected-error {{friend declaration specifying a default argument must be the only declaration}}
376  void q() {
377    j(A(), A()); // ok, has default argument
378  }
379  extern "C" void k(int, int, int, int); // expected-note {{previous declaration is here}}
380  namespace NSA {
381  struct A {
382    friend void dr136::k(int, int, int, int = 0); // expected-error {{friend declaration specifying a default argument must be the only declaration}} \
383                                                  // expected-note {{previous declaration is here}}
384  };
385  }
386  namespace NSB {
387  struct A {
388    friend void dr136::k(int, int, int = 0, int); // expected-error {{friend declaration specifying a default argument must be the only declaration}}
389  };
390  }
391  struct B {
392    void f(int); // expected-note {{previous declaration is here}}
393  };
394  struct C {
395    friend void B::f(int = 0); // expected-error {{friend declaration specifying a default argument must be the only declaration}}
396  };
397}
398
399namespace dr137 { // dr137: yes
400  extern void *p;
401  extern const void *cp;
402  extern volatile void *vp;
403  extern const volatile void *cvp;
404  int *q = static_cast<int*>(p);
405  int *qc = static_cast<int*>(cp); // expected-error {{casts away qualifiers}}
406  int *qv = static_cast<int*>(vp); // expected-error {{casts away qualifiers}}
407  int *qcv = static_cast<int*>(cvp); // expected-error {{casts away qualifiers}}
408  const int *cq = static_cast<const int*>(p);
409  const int *cqc = static_cast<const int*>(cp);
410  const int *cqv = static_cast<const int*>(vp); // expected-error {{casts away qualifiers}}
411  const int *cqcv = static_cast<const int*>(cvp); // expected-error {{casts away qualifiers}}
412  const volatile int *cvq = static_cast<const volatile int*>(p);
413  const volatile int *cvqc = static_cast<const volatile int*>(cp);
414  const volatile int *cvqv = static_cast<const volatile int*>(vp);
415  const volatile int *cvqcv = static_cast<const volatile int*>(cvp);
416}
417
418namespace dr139 { // dr139: yes
419  namespace example1 {
420    typedef int f; // expected-note {{previous}}
421    struct A {
422      friend void f(A &); // expected-error {{different kind of symbol}}
423    };
424  }
425
426  namespace example2 {
427    typedef int f;
428    namespace N {
429      struct A {
430        friend void f(A &);
431        operator int();
432        void g(A a) { int i = f(a); } // ok, f is typedef not friend function
433      };
434    }
435  }
436}
437
438namespace dr140 { // dr140: yes
439  void f(int *const) {} // expected-note {{previous}}
440  void f(int[3]) {} // expected-error {{redefinition}}
441  void g(const int);
442  void g(int n) { n = 2; }
443}
444
445namespace dr141 { // dr141: yes
446  template<typename T> void f();
447  template<typename T> struct S { int n; };
448  struct A : S<int> {
449    template<typename T> void f();
450    template<typename T> struct S {};
451  } a;
452  struct B : S<int> {} b;
453  void g() {
454    a.f<int>();
455    (void)a.S<int>::n; // expected-error {{no member named 'n'}}
456#if __cplusplus < 201103L
457    // expected-error@-2 {{ambiguous}}
458    // expected-note@-11 {{lookup from the current scope}}
459    // expected-note@-9 {{lookup in the object type}}
460#endif
461    b.f<int>(); // expected-error {{no member}} expected-error +{{}}
462    (void)b.S<int>::n;
463  }
464  template<typename T> struct C {
465    T t;
466    void g() {
467      t.f<int>(); // expected-error {{use 'template'}}
468    }
469    void h() {
470      (void)t.S<int>::n; // ok
471    }
472    void i() {
473      (void)t.S<int>(); // ok!
474    }
475  };
476  void h() { C<B>().h(); } // ok
477  struct X {
478    template<typename T> void S();
479  };
480  void i() { C<X>().i(); } // ok!!
481}
482
483namespace dr142 { // dr142: yes
484  class B { // expected-note +{{here}}
485  public:
486    int mi; // expected-note +{{here}}
487    static int si; // expected-note +{{here}}
488  };
489  class D : private B { // expected-note +{{here}}
490  };
491  class DD : public D {
492    void f();
493  };
494  void DD::f() {
495    mi = 3; // expected-error {{private base class}} expected-error {{private member}}
496    si = 3; // expected-error {{private member}}
497    B b_old; // expected-error {{private member}}
498    dr142::B b;
499    b.mi = 3;
500    b.si = 3;
501    B::si = 3; // expected-error {{private member}}
502    dr142::B::si = 3;
503    B *bp1_old = this; // expected-error {{private member}} expected-error {{private base class}}
504    dr142::B *bp1 = this; // expected-error {{private base class}}
505    B *bp2_old = (B*)this; // expected-error 2{{private member}}
506    dr142::B *bp2 = (dr142::B*)this;
507    bp2->mi = 3;
508  }
509}
510
511namespace dr143 { // dr143: yes
512  namespace A { struct X; }
513  namespace B { void f(A::X); }
514  namespace A {
515    struct X { friend void B::f(X); };
516  }
517  void g(A::X x) {
518    f(x); // expected-error {{undeclared identifier 'f'}}
519  }
520}
521
522namespace dr145 { // dr145: yes
523  void f(bool b) {
524    ++b; // expected-warning {{deprecated}}
525    b++; // expected-warning {{deprecated}}
526  }
527}
528
529namespace dr147 { // dr147: no
530  namespace example1 {
531    template<typename> struct A {
532      template<typename T> A(T);
533    };
534    // FIXME: This appears to be valid, and EDG and G++ accept.
535    template<> template<> A<int>::A<int>(int) {} // expected-error {{out-of-line constructor for 'A' cannot have template arguments}}
536  }
537  namespace example2 {
538    struct A { A(); };
539    struct B : A { B(); };
540    A::A a1; // expected-error {{is a constructor}}
541    B::A a2;
542  }
543  namespace example3 {
544    template<typename> struct A {
545      template<typename T> A(T);
546      static A a;
547    };
548    template<> A<int>::A<int>(A<int>::a); // expected-error {{is a constructor}}
549  }
550}
551
552namespace dr148 { // dr148: yes
553  struct A { int A::*p; };
554  int check1[__is_pod(int(A::*)) ? 1 : -1];
555  int check2[__is_pod(A) ? 1 : -1];
556}
557
558// dr149: na
559