Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 239953) +++ gcc/c-family/c-common.c (working copy) @@ -4618,6 +4618,14 @@ c_common_truthvalue_conversion (location_t locatio 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_zerop (TREE_OPERAND (expr, 2)) + && (!integer_onep (TREE_OPERAND (expr, 1)) + || !integer_onep (TREE_OPERAND (expr, 2)))) + warning_at (EXPR_LOCATION (expr), OPT_Wcond_in_bool_context, + "?: using integer constants in boolean context"); /* Distribute the conversion into the arms of a COND_EXPR. */ if (c_dialect_cxx ()) { Index: gcc/c-family/c.opt =================================================================== --- gcc/c-family/c.opt (revision 239953) +++ gcc/c-family/c.opt (working copy) @@ -350,6 +350,10 @@ Wcomments C ObjC C++ ObjC++ Warning Alias(Wcomment) Synonym for -Wcomment. +Wcond-in-bool-context +C ObjC C++ ObjC++ Var(warn_cond_in_bool_context) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) +Warn for conditional expressions (?:) using integer constants in boolean context. + Wconditionally-supported C++ ObjC++ Var(warn_conditionally_supported) Warning Warn for conditionally-supported constructs. Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 239953) +++ gcc/doc/invoke.texi (working copy) @@ -259,7 +259,8 @@ Objective-C and Objective-C++ Dialects}. -Wno-attributes -Wbool-compare -Wno-builtin-macro-redefined @gol -Wc90-c99-compat -Wc99-c11-compat @gol -Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol --Wchar-subscripts -Wclobbered -Wcomment -Wconditionally-supported @gol +-Wchar-subscripts -Wclobbered -Wcomment @gol +-Wcond-in-bool-context -Wconditionally-supported @gol -Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol -Wdelete-incomplete @gol -Wno-deprecated -Wno-deprecated-declarations -Wno-designated-init @gol @@ -5179,6 +5180,13 @@ programs. Warn for variables that might be changed by @code{longjmp} or @code{vfork}. This warning is also enabled by @option{-Wextra}. +@item -Wcond-in-bool-context +@opindex Wcond-in-bool-context +@opindex Wno-cond-in-bool-context +Warn for conditional expressions (?:) using non-boolean integer constants in +boolean context, like @code{if (a <= b ? 2 : 3)}. This warning is enabled +by @option{-Wall}. + @item -Wconditionally-supported @r{(C++ and Objective-C++ only)} @opindex Wconditionally-supported @opindex Wno-conditionally-supported Index: gcc/dwarf2out.c =================================================================== --- gcc/dwarf2out.c (revision 239953) +++ gcc/dwarf2out.c (working copy) @@ -2051,9 +2051,9 @@ output_loc_operands (dw_loc_descr_ref loc, int for /* Make sure the offset has been computed and that we can encode it as an operand. */ gcc_assert (die_offset > 0 - && die_offset <= (loc->dw_loc_opc == DW_OP_call2) + && die_offset <= (loc->dw_loc_opc == DW_OP_call2 ? 0xffff - : 0xffffffff); + : 0xffffffff)); dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4, die_offset, NULL); } Index: gcc/testsuite/c-c++-common/Wcond-in-bool-context.c =================================================================== --- gcc/testsuite/c-c++-common/Wcond-in-bool-context.c (revision 0) +++ gcc/testsuite/c-c++-common/Wcond-in-bool-context.c (working copy) @@ -0,0 +1,17 @@ +/* PR c++/77434 */ +/* { dg-options "-Wcond-in-bool-context" } */ +/* { dg-do compile } */ + +int foo (int a, int b) +{ + if (a > 0 && a <= (b == 1) ? 1 : 2) /* { dg-warning "using integer constants in boolean context" } */ + return 1; + + if (a > 0 && a <= (b == 2) ? 1 : 1) /* { dg-bogus "using integer constants in boolean context" } */ + return 2; + + if (a > 0 && a <= (b == 3) ? 0 : 2) /* { dg-bogus "using integer constants in boolean context" } */ + return 3; + + return 0; +}