https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84185
Bug ID: 84185
Summary: missing warning when ignoring attribute aligned on a
member
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
As mentioned in bug 84108 comment 2, GCC warns about variable declarations that
specify both attribute aligned and packed, yet it requires both attributes to
reduce the alignment of a struct member and doesn't indicate that a sole
aligned attribute has no such effect (i.e., doesn't reduce the member's
alignment).
The limitation/requirement of the aligned attribute is documented in the
manual:
When used on a struct, or struct member, the aligned attribute can only
increase the alignment; in order to decrease it, the packed attribute must be
specified as well.
but because it is subtle and inconsistent with how the attribute is treated for
non-members, it's easy to miss. See also bug 82914 for another example of
confusion this has lead to.
Assuming it's too risky to make the attribute behave consistently for both
members and non-members, GCC could help reduce the confusion (and bugs) by
issuing a warning when the attribute is specified alone on a member and known
to have no effect.
$ cat t.c && gcc -S -Wall -Wextra t.c
#define ASSERT(e) _Static_assert (e, #e)
struct {
int a __attribute__ ((aligned (2))); // attribute ignored, missing warning
} a;
ASSERT (_Alignof (a.a) == 2); // assertion failure
struct {
int b __attribute__ ((packed));
} b;
ASSERT (_Alignof (b.b) == 1); // passes
struct {
int c __attribute__ ((aligned (2), packed));
} c;
ASSERT (_Alignof (c.c) == 2); // passes
t.c:1:19: error: static assertion failed: "_Alignof (a.a) == 2"
#define ASSERT(e) _Static_assert (e, #e)
^~~~~~~~~~~~~~
t.c:7:1: note: in expansion of macro ‘ASSERT’
ASSERT (_Alignof (a.a) == 2); // assertion failure
^~~~~~