1 //===-- StructuredData.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/StructuredData.h"
10 #include "lldb/Utility/FileSpec.h"
11 #include "lldb/Utility/Status.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include <cerrno>
14 #include <cinttypes>
15 #include <cstdlib>
16 
17 using namespace lldb_private;
18 using namespace llvm;
19 
20 static StructuredData::ObjectSP ParseJSONValue(json::Value &value);
21 static StructuredData::ObjectSP ParseJSONObject(json::Object *object);
22 static StructuredData::ObjectSP ParseJSONArray(json::Array *array);
23 
24 StructuredData::ObjectSP
ParseJSON(const std::string & json_text)25 StructuredData::ParseJSON(const std::string &json_text) {
26   llvm::Expected<json::Value> value = json::parse(json_text);
27   if (!value) {
28     llvm::consumeError(value.takeError());
29     return nullptr;
30   }
31   return ParseJSONValue(*value);
32 }
33 
34 StructuredData::ObjectSP
ParseJSONFromFile(const FileSpec & input_spec,Status & error)35 StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) {
36   StructuredData::ObjectSP return_sp;
37 
38   auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.GetPath());
39   if (!buffer_or_error) {
40     error.SetErrorStringWithFormatv("could not open input file: {0} - {1}.",
41                                     input_spec.GetPath(),
42                                     buffer_or_error.getError().message());
43     return return_sp;
44   }
45   llvm::Expected<json::Value> value =
46       json::parse(buffer_or_error.get()->getBuffer().str());
47   if (value)
48     return ParseJSONValue(*value);
49   error.SetErrorString(toString(value.takeError()));
50   return StructuredData::ObjectSP();
51 }
52 
ParseJSONValue(json::Value & value)53 static StructuredData::ObjectSP ParseJSONValue(json::Value &value) {
54   if (json::Object *O = value.getAsObject())
55     return ParseJSONObject(O);
56 
57   if (json::Array *A = value.getAsArray())
58     return ParseJSONArray(A);
59 
60   if (auto s = value.getAsString())
61     return std::make_shared<StructuredData::String>(*s);
62 
63   if (auto b = value.getAsBoolean())
64     return std::make_shared<StructuredData::Boolean>(*b);
65 
66   if (auto i = value.getAsInteger())
67     return std::make_shared<StructuredData::Integer>(*i);
68 
69   if (auto d = value.getAsNumber())
70     return std::make_shared<StructuredData::Float>(*d);
71 
72   return StructuredData::ObjectSP();
73 }
74 
ParseJSONObject(json::Object * object)75 static StructuredData::ObjectSP ParseJSONObject(json::Object *object) {
76   auto dict_up = std::make_unique<StructuredData::Dictionary>();
77   for (auto &KV : *object) {
78     StringRef key = KV.first;
79     json::Value value = KV.second;
80     if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
81       dict_up->AddItem(key, value_sp);
82   }
83   return std::move(dict_up);
84 }
85 
ParseJSONArray(json::Array * array)86 static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
87   auto array_up = std::make_unique<StructuredData::Array>();
88   for (json::Value &value : *array) {
89     if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
90       array_up->AddItem(value_sp);
91   }
92   return std::move(array_up);
93 }
94 
95 StructuredData::ObjectSP
GetObjectForDotSeparatedPath(llvm::StringRef path)96 StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
97   if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
98     std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
99     std::string key = match.first.str();
100     ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
101     if (value.get()) {
102       // Do we have additional words to descend?  If not, return the value
103       // we're at right now.
104       if (match.second.empty()) {
105         return value;
106       } else {
107         return value->GetObjectForDotSeparatedPath(match.second);
108       }
109     }
110     return ObjectSP();
111   }
112 
113   if (this->GetType() == lldb::eStructuredDataTypeArray) {
114     std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
115     if (match.second.empty()) {
116       return this->shared_from_this();
117     }
118     errno = 0;
119     uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
120     if (errno == 0) {
121       return this->GetAsArray()->GetItemAtIndex(val);
122     }
123     return ObjectSP();
124   }
125 
126   return this->shared_from_this();
127 }
128 
DumpToStdout(bool pretty_print) const129 void StructuredData::Object::DumpToStdout(bool pretty_print) const {
130   json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
131   Serialize(stream);
132 }
133 
Serialize(json::OStream & s) const134 void StructuredData::Array::Serialize(json::OStream &s) const {
135   s.arrayBegin();
136   for (const auto &item_sp : m_items) {
137     item_sp->Serialize(s);
138   }
139   s.arrayEnd();
140 }
141 
Serialize(json::OStream & s) const142 void StructuredData::Integer::Serialize(json::OStream &s) const {
143   s.value(static_cast<int64_t>(m_value));
144 }
145 
Serialize(json::OStream & s) const146 void StructuredData::Float::Serialize(json::OStream &s) const {
147   s.value(m_value);
148 }
149 
Serialize(json::OStream & s) const150 void StructuredData::Boolean::Serialize(json::OStream &s) const {
151   s.value(m_value);
152 }
153 
Serialize(json::OStream & s) const154 void StructuredData::String::Serialize(json::OStream &s) const {
155   s.value(m_value);
156 }
157 
Serialize(json::OStream & s) const158 void StructuredData::Dictionary::Serialize(json::OStream &s) const {
159   s.objectBegin();
160   for (const auto &pair : m_dict) {
161     s.attributeBegin(pair.first.GetStringRef());
162     pair.second->Serialize(s);
163     s.attributeEnd();
164   }
165   s.objectEnd();
166 }
167 
Serialize(json::OStream & s) const168 void StructuredData::Null::Serialize(json::OStream &s) const {
169   s.value(nullptr);
170 }
171 
Serialize(json::OStream & s) const172 void StructuredData::Generic::Serialize(json::OStream &s) const {
173   s.value(llvm::formatv("{0:X}", m_object));
174 }
175