Skip to content

Commit 6eb1619

Browse files
Michael137github-actions[bot]
authored andcommitted
Automerge: [lldb] Add filter option to AST dump command (#142164)
Depends on llvm/llvm-project#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.
2 parents 716bf29 + 0f7e10b commit 6eb1619

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;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,15 +1630,15 @@ size_t SymbolFileNativePDB::ParseSymbolArrayInScope(
16301630
return count;
16311631
}
16321632

1633-
void SymbolFileNativePDB::DumpClangAST(Stream &s) {
1633+
void SymbolFileNativePDB::DumpClangAST(Stream &s, llvm::StringRef filter) {
16341634
auto ts_or_err = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
16351635
if (!ts_or_err)
16361636
return;
16371637
auto ts = *ts_or_err;
16381638
TypeSystemClang *clang = llvm::dyn_cast_or_null<TypeSystemClang>(ts.get());
16391639
if (!clang)
16401640
return;
1641-
clang->GetNativePDBParser()->Dump(s);
1641+
clang->GetNativePDBParser()->Dump(s, filter);
16421642
}
16431643

16441644
void SymbolFileNativePDB::FindGlobalVariables(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class SymbolFileNativePDB : public SymbolFileCommon {
157157

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

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

162162
std::optional<llvm::codeview::TypeIndex>
163163
GetParentType(llvm::codeview::TypeIndex ti);

lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
14311431
symtab.Finalize();
14321432
}
14331433

1434-
void SymbolFilePDB::DumpClangAST(Stream &s) {
1434+
void SymbolFilePDB::DumpClangAST(Stream &s, llvm::StringRef filter) {
14351435
auto type_system_or_err =
14361436
GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
14371437
if (auto err = type_system_or_err.takeError()) {
@@ -1445,7 +1445,7 @@ void SymbolFilePDB::DumpClangAST(Stream &s) {
14451445
llvm::dyn_cast_or_null<TypeSystemClang>(ts.get());
14461446
if (!clang_type_system)
14471447
return;
1448-
clang_type_system->Dump(s.AsRawOstream());
1448+
clang_type_system->Dump(s.AsRawOstream(), filter);
14491449
}
14501450

14511451
void SymbolFilePDB::FindTypesByRegex(

lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class SymbolFilePDB : public lldb_private::SymbolFileCommon {
157157

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

160-
void DumpClangAST(lldb_private::Stream &s) override;
160+
void DumpClangAST(lldb_private::Stream &s, llvm::StringRef filter) override;
161161

162162
private:
163163
struct SecContribInfo {

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "clang/AST/DeclBase.h"
1212
#include "clang/AST/ExprCXX.h"
13+
#include "clang/Frontend/ASTConsumers.h"
1314
#include "llvm/Support/Casting.h"
1415
#include "llvm/Support/FormatAdapters.h"
1516
#include "llvm/Support/FormatVariadic.h"
@@ -8499,8 +8500,16 @@ TypeSystemClang::dump(lldb::opaque_compiler_type_t type) const {
84998500
}
85008501
#endif
85018502

8502-
void TypeSystemClang::Dump(llvm::raw_ostream &output) {
8503-
GetTranslationUnitDecl()->dump(output);
8503+
void TypeSystemClang::Dump(llvm::raw_ostream &output, llvm::StringRef filter) {
8504+
auto consumer =
8505+
clang::CreateASTDumper(output, filter,
8506+
/*DumpDecls=*/true,
8507+
/*Deserialize=*/false,
8508+
/*DumpLookups=*/false,
8509+
/*DumpDeclTypes=*/false, clang::ADOF_Default);
8510+
assert(consumer);
8511+
assert(m_ast_up);
8512+
consumer->HandleTranslationUnit(*m_ast_up);
85048513
}
85058514

85068515
void TypeSystemClang::DumpFromSymbolFile(Stream &s,
@@ -9625,10 +9634,11 @@ GetNameForIsolatedASTKind(ScratchTypeSystemClang::IsolatedASTKind kind) {
96259634
llvm_unreachable("Unimplemented IsolatedASTKind?");
96269635
}
96279636

9628-
void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output) {
9637+
void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output,
9638+
llvm::StringRef filter) {
96299639
// First dump the main scratch AST.
96309640
output << "State of scratch Clang type system:\n";
9631-
TypeSystemClang::Dump(output);
9641+
TypeSystemClang::Dump(output, filter);
96329642

96339643
// Now sort the isolated sub-ASTs.
96349644
typedef std::pair<IsolatedASTKey, TypeSystem *> KeyAndTS;
@@ -9643,7 +9653,7 @@ void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output) {
96439653
static_cast<ScratchTypeSystemClang::IsolatedASTKind>(a.first);
96449654
output << "State of scratch Clang type subsystem "
96459655
<< GetNameForIsolatedASTKind(kind) << ":\n";
9646-
a.second->Dump(output);
9656+
a.second->Dump(output, filter);
96479657
}
96489658
}
96499659

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ class TypeSystemClang : public TypeSystem {
10741074
#endif
10751075

10761076
/// \see lldb_private::TypeSystem::Dump
1077-
void Dump(llvm::raw_ostream &output) override;
1077+
void Dump(llvm::raw_ostream &output, llvm::StringRef filter) override;
10781078

10791079
/// Dump clang AST types from the symbol file.
10801080
///
@@ -1318,7 +1318,7 @@ class ScratchTypeSystemClang : public TypeSystemClang {
13181318
}
13191319

13201320
/// \see lldb_private::TypeSystem::Dump
1321-
void Dump(llvm::raw_ostream &output) override;
1321+
void Dump(llvm::raw_ostream &output, llvm::StringRef filter) override;
13221322

13231323
UserExpression *GetUserExpression(llvm::StringRef expr,
13241324
llvm::StringRef prefix,

lldb/source/Symbol/SymbolFileOnDemand.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,14 @@ void SymbolFileOnDemand::Dump(lldb_private::Stream &s) {
305305
return m_sym_file_impl->Dump(s);
306306
}
307307

308-
void SymbolFileOnDemand::DumpClangAST(lldb_private::Stream &s) {
308+
void SymbolFileOnDemand::DumpClangAST(lldb_private::Stream &s,
309+
llvm::StringRef filter) {
309310
if (!m_debug_info_enabled) {
310311
LLDB_LOG(GetLog(), "[{0}] {1} is skipped", GetSymbolFileName(),
311312
__FUNCTION__);
312313
return;
313314
}
314-
return m_sym_file_impl->DumpClangAST(s);
315+
return m_sym_file_impl->DumpClangAST(s, filter);
315316
}
316317

317318
void SymbolFileOnDemand::FindGlobalVariables(const RegularExpression &regex,
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Test `image dump ast` command.
2+
3+
# RUN: split-file %s %t
4+
# RUN: %clang_host -g -gdwarf %t/main.cpp -o %t.out
5+
# RUN: %lldb -x -b -s %t/commands.input %t.out -o exit 2>&1 \
6+
# RUN: | FileCheck %s
7+
8+
#--- main.cpp
9+
10+
void A() {}
11+
void A1() {}
12+
void BA1() {}
13+
void AB() {}
14+
15+
int main() {
16+
A();
17+
A1();
18+
BA1();
19+
AB();
20+
}
21+
22+
#--- commands.input
23+
24+
break set -n main
25+
run
26+
expr A(); A1(); BA1(); AB()
27+
28+
image dump ast
29+
30+
# CHECK: image dump ast
31+
# CHECK-DAG: FunctionDecl {{.*}} main
32+
# CHECK-DAG: FunctionDecl {{.*}} A
33+
# CHECK-DAG: FunctionDecl {{.*}} A1
34+
# CHECK-DAG: FunctionDecl {{.*}} BA1
35+
# CHECK-DAG: FunctionDecl {{.*}} AB
36+
37+
image dump ast --filter A
38+
39+
# CHECK: image dump ast --filter A
40+
# CHECK: Dumping A
41+
# CHECK-NOT: FunctionDecl {{.*}} main
42+
# CHECK-DAG: FunctionDecl {{.*}} A1
43+
# CHECK-DAG: FunctionDecl {{.*}} BA1
44+
# CHECK-DAG: FunctionDecl {{.*}} AB
45+
46+
image dump ast --filter A1
47+
48+
# CHECK: image dump ast --filter A1
49+
# CHECK: Dumping A
50+
# CHECK-NOT: FunctionDecl {{.*}} main
51+
# CHECK-NOT: FunctionDecl {{.*}} AB
52+
# CHECK-DAG: FunctionDecl {{.*}} A1
53+
# CHECK-DAG: FunctionDecl {{.*}} BA1
54+
55+
image dump ast --filter ""
56+
57+
# CHECK: image dump ast --filter ""
58+
# CHECK-DAG: FunctionDecl {{.*}} main
59+
# CHECK-DAG: FunctionDecl {{.*}} AB
60+
# CHECK-DAG: FunctionDecl {{.*}} A1
61+
# CHECK-DAG: FunctionDecl {{.*}} BA1
62+
63+
image dump ast -f AB
64+
65+
# CHECK: image dump ast -f AB
66+
# CHECK: Dumping AB
67+
# CHECK-NOT: FunctionDecl {{.*}} main
68+
# CHECK-NOT: FunctionDecl {{.*}} A1
69+
# CHECK-NOT: FunctionDecl {{.*}} BA1
70+
# CHECK: FunctionDecl {{.*}} AB

0 commit comments

Comments
 (0)