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 = llvm::sys::path::parent_path(
146       llvm::sys::path::parent_path(llvm::sys::path::parent_path(ResourcePath)));
147   return ToolchainPath;
148 }
149 
150 LibclangInvocationReporter::LibclangInvocationReporter(
151     CIndexer &Idx, OperationKind Op, unsigned ParseOptions,
152     llvm::ArrayRef<const char *> Args,
153     llvm::ArrayRef<std::string> InvocationArgs,
154     llvm::ArrayRef<CXUnsavedFile> UnsavedFiles) {
155   StringRef Path = Idx.getInvocationEmissionPath();
156   if (Path.empty())
157     return;
158 
159   // Create a temporary file for the invocation log.
160   SmallString<256> TempPath;
161   TempPath = Path;
162   llvm::sys::path::append(TempPath, "libclang-%%%%%%%%%%%%");
163   int FD;
164   if (llvm::sys::fs::createUniqueFile(TempPath, FD, TempPath))
165     return;
166   File = std::string(TempPath.begin(), TempPath.end());
167   llvm::raw_fd_ostream OS(FD, /*ShouldClose=*/true);
168 
169   // Write out the information about the invocation to it.
170   auto WriteStringKey = [&OS](StringRef Key, StringRef Value) {
171     OS << R"(")" << Key << R"(":")";
172     OS << llvm::yaml::escape(Value) << '"';
173   };
174   OS << '{';
175   WriteStringKey("toolchain", Idx.getClangToolchainPath());
176   OS << ',';
177   WriteStringKey("libclang.operation",
178                  Op == OperationKind::ParseOperation ? "parse" : "complete");
179   OS << ',';
180   OS << R"("libclang.opts":)" << ParseOptions;
181   OS << ',';
182   OS << R"("args":[)";
183   for (const auto &I : llvm::enumerate(Args)) {
184     if (I.index())
185       OS << ',';
186     OS << '"' << llvm::yaml::escape(I.value()) << '"';
187   }
188   if (!InvocationArgs.empty()) {
189     OS << R"(],"invocation-args":[)";
190     for (const auto &I : llvm::enumerate(InvocationArgs)) {
191       if (I.index())
192         OS << ',';
193       OS << '"' << llvm::yaml::escape(I.value()) << '"';
194     }
195   }
196   if (!UnsavedFiles.empty()) {
197     OS << R"(],"unsaved_file_hashes":[)";
198     for (const auto &UF : llvm::enumerate(UnsavedFiles)) {
199       if (UF.index())
200         OS << ',';
201       OS << '{';
202       WriteStringKey("name", UF.value().Filename);
203       OS << ',';
204       llvm::MD5 Hash;
205       Hash.update(getContents(UF.value()));
206       llvm::MD5::MD5Result Result;
207       Hash.final(Result);
208       SmallString<32> Digest = Result.digest();
209       WriteStringKey("md5", Digest);
210       OS << '}';
211     }
212   }
213   OS << "]}";
214 }
215 
216 LibclangInvocationReporter::~LibclangInvocationReporter() {
217   if (!File.empty())
218     llvm::sys::fs::remove(File);
219 }
220