public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
@ 2020-08-29 13:12 ` dcb314 at hotmail dot com
  2021-11-29  0:23 ` [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow] egallager at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: dcb314 at hotmail dot com @ 2020-08-29 13:12 UTC (permalink / raw)
  To: gcc-bugs

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

David Binderman <dcb314 at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dcb314 at hotmail dot com

--- Comment #22 from David Binderman <dcb314 at hotmail dot com> ---
clang only seems to warn for 2 ^ X and 10 ^ Y.

There seems to be about 30 cases of this problem across the Fedora Linux
distribution, so not the biggest problem in the world.

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

* [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
  2020-08-29 13:12 ` [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64 dcb314 at hotmail dot com
@ 2021-11-29  0:23 ` egallager at gcc dot gnu.org
  2022-08-10 16:36 ` [Bug c/90885] " dmalcolm at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: egallager at gcc dot gnu.org @ 2021-11-29  0:23 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|GCC should warn about 2^16  |GCC should warn about 2^16
                   |and 2^32 and 2^64           |and 2^32 and 2^64
                   |                            |[-Wxor-used-as-pow]

--- Comment #23 from Eric Gallager <egallager at gcc dot gnu.org> ---
putting -Wxor-used-as-pow in the title since that's what clang went with

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
  2020-08-29 13:12 ` [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64 dcb314 at hotmail dot com
  2021-11-29  0:23 ` [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow] egallager at gcc dot gnu.org
@ 2022-08-10 16:36 ` dmalcolm at gcc dot gnu.org
  2022-08-11 14:29 ` dmalcolm at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-08-10 16:36 UTC (permalink / raw)
  To: gcc-bugs

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

David Malcolm <dmalcolm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |dmalcolm at gcc dot gnu.org
          Component|c++                         |c
           Assignee|unassigned at gcc dot gnu.org      |dmalcolm at gcc dot gnu.org

--- Comment #24 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
I'm working on an implementation of this.

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2022-08-10 16:36 ` [Bug c/90885] " dmalcolm at gcc dot gnu.org
@ 2022-08-11 14:29 ` dmalcolm at gcc dot gnu.org
  2022-08-12  1:40 ` dmalcolm at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-08-11 14:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #25 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
Created attachment 53435
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53435&action=edit
v1 of a patch to implement -Wxor-used-as-pow

This patch implements the warning, but doesn't work well; as noted in the text
it's implemented in the parser, when I think it might have to be implemented in
the lexer.

Attaching it here for reference (and as a backup for my hard drive).

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2022-08-11 14:29 ` dmalcolm at gcc dot gnu.org
@ 2022-08-12  1:40 ` dmalcolm at gcc dot gnu.org
  2022-09-02 22:36 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-08-12  1:40 UTC (permalink / raw)
  To: gcc-bugs

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

David Malcolm <dmalcolm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
             Status|ASSIGNED                    |WAITING

--- Comment #26 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
I implemented a better version of the patch; I've posted it for review here:
  https://gcc.gnu.org/pipermail/gcc-patches/2022-August/599609.html

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
                   ` (4 preceding siblings ...)
  2022-08-12  1:40 ` dmalcolm at gcc dot gnu.org
@ 2022-09-02 22:36 ` cvs-commit at gcc dot gnu.org
  2022-09-02 22:43 ` dmalcolm at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-09-02 22:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #27 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by David Malcolm <dmalcolm@gcc.gnu.org>:

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

commit r13-2386-gbedfca647a9e9c1adadd8924f3ee0ab4189424e0
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Fri Sep 2 18:29:33 2022 -0400

    c/c++: new warning: -Wxor-used-as-pow [PR90885]

    PR c/90885 notes various places in real-world code where people have
    written C/C++ code that uses ^ (exclusive or) where presumbably they
    meant exponentiation.

    For example
      https://codesearch.isocpp.org/cgi-bin/cgi_ppsearch?q=2%5E32&search=Search
    currently finds 11 places using "2^32", and all of them appear to be
    places where the user means 2 to the power of 32, rather than 2
    exclusive-orred with 32 (which is 34).

    This patch adds a new -Wxor-used-as-pow warning to the C and C++
    frontends to complain about ^ when the left-hand side is the decimal
    constant 2 or the decimal constant 10.

    This is the same name as the corresponding clang warning:
      https://clang.llvm.org/docs/DiagnosticsReference.html#wxor-used-as-pow

    As per the clang warning, the warning suggests converting the left-hand
    side to a hexadecimal constant if you really mean xor, which suppresses
    the warning (though this patch implements a fix-it hint for that, whereas
    the clang implementation only has a fix-it hint for the initial
    suggestion of exponentiation).

    I initially tried implementing this without checking for decimals, but
    this version had lots of false positives.  Checking for decimals
    requires extending the lexer to capture whether or not a CPP_NUMBER
    token was decimal.  I added a new DECIMAL_INT flag to cpplib.h for this.
    Unfortunately, c_token and cp_tokens both have only an unsigned char for
    their flags (as captured by c_lex_with_flags), whereas this would add
    the 12th flag to cpp_tokens.  Of the first 8 flags, all but BOL are used
    in the C or C++ frontends, but BOL is not, so I moved that to a higher
    position, using its old value for the new DECIMAL_INT flag, so that it
    is representable within an unsigned char.

    Example output:

    demo.c:5:13: warning: result of '2^8' is 10; did you mean '1 << 8' (256)?
[-Wxor-used-as-pow]
        5 | int t2_8 = 2^8;
          |             ^
          |            --
          |            1<<
    demo.c:5:12: note: you can silence this warning by using a hexadecimal
constant (0x2 rather than 2)
        5 | int t2_8 = 2^8;
          |            ^
          |            0x2
    demo.c:21:15: warning: result of '10^6' is 12; did you mean '1e6'?
[-Wxor-used-as-pow]
       21 | int t10_6 = 10^6;
          |               ^
          |             ---
          |             1e
    demo.c:21:13: note: you can silence this warning by using a hexadecimal
constant (0xa rather than 10)
       21 | int t10_6 = 10^6;
          |             ^~
          |             0xa

    gcc/c-family/ChangeLog:
            PR c/90885
            * c-common.h (check_for_xor_used_as_pow): New decl.
            * c-lex.cc (c_lex_with_flags): Add DECIMAL_INT to flags as
appropriate.
            * c-warn.cc (check_for_xor_used_as_pow): New.
            * c.opt (Wxor-used-as-pow): New.

    gcc/c/ChangeLog:
            PR c/90885
            * c-parser.cc (c_parser_string_literal): Clear ret.m_decimal.
            (c_parser_expr_no_commas): Likewise.
            (c_parser_conditional_expression): Likewise.
            (c_parser_binary_expression): Clear m_decimal when popping the
            stack.
            (c_parser_unary_expression): Clear ret.m_decimal.
            (c_parser_has_attribute_expression): Likewise for result.
            (c_parser_predefined_identifier): Likewise for expr.
            (c_parser_postfix_expression): Likewise for expr.
            Set expr.m_decimal when handling a CPP_NUMBER that was a decimal
            token.
            * c-tree.h (c_expr::m_decimal): New bitfield.
            * c-typeck.cc (parser_build_binary_op): Clear result.m_decimal.
            (parser_build_binary_op): Call check_for_xor_used_as_pow.

    gcc/cp/ChangeLog:
            PR c/90885
            * cp-tree.h (class cp_expr): Add bitfield m_decimal.  Clear it in
            existing ctors.  Add ctor that allows specifying its value.
            (cp_expr::decimal_p): New accessor.
            * parser.cc (cp_parser_expression_stack_entry::flags): New field.
            (cp_parser_primary_expression): Set m_decimal of cp_expr when
            handling numbers.
            (cp_parser_binary_expression): Extract flags from token when
            populating stack.  Call check_for_xor_used_as_pow.

    gcc/ChangeLog:
            PR c/90885
            * doc/invoke.texi (Warning Options): Add -Wxor-used-as-pow.

    gcc/testsuite/ChangeLog:
            PR c/90885
            * c-c++-common/Wxor-used-as-pow-1.c: New test.
            * c-c++-common/Wxor-used-as-pow-fixits.c: New test.
            * g++.dg/parse/expr3.C: Convert 2 to 0x2 to suppress
            -Wxor-used-as-pow.
            * g++.dg/warn/Wparentheses-10.C: Likewise.
            * g++.dg/warn/Wparentheses-18.C: Likewise.
            * g++.dg/warn/Wparentheses-19.C: Likewise.
            * g++.dg/warn/Wparentheses-9.C: Likewise.
            * g++.dg/warn/Wxor-used-as-pow-named-op.C: New test.
            * gcc.dg/Wparentheses-6.c: Convert 2 to 0x2 to suppress
            -Wxor-used-as-pow.
            * gcc.dg/Wparentheses-7.c: Likewise.
            * gcc.dg/precedence-1.c: Likewise.

    libcpp/ChangeLog:
            PR c/90885
            * include/cpplib.h (BOL): Move macro to 1 << 12 since it is
            not used by C/C++'s unsigned char token flags.
            (DECIMAL_INT): New, using 1 << 6, so that it is visible as
            part of C/C++'s 8 bits of token flags.

    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
                   ` (5 preceding siblings ...)
  2022-09-02 22:36 ` cvs-commit at gcc dot gnu.org
@ 2022-09-02 22:43 ` dmalcolm at gcc dot gnu.org
  2022-09-03  2:01 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-09-02 22:43 UTC (permalink / raw)
  To: gcc-bugs

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

David Malcolm <dmalcolm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|WAITING                     |RESOLVED

--- Comment #28 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
Implemented for GCC 13 by the above patch; marking as resolved.

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
                   ` (6 preceding siblings ...)
  2022-09-02 22:43 ` dmalcolm at gcc dot gnu.org
@ 2022-09-03  2:01 ` redi at gcc dot gnu.org
  2022-11-28 22:43 ` pinskia at gcc dot gnu.org
  2023-04-27 15:39 ` warp at iki dot fi
  9 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-09-03  2:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #29 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Excellent! Thanks, Dave

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
                   ` (7 preceding siblings ...)
  2022-09-03  2:01 ` redi at gcc dot gnu.org
@ 2022-11-28 22:43 ` pinskia at gcc dot gnu.org
  2023-04-27 15:39 ` warp at iki dot fi
  9 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-28 22:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0

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

* [Bug c/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow]
       [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
                   ` (8 preceding siblings ...)
  2022-11-28 22:43 ` pinskia at gcc dot gnu.org
@ 2023-04-27 15:39 ` warp at iki dot fi
  9 siblings, 0 replies; 10+ messages in thread
From: warp at iki dot fi @ 2023-04-27 15:39 UTC (permalink / raw)
  To: gcc-bugs

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

warp at iki dot fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |warp at iki dot fi

--- Comment #30 from warp at iki dot fi ---
Note that a few of the examples in that first tweet are actually misleading.
More particularly, the example in png.c. At first glance it looks like C and an
example of this mistake, but if you look at the context it becomes clear that
it actually isn't C at all (because that line appears in a context where it
would be illegal code, namely, inside the initialization list of an array).
It's actually BC code embedded in the C source code, for some reason. In BC
2^32 is legitimately 2 to the power of 32.

The other examples are probably legitimate, though.

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

end of thread, other threads:[~2023-04-27 15:39 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-90885-4@http.gcc.gnu.org/bugzilla/>
2020-08-29 13:12 ` [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64 dcb314 at hotmail dot com
2021-11-29  0:23 ` [Bug c++/90885] GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow] egallager at gcc dot gnu.org
2022-08-10 16:36 ` [Bug c/90885] " dmalcolm at gcc dot gnu.org
2022-08-11 14:29 ` dmalcolm at gcc dot gnu.org
2022-08-12  1:40 ` dmalcolm at gcc dot gnu.org
2022-09-02 22:36 ` cvs-commit at gcc dot gnu.org
2022-09-02 22:43 ` dmalcolm at gcc dot gnu.org
2022-09-03  2:01 ` redi at gcc dot gnu.org
2022-11-28 22:43 ` pinskia at gcc dot gnu.org
2023-04-27 15:39 ` warp at iki dot fi

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