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.
63 RefKind RefFilter = RefKind::Unknown;
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.
70 SymbolOrigin Origin = SymbolOrigin::Unknown;
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.
78 bool CollectMainFileSymbols = true;
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
84 SymbolCollector(Options Opts);
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
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::clangd::SymbolCollector::handleMacroOccurence
bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI, index::SymbolRoleSet Roles, SourceLocation Loc) override
Definition: SymbolCollector.cpp:367
clang::clangd::RefKind::Unknown
clang::clangd::SymbolCollector::Options::CollectMacro
bool CollectMacro
Collect macros.
Definition: SymbolCollector.h:75
clang::clangd::SymbolSlab
Definition: Index.h:292
clang::clangd::RefSlab
Definition: Index.h:388
Index.h
clang::clangd::SymbolCollector::takeSymbols
SymbolSlab takeSymbols()
Definition: SymbolCollector.h:106
clang::clangd::SymbolCollector
Collect declarations (symbols) from an AST.
Definition: SymbolCollector.h:44
clang::clangd::SymbolCollector::Options::CountReferences
bool CountReferences
Definition: SymbolCollector.h:57
clang::clangd::SymbolCollector::initialize
void initialize(ASTContext &Ctx) override
Definition: SymbolCollector.cpp:234
CanonicalIncludes.h
clang::clangd::SymbolSlab::Builder
Definition: Index.h:314
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:36
Ctx
Context Ctx
Definition: TUScheduler.cpp:224
clang::clangd::SymbolCollector::Options::CollectIncludePath
bool CollectIncludePath
Definition: SymbolCollector.h:52
clang::clangd::SymbolCollector::Options::FallbackDir
std::string FallbackDir
When symbol paths cannot be resolved to absolute paths (e.g.
Definition: SymbolCollector.h:51
clang::clangd::SymbolCollector::handleDeclOccurence
bool handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles, ArrayRef< index::SymbolRelation > Relations, SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override
Definition: SymbolCollector.cpp:293
clang::clangd::SymbolCollector::Options
Definition: SymbolCollector.h:46
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
D
const Decl * D
Definition: XRefs.cpp:79
clang::clangd::RefKind
RefKind
Definition: Index.h:351
clang::clangd::SymbolCollector::SymbolCollector
SymbolCollector(Options Opts)
Definition: SymbolCollector.cpp:232
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:78
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:81
clang::clangd::RefSlab::Builder
Definition: Index.h:411
clang::clangd::SymbolCollector::Options::Origin
SymbolOrigin Origin
Definition: SymbolCollector.h:70
clang::clangd::SymbolCollector::finish
void finish() override
Definition: SymbolCollector.cpp:439
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:241
clang::clangd::Symbol
Definition: Index.h:160
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:68
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::SymbolCollector::setPreprocessor
void setPreprocessor(std::shared_ptr< Preprocessor > PP) override
Definition: SymbolCollector.h:92
clang::clangd::SymbolOrigin::Unknown
clang::clangd::SymbolCollector::takeRefs
RefSlab takeRefs()
Definition: SymbolCollector.h:107
clang::clangd::SymbolOrigin
SymbolOrigin
Definition: Index.h:126
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:55
clang::clangd::SymbolCollector::Options::RefFilter
RefKind RefFilter
The symbol ref kinds that will be collected.
Definition: SymbolCollector.h:63
clang::clangd::SymbolID
Definition: SymbolID.h:31