blob: 1ce23f737e38022f642fee644ebc552c78233d19 [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"
16#include "clang/Driver/ArgList.h"
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Driver.h"
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000019#include "clang/Driver/Options.h"
20#include "clang/Driver/Tool.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Frontend/CompilerInstance.h"
22#include "clang/Frontend/FrontendDiagnostic.h"
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000023#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,
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000033 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.
Douglas Gregor02c23eb2012-10-23 22:26:28 +000037 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions,
38 ArgList.size(),
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000039 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
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +000046 // FIXME: Find a cleaner way to force the driver into restricted modes.
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000047 Args.push_back("-fsyntax-only");
48
49 // FIXME: We shouldn't have to pass in the path info.
Sebastian Pop5d8b9542011-11-01 21:33:06 +000050 driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
Rafael Espindola17c874a2012-11-27 16:10:37 +000051 "a.out", *Diags);
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000052
53 // Don't check that inputs exist, they may have been remapped.
54 TheDriver.setCheckInputsExist(false);
55
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000056 OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000057
58 // Just print the cc1 options if -### was present.
59 if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
60 C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
61 return 0;
62 }
63
64 // We expect to get back exactly one command job, if we didn't something
65 // failed.
66 const driver::JobList &Jobs = C->getJobs();
Eli Friedman4d509342011-05-21 19:15:39 +000067 if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000068 SmallString<256> Msg;
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000069 llvm::raw_svector_ostream OS(Msg);
70 C->PrintJob(OS, C->getJobs(), "; ", true);
71 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
72 return 0;
73 }
74
75 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
Chris Lattner5f9e2722011-07-23 10:55:15 +000076 if (StringRef(Cmd->getCreator().getName()) != "clang") {
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000077 Diags->Report(diag::err_fe_expected_clang_command);
78 return 0;
79 }
80
81 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000082 OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +000083 if (!CompilerInvocation::CreateFromArgs(*CI,
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000084 const_cast<const char **>(CCArgs.data()),
85 const_cast<const char **>(CCArgs.data()) +
86 CCArgs.size(),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +000087 *Diags))
88 return 0;
89 return CI.take();
Argyrios Kyrtzidisea383c02011-04-04 23:16:36 +000090}