Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 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" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 15 | #include "clang/Basic/LLVM.h" |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Version.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Dylan Noblesmith | 1ced737 | 2011-12-22 22:49:47 +0000 | [diff] [blame] | 18 | #include "llvm/Config/llvm-config.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Program.h" |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 21 | #include <cstdio> |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 22 | |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 23 | #ifdef __CYGWIN__ |
NAKAMURA Takumi | d24228a | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 24 | #include <cygwin/version.h> |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 25 | #include <sys/cygwin.h> |
| 26 | #define LLVM_ON_WIN32 1 |
| 27 | #endif |
| 28 | |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 29 | #ifdef LLVM_ON_WIN32 |
| 30 | #include <windows.h> |
| 31 | #else |
| 32 | #include <dlfcn.h> |
| 33 | #endif |
| 34 | |
| 35 | using namespace clang; |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 36 | |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 37 | const std::string &CIndexer::getClangResourcesPath() { |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 38 | // Did we already compute the path? |
Douglas Gregor | bd82998 | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 39 | if (!ResourcesPath.empty()) |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 40 | return ResourcesPath; |
| 41 | |
| 42 | SmallString<128> LibClangPath; |
| 43 | |
Douglas Gregor | bd82998 | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 44 | // Find the location where this library lives (libclang.dylib). |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 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); |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 51 | |
| 52 | #ifdef __CYGWIN__ |
| 53 | char w32path[MAX_PATH]; |
| 54 | strcpy(w32path, path); |
NAKAMURA Takumi | d24228a | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 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 |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 58 | cygwin_conv_to_full_posix_path(w32path, path); |
| 59 | #endif |
NAKAMURA Takumi | d24228a | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 60 | #endif |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 61 | |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 62 | LibClangPath += llvm::sys::path::parent_path(path); |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 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) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 67 | llvm_unreachable("Call to dladdr() failed"); |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 69 | // We now have the CIndex directory, locate clang relative to it. |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 70 | LibClangPath += llvm::sys::path::parent_path(info.dli_fname); |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 71 | #endif |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 72 | |
| 73 | llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING); |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 74 | |
| 75 | // Cache our result. |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 76 | ResourcesPath = LibClangPath.str(); |
| 77 | return ResourcesPath; |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 78 | } |