Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

BertalanD
Copy link
Member

Commit 46ca880 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

Commit 46ca880 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 llvm#71250
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 5, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2023

@llvm/pr-subscribers-clang

Author: Daniel Bertalan (BertalanD)

Changes

Commit 46ca880 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


Full diff: https://github.com/llvm/llvm-project/pull/71321.diff

2 Files Affected:

  • (modified) clang/lib/AST/ASTContext.cpp (+1-3)
  • (modified) clang/test/CodeGenObjCXX/encode.mm (+14)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index da90136752210b6..60146e9901bc44d 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -8535,14 +8535,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

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@nico nico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg2m

@BertalanD BertalanD merged commit 609fe2c into llvm:main Nov 6, 2023
@BertalanD BertalanD deleted the 71250 branch November 6, 2023 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] Assertion in getObjCEncodingForStructureImpl with [[no_unique_address]] in Objective-C++ class
4 participants