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/
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;
}
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits