jvikstrom updated this revision to Diff 208143.
jvikstrom added a comment.

Separated into three testcases.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

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
@@ -49,19 +49,34 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
       R"cpp(
-    struct A {
-      double SomeMember;
-    };
-    struct {
-    }   $Variable[[HStruct]];
-    void $Function[[foo]](int $Variable[[a]]) {
-      auto $Variable[[VeryLongVariableName]] = 12312;
-      A     $Variable[[aa]];
-    }
-  )cpp",
+      struct AS {
+        double SomeMember;
+      };
+      struct {
+      } $Variable[[S]];
+      void $Function[[foo]](int $Variable[[A]]) {
+        auto $Variable[[VeryLongVariableName]] = 12312;
+        AS     $Variable[[AA]];
+        auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+        auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
+        $Variable[[FN]](12312);
+      }
+    )cpp",
+      R"cpp(
+      void $Function[[foo]](int);
+      void $Function[[Gah]]();
+      void $Function[[foo]]() {
+        auto $Variable[[Bou]] = $Function[[Gah]];
+      }
+    )cpp",
       R"cpp(
-    void $Function[[foo]](int);
-  )cpp"};
+      struct A {
+        A();
+        ~A();
+        void $Function[[abc]]();
+        void operator<<(int);
+      };
+    )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
@@ -34,26 +34,46 @@
     return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-    addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+    // FIXME: (De)Constructors/operator need to be highlighted some other way.
+    if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
+      return true;
+
+    if (ND->getDeclName().isEmpty())
+      // Don't add symbols that don't have any length.
+      return true;
+    addToken(ND->getLocation(), ND);
     return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-    addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+    if (Ref->getNameInfo().getName().getNameKind() !=
+        DeclarationName::Identifier)
+      // Only want to highlight identifiers.
+      return true;
+
+    addToken(Ref->getLocation(), Ref->getDecl());
     return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-    if (D->getLocation().isMacroID())
-      // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl *D) {
+    if (isa<VarDecl>(D)) {
+      addToken(Loc, HighlightingKind::Variable);
       return;
+    }
+    if (isa<FunctionDecl>(D)) {
+      addToken(Loc, HighlightingKind::Function);
+      return;
+    }
+  }
 
-    if (D->getDeclName().isEmpty())
-      // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+    if (Loc.isMacroID())
+      // FIXME: skip tokens inside macros for now.
       return;
 
-    auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+    auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
     if (!R) {
       // R should always have a value, if it doesn't something is very wrong.
       elog("Tried to add semantic token with an invalid range");
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to