[clang] Allow packing fields into tail padding of record fields (PR #122197)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 8 16:28:21 PST 2025


================
@@ -803,10 +804,16 @@ bool ConstStructBuilder::Build(const InitListExpr *ILE, bool AllowOverwrite) {
       if (!AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit,
                        AllowOverwrite))
         return false;
-      // After emitting a non-empty field with [[no_unique_address]], we may
-      // need to overwrite its tail padding.
-      if (Field->hasAttr<NoUniqueAddressAttr>())
-        AllowOverwrite = true;
+
+      // Allow overwrites after a field with tail padding. This allows
+      // overwriting tail padding of fields carrying [[no_unique_address]]
+      // without checking for it, since it is not necessarily present in debug
+      // info.
+      if (const CXXRecordDecl *FieldRD = FieldTy->getAsCXXRecordDecl()) {
+        const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(FieldRD);
+        if (Layout.getDataSize() < Layout.getSize())
+          AllowOverwrite = true;
----------------
efriedma-quic wrote:

At first glance, I'm not sure what this is doing... if you have a non-empty field, only the tail padding can be overwritten... but the CGRecordLayoutBuilder change should ensure the tail padding doesn't actually count as part of the field in that case?  I'm probably missing something obvious.

Do we need to adjust the other `hasAttr<NoUniqueAddressAttr>()` in CGExprConstant.cpp?

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


More information about the cfe-commits mailing list