On Tue, Jul 25, 2017 at 08:50:24AM -0600, Martin Sebor wrote: > How hard would it be to also suppress the warning for benign > comparisons like C++ manages to do? E.g., C warns on this > code even though there's no problem with it: > > int foo (unsigned int b) > { > const int a = 1; > return (a < b); > }
The C++ FE can fold const int to it's initial value, but C's fold cannot. But there's decl_constant_value which is able to do it, so this might be just about --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -11783,9 +11783,15 @@ build_binary_op (location_t location, enum tree_code code, } if (warn_sign_compare) - warn_for_sign_compare (location, orig_op0_folded, - orig_op1_folded, op0, op1, - result_type, resultcode); + { + if (VAR_P (orig_op0_folded)) + orig_op0_folded = decl_constant_value (orig_op0_folded); + if (VAR_P (orig_op1_folded)) + orig_op1_folded = decl_constant_value (orig_op1_folded); + warn_for_sign_compare (location, orig_op0_folded, + orig_op1_folded, op0, op1, + result_type, resultcode); + } if (!in_late_binary_op && !int_operands) { if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) Marek