[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
|
|
|
//===--- SymbolCollector.h ---------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "CanonicalIncludes.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 "Index.h"
|
2018-01-10 01:32:00 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.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 "clang/Index/IndexDataConsumer.h"
|
|
|
|
#include "clang/Index/IndexSymbol.h"
|
2018-01-10 01:32:00 +08:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.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
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
[clangd] Add "member" symbols to the index
Summary:
This adds more symbols to the index:
- member variables and functions
- enum constants in scoped enums
The code completion behavior should remain intact but workspace symbols should
now provide much more useful symbols.
Other symbols should be considered such as the ones in "main files" (files not
being included) but this can be done separately as this introduces its fair
share of problems.
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewers: ioeric, sammccall
Reviewed By: ioeric, sammccall
Subscribers: hokein, sammccall, jkorous, klimek, ilya-biryukov, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44954
llvm-svn: 334017
2018-06-05 22:01:40 +08:00
|
|
|
/// \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)
|
|
|
|
///
|
2018-06-21 20:12:26 +08:00
|
|
|
/// See also shouldCollectSymbol(...).
|
2018-01-10 22:57:58 +08:00
|
|
|
///
|
|
|
|
/// Clients (e.g. clangd) can use SymbolCollector together with
|
|
|
|
/// index::indexTopLevelDecls to retrieve all symbols when the source file is
|
|
|
|
/// changed.
|
[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 SymbolCollector : public index::IndexDataConsumer {
|
|
|
|
public:
|
2018-01-10 22:57:58 +08:00
|
|
|
struct Options {
|
2018-02-07 00:10:35 +08:00
|
|
|
/// When symbol paths cannot be resolved to absolute paths (e.g. files in
|
|
|
|
/// VFS that does not have absolute path), combine the fallback directory
|
|
|
|
/// with symbols' paths to get absolute paths. This must be an absolute
|
|
|
|
/// path.
|
2018-01-29 23:13:29 +08:00
|
|
|
std::string FallbackDir;
|
2018-02-07 00:10:35 +08:00
|
|
|
/// Specifies URI schemes that can be used to generate URIs for file paths
|
|
|
|
/// in symbols. The list of schemes will be tried in order until a working
|
|
|
|
/// scheme is found. If no scheme works, symbol location will be dropped.
|
|
|
|
std::vector<std::string> URISchemes = {"file"};
|
2018-02-16 22:15:55 +08:00
|
|
|
bool CollectIncludePath = false;
|
|
|
|
/// If set, this is used to map symbol #include path to a potentially
|
|
|
|
/// different #include path.
|
|
|
|
const CanonicalIncludes *Includes = nullptr;
|
2018-03-12 22:49:09 +08:00
|
|
|
// Populate the Symbol.References field.
|
|
|
|
bool CountReferences = false;
|
2018-07-05 14:20:41 +08:00
|
|
|
// Every symbol collected will be stamped with this origin.
|
|
|
|
SymbolOrigin Origin = SymbolOrigin::Unknown;
|
2018-07-09 23:31:07 +08:00
|
|
|
/// 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;
|
2018-01-10 22:57:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
SymbolCollector(Options Opts);
|
|
|
|
|
2018-06-21 20:12:26 +08:00
|
|
|
/// 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);
|
|
|
|
|
2018-01-10 01:32:00 +08:00
|
|
|
void initialize(ASTContext &Ctx) override;
|
|
|
|
|
|
|
|
void setPreprocessor(std::shared_ptr<Preprocessor> PP) override {
|
|
|
|
this->PP = std::move(PP);
|
|
|
|
}
|
[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
|
|
|
|
|
|
|
bool
|
|
|
|
handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
|
2018-04-09 22:28:52 +08:00
|
|
|
ArrayRef<index::SymbolRelation> Relations,
|
|
|
|
SourceLocation Loc,
|
[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
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override;
|
|
|
|
|
2018-07-09 23:31:07 +08:00
|
|
|
bool handleMacroOccurence(const IdentifierInfo *Name, const MacroInfo *MI,
|
|
|
|
index::SymbolRoleSet Roles,
|
|
|
|
SourceLocation Loc) override;
|
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
SymbolSlab takeSymbols() { return std::move(Symbols).build(); }
|
[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-03-12 22:49:09 +08:00
|
|
|
void finish() override;
|
|
|
|
|
[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:
|
2018-02-09 22:42:01 +08:00
|
|
|
const Symbol *addDeclaration(const NamedDecl &, SymbolID);
|
|
|
|
void addDefinition(const NamedDecl &, const Symbol &DeclSymbol);
|
|
|
|
|
[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
|
|
|
// All Symbols collected from the AST.
|
2017-12-24 03:38:03 +08:00
|
|
|
SymbolSlab::Builder Symbols;
|
2018-01-10 01:32:00 +08:00
|
|
|
ASTContext *ASTCtx;
|
|
|
|
std::shared_ptr<Preprocessor> PP;
|
|
|
|
std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator;
|
|
|
|
std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo;
|
2018-01-10 22:57:58 +08:00
|
|
|
Options Opts;
|
2018-07-09 23:31:07 +08:00
|
|
|
// Symbols referenced from the current TU, flushed on finish().
|
2018-03-12 22:49:09 +08:00
|
|
|
llvm::DenseSet<const NamedDecl *> ReferencedDecls;
|
2018-07-09 23:31:07 +08:00
|
|
|
llvm::DenseSet<const IdentifierInfo *> ReferencedMacros;
|
2018-06-04 19:31:55 +08:00
|
|
|
// 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;
|
[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
|