blob: 653da75f2505511d26beb1806c58665576ab7563 [file] [log] [blame]
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +00001//===--- 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/Frontend/CompilerInstance.h"
16#include "clang/Frontend/DiagnosticOptions.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Driver/Compilation.h"
19#include "clang/Driver/Driver.h"
20#include "clang/Driver/ArgList.h"
21#include "clang/Driver/Options.h"
22#include "clang/Driver/Tool.h"
23#include "llvm/Support/Host.h"
24using namespace clang;
25
26/// createInvocationFromCommandLine - Construct a compiler invocation object for
27/// a command line argument vector.
28///
29/// \return A CompilerInvocation, or 0 if none was built for the given
30/// argument vector.
31CompilerInvocation *
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000032clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
David Blaikied6471f72011-09-25 23:23:43 +000033 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000034 if (!Diags.getPtr()) {
35 // No diagnostics engine was provided, so create our own diagnostics object
36 // with the default options.
37 DiagnosticOptions DiagOpts;
38 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgList.size(),
39 ArgList.begin());
40 }
41
Chris Lattner5f9e2722011-07-23 10:55:15 +000042 SmallVector<const char *, 16> Args;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000043 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
44 Args.insert(Args.end(), ArgList.begin(), ArgList.end());
45
46 // FIXME: Find a cleaner way to force the driver into restricted modes. We
47 // also want to force it to use clang.
48 Args.push_back("-fsyntax-only");
49
50 // FIXME: We shouldn't have to pass in the path info.
Sebastian Pop5d8b9542011-11-01 21:33:06 +000051 driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
Bob Wilson10a82cd2011-10-04 05:34:14 +000052 "a.out", false, *Diags);
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000053
54 // Don't check that inputs exist, they may have been remapped.
55 TheDriver.setCheckInputsExist(false);
56
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000057 OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000058
59 // Just print the cc1 options if -### was present.
60 if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
61 C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
62 return 0;
63 }
64
65 // We expect to get back exactly one command job, if we didn't something
66 // failed.
67 const driver::JobList &Jobs = C->getJobs();
Eli Friedman4d509342011-05-21 19:15:39 +000068 if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000069 SmallString<256> Msg;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000070 llvm::raw_svector_ostream OS(Msg);
71 C->PrintJob(OS, C->getJobs(), "; ", true);
72 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
73 return 0;
74 }
75
76 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
Chris Lattner5f9e2722011-07-23 10:55:15 +000077 if (StringRef(Cmd->getCreator().getName()) != "clang") {
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000078 Diags->Report(diag::err_fe_expected_clang_command);
79 return 0;
80 }
81
82 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000083 OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +000084 if (!CompilerInvocation::CreateFromArgs(*CI,
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000085 const_cast<const char **>(CCArgs.data()),
86 const_cast<const char **>(CCArgs.data()) +
87 CCArgs.size(),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +000088 *Diags))
89 return 0;
90 return CI.take();
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000091}