Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4414,7 +4414,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,
Expand Down
31 changes: 22 additions & 9 deletions clang/tools/libclang/CIndexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,21 @@ const std::string &CIndexer::getClangResourcesPath() {
if (!ResourcesPath.empty())
return ResourcesPath;

SmallString<128> LibClangPath;
if (CachedLibClangPath.empty())
getLibClangPath();

// Cache our result.
ResourcesPath = driver::Driver::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
Expand All @@ -106,17 +120,17 @@ 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)
Dl_info info;
// 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
Expand All @@ -126,19 +140,18 @@ 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 = driver::Driver::GetResourcesPath(LibClangPath);
return ResourcesPath;
CachedLibClangPath = std::string(ResultPath);
return CachedLibClangPath;
}

StringRef CIndexer::getClangToolchainPath() {
Expand Down
4 changes: 4 additions & 0 deletions clang/tools/libclang/CIndexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CIndexer {
bool StorePreamblesInMemory = false;
unsigned Options; // CXGlobalOptFlags.

std::string CachedLibClangPath;
std::string ResourcesPath;
std::shared_ptr<PCHContainerOperations> PCHContainerOps;

Expand Down Expand Up @@ -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) {
Expand Down