jvikstrom updated this revision to Diff 208354.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.

Type -> Class
Also added more tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64257/new/

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  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
@@ -34,11 +34,12 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map<HighlightingKind, std::string> KindToString{
       {HighlightingKind::Variable, "Variable"},
-      {HighlightingKind::Function, "Function"}};
+      {HighlightingKind::Function, "Function"},
+      {HighlightingKind::Class, "Class"}};
   std::vector<HighlightingToken> ExpectedTokens;
   for (const auto &KindString : KindToString) {
-    std::vector<HighlightingToken> Toks =
-        makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+    std::vector<HighlightingToken> Toks = makeHighlightingTokens(
+        Test.ranges(KindString.second), KindString.first);
     ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +50,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
     R"cpp(
-      struct AS {
+      struct $Class[[AS]] {
         double SomeMember;
       };
-      struct {
+      $Class[[struct]] {
       } $Variable[[S]];
-      void $Function[[foo]](int $Variable[[A]]) {
+      void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
         auto $Variable[[VeryLongVariableName]] = 12312;
-        AS     $Variable[[AA]];
+        $Class[[AS]]     $Variable[[AA]];
         auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
         auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
         $Variable[[FN]](12312);
@@ -70,11 +71,28 @@
       }
     )cpp",
     R"cpp(
-      struct A {
+      namespace abc {
+        template<typename T>
+        struct $Class[[A]] {
+          T t;
+        };
+      }
+      abc::$Class[[A]]<int> $Variable[[AA]];
+      typedef abc::$Class[[A]]<int> AAA;
+      enum class E {};
+      enum EE {};
+      struct $Class[[B]] {
+        E EEE;
+        EE EEEE;
+        $Class[[AAA]] AA;
+      };
+    )cpp",
+    R"cpp(
+      struct $Class[[A]] {
         A();
         ~A();
         void $Function[[abc]]();
-        void operator<<(int);
+        void operator<<($Class[[A]]);
       };
     )cpp"};
   for (const auto &TestCase : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===================================================================
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,9 @@
 # CHECK-NEXT:          [
 # CHECK-NEXT:            "entity.name.function.cpp"
 # CHECK-NEXT:          ]
+# CHECK-NEXT:          [
+# CHECK-NEXT:            "entity.name.type.class.cpp"
+# CHECK-NEXT:          ]
 # CHECK-NEXT:        ]
 # CHECK-NEXT:      },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,7 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -12,6 +12,7 @@
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/TypeLoc.h"
 
 namespace clang {
 namespace clangd {
@@ -56,6 +57,36 @@
     return true;
   }
 
+  bool VisitTypeLoc(TypeLoc &TL) {
+    // The check for DependentName is so namespace qualifiers are not
+    // highlighted. The check for elaborated type is for not getting two entries
+    // whenever there is an anonymous struct.
+    if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::DependentName ||
+        TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) {
+      return true;
+    }
+
+    TagDecl *D = TL.getTypePtr()->getAsTagDecl();
+    if (!D)
+      return true;
+
+    if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
+      if (const auto *DC = RD->getDestructor()) {
+        auto Range = DC->getSourceRange();
+        // Destructors appear as a TagTypeLoc RecordDecl. To not highlight
+        // destructors incorrectly the TagTypeLoc is skipped if it is wrapped
+        // inside the actual destructor.
+        if (Range.isValid() && Range.getBegin() < TL.getBeginLoc() &&
+            TL.getBeginLoc() < Range.getEnd()) {
+          return true;
+        }
+      }
+    }
+
+    addToken(TL.getBeginLoc(), D);
+    return true;
+  }
+
 private:
   void addToken(SourceLocation Loc, const Decl *D) {
     if (isa<VarDecl>(D)) {
@@ -66,6 +97,10 @@
       addToken(Loc, HighlightingKind::Function);
       return;
     }
+    if (isa<CXXRecordDecl>(D)) {
+      addToken(Loc, HighlightingKind::Class);
+      return;
+    }
   }
 
   void addToken(SourceLocation Loc, HighlightingKind Kind) {
@@ -176,6 +211,8 @@
     return "entity.name.function.cpp";
   case HighlightingKind::Variable:
     return "variable.cpp";
+  case HighlightingKind::Class:
+    return "entity.name.type.class.cpp";
   case HighlightingKind::NumKinds:
     llvm_unreachable("must not pass NumKinds to the function");
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to