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

Marc-Andre Laperle via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 7 21:14:51 PST 2017


malaperle added a comment.

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;
  };

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.



================
Comment at: clangd/SymbolIndex.h:50
+  virtual llvm::Expected<CompletionResult>
+  complete(const CompletionRequest &Req) const = 0;
+
----------------
sammccall wrote:
> This is finding symbols that fuzzy-match a string, right?
> We shouldn't conflate this with code completion - code completion is context-sensitive, and this query operation will be used for other operations like navigate-to-symbol.
> 
> Suggest `fuzzyFind` or similar.
> (Similarly with CompletionRequest, CompletionSymbol)
Similar to completion is "Open Workspace Symbol". So a more generic query could be useful.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D40548





More information about the cfe-commits mailing list