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 definitions (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
27using namespace llvm;
28
29static cl::OptionCategory PrintEnumsCat("Options for -print-enums");
30static cl::opt<std::string> Class("class",
31 cl::desc("Print Enum list for this class"),
32 cl::value_desc("class name"),
33 cl::cat(PrintEnumsCat));
34
35static void printRecords(const RecordKeeper &Records, raw_ostream &OS) {
36 OS << Records; // No argument, dump all contents
37}
38
39static void printEnums(const RecordKeeper &Records, raw_ostream &OS) {
40 for (const Record *Rec : Records.getAllDerivedDefinitions(ClassName: Class))
41 OS << Rec->getName() << ", ";
42 OS << "\n";
43}
44
45static void printSets(const RecordKeeper &Records, raw_ostream &OS) {
46 SetTheory Sets;
47 Sets.addFieldExpander(ClassName: "Set", FieldName: "Elements");
48 for (const Record *Rec : Records.getAllDerivedDefinitions(ClassName: "Set")) {
49 OS << Rec->getName() << " = [";
50 const std::vector<const Record *> *Elts = Sets.expand(Set: Rec);
51 assert(Elts && "Couldn't expand Set instance");
52 for (const Record *Elt : *Elts)
53 OS << ' ' << Elt->getName();
54 OS << " ]\n";
55 }
56}
57
58static TableGen::Emitter::Opt X[] = {
59 {"print-records", printRecords, "Print all records to stdout (default)",
60 true},
61 {"print-detailed-records", EmitDetailedRecords,
62 "Print full details of all records to stdout"},
63 {"null-backend",
64 TableGen::Emitter::FnT(
65 [](const RecordKeeper &Records, raw_ostream &OS) {}),
66 "Do nothing after parsing (useful for timing)"},
67 {"dump-json", EmitJSON, "Dump all records as machine-readable JSON"},
68 {"print-enums", printEnums, "Print enum values for a class"},
69 {"print-sets", printSets, "Print expanded sets for testing DAG exprs"},
70};
71
72int tblgen_main(int argc, char **argv) {
73 InitLLVM X(argc, argv);
74 cl::ParseCommandLineOptions(argc, argv);
75
76 MultiFileTableGenMainFn MainFn = nullptr;
77 return TableGenMain(argv0: argv[0], MainFn);
78}
79
80#ifndef __has_feature
81#define __has_feature(x) 0
82#endif
83
84#if __has_feature(address_sanitizer) || \
85 (defined(__SANITIZE_ADDRESS__) && defined(__GNUC__)) || \
86 __has_feature(leak_sanitizer)
87
88#include <sanitizer/lsan_interface.h>
89// Disable LeakSanitizer for this binary as it has too many leaks that are not
90// very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
91LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }
92
93#endif
94