xref: /minix3/external/bsd/llvm/dist/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc //
10*f4a2713aSLionel Sambuc // Construct a compiler invocation object for command line driver arguments
11*f4a2713aSLionel Sambuc //
12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc #include "clang/Frontend/Utils.h"
15*f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
16*f4a2713aSLionel Sambuc #include "clang/Driver/Compilation.h"
17*f4a2713aSLionel Sambuc #include "clang/Driver/Driver.h"
18*f4a2713aSLionel Sambuc #include "clang/Driver/Options.h"
19*f4a2713aSLionel Sambuc #include "clang/Driver/Tool.h"
20*f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
21*f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendDiagnostic.h"
22*f4a2713aSLionel Sambuc #include "llvm/Option/ArgList.h"
23*f4a2713aSLionel Sambuc #include "llvm/Support/Host.h"
24*f4a2713aSLionel Sambuc using namespace clang;
25*f4a2713aSLionel Sambuc using namespace llvm::opt;
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc /// createInvocationFromCommandLine - Construct a compiler invocation object for
28*f4a2713aSLionel Sambuc /// a command line argument vector.
29*f4a2713aSLionel Sambuc ///
30*f4a2713aSLionel Sambuc /// \return A CompilerInvocation, or 0 if none was built for the given
31*f4a2713aSLionel Sambuc /// argument vector.
32*f4a2713aSLionel Sambuc CompilerInvocation *
33*f4a2713aSLionel Sambuc clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
34*f4a2713aSLionel Sambuc                             IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
35*f4a2713aSLionel Sambuc   if (!Diags.getPtr()) {
36*f4a2713aSLionel Sambuc     // No diagnostics engine was provided, so create our own diagnostics object
37*f4a2713aSLionel Sambuc     // with the default options.
38*f4a2713aSLionel Sambuc     Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
39*f4a2713aSLionel Sambuc   }
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc   SmallVector<const char *, 16> Args;
42*f4a2713aSLionel Sambuc   Args.push_back("<clang>"); // FIXME: Remove dummy argument.
43*f4a2713aSLionel Sambuc   Args.insert(Args.end(), ArgList.begin(), ArgList.end());
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc   // FIXME: Find a cleaner way to force the driver into restricted modes.
46*f4a2713aSLionel Sambuc   Args.push_back("-fsyntax-only");
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc   // FIXME: We shouldn't have to pass in the path info.
49*f4a2713aSLionel Sambuc   driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
50*f4a2713aSLionel Sambuc                            "a.out", *Diags);
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   // Don't check that inputs exist, they may have been remapped.
53*f4a2713aSLionel Sambuc   TheDriver.setCheckInputsExist(false);
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc   OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   // Just print the cc1 options if -### was present.
58*f4a2713aSLionel Sambuc   if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
59*f4a2713aSLionel Sambuc     C->getJobs().Print(llvm::errs(), "\n", true);
60*f4a2713aSLionel Sambuc     return 0;
61*f4a2713aSLionel Sambuc   }
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   // We expect to get back exactly one command job, if we didn't something
64*f4a2713aSLionel Sambuc   // failed.
65*f4a2713aSLionel Sambuc   const driver::JobList &Jobs = C->getJobs();
66*f4a2713aSLionel Sambuc   if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
67*f4a2713aSLionel Sambuc     SmallString<256> Msg;
68*f4a2713aSLionel Sambuc     llvm::raw_svector_ostream OS(Msg);
69*f4a2713aSLionel Sambuc     Jobs.Print(OS, "; ", true);
70*f4a2713aSLionel Sambuc     Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
71*f4a2713aSLionel Sambuc     return 0;
72*f4a2713aSLionel Sambuc   }
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc   const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
75*f4a2713aSLionel Sambuc   if (StringRef(Cmd->getCreator().getName()) != "clang") {
76*f4a2713aSLionel Sambuc     Diags->Report(diag::err_fe_expected_clang_command);
77*f4a2713aSLionel Sambuc     return 0;
78*f4a2713aSLionel Sambuc   }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc   const ArgStringList &CCArgs = Cmd->getArguments();
81*f4a2713aSLionel Sambuc   OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
82*f4a2713aSLionel Sambuc   if (!CompilerInvocation::CreateFromArgs(*CI,
83*f4a2713aSLionel Sambuc                                      const_cast<const char **>(CCArgs.data()),
84*f4a2713aSLionel Sambuc                                      const_cast<const char **>(CCArgs.data()) +
85*f4a2713aSLionel Sambuc                                      CCArgs.size(),
86*f4a2713aSLionel Sambuc                                      *Diags))
87*f4a2713aSLionel Sambuc     return 0;
88*f4a2713aSLionel Sambuc   return CI.take();
89*f4a2713aSLionel Sambuc }
90