1 //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the Clang-C Source Indexing library. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CIndexer.h" 15 #include "clang/Basic/LLVM.h" 16 #include "clang/Basic/Version.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/Config/llvm-config.h" 19 #include "llvm/Support/Path.h" 20 #include "llvm/Support/Program.h" 21 #include <cstdio> 22 23 #ifdef __CYGWIN__ 24 #include <cygwin/version.h> 25 #include <sys/cygwin.h> 26 #define LLVM_ON_WIN32 1 27 #endif 28 29 #ifdef LLVM_ON_WIN32 30 #include <windows.h> 31 #else 32 #include <dlfcn.h> 33 #endif 34 35 using namespace clang; 36 37 const std::string &CIndexer::getClangResourcesPath() { 38 // Did we already compute the path? 39 if (!ResourcesPath.empty()) 40 return ResourcesPath; 41 42 SmallString<128> LibClangPath; 43 44 // Find the location where this library lives (libclang.dylib). 45 #ifdef LLVM_ON_WIN32 46 MEMORY_BASIC_INFORMATION mbi; 47 char path[MAX_PATH]; 48 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, 49 sizeof(mbi)); 50 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); 51 52 #ifdef __CYGWIN__ 53 char w32path[MAX_PATH]; 54 strcpy(w32path, path); 55 #if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181 56 cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH); 57 #else 58 cygwin_conv_to_full_posix_path(w32path, path); 59 #endif 60 #endif 61 62 LibClangPath += llvm::sys::path::parent_path(path); 63 #else 64 // This silly cast below avoids a C++ warning. 65 Dl_info info; 66 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) 67 llvm_unreachable("Call to dladdr() failed"); 68 69 // We now have the CIndex directory, locate clang relative to it. 70 LibClangPath += llvm::sys::path::parent_path(info.dli_fname); 71 #endif 72 73 llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING); 74 75 // Cache our result. 76 ResourcesPath = LibClangPath.str(); 77 return ResourcesPath; 78 } 79