Skip to content

Conversation

Michael137
Copy link
Member

@Michael137 Michael137 commented Mar 21, 2024

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.

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

Changes

This patch sets the necessary ASTContext flag
to ensure that the various ASTs (i.e., symbolfile-backed ASTs, scratch AST and the expression AST) in LLDB are dumped using syntax highlighting (i.e., whenever a user runs target modules dump ast or calls the dump() on a clang::Decl).

Decided to not put this behind a setting because the diagnostics object persists on some AST and gets re-used, so the setting wouldn't consistenly turn off syntax highlighting (unless we made the setter fetch all live ASTs and turn the highlighting off).


Full diff: https://github.com/llvm/llvm-project/pull/86159.diff

2 Files Affected:

  • (modified) lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp (+3)
  • (modified) lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp (+3)
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
index 574d661e2a215e..0da497d74ffe86 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -465,6 +465,9 @@ ClangExpressionParser::ClangExpressionParser(
   // A value of 0 means no limit for both LLDB and Clang.
   m_compiler->getDiagnostics().setErrorLimit(target_sp->GetExprErrorLimit());
 
+  // AST nodes will be dumped with color
+  m_compiler->getDiagnostics().setShowColors(true);
+
   auto target_info = TargetInfo::CreateTargetInfo(
       m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
   if (log) {
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 51ab13108feb3a..146bfbe965e33e 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -752,6 +752,9 @@ void TypeSystemClang::CreateASTContext() {
   m_diagnostic_consumer_up = std::make_unique<NullDiagnosticConsumer>();
   m_ast_up->getDiagnostics().setClient(m_diagnostic_consumer_up.get(), false);
 
+  // AST nodes will be dumped with color
+  m_ast_up->getDiagnostics().setShowColors(true);
+
   // This can be NULL if we don't know anything about the architecture or if
   // the target for an architecture isn't enabled in the llvm/clang that we
   // built

@bulbazord
Copy link
Member

What happens if you have colors disabled in your terminal? Does this do nothing? Or does it start inserting ANSI escape codes in plain text?

@Michael137
Copy link
Member Author

Michael137 commented Mar 21, 2024

What happens if you have colors disabled in your terminal? Does this do nothing? Or does it start inserting ANSI escape codes in plain text?

Yea good question, was about to try this out. It does whatever clang's ast-dump would do if colors aren't turned off

@JDevlieghere
Copy link
Member

What happens if you have colors disabled in your terminal? Does this do nothing? Or does it start inserting ANSI escape codes in plain text?

Yea good question, was about to try this out. It does whatever clang's ast-dump would do if colors aren't turned off

That keys off of the output stream supporting colors, but LLDB also allows you to disable colors globally. Please also try lldb --no-use-colors and see if that behaves correctly (it might if we set the properties of the stream correctly). If it doesn't you'll need to read debugger.GetUseColor().

@JDevlieghere
Copy link
Member

Either way, we should add a comment explaining that (and why) this works.

@Michael137
Copy link
Member Author

What happens if you have colors disabled in your terminal? Does this do nothing? Or does it start inserting ANSI escape codes in plain text?

Yea good question, was about to try this out. It does whatever clang's ast-dump would do if colors aren't turned off

That keys off of the output stream supporting colors, but LLDB also allows you to disable colors globally. Please also try lldb --no-use-colors and see if that behaves correctly (it might if we set the properties of the stream correctly). If it doesn't you'll need to read debugger.GetUseColor().

It behaves correctly for the target modules dump ast case because that uses the LLDB streams, as you mentioned. For other diagnostics we would have to use the GetUseColor API, though we'd have to plumb the target through to TypeSystemClang::CreateASTContext first, to get access to the debugger object. Will try that instead

@jimingham
Copy link
Collaborator

jimingham commented Mar 25, 2024 via email

This patch sets the necessary `ASTContext` flag
to ensure that the various ASTs (i.e., symbolfile-backed
ASTs, scratch AST and the expression AST) in LLDB are dumped
using syntax highlighting (i.e., whenever a user runs
`target modules dump ast` or calls the `dump() on a `clang::Decl`).

Decided to not put this behind a setting because the diagnostics
object persists on some AST and gets re-used, so the setting wouldn't
consistenly turn off syntax highlighting (unless we made the setter
fetch all live ASTs and turn the highlighting off).
@Michael137 Michael137 force-pushed the lldb/show-ast-colors-no-setting branch from fd904cc to 2b4d973 Compare September 13, 2025 14:18
@Michael137
Copy link
Member Author

You can't get from a TypeSystem to a Target directly, because TypeSystem's live in Modules and Modules live outside any particular target in the global module cache. Maybe you need to pass this in to the call to dump the functions

Ended up taking this approach

@Michael137 Michael137 merged commit 95d5d98 into llvm:main Sep 15, 2025
9 checks passed
@Michael137 Michael137 deleted the lldb/show-ast-colors-no-setting branch September 15, 2025 17:03
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 15, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building lldb at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/31135

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
UNSUPPORTED: lldb-shell :: RPC/Generator/Tests/Server/CheckConstCharPointer.test (3183 of 3192)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/independent_state.test (3184 of 3192)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Python/Crashlog/json.test (3185 of 3192)
UNSUPPORTED: lldb-shell :: SymbolFile/NativePDB/typedefs.cpp (3186 of 3192)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Python/Crashlog/interactive_crashlog_json.test (3187 of 3192)
PASS: lldb-shell :: Commands/command-image-dump-ast-colored.test (3188 of 3192)
PASS: lldb-api :: terminal/TestEditlineCompletions.py (3189 of 3192)
PASS: lldb-api :: commands/process/attach/TestProcessAttach.py (3190 of 3192)
PASS: lldb-api :: repl/clang/TestClangREPL.py (3191 of 3192)
TIMEOUT: lldb-api :: tools/lldb-dap/module/TestDAP_module.py (3192 of 3192)
******************** TEST 'lldb-api :: tools/lldb-dap/module/TestDAP_module.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib --cmake-build-type Release -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/module -p TestDAP_module.py
--
Exit Code: -9
Timeout: Reached timeout of 600 seconds

Command Output (stdout):
--
lldb version 22.0.0git (https://github.com/llvm/llvm-project.git revision 95d5d984db4092136ad4b178b765516168c31b9e)
  clang revision 95d5d984db4092136ad4b178b765516168c31b9e
  llvm revision 95d5d984db4092136ad4b178b765516168c31b9e

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/module
runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

runCmd: settings set target.auto-apply-fixits false

itzexpoexpo pushed a commit to itzexpoexpo/llvm-project that referenced this pull request Sep 21, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants