Skip to content

Commit 7965a49

Browse files
authored
Merge pull request #10474 from swiftlang/rdar148769820
[lldb] Customize the statusline for the Swift REPL
2 parents cfc0958 + 2b63062 commit 7965a49

File tree

7 files changed

+88
-3
lines changed

7 files changed

+88
-3
lines changed

lldb/include/lldb/Core/Debugger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
311311
bool GetShowStatusline() const;
312312

313313
const FormatEntity::Entry *GetStatuslineFormat() const;
314+
bool SetStatuslineFormat(const FormatEntity::Entry &format);
314315

315316
llvm::StringRef GetShowProgressAnsiPrefix() const;
316317

lldb/include/lldb/Interpreter/OptionValue.h

Lines changed: 8 additions & 2 deletions
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,13 @@ const FormatEntity::Entry *Debugger::GetStatuslineFormat() const {
480480
return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
481481
}
482482

483+
bool Debugger::SetStatuslineFormat(const FormatEntity::Entry &format) {
484+
constexpr uint32_t idx = ePropertyStatuslineFormat;
485+
bool ret = SetPropertyAtIndex(idx, format);
486+
RedrawStatusline();
487+
return ret;
488+
}
489+
483490
bool Debugger::GetUseAutosuggestion() const {
484491
const uint32_t idx = ePropertyShowAutosuggestion;
485492
return GetPropertyAtIndexAs<bool>(

lldb/source/Interpreter/OptionValue.cpp

Lines changed: 9 additions & 0 deletions
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/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,16 @@ Status SwiftREPL::DoInitialization() {
303303
std::static_pointer_cast<TypeSystemSwiftTypeRefForExpressions>(
304304
*type_system_or_err)
305305
->SetCompilerOptions(m_compiler_options.c_str());
306-
return Status();
306+
307+
std::string format_str = "${ansi.negative}Swift " +
308+
swift::version::getCompilerVersion() +
309+
"{ | {${progress.count} }${progress.message}}";
310+
FormatEntity::Entry format_entry;
311+
Status error = FormatEntity::Parse(format_str, format_entry);
312+
if (error.Success())
313+
m_target.GetDebugger().SetStatuslineFormat(format_entry);
314+
315+
return error;
307316
}
308317

309318
llvm::StringRef SwiftREPL::GetSourceFileBasename() {

lldb/unittests/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_lldb_unittest(LLDBCoreTests
2+
DebuggerTest.cpp
23
CommunicationTest.cpp
34
DiagnosticEventTest.cpp
45
DumpDataExtractorTest.cpp

lldb/unittests/Core/DebuggerTest.cpp

Lines changed: 52 additions & 0 deletions
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)