https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69505
Bug ID: 69505 Summary: attribute aligned changes alignment of the same type Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- While investigating bug 46354 (and its duplicate, bug 46354) I noticed other unexpected behavior when it comes to attribute aligned. The following program redefines type A three times, each time with different alignment. All three definitions are silently accepted even though they conflict with one another and even though the third reduces the alignment of the type (though this latter aspect is being tracked in bug 69502). The program also redeclares an object of type A three times. Unlike the alignment of the type, that of the object is only increased but never decreased. This seems like a bug. I would expect GCC to reject redeclarations of both the same type and the same object with different alignment, and to have a consistent notion of the alignment of the same type and of that of the same object in the same translation unit. Interestingly, when the same program is compiled with g++ (which also accepts it), its output is consistent but also surprising: all ones. That seems like a bug in the C++ front end that I'll raise a separate bug for. As a data point, when compiled with Clang the program prints: 1, 1 32, 32 32, 32 $ cat t.c && /home/msebor/build/gcc-trunk-git/gcc/xgcc -B/home/msebor/build/gcc-trunk-git/gcc -Wall -Wextra -Wpedantic t.c && ./a.out #define Assert(e) extern int Assert [1 - 2 * !(e)] typedef int A __attribute__ ((aligned (1))); extern A a; void f1 (void) { __builtin_printf ("%zu, %zu\n", __alignof__ (A), __alignof__ (a)); } typedef int A __attribute__ ((aligned (32))); extern A a; void f32 (void) { __builtin_printf ("%zu, %zu\n", __alignof__ (A), __alignof__ (a)); } typedef int A __attribute__ ((aligned (2))); extern A a; void f2 (void) { __builtin_printf ("%zu, %zu\n", __alignof__ (A), __alignof__ (a)); } int main (void) { f1 (); f32 (); f2 (); } A a; t.c: In function ‘f1’: t.c:7:54: warning: ISO C does not allow ‘__alignof__ (expression)’ [-Wpedantic] __builtin_printf ("%zu, %zu\n", __alignof__ (A), __alignof__ (a)); ^~~~~~~~~~~ t.c: In function ‘f32’: t.c:14:54: warning: ISO C does not allow ‘__alignof__ (expression)’ [-Wpedantic] __builtin_printf ("%zu, %zu\n", __alignof__ (A), __alignof__ (a)); ^~~~~~~~~~~ t.c: In function ‘f2’: t.c:21:54: warning: ISO C does not allow ‘__alignof__ (expression)’ [-Wpedantic] __builtin_printf ("%zu, %zu\n", __alignof__ (A), __alignof__ (a)); ^~~~~~~~~~~ 1, 1 32, 32 2, 32