From patchwork Sat Sep 12 19:47:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1362909 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: 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@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: 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=IraSianA; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Bpjnj2mNvz9sTN for ; Sun, 13 Sep 2020 05:47:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E4063857C62; Sat, 12 Sep 2020 19:47:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E4063857C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1599940037; bh=h5MRd+xVlkChHFaKl5qXp9O079gzl6GnMzySgDwF1Mk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IraSianAdI4RQxvgs+CVpClXzUNv4KZLldH8nSNS5T0G2pIFGHH/+AJvOi5LVsKmQ 9Iv1W1wlVv58Fw/wUjqLEhLlgcW73t0tZwA9QuorunPENawxb/GuqItcBLLOLPp1CG TsZfpo4nA209nkoJnhQBhSpwWmniFanv7SZ+NEPc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by sourceware.org (Postfix) with ESMTPS id 4BA903857C62 for ; Sat, 12 Sep 2020 19:47:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4BA903857C62 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (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-202.mailbox.org (Postfix) with ESMTPS id 4BpjnX62j4zQlK8; Sat, 12 Sep 2020 21:47:12 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id zNSZp0dgPhpR; Sat, 12 Sep 2020 21:47:09 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Return promoted types in d_type_promotes_to when linkage is not D Date: Sat, 12 Sep 2020 21:47:06 +0200 Message-Id: <20200912194706.582714-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: X-Rspamd-Score: -0.40 / 15.00 / 15.00 X-Rspamd-Queue-Id: 9023B14AF X-Rspamd-UID: f63998 X-Spam-Status: No, score=-15.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@gcc.gnu.org Sender: "Gcc-patches" Hi, This enables warnings to be shown when a wrong type is passed to va_arg inside an extern(C) or extern(C++) function. Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-mx32. Committed to mainline. Regards Iain --- gcc/d/ChangeLog: PR d/97002 * d-codegen.cc (d_build_call): Set input_location on CALL_EXPR. * d-lang.cc: Include function.h. (d_type_promotes_to): Do default conversions for C and C++ functions. * intrinsics.cc (expand_intrinsic_vaarg): Use build1_loc to build VA_ARG_EXPR. gcc/testsuite/ChangeLog: PR d/97002 * gdc.dg/pr97002.d: New test. --- gcc/d/d-codegen.cc | 1 + gcc/d/d-lang.cc | 58 ++++++++++++++++++++++++++++++++-- gcc/d/intrinsics.cc | 2 +- gcc/testsuite/gdc.dg/pr97002.d | 55 ++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/pr97002.d diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc index 4050b85af28..1f2d65c4ae2 100644 --- a/gcc/d/d-codegen.cc +++ b/gcc/d/d-codegen.cc @@ -2042,6 +2042,7 @@ d_build_call (TypeFunction *tf, tree callable, tree object, } tree result = build_call_vec (TREE_TYPE (ctype), callee, args); + SET_EXPR_LOCATION (result, input_location); /* Enforce left to right evaluation. */ if (tf->linkage == LINKd) diff --git a/gcc/d/d-lang.cc b/gcc/d/d-lang.cc index c5254a02446..fb95716f918 100644 --- a/gcc/d/d-lang.cc +++ b/gcc/d/d-lang.cc @@ -43,6 +43,7 @@ along with GCC; see the file COPYING3. If not see #include "langhooks.h" #include "langhooks-def.h" #include "target.h" +#include "function.h" #include "stringpool.h" #include "stor-layout.h" #include "varasm.h" @@ -1358,12 +1359,65 @@ d_type_for_size (unsigned bits, int unsignedp) return 0; } -/* Implements the lang_hooks.types.type_promotes_to routine for language D. - All promotions for variable arguments are handled by the D frontend. */ +/* Implements the lang_hooks.types.type_promotes_to routine for language D. */ static tree d_type_promotes_to (tree type) { + /* Promotions are only applied on unnamed function arguments for declarations + with `extern(C)' or `extern(C++)' linkage. */ + if (cfun && DECL_LANG_FRONTEND (cfun->decl) + && DECL_LANG_FRONTEND (cfun->decl)->linkage != LINKd) + { + /* In [type/integer-promotions], integer promotions are conversions of the + following types: + + bool int + byte int + ubyte int + short int + ushort int + char int + wchar int + dchar uint + + If an enum has as a base type one of the types in the left column, it + is converted to the type in the right column. */ + if (TREE_CODE (type) == ENUMERAL_TYPE && ENUM_IS_SCOPED (type)) + type = TREE_TYPE (type); + + type = TYPE_MAIN_VARIANT (type); + + /* Check for promotions of target-defined types first. */ + tree promoted_type = targetm.promoted_type (type); + if (promoted_type) + return promoted_type; + + if (TREE_CODE (type) == BOOLEAN_TYPE) + return d_int_type; + + if (INTEGRAL_TYPE_P (type)) + { + if (type == d_byte_type || type == d_ubyte_type + || type == d_short_type || type == d_ushort_type + || type == char8_type_node || type == char16_type_node) + return d_int_type; + + if (type == char32_type_node) + return d_uint_type; + + if (TYPE_PRECISION (type) < TYPE_PRECISION (d_int_type)) + return d_int_type; + } + + /* Float arguments are converted to doubles. */ + if (type == float_type_node) + return double_type_node; + + if (type == ifloat_type_node) + return idouble_type_node; + } + return type; } diff --git a/gcc/d/intrinsics.cc b/gcc/d/intrinsics.cc index 1b12a3ec6fb..a629472c6c5 100644 --- a/gcc/d/intrinsics.cc +++ b/gcc/d/intrinsics.cc @@ -562,7 +562,7 @@ expand_intrinsic_vaarg (tree callexp) } /* (T) VA_ARG_EXP; */ - tree exp = build1 (VA_ARG_EXPR, type, ap); + tree exp = build1_loc (EXPR_LOCATION (callexp), VA_ARG_EXPR, type, ap); /* parmn = (T) VA_ARG_EXP; */ if (parmn != NULL_TREE) diff --git a/gcc/testsuite/gdc.dg/pr97002.d b/gcc/testsuite/gdc.dg/pr97002.d new file mode 100644 index 00000000000..5c537db3a0b --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr97002.d @@ -0,0 +1,55 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97002 +// { dg-do compile } + +import core.stdc.stdarg; + +enum E1 : bool { one, two } +enum E2 : short { one, two } +enum E3 : dchar { one, two } +enum E4 : float { one, two } + +extern(C) void fun(void *p1, ...) +{ + va_arg!bool(_argptr); // { dg-warning "'bool' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: .so you should pass .int. not .bool. to .va_arg.." "int not bool" { target *-*-* } .-1 } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-2 } + + va_arg!byte(_argptr); // { dg-warning "'byte' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!ubyte(_argptr); // { dg-warning "'ubyte' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!short(_argptr); // { dg-warning "'short' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!ushort(_argptr); // { dg-warning "'ushort' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!char(_argptr); // { dg-warning "'char' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!wchar(_argptr); // { dg-warning "'wchar' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!dchar(_argptr); // { dg-warning "'dchar' is promoted to 'uint' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!float(_argptr); // { dg-warning "'float' is promoted to 'double' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!ifloat(_argptr); // { dg-warning "'ifloat' is promoted to 'idouble' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!E1(_argptr); // { dg-warning "'E1' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!E2(_argptr); // { dg-warning "'E2' is promoted to 'int' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!E3(_argptr); // { dg-warning "'E3' is promoted to 'uint' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } + + va_arg!E4(_argptr); // { dg-warning "'E4' is promoted to 'double' when passed through '...'" "promoted" } + // { dg-message "note: if this code is reached, the program will abort" "will abort" { target *-*-* } .-1 } +}