CreateInvocationFromCommandLine.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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>
  28. clang::createInvocation(ArrayRef<const char *> ArgList,
  29. CreateInvocationOptions Opts) {
  30. assert(!ArgList.empty());
  31. auto Diags = Opts.Diags
  32. ? std::move(Opts.Diags)
  33. : CompilerInstance::createDiagnostics(new DiagnosticOptions);
  34. SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
  35. // FIXME: Find a cleaner way to force the driver into restricted modes.
  36. Args.insert(
  37. llvm::find_if(
  38. Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
  39. "-fsyntax-only");
  40. // FIXME: We shouldn't have to pass in the path info.
  41. driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
  42. "clang LLVM compiler", Opts.VFS);
  43. // Don't check that inputs exist, they may have been remapped.
  44. TheDriver.setCheckInputsExist(false);
  45. TheDriver.setProbePrecompiled(Opts.ProbePrecompiled);
  46. std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
  47. if (!C)
  48. return nullptr;
  49. if (C->getArgs().hasArg(driver::options::OPT_fdriver_only))
  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 || Opts.RecoverOnError;
  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 (Opts.CC1Args)
  91. *Opts.CC1Args = {CCArgs.begin(), CCArgs.end()};
  92. auto CI = std::make_unique<CompilerInvocation>();
  93. if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
  94. !Opts.RecoverOnError)
  95. return nullptr;
  96. return CI;
  97. }