Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lldb/include/lldb/API/SBStructuredData.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,35 @@ class SBStructuredData {
/// Return the generic pointer if this data structure is a generic type.
lldb::SBScriptObject GetGenericValue() const;

/// Set the value corresponding to a key. If this data structure
/// is not a dictionary type, reset the type to be dictionary and overwrite
/// the previous data.
void SetValueForKey(const char *key, SBStructuredData &value);

/// Change the type to unsigned interger and overwrite the previous data with
/// the new value.
void SetUnsignedIntegerValue(uint64_t value);

/// Change the type to signed interger and overwrite the previous data with
/// the new value.
void SetSignedIntegerValue(int64_t value);

/// Change the type to float and overwrite the previous data with the new
/// value.
void SetFloatValue(double value);

/// Change the type to boolean and overwrite the previous data with the new
/// value.
void SetBooleanValue(bool value);

/// Change the type to string and overwrite the previous data with the new
/// value.
void SetStringValue(const char *value);

/// Change the type to generic and overwrite the previous data with the new
/// value.
void SetGenericValue(SBScriptObject value);

protected:
friend class SBAttachInfo;
friend class SBCommandReturnObject;
Expand Down
35 changes: 35 additions & 0 deletions lldb/include/lldb/Core/StructuredDataImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ class StructuredDataImpl {

void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }

void SetValueForKey(llvm::StringRef key,
const StructuredData::ObjectSP &value) {
if (!m_data_sp ||
m_data_sp->GetType() != lldb::eStructuredDataTypeDictionary) {
m_data_sp = StructuredData::FromKeyValue(key, value);
} else if (StructuredData::Dictionary *dict =
m_data_sp->GetAsDictionary()) {
dict->AddItem(key, value);
}
}

void SetUnsignedIntegerValue(uint64_t value) {
m_data_sp = StructuredData::FromInteger(value);
}

void SetSignedIntegerValue(int64_t value) {
m_data_sp = StructuredData::FromInteger(value);
}

void SetFloatValue(double value) {
m_data_sp = StructuredData::FromFloat(value);
}

void SetBooleanValue(bool value) {
m_data_sp = StructuredData::FromBoolean(value);
}

void SetStringValue(std::string value) {
m_data_sp = StructuredData::FromString(std::move(value));
}

void SetGenericValue(void *value) {
m_data_sp = StructuredData::FromGeneric(value);
}

lldb::StructuredDataType GetType() const {
return (m_data_sp ? m_data_sp->GetType() :
lldb::eStructuredDataTypeInvalid);
Expand Down
26 changes: 25 additions & 1 deletion lldb/include/lldb/Utility/StructuredData.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class StructuredData {
}
return success;
}

template <class IntType>
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const {
ObjectSP value_sp = GetValueForKey(key);
Expand Down Expand Up @@ -574,6 +574,30 @@ class StructuredData {
void *m_object;
};

template <typename T> static ObjectSP FromInteger(T value) {
return std::make_shared<Integer<T>>(value);
}

static StructuredData::ObjectSP FromFloat(double value) {
return std::make_shared<StructuredData::Float>(value);
}
static StructuredData::ObjectSP FromBoolean(bool value) {
return std::make_shared<StructuredData::Boolean>(value);
}
static StructuredData::ObjectSP FromString(std::string value) {
return std::make_shared<StructuredData::String>(value);
}
static StructuredData::ObjectSP FromGeneric(void *value) {
return std::make_shared<StructuredData::Generic>(value);
}

static StructuredData::ObjectSP
FromKeyValue(llvm::StringRef key, const StructuredData::ObjectSP &value_sp) {
auto dict_sp = std::make_shared<StructuredData::Dictionary>();
dict_sp->AddItem(key, value_sp);
return dict_sp;
}

static ObjectSP ParseJSON(llvm::StringRef json_text);
static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error);
static bool IsRecordType(const ObjectSP object_sp);
Expand Down
44 changes: 44 additions & 0 deletions lldb/source/API/SBStructuredData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,47 @@ lldb::SBScriptObject SBStructuredData::GetGenericValue() const {

return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
}

void SBStructuredData::SetValueForKey(const char *key,
SBStructuredData &value) {
LLDB_INSTRUMENT_VA(this, key, value);

if (StructuredData::ObjectSP obj_sp = value.m_impl_up->GetObjectSP())
m_impl_up->SetValueForKey(key, obj_sp);
}

void SBStructuredData::SetUnsignedIntegerValue(uint64_t value) {
LLDB_INSTRUMENT_VA(this, value);

m_impl_up->SetUnsignedIntegerValue(value);
}

void SBStructuredData::SetSignedIntegerValue(int64_t value) {
LLDB_INSTRUMENT_VA(this, value);

m_impl_up->SetSignedIntegerValue(value);
}

void SBStructuredData::SetFloatValue(double value) {
LLDB_INSTRUMENT_VA(this, value);

m_impl_up->SetFloatValue(value);
}

void SBStructuredData::SetBooleanValue(bool value) {
LLDB_INSTRUMENT_VA(this, value);

m_impl_up->SetBooleanValue(value);
}

void SBStructuredData::SetStringValue(const char *value) {
LLDB_INSTRUMENT_VA(this, value);

m_impl_up->SetStringValue(value);
}

void SBStructuredData::SetGenericValue(SBScriptObject value) {
LLDB_INSTRUMENT_VA(this, value);

m_impl_up->SetGenericValue(value.GetPointer());
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import json


class TestStructuredDataAPI(TestBase):
NO_DEBUG_INFO_TESTCASE = True

Expand Down Expand Up @@ -130,6 +129,52 @@ class MyRandomClass:
self.assertSuccess(example.SetFromJSON("null"))
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeNull)

example = lldb.SBStructuredData()
example.SetUnsignedIntegerValue(1)
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeInteger)
self.assertEqual(example.GetIntegerValue(), 1)

example.SetSignedIntegerValue(-42)
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeSignedInteger)
self.assertEqual(example.GetSignedIntegerValue(), -42)

example.SetFloatValue(4.19)
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeFloat)
self.assertEqual(example.GetFloatValue(), 4.19)

example.SetStringValue("Bonjour, 123!")
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeString)
self.assertEqual(example.GetStringValue(42), "Bonjour, 123!")

value = lldb.SBStructuredData()
example.SetValueForKey("Hello", value)
self.assertEqual(example.GetSize(), 0)

nested_obj = lldb.SBStructuredData()
nested_obj.SetStringValue("World")
example.SetValueForKey("Hello", nested_obj)
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeDictionary)
nested_obj = None
nested_obj = example.GetValueForKey("Hello")
self.assertTrue(nested_obj.IsValid())
self.assertEqual(nested_obj.GetType(), lldb.eStructuredDataTypeString)
self.assertEqual(nested_obj.GetStringValue(42), "World")

example.SetBooleanValue(True)
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeBoolean)
self.assertTrue(example.GetBooleanValue())

rnd_obj = MyRandomClass()
stp = lldb.SBScriptObject(rnd_obj, lldb.eScriptLanguagePython)
self.assertEqual(stp.ptr, rnd_obj)

example.SetGenericValue(stp)
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeGeneric)

my_random_class = example.GetGenericValue()
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()
Expand Down
Loading