Don't emit -Wxor-used-as-pow on macro expansions [PR107002]
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 28 Mar 2023 18:34:49 +0000 (14:34 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 28 Mar 2023 18:34:49 +0000 (14:34 -0400)
commit22c3a6c3c118283dfef1b9928dd21110098679b7
tree4e0c0bb61d608e1603f1b6fabf4f2460a718a0a2
parentaf45b17d0a8fe3e7ae08662008a1f41e48a4a3eb
Don't emit -Wxor-used-as-pow on macro expansions [PR107002]

PR c/107002 reports an assertion failure from deep inside the
diagnostic_shows_locus when attempting to print fix-it hints relating
to -Wxor-used-as-pow.  The case involves macro expansions with
-ftrack-macro-expansion=0.

It doesn't seem to make much sense to emit this warning for macro
expansions, so this patch updates the warning not to (which seems
to also be clang's behavior).  The patch also adds some bulletproofing
to diagnostic-show-locus.cc to be more robust against such invalid
fix-it hints.

Doing so fixes the ICE.

gcc/c-family/ChangeLog:
PR c/107002
* c-common.h (check_for_xor_used_as_pow): Add "rhs_loc" param.
* c-warn.cc (check_for_xor_used_as_pow): Add "rhs_loc" param.
Reject cases where involving macro expansions.

gcc/c/ChangeLog:
PR c/107002
* c-typeck.cc (parser_build_binary_op): Update for new param of
check_for_xor_used_as_pow.

gcc/cp/ChangeLog:
PR c/107002
* parser.cc (cp_parser_binary_expression): Update for new param of
check_for_xor_used_as_pow.

gcc/ChangeLog:
PR c/107002
* diagnostic-show-locus.cc (column_range::column_range): Factor
out assertion conditional into...
(column_range::valid_p): ...this new function.
(line_corrections::add_hint): Don't attempt to consolidate hints
if it would lead to invalid column_range instances.

gcc/testsuite/ChangeLog:
PR c/107002
* c-c++-common/Wxor-used-as-pow-1.c: Add macro test.
* c-c++-common/Wxor-used-as-pow-pr107002-0.c: New test.
* c-c++-common/Wxor-used-as-pow-pr107002-1.c: New test.
* c-c++-common/Wxor-used-as-pow-pr107002-2.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/c-family/c-common.h
gcc/c-family/c-warn.cc
gcc/c/c-typeck.cc
gcc/cp/parser.cc
gcc/diagnostic-show-locus.cc
gcc/testsuite/c-c++-common/Wxor-used-as-pow-1.c
gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-0.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/Wxor-used-as-pow-pr107002-2.c [new file with mode: 0644]