[Lldb-commits] [lldb] [lldb/API] Fix SBStructuredData JSON Array parsing (PR #101929)

via lldb-commits lldb-commits at lists.llvm.org
Mon Aug 5 00:40:59 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Med Ismail Bennani (medismailben)

<details>
<summary>Changes</summary>

This patch loosen the parsing requirement to allow parsing not only JSON dictionaries but also JSON arrays.

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


2 Files Affected:

- (modified) lldb/source/API/SBStructuredData.cpp (+6-2) 
- (modified) lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py (+18) 


``````````diff
diff --git a/lldb/source/API/SBStructuredData.cpp b/lldb/source/API/SBStructuredData.cpp
index b18fc5655fc81..32fa5d7bb30f9 100644
--- a/lldb/source/API/SBStructuredData.cpp
+++ b/lldb/source/API/SBStructuredData.cpp
@@ -86,7 +86,11 @@ lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
       StructuredData::ParseJSON(stream.GetData());
   m_impl_up->SetObjectSP(json_obj);
 
-  if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
+  static constexpr StructuredDataType structured_data_record_type[] = {
+      eStructuredDataTypeArray, eStructuredDataTypeDictionary};
+
+  if (!json_obj ||
+      !llvm::is_contained(structured_data_record_type, json_obj->GetType()))
     error.SetErrorString("Invalid Syntax");
   return error;
 }
@@ -106,7 +110,7 @@ bool SBStructuredData::IsValid() const {
 SBStructuredData::operator bool() const {
   LLDB_INSTRUMENT_VA(this);
 
-  return m_impl_up->IsValid();
+  return m_impl_up && m_impl_up->IsValid();
 }
 
 void SBStructuredData::Clear() {
diff --git a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
index b3db3bc61e4dc..c0ce482860c85 100644
--- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -110,6 +110,24 @@ class MyRandomClass:
         self.assertTrue(my_random_class)
         self.assertEqual(my_random_class.payload, MyRandomClass.payload)
 
+        example_arr = [1, 2.3, "4", {"5": False}]
+        arr_str = json.dumps(example_arr)
+        s.Clear()
+        s.Print(arr_str)
+        example = lldb.SBStructuredData()
+
+        # Check SetFromJSON API for dictionaries, integers, floating point
+        # values, strings and arrays
+        error = example.SetFromJSON(s)
+        if not error.Success():
+            self.fail("FAILED:   " + error.GetCString())
+
+        s.Clear()
+        self.assertSuccess(example.GetAsJSON(s))
+        sb_data = json.loads(s.GetData())
+        self.assertEqual(sb_data, example_arr)
+
+
     def invalid_struct_test(self, example):
         invalid_struct = lldb.SBStructuredData()
         invalid_struct = example.GetValueForKey("invalid_key")

``````````

</details>


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


More information about the lldb-commits mailing list