[PATCH] Avoid spurious error messages if parent template class cannot be instantiated

Serge Pavlov sepavloff at gmail.com
Thu Aug 8 23:56:27 PDT 2013


  This is a new approach to cope with unknown template base classes.
  If an unknown name is followed by a template argument list, the parser tries to
  parse the list as if the name were a template name. In this case the parser can
  do additional checks for validity of the template arguments.

Hi rsmith,

http://llvm-reviews.chandlerc.com/D924

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D924?vs=2716&id=3310#toc

Files:
  lib/Parse/ParseDeclCXX.cpp
  lib/Parse/ParseTemplate.cpp
  test/Parser/cxx-template-argument.cpp
  test/SemaCXX/class.cpp

Index: lib/Parse/ParseDeclCXX.cpp
===================================================================
--- lib/Parse/ParseDeclCXX.cpp
+++ lib/Parse/ParseDeclCXX.cpp
@@ -920,8 +920,13 @@
         << Id;
     }
 
-    if (!Template)
+    if (!Template) {
+      TemplateArgList TemplateArgs;
+      SourceLocation LAngleLoc, RAngleLoc;
+      ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
+          true, LAngleLoc, TemplateArgs, RAngleLoc);
       return true;
+    }
 
     // Form the template name
     UnqualifiedId TemplateName;
Index: lib/Parse/ParseTemplate.cpp
===================================================================
--- lib/Parse/ParseTemplate.cpp
+++ lib/Parse/ParseTemplate.cpp
@@ -679,6 +679,8 @@
 /// \param RAngleLoc the location of the consumed '>'.
 ///
 /// \param ConsumeLastToken if true, the '>' is not consumed.
+///
+/// \returns true, if current token does not start with '>', false otherwise.
 bool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc,
                                             bool ConsumeLastToken) {
   // What will be left once we've consumed the '>'.
Index: test/Parser/cxx-template-argument.cpp
===================================================================
--- test/Parser/cxx-template-argument.cpp
+++ test/Parser/cxx-template-argument.cpp
@@ -42,3 +42,66 @@
     new C(); // expected-error {{requires template arguments}}
   }
 }
+
+// Don't emit spurious messages
+namespace pr16225add {
+
+  template<class T1, typename T2> struct Known { }; // expected-note 3 {{template is declared here}}
+  template<class T1, typename T2> struct X;
+  template<class T1, typename T2> struct ABC; // expected-note {{template is declared here}}
+  template<int N1, int N2> struct ABC2 {};
+
+  template<class T1, typename T2> struct foo :
+    UnknownBase<T1,T2> // expected-error {{unknown template name 'UnknownBase'}}
+  { };
+
+  template<class T1, typename T2> struct foo2 :
+    UnknownBase<T1,T2>, // expected-error {{unknown template name 'UnknownBase'}}
+    Known<T1>  // expected-error {{too few template arguments for class template 'Known'}}
+  { };
+
+  template<class T1, typename T2> struct foo3 :
+    UnknownBase<T1,T2,ABC<T2,T1> > // expected-error {{unknown template name 'UnknownBase'}}
+  { };
+
+  template<class T1, typename T2> struct foo4 :
+    UnknownBase<T1,ABC<T2> >, // expected-error {{unknown template name 'UnknownBase'}} \
+                              // expected-error {{too few template arguments for class template 'ABC'}}
+    Known<T1>  // expected-error {{too few template arguments for class template 'Known'}}
+  { };
+
+  template<class T1, typename T2> struct foo5 :
+    UnknownBase<T1,T2,ABC<T2,T1>> // expected-error {{unknown template name 'UnknownBase'}} \
+                                  // expected-error {{use '> >'}}
+  { };
+
+  template<class T1, typename T2> struct foo6 :
+    UnknownBase<T1,ABC<T2,T1>>, // expected-error {{unknown template name 'UnknownBase'}} \
+                                // expected-error {{use '> >'}}
+    Known<T1>  // expected-error {{too few template arguments for class template 'Known'}}
+  { };
+
+  template<class T1, typename T2, int N> struct foo7 :
+    UnknownBase<T1,T2,(N>1)> // expected-error {{unknown template name 'UnknownBase'}}
+  { };
+
+  template<class T1, typename T2> struct foo8 :
+    UnknownBase<X<int,int>,X<int,int>> // expected-error {{unknown template name 'UnknownBase'}} \
+                                       // expected-error {{use '> >'}}
+  { };
+
+  template<class T1, typename T2> struct foo9 :
+    UnknownBase<Known<int,int>,X<int,int>> // expected-error {{unknown template name 'UnknownBase'}} \
+                                           // expected-error {{use '> >'}}
+  { };
+
+  template<class T1, typename T2> struct foo10 :
+    UnknownBase<Known<int,int>,X<int,X<int,int>>> // expected-error {{unknown template name 'UnknownBase'}} \
+                                                  // expected-error {{use '> >'}}
+  { };
+
+  template<int N1, int N2> struct foo11 :
+    UnknownBase<2<N1,N2<4> // expected-error {{unknown template name 'UnknownBase'}}
+  { };
+
+}
Index: test/SemaCXX/class.cpp
===================================================================
--- test/SemaCXX/class.cpp
+++ test/SemaCXX/class.cpp
@@ -126,12 +126,8 @@
 
 // Don't crash on this bogus code.
 namespace pr6629 {
-  // TODO: most of these errors are spurious
   template<class T1, class T2> struct foo :
-    bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}} \
-                       // BOGUS expected-error {{expected '{' after base class list}} \
-                       // BOGUS expected-error {{expected ';' after struct}} \
-                       // BOGUS expected-error {{expected unqualified-id}}
+    bogus<foo<T1,T2> > // expected-error {{unknown template name 'bogus'}}
   { };
 
   template<> struct foo<unknown,unknown> { // expected-error {{undeclared identifier 'unknown'}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D924.4.patch
Type: text/x-patch
Size: 5024 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130808/d010a60d/attachment.bin>


More information about the cfe-commits mailing list