Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1 | //===---------------------StructuredData.cpp ---------------------*- C++ |
| 2 | //-*-===// |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "lldb/Core/StructuredData.h" |
| 12 | |
| 13 | #include <errno.h> |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 14 | #include <inttypes.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 16 | |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 17 | #include "lldb/Core/DataBuffer.h" |
| 18 | #include "lldb/Core/Error.h" |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 19 | #include "lldb/Core/StreamString.h" |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 20 | #include "lldb/Host/File.h" |
| 21 | #include "lldb/Host/FileSpec.h" |
Greg Clayton | 98424c4 | 2015-07-06 23:40:40 +0000 | [diff] [blame] | 22 | #include "lldb/Host/StringConvert.h" |
| 23 | #include "lldb/Utility/JSON.h" |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 24 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 25 | using namespace lldb_private; |
| 26 | |
Greg Clayton | 98424c4 | 2015-07-06 23:40:40 +0000 | [diff] [blame] | 27 | //---------------------------------------------------------------------- |
| 28 | // Functions that use a JSONParser to parse JSON into StructuredData |
| 29 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser); |
| 31 | static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser); |
| 32 | static StructuredData::ObjectSP ParseJSONArray(JSONParser &json_parser); |
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 |
| 35 | StructuredData::ParseJSONFromFile(const FileSpec &input_spec, Error &error) { |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 36 | StructuredData::ObjectSP return_sp; |
| 37 | if (!input_spec.Exists()) { |
| 38 | error.SetErrorStringWithFormat("input file %s does not exist.", |
| 39 | input_spec.GetPath().c_str()); |
| 40 | return return_sp; |
| 41 | } |
| 42 | |
| 43 | File input_file(nullptr, File::OpenOptions::eOpenOptionRead, |
| 44 | lldb::eFilePermissionsUserRead); |
| 45 | std::string input_path = input_spec.GetPath(); |
| 46 | error = |
| 47 | input_file.Open(input_path.c_str(), File::OpenOptions::eOpenOptionRead, |
| 48 | lldb::eFilePermissionsUserRead); |
| 49 | |
| 50 | if (!error.Success()) { |
| 51 | error.SetErrorStringWithFormat("could not open input file: %s - %s.", |
| 52 | input_spec.GetPath().c_str(), |
| 53 | error.AsCString()); |
| 54 | return return_sp; |
| 55 | } |
| 56 | |
| 57 | lldb::DataBufferSP input_data; |
Tamas Berghammer | f8199a7 | 2016-09-13 09:27:21 +0000 | [diff] [blame] | 58 | size_t num_bytes = std::numeric_limits<size_t>::max(); |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 59 | off_t offset = 0; |
| 60 | error = input_file.Read(num_bytes, offset, true, input_data); |
| 61 | if (!error.Success()) { |
| 62 | error.SetErrorStringWithFormat("could not read input file: %s - %s.", |
| 63 | input_spec.GetPath().c_str(), |
| 64 | error.AsCString()); |
| 65 | return return_sp; |
| 66 | } |
| 67 | JSONParser json_parser((char *)input_data->GetBytes()); |
| 68 | return_sp = ParseJSONValue(json_parser); |
| 69 | return return_sp; |
| 70 | } |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser) { |
| 73 | // The "JSONParser::Token::ObjectStart" token should have already been |
| 74 | // consumed |
| 75 | // by the time this function is called |
| 76 | std::unique_ptr<StructuredData::Dictionary> dict_up( |
| 77 | new StructuredData::Dictionary()); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | std::string value; |
| 80 | std::string key; |
| 81 | while (1) { |
| 82 | JSONParser::Token token = json_parser.GetToken(value); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | if (token == JSONParser::Token::String) { |
| 85 | key.swap(value); |
| 86 | token = json_parser.GetToken(value); |
| 87 | if (token == JSONParser::Token::Colon) { |
Greg Clayton | 98424c4 | 2015-07-06 23:40:40 +0000 | [diff] [blame] | 88 | StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser); |
| 89 | if (value_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | dict_up->AddItem(key, value_sp); |
Greg Clayton | 98424c4 | 2015-07-06 23:40:40 +0000 | [diff] [blame] | 91 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | } else if (token == JSONParser::Token::ObjectEnd) { |
| 95 | return StructuredData::ObjectSP(dict_up.release()); |
| 96 | } else if (token == JSONParser::Token::Comma) { |
| 97 | continue; |
| 98 | } else { |
| 99 | break; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 100 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | } |
| 102 | return StructuredData::ObjectSP(); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | static StructuredData::ObjectSP ParseJSONArray(JSONParser &json_parser) { |
| 106 | // The "JSONParser::Token::ObjectStart" token should have already been |
| 107 | // consumed |
| 108 | // by the time this function is called |
| 109 | std::unique_ptr<StructuredData::Array> array_up(new StructuredData::Array()); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | std::string value; |
| 112 | std::string key; |
| 113 | while (1) { |
| 114 | StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser); |
| 115 | if (value_sp) |
| 116 | array_up->AddItem(value_sp); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 117 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | break; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | JSONParser::Token token = json_parser.GetToken(value); |
| 121 | if (token == JSONParser::Token::Comma) { |
| 122 | continue; |
| 123 | } else if (token == JSONParser::Token::ArrayEnd) { |
| 124 | return StructuredData::ObjectSP(array_up.release()); |
| 125 | } else { |
| 126 | break; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 127 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | } |
| 129 | return StructuredData::ObjectSP(); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser) { |
| 133 | std::string value; |
| 134 | const JSONParser::Token token = json_parser.GetToken(value); |
| 135 | switch (token) { |
| 136 | case JSONParser::Token::ObjectStart: |
| 137 | return ParseJSONObject(json_parser); |
| 138 | |
| 139 | case JSONParser::Token::ArrayStart: |
| 140 | return ParseJSONArray(json_parser); |
| 141 | |
| 142 | case JSONParser::Token::Integer: { |
| 143 | bool success = false; |
| 144 | uint64_t uval = StringConvert::ToUInt64(value.c_str(), 0, 0, &success); |
| 145 | if (success) |
| 146 | return StructuredData::ObjectSP(new StructuredData::Integer(uval)); |
| 147 | } break; |
| 148 | |
| 149 | case JSONParser::Token::Float: { |
| 150 | bool success = false; |
| 151 | double val = StringConvert::ToDouble(value.c_str(), 0.0, &success); |
| 152 | if (success) |
| 153 | return StructuredData::ObjectSP(new StructuredData::Float(val)); |
| 154 | } break; |
| 155 | |
| 156 | case JSONParser::Token::String: |
| 157 | return StructuredData::ObjectSP(new StructuredData::String(value)); |
| 158 | |
| 159 | case JSONParser::Token::True: |
| 160 | case JSONParser::Token::False: |
| 161 | return StructuredData::ObjectSP( |
| 162 | new StructuredData::Boolean(token == JSONParser::Token::True)); |
| 163 | |
| 164 | case JSONParser::Token::Null: |
| 165 | return StructuredData::ObjectSP(new StructuredData::Null()); |
| 166 | |
| 167 | default: |
| 168 | break; |
| 169 | } |
| 170 | return StructuredData::ObjectSP(); |
| 171 | } |
| 172 | |
| 173 | StructuredData::ObjectSP StructuredData::ParseJSON(std::string json_text) { |
| 174 | JSONParser json_parser(json_text.c_str()); |
| 175 | StructuredData::ObjectSP object_sp = ParseJSONValue(json_parser); |
| 176 | return object_sp; |
| 177 | } |
| 178 | |
| 179 | StructuredData::ObjectSP |
| 180 | StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) { |
| 181 | if (this->GetType() == Type::eTypeDictionary) { |
| 182 | std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.'); |
| 183 | std::string key = match.first.str(); |
| 184 | ObjectSP value = this->GetAsDictionary()->GetValueForKey(key.c_str()); |
| 185 | if (value.get()) { |
| 186 | // Do we have additional words to descend? If not, return the |
| 187 | // value we're at right now. |
| 188 | if (match.second.empty()) { |
| 189 | return value; |
| 190 | } else { |
| 191 | return value->GetObjectForDotSeparatedPath(match.second); |
| 192 | } |
| 193 | } |
| 194 | return ObjectSP(); |
| 195 | } |
| 196 | |
| 197 | if (this->GetType() == Type::eTypeArray) { |
| 198 | std::pair<llvm::StringRef, llvm::StringRef> match = path.split('['); |
| 199 | if (match.second.size() == 0) { |
| 200 | return this->shared_from_this(); |
| 201 | } |
| 202 | errno = 0; |
| 203 | uint64_t val = strtoul(match.second.str().c_str(), NULL, 10); |
| 204 | if (errno == 0) { |
| 205 | return this->GetAsArray()->GetItemAtIndex(val); |
| 206 | } |
| 207 | return ObjectSP(); |
| 208 | } |
| 209 | |
| 210 | return this->shared_from_this(); |
| 211 | } |
| 212 | |
| 213 | void StructuredData::Object::DumpToStdout(bool pretty_print) const { |
| 214 | StreamString stream; |
| 215 | Dump(stream, pretty_print); |
| 216 | printf("%s\n", stream.GetString().c_str()); |
| 217 | } |
| 218 | |
| 219 | void StructuredData::Array::Dump(Stream &s, bool pretty_print) const { |
| 220 | bool first = true; |
| 221 | s << "["; |
| 222 | if (pretty_print) { |
| 223 | s << "\n"; |
| 224 | s.IndentMore(); |
| 225 | } |
| 226 | for (const auto &item_sp : m_items) { |
| 227 | if (first) { |
| 228 | first = false; |
| 229 | } else { |
| 230 | s << ","; |
| 231 | if (pretty_print) |
Jason Molenda | e62dda2 | 2016-07-21 22:50:01 +0000 | [diff] [blame] | 232 | s << "\n"; |
Jason Molenda | d9c9da5 | 2016-07-20 03:49:02 +0000 | [diff] [blame] | 233 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | |
| 235 | if (pretty_print) |
| 236 | s.Indent(); |
| 237 | item_sp->Dump(s, pretty_print); |
| 238 | } |
| 239 | if (pretty_print) { |
| 240 | s.IndentLess(); |
| 241 | s.EOL(); |
| 242 | s.Indent(); |
| 243 | } |
| 244 | s << "]"; |
| 245 | } |
| 246 | |
| 247 | void StructuredData::Integer::Dump(Stream &s, bool pretty_print) const { |
| 248 | s.Printf("%" PRIu64, m_value); |
| 249 | } |
| 250 | |
| 251 | void StructuredData::Float::Dump(Stream &s, bool pretty_print) const { |
| 252 | s.Printf("%lg", m_value); |
| 253 | } |
| 254 | |
| 255 | void StructuredData::Boolean::Dump(Stream &s, bool pretty_print) const { |
| 256 | if (m_value == true) |
| 257 | s.PutCString("true"); |
| 258 | else |
| 259 | s.PutCString("false"); |
| 260 | } |
| 261 | |
| 262 | void StructuredData::String::Dump(Stream &s, bool pretty_print) const { |
| 263 | std::string quoted; |
| 264 | const size_t strsize = m_value.size(); |
| 265 | for (size_t i = 0; i < strsize; ++i) { |
| 266 | char ch = m_value[i]; |
Pavel Labath | 2a66884 | 2016-09-22 15:26:43 +0000 | [diff] [blame^] | 267 | if (ch == '"' || ch == '\\') |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | quoted.push_back('\\'); |
| 269 | quoted.push_back(ch); |
| 270 | } |
| 271 | s.Printf("\"%s\"", quoted.c_str()); |
| 272 | } |
| 273 | |
| 274 | void StructuredData::Dictionary::Dump(Stream &s, bool pretty_print) const { |
| 275 | bool first = true; |
| 276 | s << "{"; |
| 277 | if (pretty_print) { |
| 278 | s << "\n"; |
| 279 | s.IndentMore(); |
| 280 | } |
| 281 | for (const auto &pair : m_dict) { |
| 282 | if (first) |
| 283 | first = false; |
| 284 | else { |
| 285 | s << ","; |
| 286 | if (pretty_print) |
| 287 | s << "\n"; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 288 | } |
Jason Molenda | d9c9da5 | 2016-07-20 03:49:02 +0000 | [diff] [blame] | 289 | if (pretty_print) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 290 | s.Indent(); |
| 291 | s << "\"" << pair.first.AsCString() << "\" : "; |
| 292 | pair.second->Dump(s, pretty_print); |
| 293 | } |
| 294 | if (pretty_print) { |
| 295 | s.IndentLess(); |
| 296 | s.EOL(); |
| 297 | s.Indent(); |
| 298 | } |
| 299 | s << "}"; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 302 | void StructuredData::Null::Dump(Stream &s, bool pretty_print) const { |
| 303 | s << "null"; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 304 | } |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 305 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | void StructuredData::Generic::Dump(Stream &s, bool pretty_print) const { |
| 307 | s << "0x" << m_object; |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 308 | } |