From c2142af3102060a7105ec9ef9432f12c5308f7dc Mon Sep 17 00:00:00 2001
From: Nico Rieck <nico.rieck@gmail.com>
Date: Sun, 12 Feb 2012 02:10:30 +0100
Subject: [PATCH] Support/FileSystem: Add path::is_root_path

Used by clang to not strip trailing path separators from Windows drives.
---
 include/llvm/Support/PathV2.h |    6 ++++++
 lib/Support/PathV2.cpp        |   14 ++++++++++++++
 unittests/Support/Path.cpp    |    1 +
 3 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/include/llvm/Support/PathV2.h b/include/llvm/Support/PathV2.h
index 6d38c95..78fac8f 100644
--- a/include/llvm/Support/PathV2.h
+++ b/include/llvm/Support/PathV2.h
@@ -283,6 +283,12 @@ void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
 /// @result True if the path has a root name, false otherwise.
 bool has_root_name(const Twine &path);
 
+/// @brief Check whether the path is a root path (excludes network paths).
+///
+/// @param path Input path.
+/// @result True if \a path is a root path, false otherwise.
+bool is_root_path(StringRef path);
+
 /// @brief Has root directory?
 ///
 /// root_directory != ""
diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp
index 3ef13e2..0901945 100644
--- a/lib/Support/PathV2.cpp
+++ b/lib/Support/PathV2.cpp
@@ -519,6 +519,20 @@ void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
   result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
 }
 
+bool is_root_path(StringRef path) {
+  switch (path.size()) {
+     // POSIX style
+    case 1:  return is_separator(path[0]);
+#ifdef LLVM_ON_WIN32
+    // C:
+    case 2:  return path.endswith(":");
+    // C:/
+    case 3:  return path[1] == ':' && is_separator(path[2]);
+#endif
+    default: return false;
+  }
+}
+
 bool has_root_name(const Twine &path) {
   SmallString<128> path_storage;
   StringRef p = path.toStringRef(path_storage);
diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp
index 358dad0..9107bb1 100644
--- a/unittests/Support/Path.cpp
+++ b/unittests/Support/Path.cpp
@@ -108,6 +108,7 @@ TEST(Support, Path) {
 #endif
 
     path::has_root_path(*i);
+    path::is_root_path(*i);
     path::root_path(*i);
     path::has_root_name(*i);
     path::root_name(*i);
-- 
1.7.8.msysgit.0

