From patchwork Fri Jul 16 00:03:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1505954 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+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) 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=E2/9vLaI; 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 4GQs1Q31G8z9sX3 for ; Fri, 16 Jul 2021 10:04:40 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D90AC398D43C for ; Fri, 16 Jul 2021 00:04:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D90AC398D43C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1626393876; bh=WDtlY1WDedtIIqpo4WRNSebR2Z5qYwgH6QBWM0UYrP8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=E2/9vLaIguILQIYaj3BEop3zo331gFUbvfLqQ9u6CRw+aTMbcL7hOCGFI3huJwdWC WXfzyvSskDW/k6iJCWohC9fbmxlBs8DBOQtoYjolRAaMjTygrO14X1u57Zgf9tpVNr IK9xugA4ZalkyfSsYYhMohl0HgwbMNXBE8+A8duc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id AAFDD385C40F for ; Fri, 16 Jul 2021 00:03:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AAFDD385C40F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-603-fZ4xzBCfP0uE_PtMdZBzAQ-1; Thu, 15 Jul 2021 20:03:50 -0400 X-MC-Unique: fZ4xzBCfP0uE_PtMdZBzAQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 751C0180F18A for ; Fri, 16 Jul 2021 00:03:49 +0000 (UTC) Received: from pdp-11.hsd1.ma.comcast.net (ovpn-114-138.rdu2.redhat.com [10.10.114.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1112C60871; Fri, 16 Jul 2021 00:03:48 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Don't hide narrowing errors in system headers Date: Thu, 15 Jul 2021 20:03:44 -0400 Message-Id: <20210716000344.1034784-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Jonathan pointed me at this issue where constexpr unsigned f() { constexpr int n = -1; return unsigned{n}; } is accepted in system headers, despite the narrowing conversion from a constant. I suspect that whereas narrowing warnings should be disabled, ill-formed narrowing of constants should be a hard error (which can still be disabled by -Wno-narrowing). Bootstrapped/regtested on {ppc64le,x86_64}-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: * typeck2.c (check_narrowing): Don't suppress the pedantic error in system headers. libstdc++-v3/ChangeLog: * testsuite/20_util/ratio/operations/ops_overflow_neg.cc: Add dg-error. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/Wnarrowing2.C: New test. * g++.dg/cpp1y/Wnarrowing2.h: New test. --- gcc/cp/typeck2.c | 1 + gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C | 4 ++++ gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h | 2 ++ .../testsuite/20_util/ratio/operations/ops_overflow_neg.cc | 2 ++ 4 files changed, 9 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h base-commit: f364cdffa47af574f90f671b2dcf5afa91442741 diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 6679e247816..dcfdff2f905 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -986,6 +986,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain, { int savederrorcount = errorcount; global_dc->pedantic_errors = 1; + auto s = make_temp_override (global_dc->dc_warn_system_headers, true); pedwarn (loc, OPT_Wnarrowing, "narrowing conversion of %qE from %qH to %qI", init, ftype, type); diff --git a/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C new file mode 100644 index 00000000000..048d484f46f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C @@ -0,0 +1,4 @@ +// { dg-do compile { target c++14 } } + +#include "Wnarrowing2.h" +// { dg-error "narrowing conversion" "" { target *-*-* } 0 } diff --git a/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h new file mode 100644 index 00000000000..7dafa51af14 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h @@ -0,0 +1,2 @@ +#pragma GCC system_header +constexpr unsigned f() { constexpr int n = -1; return unsigned{n}; } diff --git a/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc b/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc index 47d3c3a037e..f120e599a33 100644 --- a/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc +++ b/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc @@ -39,6 +39,7 @@ test02() } // { dg-error "required from here" "" { target *-*-* } 28 } +// { dg-error "expected initializer" "" { target *-*-* } 28 } // { dg-error "expected initializer" "" { target *-*-* } 35 } // { dg-error "expected initializer" "" { target *-*-* } 37 } // { dg-error "overflow in addition" "" { target *-*-* } 0 } @@ -46,5 +47,6 @@ test02() // { dg-error "overflow in multiplication" "" { target *-*-* } 100 } // { dg-error "overflow in multiplication" "" { target *-*-* } 102 } // { dg-error "overflow in constant expression" "" { target *-*-* } 0 } +// { dg-error "narrowing conversion" "" { target *-*-* } 0 } // { dg-prune-output "out of range" } // { dg-prune-output "not usable in a constant expression" }