Driver: Avoid llvm::sys::path::append if resource directory absolute.

After #145996 CLANG_RESOURCE_DIR can be an absolute path so we need to
handle it correctly in the driver.

llvm::sys::path::append does not append absolute paths in the way
that I expected (or consistent with other similar APIs such as C++17
std::filesystem::path::append or Python os.path.join); instead, it
effectively discards the leading / and appends the resulting relative path
(e.g. append(P, "/bar") with P = "/foo" sets P to "/foo/bar").

Many tests start failing if I try to align llvm::sys::path::append with
the other APIs because of callers that expect the existing behavior,
so for now let's add a special case here for absolute resource paths,
and document the behavior in Path.h.

Reviewers: MaskRay

Reviewed By: MaskRay

Pull Request: https://github.com/llvm/llvm-project/pull/146449
This commit is contained in:
Peter Collingbourne
2025-07-01 20:21:51 -07:00
committed by GitHub
parent aa1d9a4c31
commit 2a702cdc38
2 changed files with 8 additions and 1 deletions

View File

@@ -192,7 +192,12 @@ std::string Driver::GetResourcesPath(StringRef BinaryPath) {
StringRef ConfiguredResourceDir(CLANG_RESOURCE_DIR);
if (!ConfiguredResourceDir.empty()) {
llvm::sys::path::append(P, ConfiguredResourceDir);
// FIXME: We should fix the behavior of llvm::sys::path::append so we don't
// need to check for absolute paths here.
if (llvm::sys::path::is_absolute(ConfiguredResourceDir))
P = ConfiguredResourceDir;
else
llvm::sys::path::append(P, ConfiguredResourceDir);
} else {
// On Windows, libclang.dll is in bin/.
// On non-Windows, libclang.so/.dylib is in lib/.

View File

@@ -223,6 +223,8 @@ LLVM_ABI bool remove_dots(SmallVectorImpl<char> &path,
/// /foo + bar/f => /foo/bar/f
/// /foo/ + bar/f => /foo/bar/f
/// foo + bar/f => foo/bar/f
/// foo + /bar/f => foo/bar/f (FIXME: will be changed to /bar/f to align
/// with C++17 std::filesystem::path::append)
/// @endcode
///
/// @param path Set to \a path + \a component.