2018-08-15 00:03:32 +08:00
|
|
|
//===--- XRefs.cpp -----------------------------------------------*- C++-*-===//
|
2017-12-20 01:06:07 +08:00
|
|
|
//
|
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
|
2017-12-20 01:06:07 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-20 01:06:07 +08:00
|
|
|
#include "XRefs.h"
|
2018-03-09 22:00:34 +08:00
|
|
|
#include "AST.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "CodeCompletionStrings.h"
|
[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
|
|
|
#include "FindSymbols.h"
|
2019-09-26 15:27:43 +08:00
|
|
|
#include "FindTarget.h"
|
2018-01-29 23:37:46 +08:00
|
|
|
#include "Logger.h"
|
2019-09-04 17:46:06 +08:00
|
|
|
#include "ParsedAST.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "Protocol.h"
|
2019-10-18 06:48:39 +08:00
|
|
|
#include "Selection.h"
|
2018-02-21 10:39:08 +08:00
|
|
|
#include "SourceCode.h"
|
2018-01-29 23:37:46 +08:00
|
|
|
#include "URI.h"
|
2019-07-10 01:59:50 +08:00
|
|
|
#include "index/Index.h"
|
2019-02-11 23:05:29 +08:00
|
|
|
#include "index/Merge.h"
|
2019-10-17 22:08:28 +08:00
|
|
|
#include "index/Relation.h"
|
2019-02-28 19:02:01 +08:00
|
|
|
#include "index/SymbolLocation.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2020-01-25 01:21:47 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2020-01-30 19:45:43 +08:00
|
|
|
#include "clang/AST/Attrs.inc"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2019-06-13 16:51:44 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
[clangd] Implement textDocument/hover
Summary: Implemention of textDocument/hover as described in LSP definition.
This patch adds a basic Hover implementation. When hovering a variable,
function, method or namespace, clangd will return a text containing the
declaration's scope, as well as the declaration of the hovered entity.
For example, for a variable:
Declared in class Foo::Bar
int hello = 2
For macros, the macro definition is returned.
This patch doesn't include:
- markdown support (the client I use doesn't support it yet)
- range support (optional in the Hover response)
- comments associated to variables/functions/classes
They are kept as future work to keep this patch simpler.
I added tests in XRefsTests.cpp. hover.test contains one simple
smoketest to make sure the feature works from a black box perspective.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: sammccall, mgrang, klimek, rwols, ilya-biryukov, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D35894
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325395
2018-02-17 05:38:15 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2019-06-13 16:51:44 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
[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
|
|
|
#include "clang/AST/Type.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2020-03-01 23:05:12 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2017-12-20 01:06:07 +08:00
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
2019-03-08 17:54:37 +08:00
|
|
|
#include "clang/Index/IndexSymbol.h"
|
2017-12-20 01:06:07 +08:00
|
|
|
#include "clang/Index/IndexingAction.h"
|
2019-09-07 04:08:32 +08:00
|
|
|
#include "clang/Index/IndexingOptions.h"
|
2018-04-30 23:24:17 +08:00
|
|
|
#include "clang/Index/USRGeneration.h"
|
2020-02-20 02:11:01 +08:00
|
|
|
#include "clang/Tooling/Syntax/Tokens.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2020-02-26 20:39:46 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2018-02-14 01:47:16 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-09-05 19:53:07 +08:00
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
// Returns the single definition of the entity declared by D, if visible.
|
|
|
|
// In particular:
|
|
|
|
// - for non-redeclarable kinds (e.g. local vars), return D
|
|
|
|
// - for kinds that allow multiple definitions (e.g. namespaces), return nullptr
|
|
|
|
// Kinds of nodes that always return nullptr here will not have definitions
|
|
|
|
// reported by locateSymbolAt().
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
const NamedDecl *getDefinition(const NamedDecl *D) {
|
2018-01-12 22:21:10 +08:00
|
|
|
assert(D);
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
// Decl has one definition that we can find.
|
2018-01-12 22:21:10 +08:00
|
|
|
if (const auto *TD = dyn_cast<TagDecl>(D))
|
|
|
|
return TD->getDefinition();
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(D))
|
2018-01-12 22:21:10 +08:00
|
|
|
return VD->getDefinition();
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D))
|
2018-01-12 22:21:10 +08:00
|
|
|
return FD->getDefinition();
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
// Only a single declaration is allowed.
|
2019-02-21 17:55:00 +08:00
|
|
|
if (isa<ValueDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
|
|
|
|
isa<TemplateTemplateParmDecl>(D)) // except cases above
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
return D;
|
|
|
|
// Multiple definitions are allowed.
|
|
|
|
return nullptr; // except cases above
|
2018-01-12 22:21:10 +08:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:35:24 +08:00
|
|
|
void logIfOverflow(const SymbolLocation &Loc) {
|
|
|
|
if (Loc.Start.hasOverflow() || Loc.End.hasOverflow())
|
|
|
|
log("Possible overflow in symbol location: {0}", Loc);
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
// Convert a SymbolLocation to LSP's Location.
|
2018-11-28 18:30:42 +08:00
|
|
|
// TUPath is used to resolve the path of URI.
|
2018-04-30 23:24:17 +08:00
|
|
|
// FIXME: figure out a good home for it, and share the implementation with
|
|
|
|
// FindSymbols.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<Location> toLSPLocation(const SymbolLocation &Loc,
|
|
|
|
llvm::StringRef TUPath) {
|
2018-04-30 23:24:17 +08:00
|
|
|
if (!Loc)
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-04-30 23:24:17 +08:00
|
|
|
auto Uri = URI::parse(Loc.FileURI);
|
|
|
|
if (!Uri) {
|
2018-11-28 18:30:42 +08:00
|
|
|
elog("Could not parse URI {0}: {1}", Loc.FileURI, Uri.takeError());
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
2018-11-28 18:30:42 +08:00
|
|
|
auto U = URIForFile::fromURI(*Uri, TUPath);
|
|
|
|
if (!U) {
|
|
|
|
elog("Could not resolve URI {0}: {1}", Loc.FileURI, U.takeError());
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
2018-11-28 18:30:42 +08:00
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
Location LSPLoc;
|
2018-11-28 18:30:42 +08:00
|
|
|
LSPLoc.uri = std::move(*U);
|
[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
|
|
|
LSPLoc.range.start.line = Loc.Start.line();
|
|
|
|
LSPLoc.range.start.character = Loc.Start.column();
|
|
|
|
LSPLoc.range.end.line = Loc.End.line();
|
|
|
|
LSPLoc.range.end.character = Loc.End.column();
|
2018-10-19 16:35:24 +08:00
|
|
|
logIfOverflow(Loc);
|
2018-04-30 23:24:17 +08:00
|
|
|
return LSPLoc;
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:05:29 +08:00
|
|
|
SymbolLocation toIndexLocation(const Location &Loc, std::string &URIStorage) {
|
|
|
|
SymbolLocation SymLoc;
|
|
|
|
URIStorage = Loc.uri.uri();
|
|
|
|
SymLoc.FileURI = URIStorage.c_str();
|
|
|
|
SymLoc.Start.setLine(Loc.range.start.line);
|
|
|
|
SymLoc.Start.setColumn(Loc.range.start.character);
|
|
|
|
SymLoc.End.setLine(Loc.range.end.line);
|
|
|
|
SymLoc.End.setColumn(Loc.range.end.character);
|
|
|
|
return SymLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the preferred location between an AST location and an index location.
|
|
|
|
SymbolLocation getPreferredLocation(const Location &ASTLoc,
|
2019-02-12 18:38:45 +08:00
|
|
|
const SymbolLocation &IdxLoc,
|
|
|
|
std::string &Scratch) {
|
2019-02-11 23:05:29 +08:00
|
|
|
// Also use a dummy symbol for the index location so that other fields (e.g.
|
2019-12-16 17:33:56 +08:00
|
|
|
// definition) are not factored into the preference.
|
2019-02-11 23:05:29 +08:00
|
|
|
Symbol ASTSym, IdxSym;
|
|
|
|
ASTSym.ID = IdxSym.ID = SymbolID("dummy_id");
|
2019-02-12 18:38:45 +08:00
|
|
|
ASTSym.CanonicalDeclaration = toIndexLocation(ASTLoc, Scratch);
|
2019-02-11 23:05:29 +08:00
|
|
|
IdxSym.CanonicalDeclaration = IdxLoc;
|
|
|
|
auto Merged = mergeSymbol(ASTSym, IdxSym);
|
|
|
|
return Merged.CanonicalDeclaration;
|
|
|
|
}
|
|
|
|
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
std::vector<const NamedDecl *> getDeclAtPosition(ParsedAST &AST,
|
|
|
|
SourceLocation Pos,
|
|
|
|
DeclRelationSet Relations) {
|
2020-02-24 03:03:00 +08:00
|
|
|
unsigned Offset = AST.getSourceManager().getDecomposedSpellingLoc(Pos).second;
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
std::vector<const NamedDecl *> Result;
|
2020-02-24 03:03:00 +08:00
|
|
|
SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Offset,
|
|
|
|
Offset, [&](SelectionTree ST) {
|
|
|
|
if (const SelectionTree::Node *N =
|
|
|
|
ST.commonAncestor())
|
|
|
|
llvm::copy(targetDecl(N->ASTNode, Relations),
|
|
|
|
std::back_inserter(Result));
|
|
|
|
return !Result.empty();
|
|
|
|
});
|
2019-10-18 06:48:39 +08:00
|
|
|
return Result;
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
|
|
|
|
2020-03-01 23:05:12 +08:00
|
|
|
// Expects Loc to be a SpellingLocation, will bail out otherwise as it can't
|
|
|
|
// figure out a filename.
|
|
|
|
llvm::Optional<Location> makeLocation(const ASTContext &AST, SourceLocation Loc,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef TUPath) {
|
2020-03-01 23:05:12 +08:00
|
|
|
const auto &SM = AST.getSourceManager();
|
|
|
|
const FileEntry *F = SM.getFileEntryForID(SM.getFileID(Loc));
|
2017-12-20 01:06:07 +08:00
|
|
|
if (!F)
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2020-03-01 23:05:12 +08:00
|
|
|
auto FilePath = getCanonicalPath(F, SM);
|
2018-04-30 23:24:17 +08:00
|
|
|
if (!FilePath) {
|
|
|
|
log("failed to get path!");
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-02-14 01:47:16 +08:00
|
|
|
}
|
2020-03-01 23:05:12 +08:00
|
|
|
Location L;
|
|
|
|
L.uri = URIForFile::canonicalize(*FilePath, TUPath);
|
|
|
|
// We call MeasureTokenLength here as TokenBuffer doesn't store spelled tokens
|
|
|
|
// outside the main file.
|
|
|
|
auto TokLen = Lexer::MeasureTokenLength(Loc, SM, AST.getLangOpts());
|
|
|
|
L.range = halfOpenToRange(
|
|
|
|
SM, CharSourceRange::getCharRange(Loc, Loc.getLocWithOffset(TokLen)));
|
|
|
|
return L;
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-03-03 01:45:05 +08:00
|
|
|
// Treat #included files as symbols, to enable go-to-definition on them.
|
|
|
|
static llvm::Optional<LocatedSymbol>
|
|
|
|
locateFileReferent(const Position &Pos, ParsedAST &AST,
|
|
|
|
llvm::StringRef MainFilePath) {
|
2018-07-03 16:09:29 +08:00
|
|
|
for (auto &Inc : AST.getIncludeStructure().MainFileIncludes) {
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
if (!Inc.Resolved.empty() && Inc.R.start.line == Pos.line) {
|
|
|
|
LocatedSymbol File;
|
2020-01-29 03:23:46 +08:00
|
|
|
File.Name = std::string(llvm::sys::path::filename(Inc.Resolved));
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
File.PreferredDeclaration = {
|
2020-03-03 01:45:05 +08:00
|
|
|
URIForFile::canonicalize(Inc.Resolved, MainFilePath), Range{}};
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
File.Definition = File.PreferredDeclaration;
|
|
|
|
// We're not going to find any further symbols on #include lines.
|
2020-03-03 01:45:05 +08:00
|
|
|
return File;
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
}
|
2018-03-09 00:28:12 +08:00
|
|
|
}
|
2020-03-03 01:45:05 +08:00
|
|
|
return llvm::None;
|
|
|
|
}
|
2018-03-09 00:28:12 +08:00
|
|
|
|
2020-03-03 01:45:05 +08:00
|
|
|
// Macros are simple: there's no declaration/definition distinction.
|
|
|
|
// As a consequence, there's no need to look them up in the index either.
|
|
|
|
static llvm::Optional<LocatedSymbol>
|
|
|
|
locateMacroReferent(const syntax::Token &TouchedIdentifier, ParsedAST &AST,
|
|
|
|
llvm::StringRef MainFilePath) {
|
|
|
|
if (auto M = locateMacroAt(TouchedIdentifier, AST.getPreprocessor())) {
|
|
|
|
if (auto Loc = makeLocation(AST.getASTContext(),
|
|
|
|
M->Info->getDefinitionLoc(), MainFilePath)) {
|
|
|
|
LocatedSymbol Macro;
|
|
|
|
Macro.Name = std::string(M->Name);
|
|
|
|
Macro.PreferredDeclaration = *Loc;
|
|
|
|
Macro.Definition = Loc;
|
|
|
|
return Macro;
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
}
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
2020-03-03 01:45:05 +08:00
|
|
|
return llvm::None;
|
|
|
|
}
|
2017-12-20 01:06:07 +08:00
|
|
|
|
2020-03-03 01:45:05 +08:00
|
|
|
// Decls are more complicated.
|
|
|
|
// The AST contains at least a declaration, maybe a definition.
|
|
|
|
// These are up-to-date, and so generally preferred over index results.
|
|
|
|
// We perform a single batch index lookup to find additional definitions.
|
|
|
|
static std::vector<LocatedSymbol>
|
|
|
|
locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
|
|
|
|
ParsedAST &AST, llvm::StringRef MainFilePath,
|
|
|
|
const SymbolIndex *Index) {
|
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
// Results follow the order of Symbols.Decls.
|
2020-03-03 01:45:05 +08:00
|
|
|
std::vector<LocatedSymbol> Result;
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
// Keep track of SymbolID -> index mapping, to fill in index data later.
|
|
|
|
llvm::DenseMap<SymbolID, size_t> ResultIndex;
|
2018-04-30 23:24:17 +08:00
|
|
|
|
2020-01-25 01:21:47 +08:00
|
|
|
auto AddResultDecl = [&](const NamedDecl *D) {
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
const NamedDecl *Def = getDefinition(D);
|
|
|
|
const NamedDecl *Preferred = Def ? Def : D;
|
2019-12-06 05:29:59 +08:00
|
|
|
|
2019-12-10 17:08:39 +08:00
|
|
|
auto Loc = makeLocation(AST.getASTContext(), nameLocation(*Preferred, SM),
|
2020-03-03 01:45:05 +08:00
|
|
|
MainFilePath);
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
if (!Loc)
|
2020-01-25 01:21:47 +08:00
|
|
|
return;
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
|
|
|
|
Result.emplace_back();
|
2020-01-25 01:21:47 +08:00
|
|
|
Result.back().Name = printName(AST.getASTContext(), *Preferred);
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
Result.back().PreferredDeclaration = *Loc;
|
2019-10-18 06:48:39 +08:00
|
|
|
// Preferred is always a definition if possible, so this check works.
|
|
|
|
if (Def == Preferred)
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
Result.back().Definition = *Loc;
|
|
|
|
|
|
|
|
// Record SymbolID for index lookup later.
|
2019-10-18 06:48:39 +08:00
|
|
|
if (auto ID = getSymbolID(Preferred))
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
ResultIndex[*ID] = Result.size() - 1;
|
2020-01-25 01:21:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Emit all symbol locations (declaration or definition) from AST.
|
|
|
|
DeclRelationSet Relations =
|
|
|
|
DeclRelation::TemplatePattern | DeclRelation::Alias;
|
2020-03-03 01:45:05 +08:00
|
|
|
for (const NamedDecl *D : getDeclAtPosition(AST, CurLoc, Relations)) {
|
2020-01-25 01:21:47 +08:00
|
|
|
// Special case: void foo() ^override: jump to the overridden method.
|
|
|
|
if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
|
2020-02-26 20:39:46 +08:00
|
|
|
const InheritableAttr *Attr = D->getAttr<OverrideAttr>();
|
2020-01-30 19:45:43 +08:00
|
|
|
if (!Attr)
|
|
|
|
Attr = D->getAttr<FinalAttr>();
|
2020-02-26 20:39:46 +08:00
|
|
|
if (Attr && TouchedIdentifier &&
|
|
|
|
SM.getSpellingLoc(Attr->getLocation()) ==
|
|
|
|
TouchedIdentifier->location()) {
|
2020-01-25 01:21:47 +08:00
|
|
|
// We may be overridding multiple methods - offer them all.
|
|
|
|
for (const NamedDecl *ND : CMD->overridden_methods())
|
|
|
|
AddResultDecl(ND);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case: the point of declaration of a template specialization,
|
|
|
|
// it's more useful to navigate to the template declaration.
|
2020-02-26 20:39:46 +08:00
|
|
|
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
|
|
|
if (TouchedIdentifier &&
|
|
|
|
D->getLocation() == TouchedIdentifier->location()) {
|
2020-01-25 01:21:47 +08:00
|
|
|
AddResultDecl(CTSD->getSpecializedTemplate());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise the target declaration is the right one.
|
|
|
|
AddResultDecl(D);
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
// Now query the index for all Symbol IDs we found in the AST.
|
|
|
|
if (Index && !ResultIndex.empty()) {
|
2018-04-30 23:24:17 +08:00
|
|
|
LookupRequest QueryRequest;
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
for (auto It : ResultIndex)
|
2018-04-30 23:24:17 +08:00
|
|
|
QueryRequest.IDs.insert(It.first);
|
2019-02-12 18:38:45 +08:00
|
|
|
std::string Scratch;
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
Index->lookup(QueryRequest, [&](const Symbol &Sym) {
|
|
|
|
auto &R = Result[ResultIndex.lookup(Sym.ID)];
|
|
|
|
|
2019-02-11 23:05:29 +08:00
|
|
|
if (R.Definition) { // from AST
|
2019-05-03 20:11:14 +08:00
|
|
|
// Special case: if the AST yielded a definition, then it may not be
|
|
|
|
// the right *declaration*. Prefer the one from the index.
|
2020-03-03 01:45:05 +08:00
|
|
|
if (auto Loc = toLSPLocation(Sym.CanonicalDeclaration, MainFilePath))
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
R.PreferredDeclaration = *Loc;
|
2019-05-03 20:11:14 +08:00
|
|
|
|
|
|
|
// We might still prefer the definition from the index, e.g. for
|
|
|
|
// generated symbols.
|
|
|
|
if (auto Loc = toLSPLocation(
|
|
|
|
getPreferredLocation(*R.Definition, Sym.Definition, Scratch),
|
2020-03-03 01:45:05 +08:00
|
|
|
MainFilePath))
|
2019-05-03 20:11:14 +08:00
|
|
|
R.Definition = *Loc;
|
2019-02-11 23:05:29 +08:00
|
|
|
} else {
|
2020-03-03 01:45:05 +08:00
|
|
|
R.Definition = toLSPLocation(Sym.Definition, MainFilePath);
|
2019-02-11 23:05:29 +08:00
|
|
|
|
2019-05-03 20:11:14 +08:00
|
|
|
// Use merge logic to choose AST or index declaration.
|
|
|
|
if (auto Loc = toLSPLocation(
|
|
|
|
getPreferredLocation(R.PreferredDeclaration,
|
|
|
|
Sym.CanonicalDeclaration, Scratch),
|
2020-03-03 01:45:05 +08:00
|
|
|
MainFilePath))
|
2019-05-03 20:11:14 +08:00
|
|
|
R.PreferredDeclaration = *Loc;
|
2019-02-11 23:05:29 +08:00
|
|
|
}
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
});
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-03-03 01:45:05 +08:00
|
|
|
std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
|
|
|
|
const SymbolIndex *Index) {
|
|
|
|
const auto &SM = AST.getSourceManager();
|
|
|
|
auto MainFilePath =
|
|
|
|
getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
|
|
|
|
if (!MainFilePath) {
|
|
|
|
elog("Failed to get a path for the main file, so no references");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto File = locateFileReferent(Pos, AST, *MainFilePath))
|
|
|
|
return {std::move(*File)};
|
|
|
|
|
|
|
|
auto CurLoc = sourceLocationInMainFile(SM, Pos);
|
|
|
|
if (!CurLoc) {
|
|
|
|
elog("locateSymbolAt failed to convert position to source location: {0}",
|
|
|
|
CurLoc.takeError());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const syntax::Token *TouchedIdentifier =
|
|
|
|
syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
|
|
|
|
if (TouchedIdentifier)
|
|
|
|
if (auto Macro =
|
|
|
|
locateMacroReferent(*TouchedIdentifier, AST, *MainFilePath))
|
|
|
|
// Don't look at the AST or index if we have a macro result.
|
|
|
|
// (We'd just return declarations referenced from the macro's
|
|
|
|
// expansion.)
|
|
|
|
return {*std::move(Macro)};
|
|
|
|
|
|
|
|
return locateASTReferent(*CurLoc, TouchedIdentifier, AST, *MainFilePath,
|
|
|
|
Index);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<DocumentLink> getDocumentLinks(ParsedAST &AST) {
|
|
|
|
const auto &SM = AST.getSourceManager();
|
|
|
|
auto MainFilePath =
|
|
|
|
getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
|
|
|
|
if (!MainFilePath) {
|
|
|
|
elog("Failed to get a path for the main file, so no links");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<DocumentLink> Result;
|
|
|
|
for (auto &Inc : AST.getIncludeStructure().MainFileIncludes) {
|
|
|
|
if (!Inc.Resolved.empty()) {
|
|
|
|
Result.push_back(DocumentLink(
|
|
|
|
{Inc.R, URIForFile::canonicalize(Inc.Resolved, *MainFilePath)}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
/// Collects references to symbols within the main file.
|
|
|
|
class ReferenceFinder : public index::IndexDataConsumer {
|
2017-12-20 01:06:07 +08:00
|
|
|
public:
|
2018-09-05 18:33:36 +08:00
|
|
|
struct Reference {
|
2020-03-01 23:05:12 +08:00
|
|
|
syntax::Token SpelledTok;
|
2018-09-05 18:33:36 +08:00
|
|
|
index::SymbolRoleSet Role;
|
2020-03-01 23:05:12 +08:00
|
|
|
|
|
|
|
Range range(const SourceManager &SM) const {
|
|
|
|
return halfOpenToRange(SM, SpelledTok.range(SM).toCharRange(SM));
|
|
|
|
}
|
2018-09-05 18:33:36 +08:00
|
|
|
};
|
|
|
|
|
2020-03-01 23:05:12 +08:00
|
|
|
ReferenceFinder(const ParsedAST &AST,
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
const std::vector<const NamedDecl *> &TargetDecls)
|
2018-09-05 18:33:36 +08:00
|
|
|
: AST(AST) {
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
for (const NamedDecl *D : TargetDecls)
|
2018-10-04 17:56:08 +08:00
|
|
|
CanonicalTargets.insert(D->getCanonicalDecl());
|
2018-09-05 18:33:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Reference> take() && {
|
2018-10-08 01:21:08 +08:00
|
|
|
llvm::sort(References, [](const Reference &L, const Reference &R) {
|
2020-03-01 23:05:12 +08:00
|
|
|
auto LTok = L.SpelledTok.location();
|
|
|
|
auto RTok = R.SpelledTok.location();
|
|
|
|
return std::tie(LTok, L.Role) < std::tie(RTok, R.Role);
|
2018-10-08 01:21:08 +08:00
|
|
|
});
|
2018-09-05 18:33:36 +08:00
|
|
|
// We sometimes see duplicates when parts of the AST get traversed twice.
|
2019-08-20 22:07:27 +08:00
|
|
|
References.erase(std::unique(References.begin(), References.end(),
|
|
|
|
[](const Reference &L, const Reference &R) {
|
2020-03-01 23:05:12 +08:00
|
|
|
auto LTok = L.SpelledTok.location();
|
|
|
|
auto RTok = R.SpelledTok.location();
|
|
|
|
return std::tie(LTok, L.Role) ==
|
|
|
|
std::tie(RTok, R.Role);
|
2019-08-20 22:07:27 +08:00
|
|
|
}),
|
|
|
|
References.end());
|
2018-09-05 18:33:36 +08:00
|
|
|
return std::move(References);
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-12-16 17:33:56 +08:00
|
|
|
handleDeclOccurrence(const Decl *D, index::SymbolRoleSet Roles,
|
|
|
|
llvm::ArrayRef<index::SymbolRelation> Relations,
|
|
|
|
SourceLocation Loc,
|
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
|
2018-10-04 17:56:08 +08:00
|
|
|
assert(D->isCanonicalDecl() && "expect D to be a canonical declaration");
|
2020-03-01 23:05:12 +08:00
|
|
|
if (!CanonicalTargets.count(D))
|
|
|
|
return true;
|
|
|
|
const auto &TB = AST.getTokens();
|
2018-09-05 18:33:36 +08:00
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
|
|
|
Loc = SM.getFileLoc(Loc);
|
2020-03-01 23:05:12 +08:00
|
|
|
// We are only traversing decls *inside* the main file, so this should hold.
|
|
|
|
assert(isInsideMainFile(Loc, SM));
|
|
|
|
if (const auto *Tok = TB.spelledTokenAt(Loc))
|
|
|
|
References.push_back({*Tok, Roles});
|
2017-12-20 01:06:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallSet<const Decl *, 4> CanonicalTargets;
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<Reference> References;
|
2020-03-01 23:05:12 +08:00
|
|
|
const ParsedAST &AST;
|
2017-12-20 01:06:07 +08:00
|
|
|
};
|
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<ReferenceFinder::Reference>
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
findRefs(const std::vector<const NamedDecl *> &Decls, ParsedAST &AST) {
|
2020-03-01 23:05:12 +08:00
|
|
|
ReferenceFinder RefFinder(AST, Decls);
|
2018-04-30 23:24:17 +08:00
|
|
|
index::IndexingOptions IndexOpts;
|
|
|
|
IndexOpts.SystemSymbolFilter =
|
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::All;
|
|
|
|
IndexOpts.IndexFunctionLocals = true;
|
2019-02-11 21:03:08 +08:00
|
|
|
IndexOpts.IndexParametersInDeclarations = true;
|
2019-02-21 17:55:00 +08:00
|
|
|
IndexOpts.IndexTemplateParameters = true;
|
2018-09-18 16:52:14 +08:00
|
|
|
indexTopLevelDecls(AST.getASTContext(), AST.getPreprocessor(),
|
|
|
|
AST.getLocalTopLevelDecls(), RefFinder, IndexOpts);
|
2018-09-05 18:33:36 +08:00
|
|
|
return std::move(RefFinder).take();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
|
|
|
|
Position Pos) {
|
2019-05-29 05:52:34 +08:00
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
2019-08-02 18:39:46 +08:00
|
|
|
// FIXME: show references to macro within file?
|
2019-10-18 06:48:39 +08:00
|
|
|
DeclRelationSet Relations =
|
|
|
|
DeclRelation::TemplatePattern | DeclRelation::Alias;
|
2020-02-26 20:39:46 +08:00
|
|
|
auto CurLoc = sourceLocationInMainFile(SM, Pos);
|
|
|
|
if (!CurLoc) {
|
|
|
|
llvm::consumeError(CurLoc.takeError());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto References = findRefs(getDeclAtPosition(AST, *CurLoc, Relations), AST);
|
2017-12-20 01:06:07 +08:00
|
|
|
|
2019-08-20 22:07:27 +08:00
|
|
|
// FIXME: we may get multiple DocumentHighlights with the same location and
|
|
|
|
// different kinds, deduplicate them.
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<DocumentHighlight> Result;
|
|
|
|
for (const auto &Ref : References) {
|
2020-03-01 23:05:12 +08:00
|
|
|
DocumentHighlight DH;
|
|
|
|
DH.range = Ref.range(SM);
|
|
|
|
if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write))
|
|
|
|
DH.kind = DocumentHighlightKind::Write;
|
|
|
|
else if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Read))
|
|
|
|
DH.kind = DocumentHighlightKind::Read;
|
|
|
|
else
|
|
|
|
DH.kind = DocumentHighlightKind::Text;
|
|
|
|
Result.push_back(std::move(DH));
|
2018-09-05 18:33:36 +08:00
|
|
|
}
|
|
|
|
return Result;
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-18 18:35:00 +08:00
|
|
|
ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
|
|
|
|
const SymbolIndex *Index) {
|
2019-01-15 02:11:09 +08:00
|
|
|
if (!Limit)
|
|
|
|
Limit = std::numeric_limits<uint32_t>::max();
|
2019-11-18 18:35:00 +08:00
|
|
|
ReferencesResult Results;
|
2019-05-29 05:52:34 +08:00
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
2018-12-19 18:46:21 +08:00
|
|
|
auto MainFilePath =
|
|
|
|
getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
|
2018-09-05 18:33:36 +08:00
|
|
|
if (!MainFilePath) {
|
|
|
|
elog("Failed to get a path for the main file, so no references");
|
|
|
|
return Results;
|
|
|
|
}
|
2020-01-08 20:57:00 +08:00
|
|
|
auto URIMainFile = URIForFile::canonicalize(*MainFilePath, *MainFilePath);
|
2020-02-26 20:39:46 +08:00
|
|
|
auto CurLoc = sourceLocationInMainFile(SM, Pos);
|
|
|
|
if (!CurLoc) {
|
|
|
|
llvm::consumeError(CurLoc.takeError());
|
|
|
|
return {};
|
|
|
|
}
|
2020-02-28 16:25:40 +08:00
|
|
|
llvm::Optional<DefinedMacro> Macro;
|
2020-02-26 20:39:46 +08:00
|
|
|
if (const auto *IdentifierAtCursor =
|
2020-02-28 16:25:40 +08:00
|
|
|
syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens())) {
|
|
|
|
Macro = locateMacroAt(*IdentifierAtCursor, AST.getPreprocessor());
|
|
|
|
}
|
2020-01-08 20:57:00 +08:00
|
|
|
|
2020-02-28 16:25:40 +08:00
|
|
|
RefsRequest Req;
|
|
|
|
if (Macro) {
|
2020-01-08 20:57:00 +08:00
|
|
|
// Handle references to macro.
|
|
|
|
if (auto MacroSID = getSymbolID(Macro->Name, Macro->Info, SM)) {
|
|
|
|
// Collect macro references from main file.
|
|
|
|
const auto &IDToRefs = AST.getMacros().MacroRefs;
|
|
|
|
auto Refs = IDToRefs.find(*MacroSID);
|
|
|
|
if (Refs != IDToRefs.end()) {
|
|
|
|
for (const auto Ref : Refs->second) {
|
|
|
|
Location Result;
|
|
|
|
Result.range = Ref;
|
|
|
|
Result.uri = URIMainFile;
|
|
|
|
Results.References.push_back(std::move(Result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Req.IDs.insert(*MacroSID);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Handle references to Decls.
|
|
|
|
|
|
|
|
// We also show references to the targets of using-decls, so we include
|
|
|
|
// DeclRelation::Underlying.
|
|
|
|
DeclRelationSet Relations = DeclRelation::TemplatePattern |
|
|
|
|
DeclRelation::Alias | DeclRelation::Underlying;
|
2020-02-26 20:39:46 +08:00
|
|
|
auto Decls = getDeclAtPosition(AST, *CurLoc, Relations);
|
2020-01-08 20:57:00 +08:00
|
|
|
|
|
|
|
// We traverse the AST to find references in the main file.
|
|
|
|
auto MainFileRefs = findRefs(Decls, AST);
|
|
|
|
// We may get multiple refs with the same location and different Roles, as
|
|
|
|
// cross-reference is only interested in locations, we deduplicate them
|
|
|
|
// by the location to avoid emitting duplicated locations.
|
|
|
|
MainFileRefs.erase(std::unique(MainFileRefs.begin(), MainFileRefs.end(),
|
|
|
|
[](const ReferenceFinder::Reference &L,
|
|
|
|
const ReferenceFinder::Reference &R) {
|
2020-03-01 23:05:12 +08:00
|
|
|
return L.SpelledTok.location() ==
|
|
|
|
R.SpelledTok.location();
|
2020-01-08 20:57:00 +08:00
|
|
|
}),
|
|
|
|
MainFileRefs.end());
|
|
|
|
for (const auto &Ref : MainFileRefs) {
|
2020-03-01 23:05:12 +08:00
|
|
|
Location Result;
|
|
|
|
Result.range = Ref.range(SM);
|
|
|
|
Result.uri = URIMainFile;
|
|
|
|
Results.References.push_back(std::move(Result));
|
2020-01-08 20:57:00 +08:00
|
|
|
}
|
|
|
|
if (Index && Results.References.size() <= Limit) {
|
|
|
|
for (const Decl *D : Decls) {
|
|
|
|
// Not all symbols can be referenced from outside (e.g.
|
|
|
|
// function-locals).
|
|
|
|
// TODO: we could skip TU-scoped symbols here (e.g. static functions) if
|
|
|
|
// we know this file isn't a header. The details might be tricky.
|
|
|
|
if (D->getParentFunctionOrMethod())
|
|
|
|
continue;
|
|
|
|
if (auto ID = getSymbolID(D))
|
|
|
|
Req.IDs.insert(*ID);
|
|
|
|
}
|
2019-06-25 16:01:46 +08:00
|
|
|
}
|
2018-09-05 18:33:36 +08:00
|
|
|
}
|
|
|
|
// Now query the index for references from other files.
|
2020-01-08 20:57:00 +08:00
|
|
|
if (!Req.IDs.empty() && Index && Results.References.size() <= Limit) {
|
2019-01-15 02:11:09 +08:00
|
|
|
Req.Limit = Limit;
|
2019-11-18 18:35:00 +08:00
|
|
|
Results.HasMore |= Index->refs(Req, [&](const Ref &R) {
|
2020-01-08 20:57:00 +08:00
|
|
|
// No need to continue process if we reach the limit.
|
2019-11-18 18:35:00 +08:00
|
|
|
if (Results.References.size() > Limit)
|
|
|
|
return;
|
2019-01-15 02:11:09 +08:00
|
|
|
auto LSPLoc = toLSPLocation(R.Location, *MainFilePath);
|
|
|
|
// Avoid indexed results for the main file - the AST is authoritative.
|
2019-11-18 18:35:00 +08:00
|
|
|
if (!LSPLoc || LSPLoc->uri.file() == *MainFilePath)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Results.References.push_back(std::move(*LSPLoc));
|
2019-01-15 02:11:09 +08:00
|
|
|
});
|
2018-09-05 18:33:36 +08:00
|
|
|
}
|
2019-11-18 18:35:00 +08:00
|
|
|
if (Results.References.size() > Limit) {
|
|
|
|
Results.HasMore = true;
|
|
|
|
Results.References.resize(Limit);
|
|
|
|
}
|
2018-09-05 18:33:36 +08:00
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
2018-11-28 00:40:46 +08:00
|
|
|
std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
|
2019-05-29 05:52:34 +08:00
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
2020-02-26 20:39:46 +08:00
|
|
|
auto CurLoc = sourceLocationInMainFile(SM, Pos);
|
|
|
|
if (!CurLoc) {
|
|
|
|
llvm::consumeError(CurLoc.takeError());
|
|
|
|
return {};
|
|
|
|
}
|
2018-11-28 00:40:46 +08:00
|
|
|
|
|
|
|
std::vector<SymbolDetails> Results;
|
|
|
|
|
2019-10-18 06:48:39 +08:00
|
|
|
// We also want the targets of using-decls, so we include
|
|
|
|
// DeclRelation::Underlying.
|
|
|
|
DeclRelationSet Relations = DeclRelation::TemplatePattern |
|
|
|
|
DeclRelation::Alias | DeclRelation::Underlying;
|
2020-02-26 20:39:46 +08:00
|
|
|
for (const NamedDecl *D : getDeclAtPosition(AST, *CurLoc, Relations)) {
|
2018-11-28 00:40:46 +08:00
|
|
|
SymbolDetails NewSymbol;
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
std::string QName = printQualifiedName(*D);
|
2020-01-29 03:23:46 +08:00
|
|
|
auto SplitQName = splitQualifiedName(QName);
|
|
|
|
NewSymbol.containerName = std::string(SplitQName.first);
|
|
|
|
NewSymbol.name = std::string(SplitQName.second);
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
|
|
|
|
if (NewSymbol.containerName.empty()) {
|
|
|
|
if (const auto *ParentND =
|
|
|
|
dyn_cast_or_null<NamedDecl>(D->getDeclContext()))
|
|
|
|
NewSymbol.containerName = printQualifiedName(*ParentND);
|
2018-11-28 00:40:46 +08:00
|
|
|
}
|
|
|
|
llvm::SmallString<32> USR;
|
2019-02-21 22:48:33 +08:00
|
|
|
if (!index::generateUSRForDecl(D, USR)) {
|
2020-01-29 03:23:46 +08:00
|
|
|
NewSymbol.USR = std::string(USR.str());
|
2018-11-28 00:40:46 +08:00
|
|
|
NewSymbol.ID = SymbolID(NewSymbol.USR);
|
|
|
|
}
|
|
|
|
Results.push_back(std::move(NewSymbol));
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:39:46 +08:00
|
|
|
const auto *IdentifierAtCursor =
|
|
|
|
syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
|
|
|
|
if (!IdentifierAtCursor)
|
|
|
|
return Results;
|
|
|
|
|
2020-02-28 16:25:40 +08:00
|
|
|
if (auto M = locateMacroAt(*IdentifierAtCursor, AST.getPreprocessor())) {
|
2018-11-28 00:40:46 +08:00
|
|
|
SymbolDetails NewMacro;
|
2020-01-29 03:23:46 +08:00
|
|
|
NewMacro.name = std::string(M->Name);
|
2018-11-28 00:40:46 +08:00
|
|
|
llvm::SmallString<32> USR;
|
2019-09-03 22:12:48 +08:00
|
|
|
if (!index::generateUSRForMacro(NewMacro.name, M->Info->getDefinitionLoc(),
|
|
|
|
SM, USR)) {
|
2020-01-29 03:23:46 +08:00
|
|
|
NewMacro.USR = std::string(USR.str());
|
2018-11-28 00:40:46 +08:00
|
|
|
NewMacro.ID = SymbolID(NewMacro.USR);
|
|
|
|
}
|
|
|
|
Results.push_back(std::move(NewMacro));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const LocatedSymbol &S) {
|
|
|
|
OS << S.Name << ": " << S.PreferredDeclaration;
|
|
|
|
if (S.Definition)
|
|
|
|
OS << " def=" << *S.Definition;
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
// FIXME(nridge): Reduce duplication between this function and declToSym().
|
|
|
|
static llvm::Optional<TypeHierarchyItem>
|
2020-02-20 02:11:01 +08:00
|
|
|
declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND,
|
|
|
|
const syntax::TokenBuffer &TB) {
|
[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
|
|
|
auto &SM = Ctx.getSourceManager();
|
2019-12-10 17:08:39 +08:00
|
|
|
SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
|
2020-02-20 02:11:01 +08:00
|
|
|
auto FilePath =
|
|
|
|
getCanonicalPath(SM.getFileEntryForID(SM.getFileID(NameLoc)), SM);
|
|
|
|
auto TUPath = getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
|
|
|
|
if (!FilePath || !TUPath)
|
|
|
|
return llvm::None; // Not useful without a uri.
|
|
|
|
|
|
|
|
auto DeclToks = TB.spelledForExpanded(TB.expandedTokens(ND.getSourceRange()));
|
|
|
|
if (!DeclToks || DeclToks->empty())
|
[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
|
|
|
return llvm::None;
|
|
|
|
|
2020-02-20 02:11:01 +08:00
|
|
|
auto NameToks = TB.spelledForExpanded(TB.expandedTokens(NameLoc));
|
|
|
|
if (!NameToks || NameToks->empty())
|
|
|
|
return llvm::None;
|
[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
|
|
|
|
|
|
|
index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
|
|
|
|
// FIXME: this is not classifying constructors, destructors and operators
|
|
|
|
// correctly (they're all "methods").
|
|
|
|
SymbolKind SK = indexSymbolKindToSymbolKind(SymInfo.Kind);
|
|
|
|
|
|
|
|
TypeHierarchyItem THI;
|
|
|
|
THI.name = printName(Ctx, ND);
|
|
|
|
THI.kind = SK;
|
|
|
|
THI.deprecated = ND.isDeprecated();
|
2020-02-20 02:11:01 +08:00
|
|
|
THI.range = halfOpenToRange(
|
|
|
|
SM, syntax::Token::range(SM, DeclToks->front(), DeclToks->back())
|
|
|
|
.toCharRange(SM));
|
|
|
|
THI.selectionRange = halfOpenToRange(
|
|
|
|
SM, syntax::Token::range(SM, NameToks->front(), NameToks->back())
|
|
|
|
.toCharRange(SM));
|
[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
|
|
|
if (!THI.range.contains(THI.selectionRange)) {
|
|
|
|
// 'selectionRange' must be contained in 'range', so in cases where clang
|
|
|
|
// reports unrelated ranges we need to reconcile somehow.
|
|
|
|
THI.range = THI.selectionRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
THI.uri = URIForFile::canonicalize(*FilePath, *TUPath);
|
|
|
|
|
|
|
|
return THI;
|
|
|
|
}
|
|
|
|
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
static Optional<TypeHierarchyItem>
|
|
|
|
symbolToTypeHierarchyItem(const Symbol &S, const SymbolIndex *Index,
|
|
|
|
PathRef TUPath) {
|
|
|
|
auto Loc = symbolToLocation(S, TUPath);
|
|
|
|
if (!Loc) {
|
|
|
|
log("Type hierarchy: {0}", Loc.takeError());
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
TypeHierarchyItem THI;
|
2020-01-29 03:23:46 +08:00
|
|
|
THI.name = std::string(S.Name);
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
THI.kind = indexSymbolKindToSymbolKind(S.SymInfo.Kind);
|
|
|
|
THI.deprecated = (S.Flags & Symbol::Deprecated);
|
|
|
|
THI.selectionRange = Loc->range;
|
|
|
|
// FIXME: Populate 'range' correctly
|
|
|
|
// (https://github.com/clangd/clangd/issues/59).
|
|
|
|
THI.range = THI.selectionRange;
|
|
|
|
THI.uri = Loc->uri;
|
2019-07-13 11:24:48 +08:00
|
|
|
// Store the SymbolID in the 'data' field. The client will
|
|
|
|
// send this back in typeHierarchy/resolve, allowing us to
|
|
|
|
// continue resolving additional levels of the type hierarchy.
|
|
|
|
THI.data = S.ID.str();
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
|
|
|
|
return std::move(THI);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fillSubTypes(const SymbolID &ID,
|
|
|
|
std::vector<TypeHierarchyItem> &SubTypes,
|
|
|
|
const SymbolIndex *Index, int Levels, PathRef TUPath) {
|
|
|
|
RelationsRequest Req;
|
|
|
|
Req.Subjects.insert(ID);
|
2019-10-17 22:08:28 +08:00
|
|
|
Req.Predicate = RelationKind::BaseOf;
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) {
|
|
|
|
if (Optional<TypeHierarchyItem> ChildSym =
|
|
|
|
symbolToTypeHierarchyItem(Object, Index, TUPath)) {
|
|
|
|
if (Levels > 1) {
|
|
|
|
ChildSym->children.emplace();
|
|
|
|
fillSubTypes(Object.ID, *ChildSym->children, Index, Levels - 1, TUPath);
|
|
|
|
}
|
|
|
|
SubTypes.emplace_back(std::move(*ChildSym));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-22 09:38:53 +08:00
|
|
|
using RecursionProtectionSet = llvm::SmallSet<const CXXRecordDecl *, 4>;
|
|
|
|
|
2019-07-17 23:26:49 +08:00
|
|
|
static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
|
|
|
|
std::vector<TypeHierarchyItem> &SuperTypes,
|
2020-02-20 02:11:01 +08:00
|
|
|
RecursionProtectionSet &RPSet,
|
|
|
|
const syntax::TokenBuffer &TB) {
|
2019-04-22 09:38:53 +08:00
|
|
|
// typeParents() will replace dependent template specializations
|
|
|
|
// with their class template, so to avoid infinite recursion for
|
|
|
|
// certain types of hierarchies, keep the templates encountered
|
|
|
|
// along the parent chain in a set, and stop the recursion if one
|
|
|
|
// starts to repeat.
|
|
|
|
auto *Pattern = CXXRD.getDescribedTemplate() ? &CXXRD : nullptr;
|
|
|
|
if (Pattern) {
|
|
|
|
if (!RPSet.insert(Pattern).second) {
|
2019-07-17 23:26:49 +08:00
|
|
|
return;
|
2019-04-22 09:38:53 +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
|
|
|
for (const CXXRecordDecl *ParentDecl : typeParents(&CXXRD)) {
|
|
|
|
if (Optional<TypeHierarchyItem> ParentSym =
|
2020-02-20 02:11:01 +08:00
|
|
|
declToTypeHierarchyItem(ASTCtx, *ParentDecl, TB)) {
|
2019-07-17 23:26:49 +08:00
|
|
|
ParentSym->parents.emplace();
|
2020-02-20 02:11:01 +08:00
|
|
|
fillSuperTypes(*ParentDecl, ASTCtx, *ParentSym->parents, RPSet, TB);
|
2019-07-17 23:26:49 +08:00
|
|
|
SuperTypes.emplace_back(std::move(*ParentSym));
|
[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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 09:38:53 +08:00
|
|
|
if (Pattern) {
|
|
|
|
RPSet.erase(Pattern);
|
|
|
|
}
|
[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 CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
|
2020-02-24 03:03:00 +08:00
|
|
|
auto RecordFromNode =
|
|
|
|
[](const SelectionTree::Node *N) -> const CXXRecordDecl * {
|
|
|
|
if (!N)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Note: explicitReferenceTargets() will search for both template
|
|
|
|
// instantiations and template patterns, and prefer the former if available
|
|
|
|
// (generally, one will be available for non-dependent specializations of a
|
|
|
|
// class template).
|
|
|
|
auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Underlying);
|
|
|
|
if (Decls.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const NamedDecl *D = Decls[0];
|
|
|
|
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
// If this is a variable, use the type of the variable.
|
|
|
|
return VD->getType().getTypePtr()->getAsCXXRecordDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
|
|
|
|
// If this is a method, use the type of the class.
|
|
|
|
return Method->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't handle FieldDecl because it's not clear what behaviour
|
|
|
|
// the user would expect: the enclosing class type (as with a
|
|
|
|
// method), or the field's type (as with a variable).
|
|
|
|
|
|
|
|
return dyn_cast<CXXRecordDecl>(D);
|
|
|
|
};
|
|
|
|
|
2019-09-04 18:15:27 +08:00
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
2020-02-24 03:03:00 +08:00
|
|
|
const CXXRecordDecl *Result = nullptr;
|
2020-02-26 20:39:46 +08:00
|
|
|
auto Offset = positionToOffset(SM.getBufferData(SM.getMainFileID()), Pos);
|
|
|
|
if (!Offset) {
|
|
|
|
llvm::consumeError(Offset.takeError());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), *Offset,
|
|
|
|
*Offset, [&](SelectionTree ST) {
|
2020-02-24 03:03:00 +08:00
|
|
|
Result = RecordFromNode(ST.commonAncestor());
|
|
|
|
return Result != nullptr;
|
|
|
|
});
|
|
|
|
return Result;
|
[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
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD) {
|
|
|
|
std::vector<const CXXRecordDecl *> Result;
|
|
|
|
|
2019-12-20 05:22:23 +08:00
|
|
|
// If this is an invalid instantiation, instantiation of the bases
|
|
|
|
// may not have succeeded, so fall back to the template pattern.
|
|
|
|
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CXXRD)) {
|
|
|
|
if (CTSD->isInvalidDecl())
|
|
|
|
CXXRD = CTSD->getSpecializedTemplate()->getTemplatedDecl();
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
for (auto Base : CXXRD->bases()) {
|
|
|
|
const CXXRecordDecl *ParentDecl = nullptr;
|
|
|
|
|
|
|
|
const Type *Type = Base.getType().getTypePtr();
|
|
|
|
if (const RecordType *RT = Type->getAs<RecordType>()) {
|
|
|
|
ParentDecl = RT->getAsCXXRecordDecl();
|
|
|
|
}
|
|
|
|
|
2019-04-22 09:38:53 +08:00
|
|
|
if (!ParentDecl) {
|
|
|
|
// Handle a dependent base such as "Base<T>" by using the primary
|
|
|
|
// template.
|
|
|
|
if (const TemplateSpecializationType *TS =
|
|
|
|
Type->getAs<TemplateSpecializationType>()) {
|
|
|
|
TemplateName TN = TS->getTemplateName();
|
|
|
|
if (TemplateDecl *TD = TN.getAsTemplateDecl()) {
|
|
|
|
ParentDecl = dyn_cast<CXXRecordDecl>(TD->getTemplatedDecl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[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
|
|
|
|
|
|
|
if (ParentDecl)
|
|
|
|
Result.push_back(ParentDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<TypeHierarchyItem>
|
|
|
|
getTypeHierarchy(ParsedAST &AST, Position Pos, int ResolveLevels,
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
TypeHierarchyDirection Direction, const SymbolIndex *Index,
|
|
|
|
PathRef TUPath) {
|
[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 CXXRecordDecl *CXXRD = findRecordTypeAt(AST, Pos);
|
|
|
|
if (!CXXRD)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
Optional<TypeHierarchyItem> Result =
|
2020-02-20 02:11:01 +08:00
|
|
|
declToTypeHierarchyItem(AST.getASTContext(), *CXXRD, AST.getTokens());
|
2019-07-12 08:24:45 +08:00
|
|
|
if (!Result)
|
|
|
|
return Result;
|
2019-04-22 09:38:53 +08:00
|
|
|
|
2019-07-17 23:26:49 +08:00
|
|
|
if (Direction == TypeHierarchyDirection::Parents ||
|
|
|
|
Direction == TypeHierarchyDirection::Both) {
|
|
|
|
Result->parents.emplace();
|
|
|
|
|
|
|
|
RecursionProtectionSet RPSet;
|
2020-02-20 02:11:01 +08:00
|
|
|
fillSuperTypes(*CXXRD, AST.getASTContext(), *Result->parents, RPSet,
|
|
|
|
AST.getTokens());
|
2019-07-17 23:26:49 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
if ((Direction == TypeHierarchyDirection::Children ||
|
|
|
|
Direction == TypeHierarchyDirection::Both) &&
|
|
|
|
ResolveLevels > 0) {
|
|
|
|
Result->children.emplace();
|
|
|
|
|
|
|
|
if (Index) {
|
2019-12-20 05:22:23 +08:00
|
|
|
// The index does not store relationships between implicit
|
|
|
|
// specializations, so if we have one, use the template pattern instead.
|
|
|
|
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CXXRD))
|
|
|
|
CXXRD = CTSD->getTemplateInstantiationPattern();
|
|
|
|
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
if (Optional<SymbolID> ID = getSymbolID(CXXRD))
|
|
|
|
fillSubTypes(*ID, *Result->children, Index, ResolveLevels, TUPath);
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 09:38:53 +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
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:24:48 +08:00
|
|
|
void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels,
|
|
|
|
TypeHierarchyDirection Direction,
|
|
|
|
const SymbolIndex *Index) {
|
|
|
|
// We only support typeHierarchy/resolve for children, because for parents
|
|
|
|
// we ignore ResolveLevels and return all levels of parents eagerly.
|
|
|
|
if (Direction == TypeHierarchyDirection::Parents || ResolveLevels == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Item.children.emplace();
|
|
|
|
|
|
|
|
if (Index && Item.data) {
|
|
|
|
// We store the item's SymbolID in the 'data' field, and the client
|
|
|
|
// passes it back to us in typeHierarchy/resolve.
|
|
|
|
if (Expected<SymbolID> ID = SymbolID::fromStr(*Item.data)) {
|
|
|
|
fillSubTypes(*ID, *Item.children, Index, ResolveLevels, Item.uri.file());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:27:43 +08:00
|
|
|
llvm::DenseSet<const Decl *> getNonLocalDeclRefs(ParsedAST &AST,
|
|
|
|
const FunctionDecl *FD) {
|
|
|
|
if (!FD->hasBody())
|
|
|
|
return {};
|
|
|
|
llvm::DenseSet<const Decl *> DeclRefs;
|
|
|
|
findExplicitReferences(FD, [&](ReferenceLoc Ref) {
|
|
|
|
for (const Decl *D : Ref.Targets) {
|
2019-10-18 20:07:19 +08:00
|
|
|
if (!index::isFunctionLocalSymbol(D) && !D->isTemplateParameter() &&
|
|
|
|
!Ref.IsDecl)
|
2019-09-26 15:27:43 +08:00
|
|
|
DeclRefs.insert(D);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return DeclRefs;
|
|
|
|
}
|
2017-12-20 01:06:07 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|