diff options
Diffstat (limited to 'clangd/index/SymbolCollector.h')
-rw-r--r-- | clangd/index/SymbolCollector.h | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/clangd/index/SymbolCollector.h b/clangd/index/SymbolCollector.h index 269c184b..bc882e47 100644 --- a/clangd/index/SymbolCollector.h +++ b/clangd/index/SymbolCollector.h @@ -18,13 +18,18 @@ namespace clang { namespace clangd { -/// \brief Collect top-level symbols from an AST. These are symbols defined -/// immediately inside a namespace or a translation unit scope. For example, -/// symbols in classes or functions are not collected. Note that this only -/// collects symbols that declared in at least one file that is not a main -/// file (i.e. the source file corresponding to a TU). These are symbols that -/// can be imported by other files by including the file where symbols are -/// declared. +/// \brief Collect declarations (symbols) from an AST. +/// It collects most declarations except: +/// - Implicit declarations +/// - Anonymous declarations (anonymous enum/class/struct, etc) +/// - Declarations in anonymous namespaces +/// - Local declarations (in function bodies, blocks, etc) +/// - Declarations in main files +/// - Template specializations +/// - Library-specific private declarations (e.g. private declaration generated +/// by protobuf compiler) +/// +/// See also shouldCollectSymbol(...). /// /// Clients (e.g. clangd) can use SymbolCollector together with /// index::indexTopLevelDecls to retrieve all symbols when the source file is @@ -47,10 +52,22 @@ public: const CanonicalIncludes *Includes = nullptr; // Populate the Symbol.References field. bool CountReferences = false; + // Every symbol collected will be stamped with this origin. + SymbolOrigin Origin = SymbolOrigin::Unknown; + /// Collect macros. + /// Note that SymbolCollector must be run with preprocessor in order to + /// collect macros. For example, `indexTopLevelDecls` will not index any + /// macro even if this is true. + bool CollectMacro = false; }; SymbolCollector(Options Opts); + /// Returns true is \p ND should be collected. + /// AST matchers require non-const ASTContext. + static bool shouldCollectSymbol(const NamedDecl &ND, ASTContext &ASTCtx, + const Options &Opts); + void initialize(ASTContext &Ctx) override; void setPreprocessor(std::shared_ptr<Preprocessor> PP) override { @@ -63,6 +80,10 @@ public: SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override; + bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI, + index::SymbolRoleSet Roles, + SourceLocation Loc) override; + SymbolSlab takeSymbols() { return std::move(Symbols).build(); } void finish() override; @@ -78,8 +99,15 @@ private: std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator; std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo; Options Opts; - // Decls referenced from the current TU, flushed on finish(). + // Symbols referenced from the current TU, flushed on finish(). llvm::DenseSet<const NamedDecl *> ReferencedDecls; + llvm::DenseSet<const IdentifierInfo *> ReferencedMacros; + // Maps canonical declaration provided by clang to canonical declaration for + // an index symbol, if clangd prefers a different declaration than that + // provided by clang. For example, friend declaration might be considered + // canonical by clang but should not be considered canonical in the index + // unless it's a definition. + llvm::DenseMap<const Decl *, const Decl *> CanonicalDecls; }; } // namespace clangd |