blob: 694ed606306cfb8870a319ea0d3ef5a8ee149267 [file] [log] [blame]
Ted Kremenek0ec2cca2010-01-05 19:32:54 +00001//===- 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 Amini9670f842016-07-18 19:02:11 +000015#include "clang/Basic/LLVM.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000016#include "clang/Basic/Version.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000017#include "llvm/ADT/SmallString.h"
Dylan Noblesmith1ced7372011-12-22 22:49:47 +000018#include "llvm/Config/llvm-config.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000019#include "llvm/Support/Path.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000020#include "llvm/Support/Program.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000021#include <cstdio>
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000022
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000023#ifdef __CYGWIN__
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000024#include <cygwin/version.h>
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000025#include <sys/cygwin.h>
26#define LLVM_ON_WIN32 1
27#endif
28
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000029#ifdef LLVM_ON_WIN32
30#include <windows.h>
31#else
32#include <dlfcn.h>
33#endif
34
35using namespace clang;
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000036
Benjamin Kramere3868e42013-06-13 13:56:37 +000037const std::string &CIndexer::getClangResourcesPath() {
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000038 // Did we already compute the path?
Douglas Gregorbd829982010-10-11 23:17:59 +000039 if (!ResourcesPath.empty())
Benjamin Kramere3868e42013-06-13 13:56:37 +000040 return ResourcesPath;
41
42 SmallString<128> LibClangPath;
43
Douglas Gregorbd829982010-10-11 23:17:59 +000044 // Find the location where this library lives (libclang.dylib).
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000045#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 Takumiaa63fdf2010-11-09 03:25:16 +000051
52#ifdef __CYGWIN__
53 char w32path[MAX_PATH];
54 strcpy(w32path, path);
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000055#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 Takumiaa63fdf2010-11-09 03:25:16 +000058 cygwin_conv_to_full_posix_path(w32path, path);
59#endif
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000060#endif
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000061
Benjamin Kramere3868e42013-06-13 13:56:37 +000062 LibClangPath += llvm::sys::path::parent_path(path);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000063#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 Blaikie83d382b2011-09-23 05:06:16 +000067 llvm_unreachable("Call to dladdr() failed");
Benjamin Kramere3868e42013-06-13 13:56:37 +000068
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000069 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramere3868e42013-06-13 13:56:37 +000070 LibClangPath += llvm::sys::path::parent_path(info.dli_fname);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000071#endif
Benjamin Kramere3868e42013-06-13 13:56:37 +000072
73 llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000074
75 // Cache our result.
Benjamin Kramere3868e42013-06-13 13:56:37 +000076 ResourcesPath = LibClangPath.str();
77 return ResourcesPath;
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000078}