1 //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Construct a compiler invocation object for command line driver arguments
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "clang/Driver/ArgList.h"
17 #include "clang/Driver/Compilation.h"
18 #include "clang/Driver/Driver.h"
19 #include "clang/Driver/Options.h"
20 #include "clang/Driver/Tool.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/FrontendDiagnostic.h"
23 #include "llvm/Support/Host.h"
24 using namespace clang;
26 /// createInvocationFromCommandLine - Construct a compiler invocation object for
27 /// a command line argument vector.
29 /// \return A CompilerInvocation, or 0 if none was built for the given
32 clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
33 IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
34 if (!Diags.getPtr()) {
35 // No diagnostics engine was provided, so create our own diagnostics object
36 // with the default options.
37 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
40 SmallVector<const char *, 16> Args;
41 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
42 Args.insert(Args.end(), ArgList.begin(), ArgList.end());
44 // FIXME: Find a cleaner way to force the driver into restricted modes.
45 Args.push_back("-fsyntax-only");
47 // FIXME: We shouldn't have to pass in the path info.
48 driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
51 // Don't check that inputs exist, they may have been remapped.
52 TheDriver.setCheckInputsExist(false);
54 OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
56 // Just print the cc1 options if -### was present.
57 if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
58 C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
62 // We expect to get back exactly one command job, if we didn't something
64 const driver::JobList &Jobs = C->getJobs();
65 if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
67 llvm::raw_svector_ostream OS(Msg);
68 C->PrintJob(OS, C->getJobs(), "; ", true);
69 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
73 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
74 if (StringRef(Cmd->getCreator().getName()) != "clang") {
75 Diags->Report(diag::err_fe_expected_clang_command);
79 const driver::ArgStringList &CCArgs = Cmd->getArguments();
80 OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
81 if (!CompilerInvocation::CreateFromArgs(*CI,
82 const_cast<const char **>(CCArgs.data()),
83 const_cast<const char **>(CCArgs.data()) +