blob: 91154cc1c3a079f347a6457e58be6ef48c45e13d [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"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000015#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 Noblesmith1ced7372011-12-22 22:49:47 +000023#include "llvm/Config/llvm-config.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000026#include "llvm/Support/Program.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000028#include <cstdio>
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000029#include <sstream>
Chandler Carruthcc0694c2012-12-04 09:25:21 +000030#include <vector>
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000031
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000032#ifdef __CYGWIN__
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000033#include <cygwin/version.h>
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000034#include <sys/cygwin.h>
35#define LLVM_ON_WIN32 1
36#endif
37
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000038#ifdef LLVM_ON_WIN32
39#include <windows.h>
40#else
41#include <dlfcn.h>
42#endif
43
44using namespace clang;
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000045
Benjamin Kramere3868e42013-06-13 13:56:37 +000046const std::string &CIndexer::getClangResourcesPath() {
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000047 // Did we already compute the path?
Douglas Gregorbd829982010-10-11 23:17:59 +000048 if (!ResourcesPath.empty())
Benjamin Kramere3868e42013-06-13 13:56:37 +000049 return ResourcesPath;
50
51 SmallString<128> LibClangPath;
52
Douglas Gregorbd829982010-10-11 23:17:59 +000053 // Find the location where this library lives (libclang.dylib).
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000054#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 Takumiaa63fdf2010-11-09 03:25:16 +000060
61#ifdef __CYGWIN__
62 char w32path[MAX_PATH];
63 strcpy(w32path, path);
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000064#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 Takumiaa63fdf2010-11-09 03:25:16 +000067 cygwin_conv_to_full_posix_path(w32path, path);
68#endif
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000069#endif
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000070
Benjamin Kramere3868e42013-06-13 13:56:37 +000071 LibClangPath += llvm::sys::path::parent_path(path);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000072#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 Blaikie83d382b2011-09-23 05:06:16 +000076 llvm_unreachable("Call to dladdr() failed");
Benjamin Kramere3868e42013-06-13 13:56:37 +000077
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000078 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramere3868e42013-06-13 13:56:37 +000079 LibClangPath += llvm::sys::path::parent_path(info.dli_fname);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000080#endif
Benjamin Kramere3868e42013-06-13 13:56:37 +000081
82 llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000083
84 // Cache our result.
Benjamin Kramere3868e42013-06-13 13:56:37 +000085 ResourcesPath = LibClangPath.str();
86 return ResourcesPath;
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000087}