1 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // TableGen is a tool which can be used to build up a description of something,
11 // then invoke one or more "tablegen backends" to emit information about the
12 // description in some predefined format. In practice, this is used by the LLVM
13 // code generators to automate generation of a code generator through a
14 // high-level description of the target.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/TableGen/Main.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/ToolOutputFile.h"
25 #include "llvm/TableGen/Error.h"
26 #include "llvm/TableGen/Record.h"
29 #include <system_error>
32 static cl::opt<std::string>
33 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
36 static cl::opt<std::string>
38 cl::desc("Dependency filename"),
39 cl::value_desc("filename"),
42 static cl::opt<std::string>
43 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
45 static cl::list<std::string>
46 IncludeDirs("I", cl::desc("Directory of include files"),
47 cl::value_desc("directory"), cl::Prefix);
49 static int reportError(const char *ProgName, Twine Msg) {
50 errs() << ProgName << ": " << Msg;
55 /// \brief Create a dependency file for `-d` option.
57 /// This functionality is really only for the benefit of the build system.
58 /// It is similar to GCC's `-M*` family of options.
59 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
60 if (OutputFilename == "-")
61 return reportError(argv0, "the option -d must be used together with -o\n");
64 ToolOutputFile DepOut(DependFilename, EC, sys::fs::F_Text);
66 return reportError(argv0, "error opening " + DependFilename + ":" +
68 DepOut.os() << OutputFilename << ":";
69 for (const auto &Dep : Parser.getDependencies()) {
70 DepOut.os() << ' ' << Dep.first;
77 int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
80 // Parse the input file.
81 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
82 MemoryBuffer::getFileOrSTDIN(InputFilename);
83 if (std::error_code EC = FileOrErr.getError())
84 return reportError(argv0, "Could not open input file '" + InputFilename +
85 "': " + EC.message() + "\n");
87 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
88 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
90 // Record the location of the include directory so that the lexer can find
92 SrcMgr.setIncludeDirs(IncludeDirs);
94 TGParser Parser(SrcMgr, Records);
96 if (Parser.ParseFile())
100 ToolOutputFile Out(OutputFilename, EC, sys::fs::F_Text);
102 return reportError(argv0, "error opening " + OutputFilename + ":" +
103 EC.message() + "\n");
104 if (!DependFilename.empty()) {
105 if (int Ret = createDependencyFile(Parser, argv0))
109 if (MainFn(Out.os(), Records))
112 if (ErrorsPrinted > 0)
113 return reportError(argv0, utostr(ErrorsPrinted) + " errors.\n");