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"
16#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Endian.h"
20#include "llvm/Support/JSON.h"
22#include <type_traits>
23
24namespace llvm {
25
26template <typename T> struct EnumEntry {
28 // While Name suffices in most of the cases, in certain cases
29 // GNU style and LLVM style of ELFDumper do not
30 // display same string for same enum. The AltName if initialized appropriately
31 // will hold the string that GNU style emits.
32 // Example:
33 // "EM_X86_64" string on LLVM style for Elf_Ehdr->e_machine corresponds to
34 // "Advanced Micro Devices X86-64" on GNU style
38 : Name(N), AltName(A), Value(V) {}
39 constexpr EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {}
40};
41
42struct HexNumber {
43 // To avoid sign-extension we have to explicitly cast to the appropriate
44 // unsigned type. The overloads are here so that every type that is implicitly
45 // convertible to an integer (including endian helpers) can be used without
46 // requiring type traits or call-site changes.
47 HexNumber(char Value) : Value(static_cast<unsigned char>(Value)) {}
48 HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) {}
49 HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) {}
50 HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) {}
51 HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) {}
52 HexNumber(signed long long Value)
53 : Value(static_cast<unsigned long long>(Value)) {}
54 HexNumber(unsigned char Value) : Value(Value) {}
55 HexNumber(unsigned short Value) : Value(Value) {}
56 HexNumber(unsigned int Value) : Value(Value) {}
57 HexNumber(unsigned long Value) : Value(Value) {}
58 HexNumber(unsigned long long Value) : Value(Value) {}
59 template <typename EnumT, typename = std::enable_if_t<std::is_enum_v<EnumT>>>
61 : HexNumber(static_cast<std::underlying_type_t<EnumT>>(Value)) {}
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>>>
87 : FlagEntry(Name, static_cast<std::underlying_type_t<EnumT>>(Value)) {}
88
91};
92
94
95template <class T> std::string to_string(const T &Value) {
96 std::string number;
97 raw_string_ostream stream(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 {
124 Base,
125 JSON,
126 };
127
129 ScopedPrinterKind Kind = ScopedPrinterKind::Base)
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) {
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> void printVersion(StringRef Label, T... Version) {
288 startLine() << Label << ": ";
289 printVersionInternal(Version...);
290 getOStream() << "\n";
291 }
292
293 template <typename T>
294 void printList(StringRef Label, const ArrayRef<T> List) {
296 for (const auto &Item : List)
297 StringList.emplace_back(to_string(Item));
298 printList(Label, StringList);
299 }
300
301 virtual void printList(StringRef Label, const ArrayRef<bool> List) {
302 printListImpl(Label, List);
303 }
304
305 virtual void printList(StringRef Label, const ArrayRef<std::string> List) {
306 printListImpl(Label, List);
307 }
308
309 virtual void printList(StringRef Label, const ArrayRef<uint64_t> List) {
310 printListImpl(Label, List);
311 }
312
313 virtual void printList(StringRef Label, const ArrayRef<uint32_t> List) {
314 printListImpl(Label, List);
315 }
316
317 virtual void printList(StringRef Label, const ArrayRef<uint16_t> List) {
318 printListImpl(Label, List);
319 }
320
321 virtual void printList(StringRef Label, const ArrayRef<uint8_t> List) {
322 SmallVector<unsigned> NumberList;
323 for (const uint8_t &Item : List)
324 NumberList.emplace_back(Item);
325 printListImpl(Label, NumberList);
326 }
327
328 virtual void printList(StringRef Label, const ArrayRef<int64_t> List) {
329 printListImpl(Label, List);
330 }
331
332 virtual void printList(StringRef Label, const ArrayRef<int32_t> List) {
333 printListImpl(Label, List);
334 }
335
336 virtual void printList(StringRef Label, const ArrayRef<int16_t> List) {
337 printListImpl(Label, List);
338 }
339
340 virtual void printList(StringRef Label, const ArrayRef<int8_t> List) {
341 SmallVector<int> NumberList;
342 for (const int8_t &Item : List)
343 NumberList.emplace_back(Item);
344 printListImpl(Label, NumberList);
345 }
346
347 virtual void printList(StringRef Label, const ArrayRef<APSInt> List) {
348 printListImpl(Label, List);
349 }
350
351 template <typename T, typename U>
352 void printList(StringRef Label, const T &List, const U &Printer) {
353 startLine() << Label << ": [";
354 ListSeparator LS;
355 for (const auto &Item : List) {
356 OS << LS;
357 Printer(OS, Item);
358 }
359 OS << "]\n";
360 }
361
362 template <typename T> void printHexList(StringRef Label, const T &List) {
364 for (const auto &Item : List)
365 HexList.emplace_back(Item);
366 printHexListImpl(Label, HexList);
367 }
368
369 template <typename T> void printHex(StringRef Label, T Value) {
370 printHexImpl(Label, hex(Value));
371 }
372
373 template <typename T> void printHex(StringRef Label, StringRef Str, T Value) {
374 printHexImpl(Label, Str, hex(Value));
375 }
376
377 template <typename T>
379 printSymbolOffsetImpl(Label, Symbol, hex(Value));
380 }
381
382 virtual void printString(StringRef Value) { startLine() << Value << "\n"; }
383
384 virtual void printString(StringRef Label, StringRef Value) {
385 startLine() << Label << ": " << Value << "\n";
386 }
387
389 printStringEscapedImpl(Label, Value);
390 }
391
393 printBinaryImpl(Label, Str, Value, false);
394 }
395
397 auto V =
398 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
399 printBinaryImpl(Label, Str, V, false);
400 }
401
403 printBinaryImpl(Label, StringRef(), Value, false);
404 }
405
407 auto V =
408 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
409 printBinaryImpl(Label, StringRef(), V, false);
410 }
411
413 auto V =
414 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
415 printBinaryImpl(Label, StringRef(), V, false);
416 }
417
419 uint32_t StartOffset) {
420 printBinaryImpl(Label, StringRef(), Value, true, StartOffset);
421 }
422
424 printBinaryImpl(Label, StringRef(), Value, true);
425 }
426
428 auto V =
429 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
430 printBinaryImpl(Label, StringRef(), V, true);
431 }
432
433 template <typename T> void printObject(StringRef Label, const T &Value) {
434 printString(Label, to_string(Value));
435 }
436
437 virtual void objectBegin() { scopedBegin('{'); }
438
439 virtual void objectBegin(StringRef Label) { scopedBegin(Label, '{'); }
440
441 virtual void objectEnd() { scopedEnd('}'); }
442
443 virtual void arrayBegin() { scopedBegin('['); }
444
445 virtual void arrayBegin(StringRef Label) { scopedBegin(Label, '['); }
446
447 virtual void arrayEnd() { scopedEnd(']'); }
448
450 printIndent();
451 return OS;
452 }
453
454 virtual raw_ostream &getOStream() { return OS; }
455
456private:
457 template <typename T> void printVersionInternal(T Value) {
458 getOStream() << Value;
459 }
460
461 template <typename S, typename T, typename... TArgs>
462 void printVersionInternal(S Value, T Value2, TArgs... Args) {
463 getOStream() << Value << ".";
464 printVersionInternal(Value2, Args...);
465 }
466
467 static bool flagName(const FlagEntry &LHS, const FlagEntry &RHS) {
468 return LHS.Name < RHS.Name;
469 }
470
471 virtual void printBinaryImpl(StringRef Label, StringRef Str,
472 ArrayRef<uint8_t> Value, bool Block,
473 uint32_t StartOffset = 0);
474
475 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
476 ArrayRef<FlagEntry> Flags) {
477 startLine() << Label << " [ (" << Value << ")\n";
478 for (const auto &Flag : Flags)
479 startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
480 startLine() << "]\n";
481 }
482
483 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
484 ArrayRef<HexNumber> Flags) {
485 startLine() << Label << " [ (" << Value << ")\n";
486 for (const auto &Flag : Flags)
487 startLine() << " " << Flag << '\n';
488 startLine() << "]\n";
489 }
490
491 template <typename T> void printListImpl(StringRef Label, const T List) {
492 startLine() << Label << ": [";
493 ListSeparator LS;
494 for (const auto &Item : List)
495 OS << LS << Item;
496 OS << "]\n";
497 }
498
499 virtual void printHexListImpl(StringRef Label,
500 const ArrayRef<HexNumber> List) {
501 startLine() << Label << ": [";
502 ListSeparator LS;
503 for (const auto &Item : List)
504 OS << LS << hex(Item);
505 OS << "]\n";
506 }
507
508 virtual void printHexImpl(StringRef Label, HexNumber Value) {
509 startLine() << Label << ": " << Value << "\n";
510 }
511
512 virtual void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) {
513 startLine() << Label << ": " << Str << " (" << Value << ")\n";
514 }
515
516 virtual void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
517 HexNumber Value) {
518 startLine() << Label << ": " << Symbol << '+' << Value << '\n';
519 }
520
521 virtual void printNumberImpl(StringRef Label, StringRef Str,
522 StringRef Value) {
523 startLine() << Label << ": " << Str << " (" << Value << ")\n";
524 }
525
526 virtual void printStringEscapedImpl(StringRef Label, StringRef Value) {
527 startLine() << Label << ": ";
528 OS.write_escaped(Value);
529 OS << '\n';
530 }
531
532 void scopedBegin(char Symbol) {
533 startLine() << Symbol << '\n';
534 indent();
535 }
536
537 void scopedBegin(StringRef Label, char Symbol) {
538 startLine() << Label;
539 if (!Label.empty())
540 OS << ' ';
541 OS << Symbol << '\n';
542 indent();
543 }
544
545 void scopedEnd(char Symbol) {
546 unindent();
547 startLine() << Symbol << '\n';
548 }
549
550 raw_ostream &OS;
551 int IndentLevel = 0;
552 StringRef Prefix;
553 ScopedPrinterKind Kind;
554};
555
556template <>
557inline void
558ScopedPrinter::printHex<support::ulittle16_t>(StringRef Label,
560 startLine() << Label << ": " << hex(Value) << "\n";
561}
562
565 DelimitedScope() : W(nullptr) {}
566 virtual ~DelimitedScope() = default;
567 virtual void setPrinter(ScopedPrinter &W) = 0;
569};
570
572private:
573 enum class Scope {
574 Array,
575 Object,
576 };
577
578 enum class ScopeKind {
579 NoAttribute,
580 Attribute,
581 NestedAttribute,
582 };
583
584 struct ScopeContext {
585 Scope Context;
586 ScopeKind Kind;
587 ScopeContext(Scope Context, ScopeKind Kind = ScopeKind::NoAttribute)
588 : Context(Context), Kind(Kind) {}
589 };
590
591 SmallVector<ScopeContext, 8> ScopeHistory;
592 json::OStream JOS;
593 std::unique_ptr<DelimitedScope> OuterScope;
594
595public:
596 LLVM_ABI JSONScopedPrinter(raw_ostream &OS, bool PrettyPrint = false,
597 std::unique_ptr<DelimitedScope> &&OuterScope =
598 std::unique_ptr<DelimitedScope>{});
599
600 static bool classof(const ScopedPrinter *SP) {
601 return SP->getKind() == ScopedPrinter::ScopedPrinterKind::JSON;
602 }
603
604 void printNumber(StringRef Label, char Value) override {
605 JOS.attribute(Label, Value);
606 }
607
608 void printNumber(StringRef Label, signed char Value) override {
609 JOS.attribute(Label, Value);
610 }
611
612 void printNumber(StringRef Label, unsigned char Value) override {
613 JOS.attribute(Label, Value);
614 }
615
616 void printNumber(StringRef Label, short Value) override {
617 JOS.attribute(Label, Value);
618 }
619
620 void printNumber(StringRef Label, unsigned short Value) override {
621 JOS.attribute(Label, Value);
622 }
623
624 void printNumber(StringRef Label, int Value) override {
625 JOS.attribute(Label, Value);
626 }
627
628 void printNumber(StringRef Label, unsigned int Value) override {
629 JOS.attribute(Label, Value);
630 }
631
632 void printNumber(StringRef Label, long Value) override {
633 JOS.attribute(Label, Value);
634 }
635
636 void printNumber(StringRef Label, unsigned long Value) override {
637 JOS.attribute(Label, Value);
638 }
639
640 void printNumber(StringRef Label, long long Value) override {
641 JOS.attribute(Label, Value);
642 }
643
644 void printNumber(StringRef Label, unsigned long long Value) override {
645 JOS.attribute(Label, Value);
646 }
647
648 void printNumber(StringRef Label, float Value) override {
649 JOS.attribute(Label, Value);
650 }
651
652 void printNumber(StringRef Label, double Value) override {
653 JOS.attribute(Label, Value);
654 }
655
656 void printNumber(StringRef Label, const APSInt &Value) override {
657 JOS.attributeBegin(Label);
658 printAPSInt(Value);
659 JOS.attributeEnd();
660 }
661
662 void printBoolean(StringRef Label, bool Value) override {
663 JOS.attribute(Label, Value);
664 }
665
666 void printList(StringRef Label, const ArrayRef<bool> List) override {
667 printListImpl(Label, List);
668 }
669
670 void printList(StringRef Label, const ArrayRef<std::string> List) override {
671 printListImpl(Label, List);
672 }
673
674 void printList(StringRef Label, const ArrayRef<uint64_t> List) override {
675 printListImpl(Label, List);
676 }
677
678 void printList(StringRef Label, const ArrayRef<uint32_t> List) override {
679 printListImpl(Label, List);
680 }
681
682 void printList(StringRef Label, const ArrayRef<uint16_t> List) override {
683 printListImpl(Label, List);
684 }
685
686 void printList(StringRef Label, const ArrayRef<uint8_t> List) override {
687 printListImpl(Label, List);
688 }
689
690 void printList(StringRef Label, const ArrayRef<int64_t> List) override {
691 printListImpl(Label, List);
692 }
693
694 void printList(StringRef Label, const ArrayRef<int32_t> List) override {
695 printListImpl(Label, List);
696 }
697
698 void printList(StringRef Label, const ArrayRef<int16_t> List) override {
699 printListImpl(Label, List);
700 }
701
702 void printList(StringRef Label, const ArrayRef<int8_t> List) override {
703 printListImpl(Label, List);
704 }
705
706 void printList(StringRef Label, const ArrayRef<APSInt> List) override {
707 JOS.attributeArray(Label, [&]() {
708 for (const APSInt &Item : List) {
709 printAPSInt(Item);
710 }
711 });
712 }
713
714 void printString(StringRef Value) override { JOS.value(Value); }
715
716 void printString(StringRef Label, StringRef Value) override {
717 JOS.attribute(Label, Value);
718 }
719
720 void objectBegin() override {
721 scopedBegin({Scope::Object, ScopeKind::NoAttribute});
722 }
723
724 void objectBegin(StringRef Label) override {
725 scopedBegin(Label, Scope::Object);
726 }
727
728 void objectEnd() override { scopedEnd(); }
729
730 void arrayBegin() override {
731 scopedBegin({Scope::Array, ScopeKind::NoAttribute});
732 }
733
734 void arrayBegin(StringRef Label) override {
735 scopedBegin(Label, Scope::Array);
736 }
737
738 void arrayEnd() override { scopedEnd(); }
739
740private:
741 // Output HexNumbers as decimals so that they're easier to parse.
742 uint64_t hexNumberToInt(HexNumber Hex) { return Hex.Value; }
743
744 void printAPSInt(const APSInt &Value) {
745 JOS.rawValueBegin() << Value;
746 JOS.rawValueEnd();
747 }
748
749 void printFlagsImpl(StringRef Label, HexNumber Value,
750 ArrayRef<FlagEntry> Flags) override {
751 JOS.attributeObject(Label, [&]() {
752 JOS.attribute("Value", hexNumberToInt(Value));
753 JOS.attributeArray("Flags", [&]() {
754 for (const FlagEntry &Flag : Flags) {
755 JOS.objectBegin();
756 JOS.attribute("Name", Flag.Name);
757 JOS.attribute("Value", Flag.Value);
758 JOS.objectEnd();
759 }
760 });
761 });
762 }
763
764 void printFlagsImpl(StringRef Label, HexNumber Value,
765 ArrayRef<HexNumber> Flags) override {
766 JOS.attributeObject(Label, [&]() {
767 JOS.attribute("Value", hexNumberToInt(Value));
768 JOS.attributeArray("Flags", [&]() {
769 for (const HexNumber &Flag : Flags) {
770 JOS.value(Flag.Value);
771 }
772 });
773 });
774 }
775
776 template <typename T> void printListImpl(StringRef Label, const T &List) {
777 JOS.attributeArray(Label, [&]() {
778 for (const auto &Item : List)
779 JOS.value(Item);
780 });
781 }
782
783 void printHexListImpl(StringRef Label,
784 const ArrayRef<HexNumber> List) override {
785 JOS.attributeArray(Label, [&]() {
786 for (const HexNumber &Item : List) {
787 JOS.value(hexNumberToInt(Item));
788 }
789 });
790 }
791
792 void printHexImpl(StringRef Label, HexNumber Value) override {
793 JOS.attribute(Label, hexNumberToInt(Value));
794 }
795
796 void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) override {
797 JOS.attributeObject(Label, [&]() {
798 JOS.attribute("Name", Str);
799 JOS.attribute("Value", hexNumberToInt(Value));
800 });
801 }
802
803 void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
804 HexNumber Value) override {
805 JOS.attributeObject(Label, [&]() {
806 JOS.attribute("SymName", Symbol);
807 JOS.attribute("Offset", hexNumberToInt(Value));
808 });
809 }
810
811 void printNumberImpl(StringRef Label, StringRef Str,
812 StringRef Value) override {
813 JOS.attributeObject(Label, [&]() {
814 JOS.attribute("Name", Str);
815 JOS.attributeBegin("Value");
816 JOS.rawValueBegin() << Value;
817 JOS.rawValueEnd();
818 JOS.attributeEnd();
819 });
820 }
821
822 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
823 bool Block, uint32_t StartOffset = 0) override {
824 JOS.attributeObject(Label, [&]() {
825 if (!Str.empty())
826 JOS.attribute("Value", Str);
827 JOS.attribute("Offset", StartOffset);
828 JOS.attributeArray("Bytes", [&]() {
829 for (uint8_t Val : Value)
830 JOS.value(Val);
831 });
832 });
833 }
834
835 void scopedBegin(ScopeContext ScopeCtx) {
836 if (ScopeCtx.Context == Scope::Object)
837 JOS.objectBegin();
838 else if (ScopeCtx.Context == Scope::Array)
839 JOS.arrayBegin();
840 ScopeHistory.push_back(ScopeCtx);
841 }
842
843 void scopedBegin(StringRef Label, Scope Ctx) {
844 ScopeKind Kind = ScopeKind::Attribute;
845 if (ScopeHistory.empty() || ScopeHistory.back().Context != Scope::Object) {
846 JOS.objectBegin();
847 Kind = ScopeKind::NestedAttribute;
848 }
849 JOS.attributeBegin(Label);
850 scopedBegin({Ctx, Kind});
851 }
852
853 void scopedEnd() {
854 ScopeContext ScopeCtx = ScopeHistory.back();
855 if (ScopeCtx.Context == Scope::Object)
856 JOS.objectEnd();
857 else if (ScopeCtx.Context == Scope::Array)
858 JOS.arrayEnd();
859 if (ScopeCtx.Kind == ScopeKind::Attribute ||
860 ScopeCtx.Kind == ScopeKind::NestedAttribute)
861 JOS.attributeEnd();
862 if (ScopeCtx.Kind == ScopeKind::NestedAttribute)
863 JOS.objectEnd();
864 ScopeHistory.pop_back();
865 }
866};
867
869 explicit DictScope() = default;
871
873 W.objectBegin(N);
874 }
875
876 void setPrinter(ScopedPrinter &W) override {
877 this->W = &W;
878 W.objectBegin();
879 }
880
882 if (W)
883 W->objectEnd();
884 }
885};
886
888 explicit ListScope() = default;
890
892 W.arrayBegin(N);
893 }
894
895 void setPrinter(ScopedPrinter &W) override {
896 this->W = &W;
897 W.arrayBegin();
898 }
899
901 if (W)
902 W->arrayEnd();
903 }
904};
905
906} // namespace llvm
907
908#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")
#define LLVM_ABI
Definition: Compiler.h:213
dxil pretty DXIL Metadata Pretty Printer
std::string Name
This file supports working with JSON data.
#define T
#define P(N)
const NodeList & List
Definition: RDFGraph.cpp:200
raw_pwrite_stream & OS
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
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
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)
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 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={})
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)
bool empty() const
Definition: SmallVector.h:82
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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:996
void attributeObject(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an object with attributes from the Block.
Definition: JSON.h:1059
LLVM_ABI 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:1051
LLVM_ABI void arrayBegin()
Definition: JSON.cpp:842
LLVM_ABI void objectBegin()
Definition: JSON.cpp:861
LLVM_ABI 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:1055
LLVM_ABI void arrayEnd()
Definition: JSON.cpp:850
LLVM_ABI void attributeEnd()
Definition: JSON.cpp:900
LLVM_ABI void value(const Value &V)
Emit a self-contained value (number, string, vector<string> etc).
Definition: JSON.cpp:756
LLVM_ABI void rawValueEnd()
Definition: JSON.cpp:915
LLVM_ABI 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:53
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:149
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::string enumToString(T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1669
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:126
const char * to_string(ThinOrFullLTOPhase Phase)
Definition: Pass.cpp:301
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
StringRef enumToStringRef(T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
Retrieves the Value's enum name.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
#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:37
StringRef AltName
Definition: ScopedPrinter.h:35
StringRef Name
Definition: ScopedPrinter.h:27
constexpr EnumEntry(StringRef N, T V)
Definition: ScopedPrinter.h:39
FlagEntry(StringRef Name, signed long Value)
Definition: ScopedPrinter.h:75
FlagEntry(StringRef Name, signed int Value)
Definition: ScopedPrinter.h:73
FlagEntry(StringRef Name, unsigned short Value)
Definition: ScopedPrinter.h:80
FlagEntry(StringRef Name, unsigned long long Value)
Definition: ScopedPrinter.h:83
FlagEntry(StringRef Name, unsigned long Value)
Definition: ScopedPrinter.h:82
FlagEntry(StringRef Name, signed long long Value)
Definition: ScopedPrinter.h:77
FlagEntry(StringRef Name, char Value)
Definition: ScopedPrinter.h:67
FlagEntry(StringRef Name, signed char Value)
Definition: ScopedPrinter.h:69
FlagEntry(StringRef Name, signed short Value)
Definition: ScopedPrinter.h:71
FlagEntry(StringRef Name, EnumT Value)
Definition: ScopedPrinter.h:86
FlagEntry(StringRef Name, unsigned int Value)
Definition: ScopedPrinter.h:81
FlagEntry(StringRef Name, unsigned char Value)
Definition: ScopedPrinter.h:79
StringRef Name
Definition: ScopedPrinter.h:89
HexNumber(unsigned char Value)
Definition: ScopedPrinter.h:54
HexNumber(signed int Value)
Definition: ScopedPrinter.h:50
HexNumber(signed long long Value)
Definition: ScopedPrinter.h:52
HexNumber(char Value)
Definition: ScopedPrinter.h:47
HexNumber(signed long Value)
Definition: ScopedPrinter.h:51
HexNumber(unsigned int Value)
Definition: ScopedPrinter.h:56
HexNumber(unsigned long long Value)
Definition: ScopedPrinter.h:58
HexNumber(signed char Value)
Definition: ScopedPrinter.h:48
HexNumber(signed short Value)
Definition: ScopedPrinter.h:49
HexNumber(unsigned long Value)
Definition: ScopedPrinter.h:57
HexNumber(unsigned short Value)
Definition: ScopedPrinter.h:55
HexNumber(EnumT Value)
Definition: ScopedPrinter.h:60
ListScope(ScopedPrinter &W)
ListScope()=default
void setPrinter(ScopedPrinter &W) override
ListScope(ScopedPrinter &W, StringRef N)