OSDN Git Service

[Support] Add a version of fs::make_absolute with a custom CWD.
authorBenjamin Kramer <benny.kra@googlemail.com>
Mon, 5 Oct 2015 13:02:43 +0000 (13:02 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Mon, 5 Oct 2015 13:02:43 +0000 (13:02 +0000)
This will be used soon from clang.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249309 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/FileSystem.h
lib/Support/Path.cpp
unittests/Support/Path.cpp

index 48a2f0a..84d7e85 100644 (file)
@@ -268,6 +268,20 @@ private:
 
 /// @brief Make \a path an absolute path.
 ///
+/// Makes \a path absolute using the \a current_directory if it is not already.
+/// An empty \a path will result in the \a current_directory.
+///
+/// /absolute/path   => /absolute/path
+/// relative/../path => <current-directory>/relative/../path
+///
+/// @param path A path that is modified to be an absolute path.
+/// @returns errc::success if \a path has been made absolute, otherwise a
+///          platform-specific error_code.
+std::error_code make_absolute(const Twine &current_directory,
+                              SmallVectorImpl<char> &path);
+
+/// @brief Make \a path an absolute path.
+///
 /// Makes \a path absolute using the current directory if it is not already. An
 /// empty \a path will result in the current directory.
 ///
index aa96074..653a984 100644 (file)
@@ -740,7 +740,9 @@ std::error_code createUniqueDirectory(const Twine &Prefix,
                             true, 0, FS_Dir);
 }
 
-std::error_code make_absolute(SmallVectorImpl<char> &path) {
+static std::error_code make_absolute(const Twine &current_directory,
+                                     SmallVectorImpl<char> &path,
+                                     bool use_current_directory) {
   StringRef p(path.data(), path.size());
 
   bool rootDirectory = path::has_root_directory(p),
@@ -756,7 +758,9 @@ std::error_code make_absolute(SmallVectorImpl<char> &path) {
 
   // All of the following conditions will need the current directory.
   SmallString<128> current_dir;
-  if (std::error_code ec = current_path(current_dir))
+  if (use_current_directory)
+    current_directory.toVector(current_dir);
+  else if (std::error_code ec = current_path(current_dir))
     return ec;
 
   // Relative path. Prepend the current directory.
@@ -793,6 +797,15 @@ std::error_code make_absolute(SmallVectorImpl<char> &path) {
                    "occurred above!");
 }
 
+std::error_code make_absolute(const Twine &current_directory,
+                              SmallVectorImpl<char> &path) {
+  return make_absolute(current_directory, path, true);
+}
+
+std::error_code make_absolute(SmallVectorImpl<char> &path) {
+  return make_absolute(Twine(), path, false);
+}
+
 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
                                    perms Perms) {
   SmallString<128> PathStorage;
index a4d2ab1..f35682e 100644 (file)
@@ -150,6 +150,10 @@ TEST(Support, Path) {
 
     path::native(*i, temp_store);
   }
+
+  SmallString<32> Relative("foo.cpp");
+  ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
+  ASSERT_EQ("/root/foo.cpp", Relative);
 }
 
 TEST(Support, RelativePathIterator) {