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