Coverage Report

Created: 2020-09-22 08:39

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