[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

Johan Vikström via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 4 07:43:43 PDT 2019


jvikstrom updated this revision to Diff 208041.
jvikstrom marked 2 inline comments as done.
jvikstrom added a comment.

Added testcae. Added another bailout from VisitNamedDecl.


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
@@ -57,10 +57,31 @@
     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",
+      R"cpp(
+    struct A {
+      A();
+      ~A();
+      void $Function[[abc]]();
+    };
+    void $Function[[foo]]() {
+      A $Variable[[a]];
+      $Variable[[a]].abc();
+    }
   )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: This also skips constructors and destructors.
+    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");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64199.208041.patch
Type: text/x-patch
Size: 3219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190704/68aff0f4/attachment-0001.bin>


More information about the cfe-commits mailing list