clang  3.9.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 
42  SmallVector<const char *, 16> Args(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(Args[0], llvm::sys::getDefaultTargetTriple(),
49  *Diags);
50 
51  // Don't check that inputs exist, they may have been remapped.
52  TheDriver.setCheckInputsExist(false);
53 
54  std::unique_ptr<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->getJobs().Print(llvm::errs(), "\n", true);
59  return nullptr;
60  }
61 
62  // We expect to get back exactly one command job, if we didn't something
63  // failed. Offload compilation is an exception as it creates multiple jobs. If
64  // that's the case, we proceed with the first job. If caller needs a
65  // particular job, it should be controlled via options (e.g.
66  // --cuda-{host|device}-only for CUDA) passed to the driver.
67  const driver::JobList &Jobs = C->getJobs();
68  bool OffloadCompilation = false;
69  if (Jobs.size() > 1) {
70  for (auto &A : C->getActions()){
71  // On MacOSX real actions may end up being wrapped in BindArchAction
72  if (isa<driver::BindArchAction>(A))
73  A = *A->input_begin();
74  if (isa<driver::OffloadAction>(A)) {
75  OffloadCompilation = true;
76  break;
77  }
78  }
79  }
80  if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
81  (Jobs.size() > 1 && !OffloadCompilation)) {
82  SmallString<256> Msg;
83  llvm::raw_svector_ostream OS(Msg);
84  Jobs.Print(OS, "; ", true);
85  Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
86  return nullptr;
87  }
88 
89  const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
90  if (StringRef(Cmd.getCreator().getName()) != "clang") {
91  Diags->Report(diag::err_fe_expected_clang_command);
92  return nullptr;
93  }
94 
95  const ArgStringList &CCArgs = Cmd.getArguments();
96  std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
98  const_cast<const char **>(CCArgs.data()),
99  const_cast<const char **>(CCArgs.data()) +
100  CCArgs.size(),
101  *Diags))
102  return nullptr;
103  return CI.release();
104 }
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
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)
BuildCompilation - Construct a compilation object for a command line argument vector.
Definition: Driver.cpp:453
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition: Job.h:103
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:66
JobList - A sequence of jobs to perform.
Definition: Job.h:156
void setCheckInputsExist(bool Value)
Definition: Driver.h:248
size_type size() const
Definition: Job.h:178
Options for controlling the compiler diagnostics engine.
Command - An executable path/name and argument vector to execute.
Definition: Job.h:43
iterator begin()
Definition: Job.h:179
const llvm::opt::ArgStringList & getArguments() const
Definition: Job.h:116
const char * getName() const
Definition: Tool.h:80
CompilerInvocation * createInvocationFromCommandLine(ArrayRef< const char * > Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >())
createInvocationFromCommandLine - Construct a compiler invocation object for a command line argument ...
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:336