[clang] [clang][Interp] Handle delegating constructors (PR #67823)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 29 09:03:04 PDT 2023


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/67823

>From 5e37e30bc9200f71a4ac2a8111a7e6d54d381b69 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 29 Sep 2023 18:00:08 +0200
Subject: [PATCH] [clang][Interp] Handle delegating constructors

---
 clang/lib/AST/Interp/ByteCodeStmtGen.cpp |  8 ++++++++
 clang/test/AST/Interp/records.cpp        | 23 +++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
index 15eae8e20b3a678..838e7bb67d0c076 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
@@ -193,6 +193,14 @@ bool ByteCodeStmtGen<Emitter>::visitFunc(const FunctionDecl *F) {
           return false;
         if (!this->emitPopPtr(InitExpr))
           return false;
+      } else {
+        assert(Init->isDelegatingInitializer());
+        if (!this->emitThis(InitExpr))
+          return false;
+        if (!this->visitInitializer(Init->getInit()))
+          return false;
+        if (!this->emitPopPtr(InitExpr))
+          return false;
       }
     }
   }
diff --git a/clang/test/AST/Interp/records.cpp b/clang/test/AST/Interp/records.cpp
index bcc84087fc54020..3c866825d1f077c 100644
--- a/clang/test/AST/Interp/records.cpp
+++ b/clang/test/AST/Interp/records.cpp
@@ -1066,3 +1066,26 @@ namespace ParenInit {
   constexpr B b(A(1),2);
 }
 #endif
+
+namespace DelegatingConstructors {
+  struct S {
+    int a;
+    constexpr S() : S(10) {}
+    constexpr S(int a) : a(a) {}
+  };
+  constexpr S s = {};
+  static_assert(s.a == 10, "");
+
+  struct B {
+    int a;
+    int b;
+
+    constexpr B(int a) : a(a), b(a + 2) {}
+  };
+  struct A : B {
+    constexpr A() : B(10) {};
+  };
+  constexpr A d4 = {};
+  static_assert(d4.a == 10, "");
+  static_assert(d4.b == 12, "");
+}



More information about the cfe-commits mailing list