[clang] Increase the limit for ctor initializers (PR #239011)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 12:57:30 PST 2026


https://github.com/erichkeane updated https://github.com/llvm/llvm-project/pull/239011

>From abed8c12f36e58d8888581ee700e6e130fa75a21 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Wed, 4 Nov 2026 14:43:28 -0800
Subject: [PATCH 1/6] Add diag for ctor initializers + increase limit

The below bug reports that we 'assert'/silently fail when given too many
initializers. Also, that the limit of 2^^16 was too small in some
generated code.

This patch does 2 things:

First, it adds a diagnostic/test for when we hit greater than the limit.

Second, it raises the limit to 2^^19.  1 bit was already recovered
thanks to LLDB folks.  I was able to get the other 2 by moving the
deduction candidate kind into the decl type itself, rather than in the
FunctionDecl bits, added as a pointer-int pair, so it does not increase
in size!

OF the other bits suggested: calculating written/inherited prototype
were a bit more of a change, but could trivially be picked up in the
future.

FriendConstraintRefersToEnclosingTemplate actually ended up being quite
a bit bigger than this, since it was a property of the function decl,
  not the friend decl.

SO in the end, we got 19 bits for this, which is hopefully plenty?

Fixes: #219366
---
 clang/docs/ReleaseNotes.md                    |  6 +++++
 clang/include/clang/AST/DeclBase.h            | 27 +++++++------------
 clang/include/clang/AST/DeclCXX.h             | 23 +++++++++++-----
 .../clang/Basic/DiagnosticSemaKinds.td        |  4 +++
 clang/lib/AST/Decl.cpp                        |  2 --
 clang/lib/Sema/SemaDeclCXX.cpp                | 14 ++++++++++
 clang/lib/Serialization/ASTReaderDecl.cpp     |  2 +-
 clang/lib/Serialization/ASTWriterDecl.cpp     |  6 ++---
 .../SemaCXX/ctor-too-many-initializers.cpp    | 24 +++++++++++++++++
 9 files changed, 77 insertions(+), 31 deletions(-)
 create mode 100644 clang/test/SemaCXX/ctor-too-many-initializers.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 456c861307cb83..7453f596758630 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -784,6 +784,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   `-Wunsafe-buffer-usage-in-format-attr-call` now works when it is the only
   Safe Buffers warning enabled.
 
+- Clang now diagnoses constructors that have more base and member
+  initializers than it can represent (about 500 thousand), instead of asserting
+  or, in builds without assertions, silently miscompiling the constructor.
+  The limit was previously 65535 initializers, but this patch also increased
+  the number of bits used to store the initializers. (#GH219366)
+
 ### Improvements to Clang's time-trace
 
 ### Improvements to Coverage Mapping
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 120aaf5c0b0233..7189f3c8a10a70 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -1738,12 +1738,9 @@ class DeclContext {
 
   /// Stores the bits used by FunctionDecl.
   /// If modified NumFunctionDeclBits and the accessor
-  /// methods in FunctionDecl and CXXDeductionGuideDecl
-  /// (for DeductionCandidateKind) should be updated appropriately.
+  /// methods in FunctionDecl should be updated appropriately.
   class FunctionDeclBitfields {
     friend class FunctionDecl;
-    /// For DeductionCandidateKind
-    friend class CXXDeductionGuideDecl;
     /// For the bits in DeclContextBitfields.
     LLVM_PREFERRED_TYPE(DeclContextBitfields)
     uint64_t : NumDeclContextBits;
@@ -1823,12 +1820,6 @@ class DeclContext {
     LLVM_PREFERRED_TYPE(bool)
     uint64_t IsMultiVersion : 1;
 
-    /// Only used by CXXDeductionGuideDecl. Indicates the kind
-    /// of the Deduction Guide that is implicitly generated
-    /// (used during overload resolution).
-    LLVM_PREFERRED_TYPE(DeductionCandidate)
-    uint64_t DeductionCandidateKind : 2;
-
     /// Store the ODRHash after first calculation.
     LLVM_PREFERRED_TYPE(bool)
     uint64_t HasODRHash : 1;
@@ -1844,7 +1835,7 @@ class DeclContext {
   };
 
   /// Number of inherited and non-inherited bits in FunctionDeclBitfields.
-  enum { NumFunctionDeclBits = NumDeclContextBits + 32 };
+  enum { NumFunctionDeclBits = NumDeclContextBits + 30 };
 
   /// Stores the bits used by CXXConstructorDecl. If modified
   /// NumCXXConstructorDeclBits and the accessor
@@ -1855,12 +1846,12 @@ class DeclContext {
     LLVM_PREFERRED_TYPE(FunctionDeclBitfields)
     uint64_t : NumFunctionDeclBits;
 
-    /// 19 bits to fit in the remaining available space.
-    /// Note that this makes CXXConstructorDeclBitfields take
-    /// exactly 64 bits and thus the width of NumCtorInitializers
-    /// will need to be shrunk if some bit is added to NumDeclContextBitfields,
-    /// NumFunctionDeclBitfields or CXXConstructorDeclBitfields.
-    uint64_t NumCtorInitializers : 16;
+    enum { NumCtorInitializerBits = 19 };
+    enum { MaxNumCtorInitializers = (1u << NumCtorInitializerBits) - 1 };
+
+    /// Uses all of the remaining available space, which makes
+    /// CXXConstructorDeclBitfields take exactly 64 bits.
+    uint64_t NumCtorInitializers : NumCtorInitializerBits;
     LLVM_PREFERRED_TYPE(bool)
     uint64_t IsInheritingConstructor : 1;
 
@@ -1874,7 +1865,7 @@ class DeclContext {
   };
 
   /// Number of inherited and non-inherited bits in CXXConstructorDeclBitfields.
-  enum { NumCXXConstructorDeclBits = NumFunctionDeclBits + 19 };
+  enum { NumCXXConstructorDeclBits = NumFunctionDeclBits + 22 };
 
   /// Stores the bits used by ObjCMethodDecl.
   /// If modified NumObjCMethodDeclBits and the accessor
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index cf61653728cb65..4ef109ce7192e5 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -2022,14 +2022,16 @@ class CXXDeductionGuideDecl : public FunctionDecl {
       : FunctionDecl(CXXDeductionGuide, C, DC, StartLoc, NameInfo, T, TInfo,
                      SC_None, false, false, ConstexprSpecKind::Unspecified,
                      TrailingRequiresClause),
-        Ctor(Ctor), ExplicitSpec(ES),
+        CtorAndKind(Ctor, Kind), ExplicitSpec(ES),
         SourceDeductionGuide(GeneratedFrom, SourceKind) {
     if (EndLocation.isValid())
       setRangeEnd(EndLocation);
-    setDeductionCandidateKind(Kind);
   }
 
-  CXXConstructorDecl *Ctor;
+  /// The constructor from which this guide was generated, if any, and the kind
+  /// of the guide that is implicitly generated (used during overload
+  /// resolution).
+  llvm::PointerIntPair<CXXConstructorDecl *, 2, DeductionCandidate> CtorAndKind;
   ExplicitSpecifier ExplicitSpec;
   // The deduction guide, if any, that this deduction guide was generated from,
   // in the case of alias template deduction. The SourceDeductionGuideKind
@@ -2068,7 +2070,9 @@ class CXXDeductionGuideDecl : public FunctionDecl {
 
   /// Get the constructor from which this deduction guide was generated, if
   /// this is an implicit deduction guide.
-  CXXConstructorDecl *getCorrespondingConstructor() const { return Ctor; }
+  CXXConstructorDecl *getCorrespondingConstructor() const {
+    return CtorAndKind.getPointer();
+  }
 
   /// Get the deduction guide from which this deduction guide was generated,
   /// if it was generated as part of alias template deduction or from an
@@ -2090,12 +2094,11 @@ class CXXDeductionGuideDecl : public FunctionDecl {
   }
 
   void setDeductionCandidateKind(DeductionCandidate K) {
-    FunctionDeclBits.DeductionCandidateKind = static_cast<unsigned char>(K);
+    CtorAndKind.setInt(K);
   }
 
   DeductionCandidate getDeductionCandidateKind() const {
-    return static_cast<DeductionCandidate>(
-        FunctionDeclBits.DeductionCandidateKind);
+    return CtorAndKind.getInt();
   }
 
   // Implement isa/cast/dyncast/etc.
@@ -2769,6 +2772,12 @@ class CXXConstructorDecl final
     return init_const_reverse_iterator(init_begin());
   }
 
+  /// The maximum number of base and member initializers a constructor can
+  /// have; this is limited by the width of the NumCtorInitializers bitfield.
+  static constexpr unsigned getMaxNumCtorInitializers() {
+    return DeclContext::CXXConstructorDeclBitfields::MaxNumCtorInitializers;
+  }
+
   /// Determine the number of arguments used to initialize the member
   /// or base.
   unsigned getNumCtorInitializers() const {
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 941a58ae54a05e..5bfe259cb057a3 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10244,6 +10244,10 @@ def err_multiple_mem_union_initialization : Error <
 def err_multiple_base_initialization : Error <
   "multiple initializations given for base %0">;
 
+def err_ctor_too_many_initializers : Error<
+  "constructor has too many base and member initializers (%0); the maximum "
+  "is %1">;
+
 def err_mem_init_not_member_or_class : Error<
   "member initializer %0 does not name a non-static data member or base "
   "class">;
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index c6c2043614aa90..487ab6985d67c2 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3119,8 +3119,6 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
   FunctionDeclBits.HasSkippedBody = false;
   FunctionDeclBits.WillHaveBody = false;
   FunctionDeclBits.IsMultiVersion = false;
-  FunctionDeclBits.DeductionCandidateKind =
-      static_cast<unsigned char>(DeductionCandidate::Normal);
   FunctionDeclBits.HasODRHash = false;
   FunctionDeclBits.FriendConstraintRefersToEnclosingTemplate = false;
 
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 9ddb077037397e..2e870e2d448132 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -5542,6 +5542,14 @@ bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors,
     // Just store the initializers as written, they will be checked during
     // instantiation.
     if (!Initializers.empty()) {
+      if (Initializers.size() >
+          CXXConstructorDecl::getMaxNumCtorInitializers()) {
+        Diag(Constructor->getLocation(), diag::err_ctor_too_many_initializers)
+            << Initializers.size()
+            << CXXConstructorDecl::getMaxNumCtorInitializers();
+        Constructor->setInvalidDecl();
+        return true;
+      }
       Constructor->setNumCtorInitializers(Initializers.size());
       CXXCtorInitializer **baseOrMemberInitializers =
         new (Context) CXXCtorInitializer*[Initializers.size()];
@@ -5694,6 +5702,12 @@ bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors,
   }
 
   unsigned NumInitializers = Info.AllToInit.size();
+  if (NumInitializers > CXXConstructorDecl::getMaxNumCtorInitializers()) {
+    Diag(Constructor->getLocation(), diag::err_ctor_too_many_initializers)
+        << NumInitializers << CXXConstructorDecl::getMaxNumCtorInitializers();
+    Constructor->setInvalidDecl();
+    return true;
+  }
   if (NumInitializers > 0) {
     Constructor->setNumCtorInitializers(NumInitializers);
     CXXCtorInitializer **baseOrMemberInitializers =
diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index d7d650ef021923..145a2663424644 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -2333,7 +2333,7 @@ RedeclarableResult ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
 
 void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
   D->setExplicitSpecifier(Record.readExplicitSpec());
-  D->Ctor = readDeclAs<CXXConstructorDecl>();
+  D->CtorAndKind.setPointer(readDeclAs<CXXConstructorDecl>());
   VisitFunctionDecl(D);
   D->setDeductionCandidateKind(
       static_cast<DeductionCandidate>(Record.readInt()));
diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp
index 75be3ea7ee3904..02cbdc71feedad 100644
--- a/clang/lib/Serialization/ASTWriterDecl.cpp
+++ b/clang/lib/Serialization/ASTWriterDecl.cpp
@@ -771,7 +771,7 @@ void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
 }
 
 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
-  static_assert(DeclContext::NumFunctionDeclBits == 44,
+  static_assert(DeclContext::NumFunctionDeclBits == 42,
                 "You need to update the serializer after you change the "
                 "FunctionDeclBits");
 
@@ -944,7 +944,7 @@ static void addExplicitSpecifier(ExplicitSpecifier ES,
 
 void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
   addExplicitSpecifier(D->getExplicitSpecifier(), Record);
-  Record.AddDeclRef(D->Ctor);
+  Record.AddDeclRef(D->getCorrespondingConstructor());
   VisitFunctionDecl(D);
   Record.push_back(static_cast<unsigned char>(D->getDeductionCandidateKind()));
   Record.AddDeclRef(D->getSourceDeductionGuide());
@@ -1767,7 +1767,7 @@ void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
 }
 
 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
-  static_assert(DeclContext::NumCXXConstructorDeclBits == 63,
+  static_assert(DeclContext::NumCXXConstructorDeclBits == 64,
                 "You need to update the serializer after you change the "
                 "CXXConstructorDeclBits");
 
diff --git a/clang/test/SemaCXX/ctor-too-many-initializers.cpp b/clang/test/SemaCXX/ctor-too-many-initializers.cpp
new file mode 100644
index 00000000000000..a464f26d560c51
--- /dev/null
+++ b/clang/test/SemaCXX/ctor-too-many-initializers.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// Note: This test is quite time consuming(~5s on a release-with-asserts), so if
+// the limit increases at all we should consider just deleting this test.
+
+#define F0(p) int p;
+#define F1(p) F0(p##0) F0(p##1) F0(p##2) F0(p##3)
+#define F2(p) F1(p##0) F1(p##1) F1(p##2) F1(p##3)
+#define F3(p) F2(p##0) F2(p##1) F2(p##2) F2(p##3)
+#define F4(p) F3(p##0) F3(p##1) F3(p##2) F3(p##3)
+#define F5(p) F4(p##0) F4(p##1) F4(p##2) F4(p##3)
+#define F6(p) F5(p##0) F5(p##1) F5(p##2) F5(p##3)
+#define F7(p) F6(p##0) F6(p##1) F6(p##2) F6(p##3)
+#define F8(p) F7(p##0) F7(p##1) F7(p##2) F7(p##3)
+#define F9(p) F8(p##0) F8(p##1) F8(p##2) F8(p##3)
+
+// expected-error at +1 {{constructor has too many base and member initializers (524288); the maximum is 524287}}
+struct TooMany {
+  F9(a)
+  F9(b)
+};
+
+// expected-note at +1 {{in implicit copy constructor for 'TooMany' first required here}}
+void f(TooMany &a) { TooMany b(a); }

>From 6d080b1b65b4ab2080f2aa3f520782623ac0d127 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Thu, 5 Nov 2026 06:56:55 -0800
Subject: [PATCH 2/6] Remove the num-inits from the bitfields entirely!

---
 clang/docs/ReleaseNotes.md                    |  7 ++----
 clang/include/clang/AST/DeclBase.h            | 10 +++-----
 clang/include/clang/AST/DeclCXX.h             | 17 ++++---------
 .../clang/Basic/DiagnosticSemaKinds.td        |  4 ----
 clang/lib/Sema/SemaDeclCXX.cpp                | 14 -----------
 clang/lib/Serialization/ASTWriterDecl.cpp     |  2 +-
 .../SemaCXX/ctor-too-many-initializers.cpp    | 24 -------------------
 7 files changed, 10 insertions(+), 68 deletions(-)
 delete mode 100644 clang/test/SemaCXX/ctor-too-many-initializers.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 7453f596758630..945a177422aa03 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -784,11 +784,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   `-Wunsafe-buffer-usage-in-format-attr-call` now works when it is the only
   Safe Buffers warning enabled.
 
-- Clang now diagnoses constructors that have more base and member
-  initializers than it can represent (about 500 thousand), instead of asserting
-  or, in builds without assertions, silently miscompiling the constructor.
-  The limit was previously 65535 initializers, but this patch also increased
-  the number of bits used to store the initializers. (#GH219366)
+- Clang will now no longer assert or miscompile on a constructor that has more than 65535 base and member initializers.
+  Or limit is now effectively 2^32-1.
 
 ### Improvements to Clang's time-trace
 
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 7189f3c8a10a70..317486e9c4ec6a 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -1846,12 +1846,6 @@ class DeclContext {
     LLVM_PREFERRED_TYPE(FunctionDeclBitfields)
     uint64_t : NumFunctionDeclBits;
 
-    enum { NumCtorInitializerBits = 19 };
-    enum { MaxNumCtorInitializers = (1u << NumCtorInitializerBits) - 1 };
-
-    /// Uses all of the remaining available space, which makes
-    /// CXXConstructorDeclBitfields take exactly 64 bits.
-    uint64_t NumCtorInitializers : NumCtorInitializerBits;
     LLVM_PREFERRED_TYPE(bool)
     uint64_t IsInheritingConstructor : 1;
 
@@ -1862,10 +1856,12 @@ class DeclContext {
     /// Whether this constructor is explicit specified.
     LLVM_PREFERRED_TYPE(bool)
     uint64_t IsSimpleExplicit : 1;
+
+    /// Note that there are 19 spare bits here.
   };
 
   /// Number of inherited and non-inherited bits in CXXConstructorDeclBitfields.
-  enum { NumCXXConstructorDeclBits = NumFunctionDeclBits + 22 };
+  enum { NumCXXConstructorDeclBits = NumFunctionDeclBits + 3 };
 
   /// Stores the bits used by ObjCMethodDecl.
   /// If modified NumObjCMethodDeclBits and the accessor
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index 4ef109ce7192e5..341066a51ebef9 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -2650,6 +2650,8 @@ class CXXConstructorDecl final
   /// \{
   /// The arguments used to initialize the base or member.
   LazyCXXCtorInitializersPtr CtorInitializers;
+  /// The number of base/member initializers.
+  unsigned numCtorInitializers = 0;
 
   CXXConstructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
                      const DeclarationNameInfo &NameInfo, QualType T,
@@ -2772,25 +2774,14 @@ class CXXConstructorDecl final
     return init_const_reverse_iterator(init_begin());
   }
 
-  /// The maximum number of base and member initializers a constructor can
-  /// have; this is limited by the width of the NumCtorInitializers bitfield.
-  static constexpr unsigned getMaxNumCtorInitializers() {
-    return DeclContext::CXXConstructorDeclBitfields::MaxNumCtorInitializers;
-  }
-
   /// Determine the number of arguments used to initialize the member
   /// or base.
   unsigned getNumCtorInitializers() const {
-      return CXXConstructorDeclBits.NumCtorInitializers;
+      return numCtorInitializers;
   }
 
   void setNumCtorInitializers(unsigned numCtorInitializers) {
-    CXXConstructorDeclBits.NumCtorInitializers = numCtorInitializers;
-    // This assert added because NumCtorInitializers is stored
-    // in CXXConstructorDeclBits as a bitfield and its width has
-    // been shrunk from 32 bits to fit into CXXConstructorDeclBitfields.
-    assert(CXXConstructorDeclBits.NumCtorInitializers ==
-           numCtorInitializers && "NumCtorInitializers overflow!");
+    this->numCtorInitializers = numCtorInitializers;
   }
 
   void setCtorInitializers(CXXCtorInitializer **Initializers) {
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 5bfe259cb057a3..941a58ae54a05e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10244,10 +10244,6 @@ def err_multiple_mem_union_initialization : Error <
 def err_multiple_base_initialization : Error <
   "multiple initializations given for base %0">;
 
-def err_ctor_too_many_initializers : Error<
-  "constructor has too many base and member initializers (%0); the maximum "
-  "is %1">;
-
 def err_mem_init_not_member_or_class : Error<
   "member initializer %0 does not name a non-static data member or base "
   "class">;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 2e870e2d448132..9ddb077037397e 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -5542,14 +5542,6 @@ bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors,
     // Just store the initializers as written, they will be checked during
     // instantiation.
     if (!Initializers.empty()) {
-      if (Initializers.size() >
-          CXXConstructorDecl::getMaxNumCtorInitializers()) {
-        Diag(Constructor->getLocation(), diag::err_ctor_too_many_initializers)
-            << Initializers.size()
-            << CXXConstructorDecl::getMaxNumCtorInitializers();
-        Constructor->setInvalidDecl();
-        return true;
-      }
       Constructor->setNumCtorInitializers(Initializers.size());
       CXXCtorInitializer **baseOrMemberInitializers =
         new (Context) CXXCtorInitializer*[Initializers.size()];
@@ -5702,12 +5694,6 @@ bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors,
   }
 
   unsigned NumInitializers = Info.AllToInit.size();
-  if (NumInitializers > CXXConstructorDecl::getMaxNumCtorInitializers()) {
-    Diag(Constructor->getLocation(), diag::err_ctor_too_many_initializers)
-        << NumInitializers << CXXConstructorDecl::getMaxNumCtorInitializers();
-    Constructor->setInvalidDecl();
-    return true;
-  }
   if (NumInitializers > 0) {
     Constructor->setNumCtorInitializers(NumInitializers);
     CXXCtorInitializer **baseOrMemberInitializers =
diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp
index 02cbdc71feedad..5ec9b32e1b6f5d 100644
--- a/clang/lib/Serialization/ASTWriterDecl.cpp
+++ b/clang/lib/Serialization/ASTWriterDecl.cpp
@@ -1767,7 +1767,7 @@ void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
 }
 
 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
-  static_assert(DeclContext::NumCXXConstructorDeclBits == 64,
+  static_assert(DeclContext::NumCXXConstructorDeclBits == 45,
                 "You need to update the serializer after you change the "
                 "CXXConstructorDeclBits");
 
diff --git a/clang/test/SemaCXX/ctor-too-many-initializers.cpp b/clang/test/SemaCXX/ctor-too-many-initializers.cpp
deleted file mode 100644
index a464f26d560c51..00000000000000
--- a/clang/test/SemaCXX/ctor-too-many-initializers.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
-
-// Note: This test is quite time consuming(~5s on a release-with-asserts), so if
-// the limit increases at all we should consider just deleting this test.
-
-#define F0(p) int p;
-#define F1(p) F0(p##0) F0(p##1) F0(p##2) F0(p##3)
-#define F2(p) F1(p##0) F1(p##1) F1(p##2) F1(p##3)
-#define F3(p) F2(p##0) F2(p##1) F2(p##2) F2(p##3)
-#define F4(p) F3(p##0) F3(p##1) F3(p##2) F3(p##3)
-#define F5(p) F4(p##0) F4(p##1) F4(p##2) F4(p##3)
-#define F6(p) F5(p##0) F5(p##1) F5(p##2) F5(p##3)
-#define F7(p) F6(p##0) F6(p##1) F6(p##2) F6(p##3)
-#define F8(p) F7(p##0) F7(p##1) F7(p##2) F7(p##3)
-#define F9(p) F8(p##0) F8(p##1) F8(p##2) F8(p##3)
-
-// expected-error at +1 {{constructor has too many base and member initializers (524288); the maximum is 524287}}
-struct TooMany {
-  F9(a)
-  F9(b)
-};
-
-// expected-note at +1 {{in implicit copy constructor for 'TooMany' first required here}}
-void f(TooMany &a) { TooMany b(a); }

>From 03be1e546aa81feaee6f0a29adc43fa395af8147 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Thu, 5 Nov 2026 07:27:53 -0800
Subject: [PATCH 3/6] Clang-format

---
 clang/include/clang/AST/DeclCXX.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index 341066a51ebef9..532035c8202e32 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -2776,9 +2776,7 @@ class CXXConstructorDecl final
 
   /// Determine the number of arguments used to initialize the member
   /// or base.
-  unsigned getNumCtorInitializers() const {
-      return numCtorInitializers;
-  }
+  unsigned getNumCtorInitializers() const { return numCtorInitializers; }
 
   void setNumCtorInitializers(unsigned numCtorInitializers) {
     this->numCtorInitializers = numCtorInitializers;

>From 933eb003d38e6b10e130b4c8717077b7f82b8a8e Mon Sep 17 00:00:00 2001
From: Erich Keane <ekeane at nvidia.com>
Date: Thu, 5 Nov 2026 07:54:47 -0800
Subject: [PATCH 4/6] Remove additional line from the release note.

Co-authored-by: Corentin Jabot <corentinjabot at gmail.com>
---
 clang/docs/ReleaseNotes.md | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 945a177422aa03..37179ff873ca82 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -784,9 +784,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   `-Wunsafe-buffer-usage-in-format-attr-call` now works when it is the only
   Safe Buffers warning enabled.
 
-- Clang will now no longer assert or miscompile on a constructor that has more than 65535 base and member initializers.
-  Or limit is now effectively 2^32-1.
-
+- Clang will now no longer assert or miscompile on a constructor that has more than 65535 base and member initializers. (#GH219366)
 ### Improvements to Clang's time-trace
 
 ### Improvements to Coverage Mapping

>From 9edd42cd4e7e0b9602900db68441f960c2d26b53 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Thu, 5 Nov 2026 07:57:21 -0800
Subject: [PATCH 5/6] Add missing line in release note from the suggestion
 auto-apply

---
 clang/docs/ReleaseNotes.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 37179ff873ca82..b97aa88ee5fe0b 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -785,6 +785,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   Safe Buffers warning enabled.
 
 - Clang will now no longer assert or miscompile on a constructor that has more than 65535 base and member initializers. (#GH219366)
+
 ### Improvements to Clang's time-trace
 
 ### Improvements to Coverage Mapping

>From ff9a8e37e122fd60123bf3e22f37991bb6e517eb Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Thu, 5 Nov 2026 12:56:47 -0800
Subject: [PATCH 6/6] add comment

---
 clang/include/clang/AST/DeclCXX.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index 532035c8202e32..99ae24a6956ec9 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -2650,6 +2650,16 @@ class CXXConstructorDecl final
   /// \{
   /// The arguments used to initialize the base or member.
   LazyCXXCtorInitializersPtr CtorInitializers;
+
+  /// FIXME: as a space-optimization for constructors we could probably make
+  /// LazyCXXCtorInitializersPtr refer to an ArrayRef type (that is, size + ptr)
+  /// to store this size. However, the downside to this is that querying whether
+  /// this is a 'delegating' constructor would then become expensive.
+  ///
+  /// An additional alternative we can do to negate that is to either store
+  /// 'isDelegatingConstructor', or a 2 bit 'size' (where 0b11 means 'always
+  /// check the Lazy Ptr array ref') in CXXConstructorDeclBitfields.
+  ///
   /// The number of base/member initializers.
   unsigned numCtorInitializers = 0;
 



More information about the cfe-commits mailing list