[clang] de9b3c5 - [clang][Interp] Handle delegating constructors (#67823)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 16 05:11:31 PDT 2023


Author: Timm Baeder
Date: 2023-10-16T14:11:25+02:00
New Revision: de9b3c5eba41fd024aef6dfa4dab0c8feae29b18

URL: https://github.com/llvm/llvm-project/commit/de9b3c5eba41fd024aef6dfa4dab0c8feae29b18
DIFF: https://github.com/llvm/llvm-project/commit/de9b3c5eba41fd024aef6dfa4dab0c8feae29b18.diff

LOG: [clang][Interp] Handle delegating constructors (#67823)

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeStmtGen.cpp
    clang/test/AST/Interp/records.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
index 6193a8d55a14685..509abe3ae867f93 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
@@ -198,6 +198,14 @@ bool ByteCodeStmtGen<Emitter>::visitFunc(const FunctionDecl *F) {
           return false;
         if (!this->emitInitPtrPop(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