From patchwork Thu Sep 10 16:09:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1361676 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=2620:52:3:1:0:246e:9693:128c; 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=UWoAb0NC; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4BnP342k32z9sPB for ; Fri, 11 Sep 2020 02:09:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2DAF53972032; Thu, 10 Sep 2020 16:09:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2DAF53972032 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1599754158; bh=Hsed04q5JlRK7ad+A99QdyWXjBZiqEruBwwj+me81cI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=UWoAb0NCq16jX5LCcGu4cy2qJisNAcAKgC2kVWWlL087xW+T6fEf7rfJgFzrfltP6 oiO4RxEpMzyEZVgPUi0y4bJoFnjfiay4ssbWiMwVDbi/jrWc/idV6V5a0qxTj0zx2T tDsWOZgpWcop9n21m/uIFXm/jF83kZ4l3tE009g8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by sourceware.org (Postfix) with ESMTPS id DE7A6385042C for ; Thu, 10 Sep 2020 16:09:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DE7A6385042C 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-201.mailbox.org (Postfix) with ESMTPS id 4BnP2y1NJHzQlL9; Thu, 10 Sep 2020 18:09:14 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id qsHrvI_aa8-j; Thu, 10 Sep 2020 18:09:04 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Warn when casting from a D class to a C++ class. Date: Thu, 10 Sep 2020 18:09:02 +0200 Message-Id: <20200910160902.3441121-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: * X-Rspamd-Score: 1.14 / 15.00 / 15.00 X-Rspamd-Queue-Id: 0CCF7269 X-Rspamd-UID: 902df9 X-Spam-Status: No, score=-15.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 patch fixes a missed warning opportunity when casting from a D class to a C++ class, and adds tests covering warnings within the code generator. Before, the warning was only issued when casting in the other direction. Now a warning is printed for both directions. Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-x32. Committed to mainline. Regards Iain. --- gcc/d/ChangeLog: * d-convert.cc (convert_expr): Warn when casting from a D class to a C++ class. gcc/testsuite/ChangeLog: * gdc.dg/Waddress.d: New test. * gdc.dg/Wcastresult1.d: New test. * gdc.dg/Wcastresult2.d: New test. --- gcc/d/d-convert.cc | 4 ++-- gcc/testsuite/gdc.dg/Waddress.d | 12 ++++++++++++ gcc/testsuite/gdc.dg/Wcastresult1.d | 18 ++++++++++++++++++ gcc/testsuite/gdc.dg/Wcastresult2.d | 12 ++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/Waddress.d create mode 100644 gcc/testsuite/gdc.dg/Wcastresult1.d create mode 100644 gcc/testsuite/gdc.dg/Wcastresult2.d diff --git a/gcc/d/d-convert.cc b/gcc/d/d-convert.cc index 5e3e855b2cb..40e84f05347 100644 --- a/gcc/d/d-convert.cc +++ b/gcc/d/d-convert.cc @@ -430,10 +430,10 @@ convert_expr (tree exp, Type *etype, Type *totype) /* d_convert will make a no-op cast. */ break; } - else if (cdfrom->isCPPclass ()) + else if (cdfrom->isCPPclass () || cdto->isCPPclass ()) { /* Downcasting in C++ is a no-op. */ - if (cdto->isCPPclass ()) + if (cdfrom->isCPPclass () && cdto->isCPPclass ()) break; /* Casting from a C++ interface to a class/non-C++ interface diff --git a/gcc/testsuite/gdc.dg/Waddress.d b/gcc/testsuite/gdc.dg/Waddress.d new file mode 100644 index 00000000000..74d7de80775 --- /dev/null +++ b/gcc/testsuite/gdc.dg/Waddress.d @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-Waddress" } + +void* ptr; + +int test() +{ + if (&ptr) // { dg-warning "the address of 'ptr' will always evaluate as 'true'" } + return 1; + + return 0; +} diff --git a/gcc/testsuite/gdc.dg/Wcastresult1.d b/gcc/testsuite/gdc.dg/Wcastresult1.d new file mode 100644 index 00000000000..759b338acdc --- /dev/null +++ b/gcc/testsuite/gdc.dg/Wcastresult1.d @@ -0,0 +1,18 @@ +// { dg-do compile } +// { dg-options "-Wcast-result" } + +extern(C++) class CPPClass +{ + int a; +} + +extern(D) class DClass +{ + int a; +} + +void test() +{ + auto cpptod = cast(DClass)new CPPClass; // { dg-warning "cast to 'Wcastresult1.DClass' will produce null result" } + auto dtocpp = cast(CPPClass)new DClass; // { dg-warning "cast to 'Wcastresult1.CPPClass' will produce null result" } +} diff --git a/gcc/testsuite/gdc.dg/Wcastresult2.d b/gcc/testsuite/gdc.dg/Wcastresult2.d new file mode 100644 index 00000000000..56d2dd20e82 --- /dev/null +++ b/gcc/testsuite/gdc.dg/Wcastresult2.d @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-Wcast-result" } + +void test() +{ + auto imvalue = 1.23i; + auto revalue = 1.23; + + auto imtore = cast(double)imvalue; // { dg-warning "cast from 'idouble' to 'double' will produce zero result" } + auto retoim = cast(idouble)revalue; // { dg-warning "cast from 'double' to 'idouble' will produce zero result" } + return; +}