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

>From 3c4b333869c17005deb8d4e9307babc94d0dc9b5 Mon Sep 17 00:00:00 2001
From: Jacob Lalonde <jalalo...@fb.com>
Date: Wed, 26 Feb 2025 15:38:24 -0800
Subject: [PATCH 1/3] Add a finalize method to the sbprogress, with an
 associated doc string and a test

---
 lldb/bindings/interface/SBProgressDocstrings.i    |  7 +++++++
 lldb/include/lldb/API/SBProgress.h                |  5 +++++
 lldb/source/API/SBProgress.cpp                    | 15 ++++++++++++++-
 .../API/python_api/sbprogress/TestSBProgress.py   | 14 ++++++++++++++
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/lldb/bindings/interface/SBProgressDocstrings.i 
b/lldb/bindings/interface/SBProgressDocstrings.i
index 2997fe619fcc7..50648b62411f8 100644
--- a/lldb/bindings/interface/SBProgressDocstrings.i
+++ b/lldb/bindings/interface/SBProgressDocstrings.i
@@ -12,3 +12,10 @@ and will always send an initial progress update, updates when
 Progress::Increment() is called, and also will make sure that a progress
 completed update is reported even if the user doesn't explicitly cause one
 to be sent.") lldb::SBProgress;
+
+%feature("docstring",
+"Explicitly end an SBProgress, this is a utility to send the progressEnd event
+without waiting for your language or language implementations 
non-deterministic destruction
+of the SBProgress object.
+
+Note once finalized, no further increments will be processed.") 
lldb::SBProgress::Finalize;
diff --git a/lldb/include/lldb/API/SBProgress.h 
b/lldb/include/lldb/API/SBProgress.h
index d574d1d2982b9..1558cdc08e080 100644
--- a/lldb/include/lldb/API/SBProgress.h
+++ b/lldb/include/lldb/API/SBProgress.h
@@ -59,6 +59,11 @@ class LLDB_API SBProgress {
 
   void Increment(uint64_t amount, const char *description = nullptr);
 
+  /// Explicitly finalize an SBProgress, this can be used to terminate a
+  /// progress on command instead of waiting for a garbage collection or other
+  /// finalizer.
+  void Finalize();
+
 protected:
   lldb_private::Progress &ref() const;
 
diff --git a/lldb/source/API/SBProgress.cpp b/lldb/source/API/SBProgress.cpp
index e67e289a60eff..4991d55ad0248 100644
--- a/lldb/source/API/SBProgress.cpp
+++ b/lldb/source/API/SBProgress.cpp
@@ -40,7 +40,20 @@ SBProgress::~SBProgress() = default;
 void SBProgress::Increment(uint64_t amount, const char *description) {
   LLDB_INSTRUMENT_VA(amount, description);
 
-  m_opaque_up->Increment(amount, description);
+  if (!m_opaque_up)
+    return;
+
+  std::optional<std::string> description_opt;
+  if (description && description[0])
+    description_opt = description;
+  m_opaque_up->Increment(amount, description_opt);
+}
+
+void SBProgress::Finalize() {
+  if (!m_opaque_up)
+    return;
+
+  m_opaque_up.reset();
 }
 
 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..dd7e4b6f2ac5c 100644
--- a/lldb/test/API/python_api/sbprogress/TestSBProgress.py
+++ b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
@@ -33,3 +33,17 @@ 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_progress_finalize(self):
+        """Test SBProgress finalize sends the progressEnd event"""
+
+        progress = lldb.SBProgress("Test SBProgress", "Test finalize", 
self.dbg)
+        listener = lldb.SBListener("Test listener")
+        broadcaster = self.dbg.GetBroadcaster()
+        broadcaster.AddListener(listener, 
lldb.eBroadcastBitExternalProgressCategory)
+        event = lldb.SBEvent()
+        progress.Finalize()
+        self.assertTrue(listener.WaitForEvent(5, event))
+        stream = lldb.SBStream()
+        event.GetDescription(stream)
+        self.assertIn("type = end", stream.GetData())

>From eb5d36f37b3a990e52350243b45e3907e1362ff5 Mon Sep 17 00:00:00 2001
From: Jacob Lalonde <jalalo...@fb.com>
Date: Wed, 26 Feb 2025 15:54:52 -0800
Subject: [PATCH 2/3] Drop the bug fix now that I opened 128971

---
 lldb/source/API/SBProgress.cpp | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/lldb/source/API/SBProgress.cpp b/lldb/source/API/SBProgress.cpp
index 4991d55ad0248..240b6e51eb9b9 100644
--- a/lldb/source/API/SBProgress.cpp
+++ b/lldb/source/API/SBProgress.cpp
@@ -43,10 +43,7 @@ void SBProgress::Increment(uint64_t amount, const char 
*description) {
   if (!m_opaque_up)
     return;
 
-  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, description);
 }
 
 void SBProgress::Finalize() {

>From d43f4710a4ebdfe01335603c2850aeed325221dd Mon Sep 17 00:00:00 2001
From: Jacob Lalonde <jalalo...@fb.com>
Date: Wed, 26 Feb 2025 16:44:29 -0800
Subject: [PATCH 3/3] Add a split test case of progress with a total and
 without a total ending on finalize

---
 .../python_api/sbprogress/TestSBProgress.py    | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lldb/test/API/python_api/sbprogress/TestSBProgress.py 
b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
index dd7e4b6f2ac5c..8fbe198ff9b5d 100644
--- a/lldb/test/API/python_api/sbprogress/TestSBProgress.py
+++ b/lldb/test/API/python_api/sbprogress/TestSBProgress.py
@@ -34,7 +34,7 @@ def test_without_external_bit_set(self):
         progress.Increment(1, expected_string)
         self.assertFalse(listener.PeekAtNextEvent(event))
 
-    def test_progress_finalize(self):
+    def test_progress_finalize_non_deterministic_progress(self):
         """Test SBProgress finalize sends the progressEnd event"""
 
         progress = lldb.SBProgress("Test SBProgress", "Test finalize", 
self.dbg)
@@ -47,3 +47,19 @@ def test_progress_finalize(self):
         stream = lldb.SBStream()
         event.GetDescription(stream)
         self.assertIn("type = end", stream.GetData())
+
+    def test_progress_finalize_deterministic_progress(self):
+        """Test SBProgress finalize sends the progressEnd event"""
+
+        progress = lldb.SBProgress("Test SBProgress", "Test finalize", 13, 
self.dbg)
+        listener = lldb.SBListener("Test listener")
+        broadcaster = self.dbg.GetBroadcaster()
+        broadcaster.AddListener(listener, 
lldb.eBroadcastBitExternalProgressCategory)
+        event = lldb.SBEvent()
+        progress.Finalize()
+        self.assertTrue(listener.WaitForEvent(5, event))
+        stream = lldb.SBStream()
+        event.GetDescription(stream)
+        # Note even for progresses with a total, the total isn't
+        # sent in the end message.
+        self.assertIn("type = end", stream.GetData())

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to