public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c: Extend the -Wpadded message with actual padding size
@ 2022-06-16 19:37 Vit Kabele
  2022-06-17 12:56 ` Eric Gallager
  2022-06-17 14:06 ` Marek Polacek
  0 siblings, 2 replies; 10+ messages in thread
From: Vit Kabele @ 2022-06-16 19:37 UTC (permalink / raw)
  To: gcc-patches

When the compiler warns about padding struct to alignment boundary, it
now also informs the user about the size of the alignment that needs to
be added to get rid of the warning.

This removes the need of using pahole or similar tools, or manually
determining the padding size.

Tested on x86_64-pc-linux-gnu.

gcc/ChangeLog:

	* stor-layout.cc (finalize_record_size): Improve warning message

Signed-off-by: Vit Kabele <vit.kabele@sysgo.com>
---
 gcc/stor-layout.cc | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
index 765f22f68b9..57ddb001780 100644
--- a/gcc/stor-layout.cc
+++ b/gcc/stor-layout.cc
@@ -1781,7 +1781,14 @@ finalize_record_size (record_layout_info rli)
       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
       && input_location != BUILTINS_LOCATION
       && !TYPE_ARTIFICIAL (rli->t))
-    warning (OPT_Wpadded, "padding struct size to alignment boundary");
+  {
+      tree padding_size
+		= size_binop (MINUS_EXPR,
+			TYPE_SIZE_UNIT (rli->t), unpadded_size_unit);
+      warning (OPT_Wpadded,
+		   "padding struct size to alignment boundary with %E bytes",
+		   padding_size);
+  }
 
   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
       && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
-- 
2.30.2

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] c: Extend the -Wpadded message with actual padding size
  2022-06-16 19:37 [PATCH] c: Extend the -Wpadded message with actual padding size Vit Kabele
@ 2022-06-17 12:56 ` Eric Gallager
  2022-06-17 14:06 ` Marek Polacek
  1 sibling, 0 replies; 10+ messages in thread
From: Eric Gallager @ 2022-06-17 12:56 UTC (permalink / raw)
  To: Vit Kabele; +Cc: gcc-patches

On Thu, Jun 16, 2022 at 3:37 PM Vit Kabele <vit.kabele@sysgo.com> wrote:
>
> When the compiler warns about padding struct to alignment boundary, it
> now also informs the user about the size of the alignment that needs to
> be added to get rid of the warning.

Hi, thanks for taking the time to improve -Wpadded; I have been
wishing that GCC's implementation of -Wpadded would print this
information for a while now and thought there was a bug open for it,
but can't seem to find it now...

>
> This removes the need of using pahole or similar tools, or manually
> determining the padding size.
>
> Tested on x86_64-pc-linux-gnu.
>
> gcc/ChangeLog:
>
>         * stor-layout.cc (finalize_record_size): Improve warning message
>
> Signed-off-by: Vit Kabele <vit.kabele@sysgo.com>
> ---
>  gcc/stor-layout.cc | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
> index 765f22f68b9..57ddb001780 100644
> --- a/gcc/stor-layout.cc
> +++ b/gcc/stor-layout.cc
> @@ -1781,7 +1781,14 @@ finalize_record_size (record_layout_info rli)
>        && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
>        && input_location != BUILTINS_LOCATION
>        && !TYPE_ARTIFICIAL (rli->t))
> -    warning (OPT_Wpadded, "padding struct size to alignment boundary");
> +  {
> +      tree padding_size
> +               = size_binop (MINUS_EXPR,
> +                       TYPE_SIZE_UNIT (rli->t), unpadded_size_unit);
> +      warning (OPT_Wpadded,
> +                  "padding struct size to alignment boundary with %E bytes",
> +                  padding_size);
> +  }

Style nit: indentation seems off; check your tabs vs. spaces etc.

