[PATCH] D66738: [clangd] Added highlighting for structured bindings.

Johan Vikström via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 26 06:22:44 PDT 2019


jvikstrom created this revision.
jvikstrom added reviewers: hokein, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

Structured bindings are in a BindingDecl. The decl the declRefExpr points to are the BindingDecls. So this adds an additional if statement in the addToken function to highlight them. First tries to find the "underlying" decl and highlight as that decl (for example, if it's a field it should be highligted as a field). If that does not work, it will be highlighted as a variable.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66738

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
@@ -431,6 +431,23 @@
         assert($Variable[[x]] != $Variable[[y]]);
         assert($Variable[[x]] != $Function[[f]]());
       }
+    )cpp",
+    R"cpp(
+      struct $Class[[S]] {
+        $Primitive[[float]] $Field[[Member]];
+      };
+      $Class[[S]] $Function[[foo]]();
+      $Primitive[[void]] $Function[[f]]() {
+        $Primitive[[int]] $Variable[[A]][2] = {1,2};
+        auto [$Variable[[B1]], $Variable[[B2]]] = $Variable[[A]];
+        auto& [$Variable[[R1]], $Variable[[R2]]] = $Variable[[A]];
+        $Class[[auto]] [$Field[[M1]]] = $Class[[S]]();
+        $Class[[auto]] [$Field[[F1]]] = $Function[[foo]]();
+        $Field[[M1]] += 12.2;
+        $Variable[[B1]] += 2;
+        $Class[[S]] $Variable[[SArr]][2] = {$Class[[S]](), $Class[[S]]()};
+        auto [$Variable[[S1]], $Variable[[S2]]] = $Variable[[SArr]];
+      }
     )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
@@ -229,6 +229,21 @@
       addToken(Loc, HighlightingKind::Variable);
       return;
     }
+    if (const auto *B = dyn_cast<BindingDecl>(D)) {
+      // If we can find the underlying decl and highlight that we should do it.
+      // So first try to find and highlight the underlying NamedDecl for the
+      // binding if one exist.
+      if (const auto *BB = B->getBinding())
+        if (const auto *RD = BB->getReferencedDeclOfCallee())
+          if (const auto *D = dyn_cast<NamedDecl>(RD)) {
+            addToken(Loc, D);
+            return;
+          }
+      // Could not find a more specific decl for this BindingDecl. So just
+      // highlight as a normal variable.
+      addToken(Loc, HighlightingKind::Variable);
+      return;
+    }
     if (isa<FunctionDecl>(D)) {
       addToken(Loc, HighlightingKind::Function);
       return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66738.217130.patch
Type: text/x-patch
Size: 2309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190826/bb7d0cf0/attachment.bin>


More information about the cfe-commits mailing list