Replace OwningPtr<T> with std::unique_ptr<T>.
[oota-llvm.git] / lib / TableGen / Main.cpp
1 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "TGParser.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/ToolOutputFile.h"
22 #include "llvm/Support/system_error.h"
23 #include "llvm/TableGen/Error.h"
24 #include "llvm/TableGen/Main.h"
25 #include "llvm/TableGen/Record.h"
26 #include <algorithm>
27 #include <cstdio>
28 using namespace llvm;
29
30 namespace {
31   cl::opt<std::string>
32   OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
33                  cl::init("-"));
34
35   cl::opt<std::string>
36   DependFilename("d",
37                  cl::desc("Dependency filename"),
38                  cl::value_desc("filename"),
39                  cl::init(""));
40
41   cl::opt<std::string>
42   InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
43
44   cl::list<std::string>
45   IncludeDirs("I", cl::desc("Directory of include files"),
46               cl::value_desc("directory"), cl::Prefix);
47 }
48
49 /// \brief Create a dependency file for `-d` option.
50 ///
51 /// This functionality is really only for the benefit of the build system.
52 /// It is similar to GCC's `-M*` family of options.
53 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
54   if (OutputFilename == "-") {
55     errs() << argv0 << ": the option -d must be used together with -o\n";
56     return 1;
57   }
58   std::string Error;
59   tool_output_file DepOut(DependFilename.c_str(), Error, sys::fs::F_Text);
60   if (!Error.empty()) {
61     errs() << argv0 << ": error opening " << DependFilename
62       << ":" << Error << "\n";
63     return 1;
64   }
65   DepOut.os() << OutputFilename << ":";
66   const TGLexer::DependenciesMapTy &Dependencies = Parser.getDependencies();
67   for (TGLexer::DependenciesMapTy::const_iterator I = Dependencies.begin(),
68                                                   E = Dependencies.end();
69        I != E; ++I) {
70     DepOut.os() << " " << I->first;
71   }
72   DepOut.os() << "\n";
73   DepOut.keep();
74   return 0;
75 }
76
77 namespace llvm {
78
79 int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
80   RecordKeeper Records;
81
82   // Parse the input file.
83   std::unique_ptr<MemoryBuffer> File;
84   if (error_code ec =
85         MemoryBuffer::getFileOrSTDIN(InputFilename, File)) {
86     errs() << "Could not open input file '" << InputFilename << "': "
87            << ec.message() <<"\n";
88     return 1;
89   }
90   MemoryBuffer *F = File.release();
91
92   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
93   SrcMgr.AddNewSourceBuffer(F, SMLoc());
94
95   // Record the location of the include directory so that the lexer can find
96   // it later.
97   SrcMgr.setIncludeDirs(IncludeDirs);
98
99   TGParser Parser(SrcMgr, Records);
100
101   if (Parser.ParseFile())
102     return 1;
103
104   std::string Error;
105   tool_output_file Out(OutputFilename.c_str(), Error, sys::fs::F_Text);
106   if (!Error.empty()) {
107     errs() << argv0 << ": error opening " << OutputFilename
108       << ":" << Error << "\n";
109     return 1;
110   }
111   if (!DependFilename.empty()) {
112     if (int Ret = createDependencyFile(Parser, argv0))
113       return Ret;
114   }
115
116   if (MainFn(Out.os(), Records))
117     return 1;
118
119   if (ErrorsPrinted > 0) {
120     errs() << argv0 << ": " << ErrorsPrinted << " errors.\n";
121     return 1;
122   }
123
124   // Declare success.
125   Out.keep();
126   return 0;
127 }
128
129 }