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