2010-01-06 03:32:54 +08:00
|
|
|
//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Clang-C Source Indexing library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CIndexer.h"
|
2017-12-05 10:30:43 +08:00
|
|
|
#include "CXString.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2017-12-05 05:56:36 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-12-23 06:49:47 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-12-05 10:30:43 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2017-12-05 05:56:36 +08:00
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
2010-11-09 11:25:16 +08:00
|
|
|
#ifdef __CYGWIN__
|
2011-03-09 06:17:33 +08:00
|
|
|
#include <cygwin/version.h>
|
2010-11-09 11:25:16 +08:00
|
|
|
#include <sys/cygwin.h>
|
|
|
|
#define LLVM_ON_WIN32 1
|
|
|
|
#endif
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2013-06-13 21:56:37 +08:00
|
|
|
const std::string &CIndexer::getClangResourcesPath() {
|
2010-01-06 03:32:54 +08:00
|
|
|
// Did we already compute the path?
|
2010-10-12 07:17:59 +08:00
|
|
|
if (!ResourcesPath.empty())
|
2013-06-13 21:56:37 +08:00
|
|
|
return ResourcesPath;
|
|
|
|
|
|
|
|
SmallString<128> LibClangPath;
|
|
|
|
|
2010-10-12 07:17:59 +08:00
|
|
|
// Find the location where this library lives (libclang.dylib).
|
2010-01-06 03:32:54 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
char path[MAX_PATH];
|
|
|
|
VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
|
|
|
|
sizeof(mbi));
|
|
|
|
GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
|
2010-11-09 11:25:16 +08:00
|
|
|
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
char w32path[MAX_PATH];
|
|
|
|
strcpy(w32path, path);
|
2011-03-09 06:17:33 +08:00
|
|
|
#if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181
|
|
|
|
cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH);
|
|
|
|
#else
|
2010-11-09 11:25:16 +08:00
|
|
|
cygwin_conv_to_full_posix_path(w32path, path);
|
2011-03-09 06:17:33 +08:00
|
|
|
#endif
|
2010-11-09 11:25:16 +08:00
|
|
|
#endif
|
|
|
|
|
2013-06-13 21:56:37 +08:00
|
|
|
LibClangPath += llvm::sys::path::parent_path(path);
|
2010-01-06 03:32:54 +08:00
|
|
|
#else
|
|
|
|
// This silly cast below avoids a C++ warning.
|
|
|
|
Dl_info info;
|
|
|
|
if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Call to dladdr() failed");
|
2013-06-13 21:56:37 +08:00
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
// We now have the CIndex directory, locate clang relative to it.
|
2013-06-13 21:56:37 +08:00
|
|
|
LibClangPath += llvm::sys::path::parent_path(info.dli_fname);
|
2010-01-06 03:32:54 +08:00
|
|
|
#endif
|
2013-06-13 21:56:37 +08:00
|
|
|
|
|
|
|
llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING);
|
2010-01-06 03:32:54 +08:00
|
|
|
|
|
|
|
// Cache our result.
|
2013-06-13 21:56:37 +08:00
|
|
|
ResourcesPath = LibClangPath.str();
|
|
|
|
return ResourcesPath;
|
2010-01-06 03:32:54 +08:00
|
|
|
}
|
2017-12-05 05:56:36 +08:00
|
|
|
|
|
|
|
StringRef CIndexer::getClangToolchainPath() {
|
|
|
|
if (!ToolchainPath.empty())
|
|
|
|
return ToolchainPath;
|
|
|
|
StringRef ResourcePath = getClangResourcesPath();
|
|
|
|
ToolchainPath = llvm::sys::path::parent_path(
|
|
|
|
llvm::sys::path::parent_path(llvm::sys::path::parent_path(ResourcePath)));
|
|
|
|
return ToolchainPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
LibclangInvocationReporter::LibclangInvocationReporter(
|
|
|
|
CIndexer &Idx, OperationKind Op, unsigned ParseOptions,
|
2017-12-05 10:30:43 +08:00
|
|
|
llvm::ArrayRef<const char *> Args,
|
2017-12-08 04:37:50 +08:00
|
|
|
llvm::ArrayRef<std::string> InvocationArgs,
|
2017-12-05 10:30:43 +08:00
|
|
|
llvm::ArrayRef<CXUnsavedFile> UnsavedFiles) {
|
2017-12-05 05:56:36 +08:00
|
|
|
StringRef Path = Idx.getInvocationEmissionPath();
|
|
|
|
if (Path.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a temporary file for the invocation log.
|
|
|
|
SmallString<256> TempPath;
|
|
|
|
TempPath = Path;
|
|
|
|
llvm::sys::path::append(TempPath, "libclang-%%%%%%%%%%%%");
|
|
|
|
int FD;
|
|
|
|
if (llvm::sys::fs::createUniqueFile(TempPath, FD, TempPath))
|
|
|
|
return;
|
|
|
|
File = std::string(TempPath.begin(), TempPath.end());
|
|
|
|
llvm::raw_fd_ostream OS(FD, /*ShouldClose=*/true);
|
|
|
|
|
|
|
|
// Write out the information about the invocation to it.
|
|
|
|
auto WriteStringKey = [&OS](StringRef Key, StringRef Value) {
|
|
|
|
OS << R"(")" << Key << R"(":")";
|
|
|
|
OS << Value << '"';
|
|
|
|
};
|
|
|
|
OS << '{';
|
|
|
|
WriteStringKey("toolchain", Idx.getClangToolchainPath());
|
|
|
|
OS << ',';
|
|
|
|
WriteStringKey("libclang.operation",
|
|
|
|
Op == OperationKind::ParseOperation ? "parse" : "complete");
|
|
|
|
OS << ',';
|
|
|
|
OS << R"("libclang.opts":)" << ParseOptions;
|
|
|
|
OS << ',';
|
|
|
|
OS << R"("args":[)";
|
|
|
|
for (const auto &I : llvm::enumerate(Args)) {
|
|
|
|
if (I.index())
|
|
|
|
OS << ',';
|
|
|
|
OS << '"' << I.value() << '"';
|
|
|
|
}
|
2017-12-08 04:37:50 +08:00
|
|
|
if (!InvocationArgs.empty()) {
|
|
|
|
OS << R"(],"invocation-args":[)";
|
|
|
|
for (const auto &I : llvm::enumerate(InvocationArgs)) {
|
|
|
|
if (I.index())
|
|
|
|
OS << ',';
|
|
|
|
OS << '"' << I.value() << '"';
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 10:30:43 +08:00
|
|
|
if (!UnsavedFiles.empty()) {
|
|
|
|
OS << R"(],"unsaved_file_hashes":[)";
|
|
|
|
for (const auto &UF : llvm::enumerate(UnsavedFiles)) {
|
|
|
|
if (UF.index())
|
|
|
|
OS << ',';
|
|
|
|
OS << '{';
|
|
|
|
WriteStringKey("name", UF.value().Filename);
|
|
|
|
OS << ',';
|
|
|
|
llvm::MD5 Hash;
|
|
|
|
Hash.update(getContents(UF.value()));
|
|
|
|
llvm::MD5::MD5Result Result;
|
|
|
|
Hash.final(Result);
|
|
|
|
SmallString<32> Digest = Result.digest();
|
|
|
|
WriteStringKey("md5", Digest);
|
|
|
|
OS << '}';
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 05:56:36 +08:00
|
|
|
OS << "]}";
|
|
|
|
}
|
|
|
|
|
|
|
|
LibclangInvocationReporter::~LibclangInvocationReporter() {
|
|
|
|
if (!File.empty())
|
|
|
|
llvm::sys::fs::remove(File);
|
|
|
|
}
|