[PATCH] D128977: [clangd] Support "usedAsMutableReference" in member initializations

Christian Kandeler via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 1 02:47:37 PDT 2022


ckandeler created this revision.
Herald added subscribers: usaxena95, kadircet, arphaman.
Herald added a project: All.
ckandeler requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

That is, mark constructor parameters being used to initialize
non-const reference members.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128977

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
@@ -745,6 +745,15 @@
             int &operator[](int &);
             int operator[](int) const;
         };
+        struct $Class_decl[[ClassWithRefMembers]] {
+          $Class_decl[[ClassWithRefMembers]](int $Parameter_decl[[i]])
+            : $Field[[i1]]($Parameter[[i]]),
+              $Field_readonly[[i2]]($Parameter[[i]]),
+              $Field[[i3]]($Parameter_usedAsMutableReference[[i]]) {}
+          int $Field_decl[[i1]];
+          const int &$Field_decl_readonly[[i2]];
+          int &$Field_decl[[i3]];
+        };
         void $Function_decl[[fun]](int, const int,
                                    int*, const int*,
                                    int&, const int&,
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -523,6 +523,8 @@
 /// e.g. highlights dependent names and 'auto' as the underlying type.
 class CollectExtraHighlightings
     : public RecursiveASTVisitor<CollectExtraHighlightings> {
+  using Base = RecursiveASTVisitor<CollectExtraHighlightings>;
+
 public:
   CollectExtraHighlightings(HighlightingsBuilder &H) : H(H) {}
 
@@ -533,6 +535,22 @@
     return true;
   }
 
+  bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
+    if (!Init->isMemberInitializer())
+      return Base::TraverseConstructorInitializer(Init);
+    if (const auto Member = Init->getMember()) {
+      const auto MemberType = Member->getType();
+      if (MemberType->isLValueReferenceType() &&
+          !MemberType->getPointeeType().isConstQualified()) {
+        if (const auto Param = dyn_cast<DeclRefExpr>(Init->getInit())) {
+          H.addExtraModifier(Param->getLocation(),
+                             HighlightingModifier::UsedAsMutableReference);
+        }
+      }
+    }
+    return Base::TraverseConstructorInitializer(Init);
+  }
+
   bool VisitCallExpr(CallExpr *E) {
     // Highlighting parameters passed by non-const reference does not really
     // make sense for literals...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128977.441650.patch
Type: text/x-patch
Size: 2404 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220701/d97a021b/attachment-0001.bin>


More information about the cfe-commits mailing list