[clang] [llvm] [clang-tools-extra] [clang] Fix false positive -Wmissing-field-initializer for anonymous unions (PR #70829)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 02:42:12 PST 2023


================
@@ -727,6 +734,38 @@ void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field,
     if (hadError || VerifyOnly) {
       // Do nothing
     } else if (Init < NumInits) {
+      if (WarnIfMissing) {
+        auto CheckAnonMember = [&](const FieldDecl *FD,
+                                   auto &&CheckAnonMember) -> FieldDecl * {
+          FieldDecl *Uninitialized = nullptr;
+          RecordDecl *RD = FD->getType()->getAsRecordDecl();
+          assert(RD && "Not anonymous member checked?");
+          for (auto *F : RD->fields()) {
+            if (F->isAnonymousStructOrUnion())
+              Uninitialized = CheckAnonMember(F, CheckAnonMember);
+            else if (!F->isUnnamedBitfield() &&
+                     !F->getType()->isIncompleteArrayType() && !Uninitialized &&
+                     !F->hasInClassInitializer())
+              Uninitialized = F;
+
+            if (RD->isUnion() && (F->hasInClassInitializer() || !Uninitialized))
+              return nullptr;
+          }
----------------
Fznamznon wrote:

Ok, done. Thanks for the catch!

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


More information about the cfe-commits mailing list