CreateInvocationFromCommandLine.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
  32. ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
  33. IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
  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(ArgList.begin(), ArgList.end());
  40. // FIXME: Find a cleaner way to force the driver into restricted modes.
  41. Args.push_back("-fsyntax-only");
  42. // FIXME: We shouldn't have to pass in the path info.
  43. driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(),
  44. *Diags, VFS);
  45. // Don't check that inputs exist, they may have been remapped.
  46. TheDriver.setCheckInputsExist(false);
  47. std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
  48. if (!C)
  49. return nullptr;
  50. // Just print the cc1 options if -### was present.
  51. if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
  52. C->getJobs().Print(llvm::errs(), "\n", true);
  53. return nullptr;
  54. }
  55. // We expect to get back exactly one command job, if we didn't something
  56. // failed. Offload compilation is an exception as it creates multiple jobs. If
  57. // that's the case, we proceed with the first job. If caller needs a
  58. // particular job, it should be controlled via options (e.g.
  59. // --cuda-{host|device}-only for CUDA) passed to the driver.
  60. const driver::JobList &Jobs = C->getJobs();
  61. bool OffloadCompilation = false;
  62. if (Jobs.size() > 1) {
  63. for (auto &A : C->getActions()){
  64. // On MacOSX real actions may end up being wrapped in BindArchAction
  65. if (isa<driver::BindArchAction>(A))
  66. A = *A->input_begin();
  67. if (isa<driver::OffloadAction>(A)) {
  68. OffloadCompilation = true;
  69. break;
  70. }
  71. }
  72. }
  73. if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
  74. (Jobs.size() > 1 && !OffloadCompilation)) {
  75. SmallString<256> Msg;
  76. llvm::raw_svector_ostream OS(Msg);
  77. Jobs.Print(OS, "; ", true);
  78. Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
  79. return nullptr;
  80. }
  81. const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
  82. if (StringRef(Cmd.getCreator().getName()) != "clang") {
  83. Diags->Report(diag::err_fe_expected_clang_command);
  84. return nullptr;
  85. }
  86. const ArgStringList &CCArgs = Cmd.getArguments();
  87. auto CI = llvm::make_unique<CompilerInvocation>();
  88. if (!CompilerInvocation::CreateFromArgs(*CI,
  89. const_cast<const char **>(CCArgs.data()),
  90. const_cast<const char **>(CCArgs.data()) +
  91. CCArgs.size(),
  92. *Diags))
  93. return nullptr;
  94. return CI;
  95. }