>
>    if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
>        && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
> --
> 2.30.2

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] c: Extend the -Wpadded message with actual padding size
  2022-06-16 19:37 [PATCH] c: Extend the -Wpadded message with actual padding size Vit Kabele
  2022-06-17 12:56 ` Eric Gallager
@ 2022-06-17 14:06 ` Marek Polacek
  2022-06-20 13:50   ` Vit Kabele
  1 sibling, 1 reply; 10+ messages in thread
From: Marek Polacek @ 2022-06-17 14:06 UTC (permalink / raw)
  To: Vit Kabele; +Cc: gcc-patches

On Thu, Jun 16, 2022 at 09:37:32PM +0200, Vit Kabele wrote:
> When the compiler warns about padding struct to alignment boundary, it
> now also informs the user about the size of the alignment that needs to
> be added to get rid of the warning.
> 
> This removes the need of using pahole or similar tools, or manually
> determining the padding size.

Thanks for the patch, it looks reasonable, with the formatting fixed.
It would be nice to have a testcase, at least something like

struct S {
  __UINT64_TYPE__ i;
  char c;
};

The problem is what value to check for, on 32-bit arches the padding is
probably 3 bytes large and on 64-bit arches probably 7 bytes.  So I think
you could use __attribute__((aligned (8))) and then it's always 7.

> Tested on x86_64-pc-linux-gnu.
> 
> gcc/ChangeLog:
> 
> 	* stor-layout.cc (finalize_record_size): Improve warning message

Missing '.' at the end.

> 
> Signed-off-by: Vit Kabele <vit.kabele@sysgo.com>
> ---
>  gcc/stor-layout.cc | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
> index 765f22f68b9..57ddb001780 100644
> --- a/gcc/stor-layout.cc
> +++ b/gcc/stor-layout.cc
> @@ -1781,7 +1781,14 @@ finalize_record_size (record_layout_info rli)
>        && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
>        && input_location != BUILTINS_LOCATION
>        && !TYPE_ARTIFICIAL (rli->t))
> -    warning (OPT_Wpadded, "padding struct size to alignment boundary");
> +  {
> +      tree padding_size
> +		= size_binop (MINUS_EXPR,
> +			TYPE_SIZE_UNIT (rli->t), unpadded_size_unit);
> +      warning (OPT_Wpadded,
> +		   "padding struct size to alignment boundary with %E bytes",
> +		   padding_size);
> +  }
>  
>    if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
>        && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
> -- 
> 2.30.2
> 

Marek


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] c: Extend the -Wpadded message with actual padding size
  2022-06-17 14:06 ` Marek Polacek
@ 2022-06-20 13:50   ` Vit Kabele
  2022-06-20 23:05     ` Andrew Pinski
  0 siblings, 1 reply; 10+ messages in thread
From: Vit Kabele @ 2022-06-20 13:50 UTC (permalink / raw)
  To: polacek; +Cc: vit, gcc-patches

I fixed the formatting and added the test.

The test has first element 32bit so that it should work on both 32 and
64bit architectures, even without the aligned attribute.

If there is some better way how to write the test properly formatted
(i.e. not on a single line), please let me know.

-- >8 --
Subject: [PATCH] c: Extend the -Wpadded message with actual padding size

When the compiler warns about padding struct to alignment boundary, it
now also informs the user about the size of the alignment that needs to
be added to get rid of the warning.

This removes the need of using pahole or similar tools, or manually
determining the padding size.

Tested on x86_64-pc-linux-gnu.

gcc/ChangeLog:

	* stor-layout.cc (finalize_record_size): Extend warning message.

gcc/testsuite/ChangeLog:

	* c-c++-common/Wpadded.c: New test.

Signed-off-by: Vit Kabele <vit.kabele@sysgo.com>
---
 gcc/stor-layout.cc                   |  7 ++++++-
 gcc/testsuite/c-c++-common/Wpadded.c | 10 ++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/c-c++-common/Wpadded.c

diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
index 765f22f68b9..88923c4136b 100644
--- a/gcc/stor-layout.cc
+++ b/gcc/stor-layout.cc
@@ -1781,7 +1781,12 @@ finalize_record_size (record_layout_info rli)
       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
       && input_location != BUILTINS_LOCATION
       && !TYPE_ARTIFICIAL (rli->t))
-    warning (OPT_Wpadded, "padding struct size to alignment boundary");
+  {
+	tree pad_size
+	  = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (rli->t), unpadded_size_unit);
+	  warning (OPT_Wpadded,
+		"padding struct size to alignment boundary with %E bytes", pad_size);
+  }
 
   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
       && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
