clang  10.0.0git
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 std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
29  IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool ShouldRecoverOnErorrs,
30  std::vector<std::string> *CC1Args) {
31  if (!Diags.get()) {
32  // No diagnostics engine was provided, so create our own diagnostics object
33  // with the default options.
35  }
36 
37  SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
38 
39  // FIXME: Find a cleaner way to force the driver into restricted modes.
40  Args.push_back("-fsyntax-only");
41 
42  // FIXME: We shouldn't have to pass in the path info.
43  driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(),
44  *Diags, VFS);
45 
46  // Don't check that inputs exist, they may have been remapped.
47  TheDriver.setCheckInputsExist(false);
48 
49  std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
50  if (!C)
51  return nullptr;
52 
53  // Just print the cc1 options if -### was present.
54  if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
55  C->getJobs().Print(llvm::errs(), "\n", true);
56  return nullptr;
57  }
58 
59  // We expect to get back exactly one command job, if we didn't something
60  // failed. Offload compilation is an exception as it creates multiple jobs. If
61  // that's the case, we proceed with the first job. If caller needs a
62  // particular job, it should be controlled via options (e.g.
63  // --cuda-{host|device}-only for CUDA) passed to the driver.
64  const driver::JobList &Jobs = C->getJobs();
65  bool OffloadCompilation = false;
66  if (Jobs.size() > 1) {
67  for (auto &A : C->getActions()){
68  // On MacOSX real actions may end up being wrapped in BindArchAction
69  if (isa<driver::BindArchAction>(A))
70  A = *A->input_begin();
71  if (isa<driver::OffloadAction>(A)) {
72  OffloadCompilation = true;
73  break;
74  }
75  }
76  }
77  if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
78  (Jobs.size() > 1 && !OffloadCompilation)) {
79  SmallString<256> Msg;
80  llvm::raw_svector_ostream OS(Msg);
81  Jobs.Print(OS, "; ", true);
82  Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
83  return nullptr;
84  }
85 
86  const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
87  if (StringRef(Cmd.getCreator().getName()) != "clang") {
88  Diags->Report(diag::err_fe_expected_clang_command);
89  return nullptr;
90  }
91 
92  const ArgStringList &CCArgs = Cmd.getArguments();
93  if (CC1Args)
94  *CC1Args = {CCArgs.begin(), CCArgs.end()};
95  auto CI = std::make_unique<CompilerInvocation>();
96  if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags) &&
97  !ShouldRecoverOnErorrs)
98  return nullptr;
99  return CI;
100 }
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation&#39;s diagnostic options and replace any existing one ...
Compilation * BuildCompilation(ArrayRef< const char *> Args)
BuildCompilation - Construct a compilation object for a command line argument vector.
Definition: Driver.cpp:943
CompileCommand Cmd
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition: Job.h:110
JobList - A sequence of jobs to perform.
Definition: Job.h:191
void setCheckInputsExist(bool Value)
Definition: Driver.h:323
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:492
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:215
Dataflow Directional Tag Classes.
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char *> CommandLineArgs, DiagnosticsEngine &Diags)
Create a compiler invocation from a list of input options.
const llvm::opt::ArgStringList & getArguments() const
Definition: Job.h:129
size_type size() const
Definition: Job.h:214
std::unique_ptr< CompilerInvocation > createInvocationFromCommandLine(ArrayRef< const char *> Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr, bool ShouldRecoverOnErrors=false, std::vector< std::string > *CC1Args=nullptr)
createInvocationFromCommandLine - Construct a compiler invocation object for a command line argument ...
const char * getName() const
Definition: Tool.h:79