16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringSet.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Program.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang::driver;
28 using llvm::raw_ostream;
29 using llvm::StringRef;
33 const char *Executable,
const ArgStringList &Arguments,
35 : Source(Source), Creator(Creator), Executable(Executable),
36 Arguments(Arguments), ResponseFile(nullptr) {
37 for (
const auto &II : Inputs)
39 InputFilenames.push_back(II.getFilename());
45 static bool skipArgs(
const char *Flag,
bool HaveCrashVFS,
int &SkipNum,
50 bool ShouldSkip = llvm::StringSwitch<bool>(Flag)
51 .Cases(
"-MF",
"-MT",
"-MQ",
"-serialize-diagnostic-file",
true)
52 .Cases(
"-o",
"-dependency-file",
true)
53 .Cases(
"-fdebug-compilation-dir",
"-diagnostic-log-file",
true)
54 .Cases(
"-dwarf-debug-flags",
"-ivfsoverlay",
true)
60 IsInclude = llvm::StringSwitch<bool>(Flag)
61 .Cases(
"-include",
"-header-include-file",
true)
62 .Cases(
"-idirafter",
"-internal-isystem",
"-iwithprefix",
true)
63 .Cases(
"-internal-externc-isystem",
"-iprefix",
true)
64 .Cases(
"-iwithprefixbefore",
"-isystem",
"-iquote",
true)
65 .Cases(
"-isysroot",
"-I",
"-F",
"-resource-dir",
true)
66 .Cases(
"-iframework",
"-include-pch",
true)
69 return HaveCrashVFS ?
false :
true;
74 ShouldSkip = llvm::StringSwitch<bool>(Flag)
75 .Cases(
"-M",
"-MM",
"-MG",
"-MP",
"-MD",
true)
85 StringRef FlagRef(Flag);
86 IsInclude = FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I");
88 return HaveCrashVFS ?
false :
true;
89 if (FlagRef.startswith(
"-fmodules-cache-path="))
97 const bool Escape = Arg.find_first_of(
"\"\\$") != StringRef::npos;
99 if (!Quote && !Escape) {
106 for (
const char c : Arg) {
107 if (c ==
'"' || c ==
'\\' || c ==
'$')
114 void Command::writeResponseFile(raw_ostream &OS)
const {
117 for (
const char *Arg : InputFileList) {
126 for (
const char *Arg : Arguments) {
129 for (; *Arg !=
'\0'; Arg++) {
130 if (*Arg ==
'\"' || *Arg ==
'\\') {
140 void Command::buildArgvForResponseFile(
146 Out.push_back(Executable);
147 Out.push_back(ResponseFileFlag.c_str());
151 llvm::StringSet<> Inputs;
152 for (
const char *InputName : InputFileList)
153 Inputs.insert(InputName);
154 Out.push_back(Executable);
157 bool FirstInput =
true;
158 for (
const char *Arg : Arguments) {
159 if (Inputs.count(Arg) == 0) {
161 }
else if (FirstInput) {
164 Out.push_back(ResponseFile);
174 using namespace llvm;
177 if (path::is_absolute(InInc))
179 std::error_code EC = fs::current_path(OutInc);
182 path::append(OutInc, InInc);
188 StringRef FlagRef(Args[Idx + NumArgs - 1]);
189 assert((FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I")) &&
190 "Expecting -I or -F");
191 StringRef Inc = FlagRef.slice(2, StringRef::npos);
192 if (getAbsPath(Inc, NewInc)) {
195 IncFlags.push_back(std::move(NewArg));
200 assert(NumArgs == 2 &&
"Not expecting more than two arguments");
201 StringRef Inc(Args[Idx + NumArgs - 1]);
202 if (!getAbsPath(Inc, NewInc))
205 IncFlags.push_back(std::move(NewInc));
212 printArg(OS, Executable,
true);
216 if (ResponseFile !=
nullptr) {
217 buildArgvForResponseFile(ArgsRespFile);
221 bool HaveCrashVFS = CrashInfo && !CrashInfo->
VFSPath.empty();
222 for (
size_t i = 0, e = Args.size(); i < e; ++i) {
223 const char *
const Arg = Args[i];
227 bool IsInclude =
false;
228 if (
skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
234 if (HaveCrashVFS && IsInclude) {
237 if (!NewIncFlags.empty()) {
238 for (
auto &F : NewIncFlags) {
240 printArg(OS, F.c_str(), Quote);
247 auto Found = std::find_if(InputFilenames.begin(), InputFilenames.end(),
248 [&Arg](StringRef IF) {
return IF == Arg; });
249 if (Found != InputFilenames.end() &&
250 (i == 0 || StringRef(Args[i - 1]) !=
"-main-file-name")) {
253 StringRef ShortName = llvm::sys::path::filename(CrashInfo->
Filename);
254 printArg(OS, ShortName.str(), Quote);
260 printArg(OS, Arg, Quote);
263 if (CrashInfo && HaveCrashVFS) {
265 printArg(OS,
"-ivfsoverlay", Quote);
267 printArg(OS, CrashInfo->
VFSPath.str(), Quote);
275 llvm::sys::path::parent_path(CrashInfo->
VFSPath));
276 llvm::sys::path::append(RelModCacheDir,
"repro-modules");
278 std::string ModCachePath =
"-fmodules-cache-path=";
279 ModCachePath.append(RelModCacheDir.c_str());
282 printArg(OS, ModCachePath, Quote);
285 if (ResponseFile !=
nullptr) {
286 OS <<
"\n Arguments passed via response file:\n";
287 writeResponseFile(OS);
292 OS <<
" (end of response file)";
305 Environment.reserve(NewEnvironment.size() + 1);
306 Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
307 Environment.push_back(
nullptr);
311 bool *ExecutionFailed)
const {
315 if (Environment.empty()) {
318 assert(Environment.back() ==
nullptr &&
319 "Environment vector should be null-terminated by now");
320 Envp =
const_cast<const char **
>(Environment.data());
323 if (ResponseFile ==
nullptr) {
324 Argv.push_back(Executable);
325 Argv.append(Arguments.begin(), Arguments.end());
326 Argv.push_back(
nullptr);
328 return llvm::sys::ExecuteAndWait(
329 Executable, Argv.data(), Envp, Redirects, 0,
330 0, ErrMsg, ExecutionFailed);
335 std::string RespContents;
336 llvm::raw_string_ostream SS(RespContents);
339 writeResponseFile(SS);
340 buildArgvForResponseFile(Argv);
341 Argv.push_back(
nullptr);
345 if (std::error_code EC = writeFileWithEncoding(
348 *ErrMsg = EC.message();
350 *ExecutionFailed =
true;
354 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), Envp, Redirects,
356 0, ErrMsg, ExecutionFailed);
360 const char *Executable_,
361 const ArgStringList &Arguments_,
363 std::unique_ptr<Command> Fallback_)
364 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs),
365 Fallback(std::move(Fallback_)) {}
371 Fallback->Print(OS, Terminator, Quote, CrashInfo);
378 return ExitCode != 0;
382 bool *ExecutionFailed)
const {
385 return PrimaryStatus;
391 *ExecutionFailed =
false;
394 D.
Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
396 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
397 return SecondaryStatus;
401 const Tool &Creator_,
402 const char *Executable_,
403 const ArgStringList &Arguments_,
405 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
410 OS <<
" || (exit 0)" << Terminator;
415 bool *ExecutionFailed)
const {
419 *ExecutionFailed =
false;
425 for (
const auto &Job : *
this)
426 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.
void setEnvironment(llvm::ArrayRef< const char * > NewEnvironment)
Sets the environment to be used by the new process.
void setResponseFile(const char *FileName)
Set to pass arguments via a response file when launching the command.
int Execute(const StringRef **Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
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.
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Action - Represent an abstract compilation step to perform.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
virtual int Execute(const StringRef **Redirects, std::string *ErrMsg, bool *ExecutionFailed) 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
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
ForceSuccessCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs)
FallbackCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs, std::unique_ptr< Command > Fallback_)
int Execute(const StringRef **Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override