This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/77434] warn about suspicious precedence of ternary operator (?:)
- From: "bernd.edlinger at hotmail dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 01 Sep 2016 20:32:13 +0000
- Subject: [Bug c++/77434] warn about suspicious precedence of ternary operator (?:)
- Auto-submitted: auto-generated
- References: <bug-77434-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77434
--- Comment #5 from Bernd Edlinger <bernd.edlinger at hotmail dot de> ---
This is an idea for a warning that does not focus on parentheses:
Here we had: a ? c1 : c2;
but in a context where a boolean is requested.
It is always suspicious, when c1, and c2 are integer constants
which a neither 0 or 1:
Index: gcc/c-family/c-common.c
===================================================================
--- gcc/c-family/c-common.c (revision 239944)
+++ gcc/c-family/c-common.c (working copy)
@@ -4618,6 +4618,14 @@
TREE_OPERAND (expr, 0));
case COND_EXPR:
+ if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
+ && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
+ && !integer_zerop (TREE_OPERAND (expr, 1))
+ && !integer_onep (TREE_OPERAND (expr, 1))
+ && !integer_zerop (TREE_OPERAND (expr, 2))
+ && !integer_onep (TREE_OPERAND (expr, 2)))
+ warning_at (location, 0,
+ "?: expression using integer constants in boolean
context");
/* Distribute the conversion into the arms of a COND_EXPR. */
if (c_dialect_cxx ())
{
Bootstrap obviously fails here:
In file included from ../../gcc-trunk/gcc/dwarf2out.c:59:0:
../../gcc-trunk/gcc/dwarf2out.c: In function 'void
output_loc_operands(dw_loc_descr_ref, int)':
../../gcc-trunk/gcc/system.h:725:18: error: ?: expression using integer
constants in boolean context [-Werror]
((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__), 0 : 0))
^
../../gcc-trunk/gcc/dwarf2out.c:2053:2: note: in expansion of macro
'gcc_assert'
gcc_assert (die_offset > 0
^~~~~~~~~~
../../gcc-trunk/gcc/system.h:725:18: error: ?: expression using integer
constants in boolean context [-Werror]
((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__), 0 : 0))
^
../../gcc-trunk/gcc/dwarf2out.c:2053:2: note: in expansion of macro
'gcc_assert'
gcc_assert (die_offset > 0
^~~~~~~~~~
cc1plus: all warnings being treated as errors