Coverage Report

Created: 2020-02-15 09:57

/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/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
1.00k
const std::string &CIndexer::getClangResourcesPath() {
99
1.00k
  // Did we already compute the path?
100
1.00k
  if (!ResourcesPath.empty())
101
11
    return ResourcesPath;
102
997
103
997
  SmallString<128> LibClangPath;
104
997
105
997
  // 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
997
  Dl_info info;
129
997
  if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
130
997
    
llvm_unreachable0
("Call to dladdr() failed");
131
997
132
997
  // We now have the CIndex directory, locate clang relative to it.
133
997
  LibClangPath += info.dli_fname;
134
997
#endif
135
997
136
997
  // Cache our result.
137
997
  ResourcesPath = driver::Driver::GetResourcesPath(LibClangPath);
138
997
  return ResourcesPath;
139
997
}
140
141
9
StringRef CIndexer::getClangToolchainPath() {
142
9
  if (!ToolchainPath.empty())
143
2
    return ToolchainPath;
144
7
  StringRef ResourcePath = getClangResourcesPath();
145
7
  ToolchainPath =
146
7
      std::string(llvm::sys::path::parent_path(llvm::sys::path::parent_path(
147
7
          llvm::sys::path::parent_path(ResourcePath))));
148
7
  return ToolchainPath;
149
7
}
150
151
LibclangInvocationReporter::LibclangInvocationReporter(
152
    CIndexer &Idx, OperationKind Op, unsigned ParseOptions,
153
    llvm::ArrayRef<const char *> Args,
154
    llvm::ArrayRef<std::string> InvocationArgs,
155
1.68k
    llvm::ArrayRef<CXUnsavedFile> UnsavedFiles) {
156
1.68k
  StringRef Path = Idx.getInvocationEmissionPath();
157
1.68k
  if (Path.empty())
158
1.67k
    return;
159
9
160
9
  // Create a temporary file for the invocation log.
161
9
  SmallString<256> TempPath;
162
9
  TempPath = Path;
163
9
  llvm::sys::path::append(TempPath, "libclang-%%%%%%%%%%%%");
164
9
  int FD;
165
9
  if (llvm::sys::fs::createUniqueFile(TempPath, FD, TempPath))
166
0
    return;
167
9
  File = std::string(TempPath.begin(), TempPath.end());
168
9
  llvm::raw_fd_ostream OS(FD, /*ShouldClose=*/true);
169
9
170
9
  // Write out the information about the invocation to it.
171
26
  auto WriteStringKey = [&OS](StringRef Key, StringRef Value) {
172
26
    OS << R"(")" << Key << R"(":")";
173
26
    OS << llvm::yaml::escape(Value) << '"';
174
26
  };
175
9
  OS << '{';
176
9
  WriteStringKey("toolchain", Idx.getClangToolchainPath());
177
9
  OS << ',';
178
9
  WriteStringKey("libclang.operation",
179
9
                 Op == OperationKind::ParseOperation ? 
"parse"7
:
"complete"2
);
180
9
  OS << ',';
181
9
  OS << R"("libclang.opts":)" << ParseOptions;
182
9
  OS << ',';
183
9
  OS << R"("args":[)";
184
55
  for (const auto &I : llvm::enumerate(Args)) {
185
55
    if (I.index())
186
46
      OS << ',';
187
55
    OS << '"' << llvm::yaml::escape(I.value()) << '"';
188
55
  }
189
9
  if (!InvocationArgs.empty()) {
190
2
    OS << R"(],"invocation-args":[)";
191
2
    for (const auto &I : llvm::enumerate(InvocationArgs)) {
192
2
      if (I.index())
193
0
        OS << ',';
194
2
      OS << '"' << llvm::yaml::escape(I.value()) << '"';
195
2
    }
196
2
  }
197
9
  if (!UnsavedFiles.empty()) {
198
4
    OS << R"(],"unsaved_file_hashes":[)";
199
4
    for (const auto &UF : llvm::enumerate(UnsavedFiles)) {
200
4
      if (UF.index())
201
0
        OS << ',';
202
4
      OS << '{';
203
4
      WriteStringKey("name", UF.value().Filename);
204
4
      OS << ',';
205
4
      llvm::MD5 Hash;
206
4
      Hash.update(getContents(UF.value()));
207
4
      llvm::MD5::MD5Result Result;
208
4
      Hash.final(Result);
209
4
      SmallString<32> Digest = Result.digest();
210
4
      WriteStringKey("md5", Digest);
211
4
      OS << '}';
212
4
    }
213
4
  }
214
9
  OS << "]}";
215
9
}
216
217
1.67k
LibclangInvocationReporter::~LibclangInvocationReporter() {
218
1.67k
  if (!File.empty())
219
3
    llvm::sys::fs::remove(File);
220
1.67k
}