File: StdLib.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (363 lines) | stat: -rw-r--r-- 13,322 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//===-- StdLib.cpp ----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "StdLib.h"
#include <fstream>
#include <memory>
#include <string>
#include <vector>

#include "Compiler.h"
#include "Config.h"
#include "SymbolCollector.h"
#include "index/IndexAction.h"
#include "support/Logger.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"

namespace clang {
namespace clangd {
namespace {

enum Lang { C, CXX };

Lang langFromOpts(const LangOptions &LO) { return LO.CPlusPlus ? CXX : C; }
llvm::StringLiteral mandatoryHeader(Lang L) {
  switch (L) {
  case C:
    return "stdio.h";
  case CXX:
    return "vector";
  }
  llvm_unreachable("unhandled Lang");
}

LangStandard::Kind standardFromOpts(const LangOptions &LO) {
  if (LO.CPlusPlus) {
    if (LO.CPlusPlus2b)
      return LangStandard::lang_cxx2b;
    if (LO.CPlusPlus20)
      return LangStandard::lang_cxx20;
    if (LO.CPlusPlus17)
      return LangStandard::lang_cxx17;
    if (LO.CPlusPlus14)
      return LangStandard::lang_cxx14;
    if (LO.CPlusPlus11)
      return LangStandard::lang_cxx11;
    return LangStandard::lang_cxx98;
  }
  if (LO.C2x)
    return LangStandard::lang_c2x;
  // C17 has no new features, so treat {C11,C17} as C17.
  if (LO.C11)
    return LangStandard::lang_c17;
  return LangStandard::lang_c99;
}

std::string buildUmbrella(llvm::StringLiteral Mandatory,
                          std::vector<llvm::StringLiteral> Headers) {
  std::string Result;
  llvm::raw_string_ostream OS(Result);

  // We __has_include guard all our #includes to avoid errors when using older
  // stdlib version that don't have headers for the newest language standards.
  // But make sure we get *some* error if things are totally broken.
  OS << llvm::formatv(
      "#if !__has_include(<{0}>)\n"
      "#error Mandatory header <{0}> not found in standard library!\n"
      "#endif\n",
      Mandatory);

  llvm::sort(Headers);
  auto Last = std::unique(Headers.begin(), Headers.end());
  for (auto Header = Headers.begin(); Header != Last; ++Header) {
    OS << llvm::formatv("#if __has_include({0})\n"
                        "#include {0}\n"
                        "#endif\n",
                        *Header);
  }
  OS.flush();
  return Result;
}

} // namespace

llvm::StringRef getStdlibUmbrellaHeader(const LangOptions &LO) {
  // The umbrella header is the same for all versions of each language.
  // Headers that are unsupported in old lang versions are usually guarded by
  // #if. Some headers may be not present in old stdlib versions, the umbrella
  // header guards with __has_include for this purpose.
  Lang L = langFromOpts(LO);
  switch (L) {
  case CXX:
    static std::string *UmbrellaCXX =
        new std::string(buildUmbrella(mandatoryHeader(L), {
#define SYMBOL(Name, NameSpace, Header) #Header,
#include "clang/Tooling/Inclusions/StdSymbolMap.inc"
#undef SYMBOL
                                                          }));
    return *UmbrellaCXX;
  case C:
    static std::string *UmbrellaC =
        new std::string(buildUmbrella(mandatoryHeader(L), {
#define SYMBOL(Name, NameSpace, Header) #Header,
#include "clang/Tooling/Inclusions/CSymbolMap.inc"
#undef SYMBOL
                                                          }));
    return *UmbrellaC;
  }
  llvm_unreachable("invalid Lang in langFromOpts");
}

namespace {

// Including the standard library leaks unwanted transitively included symbols.
//
// We want to drop these, they're a bit tricky to identify:
//  - we don't want to limit to symbols on our list, as our list has only
//    top-level symbols (and there may be legitimate stdlib extensions).
//  - we can't limit to only symbols defined in known stdlib headers, as stdlib
//    internal structure is murky
//  - we can't strictly require symbols to come from a particular path, e.g.
//      libstdc++ is mostly under /usr/include/c++/10/...
//      but std::ctype_base is under /usr/include/<platform>/c++/10/...
// We require the symbol to come from a header that is *either* from
// the standard library path (as identified by the location of <vector>), or
// another header that defines a symbol from our stdlib list.
SymbolSlab filter(SymbolSlab Slab, const StdLibLocation &Loc) {
  SymbolSlab::Builder Result;

  static auto &StandardHeaders = *[] {
    auto *Set = new llvm::DenseSet<llvm::StringRef>();
    for (llvm::StringRef Header : {
#define SYMBOL(Name, NameSpace, Header) #Header,
#include "clang/Tooling/Inclusions/CSymbolMap.inc"
#include "clang/Tooling/Inclusions/StdSymbolMap.inc"
#undef SYMBOL
         })
      Set->insert(Header);
    return Set;
  }();

  // Form prefixes like file:///usr/include/c++/10/
  // These can be trivially prefix-compared with URIs in the indexed symbols.
  llvm::SmallVector<std::string> StdLibURIPrefixes;
  for (const auto &Path : Loc.Paths) {
    StdLibURIPrefixes.push_back(URI::create(Path).toString());
    if (StdLibURIPrefixes.back().back() != '/')
      StdLibURIPrefixes.back().push_back('/');
  }
  // For each header URI, is it *either* prefixed by StdLibURIPrefixes *or*
  // owner of a symbol whose insertable header is in StandardHeaders?
  // Pointer key because strings in a SymbolSlab are interned.
  llvm::DenseMap<const char *, bool> GoodHeader;
  for (const Symbol &S : Slab) {
    if (!S.IncludeHeaders.empty() &&
        StandardHeaders.contains(S.IncludeHeaders.front().IncludeHeader)) {
      GoodHeader[S.CanonicalDeclaration.FileURI] = true;
      GoodHeader[S.Definition.FileURI] = true;
      continue;
    }
    for (const char *URI :
         {S.CanonicalDeclaration.FileURI, S.Definition.FileURI}) {
      auto R = GoodHeader.try_emplace(URI, false);
      if (R.second) {
        R.first->second = llvm::any_of(
            StdLibURIPrefixes,
            [&, URIStr(llvm::StringRef(URI))](const std::string &Prefix) {
              return URIStr.startswith(Prefix);
            });
      }
    }
  }
#ifndef NDEBUG
  for (const auto &Good : GoodHeader)
    if (Good.second && *Good.first)
      dlog("Stdlib header: {0}", Good.first);
#endif
  // Empty URIs aren't considered good. (Definition can be blank).
  auto IsGoodHeader = [&](const char *C) { return *C && GoodHeader.lookup(C); };

  for (const Symbol &S : Slab) {
    if (!(IsGoodHeader(S.CanonicalDeclaration.FileURI) ||
          IsGoodHeader(S.Definition.FileURI))) {
      dlog("Ignoring wrong-header symbol {0}{1} in {2}", S.Scope, S.Name,
           S.CanonicalDeclaration.FileURI);
      continue;
    }
    Result.insert(S);
  }

  return std::move(Result).build();
}

} // namespace

SymbolSlab indexStandardLibrary(llvm::StringRef HeaderSources,
                                std::unique_ptr<CompilerInvocation> CI,
                                const StdLibLocation &Loc,
                                const ThreadsafeFS &TFS) {
  if (CI->getFrontendOpts().Inputs.size() != 1 ||
      !CI->getPreprocessorOpts().ImplicitPCHInclude.empty()) {
    elog("Indexing standard library failed: bad CompilerInvocation");
    assert(false && "indexing stdlib with a dubious CompilerInvocation!");
    return SymbolSlab();
  }
  const FrontendInputFile &Input = CI->getFrontendOpts().Inputs.front();
  trace::Span Tracer("StandardLibraryIndex");
  LangStandard::Kind LangStd = standardFromOpts(*CI->getLangOpts());
  log("Indexing {0} standard library in the context of {1}",
      LangStandard::getLangStandardForKind(LangStd).getName(), Input.getFile());

  SymbolSlab Symbols;
  IgnoreDiagnostics IgnoreDiags;
  // CompilerInvocation is taken from elsewhere, and may map a dirty buffer.
  CI->getPreprocessorOpts().clearRemappedFiles();
  auto Clang = prepareCompilerInstance(
      std::move(CI), /*Preamble=*/nullptr,
      llvm::MemoryBuffer::getMemBuffer(HeaderSources, Input.getFile()),
      TFS.view(/*CWD=*/llvm::None), IgnoreDiags);
  if (!Clang) {
    elog("Standard Library Index: Couldn't build compiler instance");
    return Symbols;
  }

  SymbolCollector::Options IndexOpts;
  IndexOpts.Origin = SymbolOrigin::StdLib;
  IndexOpts.CollectMainFileSymbols = false;
  IndexOpts.CollectMainFileRefs = false;
  IndexOpts.CollectMacro = true;
  IndexOpts.StoreAllDocumentation = true;
  // Sadly we can't use IndexOpts.FileFilter to restrict indexing scope.
  // Files from outside the StdLibLocation may define true std symbols anyway.
  // We end up "blessing" such headers, and can only do that by indexing
  // everything first.

  // Refs, relations, include graph in the stdlib mostly aren't useful.
  auto Action = createStaticIndexingAction(
      IndexOpts, [&](SymbolSlab S) { Symbols = std::move(S); }, nullptr,
      nullptr, nullptr);

  if (!Action->BeginSourceFile(*Clang, Input)) {
    elog("Standard Library Index: BeginSourceFile() failed");
    return Symbols;
  }

  if (llvm::Error Err = Action->Execute()) {
    elog("Standard Library Index: Execute failed: {0}", std::move(Err));
    return Symbols;
  }

  Action->EndSourceFile();

  unsigned SymbolsBeforeFilter = Symbols.size();
  Symbols = filter(std::move(Symbols), Loc);
  bool Errors = Clang->hasDiagnostics() &&
                Clang->getDiagnostics().hasUncompilableErrorOccurred();
  log("Indexed {0} standard library{3}: {1} symbols, {2} filtered",
      LangStandard::getLangStandardForKind(LangStd).getName(), Symbols.size(),
      SymbolsBeforeFilter - Symbols.size(),
      Errors ? " (incomplete due to errors)" : "");
  SPAN_ATTACH(Tracer, "symbols", int(Symbols.size()));
  return Symbols;
}

SymbolSlab indexStandardLibrary(std::unique_ptr<CompilerInvocation> Invocation,
                                const StdLibLocation &Loc,
                                const ThreadsafeFS &TFS) {
  llvm::StringRef Header = getStdlibUmbrellaHeader(*Invocation->getLangOpts());
  return indexStandardLibrary(Header, std::move(Invocation), Loc, TFS);
}

bool StdLibSet::isBest(const LangOptions &LO) const {
  return standardFromOpts(LO) >=
         Best[langFromOpts(LO)].load(std::memory_order_acquire);
}

llvm::Optional<StdLibLocation> StdLibSet::add(const LangOptions &LO,
                                              const HeaderSearch &HS) {
  Lang L = langFromOpts(LO);
  int OldVersion = Best[L].load(std::memory_order_acquire);
  int NewVersion = standardFromOpts(LO);
  dlog("Index stdlib? {0}",
       LangStandard::getLangStandardForKind(standardFromOpts(LO)).getName());

  if (!Config::current().Index.StandardLibrary) {
    dlog("No: disabled in config");
    return llvm::None;
  }

  if (NewVersion <= OldVersion) {
    dlog("No: have {0}, {1}>={2}",
         LangStandard::getLangStandardForKind(
             static_cast<LangStandard::Kind>(NewVersion))
             .getName(),
         OldVersion, NewVersion);
    return llvm::None;
  }

  // We'd like to index a standard library here if there is one.
  // Check for the existence of <vector> on the search path.
  // We could cache this, but we only get here repeatedly when there's no
  // stdlib, and even then only once per preamble build.
  llvm::StringLiteral ProbeHeader = mandatoryHeader(L);
  llvm::SmallString<256> Path; // Scratch space.
  llvm::SmallVector<std::string> SearchPaths;
  auto RecordHeaderPath = [&](llvm::StringRef HeaderPath) {
    llvm::StringRef DirPath = llvm::sys::path::parent_path(HeaderPath);
    if (!HS.getFileMgr().getVirtualFileSystem().getRealPath(DirPath, Path))
      SearchPaths.emplace_back(Path);
  };
  for (const auto &DL :
       llvm::make_range(HS.search_dir_begin(), HS.search_dir_end())) {
    switch (DL.getLookupType()) {
    case DirectoryLookup::LT_NormalDir: {
      Path = DL.getDir()->getName();
      llvm::sys::path::append(Path, ProbeHeader);
      llvm::vfs::Status Stat;
      if (!HS.getFileMgr().getNoncachedStatValue(Path, Stat) &&
          Stat.isRegularFile())
        RecordHeaderPath(Path);
      break;
    }
    case DirectoryLookup::LT_Framework:
      // stdlib can't be a framework (framework includes must have a slash)
      continue;
    case DirectoryLookup::LT_HeaderMap:
      llvm::StringRef Target =
          DL.getHeaderMap()->lookupFilename(ProbeHeader, Path);
      if (!Target.empty())
        RecordHeaderPath(Target);
      break;
    }
  }
  if (SearchPaths.empty())
    return llvm::None;

  dlog("Found standard library in {0}", llvm::join(SearchPaths, ", "));

  while (!Best[L].compare_exchange_weak(OldVersion, NewVersion,
                                        std::memory_order_acq_rel))
    if (OldVersion >= NewVersion) {
      dlog("No: lost the race");
      return llvm::None; // Another thread won the race while we were checking.
    }

  dlog("Yes, index stdlib!");
  return StdLibLocation{std::move(SearchPaths)};
}

} // namespace clangd
} // namespace clang