CreateInvocationFromCommandLine.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Construct a compiler invocation object for command line driver arguments
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/DiagnosticOptions.h"
  13. #include "clang/Driver/Action.h"
  14. #include "clang/Driver/Compilation.h"
  15. #include "clang/Driver/Driver.h"
  16. #include "clang/Driver/Options.h"
  17. #include "clang/Driver/Tool.h"
  18. #include "clang/Frontend/CompilerInstance.h"
  19. #include "clang/Frontend/FrontendDiagnostic.h"
  20. #include "clang/Frontend/Utils.h"
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/Option/ArgList.h"
  24. #include "llvm/Support/Host.h"
  25. using namespace clang;
  26. using namespace llvm::opt;
  27. std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
  28. ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
  29. IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool ShouldRecoverOnErorrs,
  30. std::vector<std::string> *CC1Args) {
  31. assert(!ArgList.empty());
  32. if (!Diags.get()) {
  33. // No diagnostics engine was provided, so create our own diagnostics object
  34. // with the default options.
  35. Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
  36. }
  37. SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
  38. // FIXME: Find a cleaner way to force the driver into restricted modes.
  39. Args.insert(
  40. llvm::find_if(
  41. Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
  42. "-fsyntax-only");
  43. // FIXME: We shouldn't have to pass in the path info.
  44. driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
  45. "clang LLVM compiler", VFS);
  46. // Don't check that inputs exist, they may have been remapped.
  47. TheDriver.setCheckInputsExist(false);
  48. std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
  49. if (!C)
  50. return nullptr;
  51. // Just print the cc1 options if -### was present.
  52. if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
  53. C->getJobs().Print(llvm::errs(), "\n", true);
  54. return nullptr;
  55. }
  56. // We expect to get back exactly one command job, if we didn't something
  57. // failed. Offload compilation is an exception as it creates multiple jobs. If
  58. // that's the case, we proceed with the first job. If caller needs a
  59. // particular job, it should be controlled via options (e.g.
  60. // --cuda-{host|device}-only for CUDA) passed to the driver.
  61. const driver::JobList &Jobs = C->getJobs();
  62. bool OffloadCompilation = false;
  63. if (Jobs.size() > 1) {
  64. for (auto &A : C->getActions()){
  65. // On MacOSX real actions may end up being wrapped in BindArchAction
  66. if (isa<driver::BindArchAction>(A))
  67. A = *A->input_begin();
  68. if (isa<driver::OffloadAction>(A)) {
  69. OffloadCompilation = true;
  70. break;
  71. }
  72. }
  73. }
  74. bool PickFirstOfMany = OffloadCompilation || ShouldRecoverOnErorrs;
  75. if (Jobs.size() == 0 || (Jobs.size() > 1 && !PickFirstOfMany)) {
  76. SmallString<256> Msg;
  77. llvm::raw_svector_ostream OS(Msg);
  78. Jobs.Print(OS, "; ", true);
  79. Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
  80. return nullptr;
  81. }
  82. auto Cmd = llvm::find_if(Jobs, [](const driver::Command &Cmd) {
  83. return StringRef(Cmd.getCreator().getName()) == "clang";
  84. });
  85. if (Cmd == Jobs.end()) {
  86. Diags->Report(diag::err_fe_expected_clang_command);
  87. return nullptr;
  88. }
  89. const ArgStringList &CCArgs = Cmd->getArguments();
  90. if (CC1Args)
  91. *CC1Args = {CCArgs.begin(), CCArgs.end()};
  92. auto CI = std::make_unique<CompilerInvocation>();
  93. if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
  94. !ShouldRecoverOnErorrs)
  95. return nullptr;
  96. return CI;
  97. }