[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-16 16:00:19 +00: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"
|
2021-12-08 05:52:15 +00:00
|
|
|
#include "Config.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-16 16:00:19 +00:00
|
|
|
#include "FindTarget.h"
|
2020-01-10 12:11:09 +00:00
|
|
|
#include "ParsedAST.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-16 16:00:19 +00:00
|
|
|
#include "Selection.h"
|
|
|
|
#include "SourceCode.h"
|
|
|
|
#include "index/SymbolCollector.h"
|
2020-05-02 12:53:47 +00:00
|
|
|
#include "support/Markup.h"
|
2019-11-16 21:15:05 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2021-12-08 04:31:00 +00:00
|
|
|
#include "clang/AST/ASTDiagnostic.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-16 16:00:19 +00:00
|
|
|
#include "clang/AST/ASTTypeTraits.h"
|
2020-10-20 10:01:48 +00:00
|
|
|
#include "clang/AST/Attr.h"
|
2019-12-16 13:22:48 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclBase.h"
|
2020-04-03 15:09:38 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2020-12-04 20:04:25 +00:00
|
|
|
#include "clang/AST/DeclObjC.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-16 16:00:19 +00:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2020-01-10 12:11:09 +00:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
2020-04-03 15:09:38 +00:00
|
|
|
#include "clang/AST/OperationKinds.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-16 16:00:19 +00:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2021-03-11 00:20:36 +00:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2020-01-07 14:15:00 +00:00
|
|
|
#include "clang/AST/Type.h"
|
2022-08-19 12:51:36 +00:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2020-03-01 15:05:12 +00:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2020-01-24 12:04:00 +00:00
|
|
|
#include "clang/Basic/Specifiers.h"
|
2020-02-26 14:23:40 +00:00
|
|
|
#include "clang/Basic/TokenKinds.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-26 17:06:17 +00:00
|
|
|
#include "clang/Index/IndexSymbol.h"
|
2020-02-26 14:23:40 +00:00
|
|
|
#include "clang/Tooling/Syntax/Tokens.h"
|
2019-12-16 13:22:48 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2019-12-13 08:34:59 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2020-01-10 12:11:09 +00:00
|
|
|
#include "llvm/Support/Casting.h"
|
2021-02-22 22:05:48 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2021-12-08 04:31:00 +00:00
|
|
|
#include "llvm/Support/ScopedPrinter.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-26 17:06:17 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2023-01-08 04:02:20 +00:00
|
|
|
#include <optional>
|
2019-12-13 08:34:59 +00:00
|
|
|
#include <string>
|
[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-16 16:00:19 +00:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2020-12-18 17:39:20 +00:00
|
|
|
PrintingPolicy getPrintingPolicy(PrintingPolicy Base) {
|
|
|
|
Base.AnonymousTagLocations = false;
|
|
|
|
Base.TerseOutput = true;
|
|
|
|
Base.PolishForDeclaration = true;
|
|
|
|
Base.ConstantsAsWritten = true;
|
|
|
|
Base.SuppressTemplateArgsInCXXConstructors = true;
|
|
|
|
return Base;
|
[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-16 16:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a declaration \p D, return a human-readable string representing the
|
|
|
|
/// local scope in which it is declared, i.e. class(es) and method name. Returns
|
|
|
|
/// an empty string if it is not local.
|
|
|
|
std::string getLocalScope(const Decl *D) {
|
|
|
|
std::vector<std::string> Scopes;
|
|
|
|
const DeclContext *DC = D->getDeclContext();
|
2020-12-04 20:04:25 +00:00
|
|
|
|
|
|
|
// ObjC scopes won't have multiple components for us to join, instead:
|
|
|
|
// - Methods: "-[Class methodParam1:methodParam2]"
|
|
|
|
// - Classes, categories, and protocols: "MyClass(Category)"
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
|
|
|
|
return printObjCMethod(*MD);
|
2021-11-15 15:00:23 +00:00
|
|
|
if (const ObjCContainerDecl *CD = dyn_cast<ObjCContainerDecl>(DC))
|
2020-12-04 20:04:25 +00:00
|
|
|
return printObjCContainer(*CD);
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
auto GetName = [](const TypeDecl *D) {
|
|
|
|
if (!D->getDeclName().isEmpty()) {
|
|
|
|
PrintingPolicy Policy = D->getASTContext().getPrintingPolicy();
|
|
|
|
Policy.SuppressScope = true;
|
|
|
|
return declaredType(D).getAsString(Policy);
|
|
|
|
}
|
2021-12-13 15:14:31 +00:00
|
|
|
if (auto *RD = dyn_cast<RecordDecl>(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-16 16:00:19 +00:00
|
|
|
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();
|
|
|
|
|
2020-12-04 20:04:25 +00:00
|
|
|
// ObjC does not have the concept of namespaces, so instead we support
|
|
|
|
// local scopes.
|
|
|
|
if (isa<ObjCMethodDecl, ObjCContainerDecl>(DC))
|
|
|
|
return "";
|
|
|
|
|
2019-12-16 13:22:48 +00: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-16 16:00:19 +00:00
|
|
|
return getNamespaceScope(TD);
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
|
|
|
|
return getNamespaceScope(FD);
|
2019-12-16 13:22:48 +00: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-16 16:00:19 +00:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
|
2019-12-16 13:22:48 +00: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-16 16:00:19 +00:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:55:52 +00:00
|
|
|
std::string printDefinition(const Decl *D, PrintingPolicy PP,
|
|
|
|
const syntax::TokenBuffer &TB) {
|
|
|
|
if (auto *VD = llvm::dyn_cast<VarDecl>(D)) {
|
|
|
|
if (auto *IE = VD->getInit()) {
|
|
|
|
// Initializers might be huge and result in lots of memory allocations in
|
|
|
|
// some catostrophic cases. Such long lists are not useful in hover cards
|
|
|
|
// anyway.
|
|
|
|
if (200 < TB.expandedTokens(IE->getSourceRange()).size())
|
|
|
|
PP.SuppressInitializers = true;
|
|
|
|
}
|
|
|
|
}
|
[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-16 16:00:19 +00:00
|
|
|
std::string Definition;
|
|
|
|
llvm::raw_string_ostream OS(Definition);
|
2020-12-18 17:39:20 +00:00
|
|
|
D->print(OS, PP);
|
[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-16 16:00:19 +00:00
|
|
|
OS.flush();
|
|
|
|
return Definition;
|
|
|
|
}
|
|
|
|
|
2021-08-23 17:58:43 +00:00
|
|
|
const char *getMarkdownLanguage(const ASTContext &Ctx) {
|
|
|
|
const auto &LangOpts = Ctx.getLangOpts();
|
|
|
|
if (LangOpts.ObjC && LangOpts.CPlusPlus)
|
|
|
|
return "objective-cpp";
|
|
|
|
return LangOpts.ObjC ? "objective-c" : "cpp";
|
|
|
|
}
|
|
|
|
|
2021-12-08 04:31:00 +00:00
|
|
|
HoverInfo::PrintedType printType(QualType QT, ASTContext &ASTCtx,
|
|
|
|
const PrintingPolicy &PP) {
|
2020-01-16 14:20:13 +00:00
|
|
|
// TypePrinter doesn't resolve decltypes, so resolve them here.
|
|
|
|
// FIXME: This doesn't handle composite types that contain a decltype in them.
|
|
|
|
// We should rather have a printing policy for that.
|
2020-06-27 09:12:51 +00:00
|
|
|
while (!QT.isNull() && QT->isDecltypeType())
|
2022-01-23 13:24:36 +00:00
|
|
|
QT = QT->castAs<DecltypeType>()->getUnderlyingType();
|
2021-12-08 04:31:00 +00:00
|
|
|
HoverInfo::PrintedType Result;
|
|
|
|
llvm::raw_string_ostream OS(Result.Type);
|
2020-12-18 17:39:20 +00:00
|
|
|
// Special case: if the outer type is a tag type without qualifiers, then
|
|
|
|
// include the tag for extra clarity.
|
|
|
|
// This isn't very idiomatic, so don't attempt it for complex cases, including
|
|
|
|
// pointers/references, template specializations, etc.
|
|
|
|
if (!QT.isNull() && !QT.hasQualifiers() && PP.SuppressTagKeyword) {
|
|
|
|
if (auto *TT = llvm::dyn_cast<TagType>(QT.getTypePtr()))
|
|
|
|
OS << TT->getDecl()->getKindName() << " ";
|
|
|
|
}
|
|
|
|
QT.print(OS, PP);
|
2021-12-08 04:31:00 +00:00
|
|
|
OS.flush();
|
2021-12-08 05:52:15 +00:00
|
|
|
|
|
|
|
const Config &Cfg = Config::current();
|
|
|
|
if (!QT.isNull() && Cfg.Hover.ShowAKA) {
|
2021-12-08 04:31:00 +00:00
|
|
|
bool ShouldAKA = false;
|
|
|
|
QualType DesugaredTy = clang::desugarForDiagnostic(ASTCtx, QT, ShouldAKA);
|
|
|
|
if (ShouldAKA)
|
|
|
|
Result.AKA = DesugaredTy.getAsString(PP);
|
|
|
|
}
|
2020-12-18 17:39:20 +00:00
|
|
|
return Result;
|
2020-01-16 14:20:13 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 04:31:00 +00:00
|
|
|
HoverInfo::PrintedType printType(const TemplateTypeParmDecl *TTP) {
|
|
|
|
HoverInfo::PrintedType Result;
|
|
|
|
Result.Type = TTP->wasDeclaredWithTypename() ? "typename" : "class";
|
2020-01-30 13:07:42 +00:00
|
|
|
if (TTP->isParameterPack())
|
2021-12-08 04:31:00 +00:00
|
|
|
Result.Type += "...";
|
|
|
|
return Result;
|
2020-01-30 13:07:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 04:31:00 +00:00
|
|
|
HoverInfo::PrintedType printType(const NonTypeTemplateParmDecl *NTTP,
|
|
|
|
const PrintingPolicy &PP) {
|
|
|
|
auto PrintedType = printType(NTTP->getType(), NTTP->getASTContext(), PP);
|
|
|
|
if (NTTP->isParameterPack()) {
|
|
|
|
PrintedType.Type += "...";
|
|
|
|
if (PrintedType.AKA)
|
|
|
|
*PrintedType.AKA += "...";
|
|
|
|
}
|
|
|
|
return PrintedType;
|
2020-01-30 13:07:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 04:31:00 +00:00
|
|
|
HoverInfo::PrintedType printType(const TemplateTemplateParmDecl *TTP,
|
|
|
|
const PrintingPolicy &PP) {
|
|
|
|
HoverInfo::PrintedType Result;
|
|
|
|
llvm::raw_string_ostream OS(Result.Type);
|
2020-01-30 13:07:42 +00:00
|
|
|
OS << "template <";
|
|
|
|
llvm::StringRef Sep = "";
|
|
|
|
for (const Decl *Param : *TTP->getTemplateParameters()) {
|
|
|
|
OS << Sep;
|
|
|
|
Sep = ", ";
|
|
|
|
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
|
2021-12-08 04:31:00 +00:00
|
|
|
OS << printType(TTP).Type;
|
2020-01-30 13:07:42 +00:00
|
|
|
else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param))
|
2021-12-08 04:31:00 +00:00
|
|
|
OS << printType(NTTP, PP).Type;
|
2020-01-30 13:07:42 +00:00
|
|
|
else if (const auto *TTPD = dyn_cast<TemplateTemplateParmDecl>(Param))
|
2021-12-08 04:31:00 +00:00
|
|
|
OS << printType(TTPD, PP).Type;
|
2020-01-30 13:07:42 +00:00
|
|
|
}
|
|
|
|
// FIXME: TemplateTemplateParameter doesn't store the info on whether this
|
|
|
|
// param was a "typename" or "class".
|
|
|
|
OS << "> class";
|
2021-12-08 04:31:00 +00:00
|
|
|
OS.flush();
|
|
|
|
return Result;
|
2020-01-30 13:07:42 +00: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-16 16:00:19 +00:00
|
|
|
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;
|
2020-01-16 14:20:13 +00:00
|
|
|
if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
|
2020-01-30 13:07:42 +00:00
|
|
|
P.Type = printType(TTP);
|
[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-16 16:00:19 +00:00
|
|
|
|
|
|
|
if (!TTP->getName().empty())
|
|
|
|
P.Name = TTP->getNameAsString();
|
2020-01-30 13:07:42 +00: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-16 16:00:19 +00:00
|
|
|
if (TTP->hasDefaultArgument())
|
|
|
|
P.Default = TTP->getDefaultArgument().getAsString(PP);
|
2020-01-16 14:20:13 +00:00
|
|
|
} else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
|
2020-01-30 13:07:42 +00:00
|
|
|
P.Type = printType(NTTP, PP);
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
if (IdentifierInfo *II = NTTP->getIdentifier())
|
|
|
|
P.Name = II->getName().str();
|
|
|
|
|
|
|
|
if (NTTP->hasDefaultArgument()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
|
|
|
NTTP->getDefaultArgument()->printPretty(Out, nullptr, PP);
|
|
|
|
}
|
2020-01-16 14:20:13 +00:00
|
|
|
} else if (const auto *TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
|
2020-01-30 13:07:42 +00:00
|
|
|
P.Type = printType(TTPD, PP);
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
if (!TTPD->getName().empty())
|
|
|
|
P.Name = TTPD->getNameAsString();
|
2020-01-30 13:07:42 +00: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-16 16:00:19 +00:00
|
|
|
if (TTPD->hasDefaultArgument()) {
|
|
|
|
P.Default.emplace();
|
|
|
|
llvm::raw_string_ostream Out(*P.Default);
|
2021-05-12 17:28:41 +00:00
|
|
|
TTPD->getDefaultArgument().getArgument().print(PP, Out,
|
|
|
|
/*IncludeType*/ false);
|
[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-16 16:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 11:13:28 +00:00
|
|
|
// Returns the decl that should be used for querying comments, either from index
|
|
|
|
// or AST.
|
|
|
|
const NamedDecl *getDeclForComment(const NamedDecl *D) {
|
2021-10-26 11:34:10 +00:00
|
|
|
const NamedDecl *DeclForComment = D;
|
2020-01-24 12:04:00 +00:00
|
|
|
if (const auto *TSD = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
|
|
|
// Template may not be instantiated e.g. if the type didn't need to be
|
|
|
|
// complete; fallback to primary template.
|
|
|
|
if (TSD->getTemplateSpecializationKind() == TSK_Undeclared)
|
2021-10-26 11:34:10 +00:00
|
|
|
DeclForComment = TSD->getSpecializedTemplate();
|
|
|
|
else if (const auto *TIP = TSD->getTemplateInstantiationPattern())
|
|
|
|
DeclForComment = TIP;
|
|
|
|
} else if (const auto *TSD =
|
|
|
|
llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
|
2020-01-24 12:04:00 +00:00
|
|
|
if (TSD->getTemplateSpecializationKind() == TSK_Undeclared)
|
2021-10-26 11:34:10 +00:00
|
|
|
DeclForComment = TSD->getSpecializedTemplate();
|
|
|
|
else if (const auto *TIP = TSD->getTemplateInstantiationPattern())
|
|
|
|
DeclForComment = TIP;
|
|
|
|
} else if (const auto *FD = D->getAsFunction())
|
2020-01-24 12:04:00 +00:00
|
|
|
if (const auto *TIP = FD->getTemplateInstantiationPattern())
|
2021-10-26 11:34:10 +00:00
|
|
|
DeclForComment = TIP;
|
|
|
|
// Ensure that getDeclForComment(getDeclForComment(X)) = getDeclForComment(X).
|
|
|
|
// This is usually not needed, but in strange cases of comparision operators
|
|
|
|
// being instantiated from spasceship operater, which itself is a template
|
|
|
|
// instantiation the recursrive call is necessary.
|
|
|
|
if (D != DeclForComment)
|
|
|
|
DeclForComment = getDeclForComment(DeclForComment);
|
|
|
|
return DeclForComment;
|
2019-12-17 11:13:28 +00: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-16 16:00:19 +00:00
|
|
|
// Look up information about D from the index, and add it to Hover.
|
2019-12-17 11:13:28 +00: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-16 16:00:19 +00:00
|
|
|
const SymbolIndex *Index) {
|
2019-12-17 11:13:28 +00: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-16 16:00:19 +00:00
|
|
|
// We only add documentation, so don't bother if we already have some.
|
2019-12-17 11:13:28 +00: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-16 16:00:19 +00:00
|
|
|
return;
|
2019-12-17 11:13:28 +00: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-16 16:00:19 +00: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;
|
2020-10-29 15:04:53 +00:00
|
|
|
Req.IDs.insert(ID);
|
2020-01-28 19:23:46 +00:00
|
|
|
Index->lookup(Req, [&](const Symbol &S) {
|
|
|
|
Hover.Documentation = std::string(S.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-16 16:00:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 17:42:01 +00:00
|
|
|
// Default argument might exist but be unavailable, in the case of unparsed
|
|
|
|
// arguments for example. This function returns the default argument if it is
|
|
|
|
// available.
|
|
|
|
const Expr *getDefaultArg(const ParmVarDecl *PVD) {
|
2020-04-05 06:28:11 +00:00
|
|
|
// Default argument can be unparsed or uninstantiated. For the former we
|
2020-01-30 17:42:01 +00:00
|
|
|
// can't do much, as token information is only stored in Sema and not
|
|
|
|
// attached to the AST node. For the latter though, it is safe to proceed as
|
|
|
|
// the expression is still valid.
|
|
|
|
if (!PVD->hasDefaultArg() || PVD->hasUnparsedDefaultArg())
|
|
|
|
return nullptr;
|
|
|
|
return PVD->hasUninstantiatedDefaultArg() ? PVD->getUninstantiatedDefaultArg()
|
|
|
|
: PVD->getDefaultArg();
|
|
|
|
}
|
|
|
|
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
HoverInfo::Param toHoverInfoParam(const ParmVarDecl *PVD,
|
2020-12-18 17:39:20 +00:00
|
|
|
const PrintingPolicy &PP) {
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
HoverInfo::Param Out;
|
2021-12-08 04:31:00 +00:00
|
|
|
Out.Type = printType(PVD->getType(), PVD->getASTContext(), PP);
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
if (!PVD->getName().empty())
|
|
|
|
Out.Name = PVD->getNameAsString();
|
|
|
|
if (const Expr *DefArg = getDefaultArg(PVD)) {
|
|
|
|
Out.Default.emplace();
|
|
|
|
llvm::raw_string_ostream OS(*Out.Default);
|
2020-12-18 17:39:20 +00:00
|
|
|
DefArg->printPretty(OS, nullptr, PP);
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
// Populates Type, ReturnType, and Parameters for function-like decls.
|
|
|
|
void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
|
2019-12-13 08:34:59 +00:00
|
|
|
const FunctionDecl *FD,
|
2020-12-18 17:39:20 +00:00
|
|
|
const PrintingPolicy &PP) {
|
[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-16 16:00:19 +00:00
|
|
|
HI.Parameters.emplace();
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
for (const ParmVarDecl *PVD : FD->parameters())
|
2020-12-18 17:39:20 +00:00
|
|
|
HI.Parameters->emplace_back(toHoverInfoParam(PVD, PP));
|
[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-16 16:00:19 +00:00
|
|
|
|
2020-01-21 15:49:32 +00:00
|
|
|
// We don't want any type info, if name already contains it. This is true for
|
|
|
|
// constructors/destructors and conversion operators.
|
|
|
|
const auto NK = FD->getDeclName().getNameKind();
|
|
|
|
if (NK == DeclarationName::CXXConstructorName ||
|
|
|
|
NK == DeclarationName::CXXDestructorName ||
|
|
|
|
NK == DeclarationName::CXXConversionFunctionName)
|
|
|
|
return;
|
[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-16 16:00:19 +00:00
|
|
|
|
2021-12-08 04:31:00 +00:00
|
|
|
HI.ReturnType = printType(FD->getReturnType(), FD->getASTContext(), PP);
|
2020-01-21 15:49:32 +00:00
|
|
|
QualType QT = FD->getType();
|
|
|
|
if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D)) // Lambdas
|
|
|
|
QT = VD->getType().getDesugaredType(D->getASTContext());
|
2021-12-08 04:31:00 +00:00
|
|
|
HI.Type = printType(QT, D->getASTContext(), PP);
|
[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-16 16:00:19 +00:00
|
|
|
// FIXME: handle variadics.
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:05:48 +00:00
|
|
|
// Non-negative numbers are printed using min digits
|
|
|
|
// 0 => 0x0
|
|
|
|
// 100 => 0x64
|
|
|
|
// Negative numbers are sign-extended to 32/64 bits
|
|
|
|
// -2 => 0xfffffffe
|
|
|
|
// -2^32 => 0xfffffffeffffffff
|
|
|
|
static llvm::FormattedNumber printHex(const llvm::APSInt &V) {
|
|
|
|
uint64_t Bits = V.getExtValue();
|
2023-02-20 07:56:52 +00:00
|
|
|
if (V.isNegative() && V.getSignificantBits() <= 32)
|
2021-02-22 22:05:48 +00:00
|
|
|
return llvm::format_hex(uint32_t(Bits), 0);
|
|
|
|
return llvm::format_hex(Bits, 0);
|
|
|
|
}
|
|
|
|
|
2023-01-08 04:19:42 +00:00
|
|
|
std::optional<std::string> printExprValue(const Expr *E,
|
|
|
|
const ASTContext &Ctx) {
|
2020-07-10 14:18:10 +00:00
|
|
|
// InitListExpr has two forms, syntactic and semantic. They are the same thing
|
|
|
|
// (refer to a same AST node) in most cases.
|
|
|
|
// When they are different, RAV returns the syntactic form, and we should feed
|
|
|
|
// the semantic form to EvaluateAsRValue.
|
|
|
|
if (const auto *ILE = llvm::dyn_cast<InitListExpr>(E)) {
|
|
|
|
if (!ILE->isSemanticForm())
|
|
|
|
E = ILE->getSemanticForm();
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:15:05 +00:00
|
|
|
// Evaluating [[foo]]() as "&foo" isn't useful, and prevents us walking up
|
2020-08-20 23:34:48 +00:00
|
|
|
// to the enclosing call. Evaluating an expression of void type doesn't
|
|
|
|
// produce a meaningful result.
|
2019-11-16 21:15:05 +00:00
|
|
|
QualType T = E->getType();
|
2019-12-12 09:41:27 +00:00
|
|
|
if (T.isNull() || T->isFunctionType() || T->isFunctionPointerType() ||
|
2020-08-20 23:34:48 +00:00
|
|
|
T->isFunctionReferenceType() || T->isVoidType())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-07-10 14:18:10 +00:00
|
|
|
|
|
|
|
Expr::EvalResult Constant;
|
2019-11-16 21:15:05 +00:00
|
|
|
// Attempt to evaluate. If expr is dependent, evaluation crashes!
|
2020-06-22 19:53:47 +00:00
|
|
|
if (E->isValueDependent() || !E->EvaluateAsRValue(Constant, Ctx) ||
|
|
|
|
// Disable printing for record-types, as they are usually confusing and
|
|
|
|
// might make clang crash while printing the expressions.
|
|
|
|
Constant.Val.isStruct() || Constant.Val.isUnion())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2019-11-16 21:15:05 +00:00
|
|
|
|
|
|
|
// Show enums symbolically, not numerically like APValue::printPretty().
|
2022-09-05 09:14:27 +00:00
|
|
|
if (T->isEnumeralType() && Constant.Val.isInt() &&
|
2023-02-20 07:56:52 +00:00
|
|
|
Constant.Val.getInt().getSignificantBits() <= 64) {
|
2019-11-16 21:15:05 +00:00
|
|
|
// 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)
|
2021-02-22 22:05:48 +00:00
|
|
|
return llvm::formatv("{0} ({1})", ECD->getNameAsString(),
|
|
|
|
printHex(Constant.Val.getInt()))
|
|
|
|
.str();
|
2019-11-16 21:15:05 +00:00
|
|
|
}
|
2021-02-22 22:05:48 +00:00
|
|
|
// Show hex value of integers if they're at least 10 (or negative!)
|
2022-09-05 09:14:27 +00:00
|
|
|
if (T->isIntegralOrEnumerationType() && Constant.Val.isInt() &&
|
2023-02-20 07:56:52 +00:00
|
|
|
Constant.Val.getInt().getSignificantBits() <= 64 &&
|
2021-02-22 22:05:48 +00:00
|
|
|
Constant.Val.getInt().uge(10))
|
|
|
|
return llvm::formatv("{0} ({1})", Constant.Val.getAsString(Ctx, T),
|
|
|
|
printHex(Constant.Val.getInt()))
|
|
|
|
.str();
|
2020-06-22 19:53:47 +00:00
|
|
|
return Constant.Val.getAsString(Ctx, T);
|
2019-11-16 21:15:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 04:19:42 +00:00
|
|
|
std::optional<std::string> printExprValue(const SelectionTree::Node *N,
|
|
|
|
const ASTContext &Ctx) {
|
2019-11-16 21:15:05 +00:00
|
|
|
for (; N; N = N->Parent) {
|
2020-01-04 15:28:41 +00:00
|
|
|
// Try to evaluate the first evaluatable enclosing expression.
|
2019-11-16 21:15:05 +00:00
|
|
|
if (const Expr *E = N->ASTNode.get<Expr>()) {
|
2020-08-20 23:34:48 +00:00
|
|
|
// Once we cross an expression of type 'cv void', the evaluated result
|
|
|
|
// has nothing to do with our original cursor position.
|
|
|
|
if (!E->getType().isNull() && E->getType()->isVoidType())
|
|
|
|
break;
|
2019-11-16 21:15:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2019-11-16 21:15:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 04:19:42 +00:00
|
|
|
std::optional<StringRef> fieldName(const Expr *E) {
|
2020-04-03 15:09:38 +00:00
|
|
|
const auto *ME = llvm::dyn_cast<MemberExpr>(E->IgnoreCasts());
|
|
|
|
if (!ME || !llvm::isa<CXXThisExpr>(ME->getBase()->IgnoreCasts()))
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
[clangd] locateMacroAt handles patched macros
Summary: Depends on D79992.
This patch changes locateMacroAt to perform #line directive substitution
for macro identifier locations.
We first check whether a location is inside a file included through
built-in header. If so we check whether line directive maps it back to
the main file, and afterwards use TokenBuffers to find exact location of
the identifier on the line.
Instead of performing the mapping in locateMacroAt, we could also store
a mapping inside the ParsedAST whenever we use a patched preamble. But
that would imply adding more responsibility to ParsedAST and paying for
the mapping even when it is not going to be used.
====
Go-To-Definition:
Later on these locations are used for serving go-to-definition requests,
this enables jumping to definition inside the preamble section in
presence of patched macros.
=====
Go-To-Refs:
Macro references in main file are collected separetely and stored as a
map from macro's symbol id to reference ranges. Those ranges are
computed inside PPCallbacks, hence we don't have access to TokenBuffer.
In presence of preamble patch, any reference to a macro inside the
preamble section will unfortunately have the wrong range. They'll point
into the patch rather than the main file. Hence during findReferences,
we won't get any ranges reported for those.
Fixing those requires:
- Lexing the preamble section to figure out "real range" of a patched
macro definition
- Postponing range/location calculations until a later step in which we
have access to tokenbuffers.
This patch trades some accuracy in favor of code complexity. We don't do
any patching for references inside the preamble patch but get any
reference inside the main file for free.
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D80198
2020-05-14 10:26:47 +00:00
|
|
|
const auto *Field = llvm::dyn_cast<FieldDecl>(ME->getMemberDecl());
|
2020-04-03 15:09:38 +00:00
|
|
|
if (!Field || !Field->getDeclName().isIdentifier())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
return Field->getDeclName().getAsIdentifierInfo()->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If CMD is of the form T foo() { return FieldName; } then returns "FieldName".
|
2023-01-08 04:19:42 +00:00
|
|
|
std::optional<StringRef> getterVariableName(const CXXMethodDecl *CMD) {
|
2020-04-03 15:09:38 +00:00
|
|
|
assert(CMD->hasBody());
|
|
|
|
if (CMD->getNumParams() != 0 || CMD->isVariadic())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
const auto *Body = llvm::dyn_cast<CompoundStmt>(CMD->getBody());
|
|
|
|
const auto *OnlyReturn = (Body && Body->size() == 1)
|
|
|
|
? llvm::dyn_cast<ReturnStmt>(Body->body_front())
|
|
|
|
: nullptr;
|
|
|
|
if (!OnlyReturn || !OnlyReturn->getRetValue())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
return fieldName(OnlyReturn->getRetValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If CMD is one of the forms:
|
|
|
|
// void foo(T arg) { FieldName = arg; }
|
|
|
|
// R foo(T arg) { FieldName = arg; return *this; }
|
2020-09-29 20:51:14 +00:00
|
|
|
// void foo(T arg) { FieldName = std::move(arg); }
|
|
|
|
// R foo(T arg) { FieldName = std::move(arg); return *this; }
|
2020-04-03 15:09:38 +00:00
|
|
|
// then returns "FieldName"
|
2023-01-08 04:19:42 +00:00
|
|
|
std::optional<StringRef> setterVariableName(const CXXMethodDecl *CMD) {
|
2020-04-03 15:09:38 +00:00
|
|
|
assert(CMD->hasBody());
|
|
|
|
if (CMD->isConst() || CMD->getNumParams() != 1 || CMD->isVariadic())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
const ParmVarDecl *Arg = CMD->getParamDecl(0);
|
|
|
|
if (Arg->isParameterPack())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
|
|
|
|
const auto *Body = llvm::dyn_cast<CompoundStmt>(CMD->getBody());
|
|
|
|
if (!Body || Body->size() == 0 || Body->size() > 2)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
// If the second statement exists, it must be `return this` or `return *this`.
|
|
|
|
if (Body->size() == 2) {
|
|
|
|
auto *Ret = llvm::dyn_cast<ReturnStmt>(Body->body_back());
|
|
|
|
if (!Ret || !Ret->getRetValue())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
const Expr *RetVal = Ret->getRetValue()->IgnoreCasts();
|
|
|
|
if (const auto *UO = llvm::dyn_cast<UnaryOperator>(RetVal)) {
|
|
|
|
if (UO->getOpcode() != UO_Deref)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
RetVal = UO->getSubExpr()->IgnoreCasts();
|
|
|
|
}
|
|
|
|
if (!llvm::isa<CXXThisExpr>(RetVal))
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
}
|
|
|
|
// The first statement must be an assignment of the arg to a field.
|
|
|
|
const Expr *LHS, *RHS;
|
|
|
|
if (const auto *BO = llvm::dyn_cast<BinaryOperator>(Body->body_front())) {
|
|
|
|
if (BO->getOpcode() != BO_Assign)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
LHS = BO->getLHS();
|
|
|
|
RHS = BO->getRHS();
|
|
|
|
} else if (const auto *COCE =
|
|
|
|
llvm::dyn_cast<CXXOperatorCallExpr>(Body->body_front())) {
|
|
|
|
if (COCE->getOperator() != OO_Equal || COCE->getNumArgs() != 2)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
LHS = COCE->getArg(0);
|
|
|
|
RHS = COCE->getArg(1);
|
|
|
|
} else {
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
}
|
2020-09-29 20:51:14 +00:00
|
|
|
|
|
|
|
// Detect the case when the item is moved into the field.
|
|
|
|
if (auto *CE = llvm::dyn_cast<CallExpr>(RHS->IgnoreCasts())) {
|
|
|
|
if (CE->getNumArgs() != 1)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2021-05-03 07:13:56 +00:00
|
|
|
auto *ND = llvm::dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl());
|
2020-09-29 20:51:14 +00:00
|
|
|
if (!ND || !ND->getIdentifier() || ND->getName() != "move" ||
|
|
|
|
!ND->isInStdNamespace())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-09-29 20:51:14 +00:00
|
|
|
RHS = CE->getArg(0);
|
|
|
|
}
|
|
|
|
|
2020-04-03 15:09:38 +00:00
|
|
|
auto *DRE = llvm::dyn_cast<DeclRefExpr>(RHS->IgnoreCasts());
|
|
|
|
if (!DRE || DRE->getDecl() != Arg)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-03 15:09:38 +00:00
|
|
|
return fieldName(LHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string synthesizeDocumentation(const NamedDecl *ND) {
|
|
|
|
if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(ND)) {
|
|
|
|
// Is this an ordinary, non-static method whose definition is visible?
|
|
|
|
if (CMD->getDeclName().isIdentifier() && !CMD->isStatic() &&
|
|
|
|
(CMD = llvm::dyn_cast_or_null<CXXMethodDecl>(CMD->getDefinition())) &&
|
|
|
|
CMD->hasBody()) {
|
|
|
|
if (const auto GetterField = getterVariableName(CMD))
|
|
|
|
return llvm::formatv("Trivial accessor for `{0}`.", *GetterField);
|
|
|
|
if (const auto SetterField = setterVariableName(CMD))
|
|
|
|
return llvm::formatv("Trivial setter for `{0}`.", *SetterField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
/// Generate a \p Hover object given the declaration \p D.
|
2020-12-18 17:39:20 +00:00
|
|
|
HoverInfo getHoverContents(const NamedDecl *D, const PrintingPolicy &PP,
|
2022-01-26 16:55:52 +00:00
|
|
|
const SymbolIndex *Index,
|
|
|
|
const syntax::TokenBuffer &TB) {
|
[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-16 16:00:19 +00:00
|
|
|
HoverInfo HI;
|
2022-06-15 14:10:19 +00:00
|
|
|
auto &Ctx = D->getASTContext();
|
[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-16 16:00:19 +00:00
|
|
|
|
2020-05-27 16:17:07 +00:00
|
|
|
HI.AccessSpecifier = getAccessSpelling(D->getAccess()).str();
|
[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-16 16:00:19 +00:00
|
|
|
HI.NamespaceScope = getNamespaceScope(D);
|
|
|
|
if (!HI.NamespaceScope->empty())
|
|
|
|
HI.NamespaceScope->append("::");
|
|
|
|
HI.LocalScope = getLocalScope(D);
|
|
|
|
if (!HI.LocalScope.empty())
|
|
|
|
HI.LocalScope.append("::");
|
|
|
|
|
2019-12-13 08:34:59 +00:00
|
|
|
HI.Name = printName(Ctx, *D);
|
|
|
|
const auto *CommentD = getDeclForComment(D);
|
|
|
|
HI.Documentation = getDeclComment(Ctx, *CommentD);
|
|
|
|
enhanceFromIndex(HI, *CommentD, Index);
|
2020-04-03 15:09:38 +00:00
|
|
|
if (HI.Documentation.empty())
|
|
|
|
HI.Documentation = synthesizeDocumentation(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-16 16:00:19 +00: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-26 17:06:17 +00: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-16 16:00:19 +00:00
|
|
|
|
|
|
|
// Fill in template params.
|
|
|
|
if (const TemplateDecl *TD = D->getDescribedTemplate()) {
|
|
|
|
HI.TemplateParameters =
|
2020-12-18 17:39:20 +00:00
|
|
|
fetchTemplateParameters(TD->getTemplateParameters(), PP);
|
[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-16 16:00:19 +00:00
|
|
|
D = TD;
|
|
|
|
} else if (const FunctionDecl *FD = D->getAsFunction()) {
|
2020-01-16 14:20:13 +00:00
|
|
|
if (const auto *FTD = FD->getDescribedTemplate()) {
|
[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-16 16:00:19 +00:00
|
|
|
HI.TemplateParameters =
|
2020-12-18 17:39:20 +00:00
|
|
|
fetchTemplateParameters(FTD->getTemplateParameters(), PP);
|
[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-16 16:00:19 +00:00
|
|
|
D = FTD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in types and params.
|
2020-01-07 14:15:00 +00:00
|
|
|
if (const FunctionDecl *FD = getUnderlyingFunction(D))
|
2020-12-18 17:39:20 +00:00
|
|
|
fillFunctionTypeAndParams(HI, D, FD, PP);
|
2020-01-07 14:15:00 +00:00
|
|
|
else if (const auto *VD = dyn_cast<ValueDecl>(D))
|
2022-06-15 14:10:19 +00:00
|
|
|
HI.Type = printType(VD->getType(), Ctx, PP);
|
2020-01-30 13:07:42 +00:00
|
|
|
else if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(D))
|
|
|
|
HI.Type = TTP->wasDeclaredWithTypename() ? "typename" : "class";
|
|
|
|
else if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(D))
|
2020-12-18 17:39:20 +00:00
|
|
|
HI.Type = printType(TTP, PP);
|
2021-12-07 17:16:38 +00:00
|
|
|
else if (const auto *VT = dyn_cast<VarTemplateDecl>(D))
|
2022-06-15 14:10:19 +00:00
|
|
|
HI.Type = printType(VT->getTemplatedDecl()->getType(), Ctx, PP);
|
2021-12-08 04:31:00 +00:00
|
|
|
else if (const auto *TN = dyn_cast<TypedefNameDecl>(D))
|
2022-06-15 14:10:19 +00:00
|
|
|
HI.Type = printType(TN->getUnderlyingType().getDesugaredType(Ctx), Ctx, PP);
|
2021-12-08 04:31:00 +00:00
|
|
|
else if (const auto *TAT = dyn_cast<TypeAliasTemplateDecl>(D))
|
2022-06-15 14:10:19 +00:00
|
|
|
HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
|
[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-16 16:00:19 +00:00
|
|
|
|
|
|
|
// Fill in value with evaluated initializer if possible.
|
|
|
|
if (const auto *Var = dyn_cast<VarDecl>(D)) {
|
2019-11-16 21:15:05 +00: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-16 16:00:19 +00: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())
|
2021-06-11 12:19:00 +00:00
|
|
|
HI.Value = toString(ECD->getInitVal(), 10);
|
[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-16 16:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 16:55:52 +00:00
|
|
|
HI.Definition = printDefinition(D, PP, TB);
|
[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-16 16:00:19 +00:00
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:51:36 +00:00
|
|
|
/// The standard defines __func__ as a "predefined variable".
|
2022-12-16 07:56:52 +00:00
|
|
|
std::optional<HoverInfo>
|
2022-08-19 12:51:36 +00:00
|
|
|
getPredefinedExprHoverContents(const PredefinedExpr &PE, ASTContext &Ctx,
|
|
|
|
const PrintingPolicy &PP) {
|
|
|
|
HoverInfo HI;
|
|
|
|
HI.Name = PE.getIdentKindName();
|
|
|
|
HI.Kind = index::SymbolKind::Variable;
|
|
|
|
HI.Documentation = "Name of the current function (predefined variable)";
|
|
|
|
if (const StringLiteral *Name = PE.getFunctionName()) {
|
|
|
|
HI.Value.emplace();
|
|
|
|
llvm::raw_string_ostream OS(*HI.Value);
|
|
|
|
Name->outputString(OS);
|
|
|
|
HI.Type = printType(Name->getType(), Ctx, PP);
|
|
|
|
} else {
|
|
|
|
// Inside templates, the approximate type `const char[]` is still useful.
|
|
|
|
QualType StringType = Ctx.getIncompleteArrayType(
|
|
|
|
Ctx.CharTy.withConst(), ArrayType::ArraySizeModifier::Normal,
|
|
|
|
/*IndexTypeQuals=*/0);
|
|
|
|
HI.Type = printType(StringType, Ctx, PP);
|
|
|
|
}
|
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
/// Generate a \p Hover object given the macro \p MacroDecl.
|
2022-09-07 15:44:32 +00:00
|
|
|
HoverInfo getHoverContents(const DefinedMacro &Macro, const syntax::Token &Tok,
|
|
|
|
ParsedAST &AST) {
|
[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-16 16:00:19 +00:00
|
|
|
HoverInfo HI;
|
|
|
|
SourceManager &SM = AST.getSourceManager();
|
2020-01-28 19:23:46 +00:00
|
|
|
HI.Name = std::string(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-26 17:06:17 +00: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-16 16:00:19 +00:00
|
|
|
// FIXME: Populate documentation
|
2020-04-05 06:28:11 +00:00
|
|
|
// FIXME: Populate parameters
|
[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-16 16:00:19 +00:00
|
|
|
|
|
|
|
// Try to get the full definition, not just the name
|
|
|
|
SourceLocation StartLoc = Macro.Info->getDefinitionLoc();
|
|
|
|
SourceLocation EndLoc = Macro.Info->getDefinitionEndLoc();
|
[clangd] locateMacroAt handles patched macros
Summary: Depends on D79992.
This patch changes locateMacroAt to perform #line directive substitution
for macro identifier locations.
We first check whether a location is inside a file included through
built-in header. If so we check whether line directive maps it back to
the main file, and afterwards use TokenBuffers to find exact location of
the identifier on the line.
Instead of performing the mapping in locateMacroAt, we could also store
a mapping inside the ParsedAST whenever we use a patched preamble. But
that would imply adding more responsibility to ParsedAST and paying for
the mapping even when it is not going to be used.
====
Go-To-Definition:
Later on these locations are used for serving go-to-definition requests,
this enables jumping to definition inside the preamble section in
presence of patched macros.
=====
Go-To-Refs:
Macro references in main file are collected separetely and stored as a
map from macro's symbol id to reference ranges. Those ranges are
computed inside PPCallbacks, hence we don't have access to TokenBuffer.
In presence of preamble patch, any reference to a macro inside the
preamble section will unfortunately have the wrong range. They'll point
into the patch rather than the main file. Hence during findReferences,
we won't get any ranges reported for those.
Fixing those requires:
- Lexing the preamble section to figure out "real range" of a patched
macro definition
- Postponing range/location calculations until a later step in which we
have access to tokenbuffers.
This patch trades some accuracy in favor of code complexity. We don't do
any patching for references inside the preamble patch but get any
reference inside the main file for free.
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D80198
2020-05-14 10:26:47 +00:00
|
|
|
// Ensure that EndLoc is a valid offset. For example it might come from
|
|
|
|
// preamble, and source file might've changed, in such a scenario EndLoc still
|
|
|
|
// stays valid, but getLocForEndOfToken will fail as it is no longer a valid
|
|
|
|
// offset.
|
|
|
|
// Note that this check is just to ensure there's text data inside the range.
|
|
|
|
// It will still succeed even when the data inside the range is irrelevant to
|
|
|
|
// macro definition.
|
|
|
|
if (SM.getPresumedLoc(EndLoc, /*UseLineDirectives=*/false).isValid()) {
|
2019-12-04 23:09:35 +00: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-16 16:00:19 +00: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();
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 15:44:32 +00:00
|
|
|
|
|
|
|
if (auto Expansion = AST.getTokens().expansionStartingAt(&Tok)) {
|
|
|
|
// We drop expansion that's longer than the threshold.
|
|
|
|
// For extremely long expansion text, it's not readable from hover card
|
|
|
|
// anyway.
|
|
|
|
std::string ExpansionText;
|
|
|
|
for (const auto &ExpandedTok : Expansion->Expanded) {
|
|
|
|
ExpansionText += ExpandedTok.text(SM);
|
|
|
|
ExpansionText += " ";
|
|
|
|
if (ExpansionText.size() > 2048) {
|
|
|
|
ExpansionText.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ExpansionText.empty()) {
|
|
|
|
if (!HI.Definition.empty()) {
|
|
|
|
HI.Definition += "\n\n";
|
|
|
|
}
|
|
|
|
HI.Definition += "// Expands to\n";
|
|
|
|
HI.Definition += ExpansionText;
|
|
|
|
}
|
|
|
|
}
|
[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-16 16:00:19 +00:00
|
|
|
return HI;
|
|
|
|
}
|
2020-01-10 12:11:09 +00:00
|
|
|
|
2021-12-08 04:31:00 +00:00
|
|
|
std::string typeAsDefinition(const HoverInfo::PrintedType &PType) {
|
|
|
|
std::string Result;
|
|
|
|
llvm::raw_string_ostream OS(Result);
|
|
|
|
OS << PType.Type;
|
|
|
|
if (PType.AKA)
|
|
|
|
OS << " // aka: " << *PType.AKA;
|
|
|
|
OS.flush();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:56:52 +00:00
|
|
|
std::optional<HoverInfo> getThisExprHoverContents(const CXXThisExpr *CTE,
|
|
|
|
ASTContext &ASTCtx,
|
|
|
|
const PrintingPolicy &PP) {
|
2020-12-18 15:10:29 +00:00
|
|
|
QualType OriginThisType = CTE->getType()->getPointeeType();
|
|
|
|
QualType ClassType = declaredType(OriginThisType->getAsTagDecl());
|
|
|
|
// For partial specialization class, origin `this` pointee type will be
|
|
|
|
// parsed as `InjectedClassNameType`, which will ouput template arguments
|
|
|
|
// like "type-parameter-0-0". So we retrieve user written class type in this
|
|
|
|
// case.
|
|
|
|
QualType PrettyThisType = ASTCtx.getPointerType(
|
|
|
|
QualType(ClassType.getTypePtr(), OriginThisType.getCVRQualifiers()));
|
|
|
|
|
|
|
|
HoverInfo HI;
|
|
|
|
HI.Name = "this";
|
2021-12-08 04:31:00 +00:00
|
|
|
HI.Definition = typeAsDefinition(printType(PrettyThisType, ASTCtx, PP));
|
2020-12-18 15:10:29 +00:00
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a HoverInfo object given the deduced type \p QT
|
|
|
|
HoverInfo getDeducedTypeHoverContents(QualType QT, const syntax::Token &Tok,
|
|
|
|
ASTContext &ASTCtx,
|
2020-12-18 17:39:20 +00:00
|
|
|
const PrintingPolicy &PP,
|
2020-12-18 15:10:29 +00:00
|
|
|
const SymbolIndex *Index) {
|
|
|
|
HoverInfo HI;
|
|
|
|
// FIXME: distinguish decltype(auto) vs decltype(expr)
|
|
|
|
HI.Name = tok::getTokenName(Tok.kind());
|
|
|
|
HI.Kind = index::SymbolKind::TypeAlias;
|
|
|
|
|
|
|
|
if (QT->isUndeducedAutoType()) {
|
|
|
|
HI.Definition = "/* not deduced */";
|
|
|
|
} else {
|
2021-12-08 04:31:00 +00:00
|
|
|
HI.Definition = typeAsDefinition(printType(QT, ASTCtx, PP));
|
2020-12-18 15:10:29 +00:00
|
|
|
|
|
|
|
if (const auto *D = QT->getAsTagDecl()) {
|
|
|
|
const auto *CommentD = getDeclForComment(D);
|
|
|
|
HI.Documentation = getDeclComment(ASTCtx, *CommentD);
|
|
|
|
enhanceFromIndex(HI, *CommentD, Index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:15:41 +00:00
|
|
|
HoverInfo getStringLiteralContents(const StringLiteral *SL,
|
|
|
|
const PrintingPolicy &PP) {
|
|
|
|
HoverInfo HI;
|
|
|
|
|
|
|
|
HI.Name = "string-literal";
|
|
|
|
HI.Size = (SL->getLength() + 1) * SL->getCharByteWidth();
|
|
|
|
HI.Type = SL->getType().getAsString(PP).c_str();
|
|
|
|
|
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
2020-01-10 12:11:09 +00:00
|
|
|
bool isLiteral(const Expr *E) {
|
|
|
|
// Unfortunately there's no common base Literal classes inherits from
|
2020-06-19 22:05:26 +00:00
|
|
|
// (apart from Expr), therefore these exclusions.
|
2022-01-20 08:30:16 +00:00
|
|
|
return llvm::isa<CompoundLiteralExpr>(E) ||
|
2020-01-10 12:11:09 +00:00
|
|
|
llvm::isa<CXXBoolLiteralExpr>(E) ||
|
|
|
|
llvm::isa<CXXNullPtrLiteralExpr>(E) ||
|
|
|
|
llvm::isa<FixedPointLiteral>(E) || llvm::isa<FloatingLiteral>(E) ||
|
|
|
|
llvm::isa<ImaginaryLiteral>(E) || llvm::isa<IntegerLiteral>(E) ||
|
2022-12-30 11:28:29 +00:00
|
|
|
llvm::isa<StringLiteral>(E) || llvm::isa<UserDefinedLiteral>(E);
|
2020-01-10 12:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StringLiteral getNameForExpr(const Expr *E) {
|
|
|
|
// FIXME: Come up with names for `special` expressions.
|
2020-01-16 21:02:47 +00:00
|
|
|
//
|
|
|
|
// It's an known issue for GCC5, https://godbolt.org/z/Z_tbgi. Work around
|
|
|
|
// that by using explicit conversion constructor.
|
|
|
|
//
|
|
|
|
// TODO: Once GCC5 is fully retired and not the minimal requirement as stated
|
|
|
|
// in `GettingStarted`, please remove the explicit conversion constructor.
|
|
|
|
return llvm::StringLiteral("expression");
|
2020-01-10 12:11:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 11:28:29 +00:00
|
|
|
void maybeAddCalleeArgInfo(const SelectionTree::Node *N, HoverInfo &HI,
|
|
|
|
const PrintingPolicy &PP);
|
|
|
|
|
2020-12-15 08:45:56 +00:00
|
|
|
// Generates hover info for `this` and evaluatable expressions.
|
2020-01-10 12:11:09 +00:00
|
|
|
// FIXME: Support hover for literals (esp user-defined)
|
2022-12-30 11:28:29 +00:00
|
|
|
std::optional<HoverInfo> getHoverContents(const SelectionTree::Node *N,
|
|
|
|
const Expr *E, ParsedAST &AST,
|
2022-12-16 07:56:52 +00:00
|
|
|
const PrintingPolicy &PP,
|
|
|
|
const SymbolIndex *Index) {
|
2022-12-30 11:28:29 +00:00
|
|
|
std::optional<HoverInfo> HI;
|
|
|
|
|
|
|
|
if (const StringLiteral *SL = dyn_cast<StringLiteral>(E)) {
|
|
|
|
// Print the type and the size for string literals
|
|
|
|
HI = getStringLiteralContents(SL, PP);
|
|
|
|
} else if (isLiteral(E)) {
|
|
|
|
// There's not much value in hovering over "42" and getting a hover card
|
|
|
|
// saying "42 is an int", similar for most other literals.
|
|
|
|
// However, if we have CalleeArgInfo, it's still useful to show it.
|
|
|
|
maybeAddCalleeArgInfo(N, HI.emplace(), PP);
|
|
|
|
if (HI->CalleeArgInfo) {
|
|
|
|
// FIXME Might want to show the expression's value here instead?
|
|
|
|
// E.g. if the literal is in hex it might be useful to show the decimal
|
|
|
|
// value here.
|
|
|
|
HI->Name = "literal";
|
|
|
|
return HI;
|
|
|
|
}
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2022-12-30 11:28:29 +00:00
|
|
|
}
|
2020-01-10 12:11:09 +00:00
|
|
|
|
2020-12-15 08:45:56 +00:00
|
|
|
// For `this` expr we currently generate hover with pointee type.
|
2020-12-18 15:10:29 +00:00
|
|
|
if (const CXXThisExpr *CTE = dyn_cast<CXXThisExpr>(E))
|
2022-12-30 11:28:29 +00:00
|
|
|
HI = getThisExprHoverContents(CTE, AST.getASTContext(), PP);
|
2022-08-19 12:51:36 +00:00
|
|
|
if (const PredefinedExpr *PE = dyn_cast<PredefinedExpr>(E))
|
2022-12-30 11:28:29 +00:00
|
|
|
HI = getPredefinedExprHoverContents(*PE, AST.getASTContext(), PP);
|
2020-01-10 12:11:09 +00:00
|
|
|
// For expressions we currently print the type and the value, iff it is
|
|
|
|
// evaluatable.
|
|
|
|
if (auto Val = printExprValue(E, AST.getASTContext())) {
|
2022-12-30 11:28:29 +00:00
|
|
|
HI.emplace();
|
|
|
|
HI->Type = printType(E->getType(), AST.getASTContext(), PP);
|
|
|
|
HI->Value = *Val;
|
|
|
|
HI->Name = std::string(getNameForExpr(E));
|
2020-01-10 12:11:09 +00:00
|
|
|
}
|
2022-12-30 11:28:29 +00:00
|
|
|
|
|
|
|
if (HI)
|
|
|
|
maybeAddCalleeArgInfo(N, *HI, PP);
|
|
|
|
|
|
|
|
return HI;
|
2020-01-10 12:11:09 +00:00
|
|
|
}
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-10-20 10:01:48 +00:00
|
|
|
// Generates hover info for attributes.
|
2022-12-16 07:56:52 +00:00
|
|
|
std::optional<HoverInfo> getHoverContents(const Attr *A, ParsedAST &AST) {
|
2020-10-20 10:01:48 +00:00
|
|
|
HoverInfo HI;
|
|
|
|
HI.Name = A->getSpelling();
|
|
|
|
if (A->hasScope())
|
|
|
|
HI.LocalScope = A->getScopeName()->getName().str();
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(HI.Definition);
|
|
|
|
A->printPretty(OS, AST.getASTContext().getPrintingPolicy());
|
|
|
|
}
|
2021-08-07 22:41:40 +00:00
|
|
|
HI.Documentation = Attr::getDocumentation(A->getKind()).str();
|
2020-10-20 10:01:48 +00:00
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
bool isParagraphBreak(llvm::StringRef Rest) {
|
|
|
|
return Rest.ltrim(" \t").startswith("\n");
|
2020-03-26 08:10:25 +00:00
|
|
|
}
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
bool punctuationIndicatesLineBreak(llvm::StringRef Line) {
|
2020-03-24 11:30:51 +00:00
|
|
|
constexpr llvm::StringLiteral Punctuation = R"txt(.:,;!?)txt";
|
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
Line = Line.rtrim();
|
|
|
|
return !Line.empty() && Punctuation.contains(Line.back());
|
2020-03-26 08:10:25 +00:00
|
|
|
}
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
bool isHardLineBreakIndicator(llvm::StringRef Rest) {
|
2020-03-24 11:30:51 +00:00
|
|
|
// '-'/'*' md list, '@'/'\' documentation command, '>' md blockquote,
|
|
|
|
// '#' headings, '`' code blocks
|
2020-04-04 06:06:24 +00:00
|
|
|
constexpr llvm::StringLiteral LinebreakIndicators = R"txt(-*@\>#`)txt";
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
Rest = Rest.ltrim(" \t");
|
|
|
|
if (Rest.empty())
|
2020-03-24 11:30:51 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
if (LinebreakIndicators.contains(Rest.front()))
|
|
|
|
return true;
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
if (llvm::isDigit(Rest.front())) {
|
|
|
|
llvm::StringRef AfterDigit = Rest.drop_while(llvm::isDigit);
|
|
|
|
if (AfterDigit.startswith(".") || AfterDigit.startswith(")"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-03-26 08:10:25 +00:00
|
|
|
}
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
bool isHardLineBreakAfter(llvm::StringRef Line, llvm::StringRef Rest) {
|
2020-04-30 08:49:32 +00:00
|
|
|
// Should we also consider whether Line is short?
|
2020-04-04 06:06:24 +00:00
|
|
|
return punctuationIndicatesLineBreak(Line) || isHardLineBreakIndicator(Rest);
|
2020-03-24 11:30:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 01:07:10 +00:00
|
|
|
void addLayoutInfo(const NamedDecl &ND, HoverInfo &HI) {
|
2020-07-06 15:12:39 +00:00
|
|
|
if (ND.isInvalidDecl())
|
|
|
|
return;
|
2020-04-03 01:07:10 +00:00
|
|
|
|
2020-07-06 15:12:39 +00:00
|
|
|
const auto &Ctx = ND.getASTContext();
|
2020-04-03 01:07:10 +00:00
|
|
|
if (auto *RD = llvm::dyn_cast<RecordDecl>(&ND)) {
|
|
|
|
if (auto Size = Ctx.getTypeSizeInCharsIfKnown(RD->getTypeForDecl()))
|
|
|
|
HI.Size = Size->getQuantity();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *FD = llvm::dyn_cast<FieldDecl>(&ND)) {
|
[AST] Fix an assertion violation in FieldDecl::getParent.
Summary:
FieldDecl::getParent assumes that the FiledDecl::getDeclContext returns a
RecordDecl, this is true for C/C++, but not for ObjCIvarDecl:
The Decls hierarchy is like following
FieldDecl <-- ObjCIvarDecl
DeclContext <-- ObjCContainerDecl <-- ObjCInterfaceDecl
^
|----- TagDecl <-- RecordDecl
calling getParent() on ObjCIvarDecl will:
1. invoke getDeclContext(), which returns a DeclContext*, which points to an ObjCInterfaceDecl;
2. then downcast the "DeclContext" pointer to a RecordDecl*, and we will hit
the "is_a<RecordDecl>" assertion in llvm::cast (undefined behavior
in release build without assertion enabled);
Fixes https://github.com/clangd/clangd/issues/369
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: rsmith, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79627
2020-05-19 13:26:42 +00:00
|
|
|
const auto *Record = FD->getParent();
|
|
|
|
if (Record)
|
|
|
|
Record = Record->getDefinition();
|
2021-03-11 00:20:36 +00:00
|
|
|
if (Record && !Record->isInvalidDecl() && !Record->isDependentType() &&
|
|
|
|
!FD->isBitField()) {
|
|
|
|
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Record);
|
|
|
|
HI.Offset = Layout.getFieldOffset(FD->getFieldIndex()) / 8;
|
|
|
|
if (auto Size = Ctx.getTypeSizeInCharsIfKnown(FD->getType())) {
|
|
|
|
HI.Size = FD->isZeroSize(Ctx) ? 0 : Size->getQuantity();
|
|
|
|
unsigned EndOfField = *HI.Offset + *HI.Size;
|
|
|
|
|
|
|
|
// Calculate padding following the field.
|
|
|
|
if (!Record->isUnion() &&
|
|
|
|
FD->getFieldIndex() + 1 < Layout.getFieldCount()) {
|
|
|
|
// Measure padding up to the next class field.
|
|
|
|
unsigned NextOffset =
|
|
|
|
Layout.getFieldOffset(FD->getFieldIndex() + 1) / 8;
|
|
|
|
if (NextOffset >= EndOfField) // next field could be a bitfield!
|
|
|
|
HI.Padding = NextOffset - EndOfField;
|
|
|
|
} else {
|
|
|
|
// Measure padding up to the end of the object.
|
|
|
|
HI.Padding = Layout.getSize().getQuantity() - EndOfField;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Offset in a union is always zero, so not really useful to report.
|
|
|
|
if (Record->isUnion())
|
|
|
|
HI.Offset.reset();
|
2020-04-03 01:07:10 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:15:28 +00:00
|
|
|
HoverInfo::PassType::PassMode getPassMode(QualType ParmType) {
|
|
|
|
if (ParmType->isReferenceType()) {
|
|
|
|
if (ParmType->getPointeeType().isConstQualified())
|
|
|
|
return HoverInfo::PassType::ConstRef;
|
|
|
|
return HoverInfo::PassType::Ref;
|
|
|
|
}
|
|
|
|
return HoverInfo::PassType::Value;
|
|
|
|
}
|
|
|
|
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
// If N is passed as argument to a function, fill HI.CalleeArgInfo with
|
|
|
|
// information about that argument.
|
|
|
|
void maybeAddCalleeArgInfo(const SelectionTree::Node *N, HoverInfo &HI,
|
2020-12-18 17:39:20 +00:00
|
|
|
const PrintingPolicy &PP) {
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
const auto &OuterNode = N->outerImplicit();
|
|
|
|
if (!OuterNode.Parent)
|
|
|
|
return;
|
|
|
|
const auto *CE = OuterNode.Parent->ASTNode.get<CallExpr>();
|
|
|
|
if (!CE)
|
|
|
|
return;
|
|
|
|
const FunctionDecl *FD = CE->getDirectCallee();
|
|
|
|
// For non-function-call-like operatators (e.g. operator+, operator<<) it's
|
|
|
|
// not immediattely obvious what the "passed as" would refer to and, given
|
|
|
|
// fixed function signature, the value would be very low anyway, so we choose
|
|
|
|
// to not support that.
|
|
|
|
// Both variadic functions and operator() (especially relevant for lambdas)
|
|
|
|
// should be supported in the future.
|
|
|
|
if (!FD || FD->isOverloadedOperator() || FD->isVariadic())
|
|
|
|
return;
|
|
|
|
|
2023-01-18 14:15:28 +00:00
|
|
|
HoverInfo::PassType PassType;
|
|
|
|
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
// Find argument index for N.
|
|
|
|
for (unsigned I = 0; I < CE->getNumArgs() && I < FD->getNumParams(); ++I) {
|
|
|
|
if (CE->getArg(I) != OuterNode.ASTNode.get<Expr>())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Extract matching argument from function declaration.
|
2023-01-18 14:15:28 +00:00
|
|
|
if (const ParmVarDecl *PVD = FD->getParamDecl(I)) {
|
2020-12-18 17:39:20 +00:00
|
|
|
HI.CalleeArgInfo.emplace(toHoverInfoParam(PVD, PP));
|
2023-01-18 14:15:28 +00:00
|
|
|
if (N == &OuterNode)
|
|
|
|
PassType.PassBy = getPassMode(PVD->getType());
|
|
|
|
}
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!HI.CalleeArgInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If we found a matching argument, also figure out if it's a
|
|
|
|
// [const-]reference. For this we need to walk up the AST from the arg itself
|
|
|
|
// to CallExpr and check all implicit casts, constructor calls, etc.
|
|
|
|
if (const auto *E = N->ASTNode.get<Expr>()) {
|
|
|
|
if (E->getType().isConstQualified())
|
|
|
|
PassType.PassBy = HoverInfo::PassType::ConstRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *CastNode = N->Parent;
|
|
|
|
CastNode != OuterNode.Parent && !PassType.Converted;
|
|
|
|
CastNode = CastNode->Parent) {
|
|
|
|
if (const auto *ImplicitCast = CastNode->ASTNode.get<ImplicitCastExpr>()) {
|
|
|
|
switch (ImplicitCast->getCastKind()) {
|
|
|
|
case CK_NoOp:
|
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
// If it was a reference before, it's still a reference.
|
|
|
|
if (PassType.PassBy != HoverInfo::PassType::Value)
|
|
|
|
PassType.PassBy = ImplicitCast->getType().isConstQualified()
|
|
|
|
? HoverInfo::PassType::ConstRef
|
|
|
|
: HoverInfo::PassType::Ref;
|
|
|
|
break;
|
|
|
|
case CK_LValueToRValue:
|
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_NullToPointer:
|
|
|
|
case CK_NullToMemberPointer:
|
|
|
|
// No longer a reference, but we do not show this as type conversion.
|
|
|
|
PassType.PassBy = HoverInfo::PassType::Value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PassType.PassBy = HoverInfo::PassType::Value;
|
|
|
|
PassType.Converted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (const auto *CtorCall =
|
|
|
|
CastNode->ASTNode.get<CXXConstructExpr>()) {
|
|
|
|
// We want to be smart about copy constructors. They should not show up as
|
|
|
|
// type conversion, but instead as passing by value.
|
|
|
|
if (CtorCall->getConstructor()->isCopyConstructor())
|
|
|
|
PassType.PassBy = HoverInfo::PassType::Value;
|
|
|
|
else
|
|
|
|
PassType.Converted = true;
|
2023-01-25 16:21:29 +00:00
|
|
|
} else if (CastNode->ASTNode.get<MaterializeTemporaryExpr>()) {
|
2022-12-30 11:28:29 +00:00
|
|
|
// Can't bind a non-const-ref to a temporary, so has to be const-ref
|
|
|
|
PassType.PassBy = HoverInfo::PassType::ConstRef;
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
} else { // Unknown implicit node, assume type conversion.
|
|
|
|
PassType.PassBy = HoverInfo::PassType::Value;
|
|
|
|
PassType.Converted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HI.CallPassType.emplace(PassType);
|
|
|
|
}
|
|
|
|
|
2022-09-11 12:54:26 +00:00
|
|
|
const NamedDecl *pickDeclToUse(llvm::ArrayRef<const NamedDecl *> Candidates) {
|
|
|
|
if (Candidates.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// This is e.g the case for
|
|
|
|
// namespace ns { void foo(); }
|
|
|
|
// void bar() { using ns::foo; f^oo(); }
|
|
|
|
// One declaration in Candidates will refer to the using declaration,
|
|
|
|
// which isn't really useful for Hover. So use the other one,
|
|
|
|
// which in this example would be the actual declaration of foo.
|
|
|
|
if (Candidates.size() <= 2) {
|
2022-10-09 12:18:30 +00:00
|
|
|
if (llvm::isa<UsingDecl>(Candidates.front()))
|
2022-09-11 12:54:26 +00:00
|
|
|
return Candidates.back();
|
|
|
|
return Candidates.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
// For something like
|
|
|
|
// namespace ns { void foo(int); void foo(char); }
|
|
|
|
// using ns::foo;
|
|
|
|
// template <typename T> void bar() { fo^o(T{}); }
|
|
|
|
// we actually want to show the using declaration,
|
|
|
|
// it's not clear which declaration to pick otherwise.
|
2023-01-18 14:15:28 +00:00
|
|
|
auto BaseDecls = llvm::make_filter_range(
|
|
|
|
Candidates, [](const NamedDecl *D) { return llvm::isa<UsingDecl>(D); });
|
2022-09-11 12:54:26 +00:00
|
|
|
if (std::distance(BaseDecls.begin(), BaseDecls.end()) == 1)
|
|
|
|
return *BaseDecls.begin();
|
|
|
|
|
|
|
|
return Candidates.front();
|
|
|
|
}
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-12-16 07:56:52 +00:00
|
|
|
std::optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
|
|
|
|
const format::FormatStyle &Style,
|
|
|
|
const SymbolIndex *Index) {
|
2020-12-18 17:39:20 +00:00
|
|
|
PrintingPolicy PP =
|
|
|
|
getPrintingPolicy(AST.getASTContext().getPrintingPolicy());
|
[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-16 16:00:19 +00:00
|
|
|
const SourceManager &SM = AST.getSourceManager();
|
2020-02-26 14:23:40 +00:00
|
|
|
auto CurLoc = sourceLocationInMainFile(SM, Pos);
|
|
|
|
if (!CurLoc) {
|
|
|
|
llvm::consumeError(CurLoc.takeError());
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-02-26 14:23:40 +00:00
|
|
|
}
|
2020-03-01 15:05:12 +00:00
|
|
|
const auto &TB = AST.getTokens();
|
|
|
|
auto TokensTouchingCursor = syntax::spelledTokensTouching(*CurLoc, TB);
|
2020-02-28 08:25:40 +00:00
|
|
|
// Early exit if there were no tokens around the cursor.
|
2020-02-26 14:23:40 +00:00
|
|
|
if (TokensTouchingCursor.empty())
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
[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-16 16:00:19 +00:00
|
|
|
|
2021-08-12 22:04:19 +00:00
|
|
|
// Show full header file path if cursor is on include directive.
|
2022-07-28 09:08:55 +00:00
|
|
|
for (const auto &Inc : AST.getIncludeStructure().MainFileIncludes) {
|
|
|
|
if (Inc.Resolved.empty() || Inc.HashLine != Pos.line)
|
|
|
|
continue;
|
|
|
|
HoverInfo HI;
|
|
|
|
HI.Name = std::string(llvm::sys::path::filename(Inc.Resolved));
|
|
|
|
// FIXME: We don't have a fitting value for Kind.
|
|
|
|
HI.Definition =
|
|
|
|
URIForFile::canonicalize(Inc.Resolved, AST.tuPath()).file().str();
|
|
|
|
HI.DefinitionLanguage = "";
|
|
|
|
return HI;
|
2021-08-12 22:04:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:05:12 +00:00
|
|
|
// To be used as a backup for highlighting the selected token, we use back as
|
|
|
|
// it aligns better with biases elsewhere (editors tend to send the position
|
|
|
|
// for the left of the hovered token).
|
|
|
|
CharSourceRange HighlightRange =
|
|
|
|
TokensTouchingCursor.back().range(SM).toCharRange(SM);
|
2022-12-16 07:56:52 +00:00
|
|
|
std::optional<HoverInfo> HI;
|
2020-02-28 08:25:40 +00:00
|
|
|
// Macros and deducedtype only works on identifiers and auto/decltype keywords
|
|
|
|
// respectively. Therefore they are only trggered on whichever works for them,
|
|
|
|
// similar to SelectionTree::create().
|
2020-02-26 14:23:40 +00:00
|
|
|
for (const auto &Tok : TokensTouchingCursor) {
|
2020-02-28 08:25:40 +00:00
|
|
|
if (Tok.kind() == tok::identifier) {
|
2020-03-01 15:05:12 +00:00
|
|
|
// Prefer the identifier token as a fallback highlighting range.
|
|
|
|
HighlightRange = Tok.range(SM).toCharRange(SM);
|
2020-02-28 08:25:40 +00:00
|
|
|
if (auto M = locateMacroAt(Tok, AST.getPreprocessor())) {
|
2022-09-07 15:44:32 +00:00
|
|
|
HI = getHoverContents(*M, Tok, AST);
|
2020-02-28 08:25:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (Tok.kind() == tok::kw_auto || Tok.kind() == tok::kw_decltype) {
|
|
|
|
if (auto Deduced = getDeducedType(AST.getASTContext(), Tok.location())) {
|
2020-12-18 17:39:20 +00:00
|
|
|
HI = getDeducedTypeHoverContents(*Deduced, Tok, AST.getASTContext(), PP,
|
2020-12-18 15:10:29 +00:00
|
|
|
Index);
|
2020-03-01 15:05:12 +00:00
|
|
|
HighlightRange = Tok.range(SM).toCharRange(SM);
|
2020-02-28 08:25:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-12-18 15:10:29 +00:00
|
|
|
|
|
|
|
// If we can't find interesting hover information for this
|
|
|
|
// auto/decltype keyword, return nothing to avoid showing
|
|
|
|
// irrelevant or incorrect informations.
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-02-28 08:25:40 +00:00
|
|
|
}
|
2020-02-26 14:23:40 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 08:25:40 +00:00
|
|
|
// If it wasn't auto/decltype or macro, look for decls and expressions.
|
|
|
|
if (!HI) {
|
2020-02-26 14:23:40 +00:00
|
|
|
auto Offset = SM.getFileOffset(*CurLoc);
|
2020-02-23 19:03:00 +00:00
|
|
|
// Editors send the position on the left of the hovered character.
|
|
|
|
// So our selection tree should be biased right. (Tested with VSCode).
|
2020-03-01 15:05:12 +00:00
|
|
|
SelectionTree ST =
|
|
|
|
SelectionTree::createRight(AST.getASTContext(), TB, Offset, Offset);
|
2020-02-23 19:03:00 +00:00
|
|
|
if (const SelectionTree::Node *N = ST.commonAncestor()) {
|
2020-03-01 15:05:12 +00:00
|
|
|
// FIXME: Fill in HighlightRange with range coming from N->ASTNode.
|
2021-01-18 07:58:43 +00:00
|
|
|
auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Alias,
|
|
|
|
AST.getHeuristicResolver());
|
2022-09-11 12:54:26 +00:00
|
|
|
if (const auto *DeclToUse = pickDeclToUse(Decls)) {
|
|
|
|
HI = getHoverContents(DeclToUse, PP, Index, TB);
|
2020-04-03 01:07:10 +00:00
|
|
|
// Layout info only shown when hovering on the field/class itself.
|
2022-09-11 12:54:26 +00:00
|
|
|
if (DeclToUse == N->ASTNode.get<Decl>())
|
|
|
|
addLayoutInfo(*DeclToUse, *HI);
|
2019-11-16 21:15:05 +00:00
|
|
|
// Look for a close enclosing expression to show the value of.
|
|
|
|
if (!HI->Value)
|
|
|
|
HI->Value = printExprValue(N, AST.getASTContext());
|
2020-12-18 17:39:20 +00:00
|
|
|
maybeAddCalleeArgInfo(N, *HI, PP);
|
2020-01-10 12:11:09 +00:00
|
|
|
} else if (const Expr *E = N->ASTNode.get<Expr>()) {
|
2022-12-30 11:28:29 +00:00
|
|
|
HI = getHoverContents(N, E, AST, PP, Index);
|
2020-10-20 10:01:48 +00:00
|
|
|
} else if (const Attr *A = N->ASTNode.get<Attr>()) {
|
|
|
|
HI = getHoverContents(A, AST);
|
2019-11-16 21:15:05 +00:00
|
|
|
}
|
|
|
|
// FIXME: support hovers for other nodes?
|
|
|
|
// - built-in types
|
[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-16 16:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HI)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
[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-16 16:00:19 +00:00
|
|
|
|
2022-09-07 15:44:32 +00:00
|
|
|
// Reformat Definition
|
|
|
|
if (!HI->Definition.empty()) {
|
|
|
|
auto Replacements = format::reformat(
|
|
|
|
Style, HI->Definition, tooling::Range(0, HI->Definition.size()));
|
|
|
|
if (auto Formatted =
|
|
|
|
tooling::applyAllReplacements(HI->Definition, Replacements))
|
|
|
|
HI->Definition = *Formatted;
|
|
|
|
}
|
|
|
|
|
2021-08-23 17:58:43 +00:00
|
|
|
HI->DefinitionLanguage = getMarkdownLanguage(AST.getASTContext());
|
2020-03-01 15:05:12 +00:00
|
|
|
HI->SymRange = halfOpenToRange(SM, HighlightRange);
|
[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-16 16:00:19 +00:00
|
|
|
|
|
|
|
return HI;
|
|
|
|
}
|
|
|
|
|
2019-12-10 09:28:37 +00:00
|
|
|
markup::Document HoverInfo::present() const {
|
|
|
|
markup::Document Output;
|
2021-08-12 22:04:19 +00:00
|
|
|
|
2019-12-13 08:34:59 +00:00
|
|
|
// Header contains a text of the form:
|
2020-01-13 16:21:31 +00:00
|
|
|
// variable `var`
|
2019-12-13 08:34:59 +00:00
|
|
|
//
|
|
|
|
// class `X`
|
|
|
|
//
|
2020-01-13 16:21:31 +00:00
|
|
|
// function `foo`
|
|
|
|
//
|
|
|
|
// expression
|
2020-01-10 12:11:09 +00:00
|
|
|
//
|
2020-01-13 16:50:23 +00:00
|
|
|
// Note that we are making use of a level-3 heading because VSCode renders
|
|
|
|
// level 1 and 2 headers in a huge font, see
|
|
|
|
// https://github.com/microsoft/vscode/issues/88417 for details.
|
|
|
|
markup::Paragraph &Header = Output.addHeading(3);
|
2020-01-15 17:09:51 +00:00
|
|
|
if (Kind != index::SymbolKind::Unknown)
|
2020-04-29 23:03:59 +00:00
|
|
|
Header.appendText(index::getSymbolKindString(Kind)).appendSpace();
|
2019-12-13 08:34:59 +00:00
|
|
|
assert(!Name.empty() && "hover triggered on a nameless symbol");
|
|
|
|
Header.appendCode(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-16 16:00:19 +00:00
|
|
|
|
2020-01-13 16:14:24 +00:00
|
|
|
// Put a linebreak after header to increase readability.
|
|
|
|
Output.addRuler();
|
2020-01-13 16:21:31 +00:00
|
|
|
// Print Types on their own lines to reduce chances of getting line-wrapped by
|
|
|
|
// editor, as they might be long.
|
|
|
|
if (ReturnType) {
|
|
|
|
// For functions we display signature in a list form, e.g.:
|
2020-01-24 10:35:24 +00:00
|
|
|
// → `x`
|
2020-01-13 16:21:31 +00:00
|
|
|
// Parameters:
|
|
|
|
// - `bool param1`
|
|
|
|
// - `int param2 = 5`
|
2021-12-08 04:31:00 +00:00
|
|
|
Output.addParagraph().appendText("→ ").appendCode(
|
|
|
|
llvm::to_string(*ReturnType));
|
2021-12-03 04:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Parameters && !Parameters->empty()) {
|
|
|
|
Output.addParagraph().appendText("Parameters: ");
|
|
|
|
markup::BulletList &L = Output.addBulletList();
|
2021-12-08 04:31:00 +00:00
|
|
|
for (const auto &Param : *Parameters)
|
|
|
|
L.addItem().addParagraph().appendCode(llvm::to_string(Param));
|
2019-12-13 08:34:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 04:32:11 +00:00
|
|
|
// Don't print Type after Parameters or ReturnType as this will just duplicate
|
|
|
|
// the information
|
|
|
|
if (Type && !ReturnType && !Parameters)
|
2021-12-08 04:31:00 +00:00
|
|
|
Output.addParagraph().appendText("Type: ").appendCode(
|
|
|
|
llvm::to_string(*Type));
|
2021-12-03 04:32:11 +00:00
|
|
|
|
2019-12-13 08:34:59 +00:00
|
|
|
if (Value) {
|
|
|
|
markup::Paragraph &P = Output.addParagraph();
|
2020-04-29 23:03:59 +00:00
|
|
|
P.appendText("Value = ");
|
2019-12-13 08:34:59 +00:00
|
|
|
P.appendCode(*Value);
|
[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-16 16:00:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 01:07:10 +00:00
|
|
|
if (Offset)
|
|
|
|
Output.addParagraph().appendText(
|
2020-04-04 07:12:30 +00:00
|
|
|
llvm::formatv("Offset: {0} byte{1}", *Offset, *Offset == 1 ? "" : "s")
|
|
|
|
.str());
|
2021-03-11 00:20:36 +00:00
|
|
|
if (Size) {
|
|
|
|
auto &P = Output.addParagraph().appendText(
|
2020-04-04 07:12:30 +00:00
|
|
|
llvm::formatv("Size: {0} byte{1}", *Size, *Size == 1 ? "" : "s").str());
|
2021-03-11 00:20:36 +00:00
|
|
|
if (Padding && *Padding != 0)
|
|
|
|
P.appendText(llvm::formatv(" (+{0} padding)", *Padding).str());
|
|
|
|
}
|
2020-04-03 01:07:10 +00:00
|
|
|
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
if (CalleeArgInfo) {
|
|
|
|
assert(CallPassType);
|
|
|
|
std::string Buffer;
|
|
|
|
llvm::raw_string_ostream OS(Buffer);
|
|
|
|
OS << "Passed ";
|
|
|
|
if (CallPassType->PassBy != HoverInfo::PassType::Value) {
|
|
|
|
OS << "by ";
|
|
|
|
if (CallPassType->PassBy == HoverInfo::PassType::ConstRef)
|
|
|
|
OS << "const ";
|
|
|
|
OS << "reference ";
|
|
|
|
}
|
|
|
|
if (CalleeArgInfo->Name)
|
|
|
|
OS << "as " << CalleeArgInfo->Name;
|
2022-12-30 11:28:29 +00:00
|
|
|
else if (CallPassType->PassBy == HoverInfo::PassType::Value)
|
|
|
|
OS << "by value";
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
if (CallPassType->Converted && CalleeArgInfo->Type)
|
2021-12-08 04:31:00 +00:00
|
|
|
OS << " (converted to " << CalleeArgInfo->Type->Type << ")";
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00:00
|
|
|
Output.addParagraph().appendText(OS.str());
|
|
|
|
}
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
if (!Documentation.empty())
|
2020-03-24 11:30:51 +00:00
|
|
|
parseDocumentation(Documentation, Output);
|
2019-12-13 08:34:59 +00:00
|
|
|
|
|
|
|
if (!Definition.empty()) {
|
2020-01-13 16:14:24 +00:00
|
|
|
Output.addRuler();
|
2022-09-07 15:44:32 +00:00
|
|
|
std::string Buffer;
|
|
|
|
|
|
|
|
if (!Definition.empty()) {
|
|
|
|
// Append scope comment, dropping trailing "::".
|
|
|
|
// Note that we don't print anything for global namespace, to not annoy
|
|
|
|
// non-c++ projects or projects that are not making use of namespaces.
|
|
|
|
if (!LocalScope.empty()) {
|
|
|
|
// Container name, e.g. class, method, function.
|
|
|
|
// We might want to propagate some info about container type to print
|
|
|
|
// function foo, class X, method X::bar, etc.
|
|
|
|
Buffer +=
|
|
|
|
"// In " + llvm::StringRef(LocalScope).rtrim(':').str() + '\n';
|
|
|
|
} else if (NamespaceScope && !NamespaceScope->empty()) {
|
|
|
|
Buffer += "// In namespace " +
|
|
|
|
llvm::StringRef(*NamespaceScope).rtrim(':').str() + '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AccessSpecifier.empty()) {
|
|
|
|
Buffer += AccessSpecifier + ": ";
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer += Definition;
|
2019-12-13 08:34:59 +00:00
|
|
|
}
|
2022-09-07 15:44:32 +00:00
|
|
|
|
|
|
|
Output.addCodeBlock(Buffer, DefinitionLanguage);
|
2019-12-13 08:34:59 +00:00
|
|
|
}
|
[clangd] Improve hover on arguments to function call
Summary:
In cases like:
foo(a, ^b);
We now additionally show the name and type of the parameter to foo that
corresponds that "b" is passed as.
The name should help with understanding what it's used for and type can
be useful to find out if call to foo() can mutate variable "b" or not
(i.e. if it is pass by value, reference, const reference, etc).
Patch By: adamcz@ !
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81169
2020-07-03 09:20:22 +00: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-16 16:00:19 +00:00
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
2020-04-04 07:12:30 +00:00
|
|
|
// If the backtick at `Offset` starts a probable quoted range, return the range
|
|
|
|
// (including the quotes).
|
2023-01-08 04:19:42 +00:00
|
|
|
std::optional<llvm::StringRef> getBacktickQuoteRange(llvm::StringRef Line,
|
|
|
|
unsigned Offset) {
|
2020-04-04 07:12:30 +00:00
|
|
|
assert(Line[Offset] == '`');
|
|
|
|
|
|
|
|
// The open-quote is usually preceded by whitespace.
|
|
|
|
llvm::StringRef Prefix = Line.substr(0, Offset);
|
|
|
|
constexpr llvm::StringLiteral BeforeStartChars = " \t(=";
|
|
|
|
if (!Prefix.empty() && !BeforeStartChars.contains(Prefix.back()))
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-04 07:12:30 +00:00
|
|
|
|
|
|
|
// The quoted string must be nonempty and usually has no leading/trailing ws.
|
|
|
|
auto Next = Line.find('`', Offset + 1);
|
|
|
|
if (Next == llvm::StringRef::npos)
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-04 07:12:30 +00:00
|
|
|
llvm::StringRef Contents = Line.slice(Offset + 1, Next);
|
|
|
|
if (Contents.empty() || isWhitespace(Contents.front()) ||
|
|
|
|
isWhitespace(Contents.back()))
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-04 07:12:30 +00:00
|
|
|
|
|
|
|
// The close-quote is usually followed by whitespace or punctuation.
|
|
|
|
llvm::StringRef Suffix = Line.substr(Next + 1);
|
|
|
|
constexpr llvm::StringLiteral AfterEndChars = " \t)=.,;:";
|
|
|
|
if (!Suffix.empty() && !AfterEndChars.contains(Suffix.front()))
|
2022-12-03 19:54:50 +00:00
|
|
|
return std::nullopt;
|
2020-04-04 07:12:30 +00:00
|
|
|
|
[clangd] locateMacroAt handles patched macros
Summary: Depends on D79992.
This patch changes locateMacroAt to perform #line directive substitution
for macro identifier locations.
We first check whether a location is inside a file included through
built-in header. If so we check whether line directive maps it back to
the main file, and afterwards use TokenBuffers to find exact location of
the identifier on the line.
Instead of performing the mapping in locateMacroAt, we could also store
a mapping inside the ParsedAST whenever we use a patched preamble. But
that would imply adding more responsibility to ParsedAST and paying for
the mapping even when it is not going to be used.
====
Go-To-Definition:
Later on these locations are used for serving go-to-definition requests,
this enables jumping to definition inside the preamble section in
presence of patched macros.
=====
Go-To-Refs:
Macro references in main file are collected separetely and stored as a
map from macro's symbol id to reference ranges. Those ranges are
computed inside PPCallbacks, hence we don't have access to TokenBuffer.
In presence of preamble patch, any reference to a macro inside the
preamble section will unfortunately have the wrong range. They'll point
into the patch rather than the main file. Hence during findReferences,
we won't get any ranges reported for those.
Fixing those requires:
- Lexing the preamble section to figure out "real range" of a patched
macro definition
- Postponing range/location calculations until a later step in which we
have access to tokenbuffers.
This patch trades some accuracy in favor of code complexity. We don't do
any patching for references inside the preamble patch but get any
reference inside the main file for free.
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D80198
2020-05-14 10:26:47 +00:00
|
|
|
return Line.slice(Offset, Next + 1);
|
2020-04-04 07:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parseDocumentationLine(llvm::StringRef Line, markup::Paragraph &Out) {
|
|
|
|
// Probably this is appendText(Line), but scan for something interesting.
|
|
|
|
for (unsigned I = 0; I < Line.size(); ++I) {
|
|
|
|
switch (Line[I]) {
|
[clangd] locateMacroAt handles patched macros
Summary: Depends on D79992.
This patch changes locateMacroAt to perform #line directive substitution
for macro identifier locations.
We first check whether a location is inside a file included through
built-in header. If so we check whether line directive maps it back to
the main file, and afterwards use TokenBuffers to find exact location of
the identifier on the line.
Instead of performing the mapping in locateMacroAt, we could also store
a mapping inside the ParsedAST whenever we use a patched preamble. But
that would imply adding more responsibility to ParsedAST and paying for
the mapping even when it is not going to be used.
====
Go-To-Definition:
Later on these locations are used for serving go-to-definition requests,
this enables jumping to definition inside the preamble section in
presence of patched macros.
=====
Go-To-Refs:
Macro references in main file are collected separetely and stored as a
map from macro's symbol id to reference ranges. Those ranges are
computed inside PPCallbacks, hence we don't have access to TokenBuffer.
In presence of preamble patch, any reference to a macro inside the
preamble section will unfortunately have the wrong range. They'll point
into the patch rather than the main file. Hence during findReferences,
we won't get any ranges reported for those.
Fixing those requires:
- Lexing the preamble section to figure out "real range" of a patched
macro definition
- Postponing range/location calculations until a later step in which we
have access to tokenbuffers.
This patch trades some accuracy in favor of code complexity. We don't do
any patching for references inside the preamble patch but get any
reference inside the main file for free.
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D80198
2020-05-14 10:26:47 +00:00
|
|
|
case '`':
|
|
|
|
if (auto Range = getBacktickQuoteRange(Line, I)) {
|
|
|
|
Out.appendText(Line.substr(0, I));
|
|
|
|
Out.appendCode(Range->trim("`"), /*Preserve=*/true);
|
|
|
|
return parseDocumentationLine(Line.substr(I + Range->size()), Out);
|
|
|
|
}
|
|
|
|
break;
|
2020-04-04 07:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 23:03:59 +00:00
|
|
|
Out.appendText(Line).appendSpace();
|
2020-04-04 07:12:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:30:51 +00:00
|
|
|
void parseDocumentation(llvm::StringRef Input, markup::Document &Output) {
|
2020-04-04 06:06:24 +00:00
|
|
|
std::vector<llvm::StringRef> ParagraphLines;
|
|
|
|
auto FlushParagraph = [&] {
|
|
|
|
if (ParagraphLines.empty())
|
|
|
|
return;
|
|
|
|
auto &P = Output.addParagraph();
|
|
|
|
for (llvm::StringRef Line : ParagraphLines)
|
2020-04-04 07:12:30 +00:00
|
|
|
parseDocumentationLine(Line, P);
|
2020-04-04 06:06:24 +00:00
|
|
|
ParagraphLines.clear();
|
|
|
|
};
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
llvm::StringRef Line, Rest;
|
|
|
|
for (std::tie(Line, Rest) = Input.split('\n');
|
|
|
|
!(Line.empty() && Rest.empty());
|
|
|
|
std::tie(Line, Rest) = Rest.split('\n')) {
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
// After a linebreak remove spaces to avoid 4 space markdown code blocks.
|
|
|
|
// FIXME: make FlushParagraph handle this.
|
|
|
|
Line = Line.ltrim();
|
|
|
|
if (!Line.empty())
|
|
|
|
ParagraphLines.push_back(Line);
|
2020-03-24 11:30:51 +00:00
|
|
|
|
2020-04-04 06:06:24 +00:00
|
|
|
if (isParagraphBreak(Rest) || isHardLineBreakAfter(Line, Rest)) {
|
|
|
|
FlushParagraph();
|
2020-03-24 11:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 06:06:24 +00:00
|
|
|
FlushParagraph();
|
2020-03-24 11:30:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 04:31:00 +00:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
|
|
|
|
const HoverInfo::PrintedType &T) {
|
|
|
|
OS << T.Type;
|
|
|
|
if (T.AKA)
|
|
|
|
OS << " (aka " << *T.AKA << ")";
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
[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-16 16:00:19 +00:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
|
|
|
|
const HoverInfo::Param &P) {
|
|
|
|
if (P.Type)
|
2021-12-08 04:31:00 +00:00
|
|
|
OS << P.Type->Type;
|
[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-16 16:00:19 +00:00
|
|
|
if (P.Name)
|
2021-12-08 04:31:00 +00:00
|
|
|
OS << " " << *P.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-16 16:00:19 +00:00
|
|
|
if (P.Default)
|
|
|
|
OS << " = " << *P.Default;
|
2021-12-08 04:31:00 +00:00
|
|
|
if (P.Type && P.Type->AKA)
|
|
|
|
OS << " (aka " << *P.Type->AKA << ")";
|
[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-16 16:00:19 +00:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|