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" |
| 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" |
| 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. |
| 37 | DiagnosticOptions DiagOpts; |
| 38 | Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgList.size(), |
| 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 | |
| 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 Pop | 5d8b954 | 2011-11-01 21:33:06 +0000 | [diff] [blame] | 51 | driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(), |
Bob Wilson | 10a82cd | 2011-10-04 05:34:14 +0000 | [diff] [blame] | 52 | "a.out", false, *Diags); |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 53 | |
| 54 | // Don't check that inputs exist, they may have been remapped. |
| 55 | TheDriver.setCheckInputsExist(false); |
| 56 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 57 | OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args)); |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 58 | |
| 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 Friedman | 4d50934 | 2011-05-21 19:15:39 +0000 | [diff] [blame] | 68 | if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 69 | SmallString<256> Msg; |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 70 | 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 Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 77 | if (StringRef(Cmd->getCreator().getName()) != "clang") { |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 78 | Diags->Report(diag::err_fe_expected_clang_command); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 83 | OwningPtr<CompilerInvocation> CI(new CompilerInvocation()); |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 84 | if (!CompilerInvocation::CreateFromArgs(*CI, |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 85 | const_cast<const char **>(CCArgs.data()), |
| 86 | const_cast<const char **>(CCArgs.data()) + |
| 87 | CCArgs.size(), |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 88 | *Diags)) |
| 89 | return 0; |
| 90 | return CI.take(); |
Argyrios Kyrtzidis | ea383c0 | 2011-04-04 23:16:36 +0000 | [diff] [blame] | 91 | } |