[PATCH] D14393: Moving FileManager::removeDotPaths to llvm::sys::path::remove_dots

Mike Aizatsky via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 9 10:58:59 PST 2015


This revision was automatically updated to reflect the committed changes.
Closed by commit rL252499: Moving FileManager::removeDotPaths to llvm::sys::path::remove_dots (authored by aizatsky).

Changed prior to commit:
  http://reviews.llvm.org/D14393?vs=39559&id=39723#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D14393

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

Index: llvm/trunk/include/llvm/Support/Path.h
===================================================================
--- llvm/trunk/include/llvm/Support/Path.h
+++ llvm/trunk/include/llvm/Support/Path.h
@@ -423,6 +423,13 @@
 /// @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<char> &path, bool remove_dot_dot = false);
+
 } // end namespace path
 } // end namespace sys
 } // end namespace llvm
Index: llvm/trunk/lib/Support/Path.cpp
===================================================================
--- llvm/trunk/lib/Support/Path.cpp
+++ llvm/trunk/lib/Support/Path.cpp
@@ -671,6 +671,41 @@
   return Path;
 }
 
+static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) {
+  SmallVector<StringRef, 16> 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<char> &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 {
Index: llvm/trunk/unittests/Support/Path.cpp
===================================================================
--- llvm/trunk/unittests/Support/Path.cpp
+++ llvm/trunk/unittests/Support/Path.cpp
@@ -844,4 +844,24 @@
   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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14393.39723.patch
Type: text/x-patch
Size: 2814 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151109/14b345ea/attachment.bin>


More information about the llvm-commits mailing list