clang 22.0.0git
CreateInvocationFromArgs.cpp
Go to the documentation of this file.
1//===--- CreateInvocationFromArgs.h - 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
17#include "clang/Driver/Driver.h"
18#include "clang/Driver/Tool.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Option/ArgList.h"
25#include "llvm/Support/VirtualFileSystem.h"
26#include "llvm/TargetParser/Host.h"
27
28using namespace llvm::opt;
29
30namespace clang {
31
32std::unique_ptr<CompilerInvocation>
34 assert(!ArgList.empty());
35 std::optional<DiagnosticOptions> LocalDiagOpts;
37 if (Opts.Diags) {
38 Diags = std::move(Opts.Diags);
39 } else {
40 LocalDiagOpts.emplace();
42 Opts.VFS ? *Opts.VFS : *llvm::vfs::getRealFileSystem(), *LocalDiagOpts);
43 }
44
46
47 // FIXME: Find a cleaner way to force the driver into restricted modes.
48 Args.insert(
49 llvm::find_if(
50 Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
51 "-fsyntax-only");
52
53 // FIXME: We shouldn't have to pass in the path info.
54 driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
55 "clang LLVM compiler", Opts.VFS);
56
57 // Don't check that inputs exist, they may have been remapped.
58 TheDriver.setCheckInputsExist(false);
60
61 std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
62 if (!C)
63 return nullptr;
64
65 if (C->getArgs().hasArg(options::OPT_fdriver_only))
66 return nullptr;
67
68 // Just print the cc1 options if -### was present.
69 if (C->getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
70 C->getJobs().Print(llvm::errs(), "\n", true);
71 return nullptr;
72 }
73
74 // We expect to get back exactly one command job, if we didn't something
75 // failed. Offload compilation is an exception as it creates multiple jobs. If
76 // that's the case, we proceed with the first job. If caller needs a
77 // particular job, it should be controlled via options (e.g.
78 // --cuda-{host|device}-only for CUDA) passed to the driver.
79 const driver::JobList &Jobs = C->getJobs();
80 bool OffloadCompilation = false;
81 if (Jobs.size() > 1) {
82 for (auto &A : C->getActions()) {
83 // On MacOSX real actions may end up being wrapped in BindArchAction
85 A = *A->input_begin();
87 OffloadCompilation = true;
88 break;
89 }
90 }
91 }
92
93 bool PickFirstOfMany = OffloadCompilation || Opts.RecoverOnError;
94 if (Jobs.size() == 0 || (Jobs.size() > 1 && !PickFirstOfMany)) {
96 llvm::raw_svector_ostream OS(Msg);
97 Jobs.Print(OS, "; ", true);
98 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
99 return nullptr;
100 }
101 auto Cmd = llvm::find_if(Jobs, [](const driver::Command &Cmd) {
102 return StringRef(Cmd.getCreator().getName()) == "clang";
103 });
104 if (Cmd == Jobs.end()) {
105 Diags->Report(diag::err_fe_expected_clang_command);
106 return nullptr;
107 }
108
109 const ArgStringList &CCArgs = Cmd->getArguments();
110 if (Opts.CC1Args)
111 *Opts.CC1Args = {CCArgs.begin(), CCArgs.end()};
112 auto CI = std::make_unique<CompilerInvocation>();
113 if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
114 !Opts.RecoverOnError)
115 return nullptr;
116 return CI;
117}
118
119} // namespace clang
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, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
Command - An executable path/name and argument vector to execute.
Definition Job.h:106
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition Job.h:191
const llvm::opt::ArgStringList & getArguments() const
Definition Job.h:224
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
void setCheckInputsExist(bool Value)
Definition Driver.h:431
void setProbePrecompiled(bool Value)
Definition Driver.h:434
Compilation * BuildCompilation(ArrayRef< const char * > Args)
BuildCompilation - Construct a compilation object for a command line argument vector.
Definition Driver.cpp:1454
JobList - A sequence of jobs to perform.
Definition Job.h:260
size_type size() const
Definition Job.h:283
iterator end()
Definition Job.h:286
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition Job.cpp:450
const char * getName() const
Definition Tool.h:48
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
std::unique_ptr< CompilerInvocation > createInvocation(ArrayRef< const char * > Args, CreateInvocationOptions Opts={})
Interpret clang arguments in preparation to parse a file.
Optional inputs to createInvocation.
IntrusiveRefCntPtr< DiagnosticsEngine > Diags
Receives diagnostics encountered while parsing command-line flags.
bool ProbePrecompiled
Allow the driver to probe the filesystem for PCH files.
bool RecoverOnError
Whether to attempt to produce a non-null (possibly incorrect) invocation if any errors were encounter...
IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
Used e.g.
std::vector< std::string > * CC1Args
If set, the target is populated with the cc1 args produced by the driver.