Move twice-repeated clang path operation into a new function.
authorDouglas Katzman <dougk@google.com>
Wed, 2 Sep 2015 21:02:10 +0000 (21:02 +0000)
committerDouglas Katzman <dougk@google.com>
Wed, 2 Sep 2015 21:02:10 +0000 (21:02 +0000)
And make it more robust in the edge case of exactly "./" as input.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246711 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/Path.h
lib/Support/Path.cpp
unittests/Support/Path.cpp

index 6fedb93d23767fac053bf7ac8ae63c7b85e83b22..0853f916b5803585837518e3247e31e8fe201973 100644 (file)
@@ -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);
 
 /// @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
 } // end namespace path
 } // end namespace sys
 } // end namespace llvm
index 54daf24daba0613e16d90ec3f5127d5e249796c4..aa96074554e435029047d3ca29d5a7df211d93d8 100644 (file)
@@ -661,8 +661,16 @@ bool is_absolute(const Twine &path) {
   return rootDir && rootName;
 }
 
   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
 }
 
 } // end namespace path
index 11d53c83419def2a60b357f643c0d264c2477e9a..cfba227dbe93fe723d855d95bcb15a418ba916ec 100644 (file)
@@ -789,4 +789,14 @@ TEST(Support, NormalizePath) {
 
 #undef EXPECT_PATH_IS
 }
 
 #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
 } // anonymous namespace