[llvm] r249309 - [Support] Add a version of fs::make_absolute with a custom CWD.
Benjamin Kramer via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 5 06:02:44 PDT 2015
Author: d0k
Date: Mon Oct 5 08:02:43 2015
New Revision: 249309
URL: http://llvm.org/viewvc/llvm-project?rev=249309&view=rev
Log:
[Support] Add a version of fs::make_absolute with a custom CWD.
This will be used soon from clang.
Modified:
llvm/trunk/include/llvm/Support/FileSystem.h
llvm/trunk/lib/Support/Path.cpp
llvm/trunk/unittests/Support/Path.cpp
Modified: llvm/trunk/include/llvm/Support/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=249309&r1=249308&r2=249309&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)
+++ llvm/trunk/include/llvm/Support/FileSystem.h Mon Oct 5 08:02:43 2015
@@ -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 ¤t_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.
///
Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=249309&r1=249308&r2=249309&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Mon Oct 5 08:02:43 2015
@@ -740,7 +740,9 @@ std::error_code createUniqueDirectory(co
true, 0, FS_Dir);
}
-std::error_code make_absolute(SmallVectorImpl<char> &path) {
+static std::error_code make_absolute(const Twine ¤t_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(SmallVecto
// 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(SmallVecto
"occurred above!");
}
+std::error_code make_absolute(const Twine ¤t_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;
Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=249309&r1=249308&r2=249309&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Mon Oct 5 08:02:43 2015
@@ -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) {
More information about the llvm-commits
mailing list