CreateInvocationFromCommandLine.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/Basic/DiagnosticOptions.h"
  14. #include "clang/Frontend/Utils.h"
  15. #include "clang/Frontend/CompilerInstance.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. 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. Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions,
  36. 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.
  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. "a.out", false, *Diags);
  47. // Don't check that inputs exist, they may have been remapped.
  48. TheDriver.setCheckInputsExist(false);
  49. OwningPtr<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->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
  53. return 0;
  54. }
  55. // We expect to get back exactly one command job, if we didn't something
  56. // failed.
  57. const driver::JobList &Jobs = C->getJobs();
  58. if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
  59. SmallString<256> Msg;
  60. llvm::raw_svector_ostream OS(Msg);
  61. C->PrintJob(OS, C->getJobs(), "; ", true);
  62. Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
  63. return 0;
  64. }
  65. const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
  66. if (StringRef(Cmd->getCreator().getName()) != "clang") {
  67. Diags->Report(diag::err_fe_expected_clang_command);
  68. return 0;
  69. }
  70. const driver::ArgStringList &CCArgs = Cmd->getArguments();
  71. OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
  72. if (!CompilerInvocation::CreateFromArgs(*CI,
  73. const_cast<const char **>(CCArgs.data()),
  74. const_cast<const char **>(CCArgs.data()) +
  75. CCArgs.size(),
  76. *Diags))
  77. return 0;
  78. return CI.take();
  79. }