From a45409d8855a1e4538990507ef25e9b51c090193 Mon Sep 17 00:00:00 2001 From: Alexandre Ganea Date: Wed, 17 Jun 2020 18:33:18 -0400 Subject: [PATCH] [Clang] Move clang::Job::printArg to llvm::sys::printArg. NFCI. This patch is to support/simplify https://reviews.llvm.org/D80833 --- clang/include/clang/Driver/Job.h | 3 --- clang/lib/Driver/Driver.cpp | 2 +- clang/lib/Driver/Job.cpp | 32 +++++++------------------------- llvm/include/llvm/Support/Program.h | 3 +++ llvm/lib/Support/Program.cpp | 19 +++++++++++++++++++ 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/clang/include/clang/Driver/Job.h b/clang/include/clang/Driver/Job.h index 9a3cad2..1c6ea69 100644 --- a/clang/include/clang/Driver/Job.h +++ b/clang/include/clang/Driver/Job.h @@ -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; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index a8442d2..a48761a 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -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'; } diff --git a/clang/lib/Driver/Job.cpp b/clang/lib/Driver/Job.cpp index 6d1e7e6..274bda9 100644 --- a/clang/lib/Driver/Job.cpp +++ b/clang/lib/Driver/Job.cpp @@ -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 Args = Arguments; SmallVector 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 // .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) { diff --git a/llvm/include/llvm/Support/Program.h b/llvm/include/llvm/Support/Program.h index ff4777e..ce0b6db 100644 --- a/llvm/include/llvm/Support/Program.h +++ b/llvm/include/llvm/Support/Program.h @@ -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 diff --git a/llvm/lib/Support/Program.cpp b/llvm/lib/Support/Program.cpp index d90cb45..5294f65 100644 --- a/llvm/lib/Support/Program.cpp +++ b/llvm/lib/Support/Program.cpp @@ -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 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" -- 2.7.4