From patchwork Sun Apr 22 19:17:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dave.pagan@oracle.com X-Patchwork-Id: 902683 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-476707-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="aSLTmcrQ"; dkim-atps=neutral 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 40TfW44SrJz9s15 for ; Mon, 23 Apr 2018 05:17:49 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=OFU7pROetmxifV63hDL42h/AbW74P7s7Ap3FsGK3PdKvclJ5Fo WGS+1M2niMIQr3Xu3/gEf7iUTfIcXHqMlh+Vf2Mfb1jF/ZLIvG8WciRwg1m7f43a QVJCzLxRS/cItwi7Hh4zwCSTdWgRZ5e3dY+9T5IxSqo8NqtWEcJTSqoU8= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=9s0eHhaqCWLyEpbew/JbM47YH1o=; b=aSLTmcrQBx1OrjaeCKlt ra81TFJpZrsQsFRxb0ZYE4B8hJMMn6X1TvUxuKyo3RYU4U9+85l8VmpsLgwFzQfR SMCrTrW5aIu/8chx+Qye7Q2EeT/1US5Wn4u53HCmPzTP1GnHcdFyH+mzPyr2MFwL ozUoTIvDMBOQpn7rN7hK9KE= Received: (qmail 10846 invoked by alias); 22 Apr 2018 19:17:41 -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 10824 invoked by uid 89); 22 Apr 2018 19:17:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=grokdeclarator, H*Ad:D*oracle.com, werror, H*r:Gateway X-HELO: userp2120.oracle.com Received: from userp2120.oracle.com (HELO userp2120.oracle.com) (156.151.31.85) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 22 Apr 2018 19:17:33 +0000 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w3MJH2WB071074 for ; Sun, 22 Apr 2018 19:17:32 GMT Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2120.oracle.com with ESMTP id 2hfwy9apjn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Sun, 22 Apr 2018 19:17:31 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w3MJHUfx015437 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Sun, 22 Apr 2018 19:17:30 GMT Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w3MJHTZd022367 for ; Sun, 22 Apr 2018 19:17:30 GMT Received: from [192.168.1.21] (/97.86.128.167) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sun, 22 Apr 2018 12:17:29 -0700 To: Gcc Patch List From: dave.pagan@oracle.com Subject: [PATCH] c/55976 -Werror=return-type should error on returning a value from a void function Message-ID: Date: Sun, 22 Apr 2018 12:17:28 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8871 signatures=668698 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1804220217 X-IsSubscribed: yes This patch fixes handling of -Werror=return-type as well as -Wno-return-type. Currently, -Werror=return-type does not turn the warnings into errors and -Wno-return-type does not turn off warning/error. Now they both work as expected. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55976 Initialize warn_return_type only for C++/C++ with ObjC extensions. In C, this allows us to differentiate between default (no option), or cases where -Wreturn-type/-Wno-return-type are specified. Elsewhere, update references to warn_return_type (for C) to reflect change in initialization. Patch was successfully bootstrapped and tested on x86_64-linux. --Dave /c 2018-04-22 David Pagan PR c/55976 * c-decl.c (grokdeclarator): Update check for return type warnings. (start_function): Likewise. (finish_function): Likewise. * c-typeck.c (c_finish_return): Update check for return type warnings. Pass OPT_Wreturn_type to pedwarn when appropriate. * c-opts.c (c_common_post_options): Set default for warn_return_type for C++/C++ with ObjC extensions only. For C, makes it possible to differentiate between default (no option), -Wreturn-type, and -Wno-return-type. /testsuite 2018-04-22 David Pagan PR c/55976 * gcc.dg/noncompile/pr55976-1.c: New test. * gcc.dg/noncompile/pr55976-2.c: New test. Index: gcc/c/c-decl.c =================================================================== --- gcc/c/c-decl.c (revision 259017) +++ gcc/c/c-decl.c (working copy) @@ -5745,7 +5745,7 @@ grokdeclarator (const struct c_declarator *declara /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type and this is a function, or if -Wimplicit; prefer the former warning since it is more explicit. */ - if ((warn_implicit_int || warn_return_type || flag_isoc99) + if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99) && funcdef_flag) warn_about_return_type = 1; else @@ -8739,7 +8739,7 @@ start_function (struct c_declspecs *declspecs, str if (warn_about_return_type) warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int - : (warn_return_type ? OPT_Wreturn_type + : (warn_return_type > 0 ? OPT_Wreturn_type : OPT_Wimplicit_int), "return type defaults to %"); @@ -9450,8 +9450,9 @@ finish_function (void) finish_fname_decls (); - /* Complain if there's just no return statement. */ - if (warn_return_type + /* Complain if there's no return statement only if option specified on + command line. */ + if (warn_return_type > 0 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE && !current_function_returns_value && !current_function_returns_null /* Don't complain if we are no-return. */ Index: gcc/c/c-typeck.c =================================================================== --- gcc/c/c-typeck.c (revision 259017) +++ gcc/c/c-typeck.c (working copy) @@ -10129,13 +10129,13 @@ c_finish_return (location_t loc, tree retval, tree if (!retval) { current_function_returns_null = 1; - if ((warn_return_type || flag_isoc99) + if ((warn_return_type >= 0 || flag_isoc99) && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE) { bool warned_here; if (flag_isoc99) warned_here = pedwarn - (loc, 0, + (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0, "% with no value, in function returning non-void"); else warned_here = warning_at @@ -10153,7 +10153,7 @@ c_finish_return (location_t loc, tree retval, tree bool warned_here; if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) warned_here = pedwarn - (xloc, 0, + (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0, "% with a value, in function returning void"); else warned_here = pedwarn Index: gcc/c-family/c-opts.c =================================================================== --- gcc/c-family/c-opts.c (revision 259017) +++ gcc/c-family/c-opts.c (working copy) @@ -994,8 +994,9 @@ c_common_post_options (const char **pfilename) flag_extern_tls_init = 1; } - if (warn_return_type == -1) - warn_return_type = c_dialect_cxx (); + /* Enable by default only for C++ and C++ with ObjC extensions. */ + if (warn_return_type == -1 && c_dialect_cxx ()) + warn_return_type = 1; if (num_in_fnames > 1) error ("too many filenames given. Type %s --help for usage", Index: gcc/testsuite/gcc.dg/noncompile/pr55976-1.c =================================================================== --- gcc/testsuite/gcc.dg/noncompile/pr55976-1.c (revision 0) +++ gcc/testsuite/gcc.dg/noncompile/pr55976-1.c (working copy) @@ -0,0 +1,14 @@ +/* PR c/55976 */ +/* { dg-do compile } */ +/* { dg-options "-Werror=return-type" } */ + +/* Verify warnings for return type become errors. */ + +void t () { return 1; } /* { dg-error "return" "function returning void" } */ +int b () { return; } /* { dg-error "return" "function returning non-void" } */ + +int main() +{ + t(); b(); + return 0; +} Index: gcc/testsuite/gcc.dg/noncompile/pr55976-2.c =================================================================== --- gcc/testsuite/gcc.dg/noncompile/pr55976-2.c (revision 0) +++ gcc/testsuite/gcc.dg/noncompile/pr55976-2.c (working copy) @@ -0,0 +1,16 @@ +/* PR c/55976 */ +/* { dg-do compile } */ +/* { dg-options "-Wno-return-type" } */ + +/* Verify that -Wno-return-type turns off warnings about function return + type. */ + +void t () { return 1; } /* normally generates function returning void */ +int b () { return; } /* normally generates function returning non-void */ + +int main() +{ + t (); b (); + return 0; +} +