llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clangd

Author: Mythreya Kuricheti (MythreyaK)

<details>
<summary>Changes</summary>

Enabled hints for `CXXParenListInitExpr`, 

```cpp
struct Point {
  int x;
  int y;
};

int main() {
  Point p1{1, 2};  // brace syntax  &lt;-- already present
  Point p2(1, 2);  // parens syntax &lt;-- added in this commit
}
```

&lt;img width="434" height="413" alt="image" 
src="https://github.com/user-attachments/assets/710d7168-30f7-494d-8b2c-6413fa4152a7";
 /&gt;

Fixes clangd/clangd#<!-- -->2540

---
Full diff: https://github.com/llvm/llvm-project/pull/170642.diff


2 Files Affected:

- (modified) clang-tools-extra/clangd/InlayHints.cpp (+27) 
- (modified) clang-tools-extra/clangd/unittests/InlayHintTests.cpp (+70-14) 


``````````diff
diff --git a/clang-tools-extra/clangd/InlayHints.cpp 
b/clang-tools-extra/clangd/InlayHints.cpp
index 23bd02304a4fd..040015cfc2d13 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -699,6 +699,33 @@ class InlayHintVisitor : public 
RecursiveASTVisitor<InlayHintVisitor> {
     return InstantiatedFunction->getParamDecl(ParamIdx);
   }
 
+  bool VisitCXXParenListInitExpr(CXXParenListInitExpr *E) {
+    if (!Cfg.InlayHints.Designators)
+      return true;
+
+    if (const auto *CXXRecord = E->getType()->getAsCXXRecordDecl()) {
+      const auto &InitExprs = E->getUserSpecifiedInitExprs();
+      size_t InitInx = 0;
+
+      // Inherited members are first, skip them for now.
+      // FIXME: '.base=' or 'base:' hint?
+      for (const auto &_ : CXXRecord->bases()) {
+        InitInx++;
+      }
+
+      // Then the members defined in this record
+      auto RecordFields = CXXRecord->fields().begin();
+
+      for (; InitInx < InitExprs.size();
+           ++InitInx, RecordFields = std::next(RecordFields)) {
+        addDesignatorHint(InitExprs[InitInx]->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 feb4404b3d2bf..d54e36c2f8a3b 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]]};
@@ -1822,6 +1821,63 @@ 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(DesignatorHints, BasicParenInitDerived) {
+  assertDesignatorHints(R"cpp(
+    struct S1 {
+      int a;
+      int b;
+    };
+
+    struct S2 : S1 { 
+      int c;
+      int d; 
+    };
+    S2 s2 ({$a[[0]], $b[[0]]}, $c[[0]], $d[[0]]);
+  )cpp",
+                        // ExpectedHint{"S1:", "S1"},
+                        ExpectedHint{".a=", "a"}, ExpectedHint{".b=", "b"},
+                        ExpectedHint{".c=", "c"}, ExpectedHint{".d=", "d"});
+}
+
+TEST(DesignatorHints, BasicParenInitTemplate) {
+  assertDesignatorHints(R"cpp(
+    template <typename T>
+    struct S1 {
+      int a;
+      int b;
+      T* ptr;
+    };
+
+    struct S2 : S1<S2> {
+      int c;
+      int d;
+      S1<int> mem;
+    };
+
+    int main() {
+      S2 sa ({$a1[[0]], $b1[[0]]}, $c[[0]], $d[[0]], $mem[[S1<int>($a2[[1]], 
$b2[[2]], $ptr[[nullptr]])]]);
+    }
+  )cpp",
+                        ExpectedHint{".a=", "a1"}, ExpectedHint{".b=", "b1"},
+                        ExpectedHint{".c=", "c"}, ExpectedHint{".d=", "d"},
+                        ExpectedHint{".mem=", "mem"}, ExpectedHint{".a=", 
"a2"},
+                        ExpectedHint{".b=", "b2"},
+                        ExpectedHint{".ptr=", "ptr"});
+}
+
 TEST(InlayHints, RestrictRange) {
   Annotations Code(R"cpp(
     auto a = false;

``````````

</details>


https://github.com/llvm/llvm-project/pull/170642
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to