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