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" |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
| 16 | #include "clang/AST/DeclVisitor.h" |
| 17 | #include "clang/AST/StmtVisitor.h" |
| 18 | #include "clang/Basic/FileManager.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Basic/Version.h" |
| 21 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Dylan Noblesmith | 1ced737 | 2011-12-22 22:49:47 +0000 | [diff] [blame] | 23 | #include "llvm/Config/llvm-config.h" |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Program.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 28 | #include <cstdio> |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 29 | #include <sstream> |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 30 | #include <vector> |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 31 | |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 32 | #ifdef __CYGWIN__ |
NAKAMURA Takumi | d24228a | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 33 | #include <cygwin/version.h> |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 34 | #include <sys/cygwin.h> |
| 35 | #define LLVM_ON_WIN32 1 |
| 36 | #endif |
| 37 | |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 38 | #ifdef LLVM_ON_WIN32 |
| 39 | #include <windows.h> |
| 40 | #else |
| 41 | #include <dlfcn.h> |
| 42 | #endif |
| 43 | |
| 44 | using namespace clang; |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 45 | |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 46 | const std::string &CIndexer::getClangResourcesPath() { |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 47 | // Did we already compute the path? |
Douglas Gregor | bd82998 | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 48 | if (!ResourcesPath.empty()) |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 49 | return ResourcesPath; |
| 50 | |
| 51 | SmallString<128> LibClangPath; |
| 52 | |
Douglas Gregor | bd82998 | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 53 | // Find the location where this library lives (libclang.dylib). |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 54 | #ifdef LLVM_ON_WIN32 |
| 55 | MEMORY_BASIC_INFORMATION mbi; |
| 56 | char path[MAX_PATH]; |
| 57 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
| 58 | sizeof(mbi)); |
| 59 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 60 | |
| 61 | #ifdef __CYGWIN__ |
| 62 | char w32path[MAX_PATH]; |
| 63 | strcpy(w32path, path); |
NAKAMURA Takumi | d24228a | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 64 | #if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181 |
| 65 | cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH); |
| 66 | #else |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 67 | cygwin_conv_to_full_posix_path(w32path, path); |
| 68 | #endif |
NAKAMURA Takumi | d24228a | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 69 | #endif |
NAKAMURA Takumi | aa63fdf | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 70 | |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 71 | LibClangPath += llvm::sys::path::parent_path(path); |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 72 | #else |
| 73 | // This silly cast below avoids a C++ warning. |
| 74 | Dl_info info; |
| 75 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 76 | llvm_unreachable("Call to dladdr() failed"); |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 78 | // We now have the CIndex directory, locate clang relative to it. |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 79 | LibClangPath += llvm::sys::path::parent_path(info.dli_fname); |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 80 | #endif |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 81 | |
| 82 | llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING); |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 83 | |
| 84 | // Cache our result. |
Benjamin Kramer | e3868e4 | 2013-06-13 13:56:37 +0000 | [diff] [blame] | 85 | ResourcesPath = LibClangPath.str(); |
| 86 | return ResourcesPath; |
Ted Kremenek | 0ec2cca | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 87 | } |