mib updated this revision to Diff 466624.
mib added a comment.

Address @JDevlieghere comment:

- Add unittest


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135547/new/

https://reviews.llvm.org/D135547

Files:
  lldb/include/lldb/Core/StructuredDataImpl.h
  lldb/include/lldb/Utility/StructuredData.h
  lldb/source/Commands/CommandObjectProcess.cpp
  lldb/source/Utility/StructuredData.cpp
  lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
  lldb/unittests/Utility/CMakeLists.txt
  lldb/unittests/Utility/StructuredDataTest.cpp

Index: lldb/unittests/Utility/StructuredDataTest.cpp
===================================================================
--- lldb/unittests/Utility/StructuredDataTest.cpp
+++ lldb/unittests/Utility/StructuredDataTest.cpp
@@ -31,6 +31,22 @@
   }
 }
 
+TEST(StructuredDataTest, GetDescription) {
+  Status status;
+  std::string input = GetInputFilePath("StructuredData-full.json");
+  auto object_sp = StructuredData::ParseJSONFromFile(FileSpec(input), status);
+  ASSERT_NE(nullptr, object_sp);
+
+  const std::string expected =
+      "  Array :\n    3.140000\n    1.234000  \n  Dictionary :\n    FalseBool "
+      ": False  \n  Integer : 1\n  Null : NULL\n  String : value\n  TrueBool : "
+      "True";
+
+  StreamString S;
+  object_sp->GetDescription(S);
+  EXPECT_EQ(expected, S.GetString());
+}
+
 TEST(StructuredDataTest, ParseJSONFromFile) {
   Status status;
   auto object_sp = StructuredData::ParseJSONFromFile(
Index: lldb/unittests/Utility/CMakeLists.txt
===================================================================
--- lldb/unittests/Utility/CMakeLists.txt
+++ lldb/unittests/Utility/CMakeLists.txt
@@ -54,6 +54,9 @@
     Support
   )
 
-add_unittest_inputs(UtilityTests
+set(test_inputs
   StructuredData-basic.json
+  StructuredData-full.json
   )
+
+add_unittest_inputs(UtilityTests "${test_inputs}")
Index: lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
===================================================================
--- lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
+++ lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py
@@ -37,7 +37,7 @@
                     patterns=["Process .* launched: .*a.out"])
 
         self.expect('process status --verbose',
-                    patterns=["\"message\".*pointer being freed was not allocated"])
+                    patterns=["message: .*pointer being freed was not allocated"])
 
 
     @skipIfAsan # The test process intentionally hits a memory bug.
Index: lldb/source/Utility/StructuredData.cpp
===================================================================
--- lldb/source/Utility/StructuredData.cpp
+++ lldb/source/Utility/StructuredData.cpp
@@ -175,3 +175,62 @@
 void StructuredData::Generic::Serialize(json::OStream &s) const {
   s.value(llvm::formatv("{0:X}", m_object));
 }
