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 * |
| 32 | clang::createInvocationFromCommandLine(llvm::ArrayRef<const char *> ArgList, |
| 33 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags) { |
| 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 | |
| 42 | llvm::SmallVector<const char *, 16> Args; |
| 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. |
| 51 | driver::Driver TheDriver("clang", llvm::sys::getHostTriple(), |
| 52 | "a.out", false, false, *Diags); |
| 53 | |
| 54 | // Don't check that inputs exist, they may have been remapped. |
| 55 | TheDriver.setCheckInputsExist(false); |
| 56 | |
| 57 | llvm::OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args)); |
| 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(); |
| 68 | if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { |
| 69 | llvm::SmallString<256> Msg; |
| 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()); |
| 77 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
| 78 | Diags->Report(diag::err_fe_expected_clang_command); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
| 83 | CompilerInvocation *CI = new CompilerInvocation(); |
| 84 | CompilerInvocation::CreateFromArgs(*CI, |
| 85 | const_cast<const char **>(CCArgs.data()), |
| 86 | const_cast<const char **>(CCArgs.data()) + |
| 87 | CCArgs.size(), |
| 88 | *Diags); |
| 89 | return CI; |
| 90 | } |