[clang] [Clang][Sema] Diagnosis for constexpr constructor not initializing a union member (PR #81225)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 21 06:55:04 PST 2024


================
@@ -2471,6 +2480,23 @@ static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl,
   return true;
 }
 
+static bool
+checkUnionConstructorIntializer(Sema &SemaRef, const FunctionDecl *Dcl,
+                                const CXXConstructorDecl *Constructor,
+                                const CXXRecordDecl *RD,
+                                Sema::CheckConstexprKind Kind) {
+  if (Constructor->getNumCtorInitializers() == 0 && RD->hasVariantMembers()) {
+    if (Kind == Sema::CheckConstexprKind::Diagnose) {
+      SemaRef.Diag(Dcl->getLocation(),
+                   SemaRef.getLangOpts().CPlusPlus20
+                       ? diag::warn_cxx17_compat_constexpr_union_ctor_no_init
+                       : diag::ext_constexpr_union_ctor_no_init);
+    } else if (!SemaRef.getLangOpts().CPlusPlus20) {
+      return true;
----------------
mahtohappy wrote:

The place from where this function is called, is inside a UnionRecord Decl and goes on to check it's constructor to initialize the member, in the c++20 case we don't to initialize it so true but in c++ version before we do, hence the diagnosis and false result. 

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


More information about the cfe-commits mailing list