ScopedPrinter.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- ScopedPrinter.h ----------------------------------------*- C++ -*--===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_SCOPEDPRINTER_H
  14. #define LLVM_SUPPORT_SCOPEDPRINTER_H
  15. #include "llvm/ADT/APSInt.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Support/DataTypes.h"
  21. #include "llvm/Support/Endian.h"
  22. #include "llvm/Support/JSON.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. namespace llvm {
  25. template <typename T> struct EnumEntry {
  26. StringRef Name;
  27. // While Name suffices in most of the cases, in certain cases
  28. // GNU style and LLVM style of ELFDumper do not
  29. // display same string for same enum. The AltName if initialized appropriately
  30. // will hold the string that GNU style emits.
  31. // Example:
  32. // "EM_X86_64" string on LLVM style for Elf_Ehdr->e_machine corresponds to
  33. // "Advanced Micro Devices X86-64" on GNU style
  34. StringRef AltName;
  35. T Value;
  36. constexpr EnumEntry(StringRef N, StringRef A, T V)
  37. : Name(N), AltName(A), Value(V) {}
  38. constexpr EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {}
  39. };
  40. struct HexNumber {
  41. // To avoid sign-extension we have to explicitly cast to the appropriate
  42. // unsigned type. The overloads are here so that every type that is implicitly
  43. // convertible to an integer (including enums and endian helpers) can be used
  44. // without requiring type traits or call-site changes.
  45. HexNumber(char Value) : Value(static_cast<unsigned char>(Value)) {}
  46. HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) {}
  47. HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) {}
  48. HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) {}
  49. HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) {}
  50. HexNumber(signed long long Value)
  51. : Value(static_cast<unsigned long long>(Value)) {}
  52. HexNumber(unsigned char Value) : Value(Value) {}
  53. HexNumber(unsigned short Value) : Value(Value) {}
  54. HexNumber(unsigned int Value) : Value(Value) {}
  55. HexNumber(unsigned long Value) : Value(Value) {}
  56. HexNumber(unsigned long long Value) : Value(Value) {}
  57. uint64_t Value;
  58. };
  59. struct FlagEntry {
  60. FlagEntry(StringRef Name, char Value)
  61. : Name(Name), Value(static_cast<unsigned char>(Value)) {}
  62. FlagEntry(StringRef Name, signed char Value)
  63. : Name(Name), Value(static_cast<unsigned char>(Value)) {}
  64. FlagEntry(StringRef Name, signed short Value)
  65. : Name(Name), Value(static_cast<unsigned short>(Value)) {}
  66. FlagEntry(StringRef Name, signed int Value)
  67. : Name(Name), Value(static_cast<unsigned int>(Value)) {}
  68. FlagEntry(StringRef Name, signed long Value)
  69. : Name(Name), Value(static_cast<unsigned long>(Value)) {}
  70. FlagEntry(StringRef Name, signed long long Value)
  71. : Name(Name), Value(static_cast<unsigned long long>(Value)) {}
  72. FlagEntry(StringRef Name, unsigned char Value) : Name(Name), Value(Value) {}
  73. FlagEntry(StringRef Name, unsigned short Value) : Name(Name), Value(Value) {}
  74. FlagEntry(StringRef Name, unsigned int Value) : Name(Name), Value(Value) {}
  75. FlagEntry(StringRef Name, unsigned long Value) : Name(Name), Value(Value) {}
  76. FlagEntry(StringRef Name, unsigned long long Value)
  77. : Name(Name), Value(Value) {}
  78. StringRef Name;
  79. uint64_t Value;
  80. };
  81. raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value);
  82. std::string to_hexString(uint64_t Value, bool UpperCase = true);
  83. template <class T> std::string to_string(const T &Value) {
  84. std::string number;
  85. raw_string_ostream stream(number);
  86. stream << Value;
  87. return stream.str();
  88. }
  89. template <typename T, typename TEnum>
  90. std::string enumToString(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
  91. for (const EnumEntry<TEnum> &EnumItem : EnumValues)
  92. if (EnumItem.Value == Value)
  93. return std::string(EnumItem.AltName);
  94. return to_hexString(Value, false);
  95. }
  96. class ScopedPrinter {
  97. public:
  98. enum class ScopedPrinterKind {
  99. Base,
  100. JSON,
  101. };
  102. ScopedPrinter(raw_ostream &OS,
  103. ScopedPrinterKind Kind = ScopedPrinterKind::Base)
  104. : OS(OS), IndentLevel(0), Kind(Kind) {}
  105. ScopedPrinterKind getKind() const { return Kind; }
  106. static bool classof(const ScopedPrinter *SP) {
  107. return SP->getKind() == ScopedPrinterKind::Base;
  108. }
  109. virtual ~ScopedPrinter() = default;
  110. void flush() { OS.flush(); }
  111. void indent(int Levels = 1) { IndentLevel += Levels; }
  112. void unindent(int Levels = 1) {
  113. IndentLevel = IndentLevel > Levels ? IndentLevel - Levels : 0;
  114. }
  115. void resetIndent() { IndentLevel = 0; }
  116. int getIndentLevel() { return IndentLevel; }
  117. void setPrefix(StringRef P) { Prefix = P; }
  118. void printIndent() {
  119. OS << Prefix;
  120. for (int i = 0; i < IndentLevel; ++i)
  121. OS << " ";
  122. }
  123. template <typename T> HexNumber hex(T Value) { return HexNumber(Value); }
  124. template <typename T, typename TEnum>
  125. void printEnum(StringRef Label, T Value,
  126. ArrayRef<EnumEntry<TEnum>> EnumValues) {
  127. StringRef Name;
  128. bool Found = false;
  129. for (const auto &EnumItem : EnumValues) {
  130. if (EnumItem.Value == Value) {
  131. Name = EnumItem.Name;
  132. Found = true;
  133. break;
  134. }
  135. }
  136. if (Found)
  137. printHex(Label, Name, Value);
  138. else
  139. printHex(Label, Value);
  140. }
  141. template <typename T, typename TFlag>
  142. void printFlags(StringRef Label, T Value, ArrayRef<EnumEntry<TFlag>> Flags,
  143. TFlag EnumMask1 = {}, TFlag EnumMask2 = {},
  144. TFlag EnumMask3 = {}) {
  145. SmallVector<FlagEntry, 10> SetFlags;
  146. for (const auto &Flag : Flags) {
  147. if (Flag.Value == 0)
  148. continue;
  149. TFlag EnumMask{};
  150. if (Flag.Value & EnumMask1)
  151. EnumMask = EnumMask1;
  152. else if (Flag.Value & EnumMask2)
  153. EnumMask = EnumMask2;
  154. else if (Flag.Value & EnumMask3)
  155. EnumMask = EnumMask3;
  156. bool IsEnum = (Flag.Value & EnumMask) != 0;
  157. if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
  158. (IsEnum && (Value & EnumMask) == Flag.Value)) {
  159. SetFlags.emplace_back(Flag.Name, Flag.Value);
  160. }
  161. }
  162. llvm::sort(SetFlags, &flagName);
  163. printFlagsImpl(Label, hex(Value), SetFlags);
  164. }
  165. template <typename T> void printFlags(StringRef Label, T Value) {
  166. SmallVector<HexNumber, 10> SetFlags;
  167. uint64_t Flag = 1;
  168. uint64_t Curr = Value;
  169. while (Curr > 0) {
  170. if (Curr & 1)
  171. SetFlags.emplace_back(Flag);
  172. Curr >>= 1;
  173. Flag <<= 1;
  174. }
  175. printFlagsImpl(Label, hex(Value), SetFlags);
  176. }
  177. virtual void printNumber(StringRef Label, uint64_t Value) {
  178. startLine() << Label << ": " << Value << "\n";
  179. }
  180. virtual void printNumber(StringRef Label, uint32_t Value) {
  181. startLine() << Label << ": " << Value << "\n";
  182. }
  183. virtual void printNumber(StringRef Label, uint16_t Value) {
  184. startLine() << Label << ": " << Value << "\n";
  185. }
  186. virtual void printNumber(StringRef Label, uint8_t Value) {
  187. startLine() << Label << ": " << unsigned(Value) << "\n";
  188. }
  189. virtual void printNumber(StringRef Label, int64_t Value) {
  190. startLine() << Label << ": " << Value << "\n";
  191. }
  192. virtual void printNumber(StringRef Label, int32_t Value) {
  193. startLine() << Label << ": " << Value << "\n";
  194. }
  195. virtual void printNumber(StringRef Label, int16_t Value) {
  196. startLine() << Label << ": " << Value << "\n";
  197. }
  198. virtual void printNumber(StringRef Label, int8_t Value) {
  199. startLine() << Label << ": " << int(Value) << "\n";
  200. }
  201. virtual void printNumber(StringRef Label, const APSInt &Value) {
  202. startLine() << Label << ": " << Value << "\n";
  203. }
  204. template <typename T>
  205. void printNumber(StringRef Label, StringRef Str, T Value) {
  206. printNumberImpl(Label, Str, to_string(Value));
  207. }
  208. virtual void printBoolean(StringRef Label, bool Value) {
  209. startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
  210. }
  211. template <typename... T> void printVersion(StringRef Label, T... Version) {
  212. startLine() << Label << ": ";
  213. printVersionInternal(Version...);
  214. getOStream() << "\n";
  215. }
  216. template <typename T>
  217. void printList(StringRef Label, const ArrayRef<T> List) {
  218. SmallVector<std::string, 10> StringList;
  219. for (const auto &Item : List)
  220. StringList.emplace_back(to_string(Item));
  221. printList(Label, StringList);
  222. }
  223. virtual void printList(StringRef Label, const ArrayRef<bool> List) {
  224. printListImpl(Label, List);
  225. }
  226. virtual void printList(StringRef Label, const ArrayRef<std::string> List) {
  227. printListImpl(Label, List);
  228. }
  229. virtual void printList(StringRef Label, const ArrayRef<uint64_t> List) {
  230. printListImpl(Label, List);
  231. }
  232. virtual void printList(StringRef Label, const ArrayRef<uint32_t> List) {
  233. printListImpl(Label, List);
  234. }
  235. virtual void printList(StringRef Label, const ArrayRef<uint16_t> List) {
  236. printListImpl(Label, List);
  237. }
  238. virtual void printList(StringRef Label, const ArrayRef<uint8_t> List) {
  239. SmallVector<unsigned> NumberList;
  240. for (const uint8_t &Item : List)
  241. NumberList.emplace_back(Item);
  242. printListImpl(Label, NumberList);
  243. }
  244. virtual void printList(StringRef Label, const ArrayRef<int64_t> List) {
  245. printListImpl(Label, List);
  246. }
  247. virtual void printList(StringRef Label, const ArrayRef<int32_t> List) {
  248. printListImpl(Label, List);
  249. }
  250. virtual void printList(StringRef Label, const ArrayRef<int16_t> List) {
  251. printListImpl(Label, List);
  252. }
  253. virtual void printList(StringRef Label, const ArrayRef<int8_t> List) {
  254. SmallVector<int> NumberList;
  255. for (const int8_t &Item : List)
  256. NumberList.emplace_back(Item);
  257. printListImpl(Label, NumberList);
  258. }
  259. virtual void printList(StringRef Label, const ArrayRef<APSInt> List) {
  260. printListImpl(Label, List);
  261. }
  262. template <typename T, typename U>
  263. void printList(StringRef Label, const T &List, const U &Printer) {
  264. startLine() << Label << ": [";
  265. ListSeparator LS;
  266. for (const auto &Item : List) {
  267. OS << LS;
  268. Printer(OS, Item);
  269. }
  270. OS << "]\n";
  271. }
  272. template <typename T> void printHexList(StringRef Label, const T &List) {
  273. SmallVector<HexNumber> HexList;
  274. for (const auto &Item : List)
  275. HexList.emplace_back(Item);
  276. printHexListImpl(Label, HexList);
  277. }
  278. template <typename T> void printHex(StringRef Label, T Value) {
  279. printHexImpl(Label, hex(Value));
  280. }
  281. template <typename T> void printHex(StringRef Label, StringRef Str, T Value) {
  282. printHexImpl(Label, Str, hex(Value));
  283. }
  284. template <typename T>
  285. void printSymbolOffset(StringRef Label, StringRef Symbol, T Value) {
  286. printSymbolOffsetImpl(Label, Symbol, hex(Value));
  287. }
  288. virtual void printString(StringRef Value) { startLine() << Value << "\n"; }
  289. virtual void printString(StringRef Label, StringRef Value) {
  290. startLine() << Label << ": " << Value << "\n";
  291. }
  292. void printBinary(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value) {
  293. printBinaryImpl(Label, Str, Value, false);
  294. }
  295. void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) {
  296. auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
  297. Value.size());
  298. printBinaryImpl(Label, Str, V, false);
  299. }
  300. void printBinary(StringRef Label, ArrayRef<uint8_t> Value) {
  301. printBinaryImpl(Label, StringRef(), Value, false);
  302. }
  303. void printBinary(StringRef Label, ArrayRef<char> Value) {
  304. auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
  305. Value.size());
  306. printBinaryImpl(Label, StringRef(), V, false);
  307. }
  308. void printBinary(StringRef Label, StringRef Value) {
  309. auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
  310. Value.size());
  311. printBinaryImpl(Label, StringRef(), V, false);
  312. }
  313. void printBinaryBlock(StringRef Label, ArrayRef<uint8_t> Value,
  314. uint32_t StartOffset) {
  315. printBinaryImpl(Label, StringRef(), Value, true, StartOffset);
  316. }
  317. void printBinaryBlock(StringRef Label, ArrayRef<uint8_t> Value) {
  318. printBinaryImpl(Label, StringRef(), Value, true);
  319. }
  320. void printBinaryBlock(StringRef Label, StringRef Value) {
  321. auto V = makeArrayRef(reinterpret_cast<const uint8_t *>(Value.data()),
  322. Value.size());
  323. printBinaryImpl(Label, StringRef(), V, true);
  324. }
  325. template <typename T> void printObject(StringRef Label, const T &Value) {
  326. printString(Label, to_string(Value));
  327. }
  328. virtual void objectBegin() { scopedBegin('{'); }
  329. virtual void objectBegin(StringRef Label) { scopedBegin(Label, '{'); }
  330. virtual void objectEnd() { scopedEnd('}'); }
  331. virtual void arrayBegin() { scopedBegin('['); }
  332. virtual void arrayBegin(StringRef Label) { scopedBegin(Label, '['); }
  333. virtual void arrayEnd() { scopedEnd(']'); }
  334. virtual raw_ostream &startLine() {
  335. printIndent();
  336. return OS;
  337. }
  338. virtual raw_ostream &getOStream() { return OS; }
  339. private:
  340. template <typename T> void printVersionInternal(T Value) {
  341. getOStream() << Value;
  342. }
  343. template <typename S, typename T, typename... TArgs>
  344. void printVersionInternal(S Value, T Value2, TArgs... Args) {
  345. getOStream() << Value << ".";
  346. printVersionInternal(Value2, Args...);
  347. }
  348. static bool flagName(const FlagEntry &LHS, const FlagEntry &RHS) {
  349. return LHS.Name < RHS.Name;
  350. }
  351. virtual void printBinaryImpl(StringRef Label, StringRef Str,
  352. ArrayRef<uint8_t> Value, bool Block,
  353. uint32_t StartOffset = 0);
  354. virtual void printFlagsImpl(StringRef Label, HexNumber Value,
  355. ArrayRef<FlagEntry> Flags) {
  356. startLine() << Label << " [ (" << Value << ")\n";
  357. for (const auto &Flag : Flags)
  358. startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n";
  359. startLine() << "]\n";
  360. }
  361. virtual void printFlagsImpl(StringRef Label, HexNumber Value,
  362. ArrayRef<HexNumber> Flags) {
  363. startLine() << Label << " [ (" << Value << ")\n";
  364. for (const auto &Flag : Flags)
  365. startLine() << " " << Flag << '\n';
  366. startLine() << "]\n";
  367. }
  368. template <typename T> void printListImpl(StringRef Label, const T List) {
  369. startLine() << Label << ": [";
  370. ListSeparator LS;
  371. for (const auto &Item : List)
  372. OS << LS << Item;
  373. OS << "]\n";
  374. }
  375. virtual void printHexListImpl(StringRef Label,
  376. const ArrayRef<HexNumber> List) {
  377. startLine() << Label << ": [";
  378. ListSeparator LS;
  379. for (const auto &Item : List)
  380. OS << LS << hex(Item);
  381. OS << "]\n";
  382. }
  383. virtual void printHexImpl(StringRef Label, HexNumber Value) {
  384. startLine() << Label << ": " << Value << "\n";
  385. }
  386. virtual void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) {
  387. startLine() << Label << ": " << Str << " (" << Value << ")\n";
  388. }
  389. virtual void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
  390. HexNumber Value) {
  391. startLine() << Label << ": " << Symbol << '+' << Value << '\n';
  392. }
  393. virtual void printNumberImpl(StringRef Label, StringRef Str,
  394. StringRef Value) {
  395. startLine() << Label << ": " << Str << " (" << Value << ")\n";
  396. }
  397. void scopedBegin(char Symbol) {
  398. startLine() << Symbol << '\n';
  399. indent();
  400. }
  401. void scopedBegin(StringRef Label, char Symbol) {
  402. startLine() << Label;
  403. if (!Label.empty())
  404. OS << ' ';
  405. OS << Symbol << '\n';
  406. indent();
  407. }
  408. void scopedEnd(char Symbol) {
  409. unindent();
  410. startLine() << Symbol << '\n';
  411. }
  412. raw_ostream &OS;
  413. int IndentLevel;
  414. StringRef Prefix;
  415. ScopedPrinterKind Kind;
  416. };
  417. template <>
  418. inline void
  419. ScopedPrinter::printHex<support::ulittle16_t>(StringRef Label,
  420. support::ulittle16_t Value) {
  421. startLine() << Label << ": " << hex(Value) << "\n";
  422. }
  423. struct DelimitedScope;
  424. class JSONScopedPrinter : public ScopedPrinter {
  425. private:
  426. enum class Scope {
  427. Array,
  428. Object,
  429. };
  430. enum class ScopeKind {
  431. NoAttribute,
  432. Attribute,
  433. NestedAttribute,
  434. };
  435. struct ScopeContext {
  436. Scope Context;
  437. ScopeKind Kind;
  438. ScopeContext(Scope Context, ScopeKind Kind = ScopeKind::NoAttribute)
  439. : Context(Context), Kind(Kind) {}
  440. };
  441. SmallVector<ScopeContext, 8> ScopeHistory;
  442. json::OStream JOS;
  443. std::unique_ptr<DelimitedScope> OuterScope;
  444. public:
  445. JSONScopedPrinter(raw_ostream &OS, bool PrettyPrint = false,
  446. std::unique_ptr<DelimitedScope> &&OuterScope =
  447. std::unique_ptr<DelimitedScope>{});
  448. static bool classof(const ScopedPrinter *SP) {
  449. return SP->getKind() == ScopedPrinter::ScopedPrinterKind::JSON;
  450. }
  451. void printNumber(StringRef Label, uint64_t Value) override {
  452. JOS.attribute(Label, Value);
  453. }
  454. void printNumber(StringRef Label, uint32_t Value) override {
  455. JOS.attribute(Label, Value);
  456. }
  457. void printNumber(StringRef Label, uint16_t Value) override {
  458. JOS.attribute(Label, Value);
  459. }
  460. void printNumber(StringRef Label, uint8_t Value) override {
  461. JOS.attribute(Label, Value);
  462. }
  463. void printNumber(StringRef Label, int64_t Value) override {
  464. JOS.attribute(Label, Value);
  465. }
  466. void printNumber(StringRef Label, int32_t Value) override {
  467. JOS.attribute(Label, Value);
  468. }
  469. void printNumber(StringRef Label, int16_t Value) override {
  470. JOS.attribute(Label, Value);
  471. }
  472. void printNumber(StringRef Label, int8_t Value) override {
  473. JOS.attribute(Label, Value);
  474. }
  475. void printNumber(StringRef Label, const APSInt &Value) override {
  476. JOS.attributeBegin(Label);
  477. printAPSInt(Value);
  478. JOS.attributeEnd();
  479. }
  480. void printBoolean(StringRef Label, bool Value) override {
  481. JOS.attribute(Label, Value);
  482. }
  483. void printList(StringRef Label, const ArrayRef<bool> List) override {
  484. printListImpl(Label, List);
  485. }
  486. void printList(StringRef Label, const ArrayRef<std::string> List) override {
  487. printListImpl(Label, List);
  488. }
  489. void printList(StringRef Label, const ArrayRef<uint64_t> List) override {
  490. printListImpl(Label, List);
  491. }
  492. void printList(StringRef Label, const ArrayRef<uint32_t> List) override {
  493. printListImpl(Label, List);
  494. }
  495. void printList(StringRef Label, const ArrayRef<uint16_t> List) override {
  496. printListImpl(Label, List);
  497. }
  498. void printList(StringRef Label, const ArrayRef<uint8_t> List) override {
  499. printListImpl(Label, List);
  500. }
  501. void printList(StringRef Label, const ArrayRef<int64_t> List) override {
  502. printListImpl(Label, List);
  503. }
  504. void printList(StringRef Label, const ArrayRef<int32_t> List) override {
  505. printListImpl(Label, List);
  506. }
  507. void printList(StringRef Label, const ArrayRef<int16_t> List) override {
  508. printListImpl(Label, List);
  509. }
  510. void printList(StringRef Label, const ArrayRef<int8_t> List) override {
  511. printListImpl(Label, List);
  512. }
  513. void printList(StringRef Label, const ArrayRef<APSInt> List) override {
  514. JOS.attributeArray(Label, [&]() {
  515. for (const APSInt &Item : List) {
  516. printAPSInt(Item);
  517. }
  518. });
  519. }
  520. void printString(StringRef Value) override { JOS.value(Value); }
  521. void printString(StringRef Label, StringRef Value) override {
  522. JOS.attribute(Label, Value);
  523. }
  524. void objectBegin() override {
  525. scopedBegin({Scope::Object, ScopeKind::NoAttribute});
  526. }
  527. void objectBegin(StringRef Label) override {
  528. scopedBegin(Label, Scope::Object);
  529. }
  530. void objectEnd() override { scopedEnd(); }
  531. void arrayBegin() override {
  532. scopedBegin({Scope::Array, ScopeKind::NoAttribute});
  533. }
  534. void arrayBegin(StringRef Label) override {
  535. scopedBegin(Label, Scope::Array);
  536. }
  537. void arrayEnd() override { scopedEnd(); }
  538. private:
  539. // Output HexNumbers as decimals so that they're easier to parse.
  540. uint64_t hexNumberToInt(HexNumber Hex) { return Hex.Value; }
  541. void printAPSInt(const APSInt &Value) {
  542. JOS.rawValueBegin() << Value;
  543. JOS.rawValueEnd();
  544. }
  545. void printFlagsImpl(StringRef Label, HexNumber Value,
  546. ArrayRef<FlagEntry> Flags) override {
  547. JOS.attributeObject(Label, [&]() {
  548. JOS.attribute("RawFlags", hexNumberToInt(Value));
  549. JOS.attributeArray("Flags", [&]() {
  550. for (const FlagEntry &Flag : Flags) {
  551. JOS.objectBegin();
  552. JOS.attribute("Name", Flag.Name);
  553. JOS.attribute("Value", Flag.Value);
  554. JOS.objectEnd();
  555. }
  556. });
  557. });
  558. }
  559. void printFlagsImpl(StringRef Label, HexNumber Value,
  560. ArrayRef<HexNumber> Flags) override {
  561. JOS.attributeObject(Label, [&]() {
  562. JOS.attribute("RawFlags", hexNumberToInt(Value));
  563. JOS.attributeArray("Flags", [&]() {
  564. for (const HexNumber &Flag : Flags) {
  565. JOS.value(Flag.Value);
  566. }
  567. });
  568. });
  569. }
  570. template <typename T> void printListImpl(StringRef Label, const T &List) {
  571. JOS.attributeArray(Label, [&]() {
  572. for (const auto &Item : List)
  573. JOS.value(Item);
  574. });
  575. }
  576. void printHexListImpl(StringRef Label,
  577. const ArrayRef<HexNumber> List) override {
  578. JOS.attributeArray(Label, [&]() {
  579. for (const HexNumber &Item : List) {
  580. JOS.value(hexNumberToInt(Item));
  581. }
  582. });
  583. }
  584. void printHexImpl(StringRef Label, HexNumber Value) override {
  585. JOS.attribute(Label, hexNumberToInt(Value));
  586. }
  587. void printHexImpl(StringRef Label, StringRef Str, HexNumber Value) override {
  588. JOS.attributeObject(Label, [&]() {
  589. JOS.attribute("Value", Str);
  590. JOS.attribute("RawValue", hexNumberToInt(Value));
  591. });
  592. }
  593. void printSymbolOffsetImpl(StringRef Label, StringRef Symbol,
  594. HexNumber Value) override {
  595. JOS.attributeObject(Label, [&]() {
  596. JOS.attribute("SymName", Symbol);
  597. JOS.attribute("Offset", hexNumberToInt(Value));
  598. });
  599. }
  600. void printNumberImpl(StringRef Label, StringRef Str,
  601. StringRef Value) override {
  602. JOS.attributeObject(Label, [&]() {
  603. JOS.attribute("Value", Str);
  604. JOS.attributeBegin("RawValue");
  605. JOS.rawValueBegin() << Value;
  606. JOS.rawValueEnd();
  607. JOS.attributeEnd();
  608. });
  609. }
  610. void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value,
  611. bool Block, uint32_t StartOffset = 0) override {
  612. JOS.attributeObject(Label, [&]() {
  613. if (!Str.empty())
  614. JOS.attribute("Value", Str);
  615. JOS.attribute("Offset", StartOffset);
  616. JOS.attributeArray("Bytes", [&]() {
  617. for (uint8_t Val : Value)
  618. JOS.value(Val);
  619. });
  620. });
  621. }
  622. void scopedBegin(ScopeContext ScopeCtx) {
  623. if (ScopeCtx.Context == Scope::Object)
  624. JOS.objectBegin();
  625. else if (ScopeCtx.Context == Scope::Array)
  626. JOS.arrayBegin();
  627. ScopeHistory.push_back(ScopeCtx);
  628. }
  629. void scopedBegin(StringRef Label, Scope Ctx) {
  630. ScopeKind Kind = ScopeKind::Attribute;
  631. if (ScopeHistory.empty() || ScopeHistory.back().Context != Scope::Object) {
  632. JOS.objectBegin();
  633. Kind = ScopeKind::NestedAttribute;
  634. }
  635. JOS.attributeBegin(Label);
  636. scopedBegin({Ctx, Kind});
  637. }
  638. void scopedEnd() {
  639. ScopeContext ScopeCtx = ScopeHistory.back();
  640. if (ScopeCtx.Context == Scope::Object)
  641. JOS.objectEnd();
  642. else if (ScopeCtx.Context == Scope::Array)
  643. JOS.arrayEnd();
  644. if (ScopeCtx.Kind == ScopeKind::Attribute ||
  645. ScopeCtx.Kind == ScopeKind::NestedAttribute)
  646. JOS.attributeEnd();
  647. if (ScopeCtx.Kind == ScopeKind::NestedAttribute)
  648. JOS.objectEnd();
  649. ScopeHistory.pop_back();
  650. }
  651. };
  652. struct DelimitedScope {
  653. DelimitedScope(ScopedPrinter &W) : W(&W) {}
  654. DelimitedScope() : W(nullptr) {}
  655. virtual ~DelimitedScope() = default;
  656. virtual void setPrinter(ScopedPrinter &W) = 0;
  657. ScopedPrinter *W;
  658. };
  659. struct DictScope : DelimitedScope {
  660. explicit DictScope() = default;
  661. explicit DictScope(ScopedPrinter &W) : DelimitedScope(W) { W.objectBegin(); }
  662. DictScope(ScopedPrinter &W, StringRef N) : DelimitedScope(W) {
  663. W.objectBegin(N);
  664. }
  665. void setPrinter(ScopedPrinter &W) override {
  666. this->W = &W;
  667. W.objectBegin();
  668. }
  669. ~DictScope() {
  670. if (W)
  671. W->objectEnd();
  672. }
  673. };
  674. struct ListScope : DelimitedScope {
  675. explicit ListScope() = default;
  676. explicit ListScope(ScopedPrinter &W) : DelimitedScope(W) { W.arrayBegin(); }
  677. ListScope(ScopedPrinter &W, StringRef N) : DelimitedScope(W) {
  678. W.arrayBegin(N);
  679. }
  680. void setPrinter(ScopedPrinter &W) override {
  681. this->W = &W;
  682. W.arrayBegin();
  683. }
  684. ~ListScope() {
  685. if (W)
  686. W->arrayEnd();
  687. }
  688. };
  689. } // namespace llvm
  690. #endif
  691. #ifdef __GNUC__
  692. #pragma GCC diagnostic pop
  693. #endif