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"
32#include <system_error>
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)
47 InputInfoList.push_back(II);
48 for (
const auto &II : Outputs)
50 OutputFilenames.push_back(II.getFilename());
56static bool skipArgs(
const char *Flag,
bool HaveCrashVFS,
int &SkipNum,
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)
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)
87 ShouldSkip = llvm::StringSwitch<bool>(Flag)
88 .Cases({
"-M",
"-MM",
"-MG",
"-MP",
"-MD"},
true)
98 StringRef FlagRef(Flag);
99 IsInclude = FlagRef.starts_with(
"-F") || FlagRef.starts_with(
"-I");
101 return !HaveCrashVFS;
102 if (FlagRef.starts_with(
"-fmodules-cache-path="))
109void Command::writeResponseFile(raw_ostream &OS)
const {
112 for (
const auto *Arg : InputFileList) {
121 for (
const auto *Arg : Arguments) {
124 for (; *Arg !=
'\0'; Arg++) {
125 if (*Arg ==
'\"' || *Arg ==
'\\') {
135void Command::buildArgvForResponseFile(
136 llvm::SmallVectorImpl<const char *> &Out)
const {
141 Out.push_back(Executable);
142 Out.push_back(ResponseFileFlag.c_str());
146 llvm::StringSet<> Inputs(llvm::from_range, InputFileList);
147 Out.push_back(Executable);
150 Out.push_back(PrependArg);
154 bool FirstInput =
true;
155 for (
const auto *Arg : Arguments) {
156 if (Inputs.count(Arg) == 0) {
158 }
else if (FirstInput) {
160 Out.push_back(ResponseSupport.ResponseFlag);
161 Out.push_back(ResponseFile);
171 using namespace llvm;
175 if (path::is_absolute(InInc))
177 std::error_code EC = fs::current_path(OutInc);
180 path::append(OutInc, InInc);
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)) {
193 IncFlags.push_back(std::move(NewArg));
198 assert(NumArgs == 2 &&
"Not expecting more than two arguments");
199 StringRef Inc(Args[Idx + NumArgs - 1]);
200 if (!getAbsPath(Inc, NewInc))
203 IncFlags.push_back(std::move(NewInc));
210 llvm::sys::printArg(OS, Executable,
true);
214 if (ResponseFile !=
nullptr) {
215 buildArgvForResponseFile(ArgsRespFile);
217 }
else if (PrependArg) {
219 llvm::sys::printArg(OS, PrependArg,
true);
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];
228 bool IsInclude =
false;
229 if (
skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
235 if (HaveCrashVFS && IsInclude) {
238 if (!NewIncFlags.empty()) {
239 for (
auto &F : NewIncFlags) {
241 llvm::sys::printArg(OS, F.c_str(), Quote);
248 auto Found = llvm::find_if(InputInfoList, [&Arg](
const InputInfo &II) {
251 if (
Found != InputInfoList.end() &&
252 (i == 0 || StringRef(Args[i - 1]) !=
"-main-file-name")) {
255 StringRef ShortName = llvm::sys::path::filename(CrashInfo->
Filename);
256 llvm::sys::printArg(OS, ShortName.str(), Quote);
262 llvm::sys::printArg(OS, Arg, Quote);
265 if (CrashInfo && HaveCrashVFS) {
267 llvm::sys::printArg(OS,
"-ivfsoverlay", Quote);
269 llvm::sys::printArg(OS, CrashInfo->
VFSPath.str(), Quote);
277 llvm::sys::path::parent_path(CrashInfo->
VFSPath));
278 llvm::sys::path::append(RelModCacheDir,
"repro-modules");
280 std::string ModCachePath =
"-fmodules-cache-path=";
281 ModCachePath.append(RelModCacheDir.c_str());
284 llvm::sys::printArg(OS, ModCachePath, Quote);
287 if (ResponseFile !=
nullptr) {
288 OS <<
"\n Arguments passed via response file:\n";
289 writeResponseFile(OS);
294 OS <<
" (end of response file)";
302 ResponseFileFlag = ResponseSupport.ResponseFlag;
307 Environment.reserve(NewEnvironment.size() + 1);
308 Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
309 Environment.push_back(
nullptr);
313 const std::vector<std::optional<std::string>> &Redirects) {
314 RedirectFiles = Redirects;
319 for (
const auto &Arg : InputInfoList)
320 llvm::outs() << llvm::sys::path::filename(Arg.getFilename()) <<
"\n";
321 llvm::outs().flush();
326 std::string *ErrMsg,
bool *ExecutionFailed)
const {
330 if (ResponseFile ==
nullptr) {
331 Argv.push_back(Executable);
333 Argv.push_back(PrependArg);
334 Argv.append(Arguments.begin(), Arguments.end());
335 Argv.push_back(
nullptr);
338 std::string RespContents;
339 llvm::raw_string_ostream SS(RespContents);
342 writeResponseFile(SS);
343 buildArgvForResponseFile(Argv);
344 Argv.push_back(
nullptr);
347 if (std::error_code EC = writeFileWithEncoding(
348 ResponseFile, RespContents, ResponseSupport.ResponseEncoding)) {
350 *ErrMsg = EC.message();
352 *ExecutionFailed =
true;
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());
368 auto Args = llvm::toStringRefArray(Argv.data());
371 if (!RedirectFiles.empty()) {
372 std::vector<std::optional<StringRef>> RedirectFilesOptional;
373 for (
const auto &Ele : RedirectFiles)
375 RedirectFilesOptional.push_back(std::optional<StringRef>(*Ele));
377 RedirectFilesOptional.push_back(std::nullopt);
379 return llvm::sys::ExecuteAndWait(Executable, Args, Env,
382 ErrMsg, ExecutionFailed, &ProcStat);
385 return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
387 ErrMsg, ExecutionFailed, &ProcStat);
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) {
404 OS <<
" (in-process)\n";
409 std::string *ErrMsg,
bool *ExecutionFailed)
const {
421 Argv.push_back(
nullptr);
428 *ExecutionFailed =
false;
432 auto EnableSandbox = llvm::sys::sandbox::scopedEnable();
434 llvm::CrashRecoveryContext CRC;
435 CRC.DumpStackAndCleanupOnFailure =
true;
437 const void *PrettyState = llvm::SavePrettyStackState();
442 if (!CRC.RunSafely([&]() { R = D.CC1Main(Argv); })) {
443 llvm::RestorePrettyStackState(PrettyState);
452 "The CC1Command doesn't support changing the environment vars!");
457 for (
const auto &Job : *
this)
458 Job.Print(OS, Terminator, Quote, CrashInfo);
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.
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.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Action - Represent an abstract compilation step to perform.
void setEnvironment(llvm::ArrayRef< const char * > NewEnvironment) override
Sets the environment to be used by the new process.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
int Execute(ArrayRef< std::optional< StringRef > > Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
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)
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
const llvm::opt::ArgStringList & getArguments() const
void setResponseFile(const char *FileName)
Set to pass arguments via a response file when launching the command.
void setRedirectFiles(const std::vector< std::optional< std::string > > &Redirects)
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)
bool PrintInputFilenames
Whether to print the input filenames when executing.
const char * getExecutable() const
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
bool InProcess
Whether the command will be executed in this process or not.
virtual void setEnvironment(llvm::ArrayRef< const char * > NewEnvironment)
Sets the environment to be used by the new process.
void PrintFileNames() const
Optionally print the filenames to be compiled.
virtual int Execute(ArrayRef< std::optional< StringRef > > Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
void clear()
Clear the job list.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
@ 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.