[Lldb-commits] [lldb] [lldb] Fix indentation when printing stop hooks (PR #165945)
Julian Lettner via lldb-commits
lldb-commits at lists.llvm.org
Mon Nov 3 09:34:03 PST 2025
https://github.com/yln updated https://github.com/llvm/llvm-project/pull/165945
>From 76065f031ab36b327293a42929c703d0c013d752 Mon Sep 17 00:00:00 2001
From: Julian Lettner <jlettner at apple.com>
Date: Fri, 31 Oct 2025 15:25:31 -0700
Subject: [PATCH 1/4] [lldb] Fix indentation when printing stop hooks
Fix the format (i.e., indentation) when printing
stop hooks via `target stop-hook list`.
---
lldb/source/Target/Target.cpp | 3 +-
.../StopHook/stop-hook-list-format.test | 36 +++++++++++++++++++
2 files changed, 38 insertions(+), 1 deletion(-)
create mode 100644 lldb/test/Shell/ExecControl/StopHook/stop-hook-list-format.test
diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp
index d070c3d953d4a..83be3dbe69d62 100644
--- a/lldb/source/Target/Target.cpp
+++ b/lldb/source/Target/Target.cpp
@@ -3992,6 +3992,7 @@ void Target::StopHook::GetDescription(Stream &s,
s.SetIndentLevel(indent_level + 2);
}
GetSubclassDescription(s, level);
+ s.SetIndentLevel(indent_level);
}
void Target::StopHookCommandLine::GetSubclassDescription(
@@ -4002,7 +4003,7 @@ void Target::StopHookCommandLine::GetSubclassDescription(
s.PutCString(m_commands.GetStringAtIndex(0));
return;
}
- s.Indent("Commands: \n");
+ s.Indent("Commands:\n");
s.SetIndentLevel(s.GetIndentLevel() + 4);
uint32_t num_commands = m_commands.GetSize();
for (uint32_t i = 0; i < num_commands; i++) {
diff --git a/lldb/test/Shell/ExecControl/StopHook/stop-hook-list-format.test b/lldb/test/Shell/ExecControl/StopHook/stop-hook-list-format.test
new file mode 100644
index 0000000000000..a9557801cc134
--- /dev/null
+++ b/lldb/test/Shell/ExecControl/StopHook/stop-hook-list-format.test
@@ -0,0 +1,36 @@
+# Test format (e.g., indentation) when printing the list of stop hooks.
+#
+# RUN: %lldb -b -s %s | FileCheck %s --match-full-lines --strict-whitespace
+
+# Create some stop hooks
+target stop-hook add -o 'print "Hello"' --auto-continue true --at-initial-stop true
+target stop-hook add -o 'print "world,"' -o 'print "nice"' --file 'my_file'
+target stop-hook add -o 'print "weather!"' --classname 'MyClass' --thread-name 'my_thread'
+
+# Print hooks
+target stop-hook list
+
+# CHECK:(lldb) target stop-hook list
+# CHECK:Hook: 1
+# CHECK: State: enabled
+# CHECK: AutoContinue on
+# CHECK: Commands:
+# CHECK: print "Hello"
+# CHECK-EMPTY:
+# CHECK:Hook: 2
+# CHECK: State: enabled
+# CHECK: Specifier:
+# CHECK: File: my_file.
+# CHECK: Commands:
+# CHECK: print "world,"
+# CHECK: print "nice"
+# CHECK-EMPTY:
+# CHECK:Hook: 3
+# CHECK: State: enabled
+# CHECK: Specifier:
+# CHECK: Class name: MyClass.
+# CHECK: Thread:
+# CHECK: thread name: "my_thread"
+# CHECK: Commands:
+# CHECK: print "weather!"
+# CHECK-EMPTY:
>From dc15f12965ce4a5bc24eaa989f7104b69c079e24 Mon Sep 17 00:00:00 2001
From: Julian Lettner <jlettner at apple.com>
Date: Fri, 31 Oct 2025 16:05:38 -0700
Subject: [PATCH 2/4] [lldb] Add indentation scope guard
Add `IndentScope Stream::MakeIndentScope()` to
make managing (and restoring!) of the indentation
level on `Stream` instances more ergonomic and
less error prone.
---
lldb/include/lldb/Utility/Stream.h | 26 ++++++++++++++++++++------
lldb/source/Utility/Stream.cpp | 8 ++++++++
2 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/lldb/include/lldb/Utility/Stream.h b/lldb/include/lldb/Utility/Stream.h
index 82774d56922a9..eb3d6f81cb2d1 100644
--- a/lldb/include/lldb/Utility/Stream.h
+++ b/lldb/include/lldb/Utility/Stream.h
@@ -300,6 +300,12 @@ class Stream {
/// The current indentation level.
unsigned GetIndentLevel() const;
+ /// Set the current indentation level.
+ ///
+ /// \param[in] level
+ /// The new indentation level.
+ void SetIndentLevel(unsigned level);
+
/// Indent the current line in the stream.
///
/// Indent the current line using the current indentation level and print an
@@ -315,6 +321,20 @@ class Stream {
/// Increment the current indentation level.
void IndentMore(unsigned amount = 2);
+ class IndentScope {
+ Stream &m_stream;
+ unsigned m_original_indent_level;
+
+ public:
+ IndentScope(Stream &stream)
+ : m_stream(stream), m_original_indent_level(stream.GetIndentLevel()) {}
+ ~IndentScope() { m_stream.SetIndentLevel(m_original_indent_level); }
+ };
+
+ /// Create an indentation scope that restores the original indent level when
+ /// the object goes out of scope (RAII).
+ IndentScope MakeIndentScope(unsigned indent_amount = 2);
+
/// Output an offset value.
///
/// Put an offset \a uval out to the stream using the printf format in \a
@@ -364,12 +384,6 @@ class Stream {
/// address and pointer values.
void SetAddressByteSize(uint32_t addr_size);
- /// Set the current indentation level.
- ///
- /// \param[in] level
- /// The new indentation level.
- void SetIndentLevel(unsigned level);
-
/// Output a SLEB128 number to the stream.
///
/// Put an SLEB128 \a uval out to the stream using the printf format in \a
diff --git a/lldb/source/Utility/Stream.cpp b/lldb/source/Utility/Stream.cpp
index 89dce9fb0e1f7..e9632c3e1fc1f 100644
--- a/lldb/source/Utility/Stream.cpp
+++ b/lldb/source/Utility/Stream.cpp
@@ -202,6 +202,14 @@ void Stream::IndentLess(unsigned amount) {
m_indent_level = 0;
}
+// Create an indentation scope that restores the original indent level when the
+// object goes out of scope (RAII).
+Stream::IndentScope Stream::MakeIndentScope(unsigned indent_amount) {
+ IndentScope indent_scope(*this);
+ IndentMore(indent_amount);
+ return indent_scope;
+}
+
// Get the address size in bytes
uint32_t Stream::GetAddressByteSize() const { return m_addr_size; }
>From 3e60bdf0a36d34e6668e75f8ffefb4ec1c9cca93 Mon Sep 17 00:00:00 2001
From: Julian Lettner <jlettner at apple.com>
Date: Fri, 31 Oct 2025 16:39:30 -0700
Subject: [PATCH 3/4] [lldb] Simplify printing of stop hooks
Simplify printing of stop hooks using the new
IndentScope.
---
lldb/source/Target/Target.cpp | 18 +++++-------------
1 file changed, 5 insertions(+), 13 deletions(-)
diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp
index 83be3dbe69d62..0f3b826fad95f 100644
--- a/lldb/source/Target/Target.cpp
+++ b/lldb/source/Target/Target.cpp
@@ -3961,9 +3961,7 @@ void Target::StopHook::GetDescription(Stream &s,
return;
}
- unsigned indent_level = s.GetIndentLevel();
-
- s.SetIndentLevel(indent_level + 2);
+ auto indent_scope = s.MakeIndentScope();
s.Printf("Hook: %" PRIu64 "\n", GetID());
if (m_active)
@@ -3977,22 +3975,19 @@ void Target::StopHook::GetDescription(Stream &s,
if (m_specifier_sp) {
s.Indent();
s.PutCString("Specifier:\n");
- s.SetIndentLevel(indent_level + 4);
+ auto indent_scope = s.MakeIndentScope();
m_specifier_sp->GetDescription(&s, level);
- s.SetIndentLevel(indent_level + 2);
}
if (m_thread_spec_up) {
StreamString tmp;
s.Indent("Thread:\n");
m_thread_spec_up->GetDescription(&tmp, level);
- s.SetIndentLevel(indent_level + 4);
+ auto indent_scope = s.MakeIndentScope();
s.Indent(tmp.GetString());
s.PutCString("\n");
- s.SetIndentLevel(indent_level + 2);
}
GetSubclassDescription(s, level);
- s.SetIndentLevel(indent_level);
}
void Target::StopHookCommandLine::GetSubclassDescription(
@@ -4004,13 +3999,12 @@ void Target::StopHookCommandLine::GetSubclassDescription(
return;
}
s.Indent("Commands:\n");
- s.SetIndentLevel(s.GetIndentLevel() + 4);
+ auto indent_scope = s.MakeIndentScope(4);
uint32_t num_commands = m_commands.GetSize();
for (uint32_t i = 0; i < num_commands; i++) {
s.Indent(m_commands.GetStringAtIndex(i));
s.PutCString("\n");
}
- s.SetIndentLevel(s.GetIndentLevel() - 4);
}
// Target::StopHookCommandLine
@@ -4145,7 +4139,7 @@ void Target::StopHookScripted::GetSubclassDescription(
return;
s.Indent("Args:\n");
- s.SetIndentLevel(s.GetIndentLevel() + 4);
+ auto indent_scope = s.MakeIndentScope(4);
auto print_one_element = [&s](llvm::StringRef key,
StructuredData::Object *object) {
@@ -4155,8 +4149,6 @@ void Target::StopHookScripted::GetSubclassDescription(
};
as_dict->ForEach(print_one_element);
-
- s.SetIndentLevel(s.GetIndentLevel() - 4);
}
static constexpr OptionEnumValueElement g_dynamic_value_types[] = {
>From 2ac4b83fa1559447e848b04a8b5fea61c92c4faa Mon Sep 17 00:00:00 2001
From: Julian Lettner <jlettner at apple.com>
Date: Mon, 3 Nov 2025 09:07:42 -0800
Subject: [PATCH 4/4] Address PR feedback
* Make class member ordering consistent.
---
lldb/include/lldb/Utility/Stream.h | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/lldb/include/lldb/Utility/Stream.h b/lldb/include/lldb/Utility/Stream.h
index eb3d6f81cb2d1..13455552131da 100644
--- a/lldb/include/lldb/Utility/Stream.h
+++ b/lldb/include/lldb/Utility/Stream.h
@@ -321,14 +321,14 @@ class Stream {
/// Increment the current indentation level.
void IndentMore(unsigned amount = 2);
- class IndentScope {
- Stream &m_stream;
- unsigned m_original_indent_level;
-
- public:
+ struct IndentScope {
IndentScope(Stream &stream)
: m_stream(stream), m_original_indent_level(stream.GetIndentLevel()) {}
~IndentScope() { m_stream.SetIndentLevel(m_original_indent_level); }
+
+ private:
+ Stream &m_stream;
+ unsigned m_original_indent_level;
};
/// Create an indentation scope that restores the original indent level when
More information about the lldb-commits
mailing list