Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame^] | 15 | #include "clang/Basic/DiagnosticOptions.h" |
| 16 | #include "clang/Driver/ArgList.h" |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Compilation.h" |
| 18 | #include "clang/Driver/Driver.h" |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 19 | #include "clang/Driver/Options.h" |
| 20 | #include "clang/Driver/Tool.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame^] | 21 | #include "clang/Frontend/CompilerInstance.h" |
| 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Host.h" |
| 24 | using 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. |
| 31 | CompilerInvocation * |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 32 | clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList, |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 33 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags) { |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 34 | if (!Diags.getPtr()) { |
| 35 | // No diagnostics engine was provided, so create our own diagnostics object |
| 36 | // with the default options. |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 37 | Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 38 | ArgList.size(), |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 39 | ArgList.begin()); |
| 40 | } |
| 41 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 42 | SmallVector<const char *, 16> Args; |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 43 | Args.push_back("<clang>"); // FIXME: Remove dummy argument. |
| 44 | Args.insert(Args.end(), ArgList.begin(), ArgList.end()); |
| 45 | |
Argyrios Kyrtzidis | d6277fb | 2012-05-21 20:11:54 +0000 | [diff] [blame] | 46 | // FIXME: Find a cleaner way to force the driver into restricted modes. |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 47 | Args.push_back("-fsyntax-only"); |
| 48 | |
| 49 | // FIXME: We shouldn't have to pass in the path info. |
Sebastian Pop | 5d8b954 | 2011-11-01 21:33:06 +0000 | [diff] [blame] | 50 | driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(), |
Rafael Espindola | 17c874a | 2012-11-27 16:10:37 +0000 | [diff] [blame] | 51 | "a.out", *Diags); |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 52 | |
| 53 | // Don't check that inputs exist, they may have been remapped. |
| 54 | TheDriver.setCheckInputsExist(false); |
| 55 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 56 | OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args)); |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 57 | |
| 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 Friedman | 4d50934 | 2011-05-21 19:15:39 +0000 | [diff] [blame] | 67 | if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 68 | SmallString<256> Msg; |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 69 | 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 Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 76 | if (StringRef(Cmd->getCreator().getName()) != "clang") { |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 77 | Diags->Report(diag::err_fe_expected_clang_command); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 82 | OwningPtr<CompilerInvocation> CI(new CompilerInvocation()); |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 83 | if (!CompilerInvocation::CreateFromArgs(*CI, |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 84 | const_cast<const char **>(CCArgs.data()), |
| 85 | const_cast<const char **>(CCArgs.data()) + |
| 86 | CCArgs.size(), |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 87 | *Diags)) |
| 88 | return 0; |
| 89 | return CI.take(); |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 90 | } |