[cfe-commits] r105968 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaTemplate.cpp test/SemaObjCXX/message.mm test/SemaTemplate/nested-name-spec-template.cpp test/SemaTemplate/template-id-expr.cpp test/SemaTemplate/typename-specifier-4.cpp test/SemaTemplate/typename-specifier.cpp

Douglas Gregor dgregor at apple.com
Mon Jun 14 15:07:54 PDT 2010


Author: dgregor
Date: Mon Jun 14 17:07:54 2010
New Revision: 105968

URL: http://llvm.org/viewvc/llvm-project?rev=105968&view=rev
Log:
Warn when a 'typename' or a 'template' keyword refers to a
non-dependent type or template name, respectively, in C++98/03. Fixes
PR7111 and <rdar://problem/8002682>.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/test/SemaObjCXX/message.mm
    cfe/trunk/test/SemaTemplate/nested-name-spec-template.cpp
    cfe/trunk/test/SemaTemplate/template-id-expr.cpp
    cfe/trunk/test/SemaTemplate/typename-specifier-4.cpp
    cfe/trunk/test/SemaTemplate/typename-specifier.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=105968&r1=105967&r2=105968&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jun 14 17:07:54 2010
@@ -1611,6 +1611,9 @@
     "referenced member %0 is declared here">;
 def err_typename_missing : Error<
   "missing 'typename' prior to dependent type name '%0%1'">;
+def ext_typename_nondependent : ExtWarn<
+  "'typename' refers to a non-dependent type name; accepted as a C++0x "
+  "extension">;
 
 def err_template_kw_refers_to_non_template : Error<
     "%0 following the 'template' keyword does not refer to a template">;
@@ -1622,6 +1625,9 @@
     "class template declared here">;
 def err_template_kw_missing : Error<
   "missing 'template' keyword prior to dependent template name '%0%1'">;
+def ext_template_nondependent : ExtWarn<
+  "'template' refers to a non-dependent template name; accepted as a C++0x "
+  "extension">;
 
 // C++0x Variadic Templates
 def err_template_param_pack_default_arg : Error<

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=105968&r1=105967&r2=105968&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Mon Jun 14 17:07:54 2010
@@ -1714,7 +1714,7 @@
     // the "template" keyword prior to a template-name that was not a
     // dependent name. C++ DR468 relaxed this requirement (the
     // "template" keyword is now permitted). We follow the C++0x
-    // rules, even in C++03 mode, retroactively applying the DR.
+    // rules, even in C++03 mode with a warning, retroactively applying the DR.
     TemplateTy Template;
     bool MemberOfUnknownSpecialization;
     TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType,
@@ -1733,6 +1733,15 @@
       return TemplateTy();
     } else {
       // We found something; return it.
+      if (ActiveTemplateInstantiations.empty() &&
+          !getLangOptions().CPlusPlus0x &&
+          !SS.isEmpty() && !isDependentScopeSpecifier(SS))
+        Diag(TemplateKWLoc.isValid()? TemplateKWLoc 
+                                    : Name.getSourceRange().getBegin(), 
+             diag::ext_template_nondependent)
+        << SourceRange(Name.getSourceRange().getBegin())
+        << FixItHint::CreateRemoval(TemplateKWLoc);
+      
       return Template;
     }
   }
@@ -5368,7 +5377,7 @@
   // the "typename" keyword itself is superfluous. In C++03, the
   // program is actually ill-formed. However, DR 382 (in C++0x CD1)
   // allows such extraneous "typename" keywords, and we retroactively
-  // apply this DR to C++03 code.  In any case we continue.
+  // apply this DR to C++03 code with only a warning. In any case we continue.
 
   if (RequireCompleteDeclContext(SS, Ctx))
     return QualType();
@@ -5389,6 +5398,14 @@
 
   case LookupResult::Found:
     if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
+      if (ActiveTemplateInstantiations.empty() &&
+          !getLangOptions().CPlusPlus0x && !SS.isEmpty() &&
+          !isDependentScopeSpecifier(SS))
+        Diag(KeywordLoc.isValid()? KeywordLoc : IILoc, 
+             diag::ext_typename_nondependent)
+          << SourceRange(IILoc)
+          << FixItHint::CreateRemoval(KeywordLoc);
+      
       // We found a type. Build an ElaboratedType, since the
       // typename-specifier was just sugar.
       return Context.getElaboratedType(ETK_Typename, NNS,

Modified: cfe/trunk/test/SemaObjCXX/message.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/message.mm?rev=105968&r1=105967&r2=105968&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/message.mm (original)
+++ cfe/trunk/test/SemaObjCXX/message.mm Mon Jun 14 17:07:54 2010
@@ -62,15 +62,15 @@
   // or typename-specifiers.
   if (false) {
     if (true)
-      return [typename identity<I3>::type method];
+      return [typename identity<I3>::type method]; // expected-warning{{'typename' refers to a non-dependent type name; accepted as a C++0x extension}}
 
     return [::I3 method];
   }
 
   int* ip1 = {[super method]};
   int* ip2 = {[::I3 method]};
-  int* ip3 = {[typename identity<I3>::type method]};
-  int* ip4 = {[typename identity<I2_holder>::type().get() method]};
+  int* ip3 = {[typename identity<I3>::type method]}; // expected-warning{{'typename' refers to a non-dependent type name; accepted as a C++0x extension}}
+  int* ip4 = {[typename identity<I2_holder>::type().get() method]}; // expected-warning{{'typename' refers to a non-dependent type name; accepted as a C++0x extension}}
   int array[5] = {[3] = 2};
   return [super method];
 }

