1 //=== JSON.cpp - JSON value, parsing and serialization - C++ -----------*-===// 2 // 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 6 // 7 //===---------------------------------------------------------------------===// 8 9 #include "llvm/Support/JSON.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/Support/ConvertUTF.h" 12 #include "llvm/Support/Error.h" 13 #include "llvm/Support/Format.h" 14 #include "llvm/Support/raw_ostream.h" 15 #include "llvm/Support/NativeFormatting.h" 16 #include <cctype> 17 18 namespace llvm { 19 namespace json { 20 21 Value &Object::operator[](const ObjectKey &K) { 22 return try_emplace(K, nullptr).first->getSecond(); 23 } 24 Value &Object::operator[](ObjectKey &&K) { 25 return try_emplace(std::move(K), nullptr).first->getSecond(); 26 } 27 Value *Object::get(StringRef K) { 28 auto I = find(K); 29 if (I == end()) 30 return nullptr; 31 return &I->second; 32 } 33 const Value *Object::get(StringRef K) const { 34 auto I = find(K); 35 if (I == end()) 36 return nullptr; 37 return &I->second; 38 } 39 llvm::Optional<std::nullptr_t> Object::getNull(StringRef K) const { 40 if (auto *V = get(K)) 41 return V->getAsNull(); 42 return llvm::None; 43 } 44 llvm::Optional<bool> Object::getBoolean(StringRef K) const { 45 if (auto *V = get(K)) 46 return V->getAsBoolean(); 47 return llvm::None; 48 } 49 llvm::Optional<double> Object::getNumber(StringRef K) const { 50 if (auto *V = get(K)) 51 return V->getAsNumber(); 52 return llvm::None; 53 } 54 llvm::Optional<int64_t> Object::getInteger(StringRef K) const { 55 if (auto *V = get(K)) 56 return V->getAsInteger(); 57 return llvm::None; 58 } 59 llvm::Optional<llvm::StringRef> Object::getString(StringRef K) const { 60 if (auto *V = get(K)) 61 return V->getAsString(); 62 return llvm::None; 63 } 64 const json::Object *Object::getObject(StringRef K) const { 65 if (auto *V = get(K)) 66 return V->getAsObject(); 67 return nullptr; 68 } 69 json::Object *Object::getObject(StringRef K) { 70 if (auto *V = get(K)) 71 return V->getAsObject(); 72 return nullptr; 73 } 74 const json::Array *Object::getArray(StringRef K) const { 75 if (auto *V = get(K)) 76 return V->getAsArray(); 77 return nullptr; 78 } 79 json::Array *Object::getArray(StringRef K) { 80 if (auto *V = get(K)) 81 return V->getAsArray(); 82 return nullptr; 83 } 84 bool operator==(const Object &LHS, const Object &RHS) { 85 if (LHS.size() != RHS.size()) 86 return false; 87 for (const auto &L : LHS) { 88 auto R = RHS.find(L.first); 89 if (R == RHS.end() || L.second != R->second) 90 return false; 91 } 92 return true; 93 } 94 95 Array::Array(std::initializer_list<Value> Elements) { 96 V.reserve(Elements.size()); 97 for (const Value &V : Elements) { 98 emplace_back(nullptr); 99 back().moveFrom(std::move(V)); 100 } 101 } 102 103 Value::Value(std::initializer_list<Value> Elements) 104 : Value(json::Array(Elements)) {} 105 106 void Value::copyFrom(const Value &M) { 107 Type = M.Type; 108 switch (Type) { 109 case T_Null: 110 case T_Boolean: 111 case T_Double: 112 case T_Integer: 113 case T_UINT64: 114 memcpy(&Union, &M.Union, sizeof(Union)); 115 break; 116 case T_StringRef: 117 create<StringRef>(M.as<StringRef>()); 118 break; 119 case T_String: 120 create<std::string>(M.as<std::string>()); 121 break; 122 case T_Object: 123 create<json::Object>(M.as<json::Object>()); 124 break; 125 case T_Array: 126 create<json::Array>(M.as<json::Array>()); 127 break; 128 } 129 } 130 131 void Value::moveFrom(const Value &&M) { 132 Type = M.Type; 133 switch (Type) { 134 case T_Null: 135 case T_Boolean: 136 case T_Double: 137 case T_Integer: 138 case T_UINT64: 139 memcpy(&Union, &M.Union, sizeof(Union)); 140 break; 141 case T_StringRef: 142 create<StringRef>(M.as<StringRef>()); 143 break; 144 case T_String: 145 create<std::string>(std::move(M.as<std::string>())); 146 M.Type = T_Null; 147 break; 148 case T_Object: 149 create<json::Object>(std::move(M.as<json::Object>())); 150 M.Type = T_Null; 151 break; 152 case T_Array: 153 create<json::Array>(std::move(M.as<json::Array>())); 154 M.Type = T_Null; 155 break; 156 } 157 } 158 159 void Value::destroy() { 160 switch (Type) { 161 case T_Null: 162 case T_Boolean: 163 case T_Double: 164 case T_Integer: 165 case T_UINT64: 166 break; 167 case T_StringRef: 168 as<StringRef>().~StringRef(); 169 break; 170 case T_String: 171 as<std::string>().~basic_string(); 172 break; 173 case T_Object: 174 as<json::Object>().~Object(); 175 break; 176 case T_Array: 177 as<json::Array>().~Array(); 178 break; 179 } 180 } 181 182 bool operator==(const Value &L, const Value &R) { 183 if (L.kind() != R.kind()) 184 return false; 185 switch (L.kind()) { 186 case Value::Null: 187 return *L.getAsNull() == *R.getAsNull(); 188 case Value::Boolean: 189 return *L.getAsBoolean() == *R.getAsBoolean(); 190 case Value::Number: 191 // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 192 // The same integer must convert to the same double, per the standard. 193 // However we see 64-vs-80-bit precision comparisons with gcc-7 -O3 -m32. 194 // So we avoid floating point promotion for exact comparisons. 195 if (L.Type == Value::T_Integer || R.Type == Value::T_Integer) 196 return L.getAsInteger() == R.getAsInteger(); 197 return *L.getAsNumber() == *R.getAsNumber(); 198 case Value::String: 199 return *L.getAsString() == *R.getAsString(); 200 case Value::Array: 201 return *L.getAsArray() == *R.getAsArray(); 202 case Value::Object: 203 return *L.getAsObject() == *R.getAsObject(); 204 } 205 llvm_unreachable("Unknown value kind"); 206 } 207 208 void Path::report(llvm::StringLiteral Msg) { 209 // Walk up to the root context, and count the number of segments. 210 unsigned Count = 0; 211 const Path *P; 212 for (P = this; P->Parent != nullptr; P = P->Parent) 213 ++Count; 214 Path::Root *R = P->Seg.root(); 215 // Fill in the error message and copy the path (in reverse order). 216 R->ErrorMessage = Msg; 217 R->ErrorPath.resize(Count); 218 auto It = R->ErrorPath.begin(); 219 for (P = this; P->Parent != nullptr; P = P->Parent) 220 *It++ = P->Seg; 221 } 222 223 Error Path::Root::getError() const { 224 std::string S; 225 raw_string_ostream OS(S); 226 OS << (ErrorMessage.empty() ? "invalid JSON contents" : ErrorMessage); 227 if (ErrorPath.empty()) { 228 if (!Name.empty()) 229 OS << " when parsing " << Name; 230 } else { 231 OS << " at " << (Name.empty() ? "(root)" : Name); 232 for (const Path::Segment &S : llvm::reverse(ErrorPath)) { 233 if (S.isField()) 234 OS << '.' << S.field(); 235 else 236 OS << '[' << S.index() << ']'; 237 } 238 } 239 return createStringError(llvm::inconvertibleErrorCode(), OS.str()); 240 } 241 242 namespace { 243 244 std::vector<const Object::value_type *> sortedElements(const Object &O) { 245 std::vector<const Object::value_type *> Elements; 246 for (const auto &E : O) 247 Elements.push_back(&E); 248 llvm::sort(Elements, 249 [](const Object::value_type *L, const Object::value_type *R) { 250 return L->first < R->first; 251 }); 252 return Elements; 253 } 254 255 // Prints a one-line version of a value that isn't our main focus. 256 // We interleave writes to OS and JOS, exploiting the lack of extra buffering. 257 // This is OK as we own the implementation. 258 void abbreviate(const Value &V, OStream &JOS) { 259 switch (V.kind()) { 260 case Value::Array: 261 JOS.rawValue(V.getAsArray()->empty() ? "[]" : "[ ... ]"); 262 break; 263 case Value::Object: 264 JOS.rawValue(V.getAsObject()->empty() ? "{}" : "{ ... }"); 265 break; 266 case Value::String: { 267 llvm::StringRef S = *V.getAsString(); 268 if (S.size() < 40) { 269 JOS.value(V); 270 } else { 271 std::string Truncated = fixUTF8(S.take_front(37)); 272 Truncated.append("..."); 273 JOS.value(Truncated); 274 } 275 break; 276 } 277 default: 278 JOS.value(V); 279 } 280 } 281 282 // Prints a semi-expanded version of a value that is our main focus. 283 // Array/Object entries are printed, but not recursively as they may be huge. 284 void abbreviateChildren(const Value &V, OStream &JOS) { 285 switch (V.kind()) { 286 case Value::Array: 287 JOS.array([&] { 288 for (const auto &I : *V.getAsArray()) 289 abbreviate(I, JOS); 290 }); 291 break; 292 case Value::Object: 293 JOS.object([&] { 294 for (const auto *KV : sortedElements(*V.getAsObject())) { 295 JOS.attributeBegin(KV->first); 296 abbreviate(KV->second, JOS); 297 JOS.attributeEnd(); 298 } 299 }); 300 break; 301 default: 302 JOS.value(V); 303 } 304 } 305 306 } // namespace 307 308 void Path::Root::printErrorContext(const Value &R, raw_ostream &OS) const { 309 OStream JOS(OS, /*IndentSize=*/2); 310 // PrintValue recurses down the path, printing the ancestors of our target. 311 // Siblings of nodes along the path are printed with abbreviate(), and the 312 // target itself is printed with the somewhat richer abbreviateChildren(). 313 // 'Recurse' is the lambda itself, to allow recursive calls. 314 auto PrintValue = [&](const Value &V, ArrayRef<Segment> Path, auto &Recurse) { 315 // Print the target node itself, with the error as a comment. 316 // Also used if we can't follow our path, e.g. it names a field that 317 // *should* exist but doesn't. 318 auto HighlightCurrent = [&] { 319 std::string Comment = "error: "; 320 Comment.append(ErrorMessage.data(), ErrorMessage.size()); 321 JOS.comment(Comment); 322 abbreviateChildren(V, JOS); 323 }; 324 if (Path.empty()) // We reached our target. 325 return HighlightCurrent(); 326 const Segment &S = Path.back(); // Path is in reverse order. 327 if (S.isField()) { 328 // Current node is an object, path names a field. 329 llvm::StringRef FieldName = S.field(); 330 const Object *O = V.getAsObject(); 331 if (!O || !O->get(FieldName)) 332 return HighlightCurrent(); 333 JOS.object([&] { 334 for (const auto *KV : sortedElements(*O)) { 335 JOS.attributeBegin(KV->first); 336 if (FieldName.equals(KV->first)) 337 Recurse(KV->second, Path.drop_back(), Recurse); 338 else 339 abbreviate(KV->second, JOS); 340 JOS.attributeEnd(); 341 } 342 }); 343 } else { 344 // Current node is an array, path names an element. 345 const Array *A = V.getAsArray(); 346 if (!A || S.index() >= A->size()) 347 return HighlightCurrent(); 348 JOS.array([&] { 349 unsigned Current = 0; 350 for (const auto &V : *A) { 351 if (Current++ == S.index()) 352 Recurse(V, Path.drop_back(), Recurse); 353 else 354 abbreviate(V, JOS); 355 } 356 }); 357 } 358 }; 359 PrintValue(R, ErrorPath, PrintValue); 360 } 361 362 namespace { 363 // Simple recursive-descent JSON parser. 364 class Parser { 365 public: 366 Parser(StringRef JSON) 367 : Start(JSON.begin()), P(JSON.begin()), End(JSON.end()) {} 368 369 bool checkUTF8() { 370 size_t ErrOffset; 371 if (isUTF8(StringRef(Start, End - Start), &ErrOffset)) 372 return true; 373 P = Start + ErrOffset; // For line/column calculation. 374 return parseError("Invalid UTF-8 sequence"); 375 } 376 377 bool parseValue(Value &Out); 378 379 bool assertEnd() { 380 eatWhitespace(); 381 if (P == End) 382 return true; 383 return parseError("Text after end of document"); 384 } 385 386 Error takeError() { 387 assert(Err); 388 return std::move(*Err); 389 } 390 391 private: 392 void eatWhitespace() { 393 while (P != End && (*P == ' ' || *P == '\r' || *P == '\n' || *P == '\t')) 394 ++P; 395 } 396 397 // On invalid syntax, parseX() functions return false and set Err. 398 bool parseNumber(char First, Value &Out); 399 bool parseString(std::string &Out); 400 bool parseUnicode(std::string &Out); 401 bool parseError(const char *Msg); // always returns false 402 403 char next() { return P == End ? 0 : *P++; } 404 char peek() { return P == End ? 0 : *P; } 405 static bool isNumber(char C) { 406 return C == '0' || C == '1' || C == '2' || C == '3' || C == '4' || 407 C == '5' || C == '6' || C == '7' || C == '8' || C == '9' || 408 C == 'e' || C == 'E' || C == '+' || C == '-' || C == '.'; 409 } 410 411 Optional<Error> Err; 412 const char *Start, *P, *End; 413 }; 414 415 bool Parser::parseValue(Value &Out) { 416 eatWhitespace(); 417 if (P == End) 418 return parseError("Unexpected EOF"); 419 switch (char C = next()) { 420 // Bare null/true/false are easy - first char identifies them. 421 case 'n': 422 Out = nullptr; 423 return (next() == 'u' && next() == 'l' && next() == 'l') || 424 parseError("Invalid JSON value (null?)"); 425 case 't': 426 Out = true; 427 return (next() == 'r' && next() == 'u' && next() == 'e') || 428 parseError("Invalid JSON value (true?)"); 429 case 'f': 430 Out = false; 431 return (next() == 'a' && next() == 'l' && next() == 's' && next() == 'e') || 432 parseError("Invalid JSON value (false?)"); 433 case '"': { 434 std::string S; 435 if (parseString(S)) { 436 Out = std::move(S); 437 return true; 438 } 439 return false; 440 } 441 case '[': { 442 Out = Array{}; 443 Array &A = *Out.getAsArray(); 444 eatWhitespace(); 445 if (peek() == ']') { 446 ++P; 447 return true; 448 } 449 for (;;) { 450 A.emplace_back(nullptr); 451 if (!parseValue(A.back())) 452 return false; 453 eatWhitespace(); 454 switch (next()) { 455 case ',': 456 eatWhitespace(); 457 continue; 458 case ']': 459 return true; 460 default: 461 return parseError("Expected , or ] after array element"); 462 } 463 } 464 } 465 case '{': { 466 Out = Object{}; 467 Object &O = *Out.getAsObject(); 468 eatWhitespace(); 469 if (peek() == '}') { 470 ++P; 471 return true; 472 } 473 for (;;) { 474 if (next() != '"') 475 return parseError("Expected object key"); 476 std::string K; 477 if (!parseString(K)) 478 return false; 479 eatWhitespace(); 480 if (next() != ':') 481 return parseError("Expected : after object key"); 482 eatWhitespace(); 483 if (!parseValue(O[std::move(K)])) 484 return false; 485 eatWhitespace(); 486 switch (next()) { 487 case ',': 488 eatWhitespace(); 489 continue; 490 case '}': 491 return true; 492 default: 493 return parseError("Expected , or } after object property"); 494 } 495 } 496 } 497 default: 498 if (isNumber(C)) 499 return parseNumber(C, Out); 500 return parseError("Invalid JSON value"); 501 } 502 } 503 504 bool Parser::parseNumber(char First, Value &Out) { 505 // Read the number into a string. (Must be null-terminated for strto*). 506 SmallString<24> S; 507 S.push_back(First); 508 while (isNumber(peek())) 509 S.push_back(next()); 510 char *End; 511 // Try first to parse as integer, and if so preserve full 64 bits. 512 // strtoll returns long long >= 64 bits, so check it's in range too. 513 auto I = std::strtoll(S.c_str(), &End, 10); 514 if (End == S.end() && I >= std::numeric_limits<int64_t>::min() && 515 I <= std::numeric_limits<int64_t>::max()) { 516 Out = int64_t(I); 517 return true; 518 } 519 // If it's not an integer 520 Out = std::strtod(S.c_str(), &End); 521 return End == S.end() || parseError("Invalid JSON value (number?)"); 522 } 523 524 bool Parser::parseString(std::string &Out) { 525 // leading quote was already consumed. 526 for (char C = next(); C != '"'; C = next()) { 527 if (LLVM_UNLIKELY(P == End)) 528 return parseError("Unterminated string"); 529 if (LLVM_UNLIKELY((C & 0x1f) == C)) 530 return parseError("Control character in string"); 531 if (LLVM_LIKELY(C != '\\')) { 532 Out.push_back(C); 533 continue; 534 } 535 // Handle escape sequence. 536 switch (C = next()) { 537 case '"': 538 case '\\': 539 case '/': 540 Out.push_back(C); 541 break; 542 case 'b': 543 Out.push_back('\b'); 544 break; 545 case 'f': 546 Out.push_back('\f'); 547 break; 548 case 'n': 549 Out.push_back('\n'); 550 break; 551 case 'r': 552 Out.push_back('\r'); 553 break; 554 case 't': 555 Out.push_back('\t'); 556 break; 557 case 'u': 558 if (!parseUnicode(Out)) 559 return false; 560 break; 561 default: 562 return parseError("Invalid escape sequence"); 563 } 564 } 565 return true; 566 } 567 568 static void encodeUtf8(uint32_t Rune, std::string &Out) { 569 if (Rune < 0x80) { 570 Out.push_back(Rune & 0x7F); 571 } else if (Rune < 0x800) { 572 uint8_t FirstByte = 0xC0 | ((Rune & 0x7C0) >> 6); 573 uint8_t SecondByte = 0x80 | (Rune & 0x3F); 574 Out.push_back(FirstByte); 575 Out.push_back(SecondByte); 576 } else if (Rune < 0x10000) { 577 uint8_t FirstByte = 0xE0 | ((Rune & 0xF000) >> 12); 578 uint8_t SecondByte = 0x80 | ((Rune & 0xFC0) >> 6); 579 uint8_t ThirdByte = 0x80 | (Rune & 0x3F); 580 Out.push_back(FirstByte); 581 Out.push_back(SecondByte); 582 Out.push_back(ThirdByte); 583 } else if (Rune < 0x110000) { 584 uint8_t FirstByte = 0xF0 | ((Rune & 0x1F0000) >> 18); 585 uint8_t SecondByte = 0x80 | ((Rune & 0x3F000) >> 12); 586 uint8_t ThirdByte = 0x80 | ((Rune & 0xFC0) >> 6); 587 uint8_t FourthByte = 0x80 | (Rune & 0x3F); 588 Out.push_back(FirstByte); 589 Out.push_back(SecondByte); 590 Out.push_back(ThirdByte); 591 Out.push_back(FourthByte); 592 } else { 593 llvm_unreachable("Invalid codepoint"); 594 } 595 } 596 597 // Parse a UTF-16 \uNNNN escape sequence. "\u" has already been consumed. 598 // May parse several sequential escapes to ensure proper surrogate handling. 599 // We do not use ConvertUTF.h, it can't accept and replace unpaired surrogates. 600 // These are invalid Unicode but valid JSON (RFC 8259, section 8.2). 601 bool Parser::parseUnicode(std::string &Out) { 602 // Invalid UTF is not a JSON error (RFC 8529§8.2). It gets replaced by U+FFFD. 603 auto Invalid = [&] { Out.append(/* UTF-8 */ {'\xef', '\xbf', '\xbd'}); }; 604 // Decodes 4 hex digits from the stream into Out, returns false on error. 605 auto Parse4Hex = [this](uint16_t &Out) -> bool { 606 Out = 0; 607 char Bytes[] = {next(), next(), next(), next()}; 608 for (unsigned char C : Bytes) { 609 if (!std::isxdigit(C)) 610 return parseError("Invalid \\u escape sequence"); 611 Out <<= 4; 612 Out |= (C > '9') ? (C & ~0x20) - 'A' + 10 : (C - '0'); 613 } 614 return true; 615 }; 616 uint16_t First; // UTF-16 code unit from the first \u escape. 617 if (!Parse4Hex(First)) 618 return false; 619 620 // We loop to allow proper surrogate-pair error handling. 621 while (true) { 622 // Case 1: the UTF-16 code unit is already a codepoint in the BMP. 623 if (LLVM_LIKELY(First < 0xD800 || First >= 0xE000)) { 624 encodeUtf8(First, Out); 625 return true; 626 } 627 628 // Case 2: it's an (unpaired) trailing surrogate. 629 if (LLVM_UNLIKELY(First >= 0xDC00)) { 630 Invalid(); 631 return true; 632 } 633 634 // Case 3: it's a leading surrogate. We expect a trailing one next. 635 // Case 3a: there's no trailing \u escape. Don't advance in the stream. 636 if (LLVM_UNLIKELY(P + 2 > End || *P != '\\' || *(P + 1) != 'u')) { 637 Invalid(); // Leading surrogate was unpaired. 638 return true; 639 } 640 P += 2; 641 uint16_t Second; 642 if (!Parse4Hex(Second)) 643 return false; 644 // Case 3b: there was another \u escape, but it wasn't a trailing surrogate. 645 if (LLVM_UNLIKELY(Second < 0xDC00 || Second >= 0xE000)) { 646 Invalid(); // Leading surrogate was unpaired. 647 First = Second; // Second escape still needs to be processed. 648 continue; 649 } 650 // Case 3c: a valid surrogate pair encoding an astral codepoint. 651 encodeUtf8(0x10000 | ((First - 0xD800) << 10) | (Second - 0xDC00), Out); 652 return true; 653 } 654 } 655 656 bool Parser::parseError(const char *Msg) { 657 int Line = 1; 658 const char *StartOfLine = Start; 659 for (const char *X = Start; X < P; ++X) { 660 if (*X == 0x0A) { 661 ++Line; 662 StartOfLine = X + 1; 663 } 664 } 665 Err.emplace( 666 std::make_unique<ParseError>(Msg, Line, P - StartOfLine, P - Start)); 667 return false; 668 } 669 } // namespace 670 671 Expected<Value> parse(StringRef JSON) { 672 Parser P(JSON); 673 Value E = nullptr; 674 if (P.checkUTF8()) 675 if (P.parseValue(E)) 676 if (P.assertEnd()) 677 return std::move(E); 678 return P.takeError(); 679 } 680 char ParseError::ID = 0; 681 682 bool isUTF8(llvm::StringRef S, size_t *ErrOffset) { 683 // Fast-path for ASCII, which is valid UTF-8. 684 if (LLVM_LIKELY(isASCII(S))) 685 return true; 686 687 const UTF8 *Data = reinterpret_cast<const UTF8 *>(S.data()), *Rest = Data; 688 if (LLVM_LIKELY(isLegalUTF8String(&Rest, Data + S.size()))) 689 return true; 690 691 if (ErrOffset) 692 *ErrOffset = Rest - Data; 693 return false; 694 } 695 696 std::string fixUTF8(llvm::StringRef S) { 697 // This isn't particularly efficient, but is only for error-recovery. 698 std::vector<UTF32> Codepoints(S.size()); // 1 codepoint per byte suffices. 699 const UTF8 *In8 = reinterpret_cast<const UTF8 *>(S.data()); 700 UTF32 *Out32 = Codepoints.data(); 701 ConvertUTF8toUTF32(&In8, In8 + S.size(), &Out32, Out32 + Codepoints.size(), 702 lenientConversion); 703 Codepoints.resize(Out32 - Codepoints.data()); 704 std::string Res(4 * Codepoints.size(), 0); // 4 bytes per codepoint suffice 705 const UTF32 *In32 = Codepoints.data(); 706 UTF8 *Out8 = reinterpret_cast<UTF8 *>(&Res[0]); 707 ConvertUTF32toUTF8(&In32, In32 + Codepoints.size(), &Out8, Out8 + Res.size(), 708 strictConversion); 709 Res.resize(reinterpret_cast<char *>(Out8) - Res.data()); 710 return Res; 711 } 712 713 static void quote(llvm::raw_ostream &OS, llvm::StringRef S) { 714 OS << '\"'; 715 for (unsigned char C : S) { 716 if (C == 0x22 || C == 0x5C) 717 OS << '\\'; 718 if (C >= 0x20) { 719 OS << C; 720 continue; 721 } 722 OS << '\\'; 723 switch (C) { 724 // A few characters are common enough to make short escapes worthwhile. 725 case '\t': 726 OS << 't'; 727 break; 728 case '\n': 729 OS << 'n'; 730 break; 731 case '\r': 732 OS << 'r'; 733 break; 734 default: 735 OS << 'u'; 736 llvm::write_hex(OS, C, llvm::HexPrintStyle::Lower, 4); 737 break; 738 } 739 } 740 OS << '\"'; 741 } 742 743 void llvm::json::OStream::value(const Value &V) { 744 switch (V.kind()) { 745 case Value::Null: 746 valueBegin(); 747 OS << "null"; 748 return; 749 case Value::Boolean: 750 valueBegin(); 751 OS << (*V.getAsBoolean() ? "true" : "false"); 752 return; 753 case Value::Number: 754 valueBegin(); 755 if (V.Type == Value::T_Integer) 756 OS << *V.getAsInteger(); 757 else if (V.Type == Value::T_UINT64) 758 OS << *V.getAsUINT64(); 759 else 760 OS << format("%.*g", std::numeric_limits<double>::max_digits10, 761 *V.getAsNumber()); 762 return; 763 case Value::String: 764 valueBegin(); 765 quote(OS, *V.getAsString()); 766 return; 767 case Value::Array: 768 return array([&] { 769 for (const Value &E : *V.getAsArray()) 770 value(E); 771 }); 772 case Value::Object: 773 return object([&] { 774 for (const Object::value_type *E : sortedElements(*V.getAsObject())) 775 attribute(E->first, E->second); 776 }); 777 } 778 } 779 780 void llvm::json::OStream::valueBegin() { 781 assert(Stack.back().Ctx != Object && "Only attributes allowed here"); 782 if (Stack.back().HasValue) { 783 assert(Stack.back().Ctx != Singleton && "Only one value allowed here"); 784 OS << ','; 785 } 786 if (Stack.back().Ctx == Array) 787 newline(); 788 flushComment(); 789 Stack.back().HasValue = true; 790 } 791 792 void OStream::comment(llvm::StringRef Comment) { 793 assert(PendingComment.empty() && "Only one comment per value!"); 794 PendingComment = Comment; 795 } 796 797 void OStream::flushComment() { 798 if (PendingComment.empty()) 799 return; 800 OS << (IndentSize ? "/* " : "/*"); 801 // Be sure not to accidentally emit "*/". Transform to "* /". 802 while (!PendingComment.empty()) { 803 auto Pos = PendingComment.find("*/"); 804 if (Pos == StringRef::npos) { 805 OS << PendingComment; 806 PendingComment = ""; 807 } else { 808 OS << PendingComment.take_front(Pos) << "* /"; 809 PendingComment = PendingComment.drop_front(Pos + 2); 810 } 811 } 812 OS << (IndentSize ? " */" : "*/"); 813 // Comments are on their own line unless attached to an attribute value. 814 if (Stack.size() > 1 && Stack.back().Ctx == Singleton) { 815 if (IndentSize) 816 OS << ' '; 817 } else { 818 newline(); 819 } 820 } 821 822 void llvm::json::OStream::newline() { 823 if (IndentSize) { 824 OS.write('\n'); 825 OS.indent(Indent); 826 } 827 } 828 829 void llvm::json::OStream::arrayBegin() { 830 valueBegin(); 831 Stack.emplace_back(); 832 Stack.back().Ctx = Array; 833 Indent += IndentSize; 834 OS << '['; 835 } 836 837 void llvm::json::OStream::arrayEnd() { 838 assert(Stack.back().Ctx == Array); 839 Indent -= IndentSize; 840 if (Stack.back().HasValue) 841 newline(); 842 OS << ']'; 843 assert(PendingComment.empty()); 844 Stack.pop_back(); 845 assert(!Stack.empty()); 846 } 847 848 void llvm::json::OStream::objectBegin() { 849 valueBegin(); 850 Stack.emplace_back(); 851 Stack.back().Ctx = Object; 852 Indent += IndentSize; 853 OS << '{'; 854 } 855 856 void llvm::json::OStream::objectEnd() { 857 assert(Stack.back().Ctx == Object); 858 Indent -= IndentSize; 859 if (Stack.back().HasValue) 860 newline(); 861 OS << '}'; 862 assert(PendingComment.empty()); 863 Stack.pop_back(); 864 assert(!Stack.empty()); 865 } 866 867 void llvm::json::OStream::attributeBegin(llvm::StringRef Key) { 868 assert(Stack.back().Ctx == Object); 869 if (Stack.back().HasValue) 870 OS << ','; 871 newline(); 872 flushComment(); 873 Stack.back().HasValue = true; 874 Stack.emplace_back(); 875 Stack.back().Ctx = Singleton; 876 if (LLVM_LIKELY(isUTF8(Key))) { 877 quote(OS, Key); 878 } else { 879 assert(false && "Invalid UTF-8 in attribute key"); 880 quote(OS, fixUTF8(Key)); 881 } 882 OS.write(':'); 883 if (IndentSize) 884 OS.write(' '); 885 } 886 887 void llvm::json::OStream::attributeEnd() { 888 assert(Stack.back().Ctx == Singleton); 889 assert(Stack.back().HasValue && "Attribute must have a value"); 890 assert(PendingComment.empty()); 891 Stack.pop_back(); 892 assert(Stack.back().Ctx == Object); 893 } 894 895 raw_ostream &llvm::json::OStream::rawValueBegin() { 896 valueBegin(); 897 Stack.emplace_back(); 898 Stack.back().Ctx = RawValue; 899 return OS; 900 } 901 902 void llvm::json::OStream::rawValueEnd() { 903 assert(Stack.back().Ctx == RawValue); 904 Stack.pop_back(); 905 } 906 907 } // namespace json 908 } // namespace llvm 909 910 void llvm::format_provider<llvm::json::Value>::format( 911 const llvm::json::Value &E, raw_ostream &OS, StringRef Options) { 912 unsigned IndentAmount = 0; 913 if (!Options.empty() && Options.getAsInteger(/*Radix=*/10, IndentAmount)) 914 llvm_unreachable("json::Value format options should be an integer"); 915 json::OStream(OS, IndentAmount).value(E); 916 } 917 918