[llvm-pdbutil] Move global state (Filters) inside LinePrinter class.

The changes described by:

https://reviews.llvm.org/D121801
https://reviews.llvm.org/D122226

Moved some llvm-pdbutil functionality to the debug PDB library.

This patch addresses one outstanding issue concerning the global
state (Filters) created in the PDB library.

- Move 'Filters' inside the 'LinePrinter' class.
- Omit 'Optional' and just pass 'PrintScope &HeaderScope' everywhere.

Reviewed By: aganea

Differential Revision: https://reviews.llvm.org/D122887
This commit is contained in:
Carlos Alberto Enciso 2022-04-08 14:18:13 +01:00
parent 175265ef80
commit 10c11f5c43
5 changed files with 33 additions and 42 deletions

View File

@ -158,32 +158,32 @@ getModuleDebugStream(PDBFile &File, StringRef &ModuleName, uint32_t Index);
Expected<ModuleDebugStreamRef> getModuleDebugStream(PDBFile &File, Expected<ModuleDebugStreamRef> getModuleDebugStream(PDBFile &File,
uint32_t Index); uint32_t Index);
bool shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group); bool shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group,
const FilterOptions &Filters);
// TODO: Change these callbacks to be function_refs (de-templatify them). // TODO: Change these callbacks to be function_refs (de-templatify them).
template <typename CallbackT> template <typename CallbackT>
Error iterateOneModule(InputFile &File, const Optional<PrintScope> &HeaderScope, Error iterateOneModule(InputFile &File, const PrintScope &HeaderScope,
const SymbolGroup &SG, uint32_t Modi, const SymbolGroup &SG, uint32_t Modi,
CallbackT Callback) { CallbackT Callback) {
if (HeaderScope) { HeaderScope.P.formatLine(
HeaderScope->P.formatLine( "Mod {0:4} | `{1}`: ",
"Mod {0:4} | `{1}`: ", fmt_align(Modi, AlignStyle::Right, HeaderScope.LabelWidth), SG.name());
fmt_align(Modi, AlignStyle::Right, HeaderScope->LabelWidth), SG.name());
}
AutoIndent Indent(HeaderScope); AutoIndent Indent(HeaderScope);
return Callback(Modi, SG); return Callback(Modi, SG);
} }
template <typename CallbackT> template <typename CallbackT>
Error iterateSymbolGroups(InputFile &Input, Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope,
const Optional<PrintScope> &HeaderScope,
CallbackT Callback) { CallbackT Callback) {
AutoIndent Indent(HeaderScope); AutoIndent Indent(HeaderScope);
if (llvm::pdb::Filters.DumpModi > 0) { FilterOptions Filters = HeaderScope.P.getFilters();
assert(llvm::pdb::Filters.DumpModi == 1); uint32_t Modi = Filters.DumpModi;
uint32_t Modi = llvm::pdb::Filters.DumpModi;
if (Modi > 0) {
assert(Modi == 1);
SymbolGroup SG(&Input, Modi); SymbolGroup SG(&Input, Modi);
return iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(Modi)), return iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(Modi)),
SG, Modi, Callback); SG, Modi, Callback);
@ -192,7 +192,7 @@ Error iterateSymbolGroups(InputFile &Input,
uint32_t I = 0; uint32_t I = 0;
for (const auto &SG : Input.symbol_groups()) { for (const auto &SG : Input.symbol_groups()) {
if (shouldDumpSymbolGroup(I, SG)) if (shouldDumpSymbolGroup(I, SG, Filters))
if (auto Err = if (auto Err =
iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(I)), iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(I)),
SG, I, Callback)) SG, I, Callback))
@ -205,7 +205,7 @@ Error iterateSymbolGroups(InputFile &Input,
template <typename SubsectionT> template <typename SubsectionT>
Error iterateModuleSubsections( Error iterateModuleSubsections(
InputFile &File, const Optional<PrintScope> &HeaderScope, InputFile &File, const PrintScope &HeaderScope,
llvm::function_ref<Error(uint32_t, const SymbolGroup &, SubsectionT &)> llvm::function_ref<Error(uint32_t, const SymbolGroup &, SubsectionT &)>
Callback) { Callback) {

View File

@ -40,17 +40,16 @@ class MSFStreamLayout;
} // namespace msf } // namespace msf
namespace pdb { namespace pdb {
extern FilterOptions Filters;
class ClassLayout; class ClassLayout;
class PDBFile; class PDBFile;
class SymbolGroup;
class LinePrinter { class LinePrinter {
friend class WithColor; friend class WithColor;
public: public:
LinePrinter(int Indent, bool UseColor, raw_ostream &Stream, LinePrinter(int Indent, bool UseColor, raw_ostream &Stream,
FilterOptions &Filters); const FilterOptions &Filters);
void Indent(uint32_t Amount = 0); void Indent(uint32_t Amount = 0);
void Unindent(uint32_t Amount = 0); void Unindent(uint32_t Amount = 0);
@ -87,6 +86,8 @@ public:
bool IsSymbolExcluded(llvm::StringRef SymbolName); bool IsSymbolExcluded(llvm::StringRef SymbolName);
bool IsCompilandExcluded(llvm::StringRef CompilandName); bool IsCompilandExcluded(llvm::StringRef CompilandName);
const FilterOptions &getFilters() const { return Filters; }
private: private:
template <typename Iter> template <typename Iter>
void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) { void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
@ -99,6 +100,7 @@ private:
int IndentSpaces; int IndentSpaces;
int CurrentIndent; int CurrentIndent;
bool UseColor; bool UseColor;
const FilterOptions &Filters;
std::list<Regex> ExcludeCompilandFilters; std::list<Regex> ExcludeCompilandFilters;
std::list<Regex> ExcludeTypeFilters; std::list<Regex> ExcludeTypeFilters;
@ -120,11 +122,8 @@ struct PrintScope {
uint32_t LabelWidth = 0; uint32_t LabelWidth = 0;
}; };
inline Optional<PrintScope> withLabelWidth(const Optional<PrintScope> &Scope, inline PrintScope withLabelWidth(const PrintScope &Scope, uint32_t W) {
uint32_t W) { return PrintScope{Scope, W};
if (!Scope)
return None;
return PrintScope{*Scope, W};
} }
struct AutoIndent { struct AutoIndent {
@ -132,11 +131,9 @@ struct AutoIndent {
: L(&L), Amount(Amount) { : L(&L), Amount(Amount) {
L.Indent(Amount); L.Indent(Amount);
} }
explicit AutoIndent(const Optional<PrintScope> &Scope) { explicit AutoIndent(const PrintScope &Scope) {
if (Scope.hasValue()) { L = &Scope.P;
L = &Scope->P; Amount = Scope.IndentLevel;
Amount = Scope->IndentLevel;
}
} }
~AutoIndent() { ~AutoIndent() {
if (L) if (L)

View File

@ -573,14 +573,15 @@ static bool isMyCode(const SymbolGroup &Group) {
return true; return true;
} }
bool llvm::pdb::shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group) { bool llvm::pdb::shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group,
if (llvm::pdb::Filters.JustMyCode && !isMyCode(Group)) const FilterOptions &Filters) {
if (Filters.JustMyCode && !isMyCode(Group))
return false; return false;
// If the arg was not specified on the command line, always dump all modules. // If the arg was not specified on the command line, always dump all modules.
if (llvm::pdb::Filters.DumpModi == 0) if (Filters.DumpModi == 0)
return true; return true;
// Otherwise, only dump if this is the same module specified. // Otherwise, only dump if this is the same module specified.
return (llvm::pdb::Filters.DumpModi == Idx); return (Filters.DumpModi == Idx);
} }

View File

@ -30,10 +30,6 @@ using namespace llvm;
using namespace llvm::msf; using namespace llvm::msf;
using namespace llvm::pdb; using namespace llvm::pdb;
// TODO: Move this Filters state inside the LinePrinter class and pass it by
// reference to the iterate* functions.
FilterOptions llvm::pdb::Filters;
namespace { namespace {
bool IsItemExcluded(llvm::StringRef Item, bool IsItemExcluded(llvm::StringRef Item,
std::list<llvm::Regex> &IncludeFilters, std::list<llvm::Regex> &IncludeFilters,
@ -58,9 +54,9 @@ bool IsItemExcluded(llvm::StringRef Item,
using namespace llvm; using namespace llvm;
LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream, LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream,
FilterOptions &Filters) const FilterOptions &Filters)
: OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) { : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor),
llvm::pdb::Filters = Filters; Filters(Filters) {
SetFilters(ExcludeTypeFilters, Filters.ExcludeTypes.begin(), SetFilters(ExcludeTypeFilters, Filters.ExcludeTypes.begin(),
Filters.ExcludeTypes.end()); Filters.ExcludeTypes.end());
SetFilters(ExcludeSymbolFilters, Filters.ExcludeSymbols.begin(), SetFilters(ExcludeSymbolFilters, Filters.ExcludeSymbols.begin(),

View File

@ -552,10 +552,7 @@ Error DumpOutputStyle::dumpSymbolStats() {
StatCollection SymStats; StatCollection SymStats;
StatCollection ChunkStats; StatCollection ChunkStats;
PrintScope Scope(P, 2);
Optional<PrintScope> Scope;
if (File.isPdb())
Scope.emplace(P, 2);
if (Error Err = iterateSymbolGroups( if (Error Err = iterateSymbolGroups(
File, Scope, [&](uint32_t Modi, const SymbolGroup &SG) -> Error { File, Scope, [&](uint32_t Modi, const SymbolGroup &SG) -> Error {