From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.sysgo.com (mail.sysgo.com [IPv6:2a01:4f8:fff0:cf:2::25]) by sourceware.org (Postfix) with ESMTPS id 3E287384D1B3 for ; Mon, 20 Jun 2022 13:50:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3E287384D1B3 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sysgo.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sysgo.com Date: Mon, 20 Jun 2022 15:50:10 +0200 From: Vit Kabele To: polacek@redhat.com Cc: vit@kabele.me, gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c: Extend the -Wpadded message with actual padding size Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Jun 2022 13:50:14 -0000 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 --- 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