Home | History | Annotate | Download | only in libclang
      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/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"
     23 #include "llvm/Config/llvm-config.h"
     24 #include "llvm/Support/Compiler.h"
     25 #include "llvm/Support/MemoryBuffer.h"
     26 #include "llvm/Support/Program.h"
     27 #include "llvm/Support/raw_ostream.h"
     28 #include <cstdio>
     29 #include <sstream>
     30 #include <vector>
     31 
     32 #ifdef __CYGWIN__
     33 #include <cygwin/version.h>
     34 #include <sys/cygwin.h>
     35 #define LLVM_ON_WIN32 1
     36 #endif
     37 
     38 #ifdef LLVM_ON_WIN32
     39 #include <windows.h>
     40 #else
     41 #include <dlfcn.h>
     42 #endif
     43 
     44 using namespace clang;
     45 
     46 const std::string &CIndexer::getClangResourcesPath() {
     47   // Did we already compute the path?
     48   if (!ResourcesPath.empty())
     49     return ResourcesPath;
     50 
     51   SmallString<128> LibClangPath;
     52 
     53   // Find the location where this library lives (libclang.dylib).
     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);
     60 
     61 #ifdef __CYGWIN__
     62   char w32path[MAX_PATH];
     63   strcpy(w32path, path);
     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
     67   cygwin_conv_to_full_posix_path(w32path, path);
     68 #endif
     69 #endif
     70 
     71   LibClangPath += llvm::sys::path::parent_path(path);
     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)
     76     llvm_unreachable("Call to dladdr() failed");
     77 
     78   // We now have the CIndex directory, locate clang relative to it.
     79   LibClangPath += llvm::sys::path::parent_path(info.dli_fname);
     80 #endif
     81 
     82   llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING);
     83 
     84   // Cache our result.
     85   ResourcesPath = LibClangPath.str();
     86   return ResourcesPath;
     87 }
     88