[cfe-commits] r66560 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.def lib/AST/Type.cpp lib/Sema/SemaTemplate.cpp test/SemaTemplate/class-template-id-2.cpp test/SemaTemplate/class-template-spec.cpp test/SemaTemplate/instantiation-backtrace.cpp test/SemaTemplate/instantiation-depth.cpp

Douglas Gregor dgregor at apple.com
Tue Mar 10 11:33:28 PDT 2009


Author: dgregor
Date: Tue Mar 10 13:33:27 2009
New Revision: 66560

URL: http://llvm.org/viewvc/llvm-project?rev=66560&view=rev
Log:
Add pretty-printing for class template specializations, e.g.,

  'struct A<double, int>'

In the "template instantiation depth exceeded" message, print
"-ftemplate-depth-N" rather than "-ftemplate-depth=N".

An unnamed tag type that is declared with a typedef, e.g., 

  typedef struct { int x, y; } Point;

can be used as a template argument. Allow this, and check that we get
sensible pretty-printing for such things.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
    cfe/trunk/lib/AST/Type.cpp
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/test/SemaTemplate/class-template-id-2.cpp
    cfe/trunk/test/SemaTemplate/class-template-spec.cpp
    cfe/trunk/test/SemaTemplate/instantiation-backtrace.cpp
    cfe/trunk/test/SemaTemplate/instantiation-depth.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def?rev=66560&r1=66559&r2=66560&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def Tue Mar 10 13:33:27 2009
@@ -643,7 +643,7 @@
 DIAG(err_template_recursion_depth_exceeded, ERROR,
      "recursive template instantiation exceeded maximum depth of %0")
 DIAG(note_template_recursion_depth, NOTE,
-     "use -ftemplate-depth=N to increase recursive template "
+     "use -ftemplate-depth-N to increase recursive template "
      "instantiation depth")
 DIAG(err_template_implicit_instantiate_undefined, ERROR,
      "implicit instantiation of undefined template %0")

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=66560&r1=66559&r2=66560&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Tue Mar 10 13:33:27 2009
@@ -1287,10 +1287,11 @@
     InnerString = Name->getName() + InnerString;
 }
 
