gbencze created this revision.
gbencze added reviewers: rsmith, aaron.ballman.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
gbencze requested review of this revision.
Fix incorrect behavior of __has_unique_object_representations when using the
no_unique_address attribute.
Based on the bug report: https://bugs.llvm.org/show_bug.cgi?id=47722
<https://bugs.llvm.org/show_bug.cgi?id=47722>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D89649
Files:
clang/lib/AST/ASTContext.cpp
clang/test/SemaCXX/has_unique_object_reps_no_unique_addr.cpp
Index: clang/test/SemaCXX/has_unique_object_reps_no_unique_addr.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/has_unique_object_reps_no_unique_addr.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify
-std=c++2a %s
+// expected-no-diagnostics
+
+struct Empty {};
+
+struct A {
+ [[no_unique_address]] Empty e;
+ char x;
+};
+
+static_assert(__has_unique_object_representations(A));
+
+struct B {
+ char x;
+ [[no_unique_address]] Empty e;
+};
+
+static_assert(__has_unique_object_representations(B));
+
+struct C {
+ char x;
+ [[no_unique_address]] Empty e1;
+ [[no_unique_address]] Empty e2;
+};
+
+static_assert(!__has_unique_object_representations(C));
Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -2602,9 +2602,12 @@
}
for (const auto *Field : RD->fields()) {
- if (!Field->getType()->isReferenceType() &&
- !Context.hasUniqueObjectRepresentations(Field->getType()))
- return llvm::None;
+ if (!Field->getType()->isReferenceType()) {
+ if (Field->isZeroSize(Context))
+ continue;
+ else if (!Context.hasUniqueObjectRepresentations(Field->getType()))
+ return llvm::None;
+ }
int64_t FieldSizeInBits =
Context.toBits(Context.getTypeSizeInChars(Field->getType()));
Index: clang/test/SemaCXX/has_unique_object_reps_no_unique_addr.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/has_unique_object_reps_no_unique_addr.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify -std=c++2a %s
+// expected-no-diagnostics
+
+struct Empty {};
+
+struct A {
+ [[no_unique_address]] Empty e;
+ char x;
+};
+
+static_assert(__has_unique_object_representations(A));
+
+struct B {
+ char x;
+ [[no_unique_address]] Empty e;
+};
+
+static_assert(__has_unique_object_representations(B));
+
+struct C {
+ char x;
+ [[no_unique_address]] Empty e1;
+ [[no_unique_address]] Empty e2;
+};
+
+static_assert(!__has_unique_object_representations(C));
Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -2602,9 +2602,12 @@
}
for (const auto *Field : RD->fields()) {
- if (!Field->getType()->isReferenceType() &&
- !Context.hasUniqueObjectRepresentations(Field->getType()))
- return llvm::None;
+ if (!Field->getType()->isReferenceType()) {
+ if (Field->isZeroSize(Context))
+ continue;
+ else if (!Context.hasUniqueObjectRepresentations(Field->getType()))
+ return llvm::None;
+ }
int64_t FieldSizeInBits =
Context.toBits(Context.getTypeSizeInChars(Field->getType()));
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits