diff --git a/clang/include/clang/Driver/Job.h b/clang/include/clang/Driver/Job.h --- a/clang/include/clang/Driver/Job.h +++ b/clang/include/clang/Driver/Job.h @@ -86,6 +86,11 @@ void writeResponseFile(raw_ostream &OS) const; public: + enum class PrintingMode { + AlwaysQuote, + QuoteIfNeeded, + }; + Command(const Action &Source, const Tool &Creator, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef Inputs); @@ -94,7 +99,8 @@ Command(const Command &) = default; virtual ~Command() = default; - virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, + virtual void Print(llvm::raw_ostream &OS, const char *Terminator, + PrintingMode Mode, CrashReportInfo *CrashInfo = nullptr) const; virtual int Execute(ArrayRef> Redirects, @@ -126,7 +132,7 @@ const llvm::opt::ArgStringList &getArguments() const { return Arguments; } /// Print a command argument, and optionally quote it. - static void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote); + static void printArg(llvm::raw_ostream &OS, StringRef Arg, PrintingMode Mode); /// Set whether to print the input filenames when executing. void setPrintInputFilenames(bool P) { PrintInputFilenames = P; } @@ -142,7 +148,7 @@ ArrayRef Inputs, std::unique_ptr Fallback_); - void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, + void Print(llvm::raw_ostream &OS, const char *Terminator, PrintingMode Mode, CrashReportInfo *CrashInfo = nullptr) const override; int Execute(ArrayRef> Redirects, std::string *ErrMsg, @@ -160,7 +166,7 @@ const llvm::opt::ArgStringList &Arguments_, ArrayRef Inputs); - void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, + void Print(llvm::raw_ostream &OS, const char *Terminator, PrintingMode Mode, CrashReportInfo *CrashInfo = nullptr) const override; int Execute(ArrayRef> Redirects, std::string *ErrMsg, @@ -180,7 +186,8 @@ public: void Print(llvm::raw_ostream &OS, const char *Terminator, - bool Quote, CrashReportInfo *CrashInfo = nullptr) const; + Command::PrintingMode Mode, + CrashReportInfo *CrashInfo = nullptr) const; /// Add a job to the list (taking ownership). void addJob(std::unique_ptr J) { Jobs.push_back(std::move(J)); } diff --git a/clang/lib/Driver/Compilation.cpp b/clang/lib/Driver/Compilation.cpp --- a/clang/lib/Driver/Compilation.cpp +++ b/clang/lib/Driver/Compilation.cpp @@ -174,7 +174,9 @@ if (getDriver().CCPrintOptions) *OS << "[Logging clang options]"; - C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions); + C.Print(*OS, "\n", + getDriver().CCPrintOptions ? Command::PrintingMode::AlwaysQuote + : Command::PrintingMode::QuoteIfNeeded); } std::string Error; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1141,7 +1141,7 @@ for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) { if (I != ASL.begin()) OS << ' '; - Command::printArg(OS, *I, true); + Command::printArg(OS, *I, Command::PrintingMode::AlwaysQuote); } OS << '\n'; } @@ -1392,8 +1392,8 @@ << "# Driver args: "; printArgList(ScriptOS, C.getInputArgs()); ScriptOS << "# Original command: "; - Cmd.Print(ScriptOS, "\n", /*Quote=*/true); - Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo); + Cmd.Print(ScriptOS, "\n", Command::PrintingMode::AlwaysQuote); + Cmd.Print(ScriptOS, "\n", Command::PrintingMode::AlwaysQuote, &CrashInfo); if (!AdditionalInformation.empty()) ScriptOS << "\n# Additional information: " << AdditionalInformation << "\n"; @@ -1447,7 +1447,7 @@ SmallVectorImpl> &FailingCommands) { // Just print if -### was present. if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { - C.getJobs().Print(llvm::errs(), "\n", true); + C.getJobs().Print(llvm::errs(), "\n", Command::PrintingMode::AlwaysQuote); return 0; } diff --git a/clang/lib/Driver/Job.cpp b/clang/lib/Driver/Job.cpp --- a/clang/lib/Driver/Job.cpp +++ b/clang/lib/Driver/Job.cpp @@ -98,10 +98,10 @@ return false; } -void Command::printArg(raw_ostream &OS, StringRef Arg, bool Quote) { +void Command::printArg(raw_ostream &OS, StringRef Arg, PrintingMode Mode) { const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos; - if (!Quote && !Escape) { + if (Mode == PrintingMode::QuoteIfNeeded && !Escape) { OS << Arg; return; } @@ -211,11 +211,11 @@ IncFlags.push_back(std::move(NewInc)); } -void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote, +void Command::Print(raw_ostream &OS, const char *Terminator, PrintingMode Mode, CrashReportInfo *CrashInfo) const { // Always quote the exe. OS << ' '; - printArg(OS, Executable, /*Quote=*/true); + printArg(OS, Executable, PrintingMode::AlwaysQuote); ArrayRef Args = Arguments; SmallVector ArgsRespFile; @@ -243,7 +243,7 @@ if (!NewIncFlags.empty()) { for (auto &F : NewIncFlags) { OS << ' '; - printArg(OS, F.c_str(), Quote); + printArg(OS, F.c_str(), Mode); } i += NumArgs - 1; continue; @@ -257,20 +257,20 @@ // Replace the input file name with the crashinfo's file name. OS << ' '; StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename); - printArg(OS, ShortName.str(), Quote); + printArg(OS, ShortName.str(), Mode); continue; } } OS << ' '; - printArg(OS, Arg, Quote); + printArg(OS, Arg, Mode); } if (CrashInfo && HaveCrashVFS) { OS << ' '; - printArg(OS, "-ivfsoverlay", Quote); + printArg(OS, "-ivfsoverlay", Mode); OS << ' '; - printArg(OS, CrashInfo->VFSPath.str(), Quote); + printArg(OS, CrashInfo->VFSPath.str(), Mode); // The leftover modules from the crash are stored in // .cache/vfs/modules @@ -285,7 +285,7 @@ ModCachePath.append(RelModCacheDir.c_str()); OS << ' '; - printArg(OS, ModCachePath, Quote); + printArg(OS, ModCachePath, Mode); } if (ResponseFile != nullptr) { @@ -379,10 +379,11 @@ Fallback(std::move(Fallback_)) {} void FallbackCommand::Print(raw_ostream &OS, const char *Terminator, - bool Quote, CrashReportInfo *CrashInfo) const { - Command::Print(OS, "", Quote, CrashInfo); + PrintingMode Mode, + CrashReportInfo *CrashInfo) const { + Command::Print(OS, "", Mode, CrashInfo); OS << " ||"; - Fallback->Print(OS, Terminator, Quote, CrashInfo); + Fallback->Print(OS, Terminator, Mode, CrashInfo); } static bool ShouldFallback(int ExitCode) { @@ -417,8 +418,9 @@ : Command(Source_, Creator_, Executable_, Arguments_, Inputs) {} void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator, - bool Quote, CrashReportInfo *CrashInfo) const { - Command::Print(OS, "", Quote, CrashInfo); + PrintingMode Mode, + CrashReportInfo *CrashInfo) const { + Command::Print(OS, "", Mode, CrashInfo); OS << " || (exit 0)" << Terminator; } @@ -432,10 +434,11 @@ return 0; } -void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote, +void JobList::Print(raw_ostream &OS, const char *Terminator, + Command::PrintingMode Mode, CrashReportInfo *CrashInfo) const { for (const auto &Job : *this) - Job.Print(OS, Terminator, Quote, CrashInfo); + Job.Print(OS, Terminator, Mode, CrashInfo); } void JobList::clear() { Jobs.clear(); } diff --git a/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp b/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp --- a/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp +++ b/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp @@ -56,7 +56,8 @@ // Just print the cc1 options if -### was present. if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) { - C->getJobs().Print(llvm::errs(), "\n", true); + C->getJobs().Print(llvm::errs(), "\n", + driver::Command::PrintingMode::AlwaysQuote); return nullptr; } @@ -82,7 +83,7 @@ (Jobs.size() > 1 && !OffloadCompilation)) { SmallString<256> Msg; llvm::raw_svector_ostream OS(Msg); - Jobs.Print(OS, "; ", true); + Jobs.Print(OS, "; ", driver::Command::PrintingMode::AlwaysQuote); Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); return nullptr; } diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -94,7 +94,7 @@ if (Jobs.size() != 1 || !isa(*Jobs.begin())) { SmallString<256> error_msg; llvm::raw_svector_ostream error_stream(error_msg); - Jobs.Print(error_stream, "; ", true); + Jobs.Print(error_stream, "; ", driver::Command::PrintingMode::AlwaysQuote); Diagnostics->Report(diag::err_fe_expected_compiler_job) << error_stream.str(); return nullptr; @@ -337,7 +337,8 @@ // Show the invocation, with -v. if (Invocation->getHeaderSearchOpts().Verbose) { llvm::errs() << "clang Invocation:\n"; - Compilation->getJobs().Print(llvm::errs(), "\n", true); + Compilation->getJobs().Print(llvm::errs(), "\n", + driver::Command::PrintingMode::AlwaysQuote); llvm::errs() << "\n"; }