Print padding size when aligning struct member

Message ID 20230207124156.853506-1-vit.kabele@sysgo.com
State New
Headers
Series Print padding size when aligning struct member |

Commit Message

Vit Kabele Feb. 7, 2023, 12:41 p.m. UTC
  Announce the size of introduced padding when compiling
with -Wpadded.

gcc/ChangeLog:

	* stor-layout.cc (place_field): Change warning message format

gcc/testsuite/ChangeLog:

	* c-c++-common/Wpadded.c: Add new testcase
	* gcc.dg/Wpadded.c: Update the expected warning message

Signed-off-by: Vít Kabele <vit.kabele@sysgo.com>
---
 gcc/stor-layout.cc                   | 2 +-
 gcc/testsuite/c-c++-common/Wpadded.c | 1 +
 gcc/testsuite/gcc.dg/Wpadded.c       | 2 +-
 3 files changed, 3 insertions(+), 2 deletions(-)
  

Comments

Andreas Schwab Feb. 7, 2023, 1:08 p.m. UTC | #1
On Feb 07 2023, Vít Kabele wrote:

> diff --git a/gcc/testsuite/gcc.dg/Wpadded.c b/gcc/testsuite/gcc.dg/Wpadded.c
> index 70fcd79a6d4..357e7f61e4a 100644
> --- a/gcc/testsuite/gcc.dg/Wpadded.c
> +++ b/gcc/testsuite/gcc.dg/Wpadded.c
> @@ -10,5 +10,5 @@
>  
>  struct foo {
>    char bar;
> -  long baz; /* { dg-warning "padding struct to align" ""  { target { ! default_packed } } } */
> +  long baz; /* { dg-warning "padding struct with 7 bytes to align" ""  { target { ! default_packed } } } */

The actual amount of padding is target dependent.
  
Vit Kabele Feb. 7, 2023, 2:09 p.m. UTC | #2
On Tue, Feb 07, 2023 at 02:08:35PM +0100, Andreas Schwab wrote:
> On Feb 07 2023, Vít Kabele wrote:
> 
> > diff --git a/gcc/testsuite/gcc.dg/Wpadded.c b/gcc/testsuite/gcc.dg/Wpadded.c
> > index 70fcd79a6d4..357e7f61e4a 100644
> > --- a/gcc/testsuite/gcc.dg/Wpadded.c
> > +++ b/gcc/testsuite/gcc.dg/Wpadded.c
> > @@ -10,5 +10,5 @@
> >  
> >  struct foo {
> >    char bar;
> > -  long baz; /* { dg-warning "padding struct to align" ""  { target { ! default_packed } } } */
> > +  long baz; /* { dg-warning "padding struct with 7 bytes to align" ""  { target { ! default_packed } } } */
> 
> The actual amount of padding is target dependent.
Ah sorry, would removing everything after `with...` be ok?

Like this:
> +  long baz; /* { dg-warning "padding struct with" ""  { target { ! default_packed } } } */
  

Patch

diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
index 45bf2d18639..fb1948b94af 100644
--- a/gcc/stor-layout.cc
+++ b/gcc/stor-layout.cc
@@ -1346,7 +1346,7 @@  place_field (record_layout_info rli, tree field)
       if (!targetm.ms_bitfield_layout_p (rli->t)
 	  && DECL_SOURCE_LOCATION (field) != BUILTINS_LOCATION
 	  && !TYPE_ARTIFICIAL (rli->t))
-	warning (OPT_Wpadded, "padding struct to align %q+D", field);
+		warning (OPT_Wpadded, "padding struct with %d bytes to align %q+D", (desired_align - known_align)/8, field);
 
       /* If the alignment is still within offset_align, just align
 	 the bit position.  */
diff --git a/gcc/testsuite/c-c++-common/Wpadded.c b/gcc/testsuite/c-c++-common/Wpadded.c
index c5be4686822..35ff013026f 100644
--- a/gcc/testsuite/c-c++-common/Wpadded.c
+++ b/gcc/testsuite/c-c++-common/Wpadded.c
@@ -11,4 +11,5 @@ 
  * 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" } */
+struct R { char c; __UINT32_TYPE__ i; } __attribute__((aligned(4))); /* { dg-warning "padding struct with 3 bytes to align 'i'" } */
 
diff --git a/gcc/testsuite/gcc.dg/Wpadded.c b/gcc/testsuite/gcc.dg/Wpadded.c
index 70fcd79a6d4..357e7f61e4a 100644
--- a/gcc/testsuite/gcc.dg/Wpadded.c
+++ b/gcc/testsuite/gcc.dg/Wpadded.c
@@ -10,5 +10,5 @@ 
 
 struct foo {
   char bar;
-  long baz; /* { dg-warning "padding struct to align" ""  { target { ! default_packed } } } */
+  long baz; /* { dg-warning "padding struct with 7 bytes to align" ""  { target { ! default_packed } } } */
 } futz;