[PATCH] D46887: Fix llvm::sys::path::remove_dots() to return "." instead of an empty path.

Greg Clayton via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 15 09:17:39 PDT 2018


clayborg created this revision.
clayborg added reviewers: labath, zturner, aizatsky, sepavloff, dougk.

LLDB recently started using llvm::sys::path::remove_dots() to normalize paths inside of LLDB. We ran into a few cases where this was falling down for us where paths that resulted in the current working directory (".", "./", "foo/.." and more) would result in an empty file specification since llvm::sys::path::remove_dots() would return an empty path. This fix corrects this issue by having "." returned if the path specified was not empty to begin with and the resulting path would have been empty. Pavel and I believe this is the intention of this function. If this is not correct please comment.


Repository:
  rL LLVM

https://reviews.llvm.org/D46887

Files:
  lib/Support/Path.cpp
  unittests/Support/Path.cpp


Index: unittests/Support/Path.cpp
===================================================================
--- unittests/Support/Path.cpp
+++ unittests/Support/Path.cpp
@@ -1146,7 +1146,7 @@
 TEST(Support, RemoveDots) {
   EXPECT_EQ("foolz\\wat",
             remove_dots(".\\.\\\\foolz\\wat", false, path::Style::windows));
-  EXPECT_EQ("", remove_dots(".\\\\\\\\\\", false, path::Style::windows));
+  EXPECT_EQ(".", remove_dots(".\\\\\\\\\\", false, path::Style::windows));
 
   EXPECT_EQ("a\\..\\b\\c",
             remove_dots(".\\a\\..\\b\\c", false, path::Style::windows));
@@ -1163,7 +1163,7 @@
 
   EXPECT_EQ("foolz/wat",
             remove_dots("././/foolz/wat", false, path::Style::posix));
-  EXPECT_EQ("", remove_dots("./////", false, path::Style::posix));
+  EXPECT_EQ(".", remove_dots("./////", false, path::Style::posix));
 
   EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false, path::Style::posix));
   EXPECT_EQ("b/c", remove_dots("./a/../b/c", true, path::Style::posix));
Index: lib/Support/Path.cpp
===================================================================
--- lib/Support/Path.cpp
+++ lib/Support/Path.cpp
@@ -726,6 +726,10 @@
   SmallString<256> buffer = path::root_path(path, style);
   for (StringRef C : components)
     path::append(buffer, style, C);
+  // If the path passed in wasn't empty, but results in a path that is empty
+  // (like ".", "./", or "foo/..") don't return an empty string, return a ".".
+  if (buffer.empty() && !path.empty())
+    buffer.append(1, '.');
   return buffer;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46887.146851.patch
Type: text/x-patch
Size: 1536 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180515/37aaeac6/attachment.bin>


More information about the llvm-commits mailing list