-void 
-ClassTemplateSpecializationType::
-getAsStringInternal(std::string &InnerString) const {
-  std::string SpecString = Template->getNameAsString();
+/// \brief Print a template argument list, including the '<' and '>'
+/// enclosing the template arguments.
+static std::string printTemplateArgumentList(const TemplateArgument *Args,
+                                             unsigned NumArgs) {
+  std::string SpecString;
   SpecString += '<';
   for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
     if (Arg)
@@ -1298,22 +1299,22 @@
     
     // Print the argument into a string.
     std::string ArgString;
-    switch (getArg(Arg).getKind()) {
+    switch (Args[Arg].getKind()) {
     case TemplateArgument::Type:
-      getArg(Arg).getAsType().getAsStringInternal(ArgString);
+      Args[Arg].getAsType().getAsStringInternal(ArgString);
       break;
 
     case TemplateArgument::Declaration:
-      ArgString = cast<NamedDecl>(getArg(Arg).getAsDecl())->getNameAsString();
+      ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
       break;
 
     case TemplateArgument::Integral:
-      ArgString = getArg(Arg).getAsIntegral()->toString(10, true);
+      ArgString = Args[Arg].getAsIntegral()->toString(10, true);
       break;
 
     case TemplateArgument::Expression: {
       llvm::raw_string_ostream s(ArgString);
-      getArg(Arg).getAsExpr()->printPretty(s);
+      Args[Arg].getAsExpr()->printPretty(s);
       break;
     }
     }
@@ -1335,6 +1336,14 @@
 
   SpecString += '>';
 
+  return SpecString;
+}
+
+void 
+ClassTemplateSpecializationType::
+getAsStringInternal(std::string &InnerString) const {
+  std::string SpecString = Template->getNameAsString();
+  SpecString += printTemplateArgumentList(getArgs(), getNumArgs());
   if (InnerString.empty())
     InnerString.swap(SpecString);
   else
@@ -1395,6 +1404,16 @@
   } else
     ID = "<anonymous>";
 
+  // If this is a class template specialization, print the template
+  // arguments.
+  if (ClassTemplateSpecializationDecl *Spec 
+        = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
+    std::string TemplateArgs 
+      = printTemplateArgumentList(Spec->getTemplateArgs(),
+                                  Spec->getNumTemplateArgs());
+    InnerString = TemplateArgs + InnerString;
+  }
+
   if (Kind)
     InnerString = std::string(Kind) + " " + ID + InnerString;
   else

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=66560&r1=66559&r2=66560&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Tue Mar 10 13:33:27 2009
@@ -1006,7 +1006,8 @@
   if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod())
     return Diag(ArgLoc, diag::err_template_arg_local_type)
       << QualType(Tag, 0);
-  else if (Tag && !Tag->getDecl()->getDeclName()) {
+  else if (Tag && !Tag->getDecl()->getDeclName() && 
+           !Tag->getDecl()->getTypedefForAnonDecl()) {
     Diag(ArgLoc, diag::err_template_arg_unnamed_type);
     Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
     return true;

Modified: cfe/trunk/test/SemaTemplate/class-template-id-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/class-template-id-2.cpp?rev=66560&r1=66559&r2=66560&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/class-template-id-2.cpp (original)
+++ cfe/trunk/test/SemaTemplate/class-template-id-2.cpp Tue Mar 10 13:33:27 2009
@@ -4,7 +4,7 @@
 
   template<> class A<int> { };
 
-  template<> class A<float>; // expected-note{{forward declaration of 'class A'}}
+  template<> class A<float>; // expected-note{{forward declaration of 'class A<float>'}}
 
   class B : public A<int> { };
 }

Modified: cfe/trunk/test/SemaTemplate/class-template-spec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/class-template-spec.cpp?rev=66560&r1=66559&r2=66560&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/class-template-spec.cpp (original)
+++ cfe/trunk/test/SemaTemplate/class-template-spec.cpp Tue Mar 10 13:33:27 2009
@@ -19,7 +19,7 @@
                           A<double> *a2)
 {
   (void)a1->x; // expected-error{{incomplete definition of type 'A<double, double>'}}
-  (void)a2->x; // expected-error{{implicit instantiation of undefined template 'struct A'}}
+  (void)a2->x; // expected-error{{implicit instantiation of undefined template 'struct A<double, int>'}}
 }
 
 typedef float FLOAT;

Modified: cfe/trunk/test/SemaTemplate/instantiation-backtrace.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiation-backtrace.cpp?rev=66560&r1=66559&r2=66560&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiation-backtrace.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiation-backtrace.cpp Tue Mar 10 13:33:27 2009
@@ -1,7 +1,8 @@
 // RUN: clang -fsyntax-only -verify %s
-template<typename T> struct A; // expected-note{{template is declared here}}
+template<typename T> struct A; // expected-note 2{{template is declared here}}
 
-template<typename T> struct B : A<T*> { }; // expected-error{{implicit instantiation of undefined template}}
+template<typename T> struct B : A<T*> { }; // expected-error{{implicit instantiation of undefined template}} \
+// expected-error{{implicit instantiation of undefined template 'struct A<X *>'}}
 
 template<typename T> struct C : B<T> { } ; // expected-note{{instantiation of template class}}
 
@@ -14,3 +15,9 @@
 void f() {
  (void)sizeof(F<int>); // expected-note{{instantiation of template class}}
 }
+
+typedef struct { } X;
+
+void g() {
+  (void)sizeof(B<X>); // expected-note{{in instantiation of template class 'struct B<X>' requested here}}
+}

Modified: cfe/trunk/test/SemaTemplate/instantiation-depth.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiation-depth.cpp?rev=66560&r1=66559&r2=66560&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiation-depth.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiation-depth.cpp Tue Mar 10 13:33:27 2009
@@ -1,7 +1,7 @@
 // RUN: clang -fsyntax-only -ftemplate-depth=5 -verify %s
 
 template<typename T> struct X : X<T*> { }; // expected-error{{recursive template instantiation exceeded maximum depth of 5}} \
-// expected-note{{use -ftemplate-depth=N to increase recursive template instantiation depth}} \
+// expected-note{{use -ftemplate-depth-N to increase recursive template instantiation depth}} \
 // expected-note 5 {{instantiation of template class}}
 
 void test() { 





More information about the cfe-commits mailing list