[PATCH] D40548: [clangd] Symbol index interfaces and index-based code completion.

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 8 02:16:44 PST 2017


ioeric added a comment.

In https://reviews.llvm.org/D40548#949182, @malaperle wrote:

> As a follow-up, here's the interface for querying the index that I am using right now. It's meant to be able to retrieve from any kind of "backend", i.e. in-memory, ClangdIndexDataStore, libIndexStore, etc. I was able to implement "Open Workspace Symbol" (which is close to code completion in concept), Find References and Find Definitions.
>
>   using USR = llvm::SmallString<256>;
>  
>   class ClangdIndexDataOccurrence;
>  
>   class ClangdIndexDataSymbol {
>   public:
>     virtual index::SymbolKind getKind() = 0;
>     /// For example, for mynamespace::myclass::mymethod, this will be
>     /// mymethod.
>     virtual std::string getName() = 0;
>     /// For example, for mynamespace::myclass::mymethod, this will be
>     /// mynamespace::myclass::
>     virtual std::string getQualifier() = 0;
>     virtual std::string getUsr() = 0;
>  
>     virtual void foreachOccurrence(index::SymbolRoleSet Roles, llvm::function_ref<bool(ClangdIndexDataOccurrence&)> Receiver) = 0;
>  
>     virtual ~ClangdIndexDataSymbol() = default;
>   };
>  
>   class ClangdIndexDataOccurrence {
>   public:
>     enum class OccurrenceType : uint16_t {
>        OCCURRENCE,
>        DEFINITION_OCCURRENCE
>      };
>  
>     virtual OccurrenceType getKind() const = 0;
>     virtual std::string getPath() = 0;
>     /// Get the start offset of the symbol occurrence. The SourceManager can be
>     /// used for implementations that need to convert from a line/column
>     /// representation to an offset.
>     virtual uint32_t getStartOffset(SourceManager &SM) = 0;
>     /// Get the end offset of the symbol occurrence. The SourceManager can be
>     /// used for implementations that need to convert from a line/column
>     /// representation to an offset.
>     virtual uint32_t getEndOffset(SourceManager &SM) = 0;
>     virtual ~ClangdIndexDataOccurrence() = default;
>     //TODO: Add relations
>  
>     static bool classof(const ClangdIndexDataOccurrence *O) { return O->getKind() == OccurrenceType::OCCURRENCE; }
>   };
>  
>   /// An occurrence that also has definition with a body that requires additional
>   /// locations to keep track of the beginning and end of the body.
>   class ClangdIndexDataDefinitionOccurrence : public ClangdIndexDataOccurrence {
>   public:
>     virtual uint32_t getDefStartOffset(SourceManager &SM) = 0;
>     virtual uint32_t getDefEndOffset(SourceManager &SM) = 0;
>  
>     static bool classof(const ClangdIndexDataOccurrence *O) { return O->getKind() == OccurrenceType::DEFINITION_OCCURRENCE; }
>   };
>  
>   class ClangdIndexDataProvider {
>   public:
>  
>     virtual void foreachSymbols(StringRef Pattern, llvm::function_ref<bool(ClangdIndexDataSymbol&)> Receiver) = 0;
>     virtual void foreachSymbols(const USR &Usr, llvm::function_ref<bool(ClangdIndexDataSymbol&)> Receiver) = 0;
>  
>     virtual ~ClangdIndexDataProvider() = default;
>   };
>


I think some of the ideas here could be useful. This patch focuses mostly on index interfaces and https://reviews.llvm.org/D40897 emphasizes on the design of symbol structure. The way symbols are stored and used in this patch is likely to change depending on how https://reviews.llvm.org/D40897 goes.

> The "Clangd" prefix adds a bit much of clutter so maybe it should be removed.  I think the main points are that having generic foreachSymbols/foreachOccurrence with callbacks is well suited to implement multiple features with minimal copying.

Although I'm not sure if `foreachSymbols`/... would be feasible for all indexes yet, we do plan to switch to callback-based index interfaces, which Sam also proposed in the review comments.

There have been some offline discussions happening around clangd's indexing, and sorry that we have not been able to keep you up to date. I think it might be more efficient if we could meet via VC/Hangouts and sync on our designs. If you don't mind a meeting, I am happy to arrange it via emails.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D40548





More information about the cfe-commits mailing list