LLVM 23.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"
14#include "llvm/ADT/Enum.h"
18#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Endian.h"
22#include "llvm/Support/JSON.h"
24#include <type_traits>
25
26namespace llvm {
27
28struct HexNumber {
29 // To avoid sign-extension we have to explicitly cast to the appropriate
30 // unsigned type. The overloads are here so that every type that is implicitly
31 // convertible to an integer (including endian helpers) can be used without
32 // requiring type traits or call-site changes.
33 HexNumber(char Value) : Value(static_cast<unsigned char>(Value)) {}
34 HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) {}
35 HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) {}
36 HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) {}
37 HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) {}
38 HexNumber(signed long long Value)
39 : Value(static_cast<unsigned long long>(Value)) {}
40 HexNumber(unsigned char Value) : Value(Value) {}
41 HexNumber(unsigned short Value) : Value(Value) {}
42 HexNumber(unsigned int Value) : Value(Value) {}
43 HexNumber(unsigned long Value) : Value(Value) {}
44 HexNumber(unsigned long long Value) : Value(Value) {}
45 template <typename EnumT, typename = std::enable_if_t<std::is_enum_v<EnumT>>>
47
49};
50
51struct FlagEntry {
53 : Name(Name), Value(static_cast<unsigned char>(Value)) {}
55 : Name(Name), Value(static_cast<unsigned char>(Value)) {}
57 : Name(Name), Value(static_cast<unsigned short>(Value)) {}
59 : Name(Name), Value(static_cast<unsigned int>(Value)) {}
61 : Name(Name), Value(static_cast<unsigned long>(Value)) {}
62 FlagEntry(StringRef Name, signed long long Value)
63 : Name(Name), Value(static_cast<unsigned long long>(Value)) {}
64 FlagEntry(StringRef Name, unsigned char Value) : Name(Name), Value(Value) {}
65 FlagEntry(StringRef Name, unsigned short Value) : Name(Name), Value(Value) {}
67 FlagEntry(StringRef Name, unsigned long Value) : Name(Name), Value(Value) {}
68 FlagEntry(StringRef Name, unsigned long long Value)
69 : Name(Name), Value(Value) {}
70 template <typename EnumT, typename = std::enable_if_t<std::is_enum_v<EnumT>>>
73
76};
77
79
80template <class T> std::string to_string(const T &Value) {
81 std::string number;
83 stream << Value;
84 return number;
85}
86
88public:
89 enum class ScopedPrinterKind {
92 };
93
96 : OS(OS), Kind(Kind) {}
97
98 ScopedPrinterKind getKind() const { return Kind; }
99
100 static bool classof(const ScopedPrinter *SP) {
101 return SP->getKind() == ScopedPrinterKind::Base;
102 }
103
104 virtual ~ScopedPrinter() = default;
105
106 void flush() { OS.flush(); }
107
108 void indent(int Levels = 1) { IndentLevel += Levels; }
109
110 void unindent(int Levels = 1) {
111 IndentLevel = IndentLevel > Levels ? IndentLevel - Levels : 0;
112 }
113
114 void resetIndent() { IndentLevel = 0; }
115
116 int getIndentLevel() { return IndentLevel; }
117
118 void setPrefix(StringRef P) { Prefix = P; }
119
120 void printIndent() {
121 OS << Prefix;
122 for (int i = 0; i < IndentLevel; ++i)
123 OS << " ";
124 }
125
126 template <typename T> HexNumber hex(T Value) { return HexNumber(Value); }
127
128 template <typename T, typename TEnum, unsigned NumStrs>
130 EnumStrings<TEnum, NumStrs> EnumValues) {
131 if (StringRef Name = EnumValues.toString(Value); !Name.empty())
132 printHex(Label, Name, Value);
133 else
134 printHex(Label, Value);
135 }
136
137 template <typename T, typename TFlag, unsigned NumStrs>
139 TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
140 TFlag EnumMask3 = {}, ArrayRef<FlagEntry> ExtraFlags = {}) {
141 SmallVector<FlagEntry, 10> SetFlags(ExtraFlags);
142
143 for (const auto &Flag : Flags) {
144 if (Flag.value() == TFlag{})
145 continue;
146
147 TFlag EnumMask{};
148 if ((Flag.value() & EnumMask1) != TFlag{})
149 EnumMask = EnumMask1;
150 else if ((Flag.value() & EnumMask2) != TFlag{})
151 EnumMask = EnumMask2;
152 else if ((Flag.value() & EnumMask3) != TFlag{})
153 EnumMask = EnumMask3;
154 bool IsEnum = (Flag.value() & EnumMask) != TFlag{};
155 if ((!IsEnum && (Value & Flag.value()) == Flag.value()) ||
156 (IsEnum && (Value & EnumMask) == Flag.value())) {
157 SetFlags.emplace_back(Flag.name(), Flag.value());
158 }
159 }
160
161 llvm::sort(SetFlags, &flagName);
162 printFlagsImpl(Label, hex(Value), SetFlags);
163 }
164
165 template <typename T>
167 SmallVectorImpl<FlagEntry> &SetFlags) {
168 llvm::sort(SetFlags, &flagName);
169 printFlagsImpl(Label, hex(Value), SetFlags);
170 }
171
172 template <typename T> void printFlags(StringRef Label, T Value) {
174 uint64_t Flag = 1;
175 uint64_t Curr = Value;
176 while (Curr > 0) {
177 if (Curr & 1)
178 SetFlags.emplace_back(Flag);
179 Curr >>= 1;
180 Flag <<= 1;
181 }
182 printFlagsImpl(Label, hex(Value), SetFlags);
183 }
184
185 virtual void printNumber(StringRef Label, char Value) {
186 startLine() << Label << ": " << static_cast<int>(Value) << "\n";
187 }
188
189 virtual void printNumber(StringRef Label, signed char Value) {
190 startLine() << Label << ": " << static_cast<int>(Value) << "\n";
191 }
192
193 virtual void printNumber(StringRef Label, unsigned char Value) {
194 startLine() << Label << ": " << static_cast<unsigned>(Value) << "\n";
195 }
196
197 virtual void printNumber(StringRef Label, short Value) {
198 startLine() << Label << ": " << Value << "\n";
199 }
200
201 virtual void printNumber(StringRef Label, unsigned short Value) {
202 startLine() << Label << ": " << Value << "\n";
203 }
204
205 virtual void printNumber(StringRef Label, int Value) {
206 startLine() << Label << ": " << Value << "\n";
207 }
208
209 virtual void printNumber(StringRef Label, unsigned int Value) {
210 startLine() << Label << ": " << Value << "\n";
211 }
212
213 virtual void printNumber(StringRef Label, long Value) {
214 startLine() << Label << ": " << Value << "\n";
215 }
216
217 virtual void printNumber(StringRef Label, unsigned long Value) {
218 startLine() << Label << ": " << Value << "\n";
219 }
220
221 virtual void printNumber(StringRef Label, long long Value) {
222 startLine() << Label << ": " << Value << "\n";
223 }
224
225 virtual void printNumber(StringRef Label, unsigned long long Value) {
226 startLine() << Label << ": " << Value << "\n";
227 }
228
229 virtual void printNumber(StringRef Label, const APSInt &Value) {
230 startLine() << Label << ": " << Value << "\n";
231 }
232
233 virtual void printNumber(StringRef Label, float Value) {
234 startLine() << Label << ": " << format("%5.1f", Value) << "\n";
235 }
236
237 virtual void printNumber(StringRef Label, double Value) {
238 startLine() << Label << ": " << format("%5.1f", Value) << "\n";
239 }
240
241 template <typename T>
243 printNumberImpl(Label, Str, to_string(Value));
244 }
245
246 virtual void printBoolean(StringRef Label, bool Value) {
247 startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
248 }
249
250 template <typename T, typename... TArgs>
251 void printVersion(StringRef Label, T MajorVersion, TArgs... MinorVersions) {
252 startLine() << Label << ": ";
253 getOStream() << MajorVersion;
254 ((getOStream() << '.' << MinorVersions), ...);
255 getOStream() << "\n";
256 }
257
258 template <typename T>
259 void printList(StringRef Label, const ArrayRef<T> List) {
261 for (const auto &Item : List)
262 StringList.emplace_back(to_string(Item));
263 printList(Label, StringList);
264 }
265
266 virtual void printList(StringRef Label, const ArrayRef<bool> List) {
267 printListImpl(Label, List);
268 }
269
270 virtual void printList(StringRef Label, const ArrayRef<std::string> List) {
271 printListImpl(Label, List);
272 }
273
274 virtual void printList(StringRef Label, const ArrayRef<uint64_t> List) {
275 printListImpl(Label, List);
276 }
277
278 virtual void printList(StringRef Label, const ArrayRef<uint32_t> List) {
279 printListImpl(Label, List);
280 }
281
282 virtual void printList(StringRef Label, const ArrayRef<uint16_t> List) {
283 printListImpl(Label, List);
284 }
285
286 virtual void printList(StringRef Label, const ArrayRef<uint8_t> List) {
287 SmallVector<unsigned> NumberList;
288 for (const uint8_t &Item : List)
289 NumberList.emplace_back(Item);
290 printListImpl(Label, NumberList);
291 }
292
293 virtual void printList(StringRef Label, const ArrayRef<int64_t> List) {
294 printListImpl(Label, List);
295 }
296
297 virtual void printList(StringRef Label, const ArrayRef<int32_t> List) {
298 printListImpl(Label, List);
299 }
300
301 virtual void printList(StringRef Label, const ArrayRef<int16_t> List) {
302 printListImpl(Label, List);
303 }
304
305 virtual void printList(StringRef Label, const ArrayRef<int8_t> List) {
306 SmallVector<int> NumberList;
307 for (const int8_t &Item : List)
308 NumberList.emplace_back(Item);
309 printListImpl(Label, NumberList);
310 }
311
312 virtual void printList(StringRef Label, const ArrayRef<APSInt> List) {
313 printListImpl(Label, List);
314 }
315
316 template <typename T, typename U>
317 void printList(StringRef Label, const T &List, const U &Printer) {
318 startLine() << Label << ": [";
319 ListSeparator LS;
320 for (const auto &Item : List) {
321 OS << LS;
322 Printer(OS, Item);
323 }
324 OS << "]\n";
325 }
326
327 template <typename T> void printHexList(StringRef Label, const T &List) {
329 for (const auto &Item : List)
330 HexList.emplace_back(Item);
331 printHexListImpl(Label, HexList);
332 }
333
334 template <typename T> void printHex(StringRef Label, T Value) {
335 printHexImpl(Label, hex(Value));
336 }
337
338 template <typename T> void printHex(StringRef Label, StringRef Str, T Value) {
339 printHexImpl(Label, Str, hex(Value));
340 }
341
342 template <typename T>
344 printSymbolOffsetImpl(Label, Symbol, hex(Value));
345 }
346
347 virtual void printString(StringRef Value) { startLine() << Value << "\n"; }
348
349 virtual void printString(StringRef Label, StringRef Value) {
350 startLine() << Label << ": " << Value << "\n";
351 }
352
354 printStringEscapedImpl(Label, Value);
355 }
356
358 printBinaryImpl(Label, Str, Value, false);
359 }
360
362 auto V =
363 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
364 printBinaryImpl(Label, Str, V, false);
365 }
366
368 printBinaryImpl(Label, StringRef(), Value, false);
369 }
370
372 auto V =
373 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
374 printBinaryImpl(Label, StringRef(), V, false);
375 }
376
378 auto V =
379 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
380 printBinaryImpl(Label, StringRef(), V, false);
381 }
382
384 uint32_t StartOffset) {
385 printBinaryImpl(Label, StringRef(), Value, true, StartOffset);
386 }
387
389 printBinaryImpl(Label, StringRef(), Value, true);
390 }
391
393 auto V =
394 ArrayRef(reinterpret_cast<const uint8_t *>(Value.data()), Value.size());
395 printBinaryImpl(Label, StringRef(), V, true);
396 }
397
398 template <typename T> void printObject(StringRef Label, const T &Value) {
399 printString(Label, to_string(Value));
400 }
401
402 virtual void objectBegin() { scopedBegin('{'); }
403
404 virtual void objectBegin(StringRef Label) { scopedBegin(Label, '{'); }
405
406 virtual void objectEnd() { scopedEnd('}'); }
407
408 virtual void arrayBegin() { scopedBegin('['); }
409
410 virtual void arrayBegin(StringRef Label) { scopedBegin(Label, '['); }
411
412 virtual void arrayEnd() { scopedEnd(']'); }
413
415 printIndent();
416 return OS;
417 }
418
419 virtual raw_ostream &getOStream() { return OS; }
420
421private:
422 static bool flagName(const FlagEntry &LHS, const FlagEntry &RHS) {
423 return LHS.Name < RHS.Name;
424 }
425
426 virtual void printBinaryImpl(StringRef Label, StringRef Str,
427 ArrayRef<uint8_t> Value, bool Block,
428 uint32_t StartOffset = 0);
429
430 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
431 ArrayRef<FlagEntry> Flags) {
432 startLine() << Label << " [ (" << Value << ")\n";
433 for (const auto &Flag : Flags)
434 startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
435 startLine() << "]\n";
436 }
437
438 virtual void printFlagsImpl(StringRef Label, HexNumber Value,
439 ArrayRef<HexNumber> Flags) {
440 startLine() << Label << " [ (" << Value << ")\n";
441 for (const auto &Flag : Flags)
442 startLine() << " " << Flag << '\n';
443 startLine() << "]\n";
444 }
445
446 template <typename T> void printListImpl(StringRef Label, const T List) {
447 startLine() << Label << ": [";
448 ListSeparator LS;
449 for (const auto &Item : List)
450 OS << LS << Item;
451 OS << "]\n";
452 }
453
454 virtual void printHexListImpl(StringRef Label,
455 const ArrayRef<HexNumber> List) {
456 startLine() << Label << ": [";
457 ListSeparator LS;
458 for (const auto &Item : List)
459 OS << LS << hex(Item);
460 OS << "]\n";
461 }
462
463 virtual void printHexImpl(StringRef Label, HexNumber Value) {
464 startLine() << Label << ": " << Value << "\n";
465 }
466
467 virtual void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) {
468 startLine() << Label << ": " << Str << " (" << Value << ")\n";
469 }
470
471 virtual void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
472 HexNumber Value) {
473 startLine() << Label << ": " << Symbol << '+' << Value << '\n';
474 }
475
476 virtual void printNumberImpl(StringRef Label, StringRef Str,
477 StringRef Value) {
478 startLine() << Label << ": " << Str << " (" << Value << ")\n";
479 }
480
481 virtual void printStringEscapedImpl(StringRef Label, StringRef Value) {
482 startLine() << Label << ": ";
483 OS.write_escaped(Value);
484 OS << '\n';
485 }
486
487 void scopedBegin(char Symbol) {
488 startLine() << Symbol << '\n';
489 indent();
490 }
491
492 void scopedBegin(StringRef Label, char Symbol) {
493 startLine() << Label;
494 if (!Label.empty())
495 OS << ' ';
496 OS << Symbol << '\n';
497 indent();
498 }
499
500 void scopedEnd(char Symbol) {
501 unindent();
502 startLine() << Symbol << '\n';
503 }
504
505 raw_ostream &OS;
506 int IndentLevel = 0;
507 StringRef Prefix;
508 ScopedPrinterKind Kind;
509};
510
511template <>
512inline void
515 startLine() << Label << ": " << hex(Value) << "\n";
516}
517
520 DelimitedScope() : W(nullptr) {}
521 virtual ~DelimitedScope() = default;
522 virtual void setPrinter(ScopedPrinter &W) = 0;
524};
525
527private:
528 enum class Scope {
529 Array,
530 Object,
531 };
532
533 enum class ScopeKind {
534 NoAttribute,
535 Attribute,
536 NestedAttribute,
537 };
538
539 struct ScopeContext {
540 Scope Context;
541 ScopeKind Kind;
542 ScopeContext(Scope Context, ScopeKind Kind = ScopeKind::NoAttribute)
543 : Context(Context), Kind(Kind) {}
544 };
545
546 SmallVector<ScopeContext, 8> ScopeHistory;
547 json::OStream JOS;
548 std::unique_ptr<DelimitedScope> OuterScope;
549
550public:
551 LLVM_ABI JSONScopedPrinter(raw_ostream &OS, bool PrettyPrint = false,
552 std::unique_ptr<DelimitedScope> &&OuterScope =
553 std::unique_ptr<DelimitedScope>{});
554
555 static bool classof(const ScopedPrinter *SP) {
556 return SP->getKind() == ScopedPrinter::ScopedPrinterKind::JSON;
557 }
558
559 void printNumber(StringRef Label, char Value) override {
560 JOS.attribute(Label, Value);
561 }
562
563 void printNumber(StringRef Label, signed char Value) override {
564 JOS.attribute(Label, Value);
565 }
566
567 void printNumber(StringRef Label, unsigned char Value) override {
568 JOS.attribute(Label, Value);
569 }
570
571 void printNumber(StringRef Label, short Value) override {
572 JOS.attribute(Label, Value);
573 }
574
575 void printNumber(StringRef Label, unsigned short Value) override {
576 JOS.attribute(Label, Value);
577 }
578
579 void printNumber(StringRef Label, int Value) override {
580 JOS.attribute(Label, Value);
581 }
582
583 void printNumber(StringRef Label, unsigned int Value) override {
584 JOS.attribute(Label, Value);
585 }
586
587 void printNumber(StringRef Label, long Value) override {
588 JOS.attribute(Label, Value);
589 }
590
591 void printNumber(StringRef Label, unsigned long Value) override {
592 JOS.attribute(Label, Value);
593 }
594
595 void printNumber(StringRef Label, long long Value) override {
596 JOS.attribute(Label, Value);
597 }
598
599 void printNumber(StringRef Label, unsigned long long Value) override {
600 JOS.attribute(Label, Value);
601 }
602
603 void printNumber(StringRef Label, float Value) override {
604 JOS.attribute(Label, Value);
605 }
606
607 void printNumber(StringRef Label, double Value) override {
608 JOS.attribute(Label, Value);
609 }
610
611 void printNumber(StringRef Label, const APSInt &Value) override {
612 JOS.attributeBegin(Label);
613 printAPSInt(Value);
614 JOS.attributeEnd();
615 }
616
617 void printBoolean(StringRef Label, bool Value) override {
618 JOS.attribute(Label, Value);
619 }
620
621 void printList(StringRef Label, const ArrayRef<bool> List) override {
622 printListImpl(Label, List);
623 }
624
625 void printList(StringRef Label, const ArrayRef<std::string> List) override {
626 printListImpl(Label, List);
627 }
628
629 void printList(StringRef Label, const ArrayRef<uint64_t> List) override {
630 printListImpl(Label, List);
631 }
632
633 void printList(StringRef Label, const ArrayRef<uint32_t> List) override {
634 printListImpl(Label, List);
635 }
636
637 void printList(StringRef Label, const ArrayRef<uint16_t> List) override {
638 printListImpl(Label, List);
639 }
640
641 void printList(StringRef Label, const ArrayRef<uint8_t> List) override {
642 printListImpl(Label, List);
643 }
644
645 void printList(StringRef Label, const ArrayRef<int64_t> List) override {
646 printListImpl(Label, List);
647 }
648
649 void printList(StringRef Label, const ArrayRef<int32_t> List) override {
650 printListImpl(Label, List);
651 }
652
653 void printList(StringRef Label, const ArrayRef<int16_t> List) override {
654 printListImpl(Label, List);
655 }
656
657 void printList(StringRef Label, const ArrayRef<int8_t> List) override {
658 printListImpl(Label, List);
659 }
660
661 void printList(StringRef Label, const ArrayRef<APSInt> List) override {
662 JOS.attributeArray(Label, [&]() {
663 for (const APSInt &Item : List) {
664 printAPSInt(Item);
665 }
666 });
667 }
668
669 void printString(StringRef Value) override { JOS.value(Value); }
670
671 void printString(StringRef Label, StringRef Value) override {
672 JOS.attribute(Label, Value);
673 }
674
675 void objectBegin() override {
676 scopedBegin({Scope::Object, ScopeKind::NoAttribute});
677 }
678
679 void objectBegin(StringRef Label) override {
680 scopedBegin(Label, Scope::Object);
681 }
682
683 void objectEnd() override { scopedEnd(); }
684
685 void arrayBegin() override {
686 scopedBegin({Scope::Array, ScopeKind::NoAttribute});
687 }
688
689 void arrayBegin(StringRef Label) override {
690 scopedBegin(Label, Scope::Array);
691 }
692
693 void arrayEnd() override { scopedEnd(); }
694
695private:
696 // Output HexNumbers as decimals so that they're easier to parse.
697 uint64_t hexNumberToInt(HexNumber Hex) { return Hex.Value; }
698
699 void printAPSInt(const APSInt &Value) {
700 JOS.rawValueBegin() << Value;
701 JOS.rawValueEnd();
702 }
703
704 void printFlagsImpl(StringRef Label, HexNumber Value,
705 ArrayRef<FlagEntry> Flags) override {
706 JOS.attributeObject(Label, [&]() {
707 JOS.attribute("Value", hexNumberToInt(Value));
708 JOS.attributeArray("Flags", [&]() {
709 for (const FlagEntry &Flag : Flags) {
710 JOS.objectBegin();
711 JOS.attribute("Name", Flag.Name);
712 JOS.attribute("Value", Flag.Value);
713 JOS.objectEnd();
714 }
715 });
716 });
717 }
718
719 void printFlagsImpl(StringRef Label, HexNumber Value,
720 ArrayRef<HexNumber> Flags) override {
721 JOS.attributeObject(Label, [&]() {
722 JOS.attribute("Value", hexNumberToInt(Value));
723 JOS.attributeArray("Flags", [&]() {
724 for (const HexNumber &Flag : Flags) {
725 JOS.value(Flag.Value);
726 }
727 });
728 });
729 }
730
731 template <typename T> void printListImpl(StringRef Label, const T &List) {
732 JOS.attributeArray(Label, [&]() {
733 for (const auto &Item : List)
734 JOS.value(Item);
735 });
736 }
737
738 void printHexListImpl(StringRef Label,
739 const ArrayRef<HexNumber> List) override {
740 JOS.attributeArray(Label, [&]() {
741 for (const HexNumber &Item : List) {
742 JOS.value(hexNumberToInt(Item));
743 }
744 });
745 }
746
747 void printHexImpl(StringRef Label, HexNumber Value) override {
748 JOS.attribute(Label, hexNumberToInt(Value));
749 }
750
751 void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) override {
752 JOS.attributeObject(Label, [&]() {
753 JOS.attribute("Name", Str);
754 JOS.attribute("Value", hexNumberToInt(Value));
755 });
756 }
757
758 void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
759 HexNumber Value) override {
760 JOS.attributeObject(Label, [&]() {
761 JOS.attribute("SymName", Symbol);
762 JOS.attribute("Offset", hexNumberToInt(Value));
763 });
764 }
765
766 void printNumberImpl(StringRef Label, StringRef Str,
767 StringRef Value) override {
768 JOS.attributeObject(Label, [&]() {
769 JOS.attribute("Name", Str);
770 JOS.attributeBegin("Value");
771 JOS.rawValueBegin() << Value;
772 JOS.rawValueEnd();
773 JOS.attributeEnd();
774 });
775 }
776
777 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
778 bool Block, uint32_t StartOffset = 0) override {
779 JOS.attributeObject(Label, [&]() {
780 if (!Str.empty())
781 JOS.attribute("Value", Str);
782 JOS.attribute("Offset", StartOffset);
783 JOS.attributeArray("Bytes", [&]() {
784 for (uint8_t Val : Value)
785 JOS.value(Val);
786 });
787 });
788 }
789
790 void scopedBegin(ScopeContext ScopeCtx) {
791 if (ScopeCtx.Context == Scope::Object)
792 JOS.objectBegin();
793 else if (ScopeCtx.Context == Scope::Array)
794 JOS.arrayBegin();
795 ScopeHistory.push_back(ScopeCtx);
796 }
797
798 void scopedBegin(StringRef Label, Scope Ctx) {
799 ScopeKind Kind = ScopeKind::Attribute;
800 if (ScopeHistory.empty() || ScopeHistory.back().Context != Scope::Object) {
801 JOS.objectBegin();
802 Kind = ScopeKind::NestedAttribute;
803 }
804 JOS.attributeBegin(Label);
805 scopedBegin({Ctx, Kind});
806 }
807
808 void scopedEnd() {
809 ScopeContext ScopeCtx = ScopeHistory.back();
810 if (ScopeCtx.Context == Scope::Object)
811 JOS.objectEnd();
812 else if (ScopeCtx.Context == Scope::Array)
813 JOS.arrayEnd();
814 if (ScopeCtx.Kind == ScopeKind::Attribute ||
815 ScopeCtx.Kind == ScopeKind::NestedAttribute)
816 JOS.attributeEnd();
817 if (ScopeCtx.Kind == ScopeKind::NestedAttribute)
818 JOS.objectEnd();
819 ScopeHistory.pop_back();
820 }
821};
822
824 explicit DictScope() = default;
825 explicit DictScope(ScopedPrinter &W) : DelimitedScope(W) { W.objectBegin(); }
826
828 W.objectBegin(N);
829 }
830
831 void setPrinter(ScopedPrinter &W) override {
832 this->W = &W;
833 W.objectBegin();
834 }
835
836 ~DictScope() override {
837 if (W)
838 W->objectEnd();
839 }
840};
841
843 explicit ListScope() = default;
844 explicit ListScope(ScopedPrinter &W) : DelimitedScope(W) { W.arrayBegin(); }
845
847 W.arrayBegin(N);
848 }
849
850 void setPrinter(ScopedPrinter &W) override {
851 this->W = &W;
852 W.arrayBegin();
853 }
854
855 ~ListScope() override {
856 if (W)
857 W->arrayEnd();
858 }
859};
860
861} // namespace llvm
862
863#endif
amdgpu next use AMDGPU Next Use Analysis Printer
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)
#define LLVM_ABI
Definition Compiler.h:215
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
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
StringRef toString(TValue Value, unsigned StrIdx=0) const
Definition Enum.h:135
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)
void printFlags(StringRef Label, T Value, SmallVectorImpl< FlagEntry > &SetFlags)
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)
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)
void printFlags(StringRef Label, T Value, EnumStrings< TFlag, NumStrs > Flags, TFlag EnumMask1={}, TFlag EnumMask2={}, TFlag EnumMask3={}, ArrayRef< FlagEntry > ExtraFlags={})
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 printVersion(StringRef Label, T MajorVersion, TArgs... MinorVersions)
virtual ~ScopedPrinter()=default
void printEnum(StringRef Label, T Value, EnumStrings< TEnum, NumStrs > EnumValues)
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)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:982
LLVM_ABI raw_ostream & rawValueBegin()
Definition JSON.cpp:911
LLVM_ABI void rawValueEnd()
Definition JSON.cpp:918
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.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
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:94
const char * to_string(ThinOrFullLTOPhase Phase)
Definition Pass.cpp:306
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
#define N
virtual void setPrinter(ScopedPrinter &W)=0
DelimitedScope(ScopedPrinter &W)
ScopedPrinter * W
virtual ~DelimitedScope()=default
~DictScope() override
DictScope(ScopedPrinter &W)
DictScope()=default
DictScope(ScopedPrinter &W, StringRef N)
void setPrinter(ScopedPrinter &W) override
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
~ListScope() override
void setPrinter(ScopedPrinter &W) override
ListScope(ScopedPrinter &W, StringRef N)