2018-08-15 00:03:32 +08:00
|
|
|
//===--- XRefs.cpp -----------------------------------------------*- C++-*-===//
|
2017-12-20 01:06:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
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"
|
2018-01-29 23:37:46 +08:00
|
|
|
#include "Logger.h"
|
2018-02-21 10:39:08 +08:00
|
|
|
#include "SourceCode.h"
|
2018-01-29 23:37:46 +08:00
|
|
|
#include "URI.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"
|
2018-07-03 00:28:34 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2017-12-20 01:06:07 +08:00
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
|
|
|
#include "clang/Index/IndexingAction.h"
|
2018-04-30 23:24:17 +08:00
|
|
|
#include "clang/Index/USRGeneration.h"
|
2018-02-14 01:47:16 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-09-05 19:53:07 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
using namespace llvm;
|
2017-12-20 01:06:07 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2018-01-12 22:21:10 +08:00
|
|
|
// Get the definition from a given declaration `D`.
|
|
|
|
// Return nullptr if no definition is found, or the declaration type of `D` is
|
|
|
|
// not supported.
|
2018-07-26 20:05:31 +08:00
|
|
|
const Decl *getDefinition(const Decl *D) {
|
2018-01-12 22:21:10 +08:00
|
|
|
assert(D);
|
|
|
|
if (const auto *TD = dyn_cast<TagDecl>(D))
|
|
|
|
return TD->getDefinition();
|
|
|
|
else if (const auto *VD = dyn_cast<VarDecl>(D))
|
|
|
|
return VD->getDefinition();
|
|
|
|
else if (const auto *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
return FD->getDefinition();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
// HintPath is used to resolve the path of URI.
|
|
|
|
// FIXME: figure out a good home for it, and share the implementation with
|
|
|
|
// FindSymbols.
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<Location> toLSPLocation(const SymbolLocation &Loc,
|
|
|
|
StringRef HintPath) {
|
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) {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
log("Could not parse URI: {0}", Loc.FileURI);
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
|
|
|
auto Path = URI::resolve(*Uri, HintPath);
|
|
|
|
if (!Path) {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
log("Could not resolve URI: {0}", Loc.FileURI);
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
|
|
|
Location LSPLoc;
|
|
|
|
LSPLoc.uri = URIForFile(*Path);
|
[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;
|
|
|
|
}
|
|
|
|
|
[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 {
|
|
|
|
StringRef Name;
|
|
|
|
const MacroInfo *Info;
|
|
|
|
};
|
|
|
|
|
2018-09-05 20:00:15 +08:00
|
|
|
struct DeclInfo {
|
|
|
|
const Decl *D;
|
|
|
|
// Indicates the declaration is referenced by an explicit AST node.
|
|
|
|
bool IsReferencedExplicitly = false;
|
|
|
|
};
|
|
|
|
|
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;
|
2018-09-05 20:00:15 +08:00
|
|
|
// The value of the map indicates whether the declaration has been referenced
|
|
|
|
// explicitly in the code.
|
|
|
|
// True means the declaration is explicitly referenced at least once; false
|
|
|
|
// otherwise.
|
2018-10-20 23:30:37 +08:00
|
|
|
DenseMap<const Decl *, bool> 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) {}
|
|
|
|
|
2018-09-05 20:00:15 +08:00
|
|
|
// Get all DeclInfo of the found declarations.
|
|
|
|
// The results are sorted by "IsReferencedExplicitly" and declaration
|
|
|
|
// location.
|
|
|
|
std::vector<DeclInfo> getFoundDecls() const {
|
|
|
|
std::vector<DeclInfo> Result;
|
|
|
|
for (auto It : Decls) {
|
|
|
|
Result.emplace_back();
|
|
|
|
Result.back().D = It.first;
|
|
|
|
Result.back().IsReferencedExplicitly = It.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort results. Declarations being referenced explicitly come first.
|
2018-10-07 22:49:41 +08:00
|
|
|
llvm::sort(Result, [](const DeclInfo &L, const DeclInfo &R) {
|
|
|
|
if (L.IsReferencedExplicitly != R.IsReferencedExplicitly)
|
|
|
|
return L.IsReferencedExplicitly > R.IsReferencedExplicitly;
|
|
|
|
return L.D->getBeginLoc() < R.D->getBeginLoc();
|
|
|
|
});
|
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,
|
2018-04-09 22:28:52 +08:00
|
|
|
ArrayRef<index::SymbolRelation> Relations,
|
|
|
|
SourceLocation Loc,
|
2017-12-20 01:06:07 +08:00
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
|
2018-04-09 22:28:52 +08:00
|
|
|
if (Loc == SearchedLocation) {
|
2018-09-05 20:00:15 +08:00
|
|
|
// Check whether the E has an implicit AST node (e.g. ImplicitCastExpr).
|
|
|
|
auto hasImplicitExpr = [](const Expr *E) {
|
|
|
|
if (!E || E->child_begin() == E->child_end())
|
|
|
|
return false;
|
|
|
|
// Use the first child is good enough for most cases -- normally the
|
|
|
|
// expression returned by handleDeclOccurence contains exactly one
|
|
|
|
// child expression.
|
|
|
|
const auto *FirstChild = *E->child_begin();
|
2018-10-20 23:30:37 +08:00
|
|
|
return isa<ExprWithCleanups>(FirstChild) ||
|
|
|
|
isa<MaterializeTemporaryExpr>(FirstChild) ||
|
|
|
|
isa<CXXBindTemporaryExpr>(FirstChild) ||
|
|
|
|
isa<ImplicitCastExpr>(FirstChild);
|
2018-09-05 20:00:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool IsExplicit = !hasImplicitExpr(ASTNode.OrigE);
|
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)) {
|
2018-09-05 20:00:15 +08:00
|
|
|
Decls[Def] |= IsExplicit;
|
2018-01-12 22:21:10 +08:00
|
|
|
} else {
|
|
|
|
// Couldn't find a definition, fall back to use `D`.
|
2018-09-05 20:00:15 +08:00
|
|
|
Decls[D] |= IsExplicit;
|
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 {
|
2018-09-05 20:00:15 +08:00
|
|
|
std::vector<DeclInfo> 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;
|
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
|
|
|
}
|
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
Range getTokenRange(ParsedAST &AST, SourceLocation TokLoc) {
|
2017-12-20 01:06:07 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
2018-09-05 18:33:36 +08:00
|
|
|
SourceLocation LocEnd = Lexer::getLocForEndOfToken(
|
|
|
|
TokLoc, 0, SourceMgr, AST.getASTContext().getLangOpts());
|
|
|
|
return {sourceLocToPosition(SourceMgr, TokLoc),
|
|
|
|
sourceLocToPosition(SourceMgr, LocEnd)};
|
|
|
|
}
|
2017-12-20 01:06:07 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<Location> makeLocation(ParsedAST &AST, SourceLocation TokLoc) {
|
2018-09-05 18:33:36 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
|
|
|
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;
|
[clangd] Avoid duplicates in findDefinitions response
Summary:
When compile_commands.json contains some source files expressed as
relative paths, we can get duplicate responses to findDefinitions. The
responses only differ by the URI, which are different versions of the
same file:
"result": [
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/build/../src/first.h"
},
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/src/first.h"
}
]
In getAbsoluteFilePath, we try to obtain the realpath of the FileEntry
by calling tryGetRealPathName. However, this can fail and return an
empty string. It may be bug a bug in clang, but in any case we should
fall back to computing it ourselves if it happens.
I changed getAbsoluteFilePath so that if tryGetRealPathName succeeds, we
return right away (a real path is always absolute). Otherwise, we try
to build an absolute path, as we did before, but we also call
VFS->getRealPath to make sure to get the canonical path (e.g. without
any ".." in it).
Reviewers: malaperle
Subscribers: hokein, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D48687
llvm-svn: 339483
2018-08-11 06:27:53 +08:00
|
|
|
auto FilePath = getRealPath(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-04-30 23:24:17 +08:00
|
|
|
L.uri = URIForFile(*FilePath);
|
2018-09-05 18:33:36 +08:00
|
|
|
L.range = getTokenRange(AST, TokLoc);
|
2017-12-20 01:06:07 +08:00
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
std::vector<Location> findDefinitions(ParsedAST &AST, Position Pos,
|
|
|
|
const SymbolIndex *Index) {
|
2017-12-20 01:06:07 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
|
|
|
|
2018-03-09 00:28:12 +08:00
|
|
|
std::vector<Location> Result;
|
|
|
|
// Handle goto definition for #include.
|
2018-07-03 16:09:29 +08:00
|
|
|
for (auto &Inc : AST.getIncludeStructure().MainFileIncludes) {
|
2018-08-23 23:55:27 +08:00
|
|
|
if (!Inc.Resolved.empty() && Inc.R.start.line == Pos.line)
|
2018-05-14 20:19:16 +08:00
|
|
|
Result.push_back(Location{URIForFile{Inc.Resolved}, {}});
|
2018-03-09 00:28:12 +08:00
|
|
|
}
|
|
|
|
if (!Result.empty())
|
|
|
|
return Result;
|
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
// Identified symbols at a specific position.
|
2018-07-03 16:09:29 +08:00
|
|
|
SourceLocation SourceLocationBeg =
|
|
|
|
getBeginningOfIdentifier(AST, Pos, SourceMgr.getMainFileID());
|
2018-04-30 23:24:17 +08:00
|
|
|
auto Symbols = getSymbolAtPosition(AST, SourceLocationBeg);
|
2017-12-20 01:06:07 +08:00
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
for (auto Item : Symbols.Macros) {
|
|
|
|
auto Loc = Item.Info->getDefinitionLoc();
|
2018-09-05 18:33:36 +08:00
|
|
|
auto L = makeLocation(AST, Loc);
|
2017-12-20 01:06:07 +08:00
|
|
|
if (L)
|
|
|
|
Result.push_back(*L);
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
// Declaration and definition are different terms in C-family languages, and
|
|
|
|
// LSP only defines the "GoToDefinition" specification, so we try to perform
|
|
|
|
// the "most sensible" GoTo operation:
|
|
|
|
//
|
|
|
|
// - We use the location from AST and index (if available) to provide the
|
|
|
|
// final results. When there are duplicate results, we prefer AST over
|
|
|
|
// index because AST is more up-to-date.
|
|
|
|
//
|
|
|
|
// - For each symbol, we will return a location of the canonical declaration
|
|
|
|
// (e.g. function declaration in header), and a location of definition if
|
|
|
|
// they are available.
|
|
|
|
//
|
|
|
|
// So the work flow:
|
|
|
|
//
|
|
|
|
// 1. Identify the symbols being search for by traversing the AST.
|
|
|
|
// 2. Populate one of the locations with the AST location.
|
|
|
|
// 3. Use the AST information to query the index, and populate the index
|
|
|
|
// location (if available).
|
|
|
|
// 4. Return all populated locations for all symbols, definition first (
|
|
|
|
// which we think is the users wants most often).
|
|
|
|
struct CandidateLocation {
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<Location> Def;
|
|
|
|
Optional<Location> Decl;
|
2018-04-30 23:24:17 +08:00
|
|
|
};
|
2018-09-05 20:00:15 +08:00
|
|
|
// We respect the order in Symbols.Decls.
|
2018-10-20 23:30:37 +08:00
|
|
|
SmallVector<CandidateLocation, 8> ResultCandidates;
|
|
|
|
DenseMap<SymbolID, size_t> CandidatesIndex;
|
2018-04-30 23:24:17 +08:00
|
|
|
|
|
|
|
// Emit all symbol locations (declaration or definition) from AST.
|
2018-09-05 20:00:15 +08:00
|
|
|
for (const DeclInfo &DI : Symbols.Decls) {
|
|
|
|
const Decl *D = DI.D;
|
2018-04-30 23:24:17 +08:00
|
|
|
// Fake key for symbols don't have USR (no SymbolID).
|
|
|
|
// Ideally, there should be a USR for each identified symbols. Symbols
|
|
|
|
// without USR are rare and unimportant cases, we use the a fake holder to
|
|
|
|
// minimize the invasiveness of these cases.
|
|
|
|
SymbolID Key("");
|
|
|
|
if (auto ID = getSymbolID(D))
|
|
|
|
Key = *ID;
|
|
|
|
|
2018-09-05 20:00:15 +08:00
|
|
|
auto R = CandidatesIndex.try_emplace(Key, ResultCandidates.size());
|
|
|
|
if (R.second) // new entry
|
|
|
|
ResultCandidates.emplace_back();
|
|
|
|
auto &Candidate = ResultCandidates[R.first->second];
|
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
auto Loc = findNameLoc(D);
|
2018-09-05 18:33:36 +08:00
|
|
|
auto L = makeLocation(AST, Loc);
|
2018-04-30 23:24:17 +08:00
|
|
|
// The declaration in the identified symbols is a definition if possible
|
|
|
|
// otherwise it is declaration.
|
2018-07-26 20:05:31 +08:00
|
|
|
bool IsDef = getDefinition(D) == D;
|
2018-04-30 23:24:17 +08:00
|
|
|
// Populate one of the slots with location for the AST.
|
|
|
|
if (!IsDef)
|
|
|
|
Candidate.Decl = L;
|
|
|
|
else
|
|
|
|
Candidate.Def = L;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Index) {
|
|
|
|
LookupRequest QueryRequest;
|
|
|
|
// Build request for index query, using SymbolID.
|
2018-09-05 20:00:15 +08:00
|
|
|
for (auto It : CandidatesIndex)
|
2018-04-30 23:24:17 +08:00
|
|
|
QueryRequest.IDs.insert(It.first);
|
|
|
|
std::string HintPath;
|
|
|
|
const FileEntry *FE =
|
|
|
|
SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
|
[clangd] Avoid duplicates in findDefinitions response
Summary:
When compile_commands.json contains some source files expressed as
relative paths, we can get duplicate responses to findDefinitions. The
responses only differ by the URI, which are different versions of the
same file:
"result": [
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/build/../src/first.h"
},
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/src/first.h"
}
]
In getAbsoluteFilePath, we try to obtain the realpath of the FileEntry
by calling tryGetRealPathName. However, this can fail and return an
empty string. It may be bug a bug in clang, but in any case we should
fall back to computing it ourselves if it happens.
I changed getAbsoluteFilePath so that if tryGetRealPathName succeeds, we
return right away (a real path is always absolute). Otherwise, we try
to build an absolute path, as we did before, but we also call
VFS->getRealPath to make sure to get the canonical path (e.g. without
any ".." in it).
Reviewers: malaperle
Subscribers: hokein, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D48687
llvm-svn: 339483
2018-08-11 06:27:53 +08:00
|
|
|
if (auto Path = getRealPath(FE, SourceMgr))
|
2018-04-30 23:24:17 +08:00
|
|
|
HintPath = *Path;
|
|
|
|
// Query the index and populate the empty slot.
|
2018-09-05 20:00:15 +08:00
|
|
|
Index->lookup(QueryRequest, [&HintPath, &ResultCandidates,
|
|
|
|
&CandidatesIndex](const Symbol &Sym) {
|
|
|
|
auto It = CandidatesIndex.find(Sym.ID);
|
|
|
|
assert(It != CandidatesIndex.end());
|
|
|
|
auto &Value = ResultCandidates[It->second];
|
|
|
|
|
|
|
|
if (!Value.Def)
|
|
|
|
Value.Def = toLSPLocation(Sym.Definition, HintPath);
|
|
|
|
if (!Value.Decl)
|
|
|
|
Value.Decl = toLSPLocation(Sym.CanonicalDeclaration, HintPath);
|
|
|
|
});
|
2018-04-30 23:24:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the results, definition first.
|
2018-09-05 20:00:15 +08:00
|
|
|
for (const auto &Candidate : ResultCandidates) {
|
2018-04-30 23:24:17 +08:00
|
|
|
if (Candidate.Def)
|
|
|
|
Result.push_back(*Candidate.Def);
|
|
|
|
if (Candidate.Decl &&
|
|
|
|
Candidate.Decl != Candidate.Def) // Decl and Def might be the same
|
|
|
|
Result.push_back(*Candidate.Decl);
|
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,
|
2018-04-09 22:28:52 +08:00
|
|
|
ArrayRef<index::SymbolRelation> Relations,
|
|
|
|
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:
|
2018-10-20 23:30:37 +08:00
|
|
|
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;
|
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) {
|
|
|
|
const SourceManager &SM = AST.getASTContext().getSourceManager();
|
|
|
|
auto Symbols = getSymbolAtPosition(
|
|
|
|
AST, getBeginningOfIdentifier(AST, Pos, SM.getMainFileID()));
|
2018-09-05 20:00:15 +08:00
|
|
|
std::vector<const Decl *> TargetDecls;
|
|
|
|
for (const DeclInfo &DI : Symbols.Decls) {
|
|
|
|
TargetDecls.push_back(DI.D);
|
|
|
|
}
|
|
|
|
auto References = findRefs(TargetDecls, 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;
|
|
|
|
DH.range = getTokenRange(AST, Ref.Loc);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a string representation (e.g. "class MyNamespace::MyClass") of
|
|
|
|
/// the type declaration \p TD.
|
2018-07-26 20:05:31 +08:00
|
|
|
static std::string typeDeclToString(const TypeDecl *TD) {
|
[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
|
|
|
QualType Type = TD->getASTContext().getTypeDeclType(TD);
|
|
|
|
|
|
|
|
PrintingPolicy Policy =
|
2018-07-26 20:05:31 +08:00
|
|
|
printingPolicyForDecls(TD->getASTContext().getPrintingPolicy());
|
[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::string Name;
|
2018-10-20 23:30:37 +08:00
|
|
|
raw_string_ostream Stream(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
|
|
|
Type.print(Stream, Policy);
|
|
|
|
|
|
|
|
return Stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a string representation (e.g. "namespace ns1::ns2") of
|
|
|
|
/// the named declaration \p ND.
|
2018-07-26 20:05:31 +08:00
|
|
|
static std::string namedDeclQualifiedName(const NamedDecl *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
|
|
|
StringRef Prefix) {
|
|
|
|
PrintingPolicy Policy =
|
2018-07-26 20:05:31 +08:00
|
|
|
printingPolicyForDecls(ND->getASTContext().getPrintingPolicy());
|
[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::string Name;
|
2018-10-20 23:30:37 +08:00
|
|
|
raw_string_ostream Stream(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
|
|
|
Stream << Prefix << ' ';
|
|
|
|
ND->printQualifiedName(Stream, Policy);
|
|
|
|
|
|
|
|
return Stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a declaration \p D, return a human-readable string representing the
|
|
|
|
/// scope in which it is declared. If the declaration is in the global scope,
|
|
|
|
/// return the string "global namespace".
|
2018-10-20 23:30:37 +08:00
|
|
|
static Optional<std::string> getScopeName(const Decl *D) {
|
[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 DeclContext *DC = D->getDeclContext();
|
|
|
|
|
2018-02-19 17:31:26 +08:00
|
|
|
if (isa<TranslationUnitDecl>(DC))
|
[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
|
|
|
return std::string("global namespace");
|
|
|
|
if (const TypeDecl *TD = dyn_cast<TypeDecl>(DC))
|
2018-07-26 20:05:31 +08:00
|
|
|
return typeDeclToString(TD);
|
[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
|
|
|
else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
|
2018-07-26 20:05:31 +08:00
|
|
|
return namedDeclQualifiedName(ND, "namespace");
|
[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
|
|
|
else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
|
2018-07-26 20:05:31 +08:00
|
|
|
return namedDeclQualifiedName(FD, "function");
|
[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-10-20 23:30:37 +08:00
|
|
|
return None;
|
[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.
|
|
|
|
static Hover getHoverContents(const Decl *D) {
|
|
|
|
Hover H;
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<std::string> NamedScope = getScopeName(D);
|
[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 the "Declared in" section.
|
|
|
|
if (NamedScope) {
|
|
|
|
assert(!NamedScope->empty());
|
|
|
|
|
2018-02-17 07:12:26 +08:00
|
|
|
H.contents.value += "Declared in ";
|
|
|
|
H.contents.value += *NamedScope;
|
|
|
|
H.contents.value += "\n\n";
|
[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
|
|
|
}
|
|
|
|
|
|
|
|
// We want to include the template in the Hover.
|
|
|
|
if (TemplateDecl *TD = D->getDescribedTemplate())
|
|
|
|
D = TD;
|
|
|
|
|
|
|
|
std::string DeclText;
|
2018-10-20 23:30:37 +08:00
|
|
|
raw_string_ostream OS(DeclText);
|
[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 =
|
2018-07-26 20:05:31 +08:00
|
|
|
printingPolicyForDecls(D->getASTContext().getPrintingPolicy());
|
[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
|
|
|
|
|
|
|
D->print(OS, Policy);
|
|
|
|
|
|
|
|
OS.flush();
|
|
|
|
|
2018-02-17 07:12:26 +08:00
|
|
|
H.contents.value += DeclText;
|
[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
|
|
|
return H;
|
|
|
|
}
|
|
|
|
|
2018-07-03 00:28:34 +08:00
|
|
|
/// Generate a \p Hover object given the type \p T.
|
|
|
|
static Hover getHoverContents(QualType T, ASTContext &ASTCtx) {
|
|
|
|
Hover H;
|
|
|
|
std::string TypeText;
|
2018-10-20 23:30:37 +08:00
|
|
|
raw_string_ostream OS(TypeText);
|
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);
|
|
|
|
OS.flush();
|
|
|
|
H.contents.value += TypeText;
|
|
|
|
return 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
|
|
|
/// Generate a \p Hover object given the macro \p MacroInf.
|
|
|
|
static Hover getHoverContents(StringRef MacroName) {
|
|
|
|
Hover H;
|
|
|
|
|
2018-02-17 07:12:26 +08:00
|
|
|
H.contents.value = "#define ";
|
|
|
|
H.contents.value += MacroName;
|
[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
|
|
|
|
|
|
|
return H;
|
|
|
|
}
|
|
|
|
|
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;
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<QualType> DeducedType;
|
2018-07-03 00:28:34 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
DeducedTypeVisitor(SourceLocation SearchedLocation)
|
|
|
|
: SearchedLocation(SearchedLocation) {}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<QualType> getDeducedType() { return DeducedType; }
|
2018-07-03 00:28:34 +08:00
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
if (!AT->getDeducedType().isNull())
|
|
|
|
DeducedType = AT->getDeducedType();
|
2018-07-03 00:28:34 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle auto return types:
|
|
|
|
//- auto foo() {}
|
|
|
|
//- auto& foo() {}
|
|
|
|
//- 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();
|
|
|
|
} else {
|
|
|
|
// auto in a trailing return type just points to a DecltypeType and
|
|
|
|
// getContainedAutoType does not unwrap it.
|
|
|
|
const DecltypeType *DT = dyn_cast<DecltypeType>(D->getReturnType());
|
2018-07-03 00:28:34 +08:00
|
|
|
if (!DT->getUnderlyingType().isNull())
|
|
|
|
DeducedType = DT->getUnderlyingType();
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
DT = dyn_cast<DecltypeType>(DeducedType->getTypePtr());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/// Retrieves the deduced type at a given location (auto, decltype).
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<QualType> getDeducedType(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)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
AST.getPreprocessor().LookUpIdentifierInfo(Tok);
|
|
|
|
if (!(Tok.is(tok::kw_auto) || Tok.is(tok::kw_decltype)))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
DeducedTypeVisitor V(SourceLocationBeg);
|
|
|
|
for (Decl *D : AST.getLocalTopLevelDecls())
|
|
|
|
V.TraverseDecl(D);
|
|
|
|
return V.getDeducedType();
|
|
|
|
}
|
|
|
|
|
2018-06-04 18:37:16 +08:00
|
|
|
Optional<Hover> getHover(ParsedAST &AST, Position Pos) {
|
[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 SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
SourceLocation SourceLocationBeg =
|
|
|
|
getBeginningOfIdentifier(AST, Pos, SourceMgr.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())
|
|
|
|
return getHoverContents(Symbols.Macros[0].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
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
if (!Symbols.Decls.empty())
|
2018-09-05 20:00:15 +08:00
|
|
|
return getHoverContents(Symbols.Decls[0].D);
|
[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
|
|
|
auto DeducedType = getDeducedType(AST, SourceLocationBeg);
|
|
|
|
if (DeducedType && !DeducedType->isNull())
|
|
|
|
return getHoverContents(*DeducedType, AST.getASTContext());
|
|
|
|
|
2018-06-04 18:37:16 +08:00
|
|
|
return None;
|
[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,
|
|
|
|
const SymbolIndex *Index) {
|
|
|
|
std::vector<Location> Results;
|
|
|
|
const SourceManager &SM = AST.getASTContext().getSourceManager();
|
|
|
|
auto MainFilePath = getRealPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
|
|
|
|
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);
|
|
|
|
|
2018-09-05 20:00:15 +08:00
|
|
|
std::vector<const Decl *> TargetDecls;
|
|
|
|
for (const DeclInfo &DI : Symbols.Decls) {
|
|
|
|
if (DI.IsReferencedExplicitly)
|
|
|
|
TargetDecls.push_back(DI.D);
|
|
|
|
}
|
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
// We traverse the AST to find references in the main file.
|
|
|
|
// TODO: should we handle macros, too?
|
2018-09-05 20:00:15 +08:00
|
|
|
auto MainFileRefs = findRefs(TargetDecls, AST);
|
2018-09-05 18:33:36 +08:00
|
|
|
for (const auto &Ref : MainFileRefs) {
|
|
|
|
Location Result;
|
|
|
|
Result.range = getTokenRange(AST, Ref.Loc);
|
|
|
|
Result.uri = URIForFile(*MainFilePath);
|
|
|
|
Results.push_back(std::move(Result));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query the index for references from other files.
|
|
|
|
if (!Index)
|
|
|
|
return Results;
|
|
|
|
RefsRequest Req;
|
2018-09-05 20:00:15 +08:00
|
|
|
for (const Decl *D : TargetDecls) {
|
2018-09-05 18:33:36 +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, /*HintPath=*/*MainFilePath);
|
|
|
|
// Avoid indexed results for the main file - the AST is authoritative.
|
|
|
|
if (LSPLoc && LSPLoc->uri.file() != *MainFilePath)
|
|
|
|
Results.push_back(std::move(*LSPLoc));
|
|
|
|
});
|
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|