Finish off rules for z-length bitfields in ms_struct

structs. // rdar://8823265

llvm-svn: 130783
This commit is contained in:
Fariborz Jahanian 2011-05-03 20:21:04 +00:00
parent 271c36811d
commit fc0fe6eb52
6 changed files with 85 additions and 26 deletions

View File

@ -2059,8 +2059,7 @@ unsigned FieldDecl::getFieldIndex() const {
if (IsMsStruct) { if (IsMsStruct) {
// Zero-length bitfields following non-bitfield members are ignored. // Zero-length bitfields following non-bitfield members are ignored.
if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD) || if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD)) {
getASTContext().ZeroBitfieldFollowsBitfield((*i), LastFD)) {
++i; ++i;
continue; continue;
} }

View File

@ -582,7 +582,7 @@ protected:
CharUnits NonVirtualSize; CharUnits NonVirtualSize;
CharUnits NonVirtualAlignment; CharUnits NonVirtualAlignment;
CharUnits ZeroLengthBitfieldAlignment; FieldDecl *ZeroLengthBitfield;
/// PrimaryBase - the primary base class (if one exists) of the class /// PrimaryBase - the primary base class (if one exists) of the class
/// we're laying out. /// we're laying out.
@ -621,7 +621,7 @@ protected:
UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()), UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
DataSize(0), NonVirtualSize(CharUnits::Zero()), DataSize(0), NonVirtualSize(CharUnits::Zero()),
NonVirtualAlignment(CharUnits::One()), NonVirtualAlignment(CharUnits::One()),
ZeroLengthBitfieldAlignment(CharUnits::Zero()), PrimaryBase(0), ZeroLengthBitfield(0), PrimaryBase(0),
PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { } PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { }
void Layout(const RecordDecl *D); void Layout(const RecordDecl *D);
@ -1258,22 +1258,16 @@ void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
// Layout each field, for now, just sequentially, respecting alignment. In // Layout each field, for now, just sequentially, respecting alignment. In
// the future, this will need to be tweakable by targets. // the future, this will need to be tweakable by targets.
const FieldDecl *LastFD = 0; const FieldDecl *LastFD = 0;
ZeroLengthBitfield = 0;
for (RecordDecl::field_iterator Field = D->field_begin(), for (RecordDecl::field_iterator Field = D->field_begin(),
FieldEnd = D->field_end(); Field != FieldEnd; ++Field) { FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
if (IsMsStruct) { if (IsMsStruct) {
const FieldDecl *FD = (*Field); FieldDecl *FD = (*Field);
if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD)) { if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD))
// FIXME. Multiple zero bitfields may follow a bitfield. ZeroLengthBitfield = FD;
// set ZeroLengthBitfieldAlignment to max. of its
// currrent and alignment of 'FD'.
std::pair<CharUnits, CharUnits> FieldInfo =
Context.getTypeInfoInChars(FD->getType());
ZeroLengthBitfieldAlignment = FieldInfo.second;
continue;
}
// Zero-length bitfields following non-bitfield members are // Zero-length bitfields following non-bitfield members are
// ignored: // ignored:
if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD)) else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
continue; continue;
LastFD = FD; LastFD = FD;
} }
@ -1356,6 +1350,21 @@ void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
uint64_t TypeSize = FieldInfo.first; uint64_t TypeSize = FieldInfo.first;
unsigned FieldAlign = FieldInfo.second; unsigned FieldAlign = FieldInfo.second;
if (ZeroLengthBitfield) {
// If a zero-length bitfield is inserted after a bitfield,
// and the alignment of the zero-length bitfield is
// greater than the member that follows it, `bar', `bar'
// will be aligned as the type of the zero-length bitfield.
if (ZeroLengthBitfield != D) {
std::pair<uint64_t, unsigned> FieldInfo =
Context.getTypeInfo(ZeroLengthBitfield->getType());
unsigned ZeroLengthBitfieldAlignment = FieldInfo.second;
if (ZeroLengthBitfieldAlignment > FieldAlign)
FieldAlign = ZeroLengthBitfieldAlignment;
ZeroLengthBitfield = 0;
}
}
if (FieldSize > TypeSize) { if (FieldSize > TypeSize) {
LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D); LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
return; return;
@ -1455,9 +1464,19 @@ void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Context.getTypeInfoInChars(D->getType()); Context.getTypeInfoInChars(D->getType());
FieldSize = FieldInfo.first; FieldSize = FieldInfo.first;
FieldAlign = FieldInfo.second; FieldAlign = FieldInfo.second;
if (ZeroLengthBitfieldAlignment > FieldAlign)
FieldAlign = ZeroLengthBitfieldAlignment; if (ZeroLengthBitfield) {
ZeroLengthBitfieldAlignment = CharUnits::Zero(); // If a zero-length bitfield is inserted after a bitfield,
// and the alignment of the zero-length bitfield is
// greater than the member that follows it, `bar', `bar'
// will be aligned as the type of the zero-length bitfield.
std::pair<CharUnits, CharUnits> FieldInfo =
Context.getTypeInfoInChars(ZeroLengthBitfield->getType());
CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second;
if (ZeroLengthBitfieldAlignment > FieldAlign)
FieldAlign = ZeroLengthBitfieldAlignment;
ZeroLengthBitfield = 0;
}
if (Context.getLangOptions().MSBitfields) { if (Context.getLangOptions().MSBitfields) {
// If MS bitfield layout is required, figure out what type is being // If MS bitfield layout is required, figure out what type is being

View File

@ -628,8 +628,7 @@ CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
FieldDecl *field = *I; FieldDecl *field = *I;
if (IsMsStruct) { if (IsMsStruct) {
// Zero-length bitfields following non-bitfield members are ignored // Zero-length bitfields following non-bitfield members are ignored
if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD) || if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
CGM.getContext().ZeroBitfieldFollowsBitfield((field), LastFD)) {
--fieldNo; --fieldNo;
continue; continue;
} }

View File

@ -352,8 +352,7 @@ bool ConstStructBuilder::Build(InitListExpr *ILE) {
if (IsMsStruct) { if (IsMsStruct) {
// Zero-length bitfields following non-bitfield members are // Zero-length bitfields following non-bitfield members are
// ignored: // ignored:
if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD) || if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) {
CGM.getContext().ZeroBitfieldFollowsBitfield((*Field), LastFD)) {
--FieldNo; --FieldNo;
continue; continue;
} }

View File

@ -753,8 +753,7 @@ bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
// Zero-length bitfields following non-bitfield members are // Zero-length bitfields following non-bitfield members are
// ignored: // ignored:
const FieldDecl *FD = (*Field); const FieldDecl *FD = (*Field);
if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD) || if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Types.getContext().ZeroBitfieldFollowsBitfield(FD, LastFD)) {
--FieldNo; --FieldNo;
continue; continue;
} }
@ -997,8 +996,7 @@ CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
if (IsMsStruct) { if (IsMsStruct) {
// Zero-length bitfields following non-bitfield members are // Zero-length bitfields following non-bitfield members are
// ignored: // ignored:
if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD) || if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
getContext().ZeroBitfieldFollowsBitfield(FD, LastFD)) {
--i; --i;
continue; continue;
} }

View File

@ -0,0 +1,45 @@
// RUN: %clang_cc1 -emit-llvm-only -triple x86_64-apple-darwin9 %s
// rdar://8823265
#define ATTR __attribute__((__ms_struct__))
struct {
unsigned int bf_1 : 12;
unsigned int : 0;
unsigned int bf_2 : 12;
} ATTR t1;
static int a1[(sizeof(t1) == 8) -1];
struct
{
char foo : 4;
short : 0;
char bar;
} ATTR t2;
static int a2[(sizeof(t2) == 4) -1];
#pragma ms_struct on
struct
{
char foo : 4;
short : 0;
char bar;
} t3;
#pragma ms_struct off
static int a3[(sizeof(t3) == 4) -1];
struct
{
char foo : 6;
long : 0;
} ATTR t4;
static int a4[(sizeof(t4) == 8) -1];
struct
{
char foo : 4;
short : 0;
char bar : 8;
} ATTR t5;
static int a5[(sizeof(t5) == 4) -1];