From ca7e759d96d6c0c046a6e20b066d86d070f65e35 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Wed, 26 Feb 2014 16:28:08 -0500 Subject: [PATCH] re PR c++/59231 (gcc misses [-Werror=sign-compare] when inside a template) PR c++/59231 PR c++/11586 PR c++/14710 PR c++/57132 gcc/ * c-common.c (shorten_compare): Don't check c_inhibit_evaluation_warnings. gcc/cp/ * pt.c (struct warning_sentinel): New. (tsubst_copy_and_build): Use it instead of c_inhibit_evaluation_warnings. From-SVN: r208183 --- gcc/c-family/ChangeLog | 7 ++++ gcc/c-family/c-common.c | 4 +-- gcc/cp/ChangeLog | 10 ++++++ gcc/cp/pt.c | 34 +++++++++---------- gcc/cp/typeck.c | 3 +- .../g++.dg/cilk-plus/AN/array_test2_tplt.cc | 2 +- gcc/testsuite/g++.dg/cpp0x/overflow1.C | 2 ++ gcc/testsuite/g++.dg/warn/Wsign-compare-7.C | 18 ++++++++++ 8 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wsign-compare-7.C diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 2bb59357cb41..088d158a083e 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,10 @@ +2014-02-26 Jason Merrill + + PR c++/59231 + PR c++/11586 + * c-common.c (shorten_compare): Don't check + c_inhibit_evaluation_warnings. + 2014-02-19 Jakub Jelinek PR c/37743 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index e23a9df23c7e..abd96fbe9aea 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -4218,8 +4218,7 @@ shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr, type = c_common_unsigned_type (type); } - if (TREE_CODE (primop0) != INTEGER_CST - && c_inhibit_evaluation_warnings == 0) + if (TREE_CODE (primop0) != INTEGER_CST) { if (val == truthvalue_false_node) warning_at (loc, OPT_Wtype_limits, @@ -4299,7 +4298,6 @@ shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr, warning. */ bool warn = warn_type_limits && !in_system_header_at (loc) - && c_inhibit_evaluation_warnings == 0 && !(TREE_CODE (primop0) == INTEGER_CST && !TREE_OVERFLOW (convert (c_common_signed_type (type), primop0))) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index f1f1195b7fd7..41a0db6e13da 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,15 @@ 2014-02-26 Jason Merrill + PR c++/59231 + PR c++/11586 + PR c++/14710 + PR c++/57132 + * pt.c (struct warning_sentinel): New. + (tsubst_copy_and_build): Use it instead of + c_inhibit_evaluation_warnings. + * typeck.c (maybe_warn_about_useless_cast): Remove + c_inhibit_evaluation_warnings check. + PR c++/54440 * pt.c (get_template_parm_index): New. (fixed_parameter_pack_p_1, fixed_parameter_pack_p): New. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 1f5a2b73532d..8126905941e4 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -1,4 +1,4 @@ -/* Handle parameterized types (templates) for GNU C++. +/* Handle parameterized types (templates) for GNU -*- C++ -*-. Copyright (C) 1992-2014 Free Software Foundation, Inc. Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. Rewritten by Jason Merrill (jason@cygnus.com). @@ -14063,6 +14063,16 @@ tsubst_non_call_postfix_expression (tree t, tree args, return t; } +/* Sentinel to disable certain warnings during template substitution. */ + +struct warning_sentinel { + int &flag; + int val; + warning_sentinel(int& flag, bool suppress=true) + : flag(flag), val(flag) { if (suppress) flag = 0; } + ~warning_sentinel() { flag = val; } +}; + /* Like tsubst but deals with expressions and performs semantic analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ @@ -14229,7 +14239,7 @@ tsubst_copy_and_build (tree t, op = RECUR (TREE_OPERAND (t, 0)); - ++c_inhibit_evaluation_warnings; + warning_sentinel s(warn_useless_cast); switch (TREE_CODE (t)) { case CAST_EXPR: @@ -14250,7 +14260,6 @@ tsubst_copy_and_build (tree t, default: gcc_unreachable (); } - --c_inhibit_evaluation_warnings; RETURN (r); } @@ -14325,11 +14334,9 @@ tsubst_copy_and_build (tree t, case MEMBER_REF: case DOTSTAR_EXPR: { - tree r; - - ++c_inhibit_evaluation_warnings; - - r = build_x_binary_op + warning_sentinel s1(warn_type_limits); + warning_sentinel s2(warn_div_by_zero); + tree r = build_x_binary_op (input_location, TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)), (TREE_NO_WARNING (TREE_OPERAND (t, 0)) @@ -14344,8 +14351,6 @@ tsubst_copy_and_build (tree t, if (EXPR_P (r) && TREE_NO_WARNING (t)) TREE_NO_WARNING (r) = TREE_NO_WARNING (t); - --c_inhibit_evaluation_warnings; - RETURN (r); } @@ -14460,11 +14465,8 @@ tsubst_copy_and_build (tree t, case MODOP_EXPR: { - tree r; - - ++c_inhibit_evaluation_warnings; - - r = build_x_modify_expr + warning_sentinel s(warn_div_by_zero); + tree r = build_x_modify_expr (EXPR_LOCATION (t), RECUR (TREE_OPERAND (t, 0)), TREE_CODE (TREE_OPERAND (t, 1)), @@ -14479,8 +14481,6 @@ tsubst_copy_and_build (tree t, if (TREE_NO_WARNING (t)) TREE_NO_WARNING (r) = TREE_NO_WARNING (t); - --c_inhibit_evaluation_warnings; - RETURN (r); } diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 5fc0e6bb9d1d..1e14b638e6c3 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -6282,8 +6282,7 @@ void maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain) { if (warn_useless_cast - && complain & tf_warning - && c_inhibit_evaluation_warnings == 0) + && complain & tf_warning) { if (REFERENCE_REF_P (expr)) expr = TREE_OPERAND (expr, 0); diff --git a/gcc/testsuite/g++.dg/cilk-plus/AN/array_test2_tplt.cc b/gcc/testsuite/g++.dg/cilk-plus/AN/array_test2_tplt.cc index 87c37e1bb4cf..041c05289766 100644 --- a/gcc/testsuite/g++.dg/cilk-plus/AN/array_test2_tplt.cc +++ b/gcc/testsuite/g++.dg/cilk-plus/AN/array_test2_tplt.cc @@ -1,5 +1,5 @@ /* { dg-do run } */ -/* { dg-options "-fcilkplus" } */ +/* { dg-options "-fcilkplus -Wno-overflow" } */ #include #include diff --git a/gcc/testsuite/g++.dg/cpp0x/overflow1.C b/gcc/testsuite/g++.dg/cpp0x/overflow1.C index 7033e9c451ac..b8591b4af413 100644 --- a/gcc/testsuite/g++.dg/cpp0x/overflow1.C +++ b/gcc/testsuite/g++.dg/cpp0x/overflow1.C @@ -1,3 +1,5 @@ +// { dg-prune-output "-Woverflow" } + template struct Fib { diff --git a/gcc/testsuite/g++.dg/warn/Wsign-compare-7.C b/gcc/testsuite/g++.dg/warn/Wsign-compare-7.C new file mode 100644 index 000000000000..51f13f6f953c --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wsign-compare-7.C @@ -0,0 +1,18 @@ +// PR c++/59231 +// { dg-options "-Wsign-compare" } + +template +bool equals(X x, Y y) +{ + return (x == y); // { dg-warning "signed" } +} + +int main() +{ + unsigned long x = 2; + signed int y = 2; + + if(!equals (x, y)) + return 1; + return 0; +}