2017-12-20 01:06:07 +08:00
|
|
|
//===--- XRefs.cpp ----------------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
#include "XRefs.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"
|
2017-12-20 01:06:07 +08:00
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
|
|
|
#include "clang/Index/IndexingAction.h"
|
2018-02-14 01:47:16 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-12-20 01:06:07 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
using namespace llvm;
|
|
|
|
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-02-21 00:57:47 +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;
|
|
|
|
}
|
|
|
|
|
[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;
|
|
|
|
};
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
/// Finds declarations locations that a given source location refers to.
|
|
|
|
class DeclarationAndMacrosFinder : public index::IndexDataConsumer {
|
|
|
|
std::vector<const Decl *> Decls;
|
[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;
|
2017-12-20 01:06:07 +08:00
|
|
|
const SourceLocation &SearchedLocation;
|
|
|
|
const ASTContext &AST;
|
|
|
|
Preprocessor &PP;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DeclarationAndMacrosFinder(raw_ostream &OS,
|
|
|
|
const SourceLocation &SearchedLocation,
|
|
|
|
ASTContext &AST, Preprocessor &PP)
|
|
|
|
: SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
|
|
|
|
|
|
|
|
std::vector<const Decl *> takeDecls() {
|
|
|
|
// Don't keep the same declaration multiple times.
|
|
|
|
// This can happen when nodes in the AST are visited twice.
|
|
|
|
std::sort(Decls.begin(), Decls.end());
|
|
|
|
auto Last = std::unique(Decls.begin(), Decls.end());
|
|
|
|
Decls.erase(Last, Decls.end());
|
|
|
|
return std::move(Decls);
|
|
|
|
}
|
|
|
|
|
[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.
|
[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::sort(MacroInfos.begin(), MacroInfos.end(),
|
|
|
|
[](const MacroDecl &Left, const MacroDecl &Right) {
|
|
|
|
return Left.Info < Right.Info;
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
ArrayRef<index::SymbolRelation> Relations, FileID FID,
|
|
|
|
unsigned Offset,
|
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
|
2018-01-12 22:21:10 +08:00
|
|
|
if (isSearchedLocation(FID, Offset)) {
|
|
|
|
// 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-02-21 00:57:47 +08:00
|
|
|
if (const auto *Def = GetDefinition(D)) {
|
2018-01-12 22:21:10 +08:00
|
|
|
Decls.push_back(Def);
|
|
|
|
} else {
|
|
|
|
// Couldn't find a definition, fall back to use `D`.
|
|
|
|
Decls.push_back(D);
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 01:06:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool isSearchedLocation(FileID FID, unsigned Offset) const {
|
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
|
|
|
return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
|
|
|
|
SourceMgr.getFileID(SearchedLocation) == FID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void finish() override {
|
|
|
|
// Also handle possible macro at the searched location.
|
|
|
|
Token Result;
|
|
|
|
auto &Mgr = AST.getSourceManager();
|
|
|
|
if (!Lexer::getRawToken(SearchedLocation, Result, Mgr, AST.getLangOpts(),
|
|
|
|
false)) {
|
|
|
|
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});
|
2017-12-20 01:06:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::Optional<Location>
|
|
|
|
getDeclarationLocation(ParsedAST &AST, const SourceRange &ValSourceRange) {
|
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
|
|
|
const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
|
|
|
|
SourceLocation LocStart = ValSourceRange.getBegin();
|
|
|
|
|
|
|
|
const FileEntry *F =
|
|
|
|
SourceMgr.getFileEntryForID(SourceMgr.getFileID(LocStart));
|
|
|
|
if (!F)
|
|
|
|
return llvm::None;
|
|
|
|
SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(), 0,
|
|
|
|
SourceMgr, LangOpts);
|
2018-02-21 10:39:08 +08:00
|
|
|
Position Begin = sourceLocToPosition(SourceMgr, LocStart);
|
|
|
|
Position End = sourceLocToPosition(SourceMgr, LocEnd);
|
2017-12-20 01:06:07 +08:00
|
|
|
Range R = {Begin, End};
|
|
|
|
Location L;
|
|
|
|
|
2018-02-14 01:47:16 +08:00
|
|
|
SmallString<64> FilePath = F->tryGetRealPathName();
|
2017-12-20 01:06:07 +08:00
|
|
|
if (FilePath.empty())
|
|
|
|
FilePath = F->getName();
|
2018-02-14 01:47:16 +08:00
|
|
|
if (!llvm::sys::path::is_absolute(FilePath)) {
|
|
|
|
if (!SourceMgr.getFileManager().makeAbsolutePath(FilePath)) {
|
|
|
|
log("Could not turn relative path to absolute: " + FilePath);
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 20:20:47 +08:00
|
|
|
L.uri = URIForFile(FilePath.str());
|
2017-12-20 01:06:07 +08:00
|
|
|
L.range = R;
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
std::vector<Location> findDefinitions(ParsedAST &AST, Position Pos) {
|
2017-12-20 01:06:07 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
|
|
|
const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
|
|
|
|
if (!FE)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
|
|
|
|
|
|
|
|
auto DeclMacrosFinder = std::make_shared<DeclarationAndMacrosFinder>(
|
|
|
|
llvm::errs(), SourceLocationBeg, AST.getASTContext(),
|
|
|
|
AST.getPreprocessor());
|
|
|
|
index::IndexingOptions IndexOpts;
|
|
|
|
IndexOpts.SystemSymbolFilter =
|
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::All;
|
|
|
|
IndexOpts.IndexFunctionLocals = true;
|
|
|
|
|
|
|
|
indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
|
|
|
|
DeclMacrosFinder, IndexOpts);
|
|
|
|
|
|
|
|
std::vector<const Decl *> Decls = DeclMacrosFinder->takeDecls();
|
[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 = DeclMacrosFinder->takeMacroInfos();
|
2017-12-20 01:06:07 +08:00
|
|
|
std::vector<Location> Result;
|
|
|
|
|
|
|
|
for (auto Item : Decls) {
|
|
|
|
auto L = getDeclarationLocation(AST, Item->getSourceRange());
|
|
|
|
if (L)
|
|
|
|
Result.push_back(*L);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto Item : MacroInfos) {
|
[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
|
|
|
SourceRange SR(Item.Info->getDefinitionLoc(),
|
|
|
|
Item.Info->getDefinitionEndLoc());
|
2017-12-20 01:06:07 +08:00
|
|
|
auto L = getDeclarationLocation(AST, SR);
|
|
|
|
if (L)
|
|
|
|
Result.push_back(*L);
|
|
|
|
}
|
|
|
|
|
2018-02-21 10:39:08 +08:00
|
|
|
/// Process targets for paths inside #include directive.
|
|
|
|
for (auto &IncludeLoc : AST.getInclusionLocations()) {
|
|
|
|
Range R = IncludeLoc.first;
|
|
|
|
Position Pos = sourceLocToPosition(SourceMgr, SourceLocationBeg);
|
|
|
|
|
|
|
|
if (R.contains(Pos))
|
|
|
|
Result.push_back(Location{URIForFile{IncludeLoc.second}, {}});
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Finds document highlights that a given list of declarations refers to.
|
|
|
|
class DocumentHighlightsFinder : public index::IndexDataConsumer {
|
|
|
|
std::vector<const Decl *> &Decls;
|
|
|
|
std::vector<DocumentHighlight> DocumentHighlights;
|
|
|
|
const ASTContext &AST;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DocumentHighlightsFinder(raw_ostream &OS, ASTContext &AST, Preprocessor &PP,
|
|
|
|
std::vector<const Decl *> &Decls)
|
|
|
|
: Decls(Decls), AST(AST) {}
|
|
|
|
std::vector<DocumentHighlight> takeHighlights() {
|
|
|
|
// Don't keep the same highlight multiple times.
|
|
|
|
// This can happen when nodes in the AST are visited twice.
|
|
|
|
std::sort(DocumentHighlights.begin(), DocumentHighlights.end());
|
|
|
|
auto Last =
|
|
|
|
std::unique(DocumentHighlights.begin(), DocumentHighlights.end());
|
|
|
|
DocumentHighlights.erase(Last, DocumentHighlights.end());
|
|
|
|
return std::move(DocumentHighlights);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
|
|
|
|
ArrayRef<index::SymbolRelation> Relations, FileID FID,
|
|
|
|
unsigned Offset,
|
|
|
|
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
|
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
|
|
|
if (SourceMgr.getMainFileID() != FID ||
|
|
|
|
std::find(Decls.begin(), Decls.end(), D) == Decls.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SourceLocation End;
|
|
|
|
const LangOptions &LangOpts = AST.getLangOpts();
|
|
|
|
SourceLocation StartOfFileLoc = SourceMgr.getLocForStartOfFile(FID);
|
|
|
|
SourceLocation HightlightStartLoc = StartOfFileLoc.getLocWithOffset(Offset);
|
|
|
|
End =
|
|
|
|
Lexer::getLocForEndOfToken(HightlightStartLoc, 0, SourceMgr, LangOpts);
|
|
|
|
SourceRange SR(HightlightStartLoc, End);
|
|
|
|
|
|
|
|
DocumentHighlightKind Kind = DocumentHighlightKind::Text;
|
|
|
|
if (static_cast<index::SymbolRoleSet>(index::SymbolRole::Write) & Roles)
|
|
|
|
Kind = DocumentHighlightKind::Write;
|
|
|
|
else if (static_cast<index::SymbolRoleSet>(index::SymbolRole::Read) & Roles)
|
|
|
|
Kind = DocumentHighlightKind::Read;
|
|
|
|
|
|
|
|
DocumentHighlights.push_back(getDocumentHighlight(SR, Kind));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DocumentHighlight getDocumentHighlight(SourceRange SR,
|
|
|
|
DocumentHighlightKind Kind) {
|
|
|
|
const SourceManager &SourceMgr = AST.getSourceManager();
|
2018-02-21 10:39:08 +08:00
|
|
|
Position Begin = sourceLocToPosition(SourceMgr, SR.getBegin());
|
|
|
|
Position End = sourceLocToPosition(SourceMgr, SR.getEnd());
|
2017-12-20 01:06:07 +08:00
|
|
|
Range R = {Begin, End};
|
|
|
|
DocumentHighlight DH;
|
|
|
|
DH.range = R;
|
|
|
|
DH.kind = Kind;
|
|
|
|
return DH;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
|
|
|
|
Position Pos) {
|
2017-12-20 01:06:07 +08:00
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
|
|
|
const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
|
|
|
|
if (!FE)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
|
|
|
|
|
|
|
|
auto DeclMacrosFinder = std::make_shared<DeclarationAndMacrosFinder>(
|
|
|
|
llvm::errs(), SourceLocationBeg, AST.getASTContext(),
|
|
|
|
AST.getPreprocessor());
|
|
|
|
index::IndexingOptions IndexOpts;
|
|
|
|
IndexOpts.SystemSymbolFilter =
|
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::All;
|
|
|
|
IndexOpts.IndexFunctionLocals = true;
|
|
|
|
|
|
|
|
// Macro occurences are not currently handled.
|
|
|
|
indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
|
|
|
|
DeclMacrosFinder, IndexOpts);
|
|
|
|
|
|
|
|
std::vector<const Decl *> SelectedDecls = DeclMacrosFinder->takeDecls();
|
|
|
|
|
|
|
|
auto DocHighlightsFinder = std::make_shared<DocumentHighlightsFinder>(
|
|
|
|
llvm::errs(), AST.getASTContext(), AST.getPreprocessor(), SelectedDecls);
|
|
|
|
|
|
|
|
indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
|
|
|
|
DocHighlightsFinder, IndexOpts);
|
|
|
|
|
|
|
|
return DocHighlightsFinder->takeHighlights();
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
static PrintingPolicy PrintingPolicyForDecls(PrintingPolicy Base) {
|
|
|
|
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.
|
|
|
|
static std::string TypeDeclToString(const TypeDecl *TD) {
|
|
|
|
QualType Type = TD->getASTContext().getTypeDeclType(TD);
|
|
|
|
|
|
|
|
PrintingPolicy Policy =
|
|
|
|
PrintingPolicyForDecls(TD->getASTContext().getPrintingPolicy());
|
|
|
|
|
|
|
|
std::string Name;
|
|
|
|
llvm::raw_string_ostream Stream(Name);
|
|
|
|
Type.print(Stream, Policy);
|
|
|
|
|
|
|
|
return Stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a string representation (e.g. "namespace ns1::ns2") of
|
|
|
|
/// the named declaration \p ND.
|
|
|
|
static std::string NamedDeclQualifiedName(const NamedDecl *ND,
|
|
|
|
StringRef Prefix) {
|
|
|
|
PrintingPolicy Policy =
|
|
|
|
PrintingPolicyForDecls(ND->getASTContext().getPrintingPolicy());
|
|
|
|
|
|
|
|
std::string Name;
|
|
|
|
llvm::raw_string_ostream Stream(Name);
|
|
|
|
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".
|
|
|
|
static llvm::Optional<std::string> getScopeName(const Decl *D) {
|
|
|
|
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))
|
|
|
|
return TypeDeclToString(TD);
|
|
|
|
else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
|
|
|
|
return NamedDeclQualifiedName(ND, "namespace");
|
|
|
|
else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
|
|
|
|
return NamedDeclQualifiedName(FD, "function");
|
|
|
|
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a \p Hover object given the declaration \p D.
|
|
|
|
static Hover getHoverContents(const Decl *D) {
|
|
|
|
Hover H;
|
|
|
|
llvm::Optional<std::string> NamedScope = getScopeName(D);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
llvm::raw_string_ostream OS(DeclText);
|
|
|
|
|
|
|
|
PrintingPolicy Policy =
|
|
|
|
PrintingPolicyForDecls(D->getASTContext().getPrintingPolicy());
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hover getHover(ParsedAST &AST, Position Pos) {
|
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
|
|
|
const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
|
|
|
|
if (FE == nullptr)
|
|
|
|
return Hover();
|
|
|
|
|
|
|
|
SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
|
|
|
|
auto DeclMacrosFinder = std::make_shared<DeclarationAndMacrosFinder>(
|
|
|
|
llvm::errs(), SourceLocationBeg, AST.getASTContext(),
|
|
|
|
AST.getPreprocessor());
|
|
|
|
|
|
|
|
index::IndexingOptions IndexOpts;
|
|
|
|
IndexOpts.SystemSymbolFilter =
|
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::All;
|
|
|
|
IndexOpts.IndexFunctionLocals = true;
|
|
|
|
|
|
|
|
indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
|
|
|
|
DeclMacrosFinder, IndexOpts);
|
|
|
|
|
|
|
|
std::vector<MacroDecl> Macros = DeclMacrosFinder->takeMacroInfos();
|
|
|
|
if (!Macros.empty())
|
|
|
|
return getHoverContents(Macros[0].Name);
|
|
|
|
|
|
|
|
std::vector<const Decl *> Decls = DeclMacrosFinder->takeDecls();
|
|
|
|
if (!Decls.empty())
|
|
|
|
return getHoverContents(Decls[0]);
|
|
|
|
|
|
|
|
return Hover();
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|