+
+void StructuredData::Array::GetDescription(lldb_private::Stream &s) const {
+  s.IndentMore();
+  for (const auto &item_sp : m_items) {
+    s.Indent();
+    item_sp->GetDescription(s);
+    if (item_sp != *(--m_items.end()))
+      s.EOL();
+  }
+  s.IndentLess();
+  s.Indent();
+}
+
+void StructuredData::Integer::GetDescription(lldb_private::Stream &s) const {
+  s.Printf("%" PRId64, static_cast<int64_t>(m_value));
+}
+
+void StructuredData::Float::GetDescription(lldb_private::Stream &s) const {
+  s.Printf("%f", m_value);
+}
+
+void StructuredData::Boolean::GetDescription(lldb_private::Stream &s) const {
+  s.Printf(m_value ? "True" : "False");
+}
+
+void StructuredData::String::GetDescription(lldb_private::Stream &s) const {
+  s.Printf("%s", m_value.c_str());
+}
+
+void StructuredData::Dictionary::GetDescription(lldb_private::Stream &s) const {
+  s.IndentMore();
+
+  for (const auto &pair : m_dict) {
+    if (pair.first.IsNull() || pair.first.IsEmpty() || !pair.second)
+      continue;
+    s.Indent();
+    s.Printf("%s :", pair.first.AsCString());
+    auto value_type = pair.second->GetType();
+    if (value_type == lldb::eStructuredDataTypeArray ||
+        value_type == lldb::eStructuredDataTypeDictionary)
+      s.EOL();
+    else
+      s.PutChar(' ');
+    pair.second->GetDescription(s);
+    if (pair != *(--m_dict.end()))
+      s.EOL();
+  }
+
+  s.IndentLess();
+  s.Indent();
+}
+
+void StructuredData::Null::GetDescription(lldb_private::Stream &s) const {
+  s.Printf("NULL");
+}
+
+void StructuredData::Generic::GetDescription(lldb_private::Stream &s) const {
+  s.Printf("%p", m_object);
+}
Index: lldb/source/Commands/CommandObjectProcess.cpp
===================================================================
--- lldb/source/Commands/CommandObjectProcess.cpp
+++ lldb/source/Commands/CommandObjectProcess.cpp
@@ -1537,8 +1537,9 @@
       StructuredData::DictionarySP crash_info_sp = *expected_crash_info;
 
       if (crash_info_sp) {
+        strm.EOL();
         strm.PutCString("Extended Crash Information:\n");
-        crash_info_sp->Dump(strm);
+        crash_info_sp->GetDescription(strm);
       }
     }
 
Index: lldb/include/lldb/Utility/StructuredData.h
===================================================================
--- lldb/include/lldb/Utility/StructuredData.h
+++ lldb/include/lldb/Utility/StructuredData.h
@@ -158,6 +158,12 @@
       Serialize(jso);
     }
 
+    virtual void GetDescription(lldb_private::Stream &s) const {
+      s.IndentMore();
+      Dump(s, false);
+      s.IndentLess();
+    }
+
   private:
     lldb::StructuredDataType m_type;
   };
@@ -277,6 +283,8 @@
 
     void Serialize(llvm::json::OStream &s) const override;
 
+    void GetDescription(lldb_private::Stream &s) const override;
+
   protected:
     typedef std::vector<ObjectSP> collection;
     collection m_items;
@@ -295,6 +303,8 @@
 
     void Serialize(llvm::json::OStream &s) const override;
 
+    void GetDescription(lldb_private::Stream &s) const override;
+
   protected:
     uint64_t m_value;
   };
@@ -312,6 +322,8 @@
 
     void Serialize(llvm::json::OStream &s) const override;
 
+    void GetDescription(lldb_private::Stream &s) const override;
+
   protected:
     double m_value;
   };
@@ -329,6 +341,8 @@
 
     void Serialize(llvm::json::OStream &s) const override;
 
+    void GetDescription(lldb_private::Stream &s) const override;
+
   protected:
     bool m_value;
   };
@@ -345,6 +359,8 @@
 
     void Serialize(llvm::json::OStream &s) const override;
 
+    void GetDescription(lldb_private::Stream &s) const override;
+
   protected:
     std::string m_value;
   };
@@ -524,6 +540,8 @@
 
     void Serialize(llvm::json::OStream &s) const override;
 
+    void GetDescription(lldb_private::Stream &s) const override;
+
   protected:
     typedef std::map<ConstString, ObjectSP> collection;
     collection m_dict;
@@ -538,6 +556,8 @@
     bool IsValid() const override { return false; }
 
     void Serialize(llvm::json::OStream &s) const override;
+
+    void GetDescription(lldb_private::Stream &s) const override;
   };
 
   class Generic : public Object {
@@ -553,6 +573,8 @@
 
     void Serialize(llvm::json::OStream &s) const override;
 
+    void GetDescription(lldb_private::Stream &s) const override;
+
   private:
     void *m_object;
   };
Index: lldb/include/lldb/Core/StructuredDataImpl.h
===================================================================
--- lldb/include/lldb/Core/StructuredDataImpl.h
+++ lldb/include/lldb/Core/StructuredDataImpl.h
@@ -80,7 +80,7 @@
         error.SetErrorString("No data to describe.");
         return error;
       }
-      m_data_sp->Dump(stream, true);
+      m_data_sp->GetDescription(stream);
       return error;
     }
     // Get the data's description.
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to