[Lldb-commits] [lldb] [lldb] Fix a quirk in SBValue::GetDescription (PR #75793)

via lldb-commits lldb-commits at lists.llvm.org
Mon Dec 18 05:44:14 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Pavel Labath (labath)

<details>
<summary>Changes</summary>

The function was using the default version of ValueObject::Dump, which has a default of using the synthetic-ness of the top-level value for determining whether to print _all_ values as synthetic. This resulted in some unusual behavior, where e.g. a std::vector is stringified as synthetic if its dumped as the top level object, but in its raw form if it is a member of a struct without a pretty printer.

The SBValue class already has properties which determine whether one should be looking at the synthetic view of the object (and also whether to use dynamic types), so it seems more natural to use that.

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


4 Files Affected:

- (modified) lldb/source/API/SBValue.cpp (+8-3) 
- (added) lldb/test/API/python_api/sbvalue_synthetic/Makefile (+3) 
- (added) lldb/test/API/python_api/sbvalue_synthetic/TestSBValueSynthetic.py (+19) 
- (added) lldb/test/API/python_api/sbvalue_synthetic/main.cpp (+11) 


``````````diff
diff --git a/lldb/source/API/SBValue.cpp b/lldb/source/API/SBValue.cpp
index 34d01d759ba55a..89d26a1fbe2824 100644
--- a/lldb/source/API/SBValue.cpp
+++ b/lldb/source/API/SBValue.cpp
@@ -24,6 +24,7 @@
 #include "lldb/Core/ValueObject.h"
 #include "lldb/Core/ValueObjectConstResult.h"
 #include "lldb/DataFormatters/DataVisualization.h"
+#include "lldb/DataFormatters/DumpValueObjectOptions.h"
 #include "lldb/Symbol/Block.h"
 #include "lldb/Symbol/ObjectFile.h"
 #include "lldb/Symbol/Type.h"
@@ -1209,10 +1210,14 @@ bool SBValue::GetDescription(SBStream &description) {
 
   ValueLocker locker;
   lldb::ValueObjectSP value_sp(GetSP(locker));
-  if (value_sp)
-    value_sp->Dump(strm);
-  else
+  if (value_sp) {
+    DumpValueObjectOptions options;
+    options.SetUseDynamicType(m_opaque_sp->GetUseDynamic());
+    options.SetUseSyntheticValue(m_opaque_sp->GetUseSynthetic());
+    value_sp->Dump(strm, options);
+  } else {
     strm.PutCString("No value");
+  }
 
   return true;
 }
diff --git a/lldb/test/API/python_api/sbvalue_synthetic/Makefile b/lldb/test/API/python_api/sbvalue_synthetic/Makefile
new file mode 100644
index 00000000000000..99998b20bcb050
--- /dev/null
+++ b/lldb/test/API/python_api/sbvalue_synthetic/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
diff --git a/lldb/test/API/python_api/sbvalue_synthetic/TestSBValueSynthetic.py b/lldb/test/API/python_api/sbvalue_synthetic/TestSBValueSynthetic.py
new file mode 100644
index 00000000000000..5dcf3c1a9c6c44
--- /dev/null
+++ b/lldb/test/API/python_api/sbvalue_synthetic/TestSBValueSynthetic.py
@@ -0,0 +1,19 @@
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class TestSBValueSynthetic(TestBase):
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def test_str(self):
+        self.build()
+        lldbutil.run_to_source_breakpoint(
+            self, "break here", lldb.SBFileSpec("main.cpp")
+        )
+
+        vector = self.frame().FindVariable("vector")
+        has_vector = self.frame().FindVariable("has_vector")
+        self.expect(str(vector), exe=False, substrs=["42", "47"])
+        self.expect(str(has_vector), exe=False, substrs=["42", "47"])
diff --git a/lldb/test/API/python_api/sbvalue_synthetic/main.cpp b/lldb/test/API/python_api/sbvalue_synthetic/main.cpp
new file mode 100644
index 00000000000000..e6b6ec50f307f8
--- /dev/null
+++ b/lldb/test/API/python_api/sbvalue_synthetic/main.cpp
@@ -0,0 +1,11 @@
+#include <vector>
+
+struct HasVector {
+  std::vector<int> v;
+};
+
+int main() {
+  std::vector<int> vector = {42, 47};
+  HasVector has_vector = {vector};
+  return 0; // break here
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/75793


More information about the lldb-commits mailing list