Skip to content

Commit 25da15f

Browse files
authored
[lldb] Fix indentation when printing stop hooks (#165945)
This commit aggregates the following changes: 1. Fix the format (i.e., indentation) when printing stop hooks via `target stop-hook list`. 2. Add `IndentScope Stream::MakeIndentScope()` to make managing (and restoring!) of the indentation level on `Stream` instances more ergonomic and less error prone. 3. Simplify printing of stop hooks using the new `IndentScope`.
1 parent e5d9644 commit 25da15f

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

lldb/include/lldb/Utility/Stream.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ class Stream {
300300
/// The current indentation level.
301301
unsigned GetIndentLevel() const;
302302

303+
/// Set the current indentation level.
304+
///
305+
/// \param[in] level
306+
/// The new indentation level.
307+
void SetIndentLevel(unsigned level);
308+
303309
/// Indent the current line in the stream.
304310
///
305311
/// Indent the current line using the current indentation level and print an
@@ -315,6 +321,20 @@ class Stream {
315321
/// Increment the current indentation level.
316322
void IndentMore(unsigned amount = 2);
317323

324+
struct IndentScope {
325+
IndentScope(Stream &stream)
326+
: m_stream(stream), m_original_indent_level(stream.GetIndentLevel()) {}
327+
~IndentScope() { m_stream.SetIndentLevel(m_original_indent_level); }
328+
329+
private:
330+
Stream &m_stream;
331+
unsigned m_original_indent_level;
332+
};
333+
334+
/// Create an indentation scope that restores the original indent level when
335+
/// the object goes out of scope (RAII).
336+
IndentScope MakeIndentScope(unsigned indent_amount = 2);
337+
318338
/// Output an offset value.
319339
///
320340
/// Put an offset \a uval out to the stream using the printf format in \a
@@ -364,12 +384,6 @@ class Stream {
364384
/// address and pointer values.
365385
void SetAddressByteSize(uint32_t addr_size);
366386

367-
/// Set the current indentation level.
368-
///
369-
/// \param[in] level
370-
/// The new indentation level.
371-
void SetIndentLevel(unsigned level);
372-
373387
/// Output a SLEB128 number to the stream.
374388
///
375389
/// Put an SLEB128 \a uval out to the stream using the printf format in \a

lldb/source/Target/Target.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3962,9 +3962,7 @@ void Target::StopHook::GetDescription(Stream &s,
39623962
return;
39633963
}
39643964

3965-
unsigned indent_level = s.GetIndentLevel();
3966-
3967-
s.SetIndentLevel(indent_level + 2);
3965+
auto indent_scope = s.MakeIndentScope();
39683966

39693967
s.Printf("Hook: %" PRIu64 "\n", GetID());
39703968
if (m_active)
@@ -3978,19 +3976,17 @@ void Target::StopHook::GetDescription(Stream &s,
39783976
if (m_specifier_sp) {
39793977
s.Indent();
39803978
s.PutCString("Specifier:\n");
3981-
s.SetIndentLevel(indent_level + 4);
3979+
auto indent_scope = s.MakeIndentScope();
39823980
m_specifier_sp->GetDescription(&s, level);
3983-
s.SetIndentLevel(indent_level + 2);
39843981
}
39853982

39863983
if (m_thread_spec_up) {
39873984
StreamString tmp;
39883985
s.Indent("Thread:\n");
39893986
m_thread_spec_up->GetDescription(&tmp, level);
3990-
s.SetIndentLevel(indent_level + 4);
3987+
auto indent_scope = s.MakeIndentScope();
39913988
s.Indent(tmp.GetString());
39923989
s.PutCString("\n");
3993-
s.SetIndentLevel(indent_level + 2);
39943990
}
39953991
GetSubclassDescription(s, level);
39963992
}
@@ -4003,14 +3999,13 @@ void Target::StopHookCommandLine::GetSubclassDescription(
40033999
s.PutCString(m_commands.GetStringAtIndex(0));
40044000
return;
40054001
}
4006-
s.Indent("Commands: \n");
4007-
s.SetIndentLevel(s.GetIndentLevel() + 4);
4002+
s.Indent("Commands:\n");
4003+
auto indent_scope = s.MakeIndentScope(4);
40084004
uint32_t num_commands = m_commands.GetSize();
40094005
for (uint32_t i = 0; i < num_commands; i++) {
40104006
s.Indent(m_commands.GetStringAtIndex(i));
40114007
s.PutCString("\n");
40124008
}
4013-
s.SetIndentLevel(s.GetIndentLevel() - 4);
40144009
}
40154010

40164011
// Target::StopHookCommandLine
@@ -4145,7 +4140,7 @@ void Target::StopHookScripted::GetSubclassDescription(
41454140
return;
41464141

41474142
s.Indent("Args:\n");
4148-
s.SetIndentLevel(s.GetIndentLevel() + 4);
4143+
auto indent_scope = s.MakeIndentScope(4);
41494144

41504145
auto print_one_element = [&s](llvm::StringRef key,
41514146
StructuredData::Object *object) {
@@ -4155,8 +4150,6 @@ void Target::StopHookScripted::GetSubclassDescription(
41554150
};
41564151

41574152
as_dict->ForEach(print_one_element);
4158-
4159-
s.SetIndentLevel(s.GetIndentLevel() - 4);
41604153
}
41614154

41624155
static constexpr OptionEnumValueElement g_dynamic_value_types[] = {

lldb/source/Utility/Stream.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ void Stream::IndentLess(unsigned amount) {
202202
m_indent_level = 0;
203203
}
204204

205+
// Create an indentation scope that restores the original indent level when the
206+
// object goes out of scope (RAII).
207+
Stream::IndentScope Stream::MakeIndentScope(unsigned indent_amount) {
208+
IndentScope indent_scope(*this);
209+
IndentMore(indent_amount);
210+
return indent_scope;
211+
}
212+
205213
// Get the address size in bytes
206214
uint32_t Stream::GetAddressByteSize() const { return m_addr_size; }
207215

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Test format (e.g., indentation) when printing the list of stop hooks.
2+
#
3+
# RUN: %lldb -b -s %s | FileCheck %s --match-full-lines --strict-whitespace
4+
5+
# Create some stop hooks
6+
target stop-hook add -o 'print "Hello"' --auto-continue true --at-initial-stop true
7+
target stop-hook add -o 'print "world,"' -o 'print "nice"' --file 'my_file'
8+
target stop-hook add -o 'print "weather!"' --classname 'MyClass' --thread-name 'my_thread'
9+
10+
# Print hooks
11+
target stop-hook list
12+
13+
# CHECK:(lldb) target stop-hook list
14+
# CHECK:Hook: 1
15+
# CHECK: State: enabled
16+
# CHECK: AutoContinue on
17+
# CHECK: Commands:
18+
# CHECK: print "Hello"
19+
# CHECK-EMPTY:
20+
# CHECK:Hook: 2
21+
# CHECK: State: enabled
22+
# CHECK: Specifier:
23+
# CHECK: File: my_file.
24+
# CHECK: Commands:
25+
# CHECK: print "world,"
26+
# CHECK: print "nice"
27+
# CHECK-EMPTY:
28+
# CHECK:Hook: 3
29+
# CHECK: State: enabled
30+
# CHECK: Specifier:
31+
# CHECK: Class name: MyClass.
32+
# CHECK: Thread:
33+
# CHECK: thread name: "my_thread"
34+
# CHECK: Commands:
35+
# CHECK: print "weather!"
36+
# CHECK-EMPTY:

0 commit comments

Comments
 (0)