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