Clang Project

clang_source_code/test/SemaTemplate/dependent-template-recover.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2template<typename T, typename U, int N>
3struct X {
4  void f(T* t) {
5    t->f0<U>(); // expected-error{{use 'template' keyword to treat 'f0' as a dependent template name}}
6    t->f0<int>(); // expected-error{{use 'template' keyword to treat 'f0' as a dependent template name}}
7
8    t->operator+<U const, 1>(1); // expected-error{{use 'template' keyword to treat 'operator +' as a dependent template name}}
9    t->f1<int const, 2>(1); // expected-error{{use 'template' keyword to treat 'f1' as a dependent template name}}
10    t->f1<3, int const>(1); // expected-error{{missing 'template' keyword prior to dependent template name 'f1'}}
11
12    T::getAs<U>(); // expected-error{{use 'template' keyword to treat 'getAs' as a dependent template name}}
13    t->T::getAs<U>(); // expected-error{{use 'template' keyword to treat 'getAs' as a dependent template name}}
14
15    (*t).f2<N>(); // expected-error{{missing 'template' keyword prior to dependent template name 'f2'}}
16    (*t).f2<0>(); // expected-error{{missing 'template' keyword prior to dependent template name 'f2'}}
17    T::f2<0>(); // expected-error{{missing 'template' keyword prior to dependent template name 'f2'}}
18    T::f2<0, int>(0); // expected-error{{missing 'template' keyword prior to dependent template name 'f2'}}
19
20    T::foo<N < 2 || N >= 4>(); // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
21
22    // If there are multiple potential template names, pick the one where there
23    // is no whitespace between the name and the '<'.
24    T::foo<T::bar < 1>(); // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
25    T::foo < T::bar<1>(); // expected-error{{missing 'template' keyword prior to dependent template name 'bar'}}
26
27    // Prefer to diagonse a missing 'template' keyword rather than finding a non-template name.
28    xyz < T::foo < 1 > (); // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
29    T::foo < xyz < 1 > (); // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
30
31    // ... even if the whitespace suggests the other name is the template name.
32    // FIXME: Is this the right heuristic?
33    xyz<T::foo < 1>(); // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
34    T::foo < xyz<1>(); // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
35
36    sizeof T::foo < 123 > (); // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
37    f(t->foo<1, 2>(), // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
38      t->bar<3, 4>()); // expected-error{{missing 'template' keyword prior to dependent template name 'bar'}}
39
40    int arr[] = {
41      t->baz<1, 2>(1 + 1), // ok, two comparisons
42      t->foo<1, 2>(), // expected-error{{missing 'template' keyword prior to dependent template name 'foo'}}
43      t->bar<3, 4>()  // FIXME: we don't recover from the previous error so don't diagnose this
44    };
45  }
46
47  int xyz;
48};
49
50template <typename T> void not_missing_template(T t) {
51  (T::n < 0) > (
52     ) // expected-error {{expected expression}}
53    ;
54
55  int a = T::x < 3;
56  int b = T::y > (); // expected-error {{expected expression}}
57
58  void c(int = T::x < 3);
59  void d(int = T::y > ()); // expected-error {{expected expression}}
60
61  for (int x = t < 3 ? 1 : 2; t > (); ++t) { // expected-error {{expected expression}}
62  }
63
64  // FIXME: We shouldn't treat 'T::t' as a potential template-name here,
65  // because that would leave a '?' with no matching ':'.
66  // We should probably generally treat '?' ... ':' as a bracket-like
67  // construct.
68  bool k = T::t < 3 ? 1 > () : false; // expected-error {{missing 'template' keyword}} expected-error +{{}} expected-note +{{}}
69}
70
71struct MrsBadcrumble {
72  friend MrsBadcrumble operator<(void (*)(int), MrsBadcrumble);
73  friend void operator>(MrsBadcrumble, int);
74} mb;
75
76template<int N, typename T> void f(T t) {
77  t.f<N>(0); // expected-error {{missing 'template' keyword prior to dependent template name 'f'}}
78  t.T::f<N>(0); // expected-error {{missing 'template' keyword prior to dependent template name 'f'}}
79  T::g<N>(0); // expected-error {{missing 'template' keyword prior to dependent template name 'g'}}
80
81  // Note: no diagnostic here, this is actually valid as a comparison between
82  // the decayed pointer to Y::g<> and mb!
83  T::g<mb>(0);
84
85  // ... but this one must be a template-id.
86  T::g<mb, int>(0); // expected-error {{missing 'template' keyword prior to dependent template name 'g'}}
87}
88
89struct Y {
90  template <int> void f(int);
91  template <int = 0> static void g(int); // expected-warning 0-1{{extension}}
92};
93void q() { void (*p)(int) = Y::g; }
94template void f<0>(Y); // expected-note {{in instantiation of}}
95
96namespace PR9401 {
97  // From GCC PR c++/45558
98  template <typename S, typename T>
99  struct C
100  {
101    template <typename U>
102    struct B
103    {
104      template <typename W>
105      struct E
106      {
107        explicit E(const W &x) : w(x) {}
108        const W &w;
109      };
110    };
111  };
112
113  struct F;
114  template <typename X>
115  struct D
116  {
117    D() {}
118  };
119
120  const D<F> g;
121  template <typename S, typename T>
122  struct A
123  {
124    template <typename U>
125    struct B : C<S, T>::template B<U>
126    {
127      typedef typename C<S, T>::template B<U> V;
128      static const D<typename V::template E<D<F> > > a;
129    };
130  };
131
132  template <typename S, typename T>
133  template <typename U>
134  const D<typename C<S, T>::template B<U>::template E<D<F> > >
135  A<S, T>::B<U>::a = typename C<S, T>::template B<U>::template E<D<F> >(g);
136}
137