diff --git a/clang-tools-extra/clangd/SemanticHighlighting.h b/clang-tools-extra/clangd/SemanticHighlighting.h --- a/clang-tools-extra/clangd/SemanticHighlighting.h +++ b/clang-tools-extra/clangd/SemanticHighlighting.h @@ -71,6 +71,7 @@ DependentName, DefaultLibrary, UsedAsMutableReference, + UsedAsMutablePointer, FunctionScope, ClassScope, diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -572,11 +572,13 @@ 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()) { + const bool IsRef = T->isLValueReferenceType(); + const bool IsPtr = T->isPointerType(); + if ((!IsRef && !IsPtr) || T->getPointeeType().isConstQualified() || + T->isDependentType()) { return; } @@ -585,6 +587,12 @@ // FIXME Add "unwrapping" for ArraySubscriptExpr and UnaryOperator, // 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)) @@ -592,7 +600,8 @@ if (Location) H.addExtraModifier(*Location, - HighlightingModifier::UsedAsMutableReference); + IsRef ? HighlightingModifier::UsedAsMutableReference + : HighlightingModifier::UsedAsMutablePointer); } void @@ -1075,6 +1084,8 @@ return "defaultLibrary"; case HighlightingModifier::UsedAsMutableReference: return "usedAsMutableReference"; // nonstandard + case HighlightingModifier::UsedAsMutablePointer: + return "usedAsMutablePointer"; // nonstandard case HighlightingModifier::FunctionScope: return "functionScope"; // nonstandard case HighlightingModifier::ClassScope: diff --git a/clang-tools-extra/clangd/test/initialize-params.test b/clang-tools-extra/clangd/test/initialize-params.test --- a/clang-tools-extra/clangd/test/initialize-params.test +++ b/clang-tools-extra/clangd/test/initialize-params.test @@ -66,6 +66,7 @@ # CHECK-NEXT: "dependentName", # CHECK-NEXT: "defaultLibrary", # CHECK-NEXT: "usedAsMutableReference", +# CHECK-NEXT: "usedAsMutablePointer", # CHECK-NEXT: "functionScope", # CHECK-NEXT: "classScope", # CHECK-NEXT: "fileScope", diff --git a/clang-tools-extra/clangd/test/semantic-tokens.test b/clang-tools-extra/clangd/test/semantic-tokens.test --- 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: 8193 +# CHECK-NEXT: 16385 # CHECK-NEXT: ], # CHECK-NEXT: "resultId": "1" # CHECK-NEXT: } @@ -49,7 +49,7 @@ # CHECK-NEXT: 4, # CHECK-NEXT: 1, # CHECK-NEXT: 0, -# CHECK-NEXT: 8193 +# CHECK-NEXT: 16385 # 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: 8193, +# CHECK-NEXT: 16385, # CHECK-NEXT: 1, # CHECK-NEXT: 4, # CHECK-NEXT: 1, # CHECK-NEXT: 0, -# CHECK-NEXT: 8193 +# CHECK-NEXT: 16385 # 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 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -374,7 +374,7 @@ void $Function_decl[[foo]]() { $Class[[F]] $LocalVariable_decl[[FF]]; $Class[[G]]<$Class[[F]], &$Class[[F]]::$Method[[f]]> $LocalVariable_decl[[GG]]; - $LocalVariable[[GG]].$Method[[foo]](&$LocalVariable[[FF]]); + $LocalVariable[[GG]].$Method[[foo]](&$LocalVariable_usedAsMutablePointer[[FF]]); $Class[[A]]<$Function[[foo]]> $LocalVariable_decl[[AA]]; } )cpp", @@ -773,14 +773,14 @@ const int* $LocalVariable_decl_readonly[[constPtr]]; int** $LocalVariable_decl[[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]]);