clang  9.0.0svn
CreateInvocationFromCommandLine.cpp
Go to the documentation of this file.
1 //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Construct a compiler invocation object for command line driver arguments
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Frontend/Utils.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/Action.h"
18 #include "clang/Driver/Options.h"
19 #include "clang/Driver/Tool.h"
22 #include "llvm/Option/ArgList.h"
23 #include "llvm/Support/Host.h"
24 using namespace clang;
25 using namespace llvm::opt;
26 
27 /// createInvocationFromCommandLine - Construct a compiler invocation object for
28 /// a command line argument vector.
29 ///
30 /// \return A CompilerInvocation, or 0 if none was built for the given
31 /// argument vector.
32 std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
35  if (!Diags.get()) {
36  // No diagnostics engine was provided, so create our own diagnostics object
37  // with the default options.
39  }
40 
41  SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
42 
43  // FIXME: Find a cleaner way to force the driver into restricted modes.
44  Args.push_back("-fsyntax-only");
45 
46  // FIXME: We shouldn't have to pass in the path info.
47  driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(),
48  *Diags, VFS);
49 
50  // Don't check that inputs exist, they may have been remapped.
51  TheDriver.setCheckInputsExist(false);
52 
53  std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
54  if (!C)
55  return nullptr;
56 
57  // Just print the cc1 options if -### was present.
58  if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
59  C->getJobs().Print(llvm::errs(), "\n", true);
60  return nullptr;
61  }
62 
63  // We expect to get back exactly one command job, if we didn't something
64  // failed. Offload compilation is an exception as it creates multiple jobs. If
65  // that's the case, we proceed with the first job. If caller needs a
66  // particular job, it should be controlled via options (e.g.
67  // --cuda-{host|device}-only for CUDA) passed to the driver.
68  const driver::JobList &Jobs = C->getJobs();
69  bool OffloadCompilation = false;
70  if (Jobs.size() > 1) {
71  for (auto &A : C->getActions()){
72  // On MacOSX real actions may end up being wrapped in BindArchAction
73  if (isa<driver::BindArchAction>(A))
74  A = *A->input_begin();
75  if (isa<driver::OffloadAction>(A)) {
76  OffloadCompilation = true;
77  break;
78  }
79  }
80  }
81  if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
82  (Jobs.size() > 1 && !OffloadCompilation)) {
83  SmallString<256> Msg;
84  llvm::raw_svector_ostream OS(Msg);
85  Jobs.Print(OS, "; ", true);
86  Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
87  return nullptr;
88  }
89 
90  const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
91  if (StringRef(Cmd.getCreator().getName()) != "clang") {
92  Diags->Report(diag::err_fe_expected_clang_command);
93  return nullptr;
94  }
95 
96  const ArgStringList &CCArgs = Cmd.getArguments();
97  auto CI = llvm::make_unique<CompilerInvocation>();
99  const_cast<const char **>(CCArgs.data()),
100  const_cast<const char **>(CCArgs.data()) +
101  CCArgs.size(),
102  *Diags))
103  return nullptr;
104  return CI;
105 }
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation&#39;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:932
std::unique_ptr< CompilerInvocation > createInvocationFromCommandLine(ArrayRef< const char *> Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
createInvocationFromCommandLine - Construct a compiler invocation object for a command line argument ...
CompileCommand Cmd
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:57
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition: Job.h:107
JobList - A sequence of jobs to perform.
Definition: Job.h:171
void setCheckInputsExist(bool Value)
Definition: Driver.h:303
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:435
Options for controlling the compiler diagnostics engine.
Command - An executable path/name and argument vector to execute.
Definition: Job.h:41
iterator begin()
Definition: Job.h:195
Dataflow Directional Tag Classes.
const llvm::opt::ArgStringList & getArguments() const
Definition: Job.h:126
size_type size() const
Definition: Job.h:194
const char * getName() const
Definition: Tool.h:79