From patchwork Wed Nov 18 16:03:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 546091 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 7100314141D for ; Thu, 19 Nov 2015 03:04:11 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=mKv7MigZ; 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:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=HPYFXNsxuJCEs+5KWWk0XMlvg+88cYSy9SXOb41kfS582wZ1gT 8zWU/SSFJ7x3RyYN97a/btish+HzJ6624TJa2e6pT9h+Yi1a+f88AHAV7SORDWxz J4a/Ru2gmdt1D32NieeMblE1J2pwLVo6L7B5nfBnlVvoLCkf0naWy1EBM= 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:to:cc:subject:message-id:mime-version:content-type; s= default; bh=orJpsrcwUSyRVnVBHlTQRoPs1T4=; b=mKv7MigZ5xbG4tusHkkl t1tLouf51vxrpMNol/+/c4H3BP+6HQzBXfJZtKTh5ErcoFhUXOJjXjlmESM74sTV kOKRTvwpaOaRnfE9TpYxWkvEeubyj6hzObcqQ2LU/Og8J3taCoJ0B8R+qBa8wADd 0BrKw/Sam2g5PSng2dx8YmA= Received: (qmail 87476 invoked by alias); 18 Nov 2015 16:04:04 -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 87449 invoked by uid 89); 18 Nov 2015 16:04:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 18 Nov 2015 16:03:56 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id C38348E370; Wed, 18 Nov 2015 16:03:54 +0000 (UTC) Received: from redhat.com (ovpn-204-39.brq.redhat.com [10.40.204.39]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tAIG3mlK012215 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 18 Nov 2015 11:03:52 -0500 Date: Wed, 18 Nov 2015 17:03:48 +0100 From: Marek Polacek To: GCC Patches , Joseph Myers Cc: Jason Merrill Subject: [C PATCH] Don't leak C_MAYBE_CONST_EXPRs into fold() (PR c/68412) Message-ID: <20151118160348.GC21807@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Since C++ delayed folding, warn_tautological_cmp needs to fold its arguments. But sometimes this function gets C_MAYBE_CONST_EXPR from the C FE, and fold() duly ICEs. I was torn if I should just return from warn_tautological_cmp and not warn when it gets C_MAYBE_CONST_EXPR as an argument, or if I should strip C_MAYBE_CONST_EXPR before calling warn_tautological_cmp. I decided for the latter since I think warn_tautological_cmp probably should warn on code such as int i = 10; bool b = ({ i; }) == ({ i; }); (ugh). Bootstrapped/regtested on x86_64-linux, ok for trunk? 2015-11-18 Marek Polacek PR c/68412 * c-typeck.c (parser_build_binary_op): Strip C_MAYBE_CONST_EXPR when passing arguments to warn_tautological_compare. * gcc.dg/pr68412.c: New test. * gcc.dg/pr68412-2.c: New test. Marek diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c index c18c307..48c1a98 100644 --- gcc/c/c-typeck.c +++ gcc/c/c-typeck.c @@ -3512,7 +3512,11 @@ parser_build_binary_op (location_t location, enum tree_code code, code1, arg1.value, code2, arg2.value); if (warn_tautological_compare) - warn_tautological_cmp (location, code, arg1.value, arg2.value); + warn_tautological_cmp (location, code, + /* This function will try to fold both + args, so don't leak C_MAYBE_CONST_EXPR. */ + remove_c_maybe_const_expr (arg1.value), + remove_c_maybe_const_expr (arg2.value)); if (warn_logical_not_paren && TREE_CODE_CLASS (code) == tcc_comparison diff --git gcc/testsuite/gcc.dg/pr68412-2.c gcc/testsuite/gcc.dg/pr68412-2.c index e69de29..be1dcfa 100644 --- gcc/testsuite/gcc.dg/pr68412-2.c +++ gcc/testsuite/gcc.dg/pr68412-2.c @@ -0,0 +1,15 @@ +/* PR c/68412 */ +/* { dg-do compile } */ +/* { dg-options "-Wall -Wextra" } */ + +int +fn1 (int i) +{ + return ({ i; }) == ({ i; }); /* { dg-warning "self-comparison always evaluates to true" } */ +} + +int +fn2 (int i) +{ + return ({ i + 1; }) != ({ i + 1; }); /* { dg-warning "self-comparison always evaluates to false" } */ +} diff --git gcc/testsuite/gcc.dg/pr68412.c gcc/testsuite/gcc.dg/pr68412.c index e69de29..825eb63 100644 --- gcc/testsuite/gcc.dg/pr68412.c +++ gcc/testsuite/gcc.dg/pr68412.c @@ -0,0 +1,41 @@ +/* PR c/68412 */ +/* { dg-do compile } */ +/* { dg-options "-Wall -Wextra" } */ + +#define M (sizeof (int) * __CHAR_BIT__) + +int +fn1 (int i) +{ + return i == (-1 << 8); /* { dg-warning "left shift of negative value" } */ +} + +int +fn2 (int i) +{ + return i == (1 << M); /* { dg-warning "left shift count" } */ +} + +int +fn3 (int i) +{ + return i == 10 << (M - 1); /* { dg-warning "requires" } */ +} + +int +fn4 (int i) +{ + return i == 1 << -1; /* { dg-warning "left shift count" } */ +} + +int +fn5 (int i) +{ + return i == 1 >> M; /* { dg-warning "right shift count" } */ +} + +int +fn6 (int i) +{ + return i == 1 >> -1; /* { dg-warning "right shift count" } */ +}