Skip to content

Commit e9fcbe0

Browse files
committed
Rename accessor, add more tests for MS zero size conventions
1 parent d453762 commit e9fcbe0

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ def ArmMveStrictPolymorphism : TypeAttr, TargetSpecificAttr<TargetARM> {
18011801
def NoUniqueAddress : InheritableAttr {
18021802
let Spellings = [CXX11<"", "no_unique_address", 201803>,
18031803
CXX11<"msvc", "no_unique_address", 201803>];
1804-
let Accessors = [Accessor<"isDefault", [CXX11<"", "no_unique_address", 201803>]>,
1804+
let Accessors = [Accessor<"isStandard", [CXX11<"", "no_unique_address", 201803>]>,
18051805
Accessor<"isMSVC", [CXX11<"msvc", "no_unique_address", 201803>]>];
18061806
let Subjects = SubjectList<[NonBitField], ErrorDiag>;
18071807
let Documentation = [NoUniqueAddressDocs];

clang/lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4510,7 +4510,8 @@ bool FieldDecl::isZeroSize(const ASTContext &Ctx) const {
45104510
if (!Ctx.getTargetInfo().getCXXABI().isMicrosoft())
45114511
return true;
45124512

4513-
// MS ABI: nonzero if class type with class type fields
4513+
// MS ABI: has nonzero size if it is a class type with class type fields,
4514+
// whether or not they have nonzero size
45144515
return !llvm::any_of(CXXRD->fields(), [](const FieldDecl *Field) {
45154516
return Field->getType()->getAs<RecordType>();
45164517
});

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,10 +2599,6 @@ struct MicrosoftRecordLayoutBuilder {
25992599
const CXXRecordDecl *RD) const;
26002600
const ASTContext &Context;
26012601
EmptySubobjectMap *EmptySubobjects;
2602-
llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
2603-
typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
2604-
BaseSubobjectInfoMapTy;
2605-
BaseSubobjectInfoMapTy VirtualBaseInfo;
26062602

26072603
/// The size of the record being laid out.
26082604
CharUnits Size;
@@ -2873,12 +2869,12 @@ MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) {
28732869
bool CheckLeadingLayout = !PrimaryBase;
28742870
// Iterate through the bases and lay out the non-virtual ones.
28752871
for (const CXXBaseSpecifier &Base : RD->bases()) {
2876-
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
2877-
const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2878-
28792872
if (Base.isVirtual())
28802873
continue;
28812874

2875+
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
2876+
const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2877+
28822878
// Only lay out bases without extendable VFPtrs on the second pass.
28832879
if (BaseLayout.hasExtendableVFPtr()) {
28842880
VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize();
@@ -3146,7 +3142,6 @@ void MicrosoftRecordLayoutBuilder::injectVFPtr(const CXXRecordDecl *RD) {
31463142
void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) {
31473143
if (!HasVBPtr)
31483144
return;
3149-
31503145
// Vtordisps are always 4 bytes (even in 64-bit mode)
31513146
CharUnits VtorDispSize = CharUnits::fromQuantity(4);
31523147
CharUnits VtorDispAlignment = VtorDispSize;

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8374,7 +8374,7 @@ static void handleNoMergeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
83748374
static void handleNoUniqueAddressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
83758375
NoUniqueAddressAttr TmpAttr(S.Context, AL);
83768376
if (S.getLangOpts().MSVCCompat) {
8377-
if (TmpAttr.isDefault()) {
8377+
if (TmpAttr.isStandard()) {
83788378
S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
83798379
return;
83808380
}

clang/test/Layout/ms-no-unique-address.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,46 @@ namespace VBases {
336336
// CHECK-NEXT: | [sizeof=16, align=8,
337337
// CHECK-NEXT: | nvsize=16, nvalign=8]
338338
}
339+
340+
namespace ZeroSize {
341+
struct empty {};
342+
343+
union empty_union {};
344+
345+
struct empty_union_container {
346+
[[msvc::no_unique_address]] empty_union x;
347+
};
348+
349+
union union_of_empty {
350+
[[msvc::no_unique_address]] empty x;
351+
};
352+
353+
struct struct_of_empty {
354+
[[msvc::no_unique_address]] empty x;
355+
};
356+
357+
struct union_of_empty_container {
358+
[[msvc::no_unique_address]] union_of_empty x;
359+
};
360+
static_assert(sizeof(union_of_empty_container) == 1);
361+
// CHECK:*** Dumping AST Record Layout
362+
// CHECK: 0 | struct ZeroSize::union_of_empty_container
363+
// CHECK-NOT: (empty)
364+
// CHECK: 0 | union ZeroSize::union_of_empty x (empty)
365+
// CHECK: 0 | struct ZeroSize::empty x (empty)
366+
// CHECK: | [sizeof=1, align=1,
367+
// CHECK: | nvsize=1, nvalign=1]
368+
369+
struct struct_of_empty_container {
370+
[[msvc::no_unique_address]] struct_of_empty x;
371+
};
372+
static_assert(sizeof(struct_of_empty_container) == 1);
373+
// CHECK:*** Dumping AST Record Layout
374+
// CHECK: 0 | struct ZeroSize::struct_of_empty_container
375+
// CHECK-NOT: (empty)
376+
// CHECK: 0 | struct ZeroSize::struct_of_empty x (empty)
377+
// CHECK: 0 | struct ZeroSize::empty x (empty)
378+
// CHECK: | [sizeof=1, align=1,
379+
// CHECK: | nvsize=1, nvalign=1]
380+
381+
}

0 commit comments

Comments
 (0)