Ted Kremenek | ab18893 | 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" |
| 15 | |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
| 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/Version.h" |
| 22 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/Config/config.h" |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include "llvm/System/Program.h" |
| 29 | |
| 30 | #include <cstdio> |
| 31 | #include <vector> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #ifdef LLVM_ON_WIN32 |
| 35 | #include <windows.h> |
| 36 | #else |
| 37 | #include <dlfcn.h> |
| 38 | #endif |
| 39 | |
| 40 | using namespace clang; |
| 41 | using namespace idx; |
| 42 | |
| 43 | const llvm::sys::Path& CIndexer::getClangPath() { |
| 44 | // Did we already compute the path? |
| 45 | if (!ClangPath.empty()) |
| 46 | return ClangPath; |
| 47 | |
| 48 | // Find the location where this library lives (libCIndex.dylib). |
| 49 | #ifdef LLVM_ON_WIN32 |
| 50 | MEMORY_BASIC_INFORMATION mbi; |
| 51 | char path[MAX_PATH]; |
| 52 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
| 53 | sizeof(mbi)); |
| 54 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
| 55 | |
| 56 | llvm::sys::Path CIndexPath(path); |
| 57 | |
| 58 | CIndexPath.eraseComponent(); |
| 59 | CIndexPath.appendComponent("clang"); |
| 60 | CIndexPath.appendSuffix("exe"); |
| 61 | CIndexPath.makeAbsolute(); |
| 62 | #else |
| 63 | // This silly cast below avoids a C++ warning. |
| 64 | Dl_info info; |
| 65 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 66 | assert(0 && "Call to dladdr() failed"); |
| 67 | |
| 68 | llvm::sys::Path CIndexPath(info.dli_fname); |
| 69 | |
| 70 | // We now have the CIndex directory, locate clang relative to it. |
| 71 | CIndexPath.eraseComponent(); |
| 72 | CIndexPath.eraseComponent(); |
| 73 | CIndexPath.appendComponent("bin"); |
| 74 | CIndexPath.appendComponent("clang"); |
| 75 | #endif |
| 76 | |
| 77 | // Cache our result. |
| 78 | ClangPath = CIndexPath; |
| 79 | return ClangPath; |
| 80 | } |
| 81 | |
| 82 | std::string CIndexer::getClangResourcesPath() { |
| 83 | llvm::sys::Path P = getClangPath(); |
| 84 | |
| 85 | if (!P.empty()) { |
| 86 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 87 | P.eraseComponent(); // Remove /bin from foo/bin |
| 88 | |
| 89 | // Get foo/lib/clang/<version>/include |
| 90 | P.appendComponent("lib"); |
| 91 | P.appendComponent("clang"); |
| 92 | P.appendComponent(CLANG_VERSION_STRING); |
| 93 | } |
| 94 | |
| 95 | return P.str(); |
| 96 | } |