[Lldb-commits] [lldb] r304138 - Added new API to SBStructuredData class

Abhishek Aggarwal via lldb-commits lldb-commits at lists.llvm.org
Mon May 29 01:25:46 PDT 2017


Author: abhishek
Date: Mon May 29 03:25:46 2017
New Revision: 304138

URL: http://llvm.org/viewvc/llvm-project?rev=304138&view=rev
Log:
Added new API to SBStructuredData class

Summary:
 - Added API to access data types
    -- integer, double, array, string, boolean and dictionary data types
    -- Earlier user had to parse through the string output to get these
       values

 - Added Test cases for API testing

 - Added new StructuredDataType enum in public include file
   -- Replaced locally-defined enum in StructuredData.h with this new
      one       
   -- Modified other internal files using this locally-defined enum

Signed-off-by: Abhishek Aggarwal <abhishek.a.aggarwal at intel.com>

Reviewers: clayborg, lldb-commits

Reviewed By: clayborg

Subscribers: labath

Differential Revision: https://reviews.llvm.org/D33434

Added:
    lldb/trunk/packages/Python/lldbsuite/test/python_api/sbstructureddata/
    lldb/trunk/packages/Python/lldbsuite/test/python_api/sbstructureddata/TestStructuredDataAPI.py
Modified:
    lldb/trunk/include/lldb/API/SBStructuredData.h
    lldb/trunk/include/lldb/Core/StructuredData.h
    lldb/trunk/include/lldb/Core/StructuredDataImpl.h
    lldb/trunk/include/lldb/lldb-enumerations.h
    lldb/trunk/scripts/interface/SBStructuredData.i
    lldb/trunk/source/API/SBStructuredData.cpp
    lldb/trunk/source/API/SBThread.cpp
    lldb/trunk/source/Core/FormatEntity.cpp
    lldb/trunk/source/Core/StructuredData.cpp
    lldb/trunk/source/Target/Thread.cpp
    lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Modified: lldb/trunk/include/lldb/API/SBStructuredData.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBStructuredData.h?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBStructuredData.h (original)
+++ lldb/trunk/include/lldb/API/SBStructuredData.h Mon May 29 03:25:46 2017
@@ -37,11 +37,70 @@ public:
 
   lldb::SBError GetDescription(lldb::SBStream &stream) const;
 
+  //------------------------------------------------------------------
+  /// Return the type of data in this data structure
+  //------------------------------------------------------------------
+  lldb::StructuredDataType GetType() const;
+
+  //------------------------------------------------------------------
+  /// Return the size (i.e. number of elements) in this data structure
+  /// if it is an array or dictionary type. For other types, 0 will be
+  //  returned.
+  //------------------------------------------------------------------
+  size_t GetSize() const;
+
+  //------------------------------------------------------------------
+  /// Return the value corresponding to a key if this data structure
+  /// is a dictionary type.
+  //------------------------------------------------------------------
+  lldb::SBStructuredData GetValueForKey(const char *key) const;
+
+  //------------------------------------------------------------------
+  /// Return the value corresponding to an index if this data structure
+  /// is array.
+  //------------------------------------------------------------------
+  lldb::SBStructuredData GetItemAtIndex(size_t idx) const;
+
+  //------------------------------------------------------------------
+  /// Return the integer value if this data structure is an integer type.
+  //------------------------------------------------------------------
+  uint64_t GetIntegerValue(uint64_t fail_value = 0) const;
+
+  //------------------------------------------------------------------
+  /// Return the floating point value if this data structure is a floating
+  /// type.
+  //------------------------------------------------------------------
+  double GetFloatValue(double fail_value = 0.0) const;
+
+  //------------------------------------------------------------------
+  /// Return the boolean value if this data structure is a boolean type.
+  //------------------------------------------------------------------
+  bool GetBooleanValue(bool fail_value = false) const;
+
+  //------------------------------------------------------------------
+  /// Provides the string value if this data structure is a string type.
+  ///
+  /// @param[out] dst
+  ///     pointer where the string value will be written. In case it is null,
+  ///     nothing will be written at @dst.
+  ///
+  /// @param[in] dst_len
+  ///     max number of characters that can be written at @dst. In case it is
+  ///     zero, nothing will be written at @dst. If this length is not enough
+  ///     to write the complete string value, (dst_len-1) bytes of the string
+  ///     value will be written at @dst followed by a null character.
+  ///
+  /// @return
+  ///     Returns the byte size needed to completely write the string value at
+  ///     @dst in all cases.
+  //------------------------------------------------------------------
+  size_t GetStringValue(char *dst, size_t dst_len) const;
+
 protected:
   friend class SBTraceOptions;
 
   StructuredDataImplUP m_impl_up;
 };
-}
+} // namespace lldb
 
 #endif /* SBStructuredData_h */

