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