1 //===- CIndexer.cpp - Clang-C Source Indexing Library ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Clang-C Source Indexing library.
11 //===----------------------------------------------------------------------===//
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/FileSystem.h"
21 #include "llvm/Support/MD5.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/YAMLParser.h"
29 #include <cygwin/version.h>
30 #include <sys/cygwin.h>
43 using namespace clang
;
49 template <typename LibClangPathType
>
50 void getClangResourcesPathImplAIX(LibClangPathType
&LibClangPath
) {
51 int PrevErrno
= errno
;
53 size_t BufSize
= 2048u;
54 std::unique_ptr
<char[]> Buf
;
56 Buf
= std::make_unique
<char []>(BufSize
);
58 int Ret
= loadquery(L_GETXINFO
, Buf
.get(), (unsigned int)BufSize
);
60 break; // loadquery() was successful.
62 llvm_unreachable("Encountered an unexpected loadquery() failure");
64 // errno == ENOMEM; try to allocate more memory.
65 if ((BufSize
& ~((-1u) >> 1u)) != 0u)
66 llvm::report_fatal_error("BufSize needed for loadquery() too large");
72 // Extract the function entry point from the function descriptor.
74 reinterpret_cast<uintptr_t &>(clang_createTranslationUnit
);
76 // Loop to locate the function entry point in the loadquery() results.
77 ld_xinfo
*CurInfo
= reinterpret_cast<ld_xinfo
*>(Buf
.get());
79 uint64_t CurTextStart
= (uint64_t)CurInfo
->ldinfo_textorg
;
80 uint64_t CurTextEnd
= CurTextStart
+ CurInfo
->ldinfo_textsize
;
81 if (CurTextStart
<= EntryAddr
&& EntryAddr
< CurTextEnd
)
82 break; // Successfully located.
84 if (CurInfo
->ldinfo_next
== 0u)
85 llvm::report_fatal_error("Cannot locate entry point in "
86 "the loadquery() results");
87 CurInfo
= reinterpret_cast<ld_xinfo
*>(reinterpret_cast<char *>(CurInfo
) +
88 CurInfo
->ldinfo_next
);
91 LibClangPath
+= reinterpret_cast<char *>(CurInfo
) + CurInfo
->ldinfo_filename
;
95 } // end anonymous namespace
96 } // end namespace clang
99 const std::string
&CIndexer::getClangResourcesPath() {
100 // Did we already compute the path?
101 if (!ResourcesPath
.empty())
102 return ResourcesPath
;
104 SmallString
<128> LibClangPath
;
106 // Find the location where this library lives (libclang.dylib).
108 MEMORY_BASIC_INFORMATION mbi
;
110 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit
, &mbi
,
112 GetModuleFileNameA((HINSTANCE
)mbi
.AllocationBase
, path
, MAX_PATH
);
115 char w32path
[MAX_PATH
];
116 strcpy(w32path
, path
);
117 #if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181
118 cygwin_conv_path(CCP_WIN_A_TO_POSIX
, w32path
, path
, MAX_PATH
);
120 cygwin_conv_to_full_posix_path(w32path
, path
);
124 LibClangPath
+= path
;
126 getClangResourcesPathImplAIX(LibClangPath
);
130 // This silly cast below avoids a C++ warning.
131 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit
, &info
) != 0) {
132 // We now have the CIndex directory, locate clang relative to it.
133 LibClangPath
+= info
.dli_fname
;
134 } else if (!(Path
= llvm::sys::fs::getMainExecutable(nullptr, nullptr)).empty()) {
135 // If we can't get the path using dladdr, try to get the main executable
136 // path. This may be needed when we're statically linking libclang with
137 // musl libc, for example.
138 LibClangPath
+= Path
;
140 // It's rather unlikely we end up here. But it could happen, so report an
141 // error instead of crashing.
142 llvm::report_fatal_error("could not locate Clang resource path");
148 ResourcesPath
= driver::Driver::GetResourcesPath(LibClangPath
);
149 return ResourcesPath
;
152 StringRef
CIndexer::getClangToolchainPath() {
153 if (!ToolchainPath
.empty())
154 return ToolchainPath
;
155 StringRef ResourcePath
= getClangResourcesPath();
157 std::string(llvm::sys::path::parent_path(llvm::sys::path::parent_path(
158 llvm::sys::path::parent_path(ResourcePath
))));
159 return ToolchainPath
;
162 LibclangInvocationReporter::LibclangInvocationReporter(
163 CIndexer
&Idx
, OperationKind Op
, unsigned ParseOptions
,
164 llvm::ArrayRef
<const char *> Args
,
165 llvm::ArrayRef
<std::string
> InvocationArgs
,
166 llvm::ArrayRef
<CXUnsavedFile
> UnsavedFiles
) {
167 StringRef Path
= Idx
.getInvocationEmissionPath();
171 // Create a temporary file for the invocation log.
172 SmallString
<256> TempPath
;
174 llvm::sys::path::append(TempPath
, "libclang-%%%%%%%%%%%%");
176 if (llvm::sys::fs::createUniqueFile(TempPath
, FD
, TempPath
,
177 llvm::sys::fs::OF_Text
))
179 File
= static_cast<std::string
>(TempPath
);
180 llvm::raw_fd_ostream
OS(FD
, /*ShouldClose=*/true);
182 // Write out the information about the invocation to it.
183 auto WriteStringKey
= [&OS
](StringRef Key
, StringRef Value
) {
184 OS
<< R
"(")" << Key << R"(":")";
185 OS << llvm::yaml::escape(Value) << '"';
188 WriteStringKey("toolchain", Idx.getClangToolchainPath());
190 WriteStringKey("libclang.operation",
191 Op == OperationKind::ParseOperation ? "parse" : "complete");
193 OS << R"("libclang.opts":)" << ParseOptions;
196 for (const auto &I : llvm::enumerate(Args)) {
199 OS << '"' << llvm::yaml::escape(I.value()) << '"';
201 if (!InvocationArgs.empty()) {
202 OS << R"(],"invocation-args":[)";
203 for (const auto &I : llvm::enumerate(InvocationArgs)) {
206 OS << '"' << llvm::yaml::escape(I.value()) << '"';
209 if (!UnsavedFiles.empty()) {
210 OS << R"(],"unsaved_file_hashes":[)";
211 for (const auto &UF : llvm::enumerate(UnsavedFiles)) {
215 WriteStringKey("name", UF.value().Filename);
218 Hash.update(getContents(UF.value()));
219 llvm::MD5::MD5Result Result;
221 SmallString<32> Digest = Result.digest();
222 WriteStringKey("md5", Digest);
229 LibclangInvocationReporter::~LibclangInvocationReporter() {
231 llvm::sys::fs::remove(File);