16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/SmallVector.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" 30 #include <system_error> 33 using namespace clang;
34 using namespace driver;
37 const char *Executable,
38 const llvm::opt::ArgStringList &Arguments,
40 : Source(Source), Creator(Creator), Executable(Executable),
41 Arguments(Arguments) {
42 for (
const auto &II : Inputs)
44 InputFilenames.push_back(II.getFilename());
50 static bool skipArgs(
const char *Flag,
bool HaveCrashVFS,
int &SkipNum,
55 bool ShouldSkip = llvm::StringSwitch<bool>(Flag)
56 .Cases(
"-MF",
"-MT",
"-MQ",
"-serialize-diagnostic-file",
true)
57 .Cases(
"-o",
"-dependency-file",
true)
58 .Cases(
"-fdebug-compilation-dir",
"-diagnostic-log-file",
true)
59 .Cases(
"-dwarf-debug-flags",
"-ivfsoverlay",
true)
65 IsInclude = llvm::StringSwitch<bool>(Flag)
66 .Cases(
"-include",
"-header-include-file",
true)
67 .Cases(
"-idirafter",
"-internal-isystem",
"-iwithprefix",
true)
68 .Cases(
"-internal-externc-isystem",
"-iprefix",
true)
69 .Cases(
"-iwithprefixbefore",
"-isystem",
"-iquote",
true)
70 .Cases(
"-isysroot",
"-I",
"-F",
"-resource-dir",
true)
71 .Cases(
"-iframework",
"-include-pch",
true)
79 ShouldSkip = llvm::StringSwitch<bool>(Flag)
80 .Cases(
"-M",
"-MM",
"-MG",
"-MP",
"-MD",
true)
90 StringRef FlagRef(Flag);
91 IsInclude = FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I");
94 if (FlagRef.startswith(
"-fmodules-cache-path="))
102 const bool Escape = Arg.find_first_of(
" \"\\$") != StringRef::npos;
104 if (!Quote && !Escape) {
111 for (
const auto c : Arg) {
112 if (c ==
'"' || c ==
'\\' || c ==
'$')
119 void Command::writeResponseFile(raw_ostream &OS)
const {
122 for (
const auto *Arg : InputFileList) {
131 for (
const auto *Arg : Arguments) {
134 for (; *Arg !=
'\0'; Arg++) {
135 if (*Arg ==
'\"' || *Arg ==
'\\') {
145 void Command::buildArgvForResponseFile(
151 Out.push_back(Executable);
152 Out.push_back(ResponseFileFlag.c_str());
156 llvm::StringSet<> Inputs;
157 for (
const auto *InputName : InputFileList)
158 Inputs.insert(InputName);
159 Out.push_back(Executable);
162 bool FirstInput =
true;
163 for (
const auto *Arg : Arguments) {
164 if (Inputs.count(Arg) == 0) {
166 }
else if (FirstInput) {
169 Out.push_back(ResponseFile);
179 using namespace llvm;
183 if (path::is_absolute(InInc))
185 std::error_code EC = fs::current_path(OutInc);
188 path::append(OutInc, InInc);
194 StringRef FlagRef(Args[Idx + NumArgs - 1]);
195 assert((FlagRef.startswith(
"-F") || FlagRef.startswith(
"-I")) &&
196 "Expecting -I or -F");
197 StringRef Inc = FlagRef.slice(2, StringRef::npos);
198 if (getAbsPath(Inc, NewInc)) {
201 IncFlags.push_back(std::move(NewArg));
206 assert(NumArgs == 2 &&
"Not expecting more than two arguments");
207 StringRef Inc(Args[Idx + NumArgs - 1]);
208 if (!getAbsPath(Inc, NewInc))
211 IncFlags.push_back(std::move(NewInc));
222 if (ResponseFile !=
nullptr) {
223 buildArgvForResponseFile(ArgsRespFile);
227 bool HaveCrashVFS = CrashInfo && !CrashInfo->
VFSPath.empty();
228 for (
size_t i = 0, e = Args.size(); i < e; ++i) {
229 const char *
const Arg = Args[i];
233 bool IsInclude =
false;
234 if (
skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
240 if (HaveCrashVFS && IsInclude) {
243 if (!NewIncFlags.empty()) {
244 for (
auto &F : NewIncFlags) {
253 auto Found = llvm::find_if(InputFilenames,
254 [&Arg](StringRef IF) {
return IF == Arg; });
255 if (Found != InputFilenames.end() &&
256 (i == 0 || StringRef(Args[i - 1]) !=
"-main-file-name")) {
259 StringRef ShortName = llvm::sys::path::filename(CrashInfo->
Filename);
260 printArg(OS, ShortName.str(), Quote);
269 if (CrashInfo && HaveCrashVFS) {
271 printArg(OS,
"-ivfsoverlay", Quote);
281 llvm::sys::path::parent_path(CrashInfo->
VFSPath));
282 llvm::sys::path::append(RelModCacheDir,
"repro-modules");
284 std::string ModCachePath =
"-fmodules-cache-path=";
285 ModCachePath.append(RelModCacheDir.c_str());
291 if (ResponseFile !=
nullptr) {
292 OS <<
"\n Arguments passed via response file:\n";
293 writeResponseFile(OS);
298 OS <<
" (end of response file)";
305 ResponseFile = FileName;
306 ResponseFileFlag = Creator.getResponseFileFlag();
307 ResponseFileFlag += FileName;
311 Environment.reserve(NewEnvironment.size() + 1);
312 Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
313 Environment.push_back(
nullptr);
317 std::string *ErrMsg,
bool *ExecutionFailed)
const {
318 if (PrintInputFilenames) {
319 for (
const char *Arg : InputFilenames)
320 llvm::outs() << llvm::sys::path::filename(Arg) <<
"\n";
321 llvm::outs().flush();
327 std::vector<StringRef> ArgvVectorStorage;
328 if (!Environment.empty()) {
329 assert(Environment.back() ==
nullptr &&
330 "Environment vector should be null-terminated by now");
331 ArgvVectorStorage = llvm::toStringRefArray(Environment.data());
332 Env = makeArrayRef(ArgvVectorStorage);
335 if (ResponseFile ==
nullptr) {
336 Argv.push_back(Executable);
337 Argv.append(Arguments.begin(), Arguments.end());
338 Argv.push_back(
nullptr);
340 auto Args = llvm::toStringRefArray(Argv.data());
341 return llvm::sys::ExecuteAndWait(
342 Executable, Args, Env, Redirects, 0,
343 0, ErrMsg, ExecutionFailed);
348 std::string RespContents;
349 llvm::raw_string_ostream SS(RespContents);
352 writeResponseFile(SS);
353 buildArgvForResponseFile(Argv);
354 Argv.push_back(
nullptr);
358 if (std::error_code EC = writeFileWithEncoding(
359 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
361 *ErrMsg = EC.message();
363 *ExecutionFailed =
true;
367 auto Args = llvm::toStringRefArray(Argv.data());
368 return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
370 0, ErrMsg, ExecutionFailed);
374 const char *Executable_,
375 const llvm::opt::ArgStringList &Arguments_,
377 std::unique_ptr<Command> Fallback_)
378 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs),
379 Fallback(
std::move(Fallback_)) {}
385 Fallback->Print(OS, Terminator, Quote, CrashInfo);
392 return ExitCode != 0;
396 std::string *ErrMsg,
bool *ExecutionFailed)
const {
399 return PrimaryStatus;
405 *ExecutionFailed =
false;
408 D.
Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
410 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
411 return SecondaryStatus;
415 const Action &Source_,
const Tool &Creator_,
const char *Executable_,
417 :
Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
422 OS <<
" || (exit 0)" << Terminator;
427 bool *ExecutionFailed)
const {
431 *ExecutionFailed =
false;
437 for (
const auto &Job : *
this)
438 Job.Print(OS, Terminator, Quote, CrashInfo);
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
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