[clang] 609fe2c - [ObjC] Fix offsets following `[[no_unique_address]]` for `@encode()` (#71321)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 6 11:36:52 PST 2023


Author: Daniel Bertalan
Date: 2023-11-06T20:36:47+01:00
New Revision: 609fe2cb4b2037b546686d5f50c2f330dbca4111

URL: https://github.com/llvm/llvm-project/commit/609fe2cb4b2037b546686d5f50c2f330dbca4111
DIFF: https://github.com/llvm/llvm-project/commit/609fe2cb4b2037b546686d5f50c2f330dbca4111.diff

LOG: [ObjC] Fix offsets following `[[no_unique_address]]` for `@encode()` (#71321)

Commit 46ca880fca made `@encode` skip fields that are made zero-sized by
`[[no_unique_address]]`. When iterating the fields, the index which is
passed to `getFieldOffset` failed to be incremented for those due to the
use of an early `continue`, so subsequent fields reported an incorrect
offset. This caused an assertion to be triggered in
`getObjCEncodingForStructureImpl`.

Fixes #71250

Added: 
    

Modified: 
    clang/lib/AST/ASTContext.cpp
    clang/test/CodeGenObjCXX/encode.mm

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index db9284005b6e1da..014ba4913581f1a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -8541,14 +8541,12 @@ void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
     }
   }
 
-  unsigned i = 0;
   for (FieldDecl *Field : RDecl->fields()) {
     if (!Field->isZeroLengthBitField(*this) && Field->isZeroSize(*this))
       continue;
-    uint64_t offs = layout.getFieldOffset(i);
+    uint64_t offs = layout.getFieldOffset(Field->getFieldIndex());
     FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
                               std::make_pair(offs, Field));
-    ++i;
   }
 
   if (CXXRec && includeVBases) {

diff  --git a/clang/test/CodeGenObjCXX/encode.mm b/clang/test/CodeGenObjCXX/encode.mm
index f382e7f23d77335..cad70e379c386bf 100644
--- a/clang/test/CodeGenObjCXX/encode.mm
+++ b/clang/test/CodeGenObjCXX/encode.mm
@@ -339,3 +339,17 @@ @implementation N
 const char *inner0 = @encode(Outer0<int>::Inner0 *);
 const char *inner1 = @encode(Outer0<int>::Inner1<float> *);
 }
+
+#if __cplusplus >= 202002L
+namespace GH71250 {
+  struct Empty {};
+  struct S {
+    [[no_unique_address]] Empty a;
+    long b;
+    long c;
+  };
+
+  // CHECKCXX20: @_ZN7GH712501sE =  constant [7 x i8] c"{S=qq}\00", align 1
+  extern const char s[] = @encode(S);
+}
+#endif


        


More information about the cfe-commits mailing list