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

Mariya Podchishchaeva via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 7 03:02:54 PST 2023


================
@@ -727,6 +729,44 @@ void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field,
     if (hadError || VerifyOnly) {
       // Do nothing
     } else if (Init < NumInits) {
+      if (MaybeEmitMFIWarning) {
+        auto CheckAnonMember = [&](const FieldDecl *FD,
+                                   auto &&CheckAnonMember) -> bool {
+          FieldDecl *FirstUninitialized = nullptr;
+          RecordDecl *RD = FD->getType()->getAsRecordDecl();
+          assert(RD && "Not anonymous member checked?");
+          for (auto *F : RD->fields()) {
+            bool AllSet = false;
+            if (F->isAnonymousStructOrUnion())
+              AllSet = CheckAnonMember(F, CheckAnonMember);
+
+            if (AllSet || F->hasInClassInitializer()) {
+              if (RD->isUnion())
+                return true;
+              continue;
+            }
+
+            if (!F->isUnnamedBitfield() &&
+                !F->getType()->isIncompleteArrayType() &&
+                !F->isAnonymousStructOrUnion() && !FirstUninitialized)
+              FirstUninitialized = F;
+          }
+
+          if (FirstUninitialized) {
+            SemaRef.Diag(Loc, diag::warn_missing_field_initializers)
+                << FirstUninitialized;
+            return false;
----------------
Fznamznon wrote:

Thanks for the catch, fixed and added a test case.

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


More information about the llvm-commits mailing list