upsj updated this revision to Diff 426343.
upsj marked 4 inline comments as done.
upsj added a comment.
address review comments: Move reference hint to the prefix, test type aliases
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D124359/new/
https://reviews.llvm.org/D124359
Files:
clang-tools-extra/clangd/InlayHints.cpp
clang-tools-extra/clangd/unittests/InlayHintTests.cpp
Index: clang-tools-extra/clangd/unittests/InlayHintTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -157,18 +157,17 @@
foo($param[[i]]);
}
)cpp",
- ExpectedHint{"&", "param"});
+ ExpectedHint{"&: ", "param"});
}
TEST(ParameterHints, NoNameRValueReference) {
- // Reference hint for anonymous r-value ref parameter.
+ // No reference hint for anonymous r-value ref parameter.
assertParameterHints(R"cpp(
void foo(int&&);
void bar() {
foo($param[[42]]);
}
- )cpp",
- ExpectedHint{"&&", "param"});
+ )cpp");
}
TEST(ParameterHints, NameInDefinition) {
@@ -215,18 +214,31 @@
foo($param[[i]]);
}
)cpp",
- ExpectedHint{"param: &", "param"});
+ ExpectedHint{"¶m: ", "param"});
+}
+
+TEST(ParameterHints, NameTypeAliasReference) {
+ // Reference and name hint for l-value ref parameter via type alias.
+ assertParameterHints(R"cpp(
+ using alias = int&;
+ void foo(alias param);
+ void bar() {
+ int i;
+ foo($param[[i]]);
+ }
+ )cpp",
+ ExpectedHint{"¶m: ", "param"});
}
TEST(ParameterHints, NameRValueReference) {
- // Reference and name hint for r-value ref parameter.
+ // Only name hint for r-value ref parameter.
assertParameterHints(R"cpp(
void foo(int&& param);
void bar() {
foo($param[[42]]);
}
)cpp",
- ExpectedHint{"param: &&", "param"});
+ ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, Operator) {
@@ -380,7 +392,7 @@
foo2(param);
}
)cpp",
- ExpectedHint{"&", "param"});
+ ExpectedHint{"&: ", "param"});
}
TEST(ParameterHints, LeadingUnderscore) {
Index: clang-tools-extra/clangd/InlayHints.cpp
===================================================================
--- clang-tools-extra/clangd/InlayHints.cpp
+++ clang-tools-extra/clangd/InlayHints.cpp
@@ -404,17 +404,13 @@
for (size_t I = 0; I < ArgCount; ++I) {
StringRef Name = ParameterNames[I];
- bool NameHint = shouldNameHint(Args[I], Name);
- std::string Suffix = ": ";
- if (!NameHint) {
- Name = "";
- Suffix = "";
- }
- Suffix += getRefSuffix(Params[I]);
+ bool NameHint = shouldHintName(Args[I], Name);
+ bool ReferenceHint = shouldHintReference(Params[I]);
- if (!Name.empty() || !Suffix.empty()) {
+ if (NameHint || ReferenceHint) {
addInlayHint(Args[I]->getSourceRange(), HintSide::Left,
- InlayHintKind::ParameterHint, /*Prefix=*/"", Name, Suffix);
+ InlayHintKind::ParameterHint, ReferenceHint ? "&" : "",
+ NameHint ? Name : "", ": ");
}
}
}
@@ -442,16 +438,7 @@
return WhatItIsSetting.equals_insensitive(ParamNames[0]);
}
- StringRef getRefSuffix(const ParmVarDecl *Param) {
- // If the parameter is a non-const reference type, print an inlay hint
- auto Type = Param->getType();
- return Type->isReferenceType() &&
- !Type.getNonReferenceType().isConstQualified()
- ? (Type->isLValueReferenceType() ? "&" : "&&")
- : "";
- }
-
- bool shouldNameHint(const Expr *Arg, StringRef ParamName) {
+ bool shouldHintName(const Expr *Arg, StringRef ParamName) {
if (ParamName.empty())
return false;
@@ -467,6 +454,13 @@
return true;
}
+ bool shouldHintReference(const ParmVarDecl *Param) {
+ // If the parameter is a non-const reference type, print an inlay hint
+ auto Type = Param->getType();
+ return Type->isLValueReferenceType() &&
+ !Type.getNonReferenceType().isConstQualified();
+ }
+
// Checks if "E" is spelled in the main file and preceded by a C-style comment
// whose contents match ParamName (allowing for whitespace and an optional "="
// at the end.
@@ -580,7 +574,7 @@
return Result;
}
- // We pass HintSide rather than SourceLocation because we want to ensure
+ // We pass HintSide rather than SourceLocation because we want to ensure
// it is in the same file as the common file range.
void addInlayHint(SourceRange R, HintSide Side, InlayHintKind Kind,
llvm::StringRef Prefix, llvm::StringRef Label,
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits