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