clang-tools  11.0.0
SymbolCollector.h
Go to the documentation of this file.
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_SYMBOL_COLLECTOR_H
9 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_COLLECTOR_H
10 
11 #include "CanonicalIncludes.h"
12 #include "CollectMacros.h"
13 #include "Index.h"
14 #include "SymbolOrigin.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Index/IndexDataConsumer.h"
20 #include "clang/Index/IndexSymbol.h"
21 #include "clang/Sema/CodeCompleteConsumer.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/Support/Regex.h"
24 #include <functional>
25 
26 namespace clang {
27 namespace clangd {
28 
29 /// Collect declarations (symbols) from an AST.
30 /// It collects most declarations except:
31 /// - Implicit declarations
32 /// - Anonymous declarations (anonymous enum/class/struct, etc)
33 /// - Declarations in anonymous namespaces in headers
34 /// - Local declarations (in function bodies, blocks, etc)
35 /// - Template specializations
36 /// - Library-specific private declarations (e.g. private declaration generated
37 /// by protobuf compiler)
38 ///
39 /// References to main-file symbols are not collected.
40 ///
41 /// See also shouldCollectSymbol(...).
42 ///
43 /// Clients (e.g. clangd) can use SymbolCollector together with
44 /// index::indexTopLevelDecls to retrieve all symbols when the source file is
45 /// changed.
46 class SymbolCollector : public index::IndexDataConsumer {
47 public:
48  struct Options {
49  /// When symbol paths cannot be resolved to absolute paths (e.g. files in
50  /// VFS that does not have absolute path), combine the fallback directory
51  /// with symbols' paths to get absolute paths. This must be an absolute
52  /// path.
53  std::string FallbackDir;
54  bool CollectIncludePath = false;
55  /// If set, this is used to map symbol #include path to a potentially
56  /// different #include path.
57  const CanonicalIncludes *Includes = nullptr;
58  // Populate the Symbol.References field.
59  bool CountReferences = false;
60  /// The symbol ref kinds that will be collected.
61  /// If not set, SymbolCollector will not collect refs.
62  /// Note that references of namespace decls are not collected, as they
63  /// contribute large part of the index, and they are less useful compared
64  /// with other decls.
66  /// If set to true, SymbolCollector will collect all refs (from main file
67  /// and included headers); otherwise, only refs from main file will be
68  /// collected.
69  /// This flag is only meaningful when RefFilter is set.
70  bool RefsInHeaders = false;
71  // Every symbol collected will be stamped with this origin.
73  /// Collect macros.
74  /// Note that SymbolCollector must be run with preprocessor in order to
75  /// collect macros. For example, `indexTopLevelDecls` will not index any
76  /// macro even if this is true.
77  bool CollectMacro = false;
78  /// Collect symbols local to main-files, such as static functions
79  /// and symbols inside an anonymous namespace.
81  /// If set to true, SymbolCollector will collect doc for all symbols.
82  /// Note that documents of symbols being indexed for completion will always
83  /// be collected regardless of this option.
84  bool StoreAllDocumentation = false;
85  /// If this is set, only collect symbols/references from a file if
86  /// `FileFilter(SM, FID)` is true. If not set, all files are indexed.
87  std::function<bool(const SourceManager &, FileID)> FileFilter = nullptr;
88  };
89 
91 
92  /// Returns true is \p ND should be collected.
93  static bool shouldCollectSymbol(const NamedDecl &ND, const ASTContext &ASTCtx,
94  const Options &Opts, bool IsMainFileSymbol);
95 
96  void initialize(ASTContext &Ctx) override;
97 
98  void setPreprocessor(std::shared_ptr<Preprocessor> PP) override {
99  this->PP = std::move(PP);
100  }
101 
102  bool
103  handleDeclOccurrence(const Decl *D, index::SymbolRoleSet Roles,
104  ArrayRef<index::SymbolRelation> Relations,
105  SourceLocation Loc,
106  index::IndexDataConsumer::ASTNodeInfo ASTNode) override;
107 
108  bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI,
109  index::SymbolRoleSet Roles,
110  SourceLocation Loc) override;
111 
112  void handleMacros(const MainFileMacros &MacroRefsToIndex);
113 
114  SymbolSlab takeSymbols() { return std::move(Symbols).build(); }
115  RefSlab takeRefs() { return std::move(Refs).build(); }
116  RelationSlab takeRelations() { return std::move(Relations).build(); }
117 
118  /// Returns true if we are interested in references and declarations from \p
119  /// FID. If this function return false, bodies of functions inside those files
120  /// will be skipped to decrease indexing time.
121  bool shouldIndexFile(FileID FID);
122 
123  void finish() override;
124 
125 private:
126  const Symbol *addDeclaration(const NamedDecl &, SymbolID,
127  bool IsMainFileSymbol);
128  void addDefinition(const NamedDecl &, const Symbol &DeclSymbol);
129  void processRelations(const NamedDecl &ND, const SymbolID &ID,
130  ArrayRef<index::SymbolRelation> Relations);
131 
132  llvm::Optional<std::string> getIncludeHeader(llvm::StringRef QName, FileID);
133  bool isSelfContainedHeader(FileID);
134  // Heuristically headers that only want to be included via an umbrella.
135  static bool isDontIncludeMeHeader(llvm::StringRef);
136 
137  // All Symbols collected from the AST.
138  SymbolSlab::Builder Symbols;
139  // File IDs for Symbol.IncludeHeaders.
140  // The final spelling is calculated in finish().
141  llvm::DenseMap<SymbolID, FileID> IncludeFiles;
142  void setIncludeLocation(const Symbol &S, SourceLocation);
143  // Indexed macros, to be erased if they turned out to be include guards.
144  llvm::DenseSet<const IdentifierInfo *> IndexedMacros;
145  // All refs collected from the AST. It includes:
146  // 1) symbols declared in the preamble and referenced from the main file (
147  // which is not a header), or
148  // 2) symbols declared and referenced from the main file (which is a header)
149  RefSlab::Builder Refs;
150  // All relations collected from the AST.
151  RelationSlab::Builder Relations;
152  ASTContext *ASTCtx;
153  std::shared_ptr<Preprocessor> PP;
154  std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator;
155  std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo;
156  Options Opts;
157  using SymbolRef = std::pair<SourceLocation, index::SymbolRoleSet>;
158  // Symbols referenced from the current TU, flushed on finish().
159  llvm::DenseSet<const NamedDecl *> ReferencedDecls;
160  llvm::DenseSet<const IdentifierInfo *> ReferencedMacros;
161  llvm::DenseMap<const NamedDecl *, std::vector<SymbolRef>> DeclRefs;
162  llvm::DenseMap<SymbolID, std::vector<SymbolRef>> MacroRefs;
163  // Maps canonical declaration provided by clang to canonical declaration for
164  // an index symbol, if clangd prefers a different declaration than that
165  // provided by clang. For example, friend declaration might be considered
166  // canonical by clang but should not be considered canonical in the index
167  // unless it's a definition.
168  llvm::DenseMap<const Decl *, const Decl *> CanonicalDecls;
169  // Cache whether to index a file or not.
170  llvm::DenseMap<FileID, bool> FilesToIndexCache;
171  llvm::DenseMap<FileID, bool> HeaderIsSelfContainedCache;
172 };
173 
174 } // namespace clangd
175 } // namespace clang
176 
177 #endif
clang::clangd::SymbolCollector::setPreprocessor
void setPreprocessor(std::shared_ptr< Preprocessor > PP) override
Definition: SymbolCollector.h:98
SymbolOrigin.h
clang::clangd::SymbolCollector::Options::Origin
SymbolOrigin Origin
Definition: SymbolCollector.h:72
clang::clangd::SymbolCollector::Options::CollectMainFileSymbols
bool CollectMainFileSymbols
Collect symbols local to main-files, such as static functions and symbols inside an anonymous namespa...
Definition: SymbolCollector.h:80
clang::clangd::RefKind::Unknown
clang::clangd::SymbolCollector::handleMacros
void handleMacros(const MainFileMacros &MacroRefsToIndex)
Definition: SymbolCollector.cpp:348
clang::clangd::SymbolCollector::Options::RefsInHeaders
bool RefsInHeaders
If set to true, SymbolCollector will collect all refs (from main file and included headers); otherwis...
Definition: SymbolCollector.h:70
clang::clangd::SymbolCollector::shouldCollectSymbol
static bool shouldCollectSymbol(const NamedDecl &ND, const ASTContext &ASTCtx, const Options &Opts, bool IsMainFileSymbol)
Returns true is ND should be collected.
Definition: SymbolCollector.cpp:200
clang::clangd::SymbolCollector::handleDeclOccurrence
bool handleDeclOccurrence(const Decl *D, index::SymbolRoleSet Roles, ArrayRef< index::SymbolRelation > Relations, SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override
Definition: SymbolCollector.cpp:246
clang::clangd::RefKind
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:30
Index.h
clang::clangd::RefSlab::Builder
RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
Definition: Ref.h:128
clang::clangd::SymbolCollector::shouldIndexFile
bool shouldIndexFile(FileID FID)
Returns true if we are interested in references and declarations from FID.
Definition: SymbolCollector.cpp:796
clang::clangd::SymbolCollector::Options::FileFilter
std::function< bool(const SourceManager &, FileID)> FileFilter
If this is set, only collect symbols/references from a file if FileFilter(SM, FID) is true.
Definition: SymbolCollector.h:87
clang::clangd::SymbolCollector::takeRelations
RelationSlab takeRelations()
Definition: SymbolCollector.h:116
Ctx
Context Ctx
Definition: TUScheduler.cpp:324
clang::clangd::RelationSlab::Builder
RelationSlab::Builder is a mutable container that can 'freeze' to RelationSlab.
Definition: Relation.h:70
clang::clangd::SymbolCollector::finish
void finish() override
Definition: SymbolCollector.cpp:494
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:104
clang::clangd::SymbolCollector::Options::RefFilter
RefKind RefFilter
The symbol ref kinds that will be collected.
Definition: SymbolCollector.h:65
CanonicalIncludes.h
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:36
clang::clangd::RelationSlab
Definition: Relation.h:45
CollectMacros.h
clang::clangd::SymbolCollector::takeRefs
RefSlab takeRefs()
Definition: SymbolCollector.h:115
clang::clangd::MainFileMacros
Definition: CollectMacros.h:24
clang::clangd::SymbolCollector::Options::CollectMacro
bool CollectMacro
Collect macros.
Definition: SymbolCollector.h:77
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::clangd::SymbolCollector::Options::Includes
const CanonicalIncludes * Includes
If set, this is used to map symbol #include path to a potentially different #include path.
Definition: SymbolCollector.h:57
clang::clangd::SymbolOrigin
SymbolOrigin
Definition: SymbolOrigin.h:21
clang::clangd::SymbolCollector::handleMacroOccurrence
bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI, index::SymbolRoleSet Roles, SourceLocation Loc) override
Definition: SymbolCollector.cpp:371
clang::clangd::SymbolCollector::takeSymbols
SymbolSlab takeSymbols()
Definition: SymbolCollector.h:114
clang::clangd::SymbolCollector::initialize
void initialize(ASTContext &Ctx) override
Definition: SymbolCollector.cpp:193
clang::clangd::SymbolCollector
Collect declarations (symbols) from an AST.
Definition: SymbolCollector.h:46
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::SymbolCollector::Options::CountReferences
bool CountReferences
Definition: SymbolCollector.h:59
clang::clangd::SymbolCollector::Options
Definition: SymbolCollector.h:48
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::clangd::SymbolOrigin::Unknown
clang::clangd::SymbolCollector::Options::CollectIncludePath
bool CollectIncludePath
Definition: SymbolCollector.h:54
clang::clangd::SymbolSlab::Builder
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
Definition: Symbol.h:200
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::SymbolCollector::Options::StoreAllDocumentation
bool StoreAllDocumentation
If set to true, SymbolCollector will collect doc for all symbols.
Definition: SymbolCollector.h:84
clang::clangd::SymbolID
Definition: SymbolID.h:31
clang::clangd::SymbolCollector::SymbolCollector
SymbolCollector(Options Opts)
Definition: SymbolCollector.cpp:191
clang::clangd::SymbolCollector::Options::FallbackDir
std::string FallbackDir
When symbol paths cannot be resolved to absolute paths (e.g.
Definition: SymbolCollector.h:53