CreateInvocationFromCommandLine.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/Frontend/CompilerInstance.h"
  15. #include "clang/Frontend/DiagnosticOptions.h"
  16. #include "clang/Frontend/FrontendDiagnostic.h"
  17. #include "clang/Driver/Compilation.h"
  18. #include "clang/Driver/Driver.h"
  19. #include "clang/Driver/ArgList.h"
  20. #include "clang/Driver/Options.h"
  21. #include "clang/Driver/Tool.h"
  22. #include "llvm/Support/Host.h"
  23. using namespace clang;
  24. /// createInvocationFromCommandLine - Construct a compiler invocation object for
  25. /// a command line argument vector.
  26. ///
  27. /// \return A CompilerInvocation, or 0 if none was built for the given
  28. /// argument vector.
  29. CompilerInvocation *
  30. clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
  31. llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
  32. if (!Diags.getPtr()) {
  33. // No diagnostics engine was provided, so create our own diagnostics object
  34. // with the default options.
  35. DiagnosticOptions DiagOpts;
  36. Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgList.size(),
  37. ArgList.begin());
  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. We
  43. // also want to force it to use clang.
  44. Args.push_back("-fsyntax-only");
  45. // FIXME: We shouldn't have to pass in the path info.
  46. driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
  47. "a.out", false, *Diags);
  48. // Don't check that inputs exist, they may have been remapped.
  49. TheDriver.setCheckInputsExist(false);
  50. OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
  51. // Just print the cc1 options if -### was present.
  52. if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
  53. C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
  54. return 0;
  55. }
  56. // We expect to get back exactly one command job, if we didn't something
  57. // failed.
  58. const driver::JobList &Jobs = C->getJobs();
  59. if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
  60. llvm::SmallString<256> Msg;
  61. llvm::raw_svector_ostream OS(Msg);
  62. C->PrintJob(OS, C->getJobs(), "; ", true);
  63. Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
  64. return 0;
  65. }
  66. const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
  67. if (StringRef(Cmd->getCreator().getName()) != "clang") {
  68. Diags->Report(diag::err_fe_expected_clang_command);
  69. return 0;
  70. }
  71. const driver::ArgStringList &CCArgs = Cmd->getArguments();
  72. OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
  73. if (!CompilerInvocation::CreateFromArgs(*CI,
  74. const_cast<const char **>(CCArgs.data()),
  75. const_cast<const char **>(CCArgs.data()) +
  76. CCArgs.size(),
  77. *Diags))
  78. return 0;
  79. return CI.take();
  80. }