[clangd] Added highlighting for the targets in typedefs and using.
authorJohan Vikstrom <jvikstrom@google.com>
Tue, 16 Jul 2019 13:23:12 +0000 (13:23 +0000)
committerJohan Vikstrom <jvikstrom@google.com>
Tue, 16 Jul 2019 13:23:12 +0000 (13:23 +0000)
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

clang-tools-extra/clangd/SemanticHighlighting.cpp
clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

index d64472d..b6b1c8d 100644 (file)
@@ -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.
index 3a1b1c3..f69e336 100644 (file)
@@ -90,7 +90,7 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
         typename T::A* $Field[[D]];
       };
       $Namespace[[abc]]::$Class[[A]]<int> $Variable[[AA]];
-      typedef $Namespace[[abc]]::$Class[[A]]<int> AAA;
+      typedef $Namespace[[abc]]::$Class[[A]]<int> $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);