LLVM 17.0.0git
ScopedPrinter.h
Go to the documentation of this file.
1//===-- ScopedPrinter.h ----------------------------------------*- 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#ifndef LLVM_SUPPORT_SCOPEDPRINTER_H
10#define LLVM_SUPPORT_SCOPEDPRINTER_H
11
12#include "llvm/ADT/APSInt.h"
13#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/JSON.h"
21
22namespace llvm {
23
24template <typename T> struct EnumEntry {
26 // While Name suffices in most of the cases, in certain cases
27 // GNU style and LLVM style of ELFDumper do not
28 // display same string for same enum. The AltName if initialized appropriately
29 // will hold the string that GNU style emits.
30 // Example:
31 // "EM_X86_64" string on LLVM style for Elf_Ehdr->e_machine corresponds to
32 // "Advanced Micro Devices X86-64" on GNU style
36 : Name(N), AltName(A), Value(V) {}
37 constexpr EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {}
38};
39
40struct HexNumber {
41 // To avoid sign-extension we have to explicitly cast to the appropriate
42 // unsigned type. The overloads are here so that every type that is implicitly
43 // convertible to an integer (including enums and endian helpers) can be used
44 // without requiring type traits or call-site changes.
45 HexNumber(char Value) : Value(static_cast<unsigned char>(Value)) {}
46 HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) {}
47 HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) {}
48 HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) {}
49 HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) {}
50 HexNumber(signed long long Value)
51 : Value(static_cast<unsigned long long>(Value)) {}
52 HexNumber(unsigned char Value) : Value(Value) {}
53 HexNumber(unsigned short Value) : Value(Value) {}
54 HexNumber(unsigned int Value) : Value(Value) {}
55 HexNumber(unsigned long Value) : Value(Value) {}
56 HexNumber(unsigned long long Value) : Value(Value) {}
58};
59
60struct FlagEntry {
62 : Name(Name), Value(static_cast<unsigned char>(Value)) {}
64 : Name(Name), Value(static_cast<unsigned char>(Value)) {}
66 : Name(Name), Value(static_cast<unsigned short>(Value)) {}
68 : Name(Name), Value(static_cast<unsigned int>(Value)) {}
70 : Name(Name), Value(static_cast<unsigned long>(Value)) {}
71 FlagEntry(StringRef Name, signed long long Value)
72 : Name(Name), Value(static_cast<unsigned long long>(Value)) {}
73 FlagEntry(StringRef Name, unsigned char Value) : Name(Name), Value(Value) {}
74 FlagEntry(StringRef Name, unsigned short Value) : Name(Name), Value(Value) {}
76 FlagEntry(StringRef Name, unsigned long Value) : Name(Name), Value(Value) {}
77 FlagEntry(StringRef Name, unsigned long long Value)
78 : Name(Name), Value(Value) {}
81};
82
84
85template <class T> std::string to_string(const T &Value) {
86 std::string number;
87 raw_string_ostream stream(number);
88 stream << Value;
89 return stream.str();
90}
91
92template <typename T, typename TEnum>
93std::string enumToString(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
94 for (const EnumEntry<TEnum> &EnumItem : EnumValues)
95 if (EnumItem.Value == Value)
96 return std::string(EnumItem.AltName);
97 return utohexstr(Value, true);
98}
99
101public:
102 enum class ScopedPrinterKind {
103 Base,
104 JSON,
105 };
106
109 : OS(OS), Kind(Kind) {}
110
111 ScopedPrinterKind getKind() const { return Kind; }
112
113 static bool classof(const ScopedPrinter *SP) {
114 return SP->getKind() == ScopedPrinterKind::Base;
115 }
116
117 virtual ~ScopedPrinter() = default;
118
119 void flush() { OS.flush(); }
120
121 void indent(int Levels = 1) { IndentLevel += Levels; }
122
123 void unindent(int Levels = 1) {
124 IndentLevel = IndentLevel > Levels ? IndentLevel - Levels : 0;
125 }
126
127 void resetIndent() { IndentLevel = 0; }
128
129 int getIndentLevel() { return IndentLevel; }
130
131 void setPrefix(StringRef P) { Prefix = P; }
132
133 void printIndent() {
134 OS << Prefix;
135 for (int i = 0; i < IndentLevel; ++i)
136 OS << " ";
137 }
138
139 template <typename T> HexNumber hex(T Value) { return HexNumber(Value); }
140
141 template <typename T, typename TEnum>
143 ArrayRef<EnumEntry<TEnum>> EnumValues) {
145 bool Found = false;
146 for (const auto &EnumItem : EnumValues) {
147 if (EnumItem.Value == Value) {
148 Name = EnumItem.Name;
149 Found = true;
150 break;
151 }
152 }
153
154 if (Found)
155 printHex(Label, Name, Value);
156 else
157 printHex(Label, Value);
158 }
159
160 template <typename T, typename TFlag>
162 TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
163 TFlag EnumMask3 = {}) {
164 SmallVector<FlagEntry, 10> SetFlags;
165
166 for (const auto &Flag : Flags) {
167 if (Flag.Value == 0)
168 continue;
169
170 TFlag EnumMask{};
171 if (Flag.Value & EnumMask1)
172 EnumMask = EnumMask1;
173 else if (Flag.Value & EnumMask2)
174 EnumMask = EnumMask2;
175 else if (Flag.Value & EnumMask3)
176 EnumMask = EnumMask3;
177 bool IsEnum = (Flag.Value & EnumMask) != 0;
178 if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
179 (IsEnum && (Value & EnumMask) == Flag.Value)) {
180 SetFlags.emplace_back(Flag.Name, Flag.Value);
181 }
182 }
183
184 llvm::sort(SetFlags, &flagName);
185 printFlagsImpl(Label, hex(Value), SetFlags);
186 }
187
188 template <typename T> void printFlags(StringRef Label, T Value) {
190 uint64_t Flag = 1;
191 uint64_t Curr = Value;
192 while (Curr > 0) {
193 if (Curr & 1)
194 SetFlags.emplace_back(Flag);
195 Curr >>= 1;
196 Flag <<= 1;
197 }
198 printFlagsImpl(Label, hex(Value), SetFlags);
199 }
200
201 virtual void printNumber(StringRef Label, uint64_t Value) {
202 startLine() << Label << ": " << Value << "\n";
203 }
204
205 virtual void printNumber(StringRef Label, uint32_t Value) {
206 startLine() << Label << ": " << Value << "\n";
207 }
208
209 virtual void printNumber(StringRef Label, uint16_t Value) {
210 startLine() << Label << ": " << Value << "\n";
211 }
212
213 virtual void printNumber(StringRef Label, uint8_t Value) {
214 startLine() << Label << ": " << unsigned(Value) << "\n";
215 }
216
217 virtual void printNumber(StringRef Label, int64_t Value) {
218 startLine() << Label << ": " << Value << "\n";
219 }
220
221 virtual void printNumber(StringRef Label, int32_t Value) {
222 startLine() << Label << ": " << Value << "\n";
223 }
224
225 virtual void printNumber(StringRef Label, int16_t Value) {
226 startLine() << Label << ": " << Value << "\n";
227 }
228
229 virtual void printNumber(StringRef Label, int8_t Value) {
230 startLine() << Label << ": " << int(Value) << "\n";
231 }
232
233 virtual void printNumber(StringRef Label, const APSInt &Value) {
234 startLine() << Label << ": " << Value << "\n";
235 }
236
237 virtual void printNumber(StringRef Label, float Value) {
238 startLine() << Label << ": " << format("%5.1f", Value) << "\n";
239 }
240
241 virtual void printNumber(StringRef Label, double Value) {
242 startLine() << Label << ": " << format("%5.1f", Value) << "\n";
243 }
244
245 template <typename T>
247 printNumberImpl(Label, Str, to_string(Value));
248 }
249
250 virtual void printBoolean(StringRef Label, bool Value) {
251 startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
252 }
253
254 template <typename... T> void printVersion(StringRef Label, T... Version) {
255 startLine() << Label << ": ";
256 printVersionInternal(Version...);
257 getOStream() << "\n";
258 }
259
260 template <typename T>
261 void printList(StringRef Label, const ArrayRef<T> List) {
263 for (const auto &Item : List)
264 StringList.emplace_back(to_string(Item));
265 printList(Label, StringList);
266 }
267
268 virtual void printList(StringRef Label, const ArrayRef<bool> List) {
269 printListImpl(Label, List);
270 }
271
272 virtual void printList(StringRef Label, const ArrayRef<std::string> List) {
273 printListImpl(Label, List);
274 }
275
276 virtual void printList(StringRef Label, const ArrayRef<uint64_t> List) {
277 printListImpl(Label, List);
278 }
279
280 virtual void printList(StringRef Label, const ArrayRef<uint32_t> List) {
281 printListImpl(Label, List);
282 }
283
284 virtual void printList(StringRef Label, const ArrayRef<uint16_t> List) {
285 printListImpl(Label, List);
286 }
287
288 virtual void printList(StringRef Label, const ArrayRef<uint8_t> List) {
289 SmallVector<unsigned> NumberList;
290 for (const uint8_t &Item : List)
291 NumberList.emplace_back(Item);
292 printListImpl(Label, NumberList);
293 }
294
295 virtual void printList(StringRef Label, const ArrayRef<int64_t> List) {
296 printListImpl(Label, List);
297 }
298
299 virtual void printList(StringRef Label, const ArrayRef<int32_t> List) {
300 printListImpl(Label, List);
301 }
302
303 virtual void printList(StringRef Label, const ArrayRef<int16_t> List) {
304 printListImpl(Label, List);
305 }
306
307 virtual void printList(StringRef Label, const ArrayRef<int8_t> List) {
308 SmallVector<int> NumberList;
309 for (const int8_t &Item : List)
310 NumberList.emplace_back(Item);
311 printListImpl(Label, NumberList);
312 }
313
314 virtual void printList(StringRef Label, const ArrayRef<APSInt> List) {
315 printListImpl(Label, List);
316 }
317
318 template <typename T, typename U>
319 void printList(StringRef Label, const T &List, const U &Printer) {
320 startLine() << Label << ": [";
321 ListSeparator LS;
322 for (const auto &Item : List) {
323 OS << LS;
324 Printer(OS, Item);
325 }
326 OS << "]\n";
327 }
328
329 template <typename T> void printHexList(StringRef Label, const T &List) {
331 for (const auto &Item : List)
332 HexList.emplace_back(Item);
333 printHexListImpl(Label, HexList);
334 }
335
336 template <typename T> void printHex(StringRef Label, T Value) {
337 printHexImpl(Label, hex(Value));
338 }
339
340 template <typename T> void printHex(StringRef Label, StringRef Str, T Value) {
341 printHexImpl(Label, Str, hex(Value));
342 }
343
344 template <typename T>
346 printSymbolOffsetImpl(Label, Symbol, hex(Value));
347 }
348
349 virtual void printString(StringRef Value) { startLine() << Value << "\n"; }
350
351 virtual void printString(StringRef Label, StringRef Value) {
352 startLine() << Label << ": " << Value << "\n";
353 }
354
356 printStringEscapedImpl(Label, Value);
357 }
358
360 printBinaryImpl(Label, Str, Value, false);
361 }
362
364 auto V =
365 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
366 printBinaryImpl(Label, Str, V, false);
367 }
368
370 printBinaryImpl(Label, StringRef(), Value, false);
371 }
372
374 auto V =
375 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
376 printBinaryImpl(Label, StringRef(), V, false);
377 }
378
380 auto V =
381 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
382 printBinaryImpl(Label, StringRef(), V, false);
383 }
384
386 uint32_t StartOffset) {
387 printBinaryImpl(Label, StringRef(), Value, true, StartOffset);
388 }
389
391 printBinaryImpl(Label, StringRef(), Value, true);
392 }
393
395 auto V =
396 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
397 printBinaryImpl(Label, StringRef(), V, true);
398 }
399
400 template <typename T> void printObject(StringRef Label, const T &Value) {
401 printString(Label, to_string(Value));
402 }
403
404 virtual void objectBegin() { scopedBegin('{'); }
405
406 virtual void objectBegin(StringRef Label) { scopedBegin(Label, '{'); }
407
408 virtual void objectEnd() { scopedEnd('}'); }
409
410 virtual void arrayBegin() { scopedBegin('['); }
411
412 virtual void arrayBegin(StringRef Label) { scopedBegin(Label, '['); }
413
414 virtual void arrayEnd() { scopedEnd(']'); }
415
417 printIndent();
418 return OS;
419 }
420
421 virtual raw_ostream &getOStream() { return OS; }
422
423private:
424 template <typename T> void printVersionInternal(T Value) {
425 getOStream() << Value;
426 }
427
428 template <typename S, typename T, typename... TArgs>
429 void printVersionInternal(S Value, T Value2, TArgs... Args) {
430 getOStream() << Value << ".";
431 printVersionInternal(Value2, Args...);
432 }
433
434 static bool flagName(const FlagEntry &LHS, const FlagEntry &RHS) {
435 return LHS.Name < RHS.Name;
436 }
437
438 virtual void printBinaryImpl(StringRef Label, StringRef Str,
439 ArrayRef<uint8_t> Value, bool Block,
440 uint32_t StartOffset = 0);
441
442 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
443 ArrayRef<FlagEntry> Flags) {
444 startLine() << Label << " [ (" << Value << ")\n";
445 for (const auto &Flag : Flags)
446 startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
447 startLine() << "]\n";
448 }
449
450 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
451 ArrayRef<HexNumber> Flags) {
452 startLine() << Label << " [ (" << Value << ")\n";
453 for (const auto &Flag : Flags)
454 startLine() << " " << Flag << '\n';
455 startLine() << "]\n";
456 }
457
458 template <typename T> void printListImpl(StringRef Label, const T List) {
459 startLine() << Label << ": [";
460 ListSeparator LS;
461 for (const auto &Item : List)
462 OS << LS << Item;
463 OS << "]\n";
464 }
465
466 virtual void printHexListImpl(StringRef Label,
467 const ArrayRef<HexNumber> List) {
468 startLine() << Label << ": [";
469 ListSeparator LS;
470 for (const auto &Item : List)
471 OS << LS << hex(Item);
472 OS << "]\n";
473 }
474
475 virtual void printHexImpl(StringRef Label, HexNumber Value) {
476 startLine() << Label << ": " << Value << "\n";
477 }
478
479 virtual void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) {
480 startLine() << Label << ": " << Str << " (" << Value << ")\n";
481 }
482
483 virtual void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
484 HexNumber Value) {
485 startLine() << Label << ": " << Symbol << '+' << Value << '\n';
486 }
487
488 virtual void printNumberImpl(StringRef Label, StringRef Str,
489 StringRef Value) {
490 startLine() << Label << ": " << Str << " (" << Value << ")\n";
491 }
492
493 virtual void printStringEscapedImpl(StringRef Label, StringRef Value) {
494 startLine() << Label << ": ";
495 OS.write_escaped(Value);
496 OS << '\n';
497 }
498
499 void scopedBegin(char Symbol) {
500 startLine() << Symbol << '\n';
501 indent();
502 }
503
504 void scopedBegin(StringRef Label, char Symbol) {
505 startLine() << Label;
506 if (!Label.empty())
507 OS << ' ';
508 OS << Symbol << '\n';
509 indent();
510 }
511
512 void scopedEnd(char Symbol) {
513 unindent();
514 startLine() << Symbol << '\n';
515 }
516
517 raw_ostream &OS;
518 int IndentLevel = 0;
519 StringRef Prefix;
521};
522
523template <>
524inline void
525ScopedPrinter::printHex<support::ulittle16_t>(StringRef Label,
527 startLine() << Label << ": " << hex(Value) << "\n";
528}
529
530struct DelimitedScope;
531
533private:
534 enum class Scope {
535 Array,
536 Object,
537 };
538
539 enum class ScopeKind {
540 NoAttribute,
541 Attribute,
542 NestedAttribute,
543 };
544
545 struct ScopeContext {
546 Scope Context;
547 ScopeKind Kind;
548 ScopeContext(Scope Context, ScopeKind Kind = ScopeKind::NoAttribute)
549 : Context(Context), Kind(Kind) {}
550 };
551
552 SmallVector<ScopeContext, 8> ScopeHistory;
553 json::OStream JOS;
554 std::unique_ptr<DelimitedScope> OuterScope;
555
556public:
557 JSONScopedPrinter(raw_ostream &OS, bool PrettyPrint = false,
558 std::unique_ptr<DelimitedScope> &&OuterScope =
559 std::unique_ptr<DelimitedScope>{});
560
561 static bool classof(const ScopedPrinter *SP) {
563 }
564
565 void printNumber(StringRef Label, uint64_t Value) override {
566 JOS.attribute(Label, Value);
567 }
568
569 void printNumber(StringRef Label, uint32_t Value) override {
570 JOS.attribute(Label, Value);
571 }
572
573 void printNumber(StringRef Label, uint16_t Value) override {
574 JOS.attribute(Label, Value);
575 }
576
577 void printNumber(StringRef Label, uint8_t Value) override {
578 JOS.attribute(Label, Value);
579 }
580
581 void printNumber(StringRef Label, int64_t Value) override {
582 JOS.attribute(Label, Value);
583 }
584
585 void printNumber(StringRef Label, int32_t Value) override {
586 JOS.attribute(Label, Value);
587 }
588
589 void printNumber(StringRef Label, int16_t Value) override {
590 JOS.attribute(Label, Value);
591 }
592
593 void printNumber(StringRef Label, int8_t Value) override {
594 JOS.attribute(Label, Value);
595 }
596
597 void printNumber(StringRef Label, float Value) override {
598 JOS.attribute(Label, Value);
599 }
600
601 void printNumber(StringRef Label, double Value) override {
602 JOS.attribute(Label, Value);
603 }
604
605 void printNumber(StringRef Label, const APSInt &Value) override {
606 JOS.attributeBegin(Label);
607 printAPSInt(Value);
608 JOS.attributeEnd();
609 }
610
611 void printBoolean(StringRef Label, bool Value) override {
612 JOS.attribute(Label, Value);
613 }
614
615 void printList(StringRef Label, const ArrayRef<bool> List) override {
616 printListImpl(Label, List);
617 }
618
619 void printList(StringRef Label, const ArrayRef<std::string> List) override {
620 printListImpl(Label, List);
621 }
622
623 void printList(StringRef Label, const ArrayRef<uint64_t> List) override {
624 printListImpl(Label, List);
625 }
626
627 void printList(StringRef Label, const ArrayRef<uint32_t> List) override {
628 printListImpl(Label, List);
629 }
630
631 void printList(StringRef Label, const ArrayRef<uint16_t> List) override {
632 printListImpl(Label, List);
633 }
634
635 void printList(StringRef Label, const ArrayRef<uint8_t> List) override {
636 printListImpl(Label, List);
637 }
638
639 void printList(StringRef Label, const ArrayRef<int64_t> List) override {
640 printListImpl(Label, List);
641 }
642
643 void printList(StringRef Label, const ArrayRef<int32_t> List) override {
644 printListImpl(Label, List);
645 }
646
647 void printList(StringRef Label, const ArrayRef<int16_t> List) override {
648 printListImpl(Label, List);
649 }
650
651 void printList(StringRef Label, const ArrayRef<int8_t> List) override {
652 printListImpl(Label, List);
653 }
654
655 void printList(StringRef Label, const ArrayRef<APSInt> List) override {
656 JOS.attributeArray(Label, [&]() {
657 for (const APSInt &Item : List) {
658 printAPSInt(Item);
659 }
660 });
661 }
662
663 void printString(StringRef Value) override { JOS.value(Value); }
664
665 void printString(StringRef Label, StringRef Value) override {
666 JOS.attribute(Label, Value);
667 }
668
669 void objectBegin() override {
670 scopedBegin({Scope::Object, ScopeKind::NoAttribute});
671 }
672
673 void objectBegin(StringRef Label) override {
674 scopedBegin(Label, Scope::Object);
675 }
676
677 void objectEnd() override { scopedEnd(); }
678
679 void arrayBegin() override {
680 scopedBegin({Scope::Array, ScopeKind::NoAttribute});
681 }
682
683 void arrayBegin(StringRef Label) override {
684 scopedBegin(Label, Scope::Array);
685 }
686
687 void arrayEnd() override { scopedEnd(); }
688
689private:
690 // Output HexNumbers as decimals so that they're easier to parse.
691 uint64_t hexNumberToInt(HexNumber Hex) { return Hex.Value; }
692
693 void printAPSInt(const APSInt &Value) {
694 JOS.rawValueBegin() << Value;
695 JOS.rawValueEnd();
696 }
697
698 void printFlagsImpl(StringRef Label, HexNumber Value,
699 ArrayRef<FlagEntry> Flags) override {
700 JOS.attributeObject(Label, [&]() {
701 JOS.attribute("Value", hexNumberToInt(Value));
702 JOS.attributeArray("Flags", [&]() {
703 for (const FlagEntry &Flag : Flags) {
704 JOS.objectBegin();
705 JOS.attribute("Name", Flag.Name);
706 JOS.attribute("Value", Flag.Value);
707 JOS.objectEnd();
708 }
709 });
710 });
711 }
712
713 void printFlagsImpl(StringRef Label, HexNumber Value,
714 ArrayRef<HexNumber> Flags) override {
715 JOS.attributeObject(Label, [&]() {
716 JOS.attribute("Value", hexNumberToInt(Value));
717 JOS.attributeArray("Flags", [&]() {
718 for (const HexNumber &Flag : Flags) {
719 JOS.value(Flag.Value);
720 }
721 });
722 });
723 }
724
725 template <typename T> void printListImpl(StringRef Label, const T &List) {
726 JOS.attributeArray(Label, [&]() {
727 for (const auto &Item : List)
728 JOS.value(Item);
729 });
730 }
731
732 void printHexListImpl(StringRef Label,
733 const ArrayRef<HexNumber> List) override {
734 JOS.attributeArray(Label, [&]() {
735 for (const HexNumber &Item : List) {
736 JOS.value(hexNumberToInt(Item));
737 }
738 });
739 }
740
741 void printHexImpl(StringRef Label, HexNumber Value) override {
742 JOS.attribute(Label, hexNumberToInt(Value));
743 }
744
745 void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) override {
746 JOS.attributeObject(Label, [&]() {
747 JOS.attribute("Name", Str);
748 JOS.attribute("Value", hexNumberToInt(Value));
749 });
750 }
751
752 void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
753 HexNumber Value) override {
754 JOS.attributeObject(Label, [&]() {
755 JOS.attribute("SymName", Symbol);
756 JOS.attribute("Offset", hexNumberToInt(Value));
757 });
758 }
759
760 void printNumberImpl(StringRef Label, StringRef Str,
761 StringRef Value) override {
762 JOS.attributeObject(Label, [&]() {
763 JOS.attribute("Name", Str);
764 JOS.attributeBegin("Value");
765 JOS.rawValueBegin() << Value;
766 JOS.rawValueEnd();
767 JOS.attributeEnd();
768 });
769 }
770
771 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
772 bool Block, uint32_t StartOffset = 0) override {
773 JOS.attributeObject(Label, [&]() {
774 if (!Str.empty())
775 JOS.attribute("Value", Str);
776 JOS.attribute("Offset", StartOffset);
777 JOS.attributeArray("Bytes", [&]() {
778 for (uint8_t Val : Value)
779 JOS.value(Val);
780 });
781 });
782 }
783
784 void scopedBegin(ScopeContext ScopeCtx) {
785 if (ScopeCtx.Context == Scope::Object)
786 JOS.objectBegin();
787 else if (ScopeCtx.Context == Scope::Array)
788 JOS.arrayBegin();
789 ScopeHistory.push_back(ScopeCtx);
790 }
791
792 void scopedBegin(StringRef Label, Scope Ctx) {
793 ScopeKind Kind = ScopeKind::Attribute;
794 if (ScopeHistory.empty() || ScopeHistory.back().Context != Scope::Object) {
795 JOS.objectBegin();
796 Kind = ScopeKind::NestedAttribute;
797 }
798 JOS.attributeBegin(Label);
799 scopedBegin({Ctx, Kind});
800 }
801
802 void scopedEnd() {
803 ScopeContext ScopeCtx = ScopeHistory.back();
804 if (ScopeCtx.Context == Scope::Object)
805 JOS.objectEnd();
806 else if (ScopeCtx.Context == Scope::Array)
807 JOS.arrayEnd();
808 if (ScopeCtx.Kind == ScopeKind::Attribute ||
809 ScopeCtx.Kind == ScopeKind::NestedAttribute)
810 JOS.attributeEnd();
811 if (ScopeCtx.Kind == ScopeKind::NestedAttribute)
812 JOS.objectEnd();
813 ScopeHistory.pop_back();
814 }
815};
816
819 DelimitedScope() : W(nullptr) {}
820 virtual ~DelimitedScope() = default;
821 virtual void setPrinter(ScopedPrinter &W) = 0;
823};
824
826 explicit DictScope() = default;
828
830 W.objectBegin(N);
831 }
832
833 void setPrinter(ScopedPrinter &W) override {
834 this->W = &W;
835 W.objectBegin();
836 }
837
839 if (W)
840 W->objectEnd();
841 }
842};
843
845 explicit ListScope() = default;
847
849 W.arrayBegin(N);
850 }
851
852 void setPrinter(ScopedPrinter &W) override {
853 this->W = &W;
854 W.arrayBegin();
855 }
856
858 if (W)
859 W->arrayEnd();
860 }
861};
862
863} // namespace llvm
864
865#endif
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
std::string Name
This file supports working with JSON data.
Memory true print Memory SSA Printer
Definition: MemorySSA.cpp:78
#define T
LLVMContext & Context
#define P(N)
raw_pwrite_stream & OS
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
@ Flags
Definition: TextStubV5.cpp:93
Value * RHS
Value * LHS
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
void printNumber(StringRef Label, double Value) override
void printList(StringRef Label, const ArrayRef< std::string > List) override
void printNumber(StringRef Label, float Value) override
void printNumber(StringRef Label, int32_t Value) override
void printList(StringRef Label, const ArrayRef< uint8_t > List) override
void printNumber(StringRef Label, int8_t Value) override
void printList(StringRef Label, const ArrayRef< int32_t > List) override
void printNumber(StringRef Label, uint64_t Value) override
void printBoolean(StringRef Label, bool Value) override
void printList(StringRef Label, const ArrayRef< int64_t > List) override
void printList(StringRef Label, const ArrayRef< bool > List) override
void arrayBegin() override
void printList(StringRef Label, const ArrayRef< APSInt > List) override
void printNumber(StringRef Label, int64_t Value) override
void printList(StringRef Label, const ArrayRef< uint64_t > List) override
void printList(StringRef Label, const ArrayRef< uint16_t > List) override
void printList(StringRef Label, const ArrayRef< int16_t > List) override
void printNumber(StringRef Label, uint8_t Value) override
void objectEnd() override
void printNumber(StringRef Label, uint16_t Value) override
void printNumber(StringRef Label, int16_t Value) override
static bool classof(const ScopedPrinter *SP)
void printNumber(StringRef Label, const APSInt &Value) override
void printList(StringRef Label, const ArrayRef< uint32_t > List) override
void arrayBegin(StringRef Label) override
void arrayEnd() override
void printNumber(StringRef Label, uint32_t Value) override
void printString(StringRef Value) override
void objectBegin(StringRef Label) override
void printString(StringRef Label, StringRef Value) override
void printList(StringRef Label, const ArrayRef< int8_t > List) override
void objectBegin() override
virtual void printList(StringRef Label, const ArrayRef< int32_t > List)
void printHexList(StringRef Label, const T &List)
virtual void printNumber(StringRef Label, int32_t Value)
virtual void printList(StringRef Label, const ArrayRef< uint32_t > List)
virtual void printNumber(StringRef Label, uint8_t Value)
void printBinary(StringRef Label, StringRef Str, ArrayRef< char > Value)
virtual void printString(StringRef Value)
void printBinaryBlock(StringRef Label, ArrayRef< uint8_t > Value, uint32_t StartOffset)
void printStringEscaped(StringRef Label, StringRef Value)
void printBinaryBlock(StringRef Label, StringRef Value)
virtual void arrayEnd()
virtual void printList(StringRef Label, const ArrayRef< int64_t > List)
void indent(int Levels=1)
virtual void printList(StringRef Label, const ArrayRef< int16_t > List)
void printFlags(StringRef Label, T Value)
virtual void printList(StringRef Label, const ArrayRef< std::string > List)
virtual void printNumber(StringRef Label, uint16_t Value)
virtual void printNumber(StringRef Label, int8_t Value)
void unindent(int Levels=1)
void printBinaryBlock(StringRef Label, ArrayRef< uint8_t > Value)
virtual void arrayBegin(StringRef Label)
void printEnum(StringRef Label, T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
virtual void printList(StringRef Label, const ArrayRef< bool > List)
virtual void printList(StringRef Label, const ArrayRef< APSInt > List)
void printVersion(StringRef Label, T... Version)
ScopedPrinterKind getKind() const
virtual void printNumber(StringRef Label, const APSInt &Value)
ScopedPrinter(raw_ostream &OS, ScopedPrinterKind Kind=ScopedPrinterKind::Base)
HexNumber hex(T Value)
void printFlags(StringRef Label, T Value, ArrayRef< EnumEntry< TFlag > > Flags, TFlag EnumMask1={}, TFlag EnumMask2={}, TFlag EnumMask3={})
void printList(StringRef Label, const T &List, const U &Printer)
void printBinary(StringRef Label, StringRef Value)
virtual raw_ostream & getOStream()
virtual void printList(StringRef Label, const ArrayRef< int8_t > List)
static bool classof(const ScopedPrinter *SP)
virtual raw_ostream & startLine()
void printBinary(StringRef Label, StringRef Str, ArrayRef< uint8_t > Value)
virtual void printList(StringRef Label, const ArrayRef< uint64_t > List)
virtual void printNumber(StringRef Label, float Value)
void printHex(StringRef Label, T Value)
virtual void objectBegin(StringRef Label)
virtual void printNumber(StringRef Label, int64_t Value)
void setPrefix(StringRef P)
virtual void objectEnd()
virtual ~ScopedPrinter()=default
virtual void arrayBegin()
virtual void printBoolean(StringRef Label, bool Value)
void printHex(StringRef Label, StringRef Str, T Value)
void printNumber(StringRef Label, StringRef Str, T Value)
virtual void objectBegin()
virtual void printList(StringRef Label, const ArrayRef< uint16_t > List)
virtual void printString(StringRef Label, StringRef Value)
virtual void printList(StringRef Label, const ArrayRef< uint8_t > List)
void printSymbolOffset(StringRef Label, StringRef Symbol, T Value)
virtual void printNumber(StringRef Label, int16_t Value)
virtual void printNumber(StringRef Label, double Value)
void printBinary(StringRef Label, ArrayRef< char > Value)
virtual void printNumber(StringRef Label, uint32_t Value)
void printList(StringRef Label, const ArrayRef< T > List)
void printBinary(StringRef Label, ArrayRef< uint8_t > Value)
virtual void printNumber(StringRef Label, uint64_t Value)
void printObject(StringRef Label, const T &Value)
bool empty() const
Definition: SmallVector.h:94
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition: JSON.h:956
void attributeObject(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an object with attributes from the Block.
Definition: JSON.h:1019
void attributeBegin(llvm::StringRef Key)
Definition: JSON.cpp:880
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector<int> etc).
Definition: JSON.h:1011
void arrayBegin()
Definition: JSON.cpp:842
void objectBegin()
Definition: JSON.cpp:861
raw_ostream & rawValueBegin()
Definition: JSON.cpp:908
void attributeArray(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an array with elements from the Block.
Definition: JSON.h:1015
void attributeEnd()
Definition: JSON.cpp:900
void value(const Value &V)
Emit a self-contained value (number, string, vector<string> etc).
Definition: JSON.cpp:756
void rawValueEnd()
Definition: JSON.cpp:915
void objectEnd()
Definition: JSON.cpp:869
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:660
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:148
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:85
std::string enumToString(T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
Definition: ScopedPrinter.h:93
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1744
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:292
#define N
virtual void setPrinter(ScopedPrinter &W)=0
DelimitedScope(ScopedPrinter &W)
ScopedPrinter * W
virtual ~DelimitedScope()=default
DictScope(ScopedPrinter &W)
DictScope()=default
DictScope(ScopedPrinter &W, StringRef N)
void setPrinter(ScopedPrinter &W) override
constexpr EnumEntry(StringRef N, StringRef A, T V)
Definition: ScopedPrinter.h:35
StringRef AltName
Definition: ScopedPrinter.h:33
StringRef Name
Definition: ScopedPrinter.h:25
constexpr EnumEntry(StringRef N, T V)
Definition: ScopedPrinter.h:37
FlagEntry(StringRef Name, signed long Value)
Definition: ScopedPrinter.h:69
FlagEntry(StringRef Name, signed int Value)
Definition: ScopedPrinter.h:67
FlagEntry(StringRef Name, unsigned short Value)
Definition: ScopedPrinter.h:74
FlagEntry(StringRef Name, unsigned long long Value)
Definition: ScopedPrinter.h:77
FlagEntry(StringRef Name, unsigned long Value)
Definition: ScopedPrinter.h:76
FlagEntry(StringRef Name, signed long long Value)
Definition: ScopedPrinter.h:71
FlagEntry(StringRef Name, char Value)
Definition: ScopedPrinter.h:61
FlagEntry(StringRef Name, signed char Value)
Definition: ScopedPrinter.h:63
FlagEntry(StringRef Name, signed short Value)
Definition: ScopedPrinter.h:65
FlagEntry(StringRef Name, unsigned int Value)
Definition: ScopedPrinter.h:75
FlagEntry(StringRef Name, unsigned char Value)
Definition: ScopedPrinter.h:73
StringRef Name
Definition: ScopedPrinter.h:79
HexNumber(unsigned char Value)
Definition: ScopedPrinter.h:52
HexNumber(signed int Value)
Definition: ScopedPrinter.h:48
HexNumber(signed long long Value)
Definition: ScopedPrinter.h:50
HexNumber(char Value)
Definition: ScopedPrinter.h:45
HexNumber(signed long Value)
Definition: ScopedPrinter.h:49
HexNumber(unsigned int Value)
Definition: ScopedPrinter.h:54
HexNumber(unsigned long long Value)
Definition: ScopedPrinter.h:56
HexNumber(signed char Value)
Definition: ScopedPrinter.h:46
HexNumber(signed short Value)
Definition: ScopedPrinter.h:47
HexNumber(unsigned long Value)
Definition: ScopedPrinter.h:55
HexNumber(unsigned short Value)
Definition: ScopedPrinter.h:53
ListScope(ScopedPrinter &W)
ListScope()=default
void setPrinter(ScopedPrinter &W) override
ListScope(ScopedPrinter &W, StringRef N)