[PATCH] D56769: Path: enhance prefix mapping

Dan McGregor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 15 19:20:49 PST 2019


dankm created this revision.
Herald added subscribers: llvm-commits, kristina.

Return true when a path is remapped, and allow replacing the
original path entirely. This is needed for D49466 <https://reviews.llvm.org/D49466>.


Repository:
  rL LLVM

https://reviews.llvm.org/D56769

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


Index: lib/Support/Path.cpp
===================================================================
--- lib/Support/Path.cpp
+++ lib/Support/Path.cpp
@@ -521,27 +521,43 @@
   path.append(ext.begin(), ext.end());
 }
 
-void replace_path_prefix(SmallVectorImpl<char> &Path,
+bool replace_path_prefix(SmallVectorImpl<char> &Path,
                          const StringRef &OldPrefix, const StringRef &NewPrefix,
                          Style style) {
   if (OldPrefix.empty() && NewPrefix.empty())
-    return;
+    return false;
 
   StringRef OrigPath(Path.begin(), Path.size());
-  if (!OrigPath.startswith(OldPrefix))
-    return;
 
+  if ((is_separator(OldPrefix.back(), style) &&
+       !OrigPath.startswith(parent_path(OldPrefix, style))) ||
+      (!is_separator(OldPrefix.back(), style) &&
+       !OrigPath.startswith(OldPrefix)))
+    return false;
+
+  if (!is_separator(OldPrefix.back(), style) &&
+      !(OldPrefix == OrigPath ||
+        is_separator(OrigPath[OldPrefix.size()], style)))
+    return false;
+
   // If prefixes have the same size we can simply copy the new one over.
   if (OldPrefix.size() == NewPrefix.size()) {
     llvm::copy(NewPrefix, Path.begin());
-    return;
+    return true;
   }
 
   StringRef RelPath = OrigPath.substr(OldPrefix.size());
   SmallString<256> NewPath;
   path::append(NewPath, style, NewPrefix);
-  path::append(NewPath, style, RelPath);
+  if (!is_separator(RelPath[0], style))
+    path::append(NewPath, style, RelPath);
+  else
+    path::append(NewPath, style, relative_path(RelPath, style));
+  while (is_separator(NewPath.back(), style))
+    NewPath.set_size(NewPath.size() - 1);
   Path.swap(NewPath);
+
+  return true;
 }
 
 void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
Index: include/llvm/Support/Path.h
===================================================================
--- include/llvm/Support/Path.h
+++ include/llvm/Support/Path.h
@@ -160,7 +160,8 @@
 ///        start with \a NewPrefix.
 /// @param OldPrefix The path prefix to strip from \a Path.
 /// @param NewPrefix The path prefix to replace \a NewPrefix with.
-void replace_path_prefix(SmallVectorImpl<char> &Path,
+/// @result true if \a Path begins with OldPrefix
+bool replace_path_prefix(SmallVectorImpl<char> &Path,
                          const StringRef &OldPrefix, const StringRef &NewPrefix,
                          Style style = Style::native);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56769.181965.patch
Type: text/x-patch
Size: 2422 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190116/580532ee/attachment.bin>


More information about the llvm-commits mailing list