public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/104711] Unnecessary -Wshift-negative-value warning
Date: Tue, 29 Mar 2022 05:53:59 +0000	[thread overview]
Message-ID: <bug-104711-4-AVHYNHOkDV@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104711-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104711

--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:ddc0d2593fb4d2eb432e24018d36dd3f337a8138

commit r11-9726-gddc0d2593fb4d2eb432e24018d36dd3f337a8138
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Mar 9 09:15:28 2022 +0100

    c, c++, c-family: -Wshift-negative-value and -Wshift-overflow* tweaks for
-fwrapv and C++20+ [PR104711]

    As mentioned in the PR, different standards have different definition
    on what is an UB left shift.  They all agree on out of bounds (including
    negative) shift count.
    The rules used by ubsan are:
    C99-C2x ((unsigned) x >> (uprecm1 - y)) != 0 then UB
    C++11-C++17 x < 0 || ((unsigned) x >> (uprecm1 - y)) > 1 then UB
    C++20 and later everything is well defined
    Now, for C++20, I've in the P1236R1 implementation added an early
    exit for -Wshift-overflow* warning so that it never warns, but apparently
    -Wshift-negative-value remained as is.  As it is well defined in C++20,
    the following patch doesn't enable -Wshift-negative-value from -Wextra
    anymore for C++20 and later, if users want for compatibility with C++17
    and earlier get the warning, they still can by using -Wshift-negative-value
    explicitly.
    Another thing is -fwrapv, that is an extension to the standards, so it is
up
    to us how exactly we define that case.  Our ubsan code treats
    TYPE_OVERFLOW_WRAPS (type0) and cxx_dialect >= cxx20 the same as only
    diagnosing out of bounds shift count and nothing else and IMHO it is most
    sensical to treat -fwrapv signed left shifts the same as C++20 treats
    them, https://eel.is/c++draft/expr.shift#2
    "The value of E1 << E2 is the unique value congruent to E1×2^E2 modulo 2^N,
    where N is the width of the type of the result.
    [Note 1: E1 is left-shifted E2 bit positions; vacated bits are zero-filled.
    â end note]"
    with no UB dependent on the E1 values.  The UB is only
    "The behavior is undefined if the right operand is negative, or greater
    than or equal to the width of the promoted left operand."
    Under the hood (except for FEs and ubsan from FEs) GCC middle-end doesn't
    consider UB in left shifts dependent on the first operand's value, only
    the out of bounds shifts.

    While this change isn't a regression, I'd think it is useful for GCC 12,
    it doesn't add new warnings, but just removes warnings that aren't
    appropriate.

    2022-03-09  Jakub Jelinek  <jakub@redhat.com>

            PR c/104711
    gcc/
            * doc/invoke.texi (-Wextra): Document that -Wshift-negative-value
            is enabled by it only for C++11 to C++17 rather than for C++03 or
            later.
            (-Wshift-negative-value): Similarly (except here we stated
            that it is enabled for C++11 or later).
    gcc/c-family/
            * c-opts.c (c_common_post_options): Don't enable
            -Wshift-negative-value from -Wextra for C++20 or later.
            * c-ubsan.c (ubsan_instrument_shift): Adjust comments.
            * c-warn.c (maybe_warn_shift_overflow): Use TYPE_OVERFLOW_WRAPS
            instead of TYPE_UNSIGNED.
    gcc/c/
            * c-fold.c (c_fully_fold_internal): Don't emit
            -Wshift-negative-value warning if TYPE_OVERFLOW_WRAPS.
            * c-typeck.c (build_binary_op): Likewise.
    gcc/cp/
            * constexpr.c (cxx_eval_check_shift_p): Use TYPE_OVERFLOW_WRAPS
            instead of TYPE_UNSIGNED.
            * typeck.c (cp_build_binary_op): Don't emit
            -Wshift-negative-value warning if TYPE_OVERFLOW_WRAPS.
    gcc/testsuite/
            * c-c++-common/Wshift-negative-value-1.c: Remove
            dg-additional-options, instead in target selectors of each
diagnostic
            check for exact C++ versions where it should be diagnosed.
            * c-c++-common/Wshift-negative-value-2.c: Likewise.
            * c-c++-common/Wshift-negative-value-3.c: Likewise.
            * c-c++-common/Wshift-negative-value-4.c: Likewise.
            * c-c++-common/Wshift-negative-value-7.c: New test.
            * c-c++-common/Wshift-negative-value-8.c: New test.
            * c-c++-common/Wshift-negative-value-9.c: New test.
            * c-c++-common/Wshift-negative-value-10.c: New test.
            * c-c++-common/Wshift-overflow-1.c: Remove
            dg-additional-options, instead in target selectors of each
diagnostic
            check for exact C++ versions where it should be diagnosed.
            * c-c++-common/Wshift-overflow-2.c: Likewise.
            * c-c++-common/Wshift-overflow-5.c: Likewise.
            * c-c++-common/Wshift-overflow-6.c: Likewise.
            * c-c++-common/Wshift-overflow-7.c: Likewise.
            * c-c++-common/Wshift-overflow-8.c: New test.
            * c-c++-common/Wshift-overflow-9.c: New test.
            * c-c++-common/Wshift-overflow-10.c: New test.
            * c-c++-common/Wshift-overflow-11.c: New test.
            * c-c++-common/Wshift-overflow-12.c: New test.

    (cherry picked from commit d76511138dc816ef66fd16f71531f48c37dac3b4)

  parent reply	other threads:[~2022-03-29  5:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-27 21:27 [Bug c/104711] New: " arnd at linaro dot org
2022-02-27 21:40 ` [Bug c/104711] " pinskia at gcc dot gnu.org
2022-02-27 22:37 ` segher at gcc dot gnu.org
2022-02-27 22:38 ` segher at gcc dot gnu.org
2022-02-27 22:42 ` segher at gcc dot gnu.org
2022-03-01  7:58 ` rguenth at gcc dot gnu.org
2022-03-01  9:07 ` jakub at gcc dot gnu.org
2022-03-01 14:11 ` jakub at gcc dot gnu.org
2022-03-01 17:51 ` segher at gcc dot gnu.org
2022-03-09  8:16 ` cvs-commit at gcc dot gnu.org
2022-03-29  5:53 ` cvs-commit at gcc dot gnu.org [this message]
2022-05-10  8:25 ` cvs-commit at gcc dot gnu.org
2022-05-11  6:25 ` cvs-commit at gcc dot gnu.org
2022-05-11  6:36 ` jakub at gcc dot gnu.org

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=bug-104711-4-AVHYNHOkDV@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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: link
Be 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).