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