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

This makes PrintingPolicy significantly more lightweight and provides
groundwork for more printing customization hooks.
This commit is contained in:
Richard Smith 2019-10-15 17:51:08 -07:00 committed by Richard Smith
parent a69bbe02a2
commit dbcb690fb7
4 changed files with 29 additions and 11 deletions

View File

@ -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<std::string(StringRef)> remapPath;
/// Callbacks to use to allow the behavior of printing to be customized.
const PrintingCallbacks *Callbacks = nullptr;
};
} // end namespace clang

View File

@ -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();

View File

@ -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;
}

View File

@ -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;