From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 3A8783838001; Tue, 10 May 2022 08:22:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A8783838001 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-10666] c-family: Fix up -W*conversion on bitwise &/|/^ [PR101537] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 9b80a9b0466f02e6e30790396c2b1545a9f8ff94 X-Git-Newrev: 4e8c32ecfb87e723229dc6c08df8ea602e947f1e Message-Id: <20220510082244.3A8783838001@sourceware.org> Date: Tue, 10 May 2022 08:22:44 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 May 2022 08:22:44 -0000 https://gcc.gnu.org/g:4e8c32ecfb87e723229dc6c08df8ea602e947f1e commit r10-10666-g4e8c32ecfb87e723229dc6c08df8ea602e947f1e Author: Jakub Jelinek Date: Tue Jan 11 19:11:51 2022 +0100 c-family: Fix up -W*conversion on bitwise &/|/^ [PR101537] The following testcases emit a bogus -Wconversion warning. This is because conversion_warning function doesn't handle BIT_*_EXPR (only unsafe_conversion_p that is called during the default: case, and that one doesn't handle SAVE_EXPRs added because the unsigned char & or | operands promoted to int have side-effects and =| or =& is used. The patch handles BIT_IOR_EXPR/BIT_XOR_EXPR like the last 2 operands of COND_EXPR by recursing on the two operands, if either of them doesn't fit into the narrower type, complain. BIT_AND_EXPR too, but first it needs to handle some special cases that unsafe_conversion_p does, namely when one of the two operands is a constant. This fixes completely the pr101537.c test and for C also pr103881.c and doesn't regress anything in the testsuite, for C++ pr103881.c still emits the bogus warnings. This is because while the C FE emits in that case a SAVE_EXPR that conversion_warning can handle already, C++ FE emits TARGET_EXPR , something | D.whatever etc. and conversion_warning handles COMPOUND_EXPR by "recursing" on the rhs. To handle that case, we'd need for TARGET_EXPR on the lhs remember in some hash map the mapping from D.whatever to the TARGET_EXPR and when we see D.whatever, use corresponding TARGET_EXPR initializer instead. 2022-01-11 Jakub Jelinek PR c/101537 PR c/103881 gcc/c-family/ * c-warn.c (conversion_warning): Handle BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR. gcc/testsuite/ * c-c++-common/pr101537.c: New test. * c-c++-common/pr103881.c: New test. (cherry picked from commit 20e4a5e573e76f4379b353cc736215a5f10cdb84) Diff: --- gcc/c-family/c-warn.c | 28 ++++++++++++++++++++++++++++ gcc/testsuite/c-c++-common/pr101537.c | 26 ++++++++++++++++++++++++++ gcc/testsuite/c-c++-common/pr103881.c | 20 ++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index 92b3b55bd07..a4baf37cf1c 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -1302,6 +1302,34 @@ conversion_warning (location_t loc, tree type, tree expr, tree result) || conversion_warning (loc, type, op2, result)); } + case BIT_AND_EXPR: + if (TREE_CODE (expr_type) == INTEGER_TYPE + && TREE_CODE (type) == INTEGER_TYPE) + for (int i = 0; i < 2; ++i) + { + tree op = TREE_OPERAND (expr, i); + if (TREE_CODE (op) != INTEGER_CST) + continue; + + /* If one of the operands is a non-negative constant + that fits in the target type, then the type of the + other operand does not matter. */ + if (int_fits_type_p (op, c_common_signed_type (type)) + && int_fits_type_p (op, c_common_unsigned_type (type))) + return false; + + /* If constant is unsigned and fits in the target + type, then the result will also fit. */ + if (TYPE_UNSIGNED (TREE_TYPE (op)) && int_fits_type_p (op, type)) + return false; + } + /* FALLTHRU */ + case BIT_IOR_EXPR: + case BIT_XOR_EXPR: + return (conversion_warning (loc, type, TREE_OPERAND (expr, 0), result) + || conversion_warning (loc, type, TREE_OPERAND (expr, 1), + result)); + default_: default: conversion_kind = unsafe_conversion_p (type, expr, result, true); diff --git a/gcc/testsuite/c-c++-common/pr101537.c b/gcc/testsuite/c-c++-common/pr101537.c new file mode 100644 index 00000000000..c0c3cfc45cb --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr101537.c @@ -0,0 +1,26 @@ +/* PR c/101537 */ +/* { dg-do compile } */ +/* { dg-options "-Wconversion" } */ + +int +foo () +{ + int aaa = 1; + unsigned char bbb = 0; + bbb |= aaa ? 1 : 0; + return bbb; +} + +int +bar (unsigned char x, int f) +{ + x |= f ? 1 : 0; + return x; +} + +int +baz (unsigned char x, int f) +{ + x = x | f ? 1 : 0; + return x; +} diff --git a/gcc/testsuite/c-c++-common/pr103881.c b/gcc/testsuite/c-c++-common/pr103881.c new file mode 100644 index 00000000000..bb3f53bcf7b --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr103881.c @@ -0,0 +1,20 @@ +/* PR c/103881 */ +/* { dg-do compile } */ +/* { dg-options "-Wconversion" } */ + +unsigned char bar (void); + +void +foo (void) +{ + unsigned char t = 0; + t |= bar (); + t |= bar () & bar (); /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */ + t &= bar () & bar (); /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */ + t = bar () & bar (); + + unsigned char a = bar (); + t |= a & a; + t |= bar () & a; /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */ + t |= a & bar (); /* { dg-bogus "conversion from 'int' to 'unsigned char' may change value" "" { xfail c++ } } */ +}