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