diff --git a/gcc/testsuite/c-c++-common/Wpadded.c b/gcc/testsuite/c-c++-common/Wpadded.c
new file mode 100644
index 00000000000..e8f1044a36b
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wpadded.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-Wpadded" } */
+
+/*
+ * The struct is on single line, because C++ compiler emits the -Wpadded
+ * warning at the first line of the struct, while the C compiler at the last
+ * line of the struct definition. This way the test passes on both
+ */
+struct S { __UINT32_TYPE__ i; char c; }; /* { dg-warning "padding struct size to alignment boundary with 3 bytes" } */
+
-- 
2.30.2

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] c: Extend the -Wpadded message with actual padding size
  2022-06-20 13:50   ` Vit Kabele
@ 2022-06-20 23:05     ` Andrew Pinski
  2022-06-22  8:34       ` Vit Kabele
  0 siblings, 1 reply; 10+ messages in thread
From: Andrew Pinski @ 2022-06-20 23:05 UTC (permalink / raw)
  To: Vit Kabele; +Cc: Marek Polacek, vit, GCC Patches

On Mon, Jun 20, 2022 at 6:50 AM Vit Kabele <vit.kabele@sysgo.com> wrote:
>
> I fixed the formatting and added the test.
>
> The test has first element 32bit so that it should work on both 32 and
> 64bit architectures, even without the aligned attribute.
>
> If there is some better way how to write the test properly formatted
> (i.e. not on a single line), please let me know.
>
> -- >8 --
> Subject: [PATCH] c: Extend the -Wpadded message with actual padding size
>
> When the compiler warns about padding struct to alignment boundary, it
> now also informs the user about the size of the alignment that needs to
> be added to get rid of the warning.
>
> This removes the need of using pahole or similar tools, or manually
> determining the padding size.
>
> Tested on x86_64-pc-linux-gnu.
>
> gcc/ChangeLog:
>
>         * stor-layout.cc (finalize_record_size): Extend warning message.
>
> gcc/testsuite/ChangeLog:
>
>         * c-c++-common/Wpadded.c: New test.
>
> Signed-off-by: Vit Kabele <vit.kabele@sysgo.com>
> ---
>  gcc/stor-layout.cc                   |  7 ++++++-
>  gcc/testsuite/c-c++-common/Wpadded.c | 10 ++++++++++
>  2 files changed, 16 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/c-c++-common/Wpadded.c
>
> diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
> index 765f22f68b9..88923c4136b 100644
> --- a/gcc/stor-layout.cc
> +++ b/gcc/stor-layout.cc
> @@ -1781,7 +1781,12 @@ finalize_record_size (record_layout_info rli)
>        && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
>        && input_location != BUILTINS_LOCATION
>        && !TYPE_ARTIFICIAL (rli->t))
> -    warning (OPT_Wpadded, "padding struct size to alignment boundary");
> +  {
> +       tree pad_size
> +         = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (rli->t), unpadded_size_unit);
> +         warning (OPT_Wpadded,
> +               "padding struct size to alignment boundary with %E bytes", pad_size);
> +  }
>
>    if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
>        && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
> diff --git a/gcc/testsuite/c-c++-common/Wpadded.c b/gcc/testsuite/c-c++-common/Wpadded.c
> new file mode 100644
> index 00000000000..e8f1044a36b
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/Wpadded.c
> @@ -0,0 +1,10 @@
> +/* { dg-do compile } */
> +/* { dg-options "-Wpadded" } */
> +
> +/*
> + * The struct is on single line, because C++ compiler emits the -Wpadded
> + * warning at the first line of the struct, while the C compiler at the last
> + * line of the struct definition. This way the test passes on both
> + */
> +struct S { __UINT32_TYPE__ i; char c; }; /* { dg-warning "padding struct size to alignment boundary with 3 bytes" } */
> +
Note the testcase will fail on some targets where alignment is 1 for everything.
You most likely want the dg-warning to be like it is in gcc.dg/Wpadded.c:
/* { dg-warning "padding struct size to alignment boundary with 3
bytes" ""  { target { ! default_packed } } } */

You might want the following from the same file too:
/* -fpack-struct is necessary because the warning expected requires the initial
   packing to be larger than 1, which cannot be guaranteed for all targets.
   We won't get a warning anyway if the target has "packed" structure
   layout.  */
/* { dg-options "-Wpadded -fpack-struct=8" } */
/* { dg-additional-options "-mno-ms-bitfields" { target *-*-mingw* } } */


Thanks,
Andrew Pinski

> --
> 2.30.2

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] c: Extend the -Wpadded message with actual padding size
  2022-06-20 23:05     ` Andrew Pinski
