CreateInvocationFromCommandLine.cpp 3.6 KB

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