llvm.org GIT mirror llvm / b8c855b
[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 Pavel Labath 7 months ago
5 changed file(s) with 22 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
301301 /// relative/../path => /relative/../path
302302 ///
303303 /// @param path A path that is modified to be an absolute path.
304 /// @returns errc::success if \a path has been made absolute, otherwise a
305 /// platform-specific error_code.
306 std::error_code make_absolute(const Twine ¤t_directory,
307 SmallVectorImpl &path);
304 void make_absolute(const Twine ¤t_directory, SmallVectorImpl &path);
308305
309306 /// Make \a path an absolute path.
310307 ///
848848 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
849849 }
850850
851 static std::error_code make_absolute(const Twine ¤t_directory,
852 SmallVectorImpl &path,
853 bool use_current_directory) {
851 void make_absolute(const Twine ¤t_directory,
852 SmallVectorImpl &path) {
854853 StringRef p(path.data(), path.size());
855854
856855 bool rootDirectory = path::has_root_directory(p);
859858
860859 // Already absolute.
861860 if (rootName && rootDirectory)
862 return std::error_code();
861 return;
863862
864863 // All of the following conditions will need the current directory.
865864 SmallString<128> current_dir;
866 if (use_current_directory)
867 current_directory.toVector(current_dir);
868 else if (std::error_code ec = current_path(current_dir))
869 return ec;
865 current_directory.toVector(current_dir);
870866
871867 // Relative path. Prepend the current directory.
872868 if (!rootName && !rootDirectory) {
874870 path::append(current_dir, p);
875871 // Set path to the result.
876872 path.swap(current_dir);
877 return std::error_code();
873 return;
878874 }
879875
880876 if (!rootName && rootDirectory) {
883879 path::append(curDirRootName, p);
884880 // Set path to the result.
885881 path.swap(curDirRootName);
886 return std::error_code();
882 return;
887883 }
888884
889885 if (rootName && !rootDirectory) {
895891 SmallString<128> res;
896892 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
897893 path.swap(res);
898 return std::error_code();
894 return;
899895 }
900896
901897 llvm_unreachable("All rootName and rootDirectory combinations should have "
902898 "occurred above!");
903899 }
904900
905 std::error_code make_absolute(const Twine ¤t_directory,
906 SmallVectorImpl &path) {
907 return make_absolute(current_directory, path, true);
908 }
909
910901 std::error_code make_absolute(SmallVectorImpl &path) {
911 return make_absolute(Twine(), path, false);
902 if (path::is_absolute(path))
903 return {};
904
905 SmallString<128> current_dir;
906 if (std::error_code ec = current_path(current_dir))
907 return ec;
908
909 make_absolute(current_dir, path);
910 return {};
912911 }
913912
914913 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
127127 if (!WorkingDir)
128128 return WorkingDir.getError();
129129
130 return llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
130 llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
131 return {};
131132 }
132133
133134 std::error_code FileSystem::getRealPath(const Twine &Path,
230230 bool FirstFile = true;
231231 for (auto &FI : LocationInfo) {
232232 SmallString<128> FileName(FI.first);
233 if (!InputRelDir.empty()) {
234 if (std::error_code EC = sys::fs::make_absolute(InputRelDir, FileName)) {
235 WithColor::error() << "Can't resolve file path to " << FileName << ": "
236 << EC.message() << "\n";
237 return false;
238 }
239 }
233 if (!InputRelDir.empty())
234 sys::fs::make_absolute(InputRelDir, FileName);
240235
241236 const auto &FileInfo = FI.second;
242237
186186 }
187187
188188 SmallString<32> Relative("foo.cpp");
189 ASSERT_NO_ERROR(sys::fs::make_absolute("/root", Relative));
189 sys::fs::make_absolute("/root", Relative);
190190 Relative[5] = '/'; // Fix up windows paths.
191191 ASSERT_EQ("/root/foo.cpp", Relative);
192192 }