CreateInvocationFromCommandLine.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
  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. // Construct a compiler invocation object for command line driver arguments
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Frontend/Utils.h"
  14. #include "clang/Basic/DiagnosticOptions.h"
  15. #include "clang/Driver/Compilation.h"
  16. #include "clang/Driver/Driver.h"
  17. #include "clang/Driver/Action.h"
  18. #include "clang/Driver/Options.h"
  19. #include "clang/Driver/Tool.h"
  20. #include "clang/Frontend/CompilerInstance.h"
  21. #include "clang/Frontend/FrontendDiagnostic.h"
  22. #include "llvm/Option/ArgList.h"
  23. #include "llvm/Support/Host.h"
  24. using namespace clang;
  25. using namespace llvm::opt;
  26. /// createInvocationFromCommandLine - Construct a compiler invocation object for
  27. /// a command line argument vector.
  28. ///
  29. /// \return A CompilerInvocation, or 0 if none was built for the given
  30. /// argument vector.
  31. CompilerInvocation *
  32. clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
  33. IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
  34. if (!Diags.get()) {
  35. // No diagnostics engine was provided, so create our own diagnostics object
  36. // with the default options.
  37. Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
  38. }
  39. SmallVector<const char *, 16> Args;
  40. //Args.push_back("<clang>"); // FIXME: Remove dummy argument. // HLSL Change - parse directly as compilation
  41. Args.insert(Args.end(), ArgList.begin(), ArgList.end());
  42. // FIXME: Find a cleaner way to force the driver into restricted modes.
  43. Args.push_back("-fsyntax-only");
  44. #if 1 // HLSL Change Starts - no support for driver/toolchain support
  45. const SmallVector<const char *, 16> &CCArgs = Args;
  46. #else
  47. // FIXME: We shouldn't have to pass in the path info.
  48. driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
  49. *Diags);
  50. // Don't check that inputs exist, they may have been remapped.
  51. TheDriver.setCheckInputsExist(false);
  52. std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
  53. // Just print the cc1 options if -### was present.
  54. if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
  55. C->getJobs().Print(llvm::errs(), "\n", true);
  56. return nullptr;
  57. }
  58. // We expect to get back exactly one command job, if we didn't something
  59. // failed. CUDA compilation is an exception as it creates multiple jobs. If
  60. // that's the case, we proceed with the first job. If caller needs particular
  61. // CUDA job, it should be controlled via --cuda-{host|device}-only option
  62. // passed to the driver.
  63. const driver::JobList &Jobs = C->getJobs();
  64. bool CudaCompilation = false;
  65. if (Jobs.size() > 1) {
  66. for (auto &A : C->getActions()){
  67. // On MacOSX real actions may end up being wrapped in BindArchAction
  68. if (isa<driver::BindArchAction>(A))
  69. A = *A->begin();
  70. if (isa<driver::CudaDeviceAction>(A)) {
  71. CudaCompilation = true;
  72. break;
  73. }
  74. }
  75. }
  76. if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
  77. (Jobs.size() > 1 && !CudaCompilation)) {
  78. SmallString<256> Msg;
  79. llvm::raw_svector_ostream OS(Msg);
  80. Jobs.Print(OS, "; ", true);
  81. Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
  82. return nullptr;
  83. }
  84. const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
  85. if (StringRef(Cmd.getCreator().getName()) != "clang") {
  86. Diags->Report(diag::err_fe_expected_clang_command);
  87. return nullptr;
  88. }
  89. const ArgStringList &CCArgs = Cmd.getArguments();
  90. #endif // HLSL Change Ends - no support for driver/toolchain support
  91. std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
  92. if (!CompilerInvocation::CreateFromArgs(*CI,
  93. const_cast<const char **>(CCArgs.data()),
  94. const_cast<const char **>(CCArgs.data()) +
  95. CCArgs.size(),
  96. *Diags))
  97. return nullptr;
  98. return CI.release();
  99. }