From dbcb690fb78193e99452748c4af72eccb262e4e2 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 15 Oct 2019 17:51:08 -0700 Subject: [PATCH] Replace std::function in PrintingPolicy with a callbacks object. This makes PrintingPolicy significantly more lightweight and provides groundwork for more printing customization hooks. --- clang/include/clang/AST/PrettyPrinter.h | 21 ++++++++++++++------- clang/lib/AST/TypePrinter.cpp | 4 ++-- clang/lib/CodeGen/CGDebugInfo.cpp | 3 +-- clang/lib/CodeGen/CGDebugInfo.h | 12 ++++++++++++ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h index 0cd62ba..80eec6a 100644 --- a/clang/include/clang/AST/PrettyPrinter.h +++ b/clang/include/clang/AST/PrettyPrinter.h @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// // -// This file defines the PrinterHelper interface. +// This file defines helper types for AST pretty-printing. // //===----------------------------------------------------------------------===// @@ -29,6 +29,16 @@ public: virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0; }; +/// Callbacks to use to customize the behavior of the pretty-printer. +class PrintingCallbacks { +protected: + ~PrintingCallbacks() = default; + +public: + /// Remap a path to a form suitable for printing. + virtual std::string remapPath(StringRef Path) const { return Path; } +}; + /// Describes how types, statements, expressions, and declarations should be /// printed. /// @@ -50,7 +60,7 @@ struct PrintingPolicy { MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true), MSVCFormatting(false), ConstantsAsWritten(false), SuppressImplicitBase(false), FullyQualifiedName(false), - RemapFilePaths(false), PrintCanonicalTypes(false) {} + PrintCanonicalTypes(false) {} /// Adjust this printing policy for cases where it's known that we're /// printing C++ code (for instance, if AST dumping reaches a C++-only @@ -224,14 +234,11 @@ struct PrintingPolicy { /// This is the opposite of SuppressScope and thus overrules it. unsigned FullyQualifiedName : 1; - /// Whether to apply -fdebug-prefix-map to any file paths. - unsigned RemapFilePaths : 1; - /// Whether to print types as written or canonically. unsigned PrintCanonicalTypes : 1; - /// When RemapFilePaths is true, this function performs the action. - std::function remapPath; + /// Callbacks to use to allow the behavior of printing to be customized. + const PrintingCallbacks *Callbacks = nullptr; }; } // end namespace clang diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index dacbf9a..1065b99 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1189,8 +1189,8 @@ void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { if (PLoc.isValid()) { OS << " at "; StringRef File = PLoc.getFilename(); - if (Policy.RemapFilePaths) - OS << Policy.remapPath(File); + if (auto *Callbacks = Policy.Callbacks) + OS << Callbacks->remapPath(File); else OS << File; OS << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 292d13e..d2d9e8e 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -235,8 +235,7 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const { PP.MSVCFormatting = true; // Apply -fdebug-prefix-map. - PP.RemapFilePaths = true; - PP.remapPath = [this](StringRef Path) { return remapDIPath(Path); }; + PP.Callbacks = &PrintCB; return PP; } diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h index 7edbea8..5e26af4 100644 --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -89,6 +89,18 @@ class CGDebugInfo { /// represented by instantiated Metadata nodes. llvm::SmallDenseMap SizeExprCache; + /// Callbacks to use when printing names and types. + class PrintingCallbacks final : public clang::PrintingCallbacks { + const CGDebugInfo &Self; + + public: + PrintingCallbacks(const CGDebugInfo &Self) : Self(Self) {} + std::string remapPath(StringRef Path) const override { + return Self.remapDIPath(Path); + } + }; + PrintingCallbacks PrintCB = {*this}; + struct ObjCInterfaceCacheEntry { const ObjCInterfaceType *Type; llvm::DIType *Decl; -- 2.7.4