CreateInvocationFromCommandLine.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. CompilerInvocation *
  32. clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
  33. IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
  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;
  40. Args.push_back("<clang>"); // FIXME: Remove dummy argument.
  41. Args.insert(Args.end(), ArgList.begin(), ArgList.end());
  42. // FIXME: Find a cleaner way to force the driver into restricted modes.
  43. Args.push_back("-fsyntax-only");
  44. // FIXME: We shouldn't have to pass in the path info.
  45. driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
  46. *Diags);
  47. // Don't check that inputs exist, they may have been remapped.
  48. TheDriver.setCheckInputsExist(false);
  49. std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
  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. CUDA compilation is an exception as it creates multiple jobs. If
  57. // that's the case, we proceed with the first job. If caller needs particular
  58. // CUDA job, it should be controlled via --cuda-{host|device}-only option
  59. // passed to the driver.
  60. const driver::JobList &Jobs = C->getJobs();
  61. bool CudaCompilation = false;
  62. if (Jobs.size() > 1) {
  63. for (auto &A : C->getActions())
  64. if (isa<driver::CudaDeviceAction>(A)) {
  65. CudaCompilation = true;
  66. break;
  67. }
  68. }
  69. if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
  70. (Jobs.size() > 1 && !CudaCompilation)) {
  71. SmallString<256> Msg;
  72. llvm::raw_svector_ostream OS(Msg);
  73. Jobs.Print(OS, "; ", true);
  74. Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
  75. return nullptr;
  76. }
  77. const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
  78. if (StringRef(Cmd.getCreator().getName()) != "clang") {
  79. Diags->Report(diag::err_fe_expected_clang_command);
  80. return nullptr;
  81. }
  82. const ArgStringList &CCArgs = Cmd.getArguments();
  83. std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
  84. if (!CompilerInvocation::CreateFromArgs(*CI,
  85. const_cast<const char **>(CCArgs.data()),
  86. const_cast<const char **>(CCArgs.data()) +
  87. CCArgs.size(),
  88. *Diags))
  89. return nullptr;
  90. return CI.release();
  91. }