Author: player
Date: 2026-06-20T11:44:22-04:00
New Revision: c888371ff0a3e10f8472676dc992f4347fca58d9

URL: 
https://github.com/llvm/llvm-project/commit/c888371ff0a3e10f8472676dc992f4347fca58d9
DIFF: 
https://github.com/llvm/llvm-project/commit/c888371ff0a3e10f8472676dc992f4347fca58d9.diff

LOG: [clangd] Look for resource-dir relative to detected compiler path as a 
fallback (#203332)

If the standard resource directory (which is searched for relative to the clangd
executable) does not exist, look for one relative to the detected compiler as a
fallback. This handles some packaging schemes where clangd and clang are
installed in different prefixes and the resource directory is only located in 
the
latter.

Also print an error message to the log if the fallback didn't find an existing
directory either.

Added: 
    

Modified: 
    clang-tools-extra/clangd/CompileCommands.cpp

Removed: 
    


################################################################################
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;
 }


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

Reply via email to