Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent shift-overflow warnings in C++20 #52873

Closed
manxorist opened this issue Dec 25, 2021 · 0 comments
Closed

Inconsistent shift-overflow warnings in C++20 #52873

manxorist opened this issue Dec 25, 2021 · 0 comments
Assignees
Labels
c++20 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@manxorist
Copy link

manxorist commented Dec 25, 2021

Consider the following code:

int foo4s() {
    return (0x7000'0000) << 4;
}

unsigned int foo4u() {
    return (0x7000'0000u) << 4;
}

int foo1s() {
    return (0x7000'0000) << 1;
}

unsigned int foo1u() {
    return (0x7000'0000u) << 1;
}

which when compiled with -std=c++20 -Wall -Wextra -Wpedantic results in:

<source>:2:26: warning: signed shift result (0x700000000) requires 36 bits to represent, but 'int' only has 32 bits [-Wshift-overflow]
    return (0x7000'0000) << 4;
           ~~~~~~~~~~~~~ ^  ~
1 warning generated.

In C++20, the semantics are just as well defined for the signed case as they are for the unsigned case, thus generating a warning when shifting bits outside of a signed target type and not when shifting outside of a unsigned target type seems inconsistent to me.

GCC does not warn in this case in C++20 mode. See https://godbolt.org/z/9rbafz3h3.

This came up when discussing a similar (but not identical) issue in GCC. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826

@RKSimon RKSimon added c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 27, 2021
@mkurdej mkurdej removed the new issue label Dec 28, 2021
@Quuxplusone Quuxplusone added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 18, 2022
@xgupta xgupta self-assigned this Jun 8, 2022
@xgupta xgupta closed this as completed in 48e1829 Jun 14, 2022
mem-frob pushed a commit to draperlaboratory/hope-llvm-project that referenced this issue Oct 7, 2022
This fixes llvm/llvm-project#52873.
Don't warn in C++2A mode (and newer), as signed left shifts
always wrap and never overflow. Ref. -
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1236r1.html.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++20 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

5 participants