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: 1790584 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=lbtYO1ca; dkim-atps=neutral Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QZfrN1mcwz20Q8 for ; Tue, 6 Jun 2023 02:56:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6672138845A2 for ; Mon, 5 Jun 2023 16:56:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6672138845A2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685984160; 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=lbtYO1camFW8XSUBTOrrrAc5eESYXnr2Wj3+kFnB7mrz5ALC+sRG72NrtuBy5phMu CRFmDjK4qC9C1CShaQzR7nOYTgC1bt2HkfbxSuI3FFJDjLLdFvjTPXpIMsWXZGaN94 j+/ErfGoJssCQah4gbN82rv45mT8ttJnU0wQ01Bg= 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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" }