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