From patchwork Mon Jun 5 16:55:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 103397 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2822676vqr; Mon, 5 Jun 2023 09:56:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ45v1PDjIvok8AdZdeBN1MWeEFQq9YIysuBx56cgujqRrIn3rxneotG04UVqwznZbKNNkS8 X-Received: by 2002:a17:907:9709:b0:977:cc4b:eeb1 with SMTP id jg9-20020a170907970900b00977cc4beeb1mr4840995ejc.19.1685984185314; Mon, 05 Jun 2023 09:56:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685984185; cv=none; d=google.com; s=arc-20160816; b=uQIYkt8mGN/pye+zs7foh0x62bGdwT3ZKuTwkBiLdMXdZq7ubyVC67Zy6IZCjijq98 aCmeqfwcXJ79eiW8CTP1eSRM4YpFXqaLLn5V2Yn0zHSLh/cRm66BuEQiVRFOkFq6U76v vfnBezzzQxaG66XygZPtsAYj6Q0slnl3BGCDE2mBdB2sSM5O1U0uYL4KiquK3rw6H/eo lPoElVbzuQUN3jG8Tqeg/fg5TXiO0LdpFYhE/LkvdxC+3YZzGb2d+fm7X2lVnoPCu+6c diX+z1x5mLIyofIwP0gc9zbTYf9mMz2jkJHE2g5TQxZB2xrBXqvv5jyCdC/UBhJ7X+tI a2Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=/B88kuLhDjEcuzFn7esL8uI+EGVr3Gb1xDML2X1xZP4=; b=bAo9JJsxmyJB3BJW3neo7YLOBmam7kVqUr+abKGiAg0kM4b0K1gGm79P01bJv5vSZc 607Tdt8wmSnfyk8sgL2U2Fd8z82GxtKUHeL/RSmZhrlUC420Yzns0VMTEbMAawQ6w7C/ wHyQ+G58lcUTXWZ9vHeeBkDwZqhhRNGINF+bMS8YF0Z+EGAlkKoX9zTufTfAcl/Vph2k YkjULg50hkNu8bTsDOEcihVHjs2hn+q0ea2wm2JMD8IIl+8Lalvqj68HomZmlugiesxp 3nmRFOVTzLVOUcmBYCUUNnaoTH5pL9Dh047EjPmLq9u9UTtlXBvSYoPcJTtgsxB8kt9O H75w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=GJ5Dp50h; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org ([8.43.85.97]) by mx.google.com with ESMTPS id qk18-20020a1709077f9200b00977d46f8f86si2366834ejc.369.2023.06.05.09.56.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 09:56:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=GJ5Dp50h; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3D63A38845A2 for ; Mon, 5 Jun 2023 16:56:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D63A38845A2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685984184; bh=/B88kuLhDjEcuzFn7esL8uI+EGVr3Gb1xDML2X1xZP4=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=GJ5Dp50hFPC1jsjpN+Ggq6h+VCSPgfHcmMqi98eERV074jMjz1oqTX6fjogTSoiiY bJflxJK+vxYc+73N0Gtet5eOmsG4E9rkB7Adr08MmrwVoNq2OCs3thGPiV2aaCqTVO n9F4YR6uqknTC1sNz0jiqyTzb0lAEboo7nOtHo6A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [IPv6:2001:67c:2050:0:465::102]) by sourceware.org (Postfix) with ESMTPS id 6CF4C388301F for ; Mon, 5 Jun 2023 16:55:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6CF4C388301F Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4QZfqp37xLz9sbl; Mon, 5 Jun 2023 18:55:34 +0200 (CEST) To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Warn when declared size of a special enum does not match its intrinsic type. Date: Mon, 5 Jun 2023 18:55:31 +0200 Message-Id: <20230605165531.1009946-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4QZfqp37xLz9sbl X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP, 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-Patchwork-Original-From: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767882552619015488?= X-GMAIL-MSGID: =?utf-8?q?1767882552619015488?= Hi, All special enums have declarations in the D runtime library, but the compiler will recognize and treat them specially if declared in any module. When the underlying base type of a special enum is a different size to its matched intrinsic, then this can cause undefined behavior at runtime. Detect and warn about when such a mismatch occurs. This was found when merging the D front-end with the v2.103.1 release, splitting this out of the merge patch into its own standalone change. Bootstrapped and regression tested on x86_64-linux-gnu, committed to mainline and backported to the releases/gcc-13 branch. Regards, Iain. --- gcc/d/ChangeLog: * gdc.texi (Warnings): Document -Wextra and -Wmismatched-special-enum. * implement-d.texi (Special Enums): Add reference to warning option -Wmismatched-special-enum. * lang.opt: Add -Wextra and -Wmismatched-special-enum. * types.cc (TypeVisitor::visit (TypeEnum *)): Warn when declared special enum size mismatches its intrinsic type. gcc/testsuite/ChangeLog: * gdc.dg/Wmismatched_enum.d: New test. --- gcc/d/gdc.texi | 17 +++++++++++++++++ gcc/d/implement-d.texi | 5 +++++ gcc/d/lang.opt | 8 ++++++++ gcc/d/types.cc | 15 +++++++++++++++ gcc/testsuite/gdc.dg/Wmismatched_enum.d | 4 ++++ 5 files changed, 49 insertions(+) create mode 100644 gcc/testsuite/gdc.dg/Wmismatched_enum.d diff --git a/gcc/d/gdc.texi b/gcc/d/gdc.texi index 24b6ee00478..6f81967a83d 100644 --- a/gcc/d/gdc.texi +++ b/gcc/d/gdc.texi @@ -699,6 +699,23 @@ Do not warn about usage of deprecated features and symbols with @item -Werror Turns all warnings into errors. +@opindex Wextra +@opindex Wno-extra +@item -Wextra +This enables some extra warning flags that are not enabled by +@option{-Wall}. + +@gccoptlist{-Waddress +-Wcast-result +-Wmismatched-special-enum +-Wunknown-pragmas} + +@opindex Wmismatched-special-enum +@opindex Wno-mismatched-special-enum +@item -Wmismatched-special-enum +Warn when an enum the compiler recognizes as special is declared with a +different size to the built-in type it is representing. + @opindex Wspeculative @opindex Wno-speculative @item -Wspeculative diff --git a/gcc/d/implement-d.texi b/gcc/d/implement-d.texi index 039e5fbd24e..6f33bc192fe 100644 --- a/gcc/d/implement-d.texi +++ b/gcc/d/implement-d.texi @@ -2085,6 +2085,11 @@ for convenience: @code{c_complex_double}, @code{c_complex_float}, @code{c_complex_real}, @code{cpp_long}, @code{cpp_longlong}, @code{c_long_double}, @code{cpp_ulong}, @code{cpp_ulonglong}. +It may cause undefined behavior at runtime if a special enum is declared with a +base type that has a different size to the target C/C++ type it is +representing. The GNU D compiler will catch such declarations and emit a +warning when the @option{-Wmismatched-special-enum} option is seen on the +command-line. @c -------------------------------------------------------- diff --git a/gcc/d/lang.opt b/gcc/d/lang.opt index bb0a3dcc911..26ca92c4c17 100644 --- a/gcc/d/lang.opt +++ b/gcc/d/lang.opt @@ -134,6 +134,14 @@ Werror D ; Documented in common.opt +Wextra +D Warning +; Documented in common.opt + +Wmismatched-special-enum +D Warning Var(warn_mismatched_special_enum) LangEnabledBy(D, Wextra) +Warn when a special enum is declared with the wrong base type. + Wpsabi D ; Documented in C diff --git a/gcc/d/types.cc b/gcc/d/types.cc index beaf2a61af9..a4c05bfb75f 100644 --- a/gcc/d/types.cc +++ b/gcc/d/types.cc @@ -1067,6 +1067,21 @@ public: gcc_assert (underlying != NULL); t->ctype = build_variant_type_copy (build_ctype (underlying)); + + /* When the size of the declared enum base type doesn't match the target + C type that this enum is being used as a placeholder for, we can't + use the generated underlying type as it'll conflict with all sizes + the front-end has computed during semantic. */ + if (TYPE_SIZE (t->ctype) != TYPE_SIZE (basetype)) + { + warning_at (make_location_t (t->sym->loc), + OPT_Wmismatched_special_enum, + "size of %qs (%wd) differ from its declared size (%wd)", + t->sym->ident->toChars (), int_size_in_bytes (t->ctype), + int_size_in_bytes (basetype)); + t->ctype = basetype; + } + build_type_decl (t->ctype, t->sym); } else if (t->sym->ident == NULL diff --git a/gcc/testsuite/gdc.dg/Wmismatched_enum.d b/gcc/testsuite/gdc.dg/Wmismatched_enum.d new file mode 100644 index 00000000000..54f47988c2b --- /dev/null +++ b/gcc/testsuite/gdc.dg/Wmismatched_enum.d @@ -0,0 +1,4 @@ +// { dg-do compile } +// { dg-options "-Wmismatched-special-enum" } + +enum __c_longlong : byte; // { dg-warning "differ from its declared size" }