1 //===- TableGen.cpp - Top-Level TableGen implementation for LLVM ----------===//
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 file contains the main function for LLVM's TableGen.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/InitLLVM.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/TableGen/Main.h"
18 #include "llvm/TableGen/Record.h"
19 #include "llvm/TableGen/SetTheory.h"
20 #include "llvm/TableGen/TableGenBackend.h"
21 #include <cassert>
22 #include <string>
23 #include <vector>
24 
25 using namespace llvm;
26 
27 namespace llvm {
28 cl::opt<bool> EmitLongStrLiterals(
29     "long-string-literals",
30     cl::desc("when emitting large string tables, prefer string literals over "
31              "comma-separated char literals. This can be a readability and "
32              "compile-time performance win, but upsets some compilers"),
33     cl::Hidden, cl::init(true));
34 } // end namespace llvm
35 
36 namespace {
37 
38 cl::OptionCategory PrintEnumsCat("Options for -print-enums");
39 cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"),
40                            cl::value_desc("class name"),
41                            cl::cat(PrintEnumsCat));
42 
43 void PrintRecords(RecordKeeper &Records, raw_ostream &OS) {
44   OS << Records; // No argument, dump all contents
45 }
46 
47 void PrintEnums(RecordKeeper &Records, raw_ostream &OS) {
48   for (Record *Rec : Records.getAllDerivedDefinitions(Class))
49     OS << Rec->getName() << ", ";
50   OS << "\n";
51 }
52 
53 void PrintSets(RecordKeeper &Records, raw_ostream &OS) {
54   SetTheory Sets;
55   Sets.addFieldExpander("Set", "Elements");
56   for (Record *Rec : Records.getAllDerivedDefinitions("Set")) {
57     OS << Rec->getName() << " = [";
58     const std::vector<Record *> *Elts = Sets.expand(Rec);
59     assert(Elts && "Couldn't expand Set instance");
60     for (Record *Elt : *Elts)
61       OS << ' ' << Elt->getName();
62     OS << " ]\n";
63   }
64 }
65 
66 TableGen::Emitter::Opt X[] = {
67     {"print-records", PrintRecords, "Print all records to stdout (default)",
68      true},
69     {"print-detailed-records", EmitDetailedRecords,
70      "Print full details of all records to stdout"},
71     {"null-backend", [](RecordKeeper &Records, raw_ostream &OS) {},
72      "Do nothing after parsing (useful for timing)"},
73     {"dump-json", EmitJSON, "Dump all records as machine-readable JSON"},
74     {"print-enums", PrintEnums, "Print enum values for a class"},
75     {"print-sets", PrintSets, "Print expanded sets for testing DAG exprs"},
76 };
77 
78 } // namespace
79 
80 int main(int argc, char **argv) {
81   InitLLVM X(argc, argv);
82   cl::ParseCommandLineOptions(argc, argv);
83 
84   return TableGenMain(argv[0]);
85 }
86 
87 #ifndef __has_feature
88 #define __has_feature(x) 0
89 #endif
90 
91 #if __has_feature(address_sanitizer) ||                                        \
92     (defined(__SANITIZE_ADDRESS__) && defined(__GNUC__)) ||                    \
93     __has_feature(leak_sanitizer)
94 
95 #include <sanitizer/lsan_interface.h>
96 // Disable LeakSanitizer for this binary as it has too many leaks that are not
97 // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
98 LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }
99 
100 #endif
101