9#ifndef LLVM_SUPPORT_SCOPEDPRINTER_H
10#define LLVM_SUPPORT_SCOPEDPRINTER_H
59 template <
typename EnumT,
typename = std::enable_if_t<std::is_enum_v<EnumT>>>
85 template <
typename EnumT,
typename = std::enable_if_t<std::is_enum_v<EnumT>>>
102template <
typename T,
typename TEnum>
105 if (EnumItem.Value ==
Value)
106 return std::string(EnumItem.AltName);
113template <
typename T,
typename TEnum>
116 if (EnumItem.Value ==
Value)
117 return EnumItem.AltName;
130 : OS(OS), Kind(Kind) {}
142 void indent(
int Levels = 1) { IndentLevel += Levels; }
145 IndentLevel = IndentLevel > Levels ? IndentLevel - Levels : 0;
156 for (
int i = 0; i < IndentLevel; ++i)
162 template <
typename T,
typename TEnum>
167 for (
const auto &EnumItem : EnumValues) {
168 if (EnumItem.Value ==
Value) {
169 Name = EnumItem.Name;
181 template <
typename T,
typename TFlag>
183 TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
184 TFlag EnumMask3 = {}, ArrayRef<FlagEntry> ExtraFlags = {}) {
185 SmallVector<FlagEntry, 10> SetFlags(ExtraFlags);
187 for (
const auto &Flag : Flags) {
188 if (
Flag.Value == TFlag{})
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{};
200 (IsEnum && (
Value & EnumMask) ==
Flag.Value)) {
201 SetFlags.emplace_back(
Flag.Name,
Flag.Value);
206 printFlagsImpl(Label, hex(
Value), SetFlags);
219 printFlagsImpl(Label,
hex(
Value), SetFlags);
231 startLine() << Label <<
": " <<
static_cast<unsigned>(
Value) <<
"\n";
278 template <
typename T>
289 printVersionInternal(
Version...);
293 template <
typename T>
296 for (
const auto &Item :
List)
302 printListImpl(Label,
List);
306 printListImpl(Label,
List);
310 printListImpl(Label,
List);
314 printListImpl(Label,
List);
318 printListImpl(Label,
List);
325 printListImpl(Label, NumberList);
329 printListImpl(Label,
List);
333 printListImpl(Label,
List);
337 printListImpl(Label,
List);
342 for (
const int8_t &Item :
List)
344 printListImpl(Label, NumberList);
348 printListImpl(Label,
List);
351 template <
typename T,
typename U>
355 for (
const auto &Item :
List) {
364 for (
const auto &Item :
List)
366 printHexListImpl(Label, HexList);
374 printHexImpl(Label, Str,
hex(
Value));
377 template <
typename T>
379 printSymbolOffsetImpl(Label, Symbol,
hex(
Value));
389 printStringEscapedImpl(Label,
Value);
393 printBinaryImpl(Label, Str,
Value,
false);
399 printBinaryImpl(Label, Str, V,
false);
409 printBinaryImpl(Label,
StringRef(), V,
false);
415 printBinaryImpl(Label,
StringRef(), V,
false);
430 printBinaryImpl(Label,
StringRef(), V,
true);
457 template <
typename T>
void printVersionInternal(
T Value) {
458 getOStream() <<
Value;
461 template <
typename S,
typename T,
typename... TArgs>
462 void printVersionInternal(S Value,
T Value2, TArgs... Args) {
463 getOStream() << Value <<
".";
464 printVersionInternal(Value2, Args...);
467 static bool flagName(
const FlagEntry &
LHS,
const FlagEntry &
RHS) {
468 return LHS.Name <
RHS.Name;
471 virtual void printBinaryImpl(StringRef Label, StringRef Str,
472 ArrayRef<uint8_t>
Value,
bool Block,
473 uint32_t StartOffset = 0);
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";
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";
491 template <
typename T>
void printListImpl(StringRef Label,
const T List) {
492 startLine() <<
Label <<
": [";
494 for (
const auto &Item : List)
499 virtual void printHexListImpl(StringRef Label,
500 const ArrayRef<HexNumber> List) {
501 startLine() <<
Label <<
": [";
503 for (
const auto &Item : List)
504 OS <<
LS << hex(Item);
508 virtual void printHexImpl(StringRef Label, HexNumber
Value) {
509 startLine() <<
Label <<
": " <<
Value <<
"\n";
512 virtual void printHexImpl(StringRef Label, StringRef Str, HexNumber
Value) {
513 startLine() <<
Label <<
": " << Str <<
" (" <<
Value <<
")\n";
516 virtual void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
521 virtual void printNumberImpl(StringRef Label, StringRef Str,
523 startLine() <<
Label <<
": " << Str <<
" (" <<
Value <<
")\n";
526 virtual void printStringEscapedImpl(StringRef Label, StringRef
Value) {
527 startLine() <<
Label <<
": ";
528 OS.write_escaped(
Value);
532 void scopedBegin(
char Symbol) {
533 startLine() <<
Symbol <<
'\n';
537 void scopedBegin(StringRef Label,
char Symbol) {
538 startLine() <<
Label;
545 void scopedEnd(
char Symbol) {
547 startLine() <<
Symbol <<
'\n';
553 ScopedPrinterKind
Kind;
560 startLine() << Label <<
": " << hex(
Value) <<
"\n";
578 enum class ScopeKind {
584 struct ScopeContext {
587 ScopeContext(Scope Context, ScopeKind Kind = ScopeKind::NoAttribute)
588 :
Context(Context), Kind(Kind) {}
593 std::unique_ptr<DelimitedScope> OuterScope;
597 std::unique_ptr<DelimitedScope> &&OuterScope =
598 std::unique_ptr<DelimitedScope>{});
605 JOS.attribute(Label,
Value);
609 JOS.attribute(Label,
Value);
613 JOS.attribute(Label,
Value);
617 JOS.attribute(Label,
Value);
621 JOS.attribute(Label,
Value);
625 JOS.attribute(Label,
Value);
629 JOS.attribute(Label,
Value);
633 JOS.attribute(Label,
Value);
637 JOS.attribute(Label,
Value);
641 JOS.attribute(Label,
Value);
645 JOS.attribute(Label,
Value);
649 JOS.attribute(Label,
Value);
653 JOS.attribute(Label,
Value);
657 JOS.attributeBegin(Label);
663 JOS.attribute(Label,
Value);
667 printListImpl(Label,
List);
671 printListImpl(Label,
List);
675 printListImpl(Label,
List);
679 printListImpl(Label,
List);
683 printListImpl(Label,
List);
687 printListImpl(Label,
List);
691 printListImpl(Label,
List);
695 printListImpl(Label,
List);
699 printListImpl(Label,
List);
703 printListImpl(Label,
List);
707 JOS.attributeArray(Label, [&]() {
717 JOS.attribute(Label,
Value);
721 scopedBegin({Scope::Object, ScopeKind::NoAttribute});
725 scopedBegin(Label, Scope::Object);
731 scopedBegin({Scope::Array, ScopeKind::NoAttribute});
735 scopedBegin(Label, Scope::Array);
744 void printAPSInt(
const APSInt &Value) {
749 void printFlagsImpl(StringRef Label, HexNumber
Value,
751 JOS.attributeObject(Label, [&]() {
752 JOS.attribute(
"Value", hexNumberToInt(
Value));
753 JOS.attributeArray(
"Flags", [&]() {
754 for (
const FlagEntry &Flag : Flags) {
756 JOS.attribute(
"Name",
Flag.Name);
757 JOS.attribute(
"Value",
Flag.Value);
764 void printFlagsImpl(StringRef Label, HexNumber
Value,
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);
776 template <
typename T>
void printListImpl(StringRef Label,
const T &
List) {
777 JOS.attributeArray(Label, [&]() {
778 for (
const auto &Item :
List)
783 void printHexListImpl(StringRef Label,
785 JOS.attributeArray(Label, [&]() {
786 for (
const HexNumber &Item :
List) {
787 JOS.value(hexNumberToInt(Item));
792 void printHexImpl(StringRef Label, HexNumber
Value)
override {
793 JOS.attribute(Label, hexNumberToInt(
Value));
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));
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));
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;
822 void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t>
Value,
823 bool Block, uint32_t StartOffset = 0)
override {
824 JOS.attributeObject(Label, [&]() {
826 JOS.attribute(
"Value", Str);
827 JOS.attribute(
"Offset", StartOffset);
828 JOS.attributeArray(
"Bytes", [&]() {
829 for (uint8_t Val :
Value)
835 void scopedBegin(ScopeContext ScopeCtx) {
836 if (ScopeCtx.Context == Scope::Object)
838 else if (ScopeCtx.Context == Scope::Array)
840 ScopeHistory.push_back(ScopeCtx);
843 void scopedBegin(StringRef Label, Scope Ctx) {
844 ScopeKind Kind = ScopeKind::Attribute;
845 if (ScopeHistory.empty() || ScopeHistory.back().Context != Scope::Object) {
847 Kind = ScopeKind::NestedAttribute;
849 JOS.attributeBegin(Label);
850 scopedBegin({Ctx, Kind});
854 ScopeContext ScopeCtx = ScopeHistory.back();
855 if (ScopeCtx.Context == Scope::Object)
857 else if (ScopeCtx.Context == Scope::Array)
859 if (ScopeCtx.Kind == ScopeKind::Attribute ||
860 ScopeCtx.Kind == ScopeKind::NestedAttribute)
862 if (ScopeCtx.Kind == ScopeKind::NestedAttribute)
864 ScopeHistory.pop_back();
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")
dxil pretty DXIL Metadata Pretty Printer
This file supports working with JSON data.
This file defines the SmallVector class.
An arbitrary precision integer that knows its signedness.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
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)
LLVM_ABI JSONScopedPrinter(raw_ostream &OS, bool PrettyPrint=false, std::unique_ptr< DelimitedScope > &&OuterScope=std::unique_ptr< DelimitedScope >{})
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 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 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)
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)
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)
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.
LLVM Value Representation.
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
LLVM_ABI raw_ostream & rawValueBegin()
LLVM_ABI void rawValueEnd()
This class implements an extremely fast bulk output stream that can only output to a stream.
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
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
std::string enumToString(T Value, ArrayRef< EnumEntry< TEnum > > EnumValues)
FunctionAddr VTableAddr uintptr_t uintptr_t Version
void sort(IteratorTy Start, IteratorTy End)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
const char * to_string(ThinOrFullLTOPhase Phase)
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.
Implement std::hash so that hash_code can be used in STL containers.
virtual void setPrinter(ScopedPrinter &W)=0
DelimitedScope(ScopedPrinter &W)
virtual ~DelimitedScope()=default
DictScope(ScopedPrinter &W)
DictScope(ScopedPrinter &W, StringRef N)
void setPrinter(ScopedPrinter &W) override
constexpr EnumEntry(StringRef N, StringRef A, T V)
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(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)
ListScope(ScopedPrinter &W)
void setPrinter(ScopedPrinter &W) override
ListScope(ScopedPrinter &W, StringRef N)