@ 2022-06-22  8:34       ` Vit Kabele
  2022-06-22  8:47         ` Andrew Pinski
  0 siblings, 1 reply; 10+ messages in thread
From: Vit Kabele @ 2022-06-22  8:34 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: Marek Polacek, vit, GCC Patches

Hello,

On Mon, Jun 20, 2022 at 04:05:17PM -0700, Andrew Pinski wrote:
> > new file mode 100644
> > index 00000000000..e8f1044a36b
> > --- /dev/null
> > +++ b/gcc/testsuite/c-c++-common/Wpadded.c
> > @@ -0,0 +1,10 @@
> > +/* { dg-do compile } */
> > +/* { dg-options "-Wpadded" } */
> > +
> > +/*
> > + * The struct is on single line, because C++ compiler emits the -Wpadded
> > + * warning at the first line of the struct, while the C compiler at the last
> > + * line of the struct definition. This way the test passes on both
> > + */
> > +struct S { __UINT32_TYPE__ i; char c; }; /* { dg-warning "padding struct size to alignment boundary with 3 bytes" } */
> > +
> Note the testcase will fail on some targets where alignment is 1 for everything.
> You most likely want the dg-warning to be like it is in gcc.dg/Wpadded.c:
> /* { dg-warning "padding struct size to alignment boundary with 3
> bytes" ""  { target { ! default_packed } } } */
> 
> You might want the following from the same file too:
> /* -fpack-struct is necessary because the warning expected requires the initial
>    packing to be larger than 1, which cannot be guaranteed for all targets.
>    We won't get a warning anyway if the target has "packed" structure
>    layout.  */
> /* { dg-options "-Wpadded -fpack-struct=8" } */
> /* { dg-additional-options "-mno-ms-bitfields" { target *-*-mingw* } } */
I added the ! default_packed directive, but I am not sure whether the
-fpack-struct is needed. Could you please provide a name of the particular target
with such alignment constraints so I can test it?

-- 
Thank you,
Vit Kabele

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] c: Extend the -Wpadded message with actual padding size
  2022-06-22  8:34       ` Vit Kabele
@ 2022-06-22  8:47         ` Andrew Pinski
  2022-06-22 11:50           ` [PATCH v2] " Vit Kabele
  2022-06-27  8:04           ` [PATCH v3] " Vit Kabele
  0 siblings, 2 replies; 10+ messages in thread
From: Andrew Pinski @ 2022-06-22  8:47 UTC (permalink / raw)
  To: Vit Kabele; +Cc: Marek Polacek, vit, GCC Patches

On Wed, Jun 22, 2022 at 1:34 AM Vit Kabele <vit.kabele@sysgo.com> wrote:
>
> Hello,
>
> On Mon, Jun 20, 2022 at 04:05:17PM -0700, Andrew Pinski wrote:
> > > new file mode 100644
> > > index 00000000000..e8f1044a36b
> > > --- /dev/null
> > > +++ b/gcc/testsuite/c-c++-common/Wpadded.c
> > > @@ -0,0 +1,10 @@
> > > +/* { dg-do compile } */
> > > +/* { dg-options "-Wpadded" } */
> > > +
> > > +/*
> > > + * The struct is on single line, because C++ compiler emits the -Wpadded
> > > + * warning at the first line of the struct, while the C compiler at the last
> > > + * line of the struct definition. This way the test passes on both
> > > + */
> > > +struct S { __UINT32_TYPE__ i; char c; }; /* { dg-warning "padding struct size to alignment boundary with 3 bytes" } */
> > > +
> > Note the testcase will fail on some targets where alignment is 1 for everything.
> > You most likely want the dg-warning to be like it is in gcc.dg/Wpadded.c:
> > /* { dg-warning "padding struct size to alignment boundary with 3
> > bytes" ""  { target { ! default_packed } } } */
> >
> > You might want the following from the same file too:
> > /* -fpack-struct is necessary because the warning expected requires the initial
> >    packing to be larger than 1, which cannot be guaranteed for all targets.
> >    We won't get a warning anyway if the target has "packed" structure
> >    layout.  */
> > /* { dg-options "-Wpadded -fpack-struct=8" } */
> > /* { dg-additional-options "-mno-ms-bitfields" { target *-*-mingw* } } */
> I added the ! default_packed directive, but I am not sure whether the
> -fpack-struct is needed. Could you please provide a name of the particular target
> with such alignment constraints so I can test it?

