blob: 790b28f6e316b72c294d8a326446158dd0c4996b [file] [log] [blame]
Sam McCall6be38242018-07-09 10:05:41 +00001//=== JSON.cpp - JSON value, parsing and serialization - C++ -----------*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam McCall6be38242018-07-09 10:05:41 +00006//
7//===---------------------------------------------------------------------===//
8
9#include "llvm/Support/JSON.h"
Sam McCalle6057bc2018-07-10 11:51:26 +000010#include "llvm/Support/ConvertUTF.h"
Sam McCall6be38242018-07-09 10:05:41 +000011#include "llvm/Support/Format.h"
12#include <cctype>
13
14namespace llvm {
15namespace json {
16
17Value &Object::operator[](const ObjectKey &K) {
18 return try_emplace(K, nullptr).first->getSecond();
19}
20Value &Object::operator[](ObjectKey &&K) {
21 return try_emplace(std::move(K), nullptr).first->getSecond();
22}
23Value *Object::get(StringRef K) {
24 auto I = find(K);
25 if (I == end())
26 return nullptr;
27 return &I->second;
28}
29const Value *Object::get(StringRef K) const {
30 auto I = find(K);
31 if (I == end())
32 return nullptr;
33 return &I->second;
34}
35llvm::Optional<std::nullptr_t> Object::getNull(StringRef K) const {
36 if (auto *V = get(K))
37 return V->getAsNull();
38 return llvm::None;
39}
40llvm::Optional<bool> Object::getBoolean(StringRef K) const {
41 if (auto *V = get(K))
42 return V->getAsBoolean();
43 return llvm::None;
44}
45llvm::Optional<double> Object::getNumber(StringRef K) const {
46 if (auto *V = get(K))
47 return V->getAsNumber();
48 return llvm::None;
49}
50llvm::Optional<int64_t> Object::getInteger(StringRef K) const {
51 if (auto *V = get(K))
52 return V->getAsInteger();
53 return llvm::None;
54}
55llvm::Optional<llvm::StringRef> Object::getString(StringRef K) const {
56 if (auto *V = get(K))
57 return V->getAsString();
58 return llvm::None;
59}
60const json::Object *Object::getObject(StringRef K) const {
61 if (auto *V = get(K))
62 return V->getAsObject();
63 return nullptr;
64}
65json::Object *Object::getObject(StringRef K) {
66 if (auto *V = get(K))
67 return V->getAsObject();
68 return nullptr;
69}
70const json::Array *Object::getArray(StringRef K) const {
71 if (auto *V = get(K))
72 return V->getAsArray();
73 return nullptr;
74}
75json::Array *Object::getArray(StringRef K) {
76 if (auto *V = get(K))
77 return V->getAsArray();
78 return nullptr;
79}
80bool operator==(const Object &LHS, const Object &RHS) {
81 if (LHS.size() != RHS.size())
82 return false;
83 for (const auto &L : LHS) {
84 auto R = RHS.find(L.first);
85 if (R == RHS.end() || L.second != R->second)
86 return false;
87 }
88 return true;
89}
90
91Array::Array(std::initializer_list<Value> Elements) {
92 V.reserve(Elements.size());
93 for (const Value &V : Elements) {
94 emplace_back(nullptr);
95 back().moveFrom(std::move(V));
96 }
97}
98
99Value::Value(std::initializer_list<Value> Elements)
100 : Value(json::Array(Elements)) {}
101
102void Value::copyFrom(const Value &M) {
103 Type = M.Type;
104 switch (Type) {
105 case T_Null:
106 case T_Boolean:
Sam McCalld93eaeb2018-07-09 12:16:40 +0000107 case T_Double:
108 case T_Integer:
Sam McCall6be38242018-07-09 10:05:41 +0000109 memcpy(Union.buffer, M.Union.buffer, sizeof(Union.buffer));
110 break;
111 case T_StringRef:
112 create<StringRef>(M.as<StringRef>());
113 break;
114 case T_String:
115 create<std::string>(M.as<std::string>());
116 break;
117 case T_Object:
118 create<json::Object>(M.as<json::Object>());
119 break;
120 case T_Array:
121 create<json::Array>(M.as<json::Array>());
122 break;
123 }
124}
125
126void Value::moveFrom(const Value &&M) {
127 Type = M.Type;
128 switch (Type) {
129 case T_Null:
130 case T_Boolean:
Sam McCalld93eaeb2018-07-09 12:16:40 +0000131 case T_Double:
132 case T_Integer:
Sam McCall6be38242018-07-09 10:05:41 +0000133 memcpy(Union.buffer, M.Union.buffer, sizeof(Union.buffer));
134 break;
135 case T_StringRef:
136 create<StringRef>(M.as<StringRef>());
137 break;
138 case T_String:
139 create<std::string>(std::move(M.as<std::string>()));
140 M.Type = T_Null;
141 break;
142 case T_Object:
143 create<json::Object>(std::move(M.as<json::Object>()));
144 M.Type = T_Null;
145 break;
146 case T_Array:
147 create<json::Array>(std::move(M.as<json::Array>()));
148 M.Type = T_Null;
149 break;
150 }
151}
152
153void Value::destroy() {
154 switch (Type) {
155 case T_Null:
156 case T_Boolean:
Sam McCalld93eaeb2018-07-09 12:16:40 +0000157 case T_Double:
158 case T_Integer:
Sam McCall6be38242018-07-09 10:05:41 +0000159 break;
160 case T_StringRef:
161 as<StringRef>().~StringRef();
162 break;
163 case T_String:
164 as<std::string>().~basic_string();
165 break;
166 case T_Object:
167 as<json::Object>().~Object();
168 break;
169 case T_Array:
170 as<json::Array>().~Array();
171 break;
172 }
173}
174
175bool operator==(const Value &L, const Value &R) {
176 if (L.kind() != R.kind())
177 return false;
178 switch (L.kind()) {
179 case Value::Null:
180 return *L.getAsNull() == *R.getAsNull();
181 case Value::Boolean:
182 return *L.getAsBoolean() == *R.getAsBoolean();
183 case Value::Number:
Sam McCall1e7491e2019-01-25 15:05:33 +0000184 // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
185 // The same integer must convert to the same double, per the standard.
186 // However we see 64-vs-80-bit precision comparisons with gcc-7 -O3 -m32.
187 // So we avoid floating point promotion for exact comparisons.
188 if (L.Type == Value::T_Integer || R.Type == Value::T_Integer)
189 return L.getAsInteger() == R.getAsInteger();
Sam McCall6be38242018-07-09 10:05:41 +0000190 return *L.getAsNumber() == *R.getAsNumber();
191 case Value::String:
192 return *L.getAsString() == *R.getAsString();
193 case Value::Array:
194 return *L.getAsArray() == *R.getAsArray();
195 case Value::Object:
196 return *L.getAsObject() == *R.getAsObject();
197 }
198 llvm_unreachable("Unknown value kind");
199}
200
201namespace {
202// Simple recursive-descent JSON parser.
203class Parser {
204public:
205 Parser(StringRef JSON)
206 : Start(JSON.begin()), P(JSON.begin()), End(JSON.end()) {}
207
Sam McCalle6057bc2018-07-10 11:51:26 +0000208 bool checkUTF8() {
209 size_t ErrOffset;
210 if (isUTF8(StringRef(Start, End - Start), &ErrOffset))
211 return true;
212 P = Start + ErrOffset; // For line/column calculation.
213 return parseError("Invalid UTF-8 sequence");
214 }
215
Sam McCall6be38242018-07-09 10:05:41 +0000216 bool parseValue(Value &Out);
217
218 bool assertEnd() {
219 eatWhitespace();
220 if (P == End)
221 return true;
222 return parseError("Text after end of document");
223 }
224
225 Error takeError() {
226 assert(Err);
227 return std::move(*Err);
228 }
229
230private:
231 void eatWhitespace() {
232 while (P != End && (*P == ' ' || *P == '\r' || *P == '\n' || *P == '\t'))
233 ++P;
234 }
235
236 // On invalid syntax, parseX() functions return false and set Err.
Sam McCalld93eaeb2018-07-09 12:16:40 +0000237 bool parseNumber(char First, Value &Out);
Sam McCall6be38242018-07-09 10:05:41 +0000238 bool parseString(std::string &Out);
239 bool parseUnicode(std::string &Out);
240 bool parseError(const char *Msg); // always returns false
241
242 char next() { return P == End ? 0 : *P++; }
243 char peek() { return P == End ? 0 : *P; }
244 static bool isNumber(char C) {
245 return C == '0' || C == '1' || C == '2' || C == '3' || C == '4' ||
246 C == '5' || C == '6' || C == '7' || C == '8' || C == '9' ||
247 C == 'e' || C == 'E' || C == '+' || C == '-' || C == '.';
248 }
249
250 Optional<Error> Err;
251 const char *Start, *P, *End;
252};
253
254bool Parser::parseValue(Value &Out) {
255 eatWhitespace();
256 if (P == End)
257 return parseError("Unexpected EOF");
258 switch (char C = next()) {
259 // Bare null/true/false are easy - first char identifies them.
260 case 'n':
261 Out = nullptr;
262 return (next() == 'u' && next() == 'l' && next() == 'l') ||
263 parseError("Invalid JSON value (null?)");
264 case 't':
265 Out = true;
266 return (next() == 'r' && next() == 'u' && next() == 'e') ||
267 parseError("Invalid JSON value (true?)");
268 case 'f':
269 Out = false;
270 return (next() == 'a' && next() == 'l' && next() == 's' && next() == 'e') ||
271 parseError("Invalid JSON value (false?)");
272 case '"': {
273 std::string S;
274 if (parseString(S)) {
275 Out = std::move(S);
276 return true;
277 }
278 return false;
279 }
280 case '[': {
281 Out = Array{};
282 Array &A = *Out.getAsArray();
283 eatWhitespace();
284 if (peek() == ']') {
285 ++P;
286 return true;
287 }
288 for (;;) {
289 A.emplace_back(nullptr);
290 if (!parseValue(A.back()))
291 return false;
292 eatWhitespace();
293 switch (next()) {
294 case ',':
295 eatWhitespace();
296 continue;
297 case ']':
298 return true;
299 default:
300 return parseError("Expected , or ] after array element");
301 }
302 }
303 }
304 case '{': {
305 Out = Object{};
306 Object &O = *Out.getAsObject();
307 eatWhitespace();
308 if (peek() == '}') {
309 ++P;
310 return true;
311 }
312 for (;;) {
313 if (next() != '"')
314 return parseError("Expected object key");
315 std::string K;
316 if (!parseString(K))
317 return false;
318 eatWhitespace();
319 if (next() != ':')
320 return parseError("Expected : after object key");
321 eatWhitespace();
322 if (!parseValue(O[std::move(K)]))
323 return false;
324 eatWhitespace();
325 switch (next()) {
326 case ',':
327 eatWhitespace();
328 continue;
329 case '}':
330 return true;
331 default:
332 return parseError("Expected , or } after object property");
333 }
334 }
335 }
336 default:
Sam McCalld93eaeb2018-07-09 12:16:40 +0000337 if (isNumber(C))
338 return parseNumber(C, Out);
Sam McCall6be38242018-07-09 10:05:41 +0000339 return parseError("Invalid JSON value");
340 }
341}
342
Sam McCalld93eaeb2018-07-09 12:16:40 +0000343bool Parser::parseNumber(char First, Value &Out) {
344 // Read the number into a string. (Must be null-terminated for strto*).
Sam McCall6be38242018-07-09 10:05:41 +0000345 SmallString<24> S;
346 S.push_back(First);
347 while (isNumber(peek()))
348 S.push_back(next());
349 char *End;
Sam McCalld93eaeb2018-07-09 12:16:40 +0000350 // Try first to parse as integer, and if so preserve full 64 bits.
351 // strtoll returns long long >= 64 bits, so check it's in range too.
352 auto I = std::strtoll(S.c_str(), &End, 10);
353 if (End == S.end() && I >= std::numeric_limits<int64_t>::min() &&
354 I <= std::numeric_limits<int64_t>::max()) {
355 Out = int64_t(I);
356 return true;
357 }
358 // If it's not an integer
Sam McCall6be38242018-07-09 10:05:41 +0000359 Out = std::strtod(S.c_str(), &End);
360 return End == S.end() || parseError("Invalid JSON value (number?)");
361}
362
363bool Parser::parseString(std::string &Out) {
364 // leading quote was already consumed.
365 for (char C = next(); C != '"'; C = next()) {
366 if (LLVM_UNLIKELY(P == End))
367 return parseError("Unterminated string");
368 if (LLVM_UNLIKELY((C & 0x1f) == C))
369 return parseError("Control character in string");
370 if (LLVM_LIKELY(C != '\\')) {
371 Out.push_back(C);
372 continue;
373 }
374 // Handle escape sequence.
375 switch (C = next()) {
376 case '"':
377 case '\\':
378 case '/':
379 Out.push_back(C);
380 break;
381 case 'b':
382 Out.push_back('\b');
383 break;
384 case 'f':
385 Out.push_back('\f');
386 break;
387 case 'n':
388 Out.push_back('\n');
389 break;
390 case 'r':
391 Out.push_back('\r');
392 break;
393 case 't':
394 Out.push_back('\t');
395 break;
396 case 'u':
397 if (!parseUnicode(Out))
398 return false;
399 break;
400 default:
401 return parseError("Invalid escape sequence");
402 }
403 }
404 return true;
405}
406
407static void encodeUtf8(uint32_t Rune, std::string &Out) {
408 if (Rune < 0x80) {
409 Out.push_back(Rune & 0x7F);
410 } else if (Rune < 0x800) {
411 uint8_t FirstByte = 0xC0 | ((Rune & 0x7C0) >> 6);
412 uint8_t SecondByte = 0x80 | (Rune & 0x3F);
413 Out.push_back(FirstByte);
414 Out.push_back(SecondByte);
415 } else if (Rune < 0x10000) {
416 uint8_t FirstByte = 0xE0 | ((Rune & 0xF000) >> 12);
417 uint8_t SecondByte = 0x80 | ((Rune & 0xFC0) >> 6);
418 uint8_t ThirdByte = 0x80 | (Rune & 0x3F);
419 Out.push_back(FirstByte);
420 Out.push_back(SecondByte);
421 Out.push_back(ThirdByte);
422 } else if (Rune < 0x110000) {
423 uint8_t FirstByte = 0xF0 | ((Rune & 0x1F0000) >> 18);
424 uint8_t SecondByte = 0x80 | ((Rune & 0x3F000) >> 12);
425 uint8_t ThirdByte = 0x80 | ((Rune & 0xFC0) >> 6);
426 uint8_t FourthByte = 0x80 | (Rune & 0x3F);
427 Out.push_back(FirstByte);
428 Out.push_back(SecondByte);
429 Out.push_back(ThirdByte);
430 Out.push_back(FourthByte);
431 } else {
432 llvm_unreachable("Invalid codepoint");
433 }
434}
435
436// Parse a UTF-16 \uNNNN escape sequence. "\u" has already been consumed.
437// May parse several sequential escapes to ensure proper surrogate handling.
438// We do not use ConvertUTF.h, it can't accept and replace unpaired surrogates.
439// These are invalid Unicode but valid JSON (RFC 8259, section 8.2).
440bool Parser::parseUnicode(std::string &Out) {
441 // Invalid UTF is not a JSON error (RFC 8529§8.2). It gets replaced by U+FFFD.
442 auto Invalid = [&] { Out.append(/* UTF-8 */ {'\xef', '\xbf', '\xbd'}); };
443 // Decodes 4 hex digits from the stream into Out, returns false on error.
444 auto Parse4Hex = [this](uint16_t &Out) -> bool {
445 Out = 0;
446 char Bytes[] = {next(), next(), next(), next()};
447 for (unsigned char C : Bytes) {
448 if (!std::isxdigit(C))
449 return parseError("Invalid \\u escape sequence");
450 Out <<= 4;
451 Out |= (C > '9') ? (C & ~0x20) - 'A' + 10 : (C - '0');
452 }
453 return true;
454 };
455 uint16_t First; // UTF-16 code unit from the first \u escape.
456 if (!Parse4Hex(First))
457 return false;
458
459 // We loop to allow proper surrogate-pair error handling.
460 while (true) {
461 // Case 1: the UTF-16 code unit is already a codepoint in the BMP.
462 if (LLVM_LIKELY(First < 0xD800 || First >= 0xE000)) {
463 encodeUtf8(First, Out);
464 return true;
465 }
466
467 // Case 2: it's an (unpaired) trailing surrogate.
468 if (LLVM_UNLIKELY(First >= 0xDC00)) {
469 Invalid();
470 return true;
471 }
472
473 // Case 3: it's a leading surrogate. We expect a trailing one next.
474 // Case 3a: there's no trailing \u escape. Don't advance in the stream.
Sam McCalle6057bc2018-07-10 11:51:26 +0000475 if (LLVM_UNLIKELY(P + 2 > End || *P != '\\' || *(P + 1) != 'u')) {
Sam McCall6be38242018-07-09 10:05:41 +0000476 Invalid(); // Leading surrogate was unpaired.
477 return true;
478 }
479 P += 2;
480 uint16_t Second;
481 if (!Parse4Hex(Second))
482 return false;
483 // Case 3b: there was another \u escape, but it wasn't a trailing surrogate.
484 if (LLVM_UNLIKELY(Second < 0xDC00 || Second >= 0xE000)) {
485 Invalid(); // Leading surrogate was unpaired.
486 First = Second; // Second escape still needs to be processed.
487 continue;
488 }
489 // Case 3c: a valid surrogate pair encoding an astral codepoint.
490 encodeUtf8(0x10000 | ((First - 0xD800) << 10) | (Second - 0xDC00), Out);
491 return true;
492 }
493}
494
495bool Parser::parseError(const char *Msg) {
496 int Line = 1;
497 const char *StartOfLine = Start;
498 for (const char *X = Start; X < P; ++X) {
499 if (*X == 0x0A) {
500 ++Line;
501 StartOfLine = X + 1;
502 }
503 }
504 Err.emplace(
505 llvm::make_unique<ParseError>(Msg, Line, P - StartOfLine, P - Start));
506 return false;
507}
508} // namespace
509
510Expected<Value> parse(StringRef JSON) {
511 Parser P(JSON);
512 Value E = nullptr;
Sam McCalle6057bc2018-07-10 11:51:26 +0000513 if (P.checkUTF8())
514 if (P.parseValue(E))
515 if (P.assertEnd())
516 return std::move(E);
Sam McCall6be38242018-07-09 10:05:41 +0000517 return P.takeError();
518}
519char ParseError::ID = 0;
520
521static std::vector<const Object::value_type *> sortedElements(const Object &O) {
522 std::vector<const Object::value_type *> Elements;
523 for (const auto &E : O)
524 Elements.push_back(&E);
Fangrui Song0cac7262018-09-27 02:13:45 +0000525 llvm::sort(Elements,
Sam McCall6be38242018-07-09 10:05:41 +0000526 [](const Object::value_type *L, const Object::value_type *R) {
527 return L->first < R->first;
528 });
529 return Elements;
530}
531
Sam McCalle6057bc2018-07-10 11:51:26 +0000532bool isUTF8(llvm::StringRef S, size_t *ErrOffset) {
533 // Fast-path for ASCII, which is valid UTF-8.
534 if (LLVM_LIKELY(isASCII(S)))
535 return true;
536
537 const UTF8 *Data = reinterpret_cast<const UTF8 *>(S.data()), *Rest = Data;
538 if (LLVM_LIKELY(isLegalUTF8String(&Rest, Data + S.size())))
539 return true;
540
541 if (ErrOffset)
542 *ErrOffset = Rest - Data;
543 return false;
544}
545
546std::string fixUTF8(llvm::StringRef S) {
547 // This isn't particularly efficient, but is only for error-recovery.
548 std::vector<UTF32> Codepoints(S.size()); // 1 codepoint per byte suffices.
549 const UTF8 *In8 = reinterpret_cast<const UTF8 *>(S.data());
550 UTF32 *Out32 = Codepoints.data();
551 ConvertUTF8toUTF32(&In8, In8 + S.size(), &Out32, Out32 + Codepoints.size(),
552 lenientConversion);
553 Codepoints.resize(Out32 - Codepoints.data());
554 std::string Res(4 * Codepoints.size(), 0); // 4 bytes per codepoint suffice
555 const UTF32 *In32 = Codepoints.data();
556 UTF8 *Out8 = reinterpret_cast<UTF8 *>(&Res[0]);
557 ConvertUTF32toUTF8(&In32, In32 + Codepoints.size(), &Out8, Out8 + Res.size(),
558 strictConversion);
559 Res.resize(reinterpret_cast<char *>(Out8) - Res.data());
560 return Res;
561}
562
Sam McCall6be38242018-07-09 10:05:41 +0000563} // namespace json
564} // namespace llvm
565
566static void quote(llvm::raw_ostream &OS, llvm::StringRef S) {
567 OS << '\"';
568 for (unsigned char C : S) {
569 if (C == 0x22 || C == 0x5C)
570 OS << '\\';
571 if (C >= 0x20) {
572 OS << C;
573 continue;
574 }
575 OS << '\\';
576 switch (C) {
577 // A few characters are common enough to make short escapes worthwhile.
578 case '\t':
579 OS << 't';
580 break;
581 case '\n':
582 OS << 'n';
583 break;
584 case '\r':
585 OS << 'r';
586 break;
587 default:
588 OS << 'u';
589 llvm::write_hex(OS, C, llvm::HexPrintStyle::Lower, 4);
590 break;
591 }
592 }
593 OS << '\"';
594}
595
596enum IndenterAction {
597 Indent,
598 Outdent,
599 Newline,
600 Space,
601};
602
603// Prints JSON. The indenter can be used to control formatting.
604template <typename Indenter>
605void llvm::json::Value::print(raw_ostream &OS, const Indenter &I) const {
606 switch (Type) {
607 case T_Null:
608 OS << "null";
609 break;
610 case T_Boolean:
611 OS << (as<bool>() ? "true" : "false");
612 break;
Sam McCalld93eaeb2018-07-09 12:16:40 +0000613 case T_Double:
614 OS << format("%.*g", std::numeric_limits<double>::max_digits10,
615 as<double>());
616 break;
617 case T_Integer:
618 OS << as<int64_t>();
Sam McCall6be38242018-07-09 10:05:41 +0000619 break;
620 case T_StringRef:
621 quote(OS, as<StringRef>());
622 break;
623 case T_String:
624 quote(OS, as<std::string>());
625 break;
626 case T_Object: {
627 bool Comma = false;
628 OS << '{';
629 I(Indent);
630 for (const auto *P : sortedElements(as<json::Object>())) {
631 if (Comma)
632 OS << ',';
633 Comma = true;
634 I(Newline);
635 quote(OS, P->first);
636 OS << ':';
637 I(Space);
638 P->second.print(OS, I);
639 }
640 I(Outdent);
641 if (Comma)
642 I(Newline);
643 OS << '}';
644 break;
645 }
646 case T_Array: {
647 bool Comma = false;
648 OS << '[';
649 I(Indent);
650 for (const auto &E : as<json::Array>()) {
651 if (Comma)
652 OS << ',';
653 Comma = true;
654 I(Newline);
655 E.print(OS, I);
656 }
657 I(Outdent);
658 if (Comma)
659 I(Newline);
660 OS << ']';
661 break;
662 }
663 }
664}
665
666void llvm::format_provider<llvm::json::Value>::format(
667 const llvm::json::Value &E, raw_ostream &OS, StringRef Options) {
668 if (Options.empty()) {
669 OS << E;
670 return;
671 }
672 unsigned IndentAmount = 0;
673 if (Options.getAsInteger(/*Radix=*/10, IndentAmount))
674 llvm_unreachable("json::Value format options should be an integer");
675 unsigned IndentLevel = 0;
676 E.print(OS, [&](IndenterAction A) {
677 switch (A) {
678 case Newline:
679 OS << '\n';
680 OS.indent(IndentLevel);
681 break;
682 case Space:
683 OS << ' ';
684 break;
685 case Indent:
686 IndentLevel += IndentAmount;
687 break;
688 case Outdent:
689 IndentLevel -= IndentAmount;
690 break;
691 };
692 });
693}
694
695llvm::raw_ostream &llvm::json::operator<<(raw_ostream &OS, const Value &E) {
696 E.print(OS, [](IndenterAction A) { /*ignore*/ });
697 return OS;
698}