public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115626] New: relax -Wsign-conversion when initializing from a literal
@ 2024-06-25  3:13 michael.kenzel at gmail dot com
  2024-06-25  3:18 ` [Bug c++/115626] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: michael.kenzel at gmail dot com @ 2024-06-25  3:13 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115626
           Summary: relax -Wsign-conversion when initializing from a
                    literal
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: michael.kenzel at gmail dot com
  Target Milestone: ---

Initializing an unsigned integer like

        unsigned int mask = -1;

or

        unsigned int mask = ~0;

is common practice, guaranteed to produce the desired value, and arguably the
idiomatic way to initialize a bitmask to all bits set.

Alternatives like explicitly providing the unsigned value

        unsigned long mask = 0xFFFFFFFFFFFFFFFFUL;

are error prone, not generic, not portable as they cannot account for the
varying width of types across target platforms, and may not work reliably for
types for which no literal suffixes exist (e.g.: extended integer types).

Mixing signed and unsigned arithmetic is a prolific source of bugs. Thus,
enabling -Wsign-conversion is generally a good idea. However, doing so can lead
to copious amounts of false positives in code that is heavy on the use of
bitmasks. Quieting these warnings by means of explicit casts reduces
readability.

The likelihood that an unsigned integer being initialized from a literal -1 or
~0 constitutes a bug is small, while legitimate and perfectly safe uses of such
constructs are ubiquitous.

I would like to propose relaxing -Wsign-conversion to not warn upon
initialization of an unsigned integer from a literal -1 or ~0 expression, or
any unary - or ~ expression with literal operands and a signed value that does
not exceed the range of the corresponding signed type, i.e., has a
corresponding unsigned value with the same untruncated bit pattern. Maybe even
consider allowing any constant expression with such a value.

If changing the behavior of -Wsign-conversion is deemed not an option, maybe
introducing something like a -Wnonliteral-sign-conversion or
-Wnonconstant-sign-conversion option to explicitly opt into the warning only
for cases that cannot be classified as most-likely harmless at compile time
would be?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/115626] relax -Wsign-conversion when initializing from a literal
  2024-06-25  3:13 [Bug c++/115626] New: relax -Wsign-conversion when initializing from a literal michael.kenzel at gmail dot com
@ 2024-06-25  3:18 ` pinskia at gcc dot gnu.org
  2024-06-25  3:18 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-25  3:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
-1ul and ~0ul are portable by the way.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/115626] relax -Wsign-conversion when initializing from a literal
  2024-06-25  3:13 [Bug c++/115626] New: relax -Wsign-conversion when initializing from a literal michael.kenzel at gmail dot com
  2024-06-25  3:18 ` [Bug c++/115626] " pinskia at gcc dot gnu.org
@ 2024-06-25  3:18 ` pinskia at gcc dot gnu.org
  2024-06-25  3:20 ` pinskia at gcc dot gnu.org
  2024-06-25  3:24 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-25  3:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>may not work reliably for types for which no literal suffixes exist (e.g.: extended integer types)

You can always do `~(cast)0` too.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/115626] relax -Wsign-conversion when initializing from a literal
  2024-06-25  3:13 [Bug c++/115626] New: relax -Wsign-conversion when initializing from a literal michael.kenzel at gmail dot com
  2024-06-25  3:18 ` [Bug c++/115626] " pinskia at gcc dot gnu.org
  2024-06-25  3:18 ` pinskia at gcc dot gnu.org
@ 2024-06-25  3:20 ` pinskia at gcc dot gnu.org
  2024-06-25  3:24 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-25  3:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> You can always do `~(cast)0` too.

That is:
__uint128_t t = ~(__uint128_t)0;

does not warn.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/115626] relax -Wsign-conversion when initializing from a literal
  2024-06-25  3:13 [Bug c++/115626] New: relax -Wsign-conversion when initializing from a literal michael.kenzel at gmail dot com
                   ` (2 preceding siblings ...)
  2024-06-25  3:20 ` pinskia at gcc dot gnu.org
@ 2024-06-25  3:24 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-25  3:24 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
dup.

*** This bug has been marked as a duplicate of bug 92675 ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-06-25  3:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-25  3:13 [Bug c++/115626] New: relax -Wsign-conversion when initializing from a literal michael.kenzel at gmail dot com
2024-06-25  3:18 ` [Bug c++/115626] " pinskia at gcc dot gnu.org
2024-06-25  3:18 ` pinskia at gcc dot gnu.org
2024-06-25  3:20 ` pinskia at gcc dot gnu.org
2024-06-25  3:24 ` pinskia at gcc dot gnu.org

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).