blob: c003412b5f4f868689c5bcff8641f3a986dd8d37 [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
24StructuredData::ObjectSP StructuredData::ParseJSON(std::string json_text) {
25 llvm::Expected<json::Value> value = json::parse(json_text);
26 if (!value) {
27 llvm::consumeError(value.takeError());
28 return nullptr;
29 }
30 return ParseJSONValue(*value);
31}
Jason Molenda705b1802014-06-13 02:37:02 +000032
Jim Ingham01f16662016-09-14 19:07:35 +000033StructuredData::ObjectSP
Zachary Turner97206d52017-05-12 04:51:55 +000034StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000035 StructuredData::ObjectSP return_sp;
Jim Inghame14dc262016-09-12 23:10:56 +000036
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000037 auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.GetPath());
38 if (!buffer_or_error) {
39 error.SetErrorStringWithFormatv("could not open input file: {0} - {1}.",
40 input_spec.GetPath(),
41 buffer_or_error.getError().message());
Jim Inghame14dc262016-09-12 23:10:56 +000042 return return_sp;
43 }
Jonas Devlieghere57b46882019-10-01 17:41:52 +000044 return ParseJSON(buffer_or_error.get()->getBuffer().str());
Jim Inghame14dc262016-09-12 23:10:56 +000045}
46
Jonas Devlieghere57b46882019-10-01 17:41:52 +000047static StructuredData::ObjectSP ParseJSONValue(json::Value &value) {
48 if (json::Object *O = value.getAsObject())
49 return ParseJSONObject(O);
50
51 if (json::Array *A = value.getAsArray())
52 return ParseJSONArray(A);
53
54 std::string s;
55 if (json::fromJSON(value, s))
56 return std::make_shared<StructuredData::String>(s);
57
58 bool b;
59 if (json::fromJSON(value, b))
60 return std::make_shared<StructuredData::Boolean>(b);
61
62 int64_t i;
63 if (json::fromJSON(value, i))
64 return std::make_shared<StructuredData::Integer>(i);
65
66 double d;
67 if (json::fromJSON(value, d))
68 return std::make_shared<StructuredData::Float>(d);
69
70 return StructuredData::ObjectSP();
71}
72
73static StructuredData::ObjectSP ParseJSONObject(json::Object *object) {
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +000074 auto dict_up = std::make_unique<StructuredData::Dictionary>();
Jonas Devlieghere57b46882019-10-01 17:41:52 +000075 for (auto &KV : *object) {
76 StringRef key = KV.first;
77 json::Value value = KV.second;
78 if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
79 dict_up->AddItem(key, value_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 }
Jonas Devlieghered1e3b232020-01-13 18:23:39 -080081 return std::move(dict_up);
Jason Molenda705b1802014-06-13 02:37:02 +000082}
83
Jonas Devlieghere57b46882019-10-01 17:41:52 +000084static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +000085 auto array_up = std::make_unique<StructuredData::Array>();
Jonas Devlieghere57b46882019-10-01 17:41:52 +000086 for (json::Value &value : *array) {
87 if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 array_up->AddItem(value_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 }
Jonas Devlieghered1e3b232020-01-13 18:23:39 -080090 return std::move(array_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +000091}
92
93StructuredData::ObjectSP
94StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +000095 if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
97 std::string key = match.first.str();
Malcolm Parsons771ef6d2016-11-02 20:34:10 +000098 ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 if (value.get()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000100 // Do we have additional words to descend? If not, return the value
101 // we're at right now.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 if (match.second.empty()) {
103 return value;
104 } else {
105 return value->GetObjectForDotSeparatedPath(match.second);
106 }
107 }
108 return ObjectSP();
109 }
110
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000111 if (this->GetType() == lldb::eStructuredDataTypeArray) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000113 if (match.second.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 return this->shared_from_this();
115 }
116 errno = 0;
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000117 uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (errno == 0) {
119 return this->GetAsArray()->GetItemAtIndex(val);
120 }
121 return ObjectSP();
122 }
123
124 return this->shared_from_this();
125}
126
127void StructuredData::Object::DumpToStdout(bool pretty_print) const {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000128 json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
129 Serialize(stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130}
131
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000132void StructuredData::Array::Serialize(json::OStream &s) const {
133 s.arrayBegin();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 for (const auto &item_sp : m_items) {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000135 item_sp->Serialize(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 }
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000137 s.arrayEnd();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138}
139
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000140void StructuredData::Integer::Serialize(json::OStream &s) const {
141 s.value(static_cast<int64_t>(m_value));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142}
143
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000144void StructuredData::Float::Serialize(json::OStream &s) const {
145 s.value(m_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146}
147
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000148void StructuredData::Boolean::Serialize(json::OStream &s) const {
149 s.value(m_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150}
151
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000152void StructuredData::String::Serialize(json::OStream &s) const {
153 s.value(m_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154}
155
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000156void StructuredData::Dictionary::Serialize(json::OStream &s) const {
157 s.objectBegin();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 for (const auto &pair : m_dict) {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000159 s.attributeBegin(pair.first.AsCString());
160 pair.second->Serialize(s);
161 s.attributeEnd();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 }
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000163 s.objectEnd();
Jason Molenda705b1802014-06-13 02:37:02 +0000164}
165
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000166void StructuredData::Null::Serialize(json::OStream &s) const {
167 s.value(nullptr);
Jason Molenda705b1802014-06-13 02:37:02 +0000168}
Zachary Turner0641ca12015-03-17 20:04:04 +0000169
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000170void StructuredData::Generic::Serialize(json::OStream &s) const {
171 s.value(llvm::formatv("{0:X}", m_object));
Zachary Turner0641ca12015-03-17 20:04:04 +0000172}