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

Added overload for addToken and added more code to the test cases.


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
@@ -28,7 +28,7 @@
 
   return Tokens;
 }
-
+#include <iostream>
 void checkHighlightings(llvm::StringRef Code) {
   Annotations Test(Code);
   auto AST = TestTU::withCode(Test.code()).build();
@@ -43,6 +43,14 @@
   }
 
   auto ActualTokens = getSemanticHighlightings(AST);
+  std::cout << "\n\n";
+  for(auto Tok : ExpectedTokens) {
+    std::cout << "E:: " << Tok.R.start.line << ":" << Tok.R.start.character << std::endl;
+  }
+  for(auto Tok : ActualTokens) {
+    std::cout << "A:: " << Tok.R.start.line << ":" << Tok.R.start.character << std::endl;
+  }
+
   EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
 }
 
@@ -57,10 +65,20 @@
     void $Function[[foo]](int $Variable[[a]]) {
       auto $Variable[[VeryLongVariableName]] = 12312;
       A     $Variable[[aa]];
+      auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
     }
   )cpp",
       R"cpp(
     void $Function[[foo]](int);
+  )cpp",
+      R"cpp(
+    void $Function[[gah]]();
+    void $Function[[foo]]() {
+      int $Variable[[b]];
+      auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+      $Variable[[FN]](12312);
+      auto $Variable[[bou]] = $Function[[gah]];
+    }
   )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
@@ -35,25 +35,42 @@
   }
 
   bool VisitVarDecl(VarDecl *Var) {
-    addToken(Var, HighlightingKind::Variable);
+    addNamedDecl(Var);
     return true;
   }
   bool VisitFunctionDecl(FunctionDecl *Func) {
-    addToken(Func, HighlightingKind::Function);
+    addNamedDecl(Func);
+    return true;
+  }
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+    if (Ref->getNameInfo().getName().getNameKind() ==
+        DeclarationName::CXXOperatorName)
+      // Don't want to highlight operator usages.
+      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");
@@ -62,6 +79,13 @@
 
     Tokens.push_back({Kind, R.getValue()});
   }
+
+  void addNamedDecl(const NamedDecl *D) {
+    if (D->getDeclName().isEmpty())
+      // Don't add symbols that don't have any length.
+      return;
+    addToken(D->getLocation(), D);
+  }
 };
 
 // Encode binary data into base64.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to