clang-tools  8.0.0
SymbolCollector.h
Go to the documentation of this file.
1 //===--- SymbolCollector.h ---------------------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_COLLECTOR_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_COLLECTOR_H
11 
12 #include "CanonicalIncludes.h"
13 #include "Index.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/Basic/SourceLocation.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Index/IndexDataConsumer.h"
19 #include "clang/Index/IndexSymbol.h"
20 #include "clang/Sema/CodeCompleteConsumer.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include <functional>
23 
24 namespace clang {
25 namespace clangd {
26 
27 /// \brief Collect declarations (symbols) from an AST.
28 /// It collects most declarations except:
29 /// - Implicit declarations
30 /// - Anonymous declarations (anonymous enum/class/struct, etc)
31 /// - Declarations in anonymous namespaces in headers
32 /// - Local declarations (in function bodies, blocks, etc)
33 /// - Template specializations
34 /// - Library-specific private declarations (e.g. private declaration generated
35 /// by protobuf compiler)
36 ///
37 /// References to main-file symbols are not collected.
38 ///
39 /// See also shouldCollectSymbol(...).
40 ///
41 /// Clients (e.g. clangd) can use SymbolCollector together with
42 /// index::indexTopLevelDecls to retrieve all symbols when the source file is
43 /// changed.
44 class SymbolCollector : public index::IndexDataConsumer {
45 public:
46  struct Options {
47  /// When symbol paths cannot be resolved to absolute paths (e.g. files in
48  /// VFS that does not have absolute path), combine the fallback directory
49  /// with symbols' paths to get absolute paths. This must be an absolute
50  /// path.
51  std::string FallbackDir;
52  bool CollectIncludePath = false;
53  /// If set, this is used to map symbol #include path to a potentially
54  /// different #include path.
55  const CanonicalIncludes *Includes = nullptr;
56  // Populate the Symbol.References field.
57  bool CountReferences = false;
58  /// The symbol ref kinds that will be collected.
59  /// If not set, SymbolCollector will not collect refs.
60  /// Note that references of namespace decls are not collected, as they
61  /// contribute large part of the index, and they are less useful compared
62  /// with other decls.
64  /// If set to true, SymbolCollector will collect all refs (from main file
65  /// and included headers); otherwise, only refs from main file will be
66  /// collected.
67  /// This flag is only meaningful when RefFilter is set.
68  bool RefsInHeaders = false;
69  // Every symbol collected will be stamped with this origin.
71  /// Collect macros.
72  /// Note that SymbolCollector must be run with preprocessor in order to
73  /// collect macros. For example, `indexTopLevelDecls` will not index any
74  /// macro even if this is true.
75  bool CollectMacro = false;
76  /// Collect symbols local to main-files, such as static functions
77  /// and symbols inside an anonymous namespace.
79  /// If this is set, only collect symbols/references from a file if
80  /// `FileFilter(SM, FID)` is true. If not set, all files are indexed.
81  std::function<bool(const SourceManager &, FileID)> FileFilter = nullptr;
82  };
83 
85 
86  /// Returns true is \p ND should be collected.
87  static bool shouldCollectSymbol(const NamedDecl &ND, const ASTContext &ASTCtx,
88  const Options &Opts, bool IsMainFileSymbol);
89 
90  void initialize(ASTContext &Ctx) override;
91 
92  void setPreprocessor(std::shared_ptr<Preprocessor> PP) override {
93  this->PP = std::move(PP);
94  }
95 
96  bool
97  handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
98  ArrayRef<index::SymbolRelation> Relations,
99  SourceLocation Loc,
100  index::IndexDataConsumer::ASTNodeInfo ASTNode) override;
101 
102  bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI,
103  index::SymbolRoleSet Roles,
104  SourceLocation Loc) override;
105 
106  SymbolSlab takeSymbols() { return std::move(Symbols).build(); }
107  RefSlab takeRefs() { return std::move(Refs).build(); }
108 
109  void finish() override;
110 
111 private:
112  const Symbol *addDeclaration(const NamedDecl &, SymbolID, bool IsMainFileSymbol);
113  void addDefinition(const NamedDecl &, const Symbol &DeclSymbol);
114 
115  // All Symbols collected from the AST.
116  SymbolSlab::Builder Symbols;
117  // All refs collected from the AST.
118  // Only symbols declared in preamble (from #include) and referenced from the
119  // main file will be included.
120  RefSlab::Builder Refs;
121  ASTContext *ASTCtx;
122  std::shared_ptr<Preprocessor> PP;
123  std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator;
124  std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo;
125  Options Opts;
126  using DeclRef = std::pair<SourceLocation, index::SymbolRoleSet>;
127  // Symbols referenced from the current TU, flushed on finish().
128  llvm::DenseSet<const NamedDecl *> ReferencedDecls;
129  llvm::DenseSet<const IdentifierInfo *> ReferencedMacros;
130  llvm::DenseMap<const NamedDecl *, std::vector<DeclRef>> DeclRefs;
131  // Maps canonical declaration provided by clang to canonical declaration for
132  // an index symbol, if clangd prefers a different declaration than that
133  // provided by clang. For example, friend declaration might be considered
134  // canonical by clang but should not be considered canonical in the index
135  // unless it's a definition.
136  llvm::DenseMap<const Decl *, const Decl *> CanonicalDecls;
137  // Cache whether to index a file or not.
138  llvm::DenseMap<FileID, bool> FilesToIndexCache;
139 };
140 
141 } // namespace clangd
142 } // namespace clang
143 #endif
SourceLocation Loc
&#39;#&#39; location in the include directive
bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI, index::SymbolRoleSet Roles, SourceLocation Loc) override
Collect declarations (symbols) from an AST.
void initialize(ASTContext &Ctx) override
Maps a definition location onto an #include file, based on a set of filename rules.
Context Ctx
std::string FallbackDir
When symbol paths cannot be resolved to absolute paths (e.g.
bool handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles, ArrayRef< index::SymbolRelation > Relations, SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override
static constexpr llvm::StringLiteral Name
const Decl * D
Definition: XRefs.cpp:79
bool CollectMainFileSymbols
Collect symbols local to main-files, such as static functions and symbols inside an anonymous namespa...
std::function< bool(const SourceManager &, FileID)> FileFilter
If this is set, only collect symbols/references from a file if FileFilter(SM, FID) is true...
static bool shouldCollectSymbol(const NamedDecl &ND, const ASTContext &ASTCtx, const Options &Opts, bool IsMainFileSymbol)
Returns true is ND should be collected.
bool RefsInHeaders
If set to true, SymbolCollector will collect all refs (from main file and included headers); otherwis...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void setPreprocessor(std::shared_ptr< Preprocessor > PP) override
const CanonicalIncludes * Includes
If set, this is used to map symbol #include path to a potentially different #include path...
RefKind RefFilter
The symbol ref kinds that will be collected.