Skip to content

🍒[lldb] Add filter option to AST dump command #10775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: swift/release/6.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/include/clang/Frontend/ASTConsumers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ CreateASTDumper(std::unique_ptr<raw_ostream> OS, StringRef FilterString,
bool DumpDecls, bool Deserialize, bool DumpLookups,
bool DumpDeclTypes, ASTDumpOutputFormat Format);

std::unique_ptr<ASTConsumer>
CreateASTDumper(raw_ostream &OS, StringRef FilterString, bool DumpDecls,
bool Deserialize, bool DumpLookups, bool DumpDeclTypes,
ASTDumpOutputFormat Format);

// AST Decl node lister: prints qualified names of all filterable AST Decl
// nodes.
std::unique_ptr<ASTConsumer> CreateASTDeclNodeLister();
Expand Down
20 changes: 20 additions & 0 deletions clang/lib/Frontend/ASTConsumers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ namespace {
OutputKind(K), OutputFormat(Format), FilterString(FilterString),
DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {}

ASTPrinter(raw_ostream &Out, Kind K, ASTDumpOutputFormat Format,
StringRef FilterString, bool DumpLookups = false,
bool DumpDeclTypes = false)
: Out(Out), OwnedOut(nullptr), OutputKind(K), OutputFormat(Format),
FilterString(FilterString), DumpLookups(DumpLookups),
DumpDeclTypes(DumpDeclTypes) {}

void HandleTranslationUnit(ASTContext &Context) override {
TranslationUnitDecl *D = Context.getTranslationUnitDecl();

Expand Down Expand Up @@ -175,6 +182,19 @@ clang::CreateASTDumper(std::unique_ptr<raw_ostream> Out, StringRef FilterString,
Format, FilterString, DumpLookups, DumpDeclTypes);
}

std::unique_ptr<ASTConsumer>
clang::CreateASTDumper(raw_ostream &Out, StringRef FilterString, bool DumpDecls,
bool Deserialize, bool DumpLookups, bool DumpDeclTypes,
ASTDumpOutputFormat Format) {
assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump");
return std::make_unique<ASTPrinter>(Out,
Deserialize ? ASTPrinter::DumpFull
: DumpDecls ? ASTPrinter::Dump
: ASTPrinter::None,
Format, FilterString, DumpLookups,
DumpDeclTypes);
}

std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
return std::make_unique<ASTDeclNodeLister>(nullptr);
}
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Symbol/SymbolFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class SymbolFile : public PluginInterface {
lldb::SymbolContextItem resolve_scope,
SymbolContextList &sc_list);

virtual void DumpClangAST(Stream &s) {}
virtual void DumpClangAST(Stream &s, llvm::StringRef filter) {}
virtual void FindGlobalVariables(ConstString name,
const CompilerDeclContext &parent_decl_ctx,
uint32_t max_matches,
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Symbol/SymbolFileOnDemand.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class SymbolFileOnDemand : public lldb_private::SymbolFile {
lldb_private::SymbolContextList &sc_list) override;

void Dump(lldb_private::Stream &s) override;
void DumpClangAST(lldb_private::Stream &s) override;
void DumpClangAST(lldb_private::Stream &s, llvm::StringRef filter) override;

void
FindGlobalVariables(lldb_private::ConstString name,
Expand Down
6 changes: 5 additions & 1 deletion lldb/include/lldb/Symbol/TypeSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,11 @@ class TypeSystem : public PluginInterface,
/// given stream.
///
/// This should not modify the state of the TypeSystem if possible.
virtual void Dump(llvm::raw_ostream &output) = 0;
///
/// \param[out] output Stream to dup the AST into.
/// \param[in] filter If empty, dump whole AST. If non-empty, will only
/// dump decls whose names contain \c filter.
virtual void Dump(llvm::raw_ostream &output, llvm::StringRef filter) = 0;

/// This is used by swift.
virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
Expand Down
24 changes: 19 additions & 5 deletions lldb/source/Commands/CommandObjectTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,11 +2237,23 @@ class CommandObjectTargetModulesDumpClangAST
: CommandObjectTargetModulesModuleAutoComplete(
interpreter, "target modules dump ast",
"Dump the clang ast for a given module's symbol file.",
//"target modules dump ast [<file1> ...]")
nullptr, eCommandRequiresTarget) {}
"target modules dump ast [--filter <name>] [<file1> ...]",
eCommandRequiresTarget),
m_filter(LLDB_OPT_SET_1, false, "filter", 'f', 0, eArgTypeName,
"Dump only the decls whose names contain the specified filter "
"string.",
/*default_value=*/"") {
m_option_group.Append(&m_filter, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
m_option_group.Finalize();
}

Options *GetOptions() override { return &m_option_group; }

~CommandObjectTargetModulesDumpClangAST() override = default;

OptionGroupOptions m_option_group;
OptionGroupString m_filter;

protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
Target *target = &GetTarget();
Expand All @@ -2253,6 +2265,8 @@ class CommandObjectTargetModulesDumpClangAST
return;
}

llvm::StringRef filter = m_filter.GetOptionValue().GetCurrentValueAsRef();

if (command.GetArgumentCount() == 0) {
// Dump all ASTs for all modules images
result.GetOutputStream().Format("Dumping clang ast for {0} modules.\n",
Expand All @@ -2261,7 +2275,7 @@ class CommandObjectTargetModulesDumpClangAST
if (INTERRUPT_REQUESTED(GetDebugger(), "Interrupted dumping clang ast"))
break;
if (SymbolFile *sf = module_sp->GetSymbolFile())
sf->DumpClangAST(result.GetOutputStream());
sf->DumpClangAST(result.GetOutputStream(), filter);
}
result.SetStatus(eReturnStatusSuccessFinishResult);
return;
Expand Down Expand Up @@ -2290,7 +2304,7 @@ class CommandObjectTargetModulesDumpClangAST

Module *m = module_list.GetModulePointerAtIndex(i);
if (SymbolFile *sf = m->GetSymbolFile())
sf->DumpClangAST(result.GetOutputStream());
sf->DumpClangAST(result.GetOutputStream(), filter);
}
}
result.SetStatus(eReturnStatusSuccessFinishResult);
Expand Down Expand Up @@ -5290,7 +5304,7 @@ class CommandObjectTargetDumpTypesystem : public CommandObjectParsed {
// Go over every scratch TypeSystem and dump to the command output.
for (lldb::TypeSystemSP ts : GetTarget().GetScratchTypeSystems())
if (ts)
ts->Dump(result.GetOutputStream().AsRawOstream());
ts->Dump(result.GetOutputStream().AsRawOstream(), "");

result.SetStatus(eReturnStatusSuccessFinishResult);
}
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4355,15 +4355,15 @@ void SymbolFileDWARF::Dump(Stream &s) {
m_index->Dump(s);
}

