From patchwork Sun Sep 25 07:46:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 674548 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 3shfLX0w53z9sR9 for ; Sun, 25 Sep 2016 17:46:42 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=s6j9rDoN; 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=CnfX+jsyW7TH/aqPOZfl/PXmSGUDOzCAyBP/LvIQfYANsaxnzsjFR mBEy2GmOgMcoZexPtGR9eJrKGMFO7fDlSPP/RZ0UAFKK+JQXclsjT26UUe2BjdYK lp6UGjDVr69R6lGbkOz+C+jQDUQFmeeMtksP2G70+qcPXS5N7p7YcA= 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=OuB+aBYRDvKgstDeK9wajhyGXc4=; b=s6j9rDoNeXJDJ3ZsXWMN nX21E3MZ4vaAf3I6wEAq6ENup5qHHTKQRvuDbg7P2TtnAfuA4d9gvmF4aNPUfvlz Ql8MY97YKuXtl81uqoTGM55cLpEkIno3dz3RfyiVaO9b+OR4gR65mqKJMwaf8kZe ZzSlkb9vY0uP4A+UhaDaReE= Received: (qmail 121010 invoked by alias); 25 Sep 2016 07:46:34 -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 120977 invoked by uid 89); 25 Sep 2016 07:46:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=no version=3.3.2 spammy=2016-09-25, shifts, integer_zerop, H*MI:eurprd07 X-HELO: BAY004-OMC1S21.hotmail.com Received: from bay004-omc1s21.hotmail.com (HELO BAY004-OMC1S21.hotmail.com) (65.54.190.32) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 25 Sep 2016 07:46:22 +0000 Received: from EUR03-VE1-obe.outbound.protection.outlook.com ([65.54.190.60]) by BAY004-OMC1S21.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Sun, 25 Sep 2016 00:46:21 -0700 Received: from DB5EUR03FT005.eop-EUR03.prod.protection.outlook.com (10.152.20.56) by DB5EUR03HT041.eop-EUR03.prod.protection.outlook.com (10.152.21.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.629.5; Sun, 25 Sep 2016 07:46:08 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com (10.152.20.51) by DB5EUR03FT005.mail.protection.outlook.com (10.152.20.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.629.5 via Frontend Transport; Sun, 25 Sep 2016 07:46:08 +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.0629.006; Sun, 25 Sep 2016 07:46:07 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , Jeff Law , Jason Merrill Subject: [PATCH] Make -Wint-in-bool-context warn on suspicious shift ops Date: Sun, 25 Sep 2016 07:46:07 +0000 Message-ID: authentication-results: spf=softfail (sender IP is 10.152.20.51) 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.20.51 as permitted sender) x-ms-exchange-messagesentrepresentingtype: 1 x-eopattributedmessage: 0 x-microsoft-exchange-diagnostics: 1; DB5EUR03HT041; 6:msN87Cv0b/7Y4Gk3kpGOWUYAvZxYsxjvC5R/MctsFoLQ+nwspP6uY0xhzMB5QGy1igMvSlV7Duq9sBRgcy2sInVaNS+mnl6ljgG6L7kmLpomeLubQYBHwKg26nuOphFYVIh1qS3zaG2isyG+/3ycZMuqVTxDHNykbmrlmseZxBFSmQN+Zv2YljHPKskonDBcTWxkNPqU1MPx5viFwekSbg77UlM5wobkPdO2fF9X5gLChbT5rXD9Hp0uYZwJt5gNe56QS3Cmgp03lV/6yT3W9XStO/P27u1U9Ovgp1buZts=; 5:Iqrm/e+FmO+iEpJiahoZQNiNe+JzrZNBcS7i6zGG+IquggtjoCAlVXET2CjqySZTOYDznOFIl95xOpHfTLO8AJpnOk/VOcO7vL+tyPVKPVFxusxckLSx/3VelFxfwAHJJff3+hXtDHYzW1XuimZOnw==; 24:xYC9EbAKsd/MEfvAYfzibq3F48FL4cmwop8WRj4j7D3wcs/jwp2wm8COhYxmxDafc4uqJwkTDdX6vA+YMS9GnZ+bRXqwl9vpKSdlKN551ZI=; 7:ToDQTbMeutPBD4CCH8BId9m7EjTrtR2ITRKF3vU4Ujpz3DEkTC+LGbYXwf8Hc0AHCdVMSL9pIqaVLpDbAnfDKq5+txyQeUuPh5OlAstZQXJmpBaZHpbHSLrRJJAQqaTgZU4wrd54amdJg7zQ84zIhSZN9l4tGwA9EkA8QF4tP5ABVbKBmYY9AZcLDHDOns2u/nwPFoyGMTb2ohEMVmeYI4cLPbUS6gN1emGehB+1Fc6H37LR52MOMUvolBgfCqCsF4JucNFhlwiFvziGf8XHhktPI4k90p9dqdWPUVrIeUGBxPdFhGqPjJeO2Sn0KMF/ x-forefront-antispam-report: EFV:NLI; SFV:NSPM; SFS:(10019020)(98900003); DIR:OUT; SFP:1102; SCL:1; SRVR:DB5EUR03HT041; H:AM4PR0701MB2162.eurprd07.prod.outlook.com; FPR:; SPF:None; LANG:en; x-ms-office365-filtering-correlation-id: 797018d2-a46c-4710-316e-08d3e5180210 x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(1601124038)(1603103081)(1601125047); SRVR:DB5EUR03HT041; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(432015012)(102415321)(82015046); SRVR:DB5EUR03HT041; BCL:0; PCL:0; RULEID:; SRVR:DB5EUR03HT041; x-forefront-prvs: 0076F48C8A spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Sep 2016 07:46:07.6111 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB5EUR03HT041 Hi! This patch makes -Wint-in-bool-context warn on suspicious integer left shifts, when the integer is signed, which is most likely some kind of programming error, for instance using "<<" instead of "<". The warning is motivated by the fact, that an overflow on integer shift left is undefined behavior, even if gcc won't optimize the shift based on the undefined behavior. So in absence of undefined behavior the boolean result does not depend on the shift value, thus the whole shifting is pointless. Of course the warning happened to find one bug already. That is in cp/parser.c at cp_parser_condition, where we have this: bool flags = LOOKUP_ONLYCONVERTING; BUT (cp-tree.h): #define LOOKUP_ONLYCONVERTING (1 << 2) So "flags" is actually set to true, which is LOOKUP_PROTECT instead. Although I tried hard to find a test case where this changes something, I was not able to construct one. Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. gcc: 2016-09-25 Bernd Edlinger * doc/invoke.texi: Update -Wint-in-bool-context. c-family: 2016-09-25 Bernd Edlinger * c-common.c (c_common_truthvalue_conversion): Warn for suspicious signed integer left shift in boolean context. cp: 2016-09-25 Bernd Edlinger * parser.c (cp_parser_condition): Fix a warning. testsuite: 2016-09-25 Bernd Edlinger * c-c++-common/Wint-in-bool-context.c: Update test. Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 240437) +++ gcc/c-family/c-common.c (working copy) @@ -4651,6 +4651,19 @@ c_common_truthvalue_conversion (location_t locatio return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); + case LSHIFT_EXPR: + /* Warn on signed integer left shift, except 0 << 0, 1 << 0. */ + if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE + && !TYPE_UNSIGNED (TREE_TYPE (expr)) + && !(TREE_CODE (TREE_OPERAND (expr, 0)) == INTEGER_CST + && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST + && (integer_zerop (TREE_OPERAND (expr, 0)) + || integer_onep (TREE_OPERAND (expr, 0))) + && integer_zerop (TREE_OPERAND (expr, 1)))) + warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, + "<< on signed integer in boolean context"); + break; + case COND_EXPR: if (warn_int_in_bool_context && !from_macro_definition_at (EXPR_LOCATION (expr))) Index: gcc/cp/parser.c =================================================================== --- gcc/cp/parser.c (revision 240437) +++ gcc/cp/parser.c (working copy) @@ -11172,7 +11172,7 @@ cp_parser_condition (cp_parser* parser) { tree pushed_scope; bool non_constant_p; - bool flags = LOOKUP_ONLYCONVERTING; + int flags = LOOKUP_ONLYCONVERTING; /* Create the declaration. */ decl = start_decl (declarator, &type_specifiers, Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 240437) +++ gcc/doc/invoke.texi (working copy) @@ -5927,7 +5927,8 @@ of the C++ standard. @opindex Wno-int-in-bool-context Warn for suspicious use of integer values where boolean values are expected, such as conditional expressions (?:) using non-boolean integer constants in -boolean context, like @code{if (a <= b ? 2 : 3)}. +boolean context, like @code{if (a <= b ? 2 : 3)}. Or left shifting of a +signed integer in boolean context, like @code{for (a = 0; 1 << a; a++);}. This warning is enabled by @option{-Wall}. @item -Wno-int-to-pointer-cast Index: gcc/testsuite/c-c++-common/Wint-in-bool-context.c =================================================================== --- gcc/testsuite/c-c++-common/Wint-in-bool-context.c (revision 240437) +++ gcc/testsuite/c-c++-common/Wint-in-bool-context.c (working copy) @@ -25,5 +25,7 @@ int foo (int a, int b) if (b ? 1+1 : 1) /* { dg-warning "boolean context" } */ return 7; + for (a = 0; 1 << a; a++); /* { dg-warning "boolean context" } */ + return 0; }