blob: b349de7b0b781beada87229b3744cc2caf425aae [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- StructuredData.cpp ------------------------------------------------===//
Jason Molenda705b1802014-06-13 02:37:02 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jason Molenda705b1802014-06-13 02:37:02 +00006//
7//===----------------------------------------------------------------------===//
8
Pavel Labathf2a8bcc2017-06-27 10:45:31 +00009#include "lldb/Utility/StructuredData.h"
Zachary Turner5713a052017-03-22 18:40:07 +000010#include "lldb/Utility/FileSpec.h"
Zachary Turner97206d52017-05-12 04:51:55 +000011#include "lldb/Utility/Status.h"
Eugene Zemtsov11c0aab2017-11-17 03:28:58 +000012#include "llvm/Support/MemoryBuffer.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000013#include <cerrno>
14#include <cstdlib>
Zachary Turner2f3df612017-04-06 21:28:29 +000015#include <inttypes.h>
Zachary Turner0641ca12015-03-17 20:04:04 +000016
Jason Molenda705b1802014-06-13 02:37:02 +000017using namespace lldb_private;
Jonas Devlieghere2783d812019-10-01 17:41:48 +000018using namespace llvm;
Jason Molenda705b1802014-06-13 02:37:02 +000019
Jonas Devlieghere57b46882019-10-01 17:41:52 +000020static StructuredData::ObjectSP ParseJSONValue(json::Value &value);
21static StructuredData::ObjectSP ParseJSONObject(json::Object *object);
22static StructuredData::ObjectSP ParseJSONArray(json::Array *array);
23
Jonas Devlieghere0d5fc822020-07-22 13:33:03 -070024StructuredData::ObjectSP
25StructuredData::ParseJSON(const std::string &json_text) {
Jonas Devlieghere57b46882019-10-01 17:41:52 +000026 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}
Jason Molenda705b1802014-06-13 02:37:02 +000033
Jim Ingham01f16662016-09-14 19:07:35 +000034StructuredData::ObjectSP
Zachary Turner97206d52017-05-12 04:51:55 +000035StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000036 StructuredData::ObjectSP return_sp;
Jim Inghame14dc262016-09-12 23:10:56 +000037
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000038 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());
Jim Inghame14dc262016-09-12 23:10:56 +000043 return return_sp;
44 }
Walter Erquinigo74c93952020-08-17 17:21:52 -070045 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();
Jim Inghame14dc262016-09-12 23:10:56 +000051}
52
Jonas Devlieghere57b46882019-10-01 17:41:52 +000053static 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
Sam McCallfa69b602020-09-24 01:14:12 +020060 if (auto s = value.getAsString())
61 return std::make_shared<StructuredData::String>(*s);
Jonas Devlieghere57b46882019-10-01 17:41:52 +000062
Sam McCallfa69b602020-09-24 01:14:12 +020063 if (auto b = value.getAsBoolean())
64 return std::make_shared<StructuredData::Boolean>(*b);
Jonas Devlieghere57b46882019-10-01 17:41:52 +000065
Sam McCall751f5c82020-09-24 01:30:42 +020066 if (auto i = value.getAsInteger())
Sam McCallfa69b602020-09-24 01:14:12 +020067 return std::make_shared<StructuredData::Integer>(*i);
Jonas Devlieghere57b46882019-10-01 17:41:52 +000068
Sam McCallfa69b602020-09-24 01:14:12 +020069 if (auto d = value.getAsNumber())
70 return std::make_shared<StructuredData::Float>(*d);
Jonas Devlieghere57b46882019-10-01 17:41:52 +000071
72 return StructuredData::ObjectSP();
73}
74
75static StructuredData::ObjectSP ParseJSONObject(json::Object *object) {
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +000076 auto dict_up = std::make_unique<StructuredData::Dictionary>();
Jonas Devlieghere57b46882019-10-01 17:41:52 +000077 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);
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
Jonas Devlieghered1e3b232020-01-13 18:23:39 -080083 return std::move(dict_up);
Jason Molenda705b1802014-06-13 02:37:02 +000084}
85
Jonas Devlieghere57b46882019-10-01 17:41:52 +000086static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +000087 auto array_up = std::make_unique<StructuredData::Array>();
Jonas Devlieghere57b46882019-10-01 17:41:52 +000088 for (json::Value &value : *array) {
89 if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 array_up->AddItem(value_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 }
Jonas Devlieghered1e3b232020-01-13 18:23:39 -080092 return std::move(array_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +000093}
94
95StructuredData::ObjectSP
96StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +000097 if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
99 std::string key = match.first.str();
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000100 ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 if (value.get()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000102 // Do we have additional words to descend? If not, return the value
103 // we're at right now.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 if (match.second.empty()) {
105 return value;
106 } else {
107 return value->GetObjectForDotSeparatedPath(match.second);
108 }
109 }
110 return ObjectSP();
111 }
112
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000113 if (this->GetType() == lldb::eStructuredDataTypeArray) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000115 if (match.second.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 return this->shared_from_this();
117 }
118 errno = 0;
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000119 uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 if (errno == 0) {
121 return this->GetAsArray()->GetItemAtIndex(val);
122 }
123 return ObjectSP();
124 }
125
126 return this->shared_from_this();
127}
128
129void StructuredData::Object::DumpToStdout(bool pretty_print) const {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000130 json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
131 Serialize(stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132}
133
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000134void StructuredData::Array::Serialize(json::OStream &s) const {
135 s.arrayBegin();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 for (const auto &item_sp : m_items) {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000137 item_sp->Serialize(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 }
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000139 s.arrayEnd();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140}
141
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000142void StructuredData::Integer::Serialize(json::OStream &s) const {
143 s.value(static_cast<int64_t>(m_value));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144}
145
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000146void StructuredData::Float::Serialize(json::OStream &s) const {
147 s.value(m_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148}
149
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000150void StructuredData::Boolean::Serialize(json::OStream &s) const {
151 s.value(m_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152}
153
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000154void StructuredData::String::Serialize(json::OStream &s) const {
155 s.value(m_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156}
157
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000158void StructuredData::Dictionary::Serialize(json::OStream &s) const {
159 s.objectBegin();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 for (const auto &pair : m_dict) {
Raphael Isemann642bc152020-02-11 09:05:37 +0100161 s.attributeBegin(pair.first.GetStringRef());
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000162 pair.second->Serialize(s);
163 s.attributeEnd();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 }
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000165 s.objectEnd();
Jason Molenda705b1802014-06-13 02:37:02 +0000166}
167
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000168void StructuredData::Null::Serialize(json::OStream &s) const {
169 s.value(nullptr);
Jason Molenda705b1802014-06-13 02:37:02 +0000170}
Zachary Turner0641ca12015-03-17 20:04:04 +0000171
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000172void StructuredData::Generic::Serialize(json::OStream &s) const {
173 s.value(llvm::formatv("{0:X}", m_object));
Zachary Turner0641ca12015-03-17 20:04:04 +0000174}