|
27 | 27 | #include "clang/AST/ExprCXX.h"
|
28 | 28 | #include "clang/AST/OperationKinds.h"
|
29 | 29 | #include "clang/AST/PrettyPrinter.h"
|
| 30 | +#include "clang/AST/RecursiveASTVisitor.h" |
30 | 31 | #include "clang/AST/Type.h"
|
31 | 32 | #include "clang/Basic/SourceLocation.h"
|
32 | 33 | #include "clang/Basic/Specifiers.h"
|
@@ -550,29 +551,6 @@ HoverInfo getHoverContents(const NamedDecl *D, const SymbolIndex *Index) {
|
550 | 551 | return HI;
|
551 | 552 | }
|
552 | 553 |
|
553 |
| -/// Generate a \p Hover object given the type \p T. |
554 |
| -HoverInfo getHoverContents(QualType T, ASTContext &ASTCtx, |
555 |
| - const SymbolIndex *Index, |
556 |
| - bool SuppressScope = false) { |
557 |
| - HoverInfo HI; |
558 |
| - |
559 |
| - if (const auto *D = T->getAsTagDecl()) { |
560 |
| - HI.Name = printName(ASTCtx, *D); |
561 |
| - HI.Kind = index::getSymbolInfo(D).Kind; |
562 |
| - |
563 |
| - const auto *CommentD = getDeclForComment(D); |
564 |
| - HI.Documentation = getDeclComment(ASTCtx, *CommentD); |
565 |
| - enhanceFromIndex(HI, *CommentD, Index); |
566 |
| - } else { |
567 |
| - // Builtin types |
568 |
| - auto Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy()); |
569 |
| - Policy.SuppressTagKeyword = true; |
570 |
| - Policy.SuppressScope = SuppressScope; |
571 |
| - HI.Name = T.getAsString(Policy); |
572 |
| - } |
573 |
| - return HI; |
574 |
| -} |
575 |
| - |
576 | 554 | /// Generate a \p Hover object given the macro \p MacroDecl.
|
577 | 555 | HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) {
|
578 | 556 | HoverInfo HI;
|
@@ -608,6 +586,52 @@ HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) {
|
608 | 586 | return HI;
|
609 | 587 | }
|
610 | 588 |
|
| 589 | +llvm::Optional<HoverInfo> getThisExprHoverContents(const CXXThisExpr *CTE, |
| 590 | + ASTContext &ASTCtx) { |
| 591 | + QualType OriginThisType = CTE->getType()->getPointeeType(); |
| 592 | + QualType ClassType = declaredType(OriginThisType->getAsTagDecl()); |
| 593 | + // For partial specialization class, origin `this` pointee type will be |
| 594 | + // parsed as `InjectedClassNameType`, which will ouput template arguments |
| 595 | + // like "type-parameter-0-0". So we retrieve user written class type in this |
| 596 | + // case. |
| 597 | + QualType PrettyThisType = ASTCtx.getPointerType( |
| 598 | + QualType(ClassType.getTypePtr(), OriginThisType.getCVRQualifiers())); |
| 599 | + |
| 600 | + auto Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy()); |
| 601 | + Policy.SuppressTagKeyword = true; |
| 602 | + Policy.SuppressScope = true; |
| 603 | + HoverInfo HI; |
| 604 | + HI.Name = "this"; |
| 605 | + HI.Definition = PrettyThisType.getAsString(Policy); |
| 606 | + return HI; |
| 607 | +} |
| 608 | + |
| 609 | +/// Generate a HoverInfo object given the deduced type \p QT |
| 610 | +HoverInfo getDeducedTypeHoverContents(QualType QT, const syntax::Token &Tok, |
| 611 | + ASTContext &ASTCtx, |
| 612 | + const SymbolIndex *Index) { |
| 613 | + HoverInfo HI; |
| 614 | + // FIXME: distinguish decltype(auto) vs decltype(expr) |
| 615 | + HI.Name = tok::getTokenName(Tok.kind()); |
| 616 | + HI.Kind = index::SymbolKind::TypeAlias; |
| 617 | + |
| 618 | + auto PP = printingPolicyForDecls(ASTCtx.getLangOpts()); |
| 619 | + |
| 620 | + if (QT->isUndeducedAutoType()) { |
| 621 | + HI.Definition = "/* not deduced */"; |
| 622 | + } else { |
| 623 | + HI.Definition = QT.getAsString(PP); |
| 624 | + |
| 625 | + if (const auto *D = QT->getAsTagDecl()) { |
| 626 | + const auto *CommentD = getDeclForComment(D); |
| 627 | + HI.Documentation = getDeclComment(ASTCtx, *CommentD); |
| 628 | + enhanceFromIndex(HI, *CommentD, Index); |
| 629 | + } |
| 630 | + } |
| 631 | + |
| 632 | + return HI; |
| 633 | +} |
| 634 | + |
611 | 635 | bool isLiteral(const Expr *E) {
|
612 | 636 | // Unfortunately there's no common base Literal classes inherits from
|
613 | 637 | // (apart from Expr), therefore these exclusions.
|
@@ -641,18 +665,8 @@ llvm::Optional<HoverInfo> getHoverContents(const Expr *E, ParsedAST &AST,
|
641 | 665 |
|
642 | 666 | HoverInfo HI;
|
643 | 667 | // For `this` expr we currently generate hover with pointee type.
|
644 |
| - if (const CXXThisExpr *CTE = dyn_cast<CXXThisExpr>(E)) { |
645 |
| - QualType OriginThisType = CTE->getType()->getPointeeType(); |
646 |
| - QualType ClassType = declaredType(OriginThisType->getAsTagDecl()); |
647 |
| - // For partial specialization class, origin `this` pointee type will be |
648 |
| - // parsed as `InjectedClassNameType`, which will ouput template arguments |
649 |
| - // like "type-parameter-0-0". So we retrieve user written class type in this |
650 |
| - // case. |
651 |
| - QualType PrettyThisType = AST.getASTContext().getPointerType( |
652 |
| - QualType(ClassType.getTypePtr(), OriginThisType.getCVRQualifiers())); |
653 |
| - return getHoverContents(PrettyThisType, AST.getASTContext(), Index, |
654 |
| - /*SuppressScope=*/true); |
655 |
| - } |
| 668 | + if (const CXXThisExpr *CTE = dyn_cast<CXXThisExpr>(E)) |
| 669 | + return getThisExprHoverContents(CTE, AST.getASTContext()); |
656 | 670 | // For expressions we currently print the type and the value, iff it is
|
657 | 671 | // evaluatable.
|
658 | 672 | if (auto Val = printExprValue(E, AST.getASTContext())) {
|
@@ -849,10 +863,16 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
|
849 | 863 | }
|
850 | 864 | } else if (Tok.kind() == tok::kw_auto || Tok.kind() == tok::kw_decltype) {
|
851 | 865 | if (auto Deduced = getDeducedType(AST.getASTContext(), Tok.location())) {
|
852 |
| - HI = getHoverContents(*Deduced, AST.getASTContext(), Index); |
| 866 | + HI = getDeducedTypeHoverContents(*Deduced, Tok, AST.getASTContext(), |
| 867 | + Index); |
853 | 868 | HighlightRange = Tok.range(SM).toCharRange(SM);
|
854 | 869 | break;
|
855 | 870 | }
|
| 871 | + |
| 872 | + // If we can't find interesting hover information for this |
| 873 | + // auto/decltype keyword, return nothing to avoid showing |
| 874 | + // irrelevant or incorrect informations. |
| 875 | + return llvm::None; |
856 | 876 | }
|
857 | 877 | }
|
858 | 878 |
|
|
0 commit comments