blob: 783a08082174436b434ee770c2db094ec3c02646 [file] [log] [blame]
Zachary Turnerc1564272016-11-16 21:15:24 +00001//===---------------------StructuredData.cpp ---------------------*- C++-*-===//
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 Turner666cc0b2017-03-04 01:30:05 +000010#include "lldb/Utility/DataBuffer.h"
Zachary Turner5713a052017-03-22 18:40:07 +000011#include "lldb/Utility/FileSpec.h"
Zachary Turner97206d52017-05-12 04:51:55 +000012#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000013#include "lldb/Utility/StreamString.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000014#include "llvm/ADT/STLExtras.h"
Eugene Zemtsov11c0aab2017-11-17 03:28:58 +000015#include "llvm/Support/MemoryBuffer.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000016#include <cerrno>
17#include <cstdlib>
Zachary Turner2f3df612017-04-06 21:28:29 +000018#include <inttypes.h>
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000019#include <limits>
Zachary Turner0641ca12015-03-17 20:04:04 +000020
Jason Molenda705b1802014-06-13 02:37:02 +000021using namespace lldb_private;
Jonas Devlieghere2783d812019-10-01 17:41:48 +000022using namespace llvm;
Jason Molenda705b1802014-06-13 02:37:02 +000023
Jonas Devlieghere57b46882019-10-01 17:41:52 +000024static StructuredData::ObjectSP ParseJSONValue(json::Value &value);
25static StructuredData::ObjectSP ParseJSONObject(json::Object *object);
26static StructuredData::ObjectSP ParseJSONArray(json::Array *array);
27
28StructuredData::ObjectSP StructuredData::ParseJSON(std::string json_text) {
29 llvm::Expected<json::Value> value = json::parse(json_text);
30 if (!value) {
31 llvm::consumeError(value.takeError());
32 return nullptr;
33 }
34 return ParseJSONValue(*value);
35}
Jason Molenda705b1802014-06-13 02:37:02 +000036
Jim Ingham01f16662016-09-14 19:07:35 +000037StructuredData::ObjectSP
Zachary Turner97206d52017-05-12 04:51:55 +000038StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000039 StructuredData::ObjectSP return_sp;
Jim Inghame14dc262016-09-12 23:10:56 +000040
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000041 auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.GetPath());
42 if (!buffer_or_error) {
43 error.SetErrorStringWithFormatv("could not open input file: {0} - {1}.",
44 input_spec.GetPath(),
45 buffer_or_error.getError().message());
Jim Inghame14dc262016-09-12 23:10:56 +000046 return return_sp;
47 }
Jonas Devlieghere57b46882019-10-01 17:41:52 +000048 return ParseJSON(buffer_or_error.get()->getBuffer().str());
Jim Inghame14dc262016-09-12 23:10:56 +000049}
50
Jonas Devlieghere57b46882019-10-01 17:41:52 +000051static StructuredData::ObjectSP ParseJSONValue(json::Value &value) {
52 if (json::Object *O = value.getAsObject())
53 return ParseJSONObject(O);
54
55 if (json::Array *A = value.getAsArray())
56 return ParseJSONArray(A);
57
58 std::string s;
59 if (json::fromJSON(value, s))
60 return std::make_shared<StructuredData::String>(s);
61
62 bool b;
63 if (json::fromJSON(value, b))
64 return std::make_shared<StructuredData::Boolean>(b);
65
66 int64_t i;
67 if (json::fromJSON(value, i))
68 return std::make_shared<StructuredData::Integer>(i);
69
70 double d;
71 if (json::fromJSON(value, d))
72 return std::make_shared<StructuredData::Float>(d);
73
74 return StructuredData::ObjectSP();
75}
76
77static StructuredData::ObjectSP ParseJSONObject(json::Object *object) {
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +000078 auto dict_up = std::make_unique<StructuredData::Dictionary>();
Jonas Devlieghere57b46882019-10-01 17:41:52 +000079 for (auto &KV : *object) {
80 StringRef key = KV.first;
81 json::Value value = KV.second;
82 if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
83 dict_up->AddItem(key, value_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 }
Jonas Devlieghere57b46882019-10-01 17:41:52 +000085 return dict_up;
Jason Molenda705b1802014-06-13 02:37:02 +000086}
87
Jonas Devlieghere57b46882019-10-01 17:41:52 +000088static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +000089 auto array_up = std::make_unique<StructuredData::Array>();
Jonas Devlieghere57b46882019-10-01 17:41:52 +000090 for (json::Value &value : *array) {
91 if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 array_up->AddItem(value_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 }
Jonas Devlieghere57b46882019-10-01 17:41:52 +000094 return array_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +000095}
96
97StructuredData::ObjectSP
98StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +000099 if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
101 std::string key = match.first.str();
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000102 ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (value.get()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000104 // Do we have additional words to descend? If not, return the value
105 // we're at right now.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 if (match.second.empty()) {
107 return value;
108 } else {
109 return value->GetObjectForDotSeparatedPath(match.second);
110 }
111 }
112 return ObjectSP();
113 }
114
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000115 if (this->GetType() == lldb::eStructuredDataTypeArray) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000117 if (match.second.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 return this->shared_from_this();
119 }
120 errno = 0;
Jonas Devlieghere65e5e2782018-12-13 00:15:17 +0000121 uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 if (errno == 0) {
123 return this->GetAsArray()->GetItemAtIndex(val);
124 }
125 return ObjectSP();
126 }
127
128 return this->shared_from_this();
129}
130
131void StructuredData::Object::DumpToStdout(bool pretty_print) const {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000132 json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
133 Serialize(stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134}
135
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000136void StructuredData::Array::Serialize(json::OStream &s) const {
137 s.arrayBegin();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 for (const auto &item_sp : m_items) {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000139 item_sp->Serialize(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 }
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000141 s.arrayEnd();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142}
143
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000144void StructuredData::Integer::Serialize(json::OStream &s) const {
145 s.value(static_cast<int64_t>(m_value));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146}
147
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000148void StructuredData::Float::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::Boolean::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::String::Serialize(json::OStream &s) const {
157 s.value(m_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158}
159
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000160void StructuredData::Dictionary::Serialize(json::OStream &s) const {
161 s.objectBegin();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 for (const auto &pair : m_dict) {
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000163 s.attributeBegin(pair.first.AsCString());
164 pair.second->Serialize(s);
165 s.attributeEnd();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 }
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000167 s.objectEnd();
Jason Molenda705b1802014-06-13 02:37:02 +0000168}
169
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000170void StructuredData::Null::Serialize(json::OStream &s) const {
171 s.value(nullptr);
Jason Molenda705b1802014-06-13 02:37:02 +0000172}
Zachary Turner0641ca12015-03-17 20:04:04 +0000173
Jonas Devlieghere2783d812019-10-01 17:41:48 +0000174void StructuredData::Generic::Serialize(json::OStream &s) const {
175 s.value(llvm::formatv("{0:X}", m_object));
Zachary Turner0641ca12015-03-17 20:04:04 +0000176}