Skip to content

[lldb] Support programmatically setting the statusline format (NFC) #135250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025

Conversation

JDevlieghere
Copy link
Member

Support programmatically setting the statusline format. I want to use this API downstream, to change the statusline format for the Swift REPL.

Support programmatically setting the statusline format. I want to use
this API downstream, to change the statusline format for the Swift REPL.
@llvmbot
Copy link
Member

llvmbot commented Apr 10, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

Support programmatically setting the statusline format. I want to use this API downstream, to change the statusline format for the Swift REPL.


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

6 Files Affected:

  • (modified) lldb/include/lldb/Core/Debugger.h (+1)
  • (modified) lldb/include/lldb/Interpreter/OptionValue.h (+8-2)
  • (modified) lldb/source/Core/Debugger.cpp (+5)
  • (modified) lldb/source/Interpreter/OptionValue.cpp (+9)
  • (modified) lldb/unittests/Core/CMakeLists.txt (+1)
  • (added) lldb/unittests/Core/DebuggerTest.cpp (+52)
diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h
index c79a75ab61564..448e8d6a8fc6a 100644
--- a/lldb/include/lldb/Core/Debugger.h
+++ b/lldb/include/lldb/Core/Debugger.h
@@ -307,6 +307,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
   bool GetShowStatusline() const;
 
   const FormatEntity::Entry *GetStatuslineFormat() const;
+  bool SetStatuslineFormat(const FormatEntity::Entry &format);
 
   llvm::StringRef GetShowProgressAnsiPrefix() const;
 
diff --git a/lldb/include/lldb/Interpreter/OptionValue.h b/lldb/include/lldb/Interpreter/OptionValue.h
index d19c8b8fab622..ebc438517a7b1 100644
--- a/lldb/include/lldb/Interpreter/OptionValue.h
+++ b/lldb/include/lldb/Interpreter/OptionValue.h
@@ -72,7 +72,7 @@ class OptionValue {
   virtual ~OptionValue() = default;
 
   OptionValue(const OptionValue &other);
-  
+
   OptionValue& operator=(const OptionValue &other);
 
   // Subclasses should override these functions
@@ -330,6 +330,10 @@ class OptionValue {
 
   bool SetValueAs(ArchSpec v) { return SetArchSpecValue(v); }
 
+  bool SetValueAs(const FormatEntity::Entry &v) {
+    return SetFormatEntityValue(v);
+  }
+
   template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true>
   bool SetValueAs(T t) {
     return SetEnumerationValue(t);
@@ -387,8 +391,10 @@ class OptionValue {
   bool SetUUIDValue(const UUID &uuid);
 
   const FormatEntity::Entry *GetFormatEntity() const;
+  bool SetFormatEntityValue(const FormatEntity::Entry &entry);
+
   const RegularExpression *GetRegexValue() const;
-  
+
   mutable std::mutex m_mutex;
 };
 
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index 51029f91eb12d..bfec1fa64ea52 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -484,6 +484,11 @@ const FormatEntity::Entry *Debugger::GetStatuslineFormat() const {
   return GetPropertyAtIndexAs<const FormatEntity::Entry *>(idx);
 }
 
+bool Debugger::SetStatuslineFormat(const FormatEntity::Entry &format) {
+  constexpr uint32_t idx = ePropertyStatuslineFormat;
+  return SetPropertyAtIndex(idx, format);
+}
+
 bool Debugger::GetUseAutosuggestion() const {
   const uint32_t idx = ePropertyShowAutosuggestion;
   return GetPropertyAtIndexAs<bool>(
diff --git a/lldb/source/Interpreter/OptionValue.cpp b/lldb/source/Interpreter/OptionValue.cpp
index b95f4fec33949..28bc57a07ac71 100644
--- a/lldb/source/Interpreter/OptionValue.cpp
+++ b/lldb/source/Interpreter/OptionValue.cpp
@@ -474,6 +474,15 @@ bool OptionValue::SetArchSpecValue(ArchSpec arch_spec) {
   return false;
 }
 
+bool OptionValue::SetFormatEntityValue(const FormatEntity::Entry &entry) {
+  std::lock_guard<std::mutex> lock(m_mutex);
+  if (OptionValueFormatEntity *option_value = GetAsFormatEntity()) {
+    option_value->SetCurrentValue(entry);
+    return true;
+  }
+  return false;
+}
+
 const char *OptionValue::GetBuiltinTypeAsCString(Type t) {
   switch (t) {
   case eTypeInvalid:
diff --git a/lldb/unittests/Core/CMakeLists.txt b/lldb/unittests/Core/CMakeLists.txt
index 8580f5887ea2b..dc9c0577aa546 100644
--- a/lldb/unittests/Core/CMakeLists.txt
+++ b/lldb/unittests/Core/CMakeLists.txt
@@ -1,5 +1,6 @@
 
 add_lldb_unittest(LLDBCoreTests
+  DebuggerTest.cpp
   CommunicationTest.cpp
   DiagnosticEventTest.cpp
   DumpDataExtractorTest.cpp
diff --git a/lldb/unittests/Core/DebuggerTest.cpp b/lldb/unittests/Core/DebuggerTest.cpp
new file mode 100644
index 0000000000000..df7d999788553
--- /dev/null
+++ b/lldb/unittests/Core/DebuggerTest.cpp
@@ -0,0 +1,52 @@
+//===-- DebuggerTest.cpp --------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Core/Debugger.h"
+#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
+#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "gtest/gtest.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+namespace {
+class DebuggerTest : public ::testing::Test {
+public:
+  void SetUp() override {
+    FileSystem::Initialize();
+    HostInfo::Initialize();
+    PlatformMacOSX::Initialize();
+    std::call_once(TestUtilities::g_debugger_initialize_flag,
+                   []() { Debugger::Initialize(nullptr); });
+    ArchSpec arch("x86_64-apple-macosx-");
+    Platform::SetHostPlatform(
+        PlatformRemoteMacOSX::CreateInstance(true, &arch));
+  }
+  void TearDown() override {
+    PlatformMacOSX::Terminate();
+    HostInfo::Terminate();
+    FileSystem::Terminate();
+  }
+};
+} // namespace
+
+TEST_F(DebuggerTest, TestSettings) {
+  DebuggerSP debugger_sp = Debugger::CreateInstance();
+
+  EXPECT_TRUE(debugger_sp->SetUseColor(true));
+  EXPECT_TRUE(debugger_sp->GetUseColor());
+
+  FormatEntity::Entry format("foo");
+  EXPECT_TRUE(debugger_sp->SetStatuslineFormat(format));
+  EXPECT_EQ(debugger_sp->GetStatuslineFormat()->string, "foo");
+
+  Debugger::Destroy(debugger_sp);
+}

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

@JDevlieghere JDevlieghere merged commit 2b984fd into llvm:main Apr 10, 2025
12 checks passed
@JDevlieghere JDevlieghere deleted the set-statusline-format branch April 10, 2025 22:36
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Apr 11, 2025
…lvm#135250)

Support programmatically setting the statusline format. I want to use
this API downstream, to change the statusline format for the Swift REPL.

(cherry picked from commit 2b984fd)
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…lvm#135250)

Support programmatically setting the statusline format. I want to use
this API downstream, to change the statusline format for the Swift REPL.
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.

3 participants