[llvm] r246711 - Move twice-repeated clang path operation into a new function.

Douglas Katzman via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 2 14:02:10 PDT 2015


Author: dougk
Date: Wed Sep  2 16:02:10 2015
New Revision: 246711

URL: http://llvm.org/viewvc/llvm-project?rev=246711&view=rev
Log:
Move twice-repeated clang path operation into a new function.

And make it more robust in the edge case of exactly "./" as input.

Modified:
    llvm/trunk/include/llvm/Support/Path.h
    llvm/trunk/lib/Support/Path.cpp
    llvm/trunk/unittests/Support/Path.cpp

Modified: llvm/trunk/include/llvm/Support/Path.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Path.h?rev=246711&r1=246710&r2=246711&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Path.h (original)
+++ llvm/trunk/include/llvm/Support/Path.h Wed Sep  2 16:02:10 2015
@@ -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

Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=246711&r1=246710&r2=246711&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Wed Sep  2 16:02:10 2015
@@ -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

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=246711&r1=246710&r2=246711&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Wed Sep  2 16:02:10 2015
@@ -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




More information about the llvm-commits mailing list