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