Skip to content

Commit b8c855b

Browse files
committed
[Support] Remove error return value from one overload of fs::make_absolute
Summary: The version of make_absolute which accepted a specific directory to use as the "base" for the computation could never fail, even though it returned a std::error_code. The reason for that seems to be historical -- the CWD flavour (which can fail due to failure to retrieve CWD) was there first, and the new version was implemented by extending that. This removes the error return value from the non-CWD overload and reimplements the CWD version on top of that. This enables us to remove some dead code where people were pessimistically trying to handle the errors returned from this function. Reviewers: zturner, sammccall Subscribers: hiraditya, kristina, llvm-commits Differential Revision: https://reviews.llvm.org/D56599 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351317 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6dbe225 commit b8c855b

File tree

5 files changed

+22
-30
lines changed

5 files changed

+22
-30
lines changed

include/llvm/Support/FileSystem.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,7 @@ class file_status : public basic_file_status {
302302
/// relative/../path => <current-directory>/relative/../path
303303
///
304304
/// @param path A path that is modified to be an absolute path.
305-
/// @returns errc::success if \a path has been made absolute, otherwise a
306-
/// platform-specific error_code.
307-
std::error_code make_absolute(const Twine &current_directory,
308-
SmallVectorImpl<char> &path);
305+
void make_absolute(const Twine &current_directory, SmallVectorImpl<char> &path);
309306

310307
/// Make \a path an absolute path.
311308
///

lib/Support/Path.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,8 @@ getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix,
849849
return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
850850
}
851851

852-
static std::error_code make_absolute(const Twine &current_directory,
853-
SmallVectorImpl<char> &path,
854-
bool use_current_directory) {
852+
void make_absolute(const Twine &current_directory,
853+
SmallVectorImpl<char> &path) {
855854
StringRef p(path.data(), path.size());
856855

857856
bool rootDirectory = path::has_root_directory(p);
@@ -860,22 +859,19 @@ static std::error_code make_absolute(const Twine &current_directory,
860859

861860
// Already absolute.
862861
if (rootName && rootDirectory)
863-
return std::error_code();
862+
return;
864863

865864
// All of the following conditions will need the current directory.
866865
SmallString<128> current_dir;
867-
if (use_current_directory)
868-
current_directory.toVector(current_dir);
869-
else if (std::error_code ec = current_path(current_dir))
870-
return ec;
866+
current_directory.toVector(current_dir);
871867

872868
// Relative path. Prepend the current directory.
873869
if (!rootName && !rootDirectory) {
874870
// Append path to the current directory.
875871
path::append(current_dir, p);
876872
// Set path to the result.
877873
path.swap(current_dir);
878-
return std::error_code();
874+
return;
879875
}
880876

881877
if (!rootName && rootDirectory) {
@@ -884,7 +880,7 @@ static std::error_code make_absolute(const Twine &current_directory,
884880
path::append(curDirRootName, p);
885881
// Set path to the result.
886882
path.swap(curDirRootName);
887-
return std::error_code();
883+
return;
888884
}
889885

890886
if (rootName && !rootDirectory) {
@@ -896,20 +892,23 @@ static std::error_code make_absolute(const Twine &current_directory,
896892
SmallString<128> res;
897893
path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
898894
path.swap(res);
899-
return std::error_code();
895+
return;
900896
}
901897

902898
llvm_unreachable("All rootName and rootDirectory combinations should have "
903899
"occurred above!");
904900
}
905901

906-
std::error_code make_absolute(const Twine &current_directory,
907-
SmallVectorImpl<char> &path) {
908-
return make_absolute(current_directory, path, true);
909-
}
910-
911902
std::error_code make_absolute(SmallVectorImpl<char> &path) {
912-
return make_absolute(Twine(), path, false);
903+
if (path::is_absolute(path))
904+
return {};
905+
906+
SmallString<128> current_dir;
907+
if (std::error_code ec = current_path(current_dir))
908+
return ec;
909+
910+
make_absolute(current_dir, path);
911+
return {};
913912
}
914913

915914
std::error_code create_directories(const Twine &Path, bool IgnoreExisting,

lib/Support/VirtualFileSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
128128
if (!WorkingDir)
129129
return WorkingDir.getError();
130130

131-
return llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
131+
llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
132+
return {};
132133
}
133134

134135
std::error_code FileSystem::getRealPath(const Twine &Path,

tools/llvm-opt-report/OptReport.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,8 @@ static bool writeReport(LocationInfoTy &LocationInfo) {
231231
bool FirstFile = true;
232232
for (auto &FI : LocationInfo) {
233233
SmallString<128> FileName(FI.first);
234-
if (!InputRelDir.empty()) {
235-
if (std::error_code EC = sys::fs::make_absolute(InputRelDir, FileName)) {
236-
WithColor::error() << "Can't resolve file path to " << FileName << ": "
237-
<< EC.message() << "\n";
238-
return false;
239-
}
240-
}
234+
if (!InputRelDir.empty())
235+
sys::fs::make_absolute(InputRelDir, FileName);
241236

242237
const auto &FileInfo = FI.second;
243238

unittests/Support/Path.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ TEST(Support, Path) {
187187
}
188188

189189
SmallString<32> Relative("foo.cpp");
190-
ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
190+
sys::fs::make_absolute("/root", Relative);
191191
Relative[5] = '/'; // Fix up windows paths.
192192
ASSERT_EQ("/root/foo.cpp", Relative);
193193
}

0 commit comments

Comments
 (0)