public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-6486] c-family: Fix up -W*conversion on bitwise &/|/^ [PR101537] Date: Tue, 11 Jan 2022 18:15:11 +0000 (GMT) [thread overview] Message-ID: <20220111181511.1A922385840C@sourceware.org> (raw) https://gcc.gnu.org/g:20e4a5e573e76f4379b353cc736215a5f10cdb84 commit r12-6486-g20e4a5e573e76f4379b353cc736215a5f10cdb84 Author: Jakub Jelinek <jakub@redhat.com> 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 <D.whatever, ...>, 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 <jakub@redhat.com> 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. 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 005c9acc65b..1ce2202c7ff 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -1304,6 +1304,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++ } } */ +}
reply other threads:[~2022-01-11 18:15 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220111181511.1A922385840C@sourceware.org \ --to=jakub@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).