summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/include/llvm/Support/Path.h6
-rw-r--r--llvm/lib/Support/Path.cpp12
-rw-r--r--llvm/unittests/Support/Path.cpp10
3 files changed, 26 insertions, 2 deletions
diff --git a/llvm/include/llvm/Support/Path.h b/llvm/include/llvm/Support/Path.h
index 6fedb93d237..0853f916b58 100644
--- a/llvm/include/llvm/Support/Path.h
+++ b/llvm/include/llvm/Support/Path.h
@@ -401,6 +401,12 @@ bool is_absolute(const Twine &path);
/// @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.
+///
+/// @param path Input path.
+/// @result The cleaned-up \a path.
+StringRef remove_leading_dotslash(StringRef path);
+
} // end namespace path
} // end namespace sys
} // end namespace llvm
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 54daf24daba..aa96074554e 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -661,8 +661,16 @@ bool is_absolute(const Twine &path) {
return rootDir && rootName;
}
-bool is_relative(const Twine &path) {
- return !is_absolute(path);
+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);
+ }
+ return Path;
}
} // end namespace path
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index 11d53c83419..cfba227dbe9 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -789,4 +789,14 @@ TEST(Support, NormalizePath) {
#undef EXPECT_PATH_IS
}
+
+TEST(Support, RemoveLeadingDotSlash) {
+ StringRef Path1("././/foolz/wat");
+ StringRef Path2("./////");
+
+ Path1 = path::remove_leading_dotslash(Path1);
+ EXPECT_EQ(Path1, "foolz/wat");
+ Path2 = path::remove_leading_dotslash(Path2);
+ EXPECT_EQ(Path2, "");
+}
} // anonymous namespace
OpenPOWER on IntegriCloud