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