Skip to content

Commit 0f7e10b

Browse files
authored
[lldb] Add filter option to AST dump command (#142164)
Depends on #142163 This patch makes the `-ast-dump-filter` Clang option available to the `target modules dump ast` command. This allows us to selectively dump parts of the AST by name. The AST can quickly grow way too large to skim on the console. This will aid in debugging AST related issues. Example: ``` (lldb) target modules dump ast --filter func Dumping clang ast for 48 modules. Dumping func: FunctionDecl 0xc4b785008 <<invalid sloc>> <invalid sloc> func 'void (int)' extern |-ParmVarDecl 0xc4b7853d8 <<invalid sloc>> <invalid sloc> x 'int' `-AsmLabelAttr 0xc4b785358 <<invalid sloc>> Implicit "_Z4funcIiEvT_" Dumping func<int>: FunctionDecl 0xc4b7850b8 <<invalid sloc>> <invalid sloc> func<int> 'void (int)' implicit_instantiation extern |-TemplateArgument type 'int' | `-BuiltinType 0xc4b85b110 'int' `-ParmVarDecl 0xc4b7853d8 <<invalid sloc>> <invalid sloc> x 'int' ``` The majority of this patch is adjust the `Dump` API. The main change in behaviour is in `TypeSystemClang::Dump`, where we now use the `ASTPrinter` for dumping the `TranslationUnitDecl`. This is where the `-ast-dump-filter` functionality lives in Clang.
1 parent b5cf030 commit 0f7e10b

18 files changed

+132
-33
lines changed

lldb/include/lldb/Symbol/SymbolFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class SymbolFile : public PluginInterface {
296296
lldb::SymbolContextItem resolve_scope,
297297
SymbolContextList &sc_list);
298298

299-
virtual void DumpClangAST(Stream &s) {}
299+
virtual void DumpClangAST(Stream &s, llvm::StringRef filter) {}
300300
virtual void FindGlobalVariables(ConstString name,
301301
const CompilerDeclContext &parent_decl_ctx,
302302
uint32_t max_matches,

lldb/include/lldb/Symbol/SymbolFileOnDemand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class SymbolFileOnDemand : public lldb_private::SymbolFile {
127127
lldb_private::SymbolContextList &sc_list) override;
128128

129129
void Dump(lldb_private::Stream &s) override;
130-
void DumpClangAST(lldb_private::Stream &s) override;
130+
void DumpClangAST(lldb_private::Stream &s, llvm::StringRef filter) override;
131131

132132
void
133133
FindGlobalVariables(lldb_private::ConstString name,

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,11 @@ class TypeSystem : public PluginInterface,
443443
/// given stream.
444444
///
445445
/// This should not modify the state of the TypeSystem if possible.
446-
virtual void Dump(llvm::raw_ostream &output) = 0;
446+
///
447+
/// \param[out] output Stream to dup the AST into.
448+
/// \param[in] filter If empty, dump whole AST. If non-empty, will only
449+
/// dump decls whose names contain \c filter.
450+
virtual void Dump(llvm::raw_ostream &output, llvm::StringRef filter) = 0;
447451

448452
/// This is used by swift.
449453
virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,11 +2235,23 @@ class CommandObjectTargetModulesDumpClangAST
22352235
: CommandObjectTargetModulesModuleAutoComplete(
22362236
interpreter, "target modules dump ast",
22372237
"Dump the clang ast for a given module's symbol file.",
2238-
//"target modules dump ast [<file1> ...]")
2239-
nullptr, eCommandRequiresTarget) {}
2238+
"target modules dump ast [--filter <name>] [<file1> ...]",
2239+
eCommandRequiresTarget),
2240+
m_filter(LLDB_OPT_SET_1, false, "filter", 'f', 0, eArgTypeName,
2241+
"Dump only the decls whose names contain the specified filter "
2242+
"string.",
2243+
/*default_value=*/"") {
2244+
m_option_group.Append(&m_filter, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2245+
m_option_group.Finalize();
2246+
}
2247+
2248+
Options *GetOptions() override { return &m_option_group; }
22402249

22412250
~CommandObjectTargetModulesDumpClangAST() override = default;
22422251

2252+
OptionGroupOptions m_option_group;
2253+
OptionGroupString m_filter;
2254+
22432255
protected:
22442256
void DoExecute(Args &command, CommandReturnObject &result) override {
22452257
Target &target = GetTarget();
@@ -2251,6 +2263,8 @@ class CommandObjectTargetModulesDumpClangAST
22512263
return;
22522264
}
22532265

2266+
llvm::StringRef filter = m_filter.GetOptionValue().GetCurrentValueAsRef();
2267+
22542268
if (command.GetArgumentCount() == 0) {
22552269
// Dump all ASTs for all modules images
22562270
result.GetOutputStream().Format("Dumping clang ast for {0} modules.\n",
@@ -2259,7 +2273,7 @@ class CommandObjectTargetModulesDumpClangAST
22592273
if (INTERRUPT_REQUESTED(GetDebugger(), "Interrupted dumping clang ast"))
22602274
break;
22612275
if (SymbolFile *sf = module_sp->GetSymbolFile())
2262-
sf->DumpClangAST(result.GetOutputStream());
2276+
sf->DumpClangAST(result.GetOutputStream(), filter);
22632277
}
22642278
result.SetStatus(eReturnStatusSuccessFinishResult);
22652279
return;
@@ -2288,7 +2302,7 @@ class CommandObjectTargetModulesDumpClangAST
22882302

22892303
Module *m = module_list.GetModulePointerAtIndex(i);
22902304
if (SymbolFile *sf = m->GetSymbolFile())
2291-
sf->DumpClangAST(result.GetOutputStream());
2305+
sf->DumpClangAST(result.GetOutputStream(), filter);
22922306
}
22932307
}
22942308
result.SetStatus(eReturnStatusSuccessFinishResult);
@@ -5272,7 +5286,7 @@ class CommandObjectTargetDumpTypesystem : public CommandObjectParsed {
52725286
// Go over every scratch TypeSystem and dump to the command output.
52735287
for (lldb::TypeSystemSP ts : GetTarget().GetScratchTypeSystems())
52745288
if (ts)
5275-
ts->Dump(result.GetOutputStream().AsRawOstream());
5289+
ts->Dump(result.GetOutputStream().AsRawOstream(), "");
52765290

52775291
result.SetStatus(eReturnStatusSuccessFinishResult);
52785292
}

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,15 +4127,15 @@ void SymbolFileDWARF::Dump(lldb_private::Stream &s) {
41274127
m_index->Dump(s);
41284128
}
41294129

4130-
void SymbolFileDWARF::DumpClangAST(Stream &s) {
4130+
void SymbolFileDWARF::DumpClangAST(Stream &s, llvm::StringRef filter) {
41314131
auto ts_or_err = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
41324132
if (!ts_or_err)
41334133
return;
41344134
auto ts = *ts_or_err;
41354135
TypeSystemClang *clang = llvm::dyn_cast_or_null<TypeSystemClang>(ts.get());
41364136
if (!clang)
41374137
return;
4138-
clang->Dump(s.AsRawOstream());
4138+
clang->Dump(s.AsRawOstream(), filter);
41394139
}
41404140

41414141
bool SymbolFileDWARF::GetSeparateDebugInfo(StructuredData::Dictionary &d,

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class SymbolFileDWARF : public SymbolFileCommon {
276276

277277
void Dump(Stream &s) override;
278278

279-
void DumpClangAST(Stream &s) override;
279+
void DumpClangAST(Stream &s, llvm::StringRef filter) override;
280280

281281
/// List separate dwo files.
282282
bool GetSeparateDebugInfo(StructuredData::Dictionary &d,

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,9 +1267,9 @@ CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace(
12671267
return matching_namespace;
12681268
}
12691269

1270-
void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) {
1271-
ForEachSymbolFile("Dumping clang AST", [&s](SymbolFileDWARF &oso_dwarf) {
1272-
oso_dwarf.DumpClangAST(s);
1270+
void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s, llvm::StringRef filter) {
1271+
ForEachSymbolFile("Dumping clang AST", [&](SymbolFileDWARF &oso_dwarf) {
1272+
oso_dwarf.DumpClangAST(s, filter);
12731273
// The underlying assumption is that DumpClangAST(...) will obtain the
12741274
// AST from the underlying TypeSystem and therefore we only need to do
12751275
// this once and can stop after the first iteration hence we return true.

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon {
129129
std::vector<std::unique_ptr<CallEdge>>
130130
ParseCallEdgesInFunction(UserID func_id) override;
131131

132-
void DumpClangAST(Stream &s) override;
132+
void DumpClangAST(Stream &s, llvm::StringRef filter) override;
133133

134134
/// List separate oso files.
135135
bool GetSeparateDebugInfo(StructuredData::Dictionary &d,

lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,6 @@ PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) {
14491449
return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
14501450
}
14511451

1452-
void PdbAstBuilder::Dump(Stream &stream) {
1453-
m_clang.Dump(stream.AsRawOstream());
1452+
void PdbAstBuilder::Dump(Stream &stream, llvm::StringRef filter) {
1453+
m_clang.Dump(stream.AsRawOstream(), filter);
14541454
}

lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class PdbAstBuilder {
8787
TypeSystemClang &clang() { return m_clang; }
8888
ClangASTImporter &GetClangASTImporter() { return m_importer; }
8989

90-
void Dump(Stream &stream);
90+
void Dump(Stream &stream, llvm::StringRef filter);
9191

9292
private:
9393
clang::Decl *TryGetDecl(PdbSymUid uid) const;

0 commit comments

Comments
 (0)