[Support] remove_dots: Remove .. from absolute paths.

/../foo is still a proper path after removing the dotdot. This should
now finally match https://9p.io/sys/doc/lexnames.html [Cleaning names].

llvm-svn: 284384
This commit is contained in:
Benjamin Kramer 2016-10-17 13:28:21 +00:00
parent cdcd403bc2
commit 937dd7af2d
2 changed files with 12 additions and 5 deletions

View File

@ -707,12 +707,15 @@ static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) {
for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) { for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) {
if (C == ".") if (C == ".")
continue; continue;
// Leading ".." will remain in the path. // Leading ".." will remain in the path unless it's at the root.
if (remove_dot_dot && C == ".." && !components.empty() && if (remove_dot_dot && C == "..") {
components.back() != "..") { if (!components.empty() && components.back() != "..") {
components.pop_back(); components.pop_back();
continue; continue;
} }
if (path::is_absolute(path))
continue;
}
components.push_back(C); components.push_back(C);
} }

View File

@ -969,6 +969,8 @@ TEST(Support, RemoveDots) {
EXPECT_EQ("c", remove_dots(".\\.\\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\\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"); SmallString<64> Path1(".\\.\\c");
EXPECT_TRUE(path::remove_dots(Path1, true)); EXPECT_TRUE(path::remove_dots(Path1, true));
@ -982,6 +984,8 @@ TEST(Support, RemoveDots) {
EXPECT_EQ("c", remove_dots("././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/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"); SmallString<64> Path1("././c");
EXPECT_TRUE(path::remove_dots(Path1, true)); EXPECT_TRUE(path::remove_dots(Path1, true));