[PATCH] D92558: [llvm-rc] Handle driveless absolute windows paths when loading external files
Martin Storsjö via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 3 02:15:24 PST 2020
mstorsjo created this revision.
mstorsjo added reviewers: amccarth, thakis.
Herald added a project: LLVM.
mstorsjo requested review of this revision.
When llvm-rc loads an external file, it looks for it relative to a number of include directories and the current working directory. If the path is considered absolute, llvm-rc tries to open the filename as such, and doesn't try to open it relative to other paths.
On Windows, a path name like "\dir\file" isn't considered absolute as it lacks the drive name, but by appending it on top of the search dirs, it's not found.
LLVM's sys::path::append just appends such a path (same with a properly absolute posix path) after the paths it's supposed to be relative to.
This fix doesn't handle the case if the resource script and the external file are on a different drive than the current working directory; to fix that, we'd have to make LLVM's sys::path::append handle appending fully absolute and partially absolute paths (ones lacking a drive prefix but containing a root directory), or switch to C++17's std::filesystem.
There's an existing testcase for fully absolute paths, using lit's %t to expand to an absolute path. Making a testcase for this would probably require a bit more scripting in the test file, to strip out the drive name prefix from the absolute path, and embed that into a resource script.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D92558
Files:
llvm/tools/llvm-rc/ResourceFileWriter.cpp
Index: llvm/tools/llvm-rc/ResourceFileWriter.cpp
===================================================================
--- llvm/tools/llvm-rc/ResourceFileWriter.cpp
+++ llvm/tools/llvm-rc/ResourceFileWriter.cpp
@@ -1514,8 +1514,16 @@
SmallString<128> Cwd;
std::unique_ptr<MemoryBuffer> Result;
- // 0. The file path is absolute and the file exists.
- if (sys::path::is_absolute(File))
+ // 0. The file path is absolute or has a root directory, so we shouldn't
+ // try to append it on top of other base directories. (An absolute path
+ // must have a root directory, but e.g. the path "\dir\file" on windows
+ // isn't considered absolute, but it does have a root directory. As long as
+ // sys::path::append doesn't handle appending an absolute path or a path
+ // starting with a root directory on top of a base, we must handle this
+ // case separately at the top. C++17's path::append handles that case
+ // properly though, so if using that to append paths below, this early
+ // exception case could be removed.)
+ if (sys::path::has_root_directory(File))
return errorOrToExpected(MemoryBuffer::getFile(File, -1, false));
// 1. The current working directory.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92558.309197.patch
Type: text/x-patch
Size: 1193 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201203/1a380090/attachment-0001.bin>
More information about the llvm-commits
mailing list