]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Ensure !!var is not an lvalue [PR107065]
authorJakub Jelinek <jakub@redhat.com>
Thu, 15 Dec 2022 18:17:45 +0000 (19:17 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 10 Feb 2023 12:44:05 +0000 (13:44 +0100)
commitbc1ee711eeab4b0d55463cd153747d30c69225c7
tree3383e7ffe7c8862dadb761aaa0eae7912c104b21
parent7e54e5a2bba69dc7fcbc88fe8cb20c91aaafabd2
c++: Ensure !!var is not an lvalue [PR107065]

The TRUTH_NOT_EXPR case in cp_build_unary_op is one of the spots where
we somewhat fold immediately using invert_truthvalue_loc.
I've tried using
  return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
in there instead, but unfortunately that regressed
Wlogical-not-parentheses-*.c pr49706.c pr62199.c pr65120.c sequence-pt-1.C
tests, so at least for backporting that doesn't seem to be a way to go.

So, this patch instead wraps it into NON_LVALUE_EXPR if needed (which also
need a tweak for some tests in the pr47906.c test, but nothing major),
with the intent to make it backportable, and later I'll try to do further
steps to avoid folding here prematurely.  Most of the problems with
build1 TRUTH_NOT_EXPR are that it doesn't even invert comparisons as most
common case and lots of warning code isn't able to deal with ! around
comparisons; so perhaps one way to do this would be fold by hand only
invertable comparisons and for the rest create TRUTH_NOT_EXPR.

2022-12-15  Jakub Jelinek  <jakub@redhat.com>

PR c++/107065
gcc/cp/
* typeck.cc (cp_build_unary_op) <case TRUTH_NOT_EXPR>: If
invert_truthvalue_loc returns obvalue_p, wrap it into NON_LVALUE_EXPR.
* parser.cc (cp_parser_binary_expression): Don't call
warn_logical_not_parentheses if current.lhs is a NON_LVALUE_EXPR
of a decl with boolean type.
gcc/testsuite/
* g++.dg/cpp0x/pr107065.C: New test.

(cherry picked from commit 8b775b4c48a3cc4ef5c50e56144aea02da2e9cc6)
gcc/cp/parser.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp0x/pr107065.C [new file with mode: 0644]