17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/SmallVector.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" 31 #include <system_error> 34 using namespace clang;
35 using namespace driver;
38 const char *Executable,
39 const llvm::opt::ArgStringList &Arguments,
41 : Source(Source), Creator(Creator), Executable(Executable),
42 Arguments(Arguments) {
43 for (
const auto &II : Inputs)
45 InputFilenames.push_back(II.getFilename());
51 static bool skipArgs(
const char *Flag,
bool HaveCrashVFS,
int &SkipNum,
56 bool ShouldSkip = llvm::StringSwitch<bool>(Flag)
57 .Cases(
"-MF",
"-MT",
"-MQ",
"-serialize-diagnostic-file",
true)
58 .Cases(
"-o",
"-dependency-file",
true)
59 .Cases(
"-fdebug-compilation-dir",
"-diagnostic-log-file",
true)
60 .Cases(
"-dwarf-debug-flags",
"-ivfsoverlay",
true)
66 IsInclude = llvm::StringSwitch<bool>(Flag)
67 .Cases(
"-include",
"-header-include-file",
true)
68 .Cases(
"-idirafter",
"-internal-isystem",
"-iwithprefix",
true)
69 .Cases(
"-internal-externc-isystem",
"-iprefix",
true)
70 .Cases(
"-iwithprefixbefore",
"-isystem",
"-iquote",
true)
71 .Cases(
"-isysroot",
"-I",
"-F",
"-resource-dir",
true)
72 .Cases(
"-iframework",
"-include-pch",
true)
80 ShouldSkip = llvm::StringSwitch<bool>(Flag)
81 .Cases(
"-M",
"-MM",
"-MG",
"-MP",
"-MD",
true)
91 StringRef FlagRef(Flag);
92 IsInclude = FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I");
95 if (FlagRef.startswith(
"-fmodules-cache-path="))
103 const bool Escape = Arg.find_first_of(
"\"\\$") != StringRef::npos;
105 if (!Quote && !Escape) {
112 for (
const auto c : Arg) {
113 if (c ==
'"' || c ==
'\\' || c ==
'$')
120 void Command::writeResponseFile(raw_ostream &OS)
const {
123 for (
const auto *Arg : InputFileList) {
132 for (
const auto *Arg : Arguments) {
135 for (; *Arg !=
'\0'; Arg++) {
136 if (*Arg ==
'\"' || *Arg ==
'\\') {
146 void Command::buildArgvForResponseFile(
152 Out.push_back(Executable);
153 Out.push_back(ResponseFileFlag.c_str());
157 llvm::StringSet<> Inputs;
158 for (
const auto *InputName : InputFileList)
159 Inputs.insert(InputName);
160 Out.push_back(Executable);
163 bool FirstInput =
true;
164 for (
const auto *Arg : Arguments) {
165 if (Inputs.count(Arg) == 0) {
167 }
else if (FirstInput) {
170 Out.push_back(ResponseFile);
180 using namespace llvm;
184 if (path::is_absolute(InInc))
186 std::error_code EC = fs::current_path(OutInc);
189 path::append(OutInc, InInc);
195 StringRef FlagRef(Args[Idx + NumArgs - 1]);
196 assert((FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I")) &&
197 "Expecting -I or -F");
198 StringRef Inc = FlagRef.slice(2, StringRef::npos);
199 if (getAbsPath(Inc, NewInc)) {
202 IncFlags.push_back(std::move(NewArg));
207 assert(NumArgs == 2 &&
"Not expecting more than two arguments");
208 StringRef Inc(Args[Idx + NumArgs - 1]);
209 if (!getAbsPath(Inc, NewInc))
212 IncFlags.push_back(std::move(NewInc));
223 if (ResponseFile !=
nullptr) {
224 buildArgvForResponseFile(ArgsRespFile);
228 bool HaveCrashVFS = CrashInfo && !CrashInfo->
VFSPath.empty();
229 for (
size_t i = 0, e = Args.size(); i < e; ++i) {
230 const char *
const Arg = Args[i];
234 bool IsInclude =
false;
235 if (
skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
241 if (HaveCrashVFS && IsInclude) {
244 if (!NewIncFlags.empty()) {
245 for (
auto &F : NewIncFlags) {
254 auto Found = std::find_if(InputFilenames.begin(), InputFilenames.end(),
255 [&Arg](StringRef IF) {
return IF == Arg; });
256 if (Found != InputFilenames.end() &&
257 (i == 0 || StringRef(Args[i - 1]) !=
"-main-file-name")) {
260 StringRef ShortName = llvm::sys::path::filename(CrashInfo->
Filename);
261 printArg(OS, ShortName.str(), Quote);
270 if (CrashInfo && HaveCrashVFS) {
272 printArg(OS,
"-ivfsoverlay", Quote);
282 llvm::sys::path::parent_path(CrashInfo->
VFSPath));
283 llvm::sys::path::append(RelModCacheDir,
"repro-modules");
285 std::string ModCachePath =
"-fmodules-cache-path=";
286 ModCachePath.append(RelModCacheDir.c_str());
292 if (ResponseFile !=
nullptr) {
293 OS <<
"\n Arguments passed via response file:\n";
294 writeResponseFile(OS);
299 OS <<
" (end of response file)";
306 ResponseFile = FileName;
307 ResponseFileFlag = Creator.getResponseFileFlag();
308 ResponseFileFlag += FileName;
312 Environment.reserve(NewEnvironment.size() + 1);
313 Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
314 Environment.push_back(
nullptr);
318 std::string *ErrMsg,
bool *ExecutionFailed)
const {
319 if (PrintInputFilenames) {
320 for (
const char *Arg : InputFilenames)
321 llvm::outs() << llvm::sys::path::filename(Arg) <<
"\n";
322 llvm::outs().flush();
328 std::vector<StringRef> ArgvVectorStorage;
329 if (!Environment.empty()) {
330 assert(Environment.back() ==
nullptr &&
331 "Environment vector should be null-terminated by now");
332 ArgvVectorStorage = llvm::toStringRefArray(Environment.data());
333 Env = makeArrayRef(ArgvVectorStorage);
336 if (ResponseFile ==
nullptr) {
337 Argv.push_back(Executable);
338 Argv.append(Arguments.begin(), Arguments.end());
339 Argv.push_back(
nullptr);
341 auto Args = llvm::toStringRefArray(Argv.data());
342 return llvm::sys::ExecuteAndWait(
343 Executable, Args, Env, Redirects, 0,
344 0, ErrMsg, ExecutionFailed);
349 std::string RespContents;
350 llvm::raw_string_ostream SS(RespContents);
353 writeResponseFile(SS);
354 buildArgvForResponseFile(Argv);
355 Argv.push_back(
nullptr);
359 if (std::error_code EC = writeFileWithEncoding(
360 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
362 *ErrMsg = EC.message();
364 *ExecutionFailed =
true;
368 auto Args = llvm::toStringRefArray(Argv.data());
369 return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
371 0, ErrMsg, ExecutionFailed);
375 const char *Executable_,
376 const llvm::opt::ArgStringList &Arguments_,
378 std::unique_ptr<Command> Fallback_)
379 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs),
380 Fallback(
std::move(Fallback_)) {}
386 Fallback->Print(OS, Terminator, Quote, CrashInfo);
393 return ExitCode != 0;
397 std::string *ErrMsg,
bool *ExecutionFailed)
const {
400 return PrimaryStatus;
406 *ExecutionFailed =
false;
409 D.
Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
411 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
412 return SecondaryStatus;
416 const Action &Source_,
const Tool &Creator_,
const char *Executable_,
418 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
423 OS <<
" || (exit 0)" << Terminator;
428 bool *ExecutionFailed)
const {
432 *ExecutionFailed =
false;
438 for (
const auto &Job : *
this)
439 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.
Command(const Action &Source, const Tool &Creator, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef< InputInfo > Inputs)
Action - Represent an abstract compilation step to perform.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
FallbackCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const llvm::opt::ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs, std::unique_ptr< Command > Fallback_)
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 - 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
Dataflow Directional Tag Classes.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
ForceSuccessCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const llvm::opt::ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs)
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
virtual int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const