From 5cb3d699f0e37de1767eb23d26d03953a46cf1ff Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Thu, 28 Jul 2011 00:19:05 +0000 Subject: [PATCH] Add template instantiations to the output of -ast-dump. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@136306 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclBase.h | 5 ++- lib/AST/DeclPrinter.cpp | 69 +++++++++++++++++++++++++++----- lib/Frontend/ASTConsumers.cpp | 3 +- test/Misc/ast-dump-templates.cpp | 32 +++++++++++++++ 4 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 test/Misc/ast-dump-templates.cpp diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 8ba94525cc..8355e36c75 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -734,9 +734,10 @@ public: 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); diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index 08112cb7f8..64b31ec185 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/lib/AST/DeclPrinter.cpp @@ -28,6 +28,7 @@ namespace { ASTContext &Context; PrintingPolicy Policy; unsigned Indentation; + bool PrintInstantiation; raw_ostream& Indent() { return Indent(Indentation); } raw_ostream& Indent(unsigned Indentation); @@ -38,8 +39,10 @@ namespace { 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 @@ namespace { 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 @@ namespace { 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(this)); } @@ -694,10 +703,13 @@ void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 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 @@ void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 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 @@ void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 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 @@ void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { } Out << "> "; +} + +void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { + PrintTemplateParameters(D->getTemplateParameters()); if (const TemplateTemplateParmDecl *TTP = dyn_cast(D)) { @@ -757,6 +779,33 @@ void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { } } +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 //---------------------------------------------------------------------------- diff --git a/lib/Frontend/ASTConsumers.cpp b/lib/Frontend/ASTConsumers.cpp index b0746b290c..6412727cd7 100644 --- a/lib/Frontend/ASTConsumers.cpp +++ b/lib/Frontend/ASTConsumers.cpp @@ -41,7 +41,8 @@ namespace { 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 diff --git a/test/Misc/ast-dump-templates.cpp b/test/Misc/ast-dump-templates.cpp new file mode 100644 index 0000000000..6be36ec4d7 --- /dev/null +++ b/test/Misc/ast-dump-templates.cpp @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -ast-dump %s | FileCheck %s + +template +struct foo { + int constant; + foo() {} + Y getSum() { return Y(X + Z); } +}; + +template +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 struct foo { +// CHECK: template struct foo { + +// Template definition - foo +// CHECK: template int bar() + +// Template definition - bar +// CHECK: template B bar() -- 2.40.0