Skip to content

Commit dbcb690

Browse files
committed
Replace std::function in PrintingPolicy with a callbacks object.
This makes PrintingPolicy significantly more lightweight and provides groundwork for more printing customization hooks.
1 parent a69bbe0 commit dbcb690

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

clang/include/clang/AST/PrettyPrinter.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file defines the PrinterHelper interface.
9+
// This file defines helper types for AST pretty-printing.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -29,6 +29,16 @@ class PrinterHelper {
2929
virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0;
3030
};
3131

32+
/// Callbacks to use to customize the behavior of the pretty-printer.
33+
class PrintingCallbacks {
34+
protected:
35+
~PrintingCallbacks() = default;
36+
37+
public:
38+
/// Remap a path to a form suitable for printing.
39+
virtual std::string remapPath(StringRef Path) const { return Path; }
40+
};
41+
3242
/// Describes how types, statements, expressions, and declarations should be
3343
/// printed.
3444
///
@@ -50,7 +60,7 @@ struct PrintingPolicy {
5060
MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
5161
MSVCFormatting(false), ConstantsAsWritten(false),
5262
SuppressImplicitBase(false), FullyQualifiedName(false),
53-
RemapFilePaths(false), PrintCanonicalTypes(false) {}
63+
PrintCanonicalTypes(false) {}
5464

5565
/// Adjust this printing policy for cases where it's known that we're
5666
/// printing C++ code (for instance, if AST dumping reaches a C++-only
@@ -224,14 +234,11 @@ struct PrintingPolicy {
224234
/// This is the opposite of SuppressScope and thus overrules it.
225235
unsigned FullyQualifiedName : 1;
226236

227-
/// Whether to apply -fdebug-prefix-map to any file paths.
228-
unsigned RemapFilePaths : 1;
229-
230237
/// Whether to print types as written or canonically.
231238
unsigned PrintCanonicalTypes : 1;
232239

233-
/// When RemapFilePaths is true, this function performs the action.
234-
std::function<std::string(StringRef)> remapPath;
240+
/// Callbacks to use to allow the behavior of printing to be customized.
241+
const PrintingCallbacks *Callbacks = nullptr;
235242
};
236243

237244
} // end namespace clang

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,8 @@ void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) {
11891189
if (PLoc.isValid()) {
11901190
OS << " at ";
11911191
StringRef File = PLoc.getFilename();
1192-
if (Policy.RemapFilePaths)
1193-
OS << Policy.remapPath(File);
1192+
if (auto *Callbacks = Policy.Callbacks)
1193+
OS << Callbacks->remapPath(File);
11941194
else
11951195
OS << File;
11961196
OS << ':' << PLoc.getLine() << ':' << PLoc.getColumn();

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
235235
PP.MSVCFormatting = true;
236236

237237
// Apply -fdebug-prefix-map.
238-
PP.RemapFilePaths = true;
239-
PP.remapPath = [this](StringRef Path) { return remapDIPath(Path); };
238+
PP.Callbacks = &PrintCB;
240239
return PP;
241240
}
242241

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ class CGDebugInfo {
8989
/// represented by instantiated Metadata nodes.
9090
llvm::SmallDenseMap<QualType, llvm::Metadata *> SizeExprCache;
9191

92+
/// Callbacks to use when printing names and types.
93+
class PrintingCallbacks final : public clang::PrintingCallbacks {
94+
const CGDebugInfo &Self;
95+
96+
public:
97+
PrintingCallbacks(const CGDebugInfo &Self) : Self(Self) {}
98+
std::string remapPath(StringRef Path) const override {
99+
return Self.remapDIPath(Path);
100+
}
101+
};
102+
PrintingCallbacks PrintCB = {*this};
103+
92104
struct ObjCInterfaceCacheEntry {
93105
const ObjCInterfaceType *Type;
94106
llvm::DIType *Decl;

0 commit comments

Comments
 (0)