[clang] [Clang] Correctly initialize placeholder fields from their initializers (PR #114196)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 06:19:14 PST 2024


================
@@ -5560,6 +5560,27 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc,
                                    Init, InitializationContext->Context);
 }
 
+static FieldDecl *FindFieldDeclInstantiationPattern(const ASTContext &Ctx,
+                                                    FieldDecl *Field) {
+  if (FieldDecl *Pattern = Ctx.getInstantiatedFromUnnamedFieldDecl(Field))
+    return Pattern;
+  auto *ParentRD = cast<CXXRecordDecl>(Field->getParent());
+  CXXRecordDecl *ClassPattern = ParentRD->getTemplateInstantiationPattern();
+  DeclContext::lookup_result Lookup =
+      ClassPattern->lookup(Field->getDeclName());
+  FieldDecl *Found = nullptr;
+  for (auto *L : Lookup) {
+    if (FieldDecl *Pattern = dyn_cast<FieldDecl>(L)) {
+      assert(!Found && "Duplicated instantiation pattern for field decl");
----------------
erichkeane wrote:

This is probably better as a `llvm::make_filter_range`, or a llvm::find_if, either with `llvm::IsaPred<FieldDecl>`.

Also makes your "ensure we only find one" a little less odd looking, since you could do something like:
`assert(++Itr == range.end())` kinda thing.

https://github.com/llvm/llvm-project/pull/114196


More information about the cfe-commits mailing list