diff options
author | Zachary Turner <zturner@google.com> | 2017-03-16 22:28:04 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2017-03-16 22:28:04 +0000 |
commit | 5c5091fcb7813986e8a26437379c4d59451c1af7 (patch) | |
tree | e1a8c677184d51846ccf68dd0c627c4ffdf4e5e4 /llvm/unittests/Support/Path.cpp | |
parent | eb54909c8c7381fefdb9a0ebd457fee0ebd3088c (diff) | |
download | bcm5719-llvm-5c5091fcb7813986e8a26437379c4d59451c1af7.tar.gz bcm5719-llvm-5c5091fcb7813986e8a26437379c4d59451c1af7.zip |
[Support] Support both Windows and Posix paths on both platforms.
Previously which path syntax we supported dependend on what
platform we were compiling LLVM on. While this is normally
desirable, there are situations where we need to be able to
handle a path that we know was generated on a remote host.
Remote debugging, for example, or parsing debug info.
99% of the code in LLVM for handling paths was platform
agnostic and literally just a few branches were gated behind
pre-processor checks, so this changes those sites to use
runtime checks instead, and adds a flag to every path
API that allows one to override the host native syntax.
Differential Revision: https://reviews.llvm.org/D30858
llvm-svn: 298004
Diffstat (limited to 'llvm/unittests/Support/Path.cpp')
-rw-r--r-- | llvm/unittests/Support/Path.cpp | 155 |
1 files changed, 68 insertions, 87 deletions
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index 8068b320306..0e19fffa994 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -53,10 +53,13 @@ TEST(is_separator, Works) { EXPECT_FALSE(path::is_separator('-')); EXPECT_FALSE(path::is_separator(' ')); + EXPECT_TRUE(path::is_separator('\\', path::Style::windows)); + EXPECT_FALSE(path::is_separator('\\', path::Style::posix)); + #ifdef LLVM_ON_WIN32 EXPECT_TRUE(path::is_separator('\\')); #else - EXPECT_FALSE(path::is_separator('\\')); + EXPECT_FALSE(path::is_separator('\\', )); #endif } @@ -252,7 +255,6 @@ TEST(Support, AbsolutePathDotIterator) { } } -#ifdef LLVM_ON_WIN32 TEST(Support, AbsolutePathIteratorWin32) { SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); typedef SmallVector<StringRef, 4> PathComponents; @@ -265,8 +267,9 @@ TEST(Support, AbsolutePathIteratorWin32) { // when iterating. ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); - for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; - ++I) { + for (path::const_iterator I = path::begin(Path, path::Style::windows), + E = path::end(Path); + I != E; ++I) { ActualPathComponents.push_back(*I); } @@ -276,34 +279,29 @@ TEST(Support, AbsolutePathIteratorWin32) { EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); } } -#endif // LLVM_ON_WIN32 TEST(Support, AbsolutePathIteratorEnd) { // Trailing slashes are converted to '.' unless they are part of the root path. - SmallVector<StringRef, 4> Paths; - Paths.push_back("/foo/"); - Paths.push_back("/foo//"); - Paths.push_back("//net//"); -#ifdef LLVM_ON_WIN32 - Paths.push_back("c:\\\\"); -#endif - - for (StringRef Path : Paths) { - StringRef LastComponent = *path::rbegin(Path); + SmallVector<std::pair<StringRef, path::Style>, 4> Paths; + Paths.emplace_back("/foo/", path::Style::native); + Paths.emplace_back("/foo//", path::Style::native); + Paths.emplace_back("//net//", path::Style::native); + Paths.emplace_back("c:\\\\", path::Style::windows); + + for (auto &Path : Paths) { + StringRef LastComponent = *path::rbegin(Path.first, Path.second); EXPECT_EQ(".", LastComponent); } - SmallVector<StringRef, 3> RootPaths; - RootPaths.push_back("/"); - RootPaths.push_back("//net/"); -#ifdef LLVM_ON_WIN32 - RootPaths.push_back("c:\\"); -#endif + SmallVector<std::pair<StringRef, path::Style>, 3> RootPaths; + RootPaths.emplace_back("/", path::Style::native); + RootPaths.emplace_back("//net/", path::Style::native); + RootPaths.emplace_back("c:\\", path::Style::windows); - for (StringRef Path : RootPaths) { - StringRef LastComponent = *path::rbegin(Path); + for (auto &Path : RootPaths) { + StringRef LastComponent = *path::rbegin(Path.first, Path.second); EXPECT_EQ(1u, LastComponent.size()); - EXPECT_TRUE(path::is_separator(LastComponent[0])); + EXPECT_TRUE(path::is_separator(LastComponent[0], Path.second)); } } @@ -1056,40 +1054,18 @@ TEST_F(FileSystemTest, FileMapping) { } TEST(Support, NormalizePath) { -#if defined(LLVM_ON_WIN32) -#define EXPECT_PATH_IS(path__, windows__, not_windows__) \ - EXPECT_EQ(path__, windows__); -#else -#define EXPECT_PATH_IS(path__, windows__, not_windows__) \ - EXPECT_EQ(path__, not_windows__); -#endif - - SmallString<64> Path1("a"); - SmallString<64> Path2("a/b"); - SmallString<64> Path3("a\\b"); - SmallString<64> Path4("a\\\\b"); - SmallString<64> Path5("\\a"); - SmallString<64> Path6("a\\"); - - path::native(Path1); - EXPECT_PATH_IS(Path1, "a", "a"); - - path::native(Path2); - EXPECT_PATH_IS(Path2, "a\\b", "a/b"); - - path::native(Path3); - EXPECT_PATH_IS(Path3, "a\\b", "a/b"); - - path::native(Path4); - EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); - - path::native(Path5); - EXPECT_PATH_IS(Path5, "\\a", "/a"); - - path::native(Path6); - EXPECT_PATH_IS(Path6, "a\\", "a/"); - -#undef EXPECT_PATH_IS + using TestTuple = std::tuple<StringRef, StringRef, StringRef>; + TestTuple Tests[] = {{"a", "a", "a"}, {"a/b", "a\\b", "a/b"}, + {"a\\b", "a\\b", "a/b"}, {"a\\\\b", "a\\\\b", "a\\\\b"}, + {"\\a", "\\a", "/a"}, {"a\\", "a\\", "a/"}}; + for (auto &T : Tests) { + SmallString<64> Win = std::get<0>(T); + SmallString<64> Posix = Win; + path::native(Win, path::Style::windows); + path::native(Posix, path::Style::posix); + EXPECT_EQ(std::get<1>(T), Win); + EXPECT_EQ(std::get<2>(T), Posix); + } #if defined(LLVM_ON_WIN32) SmallString<64> PathHome; @@ -1129,43 +1105,48 @@ TEST(Support, RemoveLeadingDotSlash) { EXPECT_EQ(Path2, ""); } -static std::string remove_dots(StringRef path, - bool remove_dot_dot) { +static std::string remove_dots(StringRef path, bool remove_dot_dot, + path::Style style) { SmallString<256> buffer(path); - path::remove_dots(buffer, remove_dot_dot); + path::remove_dots(buffer, remove_dot_dot, style); return buffer.str(); } TEST(Support, RemoveDots) { -#if defined(LLVM_ON_WIN32) - EXPECT_EQ("foolz\\wat", remove_dots(".\\.\\\\foolz\\wat", false)); - EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false)); - - EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false)); - EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true)); - EXPECT_EQ("c", remove_dots(".\\.\\c", true)); - EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true)); - EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true)); + EXPECT_EQ("foolz\\wat", + remove_dots(".\\.\\\\foolz\\wat", false, path::Style::windows)); + EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false, path::Style::windows)); + + EXPECT_EQ("a\\..\\b\\c", + remove_dots(".\\a\\..\\b\\c", false, path::Style::windows)); + EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true, path::Style::windows)); + EXPECT_EQ("c", remove_dots(".\\.\\c", true, path::Style::windows)); + EXPECT_EQ("..\\a\\c", + remove_dots("..\\a\\b\\..\\c", true, path::Style::windows)); + EXPECT_EQ("..\\..\\a\\c", + remove_dots("..\\..\\a\\b\\..\\c", true, path::Style::windows)); SmallString<64> Path1(".\\.\\c"); - EXPECT_TRUE(path::remove_dots(Path1, true)); - EXPECT_EQ("c", Path1); -#else - EXPECT_EQ("foolz/wat", remove_dots("././/foolz/wat", false)); - EXPECT_EQ("", remove_dots("./////", false)); - - EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false)); - EXPECT_EQ("b/c", remove_dots("./a/../b/c", true)); - EXPECT_EQ("c", remove_dots("././c", true)); - EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true)); - EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true)); - EXPECT_EQ("/a/c", remove_dots("/../../a/c", true)); - EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true)); - - SmallString<64> Path1("././c"); - EXPECT_TRUE(path::remove_dots(Path1, true)); + EXPECT_TRUE(path::remove_dots(Path1, true, path::Style::windows)); EXPECT_EQ("c", Path1); -#endif + + EXPECT_EQ("foolz/wat", + remove_dots("././/foolz/wat", false, path::Style::posix)); + EXPECT_EQ("", remove_dots("./////", false, path::Style::posix)); + + EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false, path::Style::posix)); + EXPECT_EQ("b/c", remove_dots("./a/../b/c", true, path::Style::posix)); + EXPECT_EQ("c", remove_dots("././c", true, path::Style::posix)); + EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true, path::Style::posix)); + EXPECT_EQ("../../a/c", + remove_dots("../../a/b/../c", true, path::Style::posix)); + EXPECT_EQ("/a/c", remove_dots("/../../a/c", true, path::Style::posix)); + EXPECT_EQ("/a/c", + remove_dots("/../a/b//../././/c", true, path::Style::posix)); + + SmallString<64> Path2("././c"); + EXPECT_TRUE(path::remove_dots(Path2, true, path::Style::posix)); + EXPECT_EQ("c", Path2); } TEST(Support, ReplacePathPrefix) { |