[Clang] Move clang::Job::printArg to llvm::sys::printArg. NFCI.

This patch is to support/simplify https://reviews.llvm.org/D80833
This commit is contained in:
Alexandre Ganea 2020-06-17 18:33:18 -04:00
parent 92d8ad02e9
commit a45409d885
5 changed files with 30 additions and 29 deletions

View File

@ -128,9 +128,6 @@ public:
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);
protected:
/// Optionally print the filenames to be compiled
void PrintFileNames() const;

View File

@ -1167,7 +1167,7 @@ static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) {
for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) {
if (I != ASL.begin())
OS << ' ';
Command::printArg(OS, *I, true);
llvm::sys::printArg(OS, *I, true);
}
OS << '\n';
}

View File

@ -100,24 +100,6 @@ static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
return false;
}
void Command::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
if (!Quote && !Escape) {
OS << Arg;
return;
}
// Quote and escape. This isn't really complete, but good enough.
OS << '"';
for (const auto c : Arg) {
if (c == '"' || c == '\\' || c == '$')
OS << '\\';
OS << c;
}
OS << '"';
}
void Command::writeResponseFile(raw_ostream &OS) const {
// In a file list, we only write the set of inputs to the response file
if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
@ -217,7 +199,7 @@ void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
CrashReportInfo *CrashInfo) const {
// Always quote the exe.
OS << ' ';
printArg(OS, Executable, /*Quote=*/true);
llvm::sys::printArg(OS, Executable, /*Quote=*/true);
ArrayRef<const char *> Args = Arguments;
SmallVector<const char *, 128> ArgsRespFile;
@ -245,7 +227,7 @@ void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
if (!NewIncFlags.empty()) {
for (auto &F : NewIncFlags) {
OS << ' ';
printArg(OS, F.c_str(), Quote);
llvm::sys::printArg(OS, F.c_str(), Quote);
}
i += NumArgs - 1;
continue;
@ -259,20 +241,20 @@ void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
// 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);
llvm::sys::printArg(OS, ShortName.str(), Quote);
continue;
}
}
OS << ' ';
printArg(OS, Arg, Quote);
llvm::sys::printArg(OS, Arg, Quote);
}
if (CrashInfo && HaveCrashVFS) {
OS << ' ';
printArg(OS, "-ivfsoverlay", Quote);
llvm::sys::printArg(OS, "-ivfsoverlay", Quote);
OS << ' ';
printArg(OS, CrashInfo->VFSPath.str(), Quote);
llvm::sys::printArg(OS, CrashInfo->VFSPath.str(), Quote);
// The leftover modules from the crash are stored in
// <name>.cache/vfs/modules
@ -287,7 +269,7 @@ void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
ModCachePath.append(RelModCacheDir.c_str());
OS << ' ';
printArg(OS, ModCachePath, Quote);
llvm::sys::printArg(OS, ModCachePath, Quote);
}
if (ResponseFile != nullptr) {

View File

@ -210,6 +210,9 @@ namespace sys {
/// stored.
);
/// Print a command argument, and optionally quote it.
void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
#if defined(_WIN32)
/// Given a list of command line arguments, quote and escape them as necessary
/// to build a single flat command line appropriate for calling CreateProcess

View File

@ -13,6 +13,7 @@
#include "llvm/Support/Program.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>
using namespace llvm;
using namespace sys;
@ -75,6 +76,24 @@ bool sys::commandLineFitsWithinSystemLimits(StringRef Program,
return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
}
void sys::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
if (!Quote && !Escape) {
OS << Arg;
return;
}
// Quote and escape. This isn't really complete, but good enough.
OS << '"';
for (const auto c : Arg) {
if (c == '"' || c == '\\' || c == '$')
OS << '\\';
OS << c;
}
OS << '"';
}
// Include the platform-specific parts of this class.
#ifdef LLVM_ON_UNIX
#include "Unix/Program.inc"