https://github.com/playerC updated 
https://github.com/llvm/llvm-project/pull/203332

>From 8de7fee4b0d7b6db107c31944411b7c10e362aed Mon Sep 17 00:00:00 2001
From: player <[email protected]>
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 | 30 ++++++++++++++++++--
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clangd/CompileCommands.cpp 
b/clang-tools-extra/clangd/CompileCommands.cpp
index f8bc9a9ca81fd..55efc1207032c 100644
--- a/clang-tools-extra/clangd/CompileCommands.cpp
+++ b/clang-tools-extra/clangd/CompileCommands.cpp
@@ -135,6 +135,25 @@ 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;
+  }
+  log("Detect default -resource-dir={0} not valid", ResourceDir);
+
+  if (ClangPath.has_value()) {
+    ResourceDir = GetResourcesPath(*ClangPath);
+    if (llvm::sys::fs::exists(ResourceDir)) {
+      return ResourceDir;
+    }
+    log("Detect default -resource-dir={0} not valid", ResourceDir);
+  }
+
+  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 +207,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;
 }
@@ -341,8 +360,13 @@ void CommandMangler::operator()(tooling::CompileCommand 
&Command,
   });
 
   std::vector<std::string> ToAppend;
-  if (ResourceDir && !HasExact("-resource-dir") && 
!HasPrefix("-resource-dir="))
-    ToAppend.push_back(("-resource-dir=" + *ResourceDir));
+  if (!HasExact("-resource-dir") && !HasPrefix("-resource-dir=")) {
+    if (ResourceDir) {
+      ToAppend.push_back(("-resource-dir=" + *ResourceDir));
+    } else {
+      elog("No valid -resource-dir option");
+    }
+  }
 
   // Don't set `-isysroot` if it is already set or if `--sysroot` is set.
   // `--sysroot` is a superset of the `-isysroot` argument.

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to