Skip to content

Commit 2b984fd

Browse files
authored
[lldb] Support programmatically setting the statusline format (NFC) (llvm#135250)
Support programmatically setting the statusline format. I want to use this API downstream, to change the statusline format for the Swift REPL.
1 parent 6136019 commit 2b984fd

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

lldb/include/lldb/Core/Debugger.h

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
307307
bool GetShowStatusline() const;
308308

309309
const FormatEntity::Entry *GetStatuslineFormat() const;
310+
bool SetStatuslineFormat(const FormatEntity::Entry &format);
310311

311312
llvm::StringRef GetShowProgressAnsiPrefix() const;
312313

lldb/include/lldb/Interpreter/OptionValue.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class OptionValue {
7272
virtual ~OptionValue() = default;
7373

7474
OptionValue(const OptionValue &other);
75-
75+
7676
OptionValue& operator=(const OptionValue &other);
7777

7878
// Subclasses should override these functions
@@ -330,6 +330,10 @@ class OptionValue {
330330

331331
bool SetValueAs(ArchSpec v) { return SetArchSpecValue(v); }
332332

333+
bool SetValueAs(const FormatEntity::Entry &v) {
334+
return SetFormatEntityValue(v);
335+
}
336+
333337
template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true>
334338
bool SetValueAs(T t) {
335339
return SetEnumerationValue(t);
@@ -387,8 +391,10 @@ class OptionValue {
387391
bool SetUUIDValue(const UUID &uuid);
388392

389393
const FormatEntity::Entry *GetFormatEntity() const;
394+
bool SetFormatEntityValue(const FormatEntity::Entry &entry);
395+
390396
const RegularExpression *GetRegexValue() const;
391-
397+
392398
mutable std::mutex m_mutex;
393399
};
394400

lldb/source/Core/Debugger.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ const FormatEntity::Entry *Debugger::GetStatuslineFormat() const {
484484
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
485485
}
486486

487+
bool Debugger::SetStatuslineFormat(const FormatEntity::Entry &format) {
488+
constexpr uint32_t idx = ePropertyStatuslineFormat;
489+
return SetPropertyAtIndex(idx, format);
490+
}
491+
487492
bool Debugger::GetUseAutosuggestion() const {
488493
const uint32_t idx = ePropertyShowAutosuggestion;
489494
return GetPropertyAtIndexAs<bool>(

lldb/source/Interpreter/OptionValue.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,15 @@ bool OptionValue::SetArchSpecValue(ArchSpec arch_spec) {
474474
return false;
475475
}
476476

477+
bool OptionValue::SetFormatEntityValue(const FormatEntity::Entry &entry) {
478+
std::lock_guard<std::mutex> lock(m_mutex);
479+
if (OptionValueFormatEntity *option_value = GetAsFormatEntity()) {
480+
option_value->SetCurrentValue(entry);
481+
return true;
482+
}
483+
return false;
484+
}
485+
477486
const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
478487
switch (t) {
479488
case eTypeInvalid:

lldb/unittests/Core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
add_lldb_unittest(LLDBCoreTests
3+
DebuggerTest.cpp
34
CommunicationTest.cpp
45
DiagnosticEventTest.cpp
56
DumpDataExtractorTest.cpp

lldb/unittests/Core/DebuggerTest.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===-- DebuggerTest.cpp --------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/Core/Debugger.h"
10+
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
11+
#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
12+
#include "TestingSupport/TestUtilities.h"
13+
#include "lldb/Host/FileSystem.h"
14+
#include "lldb/Host/HostInfo.h"
15+
#include "gtest/gtest.h"
16+
17+
using namespace lldb;
18+
using namespace lldb_private;
19+
20+
namespace {
21+
class DebuggerTest : public ::testing::Test {
22+
public:
23+
void SetUp() override {
24+
FileSystem::Initialize();
25+
HostInfo::Initialize();
26+
PlatformMacOSX::Initialize();
27+
std::call_once(TestUtilities::g_debugger_initialize_flag,
28+
[]() { Debugger::Initialize(nullptr); });
29+
ArchSpec arch("x86_64-apple-macosx-");
30+
Platform::SetHostPlatform(
31+
PlatformRemoteMacOSX::CreateInstance(true, &arch));
32+
}
33+
void TearDown() override {
34+
PlatformMacOSX::Terminate();
35+
HostInfo::Terminate();
36+
FileSystem::Terminate();
37+
}
38+
};
39+
} // namespace
40+
41+
TEST_F(DebuggerTest, TestSettings) {
42+
DebuggerSP debugger_sp = Debugger::CreateInstance();
43+
44+
EXPECT_TRUE(debugger_sp->SetUseColor(true));
45+
EXPECT_TRUE(debugger_sp->GetUseColor());
46+
47+
FormatEntity::Entry format("foo");
48+
EXPECT_TRUE(debugger_sp->SetStatuslineFormat(format));
49+
EXPECT_EQ(debugger_sp->GetStatuslineFormat()->string, "foo");
50+
51+
Debugger::Destroy(debugger_sp);
52+
}

0 commit comments

Comments
 (0)