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