[PATCH] D64754: [clangd] Added highlighting for the targets in typedefs.

Johan Vikström via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 15 09:53:29 PDT 2019


jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64754

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


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -90,7 +90,7 @@
         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]]();
@@ -166,6 +166,16 @@
         $Variable[[AA]].$Field[[E]].$Field[[C]];
         $Class[[A]]::$Variable[[S]] = 90;
       }
+    )cpp",
+    R"cpp(
+      namespace $Namespace[[a]] {
+        struct $Class[[A]] {};
+      }
+      typedef $Namespace[[a]]::$Class[[A]] $Class[[B]];
+      enum class $Enum[[E]] {};
+      typedef $Enum[[E]] $Enum[[C]];
+      typedef $Enum[[C]] $Enum[[CC]];
+      $Enum[[CC]] $Function[[f]]($Class[[B]]);
     )cpp"};
   for (const auto &TestCase : TestCases) {
     checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -85,6 +85,12 @@
     return true;
   }
 
+  bool VisitTypedefDecl(TypedefDecl *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
@@ -93,9 +99,7 @@
     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;
   }
 
@@ -110,6 +114,12 @@
   }
 
 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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64754.209893.patch
Type: text/x-patch
Size: 2489 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190715/279e973c/attachment.bin>


More information about the cfe-commits mailing list