[clang-tools-extra] r365205 - [clangd] Added highlighting for variable references (declrefs)

Johan Vikstrom via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 5 06:06:04 PDT 2019


Author: jvikstrom
Date: Fri Jul  5 06:06:03 2019
New Revision: 365205

URL: http://llvm.org/viewvc/llvm-project?rev=365205&view=rev
Log:
[clangd] Added highlighting for variable references (declrefs)

Summary: Added highlighting for variable references using VisitDeclRefExpr.

Reviewers: hokein, sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64199

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

Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=365205&r1=365204&r2=365205&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Fri Jul  5 06:06:03 2019
@@ -34,26 +34,46 @@ public:
     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");

Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=365205&r1=365204&r2=365205&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Fri Jul  5 06:06:03 2019
@@ -48,20 +48,35 @@ void checkHighlightings(llvm::StringRef
 
 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",
-      R"cpp(
-    void $Function[[foo]](int);
-  )cpp"};
+    R"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(
+      struct A {
+        A();
+        ~A();
+        void $Function[[abc]]();
+        void operator<<(int);
+      };
+    )cpp"};
   for (const auto &TestCase : TestCases) {
     checkHighlightings(TestCase);
   }




More information about the cfe-commits mailing list