[cfe-commits] r136306 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/DeclPrinter.cpp lib/Frontend/ASTConsumers.cpp test/Misc/ast-dump-templates.cpp

Richard Trieu rtrieu at google.com
Wed Jul 27 17:19:05 PDT 2011


Author: rtrieu
Date: Wed Jul 27 19:19:05 2011
New Revision: 136306

URL: http://llvm.org/viewvc/llvm-project?rev=136306&view=rev
Log:
Add template instantiations to the output of -ast-dump.

Added:
    cfe/trunk/test/Misc/ast-dump-templates.cpp
Modified:
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/lib/AST/DeclPrinter.cpp
    cfe/trunk/lib/Frontend/ASTConsumers.cpp

Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=136306&r1=136305&r2=136306&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Wed Jul 27 19:19:05 2011
@@ -734,9 +734,10 @@
   static DeclContext *castToDeclContext(const Decl *);
   static Decl *castFromDeclContext(const DeclContext *);
 
-  void print(raw_ostream &Out, unsigned Indentation = 0) const;
+  void print(raw_ostream &Out, unsigned Indentation = 0,
+             bool PrintInstantiation = false) const;
   void print(raw_ostream &Out, const PrintingPolicy &Policy,
-             unsigned Indentation = 0) const;
+             unsigned Indentation = 0, bool PrintInstantiation = false) const;
   static void printGroup(Decl** Begin, unsigned NumDecls,
                          raw_ostream &Out, const PrintingPolicy &Policy,
                          unsigned Indentation = 0);

Modified: cfe/trunk/lib/AST/DeclPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclPrinter.cpp?rev=136306&r1=136305&r2=136306&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclPrinter.cpp (original)
+++ cfe/trunk/lib/AST/DeclPrinter.cpp Wed Jul 27 19:19:05 2011
@@ -28,6 +28,7 @@
     ASTContext &Context;
     PrintingPolicy Policy;
     unsigned Indentation;
+    bool PrintInstantiation;
 
     raw_ostream& Indent() { return Indent(Indentation); }
     raw_ostream& Indent(unsigned Indentation);
@@ -38,8 +39,10 @@
   public:
     DeclPrinter(raw_ostream &Out, ASTContext &Context,
                 const PrintingPolicy &Policy,
-                unsigned Indentation = 0)
-      : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
+                unsigned Indentation = 0,
+                bool PrintInstantiation = false)
+      : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation),
+        PrintInstantiation(PrintInstantiation) { }
 
     void VisitDeclContext(DeclContext *DC, bool Indent = true);
 
@@ -62,6 +65,8 @@
     void VisitCXXRecordDecl(CXXRecordDecl *D);
     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
     void VisitTemplateDecl(const TemplateDecl *D);
+    void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
+    void VisitClassTemplateDecl(ClassTemplateDecl *D);
     void VisitObjCMethodDecl(ObjCMethodDecl *D);
     void VisitObjCClassDecl(ObjCClassDecl *D);
     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
@@ -77,16 +82,20 @@
     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
     void VisitUsingDecl(UsingDecl *D);
     void VisitUsingShadowDecl(UsingShadowDecl *D);
+
+    void PrintTemplateParameters(const TemplateParameterList *Params,
+                                 const TemplateArgumentList *Args);
   };
 }
 
-void Decl::print(raw_ostream &Out, unsigned Indentation) const {
-  print(Out, getASTContext().PrintingPolicy, Indentation);
+void Decl::print(raw_ostream &Out, unsigned Indentation,
+                 bool PrintInstantiation) const {
+  print(Out, getASTContext().PrintingPolicy, Indentation, PrintInstantiation);
 }
 
 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
-                 unsigned Indentation) const {
-  DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
+                 unsigned Indentation, bool PrintInstantiation) const {
+  DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation);
   Printer.Visit(const_cast<Decl*>(this));
 }
 
@@ -694,10 +703,13 @@
     Visit(*D->decls_begin());
 }
 
