https://github.com/MythreyaK created
https://github.com/llvm/llvm-project/pull/170642
Enabled hints for `CXXParenListInitExpr`,
```cpp
struct Point {
int x;
int y;
};
int main() {
Point p1{1, 2}; // brace syntax <-- already present
Point p2(1, 2); // parens syntax <-- added in this commit
}
```
<img width="434" height="413" alt="image"
src="https://github.com/user-attachments/assets/710d7168-30f7-494d-8b2c-6413fa4152a7"
/>
Fixes clangd/clangd#2540
>From 64e2e838413b55262ab5cb925b80c69968b3c844 Mon Sep 17 00:00:00 2001
From: Mythreya <[email protected]>
Date: Wed, 3 Dec 2025 21:41:40 -0800
Subject: [PATCH 1/2] Enable designator hints test
---
.../clangd/unittests/InlayHintTests.cpp | 27 +++++++++----------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index feb4404b3d2bf..204c022851017 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1147,20 +1147,6 @@ TEST(ParameterHints, CopyOrMoveConstructor) {
)cpp");
}
-TEST(ParameterHints, AggregateInit) {
- // FIXME: This is not implemented yet, but it would be a natural
- // extension to show member names as hints here.
- assertParameterHints(R"cpp(
- struct Point {
- int x;
- int y;
- };
- void bar() {
- Point p{41, 42};
- }
- )cpp");
-}
-
TEST(ParameterHints, UserDefinedLiteral) {
// Do not hint call to user-defined literal operator.
assertParameterHints(R"cpp(
@@ -1746,6 +1732,19 @@ TEST(TypeHints, SubstTemplateParameterAliases) {
}
TEST(DesignatorHints, Basic) {
+ assertDesignatorHints(R"cpp(
+ struct Point {
+ int x;
+ int y;
+ };
+ void bar() {
+ Point p{$x[[41]], $y[[42]]};
+ }
+ )cpp",
+ ExpectedHint{".x=", "x"}, ExpectedHint{".y=", "y"});
+}
+
+TEST(DesignatorHints, BasicArray) {
assertDesignatorHints(R"cpp(
struct S { int x, y, z; };
S s {$x[[1]], $y[[2+2]]};
>From 50042b1b71d30f5eb4fca21d87a0ae4164f5c90b Mon Sep 17 00:00:00 2001
From: Mythreya <[email protected]>
Date: Wed, 3 Dec 2025 23:49:16 -0800
Subject: [PATCH 2/2] [clangd] Add designator hints for parenthesis aggregate
initialization
---
clang-tools-extra/clangd/InlayHints.cpp | 20 +++++++++++++++++++
.../clangd/unittests/InlayHintTests.cpp | 13 ++++++++++++
2 files changed, 33 insertions(+)
diff --git a/clang-tools-extra/clangd/InlayHints.cpp
b/clang-tools-extra/clangd/InlayHints.cpp
index 23bd02304a4fd..edd7b2d38e90c 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -699,6 +699,26 @@ class InlayHintVisitor : public
RecursiveASTVisitor<InlayHintVisitor> {
return InstantiatedFunction->getParamDecl(ParamIdx);
}
+ bool VisitCXXParenListInitExpr(CXXParenListInitExpr *E) {
+ // TODO: Lang check needed?
+ if (!Cfg.InlayHints.Designators &&
+ AST.getLangOpts().LangStd >= LangStandard::Kind::lang_cxx20)
+ return true;
+
+ if (const auto *CXXRecord = E->getType()->getAsCXXRecordDecl()) {
+ const auto &InitExprs = E->getInitExprs();
+ auto RecordFields = CXXRecord->fields().begin();
+
+ for (size_t I = 0; I < InitExprs.size();
+ ++I, RecordFields = std::next(RecordFields)) {
+ // TODO: is prepending "." sufficient?
+ addDesignatorHint(InitExprs[I]->getSourceRange(),
+ "." + RecordFields->getName().str());
+ }
+ }
+ return true;
+ }
+
bool VisitInitListExpr(InitListExpr *Syn) {
// We receive the syntactic form here (shouldVisitImplicitCode() is false).
// This is the one we will ultimately attach designators to.
diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 204c022851017..72e7be2f3d943 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1821,6 +1821,19 @@ TEST(DesignatorHints, NoCrash) {
ExpectedHint{".b=", "b"});
}
+TEST(DesignatorHints, BasicParenInit) {
+ assertDesignatorHints(R"cpp(
+ struct S {
+ int x;
+ int y;
+ int z;
+ };
+ S s ($x[[1]], $y[[2+2]], $z[[4]]);
+ )cpp",
+ ExpectedHint{".x=", "x"}, ExpectedHint{".y=", "y"},
+ ExpectedHint{".z=", "z"});
+}
+
TEST(InlayHints, RestrictRange) {
Annotations Code(R"cpp(
auto a = false;
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits