]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / lib / Frontend / CreateInvocationFromCommandLine.cpp
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/Basic/DiagnosticOptions.h"
16 #include "clang/Driver/ArgList.h"
17 #include "clang/Driver/Compilation.h"
18 #include "clang/Driver/Driver.h"
19 #include "clang/Driver/Options.h"
20 #include "clang/Driver/Tool.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/FrontendDiagnostic.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(ArrayRef<const char *> ArgList,
33                             IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
34   if (!Diags.getPtr()) {
35     // No diagnostics engine was provided, so create our own diagnostics object
36     // with the default options.
37     Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
38   }
39
40   SmallVector<const char *, 16> Args;
41   Args.push_back("<clang>"); // FIXME: Remove dummy argument.
42   Args.insert(Args.end(), ArgList.begin(), ArgList.end());
43
44   // FIXME: Find a cleaner way to force the driver into restricted modes.
45   Args.push_back("-fsyntax-only");
46
47   // FIXME: We shouldn't have to pass in the path info.
48   driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
49                            "a.out", *Diags);
50
51   // Don't check that inputs exist, they may have been remapped.
52   TheDriver.setCheckInputsExist(false);
53
54   OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
55
56   // Just print the cc1 options if -### was present.
57   if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
58     C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
59     return 0;
60   }
61
62   // We expect to get back exactly one command job, if we didn't something
63   // failed.
64   const driver::JobList &Jobs = C->getJobs();
65   if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
66     SmallString<256> Msg;
67     llvm::raw_svector_ostream OS(Msg);
68     C->PrintJob(OS, C->getJobs(), "; ", true);
69     Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
70     return 0;
71   }
72
73   const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
74   if (StringRef(Cmd->getCreator().getName()) != "clang") {
75     Diags->Report(diag::err_fe_expected_clang_command);
76     return 0;
77   }
78
79   const driver::ArgStringList &CCArgs = Cmd->getArguments();
80   OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
81   if (!CompilerInvocation::CreateFromArgs(*CI,
82                                      const_cast<const char **>(CCArgs.data()),
83                                      const_cast<const char **>(CCArgs.data()) +
84                                      CCArgs.size(),
85                                      *Diags))
86     return 0;
87   return CI.take();
88 }