16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/Optional.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/StringSet.h" 22 #include "llvm/ADT/StringSwitch.h" 23 #include "llvm/Support/FileSystem.h" 24 #include "llvm/Support/Path.h" 25 #include "llvm/Support/Program.h" 26 #include "llvm/Support/raw_ostream.h" 29 using llvm::raw_ostream;
30 using llvm::StringRef;
34 const char *Executable,
const ArgStringList &Arguments,
36 : Source(Source), Creator(Creator), Executable(Executable),
37 Arguments(Arguments), ResponseFile(nullptr) {
38 for (
const auto &II : Inputs)
40 InputFilenames.push_back(II.getFilename());
46 static bool skipArgs(
const char *Flag,
bool HaveCrashVFS,
int &SkipNum,
51 bool ShouldSkip = llvm::StringSwitch<bool>(Flag)
52 .Cases(
"-MF",
"-MT",
"-MQ",
"-serialize-diagnostic-file",
true)
53 .Cases(
"-o",
"-dependency-file",
true)
54 .Cases(
"-fdebug-compilation-dir",
"-diagnostic-log-file",
true)
55 .Cases(
"-dwarf-debug-flags",
"-ivfsoverlay",
true)
61 IsInclude = llvm::StringSwitch<bool>(Flag)
62 .Cases(
"-include",
"-header-include-file",
true)
63 .Cases(
"-idirafter",
"-internal-isystem",
"-iwithprefix",
true)
64 .Cases(
"-internal-externc-isystem",
"-iprefix",
true)
65 .Cases(
"-iwithprefixbefore",
"-isystem",
"-iquote",
true)
66 .Cases(
"-isysroot",
"-I",
"-F",
"-resource-dir",
true)
67 .Cases(
"-iframework",
"-include-pch",
true)
70 return HaveCrashVFS ?
false :
true;
75 ShouldSkip = llvm::StringSwitch<bool>(Flag)
76 .Cases(
"-M",
"-MM",
"-MG",
"-MP",
"-MD",
true)
86 StringRef FlagRef(Flag);
87 IsInclude = FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I");
89 return HaveCrashVFS ?
false :
true;
90 if (FlagRef.startswith(
"-fmodules-cache-path="))
98 const bool Escape = Arg.find_first_of(
"\"\\$") != StringRef::npos;
100 if (!Quote && !Escape) {
107 for (
const char c : Arg) {
108 if (c ==
'"' || c ==
'\\' || c ==
'$')
115 void Command::writeResponseFile(raw_ostream &OS)
const {
118 for (
const char *Arg : InputFileList) {
127 for (
const char *Arg : Arguments) {
130 for (; *Arg !=
'\0'; Arg++) {
131 if (*Arg ==
'\"' || *Arg ==
'\\') {
141 void Command::buildArgvForResponseFile(
147 Out.push_back(Executable);
148 Out.push_back(ResponseFileFlag.c_str());
152 llvm::StringSet<> Inputs;
153 for (
const char *InputName : InputFileList)
154 Inputs.insert(InputName);
155 Out.push_back(Executable);
158 bool FirstInput =
true;
159 for (
const char *Arg : Arguments) {
160 if (Inputs.count(Arg) == 0) {
162 }
else if (FirstInput) {
165 Out.push_back(ResponseFile);
175 using namespace llvm;
178 if (path::is_absolute(InInc))
180 std::error_code EC = fs::current_path(OutInc);
183 path::append(OutInc, InInc);
189 StringRef FlagRef(Args[Idx + NumArgs - 1]);
190 assert((FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I")) &&
191 "Expecting -I or -F");
192 StringRef Inc = FlagRef.slice(2, StringRef::npos);
193 if (getAbsPath(Inc, NewInc)) {
196 IncFlags.push_back(std::move(NewArg));
201 assert(NumArgs == 2 &&
"Not expecting more than two arguments");
202 StringRef Inc(Args[Idx + NumArgs - 1]);
203 if (!getAbsPath(Inc, NewInc))
206 IncFlags.push_back(std::move(NewInc));
213 printArg(OS, Executable,
true);
217 if (ResponseFile !=
nullptr) {
218 buildArgvForResponseFile(ArgsRespFile);
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 printArg(OS, F.c_str(), Quote);
248 auto Found = std::find_if(InputFilenames.begin(), InputFilenames.end(),
249 [&Arg](StringRef IF) {
return IF == Arg; });
250 if (Found != InputFilenames.end() &&
251 (i == 0 || StringRef(Args[i - 1]) !=
"-main-file-name")) {
254 StringRef ShortName = llvm::sys::path::filename(CrashInfo->
Filename);
255 printArg(OS, ShortName.str(), Quote);
261 printArg(OS, Arg, Quote);
264 if (CrashInfo && HaveCrashVFS) {
266 printArg(OS,
"-ivfsoverlay", Quote);
268 printArg(OS, CrashInfo->
VFSPath.str(), Quote);
276 llvm::sys::path::parent_path(CrashInfo->
VFSPath));
277 llvm::sys::path::append(RelModCacheDir,
"repro-modules");
279 std::string ModCachePath =
"-fmodules-cache-path=";
280 ModCachePath.append(RelModCacheDir.c_str());
283 printArg(OS, ModCachePath, Quote);
286 if (ResponseFile !=
nullptr) {
287 OS <<
"\n Arguments passed via response file:\n";
288 writeResponseFile(OS);
293 OS <<
" (end of response file)";
300 ResponseFile = FileName;
302 ResponseFileFlag += FileName;
306 Environment.reserve(NewEnvironment.size() + 1);
307 Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
308 Environment.push_back(
nullptr);
312 std::string *ErrMsg,
bool *ExecutionFailed)
const {
316 if (Environment.empty()) {
319 assert(Environment.back() ==
nullptr &&
320 "Environment vector should be null-terminated by now");
321 Envp =
const_cast<const char **
>(Environment.data());
324 if (ResponseFile ==
nullptr) {
325 Argv.push_back(Executable);
326 Argv.append(Arguments.begin(), Arguments.end());
327 Argv.push_back(
nullptr);
329 return llvm::sys::ExecuteAndWait(
330 Executable, Argv.data(), Envp, Redirects, 0,
331 0, ErrMsg, ExecutionFailed);
336 std::string RespContents;
337 llvm::raw_string_ostream SS(RespContents);
340 writeResponseFile(SS);
341 buildArgvForResponseFile(Argv);
342 Argv.push_back(
nullptr);
346 if (std::error_code EC = writeFileWithEncoding(
349 *ErrMsg = EC.message();
351 *ExecutionFailed =
true;
355 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), Envp, Redirects,
357 0, ErrMsg, ExecutionFailed);
361 const char *Executable_,
362 const ArgStringList &Arguments_,
364 std::unique_ptr<Command> Fallback_)
365 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs),
366 Fallback(
std::move(Fallback_)) {}
372 Fallback->Print(OS, Terminator, Quote, CrashInfo);
379 return ExitCode != 0;
383 std::string *ErrMsg,
bool *ExecutionFailed)
const {
386 return PrimaryStatus;
392 *ExecutionFailed =
false;
395 D.
Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
397 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
398 return SecondaryStatus;
402 const Tool &Creator_,
403 const char *Executable_,
404 const ArgStringList &Arguments_,
406 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
411 OS <<
" || (exit 0)" << Terminator;
416 bool *ExecutionFailed)
const {
420 *ExecutionFailed =
false;
426 for (
const auto &Job : *
this)
427 Job.Print(OS, Terminator, Quote, CrashInfo);
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
void setResponseFile(const char *FileName)
Set to pass arguments via a response file when launching the command.
DiagnosticBuilder Diag(unsigned DiagID) const
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.
Action - Represent an abstract compilation step to perform.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
void setEnvironment(llvm::ArrayRef< const char *> NewEnvironment)
Sets the environment to be used by the new process.
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.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
static bool ShouldFallback(int ExitCode)
static void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
void clear()
Clear the job list.
Command(const Action &Source, const Tool &Creator, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef< InputInfo > Inputs)
Command - An executable path/name and argument vector to execute.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
ForceSuccessCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs)
int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
FallbackCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs, std::unique_ptr< Command > Fallback_)
virtual int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const