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