mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 20:51:52 +01:00
[Support/Path] sys::path::replace_path_prefix fix and simplifications
Added unit tests for 2 scenarios that were failing. Made replace_path_prefix back to 3 parameters instead of 5, simplifying the implementation. The other 2 were always used with the default value. This commit is intended to be the first of 3: 1) simplify/fix replace_path_prefix. 2) use it in the context of -fdebug-prefix-map and -fmacro-prefix-map (see D76869). 3) Make Windows version of replace_path_prefix insensitive to both case and separators (slash vs backslash). Differential Revision: https://reviews.llvm.org/D77223
This commit is contained in:
parent
7f5ee4136e
commit
4e03034619
@ -153,32 +153,23 @@ void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
|
|||||||
/// @code
|
/// @code
|
||||||
/// /foo, /old, /new => /foo
|
/// /foo, /old, /new => /foo
|
||||||
/// /old, /old, /new => /new
|
/// /old, /old, /new => /new
|
||||||
/// /old, /old/, /new, false => /old
|
/// /old, /old/, /new => /old
|
||||||
/// /old, /old/, /new, true => /new
|
|
||||||
/// /old/foo, /old, /new => /new/foo
|
/// /old/foo, /old, /new => /new/foo
|
||||||
/// /old/foo, /old/, /new => /new/foo
|
/// /old/foo, /old/, /new => /new/foo
|
||||||
/// /old/foo, /old/, /new/ => /new/foo
|
/// /old/foo, /old/, /new/ => /new/foo
|
||||||
/// /oldfoo, /old, /new => /oldfoo
|
/// /oldfoo, /old, /new => /oldfoo
|
||||||
/// /foo, <empty>, /new => /new/foo
|
/// /foo, <empty>, /new => /new/foo
|
||||||
/// /foo, <empty>, new => new/foo
|
/// /foo, <empty>, new => new/foo
|
||||||
/// /old/foo, /old, <empty>, false => /foo
|
/// /old/foo, /old, <empty> => /foo
|
||||||
/// /old/foo, /old, <empty>, true => foo
|
|
||||||
/// @endcode
|
/// @endcode
|
||||||
///
|
///
|
||||||
/// @param Path If \a Path starts with \a OldPrefix modify to instead
|
/// @param Path If \a Path starts with \a OldPrefix modify to instead
|
||||||
/// start with \a NewPrefix.
|
/// start with \a NewPrefix.
|
||||||
/// @param OldPrefix The path prefix to strip from \a Path. Any trailing
|
/// @param OldPrefix The path prefix to strip from \a Path.
|
||||||
/// path separator is ignored if strict is true.
|
|
||||||
/// @param NewPrefix The path prefix to replace \a NewPrefix with.
|
/// @param NewPrefix The path prefix to replace \a NewPrefix with.
|
||||||
/// @param style The path separator style
|
|
||||||
/// @param strict If strict is true, a directory separator following
|
|
||||||
/// \a OldPrefix will also be stripped. Otherwise, directory
|
|
||||||
/// separators will only be matched and stripped when present
|
|
||||||
/// in \a OldPrefix.
|
|
||||||
/// @result true if \a Path begins with OldPrefix
|
/// @result true if \a Path begins with OldPrefix
|
||||||
bool replace_path_prefix(SmallVectorImpl<char> &Path,
|
bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix,
|
||||||
const StringRef &OldPrefix, const StringRef &NewPrefix,
|
StringRef NewPrefix);
|
||||||
Style style = Style::native, bool strict = false);
|
|
||||||
|
|
||||||
/// Append to path.
|
/// Append to path.
|
||||||
///
|
///
|
||||||
|
@ -496,49 +496,25 @@ void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
|
|||||||
path.append(ext.begin(), ext.end());
|
path.append(ext.begin(), ext.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool replace_path_prefix(SmallVectorImpl<char> &Path,
|
bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix,
|
||||||
const StringRef &OldPrefix, const StringRef &NewPrefix,
|
StringRef NewPrefix) {
|
||||||
Style style, bool strict) {
|
|
||||||
if (OldPrefix.empty() && NewPrefix.empty())
|
if (OldPrefix.empty() && NewPrefix.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
StringRef OrigPath(Path.begin(), Path.size());
|
StringRef OrigPath(Path.begin(), Path.size());
|
||||||
StringRef OldPrefixDir;
|
if (!OrigPath.startswith(OldPrefix))
|
||||||
|
|
||||||
if (!strict && OldPrefix.size() > OrigPath.size())
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Ensure OldPrefixDir does not have a trailing separator.
|
|
||||||
if (!OldPrefix.empty() && is_separator(OldPrefix.back()))
|
|
||||||
OldPrefixDir = parent_path(OldPrefix, style);
|
|
||||||
else
|
|
||||||
OldPrefixDir = OldPrefix;
|
|
||||||
|
|
||||||
if (!OrigPath.startswith(OldPrefixDir))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (OrigPath.size() > OldPrefixDir.size())
|
|
||||||
if (!is_separator(OrigPath[OldPrefixDir.size()], style) && strict)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// If prefixes have the same size we can simply copy the new one over.
|
// If prefixes have the same size we can simply copy the new one over.
|
||||||
if (OldPrefixDir.size() == NewPrefix.size() && !strict) {
|
if (OldPrefix.size() == NewPrefix.size()) {
|
||||||
llvm::copy(NewPrefix, Path.begin());
|
llvm::copy(NewPrefix, Path.begin());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringRef RelPath = OrigPath.substr(OldPrefixDir.size());
|
StringRef RelPath = OrigPath.substr(OldPrefix.size());
|
||||||
SmallString<256> NewPath;
|
SmallString<256> NewPath;
|
||||||
path::append(NewPath, style, NewPrefix);
|
(Twine(NewPrefix) + RelPath).toVector(NewPath);
|
||||||
if (!RelPath.empty()) {
|
|
||||||
if (!is_separator(RelPath[0], style) || !strict)
|
|
||||||
path::append(NewPath, style, RelPath);
|
|
||||||
else
|
|
||||||
path::append(NewPath, style, relative_path(RelPath, style));
|
|
||||||
}
|
|
||||||
|
|
||||||
Path.swap(NewPath);
|
Path.swap(NewPath);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1254,21 +1254,14 @@ TEST(Support, ReplacePathPrefix) {
|
|||||||
path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
|
path::replace_path_prefix(Path, OldPrefix, EmptyPrefix);
|
||||||
EXPECT_EQ(Path, "/foo");
|
EXPECT_EQ(Path, "/foo");
|
||||||
Path = Path2;
|
Path = Path2;
|
||||||
path::replace_path_prefix(Path, OldPrefix, EmptyPrefix, path::Style::native,
|
path::replace_path_prefix(Path, OldPrefixSep, EmptyPrefix);
|
||||||
true);
|
|
||||||
EXPECT_EQ(Path, "foo");
|
EXPECT_EQ(Path, "foo");
|
||||||
Path = Path3;
|
Path = Path3;
|
||||||
path::replace_path_prefix(Path, OldPrefix, NewPrefix, path::Style::native,
|
path::replace_path_prefix(Path, OldPrefix, NewPrefix);
|
||||||
false);
|
|
||||||
EXPECT_EQ(Path, "/newnew/foo");
|
EXPECT_EQ(Path, "/newnew/foo");
|
||||||
Path = Path3;
|
Path = Path3;
|
||||||
path::replace_path_prefix(Path, OldPrefix, NewPrefix, path::Style::native,
|
path::replace_path_prefix(Path, OldPrefix, NewPrefix2);
|
||||||
true);
|
EXPECT_EQ(Path, "/longernewnew/foo");
|
||||||
EXPECT_EQ(Path, "/oldnew/foo");
|
|
||||||
Path = Path3;
|
|
||||||
path::replace_path_prefix(Path, OldPrefixSep, NewPrefix, path::Style::native,
|
|
||||||
true);
|
|
||||||
EXPECT_EQ(Path, "/oldnew/foo");
|
|
||||||
Path = Path1;
|
Path = Path1;
|
||||||
path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
|
path::replace_path_prefix(Path, EmptyPrefix, NewPrefix);
|
||||||
EXPECT_EQ(Path, "/new/foo");
|
EXPECT_EQ(Path, "/new/foo");
|
||||||
@ -1279,13 +1272,8 @@ TEST(Support, ReplacePathPrefix) {
|
|||||||
path::replace_path_prefix(Path, OldPrefix, NewPrefix);
|
path::replace_path_prefix(Path, OldPrefix, NewPrefix);
|
||||||
EXPECT_EQ(Path, "/new/");
|
EXPECT_EQ(Path, "/new/");
|
||||||
Path = OldPrefix;
|
Path = OldPrefix;
|
||||||
path::replace_path_prefix(Path, OldPrefixSep, NewPrefix, path::Style::native,
|
path::replace_path_prefix(Path, OldPrefixSep, NewPrefix);
|
||||||
false);
|
|
||||||
EXPECT_EQ(Path, "/old");
|
EXPECT_EQ(Path, "/old");
|
||||||
Path = OldPrefix;
|
|
||||||
path::replace_path_prefix(Path, OldPrefixSep, NewPrefix, path::Style::native,
|
|
||||||
true);
|
|
||||||
EXPECT_EQ(Path, "/new");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FileSystemTest, OpenFileForRead) {
|
TEST_F(FileSystemTest, OpenFileForRead) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user