[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
//===--- Hover.cpp - Information about code at the cursor location --------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Hover.h"
|
|
|
|
|
|
|
|
#include "AST.h"
|
|
|
|
#include "CodeCompletionStrings.h"
|
|
|
|
#include "FindTarget.h"
|
2019-12-10 17:28:37 +08:00
|
|
|
#include "FormattedString.h"
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "Selection.h"
|
|
|
|
#include "SourceCode.h"
|
|
|
|
#include "index/SymbolCollector.h"
|
|
|
|
|
2019-11-17 05:15:05 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
#include "clang/AST/ASTTypeTraits.h"
|
2019-12-16 21:22:48 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclBase.h"
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
[clangd] Store index::SymbolKind in HoverInfo
Summary:
LSP's SymbolKind has some shortcomings when it comes to C++ types,
index::SymbolKind has more detailed info like Destructor, Parameter, MACRO etc.
We are planning to make use of that information in our new Hover response, and
it would be nice to display the Symbol type in full detail, rather than some
approximation.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70723
2019-11-27 01:06:17 +08:00
|
|
|
#include "clang/Index/IndexSymbol.h"
|
2019-12-16 21:22:48 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2019-12-17 19:13:28 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2019-12-16 21:22:48 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
[clangd] Store index::SymbolKind in HoverInfo
Summary:
LSP's SymbolKind has some shortcomings when it comes to C++ types,
index::SymbolKind has more detailed info like Destructor, Parameter, MACRO etc.
We are planning to make use of that information in our new Hover response, and
it would be nice to display the Symbol type in full detail, rather than some
approximation.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70723
2019-11-27 01:06:17 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
PrintingPolicy printingPolicyForDecls(PrintingPolicy Base) {
|
|
|
|
PrintingPolicy Policy(Base);
|
|
|
|
|
|
|
|
Policy.AnonymousTagLocations = false;
|
|
|
|
Policy.TerseOutput = true;
|
|
|
|
Policy.PolishForDeclaration = true;
|
|
|
|
Policy.ConstantsAsWritten = true;
|
|
|
|
Policy.SuppressTagKeyword = false;
|
|
|
|
|
|
|
|
return Policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a declaration \p D, return a human-readable string representing the
|
|
|
|
/// local scope in which it is declared, i.e. class(es) and method name. Returns
|
|
|
|
/// an empty string if it is not local.
|
|
|
|
std::string getLocalScope(const Decl *D) {
|
|
|
|
std::vector<std::string> Scopes;
|
|
|
|
const DeclContext *DC = D->getDeclContext();
|
|
|
|
auto GetName = [](const TypeDecl *D) {
|
|
|
|
if (!D->getDeclName().isEmpty()) {
|
|
|
|
PrintingPolicy Policy = D->getASTContext().getPrintingPolicy();
|
|
|
|
Policy.SuppressScope = true;
|
|
|
|
return declaredType(D).getAsString(Policy);
|
|
|
|
}
|
|
|
|
if (auto RD = dyn_cast<RecordDecl>(D))
|
|
|
|
return ("(anonymous " + RD->getKindName() + ")").str();
|
|
|
|
return std::string("");
|
|
|
|
};
|
|
|
|
while (DC) {
|
|
|
|
if (const TypeDecl *TD = dyn_cast<TypeDecl>(DC))
|
|
|
|
Scopes.push_back(GetName(TD));
|
|
|
|
else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
|
|
|
|
Scopes.push_back(FD->getNameAsString());
|
|
|
|
DC = DC->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::join(llvm::reverse(Scopes), "::");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the human-readable representation for namespace containing the
|
|
|
|
/// declaration \p D. Returns empty if it is contained global namespace.
|
|
|
|
std::string getNamespaceScope(const Decl *D) {
|
|
|
|
const DeclContext *DC = D->getDeclContext();
|
|
|
|
|
2019-12-16 21:22:48 +08:00
|
|
|
if (const TagDecl *TD = dyn_cast<TagDecl>(DC))
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
return getNamespaceScope(TD);
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
|
|
|
|
return getNamespaceScope(FD);
|
2019-12-16 21:22:48 +08:00
|
|
|
if (const NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(DC)) {
|
|
|
|
// Skip inline/anon namespaces.
|
|
|
|
if (NSD->isInline() || NSD->isAnonymousNamespace())
|
|
|
|
return getNamespaceScope(NSD);
|
|
|
|
}
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
|
2019-12-16 21:22:48 +08:00
|
|
|
return printQualifiedName(*ND);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string printDefinition(const Decl *D) {
|
|
|
|
std::string Definition;
|
|
|
|
llvm::raw_string_ostream OS(Definition);
|
|
|
|
PrintingPolicy Policy =
|
|
|
|
printingPolicyForDecls(D->getASTContext().getPrintingPolicy());
|
|
|
|
Policy.IncludeTagDefinition = false;
|
|
|
|
Policy.SuppressTemplateArgsInCXXConstructors = true;
|
|
|
|
D->print(OS, Policy);
|
|
|
|
OS.flush();
|
|
|
|
return Definition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printParams(llvm::raw_ostream &OS,
|
2019-12-12 02:14:15 +08:00
|
|
|
const std::vector<HoverInfo::Param> &Params) {
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
for (size_t I = 0, E = Params.size(); I != E; ++I) {
|
|
|
|
if (I)
|
|
|
|
OS << ", ";
|
|
|
|
OS << Params.at(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HoverInfo::Param>
|
|
|
|
fetchTemplateParameters(const TemplateParameterList *Params,
|
|
|
|
const PrintingPolicy &PP) {
|
|
|
|
assert(Params);
|
|
|
|
std::vector<HoverInfo::Param> TempParameters;
|
|
|
|
|
|
|
|
for (const Decl *Param : *Params) {
|
|
|
|
HoverInfo::Param P;
|
|
|
|
P.Type.emplace();
|
|
|
|
if (const auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
|
|
|
|
P.Type = TTP->wasDeclaredWithTypename() ? "typename" : "class";
|
|
|
|
if (TTP->isParameterPack())
|
|
|
|
*P.Type += "...";
|
|
|
|
|
|
|
|
if (!TTP->getName().empty())
|
|
|
|
P.Name = TTP->getNameAsString();
|
|
|
|
if (TTP->hasDefaultArgument())
|
|
|
|
P.Default = TTP->getDefaultArgument().getAsString(PP);
|
|
|
|
} else if (const auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
|
|
|
|
if (IdentifierInfo *II = NTTP->getIdentifier())
|
|
|
|
P.Name = II->getName().str();
|
|
|
|
|
|
|
|
llvm::raw_string_ostream Out(*P.Type);
|
|
|
|
NTTP->getType().print(Out, PP);
|
|
|
|
if (NTTP->isParameterPack())
|
|
|
|
Out << "...";
|
|
|
|
|
|
|
|
if (NTTP->hasDefaultArgument()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
|
|
|
NTTP->getDefaultArgument()->printPretty(Out, nullptr, PP);
|
|
|
|
}
|
|
|
|
} else if (const auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
|
|
|
|
llvm::raw_string_ostream OS(*P.Type);
|
|
|
|
OS << "template <";
|
|
|
|
printParams(OS,
|
|
|
|
fetchTemplateParameters(TTPD->getTemplateParameters(), PP));
|
|
|
|
OS << "> class"; // FIXME: TemplateTemplateParameter doesn't store the
|
|
|
|
// info on whether this param was a "typename" or
|
|
|
|
// "class".
|
|
|
|
if (!TTPD->getName().empty())
|
|
|
|
P.Name = TTPD->getNameAsString();
|
|
|
|
if (TTPD->hasDefaultArgument()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
|
|
|
TTPD->getDefaultArgument().getArgument().print(PP, Out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TempParameters.push_back(std::move(P));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TempParameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FunctionDecl *getUnderlyingFunction(const Decl *D) {
|
|
|
|
// Extract lambda from variables.
|
|
|
|
if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D)) {
|
|
|
|
auto QT = VD->getType();
|
|
|
|
if (!QT.isNull()) {
|
|
|
|
while (!QT->getPointeeType().isNull())
|
|
|
|
QT = QT->getPointeeType();
|
|
|
|
|
|
|
|
if (const auto *CD = QT->getAsCXXRecordDecl())
|
|
|
|
return CD->getLambdaCallOperator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-lambda functions.
|
|
|
|
return D->getAsFunction();
|
|
|
|
}
|
|
|
|
|
2019-12-17 19:13:28 +08:00
|
|
|
// Returns the decl that should be used for querying comments, either from index
|
|
|
|
// or AST.
|
|
|
|
const NamedDecl *getDeclForComment(const NamedDecl *D) {
|
|
|
|
if (auto *CTSD = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D))
|
|
|
|
if (!CTSD->isExplicitInstantiationOrSpecialization())
|
|
|
|
return CTSD->getTemplateInstantiationPattern();
|
|
|
|
if (auto *VTSD = llvm::dyn_cast<VarTemplateSpecializationDecl>(D))
|
|
|
|
if (!VTSD->isExplicitInstantiationOrSpecialization())
|
|
|
|
return VTSD->getTemplateInstantiationPattern();
|
|
|
|
if (auto *FD = D->getAsFunction())
|
|
|
|
if (FD->isTemplateInstantiation())
|
2019-12-27 16:11:03 +08:00
|
|
|
return FD->getTemplateInstantiationPattern();
|
2019-12-17 19:13:28 +08:00
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
// Look up information about D from the index, and add it to Hover.
|
2019-12-17 19:13:28 +08:00
|
|
|
void enhanceFromIndex(HoverInfo &Hover, const NamedDecl &ND,
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
const SymbolIndex *Index) {
|
2019-12-17 19:13:28 +08:00
|
|
|
assert(&ND == getDeclForComment(&ND));
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
// We only add documentation, so don't bother if we already have some.
|
2019-12-17 19:13:28 +08:00
|
|
|
if (!Hover.Documentation.empty() || !Index)
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
return;
|
2019-12-17 19:13:28 +08:00
|
|
|
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
// Skip querying for non-indexable symbols, there's no point.
|
|
|
|
// We're searching for symbols that might be indexed outside this main file.
|
|
|
|
if (!SymbolCollector::shouldCollectSymbol(ND, ND.getASTContext(),
|
|
|
|
SymbolCollector::Options(),
|
|
|
|
/*IsMainFileOnly=*/false))
|
|
|
|
return;
|
|
|
|
auto ID = getSymbolID(&ND);
|
|
|
|
if (!ID)
|
|
|
|
return;
|
|
|
|
LookupRequest Req;
|
|
|
|
Req.IDs.insert(*ID);
|
|
|
|
Index->lookup(
|
|
|
|
Req, [&](const Symbol &S) { Hover.Documentation = S.Documentation; });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populates Type, ReturnType, and Parameters for function-like decls.
|
|
|
|
void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
|
|
|
|
const FunctionDecl *FD,
|
|
|
|
const PrintingPolicy &Policy) {
|
|
|
|
HI.Parameters.emplace();
|
|
|
|
for (const ParmVarDecl *PVD : FD->parameters()) {
|
|
|
|
HI.Parameters->emplace_back();
|
|
|
|
auto &P = HI.Parameters->back();
|
|
|
|
if (!PVD->getType().isNull()) {
|
|
|
|
P.Type.emplace();
|
|
|
|
llvm::raw_string_ostream OS(*P.Type);
|
|
|
|
PVD->getType().print(OS, Policy);
|
|
|
|
} else {
|
|
|
|
std::string Param;
|
|
|
|
llvm::raw_string_ostream OS(Param);
|
|
|
|
PVD->dump(OS);
|
|
|
|
OS.flush();
|
|
|
|
elog("Got param with null type: {0}", Param);
|
|
|
|
}
|
|
|
|
if (!PVD->getName().empty())
|
|
|
|
P.Name = PVD->getNameAsString();
|
|
|
|
if (PVD->hasDefaultArg()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
|
|
|
PVD->getDefaultArg()->printPretty(Out, nullptr, Policy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto* CCD = llvm::dyn_cast<CXXConstructorDecl>(FD)) {
|
|
|
|
// Constructor's "return type" is the class type.
|
|
|
|
HI.ReturnType = declaredType(CCD->getParent()).getAsString(Policy);
|
|
|
|
// Don't provide any type for the constructor itself.
|
2019-11-22 00:09:19 +08:00
|
|
|
} else if (llvm::isa<CXXDestructorDecl>(FD)){
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
HI.ReturnType = "void";
|
|
|
|
} else {
|
|
|
|
HI.ReturnType = FD->getReturnType().getAsString(Policy);
|
|
|
|
|
|
|
|
QualType FunctionType = FD->getType();
|
|
|
|
if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D)) // Lambdas
|
|
|
|
FunctionType = VD->getType().getDesugaredType(D->getASTContext());
|
|
|
|
HI.Type = FunctionType.getAsString(Policy);
|
|
|
|
}
|
|
|
|
// FIXME: handle variadics.
|
|
|
|
}
|
|
|
|
|
2019-12-12 17:41:27 +08:00
|
|
|
llvm::Optional<std::string> printExprValue(const Expr *E,
|
|
|
|
const ASTContext &Ctx) {
|
2019-11-17 05:15:05 +08:00
|
|
|
Expr::EvalResult Constant;
|
|
|
|
// Evaluating [[foo]]() as "&foo" isn't useful, and prevents us walking up
|
|
|
|
// to the enclosing call.
|
|
|
|
QualType T = E->getType();
|
2019-12-12 17:41:27 +08:00
|
|
|
if (T.isNull() || T->isFunctionType() || T->isFunctionPointerType() ||
|
2019-11-17 05:15:05 +08:00
|
|
|
T->isFunctionReferenceType())
|
|
|
|
return llvm::None;
|
|
|
|
// Attempt to evaluate. If expr is dependent, evaluation crashes!
|
|
|
|
if (E->isValueDependent() || !E->EvaluateAsRValue(Constant, Ctx))
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
// Show enums symbolically, not numerically like APValue::printPretty().
|
|
|
|
if (T->isEnumeralType() && Constant.Val.getInt().getMinSignedBits() <= 64) {
|
|
|
|
// Compare to int64_t to avoid bit-width match requirements.
|
|
|
|
int64_t Val = Constant.Val.getInt().getExtValue();
|
|
|
|
for (const EnumConstantDecl *ECD :
|
|
|
|
T->castAs<EnumType>()->getDecl()->enumerators())
|
|
|
|
if (ECD->getInitVal() == Val)
|
|
|
|
return llvm::formatv("{0} ({1})", ECD->getNameAsString(), Val).str();
|
|
|
|
}
|
|
|
|
return Constant.Val.getAsString(Ctx, E->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<std::string> printExprValue(const SelectionTree::Node *N,
|
|
|
|
const ASTContext &Ctx) {
|
|
|
|
for (; N; N = N->Parent) {
|
2020-01-04 23:28:41 +08:00
|
|
|
// Try to evaluate the first evaluatable enclosing expression.
|
2019-11-17 05:15:05 +08:00
|
|
|
if (const Expr *E = N->ASTNode.get<Expr>()) {
|
|
|
|
if (auto Val = printExprValue(E, Ctx))
|
|
|
|
return Val;
|
|
|
|
} else if (N->ASTNode.get<Decl>() || N->ASTNode.get<Stmt>()) {
|
|
|
|
// Refuse to cross certain non-exprs. (TypeLoc are OK as part of Exprs).
|
|
|
|
// This tries to ensure we're showing a value related to the cursor.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
/// Generate a \p Hover object given the declaration \p D.
|
|
|
|
HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) {
|
|
|
|
HoverInfo HI;
|
|
|
|
const ASTContext &Ctx = D->getASTContext();
|
|
|
|
|
|
|
|
HI.NamespaceScope = getNamespaceScope(D);
|
|
|
|
if (!HI.NamespaceScope->empty())
|
|
|
|
HI.NamespaceScope->append("::");
|
|
|
|
HI.LocalScope = getLocalScope(D);
|
|
|
|
if (!HI.LocalScope.empty())
|
|
|
|
HI.LocalScope.append("::");
|
|
|
|
|
|
|
|
PrintingPolicy Policy = printingPolicyForDecls(Ctx.getPrintingPolicy());
|
|
|
|
if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) {
|
|
|
|
HI.Name = printName(Ctx, *ND);
|
2019-12-17 19:13:28 +08:00
|
|
|
ND = getDeclForComment(ND);
|
|
|
|
HI.Documentation = getDeclComment(Ctx, *ND);
|
|
|
|
enhanceFromIndex(HI, *ND, Index);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Store index::SymbolKind in HoverInfo
Summary:
LSP's SymbolKind has some shortcomings when it comes to C++ types,
index::SymbolKind has more detailed info like Destructor, Parameter, MACRO etc.
We are planning to make use of that information in our new Hover response, and
it would be nice to display the Symbol type in full detail, rather than some
approximation.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70723
2019-11-27 01:06:17 +08:00
|
|
|
HI.Kind = index::getSymbolInfo(D).Kind;
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
|
|
|
|
// Fill in template params.
|
|
|
|
if (const TemplateDecl *TD = D->getDescribedTemplate()) {
|
|
|
|
HI.TemplateParameters =
|
|
|
|
fetchTemplateParameters(TD->getTemplateParameters(), Policy);
|
|
|
|
D = TD;
|
|
|
|
} else if (const FunctionDecl *FD = D->getAsFunction()) {
|
|
|
|
if (const auto FTD = FD->getDescribedTemplate()) {
|
|
|
|
HI.TemplateParameters =
|
|
|
|
fetchTemplateParameters(FTD->getTemplateParameters(), Policy);
|
|
|
|
D = FTD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in types and params.
|
|
|
|
if (const FunctionDecl *FD = getUnderlyingFunction(D)) {
|
|
|
|
fillFunctionTypeAndParams(HI, D, FD, Policy);
|
|
|
|
} else if (const auto *VD = dyn_cast<ValueDecl>(D)) {
|
|
|
|
HI.Type.emplace();
|
|
|
|
llvm::raw_string_ostream OS(*HI.Type);
|
|
|
|
VD->getType().print(OS, Policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in value with evaluated initializer if possible.
|
|
|
|
if (const auto *Var = dyn_cast<VarDecl>(D)) {
|
2019-11-17 05:15:05 +08:00
|
|
|
if (const Expr *Init = Var->getInit())
|
|
|
|
HI.Value = printExprValue(Init, Ctx);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
|
|
|
|
// Dependent enums (e.g. nested in template classes) don't have values yet.
|
|
|
|
if (!ECD->getType()->isDependentType())
|
|
|
|
HI.Value = ECD->getInitVal().toString(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
HI.Definition = printDefinition(D);
|
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a \p Hover object given the type \p T.
|
2019-12-17 20:00:12 +08:00
|
|
|
HoverInfo getHoverContents(QualType T, ASTContext &ASTCtx,
|
|
|
|
const SymbolIndex *Index) {
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
HoverInfo HI;
|
|
|
|
|
2019-12-17 20:00:12 +08:00
|
|
|
if (const auto *D = T->getAsTagDecl()) {
|
2019-12-16 21:22:48 +08:00
|
|
|
HI.Name = printName(ASTCtx, *D);
|
[clangd] Store index::SymbolKind in HoverInfo
Summary:
LSP's SymbolKind has some shortcomings when it comes to C++ types,
index::SymbolKind has more detailed info like Destructor, Parameter, MACRO etc.
We are planning to make use of that information in our new Hover response, and
it would be nice to display the Symbol type in full detail, rather than some
approximation.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70723
2019-11-27 01:06:17 +08:00
|
|
|
HI.Kind = index::getSymbolInfo(D).Kind;
|
2019-12-16 21:22:48 +08:00
|
|
|
|
2019-12-17 19:13:28 +08:00
|
|
|
const auto *CommentD = getDeclForComment(D);
|
|
|
|
HI.Documentation = getDeclComment(ASTCtx, *CommentD);
|
|
|
|
enhanceFromIndex(HI, *CommentD, Index);
|
|
|
|
} else {
|
2019-12-16 21:22:48 +08:00
|
|
|
// Builtin types
|
|
|
|
llvm::raw_string_ostream OS(HI.Name);
|
|
|
|
PrintingPolicy Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy());
|
|
|
|
T.print(OS, Policy);
|
|
|
|
}
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a \p Hover object given the macro \p MacroDecl.
|
|
|
|
HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) {
|
|
|
|
HoverInfo HI;
|
|
|
|
SourceManager &SM = AST.getSourceManager();
|
|
|
|
HI.Name = Macro.Name;
|
[clangd] Store index::SymbolKind in HoverInfo
Summary:
LSP's SymbolKind has some shortcomings when it comes to C++ types,
index::SymbolKind has more detailed info like Destructor, Parameter, MACRO etc.
We are planning to make use of that information in our new Hover response, and
it would be nice to display the Symbol type in full detail, rather than some
approximation.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70723
2019-11-27 01:06:17 +08:00
|
|
|
HI.Kind = index::SymbolKind::Macro;
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
// FIXME: Populate documentation
|
|
|
|
// FIXME: Pupulate parameters
|
|
|
|
|
|
|
|
// Try to get the full definition, not just the name
|
|
|
|
SourceLocation StartLoc = Macro.Info->getDefinitionLoc();
|
|
|
|
SourceLocation EndLoc = Macro.Info->getDefinitionEndLoc();
|
|
|
|
if (EndLoc.isValid()) {
|
2019-12-05 07:09:35 +08:00
|
|
|
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM, AST.getLangOpts());
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
bool Invalid;
|
|
|
|
StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc), &Invalid);
|
|
|
|
if (!Invalid) {
|
|
|
|
unsigned StartOffset = SM.getFileOffset(StartLoc);
|
|
|
|
unsigned EndOffset = SM.getFileOffset(EndLoc);
|
|
|
|
if (EndOffset <= Buffer.size() && StartOffset < EndOffset)
|
|
|
|
HI.Definition =
|
|
|
|
("#define " + Buffer.substr(StartOffset, EndOffset - StartOffset))
|
|
|
|
.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
|
|
|
|
format::FormatStyle Style,
|
|
|
|
const SymbolIndex *Index) {
|
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
|
|
|
llvm::Optional<HoverInfo> HI;
|
|
|
|
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
|
2019-12-05 07:09:35 +08:00
|
|
|
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
|
|
|
|
if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
|
2019-12-17 20:00:12 +08:00
|
|
|
HI = getHoverContents(*Deduced, AST.getASTContext(), Index);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
} else if (auto M = locateMacroAt(SourceLocationBeg, AST.getPreprocessor())) {
|
|
|
|
HI = getHoverContents(*M, AST);
|
|
|
|
} else {
|
|
|
|
auto Offset = positionToOffset(SM.getBufferData(SM.getMainFileID()), Pos);
|
|
|
|
if (!Offset) {
|
|
|
|
llvm::consumeError(Offset.takeError());
|
|
|
|
return llvm::None;
|
|
|
|
}
|
2019-12-17 02:07:49 +08:00
|
|
|
SelectionTree Selection(AST.getASTContext(), AST.getTokens(), *Offset);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
std::vector<const Decl *> Result;
|
2019-12-17 02:07:49 +08:00
|
|
|
if (const SelectionTree::Node *N = Selection.commonAncestor()) {
|
2019-12-17 19:13:28 +08:00
|
|
|
auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Alias);
|
2019-11-17 05:15:05 +08:00
|
|
|
if (!Decls.empty()) {
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
HI = getHoverContents(Decls.front(), Index);
|
2019-11-17 05:15:05 +08:00
|
|
|
// Look for a close enclosing expression to show the value of.
|
|
|
|
if (!HI->Value)
|
|
|
|
HI->Value = printExprValue(N, AST.getASTContext());
|
|
|
|
}
|
|
|
|
// FIXME: support hovers for other nodes?
|
|
|
|
// - certain expressions (sizeof etc)
|
|
|
|
// - built-in types
|
|
|
|
// - literals (esp user-defined)
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HI)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
auto Replacements = format::reformat(
|
|
|
|
Style, HI->Definition, tooling::Range(0, HI->Definition.size()));
|
|
|
|
if (auto Formatted =
|
|
|
|
tooling::applyAllReplacements(HI->Definition, Replacements))
|
|
|
|
HI->Definition = *Formatted;
|
|
|
|
|
2019-12-05 22:30:04 +08:00
|
|
|
HI->SymRange = getTokenRange(AST.getSourceManager(),
|
2019-12-05 07:09:35 +08:00
|
|
|
AST.getLangOpts(), SourceLocationBeg);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:28:37 +08:00
|
|
|
markup::Document HoverInfo::present() const {
|
|
|
|
markup::Document Output;
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
if (NamespaceScope) {
|
2019-12-10 17:28:37 +08:00
|
|
|
auto &P = Output.addParagraph();
|
|
|
|
P.appendText("Declared in");
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
// Drop trailing "::".
|
|
|
|
if (!LocalScope.empty())
|
2019-12-10 17:28:37 +08:00
|
|
|
P.appendCode(llvm::StringRef(LocalScope).drop_back(2));
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
else if (NamespaceScope->empty())
|
2019-12-10 17:28:37 +08:00
|
|
|
P.appendCode("global namespace");
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
else
|
2019-12-10 17:28:37 +08:00
|
|
|
P.appendCode(llvm::StringRef(*NamespaceScope).drop_back(2));
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Definition.empty()) {
|
2019-12-12 02:14:15 +08:00
|
|
|
Output.addCodeBlock(Definition);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
} else {
|
|
|
|
// Builtin types
|
2019-12-12 02:14:15 +08:00
|
|
|
Output.addCodeBlock(Name);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Documentation.empty())
|
2019-12-10 17:28:37 +08:00
|
|
|
Output.addParagraph().appendText(Documentation);
|
[clangd] Untangle Hover from XRefs, move into own file.
Summary:
This is mostly mechanical, with a few exceptions:
- getDeducedType moved into AST.h where it belongs. It now takes
ASTContext instead of ParsedAST, and avoids using the preprocessor.
- hover now uses SelectionTree directly rather than via
getDeclAtPosition helper
- hover on 'auto' used to find the decl that contained the 'auto' and
use that to set Kind and documentation for the hover result.
Now we use targetDecl() to find the decl matching the deduced type instead.
This changes tests, e.g. 'variable' -> class for auto on lambdas.
I think this is better, but the motivation was to avoid depending on
the internals of DeducedTypeVisitor. This functionality is removed
from the visitor.
Reviewers: kadircet
Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70357
2019-11-17 00:00:19 +08:00
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
|
|
|
|
const HoverInfo::Param &P) {
|
|
|
|
std::vector<llvm::StringRef> Output;
|
|
|
|
if (P.Type)
|
|
|
|
Output.push_back(*P.Type);
|
|
|
|
if (P.Name)
|
|
|
|
Output.push_back(*P.Name);
|
|
|
|
OS << llvm::join(Output, " ");
|
|
|
|
if (P.Default)
|
|
|
|
OS << " = " << *P.Default;
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|