CreateInvocationFromCommandLine.cpp 4.0 KB

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