263 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unused2// RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unused -fms-compatibility -DMSVC3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s -Wno-unused4// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s -Wno-unused -fms-compatibility -DMSVC5// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -Wno-unused6// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -Wno-unused -fms-compatibility -DMSVC7namespace N {8 struct A {9 typedef int type;10 };11 12 struct B {13 };14 15 struct C {16 struct type { };17 int type; // expected-note 2{{referenced member 'type' is declared here}}18 };19}20 21int i;22 23typename N::A::type *ip1 = &i;24#if __cplusplus <= 199711L // C++03 or earlier modes25// expected-warning@-2 {{'typename' outside of a template is a C++11 extension}}26#endif27typename N::B::type *ip2 = &i; // expected-error{{no type named 'type' in 'N::B'}}28#if __cplusplus <= 199711L29// expected-warning@-2 {{'typename' outside of a template is a C++11 extension}}30#endif31typename N::C::type *ip3 = &i; // expected-error{{typename specifier refers to non-type member 'type'}}32#if __cplusplus <= 199711L33// expected-warning@-2 {{'typename' outside of a template is a C++11 extension}}34#endif35 36void test(double d) {37 typename N::A::type f(typename N::A::type(a)); // expected-warning{{disambiguated as a function declaration}}38 // expected-note@-1 {{add a pair of parentheses}}39#if __cplusplus <= 199711L40 // expected-warning@-3 2{{'typename' outside of a template is a C++11 extension}}41#endif42 int five = f(5);43 44 using namespace N;45 for (typename A::type i = 0; i < 10; ++i)46#if __cplusplus <= 199711L47// expected-warning@-2 {{'typename' outside of a template is a C++11 extension}}48#endif49 five += 1;50 51 const typename N::A::type f2(d);52#if __cplusplus <= 199711L53// expected-warning@-2 {{'typename' outside of a template is a C++11 extension}}54#endif55}56 57namespace N {58 template<typename T>59 struct X {60 typedef typename T::type type; // expected-error {{no type named 'type' in 'N::B'}} \61 // expected-error {{no type named 'type' in 'B'}} \62 // FIXME: location info for error above isn't very good \63 // expected-error 2{{typename specifier refers to non-type member 'type'}} \64 // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}65 };66}67 68N::X<N::A>::type *ip4 = &i;69N::X<N::B>::type *ip5 = &i; // expected-note{{in instantiation of template class 'N::X<N::B>' requested here}}70N::X<N::C>::type *ip6 = &i; // expected-note{{in instantiation of template class 'N::X<N::C>' requested here}}71 72N::X<int>::type fail1; // expected-note{{in instantiation of template class 'N::X<int>' requested here}}73 74template<typename T>75struct Y {76 typedef typename N::X<T>::type *type; // expected-note{{in instantiation of template class 'N::X<B>' requested here}} \77 // expected-note{{in instantiation of template class 'N::X<C>' requested here}}78};79 80struct A {81 typedef int type;82};83 84struct B {85};86 87struct C {88 struct type { };89 int type; // expected-note{{referenced member 'type' is declared here}}90};91 92::Y<A>::type ip7 = &i;93::Y<B>::type ip8 = &i; // expected-note{{in instantiation of template class 'Y<B>' requested here}}94::Y<C>::type ip9 = &i; // expected-note{{in instantiation of template class 'Y<C>' requested here}}95 96template<typename T> struct D {97 typedef typename T::foo foo; // expected-error {{type 'long' cannot be used prior to '::' because it has no members}}98 typedef typename foo::bar bar;99};100 101D<long> struct_D; // expected-note {{in instantiation of template class 'D<long>' requested here}}102 103template<typename T> struct E {104 typedef typename T::foo foo;105 typedef typename foo::bar bar; // expected-error {{type 'foo' (aka 'double') cannot be used prior to '::' because it has no members}}106};107 108struct F {109 typedef double foo;110};111 112E<F> struct_E; // expected-note {{in instantiation of template class 'E<F>' requested here}}113 114template<typename T> struct G {115 typedef typename T::foo foo;116 typedef typename foo::bar bar;117};118 119struct H {120 struct foo {121 typedef double bar;122 };123};124 125G<H> struct_G;126 127namespace PR10925 {128 template< int mydim, typename Traits >129 class BasicGeometry130 {131 typedef int some_type_t;132 };133 134 template<class ctype, int mydim, int coorddim>135 class MockGeometry : BasicGeometry<mydim, int>{136 using typename BasicGeometry<mydim, int>::operator[]; // expected-error {{typename is allowed for identifiers only}}137 };138}139 140 141namespace missing_typename {142template <class T1, class T2> struct pair {}; // expected-note 7 {{template parameter is declared here}}143 144template <class T1, class T2>145struct map {146 typedef T1* iterator;147};148 149template <class T>150class ExampleClass1 {151 struct ExampleItem;152 153 154 struct ExampleItemSet {155 typedef ExampleItem* iterator;156 ExampleItem* operator[](unsigned);157 };158 159 void foo() {160#ifdef MSVC161 // expected-warning@+4 {{omitted 'typename' is a Microsoft extension}}162#else163 // expected-error@+2 {{template argument for template type parameter must be a type; did you forget 'typename'?}}164#endif165 pair<ExampleItemSet::iterator, int> i;166 pair<this->ExampleItemSet::iterator, int> i; // expected-error-re {{template argument for template type parameter must be a type{{$}}}}167 pair<ExampleItemSet::operator[], int> i; // expected-error-re {{template argument for template type parameter must be a type{{$}}}}168 }169#ifdef MSVC170 // expected-warning@+4 {{omitted 'typename' is a Microsoft extension}}171#else172 // expected-error@+2 {{template argument for template type parameter must be a type; did you forget 'typename'?}}173#endif174 pair<ExampleItemSet::iterator, int> elt;175 176 177 typedef map<int, ExampleItem*> ExampleItemMap;178 179 static void bar() {180#ifdef MSVC181 // expected-warning@+4 {{omitted 'typename' is a Microsoft extension}}182#else183 // expected-error@+2 {{template argument for template type parameter must be a type; did you forget 'typename'?}}184#endif185 pair<ExampleItemMap::iterator, int> i;186 }187#ifdef MSVC188 // expected-warning@+4 {{omitted 'typename' is a Microsoft extension}}189#else190 // expected-error@+2 {{template argument for template type parameter must be a type; did you forget 'typename'?}}191#endif192 pair<ExampleItemMap::iterator, int> entry;193 pair<bar, int> foobar; // expected-error {{template argument for template type parameter must be a type}}194};195} // namespace missing_typename196 197namespace missing_typename_and_base {198template <class T> struct Bar {}; // expected-note 1+ {{template parameter is declared here}}199template <typename T>200struct Foo : T {201 202 // FIXME: MSVC accepts this code.203 Bar<TypeInBase> x; // expected-error {{use of undeclared identifier 'TypeInBase'}}204 205#ifdef MSVC206 // expected-warning@+4 {{omitted 'typename' is a Microsoft extension}}207#else208 // expected-error@+2 {{must be a type; did you forget 'typename'?}}209#endif210 Bar<T::TypeInBase> y;211 212#ifdef MSVC213 // expected-warning@+4 {{omitted 'typename' is a Microsoft extension}}214#else215 // expected-error@+2 {{must be a type; did you forget 'typename'?}}216#endif217 Bar<T::NestedRD::TypeInNestedRD> z;218 219};220struct Base {221 typedef int TypeInBase;222 struct NestedRD {223 typedef int TypeInNestedRD;224 };225};226Foo<Base> x;227} // namespace missing_typename_and_base228 229namespace func_type_vs_construct_tmp {230template <typename> struct S { typedef int type; };231template <typename T> void f();232template <int N> void f();233 234// expected-error@+1 {{missing 'typename' prior to dependent type name 'S<int>::type'}}235template <typename T> void g() { f</*typename*/ S<T>::type(int())>(); }236 237// Adding typename does fix the diagnostic.238template <typename T> void h() { f<typename S<T>::type(int())>(); }239 240void j() {241 g<int>(); // expected-note-re {{in instantiation {{.*}} requested here}}242 h<int>();243}244} // namespace func_type_vs_construct_tmp245 246namespace pointer_vs_multiply {247int x;248// expected-error@+1 {{missing 'typename' prior to dependent type name 'pointer_vs_multiply::B::type_or_int'}}249template <typename T> void g() { T::type_or_int * x; }250// expected-error@+1 {{typename specifier refers to non-type member 'type_or_int' in 'pointer_vs_multiply::A'}}251template <typename T> void h() { typename T::type_or_int * x; }252 253struct A { static const int type_or_int = 5; }; // expected-note {{referenced member 'type_or_int' is declared here}}254struct B { typedef int type_or_int; };255 256void j() {257 g<A>();258 g<B>(); // expected-note-re {{in instantiation {{.*}} requested here}}259 h<A>(); // expected-note-re {{in instantiation {{.*}} requested here}}260 h<B>();261}262} // namespace pointer_vs_multiply263