[Lldb-commits] [lldb] [LLDB][SBProgress] Fix bad optional in sbprogress (PR #128971)

Jacob Lalonde via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 27 09:28:07 PST 2025


https://github.com/Jlalond updated https://github.com/llvm/llvm-project/pull/128971

>From ac90ec73ccfb02923ff0343189d2efaeb6108fa3 Mon Sep 17 00:00:00 2001
From: Jacob Lalonde <jalalonde at fb.com>
Date: Wed, 26 Feb 2025 15:48:14 -0800
Subject: [PATCH 1/4] Fix bad optional access in sbprogress

---
 lldb/source/API/SBProgress.cpp                       |  5 ++++-
 .../test/API/python_api/sbprogress/TestSBProgress.py | 12 ++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lldb/source/API/SBProgress.cpp b/lldb/source/API/SBProgress.cpp
index e67e289a60eff..d40e11da973d4 100644
--- a/lldb/source/API/SBProgress.cpp
+++ b/lldb/source/API/SBProgress.cpp
@@ -40,7 +40,10 @@ SBProgress::~SBProgress() = default;
 void SBProgress::Increment(uint64_t amount, const char *description) {
   LLDB_INSTRUMENT_VA(amount, description);
 
-  m_opaque_up->Increment(amount, description);
+  std::optional<std::string> description_opt;
+  if (description && description[0])
+    description_opt = description;
+  m_opaque_up->Increment(amount, description_opt);
 }
 
 lldb_private::Progress &SBProgress::ref() const { return *m_opaque_up; }
diff --git a/lldb/test/API/python_api/sbprogress/TestSBProgress.py b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
index c456247da80c6..5f7820a5bd81e 100644
--- a/lldb/test/API/python_api/sbprogress/TestSBProgress.py
+++ b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
@@ -33,3 +33,15 @@ def test_without_external_bit_set(self):
         expected_string = "Test progress first increment"
         progress.Increment(1, expected_string)
         self.assertFalse(listener.PeekAtNextEvent(event))
+
+    def test_with_external_bit_set(self):
+        """Test SBProgress can handle null events."""
+
+        progress = lldb.SBProgress("Test SBProgress", "Test progress", self.dbg)
+        listener = lldb.SBListener("Test listener")
+        broadcaster = self.dbg.GetBroadcaster()
+        broadcaster.AddListener(listener, lldb.eBroadcastBitExternalProgress)
+        event = lldb.SBEvent()
+
+        progress.Increment(1, None)
+        self.assertTrue(listener.PeekAtNextEvent(event))

>From 4f1aaa6a2cf81da53db634a69155ef1e8aa29c6c Mon Sep 17 00:00:00 2001
From: Jacob Lalonde <jalalonde at fb.com>
Date: Wed, 26 Feb 2025 16:18:11 -0800
Subject: [PATCH 2/4] Expand test case based on Dave's feedback

---
 lldb/test/API/python_api/sbprogress/TestSBProgress.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/lldb/test/API/python_api/sbprogress/TestSBProgress.py b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
index 5f7820a5bd81e..eab5e5bb18cf0 100644
--- a/lldb/test/API/python_api/sbprogress/TestSBProgress.py
+++ b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
@@ -37,11 +37,18 @@ def test_without_external_bit_set(self):
     def test_with_external_bit_set(self):
         """Test SBProgress can handle null events."""
 
-        progress = lldb.SBProgress("Test SBProgress", "Test progress", self.dbg)
+        progress = lldb.SBProgress("Test SBProgress", "Test progress", 3, self.dbg)
         listener = lldb.SBListener("Test listener")
         broadcaster = self.dbg.GetBroadcaster()
         broadcaster.AddListener(listener, lldb.eBroadcastBitExternalProgress)
         event = lldb.SBEvent()
 
         progress.Increment(1, None)
-        self.assertTrue(listener.PeekAtNextEvent(event))
+        self.assertTrue(listener.GetNextEvent(event))
+        progress.Increment(1, "")
+        self.assertTrue(listener.GetNextEvent(event))
+        progress.Increment(1, "Step 3")
+        self.assertTrue(listener.GetNextEvent(event))
+        stream = lldb.SBStream()
+        event.GetDescription(stream)
+        self.assertIn("Step 3", stream.GetData())

>From 6e9ce7e7e55924e93231799e03c4ea3cc45c1810 Mon Sep 17 00:00:00 2001
From: Jacob Lalonde <jalalonde at fb.com>
Date: Wed, 26 Feb 2025 16:35:55 -0800
Subject: [PATCH 3/4] Further asserts based on Greg's feedback

---
 .../API/python_api/sbprogress/TestSBProgress.py   | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/lldb/test/API/python_api/sbprogress/TestSBProgress.py b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
index eab5e5bb18cf0..1b8f01d3e8bb1 100644
--- a/lldb/test/API/python_api/sbprogress/TestSBProgress.py
+++ b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
@@ -42,11 +42,24 @@ def test_with_external_bit_set(self):
         broadcaster = self.dbg.GetBroadcaster()
         broadcaster.AddListener(listener, lldb.eBroadcastBitExternalProgress)
         event = lldb.SBEvent()
-
+        # Sample JSON we're expecting:
+        # { id = 2, title = "Test SBProgress", details = "Test progress", type = update, progress = 1 of 3}
+        # details remains the same as specified in the constructor of the progress
+        # until we update it in the increment function, so we check for the Null and empty string case
+        # that details hasn't changed, but progress x of 3 has.
         progress.Increment(1, None)
         self.assertTrue(listener.GetNextEvent(event))
+        stream = lldb.SBStream()
+        event.GetDescription(stream)
+        self.assertIn("Test progress", stream.GetData())
+        self.assertIn("1 of 3", stream.GetData())
+
         progress.Increment(1, "")
         self.assertTrue(listener.GetNextEvent(event))
+        event.GetDescription(stream)
+        self.assertIn("Test progress", stream.GetData())
+        self.assertIn("2 of 3", stream.GetData())
+
         progress.Increment(1, "Step 3")
         self.assertTrue(listener.GetNextEvent(event))
         stream = lldb.SBStream()

>From eea877a90228dd9b46136176e204e5503eb14155 Mon Sep 17 00:00:00 2001
From: Jacob Lalonde <jalalonde at fb.com>
Date: Thu, 27 Feb 2025 09:27:54 -0800
Subject: [PATCH 4/4] Move the optional

---
 lldb/source/API/SBProgress.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/source/API/SBProgress.cpp b/lldb/source/API/SBProgress.cpp
index d40e11da973d4..3e548854ab739 100644
--- a/lldb/source/API/SBProgress.cpp
+++ b/lldb/source/API/SBProgress.cpp
@@ -43,7 +43,7 @@ void SBProgress::Increment(uint64_t amount, const char *description) {
   std::optional<std::string> description_opt;
   if (description && description[0])
     description_opt = description;
-  m_opaque_up->Increment(amount, description_opt);
+  m_opaque_up->Increment(amount, std::move(description_opt));
 }
 
 lldb_private::Progress &SBProgress::ref() const { return *m_opaque_up; }



More information about the lldb-commits mailing list