Do not delete leading ../ in remove_dots.

Reviewers: bkramer

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D25561

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284129 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Liu 2016-10-13 15:07:14 +00:00
parent 172d6c078b
commit e0d080d15d
3 changed files with 11 additions and 7 deletions

View File

@ -445,7 +445,8 @@ StringRef remove_leading_dotslash(StringRef path);
/// @brief In-place remove any './' and optionally '../' components from a path.
///
/// @param path processed path
/// @param remove_dot_dot specify if '../' should be removed
/// @param remove_dot_dot specify if '../' (except for leading "../") should be
/// removed
/// @result True if path was changed
bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false);

View File

@ -707,12 +707,11 @@ 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;
if (remove_dot_dot) {
if (C == "..") {
if (!components.empty())
components.pop_back();
continue;
}
// Leading ".." will remain in the path.
if (remove_dot_dot && C == ".." && !components.empty() &&
components.back() != "..") {
components.pop_back();
continue;
}
components.push_back(C);
}

View File

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