[llvm-branch-commits] [llvm] 879c15e - [llvm-rc] Handle driveless absolute windows paths when loading external files
Martin Storsjö via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Dec 10 04:15:20 PST 2020
Author: Martin Storsjö
Date: 2020-12-10T14:11:06+02:00
New Revision: 879c15e890b4d25d28ea904e92497f091f796019
URL: https://github.com/llvm/llvm-project/commit/879c15e890b4d25d28ea904e92497f091f796019
DIFF: https://github.com/llvm/llvm-project/commit/879c15e890b4d25d28ea904e92497f091f796019.diff
LOG: [llvm-rc] Handle driveless absolute windows paths when loading external files
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.
Differential Revision: https://reviews.llvm.org/D92558
Added:
Modified:
llvm/test/tools/llvm-rc/absolute.test
llvm/tools/llvm-rc/ResourceFileWriter.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-rc/absolute.test b/llvm/test/tools/llvm-rc/absolute.test
index 95aff3e42440..fd8b2d68d41e 100644
--- a/llvm/test/tools/llvm-rc/absolute.test
+++ b/llvm/test/tools/llvm-rc/absolute.test
@@ -1,3 +1,7 @@
; RUN: touch %t.manifest
; RUN: echo "1 24 \"%t.manifest\"" > %t.rc
; RUN: llvm-rc -- %t.rc
+;; On Windows, try stripping out the drive name from the absolute path,
+;; and make sure the path still is found.
+; RUN: cat %t.rc | sed 's/"[a-zA-Z]:/"/' > %t2.rc
+; RUN: llvm-rc -- %t2.rc
diff --git a/llvm/tools/llvm-rc/ResourceFileWriter.cpp b/llvm/tools/llvm-rc/ResourceFileWriter.cpp
index c80605aed446..553bb754aea0 100644
--- a/llvm/tools/llvm-rc/ResourceFileWriter.cpp
+++ b/llvm/tools/llvm-rc/ResourceFileWriter.cpp
@@ -1514,8 +1514,16 @@ ResourceFileWriter::loadFile(StringRef File) const {
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.
More information about the llvm-branch-commits
mailing list