Created attachment 63608 [details] Sample file reproducing the problem First of all, we are not sure that this is a bug. We found it while trying to replicate the behavior of GCC in an analyzer for C. The sample file is attached. The result that we get is: field_t : size 2 -- align 1 struct S1: size 3 -- align 1 struct S2: size 4 -- align 2 struct S3: size 3 -- align 1 We would have expected to be either: field_t : size 2 -- align 1 struct S1: size 3 -- align 1 struct S2: size 3 -- align 1 struct S3: size 3 -- align 1 or (even if it would be still surprising) : field_t : size 2 -- align 1 struct S1: size 4 -- align 2 struct S2: size 4 -- align 2 struct S3: size 3 -- align 1 It seems like when using an attribute in a typedef, if we use it for the type of a field, the attribute is attached to the field instead of being attached to the type, which is surprising. Is it expected? If so, can someone explain the semantics of this code? I found two issues that may be related, but I am not sure that it is the same: - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109824 - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46354
The aligned attribute on types cannot decrease alignment without also specifying packed. It can do that when placed on decls. I didn't expect that to include 'typedef' decls. The existing wording in the documentation might need some clarification for that I guess. But - is the behavior expected? I'm not sure...
Oh indeed, now that you tell it we should at least tell the user that they must specify packed if they want to reduce the alignment.
Created attachment 63634 [details] Second example with introduced packed but there is still a strange behavior
I added a new example. This time, all structures are indicated as packed but instead of a short, I use an int that I try to align to 2. We get: field_t : size 4 -- align 2 struct S1: size 5 -- align 1 struct S2: size 6 -- align 2 struct S3: size 5 -- align 1 Which is still surprising.