• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
createInvocationFromCommandLine(ArrayRef<const char * > ArgList,IntrusiveRefCntPtr<DiagnosticsEngine> Diags)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     DiagnosticOptions DiagOpts;
38     Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgList.size(),
39                                                 ArgList.begin());
40   }
41 
42   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.
47   Args.push_back("-fsyntax-only");
48 
49   // FIXME: We shouldn't have to pass in the path info.
50   driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
51                            "a.out", false, *Diags);
52   // Force driver to use clang.
53   // FIXME: This seems like a hack. Maybe the "Clang" tool subclass should be
54   // available for using it to get the arguments, thus avoiding the overkill
55   // of using the driver.
56   TheDriver.setForcedClangUse();
57 
58   // Don't check that inputs exist, they may have been remapped.
59   TheDriver.setCheckInputsExist(false);
60 
61   OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
62 
63   // Just print the cc1 options if -### was present.
64   if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
65     C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
66     return 0;
67   }
68 
69   // We expect to get back exactly one command job, if we didn't something
70   // failed.
71   const driver::JobList &Jobs = C->getJobs();
72   if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
73     SmallString<256> Msg;
74     llvm::raw_svector_ostream OS(Msg);
75     C->PrintJob(OS, C->getJobs(), "; ", true);
76     Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
77     return 0;
78   }
79 
80   const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
81   if (StringRef(Cmd->getCreator().getName()) != "clang") {
82     Diags->Report(diag::err_fe_expected_clang_command);
83     return 0;
84   }
85 
86   const driver::ArgStringList &CCArgs = Cmd->getArguments();
87   OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
88   if (!CompilerInvocation::CreateFromArgs(*CI,
89                                      const_cast<const char **>(CCArgs.data()),
90                                      const_cast<const char **>(CCArgs.data()) +
91                                      CCArgs.size(),
92                                      *Diags))
93     return 0;
94   return CI.take();
95 }
96