CreateInvocationFromCommandLine.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/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. CompilerInvocation *
  31. clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
  32. IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
  33. if (!Diags.getPtr()) {
  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;
  39. Args.push_back("<clang>"); // FIXME: Remove dummy argument.
  40. Args.insert(Args.end(), ArgList.begin(), ArgList.end());
  41. // FIXME: Find a cleaner way to force the driver into restricted modes.
  42. Args.push_back("-fsyntax-only");
  43. // FIXME: We shouldn't have to pass in the path info.
  44. driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
  45. *Diags);
  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. // 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.
  56. const driver::JobList &Jobs = C->getJobs();
  57. if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
  58. SmallString<256> Msg;
  59. llvm::raw_svector_ostream OS(Msg);
  60. Jobs.Print(OS, "; ", true);
  61. Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
  62. return nullptr;
  63. }
  64. const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
  65. if (StringRef(Cmd->getCreator().getName()) != "clang") {
  66. Diags->Report(diag::err_fe_expected_clang_command);
  67. return nullptr;
  68. }
  69. const ArgStringList &CCArgs = Cmd->getArguments();
  70. std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
  71. if (!CompilerInvocation::CreateFromArgs(*CI,
  72. const_cast<const char **>(CCArgs.data()),
  73. const_cast<const char **>(CCArgs.data()) +
  74. CCArgs.size(),
  75. *Diags))
  76. return nullptr;
  77. return CI.release();
  78. }