From 8cda80ad2486782d40ac51cb07dfc630fee47639 Mon Sep 17 00:00:00 2001 From: Mike Aizatsky Date: Mon, 9 Nov 2015 18:56:31 +0000 Subject: [PATCH] Moving FileManager::removeDotPaths to llvm::sys::path::remove_dots Differential Revision: http://reviews.llvm.org/D14393 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252499 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Path.h | 7 +++++++ lib/Support/Path.cpp | 35 +++++++++++++++++++++++++++++++++++ unittests/Support/Path.cpp | 20 ++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/include/llvm/Support/Path.h b/include/llvm/Support/Path.h index 1e56be4a5fc..955cc991d9b 100644 --- a/include/llvm/Support/Path.h +++ b/include/llvm/Support/Path.h @@ -423,6 +423,13 @@ bool is_relative(const Twine &path); /// @result The cleaned-up \a path. 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 +/// @result True if path was changed +bool remove_dots(SmallVectorImpl &path, bool remove_dot_dot = false); + } // end namespace path } // end namespace sys } // end namespace llvm diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index f45774bca7b..4952f59fc24 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -671,6 +671,41 @@ StringRef remove_leading_dotslash(StringRef Path) { return Path; } +static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) { + SmallVector components; + + // Skip the root path, then look for traversal in the components. + StringRef rel = path::relative_path(path); + 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; + } + } + components.push_back(C); + } + + SmallString<256> buffer = path::root_path(path); + for (StringRef C : components) + path::append(buffer, C); + return buffer; +} + +bool remove_dots(SmallVectorImpl &path, bool remove_dot_dot) { + StringRef p(path.data(), path.size()); + + SmallString<256> result = remove_dots(p, remove_dot_dot); + if (result == path) + return false; + + path.swap(result); + return true; +} + } // end namespace path namespace fs { diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index a7a6a4add7c..07ad3fc6b63 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -844,4 +844,24 @@ TEST(Support, RemoveLeadingDotSlash) { Path2 = path::remove_leading_dotslash(Path2); EXPECT_EQ(Path2, ""); } + +static std::string remove_dots(StringRef path, + bool remove_dot_dot) { + SmallString<256> buffer(path); + path::remove_dots(buffer, remove_dot_dot); + return buffer.str(); +} + +TEST(Support, RemoveDots) { + 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)); + + SmallString<64> Path1("././c"); + EXPECT_TRUE(path::remove_dots(Path1, true)); + EXPECT_EQ("c", Path1); +} } // anonymous namespace -- 2.34.1