[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.cpp -------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SymbolCollector.h"
|
2018-01-10 01:32:00 +08:00
|
|
|
#include "../CodeCompletionStrings.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/AST/DeclCXX.h"
|
2018-01-10 22:57:58 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.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/Basic/SourceManager.h"
|
|
|
|
#include "clang/Index/IndexSymbol.h"
|
|
|
|
#include "clang/Index/USRGeneration.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// Make the Path absolute using the current working directory of the given
|
|
|
|
// SourceManager if the Path is not an absolute path.
|
|
|
|
//
|
|
|
|
// The Path can be a path relative to the build directory, or retrieved from
|
|
|
|
// the SourceManager.
|
|
|
|
std::string makeAbsolutePath(const SourceManager &SM, StringRef Path) {
|
|
|
|
llvm::SmallString<128> AbsolutePath(Path);
|
|
|
|
if (std::error_code EC =
|
|
|
|
SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
|
|
|
|
AbsolutePath))
|
|
|
|
llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
|
|
|
|
<< '\n';
|
|
|
|
// Handle the symbolic link path case where the current working directory
|
|
|
|
// (getCurrentWorkingDirectory) is a symlink./ We always want to the real
|
|
|
|
// file path (instead of the symlink path) for the C++ symbols.
|
|
|
|
//
|
|
|
|
// Consider the following example:
|
|
|
|
//
|
|
|
|
// src dir: /project/src/foo.h
|
|
|
|
// current working directory (symlink): /tmp/build -> /project/src/
|
|
|
|
//
|
|
|
|
// The file path of Symbol is "/project/src/foo.h" instead of
|
|
|
|
// "/tmp/build/foo.h"
|
|
|
|
const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
|
|
|
|
llvm::sys::path::parent_path(AbsolutePath.str()));
|
|
|
|
if (Dir) {
|
|
|
|
StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
|
2017-12-28 22:47:01 +08:00
|
|
|
SmallString<128> AbsoluteFilename;
|
[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
|
|
|
llvm::sys::path::append(AbsoluteFilename, DirName,
|
|
|
|
llvm::sys::path::filename(AbsolutePath.str()));
|
2017-12-28 22:47:01 +08:00
|
|
|
return AbsoluteFilename.str();
|
[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 AbsolutePath.str();
|
|
|
|
}
|
2017-12-19 19:37:40 +08:00
|
|
|
|
|
|
|
// "a::b::c", return {"a::b", "c"}. Scope is empty if it doesn't exist.
|
|
|
|
std::pair<llvm::StringRef, llvm::StringRef>
|
|
|
|
splitQualifiedName(llvm::StringRef QName) {
|
|
|
|
assert(!QName.startswith("::") && "Qualified names should not start with ::");
|
|
|
|
size_t Pos = QName.rfind("::");
|
|
|
|
if (Pos == llvm::StringRef::npos)
|
|
|
|
return {StringRef(), QName};
|
|
|
|
return {QName.substr(0, Pos), QName.substr(Pos + 2)};
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:57:58 +08:00
|
|
|
bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx,
|
|
|
|
const SymbolCollector::Options &Opts) {
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
if (ND->isImplicit())
|
|
|
|
return true;
|
|
|
|
// FIXME: figure out a way to handle internal linkage symbols (e.g. static
|
|
|
|
// variables, function) defined in the .cc files. Also we skip the symbols
|
|
|
|
// in anonymous namespace as the qualifier names of these symbols are like
|
|
|
|
// `foo::<anonymous>::bar`, which need a special handling.
|
|
|
|
// In real world projects, we have a relatively large set of header files
|
|
|
|
// that define static variables (like "static const int A = 1;"), we still
|
|
|
|
// want to collect these symbols, although they cause potential ODR
|
|
|
|
// violations.
|
|
|
|
if (ND->isInAnonymousNamespace())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We only want symbols in namespaces or translation unit scopes (e.g. no
|
|
|
|
// class members).
|
|
|
|
if (match(decl(allOf(
|
|
|
|
Opts.IndexMainFiles ? decl()
|
|
|
|
: decl(unless(isExpansionInMainFile())),
|
|
|
|
hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())))),
|
|
|
|
*ND, *ASTCtx)
|
|
|
|
.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
|
|
2018-01-10 22:57:58 +08:00
|
|
|
SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
|
|
|
|
|
2018-01-10 01:32:00 +08:00
|
|
|
void SymbolCollector::initialize(ASTContext &Ctx) {
|
|
|
|
ASTCtx = &Ctx;
|
|
|
|
CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
|
|
|
|
CompletionTUInfo =
|
|
|
|
llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
// Always return true to continue indexing.
|
|
|
|
bool SymbolCollector::handleDeclOccurence(
|
|
|
|
const Decl *D, index::SymbolRoleSet Roles,
|
|
|
|
ArrayRef<index::SymbolRelation> Relations, FileID FID, unsigned Offset,
|
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) {
|
2018-01-10 22:57:58 +08:00
|
|
|
assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
|
|
|
|
|
[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: collect all symbol references.
|
|
|
|
if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
|
|
|
|
Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
|
|
|
|
return true;
|
|
|
|
|
2018-01-10 01:32:00 +08:00
|
|
|
assert(CompletionAllocator && CompletionTUInfo);
|
|
|
|
|
[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
|
|
|
if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) {
|
2018-01-10 22:57:58 +08:00
|
|
|
if (shouldFilterDecl(ND, ASTCtx, Opts))
|
[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 true;
|
2017-12-28 22:47:01 +08:00
|
|
|
llvm::SmallString<128> USR;
|
|
|
|
if (index::generateUSRForDecl(ND, 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
|
|
|
return true;
|
|
|
|
|
|
|
|
auto ID = SymbolID(USR);
|
2017-12-24 03:38:03 +08:00
|
|
|
if (Symbols.find(ID) != nullptr)
|
[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 true;
|
|
|
|
|
|
|
|
auto &SM = ND->getASTContext().getSourceManager();
|
2017-12-22 01:51:35 +08:00
|
|
|
std::string FilePath =
|
|
|
|
makeAbsolutePath(SM, SM.getFilename(D->getLocation()));
|
|
|
|
SymbolLocation Location = {FilePath, SM.getFileOffset(D->getLocStart()),
|
|
|
|
SM.getFileOffset(D->getLocEnd())};
|
2017-12-19 19:37:40 +08:00
|
|
|
std::string QName = ND->getQualifiedNameAsString();
|
|
|
|
auto ScopeAndName = splitQualifiedName(QName);
|
2017-12-22 16:12:39 +08:00
|
|
|
|
|
|
|
Symbol S;
|
|
|
|
S.ID = std::move(ID);
|
|
|
|
S.Scope = ScopeAndName.first;
|
|
|
|
S.Name = ScopeAndName.second;
|
|
|
|
S.SymInfo = index::getSymbolInfo(D);
|
|
|
|
S.CanonicalDeclaration = Location;
|
2018-01-10 01:32:00 +08:00
|
|
|
|
|
|
|
// Add completion info.
|
|
|
|
assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
|
|
|
|
CodeCompletionResult SymbolCompletion(ND, 0);
|
|
|
|
const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
|
|
|
|
*ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator,
|
|
|
|
*CompletionTUInfo,
|
|
|
|
/*IncludeBriefComments*/ true);
|
|
|
|
std::string Label;
|
|
|
|
std::string SnippetInsertText;
|
|
|
|
std::string IgnoredLabel;
|
|
|
|
std::string PlainInsertText;
|
|
|
|
getLabelAndInsertText(*CCS, &Label, &SnippetInsertText,
|
|
|
|
/*EnableSnippets=*/true);
|
|
|
|
getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText,
|
|
|
|
/*EnableSnippets=*/false);
|
|
|
|
std::string FilterText = getFilterText(*CCS);
|
|
|
|
std::string Documentation = getDocumentation(*CCS);
|
|
|
|
std::string CompletionDetail = getDetail(*CCS);
|
|
|
|
|
|
|
|
S.CompletionFilterText = FilterText;
|
|
|
|
S.CompletionLabel = Label;
|
|
|
|
S.CompletionPlainInsertText = PlainInsertText;
|
|
|
|
S.CompletionSnippetInsertText = SnippetInsertText;
|
|
|
|
Symbol::Details Detail;
|
|
|
|
Detail.Documentation = Documentation;
|
|
|
|
Detail.CompletionDetail = CompletionDetail;
|
|
|
|
S.Detail = &Detail;
|
|
|
|
|
2017-12-22 16:12:39 +08:00
|
|
|
Symbols.insert(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
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|