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-05-29 18:01:00 +08:00
|
|
|
#include "FormattedString.h"
|
2018-01-29 23:37:46 +08:00
|
|
|
#include "Logger.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "Protocol.h"
|
2018-02-21 10:39:08 +08:00
|
|
|
#include "SourceCode.h"
|
2018-01-29 23:37:46 +08:00
|
|
|
#include "URI.h"
|
2019-02-11 23:05:29 +08:00
|
|
|
#include "index/Merge.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 "index/SymbolCollector.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"
|
|
|
|
#include "clang/AST/Decl.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-05-28 18:29:58 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2018-07-03 00:28:34 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.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"
|
|
|
|
#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"
|
2018-04-30 23:24:17 +08:00
|
|
|
#include "clang/Index/USRGeneration.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"
|
|
|
|
#include "llvm/Support/FormatVariadic.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().
|
2018-07-26 20:05:31 +08:00
|
|
|
const Decl *getDefinition(const Decl *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.
|
|
|
|
// definition) are not factored into the preferrence.
|
|
|
|
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] 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
|
|
|
struct MacroDecl {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef Name;
|
[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
|
|
|
const MacroInfo *Info;
|
|
|
|
};
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
/// Finds declarations locations that a given source location refers to.
|
|
|
|
class DeclarationAndMacrosFinder : public index::IndexDataConsumer {
|
[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
|
|
|
std::vector<MacroDecl> MacroInfos;
|
2019-02-21 22:48:33 +08:00
|
|
|
llvm::DenseSet<const Decl *> Decls;
|
2017-12-20 01:06:07 +08:00
|
|
|
const SourceLocation &SearchedLocation;
|
|
|
|
const ASTContext &AST;
|
|
|
|
Preprocessor &PP;
|
|
|
|
|
|
|
|
public:
|
2018-08-28 19:04:07 +08:00
|
|
|
DeclarationAndMacrosFinder(const SourceLocation &SearchedLocation,
|
2017-12-20 01:06:07 +08:00
|
|
|
ASTContext &AST, Preprocessor &PP)
|
|
|
|
: SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
|
|
|
|
|
2019-02-21 22:48:33 +08:00
|
|
|
// The results are sorted by declaration location.
|
|
|
|
std::vector<const Decl *> getFoundDecls() const {
|
|
|
|
std::vector<const Decl *> Result;
|
|
|
|
for (const Decl *D : Decls)
|
|
|
|
Result.push_back(D);
|
2018-09-05 20:00:15 +08:00
|
|
|
|
2019-02-21 22:48:33 +08:00
|
|
|
llvm::sort(Result, [](const Decl *L, const Decl *R) {
|
|
|
|
return L->getBeginLoc() < R->getBeginLoc();
|
2018-10-07 22:49:41 +08:00
|
|
|
});
|
2018-09-05 20:00:15 +08:00
|
|
|
return Result;
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
[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
|
|
|
std::vector<MacroDecl> takeMacroInfos() {
|
2017-12-20 01:06:07 +08:00
|
|
|
// Don't keep the same Macro info multiple times.
|
2018-10-07 22:49:41 +08:00
|
|
|
llvm::sort(MacroInfos, [](const MacroDecl &Left, const MacroDecl &Right) {
|
|
|
|
return Left.Info < Right.Info;
|
|
|
|
});
|
[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
|
|
|
|
|
|
|
auto Last = std::unique(MacroInfos.begin(), MacroInfos.end(),
|
|
|
|
[](const MacroDecl &Left, const MacroDecl &Right) {
|
|
|
|
return Left.Info == Right.Info;
|
|
|
|
});
|
2017-12-20 01:06:07 +08:00
|
|
|
MacroInfos.erase(Last, MacroInfos.end());
|
|
|
|
return std::move(MacroInfos);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::ArrayRef<index::SymbolRelation> Relations,
|
2018-04-09 22:28:52 +08:00
|
|
|
SourceLocation Loc,
|
2017-12-20 01:06:07 +08:00
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
|
2019-03-08 17:54:37 +08:00
|
|
|
// Skip non-semantic references.
|
|
|
|
if (Roles & static_cast<unsigned>(index::SymbolRole::NameReference))
|
|
|
|
return true;
|
|
|
|
|
2018-04-09 22:28:52 +08:00
|
|
|
if (Loc == SearchedLocation) {
|
2019-01-25 23:14:03 +08:00
|
|
|
auto IsImplicitExpr = [](const Expr *E) {
|
2018-12-13 21:17:04 +08:00
|
|
|
if (!E)
|
2018-09-05 20:00:15 +08:00
|
|
|
return false;
|
2018-12-13 21:17:04 +08:00
|
|
|
// We assume that a constructor expression is implict (was inserted by
|
|
|
|
// clang) if it has an invalid paren/brace location, since such
|
|
|
|
// experssion is impossible to write down.
|
|
|
|
if (const auto *CtorExpr = dyn_cast<CXXConstructExpr>(E))
|
2019-02-21 22:48:33 +08:00
|
|
|
return CtorExpr->getParenOrBraceRange().isInvalid();
|
2018-12-13 21:17:04 +08:00
|
|
|
return isa<ImplicitCastExpr>(E);
|
2018-09-05 20:00:15 +08:00
|
|
|
};
|
|
|
|
|
2019-02-21 22:48:33 +08:00
|
|
|
if (IsImplicitExpr(ASTNode.OrigE))
|
|
|
|
return true;
|
2018-01-12 22:21:10 +08:00
|
|
|
// Find and add definition declarations (for GoToDefinition).
|
|
|
|
// We don't use parameter `D`, as Parameter `D` is the canonical
|
|
|
|
// declaration, which is the first declaration of a redeclarable
|
|
|
|
// declaration, and it could be a forward declaration.
|
2018-07-26 20:05:31 +08:00
|
|
|
if (const auto *Def = getDefinition(D)) {
|
2019-02-21 22:48:33 +08:00
|
|
|
Decls.insert(Def);
|
2018-01-12 22:21:10 +08:00
|
|
|
} else {
|
|
|
|
// Couldn't find a definition, fall back to use `D`.
|
2019-02-21 22:48:33 +08:00
|
|
|
Decls.insert(D);
|
2018-01-12 22:21:10 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-20 01:06:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void finish() override {
|
|
|
|
// Also handle possible macro at the searched location.
|
|
|
|
Token Result;
|
|
|
|
auto &Mgr = AST.getSourceManager();
|
2018-04-09 22:28:52 +08:00
|
|
|
if (!Lexer::getRawToken(Mgr.getSpellingLoc(SearchedLocation), Result, Mgr,
|
|
|
|
AST.getLangOpts(), false)) {
|
2017-12-20 01:06:07 +08:00
|
|
|
if (Result.is(tok::raw_identifier)) {
|
|
|
|
PP.LookUpIdentifierInfo(Result);
|
|
|
|
}
|
|
|
|
IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
|
|
|
|
if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
|
|
|
|
std::pair<FileID, unsigned int> DecLoc =
|
|
|
|
Mgr.getDecomposedExpansionLoc(SearchedLocation);
|
|
|
|
// Get the definition just before the searched location so that a macro
|
|
|
|
// referenced in a '#undef MACRO' can still be found.
|
|
|
|
SourceLocation BeforeSearchedLocation = Mgr.getMacroArgExpandedLocation(
|
|
|
|
Mgr.getLocForStartOfFile(DecLoc.first)
|
|
|
|
.getLocWithOffset(DecLoc.second - 1));
|
|
|
|
MacroDefinition MacroDef =
|
|
|
|
PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
|
|
|
|
MacroInfo *MacroInf = MacroDef.getMacroInfo();
|
|
|
|
if (MacroInf) {
|
[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
|
|
|
MacroInfos.push_back(MacroDecl{IdentifierInfo->getName(), MacroInf});
|
2018-04-09 22:28:52 +08:00
|
|
|
assert(Decls.empty());
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
struct IdentifiedSymbol {
|
2019-02-21 22:48:33 +08:00
|
|
|
std::vector<const Decl *> Decls;
|
2018-04-30 23:24:17 +08:00
|
|
|
std::vector<MacroDecl> Macros;
|
|
|
|
};
|
|
|
|
|
|
|
|
IdentifiedSymbol getSymbolAtPosition(ParsedAST &AST, SourceLocation Pos) {
|
2018-08-28 19:04:07 +08:00
|
|
|
auto DeclMacrosFinder = DeclarationAndMacrosFinder(Pos, AST.getASTContext(),
|
|
|
|
AST.getPreprocessor());
|
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(), DeclMacrosFinder, IndexOpts);
|
2018-04-30 23:24:17 +08:00
|
|
|
|
2018-09-05 20:00:15 +08:00
|
|
|
return {DeclMacrosFinder.getFoundDecls(), DeclMacrosFinder.takeMacroInfos()};
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
Range getTokenRange(ASTContext &AST, SourceLocation TokLoc) {
|
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
|
|
|
SourceLocation LocEnd =
|
|
|
|
Lexer::getLocForEndOfToken(TokLoc, 0, SourceMgr, AST.getLangOpts());
|
2018-09-05 18:33:36 +08:00
|
|
|
return {sourceLocToPosition(SourceMgr, TokLoc),
|
|
|
|
sourceLocToPosition(SourceMgr, LocEnd)};
|
|
|
|
}
|
2017-12-20 01:06:07 +08:00
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
llvm::Optional<Location> makeLocation(ASTContext &AST, SourceLocation TokLoc,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef TUPath) {
|
2019-05-28 18:29:58 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
2018-09-05 18:33:36 +08:00
|
|
|
const FileEntry *F = SourceMgr.getFileEntryForID(SourceMgr.getFileID(TokLoc));
|
2017-12-20 01:06:07 +08:00
|
|
|
if (!F)
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-12-19 18:46:21 +08:00
|
|
|
auto FilePath = getCanonicalPath(F, SourceMgr);
|
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
|
|
|
}
|
2018-09-05 18:33:36 +08:00
|
|
|
Location L;
|
2018-11-28 18:30:42 +08:00
|
|
|
L.uri = URIForFile::canonicalize(*FilePath, TUPath);
|
2018-09-05 18:33:36 +08:00
|
|
|
L.range = getTokenRange(AST, TokLoc);
|
2017-12-20 01:06:07 +08:00
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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
|
|
|
std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
|
|
|
|
const SymbolIndex *Index) {
|
2019-05-29 05:52:34 +08:00
|
|
|
const auto &SM = AST.getSourceManager();
|
2018-12-19 18:46:21 +08:00
|
|
|
auto MainFilePath =
|
|
|
|
getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
|
2018-11-28 18:30:42 +08:00
|
|
|
if (!MainFilePath) {
|
|
|
|
elog("Failed to get a path for the main file, so no references");
|
|
|
|
return {};
|
|
|
|
}
|
2017-12-20 01:06:07 +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
|
|
|
// Treat #included files as symbols, to enable go-to-definition on them.
|
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;
|
|
|
|
File.Name = llvm::sys::path::filename(Inc.Resolved);
|
|
|
|
File.PreferredDeclaration = {
|
|
|
|
URIForFile::canonicalize(Inc.Resolved, *MainFilePath), Range{}};
|
|
|
|
File.Definition = File.PreferredDeclaration;
|
|
|
|
// We're not going to find any further symbols on #include lines.
|
|
|
|
return {std::move(File)};
|
|
|
|
}
|
2018-03-09 00:28:12 +08:00
|
|
|
}
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
SourceLocation SourceLocationBeg =
|
2018-11-28 18:30:42 +08:00
|
|
|
getBeginningOfIdentifier(AST, Pos, SM.getMainFileID());
|
2018-04-30 23:24:17 +08:00
|
|
|
auto Symbols = getSymbolAtPosition(AST, SourceLocationBeg);
|
2017-12-20 01:06:07 +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
|
|
|
// Macros are simple: there's no declaration/definition distinction.
|
|
|
|
// As a consequence, there's no need to look them up in the index either.
|
|
|
|
std::vector<LocatedSymbol> Result;
|
|
|
|
for (auto M : Symbols.Macros) {
|
2019-05-28 18:29:58 +08:00
|
|
|
if (auto Loc = makeLocation(AST.getASTContext(), M.Info->getDefinitionLoc(),
|
|
|
|
*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
|
|
|
LocatedSymbol Macro;
|
|
|
|
Macro.Name = M.Name;
|
|
|
|
Macro.PreferredDeclaration = *Loc;
|
|
|
|
Macro.Definition = Loc;
|
|
|
|
Result.push_back(std::move(Macro));
|
|
|
|
}
|
2017-12-20 01:06:07 +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
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Results follow the order of Symbols.Decls.
|
|
|
|
// 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
|
|
|
|
|
|
|
// Emit all symbol locations (declaration or definition) from AST.
|
2019-02-21 22:48:33 +08:00
|
|
|
for (const Decl *D : Symbols.Decls) {
|
2019-05-28 18:29:58 +08:00
|
|
|
auto Loc = makeLocation(AST.getASTContext(), findNameLoc(D), *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)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Result.emplace_back();
|
|
|
|
if (auto *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
Result.back().Name = printName(AST.getASTContext(), *ND);
|
|
|
|
Result.back().PreferredDeclaration = *Loc;
|
|
|
|
// DeclInfo.D is always a definition if possible, so this check works.
|
|
|
|
if (getDefinition(D) == D)
|
|
|
|
Result.back().Definition = *Loc;
|
|
|
|
|
|
|
|
// Record SymbolID for index lookup later.
|
2018-04-30 23:24:17 +08:00
|
|
|
if (auto ID = getSymbolID(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
|
|
|
ResultIndex[*ID] = Result.size() - 1;
|
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.
|
[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 (auto Loc = toLSPLocation(Sym.CanonicalDeclaration, *MainFilePath))
|
|
|
|
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),
|
|
|
|
*MainFilePath))
|
|
|
|
R.Definition = *Loc;
|
2019-02-11 23:05:29 +08:00
|
|
|
} else {
|
[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.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),
|
|
|
|
*MainFilePath))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-10-04 17:56:08 +08:00
|
|
|
const Decl *CanonicalTarget;
|
2018-09-05 18:33:36 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
index::SymbolRoleSet Role;
|
|
|
|
};
|
|
|
|
|
|
|
|
ReferenceFinder(ASTContext &AST, Preprocessor &PP,
|
|
|
|
const std::vector<const Decl *> &TargetDecls)
|
|
|
|
: AST(AST) {
|
|
|
|
for (const Decl *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) {
|
|
|
|
return std::tie(L.Loc, L.CanonicalTarget, L.Role) <
|
|
|
|
std::tie(R.Loc, R.CanonicalTarget, R.Role);
|
|
|
|
});
|
2018-09-05 18:33:36 +08:00
|
|
|
// We sometimes see duplicates when parts of the AST get traversed twice.
|
2018-10-04 17:56:08 +08:00
|
|
|
References.erase(
|
|
|
|
std::unique(References.begin(), References.end(),
|
|
|
|
[](const Reference &L, const Reference &R) {
|
|
|
|
return std::tie(L.CanonicalTarget, L.Loc, L.Role) ==
|
|
|
|
std::tie(R.CanonicalTarget, R.Loc, R.Role);
|
|
|
|
}),
|
|
|
|
References.end());
|
2018-09-05 18:33:36 +08:00
|
|
|
return std::move(References);
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::ArrayRef<index::SymbolRelation> Relations,
|
2018-04-09 22:28:52 +08:00
|
|
|
SourceLocation Loc,
|
2017-12-20 01:06:07 +08:00
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
|
2018-10-04 17:56:08 +08:00
|
|
|
assert(D->isCanonicalDecl() && "expect D to be a canonical declaration");
|
2018-09-05 18:33:36 +08:00
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
|
|
|
Loc = SM.getFileLoc(Loc);
|
2018-10-04 17:56:08 +08:00
|
|
|
if (SM.isWrittenInMainFile(Loc) && CanonicalTargets.count(D))
|
2018-09-05 18:33:36 +08:00
|
|
|
References.push_back({D, Loc, 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;
|
|
|
|
const ASTContext &AST;
|
2017-12-20 01:06:07 +08:00
|
|
|
};
|
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<ReferenceFinder::Reference>
|
|
|
|
findRefs(const std::vector<const Decl *> &Decls, ParsedAST &AST) {
|
|
|
|
ReferenceFinder RefFinder(AST.getASTContext(), AST.getPreprocessor(), 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();
|
2018-09-05 18:33:36 +08:00
|
|
|
auto Symbols = getSymbolAtPosition(
|
|
|
|
AST, getBeginningOfIdentifier(AST, Pos, SM.getMainFileID()));
|
2019-02-21 22:48:33 +08:00
|
|
|
auto References = findRefs(Symbols.Decls, AST);
|
2017-12-20 01:06:07 +08:00
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<DocumentHighlight> Result;
|
|
|
|
for (const auto &Ref : References) {
|
|
|
|
DocumentHighlight DH;
|
2019-05-28 18:29:58 +08:00
|
|
|
DH.range = getTokenRange(AST.getASTContext(), Ref.Loc);
|
2018-09-05 18:33:36 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
return Result;
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 20:05:31 +08:00
|
|
|
static PrintingPolicy printingPolicyForDecls(PrintingPolicy Base) {
|
[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
|
|
|
PrintingPolicy Policy(Base);
|
|
|
|
|
|
|
|
Policy.AnonymousTagLocations = false;
|
|
|
|
Policy.TerseOutput = true;
|
|
|
|
Policy.PolishForDeclaration = true;
|
|
|
|
Policy.ConstantsAsWritten = true;
|
|
|
|
Policy.SuppressTagKeyword = false;
|
|
|
|
|
|
|
|
return Policy;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
/// Given a declaration \p D, return a human-readable string representing the
|
|
|
|
/// local scope in which it is declared, i.e. class(es) and method name. Returns
|
|
|
|
/// an empty string if it is not local.
|
|
|
|
static std::string getLocalScope(const Decl *D) {
|
|
|
|
std::vector<std::string> Scopes;
|
|
|
|
const DeclContext *DC = D->getDeclContext();
|
|
|
|
auto GetName = [](const Decl *D) {
|
|
|
|
const NamedDecl *ND = dyn_cast<NamedDecl>(D);
|
|
|
|
std::string Name = ND->getNameAsString();
|
|
|
|
if (!Name.empty())
|
|
|
|
return Name;
|
|
|
|
if (auto RD = dyn_cast<RecordDecl>(D))
|
|
|
|
return ("(anonymous " + RD->getKindName() + ")").str();
|
|
|
|
return std::string("");
|
|
|
|
};
|
|
|
|
while (DC) {
|
|
|
|
if (const TypeDecl *TD = dyn_cast<TypeDecl>(DC))
|
|
|
|
Scopes.push_back(GetName(TD));
|
|
|
|
else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
|
|
|
|
Scopes.push_back(FD->getNameAsString());
|
|
|
|
DC = DC->getParent();
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
return llvm::join(llvm::reverse(Scopes), "::");
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
/// Returns the human-readable representation for namespace containing the
|
|
|
|
/// declaration \p D. Returns empty if it is contained global namespace.
|
|
|
|
static std::string getNamespaceScope(const Decl *D) {
|
|
|
|
const DeclContext *DC = D->getDeclContext();
|
|
|
|
|
|
|
|
if (const TypeDecl *TD = dyn_cast<TypeDecl>(DC))
|
|
|
|
return getNamespaceScope(TD);
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
|
|
|
|
return getNamespaceScope(FD);
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
|
|
|
|
return ND->getQualifiedNameAsString();
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
return "";
|
[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
|
|
|
}
|
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
static std::string printDefinition(const Decl *D) {
|
|
|
|
std::string Definition;
|
|
|
|
llvm::raw_string_ostream OS(Definition);
|
[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
|
|
|
PrintingPolicy Policy =
|
2019-05-28 18:29:58 +08:00
|
|
|
printingPolicyForDecls(D->getASTContext().getPrintingPolicy());
|
|
|
|
Policy.IncludeTagDefinition = false;
|
|
|
|
D->print(OS, Policy);
|
|
|
|
return Definition;
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
static void printParams(llvm::raw_ostream &OS,
|
|
|
|
const std::vector<HoverInfo::Param> &Params) {
|
|
|
|
for (size_t I = 0, E = Params.size(); I != E; ++I) {
|
|
|
|
if (I)
|
|
|
|
OS << ", ";
|
|
|
|
OS << Params.at(I);
|
|
|
|
}
|
[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
|
|
|
}
|
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
static std::vector<HoverInfo::Param>
|
|
|
|
fetchTemplateParameters(const TemplateParameterList *Params,
|
|
|
|
const PrintingPolicy &PP) {
|
|
|
|
assert(Params);
|
|
|
|
std::vector<HoverInfo::Param> TempParameters;
|
|
|
|
|
|
|
|
for (const Decl *Param : *Params) {
|
|
|
|
HoverInfo::Param P;
|
|
|
|
P.Type.emplace();
|
|
|
|
if (const auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
|
|
|
|
P.Type = TTP->wasDeclaredWithTypename() ? "typename" : "class";
|
|
|
|
if (TTP->isParameterPack())
|
|
|
|
*P.Type += "...";
|
|
|
|
|
|
|
|
if (!TTP->getName().empty())
|
|
|
|
P.Name = TTP->getNameAsString();
|
|
|
|
if (TTP->hasDefaultArgument())
|
|
|
|
P.Default = TTP->getDefaultArgument().getAsString(PP);
|
|
|
|
} else if (const auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
|
|
|
|
if (IdentifierInfo *II = NTTP->getIdentifier())
|
|
|
|
P.Name = II->getName().str();
|
|
|
|
|
|
|
|
llvm::raw_string_ostream Out(*P.Type);
|
|
|
|
NTTP->getType().print(Out, PP);
|
|
|
|
if (NTTP->isParameterPack())
|
|
|
|
Out << "...";
|
|
|
|
|
|
|
|
if (NTTP->hasDefaultArgument()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
|
|
|
NTTP->getDefaultArgument()->printPretty(Out, nullptr, PP);
|
|
|
|
}
|
|
|
|
} else if (const auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
|
|
|
|
llvm::raw_string_ostream OS(*P.Type);
|
|
|
|
OS << "template <";
|
|
|
|
printParams(OS,
|
|
|
|
fetchTemplateParameters(TTPD->getTemplateParameters(), PP));
|
|
|
|
OS << "> class"; // FIXME: TemplateTemplateParameter doesn't store the
|
|
|
|
// info on whether this param was a "typename" or
|
|
|
|
// "class".
|
|
|
|
if (!TTPD->getName().empty())
|
|
|
|
P.Name = TTPD->getNameAsString();
|
|
|
|
if (TTPD->hasDefaultArgument()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
|
|
|
TTPD->getDefaultArgument().getArgument().print(PP, Out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TempParameters.push_back(std::move(P));
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
return TempParameters;
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
static llvm::Optional<Range> getTokenRange(SourceLocation Loc,
|
|
|
|
const ASTContext &Ctx) {
|
|
|
|
if (!Loc.isValid())
|
|
|
|
return llvm::None;
|
|
|
|
SourceLocation End = Lexer::getLocForEndOfToken(
|
|
|
|
Loc, 0, Ctx.getSourceManager(), Ctx.getLangOpts());
|
|
|
|
if (!End.isValid())
|
|
|
|
return llvm::None;
|
|
|
|
return halfOpenToRange(Ctx.getSourceManager(),
|
|
|
|
CharSourceRange::getCharRange(Loc, End));
|
[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
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a \p Hover object given the declaration \p D.
|
2019-05-28 18:29:58 +08:00
|
|
|
static HoverInfo getHoverContents(const Decl *D) {
|
|
|
|
HoverInfo HI;
|
|
|
|
const ASTContext &Ctx = D->getASTContext();
|
|
|
|
|
|
|
|
HI.NamespaceScope = getNamespaceScope(D);
|
|
|
|
if (!HI.NamespaceScope->empty())
|
|
|
|
HI.NamespaceScope->append("::");
|
|
|
|
HI.LocalScope = getLocalScope(D);
|
|
|
|
if (!HI.LocalScope.empty())
|
|
|
|
HI.LocalScope.append("::");
|
|
|
|
|
|
|
|
PrintingPolicy Policy = printingPolicyForDecls(Ctx.getPrintingPolicy());
|
|
|
|
if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) {
|
|
|
|
HI.Documentation = getDeclComment(Ctx, *ND);
|
|
|
|
HI.Name = printName(Ctx, *ND);
|
[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
|
|
|
}
|
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
HI.Kind = indexSymbolKindToSymbolKind(index::getSymbolInfo(D).Kind);
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
// Fill in template params.
|
|
|
|
if (const TemplateDecl *TD = D->getDescribedTemplate()) {
|
|
|
|
HI.TemplateParameters =
|
|
|
|
fetchTemplateParameters(TD->getTemplateParameters(), Policy);
|
|
|
|
D = TD;
|
|
|
|
} else if (const FunctionDecl *FD = D->getAsFunction()) {
|
|
|
|
if (const auto FTD = FD->getDescribedTemplate()) {
|
|
|
|
HI.TemplateParameters =
|
|
|
|
fetchTemplateParameters(FTD->getTemplateParameters(), Policy);
|
|
|
|
D = FTD;
|
|
|
|
}
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
// Fill in types and params.
|
|
|
|
if (const FunctionDecl *FD = D->getAsFunction()) {
|
|
|
|
HI.ReturnType.emplace();
|
|
|
|
llvm::raw_string_ostream OS(*HI.ReturnType);
|
|
|
|
FD->getReturnType().print(OS, Policy);
|
|
|
|
|
|
|
|
HI.Type.emplace();
|
|
|
|
llvm::raw_string_ostream TypeOS(*HI.Type);
|
|
|
|
FD->getReturnType().print(TypeOS, Policy);
|
|
|
|
TypeOS << '(';
|
|
|
|
|
|
|
|
HI.Parameters.emplace();
|
|
|
|
for (const ParmVarDecl *PVD : FD->parameters()) {
|
|
|
|
if (HI.Parameters->size())
|
|
|
|
TypeOS << ", ";
|
|
|
|
HI.Parameters->emplace_back();
|
|
|
|
auto &P = HI.Parameters->back();
|
|
|
|
if (!PVD->getType().isNull()) {
|
|
|
|
P.Type.emplace();
|
|
|
|
llvm::raw_string_ostream OS(*P.Type);
|
|
|
|
PVD->getType().print(OS, Policy);
|
|
|
|
PVD->getType().print(TypeOS, Policy);
|
|
|
|
} else {
|
|
|
|
std::string Param;
|
|
|
|
llvm::raw_string_ostream OS(Param);
|
|
|
|
PVD->dump(OS);
|
|
|
|
OS.flush();
|
|
|
|
elog("Got param with null type: {0}", Param);
|
|
|
|
}
|
|
|
|
if (!PVD->getName().empty())
|
|
|
|
P.Name = PVD->getNameAsString();
|
|
|
|
if (PVD->hasDefaultArg()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
|
|
|
PVD->getDefaultArg()->printPretty(Out, nullptr, Policy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TypeOS << ')';
|
|
|
|
// FIXME: handle variadics.
|
|
|
|
} else if (const auto *VD = dyn_cast<ValueDecl>(D)) {
|
|
|
|
// FIXME: Currently lambdas are also handled as ValueDecls, they should be
|
|
|
|
// more similar to functions.
|
|
|
|
HI.Type.emplace();
|
|
|
|
llvm::raw_string_ostream OS(*HI.Type);
|
|
|
|
VD->getType().print(OS, Policy);
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
HI.Definition = printDefinition(D);
|
|
|
|
return HI;
|
[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
|
|
|
}
|
|
|
|
|
2018-07-03 00:28:34 +08:00
|
|
|
/// Generate a \p Hover object given the type \p T.
|
2019-05-28 18:29:58 +08:00
|
|
|
static HoverInfo getHoverContents(QualType T, const Decl *D,
|
|
|
|
ASTContext &ASTCtx) {
|
|
|
|
HoverInfo HI;
|
|
|
|
llvm::raw_string_ostream OS(HI.Name);
|
2018-07-26 20:05:31 +08:00
|
|
|
PrintingPolicy Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy());
|
2018-07-03 00:28:34 +08:00
|
|
|
T.print(OS, Policy);
|
2019-05-28 18:29:58 +08:00
|
|
|
|
|
|
|
if (D)
|
|
|
|
HI.Kind = indexSymbolKindToSymbolKind(index::getSymbolInfo(D).Kind);
|
|
|
|
return HI;
|
2018-07-03 00:28:34 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Enhance macro hover to see full definition
Summary: Signed-off-by: Marc-Andre Laperle <malaperle@gmail.com>
Reviewers: simark, ilya-biryukov, sammccall, ioeric, hokein
Reviewed By: ilya-biryukov
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D55250
llvm-svn: 354761
2019-02-25 07:47:03 +08:00
|
|
|
/// Generate a \p Hover object given the macro \p MacroDecl.
|
2019-05-28 18:29:58 +08:00
|
|
|
static HoverInfo getHoverContents(MacroDecl Decl, ParsedAST &AST) {
|
|
|
|
HoverInfo HI;
|
2019-05-29 05:52:34 +08:00
|
|
|
SourceManager &SM = AST.getSourceManager();
|
2019-05-28 18:29:58 +08:00
|
|
|
HI.Name = Decl.Name;
|
|
|
|
HI.Kind = indexSymbolKindToSymbolKind(
|
|
|
|
index::getSymbolInfoForMacro(*Decl.Info).Kind);
|
|
|
|
// FIXME: Populate documentation
|
|
|
|
// FIXME: Pupulate parameters
|
[clangd] Enhance macro hover to see full definition
Summary: Signed-off-by: Marc-Andre Laperle <malaperle@gmail.com>
Reviewers: simark, ilya-biryukov, sammccall, ioeric, hokein
Reviewed By: ilya-biryukov
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D55250
llvm-svn: 354761
2019-02-25 07:47:03 +08:00
|
|
|
|
|
|
|
// Try to get the full definition, not just the name
|
|
|
|
SourceLocation StartLoc = Decl.Info->getDefinitionLoc();
|
|
|
|
SourceLocation EndLoc = Decl.Info->getDefinitionEndLoc();
|
|
|
|
if (EndLoc.isValid()) {
|
|
|
|
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM,
|
|
|
|
AST.getASTContext().getLangOpts());
|
|
|
|
bool Invalid;
|
|
|
|
StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc), &Invalid);
|
|
|
|
if (!Invalid) {
|
|
|
|
unsigned StartOffset = SM.getFileOffset(StartLoc);
|
|
|
|
unsigned EndOffset = SM.getFileOffset(EndLoc);
|
|
|
|
if (EndOffset <= Buffer.size() && StartOffset < EndOffset)
|
2019-05-28 18:29:58 +08:00
|
|
|
HI.Definition =
|
|
|
|
("#define " + Buffer.substr(StartOffset, EndOffset - StartOffset))
|
|
|
|
.str();
|
[clangd] Enhance macro hover to see full definition
Summary: Signed-off-by: Marc-Andre Laperle <malaperle@gmail.com>
Reviewers: simark, ilya-biryukov, sammccall, ioeric, hokein
Reviewed By: ilya-biryukov
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D55250
llvm-svn: 354761
2019-02-25 07:47:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 18:29:58 +08:00
|
|
|
return HI;
|
[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
|
|
|
}
|
|
|
|
|
2018-07-03 00:28:34 +08:00
|
|
|
namespace {
|
|
|
|
/// Computes the deduced type at a given location by visiting the relevant
|
|
|
|
/// nodes. We use this to display the actual type when hovering over an "auto"
|
|
|
|
/// keyword or "decltype()" expression.
|
|
|
|
/// FIXME: This could have been a lot simpler by visiting AutoTypeLocs but it
|
|
|
|
/// seems that the AutoTypeLocs that can be visited along with their AutoType do
|
|
|
|
/// not have the deduced type set. Instead, we have to go to the appropriate
|
|
|
|
/// DeclaratorDecl/FunctionDecl and work our back to the AutoType that does have
|
|
|
|
/// a deduced type set. The AST should be improved to simplify this scenario.
|
|
|
|
class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
|
|
|
|
SourceLocation SearchedLocation;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DeducedTypeVisitor(SourceLocation SearchedLocation)
|
|
|
|
: SearchedLocation(SearchedLocation) {}
|
|
|
|
|
|
|
|
// Handle auto initializers:
|
|
|
|
//- auto i = 1;
|
|
|
|
//- decltype(auto) i = 1;
|
|
|
|
//- auto& i = 1;
|
2018-10-12 18:11:02 +08:00
|
|
|
//- auto* i = &a;
|
2018-07-03 00:28:34 +08:00
|
|
|
bool VisitDeclaratorDecl(DeclaratorDecl *D) {
|
|
|
|
if (!D->getTypeSourceInfo() ||
|
2018-08-10 06:42:26 +08:00
|
|
|
D->getTypeSourceInfo()->getTypeLoc().getBeginLoc() != SearchedLocation)
|
2018-07-03 00:28:34 +08:00
|
|
|
return true;
|
|
|
|
|
2018-10-24 18:09:34 +08:00
|
|
|
if (auto *AT = D->getType()->getContainedAutoType()) {
|
2019-05-28 18:29:58 +08:00
|
|
|
if (!AT->getDeducedType().isNull()) {
|
2018-10-24 18:09:34 +08:00
|
|
|
DeducedType = AT->getDeducedType();
|
2019-05-28 18:29:58 +08:00
|
|
|
this->D = D;
|
|
|
|
}
|
2018-07-03 00:28:34 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle auto return types:
|
|
|
|
//- auto foo() {}
|
|
|
|
//- auto& foo() {}
|
2018-11-17 08:41:14 +08:00
|
|
|
//- auto foo() -> int {}
|
2018-07-03 00:28:34 +08:00
|
|
|
//- auto foo() -> decltype(1+1) {}
|
|
|
|
//- operator auto() const { return 10; }
|
|
|
|
bool VisitFunctionDecl(FunctionDecl *D) {
|
|
|
|
if (!D->getTypeSourceInfo())
|
|
|
|
return true;
|
|
|
|
// Loc of auto in return type (c++14).
|
|
|
|
auto CurLoc = D->getReturnTypeSourceRange().getBegin();
|
|
|
|
// Loc of "auto" in operator auto()
|
|
|
|
if (CurLoc.isInvalid() && dyn_cast<CXXConversionDecl>(D))
|
|
|
|
CurLoc = D->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
|
|
|
|
// Loc of "auto" in function with traling return type (c++11).
|
|
|
|
if (CurLoc.isInvalid())
|
|
|
|
CurLoc = D->getSourceRange().getBegin();
|
|
|
|
if (CurLoc != SearchedLocation)
|
|
|
|
return true;
|
|
|
|
|
2018-10-24 18:09:34 +08:00
|
|
|
const AutoType *AT = D->getReturnType()->getContainedAutoType();
|
2018-07-03 00:28:34 +08:00
|
|
|
if (AT && !AT->getDeducedType().isNull()) {
|
2018-10-24 18:09:34 +08:00
|
|
|
DeducedType = AT->getDeducedType();
|
2019-05-28 18:29:58 +08:00
|
|
|
this->D = D;
|
2018-11-17 08:41:14 +08:00
|
|
|
} else if (auto DT = dyn_cast<DecltypeType>(D->getReturnType())) {
|
2018-10-24 18:09:34 +08:00
|
|
|
// auto in a trailing return type just points to a DecltypeType and
|
|
|
|
// getContainedAutoType does not unwrap it.
|
2019-05-28 18:29:58 +08:00
|
|
|
if (!DT->getUnderlyingType().isNull()) {
|
2018-07-03 00:28:34 +08:00
|
|
|
DeducedType = DT->getUnderlyingType();
|
2019-05-28 18:29:58 +08:00
|
|
|
this->D = D;
|
|
|
|
}
|
2018-11-17 08:41:14 +08:00
|
|
|
} else if (!D->getReturnType().isNull()) {
|
|
|
|
DeducedType = D->getReturnType();
|
2019-05-28 18:29:58 +08:00
|
|
|
this->D = D;
|
2018-07-03 00:28:34 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle non-auto decltype, e.g.:
|
|
|
|
// - auto foo() -> decltype(expr) {}
|
|
|
|
// - decltype(expr);
|
|
|
|
bool VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
|
|
|
|
if (TL.getBeginLoc() != SearchedLocation)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// A DecltypeType's underlying type can be another DecltypeType! E.g.
|
|
|
|
// int I = 0;
|
|
|
|
// decltype(I) J = I;
|
|
|
|
// decltype(J) K = J;
|
|
|
|
const DecltypeType *DT = dyn_cast<DecltypeType>(TL.getTypePtr());
|
|
|
|
while (DT && !DT->getUnderlyingType().isNull()) {
|
|
|
|
DeducedType = DT->getUnderlyingType();
|
2019-05-28 18:29:58 +08:00
|
|
|
D = DT->getAsTagDecl();
|
|
|
|
DT = dyn_cast<DecltypeType>(DeducedType.getTypePtr());
|
2018-07-03 00:28:34 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-28 18:29:58 +08:00
|
|
|
|
|
|
|
QualType DeducedType;
|
|
|
|
const Decl *D = nullptr;
|
2018-07-03 00:28:34 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/// Retrieves the deduced type at a given location (auto, decltype).
|
2019-05-28 18:29:58 +08:00
|
|
|
bool hasDeducedType(ParsedAST &AST, SourceLocation SourceLocationBeg) {
|
2018-07-03 00:28:34 +08:00
|
|
|
Token Tok;
|
|
|
|
auto &ASTCtx = AST.getASTContext();
|
|
|
|
// Only try to find a deduced type if the token is auto or decltype.
|
|
|
|
if (!SourceLocationBeg.isValid() ||
|
|
|
|
Lexer::getRawToken(SourceLocationBeg, Tok, ASTCtx.getSourceManager(),
|
|
|
|
ASTCtx.getLangOpts(), false) ||
|
|
|
|
!Tok.is(tok::raw_identifier)) {
|
2019-05-28 18:29:58 +08:00
|
|
|
return false;
|
2018-07-03 00:28:34 +08:00
|
|
|
}
|
|
|
|
AST.getPreprocessor().LookUpIdentifierInfo(Tok);
|
|
|
|
if (!(Tok.is(tok::kw_auto) || Tok.is(tok::kw_decltype)))
|
2019-05-28 18:29:58 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
2018-07-03 00:28:34 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
|
|
|
|
format::FormatStyle Style) {
|
|
|
|
llvm::Optional<HoverInfo> HI;
|
2019-05-29 05:52:34 +08:00
|
|
|
SourceLocation SourceLocationBeg = getBeginningOfIdentifier(
|
|
|
|
AST, Pos, AST.getSourceManager().getMainFileID());
|
2018-04-30 23:24:17 +08:00
|
|
|
// Identified symbols at a specific position.
|
|
|
|
auto Symbols = getSymbolAtPosition(AST, SourceLocationBeg);
|
[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
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
if (!Symbols.Macros.empty())
|
2019-05-28 18:29:58 +08:00
|
|
|
HI = getHoverContents(Symbols.Macros[0], AST);
|
|
|
|
else if (!Symbols.Decls.empty())
|
|
|
|
HI = getHoverContents(Symbols.Decls[0]);
|
|
|
|
else {
|
|
|
|
if (!hasDeducedType(AST, SourceLocationBeg))
|
|
|
|
return None;
|
|
|
|
|
|
|
|
DeducedTypeVisitor V(SourceLocationBeg);
|
|
|
|
V.TraverseAST(AST.getASTContext());
|
|
|
|
if (V.DeducedType.isNull())
|
|
|
|
return None;
|
|
|
|
HI = getHoverContents(V.DeducedType, V.D, AST.getASTContext());
|
|
|
|
}
|
[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
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
auto Replacements = format::reformat(
|
|
|
|
Style, HI->Definition, tooling::Range(0, HI->Definition.size()));
|
|
|
|
if (auto Formatted =
|
|
|
|
tooling::applyAllReplacements(HI->Definition, Replacements))
|
|
|
|
HI->Definition = *Formatted;
|
2018-07-03 00:28:34 +08:00
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
HI->SymRange = getTokenRange(SourceLocationBeg, AST.getASTContext());
|
|
|
|
return HI;
|
[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
|
|
|
}
|
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<Location> findReferences(ParsedAST &AST, Position Pos,
|
2019-01-15 02:11:09 +08:00
|
|
|
uint32_t Limit, const SymbolIndex *Index) {
|
|
|
|
if (!Limit)
|
|
|
|
Limit = std::numeric_limits<uint32_t>::max();
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<Location> 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;
|
|
|
|
}
|
|
|
|
auto Loc = getBeginningOfIdentifier(AST, Pos, SM.getMainFileID());
|
|
|
|
auto Symbols = getSymbolAtPosition(AST, Loc);
|
|
|
|
|
|
|
|
// We traverse the AST to find references in the main file.
|
|
|
|
// TODO: should we handle macros, too?
|
2019-02-21 22:48:33 +08:00
|
|
|
auto MainFileRefs = findRefs(Symbols.Decls, AST);
|
2018-09-05 18:33:36 +08:00
|
|
|
for (const auto &Ref : MainFileRefs) {
|
|
|
|
Location Result;
|
2019-05-28 18:29:58 +08:00
|
|
|
Result.range = getTokenRange(AST.getASTContext(), Ref.Loc);
|
2018-11-28 18:30:42 +08:00
|
|
|
Result.uri = URIForFile::canonicalize(*MainFilePath, *MainFilePath);
|
2018-09-05 18:33:36 +08:00
|
|
|
Results.push_back(std::move(Result));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query the index for references from other files.
|
2019-01-15 02:11:09 +08:00
|
|
|
if (Index && Results.size() < Limit) {
|
|
|
|
RefsRequest Req;
|
|
|
|
Req.Limit = Limit;
|
|
|
|
|
2019-02-21 22:48:33 +08:00
|
|
|
for (const Decl *D : Symbols.Decls) {
|
2019-01-15 02:11:09 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
if (Req.IDs.empty())
|
|
|
|
return Results;
|
|
|
|
Index->refs(Req, [&](const Ref &R) {
|
|
|
|
auto LSPLoc = toLSPLocation(R.Location, *MainFilePath);
|
|
|
|
// Avoid indexed results for the main file - the AST is authoritative.
|
|
|
|
if (LSPLoc && LSPLoc->uri.file() != *MainFilePath)
|
|
|
|
Results.push_back(std::move(*LSPLoc));
|
|
|
|
});
|
2018-09-05 18:33:36 +08:00
|
|
|
}
|
2019-01-15 02:11:09 +08:00
|
|
|
if (Results.size() > Limit)
|
|
|
|
Results.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();
|
2018-11-28 00:40:46 +08:00
|
|
|
|
|
|
|
auto Loc = getBeginningOfIdentifier(AST, Pos, SM.getMainFileID());
|
|
|
|
auto Symbols = getSymbolAtPosition(AST, Loc);
|
|
|
|
|
|
|
|
std::vector<SymbolDetails> Results;
|
|
|
|
|
2019-02-21 22:48:33 +08:00
|
|
|
for (const Decl *D : Symbols.Decls) {
|
2018-11-28 00:40:46 +08:00
|
|
|
SymbolDetails NewSymbol;
|
2019-02-21 22:48:33 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
|
2018-11-28 00:40:46 +08:00
|
|
|
std::string QName = printQualifiedName(*ND);
|
|
|
|
std::tie(NewSymbol.containerName, NewSymbol.name) =
|
|
|
|
splitQualifiedName(QName);
|
|
|
|
|
|
|
|
if (NewSymbol.containerName.empty()) {
|
|
|
|
if (const auto *ParentND =
|
|
|
|
dyn_cast_or_null<NamedDecl>(ND->getDeclContext()))
|
|
|
|
NewSymbol.containerName = printQualifiedName(*ParentND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm::SmallString<32> USR;
|
2019-02-21 22:48:33 +08:00
|
|
|
if (!index::generateUSRForDecl(D, USR)) {
|
2018-11-28 00:40:46 +08:00
|
|
|
NewSymbol.USR = USR.str();
|
|
|
|
NewSymbol.ID = SymbolID(NewSymbol.USR);
|
|
|
|
}
|
|
|
|
Results.push_back(std::move(NewSymbol));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &Macro : Symbols.Macros) {
|
|
|
|
SymbolDetails NewMacro;
|
|
|
|
NewMacro.name = Macro.Name;
|
|
|
|
llvm::SmallString<32> USR;
|
2019-01-29 19:19:15 +08:00
|
|
|
if (!index::generateUSRForMacro(NewMacro.name,
|
|
|
|
Macro.Info->getDefinitionLoc(), SM, USR)) {
|
2018-11-28 00:40:46 +08:00
|
|
|
NewMacro.USR = USR.str();
|
|
|
|
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>
|
|
|
|
declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND) {
|
|
|
|
auto &SM = Ctx.getSourceManager();
|
|
|
|
|
|
|
|
SourceLocation NameLoc = findNameLoc(&ND);
|
|
|
|
// getFileLoc is a good choice for us, but we also need to make sure
|
|
|
|
// sourceLocToPosition won't switch files, so we call getSpellingLoc on top of
|
|
|
|
// that to make sure it does not switch files.
|
|
|
|
// FIXME: sourceLocToPosition should not switch files!
|
|
|
|
SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
|
|
|
|
SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
|
|
|
|
if (NameLoc.isInvalid() || BeginLoc.isInvalid() || EndLoc.isInvalid())
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
Position NameBegin = sourceLocToPosition(SM, NameLoc);
|
|
|
|
Position NameEnd = sourceLocToPosition(
|
|
|
|
SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
|
|
|
|
|
|
|
|
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();
|
|
|
|
THI.range =
|
|
|
|
Range{sourceLocToPosition(SM, BeginLoc), sourceLocToPosition(SM, EndLoc)};
|
|
|
|
THI.selectionRange = Range{NameBegin, NameEnd};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto FilePath =
|
|
|
|
getCanonicalPath(SM.getFileEntryForID(SM.getFileID(BeginLoc)), SM);
|
|
|
|
auto TUPath = getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
|
|
|
|
if (!FilePath || !TUPath)
|
|
|
|
return llvm::None; // Not useful without a uri.
|
|
|
|
THI.uri = URIForFile::canonicalize(*FilePath, *TUPath);
|
|
|
|
|
|
|
|
return THI;
|
|
|
|
}
|
|
|
|
|
2019-04-22 09:38:53 +08:00
|
|
|
using RecursionProtectionSet = llvm::SmallSet<const CXXRecordDecl *, 4>;
|
|
|
|
|
|
|
|
static Optional<TypeHierarchyItem>
|
|
|
|
getTypeAncestors(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
|
|
|
|
RecursionProtectionSet &RPSet) {
|
[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
|
|
|
Optional<TypeHierarchyItem> Result = declToTypeHierarchyItem(ASTCtx, CXXRD);
|
|
|
|
if (!Result)
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
Result->parents.emplace();
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
for (const CXXRecordDecl *ParentDecl : typeParents(&CXXRD)) {
|
|
|
|
if (Optional<TypeHierarchyItem> ParentSym =
|
2019-04-22 09:38:53 +08:00
|
|
|
getTypeAncestors(*ParentDecl, ASTCtx, RPSet)) {
|
[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
|
|
|
Result->parents->emplace_back(std::move(*ParentSym));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
|
2019-05-29 05:52:34 +08:00
|
|
|
SourceLocation SourceLocationBeg = getBeginningOfIdentifier(
|
|
|
|
AST, Pos, AST.getSourceManager().getMainFileID());
|
[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
|
|
|
IdentifiedSymbol Symbols = getSymbolAtPosition(AST, SourceLocationBeg);
|
|
|
|
if (Symbols.Decls.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Decl *D = Symbols.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD) {
|
|
|
|
std::vector<const CXXRecordDecl *> Result;
|
|
|
|
|
|
|
|
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,
|
|
|
|
TypeHierarchyDirection Direction) {
|
|
|
|
const CXXRecordDecl *CXXRD = findRecordTypeAt(AST, Pos);
|
|
|
|
if (!CXXRD)
|
|
|
|
return llvm::None;
|
|
|
|
|
2019-04-22 09:38:53 +08:00
|
|
|
RecursionProtectionSet RPSet;
|
[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
|
|
|
Optional<TypeHierarchyItem> Result =
|
2019-04-22 09:38:53 +08:00
|
|
|
getTypeAncestors(*CXXRD, AST.getASTContext(), RPSet);
|
|
|
|
|
[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): Resolve type descendants if direction is Children or Both,
|
|
|
|
// and ResolveLevels > 0.
|
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-05-29 18:01:00 +08:00
|
|
|
FormattedString HoverInfo::present() const {
|
|
|
|
FormattedString Output;
|
2019-05-28 18:29:58 +08:00
|
|
|
if (NamespaceScope) {
|
2019-05-29 18:01:00 +08:00
|
|
|
Output.appendText("Declared in");
|
2019-05-28 18:29:58 +08:00
|
|
|
// Drop trailing "::".
|
|
|
|
if (!LocalScope.empty())
|
2019-05-29 18:01:00 +08:00
|
|
|
Output.appendInlineCode(llvm::StringRef(LocalScope).drop_back(2));
|
2019-05-28 18:29:58 +08:00
|
|
|
else if (NamespaceScope->empty())
|
2019-05-29 18:01:00 +08:00
|
|
|
Output.appendInlineCode("global namespace");
|
2019-05-28 18:29:58 +08:00
|
|
|
else
|
2019-05-29 18:01:00 +08:00
|
|
|
Output.appendInlineCode(llvm::StringRef(*NamespaceScope).drop_back(2));
|
2019-05-28 18:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Definition.empty()) {
|
2019-05-29 18:01:00 +08:00
|
|
|
Output.appendCodeBlock(Definition);
|
2019-05-28 18:29:58 +08:00
|
|
|
} else {
|
|
|
|
// Builtin types
|
2019-05-29 18:01:00 +08:00
|
|
|
Output.appendCodeBlock(Name);
|
2019-05-28 18:29:58 +08:00
|
|
|
}
|
2019-05-29 18:01:00 +08:00
|
|
|
return Output;
|
2019-05-28 18:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
|
|
|
|
const HoverInfo::Param &P) {
|
|
|
|
std::vector<llvm::StringRef> Output;
|
|
|
|
if (P.Type)
|
|
|
|
Output.push_back(*P.Type);
|
|
|
|
if (P.Name)
|
|
|
|
Output.push_back(*P.Name);
|
|
|
|
OS << llvm::join(Output, " ");
|
|
|
|
if (P.Default)
|
|
|
|
OS << " = " << *P.Default;
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|