[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++-*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
[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 "SymbolCollector.h"
|
2018-09-07 17:40:36 +08:00
|
|
|
#include "AST.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "CanonicalIncludes.h"
|
2018-09-07 17:40:36 +08:00
|
|
|
#include "CodeComplete.h"
|
|
|
|
#include "CodeCompletionStrings.h"
|
2019-02-28 21:49:25 +08:00
|
|
|
#include "ExpectedTypes.h"
|
2018-09-07 17:40:36 +08:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "SourceCode.h"
|
2019-02-28 19:02:01 +08:00
|
|
|
#include "SymbolLocation.h"
|
2018-09-07 17:40:36 +08:00
|
|
|
#include "URI.h"
|
2018-09-17 15:43:49 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclBase.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-04-13 19:03:07 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2018-10-17 16:38:36 +08:00
|
|
|
#include "clang/Basic/SourceLocation.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"
|
2018-09-17 15:43:49 +08:00
|
|
|
#include "clang/Basic/Specifiers.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/IndexSymbol.h"
|
2019-05-03 00:12:36 +08:00
|
|
|
#include "clang/Index/IndexingAction.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/USRGeneration.h"
|
[clangd] Include insertion: require header guards, drop other heuristics, treat .def like .inc.
Summary:
We do have some reports of include insertion behaving badly in some
codebases. Requiring header guards both makes sense in principle, and is
likely to disable this "nice-to-have" feature in codebases where headers don't
follow the expected pattern.
With this we can drop some other heuristics, such as looking at file
extensions to detect known non-headers - implementation files have no guards.
One wrinkle here is #import - objc headers may not have guards because
they're intended to be used via #import. If the header is the main file
or is #included, we won't collect locations - merge should take care of
this if we see the file #imported somewhere. Seems likely to be OK.
Headers which have a canonicalization (stdlib, IWYU) are exempt from this check.
*.inc files continue to be handled by looking up to the including file.
This patch also adds *.def here - tablegen wants this pattern too.
In terms of code structure, the division between SymbolCollector and
CanonicalIncludes has shifted: SymbolCollector is responsible for more.
This is because SymbolCollector has all the SourceManager/HeaderSearch access
needed for checking for guards, and we interleave these checks with the *.def
checks in a loop (potentially).
We could hand all the info into CanonicalIncludes and put the logic there
if that's preferable.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D60316
llvm-svn: 358571
2019-04-17 18:36:02 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2018-09-17 15:43:49 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2018-01-29 23:13:29 +08:00
|
|
|
#include "llvm/Support/FileSystem.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/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
2018-10-20 23:30:37 +08:00
|
|
|
|
2018-04-15 00:27:35 +08:00
|
|
|
/// If \p ND is a template specialization, returns the described template.
|
2018-04-13 19:03:07 +08:00
|
|
|
/// Otherwise, returns \p ND.
|
|
|
|
const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
|
2018-04-15 00:27:35 +08:00
|
|
|
if (auto T = ND.getDescribedTemplate())
|
|
|
|
return *T;
|
2018-04-13 19:03:07 +08:00
|
|
|
return ND;
|
|
|
|
}
|
|
|
|
|
2018-02-07 00:10:35 +08:00
|
|
|
// Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the
|
|
|
|
// current working directory of the given SourceManager if the Path is not an
|
|
|
|
// absolute path. If failed, this resolves relative paths against \p FallbackDir
|
|
|
|
// to get an absolute path. Then, this tries creating an URI for the absolute
|
|
|
|
// path with schemes specified in \p Opts. This returns an URI with the first
|
|
|
|
// working scheme, if there is any; otherwise, this returns None.
|
[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 Path can be a path relative to the build directory, or retrieved from
|
|
|
|
// the SourceManager.
|
2018-12-19 18:46:21 +08:00
|
|
|
std::string toURI(const SourceManager &SM, llvm::StringRef Path,
|
|
|
|
const SymbolCollector::Options &Opts) {
|
|
|
|
llvm::SmallString<128> AbsolutePath(Path);
|
2019-08-02 05:32:01 +08:00
|
|
|
if (auto File = SM.getFileManager().getFile(Path)) {
|
|
|
|
if (auto CanonPath = getCanonicalPath(*File, SM)) {
|
|
|
|
AbsolutePath = *CanonPath;
|
|
|
|
}
|
[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-12-19 18:46:21 +08:00
|
|
|
// We don't perform is_absolute check in an else branch because makeAbsolute
|
|
|
|
// might return a relative path on some InMemoryFileSystems.
|
2019-01-07 23:45:19 +08:00
|
|
|
if (!llvm::sys::path::is_absolute(AbsolutePath) && !Opts.FallbackDir.empty())
|
|
|
|
llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath);
|
|
|
|
llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true);
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
return URI::create(AbsolutePath).toString();
|
[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-19 19:37:40 +08:00
|
|
|
|
2018-05-16 20:12:30 +08:00
|
|
|
// All proto generated headers should start with this line.
|
|
|
|
static const char *PROTO_HEADER_COMMENT =
|
|
|
|
"// Generated by the protocol buffer compiler. DO NOT EDIT!";
|
|
|
|
|
|
|
|
// Checks whether the decl is a private symbol in a header generated by
|
|
|
|
// protobuf compiler.
|
|
|
|
// To identify whether a proto header is actually generated by proto compiler,
|
|
|
|
// we check whether it starts with PROTO_HEADER_COMMENT.
|
|
|
|
// FIXME: make filtering extensible when there are more use cases for symbol
|
|
|
|
// filters.
|
|
|
|
bool isPrivateProtoDecl(const NamedDecl &ND) {
|
|
|
|
const auto &SM = ND.getASTContext().getSourceManager();
|
2019-08-07 04:25:59 +08:00
|
|
|
auto Loc = spellingLocIfSpelled(findName(&ND), SM);
|
2018-05-16 20:12:30 +08:00
|
|
|
auto FileName = SM.getFilename(Loc);
|
|
|
|
if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
|
|
|
|
return false;
|
|
|
|
auto FID = SM.getFileID(Loc);
|
|
|
|
// Double check that this is an actual protobuf header.
|
|
|
|
if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// ND without identifier can be operators.
|
|
|
|
if (ND.getIdentifier() == nullptr)
|
|
|
|
return false;
|
|
|
|
auto Name = ND.getIdentifier()->getName();
|
|
|
|
if (!Name.contains('_'))
|
|
|
|
return false;
|
|
|
|
// Nested proto entities (e.g. Message::Nested) have top-level decls
|
|
|
|
// that shouldn't be used (Message_Nested). Ignore them completely.
|
|
|
|
// The nested entities are dangling type aliases, we may want to reconsider
|
|
|
|
// including them in the future.
|
|
|
|
// For enum constants, SOME_ENUM_CONSTANT is not private and should be
|
|
|
|
// indexed. Outer_INNER is private. This heuristic relies on naming style, it
|
|
|
|
// will include OUTER_INNER and exclude some_enum_constant.
|
|
|
|
// FIXME: the heuristic relies on naming style (i.e. no underscore in
|
|
|
|
// user-defined names) and can be improved.
|
2019-01-07 23:45:19 +08:00
|
|
|
return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower);
|
2018-05-16 20:12:30 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
// We only collect #include paths for symbols that are suitable for global code
|
|
|
|
// completion, except for namespaces since #include path for a namespace is hard
|
|
|
|
// to define.
|
|
|
|
bool shouldCollectIncludePath(index::SymbolKind Kind) {
|
|
|
|
using SK = index::SymbolKind;
|
|
|
|
switch (Kind) {
|
|
|
|
case SK::Macro:
|
|
|
|
case SK::Enum:
|
|
|
|
case SK::Struct:
|
|
|
|
case SK::Class:
|
|
|
|
case SK::Union:
|
|
|
|
case SK::TypeAlias:
|
|
|
|
case SK::Using:
|
|
|
|
case SK::Function:
|
|
|
|
case SK::Variable:
|
|
|
|
case SK::EnumConstant:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 20:54:13 +08:00
|
|
|
// Return the symbol range of the token at \p TokLoc.
|
|
|
|
std::pair<SymbolLocation::Position, SymbolLocation::Position>
|
|
|
|
getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
auto CreatePosition = [&SM](SourceLocation Loc) {
|
|
|
|
auto LSPLoc = sourceLocToPosition(SM, Loc);
|
|
|
|
SymbolLocation::Position Pos;
|
[clangd] Encode Line/Column as a 32-bits integer.
Summary:
This would buy us more memory. Using a 32-bits integer is enough for
most human-readable source code (up to 4M lines and 4K columns).
Previsouly, we used 8 bytes for a position, now 4 bytes, it would save
us 8 bytes for each Ref and each Symbol instance.
For LLVM-project binary index file, we save ~13% memory.
| Before | After |
| 412MB | 355MB |
Reviewers: sammccall
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53363
llvm-svn: 344735
2018-10-18 18:43:50 +08:00
|
|
|
Pos.setLine(LSPLoc.line);
|
|
|
|
Pos.setColumn(LSPLoc.character);
|
2018-08-31 20:54:13 +08:00
|
|
|
return Pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
|
|
|
|
return {CreatePosition(TokLoc),
|
|
|
|
CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the symbol location of the token at \p TokLoc.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<SymbolLocation>
|
|
|
|
getTokenLocation(SourceLocation TokLoc, const SourceManager &SM,
|
|
|
|
const SymbolCollector::Options &Opts,
|
|
|
|
const clang::LangOptions &LangOpts,
|
|
|
|
std::string &FileURIStorage) {
|
2018-12-19 18:46:21 +08:00
|
|
|
auto Path = SM.getFilename(TokLoc);
|
|
|
|
if (Path.empty())
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-12-19 18:46:21 +08:00
|
|
|
FileURIStorage = toURI(SM, Path, Opts);
|
2018-02-09 22:42:01 +08:00
|
|
|
SymbolLocation Result;
|
2018-11-14 19:55:45 +08:00
|
|
|
Result.FileURI = FileURIStorage.c_str();
|
2018-08-31 20:54:13 +08:00
|
|
|
auto Range = getTokenRange(TokLoc, SM, LangOpts);
|
|
|
|
Result.Start = Range.first;
|
|
|
|
Result.End = Range.second;
|
2018-04-13 16:30:39 +08:00
|
|
|
|
2018-12-19 18:46:21 +08:00
|
|
|
return Result;
|
2018-01-31 20:56:51 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Prefer the definition of a TagDecl (e.g. class) as CanonicalDeclaration.
Summary:
Currently, we pick the first declaration of a symbol in a TU, which is considered
canonical in the clangIndex, as the canonical declaration in clangd. This causes
forward declarations that might appear in a random header to be used as a
canonical declaration, which is not desirable for features like go-to-declaration
or include insertion.
For example, for class X, we would consider the forward declaration in fwd.h to
be the canonical declaration, while the preferred canonical declaration should
be the actual definition in x.h.
```
// fwd.h
class X; // forward decl
// x.h
class X {};
```
This patch fixes the issue by making symbol collector favor the actual definition of
a TagDecl (i.e. class/struct/enum/union) found in a header file over the first seen
declarations in a TU. Other symbol types like functions are not handled because
using the first seen declarations as canonical declarations is usually a good
heuristic for them.
Reviewers: sammccall
Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43823
llvm-svn: 326313
2018-02-28 17:33:15 +08:00
|
|
|
// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
|
|
|
|
// in a header file, in which case clangd would prefer to use ND as a canonical
|
|
|
|
// declaration.
|
|
|
|
// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
|
2018-03-30 04:03:16 +08:00
|
|
|
// the first seen declaration as canonical declaration is not a good enough
|
[clangd] Prefer the definition of a TagDecl (e.g. class) as CanonicalDeclaration.
Summary:
Currently, we pick the first declaration of a symbol in a TU, which is considered
canonical in the clangIndex, as the canonical declaration in clangd. This causes
forward declarations that might appear in a random header to be used as a
canonical declaration, which is not desirable for features like go-to-declaration
or include insertion.
For example, for class X, we would consider the forward declaration in fwd.h to
be the canonical declaration, while the preferred canonical declaration should
be the actual definition in x.h.
```
// fwd.h
class X; // forward decl
// x.h
class X {};
```
This patch fixes the issue by making symbol collector favor the actual definition of
a TagDecl (i.e. class/struct/enum/union) found in a header file over the first seen
declarations in a TU. Other symbol types like functions are not handled because
using the first seen declarations as canonical declarations is usually a good
heuristic for them.
Reviewers: sammccall
Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43823
llvm-svn: 326313
2018-02-28 17:33:15 +08:00
|
|
|
// heuristic.
|
|
|
|
bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
|
2019-03-08 17:54:37 +08:00
|
|
|
const auto &SM = ND.getASTContext().getSourceManager();
|
[clangd] Prefer the definition of a TagDecl (e.g. class) as CanonicalDeclaration.
Summary:
Currently, we pick the first declaration of a symbol in a TU, which is considered
canonical in the clangIndex, as the canonical declaration in clangd. This causes
forward declarations that might appear in a random header to be used as a
canonical declaration, which is not desirable for features like go-to-declaration
or include insertion.
For example, for class X, we would consider the forward declaration in fwd.h to
be the canonical declaration, while the preferred canonical declaration should
be the actual definition in x.h.
```
// fwd.h
class X; // forward decl
// x.h
class X {};
```
This patch fixes the issue by making symbol collector favor the actual definition of
a TagDecl (i.e. class/struct/enum/union) found in a header file over the first seen
declarations in a TU. Other symbol types like functions are not handled because
using the first seen declarations as canonical declarations is usually a good
heuristic for them.
Reviewers: sammccall
Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43823
llvm-svn: 326313
2018-02-28 17:33:15 +08:00
|
|
|
return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
|
2019-07-19 16:33:39 +08:00
|
|
|
isa<TagDecl>(&ND) && !isInsideMainFile(ND.getLocation(), SM);
|
[clangd] Prefer the definition of a TagDecl (e.g. class) as CanonicalDeclaration.
Summary:
Currently, we pick the first declaration of a symbol in a TU, which is considered
canonical in the clangIndex, as the canonical declaration in clangd. This causes
forward declarations that might appear in a random header to be used as a
canonical declaration, which is not desirable for features like go-to-declaration
or include insertion.
For example, for class X, we would consider the forward declaration in fwd.h to
be the canonical declaration, while the preferred canonical declaration should
be the actual definition in x.h.
```
// fwd.h
class X; // forward decl
// x.h
class X {};
```
This patch fixes the issue by making symbol collector favor the actual definition of
a TagDecl (i.e. class/struct/enum/union) found in a header file over the first seen
declarations in a TU. Other symbol types like functions are not handled because
using the first seen declarations as canonical declarations is usually a good
heuristic for them.
Reviewers: sammccall
Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43823
llvm-svn: 326313
2018-02-28 17:33:15 +08:00
|
|
|
}
|
|
|
|
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
RefKind toRefKind(index::SymbolRoleSet Roles) {
|
|
|
|
return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles);
|
2018-08-31 20:54:13 +08:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:25:44 +08:00
|
|
|
bool shouldIndexRelation(const index::SymbolRelation &R) {
|
|
|
|
// We currently only index BaseOf relations, for type hierarchy subtypes.
|
|
|
|
return R.Roles & static_cast<unsigned>(index::SymbolRole::RelationBaseOf);
|
|
|
|
}
|
|
|
|
|
[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 =
|
2019-08-15 07:52:23 +08:00
|
|
|
std::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
|
2018-01-10 01:32:00 +08:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:12:26 +08:00
|
|
|
bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
|
2018-12-03 21:16:04 +08:00
|
|
|
const ASTContext &ASTCtx,
|
2019-01-14 18:01:17 +08:00
|
|
|
const Options &Opts,
|
|
|
|
bool IsMainFileOnly) {
|
2018-06-21 20:12:26 +08:00
|
|
|
// Skip anonymous declarations, e.g (anonymous enum/class/struct).
|
|
|
|
if (ND.getDeclName().isEmpty())
|
|
|
|
return false;
|
|
|
|
|
2019-01-14 18:01:17 +08:00
|
|
|
// Skip main-file symbols if we are not collecting them.
|
|
|
|
if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Skip symbols in anonymous namespaces in header files.
|
|
|
|
if (!IsMainFileOnly && ND.isInAnonymousNamespace())
|
2018-06-21 20:12:26 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// We want most things but not "local" symbols such as symbols inside
|
|
|
|
// FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
|
|
|
|
// FIXME: Need a matcher for ExportDecl in order to include symbols declared
|
|
|
|
// within an export.
|
2018-09-17 15:43:49 +08:00
|
|
|
const auto *DeclCtx = ND.getDeclContext();
|
|
|
|
switch (DeclCtx->getDeclKind()) {
|
|
|
|
case Decl::TranslationUnit:
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Record has a few derivations (e.g. CXXRecord, Class specialization), it's
|
|
|
|
// easier to cast.
|
2018-10-20 23:30:37 +08:00
|
|
|
if (!isa<RecordDecl>(DeclCtx))
|
2018-09-17 15:43:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-21 20:12:26 +08:00
|
|
|
|
|
|
|
// Avoid indexing internal symbols in protobuf generated headers.
|
|
|
|
if (isPrivateProtoDecl(ND))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[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,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::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) {
|
2018-01-10 22:57:58 +08:00
|
|
|
assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
|
2018-03-12 22:49:09 +08:00
|
|
|
assert(CompletionAllocator && CompletionTUInfo);
|
2018-06-04 19:31:55 +08:00
|
|
|
assert(ASTNode.OrigD);
|
2019-04-15 22:38:46 +08:00
|
|
|
// Indexing API puts cannonical decl into D, which might not have a valid
|
|
|
|
// source location for implicit/built-in decls. Fallback to original decl in
|
|
|
|
// such cases.
|
|
|
|
if (D->getLocation().isInvalid())
|
|
|
|
D = ASTNode.OrigD;
|
2018-06-04 19:31:55 +08:00
|
|
|
// If OrigD is an declaration associated with a friend declaration and it's
|
|
|
|
// not a definition, skip it. Note that OrigD is the occurrence that the
|
|
|
|
// collector is currently visiting.
|
|
|
|
if ((ASTNode.OrigD->getFriendObjectKind() !=
|
|
|
|
Decl::FriendObjectKind::FOK_None) &&
|
|
|
|
!(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
|
|
|
|
return true;
|
2019-03-08 17:54:37 +08:00
|
|
|
// Skip non-semantic references, we should start processing these when we
|
|
|
|
// decide to implement renaming with index support.
|
|
|
|
if ((Roles & static_cast<unsigned>(index::SymbolRole::NameReference)))
|
|
|
|
return true;
|
2018-06-04 19:31:55 +08:00
|
|
|
// A declaration created for a friend declaration should not be used as the
|
|
|
|
// canonical declaration in the index. Use OrigD instead, unless we've already
|
|
|
|
// picked a replacement for D
|
|
|
|
if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
|
|
|
|
D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
|
2018-10-20 23:30:37 +08:00
|
|
|
const NamedDecl *ND = dyn_cast<NamedDecl>(D);
|
2018-03-12 22:49:09 +08:00
|
|
|
if (!ND)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Mark D as referenced if this is a reference coming from the main file.
|
|
|
|
// D may not be an interesting symbol, but it's cheaper to check at the end.
|
2018-04-09 22:28:52 +08:00
|
|
|
auto &SM = ASTCtx->getSourceManager();
|
2018-08-31 20:54:13 +08:00
|
|
|
auto SpellingLoc = SM.getSpellingLoc(Loc);
|
2018-03-12 22:49:09 +08:00
|
|
|
if (Opts.CountReferences &&
|
|
|
|
(Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
|
2018-08-31 20:54:13 +08:00
|
|
|
SM.getFileID(SpellingLoc) == SM.getMainFileID())
|
2018-03-12 22:49:09 +08:00
|
|
|
ReferencedDecls.insert(ND);
|
2018-01-10 22:57:58 +08:00
|
|
|
|
2019-06-04 12:25:44 +08:00
|
|
|
auto ID = getSymbolID(ND);
|
|
|
|
if (!ID)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Note: we need to process relations for all decl occurrences, including
|
|
|
|
// refs, because the indexing code only populates relations for specific
|
|
|
|
// occurrences. For example, RelationBaseOf is only populated for the
|
|
|
|
// occurrence inside the base-specifier.
|
|
|
|
processRelations(*ND, *ID, Relations);
|
|
|
|
|
2018-10-15 19:46:26 +08:00
|
|
|
bool CollectRef = static_cast<unsigned>(Opts.RefFilter) & Roles;
|
|
|
|
bool IsOnlyRef =
|
|
|
|
!(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) |
|
|
|
|
static_cast<unsigned>(index::SymbolRole::Definition)));
|
2018-08-31 20:54:13 +08:00
|
|
|
|
2018-10-15 19:46:26 +08:00
|
|
|
if (IsOnlyRef && !CollectRef)
|
[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;
|
2019-01-14 18:01:17 +08:00
|
|
|
|
[clangd] Collect the refs when the main file is header.
Summary:
Previously, we only collect refs of the symbols which are declared in
the preamble and referenced in the main file, it works well when the
main file is .cpp file.
However, when the main file is .h file (when opening a .h file in the
editor), we don't collect refs of the symbol declared in this file, so we miss
these refs in our dynamic index.
A typical scenario:
1. Open Foo.h (which contains class Foo)
2. Open Foo.cpp, call find references for Foo
And we only get refs from Foo.cpp.
Reviewers: ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D63818
llvm-svn: 364893
2019-07-02 17:16:21 +08:00
|
|
|
// ND is the canonical (i.e. first) declaration. If it's in the main file
|
|
|
|
// (which is not a header), then no public declaration was visible, so assume
|
|
|
|
// it's main-file only.
|
[clangd] Add support for type hierarchy (super types only for now)
Summary:
Patch by Nathan Ridge(@nridge)!
This is an LSP extension proposed here:
https://github.com/Microsoft/vscode-languageserver-node/pull/426
An example client implementation can be found here:
https://github.com/theia-ide/theia/pull/3802
Reviewers: kadircet, sammccall
Reviewed By: kadircet
Subscribers: jdoerfert, sammccall, cfe-commits, mgorny, dschaefer, simark, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet
Tags: #clang
Differential Revision: https://reviews.llvm.org/D56370
llvm-svn: 356445
2019-03-19 17:27:04 +08:00
|
|
|
bool IsMainFileOnly =
|
[clangd] Collect the refs when the main file is header.
Summary:
Previously, we only collect refs of the symbols which are declared in
the preamble and referenced in the main file, it works well when the
main file is .cpp file.
However, when the main file is .h file (when opening a .h file in the
editor), we don't collect refs of the symbol declared in this file, so we miss
these refs in our dynamic index.
A typical scenario:
1. Open Foo.h (which contains class Foo)
2. Open Foo.cpp, call find references for Foo
And we only get refs from Foo.cpp.
Reviewers: ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D63818
llvm-svn: 364893
2019-07-02 17:16:21 +08:00
|
|
|
SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc())) &&
|
|
|
|
!ASTCtx->getLangOpts().IsHeaderFile;
|
2019-04-11 00:26:58 +08:00
|
|
|
// In C, printf is a redecl of an implicit builtin! So check OrigD instead.
|
|
|
|
if (ASTNode.OrigD->isImplicit() ||
|
|
|
|
!shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
|
2018-03-12 22:49:09 +08:00
|
|
|
return true;
|
2019-01-14 18:01:17 +08:00
|
|
|
// Do not store references to main-file symbols.
|
|
|
|
if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) &&
|
2018-10-17 16:38:36 +08:00
|
|
|
(Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID()))
|
2018-10-15 19:46:26 +08:00
|
|
|
DeclRefs[ND].emplace_back(SpellingLoc, Roles);
|
|
|
|
// Don't continue indexing if this is a mere reference.
|
|
|
|
if (IsOnlyRef)
|
|
|
|
return true;
|
[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
|
|
|
|
2019-01-23 18:35:12 +08:00
|
|
|
// FIXME: ObjCPropertyDecl are not properly indexed here:
|
|
|
|
// - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
|
|
|
|
// not a NamedDecl.
|
|
|
|
auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
|
|
|
|
if (!OriginalDecl)
|
|
|
|
return true;
|
|
|
|
|
2018-08-07 16:57:52 +08:00
|
|
|
const Symbol *BasicSymbol = Symbols.find(*ID);
|
2018-03-12 22:49:09 +08:00
|
|
|
if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
|
2019-01-14 18:01:17 +08:00
|
|
|
BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly);
|
2019-01-23 18:35:12 +08:00
|
|
|
else if (isPreferredDeclaration(*OriginalDecl, Roles))
|
2018-03-12 22:49:09 +08:00
|
|
|
// If OriginalDecl is preferred, replace the existing canonical
|
|
|
|
// declaration (e.g. a class forward declaration). There should be at most
|
|
|
|
// one duplicate as we expect to see only one preferred declaration per
|
|
|
|
// TU, because in practice they are definitions.
|
2019-01-23 18:35:12 +08:00
|
|
|
BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly);
|
[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
|
|
|
if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
|
2019-01-23 18:35:12 +08:00
|
|
|
addDefinition(*OriginalDecl, *BasicSymbol);
|
2019-06-04 12:25:44 +08:00
|
|
|
|
2018-03-12 22:49:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
[clangd] Prefer the definition of a TagDecl (e.g. class) as CanonicalDeclaration.
Summary:
Currently, we pick the first declaration of a symbol in a TU, which is considered
canonical in the clangIndex, as the canonical declaration in clangd. This causes
forward declarations that might appear in a random header to be used as a
canonical declaration, which is not desirable for features like go-to-declaration
or include insertion.
For example, for class X, we would consider the forward declaration in fwd.h to
be the canonical declaration, while the preferred canonical declaration should
be the actual definition in x.h.
```
// fwd.h
class X; // forward decl
// x.h
class X {};
```
This patch fixes the issue by making symbol collector favor the actual definition of
a TagDecl (i.e. class/struct/enum/union) found in a header file over the first seen
declarations in a TU. Other symbol types like functions are not handled because
using the first seen declarations as canonical declarations is usually a good
heuristic for them.
Reviewers: sammccall
Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43823
llvm-svn: 326313
2018-02-28 17:33:15 +08:00
|
|
|
|
2018-07-09 23:31:07 +08:00
|
|
|
bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name,
|
|
|
|
const MacroInfo *MI,
|
|
|
|
index::SymbolRoleSet Roles,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
if (!Opts.CollectMacro)
|
|
|
|
return true;
|
|
|
|
assert(PP.get());
|
|
|
|
|
|
|
|
const auto &SM = PP->getSourceManager();
|
2018-11-06 18:55:21 +08:00
|
|
|
auto DefLoc = MI->getDefinitionLoc();
|
2019-01-28 22:11:49 +08:00
|
|
|
|
2019-05-03 21:17:29 +08:00
|
|
|
// Builtin macros don't have useful locations and aren't needed in completion.
|
|
|
|
if (MI->isBuiltinMacro())
|
2018-07-09 23:31:07 +08:00
|
|
|
return true;
|
|
|
|
|
2019-01-28 22:11:49 +08:00
|
|
|
// Skip main-file symbols if we are not collecting them.
|
|
|
|
bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc));
|
|
|
|
if (IsMainFileSymbol && !Opts.CollectMainFileSymbols)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Also avoid storing predefined macros like __DBL_MIN__.
|
|
|
|
if (SM.isWrittenInBuiltinFile(DefLoc))
|
|
|
|
return true;
|
|
|
|
|
2018-07-09 23:31:07 +08:00
|
|
|
// Mark the macro as referenced if this is a reference coming from the main
|
|
|
|
// file. The macro may not be an interesting symbol, but it's cheaper to check
|
|
|
|
// at the end.
|
|
|
|
if (Opts.CountReferences &&
|
|
|
|
(Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
|
|
|
|
SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
|
|
|
|
ReferencedMacros.insert(Name);
|
|
|
|
// Don't continue indexing if this is a mere reference.
|
|
|
|
// FIXME: remove macro with ID if it is undefined.
|
|
|
|
if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
|
|
|
|
Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
|
|
|
|
return true;
|
|
|
|
|
2018-09-06 17:59:37 +08:00
|
|
|
auto ID = getSymbolID(*Name, MI, SM);
|
|
|
|
if (!ID)
|
2018-07-09 23:31:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Only collect one instance in case there are multiple.
|
2018-09-06 17:59:37 +08:00
|
|
|
if (Symbols.find(*ID) != nullptr)
|
2018-07-09 23:31:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
Symbol S;
|
2018-09-06 17:59:37 +08:00
|
|
|
S.ID = std::move(*ID);
|
2018-07-09 23:31:07 +08:00
|
|
|
S.Name = Name->getName();
|
2019-01-28 22:11:49 +08:00
|
|
|
if (!IsMainFileSymbol) {
|
|
|
|
S.Flags |= Symbol::IndexedForCodeCompletion;
|
|
|
|
S.Flags |= Symbol::VisibleOutsideFile;
|
|
|
|
}
|
2018-07-09 23:31:07 +08:00
|
|
|
S.SymInfo = index::getSymbolInfoForMacro(*MI);
|
|
|
|
std::string FileURI;
|
2018-11-06 18:55:21 +08:00
|
|
|
// FIXME: use the result to filter out symbols.
|
2019-08-20 16:54:30 +08:00
|
|
|
shouldIndexFile(SM.getFileID(Loc));
|
2018-11-06 18:55:21 +08:00
|
|
|
if (auto DeclLoc =
|
|
|
|
getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI))
|
2018-07-09 23:31:07 +08:00
|
|
|
S.CanonicalDeclaration = *DeclLoc;
|
|
|
|
|
|
|
|
CodeCompletionResult SymbolCompletion(Name);
|
|
|
|
const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
|
|
|
|
*PP, *CompletionAllocator, *CompletionTUInfo);
|
|
|
|
std::string Signature;
|
|
|
|
std::string SnippetSuffix;
|
|
|
|
getSignature(*CCS, &Signature, &SnippetSuffix);
|
|
|
|
S.Signature = Signature;
|
|
|
|
S.CompletionSnippetSuffix = SnippetSuffix;
|
[clangd] Support multiple #include headers in one symbol.
Summary:
Currently, a symbol can have only one #include header attached, which
might not work well if the symbol can be imported via different #includes depending
on where it's used. This patch stores multiple #include headers (with # references)
for each symbol, so that CodeCompletion can decide which include to insert.
In this patch, code completion simply picks the most popular include as the default inserted header. We also return all possible includes and their edits in the `CodeCompletion` results.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: mgrang, ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51291
llvm-svn: 341304
2018-09-03 18:18:21 +08:00
|
|
|
|
2019-05-03 21:17:29 +08:00
|
|
|
IndexedMacros.insert(Name);
|
|
|
|
setIncludeLocation(S, DefLoc);
|
2018-07-09 23:31:07 +08:00
|
|
|
Symbols.insert(S);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:25:44 +08:00
|
|
|
void SymbolCollector::processRelations(
|
|
|
|
const NamedDecl &ND, const SymbolID &ID,
|
|
|
|
ArrayRef<index::SymbolRelation> Relations) {
|
|
|
|
// Store subtype relations.
|
|
|
|
if (!dyn_cast<TagDecl>(&ND))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const auto &R : Relations) {
|
|
|
|
if (!shouldIndexRelation(R))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const Decl *Object = R.RelatedSymbol;
|
|
|
|
|
|
|
|
auto ObjectID = getSymbolID(Object);
|
|
|
|
if (!ObjectID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Record the relation.
|
|
|
|
// TODO: There may be cases where the object decl is not indexed for some
|
|
|
|
// reason. Those cases should probably be removed in due course, but for
|
|
|
|
// now there are two possible ways to handle it:
|
|
|
|
// (A) Avoid storing the relation in such cases.
|
|
|
|
// (B) Store it anyways. Clients will likely lookup() the SymbolID
|
|
|
|
// in the index and find nothing, but that's a situation they
|
|
|
|
// probably need to handle for other reasons anyways.
|
|
|
|
// We currently do (B) because it's simpler.
|
|
|
|
this->Relations.insert(
|
|
|
|
Relation{ID, index::SymbolRole::RelationBaseOf, *ObjectID});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 07:54:43 +08:00
|
|
|
void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) {
|
2019-05-03 21:17:29 +08:00
|
|
|
if (Opts.CollectIncludePath)
|
|
|
|
if (shouldCollectIncludePath(S.SymInfo.Kind))
|
|
|
|
// Use the expansion location to get the #include header since this is
|
|
|
|
// where the symbol is exposed.
|
|
|
|
IncludeFiles[S.ID] =
|
|
|
|
PP->getSourceManager().getDecomposedExpansionLoc(Loc).first;
|
|
|
|
}
|
|
|
|
|
2018-03-12 22:49:09 +08:00
|
|
|
void SymbolCollector::finish() {
|
2018-07-09 23:31:07 +08:00
|
|
|
// At the end of the TU, add 1 to the refcount of all referenced symbols.
|
|
|
|
auto IncRef = [this](const SymbolID &ID) {
|
|
|
|
if (const auto *S = Symbols.find(ID)) {
|
|
|
|
Symbol Inc = *S;
|
|
|
|
++Inc.References;
|
|
|
|
Symbols.insert(Inc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (const NamedDecl *ND : ReferencedDecls) {
|
2018-08-07 16:57:52 +08:00
|
|
|
if (auto ID = getSymbolID(ND)) {
|
|
|
|
IncRef(*ID);
|
|
|
|
}
|
2018-07-09 23:31:07 +08:00
|
|
|
}
|
|
|
|
if (Opts.CollectMacro) {
|
|
|
|
assert(PP);
|
2019-05-03 21:17:29 +08:00
|
|
|
// First, drop header guards. We can't identify these until EOF.
|
|
|
|
for (const IdentifierInfo *II : IndexedMacros) {
|
|
|
|
if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
|
|
|
|
if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
|
|
|
|
if (MI->isUsedForHeaderGuard())
|
|
|
|
Symbols.erase(*ID);
|
|
|
|
}
|
|
|
|
// Now increment refcounts.
|
2018-07-09 23:31:07 +08:00
|
|
|
for (const IdentifierInfo *II : ReferencedMacros) {
|
2018-07-10 02:54:51 +08:00
|
|
|
if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
|
2018-09-06 17:59:37 +08:00
|
|
|
if (auto ID = getSymbolID(*II, MI, PP->getSourceManager()))
|
|
|
|
IncRef(*ID);
|
2018-07-09 23:31:07 +08:00
|
|
|
}
|
2018-02-09 22:42:01 +08:00
|
|
|
}
|
2018-08-31 20:54:13 +08:00
|
|
|
|
2019-05-03 21:17:29 +08:00
|
|
|
// Fill in IncludeHeaders.
|
|
|
|
// We delay this until end of TU so header guards are all resolved.
|
|
|
|
// Symbols in slabs aren' mutable, so insert() has to walk all the strings :-(
|
|
|
|
llvm::SmallString<256> QName;
|
|
|
|
for (const auto &Entry : IncludeFiles)
|
|
|
|
if (const Symbol *S = Symbols.find(Entry.first)) {
|
|
|
|
QName = S->Scope;
|
|
|
|
QName.append(S->Name);
|
|
|
|
if (auto Header = getIncludeHeader(QName, Entry.second)) {
|
|
|
|
Symbol NewSym = *S;
|
|
|
|
NewSym.IncludeHeaders.push_back({*Header, 1});
|
|
|
|
Symbols.insert(NewSym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 20:54:13 +08:00
|
|
|
const auto &SM = ASTCtx->getSourceManager();
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::DenseMap<FileID, std::string> URICache;
|
|
|
|
auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> {
|
2018-10-17 16:38:36 +08:00
|
|
|
auto Found = URICache.find(FID);
|
|
|
|
if (Found == URICache.end()) {
|
|
|
|
if (auto *FileEntry = SM.getFileEntryForID(FID)) {
|
|
|
|
auto FileURI = toURI(SM, FileEntry->getName(), Opts);
|
2018-12-19 18:46:21 +08:00
|
|
|
Found = URICache.insert({FID, FileURI}).first;
|
2018-10-17 16:54:48 +08:00
|
|
|
} else {
|
|
|
|
// Ignore cases where we can not find a corresponding file entry
|
|
|
|
// for the loc, thoses are not interesting, e.g. symbols formed
|
|
|
|
// via macro concatenation.
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-10-17 16:38:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Found->second;
|
|
|
|
};
|
2019-05-03 21:17:29 +08:00
|
|
|
// Populate Refs slab from DeclRefs.
|
2018-10-17 16:38:36 +08:00
|
|
|
if (auto MainFileURI = GetURI(SM.getMainFileID())) {
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
for (const auto &It : DeclRefs) {
|
2018-08-31 20:54:13 +08:00
|
|
|
if (auto ID = getSymbolID(It.first)) {
|
2018-10-15 19:46:26 +08:00
|
|
|
for (const auto &LocAndRole : It.second) {
|
2018-10-17 16:38:36 +08:00
|
|
|
auto FileID = SM.getFileID(LocAndRole.first);
|
2018-11-06 18:55:21 +08:00
|
|
|
// FIXME: use the result to filter out references.
|
2019-08-20 16:54:30 +08:00
|
|
|
shouldIndexFile(FileID);
|
2018-10-17 16:38:36 +08:00
|
|
|
if (auto FileURI = GetURI(FileID)) {
|
|
|
|
auto Range =
|
|
|
|
getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts());
|
|
|
|
Ref R;
|
|
|
|
R.Location.Start = Range.first;
|
|
|
|
R.Location.End = Range.second;
|
2018-11-14 19:55:45 +08:00
|
|
|
R.Location.FileURI = FileURI->c_str();
|
2018-10-17 16:38:36 +08:00
|
|
|
R.Kind = toRefKind(LocAndRole.second);
|
|
|
|
Refs.insert(*ID, R);
|
|
|
|
}
|
2018-08-31 20:54:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 22:49:09 +08:00
|
|
|
ReferencedDecls.clear();
|
2018-07-09 23:31:07 +08:00
|
|
|
ReferencedMacros.clear();
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
DeclRefs.clear();
|
2018-11-06 18:55:21 +08:00
|
|
|
FilesToIndexCache.clear();
|
2019-04-18 02:33:07 +08:00
|
|
|
HeaderIsSelfContainedCache.clear();
|
2019-05-03 21:17:29 +08:00
|
|
|
IncludeFiles.clear();
|
2018-02-09 22:42:01 +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
|
|
|
|
[clangd] Add support for type hierarchy (super types only for now)
Summary:
Patch by Nathan Ridge(@nridge)!
This is an LSP extension proposed here:
https://github.com/Microsoft/vscode-languageserver-node/pull/426
An example client implementation can be found here:
https://github.com/theia-ide/theia/pull/3802
Reviewers: kadircet, sammccall
Reviewed By: kadircet
Subscribers: jdoerfert, sammccall, cfe-commits, mgorny, dschaefer, simark, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet
Tags: #clang
Differential Revision: https://reviews.llvm.org/D56370
llvm-svn: 356445
2019-03-19 17:27:04 +08:00
|
|
|
const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
|
2019-01-14 18:01:17 +08:00
|
|
|
bool IsMainFileOnly) {
|
2018-05-16 20:32:44 +08:00
|
|
|
auto &Ctx = ND.getASTContext();
|
|
|
|
auto &SM = Ctx.getSourceManager();
|
2018-02-02 18:31:42 +08:00
|
|
|
|
2018-02-09 22:42:01 +08:00
|
|
|
Symbol S;
|
|
|
|
S.ID = std::move(ID);
|
2018-06-22 18:46:59 +08:00
|
|
|
std::string QName = printQualifiedName(ND);
|
2018-06-22 14:41:43 +08:00
|
|
|
// FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
|
|
|
|
// for consistency with CodeCompletionString and a clean name/signature split.
|
2019-04-12 18:09:24 +08:00
|
|
|
std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
|
|
|
|
std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
|
|
|
|
S.TemplateSpecializationArgs = TemplateSpecializationArgs;
|
[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
|
|
|
|
2019-01-14 18:01:17 +08:00
|
|
|
// We collect main-file symbols, but do not use them for code completion.
|
|
|
|
if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
|
2018-09-07 02:52:26 +08:00
|
|
|
S.Flags |= Symbol::IndexedForCodeCompletion;
|
2018-10-18 20:23:05 +08:00
|
|
|
if (isImplementationDetail(&ND))
|
|
|
|
S.Flags |= Symbol::ImplementationDetail;
|
2019-01-14 18:01:17 +08:00
|
|
|
if (!IsMainFileOnly)
|
|
|
|
S.Flags |= Symbol::VisibleOutsideFile;
|
2018-02-09 22:42:01 +08:00
|
|
|
S.SymInfo = index::getSymbolInfo(&ND);
|
|
|
|
std::string FileURI;
|
2019-08-07 04:25:59 +08:00
|
|
|
auto Loc = spellingLocIfSpelled(findName(&ND), SM);
|
2019-04-15 22:38:46 +08:00
|
|
|
assert(Loc.isValid() && "Invalid source location for NamedDecl");
|
2018-11-06 18:55:21 +08:00
|
|
|
// FIXME: use the result to filter out symbols.
|
2019-08-20 16:54:30 +08:00
|
|
|
shouldIndexFile(SM.getFileID(Loc));
|
2018-11-06 18:55:21 +08:00
|
|
|
if (auto DeclLoc =
|
|
|
|
getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
|
2018-02-09 22:42:01 +08:00
|
|
|
S.CanonicalDeclaration = *DeclLoc;
|
2018-01-10 01:32:00 +08:00
|
|
|
|
2019-01-10 17:22:40 +08:00
|
|
|
S.Origin = Opts.Origin;
|
|
|
|
if (ND.getAvailability() == AR_Deprecated)
|
|
|
|
S.Flags |= Symbol::Deprecated;
|
|
|
|
|
2018-02-09 22:42:01 +08:00
|
|
|
// Add completion info.
|
|
|
|
// FIXME: we may want to choose a different redecl, or combine from several.
|
|
|
|
assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set.");
|
2018-04-13 19:03:07 +08:00
|
|
|
// We use the primary template, as clang does during code completion.
|
|
|
|
CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
|
2018-02-09 22:42:01 +08:00
|
|
|
const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
|
2018-10-24 23:24:29 +08:00
|
|
|
*ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
|
2018-02-09 22:42:01 +08:00
|
|
|
*CompletionTUInfo,
|
2018-05-16 20:32:44 +08:00
|
|
|
/*IncludeBriefComments*/ false);
|
|
|
|
std::string Documentation =
|
2018-05-24 22:49:23 +08:00
|
|
|
formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
|
|
|
|
/*CommentsFromHeaders=*/true));
|
2019-01-10 17:22:40 +08:00
|
|
|
if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
|
2019-02-26 00:00:00 +08:00
|
|
|
if (Opts.StoreAllDocumentation)
|
|
|
|
S.Documentation = Documentation;
|
2019-01-10 17:22:40 +08:00
|
|
|
Symbols.insert(S);
|
|
|
|
return Symbols.find(S.ID);
|
|
|
|
}
|
2019-02-26 00:00:00 +08:00
|
|
|
S.Documentation = Documentation;
|
2019-01-10 17:22:40 +08:00
|
|
|
std::string Signature;
|
|
|
|
std::string SnippetSuffix;
|
|
|
|
getSignature(*CCS, &Signature, &SnippetSuffix);
|
|
|
|
S.Signature = Signature;
|
|
|
|
S.CompletionSnippetSuffix = SnippetSuffix;
|
2018-06-23 00:11:35 +08:00
|
|
|
std::string ReturnType = getReturnType(*CCS);
|
2019-01-10 17:22:40 +08:00
|
|
|
S.ReturnType = ReturnType;
|
2018-01-10 01:32:00 +08:00
|
|
|
|
2018-11-26 23:52:16 +08:00
|
|
|
llvm::Optional<OpaqueType> TypeStorage;
|
2018-11-26 23:29:14 +08:00
|
|
|
if (S.Flags & Symbol::IndexedForCodeCompletion) {
|
2018-11-26 23:52:16 +08:00
|
|
|
TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
|
|
|
|
if (TypeStorage)
|
|
|
|
S.Type = TypeStorage->raw();
|
2018-11-26 23:29:14 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 22:42:01 +08:00
|
|
|
Symbols.insert(S);
|
2019-05-03 21:17:29 +08:00
|
|
|
setIncludeLocation(S, ND.getLocation());
|
2018-02-09 22:42:01 +08:00
|
|
|
return Symbols.find(S.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
|
|
|
|
2018-02-09 22:42:01 +08:00
|
|
|
void SymbolCollector::addDefinition(const NamedDecl &ND,
|
|
|
|
const Symbol &DeclSym) {
|
|
|
|
if (DeclSym.Definition)
|
|
|
|
return;
|
|
|
|
// If we saw some forward declaration, we end up copying the symbol.
|
|
|
|
// This is not ideal, but avoids duplicating the "is this a definition" check
|
|
|
|
// in clang::index. We should only see one definition.
|
|
|
|
Symbol S = DeclSym;
|
|
|
|
std::string FileURI;
|
2018-11-06 18:55:21 +08:00
|
|
|
const auto &SM = ND.getASTContext().getSourceManager();
|
2019-08-07 04:25:59 +08:00
|
|
|
auto Loc = spellingLocIfSpelled(findName(&ND), SM);
|
2018-11-06 18:55:21 +08:00
|
|
|
// FIXME: use the result to filter out symbols.
|
2019-08-20 16:54:30 +08:00
|
|
|
shouldIndexFile(SM.getFileID(Loc));
|
2018-11-06 18:55:21 +08:00
|
|
|
if (auto DefLoc =
|
|
|
|
getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI))
|
2018-02-09 22:42:01 +08:00
|
|
|
S.Definition = *DefLoc;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-18 02:33:07 +08:00
|
|
|
/// Gets a canonical include (URI of the header or <header> or "header") for
|
|
|
|
/// header of \p FID (which should usually be the *expansion* file).
|
|
|
|
/// Returns None if includes should not be inserted for this file.
|
|
|
|
llvm::Optional<std::string>
|
|
|
|
SymbolCollector::getIncludeHeader(llvm::StringRef QName, FileID FID) {
|
|
|
|
const SourceManager &SM = ASTCtx->getSourceManager();
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
|
|
|
if (!FE || FE->getName().empty())
|
|
|
|
return llvm::None;
|
|
|
|
llvm::StringRef Filename = FE->getName();
|
|
|
|
// If a file is mapped by canonical headers, use that mapping, regardless
|
|
|
|
// of whether it's an otherwise-good header (header guards etc).
|
|
|
|
if (Opts.Includes) {
|
|
|
|
llvm::StringRef Canonical = Opts.Includes->mapHeader(Filename, QName);
|
|
|
|
// If we had a mapping, always use it.
|
|
|
|
if (Canonical.startswith("<") || Canonical.startswith("\""))
|
|
|
|
return Canonical.str();
|
|
|
|
if (Canonical != Filename)
|
|
|
|
return toURI(SM, Canonical, Opts);
|
|
|
|
}
|
|
|
|
if (!isSelfContainedHeader(FID)) {
|
|
|
|
// A .inc or .def file is often included into a real header to define
|
|
|
|
// symbols (e.g. LLVM tablegen files).
|
|
|
|
if (Filename.endswith(".inc") || Filename.endswith(".def"))
|
|
|
|
return getIncludeHeader(QName, SM.getFileID(SM.getIncludeLoc(FID)));
|
|
|
|
// Conservatively refuse to insert #includes to files without guards.
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
// Standard case: just insert the file itself.
|
|
|
|
return toURI(SM, Filename, Opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolCollector::isSelfContainedHeader(FileID FID) {
|
|
|
|
// The real computation (which will be memoized).
|
|
|
|
auto Compute = [&] {
|
|
|
|
const SourceManager &SM = ASTCtx->getSourceManager();
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
|
|
|
if (!FE)
|
|
|
|
return false;
|
|
|
|
if (!PP->getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
|
|
|
|
return false;
|
|
|
|
// This pattern indicates that a header can't be used without
|
|
|
|
// particular preprocessor state, usually set up by another header.
|
2019-04-26 01:47:07 +08:00
|
|
|
if (isDontIncludeMeHeader(SM.getBufferData(FID)))
|
2019-04-18 02:33:07 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto R = HeaderIsSelfContainedCache.try_emplace(FID, false);
|
|
|
|
if (R.second)
|
|
|
|
R.first->second = Compute();
|
|
|
|
return R.first->second;
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:47:07 +08:00
|
|
|
// Is Line an #if or #ifdef directive?
|
|
|
|
static bool isIf(llvm::StringRef Line) {
|
|
|
|
Line = Line.ltrim();
|
|
|
|
if (!Line.consume_front("#"))
|
|
|
|
return false;
|
|
|
|
Line = Line.ltrim();
|
|
|
|
return Line.startswith("if");
|
|
|
|
}
|
|
|
|
// Is Line an #error directive mentioning includes?
|
|
|
|
static bool isErrorAboutInclude(llvm::StringRef Line) {
|
|
|
|
Line = Line.ltrim();
|
|
|
|
if (!Line.consume_front("#"))
|
|
|
|
return false;
|
|
|
|
Line = Line.ltrim();
|
2019-05-31 07:54:43 +08:00
|
|
|
if (!Line.startswith("error"))
|
2019-04-26 01:47:07 +08:00
|
|
|
return false;
|
|
|
|
return Line.contains_lower("includ"); // Matches "include" or "including".
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolCollector::isDontIncludeMeHeader(llvm::StringRef Content) {
|
|
|
|
llvm::StringRef Line;
|
|
|
|
// Only sniff up to 100 lines or 10KB.
|
2019-05-31 07:54:43 +08:00
|
|
|
Content = Content.take_front(100 * 100);
|
2019-04-26 01:47:07 +08:00
|
|
|
for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
|
|
|
|
std::tie(Line, Content) = Content.split('\n');
|
|
|
|
if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:54:30 +08:00
|
|
|
bool SymbolCollector::shouldIndexFile(FileID FID) {
|
|
|
|
if (!Opts.FileFilter)
|
|
|
|
return true;
|
|
|
|
auto I = FilesToIndexCache.try_emplace(FID);
|
|
|
|
if (I.second)
|
|
|
|
I.first->second = Opts.FileFilter(ASTCtx->getSourceManager(), FID);
|
|
|
|
return I.first->second;
|
|
|
|
}
|
|
|
|
|
[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
|