Modified: lldb/trunk/include/lldb/Core/StructuredData.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/StructuredData.h?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/StructuredData.h (original)
+++ lldb/trunk/include/lldb/Core/StructuredData.h Mon May 29 03:25:46 2017
@@ -13,7 +13,8 @@
 #include "llvm/ADT/StringRef.h"
 
 #include "lldb/Utility/ConstString.h"
-#include "lldb/Utility/FileSpec.h" // for FileSpec
+#include "lldb/Utility/FileSpec.h"  // for FileSpec
+#include "lldb/lldb-enumerations.h" // for StructuredDataType
 
 #include <functional>
 #include <map>
@@ -71,46 +72,38 @@ public:
   typedef std::shared_ptr<Dictionary> DictionarySP;
   typedef std::shared_ptr<Generic> GenericSP;
 
-  enum class Type {
-    eTypeInvalid = -1,
-    eTypeNull = 0,
-    eTypeGeneric,
-    eTypeArray,
-    eTypeInteger,
-    eTypeFloat,
-    eTypeBoolean,
-    eTypeString,
-    eTypeDictionary
-  };
-
   class Object : public std::enable_shared_from_this<Object> {
   public:
-    Object(Type t = Type::eTypeInvalid) : m_type(t) {}
+    Object(lldb::StructuredDataType t = lldb::eStructuredDataTypeInvalid)
+        : m_type(t) {}
 
     virtual ~Object() = default;
 
     virtual bool IsValid() const { return true; }
 
-    virtual void Clear() { m_type = Type::eTypeInvalid; }
+    virtual void Clear() { m_type = lldb::eStructuredDataTypeInvalid; }
 
-    Type GetType() const { return m_type; }
+    lldb::StructuredDataType GetType() const { return m_type; }
 
-    void SetType(Type t) { m_type = t; }
+    void SetType(lldb::StructuredDataType t) { m_type = t; }
 
     Array *GetAsArray() {
-      return ((m_type == Type::eTypeArray) ? static_cast<Array *>(this)
-                                           : nullptr);
+      return ((m_type == lldb::eStructuredDataTypeArray)
+                  ? static_cast<Array *>(this)
+                  : nullptr);
     }
 
     Dictionary *GetAsDictionary() {
-      return ((m_type == Type::eTypeDictionary)
-                  ? static_cast<Dictionary *>(this)
-                  : nullptr);
+      return (
+          (m_type == lldb::eStructuredDataTypeDictionary)
+              ? static_cast<Dictionary *>(this)
+              : nullptr);
     }
 
     Integer *GetAsInteger() {
-      return ((m_type == Type::eTypeInteger) ? static_cast<Integer *>(this)
-                                             : nullptr);
+      return ((m_type == lldb::eStructuredDataTypeInteger)
+                  ? static_cast<Integer *>(this)
+                  : nullptr);
     }
 
     uint64_t GetIntegerValue(uint64_t fail_value = 0) {
@@ -119,8 +112,9 @@ public:
     }
 
     Float *GetAsFloat() {
-      return ((m_type == Type::eTypeFloat) ? static_cast<Float *>(this)
-                                           : nullptr);
+      return ((m_type == lldb::eStructuredDataTypeFloat)
+                  ? static_cast<Float *>(this)
+                  : nullptr);
     }
 
     double GetFloatValue(double fail_value = 0.0) {
@@ -129,8 +123,9 @@ public:
     }
 
     Boolean *GetAsBoolean() {
-      return ((m_type == Type::eTypeBoolean) ? static_cast<Boolean *>(this)
-                                             : nullptr);
+      return ((m_type == lldb::eStructuredDataTypeBoolean)
+                  ? static_cast<Boolean *>(this)
+                  : nullptr);
     }
 
     bool GetBooleanValue(bool fail_value = false) {
@@ -139,8 +134,9 @@ public:
     }
 
     String *GetAsString() {
-      return ((m_type == Type::eTypeString) ? static_cast<String *>(this)
-                                            : nullptr);
+      return ((m_type == lldb::eStructuredDataTypeString)
+                  ? static_cast<String *>(this)
+                  : nullptr);
     }
 
     llvm::StringRef GetStringValue(const char *fail_value = nullptr) {
@@ -152,8 +148,9 @@ public:
     }
 
     Generic *GetAsGeneric() {
-      return ((m_type == Type::eTypeGeneric) ? static_cast<Generic *>(this)
-                                             : nullptr);
+      return ((m_type == lldb::eStructuredDataTypeGeneric)
+                  ? static_cast<Generic *>(this)
+                  : nullptr);
     }
 
     ObjectSP GetObjectForDotSeparatedPath(llvm::StringRef path);
@@ -163,12 +160,12 @@ public:
     virtual void Dump(Stream &s, bool pretty_print = true) const = 0;
 
   private:
-    Type m_type;
+    lldb::StructuredDataType m_type;
   };
 
   class Array : public Object {
   public:
-    Array() : Object(Type::eTypeArray) {}
+    Array() : Object(lldb::eStructuredDataTypeArray) {}
 
     ~Array() override = default;
 
@@ -288,7 +285,8 @@ public:
 
   class Integer : public Object {
   public:
-    Integer(uint64_t i = 0) : Object(Type::eTypeInteger), m_value(i) {}
+    Integer(uint64_t i = 0)
+        : Object(lldb::eStructuredDataTypeInteger), m_value(i) {}
 
     ~Integer() override = default;
 
@@ -304,7 +302,8 @@ public:
 
   class Float : public Object {
   public:
-    Float(double d = 0.0) : Object(Type::eTypeFloat), m_value(d) {}
+    Float(double d = 0.0) : Object(lldb::eStructuredDataTypeFloat),
+          m_value(d) {}
 
     ~Float() override = default;
 
@@ -320,7 +319,8 @@ public:
 
   class Boolean : public Object {
   public:
-    Boolean(bool b = false) : Object(Type::eTypeBoolean), m_value(b) {}
+    Boolean(bool b = false) : Object(lldb::eStructuredDataTypeBoolean),
+          m_value(b) {}
 
     ~Boolean() override = default;
 
@@ -336,9 +336,10 @@ public:
 
   class String : public Object {
   public:
-    String() : Object(Type::eTypeString) {}
+    String() : Object(lldb::eStructuredDataTypeString) {}
     explicit String(llvm::StringRef S)
-        : Object(Type::eTypeString), m_value(S) {}
+        : Object(lldb::eStructuredDataTypeString),
+          m_value(S) {}
 
     void SetValue(llvm::StringRef S) { m_value = S; }
 
@@ -352,7 +353,8 @@ public:
 
   class Dictionary : public Object {
   public:
-    Dictionary() : Object(Type::eTypeDictionary), m_dict() {}
+    Dictionary() : Object(lldb::eStructuredDataTypeDictionary),
+          m_dict() {}
 
     ~Dictionary() override = default;
 
@@ -522,7 +524,7 @@ public:
 
   class Null : public Object {
   public:
-    Null() : Object(Type::eTypeNull) {}
+    Null() : Object(lldb::eStructuredDataTypeNull) {}
 
     ~Null() override = default;
 
@@ -534,7 +536,7 @@ public:
   class Generic : public Object {
   public:
     explicit Generic(void *object = nullptr)
-        : Object(Type::eTypeGeneric), m_object(object) {}
+        : Object(lldb::eStructuredDataTypeGeneric), m_object(object) {}
 
     void SetValue(void *value) { m_object = value; }
 

Modified: lldb/trunk/include/lldb/Core/StructuredDataImpl.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/StructuredDataImpl.h?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/StructuredDataImpl.h (original)
+++ lldb/trunk/include/lldb/Core/StructuredDataImpl.h Mon May 29 03:25:46 2017
@@ -15,7 +15,9 @@
 #include "lldb/Target/StructuredDataPlugin.h"
 #include "lldb/Utility/Status.h"
 #include "lldb/Utility/Stream.h"
+#include "lldb/lldb-enumerations.h"
 #include "lldb/lldb-forward.h"
+#include "llvm/ADT/StringRef.h"
 
 #pragma mark--
 #pragma mark StructuredDataImpl
@@ -78,18 +80,77 @@ public:
     return plugin_sp->GetDescription(m_data_sp, stream);
   }
 
-  StructuredData::ObjectSP GetObjectSP() {
-    return m_data_sp;
+  StructuredData::ObjectSP GetObjectSP() { return m_data_sp; }
+
+  void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
+
+  lldb::StructuredDataType GetType() const {
+    return (m_data_sp ? m_data_sp->GetType() :
+        lldb::eStructuredDataTypeInvalid);
+  }
+
+  size_t GetSize() const {
+    if (!m_data_sp)
+      return 0;
+
+    if (m_data_sp->GetType() == lldb::eStructuredDataTypeDictionary) {
+      auto dict = m_data_sp->GetAsDictionary();
+      return (dict->GetSize());
+    } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
+      auto array = m_data_sp->GetAsArray();
+      return (array->GetSize());
+    } else
+      return 0;
+  }
+
+  StructuredData::ObjectSP GetValueForKey(const char *key) const {
+    if (m_data_sp) {
+      auto dict = m_data_sp->GetAsDictionary();
+      if (dict)
+        return dict->GetValueForKey(llvm::StringRef(key));
+    }
+    return StructuredData::ObjectSP();
+  }
+
+  StructuredData::ObjectSP GetItemAtIndex(size_t idx) const {
+    if (m_data_sp) {
+      auto array = m_data_sp->GetAsArray();
+      if (array)
+        return array->GetItemAtIndex(idx);
+    }
+    return StructuredData::ObjectSP();
   }
 
-  void SetObjectSP(const StructuredData::ObjectSP &obj) {
-    m_data_sp = obj;
+  uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
+    return (m_data_sp ? m_data_sp->GetIntegerValue(fail_value) : fail_value);
   }
 
-private:
+  double GetFloatValue(double fail_value = 0.0) const {
+    return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : fail_value);
+  }
 
+  bool GetBooleanValue(bool fail_value = false) const {
+    return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
+  }
+
+  size_t GetStringValue(char *dst, size_t dst_len) const {
+    if (!m_data_sp)
+      return 0;
+
+    llvm::StringRef result = m_data_sp->GetStringValue();
+    if (result.empty())
+      return 0;
+
+    if (!dst || !dst_len) {
+      char s[1];
+      return (::snprintf(s, 1, "%s", result.data()));
+    }
+    return (::snprintf(dst, dst_len, "%s", result.data()));
+  }
+
+private:
   lldb::StructuredDataPluginWP m_plugin_wp;
   StructuredData::ObjectSP m_data_sp;
 };
-}
+} // namespace lldb_private
 #endif

Modified: lldb/trunk/include/lldb/lldb-enumerations.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/lldb-enumerations.h?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/include/lldb/lldb-enumerations.h (original)
+++ lldb/trunk/include/lldb/lldb-enumerations.h Mon May 29 03:25:46 2017
@@ -725,6 +725,18 @@ enum TraceType {
   eTraceTypeProcessorTrace
 };
 
+enum StructuredDataType {
+  eStructuredDataTypeInvalid = -1,
+  eStructuredDataTypeNull = 0,
+  eStructuredDataTypeGeneric,
+  eStructuredDataTypeArray,
+  eStructuredDataTypeInteger,
+  eStructuredDataTypeFloat,
+  eStructuredDataTypeBoolean,
+  eStructuredDataTypeString,
+  eStructuredDataTypeDictionary
+};
+
 FLAGS_ENUM(TypeClass){
     eTypeClassInvalid = (0u), eTypeClassArray = (1u << 0),
     eTypeClassBlockPointer = (1u << 1), eTypeClassBuiltin = (1u << 2),

Added: lldb/trunk/packages/Python/lldbsuite/test/python_api/sbstructureddata/TestStructuredDataAPI.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/sbstructureddata/TestStructuredDataAPI.py?rev=304138&view=auto
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/python_api/sbstructureddata/TestStructuredDataAPI.py (added)
+++ lldb/trunk/packages/Python/lldbsuite/test/python_api/sbstructureddata/TestStructuredDataAPI.py Mon May 29 03:25:46 2017
@@ -0,0 +1,206 @@
+"""
+Test some SBStructuredData API.
+"""
+
+from __future__ import print_function
+
+import os
+import re
+import time
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class TestStructuredDataAPI(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def test(self):
+        self.structured_data_api_test()
+
+    def setUp(self):
+        TestBase.setUp(self)
+
+    @add_test_categories(['pyapi'])
+    def structured_data_api_test(self):
+        error = lldb.SBError()
+        s = lldb.SBStream()
+        s.Print(
+            "{\"key_dict\":{\"key_string\":\"STRING\",\"key_int\":3,\"key_float\":2.99,\"key_bool\":true,\"key_array\":[\"23\",\"arr\"]}}")
+        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())
+
+        # Tests for invalid data type
+        self.invalid_struct_test(example)
+
+        dict_struct = lldb.SBStructuredData()
+        dict_struct = example.GetValueForKey("key_dict")
+
+        # Tests for dictionary data type
+        self.dictionary_struct_test(example)
+
+        # Tests for string data type
+        self.string_struct_test(dict_struct)
+
+        # Tests for integer data type
+        self.int_struct_test(dict_struct)
+
+        # Tests for floating point data type
+        self.double_struct_test(dict_struct)
+
+        # Tests for boolean data type
+        self.bool_struct_test(dict_struct)
+
+        # Tests for array data type
+        self.array_struct_test(dict_struct)
+
+    def invalid_struct_test(self, example):
+        invalid_struct = lldb.SBStructuredData()
+        invalid_struct = example.GetValueForKey("invalid_key")
+        if invalid_struct.IsValid():
+            self.fail("An invalid object should have been returned")
+
+        # Check Type API
+        if not invalid_struct.GetType() == lldb.eStructuredDataTypeInvalid:
+            self.fail("Wrong type returned: " + str(invalid_struct.GetType()))
+
+    def dictionary_struct_test(self, example):
+        # Check API returning a valid SBStructuredData of 'dictionary' type
+        dict_struct = lldb.SBStructuredData()
+        dict_struct = example.GetValueForKey("key_dict")
+        if not dict_struct.IsValid():
+            self.fail("A valid object should have been returned")
+
+        # Check Type API
+        if not dict_struct.GetType() == lldb.eStructuredDataTypeDictionary:
+            self.fail("Wrong type returned: " + str(dict_struct.GetType()))
+
+        # Check Size API for 'dictionary' type
+        if not dict_struct.GetSize() == 5:
+            self.fail("Wrong no of elements returned: " +
+                      str(dict_struct.GetSize()))
+
+    def string_struct_test(self, dict_struct):
+        string_struct = lldb.SBStructuredData()
+        string_struct = dict_struct.GetValueForKey("key_string")
+        if not string_struct.IsValid():
+            self.fail("A valid object should have been returned")
+
+        # Check Type API
+        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
+            self.fail("Wrong type returned: " + str(string_struct.GetType()))
+
+        # Check API returning 'string' value
+        output = string_struct.GetStringValue(25)
+        if not "STRING" in output:
+            self.fail("wrong output: " + output)
+
+        # Calling wrong API on a SBStructuredData
+        # (e.g. getting an integer from a string type structure)
+        output = string_struct.GetIntegerValue()
+        if output:
+            self.fail(
+                "Valid integer value " +
+                str(output) +
+                " returned for a string object")
+
+    def int_struct_test(self, dict_struct):
+        # Check a valid SBStructuredData containing an 'integer' by
+        int_struct = lldb.SBStructuredData()
+        int_struct = dict_struct.GetValueForKey("key_int")
+        if not int_struct.IsValid():
+            self.fail("A valid object should have been returned")
+
+        # Check Type API
+        if not int_struct.GetType() == lldb.eStructuredDataTypeInteger:
+            self.fail("Wrong type returned: " + str(int_struct.GetType()))
+
+        # Check API returning 'integer' value
+        output = int_struct.GetIntegerValue()
+        if not output == 3:
+            self.fail("wrong output: " + str(output))
+
+        # Calling wrong API on a SBStructuredData
+        # (e.g. getting a string value from an integer type structure)
+        output = int_struct.GetStringValue(25)
+        if output:
+            self.fail(
+                "Valid string " +
+                output +
+                " returned for an integer object")
+
+    def double_struct_test(self, dict_struct):
+        floating_point_struct = lldb.SBStructuredData()
+        floating_point_struct = dict_struct.GetValueForKey("key_float")
+        if not floating_point_struct.IsValid():
+            self.fail("A valid object should have been returned")
+
+        # Check Type API
+        if not floating_point_struct.GetType() == lldb.eStructuredDataTypeFloat:
+            self.fail("Wrong type returned: " +
+                      str(floating_point_struct.GetType()))
+
+        # Check API returning 'double' value
+        output = floating_point_struct.GetFloatValue()
+        if not output == 2.99:
+            self.fail("wrong output: " + str(output))
+
+    def bool_struct_test(self, dict_struct):
+        bool_struct = lldb.SBStructuredData()
+        bool_struct = dict_struct.GetValueForKey("key_bool")
+        if not bool_struct.IsValid():
+            self.fail("A valid object should have been returned")
+
+        # Check Type API
+        if not bool_struct.GetType() == lldb.eStructuredDataTypeBoolean:
+            self.fail("Wrong type returned: " + str(bool_struct.GetType()))
+
+        # Check API returning 'bool' value
+        output = bool_struct.GetBooleanValue()
+        if not output:
+            self.fail("wrong output: " + str(output))
+
+    def array_struct_test(self, dict_struct):
+        # Check API returning a valid SBStructuredData of 'array' type
+        array_struct = lldb.SBStructuredData()
+        array_struct = dict_struct.GetValueForKey("key_array")
+        if not array_struct.IsValid():
+            self.fail("A valid object should have been returned")
+
+        # Check Type API
+        if not array_struct.GetType() == lldb.eStructuredDataTypeArray:
+            self.fail("Wrong type returned: " + str(array_struct.GetType()))
+
+        # Check Size API for 'array' type
+        if not array_struct.GetSize() == 2:
+            self.fail("Wrong no of elements returned: " +
+                      str(array_struct.GetSize()))
+
+        # Check API returning a valid SBStructuredData for different 'array'
+        # indices
+        string_struct = array_struct.GetItemAtIndex(0)
+        if not string_struct.IsValid():
+            self.fail("A valid object should have been returned")
+        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
+            self.fail("Wrong type returned: " + str(string_struct.GetType()))
+        output = string_struct.GetStringValue(5)
+        if not output == "23":
+            self.fail("wrong output: " + str(output))
+
+        string_struct = array_struct.GetItemAtIndex(1)
+        if not string_struct.IsValid():
+            self.fail("A valid object should have been returned")
+        if not string_struct.GetType() == lldb.eStructuredDataTypeString:
+            self.fail("Wrong type returned: " + str(string_struct.GetType()))
+        output = string_struct.GetStringValue(5)
+        if not output == "arr":
+            self.fail("wrong output: " + str(output))

Modified: lldb/trunk/scripts/interface/SBStructuredData.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/interface/SBStructuredData.i?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/scripts/interface/SBStructuredData.i (original)
+++ lldb/trunk/scripts/interface/SBStructuredData.i Mon May 29 03:25:46 2017
@@ -18,21 +18,38 @@ namespace lldb {
     class SBStructuredData
     {
     public:
-        
         SBStructuredData();
-        
+
         SBStructuredData(const lldb::SBStructuredData &rhs);
 
         SBStructuredData(const lldb::EventSP &event_sp);
 
         ~SBStructuredData();
-                 
+
         bool
         IsValid() const;
-                 
+
         void
         Clear();
 
+        lldb::SBStructuredData &operator=(const lldb::SBStructuredData &rhs);
+
+        lldb::StructuredDataType GetType() const;
+
+        size_t GetSize() const;
+
+        lldb::SBStructuredData GetValueForKey(const char *key) const;
+
+        lldb::SBStructuredData GetItemAtIndex(size_t idx) const;
+
+        uint64_t GetIntegerValue(uint64_t fail_value = 0) const;
+
+        double GetFloatValue(double fail_value = 0.0) const;
+
+        bool GetBooleanValue(bool fail_value = false) const;
+
+        size_t GetStringValue(char *dst, size_t dst_len) const;
+
         lldb::SBError
         GetAsJSON(lldb::SBStream &stream) const;
 

Modified: lldb/trunk/source/API/SBStructuredData.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBStructuredData.cpp?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/source/API/SBStructuredData.cpp (original)
+++ lldb/trunk/source/API/SBStructuredData.cpp Mon May 29 03:25:46 2017
@@ -46,7 +46,7 @@ lldb::SBError SBStructuredData::SetFromJ
   StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
   m_impl_up->SetObjectSP(json_obj);
 
-  if (!json_obj || json_obj->GetType() != StructuredData::Type::eTypeDictionary)
+  if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
     error.SetErrorString("Invalid Syntax");
   return error;
 }
@@ -67,3 +67,45 @@ lldb::SBError SBStructuredData::GetDescr
   sb_error.SetError(error);
   return sb_error;
 }
+
+StructuredDataType SBStructuredData::GetType() const {
+  return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
+}
+
+size_t SBStructuredData::GetSize() const {
+  return (m_impl_up ? m_impl_up->GetSize() : 0);
+}
+
+lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
+  if (!m_impl_up)
+    return SBStructuredData();
+
+  SBStructuredData result;
+  result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
+  return result;
+}
+
+lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
+  if (!m_impl_up)
+    return SBStructuredData();
+
+  SBStructuredData result;
+  result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
+  return result;
+}
+
+uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
+  return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
+}
+
+double SBStructuredData::GetFloatValue(double fail_value) const {
+  return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
+}
+
+bool SBStructuredData::GetBooleanValue(bool fail_value) const {
+  return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
+}
+
+size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
+  return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
+}

