From patchwork Fri Sep 2 18:53:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 665293 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 3sQpDX64bVz9s9Y for ; Sat, 3 Sep 2016 04:53:30 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=q79JP0CD; 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:from :to:subject:date:message-id:content-type:mime-version; q=dns; s= default; b=rQ+LQptOpangDZdcWThuQrkruCNpd2D1gpOgOTL/SjJ8BDBSpAOZH 22xu0cP+qnxdRepIclSHNgq5BKFm8aQQzcganB7LdiwDZ1uG6HbQnJsofZtWyAAx Hqs2aNXkXWxtdZB/d5/vw3YsLIuhyKnECO5qYUh4Tdvr08p8hD/SHQ= 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:from :to:subject:date:message-id:content-type:mime-version; s= default; bh=wQztKTRPYbxi+KvifM1yHkzBL3c=; b=q79JP0CD2lSxSlUxH65H zfLwoH6k+5+efSWjs7osT/2Pji/BL+gnHtjXAge1bMOMIR3ywDTraV+ybUyxYxHG q3aZOessbjBWNxZ+D8M96+d6SPbeGnMsDe5YUbpgYMDQht35ShEtZIJfanT9sO3l bHPnZ9C9Dr5bZBnoKCJcKYs= Received: (qmail 1525 invoked by alias); 2 Sep 2016 18:53:22 -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 1501 invoked by uid 89); 2 Sep 2016 18:53:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.1 required=5.0 tests=AWL, BAYES_40, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 spammy=sk:bernde, D*hotmail.de, sk:bernd.e, U*bernd.edlinger X-HELO: BAY004-OMC4S15.hotmail.com Received: from bay004-omc4s15.hotmail.com (HELO BAY004-OMC4S15.hotmail.com) (65.54.190.217) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 02 Sep 2016 18:53:10 +0000 Received: from EUR01-VE1-obe.outbound.protection.outlook.com ([65.54.190.199]) by BAY004-OMC4S15.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Fri, 2 Sep 2016 11:53:09 -0700 Received: from VE1EUR01FT060.eop-EUR01.prod.protection.outlook.com (10.152.2.53) by VE1EUR01HT195.eop-EUR01.prod.protection.outlook.com (10.152.3.153) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.587.6; Fri, 2 Sep 2016 18:53:05 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com (10.152.2.58) by VE1EUR01FT060.mail.protection.outlook.com (10.152.3.93) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.587.6 via Frontend Transport; Fri, 2 Sep 2016 18:53:05 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com ([10.167.132.147]) by AM4PR0701MB2162.eurprd07.prod.outlook.com ([10.167.132.147]) with mapi id 15.01.0599.010; Fri, 2 Sep 2016 18:53:05 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , Joseph Myers , Jason Merrill Subject: [PATCH] Add a warning for suspicious use of conditional expressions in boolean context Date: Fri, 2 Sep 2016 18:53:05 +0000 Message-ID: authentication-results: spf=softfail (sender IP is 10.152.2.58) smtp.mailfrom=hotmail.de; gcc.gnu.org; dkim=none (message not signed) header.d=none; gcc.gnu.org; dmarc=none action=none header.from=hotmail.de; received-spf: SoftFail (protection.outlook.com: domain of transitioning hotmail.de discourages use of 10.152.2.58 as permitted sender) x-ms-exchange-messagesentrepresentingtype: 1 x-eopattributedmessage: 0 x-forefront-antispam-report: CIP:10.152.2.58; IPV:NLI; CTRY:; EFV:NLI; SFV:NSPM; SFS:(10019020)(98900003); DIR:OUT; SFP:1102; SCL:1; SRVR:VE1EUR01HT195; H:AM4PR0701MB2162.eurprd07.prod.outlook.com; FPR:; SPF:None; LANG:en; x-microsoft-exchange-diagnostics: 1; VE1EUR01HT195; 6:PClqe/Oi89VsBaVOA7x3ZjijcE+FOjoqP2FFINusc+455QWO5tKkid4wufqiFJM3dJ9hM7tuguTIFpK4dzuUL6nzCb14Qw0DICF1ITZQZUN4WyqbFwsveq0RtZBakXr2KqXxOBJlOJiyUmOyMwEagBVTPXjV5TWwD1jQJ93KlpFo8SqF9xyYdE5YT1HGF8v51sykI3CponATBYl7hzOpKaA4cMEd96qXroqic6HC6OXuybbOjsaA1YezPvpvnTcIaSe4JnP2fGMxnVxfu5LtRREJEWtnYZzkXGlD99MT3Pp4aBlQPL2Y9YlhuoBIwlGu; 5:640BEu84Sh2uEr71B3NKGo0iEY/qnwIdIl+Ux5uoObM0ycRid9r3+nYvDMgp4c9OQPQuzkkjH0SOL3y6xS7NIBt5GvHL/YDyqDwUoI/Whke94zDKALzztp+Oa60IZOMVNX2enLGuo+z0jd655g/m2A==; 24:v63CuMJhMvpZEQUydUI3Dmjye+6vlrKayBwPyYHgZ8sLu8R8SY3mjacXwL9+NAAwGkhXQl7gJPhwil8rEXDxFIOJgIy2FNdi5+FEm5sjOUA=; 7:/vXgn9gBp45Kv0Ery6EEcfms+l3uZnJec6GyNLXx5OmXyppIXxWmIqOsGm3MUWdNcpTKXWgSAC6J0lw0esSKVC9o4qTqPDJMSNZ8SPipovDblmsDDnck7lS/pDJlCYVBVUHBg5EIKJFNczPoPtcF4nO0xL18QvLLlIUE1js9QOh9QEiRSfrYfk6Nn6LiHa8djdGPCrcftZAuLO4Zcyk3W6WXqRT0meYdg55j4aRXJQR/eiDoERliACHkCZAJyosh x-ms-office365-filtering-correlation-id: 7bd87a9a-4815-4cf3-0a27-08d3d3625ee1 x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(1601124038)(1603103081)(1601125047); SRVR:VE1EUR01HT195; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(432015012)(102415321)(82015046); SRVR:VE1EUR01HT195; BCL:0; PCL:0; RULEID:; SRVR:VE1EUR01HT195; x-forefront-prvs: 00531FAC2C spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Sep 2016 18:53:05.1682 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1EUR01HT195 Hi! As reported in PR77434 and PR77421 there should be a warning for suspicious uses of conditional expressions with non-boolean arguments. This warning triggers on conditional expressions in boolean context, when both possible results are non-zero integer constants, so that the resulting truth value does in fact not depend on the condition itself. Thus something like "if (a == b ? 1 : 2)" is always bogus, and was most likely meant to be "if (a == (b ? 1 : 2))". Boot-strap and reg-testing on x86_64-pc-linux-gnu without regressions. Is it OK for trunk. Thanks Bernd. gcc: 2016-09-02 Bernd Edlinger PR c++/77434 * doc/invoke.texi: Document -Wcond-in-bool-context. PR middle-end/77421 * dwarf2out.c (output_loc_operands): Fix assertion. c-family: 2016-09-02 Bernd Edlinger PR c++/77434 * c.opt (Wcond-in-bool-context): New warning. * c-common.c (c_common_truthvalue_conversion): Warn on integer constants in boolean context. testsuite: 2016-09-02 Bernd Edlinger PR c++/77434 * c-c++-common/Wcond-in-bool-context.c: New test. Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 239953) +++ gcc/c-family/c-common.c (working copy) @@ -4618,6 +4618,14 @@ c_common_truthvalue_conversion (location_t locatio TREE_OPERAND (expr, 0)); case COND_EXPR: + if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST + && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST + && !integer_zerop (TREE_OPERAND (expr, 1)) + && !integer_zerop (TREE_OPERAND (expr, 2)) + && (!integer_onep (TREE_OPERAND (expr, 1)) + || !integer_onep (TREE_OPERAND (expr, 2)))) + warning_at (EXPR_LOCATION (expr), OPT_Wcond_in_bool_context, + "?: using integer constants in boolean context"); /* Distribute the conversion into the arms of a COND_EXPR. */ if (c_dialect_cxx ()) { Index: gcc/c-family/c.opt =================================================================== --- gcc/c-family/c.opt (revision 239953) +++ gcc/c-family/c.opt (working copy) @@ -350,6 +350,10 @@ Wcomments C ObjC C++ ObjC++ Warning Alias(Wcomment) Synonym for -Wcomment. +Wcond-in-bool-context +C ObjC C++ ObjC++ Var(warn_cond_in_bool_context) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) +Warn for conditional expressions (?:) using integer constants in boolean context. + Wconditionally-supported C++ ObjC++ Var(warn_conditionally_supported) Warning Warn for conditionally-supported constructs. Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 239953) +++ gcc/doc/invoke.texi (working copy) @@ -259,7 +259,8 @@ Objective-C and Objective-C++ Dialects}. -Wno-attributes -Wbool-compare -Wno-builtin-macro-redefined @gol -Wc90-c99-compat -Wc99-c11-compat @gol -Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol --Wchar-subscripts -Wclobbered -Wcomment -Wconditionally-supported @gol +-Wchar-subscripts -Wclobbered -Wcomment @gol +-Wcond-in-bool-context -Wconditionally-supported @gol -Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol -Wdelete-incomplete @gol -Wno-deprecated -Wno-deprecated-declarations -Wno-designated-init @gol @@ -5179,6 +5180,13 @@ programs. Warn for variables that might be changed by @code{longjmp} or @code{vfork}. This warning is also enabled by @option{-Wextra}. +@item -Wcond-in-bool-context +@opindex Wcond-in-bool-context +@opindex Wno-cond-in-bool-context +Warn for conditional expressions (?:) using non-boolean integer constants in +boolean context, like @code{if (a <= b ? 2 : 3)}. This warning is enabled +by @option{-Wall}. + @item -Wconditionally-supported @r{(C++ and Objective-C++ only)} @opindex Wconditionally-supported @opindex Wno-conditionally-supported Index: gcc/dwarf2out.c =================================================================== --- gcc/dwarf2out.c (revision 239953) +++ gcc/dwarf2out.c (working copy) @@ -2051,9 +2051,9 @@ output_loc_operands (dw_loc_descr_ref loc, int for /* Make sure the offset has been computed and that we can encode it as an operand. */ gcc_assert (die_offset > 0 - && die_offset <= (loc->dw_loc_opc == DW_OP_call2) + && die_offset <= (loc->dw_loc_opc == DW_OP_call2 ? 0xffff - : 0xffffffff); + : 0xffffffff)); dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4, die_offset, NULL); } Index: gcc/testsuite/c-c++-common/Wcond-in-bool-context.c =================================================================== --- gcc/testsuite/c-c++-common/Wcond-in-bool-context.c (revision 0) +++ gcc/testsuite/c-c++-common/Wcond-in-bool-context.c (working copy) @@ -0,0 +1,17 @@ +/* PR c++/77434 */ +/* { dg-options "-Wcond-in-bool-context" } */ +/* { dg-do compile } */ + +int foo (int a, int b) +{ + if (a > 0 && a <= (b == 1) ? 1 : 2) /* { dg-warning "using integer constants in boolean context" } */ + return 1; + + if (a > 0 && a <= (b == 2) ? 1 : 1) /* { dg-bogus "using integer constants in boolean context" } */ + return 2; + + if (a > 0 && a <= (b == 3) ? 0 : 2) /* { dg-bogus "using integer constants in boolean context" } */ + return 3; + + return 0; +}