blob: 4a8a8a029e796fccce3c291fd6b983d968f2e926 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/Basic/DiagnosticOptions.h"
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000016#include "clang/Driver/Compilation.h"
17#include "clang/Driver/Driver.h"
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000018#include "clang/Driver/Options.h"
19#include "clang/Driver/Tool.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Frontend/CompilerInstance.h"
21#include "clang/Frontend/FrontendDiagnostic.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000022#include "llvm/Option/ArgList.h"
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000023#include "llvm/Support/Host.h"
24using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000025using namespace llvm::opt;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000026
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 *
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000033clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000034 IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -070035 if (!Diags.get()) {
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000036 // No diagnostics engine was provided, so create our own diagnostics object
37 // with the default options.
Sean Silvad47afb92013-01-20 01:58:28 +000038 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000039 }
40
Chris Lattner5f9e2722011-07-23 10:55:15 +000041 SmallVector<const char *, 16> Args;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000042 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
43 Args.insert(Args.end(), ArgList.begin(), ArgList.end());
44
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +000045 // FIXME: Find a cleaner way to force the driver into restricted modes.
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000046 Args.push_back("-fsyntax-only");
47
48 // FIXME: We shouldn't have to pass in the path info.
Sebastian Pop5d8b9542011-11-01 21:33:06 +000049 driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070050 *Diags);
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000051
52 // Don't check that inputs exist, they may have been remapped.
53 TheDriver.setCheckInputsExist(false);
54
Stephen Hines651f13c2014-04-23 16:59:28 -070055 std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000056
57 // Just print the cc1 options if -### was present.
58 if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
Hans Wennborgfc338972013-09-12 18:23:34 +000059 C->getJobs().Print(llvm::errs(), "\n", true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -070060 return nullptr;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000061 }
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();
Eli Friedman4d509342011-05-21 19:15:39 +000066 if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000067 SmallString<256> Msg;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000068 llvm::raw_svector_ostream OS(Msg);
Hans Wennborgfc338972013-09-12 18:23:34 +000069 Jobs.Print(OS, "; ", true);
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000070 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
Stephen Hines6bcf27b2014-05-29 04:14:42 -070071 return nullptr;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000072 }
73
Stephen Hines176edba2014-12-01 14:53:08 -080074 const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
75 if (StringRef(Cmd.getCreator().getName()) != "clang") {
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000076 Diags->Report(diag::err_fe_expected_clang_command);
Stephen Hines6bcf27b2014-05-29 04:14:42 -070077 return nullptr;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000078 }
79
Stephen Hines176edba2014-12-01 14:53:08 -080080 const ArgStringList &CCArgs = Cmd.getArguments();
Stephen Hines651f13c2014-04-23 16:59:28 -070081 std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +000082 if (!CompilerInvocation::CreateFromArgs(*CI,
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000083 const_cast<const char **>(CCArgs.data()),
84 const_cast<const char **>(CCArgs.data()) +
85 CCArgs.size(),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +000086 *Diags))
Stephen Hines6bcf27b2014-05-29 04:14:42 -070087 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -070088 return CI.release();
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000089}