From aae7553b3463558c2f7ad40de1b60b989fea0f05 Mon Sep 17 00:00:00 2001 From: Johan Vikstrom Date: Tue, 16 Jul 2019 13:23:12 +0000 Subject: [PATCH] [clangd] Added highlighting for the targets in typedefs and using. Summary: In `typedef int A` the `A` was not highlighted previously. This patch gives `A` the same kind of highlighting that the underlying type has (class/enum) (which in this example is no special highlighting because builtins are not handled yet) Will add highlightings for built ins in another patch. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64754 llvm-svn: 366207 --- .../clangd/SemanticHighlighting.cpp | 16 +++++++++++++--- .../unittests/SemanticHighlightingTests.cpp | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index d64472d8fdb1..b6b1c8d8a2cf 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -93,6 +93,12 @@ public: return true; } + bool VisitTypedefNameDecl(TypedefNameDecl *TD) { + if(const auto *TSI = TD->getTypeSourceInfo()) + addTypeLoc(TD->getLocation(), TSI->getTypeLoc()); + return true; + } + bool VisitTypeLoc(TypeLoc &TL) { // This check is for not getting two entries when there are anonymous // structs. It also makes us not highlight certain namespace qualifiers @@ -101,9 +107,7 @@ public: if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) return true; - if (const Type *TP = TL.getTypePtr()) - if (const TagDecl *TD = TP->getAsTagDecl()) - addToken(TL.getBeginLoc(), TD); + addTypeLoc(TL.getBeginLoc(), TL); return true; } @@ -118,6 +122,12 @@ public: } private: + void addTypeLoc(SourceLocation Loc, const TypeLoc &TL) { + if (const Type *TP = TL.getTypePtr()) + if (const TagDecl *TD = TP->getAsTagDecl()) + addToken(Loc, TD); + } + void addToken(SourceLocation Loc, const NamedDecl *D) { if (D->getDeclName().isIdentifier() && D->getName().empty()) // Don't add symbols that don't have any length. diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index 3a1b1c3e7057..f69e336253ca 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -90,7 +90,7 @@ TEST(SemanticHighlighting, GetsCorrectTokens) { typename T::A* $Field[[D]]; }; $Namespace[[abc]]::$Class[[A]] $Variable[[AA]]; - typedef $Namespace[[abc]]::$Class[[A]] AAA; + typedef $Namespace[[abc]]::$Class[[A]] $Class[[AAA]]; struct $Class[[B]] { $Class[[B]](); ~$Class[[B]](); @@ -173,6 +173,19 @@ TEST(SemanticHighlighting, GetsCorrectTokens) { } int $Variable[[B]]; $Class[[AA]] $Variable[[A]]{$Variable[[B]]}; + )cpp", + R"cpp( + namespace $Namespace[[a]] { + struct $Class[[A]] {}; + } + typedef $Namespace[[a]]::$Class[[A]] $Class[[B]]; + using $Class[[BB]] = $Namespace[[a]]::$Class[[A]]; + enum class $Enum[[E]] {}; + typedef $Enum[[E]] $Enum[[C]]; + typedef $Enum[[C]] $Enum[[CC]]; + using $Enum[[CD]] = $Enum[[CC]]; + $Enum[[CC]] $Function[[f]]($Class[[B]]); + $Enum[[CD]] $Function[[f]]($Class[[BB]]); )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase);