[PATCH] D87732: [Support] Provide sys::path::guess_style

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 17:08:51 PDT 2020


phosek created this revision.
phosek added a reviewer: dblaikie.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
phosek requested review of this revision.

This function can be used to guess separator style from an absolute
path. This is equivalent to lldb's GuessPathStyle. The primary use
case is DWARFDebugLine::Prologue::getFileNameByIndex where instead
of using native style, which may not match the style of the platform
where the debug info was generated, we should instead try to guess
the style from the path itself and then use it if at all possible.
There are other places in LLVM we implement this functionality and
could all use this function instead.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87732

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


Index: llvm/unittests/Support/Path.cpp
===================================================================
--- llvm/unittests/Support/Path.cpp
+++ llvm/unittests/Support/Path.cpp
@@ -1521,6 +1521,17 @@
   EXPECT_EQ(Path, "C:\\old/foo\\bar");
 }
 
+TEST(Support, GuessStyle) {
+  EXPECT_EQ(path::Style::posix, path::guess_style("/foo/bar.txt"));
+  EXPECT_EQ(path::Style::posix, path::guess_style("//net/bar.txt"));
+  EXPECT_EQ(path::Style::windows,
+            path::guess_style(R"(C:\foo.txt)"));
+  EXPECT_EQ(path::Style::windows,
+            path::guess_style(R"(\\net\foo.txt)"));
+  EXPECT_EQ(llvm::None, path::guess_style("foo.txt"));
+  EXPECT_EQ(llvm::None, path::guess_style("foo/bar.txt"));
+}
+
 TEST_F(FileSystemTest, OpenFileForRead) {
   // Create a temp file.
   int FileDescriptor;
Index: llvm/lib/Support/Path.cpp
===================================================================
--- llvm/lib/Support/Path.cpp
+++ llvm/lib/Support/Path.cpp
@@ -763,6 +763,17 @@
   return true;
 }
 
+llvm::Optional<Style> guess_style(StringRef absolute_path) {
+  if (absolute_path.startswith("/"))
+    return Style::posix;
+  if (absolute_path.startswith(R"(\\)"))
+    return Style::windows;
+  if (absolute_path.size() > 3 && llvm::isAlpha(absolute_path[0]) &&
+      absolute_path.substr(1, 2) == R"(:\)")
+    return Style::windows;
+  return llvm::None;
+}
+
 } // end namespace path
 
 namespace fs {
Index: llvm/include/llvm/Support/Path.h
===================================================================
--- llvm/include/llvm/Support/Path.h
+++ llvm/include/llvm/Support/Path.h
@@ -476,6 +476,11 @@
 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false,
                  Style style = Style::native);
 
+/// Attempt to guess path style for a given path string.
+///
+/// @param path Input path.
+llvm::Optional<Style> guess_style(StringRef absolute_path);
+
 } // end namespace path
 } // end namespace sys
 } // end namespace llvm


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87732.292062.patch
Type: text/x-patch
Size: 1972 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200916/f2dbe576/attachment.bin>


More information about the llvm-commits mailing list