This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: RFC: [PATCH] Add warn_if_not_aligned attribute
- From: Joseph Myers <joseph at codesourcery dot com>
- To: "H.J. Lu" <hjl dot tools at gmail dot com>
- Cc: Martin Sebor <msebor at gmail dot com>, Jason Merrill <jason at redhat dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 6 Jul 2017 15:44:57 +0000
- Subject: Re: RFC: [PATCH] Add warn_if_not_aligned attribute
- Authentication-results: sourceware.org; auth=none
- References: <2f2c702d-91e0-edc5-de77-04957b33969e@gmail.com> <CAMe9rOrq4c-O3MAHkftFJB2ALH9fbC6n9OJRFgm9m3uQazwSaw@mail.gmail.com> <6c6c268d-a40b-cfa9-574c-ad235f9205b7@gmail.com> <CAMe9rOqR6w+vy+3d045cACetg9ZW706naBD-FtVdCOmgj+Dv-g@mail.gmail.com> <CAMe9rOo0WL+1rJQ_3EMdi2wJq-KJ4YWmTmGodeoVZ5e10vV-pA@mail.gmail.com> <4a4ac20f-9c27-4844-55dc-38ff56ac138b@gmail.com> <CAMe9rOryx5NH9Tp6Y26f=h5pWPeg9kNyOBxuhTjT=aOJxvDYcA@mail.gmail.com> <ea1100d5-fe57-fe31-134c-6dcf94289437@gmail.com> <CAMe9rOp-X1S_HY70hr_-CO2HDvsDAJEw1FTMEj+CKS5Ds7C8-A@mail.gmail.com> <alpine.DEB.2.20.1706151728020.3768@digraph.polyomino.org.uk> <20170616115455.GA3457@gmail.com>
On Fri, 16 Jun 2017, H.J. Lu wrote:
> +@code{warning: alignment 8 of 'struct foo' is less than 16}.
I think @samp is better than @code for warnings, throughout, since they
aren't pieces of program code.
> +This warning can be disabled by @option{-Wno-if-not-aligned}.
> +The @code{warn_if_not_aligned } attribute can also be used for types
Stray space before }.
> +static void
> +handle_warn_if_not_align (tree field, unsigned int record_align)
Missing comment above this function explaining its semantics and those of
its arguments.
> + if ((record_align % warn_if_not_align) != 0)
> + warning (opt_w, "alignment %d of %qT is less than %d",
> + record_align, context, warn_if_not_align);
I'd expect %u for unsigned int alignments, instead of %d.
> + unsigned int off
> + = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
> + + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)) / BITS_PER_UNIT);
> + if ((off % warn_if_not_align) != 0)
> + warning (opt_w, "%q+D offset %d in %qT isn't aligned to %d",
> + field, off, context, warn_if_not_align);
And you can have struct offsets that don't fit in unsigned int (i.e.
structures over 4 GB), so should be using unsigned HOST_WIDE_INT to store
the offset and %wu to print it. (Whereas various places in GCC restrict
alignments to unsigned int.)
What happens if you specify the attribute on a bit-field, or on a type
used to declare a bit-field? I don't think either of those particularly
makes sense, but I don't see tests for it either.
--
Joseph S. Myers
joseph@codesourcery.com