[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
//===--- Symbol.h -----------------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
|
|
|
|
|
|
|
|
#include "clang/Index/IndexSymbol.h"
|
2018-02-13 17:53:50 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-12-24 03:38:03 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2017-12-13 16:34:48 +08:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2018-01-10 01:32:00 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
struct SymbolLocation {
|
2018-02-07 00:10:35 +08:00
|
|
|
// The URI of the source file where a symbol occurs.
|
|
|
|
llvm::StringRef FileURI;
|
2018-02-13 17:53:50 +08:00
|
|
|
// The 0-based offsets of the symbol from the beginning of the source file,
|
|
|
|
// using half-open range, [StartOffset, EndOffset).
|
2018-02-09 22:42:01 +08:00
|
|
|
unsigned StartOffset = 0;
|
|
|
|
unsigned EndOffset = 0;
|
|
|
|
|
|
|
|
operator bool() const { return !FileURI.empty(); }
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
};
|
2018-02-09 22:42:01 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &);
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
|
|
|
// The class identifies a particular C++ symbol (class, function, method, etc).
|
|
|
|
//
|
|
|
|
// As USRs (Unified Symbol Resolution) could be large, especially for functions
|
|
|
|
// with long type arguments, SymbolID is using 160-bits SHA1(USR) values to
|
|
|
|
// guarantee the uniqueness of symbols while using a relatively small amount of
|
|
|
|
// memory (vs storing USRs directly).
|
|
|
|
//
|
|
|
|
// SymbolID can be used as key in the symbol indexes to lookup the symbol.
|
|
|
|
class SymbolID {
|
|
|
|
public:
|
|
|
|
SymbolID() = default;
|
2018-02-09 22:42:01 +08:00
|
|
|
explicit SymbolID(llvm::StringRef USR);
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2017-12-13 20:53:16 +08:00
|
|
|
bool operator==(const SymbolID &Sym) const {
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
return HashValue == Sym.HashValue;
|
|
|
|
}
|
2017-12-24 03:38:03 +08:00
|
|
|
bool operator<(const SymbolID &Sym) const {
|
|
|
|
return HashValue < Sym.HashValue;
|
|
|
|
}
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
|
|
|
private:
|
2017-12-13 16:34:48 +08:00
|
|
|
friend llvm::hash_code hash_value(const SymbolID &ID) {
|
2017-12-22 04:11:46 +08:00
|
|
|
// We already have a good hash, just return the first bytes.
|
|
|
|
static_assert(sizeof(size_t) <= 20, "size_t longer than SHA1!");
|
|
|
|
return *reinterpret_cast<const size_t *>(ID.HashValue.data());
|
2017-12-13 16:34:48 +08:00
|
|
|
}
|
2017-12-14 20:17:14 +08:00
|
|
|
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
|
|
|
|
const SymbolID &ID);
|
|
|
|
friend void operator>>(llvm::StringRef Str, SymbolID &ID);
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
|
|
|
std::array<uint8_t, 20> HashValue;
|
|
|
|
};
|
|
|
|
|
2017-12-14 20:17:14 +08:00
|
|
|
// Write SymbolID into the given stream. SymbolID is encoded as a 40-bytes
|
|
|
|
// hex string.
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID);
|
|
|
|
|
|
|
|
// Construct SymbolID from a hex string.
|
|
|
|
// The HexStr is required to be a 40-bytes hex string, which is encoded from the
|
|
|
|
// "<<" operator.
|
|
|
|
void operator>>(llvm::StringRef HexStr, SymbolID &ID);
|
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
namespace llvm {
|
|
|
|
// Support SymbolIDs as DenseMap keys.
|
|
|
|
template <> struct DenseMapInfo<clang::clangd::SymbolID> {
|
|
|
|
static inline clang::clangd::SymbolID getEmptyKey() {
|
|
|
|
static clang::clangd::SymbolID EmptyKey("EMPTYKEY");
|
|
|
|
return EmptyKey;
|
|
|
|
}
|
|
|
|
static inline clang::clangd::SymbolID getTombstoneKey() {
|
|
|
|
static clang::clangd::SymbolID TombstoneKey("TOMBSTONEKEY");
|
|
|
|
return TombstoneKey;
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(const clang::clangd::SymbolID &Sym) {
|
|
|
|
return hash_value(Sym);
|
|
|
|
}
|
|
|
|
static bool isEqual(const clang::clangd::SymbolID &LHS,
|
|
|
|
const clang::clangd::SymbolID &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace llvm
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
// The class presents a C++ symbol, e.g. class, function.
|
|
|
|
//
|
2017-12-21 22:58:44 +08:00
|
|
|
// WARNING: Symbols do not own much of their underlying data - typically strings
|
|
|
|
// are owned by a SymbolSlab. They should be treated as non-owning references.
|
|
|
|
// Copies are shallow.
|
2018-01-15 20:33:00 +08:00
|
|
|
// When adding new unowned data fields to Symbol, remember to update:
|
|
|
|
// - SymbolSlab::Builder in Index.cpp, to copy them to the slab's storage.
|
|
|
|
// - mergeSymbol in Merge.cpp, to properly combine two Symbols.
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
struct Symbol {
|
|
|
|
// The ID of the symbol.
|
|
|
|
SymbolID ID;
|
2017-12-22 16:12:39 +08:00
|
|
|
// The symbol information, like symbol kind.
|
|
|
|
index::SymbolInfo SymInfo;
|
2018-01-20 06:18:21 +08:00
|
|
|
// The unqualified name of the symbol, e.g. "bar" (for ns::bar).
|
2017-12-21 22:58:44 +08:00
|
|
|
llvm::StringRef Name;
|
2018-01-20 06:18:21 +08:00
|
|
|
// The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
|
2017-12-21 22:58:44 +08:00
|
|
|
llvm::StringRef Scope;
|
2018-02-09 22:42:01 +08:00
|
|
|
// The location of the symbol's definition, if one was found.
|
2018-02-13 17:53:50 +08:00
|
|
|
// This just covers the symbol name (e.g. without class/function body).
|
2018-02-09 22:42:01 +08:00
|
|
|
SymbolLocation Definition;
|
|
|
|
// The location of the preferred declaration of the symbol.
|
2018-02-13 17:53:50 +08:00
|
|
|
// This just covers the symbol name.
|
2018-02-09 22:42:01 +08:00
|
|
|
// This may be the same as Definition.
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
//
|
2018-02-09 22:42:01 +08:00
|
|
|
// A C++ symbol may have multiple declarations, and we pick one to prefer.
|
|
|
|
// * For classes, the canonical declaration should be the definition.
|
|
|
|
// * For non-inline functions, the canonical declaration typically appears
|
|
|
|
// in the ".h" file corresponding to the definition.
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
SymbolLocation CanonicalDeclaration;
|
2018-03-12 22:49:09 +08:00
|
|
|
// The number of translation units that reference this symbol from their main
|
|
|
|
// file. This number is only meaningful if aggregated in an index.
|
|
|
|
unsigned References = 0;
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2018-01-10 01:32:00 +08:00
|
|
|
/// A brief description of the symbol that can be displayed in the completion
|
|
|
|
/// candidate list. For example, "Foo(X x, Y y) const" is a labal for a
|
|
|
|
/// function.
|
|
|
|
llvm::StringRef CompletionLabel;
|
|
|
|
/// The piece of text that the user is expected to type to match the
|
|
|
|
/// code-completion string, typically a keyword or the name of a declarator or
|
|
|
|
/// macro.
|
|
|
|
llvm::StringRef CompletionFilterText;
|
|
|
|
/// What to insert when completing this symbol (plain text version).
|
|
|
|
llvm::StringRef CompletionPlainInsertText;
|
|
|
|
/// What to insert when completing this symbol (snippet version). This is
|
|
|
|
/// empty if it is the same as the plain insert text above.
|
|
|
|
llvm::StringRef CompletionSnippetInsertText;
|
|
|
|
|
|
|
|
/// Optional symbol details that are not required to be set. For example, an
|
|
|
|
/// index fuzzy match can return a large number of symbol candidates, and it
|
|
|
|
/// is preferable to send only core symbol information in the batched results
|
|
|
|
/// and have clients resolve full symbol information for a specific candidate
|
|
|
|
/// if needed.
|
|
|
|
struct Details {
|
2018-02-16 22:15:55 +08:00
|
|
|
/// Documentation including comment for the symbol declaration.
|
2018-01-10 01:32:00 +08:00
|
|
|
llvm::StringRef Documentation;
|
2018-02-16 22:15:55 +08:00
|
|
|
/// This is what goes into the LSP detail field in a completion item. For
|
|
|
|
/// example, the result type of a function.
|
2018-01-10 01:32:00 +08:00
|
|
|
llvm::StringRef CompletionDetail;
|
2018-02-16 22:15:55 +08:00
|
|
|
/// This can be either a URI of the header to be #include'd for this symbol,
|
|
|
|
/// or a literal header quoted with <> or "" that is suitable to be included
|
|
|
|
/// directly. When this is a URI, the exact #include path needs to be
|
|
|
|
/// calculated according to the URI scheme.
|
|
|
|
///
|
[clangd] Not collect include headers for dynamic index for now.
Summary:
The new behaviors introduced by this patch:
o When include collection is enabled, we always set IncludeHeader field in Symbol
even if it's the same as FileURI in decl.
o Disable include collection in FileIndex which is currently only used to build
dynamic index. We should revisit when we actually want to use FileIndex to global
index.
o Code-completion only uses IncludeHeader to insert headers but not FileURI in
CanonicalDeclaration. This ensures that inserted headers are always canonicalized.
Note that include insertion can still be triggered for symbols that are already
included if they are merged from dynamic index and static index, but we would
only use includes that are already canonicalized (e.g. from static index).
Reason for change:
Collecting header includes in dynamic index enables inserting includes for headers
that are not indexed but opened in the editor. Comparing to inserting includes for
symbols in global/static index, this is nice-to-have but would probably require
non-trivial amount of work to get right. For example:
o Currently it's not easy to fully support CanonicalIncludes in dynamic index, given the way
we run dynamic index.
o It's also harder to reason about the correctness of include canonicalization for dynamic index
(i.e. symbols in the current file/TU) than static index where symbols are collected
offline and sanity check is possible before shipping to production.
o We have less control/flexibility over symbol info in the dynamic index
(e.g. URIs, path normalization), which could be used to help make decision when inserting includes.
As header collection (especially canonicalization) is relatively new, and enabling
it for dynamic index would immediately affect current users with only dynamic
index support, I propose we disable it for dynamic index for now to avoid
compromising other hot features like code completion and only support it for
static index where include insertion would likely to bring more value.
Reviewers: ilya-biryukov, sammccall, hokein
Subscribers: klimek, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43550
llvm-svn: 325764
2018-02-22 18:14:05 +08:00
|
|
|
/// This is a canonical include for the symbol and can be different from
|
|
|
|
/// FileURI in the CanonicalDeclaration.
|
2018-02-16 22:15:55 +08:00
|
|
|
llvm::StringRef IncludeHeader;
|
2018-01-10 01:32:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Optional details of the symbol.
|
2018-01-16 04:09:09 +08:00
|
|
|
const Details *Detail = nullptr;
|
2018-01-10 01:32:00 +08:00
|
|
|
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
// FIXME: add all occurrences support.
|
|
|
|
// FIXME: add extra fields for index scoring signals.
|
|
|
|
};
|
2018-02-09 22:42:01 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S);
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
// An immutable symbol container that stores a set of symbols.
|
|
|
|
// The container will maintain the lifetime of the symbols.
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
class SymbolSlab {
|
2017-12-13 20:53:16 +08:00
|
|
|
public:
|
2017-12-24 03:38:03 +08:00
|
|
|
using const_iterator = std::vector<Symbol>::const_iterator;
|
2018-02-09 22:42:01 +08:00
|
|
|
using iterator = const_iterator;
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
|
|
|
SymbolSlab() = default;
|
|
|
|
|
2017-12-28 22:47:01 +08:00
|
|
|
const_iterator begin() const { return Symbols.begin(); }
|
|
|
|
const_iterator end() const { return Symbols.end(); }
|
2017-12-13 20:53:16 +08:00
|
|
|
const_iterator find(const SymbolID &SymID) const;
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
size_t size() const { return Symbols.size(); }
|
|
|
|
// Estimates the total memory usage.
|
|
|
|
size_t bytes() const {
|
|
|
|
return sizeof(*this) + Arena.getTotalMemory() +
|
|
|
|
Symbols.capacity() * sizeof(Symbol);
|
|
|
|
}
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
// SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
|
|
|
|
// The frozen SymbolSlab will use less memory.
|
|
|
|
class Builder {
|
|
|
|
public:
|
|
|
|
// Adds a symbol, overwriting any existing one with the same ID.
|
|
|
|
// This is a deep copy: underlying strings will be owned by the slab.
|
|
|
|
void insert(const Symbol& S);
|
|
|
|
|
|
|
|
// Returns the symbol with an ID, if it exists. Valid until next insert().
|
|
|
|
const Symbol* find(const SymbolID &ID) {
|
|
|
|
auto I = SymbolIndex.find(ID);
|
|
|
|
return I == SymbolIndex.end() ? nullptr : &Symbols[I->second];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consumes the builder to finalize the slab.
|
|
|
|
SymbolSlab build() &&;
|
|
|
|
|
|
|
|
private:
|
|
|
|
llvm::BumpPtrAllocator Arena;
|
|
|
|
// Intern table for strings. Contents are on the arena.
|
|
|
|
llvm::DenseSet<llvm::StringRef> Strings;
|
|
|
|
std::vector<Symbol> Symbols;
|
|
|
|
// Values are indices into Symbols vector.
|
|
|
|
llvm::DenseMap<SymbolID, size_t> SymbolIndex;
|
|
|
|
};
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2017-12-13 20:53:16 +08:00
|
|
|
private:
|
2017-12-24 03:38:03 +08:00
|
|
|
SymbolSlab(llvm::BumpPtrAllocator Arena, std::vector<Symbol> Symbols)
|
|
|
|
: Arena(std::move(Arena)), Symbols(std::move(Symbols)) {}
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
llvm::BumpPtrAllocator Arena; // Owns Symbol data that the Symbols do not.
|
|
|
|
std::vector<Symbol> Symbols; // Sorted by SymbolID to allow lookup.
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
};
|
|
|
|
|
2017-12-14 19:25:49 +08:00
|
|
|
struct FuzzyFindRequest {
|
|
|
|
/// \brief A query string for the fuzzy find. This is matched against symbols'
|
2017-12-19 19:37:40 +08:00
|
|
|
/// un-qualified identifiers and should not contain qualifiers like "::".
|
2017-12-14 19:25:49 +08:00
|
|
|
std::string Query;
|
2017-12-19 19:37:40 +08:00
|
|
|
/// \brief If this is non-empty, symbols must be in at least one of the scopes
|
2018-01-20 06:18:21 +08:00
|
|
|
/// (e.g. namespaces) excluding nested scopes. For example, if a scope "xyz::"
|
|
|
|
/// is provided, the matched symbols must be defined in namespace xyz but not
|
|
|
|
/// namespace xyz::abc.
|
2017-12-19 19:37:40 +08:00
|
|
|
///
|
2018-01-20 06:18:21 +08:00
|
|
|
/// The global scope is "", a top level scope is "foo::", etc.
|
2017-12-19 19:37:40 +08:00
|
|
|
std::vector<std::string> Scopes;
|
2018-01-15 20:33:00 +08:00
|
|
|
/// \brief The number of top candidates to return. The index may choose to
|
|
|
|
/// return more than this, e.g. if it doesn't know which candidates are best.
|
2017-12-14 19:25:49 +08:00
|
|
|
size_t MaxCandidateCount = UINT_MAX;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \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 {
|
|
|
|
public:
|
|
|
|
virtual ~SymbolIndex() = default;
|
|
|
|
|
|
|
|
/// \brief Matches symbols in the index fuzzily and applies \p Callback on
|
|
|
|
/// each matched symbol before returning.
|
2018-01-15 20:33:00 +08:00
|
|
|
/// If returned Symbols are used outside Callback, they must be deep-copied!
|
2017-12-14 19:25:49 +08:00
|
|
|
///
|
2018-02-19 21:04:41 +08:00
|
|
|
/// Returns true if there may be more results (limited by MaxCandidateCount).
|
2017-12-14 19:25:49 +08:00
|
|
|
virtual bool
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
fuzzyFind(const FuzzyFindRequest &Req,
|
2017-12-28 22:47:01 +08:00
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const = 0;
|
2017-12-14 19:25:49 +08:00
|
|
|
|
|
|
|
// FIXME: add interfaces for more index use cases:
|
|
|
|
// - Symbol getSymbolInfo(SymbolID);
|
|
|
|
// - getAllOccurrences(SymbolID);
|
|
|
|
};
|
|
|
|
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
|