DetailedRecordsBackend.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //===- DetailedRecordBackend.cpp - Detailed Records Report -*- 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. // This Tablegen backend prints a report that includes all the global
  10. // variables, classes, and records in complete detail. It includes more
  11. // detail than the default TableGen printer backend.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/FormatVariadic.h"
  18. #include "llvm/Support/SMLoc.h"
  19. #include "llvm/Support/SourceMgr.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include "llvm/TableGen/Error.h"
  22. #include "llvm/TableGen/Record.h"
  23. #include <map>
  24. #include <memory>
  25. #include <string>
  26. #include <utility>
  27. #define DEBUG_TYPE "detailed-records-backend"
  28. #define NL "\n"
  29. using namespace llvm;
  30. namespace {
  31. class DetailedRecordsEmitter {
  32. private:
  33. RecordKeeper &Records;
  34. public:
  35. DetailedRecordsEmitter(RecordKeeper &RK) : Records(RK) {}
  36. void run(raw_ostream &OS);
  37. void printReportHeading(raw_ostream &OS);
  38. void printVariables(raw_ostream &OS);
  39. void printClasses(raw_ostream &OS);
  40. void printRecords(raw_ostream &OS);
  41. void printSectionHeading(StringRef Title, int Count, raw_ostream &OS);
  42. void printDefms(Record *Rec, raw_ostream &OS);
  43. void printTemplateArgs(Record *Rec, raw_ostream &OS);
  44. void printSuperclasses(Record *Rec, raw_ostream &OS);
  45. void printFields(Record *Rec, raw_ostream &OS);
  46. }; // emitter class
  47. } // anonymous namespace
  48. // Print the report.
  49. void DetailedRecordsEmitter::run(raw_ostream &OS) {
  50. printReportHeading(OS);
  51. printVariables(OS);
  52. printClasses(OS);
  53. printRecords(OS);
  54. }
  55. // Print the report heading, including the source file name.
  56. void DetailedRecordsEmitter::printReportHeading(raw_ostream &OS) {
  57. OS << formatv("DETAILED RECORDS for file {0}\n", Records.getInputFilename());
  58. }
  59. // Print the global variables.
  60. void DetailedRecordsEmitter::printVariables(raw_ostream &OS) {
  61. const auto GlobalList = Records.getGlobals();
  62. printSectionHeading("Global Variables", GlobalList.size(), OS);
  63. OS << NL;
  64. for (const auto &Var : GlobalList) {
  65. OS << Var.first << " = " << Var.second->getAsString() << NL;
  66. }
  67. }
  68. // Print the classes, including the template arguments, superclasses,
  69. // and fields.
  70. void DetailedRecordsEmitter::printClasses(raw_ostream &OS) {
  71. const auto &ClassList = Records.getClasses();
  72. printSectionHeading("Classes", ClassList.size(), OS);
  73. for (const auto &ClassPair : ClassList) {
  74. auto *const Class = ClassPair.second.get();
  75. OS << formatv("\n{0} |{1}|\n", Class->getNameInitAsString(),
  76. SrcMgr.getFormattedLocationNoOffset(Class->getLoc().front()));
  77. printTemplateArgs(Class, OS);
  78. printSuperclasses(Class, OS);
  79. printFields(Class, OS);
  80. }
  81. }
  82. // Print the records, including the defm sequences, supercasses,
  83. // and fields.
  84. void DetailedRecordsEmitter::printRecords(raw_ostream &OS) {
  85. const auto &RecordList = Records.getDefs();
  86. printSectionHeading("Records", RecordList.size(), OS);
  87. for (const auto &RecPair : RecordList) {
  88. auto *const Rec = RecPair.second.get();
  89. std::string Name = Rec->getNameInitAsString();
  90. OS << formatv("\n{0} |{1}|\n", Name.empty() ? "\"\"" : Name,
  91. SrcMgr.getFormattedLocationNoOffset(Rec->getLoc().front()));
  92. printDefms(Rec, OS);
  93. printSuperclasses(Rec, OS);
  94. printFields(Rec, OS);
  95. }
  96. }
  97. // Print a section heading with the name of the section and
  98. // the item count.
  99. void DetailedRecordsEmitter::printSectionHeading(StringRef Title, int Count,
  100. raw_ostream &OS) {
  101. OS << formatv("\n{0} {1} ({2}) {0}\n", "--------------------", Title, Count);
  102. }
  103. // Print the record's defm source locations, if any. Note that they
  104. // are stored in the reverse order of their invocation.
  105. void DetailedRecordsEmitter::printDefms(Record *Rec, raw_ostream &OS) {
  106. const auto &LocList = Rec->getLoc();
  107. if (LocList.size() < 2)
  108. return;
  109. OS << " Defm sequence:";
  110. for (unsigned I = LocList.size() - 1; I >= 1; --I) {
  111. OS << formatv(" |{0}|", SrcMgr.getFormattedLocationNoOffset(LocList[I]));
  112. }
  113. OS << NL;
  114. }
  115. // Print the template arguments of a class.
  116. void DetailedRecordsEmitter::printTemplateArgs(Record *Rec,
  117. raw_ostream &OS) {
  118. ArrayRef<Init *> Args = Rec->getTemplateArgs();
  119. if (Args.empty()) {
  120. OS << " Template args: (none)\n";
  121. return;
  122. }
  123. OS << " Template args:\n";
  124. for (const Init *ArgName : Args) {
  125. const RecordVal *Value = Rec->getValue(ArgName);
  126. assert(Value && "Template argument value not found.");
  127. OS << " ";
  128. Value->print(OS, false);
  129. OS << formatv(" |{0}|", SrcMgr.getFormattedLocationNoOffset(Value->getLoc()));
  130. OS << NL;
  131. }
  132. }
  133. // Print the superclasses of a class or record. Indirect superclasses
  134. // are enclosed in parentheses.
  135. void DetailedRecordsEmitter::printSuperclasses(Record *Rec, raw_ostream &OS) {
  136. ArrayRef<std::pair<Record *, SMRange>> Superclasses = Rec->getSuperClasses();
  137. if (Superclasses.empty()) {
  138. OS << " Superclasses: (none)\n";
  139. return;
  140. }
  141. OS << " Superclasses:";
  142. for (const auto &SuperclassPair : Superclasses) {
  143. auto *ClassRec = SuperclassPair.first;
  144. if (Rec->hasDirectSuperClass(ClassRec))
  145. OS << formatv(" {0}", ClassRec->getNameInitAsString());
  146. else
  147. OS << formatv(" ({0})", ClassRec->getNameInitAsString());
  148. }
  149. OS << NL;
  150. }
  151. // Print the fields of a class or record, including their source locations.
  152. void DetailedRecordsEmitter::printFields(Record *Rec, raw_ostream &OS) {
  153. const auto &ValueList = Rec->getValues();
  154. if (ValueList.empty()) {
  155. OS << " Fields: (none)\n";
  156. return;
  157. }
  158. OS << " Fields:\n";
  159. for (const RecordVal &Value : ValueList)
  160. if (!Rec->isTemplateArg(Value.getNameInit())) {
  161. OS << " ";
  162. Value.print(OS, false);
  163. OS << formatv(" |{0}|\n",
  164. SrcMgr.getFormattedLocationNoOffset(Value.getLoc()));
  165. }
  166. }
  167. namespace llvm {
  168. // This function is called by TableGen after parsing the files.
  169. void EmitDetailedRecords(RecordKeeper &RK, raw_ostream &OS) {
  170. // Instantiate the emitter class and invoke run().
  171. DetailedRecordsEmitter(RK).run(OS);
  172. }
  173. } // namespace llvm