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 <cstdlib>
15 #include <inttypes.h>
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
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
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 
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   std::string s;
61   if (json::fromJSON(value, s))
62     return std::make_shared<StructuredData::String>(s);
63 
64   bool b;
65   if (json::fromJSON(value, b))
66     return std::make_shared<StructuredData::Boolean>(b);
67 
68   int64_t i;
69   if (json::fromJSON(value, i))
70     return std::make_shared<StructuredData::Integer>(i);
71 
72   double d;
73   if (json::fromJSON(value, d))
74     return std::make_shared<StructuredData::Float>(d);
75 
76   return StructuredData::ObjectSP();
77 }
78 
79 static StructuredData::ObjectSP ParseJSONObject(json::Object *object) {
80   auto dict_up = std::make_unique<StructuredData::Dictionary>();
81   for (auto &KV : *object) {
82     StringRef key = KV.first;
83     json::Value value = KV.second;
84     if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
85       dict_up->AddItem(key, value_sp);
86   }
87   return std::move(dict_up);
88 }
89 
90 static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
91   auto array_up = std::make_unique<StructuredData::Array>();
92   for (json::Value &value : *array) {
93     if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
94       array_up->AddItem(value_sp);
95   }
96   return std::move(array_up);
97 }
98 
99 StructuredData::ObjectSP
100 StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
101   if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
102     std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
103     std::string key = match.first.str();
104     ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
105     if (value.get()) {
106       // Do we have additional words to descend?  If not, return the value
107       // we're at right now.
108       if (match.second.empty()) {
109         return value;
110       } else {
111         return value->GetObjectForDotSeparatedPath(match.second);
112       }
113     }
114     return ObjectSP();
115   }
116 
117   if (this->GetType() == lldb::eStructuredDataTypeArray) {
118     std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
119     if (match.second.empty()) {
120       return this->shared_from_this();
121     }
122     errno = 0;
123     uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
124     if (errno == 0) {
125       return this->GetAsArray()->GetItemAtIndex(val);
126     }
127     return ObjectSP();
128   }
129 
130   return this->shared_from_this();
131 }
132 
133 void StructuredData::Object::DumpToStdout(bool pretty_print) const {
134   json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
135   Serialize(stream);
136 }
137 
138 void StructuredData::Array::Serialize(json::OStream &s) const {
139   s.arrayBegin();
140   for (const auto &item_sp : m_items) {
141     item_sp->Serialize(s);
142   }
143   s.arrayEnd();
144 }
145 
146 void StructuredData::Integer::Serialize(json::OStream &s) const {
147   s.value(static_cast<int64_t>(m_value));
148 }
149 
150 void StructuredData::Float::Serialize(json::OStream &s) const {
151   s.value(m_value);
152 }
153 
154 void StructuredData::Boolean::Serialize(json::OStream &s) const {
155   s.value(m_value);
156 }
157 
158 void StructuredData::String::Serialize(json::OStream &s) const {
159   s.value(m_value);
160 }
161 
162 void StructuredData::Dictionary::Serialize(json::OStream &s) const {
163   s.objectBegin();
164   for (const auto &pair : m_dict) {
165     s.attributeBegin(pair.first.GetStringRef());
166     pair.second->Serialize(s);
167     s.attributeEnd();
168   }
169   s.objectEnd();
170 }
171 
172 void StructuredData::Null::Serialize(json::OStream &s) const {
173   s.value(nullptr);
174 }
175 
176 void StructuredData::Generic::Serialize(json::OStream &s) const {
177   s.value(llvm::formatv("{0:X}", m_object));
178 }
179