Main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include "llvm/TableGen/Main.h"
  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/Record.h"
  25. #include <algorithm>
  26. #include <cstdio>
  27. #include <system_error>
  28. using namespace llvm;
  29. static cl::opt<std::string>
  30. OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
  31. cl::init("-"));
  32. static cl::opt<std::string>
  33. DependFilename("d",
  34. cl::desc("Dependency filename"),
  35. cl::value_desc("filename"),
  36. cl::init(""));
  37. static cl::opt<std::string>
  38. InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
  39. static cl::list<std::string>
  40. IncludeDirs("I", cl::desc("Directory of include files"),
  41. cl::value_desc("directory"), cl::Prefix);
  42. /// \brief Create a dependency file for `-d` option.
  43. ///
  44. /// This functionality is really only for the benefit of the build system.
  45. /// It is similar to GCC's `-M*` family of options.
  46. static int createDependencyFile(const TGParser &Parser, const char *argv0) {
  47. if (OutputFilename == "-") {
  48. errs() << argv0 << ": the option -d must be used together with -o\n";
  49. return 1;
  50. }
  51. std::error_code EC;
  52. tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text);
  53. if (EC) {
  54. errs() << argv0 << ": error opening " << DependFilename << ":"
  55. << EC.message() << "\n";
  56. return 1;
  57. }
  58. DepOut.os() << OutputFilename << ":";
  59. for (const auto &Dep : Parser.getDependencies()) {
  60. DepOut.os() << ' ' << Dep.first;
  61. }
  62. DepOut.os() << "\n";
  63. DepOut.keep();
  64. return 0;
  65. }
  66. int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
  67. RecordKeeper Records;
  68. // HLSL Change Starts
  69. struct SrcMgrCleanup {
  70. ~SrcMgrCleanup() {
  71. SrcMgr.Reset();
  72. }
  73. };
  74. SrcMgrCleanup SrcMgrCleanupInstance;
  75. // HLSL Change Ends
  76. // Parse the input file.
  77. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  78. MemoryBuffer::getFileOrSTDIN(InputFilename);
  79. if (std::error_code EC = FileOrErr.getError()) {
  80. errs() << "Could not open input file '" << InputFilename
  81. << "': " << EC.message() << "\n";
  82. return 1;
  83. }
  84. // Tell SrcMgr about this buffer, which is what TGParser will pick up.
  85. SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
  86. // Record the location of the include directory so that the lexer can find
  87. // it later.
  88. SrcMgr.setIncludeDirs(IncludeDirs);
  89. TGParser Parser(SrcMgr, Records);
  90. if (Parser.ParseFile())
  91. return 1;
  92. std::error_code EC;
  93. tool_output_file Out(OutputFilename, EC, sys::fs::F_Text);
  94. if (EC) {
  95. errs() << argv0 << ": error opening " << OutputFilename << ":"
  96. << EC.message() << "\n";
  97. return 1;
  98. }
  99. if (!DependFilename.empty()) {
  100. if (int Ret = createDependencyFile(Parser, argv0))
  101. return Ret;
  102. }
  103. if (MainFn(Out.os(), Records))
  104. return 1;
  105. if (ErrorsPrinted > 0) {
  106. errs() << argv0 << ": " << ErrorsPrinted << " errors.\n";
  107. return 1;
  108. }
  109. // Declare success.
  110. Out.keep();
  111. return 0;
  112. }