clang  3.7.0
CreateInvocationFromCommandLine.cpp
Go to the documentation of this file.
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"
17 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/Action.h"
19 #include "clang/Driver/Options.h"
20 #include "clang/Driver/Tool.h"
23 #include "llvm/Option/ArgList.h"
24 #include "llvm/Support/Host.h"
25 using namespace clang;
26 using namespace llvm::opt;
27 
28 /// createInvocationFromCommandLine - Construct a compiler invocation object for
29 /// a command line argument vector.
30 ///
31 /// \return A CompilerInvocation, or 0 if none was built for the given
32 /// argument vector.
36  if (!Diags.get()) {
37  // No diagnostics engine was provided, so create our own diagnostics object
38  // with the default options.
40  }
41 
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  *Diags);
52 
53  // Don't check that inputs exist, they may have been remapped.
54  TheDriver.setCheckInputsExist(false);
55 
56  std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
57 
58  // Just print the cc1 options if -### was present.
59  if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
60  C->getJobs().Print(llvm::errs(), "\n", true);
61  return nullptr;
62  }
63 
64  // We expect to get back exactly one command job, if we didn't something
65  // failed. CUDA compilation is an exception as it creates multiple jobs. If
66  // that's the case, we proceed with the first job. If caller needs particular
67  // CUDA job, it should be controlled via --cuda-{host|device}-only option
68  // passed to the driver.
69  const driver::JobList &Jobs = C->getJobs();
70  bool CudaCompilation = false;
71  if (Jobs.size() > 1) {
72  for (auto &A : C->getActions()){
73  // On MacOSX real actions may end up being wrapped in BindArchAction
74  if (isa<driver::BindArchAction>(A))
75  A = *A->begin();
76  if (isa<driver::CudaDeviceAction>(A)) {
77  CudaCompilation = true;
78  break;
79  }
80  }
81  }
82  if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
83  (Jobs.size() > 1 && !CudaCompilation)) {
84  SmallString<256> Msg;
85  llvm::raw_svector_ostream OS(Msg);
86  Jobs.Print(OS, "; ", true);
87  Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
88  return nullptr;
89  }
90 
91  const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
92  if (StringRef(Cmd.getCreator().getName()) != "clang") {
93  Diags->Report(diag::err_fe_expected_clang_command);
94  return nullptr;
95  }
96 
97  const ArgStringList &CCArgs = Cmd.getArguments();
98  std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
100  const_cast<const char **>(CCArgs.data()),
101  const_cast<const char **>(CCArgs.data()) +
102  CCArgs.size(),
103  *Diags))
104  return nullptr;
105  return CI.release();
106 }
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
static bool CreateFromArgs(CompilerInvocation &Res, const char *const *ArgBegin, const char *const *ArgEnd, DiagnosticsEngine &Diags)
Create a compiler invocation from a list of input options.
Compilation * BuildCompilation(ArrayRef< const char * > Args)
Definition: Driver.cpp:363
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition: Job.h:95
JobList - A sequence of jobs to perform.
Definition: Job.h:133
void setCheckInputsExist(bool Value)
Definition: Driver.h:221
size_type size() const
Definition: Job.h:155
Options for controlling the compiler diagnostics engine.
iterator begin()
Definition: Job.h:156
const llvm::opt::ArgStringList & getArguments() const
Definition: Job.h:108
const char * getName() const
Definition: Tool.h:80
CompilerInvocation * createInvocationFromCommandLine(ArrayRef< const char * > Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >())
Helper class for holding the data necessary to invoke the compiler.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:294