Index: include/llvm/Support/Path.h =================================================================== --- include/llvm/Support/Path.h +++ include/llvm/Support/Path.h @@ -417,11 +417,19 @@ /// @result True if the path is relative, false if it is not. bool is_relative(const Twine &path); -/// @brief Remove redundant leading "./" pieces and consecutive separators. +/// \brief Remove any './' and optionally '../' components from a path. /// -/// @param path Input path. -/// @result The cleaned-up \a path. -StringRef remove_leading_dotslash(StringRef path); +/// @param path Input path +/// @param remove_dot_dot specify if '../' should be removed +/// @result processed path +StringRef remove_dots(StringRef path, bool remove_dot_dot = false); + +/// \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 Index: lib/Support/Path.cpp =================================================================== --- lib/Support/Path.cpp +++ lib/Support/Path.cpp @@ -661,14 +661,40 @@ bool is_relative(const Twine &path) { return !is_absolute(path); } -StringRef remove_leading_dotslash(StringRef Path) { - // Remove leading "./" (or ".//" or "././" etc.) - while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1])) { - Path = Path.substr(2); - while (Path.size() > 0 && is_separator(Path[0])) - Path = Path.substr(1); +StringRef 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); } - return Path; + + 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()); + + StringRef result = remove_dots(p, remove_dot_dot); + if (result == p) + return false; + + path.erase(path.begin()); + path.append(result.begin(), result.end()); + return true; } } // end namespace path Index: unittests/Support/Path.cpp =================================================================== --- unittests/Support/Path.cpp +++ unittests/Support/Path.cpp @@ -836,12 +836,10 @@ } TEST(Support, RemoveLeadingDotSlash) { - StringRef Path1("././/foolz/wat"); - StringRef Path2("./////"); + EXPECT_EQ("foolz/wat", path::remove_dots("././/foolz/wat")); + EXPECT_EQ("", path::remove_dots("./////")); - Path1 = path::remove_leading_dotslash(Path1); - EXPECT_EQ(Path1, "foolz/wat"); - Path2 = path::remove_leading_dotslash(Path2); - EXPECT_EQ(Path2, ""); + EXPECT_EQ("a/../b/c", path::remove_dots("./a/../b/c")); + EXPECT_EQ("b/c", path::remove_dots("./a/../b/c", true)); } } // anonymous namespace