diff options
author | Martin Storsjö <martin@martin.st> | 2021-10-04 17:17:00 +0300 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2021-11-05 10:41:51 +0200 |
commit | a8b54834a186f5570b49b614e31b961a9cf1cbfe (patch) | |
tree | cf9f9891831cdbffdc969e5c657c0a4a69a614b2 /llvm/unittests/Support/Path.cpp | |
parent | f95bd18b5faa6a5af4b5786312c373c5b2dce687 (diff) | |
download | llvm-a8b54834a186f5570b49b614e31b961a9cf1cbfe.zip llvm-a8b54834a186f5570b49b614e31b961a9cf1cbfe.tar.gz llvm-a8b54834a186f5570b49b614e31b961a9cf1cbfe.tar.bz2 |
[Support] Add a new path style for Windows with forward slashes
This behaves just like the regular Windows style, with both separator
forms accepted, but with get_separator() returning forward slashes.
Add a more descriptive name for the existing style, keeping the old
name around as an alias initially.
Add a new function `make_preferred()` (like the C++17
`std::filesystem::path` function with the same name), which converts
windows paths to the preferred separator form (while this one works on
any platform and takes a `path::Style` argument).
Contrary to `native()` (just like `make_preferred()` in `std::filesystem`),
this doesn't do anything at all on Posix, it doesn't try to reinterpret
backslashes into forward slashes there.
Differential Revision: https://reviews.llvm.org/D111879
Diffstat (limited to 'llvm/unittests/Support/Path.cpp')
-rw-r--r-- | llvm/unittests/Support/Path.cpp | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index 927b7eb..cbde1f1 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -75,7 +75,9 @@ TEST(is_style_Style, Works) { // Check platform-independent results. EXPECT_TRUE(is_style_posix(Style::posix)); EXPECT_TRUE(is_style_windows(Style::windows)); + EXPECT_TRUE(is_style_windows(Style::windows_slash)); EXPECT_FALSE(is_style_posix(Style::windows)); + EXPECT_FALSE(is_style_posix(Style::windows_slash)); EXPECT_FALSE(is_style_windows(Style::posix)); // Check platform-dependent results. @@ -95,12 +97,19 @@ TEST(is_separator, Works) { EXPECT_FALSE(path::is_separator(' ')); EXPECT_TRUE(path::is_separator('\\', path::Style::windows)); + EXPECT_TRUE(path::is_separator('\\', path::Style::windows_slash)); EXPECT_FALSE(path::is_separator('\\', path::Style::posix)); EXPECT_EQ(path::is_style_windows(path::Style::native), path::is_separator('\\')); } +TEST(get_separator, Works) { + EXPECT_EQ(path::get_separator(path::Style::posix), "/"); + EXPECT_EQ(path::get_separator(path::Style::windows_backslash), "\\"); + EXPECT_EQ(path::get_separator(path::Style::windows_slash), "/"); +} + TEST(is_absolute_gnu, Works) { // Test tuple <Path, ExpectedPosixValue, ExpectedWindowsValue>. const std::tuple<StringRef, bool, bool> Paths[] = { @@ -383,6 +392,8 @@ TEST(Support, PathIterator) { testing::ElementsAre("/", ".c", ".d", "..", ".")); EXPECT_THAT(GetComponents("c:\\c\\e\\foo.txt", path::Style::windows), testing::ElementsAre("c:", "\\", "c", "e", "foo.txt")); + EXPECT_THAT(GetComponents("c:\\c\\e\\foo.txt", path::Style::windows_slash), + testing::ElementsAre("c:", "\\", "c", "e", "foo.txt")); EXPECT_THAT(GetComponents("//net/"), testing::ElementsAre("//net", "/")); EXPECT_THAT(GetComponents("//net/c/foo.txt"), testing::ElementsAre("//net", "/", "c", "foo.txt")); @@ -1425,10 +1436,25 @@ TEST(Support, NormalizePath) { for (auto &T : Tests) { SmallString<64> Win(std::get<0>(T)); SmallString<64> Posix(Win); + SmallString<64> WinSlash(Win); path::native(Win, path::Style::windows); path::native(Posix, path::Style::posix); + path::native(WinSlash, path::Style::windows_slash); EXPECT_EQ(std::get<1>(T), Win); EXPECT_EQ(std::get<2>(T), Posix); + EXPECT_EQ(std::get<2>(T), WinSlash); + } + + for (auto &T : Tests) { + SmallString<64> WinBackslash(std::get<0>(T)); + SmallString<64> Posix(WinBackslash); + SmallString<64> WinSlash(WinBackslash); + path::make_preferred(WinBackslash, path::Style::windows_backslash); + path::make_preferred(Posix, path::Style::posix); + path::make_preferred(WinSlash, path::Style::windows_slash); + EXPECT_EQ(std::get<1>(T), WinBackslash); + EXPECT_EQ(std::get<0>(T), Posix); // Posix remains unchanged here + EXPECT_EQ(std::get<2>(T), WinSlash); } #if defined(_WIN32) @@ -1437,10 +1463,15 @@ TEST(Support, NormalizePath) { const char *Path7a = "~/aaa"; SmallString<64> Path7(Path7a); - path::native(Path7); + path::native(Path7, path::Style::windows_backslash); EXPECT_TRUE(Path7.endswith("\\aaa")); EXPECT_TRUE(Path7.startswith(PathHome)); EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1)); + Path7 = Path7a; + path::native(Path7, path::Style::windows_slash); + EXPECT_TRUE(Path7.endswith("/aaa")); + EXPECT_TRUE(Path7.startswith(PathHome)); + EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1)); const char *Path8a = "~"; SmallString<64> Path8(Path8a); @@ -1454,7 +1485,7 @@ TEST(Support, NormalizePath) { const char *Path10a = "aaa/~/b"; SmallString<64> Path10(Path10a); - path::native(Path10); + path::native(Path10, path::Style::windows_backslash); EXPECT_EQ(Path10, "aaa\\~\\b"); #endif } |