Modified: lldb/trunk/source/API/SBThread.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBThread.cpp?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/source/API/SBThread.cpp (original)
+++ lldb/trunk/source/API/SBThread.cpp Mon May 29 03:25:46 2017
@@ -43,6 +43,7 @@
 #include "lldb/API/SBThreadCollection.h"
 #include "lldb/API/SBThreadPlan.h"
 #include "lldb/API/SBValue.h"
+#include "lldb/lldb-enumerations.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -561,26 +562,26 @@ bool SBThread::GetInfoItemByPathAsString
         StructuredData::ObjectSP node =
             info_root_sp->GetObjectForDotSeparatedPath(path);
         if (node) {
-          if (node->GetType() == StructuredData::Type::eTypeString) {
+          if (node->GetType() == eStructuredDataTypeString) {
             strm.Printf("%s", node->GetAsString()->GetValue().str().c_str());
             success = true;
           }
-          if (node->GetType() == StructuredData::Type::eTypeInteger) {
+          if (node->GetType() == eStructuredDataTypeInteger) {
             strm.Printf("0x%" PRIx64, node->GetAsInteger()->GetValue());
             success = true;
           }
-          if (node->GetType() == StructuredData::Type::eTypeFloat) {
+          if (node->GetType() == eStructuredDataTypeFloat) {
             strm.Printf("0x%f", node->GetAsFloat()->GetValue());
             success = true;
           }
-          if (node->GetType() == StructuredData::Type::eTypeBoolean) {
+          if (node->GetType() == eStructuredDataTypeBoolean) {
             if (node->GetAsBoolean()->GetValue() == true)
               strm.Printf("true");
             else
               strm.Printf("false");
             success = true;
           }
-          if (node->GetType() == StructuredData::Type::eTypeNull) {
+          if (node->GetType() == eStructuredDataTypeNull) {
             strm.Printf("null");
             success = true;
           }

Modified: lldb/trunk/source/Core/FormatEntity.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/FormatEntity.cpp?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/source/Core/FormatEntity.cpp (original)
+++ lldb/trunk/source/Core/FormatEntity.cpp Mon May 29 03:25:46 2017
@@ -1040,24 +1040,24 @@ static bool FormatThreadExtendedInfoRecu
       thread_info_dictionary->GetObjectForDotSeparatedPath(path);
 
   if (value) {
-    if (value->GetType() == StructuredData::Type::eTypeInteger) {
+    if (value->GetType() == eStructuredDataTypeInteger) {
       const char *token_format = "0x%4.4" PRIx64;
       if (!entry.printf_format.empty())
         token_format = entry.printf_format.c_str();
       s.Printf(token_format, value->GetAsInteger()->GetValue());
       return true;
-    } else if (value->GetType() == StructuredData::Type::eTypeFloat) {
+    } else if (value->GetType() == eStructuredDataTypeFloat) {
       s.Printf("%f", value->GetAsFloat()->GetValue());
       return true;
-    } else if (value->GetType() == StructuredData::Type::eTypeString) {
+    } else if (value->GetType() == eStructuredDataTypeString) {
       s.Format("{0}", value->GetAsString()->GetValue());
       return true;
-    } else if (value->GetType() == StructuredData::Type::eTypeArray) {
+    } else if (value->GetType() == eStructuredDataTypeArray) {
       if (value->GetAsArray()->GetSize() > 0) {
         s.Printf("%zu", value->GetAsArray()->GetSize());
         return true;
       }
-    } else if (value->GetType() == StructuredData::Type::eTypeDictionary) {
+    } else if (value->GetType() == eStructuredDataTypeDictionary) {
       s.Printf("%zu",
                value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
       return true;
@@ -1346,7 +1346,7 @@ bool FormatEntity::Format(const Entry &e
       if (thread) {
         StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
         if (object_sp &&
-            object_sp->GetType() == StructuredData::Type::eTypeDictionary) {
+            object_sp->GetType() == eStructuredDataTypeDictionary) {
           if (FormatThreadExtendedInfoRecurse(entry, object_sp, sc, exe_ctx, s))
             return true;
         }

Modified: lldb/trunk/source/Core/StructuredData.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/StructuredData.cpp?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/source/Core/StructuredData.cpp (original)
+++ lldb/trunk/source/Core/StructuredData.cpp Mon May 29 03:25:46 2017
@@ -184,7 +184,7 @@ StructuredData::ObjectSP StructuredData:
 
 StructuredData::ObjectSP
 StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
-  if (this->GetType() == Type::eTypeDictionary) {
+  if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
     std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
     std::string key = match.first.str();
     ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
@@ -200,7 +200,7 @@ StructuredData::Object::GetObjectForDotS
     return ObjectSP();
   }
 
-  if (this->GetType() == Type::eTypeArray) {
+  if (this->GetType() == lldb::eStructuredDataTypeArray) {
     std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
     if (match.second.size() == 0) {
       return this->shared_from_this();

Modified: lldb/trunk/source/Target/Thread.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Thread.cpp?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/source/Target/Thread.cpp (original)
+++ lldb/trunk/source/Target/Thread.cpp Mon May 29 03:25:46 2017
@@ -51,6 +51,7 @@
 #include "lldb/Utility/RegularExpression.h"
 #include "lldb/Utility/Stream.h"
 #include "lldb/Utility/StreamString.h"
+#include "lldb/lldb-enumerations.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -397,7 +398,7 @@ lldb::StopInfoSP Thread::GetStopInfo() {
   bool plan_overrides_trace =
     have_valid_stop_info && have_valid_completed_plan
     && (m_stop_info_sp->GetStopReason() == eStopReasonTrace);
-    
+
   if (have_valid_stop_info && !plan_overrides_trace) {
     return m_stop_info_sp;
   } else if (have_valid_completed_plan) {
@@ -541,7 +542,7 @@ bool Thread::CheckpointThreadState(Threa
     saved_state.orig_stop_id = process_sp->GetStopID();
   saved_state.current_inlined_depth = GetCurrentInlinedDepth();
   saved_state.m_completed_plan_stack = m_completed_plan_stack;
-	
+
   return true;
 }
 
@@ -1994,13 +1995,12 @@ bool Thread::GetDescription(Stream &strm
         thread_info->GetObjectForDotSeparatedPath("trace_messages");
 
     bool printed_activity = false;
-    if (activity &&
-        activity->GetType() == StructuredData::Type::eTypeDictionary) {
+    if (activity && activity->GetType() == eStructuredDataTypeDictionary) {
       StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
       StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
       StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
-      if (name && name->GetType() == StructuredData::Type::eTypeString && id &&
-          id->GetType() == StructuredData::Type::eTypeInteger) {
+      if (name && name->GetType() == eStructuredDataTypeString && id &&
+          id->GetType() == eStructuredDataTypeInteger) {
         strm.Format("  Activity '{0}', {1:x}\n",
                     name->GetAsString()->GetValue(),
                     id->GetAsInteger()->GetValue());
@@ -2008,8 +2008,7 @@ bool Thread::GetDescription(Stream &strm
       printed_activity = true;
     }
     bool printed_breadcrumb = false;
-    if (breadcrumb &&
-        breadcrumb->GetType() == StructuredData::Type::eTypeDictionary) {
+    if (breadcrumb && breadcrumb->GetType() == eStructuredDataTypeDictionary) {
       if (printed_activity)
         strm.Printf("\n");
       StructuredData::Dictionary *breadcrumb_dict =
@@ -2017,13 +2016,13 @@ bool Thread::GetDescription(Stream &strm
       StructuredData::ObjectSP breadcrumb_text =
           breadcrumb_dict->GetValueForKey("name");
       if (breadcrumb_text &&
-          breadcrumb_text->GetType() == StructuredData::Type::eTypeString) {
+          breadcrumb_text->GetType() == eStructuredDataTypeString) {
         strm.Format("  Current Breadcrumb: {0}\n",
                     breadcrumb_text->GetAsString()->GetValue());
       }
       printed_breadcrumb = true;
     }
-    if (messages && messages->GetType() == StructuredData::Type::eTypeArray) {
+    if (messages && messages->GetType() == eStructuredDataTypeArray) {
       if (printed_breadcrumb)
         strm.Printf("\n");
       StructuredData::Array *messages_array = messages->GetAsArray();
@@ -2032,14 +2031,13 @@ bool Thread::GetDescription(Stream &strm
         strm.Printf("  %zu trace messages:\n", msg_count);
         for (size_t i = 0; i < msg_count; i++) {
           StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
-          if (message &&
-              message->GetType() == StructuredData::Type::eTypeDictionary) {
+          if (message && message->GetType() == eStructuredDataTypeDictionary) {
             StructuredData::Dictionary *message_dict =
                 message->GetAsDictionary();
             StructuredData::ObjectSP message_text =
                 message_dict->GetValueForKey("message");
             if (message_text &&
-                message_text->GetType() == StructuredData::Type::eTypeString) {
+                message_text->GetType() == eStructuredDataTypeString) {
               strm.Format("    {0}\n", message_text->GetAsString()->GetValue());
             }
           }

Modified: lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp?rev=304138&r1=304137&r2=304138&view=diff
==============================================================================
--- lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp (original)
+++ lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp Mon May 29 03:25:46 2017
@@ -15,6 +15,7 @@
 #include "lldb/Host/File.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
+#include "lldb/lldb-enumerations.h"
 
 #include "PythonTestSuite.h"
 
@@ -355,9 +356,9 @@ TEST_F(PythonDataObjectsTest, TestPython
   list.AppendItem(PythonString(string_value1));
 
   auto array_sp = list.CreateStructuredArray();
-  EXPECT_EQ(StructuredData::Type::eTypeInteger,
+  EXPECT_EQ(lldb::eStructuredDataTypeInteger,
             array_sp->GetItemAtIndex(0)->GetType());
-  EXPECT_EQ(StructuredData::Type::eTypeString,
+  EXPECT_EQ(lldb::eStructuredDataTypeString,
             array_sp->GetItemAtIndex(1)->GetType());
 
   auto int_sp = array_sp->GetItemAtIndex(0)->GetAsInteger();
@@ -424,9 +425,9 @@ TEST_F(PythonDataObjectsTest, TestPython
 
   auto array_sp = tuple.CreateStructuredArray();
   EXPECT_EQ(tuple.GetSize(), array_sp->GetSize());
-  EXPECT_EQ(StructuredData::Type::eTypeInteger,
+  EXPECT_EQ(lldb::eStructuredDataTypeInteger,
             array_sp->GetItemAtIndex(0)->GetType());
-  EXPECT_EQ(StructuredData::Type::eTypeString,
+  EXPECT_EQ(lldb::eStructuredDataTypeString,
             array_sp->GetItemAtIndex(1)->GetType());
 }
 




More information about the lldb-commits mailing list