Index: clangd/index/FileIndex.h =================================================================== --- clangd/index/FileIndex.h +++ clangd/index/FileIndex.h @@ -73,6 +73,10 @@ void lookup(const LookupRequest &Req, llvm::function_ref Callback) const override; + + void findOccurrences(const OccurrencesRequest &Req, + llvm::function_ref + Callback) const override; private: FileSymbols FSymbols; MemIndex Index; Index: clangd/index/FileIndex.cpp =================================================================== --- clangd/index/FileIndex.cpp +++ clangd/index/FileIndex.cpp @@ -9,6 +9,7 @@ #include "FileIndex.h" #include "SymbolCollector.h" +#include "../Logger.h" #include "clang/Index/IndexingAction.h" #include "clang/Lex/Preprocessor.h" @@ -105,5 +106,11 @@ Index.lookup(Req, Callback); } +void FileIndex::findOccurrences( + const OccurrencesRequest &Req, + llvm::function_ref Callback) const { + log("findOccurrences is not implemented."); +} + } // namespace clangd } // namespace clang Index: clangd/index/Index.h =================================================================== --- clangd/index/Index.h +++ clangd/index/Index.h @@ -280,6 +280,40 @@ std::vector Symbols; // Sorted by SymbolID to allow lookup. }; +// Describes the kind of a symbol occurrence. +// +// This is a bitfield which can be combined from different kinds. +enum class SymbolOccurrenceKind : uint8_t { + Unknown = 0, + Declaration = static_cast(index::SymbolRole::Declaration), + Definition = static_cast(index::SymbolRole::Definition), + Reference = static_cast(index::SymbolRole::Reference), +}; +inline SymbolOccurrenceKind operator|(SymbolOccurrenceKind L, + SymbolOccurrenceKind R) { + return static_cast(static_cast(L) | + static_cast(R)); +} +inline SymbolOccurrenceKind &operator|=(SymbolOccurrenceKind &L, + SymbolOccurrenceKind R) { + return L = L | R; +} +inline SymbolOccurrenceKind operator&(SymbolOccurrenceKind A, + SymbolOccurrenceKind B) { + return static_cast(static_cast(A) & + static_cast(B)); +} + +// Represents a symbol occurrence in the source file. It could be a +// declaration/definition/reference occurrence. +// +// WARNING: Location does not own the underlying data - Copies are shallow. +struct SymbolOccurrence { + // The location of the occurrence. + SymbolLocation Location; + SymbolOccurrenceKind Kind; +}; + struct FuzzyFindRequest { /// \brief A query string for the fuzzy find. This is matched against symbols' /// un-qualified identifiers and should not contain qualifiers like "::". @@ -305,6 +339,11 @@ llvm::DenseSet IDs; }; +struct OccurrencesRequest { + llvm::DenseSet IDs; + SymbolOccurrenceKind Filter; +}; + /// \brief Interface for symbol indexes that can be used for searching or /// matching symbols among a set of symbols based on names or unique IDs. class SymbolIndex { @@ -327,8 +366,15 @@ lookup(const LookupRequest &Req, llvm::function_ref Callback) const = 0; - // FIXME: add interfaces for more index use cases: - // - getAllOccurrences(SymbolID); + /// CrossReference finds all symbol occurrences (e.g. references, + /// declarations, definitions) and applies \p Callback on each result. + /// + /// Resutls are returned in arbitrary order. + /// + /// The returned result must be deep-copied if it's used outside Callback. + virtual void findOccurrences( + const OccurrencesRequest &Req, + llvm::function_ref Callback) const = 0; }; } // namespace clangd Index: clangd/index/MemIndex.h =================================================================== --- clangd/index/MemIndex.h +++ clangd/index/MemIndex.h @@ -31,10 +31,14 @@ fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref Callback) const override; - virtual void + void lookup(const LookupRequest &Req, llvm::function_ref Callback) const override; + void findOccurrences(const OccurrencesRequest &Req, + llvm::function_ref + Callback) const override; + private: std::shared_ptr> Symbols; // Index is a set of symbols that are deduplicated by symbol IDs. Index: clangd/index/MemIndex.cpp =================================================================== --- clangd/index/MemIndex.cpp +++ clangd/index/MemIndex.cpp @@ -87,5 +87,11 @@ return std::move(MemIdx); } +void MemIndex::findOccurrences( + const OccurrencesRequest &Req, + llvm::function_ref Callback) const { + log("findOccurrences is not implemented."); +} + } // namespace clangd } // namespace clang Index: clangd/index/Merge.cpp =================================================================== --- clangd/index/Merge.cpp +++ clangd/index/Merge.cpp @@ -7,6 +7,7 @@ // //===---------------------------------------------------------------------===// #include "Merge.h" +#include "../Logger.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/raw_ostream.h" namespace clang { @@ -74,6 +75,12 @@ Callback(*Sym); } + void findOccurrences(const OccurrencesRequest &Req, + llvm::function_ref + Callback) const override { + log("findOccurrences is not implemented."); + } + private: const SymbolIndex *Dynamic, *Static; }; Index: unittests/clangd/CodeCompleteTests.cpp =================================================================== --- unittests/clangd/CodeCompleteTests.cpp +++ unittests/clangd/CodeCompleteTests.cpp @@ -893,6 +893,10 @@ void lookup(const LookupRequest &, llvm::function_ref) const override {} + void findOccurrences(const OccurrencesRequest &Req, + llvm::function_ref + Callback) const override {} + const std::vector allRequests() const { return Requests; } private: