[clang] dbcb690 - Replace std::function in PrintingPolicy with a callbacks object.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 29 15:29:54 PDT 2019


Author: Richard Smith
Date: 2019-10-29T15:29:44-07:00
New Revision: dbcb690fb78193e99452748c4af72eccb262e4e2

URL: https://github.com/llvm/llvm-project/commit/dbcb690fb78193e99452748c4af72eccb262e4e2
DIFF: https://github.com/llvm/llvm-project/commit/dbcb690fb78193e99452748c4af72eccb262e4e2.diff

LOG: Replace std::function in PrintingPolicy with a callbacks object.

This makes PrintingPolicy significantly more lightweight and provides
groundwork for more printing customization hooks.

Added: 
    

Modified: 
    clang/include/clang/AST/PrettyPrinter.h
    clang/lib/AST/TypePrinter.cpp
    clang/lib/CodeGen/CGDebugInfo.cpp
    clang/lib/CodeGen/CGDebugInfo.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h
index 0cd62ba373dc..80eec6a5a8be 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 @@ class PrinterHelper {
   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<std::string(StringRef)> 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 dacbf9a96d81..1065b99e0ce3 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 292d13e62bd4..d2d9e8e59119 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 7edbea86633a..5e26af4a4f75 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<QualType, llvm::Metadata *> 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;


        


More information about the cfe-commits mailing list