1 //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Clang-C Source Indexing library.
12 //===----------------------------------------------------------------------===//
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Basic/Version.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/MD5.h"
21 #include "llvm/Support/MutexGuard.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/YAMLParser.h"
28 #include <cygwin/version.h>
29 #include <sys/cygwin.h>
39 using namespace clang;
41 const std::string &CIndexer::getClangResourcesPath() {
42 // Did we already compute the path?
43 if (!ResourcesPath.empty())
46 SmallString<128> LibClangPath;
48 // Find the location where this library lives (libclang.dylib).
50 MEMORY_BASIC_INFORMATION mbi;
52 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
54 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
57 char w32path[MAX_PATH];
58 strcpy(w32path, path);
59 #if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181
60 cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH);
62 cygwin_conv_to_full_posix_path(w32path, path);
66 LibClangPath += llvm::sys::path::parent_path(path);
68 // This silly cast below avoids a C++ warning.
70 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
71 llvm_unreachable("Call to dladdr() failed");
73 // We now have the CIndex directory, locate clang relative to it.
74 LibClangPath += llvm::sys::path::parent_path(info.dli_fname);
77 llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING);
80 ResourcesPath = LibClangPath.str();
84 StringRef CIndexer::getClangToolchainPath() {
85 if (!ToolchainPath.empty())
87 StringRef ResourcePath = getClangResourcesPath();
88 ToolchainPath = llvm::sys::path::parent_path(
89 llvm::sys::path::parent_path(llvm::sys::path::parent_path(ResourcePath)));
93 LibclangInvocationReporter::LibclangInvocationReporter(
94 CIndexer &Idx, OperationKind Op, unsigned ParseOptions,
95 llvm::ArrayRef<const char *> Args,
96 llvm::ArrayRef<std::string> InvocationArgs,
97 llvm::ArrayRef<CXUnsavedFile> UnsavedFiles) {
98 StringRef Path = Idx.getInvocationEmissionPath();
102 // Create a temporary file for the invocation log.
103 SmallString<256> TempPath;
105 llvm::sys::path::append(TempPath, "libclang-%%%%%%%%%%%%");
107 if (llvm::sys::fs::createUniqueFile(TempPath, FD, TempPath))
109 File = std::string(TempPath.begin(), TempPath.end());
110 llvm::raw_fd_ostream OS(FD, /*ShouldClose=*/true);
112 // Write out the information about the invocation to it.
113 auto WriteStringKey = [&OS](StringRef Key, StringRef Value) {
114 OS << R"(")" << Key << R"(":")";
115 OS << llvm::yaml::escape(Value) << '"';
118 WriteStringKey("toolchain", Idx.getClangToolchainPath());
120 WriteStringKey("libclang.operation",
121 Op == OperationKind::ParseOperation ? "parse" : "complete");
123 OS << R"("libclang.opts":)" << ParseOptions;
126 for (const auto &I : llvm::enumerate(Args)) {
129 OS << '"' << llvm::yaml::escape(I.value()) << '"';
131 if (!InvocationArgs.empty()) {
132 OS << R"(],"invocation-args":[)";
133 for (const auto &I : llvm::enumerate(InvocationArgs)) {
136 OS << '"' << llvm::yaml::escape(I.value()) << '"';
139 if (!UnsavedFiles.empty()) {
140 OS << R"(],"unsaved_file_hashes":[)";
141 for (const auto &UF : llvm::enumerate(UnsavedFiles)) {
145 WriteStringKey("name", UF.value().Filename);
148 Hash.update(getContents(UF.value()));
149 llvm::MD5::MD5Result Result;
151 SmallString<32> Digest = Result.digest();
152 WriteStringKey("md5", Digest);
159 LibclangInvocationReporter::~LibclangInvocationReporter() {
161 llvm::sys::fs::remove(File);