https://github.com/Lachlan-Frawley updated https://github.com/llvm/llvm-project/pull/164234
>From 1671830f83830b4d1ed0da5bb4ad6330b484c090 Mon Sep 17 00:00:00 2001 From: Lachlan Frawley <[email protected]> Date: Sat, 14 Mar 2026 15:57:40 +1000 Subject: [PATCH] Feed in libclang path inside clang_parseTranslationUnit2() to ensure clang correctly determines its resource paths. Also add a new function to explicitly get the path to libclang. --- clang/tools/libclang/CIndex.cpp | 6 +++++- clang/tools/libclang/CIndexer.cpp | 30 ++++++++++++++++++++++-------- clang/tools/libclang/CIndexer.h | 4 ++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 31b6a3222d916..ea88f9ce9b591 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -4445,7 +4445,11 @@ enum CXErrorCode clang_parseTranslationUnit2( unsigned options, CXTranslationUnit *out_TU) { noteBottomOfStack(); SmallVector<const char *, 4> Args; - Args.push_back("clang"); + + CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); + auto library_path = CXXIdx->getLibClangPath(); + Args.push_back(library_path.c_str()); + Args.append(command_line_args, command_line_args + num_command_line_args); return clang_parseTranslationUnit2FullArgv( CIdx, source_filename, Args.data(), Args.size(), unsaved_files, diff --git a/clang/tools/libclang/CIndexer.cpp b/clang/tools/libclang/CIndexer.cpp index 853a936b43e37..87b66e1af37c4 100644 --- a/clang/tools/libclang/CIndexer.cpp +++ b/clang/tools/libclang/CIndexer.cpp @@ -97,7 +97,21 @@ const std::string &CIndexer::getClangResourcesPath() { if (!ResourcesPath.empty()) return ResourcesPath; - SmallString<128> LibClangPath; + if (CachedLibClangPath.empty()) + getLibClangPath(); + + // Cache our result. + ResourcesPath = GetResourcesPath(CachedLibClangPath); + return ResourcesPath; +} + +const std::string &CIndexer::getLibClangPath() +{ + // Did we already compute the path? + if (!CachedLibClangPath.empty()) + return CachedLibClangPath; + + SmallString<128> ResultPath; // Find the location where this library lives (libclang.dylib). #ifdef _WIN32 @@ -107,9 +121,9 @@ const std::string &CIndexer::getClangResourcesPath() { sizeof(mbi)); GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); - LibClangPath += path; + ResultPath += path; #elif defined(_AIX) - getClangResourcesPathImplAIX(LibClangPath); + getClangResourcesPathImplAIX(ResultPath); #else bool PathFound = false; #if defined(CLANG_HAVE_DLFCN_H) && defined(CLANG_HAVE_DLADDR) @@ -117,7 +131,7 @@ const std::string &CIndexer::getClangResourcesPath() { // This silly cast below avoids a C++ warning. if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) != 0) { // We now have the CIndex directory, locate clang relative to it. - LibClangPath += info.dli_fname; + ResultPath += info.dli_fname; PathFound = true; } #endif @@ -127,19 +141,19 @@ const std::string &CIndexer::getClangResourcesPath() { // If we can't get the path using dladdr, try to get the main executable // path. This may be needed when we're statically linking libclang with // musl libc, for example. - LibClangPath += Path; + ResultPath += Path; } else { // It's rather unlikely we end up here. But it could happen, so report an // error instead of crashing. - llvm::report_fatal_error("could not locate Clang resource path"); + llvm::report_fatal_error("could not locate libclang path"); } } #endif // Cache our result. - ResourcesPath = GetResourcesPath(LibClangPath); - return ResourcesPath; + CachedLibClangPath = std::string(ResultPath); + return CachedLibClangPath; } StringRef CIndexer::getClangToolchainPath() { diff --git a/clang/tools/libclang/CIndexer.h b/clang/tools/libclang/CIndexer.h index 83268a2016c8f..226fa5bf6e3f1 100644 --- a/clang/tools/libclang/CIndexer.h +++ b/clang/tools/libclang/CIndexer.h @@ -37,6 +37,7 @@ class CIndexer { bool StorePreamblesInMemory = false; unsigned Options; // CXGlobalOptFlags. + std::string CachedLibClangPath; std::string ResourcesPath; std::shared_ptr<PCHContainerOperations> PCHContainerOps; @@ -77,6 +78,9 @@ class CIndexer { /// Get the path of the clang resource files. const std::string &getClangResourcesPath(); + /// Get the path of libclang. + const std::string &getLibClangPath(); + StringRef getClangToolchainPath(); void setStorePreamblesInMemory(bool StoreInMemory) { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
