re PR c++/77434 (warn about suspicious precedence of ternary operator (?:))
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Mon, 19 Sep 2016 22:10:11 +0000 (22:10 +0000)
committerBernd Edlinger <edlinger@gcc.gnu.org>
Mon, 19 Sep 2016 22:10:11 +0000 (22:10 +0000)
gcc:
2016-09-19  Bernd Edlinger  <bernd.edlinger@hotmail.de>

        PR c++/77434
        * doc/invoke.texi: Document -Wint-in-bool-context.

c-family:
2016-09-19  Bernd Edlinger  <bernd.edlinger@hotmail.de>

        PR c++/77434
        * c.opt (Wcond-in-bool-context): New warning.
        * c-common.c (c_common_truthvalue_conversion): Warn on integer
        constants in boolean context.

cp:
2016-09-19  Bernd Edlinger  <bernd.edlinger@hotmail.de>

        PR c++/77434
        * cvt.c (cp_convert_and_check): Suppress Wint-in-bool-context here.

testsuite:
2016-09-19  Bernd Edlinger  <bernd.edlinger@hotmail.de>

        PR c++/77434
        * c-c++-common/Wint-in-bool-context.c: New test.

From-SVN: r240251

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/c-family/c.opt
gcc/cp/ChangeLog
gcc/cp/cvt.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/Wint-in-bool-context.c [new file with mode: 0644]

index 5011595..e42b2e7 100644 (file)
@@ -1,5 +1,8 @@
 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.
 
index 41afbc5..5efbf55 100644 (file)
@@ -1,3 +1,10 @@
+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
index b6545c0..d1372a4 100644 (file)
@@ -4652,6 +4652,19 @@ c_common_truthvalue_conversion (location_t location, tree expr)
                                               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 ())
        {
index c55c7c3..6cf915d 100644 (file)
@@ -545,6 +545,10 @@ Wint-conversion
 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.
index e4bd392..6aa79a9 100644 (file)
@@ -1,3 +1,8 @@
+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
index 977c665..2f5f15a 100644 (file)
@@ -645,6 +645,7 @@ cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
        {
          /* 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);
index 8eb5eff..3c27283 100644 (file)
@@ -273,7 +273,7 @@ Objective-C and Objective-C++ Dialects}.
 -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
@@ -3660,6 +3660,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}.
 -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
@@ -5837,6 +5838,14 @@ warning about it.
 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
index c825702..ca54995 100644 (file)
@@ -1,3 +1,8 @@
+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.
diff --git a/gcc/testsuite/c-c++-common/Wint-in-bool-context.c b/gcc/testsuite/c-c++-common/Wint-in-bool-context.c
new file mode 100644 (file)
index 0000000..38832d7
--- /dev/null
@@ -0,0 +1,29 @@
+/* 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;
+}