From: Vit Kabele <vit.kabele@sysgo.com>
To: pinskia@gmail.com
Cc: gcc-patches@gcc.gnu.org, polacek@redhat.com, vit@kabele.me,
zsojka@seznam.cz
Subject: [PATCH v3] c: Extend the -Wpadded message with actual padding size
Date: Mon, 27 Jun 2022 10:04:20 +0200 [thread overview]
Message-ID: <YrlkhETL4ZhFCU6X@czspare1-lap.sysgo.cz> (raw)
In-Reply-To: <CA+=Sn1mtQ3C+6wfZRSeiY9bX_3Awn2JmcBRmquacSU0LzZ6TMg@mail.gmail.com>
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
next prev parent reply other threads:[~2022-06-27 8:04 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-16 19:37 [PATCH] " 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 ` Vit Kabele [this message]
2022-07-09 17:07 ` [PATCH v3] " Jeff Law
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YrlkhETL4ZhFCU6X@czspare1-lap.sysgo.cz \
--to=vit.kabele@sysgo.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=pinskia@gmail.com \
--cc=polacek@redhat.com \
--cc=vit@kabele.me \
--cc=zsojka@seznam.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).