From d9a8ccc22673c80adc9256bbf9c25ae6cd7d3c6c Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 9 Jan 2017 01:47:15 +0000 Subject: [PATCH] Define sys::path::convert_to_slash This patch moves convertToUnixPathSeparator from LLD to LLVM. Differential Revision: https://reviews.llvm.org/D28444 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291414 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Path.h | 8 ++++++++ lib/Support/Path.cpp | 10 ++++++++++ lib/Support/TarWriter.cpp | 12 ++---------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/include/llvm/Support/Path.h b/include/llvm/Support/Path.h index 0513350d446..2bbcef0c293 100644 --- a/include/llvm/Support/Path.h +++ b/include/llvm/Support/Path.h @@ -207,6 +207,14 @@ void native(const Twine &path, SmallVectorImpl &result); /// @param path A path that is transformed to native format. void native(SmallVectorImpl &path); +/// @brief Replaces backslashes with slashes if Windows. +/// +/// @param path processed path +/// @result The result of replacing backslashes with forward slashes if Windows. +/// On Unix, this function is a no-op because backslashes are valid path +/// chracters. +std::string convert_to_slash(StringRef path); + /// @} /// @name Lexical Observers /// @{ diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 0616d05aff5..4bb035eeccc 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -571,6 +571,16 @@ void native(SmallVectorImpl &Path) { #endif } +std::string convert_to_slash(StringRef path) { +#ifdef LLVM_ON_WIN32 + std::string s = path.str(); + std::replace(s.begin(), s.end(), '\\', '/'); + return s; +#else + return path; +#endif +} + StringRef filename(StringRef path) { return *rbegin(path); } diff --git a/lib/Support/TarWriter.cpp b/lib/Support/TarWriter.cpp index 57a4c8f4fc0..f79b364dc1f 100644 --- a/lib/Support/TarWriter.cpp +++ b/lib/Support/TarWriter.cpp @@ -26,6 +26,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/Path.h" using namespace llvm; @@ -147,15 +148,6 @@ static void writeUstarHeader(raw_fd_ostream &OS, StringRef Path, size_t Size) { OS << StringRef(reinterpret_cast(&Hdr), sizeof(Hdr)); } -// We want to use '/' as a path separator even on Windows. -// This function canonicalizes a given path. -static std::string canonicalize(std::string S) { -#ifdef LLVM_ON_WIN32 - std::replace(S.begin(), S.end(), '\\', '/'); -#endif - return S; -} - // Creates a TarWriter instance and returns it. Expected> TarWriter::create(StringRef OutputPath, StringRef BaseDir) { @@ -171,7 +163,7 @@ TarWriter::TarWriter(int FD, StringRef BaseDir) // Append a given file to an archive. void TarWriter::append(StringRef Path, StringRef Data) { // Write Path and Data. - std::string S = BaseDir + "/" + canonicalize(Path) + "\0"; + std::string S = BaseDir + "/" + sys::path::convert_to_slash(Path) + "\0"; if (fitsInUstar(S)) { writeUstarHeader(OS, S, Data.size()); } else {