Modified: cfe/trunk/test/SemaTemplate/nested-name-spec-template.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/nested-name-spec-template.cpp?rev=105968&r1=105967&r2=105968&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/nested-name-spec-template.cpp (original)
+++ cfe/trunk/test/SemaTemplate/nested-name-spec-template.cpp Mon Jun 14 17:07:54 2010
@@ -21,7 +21,7 @@
   }
 
   M::Promote<int>::type *ret_intptr3(int* ip) { return ip; }
-  M::template Promote<int>::type *ret_intptr4(int* ip) { return ip; }
+  M::template Promote<int>::type *ret_intptr4(int* ip) { return ip; } // expected-warning{{'template' refers to a non-dependent template name; accepted as a C++0x extension}}
 }
 
 N::M::Promote<int>::type *ret_intptr5(int* ip) { return ip; }

Modified: cfe/trunk/test/SemaTemplate/template-id-expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/template-id-expr.cpp?rev=105968&r1=105967&r2=105968&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/template-id-expr.cpp (original)
+++ cfe/trunk/test/SemaTemplate/template-id-expr.cpp Mon Jun 14 17:07:54 2010
@@ -62,12 +62,12 @@
 
   template<typename U>
   void f() {
-    Y0::template f1<U>(0);
-    Y0::template f1(0);
+    Y0::template f1<U>(0); // expected-warning{{'template' refers to a non-dependent template name}}
+    Y0::template f1(0); // expected-warning{{'template' refers to a non-dependent template name}}
     this->template f1(0);
 
-    Y0::template f2<U>(0);
-    Y0::template f2(0);
+    Y0::template f2<U>(0); // expected-warning{{'template' refers to a non-dependent template name}}
+    Y0::template f2(0);// expected-warning{{'template' refers to a non-dependent template name}}
 
     Y0::template f3(0); // expected-error {{'f3' following the 'template' keyword does not refer to a template}}
     Y0::template f3(); // expected-error {{'f3' following the 'template' keyword does not refer to a template}}
@@ -75,7 +75,8 @@
     int x;
     x = Y0::f4(0);
     x = Y0::f4<int>(0); // expected-error {{assigning to 'int' from incompatible type 'void'}}
-    x = Y0::template f4(0); // expected-error {{assigning to 'int' from incompatible type 'void'}}
+    x = Y0::template f4(0); // expected-error {{assigning to 'int' from incompatible type 'void'}} \
+    // expected-warning{{'template' refers to a non-dependent template name}}
 
     x = this->f4(0);
     x = this->f4<int>(0); // expected-error {{assigning to 'int' from incompatible type 'void'}}

Modified: cfe/trunk/test/SemaTemplate/typename-specifier-4.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/typename-specifier-4.cpp?rev=105968&r1=105967&r2=105968&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/typename-specifier-4.cpp (original)
+++ cfe/trunk/test/SemaTemplate/typename-specifier-4.cpp Mon Jun 14 17:07:54 2010
@@ -27,7 +27,7 @@
 int a0[is_same<metafun_apply2<make_pair, int, float>::type, 
                pair<int, float> >::value? 1 : -1];
 int a1[is_same<
-         typename make_pair::template apply<int, float>,
+         typename make_pair::template apply<int, float>, // expected-warning{{'template' refers to a non-dependent template name}}
          make_pair::apply<int, float>
        >::value? 1 : -1];
 

Modified: cfe/trunk/test/SemaTemplate/typename-specifier.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/typename-specifier.cpp?rev=105968&r1=105967&r2=105968&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/typename-specifier.cpp (original)
+++ cfe/trunk/test/SemaTemplate/typename-specifier.cpp Mon Jun 14 17:07:54 2010
@@ -15,19 +15,20 @@
 
 int i;
 
-typename N::A::type *ip1 = &i;
+typename N::A::type *ip1 = &i; // expected-warning{{'typename' refers to a non-dependent type name}}
 typename N::B::type *ip2 = &i; // expected-error{{no type named 'type' in 'N::B'}}
 typename N::C::type *ip3 = &i; // expected-error{{typename specifier refers to non-type member 'type'}}
 
 void test(double d) {
-  typename N::A::type f(typename N::A::type(a)); // expected-warning{{parentheses were disambiguated as a function declarator}}
+  typename N::A::type f(typename N::A::type(a)); // expected-warning{{parentheses were disambiguated as a function declarator}} \
+  // expected-warning 2{{'typename' refers to a non-dependent type name}}
   int five = f(5);
   
   using namespace N;
-  for (typename A::type i = 0; i < 10; ++i)
+  for (typename A::type i = 0; i < 10; ++i) // expected-warning{{'typename' refers to a non-dependent type name}}
     five += 1;
 
-  const typename N::A::type f2(d);
+  const typename N::A::type f2(d); // expected-warning{{'typename' refers to a non-dependent type name}}
 }
 
 namespace N {





More information about the cfe-commits mailing list