-void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
+void DeclPrinter::PrintTemplateParameters(
+    const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
+  assert(Params);
+  assert(!Args || Params->size() == Args->size());
+
   Out << "template <";
 
-  TemplateParameterList *Params = D->getTemplateParameters();
   for (unsigned i = 0, e = Params->size(); i != e; ++i) {
     if (i != 0)
       Out << ", ";
@@ -716,7 +728,10 @@
 
       Out << TTP->getNameAsString();
 
-      if (TTP->hasDefaultArgument()) {
+      if (Args) {
+        Out << " = ";
+        Args->get(i).print(Policy, Out);
+      } else if (TTP->hasDefaultArgument()) {
         Out << " = ";
         Out << TTP->getDefaultArgument().getAsString(Policy);
       };
@@ -732,7 +747,10 @@
         Out << Name->getName();
       }
 
-      if (NTTP->hasDefaultArgument()) {
+      if (Args) {
+        Out << " = ";
+        Args->get(i).print(Policy, Out);
+      } else if (NTTP->hasDefaultArgument()) {
         Out << " = ";
         NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
                                                 Indentation);
@@ -745,6 +763,10 @@
   }
 
   Out << "> ";
+}
+
+void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
+  PrintTemplateParameters(D->getTemplateParameters());
 
   if (const TemplateTemplateParmDecl *TTP =
         dyn_cast<TemplateTemplateParmDecl>(D)) {
@@ -757,6 +779,33 @@
   }
 }
 
+void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
+  if (PrintInstantiation) {
+    TemplateParameterList *Params = D->getTemplateParameters();
+    for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
+         I != E; ++I) {
+      PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
+      Visit(*I);
+    }
+  }
+
+  return VisitRedeclarableTemplateDecl(D);
+}
+
+void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
+  if (PrintInstantiation) {
+    TemplateParameterList *Params = D->getTemplateParameters();
+    for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
+         I != E; ++I) {
+      PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
+      Visit(*I);
+      Out << '\n';
+    }
+  }
+
+  return VisitRedeclarableTemplateDecl(D);
+}
+
 //----------------------------------------------------------------------------
 // Objective-C declarations
 //----------------------------------------------------------------------------

Modified: cfe/trunk/lib/Frontend/ASTConsumers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTConsumers.cpp?rev=136306&r1=136305&r2=136306&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTConsumers.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTConsumers.cpp Wed Jul 27 19:19:05 2011
@@ -41,7 +41,8 @@
     virtual void HandleTranslationUnit(ASTContext &Context) {
       PrintingPolicy Policy = Context.PrintingPolicy;
       Policy.Dump = Dump;
-      Context.getTranslationUnitDecl()->print(Out, Policy);
+      Context.getTranslationUnitDecl()->print(Out, Policy, /*Indentation=*/0,
+                                              /*PrintInstantiation=*/true);
     }
   };
 } // end anonymous namespace

Added: cfe/trunk/test/Misc/ast-dump-templates.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/ast-dump-templates.cpp?rev=136306&view=auto
==============================================================================
--- cfe/trunk/test/Misc/ast-dump-templates.cpp (added)
+++ cfe/trunk/test/Misc/ast-dump-templates.cpp Wed Jul 27 19:19:05 2011
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -ast-dump %s | FileCheck %s
+
+template <int X, typename Y, int Z = 5>
+struct foo {
+  int constant;
+  foo() {}
+  Y getSum() { return Y(X + Z); }
+};
+
+template <int A, typename B>
+B bar() {
+  return B(A);
+}
+
+void baz() {
+  int x = bar<5, int>();
+  int y = foo<5, int>().getSum();
+  double z = foo<2, double, 3>().getSum();
+}
+
+// Template instantiation - foo
+// CHECK: template <int X = 5, typename Y = int, int Z = 5> struct foo {
+// CHECK: template <int X = 2, typename Y = double, int Z = 3> struct foo {
+
+// Template definition - foo
+// CHECK: template <int X, typename Y, int Z = (IntegerLiteral {{.*}} 'int' 5)
+
+// Template instantiation - bar
+// CHECK: template <int A = 5, typename B = int> int bar()
+
+// Template definition - bar
+// CHECK: template <int A, typename B> B bar()





More information about the cfe-commits mailing list