[PATCH] D139261: [Clang] Modify sanity check assert in AggExprEmitter::VisitInitListExpr to cover anonymous struct in a union GNU extension

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 3 14:43:48 PST 2022


shafik created this revision.
shafik added reviewers: aaron.ballman, erichkeane.
Herald added a project: All.
shafik requested review of this revision.

`AggExprEmitter::VisitInitListExpr` sanity checks that an empty union is really empty and not a semantic analysis failure. The assert is missing that we allow anonymous structs as a GNU extension. I have updated the assert to take that into account.

This fixes: https://github.com/llvm/llvm-project/issues/58800


https://reviews.llvm.org/D139261

Files:
  clang/lib/CodeGen/CGExprAgg.cpp
  clang/test/SemaCXX/anonymous-struct.cpp


Index: clang/test/SemaCXX/anonymous-struct.cpp
===================================================================
--- clang/test/SemaCXX/anonymous-struct.cpp
+++ clang/test/SemaCXX/anonymous-struct.cpp
@@ -189,3 +189,20 @@
   }
 } A; // expected-note {{given name 'A' for linkage purposes by this typedef}}
 }
+
+#if __cplusplus > 201103L
+namespace GH58800 {
+struct A {
+  union {
+    struct {
+      float red = 0.0f;
+    };
+  };
+};
+
+A GetA() {
+  A result{};
+  return result;
+}
+}
+#endif
Index: clang/lib/CodeGen/CGExprAgg.cpp
===================================================================
--- clang/lib/CodeGen/CGExprAgg.cpp
+++ clang/lib/CodeGen/CGExprAgg.cpp
@@ -1685,7 +1685,7 @@
       // Make sure that it's really an empty and not a failure of
       // semantic analysis.
       for (const auto *Field : record->fields())
-        assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
+        assert((Field->isUnnamedBitfield() || Field->isAnonymousStructOrUnion()) && "Only unnamed bitfields or ananymous class allowed");
 #endif
       return;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139261.479870.patch
Type: text/x-patch
Size: 1099 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221203/565137da/attachment.bin>


More information about the cfe-commits mailing list