From 2bf960aef08e93d687f21e6d636186561b56cbf3 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 21 Jun 2022 15:56:21 +0200 Subject: [PATCH] [clangd] Add "usedAsMutablePointer" highlighting modifier 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 | 21 ++++++++++++++++----- clang-tools-extra/clangd/SemanticHighlighting.h | 1 + .../clangd/test/initialize-params.test | 1 + clang-tools-extra/clangd/test/semantic-tokens.test | 8 ++++---- .../clangd/unittests/SemanticHighlightingTests.cpp | 6 +++--- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index af3a3e6..dd9392b 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -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& 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 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(Arg)) + Arg = IC->getSubExprAsWritten(); + if (auto *UO = dyn_cast(Arg)) { + if (UO->getOpcode() == UO_AddrOf) + Arg = UO->getSubExpr(); + } if (auto *DR = dyn_cast(Arg)) Location = DR->getLocation(); else if (auto *M = dyn_cast(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: diff --git a/clang-tools-extra/clangd/SemanticHighlighting.h b/clang-tools-extra/clangd/SemanticHighlighting.h index 79ecb34..64ad431 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.h +++ b/clang-tools-extra/clangd/SemanticHighlighting.h @@ -71,6 +71,7 @@ enum class HighlightingModifier { DependentName, DefaultLibrary, UsedAsMutableReference, + UsedAsMutablePointer, ConstructorOrDestructor, FunctionScope, diff --git a/clang-tools-extra/clangd/test/initialize-params.test b/clang-tools-extra/clangd/test/initialize-params.test index eb958ca..a2df61c 100644 --- a/clang-tools-extra/clangd/test/initialize-params.test +++ b/clang-tools-extra/clangd/test/initialize-params.test @@ -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", diff --git a/clang-tools-extra/clangd/test/semantic-tokens.test b/clang-tools-extra/clangd/test/semantic-tokens.test index 5abe78e..b3a92b7 100644 --- a/clang-tools-extra/clangd/test/semantic-tokens.test +++ b/clang-tools-extra/clangd/test/semantic-tokens.test @@ -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, @@ -72,12 +72,12 @@ # 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: } diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index 9abc49b..3ea4a58 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -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]]); -- 2.7.4