From patchwork Mon May 1 08:38:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volker Reichelt X-Patchwork-Id: 757006 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wGdB303Szz9s82 for ; Mon, 1 May 2017 18:38:49 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="o7XpRoNv"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:subject:to:message-id:mime-version:content-type; q=dns; s= default; b=VRXBfO1us1Fny28W2CMtIjOLoFR5k6V8VTdtvzVnu2h+Yjik8sT7t eQ6A+gkGrtl5twjrvEKFLKAaiN3nTi5vImomBS4/BE6UT6PZ6oIZYtQYCDqUFOwR 3S9K0Hscm4ZoBj06IK9RpZWujFeJQp+aUsIR+XhdY9mmJPVulei/ks= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:subject:to:message-id:mime-version:content-type; s= default; bh=Pl9pE2itutSI6kkPEiPZDmisFMc=; b=o7XpRoNvL7DMISzQu2S/ XvygTQ3Vz49wbsHFQWzwZlUSt/FJd+1824I7BkPD68VlNsIN12DiLqd0/MqMDN21 3Ruq1os82JPdo550pHGyqFlS2vWoRLLWR0yMSvmViNNvfY0XZYos+LOts9BEKmev Jxplj8he/POCpp82QONKvKk= Received: (qmail 92305 invoked by alias); 1 May 2017 08:38:36 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 92282 invoked by uid 89); 1 May 2017 08:38:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-10.8 required=5.0 tests=BAYES_00, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPAM_BODY, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: cc-smtpout3.netcologne.de Received: from cc-smtpout3.netcologne.de (HELO cc-smtpout3.netcologne.de) (89.1.8.213) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 01 May 2017 08:38:28 +0000 Received: from cc-smtpin1.netcologne.de (cc-smtpin1.netcologne.de [89.1.8.201]) by cc-smtpout3.netcologne.de (Postfix) with ESMTP id D251A12563 for ; Mon, 1 May 2017 10:38:27 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin1.netcologne.de (Postfix) with ESMTP id C4EC811DEE for ; Mon, 1 May 2017 10:38:27 +0200 (CEST) Received: from [89.0.1.231] (helo=cc-smtpin1.netcologne.de) by localhost with ESMTP (eXpurgate 4.1.9) (envelope-from ) id 5906f403-021e-7f0000012729-7f000001a0d7-1 for ; Mon, 01 May 2017 10:38:27 +0200 Received: from linux-w03z.fritz.box (xdsl-89-0-1-231.netcologne.de [89.0.1.231]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by cc-smtpin1.netcologne.de (Postfix) with ESMTPSA for ; Mon, 1 May 2017 10:38:27 +0200 (CEST) Date: Mon, 1 May 2017 10:38:26 +0200 (CEST) From: Volker Reichelt Subject: {PATCH] New C++ warning -Wcatch-value To: gcc-patches@gcc.gnu.org Message-ID: MIME-Version: 1.0 Content-Disposition: INLINE Hi, catching exceptions by value is a bad thing, as it may cause slicing, i.e. a) a superfluous copy b) which is only partial. See also https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e15-catch-exceptions-from-a-hierarchy-by-reference To warn the user about catch handlers of non-reference type, the following patch adds a new C++/ObjC++ warning option "-Wcatch-value". Bootstrapped and regtested on x86_64-pc-linux-gnu. OK for trunk? Regards, Volker 2017-05-01 Volker Reichelt * doc/invoke.texi (-Wcatch-value): Document new warning option. =================================================================== Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 247416) +++ gcc/doc/invoke.texi (working copy) @@ -265,7 +265,7 @@ -Wno-builtin-declaration-mismatch @gol -Wno-builtin-macro-redefined -Wc90-c99-compat -Wc99-c11-compat @gol -Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol --Wchar-subscripts -Wchkp -Wclobbered -Wcomment @gol +-Wchar-subscripts -Wchkp -Wcatch-value -Wclobbered -Wcomment @gol -Wconditionally-supported @gol -Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol -Wdelete-incomplete @gol @@ -5827,6 +5827,11 @@ literals to @code{char *}. This warning is enabled by default for C++ programs. +@item -Wcatch-value @r{(C++ and Objective-C++ only)} +@opindex Wcatch-value +@opindex Wno-catch-value +Warn about catch handler of non-reference type. + @item -Wclobbered @opindex Wclobbered @opindex Wno-clobbered =================================================================== 2017-05-01 Volker Reichelt * c.opt (Wcatch-value): New C++ warning flag. Index: gcc/c-family/c.opt =================================================================== --- gcc/c-family/c.opt (revision 247416) +++ gcc/c-family/c.opt (working copy) @@ -388,6 +388,10 @@ C ObjC C++ ObjC++ Var(warn_cast_qual) Warning Warn about casts which discard qualifiers. +Wcatch-value +C++ ObjC++ Var(warn_catch_value) Warning +Warn about catch handlers of non-reference type. + Wchar-subscripts C ObjC C++ ObjC++ Var(warn_char_subscripts) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn about subscripts whose type is \"char\". =================================================================== 2017-05-01 Volker Reichelt * semantics.c (finish_handler_parms): Warn about non-reference type catch handlers. Index: gcc/cp/semantics.c =================================================================== --- gcc/cp/semantics.c (revision 247416) +++ gcc/cp/semantics.c (working copy) @@ -1321,7 +1321,15 @@ } } else - type = expand_start_catch_block (decl); + { + type = expand_start_catch_block (decl); + if (warn_catch_value + && type != NULL_TREE + && type != error_mark_node + && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE) + warning (OPT_Wcatch_value, + "catching non-reference type %qT", TREE_TYPE (decl)); + } HANDLER_TYPE (handler) = type; } =================================================================== 2017-05-01 Volker Reichelt * g++.dg/warn/Wcatch-value-1.C: New test. Index: gcc/testsuite/g++.dg/warn/Wcatch-value-1.C =================================================================== --- gcc/testsuite/g++.dg/warn/Wcatch-value-1.C 2017-05-01 +++ gcc/testsuite/g++.dg/warn/Wcatch-value-1.C 2017-05-01 @@ -0,0 +1,45 @@ +// { dg-options "-Wcatch-value" } + +struct A {}; +struct B {}; +struct C {}; + +void foo() +{ + try {} + catch (int) {} // { dg-warning "catching non-reference type" } + catch (double*) {} // { dg-warning "catching non-reference type" } + catch (float&) {} + catch (A) {} // { dg-warning "catching non-reference type" } + catch (A[2]) {} // { dg-warning "catching non-reference type" } + catch (B*) {} // { dg-warning "catching non-reference type" } + catch (B&) {} + catch (const C&) {} +} + +template void foo1() +{ + try {} + catch (T) {} +} + +void bar1() +{ + foo1(); + foo1(); +} + +template void foo2() +{ + try {} + catch (T) {} // { dg-warning "catching non-reference type" } + + try {} + catch (T&) {} +} + +void bar2() +{ + foo2(); // { dg-message "required" } + foo2(); // { dg-message "required" } +}