Skip to content

Commit 95d5d98

Browse files
authored
[lldb][TypeSystem] Enable colored AST dump (#86159)
This patch causes the various AST dump commands (`target modules dump ast`/`target dump typesystem`) to be color-highlighted. I added a `bool show_color` parameter to `SymbolFile::DumpClangAST` and `TypeSystem::Dump`. In `TypeSystemClang` I temporarily sets the `getShowColors` flag on the owned Clang AST (using an RAII helper) for the duration of the AST dump. We use `Debugger::GetUseColors` to decide whether to color the AST dump.
1 parent f9cd2ee commit 95d5d98

18 files changed

+99
-29
lines changed

lldb/include/lldb/Symbol/SymbolFile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ class SymbolFile : public PluginInterface {
297297
lldb::SymbolContextItem resolve_scope,
298298
SymbolContextList &sc_list);
299299

300-
virtual void DumpClangAST(Stream &s, llvm::StringRef filter) {}
300+
virtual void DumpClangAST(Stream &s, llvm::StringRef filter,
301+
bool show_colors) {}
301302
virtual void FindGlobalVariables(ConstString name,
302303
const CompilerDeclContext &parent_decl_ctx,
303304
uint32_t max_matches,

lldb/include/lldb/Symbol/SymbolFileOnDemand.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ 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, llvm::StringRef filter) override;
130+
void DumpClangAST(lldb_private::Stream &s, llvm::StringRef filter,
131+
bool show_color) override;
131132

132133
void
133134
FindGlobalVariables(lldb_private::ConstString name,

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,9 @@ class TypeSystem : public PluginInterface,
448448
/// \param[out] output Stream to dup the AST into.
449449
/// \param[in] filter If empty, dump whole AST. If non-empty, will only
450450
/// dump decls whose names contain \c filter.
451-
virtual void Dump(llvm::raw_ostream &output, llvm::StringRef filter) = 0;
451+
/// \param[in] show_color If true, prints the AST color-highlighted.
452+
virtual void Dump(llvm::raw_ostream &output, llvm::StringRef filter,
453+
bool show_color) = 0;
452454

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

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,8 @@ class CommandObjectTargetModulesDumpClangAST
22752275
if (INTERRUPT_REQUESTED(GetDebugger(), "Interrupted dumping clang ast"))
22762276
break;
22772277
if (SymbolFile *sf = module_sp->GetSymbolFile())
2278-
sf->DumpClangAST(result.GetOutputStream(), filter);
2278+
sf->DumpClangAST(result.GetOutputStream(), filter,
2279+
GetCommandInterpreter().GetDebugger().GetUseColor());
22792280
}
22802281
result.SetStatus(eReturnStatusSuccessFinishResult);
22812282
return;
@@ -2304,7 +2305,8 @@ class CommandObjectTargetModulesDumpClangAST
23042305

23052306
Module *m = module_list.GetModulePointerAtIndex(i);
23062307
if (SymbolFile *sf = m->GetSymbolFile())
2307-
sf->DumpClangAST(result.GetOutputStream(), filter);
2308+
sf->DumpClangAST(result.GetOutputStream(), filter,
2309+
GetCommandInterpreter().GetDebugger().GetUseColor());
23082310
}
23092311
}
23102312
result.SetStatus(eReturnStatusSuccessFinishResult);
@@ -5294,7 +5296,8 @@ class CommandObjectTargetDumpTypesystem : public CommandObjectParsed {
52945296
// Go over every scratch TypeSystem and dump to the command output.
52955297
for (lldb::TypeSystemSP ts : GetTarget().GetScratchTypeSystems())
52965298
if (ts)
5297-
ts->Dump(result.GetOutputStream().AsRawOstream(), "");
5299+
ts->Dump(result.GetOutputStream().AsRawOstream(), "",
5300+
GetCommandInterpreter().GetDebugger().GetUseColor());
52985301

52995302
result.SetStatus(eReturnStatusSuccessFinishResult);
53005303
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,15 +4325,16 @@ void SymbolFileDWARF::Dump(lldb_private::Stream &s) {
43254325
m_index->Dump(s);
43264326
}
43274327

4328-
void SymbolFileDWARF::DumpClangAST(Stream &s, llvm::StringRef filter) {
4328+
void SymbolFileDWARF::DumpClangAST(Stream &s, llvm::StringRef filter,
4329+
bool show_color) {
43294330
auto ts_or_err = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus);
43304331
if (!ts_or_err)
43314332
return;
43324333
auto ts = *ts_or_err;
43334334
TypeSystemClang *clang = llvm::dyn_cast_or_null<TypeSystemClang>(ts.get());
43344335
if (!clang)
43354336
return;
4336-
clang->Dump(s.AsRawOstream(), filter);
4337+
clang->Dump(s.AsRawOstream(), filter, show_color);
43374338
}
43384339

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ class SymbolFileDWARF : public SymbolFileCommon {
277277

278278
void Dump(Stream &s) override;
279279

280-
void DumpClangAST(Stream &s, llvm::StringRef filter) override;
280+
void DumpClangAST(Stream &s, llvm::StringRef filter,
281+
bool show_colors) override;
281282

282283
/// List separate dwo files.
283284
bool GetSeparateDebugInfo(StructuredData::Dictionary &d, bool errors_only,

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

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

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

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

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

132-
void DumpClangAST(Stream &s, llvm::StringRef filter) override;
132+
void DumpClangAST(Stream &s, llvm::StringRef filter,
133+
bool show_color) override;
133134

134135
/// List separate oso files.
135136
bool GetSeparateDebugInfo(StructuredData::Dictionary &d, bool errors_only,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,8 +1454,9 @@ PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) {
14541454
return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
14551455
}
14561456

1457-
void PdbAstBuilder::Dump(Stream &stream, llvm::StringRef filter) {
1458-
m_clang.Dump(stream.AsRawOstream(), filter);
1457+
void PdbAstBuilder::Dump(Stream &stream, llvm::StringRef filter,
1458+
bool show_color) {
1459+
m_clang.Dump(stream.AsRawOstream(), filter, show_color);
14591460
}
14601461

14611462
clang::NamespaceDecl *

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, llvm::StringRef filter);
90+
void Dump(Stream &stream, llvm::StringRef filter, bool show_color);
9191

9292
clang::NamespaceDecl *FindNamespaceDecl(const clang::DeclContext *parent,
9393
llvm::StringRef name);

0 commit comments

Comments
 (0)