From 4fe0c8e9c76b96e7aff21696a40dacc09d0237bc Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sat, 30 May 2009 00:08:05 +0000 Subject: [PATCH] Refactor and clean up the AST printer, so that it uses a DeclVisitor, walks through DeclContexts properly, and prints more of the information available in the AST. The functionality is still available via -ast-print, -ast-dump, etc., and also via the new member functions Decl::dump() and Decl::print(). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72597 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclBase.h | 6 + include/clang/AST/PrettyPrinter.h | 8 +- include/clang/Frontend/ASTConsumers.h | 6 +- lib/AST/CMakeLists.txt | 1 + lib/AST/DeclPrinter.cpp | 530 +++++++++++++++++++++ lib/AST/StmtPrinter.cpp | 17 +- lib/Frontend/ASTConsumers.cpp | 653 +------------------------- lib/Frontend/DocumentXML.cpp | 2 + lib/Sema/SemaDecl.cpp | 6 +- test/Coverage/c-language-features.inc | 1 + tools/clang-cc/clang-cc.cpp | 12 +- utils/pch-test.pl | 2 +- 12 files changed, 587 insertions(+), 657 deletions(-) create mode 100644 lib/AST/DeclPrinter.cpp diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 271e59aadc..c25f467715 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -310,6 +310,12 @@ public: /// Destroy - Call destructors and release memory. virtual void Destroy(ASTContext& C); + void print(llvm::raw_ostream &Out, ASTContext &Context, + unsigned Indentation = 0); + void print(llvm::raw_ostream &Out, ASTContext &Context, + const PrintingPolicy &Policy, unsigned Indentation = 0); + void dump(ASTContext &Context); + private: const Attr *getAttrsImpl() const; diff --git a/include/clang/AST/PrettyPrinter.h b/include/clang/AST/PrettyPrinter.h index 76574bb2b9..dcbb823056 100644 --- a/include/clang/AST/PrettyPrinter.h +++ b/include/clang/AST/PrettyPrinter.h @@ -35,7 +35,7 @@ struct PrintingPolicy { /// \brief Create a default printing policy for C. PrintingPolicy() : Indentation(2), CPlusPlus(false), SuppressTypeSpecifiers(false), - SuppressTagKind(false), OwnedTag(0) { } + SuppressTagKind(false), Dump(false), OwnedTag(0) { } /// \brief The number of spaces to use to indent each line. unsigned Indentation : 8; @@ -64,6 +64,12 @@ struct PrintingPolicy { /// kind of tag, e.g., "struct", "union", "enum". bool SuppressTagKind : 1; + /// \brief True when we are "dumping" rather than "pretty-printing", + /// where dumping involves printing the internal details of the AST + /// and pretty-printing involves printing something similar to + /// source code. + bool Dump : 1; + /// \brief If we are printing a type where the tag type (e.g., a /// class or enum type) was declared or defined within the type /// itself, OwnedTag will point at the declaration node owned by diff --git a/include/clang/Frontend/ASTConsumers.h b/include/clang/Frontend/ASTConsumers.h index c12062d794..04365d7c78 100644 --- a/include/clang/Frontend/ASTConsumers.h +++ b/include/clang/Frontend/ASTConsumers.h @@ -45,10 +45,8 @@ ASTConsumer *CreateASTPrinter(llvm::raw_ostream* OS); ASTConsumer *CreateASTPrinterXML(llvm::raw_ostream* OS); // AST dumper: dumps the raw AST in human-readable form to stderr; this is -// intended for debugging. A normal dump is done with FullDump = false; -// with FullDump = true, the dumper waits until the end of the translation -// unit to dump the AST. -ASTConsumer *CreateASTDumper(bool FullDump); +// intended for debugging. +ASTConsumer *CreateASTDumper(); // Graphical AST viewer: for each function definition, creates a graph of // the AST and displays it with the graph viewer "dotty". Also outputs diff --git a/lib/AST/CMakeLists.txt b/lib/AST/CMakeLists.txt index a282f202f2..19ab9f650e 100644 --- a/lib/AST/CMakeLists.txt +++ b/lib/AST/CMakeLists.txt @@ -12,6 +12,7 @@ add_clang_library(clangAST DeclCXX.cpp DeclGroup.cpp DeclObjC.cpp + DeclPrinter.cpp DeclTemplate.cpp ExprConstant.cpp Expr.cpp diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp new file mode 100644 index 0000000000..a48737560e --- /dev/null +++ b/lib/AST/DeclPrinter.cpp @@ -0,0 +1,530 @@ +//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the Decl::dump method, which pretty print the +// AST back out to C/Objective-C/C++/Objective-C++ code. +// +//===----------------------------------------------------------------------===// +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclVisitor.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/Expr.h" +#include "clang/AST/PrettyPrinter.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/Streams.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" +using namespace clang; + +namespace { + class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor { + llvm::raw_ostream &Out; + ASTContext &Context; + PrintingPolicy Policy; + unsigned Indentation; + + llvm::raw_ostream& Indent(); + + public: + DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context, + const PrintingPolicy &Policy, + unsigned Indentation = 0) + : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { } + + void VisitDeclContext(DeclContext *DC, bool Indent = true); + + void VisitTranslationUnitDecl(TranslationUnitDecl *D); + void VisitTypedefDecl(TypedefDecl *D); + void VisitEnumDecl(EnumDecl *D); + void VisitRecordDecl(RecordDecl *D); + void VisitEnumConstantDecl(EnumConstantDecl *D); + void VisitFunctionDecl(FunctionDecl *D); + void VisitFieldDecl(FieldDecl *D); + void VisitVarDecl(VarDecl *D); + void VisitParmVarDecl(ParmVarDecl *D); + void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); + void VisitNamespaceDecl(NamespaceDecl *D); + void VisitLinkageSpecDecl(LinkageSpecDecl *D); + void VisitTemplateDecl(TemplateDecl *D); + void VisitObjCClassDecl(ObjCClassDecl *D); + void VisitObjCMethodDecl(ObjCMethodDecl *D); + void VisitObjCImplementationDecl(ObjCImplementationDecl *D); + void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); + void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); + void VisitObjCProtocolDecl(ObjCProtocolDecl *D); + void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); + void VisitObjCCategoryDecl(ObjCCategoryDecl *D); + void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); + void VisitObjCPropertyDecl(ObjCPropertyDecl *D); + void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); + }; +} + +void Decl::print(llvm::raw_ostream &Out, ASTContext &Context, + unsigned Indentation) { + print(Out, Context, Context.PrintingPolicy, Indentation); +} + +void Decl::print(llvm::raw_ostream &Out, ASTContext &Context, + const PrintingPolicy &Policy, unsigned Indentation) { + DeclPrinter Printer(Out, Context, Policy, Indentation); + Printer.Visit(this); +} + +void Decl::dump(ASTContext &Context) { + print(llvm::errs(), Context); +} + +llvm::raw_ostream& DeclPrinter::Indent() { + for (unsigned i = 0; i < Indentation; ++i) + Out << " "; + return Out; +} + +//---------------------------------------------------------------------------- +// Common C declarations +//---------------------------------------------------------------------------- + +void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { + if (Indent) + Indentation += Policy.Indentation; + + for (DeclContext::decl_iterator D = DC->decls_begin(Context), + DEnd = DC->decls_end(Context); + D != DEnd; ++D) { + this->Indent(); + Visit(*D); + + // FIXME: Need to be able to tell the DeclPrinter when + const char *Terminator = 0; + if (isa(*D) && + cast(*D)->isThisDeclarationADefinition()) + Terminator = 0; + else if (isa(*D) || isa(*D)) + Terminator = 0; + else if (isa(*D)) { + DeclContext::decl_iterator Next = D; + ++Next; + if (Next != DEnd) + Terminator = ","; + } else + Terminator = ";"; + + if (Terminator) + Out << Terminator; + Out << "\n"; + } + + if (Indent) + Indentation -= Policy.Indentation; +} + +void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { + VisitDeclContext(D, false); +} + +void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { + std::string S = D->getNameAsString(); + D->getUnderlyingType().getAsStringInternal(S, Policy); + Out << "typedef " << S; +} + +void DeclPrinter::VisitEnumDecl(EnumDecl *D) { + Out << "enum " << D->getNameAsString() << " {\n"; + VisitDeclContext(D); + Indent() << "}"; +} + +void DeclPrinter::VisitRecordDecl(RecordDecl *D) { + // print a free standing tag decl (e.g. "struct x;"). + Out << D->getKindName(); + Out << " "; + Out << D->getNameAsString(); + + if (D->isDefinition()) { + Out << " {\n"; + VisitDeclContext(D); + Indent() << "}"; + } +} + +void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { + Out << D->getNameAsString(); + if (Expr *Init = D->getInitExpr()) { + Out << " = "; + Init->printPretty(Out, 0, Policy, Indentation); + } +} + +void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { + switch (D->getStorageClass()) { + case FunctionDecl::None: break; + case FunctionDecl::Extern: Out << "extern "; break; + case FunctionDecl::Static: Out << "static "; break; + case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; + } + + if (D->isInline()) Out << "inline "; + if (D->isVirtualAsWritten()) Out << "virtual "; + + std::string Proto = D->getNameAsString(); + if (isa(D->getType().getTypePtr())) { + const FunctionType *AFT = D->getType()->getAsFunctionType(); + + const FunctionProtoType *FT = 0; + if (D->hasWrittenPrototype()) + FT = dyn_cast(AFT); + + Proto += "("; + if (FT) { + llvm::raw_string_ostream POut(Proto); + DeclPrinter ParamPrinter(POut, Context, Policy, Indentation); + for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { + if (i) POut << ", "; + ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); + } + + if (FT->isVariadic()) { + if (D->getNumParams()) POut << ", "; + POut << "..."; + } + } + + Proto += ")"; + AFT->getResultType().getAsStringInternal(Proto, Policy); + } else { + D->getType().getAsStringInternal(Proto, Policy); + } + + Out << Proto; + + if (D->isPure()) + Out << " = 0"; + else if (D->isDeleted()) + Out << " = delete"; + else if (D->isThisDeclarationADefinition()) { + if (!D->hasPrototype() && D->getNumParams()) { + // This is a K&R function definition, so we need to print the + // parameters. + Out << '\n'; + Indentation += Policy.Indentation; + for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { + Indent(); + VisitParmVarDecl(D->getParamDecl(i)); + Out << ";\n"; + } + Indentation -= Policy.Indentation; + } else + Out << ' '; + + D->getBody(Context)->printPretty(Out, 0, Policy, Indentation); + Out << '\n'; + } +} + +void DeclPrinter::VisitFieldDecl(FieldDecl *D) { + if (D->isMutable()) + Out << "mutable "; + + std::string Name = D->getNameAsString(); + D->getType().getAsStringInternal(Name, Policy); + Out << Name; + + if (D->isBitField()) { + Out << " : "; + D->getBitWidth()->printPretty(Out, 0, Policy, Indentation); + } +} + +void DeclPrinter::VisitVarDecl(VarDecl *D) { + if (D->getStorageClass() != VarDecl::None) + Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " "; + + if (D->isThreadSpecified()) + Out << "__thread "; + + std::string Name = D->getNameAsString(); + QualType T = D->getType(); + if (OriginalParmVarDecl *Parm = dyn_cast(D)) + T = Parm->getOriginalType(); + T.getAsStringInternal(Name, Policy); + Out << Name; + if (D->getInit()) { + if (D->hasCXXDirectInitializer()) + Out << "("; + else + Out << " = "; + D->getInit()->printPretty(Out, 0, Policy, Indentation); + if (D->hasCXXDirectInitializer()) + Out << ")"; + } +} + +void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { + VisitVarDecl(D); +} + +void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { + Out << "__asm ("; + D->getAsmString()->printPretty(Out, 0, Policy, Indentation); + Out << ")"; +} + +//---------------------------------------------------------------------------- +// C++ declarations +//---------------------------------------------------------------------------- +void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { + Out << "namespace " << D->getNameAsString() << " {\n"; + VisitDeclContext(D); + Indent() << "}"; +} + +void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { + const char *l; + if (D->getLanguage() == LinkageSpecDecl::lang_c) + l = "C"; + else { + assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && + "unknown language in linkage specification"); + l = "C++"; + } + + Out << "extern \"" << l << "\" "; + if (D->hasBraces()) { + Out << "{\n"; + VisitDeclContext(D); + Indent() << "}"; + } else + Visit(*D->decls_begin(Context)); +} + +void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) { + // TODO: Write template parameters. + Out << "template <...> "; + Visit(D->getTemplatedDecl()); +} + +//---------------------------------------------------------------------------- +// Objective-C declarations +//---------------------------------------------------------------------------- + +void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) { + Out << "@class "; + for (ObjCClassDecl::iterator I = D->begin(), E = D->end(); + I != E; ++I) { + if (I != D->begin()) Out << ", "; + Out << (*I)->getNameAsString(); + } + Out << ";\n"; +} + +void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { + if (OMD->isInstanceMethod()) + Out << "\n- "; + else + Out << "\n+ "; + if (!OMD->getResultType().isNull()) + Out << '(' << OMD->getResultType().getAsString() << ")"; + + std::string name = OMD->getSelector().getAsString(); + std::string::size_type pos, lastPos = 0; + for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), + E = OMD->param_end(); PI != E; ++PI) { + // FIXME: selector is missing here! + pos = name.find_first_of(":", lastPos); + Out << " " << name.substr(lastPos, pos - lastPos); + Out << ":(" << (*PI)->getType().getAsString() << ")" + << (*PI)->getNameAsString(); + lastPos = pos + 1; + } + + if (OMD->param_begin() == OMD->param_end()) + Out << " " << name; + + if (OMD->isVariadic()) + Out << ", ..."; + + if (OMD->getBody()) { + Out << ' '; + OMD->getBody()->printPretty(Out, 0, Policy); + Out << '\n'; + } else + Out << ";"; +} + +void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { + std::string I = OID->getNameAsString(); + ObjCInterfaceDecl *SID = OID->getSuperClass(); + + if (SID) + Out << "@implementation " << I << " : " << SID->getNameAsString(); + else + Out << "@implementation " << I; + + VisitDeclContext(OID); + Out << "@end\n"; +} + +void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { + std::string I = OID->getNameAsString(); + ObjCInterfaceDecl *SID = OID->getSuperClass(); + + if (SID) + Out << "@interface " << I << " : " << SID->getNameAsString(); + else + Out << "@interface " << I; + + // Protocols? + const ObjCList &Protocols = OID->getReferencedProtocols(); + if (!Protocols.empty()) { + for (ObjCList::iterator I = Protocols.begin(), + E = Protocols.end(); I != E; ++I) + Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString(); + } + + if (!Protocols.empty()) + Out << ">"; + Out << '\n'; + + if (OID->ivar_size() > 0) { + Out << '{'; + for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), + E = OID->ivar_end(); I != E; ++I) { + Out << '\t' << (*I)->getType().getAsString(Policy) + << ' ' << (*I)->getNameAsString() << ";\n"; + } + Out << "}\n"; + } + + VisitDeclContext(OID, false); + Out << "@end\n"; + // FIXME: implement the rest... +} + +void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { + Out << "@protocol "; + for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), + E = D->protocol_end(); + I != E; ++I) { + if (I != D->protocol_begin()) Out << ", "; + Out << (*I)->getNameAsString(); + } + Out << ";\n"; +} + +void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { + Out << "@protocol " << PID->getNameAsString() << '\n'; + + for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(Context), + E = PID->prop_end(Context); I != E; ++I) + VisitObjCPropertyDecl(*I); + Out << "@end\n"; + // FIXME: implement the rest... +} + +void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { + Out << "@implementation " + << PID->getClassInterface()->getNameAsString() + << '(' << PID->getNameAsString() << ");\n"; + + VisitDeclContext(PID, false); + Out << "@end\n"; + // FIXME: implement the rest... +} + +void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { + Out << "@interface " + << PID->getClassInterface()->getNameAsString() + << '(' << PID->getNameAsString() << ");\n"; + VisitDeclContext(PID, false); + Out << "@end\n"; + + // FIXME: implement the rest... +} + +void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { + Out << "@compatibility_alias " << AID->getNameAsString() + << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; +} + +/// PrintObjCPropertyDecl - print a property declaration. +/// +void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { + if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) + Out << "@required\n"; + else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) + Out << "@optional\n"; + + Out << "@property"; + if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { + bool first = true; + Out << " ("; + if (PDecl->getPropertyAttributes() & + ObjCPropertyDecl::OBJC_PR_readonly) { + Out << (first ? ' ' : ',') << "readonly"; + first = false; + } + + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { + Out << (first ? ' ' : ',') << "getter = " + << PDecl->getGetterName().getAsString(); + first = false; + } + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { + Out << (first ? ' ' : ',') << "setter = " + << PDecl->getSetterName().getAsString(); + first = false; + } + + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { + Out << (first ? ' ' : ',') << "assign"; + first = false; + } + + if (PDecl->getPropertyAttributes() & + ObjCPropertyDecl::OBJC_PR_readwrite) { + Out << (first ? ' ' : ',') << "readwrite"; + first = false; + } + + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { + Out << (first ? ' ' : ',') << "retain"; + first = false; + } + + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { + Out << (first ? ' ' : ',') << "copy"; + first = false; + } + + if (PDecl->getPropertyAttributes() & + ObjCPropertyDecl::OBJC_PR_nonatomic) { + Out << (first ? ' ' : ',') << "nonatomic"; + first = false; + } + Out << " )"; + } + Out << ' ' << PDecl->getType().getAsString(Policy) + << ' ' << PDecl->getNameAsString(); + + Out << ";\n"; +} + +void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { + if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) + Out << "\n@synthesize "; + else + Out << "\n@dynamic "; + Out << PID->getPropertyDecl()->getNameAsString(); + if (PID->getPropertyIvarDecl()) + Out << "=" << PID->getPropertyIvarDecl()->getNameAsString(); + Out << ";\n"; +} diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index e6a2bad7a5..fc9fdf975d 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -816,7 +816,7 @@ void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { PrintExpr(Node->getLHS()); OS << " : "; } - else { // Handle GCC extention where LHS can be NULL. + else { // Handle GCC extension where LHS can be NULL. OS << " ?: "; } @@ -903,7 +903,15 @@ void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { } void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { - OS << "/*implicit*/" << Node->getType().getAsString() << "()"; + if (Policy.CPlusPlus) + OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()"; + else { + OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")"; + if (Node->getType()->isRecordType()) + OS << "{}"; + else + OS << 0; + } } void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { @@ -1257,6 +1265,11 @@ void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper, return; } + if (Policy.Dump) { + dump(); + return; + } + StmtPrinter P(OS, Helper, Policy, Indentation); P.Visit(const_cast(this)); } diff --git a/lib/Frontend/ASTConsumers.cpp b/lib/Frontend/ASTConsumers.cpp index c35f4c9e20..11c9251ae9 100644 --- a/lib/Frontend/ASTConsumers.cpp +++ b/lib/Frontend/ASTConsumers.cpp @@ -30,559 +30,21 @@ using namespace clang; //===----------------------------------------------------------------------===// -/// DeclPrinter - Utility class for printing top-level decls. +/// ASTPrinter - Pretty-printer and dumper of ASTs namespace { - class DeclPrinter { - public: - llvm::raw_ostream& Out; - PrintingPolicy Policy; - unsigned Indentation; - - DeclPrinter(llvm::raw_ostream* out, - const PrintingPolicy &Policy = PrintingPolicy()) - : Out(out ? *out : llvm::errs()), Policy(Policy), - Indentation(0) {} - DeclPrinter() : Out(llvm::errs()), Indentation(0) {} - virtual ~DeclPrinter(); - - void ChangeIndent(int I) { - Indentation += I; - } - - llvm::raw_ostream& Indent() { - for (unsigned i = 0; i < Indentation; ++i) - Out << " "; - return Out; - } - - void PrintDecl(Decl *D); - void Print(NamedDecl *ND); - void Print(NamespaceDecl *NS); - void PrintFunctionDeclStart(FunctionDecl *FD); - void PrintTypeDefDecl(TypedefDecl *TD); - void PrintLinkageSpec(LinkageSpecDecl *LS); - void PrintObjCMethodDecl(ObjCMethodDecl *OMD); - void PrintObjCImplementationDecl(ObjCImplementationDecl *OID); - void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID); - void PrintObjCProtocolDecl(ObjCProtocolDecl *PID); - void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID); - void PrintObjCCategoryDecl(ObjCCategoryDecl *PID); - void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID); - void PrintObjCPropertyDecl(ObjCPropertyDecl *PD); - void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID); - - void PrintTemplateDecl(TemplateDecl *TD); - }; -} // end anonymous namespace - -DeclPrinter::~DeclPrinter() { - Out.flush(); -} - -void DeclPrinter:: PrintDecl(Decl *D) { - Indent(); - if (FunctionDecl *FD = dyn_cast(D)) { - PrintFunctionDeclStart(FD); - - // FIXME: Pass a context here so we can use getBody() - if (FD->getBodyIfAvailable()) { - Out << ' '; - FD->getBodyIfAvailable()->printPretty(Out, 0, Policy); - Out << '\n'; - } - } else if (isa(D)) { - // Do nothing, methods definitions are printed in - // PrintObjCImplementationDecl. - } else if (TypedefDecl *TD = dyn_cast(D)) { - PrintTypeDefDecl(TD); - } else if (ObjCInterfaceDecl *OID = dyn_cast(D)) { - PrintObjCInterfaceDecl(OID); - } else if (ObjCProtocolDecl *PID = dyn_cast(D)) { - PrintObjCProtocolDecl(PID); - } else if (ObjCForwardProtocolDecl *OFPD = - dyn_cast(D)) { - Out << "@protocol "; - for (ObjCForwardProtocolDecl::protocol_iterator I = OFPD->protocol_begin(), - E = OFPD->protocol_end(); - I != E; ++I) { - if (I != OFPD->protocol_begin()) Out << ", "; - Out << (*I)->getNameAsString(); - } - Out << ";\n"; - } else if (ObjCImplementationDecl *OID = - dyn_cast(D)) { - PrintObjCImplementationDecl(OID); - } else if (ObjCCategoryImplDecl *OID = - dyn_cast(D)) { - PrintObjCCategoryImplDecl(OID); - } else if (ObjCCategoryDecl *OID = - dyn_cast(D)) { - PrintObjCCategoryDecl(OID); - } else if (ObjCCompatibleAliasDecl *OID = - dyn_cast(D)) { - PrintObjCCompatibleAliasDecl(OID); - } else if (ObjCClassDecl *OFCD = dyn_cast(D)) { - Out << "@class "; - for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end(); - I != E; ++I) { - if (I != OFCD->begin()) Out << ", "; - Out << (*I)->getNameAsString(); - } - Out << ";\n"; - } else if (EnumDecl *ED = dyn_cast(D)) { - Out << "enum " << ED->getNameAsString() << " {\n"; - // FIXME: Shouldn't pass a NULL context - ASTContext *Context = 0; - for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context), - EEnd = ED->enumerator_end(*Context); - E != EEnd; ++E) - Out << " " << (*E)->getNameAsString() << ",\n"; - Out << "};\n"; - } else if (TagDecl *TD = dyn_cast(D)) { - // print a free standing tag decl (e.g. "struct x;"). - Out << TD->getKindName(); - Out << " "; - if (const IdentifierInfo *II = TD->getIdentifier()) - Out << II->getName(); - - if (TD->isDefinition()) { - Out << " {\n"; - ChangeIndent(1); - // FIXME: Shouldn't pass a NULL context - ASTContext *Context = 0; - for (DeclContext::decl_iterator i = TD->decls_begin(*Context); - i != TD->decls_end(*Context); - ++i) - PrintDecl(*i); - ChangeIndent(-1); - Indent(); - Out << "}"; - } - Out << ";\n"; - } else if (TemplateDecl *TempD = dyn_cast(D)) { - PrintTemplateDecl(TempD); - } else if (LinkageSpecDecl *LSD = dyn_cast(D)) { - PrintLinkageSpec(LSD); - } else if (FileScopeAsmDecl *AD = dyn_cast(D)) { - Out << "asm("; - AD->getAsmString()->printPretty(Out, 0, Policy); - Out << ")\n"; - } else if (NamedDecl *ND = dyn_cast(D)) { - Print(ND); - } else { - assert(0 && "Unknown decl type!"); - } -} - -void DeclPrinter::Print(NamedDecl *ND) { - switch (ND->getKind()) { - default: - // FIXME: Handle the rest of the NamedDecls. - Out << "### NamedDecl " << ND->getNameAsString() << "\n"; - break; - case Decl::Field: - case Decl::Var: { - // Emit storage class for vardecls. - if (VarDecl *V = dyn_cast(ND)) { - switch (V->getStorageClass()) { - default: assert(0 && "Unknown storage class!"); - case VarDecl::None: break; - case VarDecl::Auto: Out << "auto "; break; - case VarDecl::Register: Out << "register "; break; - case VarDecl::Extern: Out << "extern "; break; - case VarDecl::Static: Out << "static "; break; - case VarDecl::PrivateExtern: Out << "__private_extern__ "; break; - } - } - std::string Name = ND->getNameAsString(); - // This forms: "int a". - dyn_cast(ND)->getType().getAsStringInternal(Name, Policy); - Out << Name; - if (VarDecl *Var = dyn_cast(ND)) { - if (Var->getInit()) { - Out << " = "; - Var->getInit()->printPretty(Out, 0, Policy); - } - } - Out << ";\n"; - break; - } - case Decl::Namespace: - Print(dyn_cast(ND)); - break; - } -} - -void DeclPrinter::Print(NamespaceDecl *NS) { - Out << "namespace " << NS->getNameAsString() << " {\n"; - ChangeIndent(1); - // FIXME: Shouldn't pass a NULL context - ASTContext *Context = 0; - for (DeclContext::decl_iterator i = NS->decls_begin(*Context); - i != NS->decls_end(*Context); - ++i) - PrintDecl(*i); - ChangeIndent(-1); - Indent(); - Out << "}\n"; -} - -void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { - // FIXME: pass a context so that we can use getBody. - bool HasBody = FD->getBodyIfAvailable(); - - Out << '\n'; - - Indent(); - switch (FD->getStorageClass()) { - default: assert(0 && "Unknown storage class"); - case FunctionDecl::None: break; - case FunctionDecl::Extern: Out << "extern "; break; - case FunctionDecl::Static: Out << "static "; break; - case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; - } - - if (FD->isInline()) - Out << "inline "; - - std::string Proto = FD->getNameAsString(); - const FunctionType *AFT = FD->getType()->getAsFunctionType(); - - if (const FunctionProtoType *FT = dyn_cast(AFT)) { - Proto += "("; - for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { - if (i) Proto += ", "; - std::string ParamStr; - if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString(); - - FT->getArgType(i).getAsStringInternal(ParamStr, Policy); - Proto += ParamStr; - } - - if (FT->isVariadic()) { - if (FD->getNumParams()) Proto += ", "; - Proto += "..."; - } - Proto += ")"; - } else { - assert(isa(AFT)); - Proto += "()"; - } - - AFT->getResultType().getAsStringInternal(Proto, Policy); - Out << Proto; - - if (!FD->getBodyIfAvailable()) - Out << ";\n"; - // Doesn't print the body. -} - -void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { - std::string S = TD->getNameAsString(); - TD->getUnderlyingType().getAsStringInternal(S, Policy); - Out << "typedef " << S << ";\n"; -} - -void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) { - const char *l; - if (LS->getLanguage() == LinkageSpecDecl::lang_c) - l = "C"; - else { - assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx && - "unknown language in linkage specification"); - l = "C++"; - } - - Out << "extern \"" << l << "\" "; - if (LS->hasBraces()) { - Out << "{\n"; - ChangeIndent(1); - } - - // FIXME: Should not use a NULL DeclContext! - ASTContext *Context = 0; - for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context), - DEnd = LS->decls_end(*Context); - D != DEnd; ++D) - PrintDecl(*D); - - if (LS->hasBraces()) { - ChangeIndent(-1); - Indent() << "}"; - } - Out << "\n"; -} - -void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) { - if (OMD->isInstanceMethod()) - Out << "\n- "; - else - Out << "\n+ "; - if (!OMD->getResultType().isNull()) - Out << '(' << OMD->getResultType().getAsString() << ")"; - - std::string name = OMD->getSelector().getAsString(); - std::string::size_type pos, lastPos = 0; - for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), - E = OMD->param_end(); PI != E; ++PI) { - // FIXME: selector is missing here! - pos = name.find_first_of(":", lastPos); - Out << " " << name.substr(lastPos, pos - lastPos); - Out << ":(" << (*PI)->getType().getAsString() << ")" - << (*PI)->getNameAsString(); - lastPos = pos + 1; - } - - if (OMD->param_begin() == OMD->param_end()) - Out << " " << name; - - if (OMD->isVariadic()) - Out << ", ..."; - - Out << ";"; -} - -void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) { - std::string I = OID->getNameAsString(); - ObjCInterfaceDecl *SID = OID->getSuperClass(); - - if (SID) - Out << "@implementation " << I << " : " << SID->getNameAsString(); - else - Out << "@implementation " << I; - - // FIXME: Don't use a NULL context - ASTContext *Context = 0; - for (ObjCImplementationDecl::instmeth_iterator - I = OID->instmeth_begin(*Context), - E = OID->instmeth_end(*Context); - I != E; ++I) { - ObjCMethodDecl *OMD = *I; - PrintObjCMethodDecl(OMD); - if (OMD->getBody()) { - Out << ' '; - OMD->getBody()->printPretty(Out, 0, Policy); - Out << '\n'; - } - } - - for (ObjCImplementationDecl::classmeth_iterator - I = OID->classmeth_begin(*Context), - E = OID->classmeth_end(*Context); - I != E; ++I) { - ObjCMethodDecl *OMD = *I; - PrintObjCMethodDecl(OMD); - if (OMD->getBody()) { - Out << ' '; - OMD->getBody()->printPretty(Out, 0, Policy); - Out << '\n'; - } - } - - for (ObjCImplementationDecl::propimpl_iterator - I = OID->propimpl_begin(*Context), - E = OID->propimpl_end(*Context); I != E; ++I) - PrintObjCPropertyImplDecl(*I); - - Out << "@end\n"; -} - - -void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) { - std::string I = OID->getNameAsString(); - ObjCInterfaceDecl *SID = OID->getSuperClass(); - - if (SID) - Out << "@interface " << I << " : " << SID->getNameAsString(); - else - Out << "@interface " << I; - - // Protocols? - const ObjCList &Protocols = OID->getReferencedProtocols(); - if (!Protocols.empty()) { - for (ObjCList::iterator I = Protocols.begin(), - E = Protocols.end(); I != E; ++I) - Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString(); - } - - if (!Protocols.empty()) - Out << ">"; - Out << '\n'; - - if (OID->ivar_size() > 0) { - Out << '{'; - for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), - E = OID->ivar_end(); I != E; ++I) { - Out << '\t' << (*I)->getType().getAsString() - << ' ' << (*I)->getNameAsString() << ";\n"; - } - Out << "}\n"; - } - - // FIXME: Should not use a NULL DeclContext! - ASTContext *Context = 0; - for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context), - E = OID->prop_end(*Context); I != E; ++I) - PrintObjCPropertyDecl(*I); - bool eol_needed = false; - for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context), - E = OID->classmeth_end(*Context); I != E; ++I) - eol_needed = true, PrintObjCMethodDecl(*I); - - for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context), - E = OID->instmeth_end(*Context); I != E; ++I) - eol_needed = true, PrintObjCMethodDecl(*I); - - Out << (eol_needed ? "\n@end\n" : "@end\n"); - // FIXME: implement the rest... -} - -void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) { - Out << "@protocol " << PID->getNameAsString() << '\n'; - - // FIXME: Should not use a NULL DeclContext! - ASTContext *Context = 0; - for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context), - E = PID->prop_end(*Context); I != E; ++I) - PrintObjCPropertyDecl(*I); - Out << "@end\n"; - // FIXME: implement the rest... -} - -void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { - Out << "@implementation " - << PID->getClassInterface()->getNameAsString() - << '(' << PID->getNameAsString() << ");\n"; - - // FIXME: Don't use a NULL context here - ASTContext *Context = 0; - for (ObjCCategoryImplDecl::propimpl_iterator - I = PID->propimpl_begin(*Context), - E = PID->propimpl_end(*Context); I != E; ++I) - PrintObjCPropertyImplDecl(*I); - Out << "@end\n"; - // FIXME: implement the rest... -} - -void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) { - // FIXME: Should not use a NULL DeclContext! - ASTContext *Context = 0; - Out << "@interface " - << PID->getClassInterface()->getNameAsString() - << '(' << PID->getNameAsString() << ");\n"; - // Output property declarations. - for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context), - E = PID->prop_end(*Context); I != E; ++I) - PrintObjCPropertyDecl(*I); - Out << "@end\n"; - - // FIXME: implement the rest... -} - -void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { - Out << "@compatibility_alias " << AID->getNameAsString() - << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; -} - -/// PrintObjCPropertyDecl - print a property declaration. -/// -void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) { - if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) - Out << "@required\n"; - else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) - Out << "@optional\n"; - - Out << "@property"; - if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { - bool first = true; - Out << " ("; - if (PDecl->getPropertyAttributes() & - ObjCPropertyDecl::OBJC_PR_readonly) { - Out << (first ? ' ' : ',') << "readonly"; - first = false; - } - - if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { - Out << (first ? ' ' : ',') << "getter = " - << PDecl->getGetterName().getAsString(); - first = false; - } - if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { - Out << (first ? ' ' : ',') << "setter = " - << PDecl->getSetterName().getAsString(); - first = false; - } - - if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { - Out << (first ? ' ' : ',') << "assign"; - first = false; - } - - if (PDecl->getPropertyAttributes() & - ObjCPropertyDecl::OBJC_PR_readwrite) { - Out << (first ? ' ' : ',') << "readwrite"; - first = false; - } - - if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { - Out << (first ? ' ' : ',') << "retain"; - first = false; - } - - if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { - Out << (first ? ' ' : ',') << "copy"; - first = false; - } - - if (PDecl->getPropertyAttributes() & - ObjCPropertyDecl::OBJC_PR_nonatomic) { - Out << (first ? ' ' : ',') << "nonatomic"; - first = false; - } - Out << " )"; - } - Out << ' ' << PDecl->getType().getAsString() - << ' ' << PDecl->getNameAsString(); + class ASTPrinter : public ASTConsumer { + llvm::raw_ostream &Out; + bool Dump; - Out << ";\n"; -} - -/// PrintObjCPropertyImplDecl - Print an objective-c property implementation -/// declaration syntax. -/// -void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { - if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) - Out << "\n@synthesize "; - else - Out << "\n@dynamic "; - Out << PID->getPropertyDecl()->getNameAsString(); - if (PID->getPropertyIvarDecl()) - Out << "=" << PID->getPropertyIvarDecl()->getNameAsString(); - Out << ";\n"; -} - -/// PrintTemplateParams - Print a template parameter list and recursively print -/// it's underlying top-level definition. -void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) { - // TODO: Write template parameters. - Out << "template <...> "; - PrintDecl(TD->getTemplatedDecl()); -} - - - -//===----------------------------------------------------------------------===// -/// ASTPrinter - Pretty-printer of ASTs - -namespace { - class ASTPrinter : public ASTConsumer, public DeclPrinter { public: - ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {} + ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false) + : Out(o? *o : llvm::errs()), Dump(Dump) { } - virtual void HandleTopLevelDecl(DeclGroupRef D) { - for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) - PrintDecl(*I); + virtual void HandleTranslationUnit(ASTContext &Context) { + PrintingPolicy Policy = Context.PrintingPolicy; + Policy.Dump = Dump; + Context.getTranslationUnitDecl()->print(Out, Context, Policy); } }; } // end anonymous namespace @@ -626,91 +88,8 @@ ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) { return new ASTPrinterXML(out ? *out : llvm::outs()); } -//===----------------------------------------------------------------------===// -/// ASTDumper - Low-level dumper of ASTs - -namespace { - class ASTDumper : public ASTConsumer, public DeclPrinter { - ASTContext *Ctx; - bool FullDump; - - public: - explicit ASTDumper(bool FullDump) : DeclPrinter(), FullDump(FullDump) {} - - void Initialize(ASTContext &Context) { - Ctx = &Context; - } - - virtual void HandleTopLevelDecl(DeclGroupRef D) { - if (FullDump) - return; - for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) - HandleTopLevelSingleDecl(*I); - } - void HandleTopLevelSingleDecl(Decl *D); - - virtual void HandleTranslationUnit(ASTContext &Ctx) { - if (!FullDump) - return; - - for (DeclContext::decl_iterator - D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx), - DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx); - D != DEnd; - ++D) - HandleTopLevelSingleDecl(*D); - } - }; -} // end anonymous namespace - -void ASTDumper::HandleTopLevelSingleDecl(Decl *D) { - if (FunctionDecl *FD = dyn_cast(D)) { - PrintFunctionDeclStart(FD); - - if (Stmt *Body = FD->getBody(*Ctx)) { - Out << '\n'; - // FIXME: convert dumper to use raw_ostream. - Body->dumpAll(Ctx->getSourceManager()); - Out << '\n'; - } - } else if (TypedefDecl *TD = dyn_cast(D)) { - PrintTypeDefDecl(TD); - } else if (ObjCInterfaceDecl *OID = dyn_cast(D)) { - Out << "Read objc interface '" << OID->getNameAsString() << "'\n"; - } else if (ObjCProtocolDecl *OPD = dyn_cast(D)) { - Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n"; - } else if (ObjCCategoryDecl *OCD = dyn_cast(D)) { - Out << "Read objc category '" << OCD->getNameAsString() << "'\n"; - } else if (isa(D)) { - Out << "Read objc fwd protocol decl\n"; - } else if (isa(D)) { - Out << "Read objc fwd class decl\n"; - } else if (isa(D)) { - Out << "Read file scope asm decl\n"; - } else if (ObjCMethodDecl* MD = dyn_cast(D)) { - Out << "Read objc method decl: '" << MD->getSelector().getAsString() - << "'\n"; - if (Stmt *S = MD->getBody()) { - // FIXME: convert dumper to use raw_ostream. - S->dumpAll(Ctx->getSourceManager()); - Out << '\n'; - } - } else if (isa(D)) { - Out << "Read objc implementation decl\n"; - } else if (isa(D)) { - Out << "Read objc category implementation decl\n"; - } else if (isa(D)) { - Out << "Read linkage spec decl\n"; - } else if (NamedDecl *ND = dyn_cast(D)) { - Out << "Read top-level variable decl: '" << ND->getNameAsString() - << "'\n"; - } else { - assert(0 && "Unknown decl type!"); - } -} - -ASTConsumer *clang::CreateASTDumper(bool FullDump) { - return new ASTDumper(FullDump); +ASTConsumer *clang::CreateASTDumper() { + return new ASTPrinter(0, true); } //===----------------------------------------------------------------------===// @@ -718,10 +97,10 @@ ASTConsumer *clang::CreateASTDumper(bool FullDump) { namespace { class ASTViewer : public ASTConsumer { - SourceManager *SM; + ASTContext *Context; public: void Initialize(ASTContext &Context) { - SM = &Context.getSourceManager(); + this->Context = &Context; } virtual void HandleTopLevelDecl(DeclGroupRef D) { @@ -735,7 +114,7 @@ namespace { void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { if (FunctionDecl *FD = dyn_cast(D)) { - DeclPrinter().PrintFunctionDeclStart(FD); + FD->print(llvm::errs(), *Context); if (FD->getBodyIfAvailable()) { llvm::cerr << '\n'; @@ -746,7 +125,7 @@ void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { } if (ObjCMethodDecl *MD = dyn_cast(D)) { - DeclPrinter().PrintObjCMethodDecl(MD); + MD->print(llvm::errs(), *Context); if (MD->getBody()) { llvm::cerr << '\n'; diff --git a/lib/Frontend/DocumentXML.cpp b/lib/Frontend/DocumentXML.cpp index ac1d7d2a8b..7562d2ae87 100644 --- a/lib/Frontend/DocumentXML.cpp +++ b/lib/Frontend/DocumentXML.cpp @@ -566,6 +566,8 @@ void DocumentXML::PrintDecl(Decl *D) case LinkageSpecDecl::lang_cxx: addAttribute("lang", "CXX"); break; default: assert(0 && "Unexpected lang id"); } + } else if (isa(D)) { + // FIXME: Implement this } else { assert(0 && "Unexpected decl"); } diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 8375a68507..562b2dab05 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -4416,6 +4416,8 @@ Sema::DeclPtrTy Sema::ActOnFileScopeAsmDecl(SourceLocation Loc, ExprArg expr) { StringLiteral *AsmString = cast(expr.takeAs()); - return DeclPtrTy::make(FileScopeAsmDecl::Create(Context, CurContext, - Loc, AsmString)); + FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext, + Loc, AsmString); + CurContext->addDecl(Context, New); + return DeclPtrTy::make(New); } diff --git a/test/Coverage/c-language-features.inc b/test/Coverage/c-language-features.inc index 67d5f3b6da..27fae62d3e 100644 --- a/test/Coverage/c-language-features.inc +++ b/test/Coverage/c-language-features.inc @@ -87,6 +87,7 @@ void f4(int a0, int a1, int a2, va_list ap) { int t0 = a0 ? a1 : a2; float t1 = (float) a0; ipair t2 = {1, 2}; + ipair t2a = { .second = 2 }; int t3 = sizeof(ipair); ipair t4; t4 = (ipair) {1, 2}; diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 1503937afa..a0ccafa9af 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -196,8 +196,6 @@ enum ProgActions { ASTPrint, // Parse ASTs and print them. ASTPrintXML, // Parse ASTs and print them in XML. ASTDump, // Parse ASTs and dump them. - ASTDumpFull, // Parse ASTs and dump them, including the - // contents of a PCH file. ASTView, // Parse ASTs and view them in Graphviz. PrintDeclContext, // Print DeclContext and their Decls. ParsePrintCallbacks, // Parse and print each callback. @@ -241,8 +239,6 @@ ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, "Build ASTs and then print them in XML format"), clEnumValN(ASTDump, "ast-dump", "Build ASTs and then debug dump them"), - clEnumValN(ASTDumpFull, "ast-dump-full", - "Build ASTs and then debug dump them, including PCH"), clEnumValN(ASTView, "ast-view", "Build ASTs and view them with GraphViz"), clEnumValN(PrintDeclContext, "print-decl-contexts", @@ -1770,15 +1766,11 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, break; case ASTDump: - Consumer.reset(CreateASTDumper(false)); + Consumer.reset(CreateASTDumper()); break; - case ASTDumpFull: - Consumer.reset(CreateASTDumper(true)); - break; - case ASTView: - Consumer.reset(CreateASTViewer()); + Consumer.reset(CreateASTViewer()); break; case PrintDeclContext: diff --git a/utils/pch-test.pl b/utils/pch-test.pl index f9f4e9d83c..2e17117a2a 100755 --- a/utils/pch-test.pl +++ b/utils/pch-test.pl @@ -22,7 +22,7 @@ sub testfiles($$) { print("."); $code = system("clang-cc -emit-pch -x $language -o $file.pch $file > /dev/null 2>&1"); if ($code == 0) { - $code = system("clang-cc -include-pch $file.pch -x $language -ast-dump-full /dev/null > /dev/null 2>&1"); + $code = system("clang-cc -include-pch $file.pch -x $language -ast-dump /dev/null > /dev/null 2>&1"); if ($code == 0) { $passed++; } elsif (($code & 0xFF) == SIGINT) { -- 2.40.0