[PATCH] fix struct alignment problem with empty base class

Eli Friedman eli.friedman at gmail.com
Fri Jul 12 15:41:22 PDT 2013


On Fri, Jul 12, 2013 at 6:46 AM,  <Andrea_DiBiagio at sn.scee.net> wrote:
> Hi,
> attached is a patch to fix an alignment problem affecting classes derived
> from a base with no fields but with strong alignment constraints.
>
> Let A be a strongly aligned struct/class with no fields.
> Let B be a derived class with A as its direct base class.
>
> When performing the layout of B, A is correctly placed at offset 0 of B.
> However, the alignment of B is wrongly left unchanged.
>
> See example below:
>
> /////////////
> struct A {
>   // no fields
> } __attribute__ ((aligned(16)));
>
> struct B : public A {
> };
> /////////////
>
> The alignment of B is wrongly set to 1 while the alignment of A is
> correctly
> set to 16. According to gcc, the alignment of B should be 16.
>
> The problem is in method RecordLayoutBuilder::LayoutBase() (file
> "lib/AST/RecordLayoutBuilder.cpp"), which forgets to update field
> `Alignment' with the alignment information associated to the empty base
> class in the case where base is placed at relative offset 0.
>
> This patch fixes the problem with the alignment and adds a test case to
> verify that the alignment info is correctly updated.
> One thing to note is that this change may have an effect on binary
> compatibility between obj/libs built with and without this change (but
> only in the - hopefully rare - case described above).

Index: test/CodeGenCXX/alignof.cpp
===================================================================
--- test/CodeGenCXX/alignof.cpp (revision 0)
+++ test/CodeGenCXX/alignof.cpp (revision 0)

Please move the test into SemaCXX, and use static_assert to verify the
alignment instead of checking the IR.

+    // Remember max struct/class alignment.

This comment is useless.

Otherwise, looks fine.

-Eli



More information about the cfe-commits mailing list