summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2016-10-17 13:28:21 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2016-10-17 13:28:21 +0000
commit937dd7af2d262af1e3deaddd11906484d7ad8b76 (patch)
tree07052d294842830e0ebe484d71ae183cae3ddb9a
parentcdcd403bc2223e6ed71323fe2b36030e9a7bc674 (diff)
downloadbcm5719-llvm-937dd7af2d262af1e3deaddd11906484d7ad8b76.tar.gz
bcm5719-llvm-937dd7af2d262af1e3deaddd11906484d7ad8b76.zip
[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
-rw-r--r--llvm/lib/Support/Path.cpp13
-rw-r--r--llvm/unittests/Support/Path.cpp4
2 files changed, 12 insertions, 5 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 4374cba4988..b2d645779b0 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -707,11 +707,14 @@ static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) {
for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) {
if (C == ".")
continue;
- // Leading ".." will remain in the path.
- if (remove_dot_dot && C == ".." && !components.empty() &&
- components.back() != "..") {
- components.pop_back();
- continue;
+ // Leading ".." will remain in the path unless it's at the root.
+ if (remove_dot_dot && C == "..") {
+ if (!components.empty() && components.back() != "..") {
+ components.pop_back();
+ continue;
+ }
+ if (path::is_absolute(path))
+ continue;
}
components.push_back(C);
}
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index 65bd1de8b70..7ddc5dbb396 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -969,6 +969,8 @@ TEST(Support, RemoveDots) {
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));
@@ -982,6 +984,8 @@ TEST(Support, RemoveDots) {
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));
OpenPOWER on IntegriCloud