From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1873) id 6233739A2433; Wed, 9 Jun 2021 18:04:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6233739A2433 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Iain Buclaw To: gcc-cvs@gcc.gnu.org Subject: [gcc r9-9575] d: Respect explicit align(N) type alignment (PR100935) X-Act-Checkin: gcc X-Git-Author: Iain Buclaw X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: a4c9c028e77d162fae375220611dcad31170b903 X-Git-Newrev: fe555102dc224b4d946becc8a9db514ecec66161 Message-Id: <20210609180420.6233739A2433@sourceware.org> Date: Wed, 9 Jun 2021 18:04:20 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Jun 2021 18:04:20 -0000 https://gcc.gnu.org/g:fe555102dc224b4d946becc8a9db514ecec66161 commit r9-9575-gfe555102dc224b4d946becc8a9db514ecec66161 Author: Iain Buclaw Date: Wed Jun 9 19:37:22 2021 +0200 d: Respect explicit align(N) type alignment (PR100935) It was previously the natural type alignment, defined as the maximum of the field alignments for an aggregate. Make sure an explicit align(N) overrides it. gcc/d/ChangeLog: PR d/100935 * dmd/mtype.c (Type::getProperty): Prefer explicit alignment over natural alignment for alignof property. gcc/testsuite/ChangeLog: PR d/100935 * gdc.test/compilable/aggr_alignment.d: Add test cases. (cherry picked from commit 3ba036dd1a752d29764ad44adca6e68bec9599fe) Diff: --- gcc/d/dmd/mtype.c | 5 ++++- gcc/testsuite/gdc.test/compilable/aggr_alignment.d | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/gcc/d/dmd/mtype.c b/gcc/d/dmd/mtype.c index d0e73967d45..585199033af 100644 --- a/gcc/d/dmd/mtype.c +++ b/gcc/d/dmd/mtype.c @@ -2059,7 +2059,10 @@ Expression *Type::getProperty(Loc loc, Identifier *ident, int flag) } else if (ident == Id::__xalignof) { - e = new IntegerExp(loc, alignsize(), Type::tsize_t); + unsigned explicitAlignment = alignment(); + unsigned naturalAlignment = alignsize(); + unsigned actualAlignment = (explicitAlignment == STRUCTALIGN_DEFAULT ? naturalAlignment : explicitAlignment); + e = new IntegerExp(loc, actualAlignment, Type::tsize_t); } else if (ident == Id::_init) { diff --git a/gcc/testsuite/gdc.test/compilable/aggr_alignment.d b/gcc/testsuite/gdc.test/compilable/aggr_alignment.d index 3a80a039e26..5b3542828dd 100644 --- a/gcc/testsuite/gdc.test/compilable/aggr_alignment.d +++ b/gcc/testsuite/gdc.test/compilable/aggr_alignment.d @@ -26,3 +26,24 @@ enum payloadOffset = C2.bytes.offsetof; static assert(C2.int1.offsetof == payloadOffset + 8); static assert(C2.alignof == size_t.sizeof); static assert(__traits(classInstanceSize, C2) == payloadOffset + 12); + +align(8) struct PaddedStruct +{ + bool flag; + align(2) S1 s1; +} + +static assert(PaddedStruct.s1.offsetof == 2); +static assert(PaddedStruct.alignof == 8); +static assert(PaddedStruct.sizeof == 16); + +align(1) struct UglyStruct +{ + bool flag; + int i; + ubyte u; +} + +static assert(UglyStruct.i.offsetof == 4); +static assert(UglyStruct.alignof == 1); +static assert(UglyStruct.sizeof == 9);