Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- StructuredData.cpp ------------------------------------------------===// |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Pavel Labath | f2a8bcc | 2017-06-27 10:45:31 +0000 | [diff] [blame] | 9 | #include "lldb/Utility/StructuredData.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 10 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 11 | #include "lldb/Utility/Status.h" |
Eugene Zemtsov | 11c0aab | 2017-11-17 03:28:58 +0000 | [diff] [blame] | 12 | #include "llvm/Support/MemoryBuffer.h" |
Pavel Labath | f2a8bcc | 2017-06-27 10:45:31 +0000 | [diff] [blame] | 13 | #include <cerrno> |
| 14 | #include <cstdlib> |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 15 | #include <inttypes.h> |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 16 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 17 | using namespace lldb_private; |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 19 | |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 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 | |
Jonas Devlieghere | 0d5fc82 | 2020-07-22 13:33:03 -0700 | [diff] [blame] | 24 | StructuredData::ObjectSP |
| 25 | StructuredData::ParseJSON(const std::string &json_text) { |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 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 | } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 33 | |
Jim Ingham | 01f1666 | 2016-09-14 19:07:35 +0000 | [diff] [blame] | 34 | StructuredData::ObjectSP |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 35 | StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Status &error) { |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 36 | StructuredData::ObjectSP return_sp; |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 37 | |
Pavel Labath | f2a8bcc | 2017-06-27 10:45:31 +0000 | [diff] [blame] | 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()); |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 43 | return return_sp; |
| 44 | } |
Walter Erquinigo | 74c9395 | 2020-08-17 17:21:52 -0700 | [diff] [blame] | 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(); |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 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 | |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 60 | if (auto s = value.getAsString()) |
| 61 | return std::make_shared<StructuredData::String>(*s); |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 62 | |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 63 | if (auto b = value.getAsBoolean()) |
| 64 | return std::make_shared<StructuredData::Boolean>(*b); |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 65 | |
Sam McCall | 751f5c8 | 2020-09-24 01:30:42 +0200 | [diff] [blame] | 66 | if (auto i = value.getAsInteger()) |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 67 | return std::make_shared<StructuredData::Integer>(*i); |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 68 | |
Sam McCall | fa69b60 | 2020-09-24 01:14:12 +0200 | [diff] [blame] | 69 | if (auto d = value.getAsNumber()) |
| 70 | return std::make_shared<StructuredData::Float>(*d); |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 71 | |
| 72 | return StructuredData::ObjectSP(); |
| 73 | } |
| 74 | |
| 75 | static StructuredData::ObjectSP ParseJSONObject(json::Object *object) { |
Jonas Devlieghere | a8f3ae7 | 2019-08-14 22:19:23 +0000 | [diff] [blame] | 76 | auto dict_up = std::make_unique<StructuredData::Dictionary>(); |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 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); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | } |
Jonas Devlieghere | d1e3b23 | 2020-01-13 18:23:39 -0800 | [diff] [blame] | 83 | return std::move(dict_up); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 86 | static StructuredData::ObjectSP ParseJSONArray(json::Array *array) { |
Jonas Devlieghere | a8f3ae7 | 2019-08-14 22:19:23 +0000 | [diff] [blame] | 87 | auto array_up = std::make_unique<StructuredData::Array>(); |
Jonas Devlieghere | 57b4688 | 2019-10-01 17:41:52 +0000 | [diff] [blame] | 88 | for (json::Value &value : *array) { |
| 89 | if (StructuredData::ObjectSP value_sp = ParseJSONValue(value)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | array_up->AddItem(value_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | } |
Jonas Devlieghere | d1e3b23 | 2020-01-13 18:23:39 -0800 | [diff] [blame] | 92 | return std::move(array_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | StructuredData::ObjectSP |
| 96 | StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) { |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 97 | if (this->GetType() == lldb::eStructuredDataTypeDictionary) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.'); |
| 99 | std::string key = match.first.str(); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 100 | ObjectSP value = this->GetAsDictionary()->GetValueForKey(key); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | if (value.get()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 102 | // Do we have additional words to descend? If not, return the value |
| 103 | // we're at right now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | if (match.second.empty()) { |
| 105 | return value; |
| 106 | } else { |
| 107 | return value->GetObjectForDotSeparatedPath(match.second); |
| 108 | } |
| 109 | } |
| 110 | return ObjectSP(); |
| 111 | } |
| 112 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 113 | if (this->GetType() == lldb::eStructuredDataTypeArray) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | std::pair<llvm::StringRef, llvm::StringRef> match = path.split('['); |
Jonas Devlieghere | 65e5e278 | 2018-12-13 00:15:17 +0000 | [diff] [blame] | 115 | if (match.second.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | return this->shared_from_this(); |
| 117 | } |
| 118 | errno = 0; |
Jonas Devlieghere | 65e5e278 | 2018-12-13 00:15:17 +0000 | [diff] [blame] | 119 | uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | if (errno == 0) { |
| 121 | return this->GetAsArray()->GetItemAtIndex(val); |
| 122 | } |
| 123 | return ObjectSP(); |
| 124 | } |
| 125 | |
| 126 | return this->shared_from_this(); |
| 127 | } |
| 128 | |
| 129 | void StructuredData::Object::DumpToStdout(bool pretty_print) const { |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 130 | json::OStream stream(llvm::outs(), pretty_print ? 2 : 0); |
| 131 | Serialize(stream); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 134 | void StructuredData::Array::Serialize(json::OStream &s) const { |
| 135 | s.arrayBegin(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | for (const auto &item_sp : m_items) { |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 137 | item_sp->Serialize(s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | } |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 139 | s.arrayEnd(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 142 | void StructuredData::Integer::Serialize(json::OStream &s) const { |
| 143 | s.value(static_cast<int64_t>(m_value)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 146 | void StructuredData::Float::Serialize(json::OStream &s) const { |
| 147 | s.value(m_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 150 | void StructuredData::Boolean::Serialize(json::OStream &s) const { |
| 151 | s.value(m_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 154 | void StructuredData::String::Serialize(json::OStream &s) const { |
| 155 | s.value(m_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 158 | void StructuredData::Dictionary::Serialize(json::OStream &s) const { |
| 159 | s.objectBegin(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | for (const auto &pair : m_dict) { |
Raphael Isemann | 642bc15 | 2020-02-11 09:05:37 +0100 | [diff] [blame] | 161 | s.attributeBegin(pair.first.GetStringRef()); |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 162 | pair.second->Serialize(s); |
| 163 | s.attributeEnd(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | } |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 165 | s.objectEnd(); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 168 | void StructuredData::Null::Serialize(json::OStream &s) const { |
| 169 | s.value(nullptr); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 170 | } |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 171 | |
Jonas Devlieghere | 2783d81 | 2019-10-01 17:41:48 +0000 | [diff] [blame] | 172 | void StructuredData::Generic::Serialize(json::OStream &s) const { |
| 173 | s.value(llvm::formatv("{0:X}", m_object)); |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 174 | } |