[PATCH] [Sema] Fix crash in CheckStructUnionType()

Davide Italiano dccitaliano at gmail.com
Mon Apr 27 18:00:05 PDT 2015


Hi rsmith, aaron.ballman,

This is an attempt to fix the first half of the problem in https://llvm.org/bugs/show_bug.cgi?id=19002
We {co,sh}ould be able to override a union initializer with an empty initializer but the code doesn't deal with that (I think). I'm still relatively new to Sema so there might be something I'm missing.

I double checked the generated AST after the fix and it seems correct:
[...]
      `-VarDecl 0x807d35000 <line:21:3, line:27:3> line:21:11 v 'union a [2]' cinit
        `-InitListExpr 0x807d35268 <col:18, line:27:3> 'union a [2]'
          |-array filler
          | `-ImplicitValueInitExpr 0x807d35310 <<invalid sloc>> 'union a':'union a'
          `-InitListExpr 0x807d352b8 <line:26:11, col:14> 'union a':'union a' field Field 0x807caac90 'f' 'int'
[...]

I'll take a look at the FIXME and add the testcase reported in the PR if this sounds reasonable to you.

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D9316

Files:
  lib/Sema/SemaInit.cpp

Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -1632,8 +1632,26 @@
           CheckEmptyInitializable(
               InitializedEntity::InitializeMember(*Field, &Entity),
               IList->getLocEnd());
-        else
+        else {
+          FieldDecl *CurrentField =
+            StructuredList->getInitializedFieldInUnion();
+          if (CurrentField && *Field != CurrentField) {
+            assert(StructuredList->getNumInits() == 1
+                   && "A union should never have more than one initializer!");
+
+            // FIXME: emit warning because we're overriding the initializer.
+            Expr *ExistingInit = StructuredList->getInit(0);
+            SemaRef.Diag(ExistingInit->getLocStart(),
+                         diag::note_previous_initializer)
+              << /*FIXME:has side effects=*/0
+              << ExistingInit->getSourceRange();
+
+            // remove existing initializer
+            StructuredList->resizeInits(SemaRef.Context, 0);
+            StructuredList->setInitializedFieldInUnion(nullptr);
+          }
           StructuredList->setInitializedFieldInUnion(*Field);
+        }
         break;
       }
     }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9316.24523.patch
Type: text/x-patch
Size: 1277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150428/102aa987/attachment.bin>


More information about the cfe-commits mailing list