[clangd] Add "usedAsMutablePointer" highlighting modifier
authorChristian Kandeler <christian.kandeler@qt.io>
Tue, 21 Jun 2022 13:56:21 +0000 (15:56 +0200)
committerChristian Kandeler <christian.kandeler@qt.io>
Mon, 7 Nov 2022 10:58:33 +0000 (11:58 +0100)
Counterpart to "usedAsMutableReference". Just as for references, there
are const and non-const pointer parameters, and it's valuable to be able
to have different highlighting for the two cases at the call site.
We could have re-used the existing modifier, but having a dedicated one
maximizes client flexibility.

Reviewed By: nridge

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

clang-tools-extra/clangd/SemanticHighlighting.cpp
clang-tools-extra/clangd/SemanticHighlighting.h
clang-tools-extra/clangd/test/initialize-params.test
clang-tools-extra/clangd/test/semantic-tokens.test
clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

index af3a3e6..dd9392b 100644 (file)
@@ -597,19 +597,27 @@ public:
     if (!Arg)
       return;
 
-    // Is this parameter passed by non-const reference?
+    // Is this parameter passed by non-const pointer or reference?
     // FIXME The condition T->idDependentType() could be relaxed a bit,
     // e.g. std::vector<T>& is dependent but we would want to highlight it
-    if (!T->isLValueReferenceType() ||
-        T.getNonReferenceType().isConstQualified() || T->isDependentType()) {
+    bool IsRef = T->isLValueReferenceType();
+    bool IsPtr = T->isPointerType();
+    if ((!IsRef && !IsPtr) || T->getPointeeType().isConstQualified() ||
+        T->isDependentType()) {
       return;
     }
 
     llvm::Optional<SourceLocation> Location;
 
-    // FIXME Add "unwrapping" for ArraySubscriptExpr and UnaryOperator,
+    // FIXME Add "unwrapping" for ArraySubscriptExpr,
     //  e.g. highlight `a` in `a[i]`
     // FIXME Handle dependent expression types
+    if (auto *IC = dyn_cast<ImplicitCastExpr>(Arg))
+      Arg = IC->getSubExprAsWritten();
+    if (auto *UO = dyn_cast<UnaryOperator>(Arg)) {
+      if (UO->getOpcode() == UO_AddrOf)
+        Arg = UO->getSubExpr();
+    }
     if (auto *DR = dyn_cast<DeclRefExpr>(Arg))
       Location = DR->getLocation();
     else if (auto *M = dyn_cast<MemberExpr>(Arg))
@@ -617,7 +625,8 @@ public:
 
     if (Location)
       H.addExtraModifier(*Location,
-                         HighlightingModifier::UsedAsMutableReference);
+                         IsRef ? HighlightingModifier::UsedAsMutableReference
+                               : HighlightingModifier::UsedAsMutablePointer);
   }
 
   void
@@ -1140,6 +1149,8 @@ llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier) {
     return "defaultLibrary";
   case HighlightingModifier::UsedAsMutableReference:
     return "usedAsMutableReference"; // nonstandard
+  case HighlightingModifier::UsedAsMutablePointer:
+    return "usedAsMutablePointer"; // nonstandard
   case HighlightingModifier::ConstructorOrDestructor:
     return "constructorOrDestructor"; // nonstandard
   case HighlightingModifier::FunctionScope:
index 79ecb34..64ad431 100644 (file)
@@ -71,6 +71,7 @@ enum class HighlightingModifier {
   DependentName,
   DefaultLibrary,
   UsedAsMutableReference,
+  UsedAsMutablePointer,
   ConstructorOrDestructor,
 
   FunctionScope,
index eb958ca..a2df61c 100644 (file)
@@ -68,6 +68,7 @@
 # CHECK-NEXT:            "dependentName",
 # CHECK-NEXT:            "defaultLibrary",
 # CHECK-NEXT:            "usedAsMutableReference",
+# CHECK-NEXT:            "usedAsMutablePointer",
 # CHECK-NEXT:            "constructorOrDestructor",
 # CHECK-NEXT:            "functionScope",
 # CHECK-NEXT:            "classScope",
index 5abe78e..b3a92b7 100644 (file)
@@ -23,7 +23,7 @@
 # CHECK-NEXT:      4,
 # CHECK-NEXT:      1,
 # CHECK-NEXT:      0,
-# CHECK-NEXT:      32771
+# CHECK-NEXT:      65539
 # CHECK-NEXT:    ],
 # CHECK-NEXT:    "resultId": "1"
 # CHECK-NEXT:  }
@@ -49,7 +49,7 @@
 # CHECK-NEXT:          4,
 # CHECK-NEXT:          1,
 # CHECK-NEXT:          0,
-# CHECK-NEXT:          32771
+# CHECK-NEXT:          65539
 # CHECK-NEXT:        ],
 #                    Inserted at position 1
 # CHECK-NEXT:        "deleteCount": 0,
 # CHECK-NEXT:      4,
 # CHECK-NEXT:      1,
 # CHECK-NEXT:      0,
-# CHECK-NEXT:      32771,
+# CHECK-NEXT:      65539,
 # CHECK-NEXT:      1,
 # CHECK-NEXT:      4,
 # CHECK-NEXT:      1,
 # CHECK-NEXT:      0,
-# CHECK-NEXT:      32771
+# CHECK-NEXT:      65539
 # CHECK-NEXT:    ],
 # CHECK-NEXT:    "resultId": "3"
 # CHECK-NEXT:  }
index 9abc49b..3ea4a58 100644 (file)
@@ -382,7 +382,7 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
       void $Function_def[[foo]]() {
         $Class[[F]] $LocalVariable_def[[FF]];
         $Class[[G]]<$Class[[F]], &$Class[[F]]::$Method[[f]]> $LocalVariable_def[[GG]];
-        $LocalVariable[[GG]].$Method[[foo]](&$LocalVariable[[FF]]);
+        $LocalVariable[[GG]].$Method[[foo]](&$LocalVariable_usedAsMutablePointer[[FF]]);
         $Class[[A]]<$Function[[foo]]> $LocalVariable_def[[AA]];
       }
     )cpp",
@@ -781,14 +781,14 @@ sizeof...($TemplateParameter[[Elements]]);
           const int* $LocalVariable_def_readonly[[constPtr]];
           int** $LocalVariable_def[[array]];
           $Function[[fun]]($LocalVariable[[val]], $LocalVariable[[val]],
-                           $LocalVariable[[ptr]], $LocalVariable_readonly[[constPtr]],
+                           $LocalVariable_usedAsMutablePointer[[ptr]], $LocalVariable_readonly[[constPtr]],
                            $LocalVariable_usedAsMutableReference[[val]], $LocalVariable[[val]],
 
                            $LocalVariable_usedAsMutableReference[[ptr]],
                            $LocalVariable_readonly_usedAsMutableReference[[constPtr]],
                            $LocalVariable_readonly[[constPtr]],
 
-                           $LocalVariable[[array]], $LocalVariable_usedAsMutableReference[[array]],
+                           $LocalVariable_usedAsMutablePointer[[array]], $LocalVariable_usedAsMutableReference[[array]],
                            $LocalVariable[[array]]
                            );
           [](int){}($LocalVariable[[val]]);