diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-05-29 20:38:28 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-05-29 20:38:28 +0000 |
commit | d249e1d1f1498b81314459ceda19d6ff25c278ad (patch) | |
tree | 64980c7c4974845750bbfefa695204a8feff0861 | |
parent | e540858b289b23653bcb23646f135729203635cb (diff) |
Create a new PrintingPolicy class, which we pass down through the AST
printing logic to help customize the output. For now, we use this
rather than a special flag to suppress the "struct" when printing
"struct X" and to print the Boolean type as "bool" in C++ but "_Bool"
in C.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72590 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/AST/ASTContext.h | 4 | ||||
-rw-r--r-- | include/clang/AST/NestedNameSpecifier.h | 3 | ||||
-rw-r--r-- | include/clang/AST/PrettyPrinter.h | 60 | ||||
-rw-r--r-- | include/clang/AST/Stmt.h | 7 | ||||
-rw-r--r-- | include/clang/AST/TemplateName.h | 4 | ||||
-rw-r--r-- | include/clang/AST/Type.h | 74 | ||||
-rw-r--r-- | lib/AST/ASTContext.cpp | 1 | ||||
-rw-r--r-- | lib/AST/CFG.cpp | 37 | ||||
-rw-r--r-- | lib/AST/Decl.cpp | 6 | ||||
-rw-r--r-- | lib/AST/NestedNameSpecifier.cpp | 18 | ||||
-rw-r--r-- | lib/AST/StmtDumper.cpp | 5 | ||||
-rw-r--r-- | lib/AST/StmtPrinter.cpp | 41 | ||||
-rw-r--r-- | lib/AST/TemplateName.cpp | 13 | ||||
-rw-r--r-- | lib/AST/Type.cpp | 175 | ||||
-rw-r--r-- | lib/CodeGen/CGDebugInfo.cpp | 4 | ||||
-rw-r--r-- | lib/Frontend/ASTConsumers.cpp | 26 | ||||
-rw-r--r-- | lib/Frontend/DocumentXML.cpp | 2 | ||||
-rw-r--r-- | lib/Frontend/RewriteBlocks.cpp | 20 | ||||
-rw-r--r-- | lib/Frontend/RewriteObjC.cpp | 22 | ||||
-rw-r--r-- | lib/Sema/Sema.cpp | 2 | ||||
-rw-r--r-- | lib/Sema/SemaTemplateInstantiate.cpp | 3 | ||||
-rw-r--r-- | test/SemaCXX/bool.cpp | 2 | ||||
-rw-r--r-- | test/SemaCXX/convert-to-bool.cpp | 2 | ||||
-rw-r--r-- | test/SemaCXX/overloaded-operator.cpp | 2 | ||||
-rw-r--r-- | test/SemaTemplate/typename-specifier-2.cpp | 2 |
25 files changed, 326 insertions, 209 deletions
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 0badd81be0..e99e9f2b18 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -20,6 +20,7 @@ #include "clang/AST/Builtins.h" #include "clang/AST/Decl.h" #include "clang/AST/NestedNameSpecifier.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" #include "clang/Basic/SourceLocation.h" @@ -143,6 +144,7 @@ public: SelectorTable &Selectors; DeclarationNameTable DeclarationNames; llvm::OwningPtr<ExternalASTSource> ExternalSource; + clang::PrintingPolicy PrintingPolicy; SourceManager& getSourceManager() { return SourceMgr; } const SourceManager& getSourceManager() const { return SourceMgr; } @@ -207,7 +209,7 @@ public: void PrintStats() const; const std::vector<Type*>& getTypes() const { return Types; } - + //===--------------------------------------------------------------------===// // Type Constructors //===--------------------------------------------------------------------===// diff --git a/include/clang/AST/NestedNameSpecifier.h b/include/clang/AST/NestedNameSpecifier.h index 96eebe8be6..4eea1031f0 100644 --- a/include/clang/AST/NestedNameSpecifier.h +++ b/include/clang/AST/NestedNameSpecifier.h @@ -26,6 +26,7 @@ namespace clang { class ASTContext; class NamespaceDecl; class IdentifierInfo; +class PrintingPolicy; class Type; /// \brief Represents a C++ nested name specifier, such as @@ -163,7 +164,7 @@ public: /// \brief Print this nested name specifier to the given output /// stream. - void print(llvm::raw_ostream &OS) const; + void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddPointer(Prefix.getOpaqueValue()); diff --git a/include/clang/AST/PrettyPrinter.h b/include/clang/AST/PrettyPrinter.h index f43b59f693..76574bb2b9 100644 --- a/include/clang/AST/PrettyPrinter.h +++ b/include/clang/AST/PrettyPrinter.h @@ -14,18 +14,74 @@ #ifndef LLVM_CLANG_AST_PRETTY_PRINTER_H #define LLVM_CLANG_AST_PRETTY_PRINTER_H -#include "llvm/Support/raw_ostream.h" +namespace llvm { + class raw_ostream; +} namespace clang { class Stmt; - +class TagDecl; + class PrinterHelper { public: virtual ~PrinterHelper(); virtual bool handledStmt(Stmt* E, llvm::raw_ostream& OS) = 0; }; +/// \brief Describes how types, statements, expressions, and +/// declarations should be printed. +struct PrintingPolicy { + /// \brief Create a default printing policy for C. + PrintingPolicy() + : Indentation(2), CPlusPlus(false), SuppressTypeSpecifiers(false), + SuppressTagKind(false), OwnedTag(0) { } + + /// \brief The number of spaces to use to indent each line. + unsigned Indentation : 8; + + /// \brief Whether we're printing C++ code (otherwise, we're + /// printing C code). + bool CPlusPlus : 1; + + /// \brief Whether we should suppress printing of the actual type + /// specifiers within the type that we are printing. + /// + /// This flag is only used when we are printing declarators beyond + /// the first declarator within a declaration group. For example, given: + /// + /// \code + /// const int *x, *y; + /// \endcode + /// + /// SuppressTypeSpecifiers will be false when printing the + /// declaration for "x", so that we will print "int *x"; it will be + /// \c true when we print "y", so that we suppress printing the + /// "const int" type specifier and instead only print the "*y". + bool SuppressTypeSpecifiers : 1; + + /// \brief If we are printing a tag type, suppresses printing of the + /// kind of tag, e.g., "struct", "union", "enum". + bool SuppressTagKind : 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 + /// this type. + /// + /// Owned tags occur when a tag type is defined as part of the + /// declaration specifiers of another declarator, e.g., + /// + /// \code + /// typedef struct { int x, y; } Point; + /// \endcode + /// + /// Here, the anonymous struct definition is owned by the type of + /// Point. The actual representation uses a DeclGroup to store both + /// the RecordDecl and the TypedefDecl. + TagDecl *OwnedTag; +}; + } // end namespace clang #endif diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 136a83e1a9..9ac540740e 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -17,6 +17,7 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/raw_ostream.h" #include "clang/Basic/SourceLocation.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/StmtIterator.h" #include "clang/AST/DeclGroup.h" #include "llvm/ADT/SmallVector.h" @@ -35,7 +36,6 @@ namespace clang { class SourceManager; class StringLiteral; class SwitchStmt; - class PrinterHelper; //===----------------------------------------------------------------------===// // ExprIterator - Iterators for iterating over Stmt* arrays that contain @@ -187,8 +187,9 @@ public: /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST /// back to its original source language syntax. void dumpPretty() const; - void printPretty(llvm::raw_ostream &OS, PrinterHelper* = NULL, unsigned = 0, - bool NoIndent=false) const; + void printPretty(llvm::raw_ostream &OS, PrinterHelper* = 0, + const PrintingPolicy &Policy = PrintingPolicy(), + unsigned Indentation = 0) const; /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only /// works on systems with GraphViz (Mac OS X) or dot+gv installed. diff --git a/include/clang/AST/TemplateName.h b/include/clang/AST/TemplateName.h index 76ac8a6f98..511934c1db 100644 --- a/include/clang/AST/TemplateName.h +++ b/include/clang/AST/TemplateName.h @@ -26,6 +26,7 @@ namespace clang { class DependentTemplateName; class IdentifierInfo; class NestedNameSpecifier; +class PrintingPolicy; class QualifiedTemplateName; class TemplateDecl; @@ -103,7 +104,8 @@ public: /// \param SuppressNNS if true, don't print the /// nested-name-specifier that precedes the template name (if it has /// one). - void print(llvm::raw_ostream &OS, bool SuppressNNS = false) const; + void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy, + bool SuppressNNS = false) const; /// \brief Debugging aid that dumps the template name to standard /// error. diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 5361ffb730..beca7088be 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -66,6 +66,7 @@ namespace clang { class StmtIteratorBase; class TemplateArgument; class QualifiedNameType; + class PrintingPolicy; // Provide forward declarations for all of the *Type classes #define TYPE(Class, Base) class Class##Type; @@ -180,12 +181,14 @@ public: bool operator!=(const QualType &RHS) const { return Value != RHS.Value; } - std::string getAsString() const { + std::string getAsString() const; + + std::string getAsString(const PrintingPolicy &Policy) const { std::string S; - getAsStringInternal(S); + getAsStringInternal(S, Policy); return S; } - void getAsStringInternal(std::string &Str) const; + void getAsStringInternal(std::string &Str, const PrintingPolicy &Policy) const; void dump(const char *s) const; void dump() const; @@ -483,7 +486,7 @@ public: QualType getCanonicalTypeInternal() const { return CanonicalType; } void dump() const; - virtual void getAsStringInternal(std::string &InnerString) const = 0; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const = 0; static bool classof(const Type *) { return true; } }; @@ -516,7 +519,7 @@ public: QualType::GCAttrTypes getObjCGCAttr() const { return GCAttrType; } unsigned getAddressSpace() const { return AddressSpace; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, getBaseType(), AddressSpace, GCAttrType); @@ -573,9 +576,9 @@ public: TypeKind(K) {} Kind getKind() const { return TypeKind; } - const char *getName() const; + const char *getName(bool CPlusPlus) const; - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } static bool classof(const BuiltinType *) { return true; } @@ -596,7 +599,7 @@ public: bool isSigned() const { return Signed; } const char *getName() const; - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == FixedWidthInt; } static bool classof(const FixedWidthIntType *) { return true; } @@ -615,7 +618,7 @@ class ComplexType : public Type, public llvm::FoldingSetNode { public: QualType getElementType() const { return ElementType; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, getElementType()); @@ -639,7 +642,7 @@ class PointerType : public Type, public llvm::FoldingSetNode { friend class ASTContext; // ASTContext creates these. public: - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; QualType getPointeeType() const { return PointeeType; } @@ -670,7 +673,7 @@ public: // Get the pointee type. Pointee is required to always be a function type. QualType getPointeeType() const { return PointeeType; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, getPointeeType()); @@ -720,7 +723,7 @@ class LValueReferenceType : public ReferenceType { } friend class ASTContext; // ASTContext creates these public: - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == LValueReference; @@ -736,7 +739,7 @@ class RValueReferenceType : public ReferenceType { } friend class ASTContext; // ASTContext creates these public: - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == RValueReference; @@ -764,7 +767,7 @@ public: const Type *getClass() const { return Class; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, getPointeeType(), getClass()); @@ -844,7 +847,7 @@ class ConstantArrayType : public ArrayType { friend class ASTContext; // ASTContext creates these. public: const llvm::APInt &getSize() const { return Size; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, getElementType(), getSize(), @@ -874,7 +877,7 @@ class IncompleteArrayType : public ArrayType { friend class ASTContext; // ASTContext creates these. public: - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == IncompleteArray; @@ -928,7 +931,7 @@ public: return (Expr*) SizeExpr; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == VariableArray; @@ -971,7 +974,7 @@ public: return (Expr*) SizeExpr; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == DependentSizedArray; @@ -1010,7 +1013,7 @@ public: QualType getElementType() const { return ElementType; } unsigned getNumElements() const { return NumElements; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, getElementType(), getNumElements(), getTypeClass()); @@ -1078,7 +1081,7 @@ public: return unsigned(idx-1) < NumElements; return false; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == ExtVector; @@ -1134,7 +1137,7 @@ class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { public: // No additional state past what FunctionType provides. - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, getResultType()); @@ -1237,7 +1240,7 @@ public: return exception_begin() + NumExceptions; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == FunctionProto; @@ -1273,7 +1276,7 @@ public: /// looking through the typedefs for B will give you "const volatile A". QualType LookThroughTypedefs() const; - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } static bool classof(const TypedefType *) { return true; } @@ -1287,7 +1290,7 @@ class TypeOfExprType : public Type { public: Expr *getUnderlyingExpr() const { return TOExpr; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } static bool classof(const TypeOfExprType *) { return true; } @@ -1304,7 +1307,7 @@ class TypeOfType : public Type { public: QualType getUnderlyingType() const { return TOType; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } static bool classof(const TypeOfType *) { return true; } @@ -1331,9 +1334,7 @@ public: bool isBeingDefined() const { return decl.getInt(); } void setBeingDefined(bool Def) { decl.setInt(Def? 1 : 0); } - virtual void getAsStringInternal(std::string &InnerString) const; - void getAsStringInternal(std::string &InnerString, - bool SuppressTagKind) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; @@ -1414,7 +1415,7 @@ public: unsigned getIndex() const { return Index; } IdentifierInfo *getName() const { return Name; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Depth, Index, Name); @@ -1475,7 +1476,8 @@ public: /// \brief Print a template argument list, including the '<' and '>' /// enclosing the template arguments. static std::string PrintTemplateArgumentList(const TemplateArgument *Args, - unsigned NumArgs); + unsigned NumArgs, + const PrintingPolicy &Policy); typedef const TemplateArgument * iterator; @@ -1497,7 +1499,7 @@ public: /// \precondition @c isArgType(Arg) const TemplateArgument &getArg(unsigned Idx) const; - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Template, getArgs(), NumArgs); @@ -1540,7 +1542,7 @@ public: /// \brief Retrieve the type named by the qualified-id. QualType getNamedType() const { return NamedType; } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, NNS, NamedType); @@ -1616,7 +1618,7 @@ public: return Name.dyn_cast<const TemplateSpecializationType *>(); } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, NNS, Name); @@ -1663,7 +1665,7 @@ public: /// interface type, or 0 if there are none. inline unsigned getNumProtocols() const; - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; static bool classof(const Type *T) { return T->getTypeClass() == ObjCInterface || T->getTypeClass() == ObjCQualifiedInterface; @@ -1697,7 +1699,7 @@ public: qual_iterator qual_begin() const { return Protocols.begin(); } qual_iterator qual_end() const { return Protocols.end(); } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID); static void Profile(llvm::FoldingSetNodeID &ID, @@ -1757,7 +1759,7 @@ public: qual_iterator qual_begin() const { return Protocols.begin(); } qual_iterator qual_end() const { return Protocols.end(); } - virtual void getAsStringInternal(std::string &InnerString) const; + virtual void getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const; void Profile(llvm::FoldingSetNodeID &ID); static void Profile(llvm::FoldingSetNodeID &ID, diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 00eaa361a3..c719b0bc7d 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -44,6 +44,7 @@ ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, BuiltinInfo.InitializeTargetBuiltins(Target); if (InitializeBuiltins) this->InitializeBuiltins(idents); + PrintingPolicy.CPlusPlus = LangOpts.CPlusPlus; } ASTContext::~ASTContext() { diff --git a/lib/AST/CFG.cpp b/lib/AST/CFG.cpp index af2a8910d8..9f2f2079a0 100644 --- a/lib/AST/CFG.cpp +++ b/lib/AST/CFG.cpp @@ -1525,23 +1525,26 @@ class VISIBILITY_HIDDEN CFGBlockTerminatorPrint llvm::raw_ostream& OS; StmtPrinterHelper* Helper; + PrintingPolicy Policy; + public: - CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper) - : OS(os), Helper(helper) {} + CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper, + const PrintingPolicy &Policy = PrintingPolicy()) + : OS(os), Helper(helper), Policy(Policy) {} void VisitIfStmt(IfStmt* I) { OS << "if "; - I->getCond()->printPretty(OS,Helper); + I->getCond()->printPretty(OS,Helper,Policy); } // Default case. - void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); } + void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS, Helper, Policy); } void VisitForStmt(ForStmt* F) { OS << "for (" ; if (F->getInit()) OS << "..."; OS << "; "; - if (Stmt* C = F->getCond()) C->printPretty(OS,Helper); + if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy); OS << "; "; if (F->getInc()) OS << "..."; OS << ")"; @@ -1549,33 +1552,33 @@ public: void VisitWhileStmt(WhileStmt* W) { OS << "while " ; - if (Stmt* C = W->getCond()) C->printPretty(OS,Helper); + if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy); } void VisitDoStmt(DoStmt* D) { OS << "do ... while "; - if (Stmt* C = D->getCond()) C->printPretty(OS,Helper); + if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy); } void VisitSwitchStmt(SwitchStmt* Terminator) { OS << "switch "; - Terminator->getCond()->printPretty(OS,Helper); + Terminator->getCond()->printPretty(OS, Helper, Policy); } void VisitConditionalOperator(ConditionalOperator* C) { - C->getCond()->printPretty(OS,Helper); + C->getCond()->printPretty(OS, Helper, Policy); OS << " ? ... : ..."; } void VisitChooseExpr(ChooseExpr* C) { OS << "__builtin_choose_expr( "; - C->getCond()->printPretty(OS,Helper); + C->getCond()->printPretty(OS, Helper, Policy); OS << " )"; } void VisitIndirectGotoStmt(IndirectGotoStmt* I) { OS << "goto *"; - I->getTarget()->printPretty(OS,Helper); + I->getTarget()->printPretty(OS, Helper, Policy); } void VisitBinaryOperator(BinaryOperator* B) { @@ -1584,7 +1587,7 @@ public: return; } - B->getLHS()->printPretty(OS,Helper); + B->getLHS()->printPretty(OS, Helper, Policy); switch (B->getOpcode()) { case BinaryOperator::LOr: @@ -1599,7 +1602,7 @@ public: } void VisitExpr(Expr* E) { - E->printPretty(OS,Helper); + E->printPretty(OS, Helper, Policy); } }; @@ -1629,7 +1632,7 @@ void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminato } } - Terminator->printPretty(OS, Helper); + Terminator->printPretty(OS, Helper, /*FIXME:*/PrintingPolicy()); // Expressions need a newline. if (isa<Expr>(Terminator)) OS << '\n'; @@ -1662,10 +1665,10 @@ void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B, OS << L->getName(); else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) { OS << "case "; - C->getLHS()->printPretty(OS); + C->getLHS()->printPretty(OS, Helper, /*FIXME:*/PrintingPolicy()); if (C->getRHS()) { OS << " ... "; - C->getRHS()->printPretty(OS); + C->getRHS()->printPretty(OS, Helper, /*FIXME:*/PrintingPolicy()); } } else if (isa<DefaultStmt>(Terminator)) @@ -1703,7 +1706,7 @@ void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B, if (Helper) Helper->setBlockID(-1); - CFGBlockTerminatorPrint TPrinter(OS,Helper); + CFGBlockTerminatorPrint TPrinter(OS, Helper, /*FIXME*/PrintingPolicy()); TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); OS << '\n'; } diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 063914092e..cb3ec1f487 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -18,6 +18,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Stmt.h" #include "clang/AST/Expr.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/Basic/IdentifierTable.h" #include <vector> @@ -224,10 +225,13 @@ std::string NamedDecl::getQualifiedNameAsString() const { if (const ClassTemplateSpecializationDecl *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); + PrintingPolicy Policy; + Policy.CPlusPlus = true; std::string TemplateArgsStr = TemplateSpecializationType::PrintTemplateArgumentList( TemplateArgs.getFlatArgumentList(), - TemplateArgs.flat_size()); + TemplateArgs.flat_size(), + Policy); Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr); } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx)) Names.push_back(ND->getNameAsString()); diff --git a/lib/AST/NestedNameSpecifier.cpp b/lib/AST/NestedNameSpecifier.cpp index c94a4da7b6..09522a2086 100644 --- a/lib/AST/NestedNameSpecifier.cpp +++ b/lib/AST/NestedNameSpecifier.cpp @@ -14,6 +14,7 @@ #include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/Type.h" #include "llvm/Support/raw_ostream.h" #include <cassert> @@ -104,9 +105,11 @@ bool NestedNameSpecifier::isDependent() const { /// \brief Print this nested name specifier to the given output /// stream. -void NestedNameSpecifier::print(llvm::raw_ostream &OS) const { +void +NestedNameSpecifier::print(llvm::raw_ostream &OS, + const PrintingPolicy &Policy) const { if (getPrefix()) - getPrefix()->print(OS); + getPrefix()->print(OS, Policy); switch (getKind()) { case Identifier: @@ -134,10 +137,9 @@ void NestedNameSpecifier::print(llvm::raw_ostream &OS) const { if (const QualifiedNameType *QualT = dyn_cast<QualifiedNameType>(T)) T = QualT->getNamedType().getTypePtr(); - if (const TagType *TagT = dyn_cast<TagType>(T)) - TagT->getAsStringInternal(TypeStr, true); - else - T->getAsStringInternal(TypeStr); + PrintingPolicy InnerPolicy(Policy); + InnerPolicy.SuppressTagKind = true; + T->getAsStringInternal(TypeStr, InnerPolicy); OS << TypeStr; break; } @@ -152,5 +154,7 @@ void NestedNameSpecifier::Destroy(ASTContext &Context) { } void NestedNameSpecifier::dump() { - print(llvm::errs()); + PrintingPolicy Policy; + Policy.CPlusPlus = true; + print(llvm::errs(), Policy); } diff --git a/lib/AST/StmtDumper.cpp b/lib/AST/StmtDumper.cpp index 821eb55c72..b24e912582 100644 --- a/lib/AST/StmtDumper.cpp +++ b/lib/AST/StmtDumper.cpp @@ -15,6 +15,7 @@ #include "clang/AST/StmtVisitor.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/Basic/SourceManager.h" #include "llvm/Support/Compiler.h" #include <cstdio> @@ -39,6 +40,8 @@ namespace { /// out so that we can print out deltas from then on out. const char *LastLocFilename; unsigned LastLocLine; + + PrintingPolicy Policy; public: StmtDumper(SourceManager *sm, FILE *f, unsigned maxDepth) : SM(sm), F(f), IndentLevel(0-1), MaxDepth(maxDepth) { @@ -223,7 +226,7 @@ void StmtDumper::DumpDeclarator(Decl *D) { } std::string Name = VD->getNameAsString(); - VD->getType().getAsStringInternal(Name); + VD->getType().getAsStringInternal(Name, Policy); fprintf(F, "%s", Name.c_str()); // If this is a vardecl with an initializer, emit it. diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index 80aa2d1d8f..e6a2bad7a5 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -29,14 +29,20 @@ namespace { class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> { llvm::raw_ostream &OS; unsigned IndentLevel; - bool NoIndent; clang::PrinterHelper* Helper; + PrintingPolicy Policy; + public: - StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper, unsigned I=0, - bool noIndent=false) : - OS(os), IndentLevel(I), NoIndent(noIndent), Helper(helper) {} + StmtPrinter(ll |