[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clangd / index / SymbolCollector.h
blob10765020de518bd6a163ce5cb50816b048c28393
1 //===--- SymbolCollector.h ---------------------------------------*- C++-*-===//
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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLCOLLECTOR_H
9 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLCOLLECTOR_H
11 #include "CollectMacros.h"
12 #include "clang-include-cleaner/Record.h"
13 #include "clang-include-cleaner/Types.h"
14 #include "index/Ref.h"
15 #include "index/Relation.h"
16 #include "index/Symbol.h"
17 #include "index/SymbolID.h"
18 #include "index/SymbolOrigin.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Index/IndexDataConsumer.h"
24 #include "clang/Index/IndexSymbol.h"
25 #include "clang/Sema/CodeCompleteConsumer.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include <functional>
28 #include <memory>
29 #include <optional>
31 namespace clang {
32 namespace clangd {
34 /// Collect declarations (symbols) from an AST.
35 /// It collects most declarations except:
36 /// - Implicit declarations
37 /// - Anonymous declarations (anonymous enum/class/struct, etc)
38 /// - Declarations in anonymous namespaces in headers
39 /// - Local declarations (in function bodies, blocks, etc)
40 /// - Template specializations
41 /// - Library-specific private declarations (e.g. private declaration generated
42 /// by protobuf compiler)
43 ///
44 /// References to main-file symbols are not collected.
45 ///
46 /// See also shouldCollectSymbol(...).
47 ///
48 /// Clients (e.g. clangd) can use SymbolCollector together with
49 /// index::indexTopLevelDecls to retrieve all symbols when the source file is
50 /// changed.
51 class SymbolCollector : public index::IndexDataConsumer {
52 public:
53 struct Options {
54 /// When symbol paths cannot be resolved to absolute paths (e.g. files in
55 /// VFS that does not have absolute path), combine the fallback directory
56 /// with symbols' paths to get absolute paths. This must be an absolute
57 /// path.
58 std::string FallbackDir;
59 bool CollectIncludePath = false;
60 /// If set, this is used to map symbol #include path to a potentially
61 /// different #include path specified by IWYU pragmas.
62 const include_cleaner::PragmaIncludes *PragmaIncludes = nullptr;
63 // Populate the Symbol.References field.
64 bool CountReferences = false;
65 /// The symbol ref kinds that will be collected.
66 /// If not set, SymbolCollector will not collect refs.
67 /// Note that references of namespace decls are not collected, as they
68 /// contribute large part of the index, and they are less useful compared
69 /// with other decls.
70 RefKind RefFilter = RefKind::Unknown;
71 /// If set to true, SymbolCollector will collect all refs (from main file
72 /// and included headers); otherwise, only refs from main file will be
73 /// collected.
74 /// This flag is only meaningful when RefFilter is set.
75 bool RefsInHeaders = false;
76 // Every symbol collected will be stamped with this origin.
77 SymbolOrigin Origin = SymbolOrigin::Unknown;
78 /// Collect macros.
79 /// Note that SymbolCollector must be run with preprocessor in order to
80 /// collect macros. For example, `indexTopLevelDecls` will not index any
81 /// macro even if this is true.
82 bool CollectMacro = false;
83 /// Collect symbols local to main-files, such as static functions, symbols
84 /// inside an anonymous namespace, function-local classes and its member
85 /// functions.
86 bool CollectMainFileSymbols = true;
87 /// Collect references to main-file symbols.
88 bool CollectMainFileRefs = false;
89 /// Collect symbols with reserved names, like __Vector_base.
90 /// This does not currently affect macros (many like _WIN32 are important!)
91 /// This only affects system headers.
92 bool CollectReserved = false;
93 /// If set to true, SymbolCollector will collect doc for all symbols.
94 /// Note that documents of symbols being indexed for completion will always
95 /// be collected regardless of this option.
96 bool StoreAllDocumentation = false;
97 /// If this is set, only collect symbols/references from a file if
98 /// `FileFilter(SM, FID)` is true. If not set, all files are indexed.
99 std::function<bool(const SourceManager &, FileID)> FileFilter = nullptr;
102 SymbolCollector(Options Opts);
103 ~SymbolCollector();
105 /// Returns true is \p ND should be collected.
106 static bool shouldCollectSymbol(const NamedDecl &ND, const ASTContext &ASTCtx,
107 const Options &Opts, bool IsMainFileSymbol);
109 // Given a ref contained in enclosing decl `Enclosing`, return
110 // the decl that should be used as that ref's Ref::Container. This is
111 // usually `Enclosing` itself, but in cases where `Enclosing` is not
112 // indexed, we walk further up because Ref::Container should always be
113 // an indexed symbol.
114 // Note: we don't use DeclContext as the container as in some cases
115 // it's useful to use a Decl which is not a DeclContext. For example,
116 // for a ref occurring in the initializer of a namespace-scope variable,
117 // it's useful to use that variable as the container, as otherwise the
118 // next enclosing DeclContext would be a NamespaceDecl or TranslationUnitDecl,
119 // which are both not indexed and less granular than we'd like for use cases
120 // like call hierarchy.
121 static const Decl *getRefContainer(const Decl *Enclosing,
122 const SymbolCollector::Options &Opts);
124 void initialize(ASTContext &Ctx) override;
126 void setPreprocessor(std::shared_ptr<Preprocessor> PP) override {
127 this->PP = PP.get();
129 void setPreprocessor(Preprocessor &PP) { this->PP = &PP; }
131 bool
132 handleDeclOccurrence(const Decl *D, index::SymbolRoleSet Roles,
133 ArrayRef<index::SymbolRelation> Relations,
134 SourceLocation Loc,
135 index::IndexDataConsumer::ASTNodeInfo ASTNode) override;
137 bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI,
138 index::SymbolRoleSet Roles,
139 SourceLocation Loc) override;
141 void handleMacros(const MainFileMacros &MacroRefsToIndex);
143 SymbolSlab takeSymbols() { return std::move(Symbols).build(); }
144 RefSlab takeRefs() { return std::move(Refs).build(); }
145 RelationSlab takeRelations() { return std::move(Relations).build(); }
147 /// Returns true if we are interested in references and declarations from \p
148 /// FID. If this function return false, bodies of functions inside those files
149 /// will be skipped to decrease indexing time.
150 bool shouldIndexFile(FileID FID);
152 void finish() override;
154 private:
155 const Symbol *addDeclaration(const NamedDecl &, SymbolID,
156 bool IsMainFileSymbol);
157 void addDefinition(const NamedDecl &, const Symbol &DeclSymbol);
158 void processRelations(const NamedDecl &ND, const SymbolID &ID,
159 ArrayRef<index::SymbolRelation> Relations);
161 std::optional<SymbolLocation> getTokenLocation(SourceLocation TokLoc);
163 std::optional<std::string> getIncludeHeader(const Symbol &S, FileID);
165 SymbolID getSymbolIDCached(const Decl *D);
166 SymbolID getSymbolIDCached(const llvm::StringRef MacroName,
167 const MacroInfo *MI, const SourceManager &SM);
169 // All Symbols collected from the AST.
170 SymbolSlab::Builder Symbols;
171 // File IDs used to determine if the code contains Obj-C constructs.
172 // For Obj-C symbols, these File IDs are used to compute the include
173 // headers.
174 llvm::DenseMap<SymbolID, FileID> IncludeFiles;
175 void setIncludeLocation(const Symbol &S, SourceLocation,
176 const include_cleaner::Symbol &Sym);
178 // Providers for Symbol.IncludeHeaders.
179 // The final spelling is calculated in finish().
180 llvm::DenseMap<SymbolID, std::optional<include_cleaner::Header>>
181 SymbolProviders;
182 // Files which contain ObjC symbols.
183 // This is finalized and used in finish().
184 llvm::DenseSet<FileID> FilesWithObjCConstructs;
186 // Indexed macros, to be erased if they turned out to be include guards.
187 llvm::DenseSet<const IdentifierInfo *> IndexedMacros;
188 // All refs collected from the AST. It includes:
189 // 1) symbols declared in the preamble and referenced from the main file (
190 // which is not a header), or
191 // 2) symbols declared and referenced from the main file (which is a header)
192 RefSlab::Builder Refs;
193 // All relations collected from the AST.
194 RelationSlab::Builder Relations;
195 ASTContext *ASTCtx;
196 Preprocessor *PP = nullptr;
197 std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator;
198 std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo;
199 Options Opts;
200 struct SymbolRef {
201 SourceLocation Loc;
202 FileID FID;
203 index::SymbolRoleSet Roles;
204 const Decl *Container;
205 bool Spelled;
207 void addRef(SymbolID ID, const SymbolRef &SR);
208 // Symbols referenced from the current TU, flushed on finish().
209 llvm::DenseSet<SymbolID> ReferencedSymbols;
210 // Maps canonical declaration provided by clang to canonical declaration for
211 // an index symbol, if clangd prefers a different declaration than that
212 // provided by clang. For example, friend declaration might be considered
213 // canonical by clang but should not be considered canonical in the index
214 // unless it's a definition.
215 llvm::DenseMap<const Decl *, const Decl *> CanonicalDecls;
216 // Cache whether to index a file or not.
217 llvm::DenseMap<FileID, bool> FilesToIndexCache;
218 // Encapsulates calculations and caches around header paths, which headers
219 // to insert for which symbol, etc.
220 class HeaderFileURICache;
221 std::unique_ptr<HeaderFileURICache> HeaderFileURIs;
222 llvm::DenseMap<const Decl *, SymbolID> DeclToIDCache;
223 llvm::DenseMap<const MacroInfo *, SymbolID> MacroToIDCache;
226 } // namespace clangd
227 } // namespace clang
229 #endif