1 //===--- StdLib.h - Index the C and C++ standard library ---------*- C++-*-===//
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 //===----------------------------------------------------------------------===//
8 // Eagerly indexing the standard library gives a much friendlier "warm start"
9 // with working code completion in a standalone file or small project.
11 // We act as if we saw a file which included the whole standard library:
16 // We index this TU and feed the result into the dynamic index.
18 // This happens within the context of some particular open file, and we reuse
19 // its CompilerInvocation. Matching its include path, LangOpts etc ensures that
20 // we see the standard library and configuration that matches the project.
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_STDLIB_H
24 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_STDLIB_H
26 #include "index/Symbol.h"
27 #include "support/ThreadsafeFS.h"
28 #include "llvm/ADT/StringRef.h"
32 class CompilerInvocation
;
37 // The filesystem location where a standard library was found.
39 // This is the directory containing <vector> or <stdio.h>.
40 // It's used to ensure we only index files that are in the standard library.
42 // The paths are canonicalized (FS "real path" with symlinks resolved).
43 // This allows them to be easily compared against paths the indexer returns.
44 struct StdLibLocation
{
45 llvm::SmallVector
<std::string
> Paths
;
48 // Tracks the state of standard library indexing within a particular index.
50 // In general, we don't want to index the standard library multiple times.
51 // In most cases, this class just acts as a flag to ensure we only do it once.
53 // However, if we first open a C++11 file, and then a C++20 file, we *do*
54 // want the index to be upgraded to include the extra symbols.
55 // Similarly, the C and C++ standard library can coexist.
57 std::atomic
<int> Best
[2] = {{-1}, {-1}};
60 // Determines if we should index the standard library in a configuration.
63 // - standard library indexing is enabled for the file
64 // - the language version is higher than any previous add() for the language
65 // - the standard library headers exist on the search path
66 // Returns the location where the standard library was found.
68 // This function is threadsafe.
69 llvm::Optional
<StdLibLocation
> add(const LangOptions
&, const HeaderSearch
&);
71 // Indicates whether a built index should be used.
72 // It should not be used if a newer version has subsequently been added.
74 // Intended pattern is:
76 // symbols = indexStandardLibrary();
78 // index.update(symbols);
81 // This is still technically racy: we could return true here, then another
82 // thread could add->index->update a better library before we can update.
83 // We'd then overwrite it with the older version.
84 // However, it's very unlikely: indexing takes a long time.
85 bool isBest(const LangOptions
&) const;
88 // Index a standard library and return the discovered symbols.
90 // The compiler invocation should describe the file whose config we're reusing.
91 // We overwrite its virtual buffer with a lot of #include statements.
92 SymbolSlab
indexStandardLibrary(std::unique_ptr
<CompilerInvocation
> Invocation
,
93 const StdLibLocation
&Loc
,
94 const ThreadsafeFS
&TFS
);
96 // Variant that allows the umbrella header source to be specified.
97 // Exposed for testing.
98 SymbolSlab
indexStandardLibrary(llvm::StringRef HeaderSources
,
99 std::unique_ptr
<CompilerInvocation
> CI
,
100 const StdLibLocation
&Loc
,
101 const ThreadsafeFS
&TFS
);
103 // Generate header containing #includes for all standard library headers.
104 // Exposed for testing.
105 llvm::StringRef
getStdlibUmbrellaHeader(const LangOptions
&);
107 } // namespace clangd
110 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_STDLIB_H