From: Bernd Edlinger Date: Mon, 19 Sep 2016 22:10:11 +0000 (+0000) Subject: re PR c++/77434 (warn about suspicious precedence of ternary operator (?:)) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=144a96e4c914710cacdb95d33f4e03dc9fed7655;p=gcc.git re PR c++/77434 (warn about suspicious precedence of ternary operator (?:)) gcc: 2016-09-19 Bernd Edlinger PR c++/77434 * doc/invoke.texi: Document -Wint-in-bool-context. c-family: 2016-09-19 Bernd Edlinger 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 PR c++/77434 * cvt.c (cp_convert_and_check): Suppress Wint-in-bool-context here. testsuite: 2016-09-19 Bernd Edlinger PR c++/77434 * c-c++-common/Wint-in-bool-context.c: New test. From-SVN: r240251 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5011595c5b9..e42b2e77f4e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,8 @@ 2016-09-19 Bernd Edlinger + PR c++/77434 + * doc/invoke.texi: Document -Wint-in-bool-context. + PR middle-end/77421 * dwarf2out.c (output_loc_operands): Fix an assertion. diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 41afbc5af9b..5efbf55e1a7 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,10 @@ +2016-09-19 Bernd Edlinger + + 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 * c-common.c (max_align_t_align): Also consider alignment of diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index b6545c08105..d1372a4feea 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -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 ()) { diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index c55c7c34961..6cf915d5143 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -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. diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index e4bd392e953..6aa79a9409f 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2016-09-19 Bernd Edlinger + + PR c++/77434 + * cvt.c (cp_convert_and_check): Suppress Wint-in-bool-context here. + 2016-09-16 Patrick Palka PR c++/77639 diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index 977c6652211..2f5f15a2c82 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -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); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 8eb5eff098e..3c272836feb 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -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 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c825702acba..ca54995cc6a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-09-19 Bernd Edlinger + + PR c++/77434 + * c-c++-common/Wint-in-bool-context.c: New test. + 2016-09-19 Joseph Myers * 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 index 00000000000..38832d78f2f --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wint-in-bool-context.c @@ -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; +}