File: StdLib.h

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 (110 lines) | stat: -rw-r--r-- 4,409 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
//===--- StdLib.h - Index the C and C++ standard library ---------*- 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
//
//===----------------------------------------------------------------------===//
// Eagerly indexing the standard library gives a much friendlier "warm start"
// with working code completion in a standalone file or small project.
//
// We act as if we saw a file which included the whole standard library:
//   #include <array>
//   #include <bitset>
//   #include <chrono>
//   ...
// We index this TU and feed the result into the dynamic index.
//
// This happens within the context of some particular open file, and we reuse
// its CompilerInvocation. Matching its include path, LangOpts etc ensures that
// we see the standard library and configuration that matches the project.
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_STDLIB_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_STDLIB_H

#include "index/Symbol.h"
#include "support/ThreadsafeFS.h"
#include "llvm/ADT/StringRef.h"
#include <string>

namespace clang {
class CompilerInvocation;
class LangOptions;
class HeaderSearch;
namespace clangd {

// The filesystem location where a standard library was found.
//
// This is the directory containing <vector> or <stdio.h>.
// It's used to ensure we only index files that are in the standard library.
//
// The paths are canonicalized (FS "real path" with symlinks resolved).
// This allows them to be easily compared against paths the indexer returns.
struct StdLibLocation {
  llvm::SmallVector<std::string> Paths;
};

// Tracks the state of standard library indexing within a particular index.
//
// In general, we don't want to index the standard library multiple times.
// In most cases, this class just acts as a flag to ensure we only do it once.
//
// However, if we first open a C++11 file, and then a C++20 file, we *do*
// want the index to be upgraded to include the extra symbols.
// Similarly, the C and C++ standard library can coexist.
class StdLibSet {
  std::atomic<int> Best[2] = {{-1}, {-1}};

public:
  // Determines if we should index the standard library in a configuration.
  //
  // This is true if:
  //  - standard library indexing is enabled for the file
  //  - the language version is higher than any previous add() for the language
  //  - the standard library headers exist on the search path
  // Returns the location where the standard library was found.
  //
  // This function is threadsafe.
  llvm::Optional<StdLibLocation> add(const LangOptions &, const HeaderSearch &);

  // Indicates whether a built index should be used.
  // It should not be used if a newer version has subsequently been added.
  //
  // Intended pattern is:
  //   if (add()) {
  //     symbols = indexStandardLibrary();
  //     if (isBest())
  //       index.update(symbols);
  //   }
  //
  // This is still technically racy: we could return true here, then another
  // thread could add->index->update a better library before we can update.
  // We'd then overwrite it with the older version.
  // However, it's very unlikely: indexing takes a long time.
  bool isBest(const LangOptions &) const;
};

// Index a standard library and return the discovered symbols.
//
// The compiler invocation should describe the file whose config we're reusing.
// We overwrite its virtual buffer with a lot of #include statements.
SymbolSlab indexStandardLibrary(std::unique_ptr<CompilerInvocation> Invocation,
                                const StdLibLocation &Loc,
                                const ThreadsafeFS &TFS);

// Variant that allows the umbrella header source to be specified.
// Exposed for testing.
SymbolSlab indexStandardLibrary(llvm::StringRef HeaderSources,
                                std::unique_ptr<CompilerInvocation> CI,
                                const StdLibLocation &Loc,
                                const ThreadsafeFS &TFS);

// Generate header containing #includes for all standard library headers.
// Exposed for testing.
llvm::StringRef getStdlibUmbrellaHeader(const LangOptions &);

} // namespace clangd
} // namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_STDLIB_H