Support/PathV2: Add is_{absolute,relative} implementation.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 1 Dec 2010 06:21:53 +0000 (06:21 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 1 Dec 2010 06:21:53 +0000 (06:21 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120560 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/PathV2.cpp
unittests/Support/Path.cpp

index 57ccf893d9bb2d04ffe2ec3db77e8421882f90eb..a4a3f383411d90364f48bd5f049559f1a81622a0 100644 (file)
@@ -642,6 +642,30 @@ error_code has_extension(const Twine &path, bool &result) {
   return make_error_code(errc::success);
 }
 
+error_code is_absolute(const Twine &path, bool &result) {
+  SmallString<128> path_storage;
+  StringRef p = path.toStringRef(path_storage);
+
+  bool rootDir = false,
+       rootName = false;
+  if (error_code ec = has_root_directory(p, rootDir)) return ec;
+#ifdef LLVM_ON_WIN32
+  if (error_code ec = has_root_name(p, rootName)) return ec;
+#else
+  rootName = true;
+#endif
+
+  result = rootDir && rootName;
+  return make_error_code(errc::success);
+}
+
+error_code is_relative(const Twine &path, bool &result) {
+  bool res;
+  error_code ec = is_absolute(path, res);
+  result = !res;
+  return ec;
+}
+
 }
 }
 }
index 3f50e708e646a9be8e61a7bf0d1a6698ac8e930b..24718648996847c413ad7fda5b2f3722c8b90c52 100644 (file)
@@ -112,6 +112,8 @@ TEST(Support, Path) {
     TEST_PATH(stem, *i, sfres);
     TEST_PATH(has_extension, *i, bres);
     TEST_PATH(extension, *i, sfres);
+    TEST_PATH(is_absolute, *i, bres);
+    TEST_PATH(is_relative, *i, bres);
 
     SmallString<16> temp_store;
     TEST_PATH_SMALLVEC(make_absolute, *i, temp_store);