From c56e97274f164e704e7f13dfe53531ced3cb24ca Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 18 Jul 2018 13:13:11 +0000 Subject: [PATCH] PR c++/86190 - bogus -Wsign-conversion warning PR c++/86190 - bogus -Wsign-conversion warning * typeck.c (cp_build_binary_op): Fix formatting. Add a warning sentinel. * g++.dg/warn/Wsign-conversion-3.C: New test. * g++.dg/warn/Wsign-conversion-4.C: New test. From-SVN: r262855 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/typeck.c | 10 ++++++---- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/g++.dg/warn/Wsign-conversion-3.C | 13 +++++++++++++ gcc/testsuite/g++.dg/warn/Wsign-conversion-4.C | 14 ++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wsign-conversion-3.C create mode 100644 gcc/testsuite/g++.dg/warn/Wsign-conversion-4.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index fa5869921de..738b1795322 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-07-18 Marek Polacek + + PR c++/86190 - bogus -Wsign-conversion warning + * typeck.c (cp_build_binary_op): Fix formatting. Add a warning + sentinel. + 2018-07-18 Paolo Carlini PR c++/59480, DR 136 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index ea4ce9649cd..1335da5e9bc 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5312,12 +5312,13 @@ cp_build_binary_op (location_t location, if (short_compare) { - /* We call shorten_compare only for diagnostic-reason. */ - tree xop0 = fold_simple (op0), xop1 = fold_simple (op1), - xresult_type = result_type; + /* We call shorten_compare only for diagnostics. */ + tree xop0 = fold_simple (op0); + tree xop1 = fold_simple (op1); + tree xresult_type = result_type; enum tree_code xresultcode = resultcode; shorten_compare (location, &xop0, &xop1, &xresult_type, - &xresultcode); + &xresultcode); } if ((short_compare || code == MIN_EXPR || code == MAX_EXPR) @@ -5350,6 +5351,7 @@ cp_build_binary_op (location_t location, otherwise, it will be given type RESULT_TYPE. */ if (! converted) { + warning_sentinel w (warn_sign_conversion, short_compare); if (TREE_TYPE (op0) != result_type) op0 = cp_convert_and_check (result_type, op0, complain); if (TREE_TYPE (op1) != result_type) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e1832f82e92..47aec4f5c7a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-07-18 Marek Polacek + + PR c++/86190 - bogus -Wsign-conversion warning + * g++.dg/warn/Wsign-conversion-3.C: New test. + * g++.dg/warn/Wsign-conversion-4.C: New test. + 2018-07-18 Paolo Carlini PR c++/59480, DR 136 diff --git a/gcc/testsuite/g++.dg/warn/Wsign-conversion-3.C b/gcc/testsuite/g++.dg/warn/Wsign-conversion-3.C new file mode 100644 index 00000000000..2c3fef31475 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wsign-conversion-3.C @@ -0,0 +1,13 @@ +// PR c++/86190 +// { dg-options "-Wsign-conversion -Wsign-compare" } + +typedef unsigned long sz_t; +sz_t s(); +bool f(int i) { return s() < (unsigned long) i; } +bool f2(int i) { return s() < static_cast(i); } +bool f3(int i) { return s() < i; } // { dg-warning "comparison of integer expressions of different signedness" } +bool f4(int i) { return s() < (long) i; } // { dg-warning "comparison of integer expressions of different signedness" } +bool f5(short int i) { return s() < (int) i; } // { dg-warning "comparison of integer expressions of different signedness" } +bool f6(signed char i) { return s() < (int) i; } // { dg-warning "comparison of integer expressions of different signedness" } +bool f7(unsigned char i) { return s() < i; } +bool f8(signed char i) { return s() < i; } // { dg-warning "comparison of integer expressions of different signedness" } diff --git a/gcc/testsuite/g++.dg/warn/Wsign-conversion-4.C b/gcc/testsuite/g++.dg/warn/Wsign-conversion-4.C new file mode 100644 index 00000000000..40814b95587 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wsign-conversion-4.C @@ -0,0 +1,14 @@ +// PR c++/86190 +// { dg-options "-Wsign-conversion -Wsign-compare" } + +typedef unsigned long size_t; + +struct vector { + typedef size_t size_type; + size_type size(); +}; + +bool func(vector vec, int var) +{ + return vec.size() < static_cast(var); // { dg-bogus "may change" } +} -- 2.30.2