[PATCH] D54448: [FileSystem] Add expand_tilde function

Jonas Devlieghere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 12 14:20:22 PST 2018


JDevlieghere updated this revision to Diff 173762.
JDevlieghere added a comment.

Update unit test.


https://reviews.llvm.org/D54448

Files:
  llvm/include/llvm/Support/FileSystem.h
  llvm/lib/Support/Unix/Path.inc
  llvm/lib/Support/Windows/Path.inc
  llvm/unittests/Support/Path.cpp


Index: llvm/unittests/Support/Path.cpp
===================================================================
--- llvm/unittests/Support/Path.cpp
+++ llvm/unittests/Support/Path.cpp
@@ -528,6 +528,26 @@
   ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
 }
 
+TEST_F(FileSystemTest, ExpandTilde) {
+  ASSERT_NO_ERROR(
+      fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3"));
+  ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3"));
+
+  SmallString<64> RealBase;
+  SmallString<64> Expected;
+  SmallString<64> Actual;
+
+  SmallString<64> HomeDir;
+  bool Result = llvm::sys::path::home_directory(HomeDir);
+  if (Result) {
+    ASSERT_NO_ERROR(fs::expand_tilde(HomeDir, Expected));
+    ASSERT_NO_ERROR(fs::expand_tilde("~", Actual));
+    EXPECT_EQ(Expected, Actual);
+  }
+
+  ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
+}
+
 #ifdef LLVM_ON_UNIX
 TEST_F(FileSystemTest, RealPathNoReadPerm) {
   SmallString<64> Expanded;
Index: llvm/lib/Support/Windows/Path.inc
===================================================================
--- llvm/lib/Support/Windows/Path.inc
+++ llvm/lib/Support/Windows/Path.inc
@@ -1253,6 +1253,17 @@
   Path.insert(Path.begin() + 1, HomeDir.begin() + 1, HomeDir.end());
 }
 
+std::error_code expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) {
+  dest.clear();
+  if (path.isTriviallyEmpty())
+    return std::error_code();
+
+  path.toVector(dest);
+  expandTildeExpr(dest);
+
+  return std::error_code();
+}
+
 std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
                           bool expand_tilde) {
   dest.clear();
Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -550,6 +550,18 @@
   llvm::sys::path::append(Path, Storage);
 }
 
+
+std::error_code expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) {
+  dest.clear();
+  if (path.isTriviallyEmpty())
+    return std::error_code();
+
+  path.toVector(dest);
+  expandTildeExpr(dest);
+
+  return std::error_code();
+}
+
 static file_type typeForMode(mode_t Mode) {
   if (S_ISDIR(Mode))
     return file_type::directory_file;
Index: llvm/include/llvm/Support/FileSystem.h
===================================================================
--- llvm/include/llvm/Support/FileSystem.h
+++ llvm/include/llvm/Support/FileSystem.h
@@ -349,6 +349,13 @@
 std::error_code real_path(const Twine &path, SmallVectorImpl<char> &output,
                           bool expand_tilde = false);
 
+/// Expands ~ expressions to the user's home directory.
+///
+/// @param path The path to resolve.
+/// @returns errc::success if the current path has been resolved in output,
+///          otherwise a platform-specific error_code.
+std::error_code expand_tilde(const Twine &path, SmallVectorImpl<char> &output);
+
 /// Get the current path.
 ///
 /// @param result Holds the current path on return.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54448.173762.patch
Type: text/x-patch
Size: 3043 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181112/feaf4e02/attachment.bin>


More information about the llvm-commits mailing list