void SymbolFileDWARF::DumpClangAST(Stream &s) {
void SymbolFileDWARF::DumpClangAST(Stream &s, llvm::StringRef filter) {
auto ts_or_err = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
if (!ts_or_err)
return;
auto ts = *ts_or_err;
TypeSystemClang *clang = llvm::dyn_cast_or_null<TypeSystemClang>(ts.get());
if (!clang)
return;
clang->Dump(s.AsRawOstream());
clang->Dump(s.AsRawOstream(), filter);
}

bool SymbolFileDWARF::GetSeparateDebugInfo(StructuredData::Dictionary &d,
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class SymbolFileDWARF : public SymbolFileCommon {

void Dump(Stream &s) override;

void DumpClangAST(Stream &s) override;
void DumpClangAST(Stream &s, llvm::StringRef filter) override;

/// List separate dwo files.
bool GetSeparateDebugInfo(StructuredData::Dictionary &d,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,9 +1280,9 @@ bool SymbolFileDWARFDebugMap::GetCompileOption(const char *option,
return success;
}

void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) {
void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s, llvm::StringRef filter) {
ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) {
oso_dwarf->DumpClangAST(s);
oso_dwarf->DumpClangAST(s, filter);
// The underlying assumption is that DumpClangAST(...) will obtain the
// AST from the underlying TypeSystem and therefore we only need to do
// this once and can stop after the first iteration hence we return true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon {
std::vector<lldb::DataBufferSP>
GetASTData(lldb::LanguageType language) override;

void DumpClangAST(Stream &s) override;
void DumpClangAST(Stream &s, llvm::StringRef filter) override;

/// List separate oso files.
bool GetSeparateDebugInfo(StructuredData::Dictionary &d,
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,6 @@ PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) {
return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
}

void PdbAstBuilder::Dump(Stream &stream) {
m_clang.Dump(stream.AsRawOstream());
void PdbAstBuilder::Dump(Stream &stream, llvm::StringRef filter) {
m_clang.Dump(stream.AsRawOstream(), filter);
}
2 changes: 1 addition & 1 deletion lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class PdbAstBuilder {
TypeSystemClang &clang() { return m_clang; }
ClangASTImporter &GetClangASTImporter() { return m_importer; }

void Dump(Stream &stream);
void Dump(Stream &stream, llvm::StringRef filter);

private:
clang::Decl *TryGetDecl(PdbSymUid uid) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1640,15 +1640,15 @@ size_t SymbolFileNativePDB::ParseSymbolArrayInScope(
return count;
}

void SymbolFileNativePDB::DumpClangAST(Stream &s) {
void SymbolFileNativePDB::DumpClangAST(Stream &s, llvm::StringRef filter) {
auto ts_or_err = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
if (!ts_or_err)
return;
auto ts = *ts_or_err;
TypeSystemClang *clang = llvm::dyn_cast_or_null<TypeSystemClang>(ts.get());
if (!clang)
return;
clang->GetNativePDBParser()->Dump(s);
clang->GetNativePDBParser()->Dump(s, filter);
}

void SymbolFileNativePDB::FindGlobalVariables(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class SymbolFileNativePDB : public SymbolFileCommon {

PdbIndex &GetIndex() { return *m_index; };

void DumpClangAST(Stream &s) override;
void DumpClangAST(Stream &s, llvm::StringRef filter) override;

std::optional<llvm::codeview::TypeIndex>
GetParentType(llvm::codeview::TypeIndex ti);
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
symtab.Finalize();
}

void SymbolFilePDB::DumpClangAST(Stream &s) {
void SymbolFilePDB::DumpClangAST(Stream &s, llvm::StringRef filter) {
auto type_system_or_err =
GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
if (auto err = type_system_or_err.takeError()) {
Expand All @@ -1460,7 +1460,7 @@ void SymbolFilePDB::DumpClangAST(Stream &s) {
llvm::dyn_cast_or_null<TypeSystemClang>(ts.get());
if (!clang_type_system)
return;
clang_type_system->Dump(s.AsRawOstream());
clang_type_system->Dump(s.AsRawOstream(), filter);
}

void SymbolFilePDB::FindTypesByRegex(
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class SymbolFilePDB : public lldb_private::SymbolFileCommon {

const llvm::pdb::IPDBSession &GetPDBSession() const;

void DumpClangAST(lldb_private::Stream &s) override;
void DumpClangAST(lldb_private::Stream &s, llvm::StringRef filter) override;

private:
struct SecContribInfo {
Expand Down
20 changes: 15 additions & 5 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/ExprCXX.h"
#include "clang/Frontend/ASTConsumers.h"
#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/FormatAdapters.h"
Expand Down Expand Up @@ -8819,8 +8820,16 @@ TypeSystemClang::dump(lldb::opaque_compiler_type_t type) const {
}
#endif

void TypeSystemClang::Dump(llvm::raw_ostream &output) {
GetTranslationUnitDecl()->dump(output);
void TypeSystemClang::Dump(llvm::raw_ostream &output, llvm::StringRef filter) {
auto consumer =
clang::CreateASTDumper(output, filter,
/*DumpDecls=*/true,
/*Deserialize=*/false,
/*DumpLookups=*/false,
/*DumpDeclTypes=*/false, clang::ADOF_Default);
assert(consumer);
assert(m_ast_up);
consumer->HandleTranslationUnit(*m_ast_up);
}

void TypeSystemClang::DumpFromSymbolFile(Stream &s,
Expand Down Expand Up @@ -10029,10 +10038,11 @@ GetNameForIsolatedASTKind(ScratchTypeSystemClang::IsolatedASTKind kind) {
llvm_unreachable("Unimplemented IsolatedASTKind?");
}

void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output) {
void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output,
llvm::StringRef filter) {
// First dump the main scratch AST.
output << "State of scratch Clang type system:\n";
TypeSystemClang::Dump(output);
TypeSystemClang::Dump(output, filter);

// Now sort the isolated sub-ASTs.
typedef std::pair<IsolatedASTKey, TypeSystem *> KeyAndTS;
Expand All @@ -10047,7 +10057,7 @@ void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output) {
static_cast<ScratchTypeSystemClang::IsolatedASTKind>(a.first);
output << "State of scratch Clang type subsystem "
<< GetNameForIsolatedASTKind(kind) << ":\n";
a.second->Dump(output);
a.second->Dump(output, filter);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ class TypeSystemClang : public TypeSystem {
#endif

/// \see lldb_private::TypeSystem::Dump
void Dump(llvm::raw_ostream &output) override;
void Dump(llvm::raw_ostream &output, llvm::StringRef filter) override;

/// Dump clang AST types from the symbol file.
///
Expand Down Expand Up @@ -1392,7 +1392,7 @@ class ScratchTypeSystemClang : public TypeSystemClang {
}

/// \see lldb_private::TypeSystem::Dump
void Dump(llvm::raw_ostream &output) override;
void Dump(llvm::raw_ostream &output, llvm::StringRef filter) override;

UserExpression *GetUserExpression(llvm::StringRef expr,
llvm::StringRef prefix,
Expand Down
5 changes: 3 additions & 2 deletions lldb/source/Symbol/SymbolFileOnDemand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,14 @@ void SymbolFileOnDemand::Dump(lldb_private::Stream &s) {
return m_sym_file_impl->Dump(s);
}

void SymbolFileOnDemand::DumpClangAST(lldb_private::Stream &s) {
void SymbolFileOnDemand::DumpClangAST(lldb_private::Stream &s,
llvm::StringRef filter) {
if (!m_debug_info_enabled) {
LLDB_LOG(GetLog(), "[{0}] {1} is skipped", GetSymbolFileName(),
__FUNCTION__);
return;
}
return m_sym_file_impl->DumpClangAST(s);
return m_sym_file_impl->DumpClangAST(s, filter);
}

void SymbolFileOnDemand::FindGlobalVariables(const RegularExpression &regex,
Expand Down
Loading