2016-09-19 Bernd Edlinger <bernd.edlinger@hotmail.de>
+ PR c++/77434
+ * doc/invoke.texi: Document -Wint-in-bool-context.
+
PR middle-end/77421
* dwarf2out.c (output_loc_operands): Fix an assertion.
+2016-09-19 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ PR c++/77434
+ * c.opt (Wint-in-bool-context): New warning.
+ * c-common.c (c_common_truthvalue_conversion): Warn on integer
+ constants in boolean context.
+
2016-09-19 Joseph Myers <joseph@codesourcery.com>
* c-common.c (max_align_t_align): Also consider alignment of
TREE_OPERAND (expr, 0));
case COND_EXPR:
+ if (warn_int_in_bool_context)
+ {
+ tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
+ tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
+ if (TREE_CODE (val1) == INTEGER_CST
+ && TREE_CODE (val2) == INTEGER_CST
+ && !integer_zerop (val1)
+ && !integer_zerop (val2)
+ && (!integer_onep (val1)
+ || !integer_onep (val2)))
+ warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
+ "?: using integer constants in boolean context");
+ }
/* Distribute the conversion into the arms of a COND_EXPR. */
if (c_dialect_cxx ())
{
C ObjC Var(warn_int_conversion) Init(1) Warning
Warn about incompatible integer to pointer and pointer to integer conversions.
+Wint-in-bool-context
+C ObjC C++ ObjC++ Var(warn_int_in_bool_context) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
+Warn for suspicious integer expressions in boolean context.
+
Wint-to-pointer-cast
C ObjC C++ ObjC++ Var(warn_int_to_pointer_cast) Init(1) Warning
Warn when there is a cast to a pointer from an integer of a different size.
+2016-09-19 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ PR c++/77434
+ * cvt.c (cp_convert_and_check): Suppress Wint-in-bool-context here.
+
2016-09-16 Patrick Palka <ppalka@gcc.gnu.org>
PR c++/77639
{
/* Avoid bogus -Wparentheses warnings. */
warning_sentinel w (warn_parentheses);
+ warning_sentinel c (warn_int_in_bool_context);
folded_result = cp_convert (type, folded, tf_none);
}
folded_result = fold_simple (folded_result);
-Wframe-larger-than=@var{len} -Wno-free-nonheap-object -Wjump-misses-init @gol
-Wignored-qualifiers -Wignored-attributes -Wincompatible-pointer-types @gol
-Wimplicit -Wimplicit-function-declaration -Wimplicit-int @gol
--Winit-self -Winline -Wno-int-conversion @gol
+-Winit-self -Winline -Wno-int-conversion -Wint-in-bool-context @gol
-Wno-int-to-pointer-cast -Winvalid-memory-model -Wno-invalid-offsetof @gol
-Winvalid-pch -Wlarger-than=@var{len} @gol
-Wlogical-op -Wlogical-not-parentheses -Wlong-long @gol
-Wduplicate-decl-specifier @r{(C and Objective-C only)} @gol
-Wenum-compare @r{(in C/ObjC; this is on by default in C++)} @gol
-Wformat @gol
+-Wint-in-bool-context @gol
-Wimplicit @r{(C and Objective-C only)} @gol
-Wimplicit-int @r{(C and Objective-C only)} @gol
-Wimplicit-function-declaration @r{(C and Objective-C only)} @gol
The restrictions on @code{offsetof} may be relaxed in a future version
of the C++ standard.
+@item -Wint-in-bool-context
+@opindex Wint-in-bool-context
+@opindex Wno-int-in-bool-context
+Warn for suspicious use of integer values where boolean values are expected,
+such as 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 -Wno-int-to-pointer-cast
@opindex Wno-int-to-pointer-cast
@opindex Wint-to-pointer-cast
+2016-09-19 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ PR c++/77434
+ * c-c++-common/Wint-in-bool-context.c: New test.
+
2016-09-19 Joseph Myers <joseph@codesourcery.com>
* gcc.dg/cr-decimal-dig-1.c: New test.
--- /dev/null
+/* PR c++/77434 */
+/* { dg-options "-Wint-in-bool-context" } */
+/* { dg-do compile } */
+
+int foo (int a, int b)
+{
+ if (a > 0 && a <= (b == 1) ? 1 : 2) /* { dg-warning "boolean context" } */
+ return 1;
+
+ if (a > 0 && a <= (b == 2) ? 1 : 1) /* { dg-bogus "boolean context" } */
+ return 2;
+
+ if (a > 0 && a <= (b == 3) ? 0 : 2) /* { dg-bogus "boolean context" } */
+ return 3;
+
+ if (a == b ? 0 : 0) /* { dg-bogus "boolean context" } */
+ return 4;
+
+ if (a == ((b ? 2|4 : 1) & 3 ? 0 : 2)) /* { dg-bogus "boolean context" } */
+ return 5;
+
+ if (a ? 1 : 1+1) /* { dg-warning "boolean context" } */
+ return 6;
+
+ if (b ? 1+1 : 1) /* { dg-warning "boolean context" } */
+ return 7;
+
+ return 0;
+}