cris is one example. See
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=23304 (which is why
default_packed was added).

Thanks,
Andrew


>
> --
> Thank you,
> Vit Kabele

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2] c: Extend the -Wpadded message with actual padding size
  2022-06-22  8:47         ` Andrew Pinski
@ 2022-06-22 11:50           ` Vit Kabele
  2022-06-27  8:04           ` [PATCH v3] " Vit Kabele
  1 sibling, 0 replies; 10+ messages in thread
From: Vit Kabele @ 2022-06-22 11:50 UTC (permalink / raw)
  To: pinskia; +Cc: gcc-patches, polacek, vit

Hello,
I added the ! default_packed directive, and now the test is properly
skipped on the targets with that property. I tested with cris-elf
target and the test behaves properly.

Best regards,
Vit Kabele

-- >8 --
Subject: [PATCH v2] c: Extend the -Wpadded message with actual padding size

When the compiler warns about padding struct to alignment boundary, it
now also informs the user about the size of the alignment that needs to
be added to get rid of the warning.

This removes the need of using pahole or similar tools, or manually
determining the padding size.

Tested for x86_64-pc-linux-gnu and cris-elf targets.

gcc/ChangeLog:

	* stor-layout.cc (finalize_record_size): Extend warning message.

gcc/testsuite/ChangeLog:

	* c-c++-common/Wpadded.c: New test.

Signed-off-by: Vit Kabele <vit.kabele@sysgo.com>
---
 gcc/stor-layout.cc                   |  7 ++++++-
 gcc/testsuite/c-c++-common/Wpadded.c | 13 +++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/c-c++-common/Wpadded.c

diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
index 765f22f68b9..88923c4136b 100644
--- a/gcc/stor-layout.cc
+++ b/gcc/stor-layout.cc
@@ -1781,7 +1781,12 @@ finalize_record_size (record_layout_info rli)
       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
       && input_location != BUILTINS_LOCATION
       && !TYPE_ARTIFICIAL (rli->t))
-    warning (OPT_Wpadded, "padding struct size to alignment boundary");
+  {
+	tree pad_size
+	  = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (rli->t), unpadded_size_unit);
+	  warning (OPT_Wpadded,
+		"padding struct size to alignment boundary with %E bytes", pad_size);
+  }
 
   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
       && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
diff --git a/gcc/testsuite/c-c++-common/Wpadded.c b/gcc/testsuite/c-c++-common/Wpadded.c
new file mode 100644
index 00000000000..9e7e9f240c1
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wpadded.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-Wpadded" } */
+
+/*
+ * The struct is on single line, because C++ compiler emits the -Wpadded
+ * warning at the first line of the struct definition, while the C compiler at
+ * the last line. This way the test passes on both.
+ * 
+ * The test is skipped on targets where default behavior is to pack the
+ * structs because there is no warning triggered.
+ */
+struct S { __UINT32_TYPE__ i; char c; } foo; /* { dg-warning "padding struct size to alignment boundary with 3 bytes" "" { target { ! default_packed } } } */
+
-- 
2.30.2

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v3] c: Extend the -Wpadded message with actual padding size
  2022-06-22  8:47         ` Andrew Pinski
  2022-06-22 11:50           ` [PATCH v2] " Vit Kabele
@ 2022-06-27  8:04           ` Vit Kabele
  2022-07-09 17:07             ` Jeff Law
  1 sibling, 1 reply; 10+ messages in thread
From: Vit Kabele @ 2022-06-27  8:04 UTC (permalink / raw)
  To: pinskia; +Cc: gcc-patches, polacek, vit, zsojka

Hello,

after further discussion I changed the ! default_packed to attribute
aligned, so that the test passes also on targets where 4 bytes types are
aligned on 2 byte boundaries.

Best regards,
Vit Kabele

-- >8 --
Subject: [PATCH v3] c: Extend the -Wpadded message with actual padding size

When the compiler warns about padding struct to alignment boundary, it
now also informs the user about the size of the alignment that needs to
be added to get rid of the warning.

This removes the need of using pahole or similar tools, or manually
determining the padding size.

Tested for x86_64-pc-linux-gnu and cris-elf targets.

gcc/ChangeLog:

	* stor-layout.cc (finalize_record_size): Extend warning message.

gcc/testsuite/ChangeLog:

	* c-c++-common/Wpadded.c: New test.

Signed-off-by: Vit Kabele <vit.kabele@sysgo.com>
---
 gcc/stor-layout.cc                   |  7 ++++++-
 gcc/testsuite/c-c++-common/Wpadded.c | 14 ++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/c-c++-common/Wpadded.c

diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
index 765f22f68b9..88923c4136b 100644
--- a/gcc/stor-layout.cc
+++ b/gcc/stor-layout.cc
@@ -1781,7 +1781,12 @@ finalize_record_size (record_layout_info rli)
       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
       && input_location != BUILTINS_LOCATION
       && !TYPE_ARTIFICIAL (rli->t))
-    warning (OPT_Wpadded, "padding struct size to alignment boundary");
+  {
+	tree pad_size
+	  = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (rli->t), unpadded_size_unit);
+	  warning (OPT_Wpadded,
+		"padding struct size to alignment boundary with %E bytes", pad_size);
+  }
 
   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
       && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
diff --git a/gcc/testsuite/c-c++-common/Wpadded.c b/gcc/testsuite/c-c++-common/Wpadded.c
new file mode 100644
index 00000000000..c5be4686822
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wpadded.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-Wpadded" } */
+
+/*
+ * The struct is on single line, because C++ compiler emits the -Wpadded
+ * warning at the first line of the struct definition, while the C compiler at
+ * the last line. This way the test passes on both.
+ *
+ * Attribute aligned is needed for the test to pass on targets where
+ * the default behaviour is to pack the struct and also on targets that align
+ * 4 byte fields to 2 byte boundary.
+ */
+struct S { __UINT32_TYPE__ i; char c; } __attribute__((aligned(4))); /* { dg-warning "padding struct size to alignment boundary with 3 bytes" } */
+
-- 
2.30.2

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3] c: Extend the -Wpadded message with actual padding size
  2022-06-27  8:04           ` [PATCH v3] " Vit Kabele
@ 2022-07-09 17:07             ` Jeff Law
  0 siblings, 0 replies; 10+ messages in thread
From: Jeff Law @ 2022-07-09 17:07 UTC (permalink / raw)
  To: gcc-patches



On 6/27/2022 2:04 AM, Vit Kabele wrote:
> gcc/ChangeLog:
>
> 	* stor-layout.cc (finalize_record_size): Extend warning message.
>
> gcc/testsuite/ChangeLog:
>
> 	* c-c++-common/Wpadded.c: New test.
Thanks.  I've pushed this to the trunk.

jeff


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-07-09 17:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-16 19:37 [PATCH] c: Extend the -Wpadded message with actual padding size Vit Kabele
2022-06-17 12:56 ` Eric Gallager
2022-06-17 14:06 ` Marek Polacek
2022-06-20 13:50   ` Vit Kabele
2022-06-20 23:05     ` Andrew Pinski
2022-06-22  8:34       ` Vit Kabele
2022-06-22  8:47         ` Andrew Pinski
2022-06-22 11:50           ` [PATCH v2] " Vit Kabele
2022-06-27  8:04           ` [PATCH v3] " Vit Kabele
2022-07-09 17:07             ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).