[clang-tools-extra] [clangd] fix wrong resouce-dir (PR #203332)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 18 18:12:57 PDT 2026
https://github.com/playerC updated https://github.com/llvm/llvm-project/pull/203332
>From 43e24b2daf354ebb4c45af990d9b858b92031f46 Mon Sep 17 00:00:00 2001
From: player <playerc at msn.cn>
Date: Fri, 12 Jun 2026 00:22:01 +0800
Subject: [PATCH] [clangd] fix wrong resouce-dir
clangd and LLVM are installed separately. When clangd and
LLVM are installed under different install prefixes,
clangd constructs a fake resource-dir based on its own
installation directory and set it to the underlying Clang compilers.
This change modifies the logic to detect default resource-dir, and
add some logs.
detect resource-dir logic:
`XX` is clang version clangd is depend on.
if relative to clangd , `../lib/clang/XX` exsits, use it.
if relative to default clang , `../lib/clang/XX` exsits, use it.
or ResourceDir is null.
And when default resource-dir is null , it will show an error
on create CommandMangler.
---
clang-tools-extra/clangd/CompileCommands.cpp | 24 +++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clangd/CompileCommands.cpp b/clang-tools-extra/clangd/CompileCommands.cpp
index f8bc9a9ca81fd..e8005435e1836 100644
--- a/clang-tools-extra/clangd/CompileCommands.cpp
+++ b/clang-tools-extra/clangd/CompileCommands.cpp
@@ -135,6 +135,28 @@ std::string detectStandardResourceDir() {
return GetResourcesPath("clangd", (void *)&StaticForMainAddr);
}
+std::optional<std::string>
+detectResourceDirWithClangPath(std::optional<std::string> ClangPath) {
+ std::string ResourceDir = detectStandardResourceDir();
+ if (llvm::sys::fs::exists(ResourceDir))
+ return ResourceDir;
+ vlog("Auto-detected standard resource directory '{0}' doesn't exist",
+ ResourceDir);
+
+ if (ClangPath) {
+ ResourceDir = GetResourcesPath(*ClangPath);
+ if (llvm::sys::fs::exists(ResourceDir))
+ return ResourceDir;
+ vlog("Auto-detected using clang path '{0}' "
+ "resource directory '{1}' doesn't exist",
+ *ClangPath, ResourceDir);
+ }
+
+ elog("Failed to auto-detect resource directory, "
+ "specify it manually via --resource-dir command line argument");
+ return std::nullopt;
+}
+
// The path passed to argv[0] is important:
// - its parent directory is Driver::Dir, used for library discovery
// - its basename affects CLI parsing (clang-cl) and other settings
@@ -188,7 +210,7 @@ static std::string resolveDriver(llvm::StringRef Driver, bool FollowSymlink,
CommandMangler CommandMangler::detect() {
CommandMangler Result;
Result.ClangPath = detectClangPath();
- Result.ResourceDir = detectStandardResourceDir();
+ Result.ResourceDir = detectResourceDirWithClangPath(Result.ClangPath);
Result.Sysroot = detectSysroot();
return Result;
}
More information about the cfe-commits
mailing list