clang  15.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 
14 #include "clang/Driver/Action.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/Options.h"
18 #include "clang/Driver/Tool.h"
21 #include "clang/Frontend/Utils.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Option/ArgList.h"
25 #include "llvm/Support/Host.h"
26 using namespace clang;
27 using namespace llvm::opt;
28 
29 std::unique_ptr<CompilerInvocation>
32  assert(!ArgList.empty());
33  auto Diags = Opts.Diags
34  ? std::move(Opts.Diags)
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.insert(
41  llvm::find_if(
42  Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
43  "-fsyntax-only");
44 
45  // FIXME: We shouldn't have to pass in the path info.
46  driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
47  "clang LLVM compiler", Opts.VFS);
48 
49  // Don't check that inputs exist, they may have been remapped.
50  TheDriver.setCheckInputsExist(false);
51  TheDriver.setProbePrecompiled(Opts.ProbePrecompiled);
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 
82  bool PickFirstOfMany = OffloadCompilation || Opts.RecoverOnError;
83  if (Jobs.size() == 0 || (Jobs.size() > 1 && !PickFirstOfMany)) {
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  auto Cmd = llvm::find_if(Jobs, [](const driver::Command &Cmd) {
91  return StringRef(Cmd.getCreator().getName()) == "clang";
92  });
93  if (Cmd == Jobs.end()) {
94  Diags->Report(diag::err_fe_expected_clang_command);
95  return nullptr;
96  }
97 
98  const ArgStringList &CCArgs = Cmd->getArguments();
99  if (Opts.CC1Args)
100  *Opts.CC1Args = {CCArgs.begin(), CCArgs.end()};
101  auto CI = std::make_unique<CompilerInvocation>();
102  if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
103  !Opts.RecoverOnError)
104  return nullptr;
105  return CI;
106 }
Driver.h
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::JobList::Print
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:444
clang::createInvocation
std::unique_ptr< CompilerInvocation > createInvocation(ArrayRef< const char * > Args, CreateInvocationOptions Opts={})
Interpret clang arguments in preparation to parse a file.
Definition: CreateInvocationFromCommandLine.cpp:30
clang::driver::JobList::end
iterator end()
Definition: Job.h:293
clang::CreateInvocationOptions::ProbePrecompiled
bool ProbePrecompiled
Allow the driver to probe the filesystem for PCH files.
Definition: Utils.h:209
Action.h
clang::CompilerInstance::createDiagnostics
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
Definition: CompilerInstance.cpp:330
Options.h
clang::CreateInvocationOptions::VFS
IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
Used e.g.
Definition: Utils.h:200
llvm::opt
Definition: DiagnosticOptions.h:19
Utils.h
Tool.h
clang::driver::JobList
JobList - A sequence of jobs to perform.
Definition: Job.h:267
llvm::SmallString
Definition: LLVM.h:37
Cmd
CompileCommand Cmd
Definition: InterpolatingCompilationDatabase.cpp:130
clang::CreateInvocationOptions::CC1Args
std::vector< std::string > * CC1Args
If set, the target is populated with the cc1 args produced by the driver.
Definition: Utils.h:212
clang::driver::Command
Command - An executable path/name and argument vector to execute.
Definition: Job.h:106
Compilation.h
llvm::ArrayRef< const char * >
clang::CreateInvocationOptions
Optional inputs to createInvocation.
Definition: Utils.h:193
clang::driver::JobList::size
size_type size() const
Definition: Job.h:290
clang::driver::Driver::setProbePrecompiled
void setProbePrecompiled(bool Value)
Definition: Driver.h:369
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:71
FrontendDiagnostic.h
DiagnosticOptions.h
CompilerInstance.h
clang::CreateInvocationOptions::RecoverOnError
bool RecoverOnError
Whether to attempt to produce a non-null (possibly incorrect) invocation if any errors were encounter...
Definition: Utils.h:204
clang::CompilerInvocation::CreateFromArgs
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
Definition: CompilerInvocation.cpp:4527
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70
clang::driver::Driver::setCheckInputsExist
void setCheckInputsExist(bool Value)
Definition: Driver.h:366
clang::CreateInvocationOptions::Diags
IntrusiveRefCntPtr< DiagnosticsEngine > Diags
Receives diagnostics encountered while parsing command-line flags.
Definition: Utils.h:196
clang::driver::Driver::BuildCompilation
Compilation * BuildCompilation(ArrayRef< const char * > Args)
BuildCompilation - Construct a compilation object for a command line argument vector.
Definition: Driver.cpp:1113