public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/redhat/heads/gcc-10-branch)] c++: Fix -Wtype-limits in templates.
@ 2020-01-30 20:59 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2020-01-30 20:59 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4dd468a042e19ef0fdbb1c53ca4060d4cb4972c5

commit 4dd468a042e19ef0fdbb1c53ca4060d4cb4972c5
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jan 30 13:12:05 2020 -0500

    c++: Fix -Wtype-limits in templates.
    
    When instantiating a template tsubst_copy_and_build suppresses -Wtype-limits
    warnings about e.g. == always being false because it might not always be
    false for an instantiation with other template arguments.  But we should
    warn if the operands don't depend on template arguments.
    
    	PR c++/82521
    	* pt.c (tsubst_copy_and_build) [EQ_EXPR]: Only suppress warnings if
    	the expression was dependent before substitution.

Diff:
---
 gcc/cp/ChangeLog                          |  6 ++++++
 gcc/cp/pt.c                               | 12 ++++++++----
 gcc/testsuite/g++.dg/warn/Wtype-limits3.C | 13 +++++++++++++
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index a402b97..f260d5d 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2020-01-29  Jason Merrill  <jason@redhat.com>
+
+	PR c++/82521
+	* pt.c (tsubst_copy_and_build) [EQ_EXPR]: Only suppress warnings if
+	the expression was dependent before substitution.
+
 2020-01-30  Bin Cheng  <bin.cheng@linux.alibaba.com>
 
 	* coroutines.cc (act_des_fn): New.
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 416ff63..40ff3c3 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -19279,10 +19279,14 @@ tsubst_copy_and_build (tree t,
     case MEMBER_REF:
     case DOTSTAR_EXPR:
       {
-	warning_sentinel s1(warn_type_limits);
-	warning_sentinel s2(warn_div_by_zero);
-	warning_sentinel s3(warn_logical_op);
-	warning_sentinel s4(warn_tautological_compare);
+	/* If T was type-dependent, suppress warnings that depend on the range
+	   of the types involved.  */
+	bool was_dep = uses_template_parms (t);
+	warning_sentinel s1(warn_type_limits, was_dep);
+	warning_sentinel s2(warn_div_by_zero, was_dep);
+	warning_sentinel s3(warn_logical_op, was_dep);
+	warning_sentinel s4(warn_tautological_compare, was_dep);
+
 	tree op0 = RECUR (TREE_OPERAND (t, 0));
 	tree op1 = RECUR (TREE_OPERAND (t, 1));
 	tree r = build_x_binary_op
diff --git a/gcc/testsuite/g++.dg/warn/Wtype-limits3.C b/gcc/testsuite/g++.dg/warn/Wtype-limits3.C
new file mode 100644
index 0000000..b9059ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wtype-limits3.C
@@ -0,0 +1,13 @@
+// PR c++/82521
+// { dg-additional-options "-Wtype-limits" }
+
+template <typename T>
+const char * g(const unsigned char value)
+{
+  return value == -1 ? "-1" : "no"; // { dg-warning "always false" }
+}
+
+int main()
+{
+  g<int>(-1);
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-01-30 20:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-30 20:59 [gcc(refs/vendors/redhat/heads/gcc-10-branch)] c++: Fix -Wtype-limits in templates Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).