clang 22.0.0git
Job.cpp
Go to the documentation of this file.
1//===- Job.cpp - Command to Execute ---------------------------------------===//
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#include "clang/Driver/Job.h"
10#include "clang/Basic/LLVM.h"
11#include "clang/Driver/Driver.h"
13#include "clang/Driver/Tool.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/StringSet.h"
21#include "llvm/ADT/StringSwitch.h"
22#include "llvm/Support/CrashRecoveryContext.h"
23#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/IOSandbox.h"
25#include "llvm/Support/Path.h"
26#include "llvm/Support/PrettyStackTrace.h"
27#include "llvm/Support/Program.h"
28#include "llvm/Support/raw_ostream.h"
29#include <cassert>
30#include <cstddef>
31#include <string>
32#include <system_error>
33#include <utility>
34
35using namespace clang;
36using namespace driver;
37
38Command::Command(const Action &Source, const Tool &Creator,
39 ResponseFileSupport ResponseSupport, const char *Executable,
40 const llvm::opt::ArgStringList &Arguments,
42 const char *PrependArg)
43 : Source(Source), Creator(Creator), ResponseSupport(ResponseSupport),
44 Executable(Executable), PrependArg(PrependArg), Arguments(Arguments) {
45 for (const auto &II : Inputs)
46 if (II.isFilename())
47 InputInfoList.push_back(II);
48 for (const auto &II : Outputs)
49 if (II.isFilename())
50 OutputFilenames.push_back(II.getFilename());
51}
52
53/// Check if the compiler flag in question should be skipped when
54/// emitting a reproducer. Also track how many arguments it has and if the
55/// option is some kind of include path.
56static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
57 bool &IsInclude) {
58 SkipNum = 2;
59 // These flags are all of the form -Flag <Arg> and are treated as two
60 // arguments. Therefore, we need to skip the flag and the next argument.
61 bool ShouldSkip =
62 llvm::StringSwitch<bool>(Flag)
63 .Cases({"-MF", "-MT", "-MQ", "-serialize-diagnostic-file"}, true)
64 .Cases({"-o", "-dependency-file"}, true)
65 .Cases({"-fdebug-compilation-dir", "-diagnostic-log-file"}, true)
66 .Cases({"-dwarf-debug-flags", "-ivfsoverlay"}, true)
67 .Default(false);
68 if (ShouldSkip)
69 return true;
70
71 // Some include flags shouldn't be skipped if we have a crash VFS
72 IsInclude =
73 llvm::StringSwitch<bool>(Flag)
74 .Cases({"-include", "-header-include-file"}, true)
75 .Cases({"-idirafter", "-internal-isystem", "-iwithprefix"}, true)
76 .Cases({"-internal-externc-isystem", "-iprefix"}, true)
77 .Cases({"-iwithprefixbefore", "-isystem", "-iquote"}, true)
78 .Cases({"-isysroot", "-I", "-F", "-resource-dir"}, true)
79 .Cases({"-internal-iframework", "-iframework", "-include-pch"}, true)
80 .Default(false);
81 if (IsInclude)
82 return !HaveCrashVFS;
83
84 // The remaining flags are treated as a single argument.
85
86 // These flags are all of the form -Flag and have no second argument.
87 ShouldSkip = llvm::StringSwitch<bool>(Flag)
88 .Cases({"-M", "-MM", "-MG", "-MP", "-MD"}, true)
89 .Case("-MMD", true)
90 .Default(false);
91
92 // Match found.
93 SkipNum = 1;
94 if (ShouldSkip)
95 return true;
96
97 // These flags are treated as a single argument (e.g., -F<Dir>).
98 StringRef FlagRef(Flag);
99 IsInclude = FlagRef.starts_with("-F") || FlagRef.starts_with("-I");
100 if (IsInclude)
101 return !HaveCrashVFS;
102 if (FlagRef.starts_with("-fmodules-cache-path="))
103 return true;
104
105 SkipNum = 0;
106 return false;
107}
108
109void Command::writeResponseFile(raw_ostream &OS) const {
110 // In a file list, we only write the set of inputs to the response file
111 if (ResponseSupport.ResponseKind == ResponseFileSupport::RF_FileList) {
112 for (const auto *Arg : InputFileList) {
113 OS << Arg << '\n';
114 }
115 return;
116 }
117
118 // In regular response files, we send all arguments to the response file.
119 // Wrapping all arguments in double quotes ensures that both Unix tools and
120 // Windows tools understand the response file.
121 for (const auto *Arg : Arguments) {
122 OS << '"';
123
124 for (; *Arg != '\0'; Arg++) {
125 if (*Arg == '\"' || *Arg == '\\') {
126 OS << '\\';
127 }
128 OS << *Arg;
129 }
130
131 OS << "\" ";
132 }
133}
134
135void Command::buildArgvForResponseFile(
136 llvm::SmallVectorImpl<const char *> &Out) const {
137 // When not a file list, all arguments are sent to the response file.
138 // This leaves us to set the argv to a single parameter, requesting the tool
139 // to read the response file.
140 if (ResponseSupport.ResponseKind != ResponseFileSupport::RF_FileList) {
141 Out.push_back(Executable);
142 Out.push_back(ResponseFileFlag.c_str());
143 return;
144 }
145
146 llvm::StringSet<> Inputs(llvm::from_range, InputFileList);
147 Out.push_back(Executable);
148
149 if (PrependArg)
150 Out.push_back(PrependArg);
151
152 // In a file list, build args vector ignoring parameters that will go in the
153 // response file (elements of the InputFileList vector)
154 bool FirstInput = true;
155 for (const auto *Arg : Arguments) {
156 if (Inputs.count(Arg) == 0) {
157 Out.push_back(Arg);
158 } else if (FirstInput) {
159 FirstInput = false;
160 Out.push_back(ResponseSupport.ResponseFlag);
161 Out.push_back(ResponseFile);
162 }
163 }
164}
165
166/// Rewrite relative include-like flag paths to absolute ones.
167static void
169 size_t NumArgs,
171 using namespace llvm;
172 using namespace sys;
173
174 auto getAbsPath = [](StringRef InInc, SmallVectorImpl<char> &OutInc) -> bool {
175 if (path::is_absolute(InInc)) // Nothing to do here...
176 return false;
177 std::error_code EC = fs::current_path(OutInc);
178 if (EC)
179 return false;
180 path::append(OutInc, InInc);
181 return true;
182 };
183
184 SmallString<128> NewInc;
185 if (NumArgs == 1) {
186 StringRef FlagRef(Args[Idx + NumArgs - 1]);
187 assert((FlagRef.starts_with("-F") || FlagRef.starts_with("-I")) &&
188 "Expecting -I or -F");
189 StringRef Inc = FlagRef.substr(2);
190 if (getAbsPath(Inc, NewInc)) {
191 SmallString<128> NewArg(FlagRef.slice(0, 2));
192 NewArg += NewInc;
193 IncFlags.push_back(std::move(NewArg));
194 }
195 return;
196 }
197
198 assert(NumArgs == 2 && "Not expecting more than two arguments");
199 StringRef Inc(Args[Idx + NumArgs - 1]);
200 if (!getAbsPath(Inc, NewInc))
201 return;
202 IncFlags.push_back(SmallString<128>(Args[Idx]));
203 IncFlags.push_back(std::move(NewInc));
204}
205
206void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
207 CrashReportInfo *CrashInfo) const {
208 // Always quote the exe.
209 OS << ' ';
210 llvm::sys::printArg(OS, Executable, /*Quote=*/true);
211
212 ArrayRef<const char *> Args = Arguments;
214 if (ResponseFile != nullptr) {
215 buildArgvForResponseFile(ArgsRespFile);
216 Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
217 } else if (PrependArg) {
218 OS << ' ';
219 llvm::sys::printArg(OS, PrependArg, /*Quote=*/true);
220 }
221
222 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
223 for (size_t i = 0, e = Args.size(); i < e; ++i) {
224 const char *const Arg = Args[i];
225
226 if (CrashInfo) {
227 int NumArgs = 0;
228 bool IsInclude = false;
229 if (skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
230 i += NumArgs - 1;
231 continue;
232 }
233
234 // Relative includes need to be expanded to absolute paths.
235 if (HaveCrashVFS && IsInclude) {
236 SmallVector<SmallString<128>, 2> NewIncFlags;
237 rewriteIncludes(Args, i, NumArgs, NewIncFlags);
238 if (!NewIncFlags.empty()) {
239 for (auto &F : NewIncFlags) {
240 OS << ' ';
241 llvm::sys::printArg(OS, F.c_str(), Quote);
242 }
243 i += NumArgs - 1;
244 continue;
245 }
246 }
247
248 auto Found = llvm::find_if(InputInfoList, [&Arg](const InputInfo &II) {
249 return II.getFilename() == Arg;
250 });
251 if (Found != InputInfoList.end() &&
252 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
253 // Replace the input file name with the crashinfo's file name.
254 OS << ' ';
255 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
256 llvm::sys::printArg(OS, ShortName.str(), Quote);
257 continue;
258 }
259 }
260
261 OS << ' ';
262 llvm::sys::printArg(OS, Arg, Quote);
263 }
264
265 if (CrashInfo && HaveCrashVFS) {
266 OS << ' ';
267 llvm::sys::printArg(OS, "-ivfsoverlay", Quote);
268 OS << ' ';
269 llvm::sys::printArg(OS, CrashInfo->VFSPath.str(), Quote);
270
271 // The leftover modules from the crash are stored in
272 // <name>.cache/vfs/modules
273 // Leave it untouched for pcm inspection and provide a clean/empty dir
274 // path to contain the future generated module cache:
275 // <name>.cache/vfs/repro-modules
276 SmallString<128> RelModCacheDir = llvm::sys::path::parent_path(
277 llvm::sys::path::parent_path(CrashInfo->VFSPath));
278 llvm::sys::path::append(RelModCacheDir, "repro-modules");
279
280 std::string ModCachePath = "-fmodules-cache-path=";
281 ModCachePath.append(RelModCacheDir.c_str());
282
283 OS << ' ';
284 llvm::sys::printArg(OS, ModCachePath, Quote);
285 }
286
287 if (ResponseFile != nullptr) {
288 OS << "\n Arguments passed via response file:\n";
289 writeResponseFile(OS);
290 // Avoiding duplicated newline terminator, since FileLists are
291 // newline-separated.
292 if (ResponseSupport.ResponseKind != ResponseFileSupport::RF_FileList)
293 OS << "\n";
294 OS << " (end of response file)";
295 }
296
297 OS << Terminator;
298}
299
301 ResponseFile = FileName;
302 ResponseFileFlag = ResponseSupport.ResponseFlag;
303 ResponseFileFlag += FileName;
304}
305
307 Environment.reserve(NewEnvironment.size() + 1);
308 Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
309 Environment.push_back(nullptr);
310}
311
313 const std::vector<std::optional<std::string>> &Redirects) {
314 RedirectFiles = Redirects;
315}
316
319 for (const auto &Arg : InputInfoList)
320 llvm::outs() << llvm::sys::path::filename(Arg.getFilename()) << "\n";
321 llvm::outs().flush();
322 }
323}
324
325int Command::Execute(ArrayRef<std::optional<StringRef>> Redirects,
326 std::string *ErrMsg, bool *ExecutionFailed) const {
328
330 if (ResponseFile == nullptr) {
331 Argv.push_back(Executable);
332 if (PrependArg)
333 Argv.push_back(PrependArg);
334 Argv.append(Arguments.begin(), Arguments.end());
335 Argv.push_back(nullptr);
336 } else {
337 // If the command is too large, we need to put arguments in a response file.
338 std::string RespContents;
339 llvm::raw_string_ostream SS(RespContents);
340
341 // Write file contents and build the Argv vector
342 writeResponseFile(SS);
343 buildArgvForResponseFile(Argv);
344 Argv.push_back(nullptr);
345
346 // Save the response file in the appropriate encoding
347 if (std::error_code EC = writeFileWithEncoding(
348 ResponseFile, RespContents, ResponseSupport.ResponseEncoding)) {
349 if (ErrMsg)
350 *ErrMsg = EC.message();
351 if (ExecutionFailed)
352 *ExecutionFailed = true;
353 // Return -1 by convention (see llvm/include/llvm/Support/Program.h) to
354 // indicate the requested executable cannot be started.
355 return -1;
356 }
357 }
358
359 std::optional<ArrayRef<StringRef>> Env;
360 std::vector<StringRef> ArgvVectorStorage;
361 if (!Environment.empty()) {
362 assert(Environment.back() == nullptr &&
363 "Environment vector should be null-terminated by now");
364 ArgvVectorStorage = llvm::toStringRefArray(Environment.data());
365 Env = ArrayRef(ArgvVectorStorage);
366 }
367
368 auto Args = llvm::toStringRefArray(Argv.data());
369
370 // Use Job-specific redirect files if they are present.
371 if (!RedirectFiles.empty()) {
372 std::vector<std::optional<StringRef>> RedirectFilesOptional;
373 for (const auto &Ele : RedirectFiles)
374 if (Ele)
375 RedirectFilesOptional.push_back(std::optional<StringRef>(*Ele));
376 else
377 RedirectFilesOptional.push_back(std::nullopt);
378
379 return llvm::sys::ExecuteAndWait(Executable, Args, Env,
380 ArrayRef(RedirectFilesOptional),
381 /*secondsToWait=*/0, /*memoryLimit=*/0,
382 ErrMsg, ExecutionFailed, &ProcStat);
383 }
384
385 return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
386 /*secondsToWait*/ 0, /*memoryLimit*/ 0,
387 ErrMsg, ExecutionFailed, &ProcStat);
388}
389
390CC1Command::CC1Command(const Action &Source, const Tool &Creator,
391 ResponseFileSupport ResponseSupport,
392 const char *Executable,
393 const llvm::opt::ArgStringList &Arguments,
395 const char *PrependArg)
396 : Command(Source, Creator, ResponseSupport, Executable, Arguments, Inputs,
397 Outputs, PrependArg) {
398 InProcess = true;
399}
400
401void CC1Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
402 CrashReportInfo *CrashInfo) const {
403 if (InProcess)
404 OS << " (in-process)\n";
405 Command::Print(OS, Terminator, Quote, CrashInfo);
406}
407
408int CC1Command::Execute(ArrayRef<std::optional<StringRef>> Redirects,
409 std::string *ErrMsg, bool *ExecutionFailed) const {
410 // FIXME: Currently, if there're more than one job, we disable
411 // -fintegrate-cc1. If we're no longer a integrated-cc1 job, fallback to
412 // out-of-process execution. See discussion in https://reviews.llvm.org/D74447
413 if (!InProcess)
414 return Command::Execute(Redirects, ErrMsg, ExecutionFailed);
415
417
419 Argv.push_back(getExecutable());
420 Argv.append(getArguments().begin(), getArguments().end());
421 Argv.push_back(nullptr);
422 Argv.pop_back(); // The terminating null element shall not be part of the
423 // slice (main() behavior).
424
425 // This flag simply indicates that the program couldn't start, which isn't
426 // applicable here.
427 if (ExecutionFailed)
428 *ExecutionFailed = false;
429
430 // Enabling the sandbox here allows us to restore its previous state even when
431 // this cc1 invocation crashes.
432 auto EnableSandbox = llvm::sys::sandbox::scopedEnable();
433
434 llvm::CrashRecoveryContext CRC;
435 CRC.DumpStackAndCleanupOnFailure = true;
436
437 const void *PrettyState = llvm::SavePrettyStackState();
438 const Driver &D = getCreator().getToolChain().getDriver();
439
440 int R = 0;
441 // Enter ExecuteCC1Tool() instead of starting up a new process
442 if (!CRC.RunSafely([&]() { R = D.CC1Main(Argv); })) {
443 llvm::RestorePrettyStackState(PrettyState);
444 return CRC.RetCode;
445 }
446 return R;
447}
448
450 // We don't support set a new environment when calling into ExecuteCC1Tool()
451 llvm_unreachable(
452 "The CC1Command doesn't support changing the environment vars!");
453}
454
455void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
456 CrashReportInfo *CrashInfo) const {
457 for (const auto &Job : *this)
458 Job.Print(OS, Terminator, Quote, CrashInfo);
459}
460
461void JobList::clear() { Jobs.clear(); }
static void rewriteIncludes(const llvm::ArrayRef< const char * > &Args, size_t Idx, size_t NumArgs, llvm::SmallVectorImpl< llvm::SmallString< 128 > > &IncFlags)
Rewrite relative include-like flag paths to absolute ones.
Definition Job.cpp:168
static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum, bool &IsInclude)
Check if the compiler flag in question should be skipped when emitting a reproducer.
Definition Job.cpp:56
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Action - Represent an abstract compilation step to perform.
Definition Action.h:47
void setEnvironment(llvm::ArrayRef< const char * > NewEnvironment) override
Sets the environment to be used by the new process.
Definition Job.cpp:449
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
Definition Job.cpp:401
int Execute(ArrayRef< std::optional< StringRef > > Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
Definition Job.cpp:408
CC1Command(const Action &Source, const Tool &Creator, ResponseFileSupport ResponseSupport, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef< InputInfo > Inputs, ArrayRef< InputInfo > Outputs={}, const char *PrependArg=nullptr)
Definition Job.cpp:390
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
void setResponseFile(const char *FileName)
Set to pass arguments via a response file when launching the command.
Definition Job.cpp:300
void setRedirectFiles(const std::vector< std::optional< std::string > > &Redirects)
Definition Job.cpp:312
Command(const Action &Source, const Tool &Creator, ResponseFileSupport ResponseSupport, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef< InputInfo > Inputs, ArrayRef< InputInfo > Outputs={}, const char *PrependArg=nullptr)
Definition Job.cpp:38
bool PrintInputFilenames
Whether to print the input filenames when executing.
Definition Job.h:167
const char * getExecutable() const
Definition Job.h:222
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition Job.cpp:206
bool InProcess
Whether the command will be executed in this process or not.
Definition Job.h:170
virtual void setEnvironment(llvm::ArrayRef< const char * > NewEnvironment)
Sets the environment to be used by the new process.
Definition Job.cpp:306
void PrintFileNames() const
Optionally print the filenames to be compiled.
Definition Job.cpp:317
virtual int Execute(ArrayRef< std::optional< StringRef > > Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
Definition Job.cpp:325
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
const char * getFilename() const
Definition InputInfo.h:83
void clear()
Clear the job list.
Definition Job.cpp:461
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition Job.cpp:455
const Driver & getDriver() const
Definition ToolChain.h:253
Tool - Information on a specific compilation tool.
Definition Tool.h:32
const ToolChain & getToolChain() const
Definition Tool.h:52
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
The JSON file list parser is used to communicate input to InstallAPI.
for(const auto &A :T->param_types())
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30