1//===- CIndexer.cpp - Clang-C Source Indexing Library ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Clang-C Source Indexing library.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIndexer.h"
14#include "CXString.h"
15#include "clang/Basic/LLVM.h"
16#include "clang/Basic/Version.h"
17#include "clang/Config/config.h"
18#include "clang/Driver/Driver.h"
19#include "clang/Options/OptionUtils.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/MD5.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/Program.h"
26#include "llvm/Support/YAMLParser.h"
27#include <cstdio>
28#include <mutex>
29
30#ifdef _WIN32
31#include <windows.h>
32#elif defined(_AIX)
33#include <errno.h>
34#include <sys/ldr.h>
35#else
36#include <dlfcn.h>
37#endif
38
39using namespace clang;
40
41#ifdef _AIX
42namespace clang {
43namespace {
44
45template <typename LibClangPathType>
46void getClangResourcesPathImplAIX(LibClangPathType &LibClangPath) {
47 int PrevErrno = errno;
48
49 size_t BufSize = 2048u;
50 std::unique_ptr<char[]> Buf;
51 while (true) {
52 Buf = std::make_unique<char []>(BufSize);
53 errno = 0;
54 int Ret = loadquery(L_GETXINFO, Buf.get(), (unsigned int)BufSize);
55 if (Ret != -1)
56 break; // loadquery() was successful.
57 if (errno != ENOMEM)
58 llvm_unreachable("Encountered an unexpected loadquery() failure");
59
60 // errno == ENOMEM; try to allocate more memory.
61 if ((BufSize & ~((-1u) >> 1u)) != 0u)
62 llvm::report_fatal_error("BufSize needed for loadquery() too large");
63
64 Buf.release();
65 BufSize <<= 1u;
66 }
67
68 // Extract the function entry point from the function descriptor.
69 uint64_t EntryAddr =
70 reinterpret_cast<uintptr_t &>(clang_createTranslationUnit);
71
72 // Loop to locate the function entry point in the loadquery() results.
73 ld_xinfo *CurInfo = reinterpret_cast<ld_xinfo *>(Buf.get());
74 while (true) {
75 uint64_t CurTextStart = (uint64_t)CurInfo->ldinfo_textorg;
76 uint64_t CurTextEnd = CurTextStart + CurInfo->ldinfo_textsize;
77 if (CurTextStart <= EntryAddr && EntryAddr < CurTextEnd)
78 break; // Successfully located.
79
80 if (CurInfo->ldinfo_next == 0u)
81 llvm::report_fatal_error("Cannot locate entry point in "
82 "the loadquery() results");
83 CurInfo = reinterpret_cast<ld_xinfo *>(reinterpret_cast<char *>(CurInfo) +
84 CurInfo->ldinfo_next);
85 }
86
87 LibClangPath += reinterpret_cast<char *>(CurInfo) + CurInfo->ldinfo_filename;
88 errno = PrevErrno;
89}
90
91} // end anonymous namespace
92} // end namespace clang
93#endif
94
95const std::string &CIndexer::getClangResourcesPath() {
96 // Did we already compute the path?
97 if (!ResourcesPath.empty())
98 return ResourcesPath;
99
100 SmallString<128> LibClangPath;
101
102 // Find the location where this library lives (libclang.dylib).
103#ifdef _WIN32
104 MEMORY_BASIC_INFORMATION mbi;
105 char path[MAX_PATH];
106 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
107 sizeof(mbi));
108 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
109
110 LibClangPath += path;
111#elif defined(_AIX)
112 getClangResourcesPathImplAIX(LibClangPath);
113#else
114 bool PathFound = false;
115#if defined(CLANG_HAVE_DLFCN_H) && defined(CLANG_HAVE_DLADDR)
116 Dl_info info;
117 // This silly cast below avoids a C++ warning.
118 if (dladdr(address: (void *)(uintptr_t)clang_createTranslationUnit, info: &info) != 0) {
119 // We now have the CIndex directory, locate clang relative to it.
120 LibClangPath += info.dli_fname;
121 PathFound = true;
122 }
123#endif
124 std::string Path;
125 if (!PathFound) {
126 if (!(Path = llvm::sys::fs::getMainExecutable(argv0: nullptr, MainExecAddr: nullptr)).empty()) {
127 // If we can't get the path using dladdr, try to get the main executable
128 // path. This may be needed when we're statically linking libclang with
129 // musl libc, for example.
130 LibClangPath += Path;
131 } else {
132 // It's rather unlikely we end up here. But it could happen, so report an
133 // error instead of crashing.
134 llvm::report_fatal_error(reason: "could not locate Clang resource path");
135 }
136 }
137
138#endif
139
140 // Cache our result.
141 ResourcesPath = GetResourcesPath(BinaryPath: LibClangPath);
142 return ResourcesPath;
143}
144
145StringRef CIndexer::getClangToolchainPath() {
146 if (!ToolchainPath.empty())
147 return ToolchainPath;
148 StringRef ResourcePath = getClangResourcesPath();
149 ToolchainPath =
150 std::string(llvm::sys::path::parent_path(path: llvm::sys::path::parent_path(
151 path: llvm::sys::path::parent_path(path: ResourcePath))));
152 return ToolchainPath;
153}
154
155LibclangInvocationReporter::LibclangInvocationReporter(
156 CIndexer &Idx, OperationKind Op, unsigned ParseOptions,
157 llvm::ArrayRef<const char *> Args,
158 llvm::ArrayRef<std::string> InvocationArgs,
159 llvm::ArrayRef<CXUnsavedFile> UnsavedFiles) {
160 StringRef Path = Idx.getInvocationEmissionPath();
161 if (Path.empty())
162 return;
163
164 // Create a temporary file for the invocation log.
165 SmallString<256> TempPath;
166 TempPath = Path;
167 llvm::sys::path::append(path&: TempPath, a: "libclang-%%%%%%%%%%%%");
168 int FD;
169 if (llvm::sys::fs::createUniqueFile(Model: TempPath, ResultFD&: FD, ResultPath&: TempPath,
170 Flags: llvm::sys::fs::OF_Text))
171 return;
172 File = static_cast<std::string>(TempPath);
173 llvm::raw_fd_ostream OS(FD, /*ShouldClose=*/true);
174
175 // Write out the information about the invocation to it.
176 auto WriteStringKey = [&OS](StringRef Key, StringRef Value) {
177 OS << R"(")" << Key << R"(":")";
178 OS << llvm::yaml::escape(Input: Value) << '"';
179 };
180 OS << '{';
181 WriteStringKey("toolchain", Idx.getClangToolchainPath());
182 OS << ',';
183 WriteStringKey("libclang.operation",
184 Op == OperationKind::ParseOperation ? "parse" : "complete");
185 OS << ',';
186 OS << R"("libclang.opts":)" << ParseOptions;
187 OS << ',';
188 OS << R"("args":[)";
189 for (const auto &I : llvm::enumerate(First&: Args)) {
190 if (I.index())
191 OS << ',';
192 OS << '"' << llvm::yaml::escape(Input: I.value()) << '"';
193 }
194 if (!InvocationArgs.empty()) {
195 OS << R"(],"invocation-args":[)";
196 for (const auto &I : llvm::enumerate(First&: InvocationArgs)) {
197 if (I.index())
198 OS << ',';
199 OS << '"' << llvm::yaml::escape(Input: I.value()) << '"';
200 }
201 }
202 if (!UnsavedFiles.empty()) {
203 OS << R"(],"unsaved_file_hashes":[)";
204 for (const auto &UF : llvm::enumerate(First&: UnsavedFiles)) {
205 if (UF.index())
206 OS << ',';
207 OS << '{';
208 WriteStringKey("name", UF.value().Filename);
209 OS << ',';
210 llvm::MD5 Hash;
211 Hash.update(Str: getContents(UF: UF.value()));
212 llvm::MD5::MD5Result Result;
213 Hash.final(Result);
214 SmallString<32> Digest = Result.digest();
215 WriteStringKey("md5", Digest);
216 OS << '}';
217 }
218 }
219 OS << "]}";
220}
221
222LibclangInvocationReporter::~LibclangInvocationReporter() {
223 if (!File.empty())
224 llvm::sys::fs::remove(path: File);
225}
226