public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: -Waddress and value-dependent expr [PR105885]
@ 2022-06-23 15:06 Jason Merrill
  2022-06-23 15:07 ` [pushed] c++: -Waddress and if constexpr [PR94554] Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2022-06-23 15:06 UTC (permalink / raw)
  To: gcc-patches

We already suppress various warnings for code that would be tautological if
written directly, but not when it's the result of template substitution.  It
seems we need to do this for -Waddress as well.

Tested x86_64-pc-linux-gnu, applying to trunk.

	PR c++/105885

gcc/cp/ChangeLog:

	* pt.cc (tsubst_copy_and_build): Also suppress -Waddress for
	comparison of dependent operands.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1z/constexpr-if37.C: New test.
---
 gcc/cp/pt.cc                                |  1 +
 gcc/testsuite/g++.dg/cpp1z/constexpr-if37.C | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if37.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 80d2bec2348..4535c14c26a 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -20438,6 +20438,7 @@ tsubst_copy_and_build (tree t,
 	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);
+	warning_sentinel s5(warn_address, was_dep);
 
 	tree r = build_x_binary_op
 	  (input_location, TREE_CODE (t),
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if37.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if37.C
new file mode 100644
index 00000000000..e11e02cfa0e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if37.C
@@ -0,0 +1,21 @@
+// PR c++/105885
+// { dg-do compile { target c++17 } }
+// { dg-additional-options -Wall }
+
+int i;
+
+template<const char* ARG = nullptr>
+void test() {
+  if constexpr(ARG == nullptr) {
+    ++i;
+  } else {
+    --i;
+  }
+}
+
+const char CONSTSTR[] = {'\n', '\t', ' ', '\0'};
+
+int main() {
+  test();
+  test<CONSTSTR>();
+}

base-commit: 8a15cd3396aa08dc2633982481fe392af0aa9e78
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pushed] c++: -Waddress and if constexpr [PR94554]
  2022-06-23 15:06 [pushed] c++: -Waddress and value-dependent expr [PR105885] Jason Merrill
@ 2022-06-23 15:07 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-06-23 15:07 UTC (permalink / raw)
  To: gcc-patches

Like we avoid various warnings for seemingly tautological expressions when
substituting a template, we should avoid warning for the implicit conversion
to bool in an if statement.  I considered also doing this for the conditions
in loop expressions, but that seems unnecessary, as a loop condition is
unlikely to be a constant.

The change to finish_if_stmt_cond isn't necessary since dependent_operand_p
looks through IMPLICIT_CONV_EXPR, but makes it more constent with
e.g. build_x_binary_op that determines the type of an expression and then
builds it using the original operands.

Tested x86_64-pc-linux-gnu, applying to trunk.

	PR c++/94554

gcc/cp/ChangeLog:

	* pt.cc (dependent_operand_p): Split out from...
	(tsubst_copy_and_build): ...here.
	(tsubst_expr) [IF_STMT]: Use it.
	* semantics.cc (finish_if_stmt_cond): Keep the pre-conversion
	condition in the template tree.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1z/constexpr-if38.C: New test.
---
 gcc/cp/pt.cc                                | 43 +++++++++++++++------
 gcc/cp/semantics.cc                         | 10 +++--
 gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C | 16 ++++++++
 3 files changed, 55 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 4535c14c26a..12a2b57d9e3 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -18547,6 +18547,29 @@ lookup_init_capture_pack (tree decl)
   return r;
 }
 
+/* T is an operand of a template tree being substituted.  Return whether
+   T is dependent such that we should suppress some warnings that would
+   make sense if the substituted expression were written directly, like
+     template <int I> bool f() { return I == 2; }
+   We don't want to warn when instantiating f that comparing two constants
+   always has the same value.
+
+   This is a more limited concept of dependence than instantiation-dependent;
+   here we don't care whether substitution could fail.  */
+
+static bool
+dependent_operand_p (tree t)
+{
+  while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
+    t = TREE_OPERAND (t, 0);
+  ++processing_template_decl;
+  bool r = (potential_constant_expression (t)
+	    ? value_dependent_expression_p (t)
+	    : type_dependent_expression_p (t));
+  --processing_template_decl;
+  return r;
+}
+
 /* Like tsubst_copy for expressions, etc. but also does semantic
    processing.  */
 
@@ -18872,8 +18895,13 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
       IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
       if (IF_STMT_CONSTEXPR_P (t))
 	args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
-      tmp = RECUR (IF_COND (t));
-      tmp = finish_if_stmt_cond (tmp, stmt);
+      {
+	tree cond = IF_COND (t);
+	bool was_dep = dependent_operand_p (cond);
+	cond = RECUR (cond);
+	warning_sentinel s1(warn_address, was_dep);
+	tmp = finish_if_stmt_cond (cond, stmt);
+      }
       if (IF_STMT_CONSTEXPR_P (t)
 	  && instantiation_dependent_expression_p (tmp))
 	{
@@ -20422,15 +20450,8 @@ tsubst_copy_and_build (tree t,
 	   warnings that depend on the range of the types involved.  */
 	tree op0 = TREE_OPERAND (t, 0);
 	tree op1 = TREE_OPERAND (t, 1);
-	auto dep_p = [](tree t) {
-	  ++processing_template_decl;
-	  bool r = (potential_constant_expression (t)
-		    ? value_dependent_expression_p (t)
-		    : type_dependent_expression_p (t));
-	  --processing_template_decl;
-	  return r;
-	};
-	const bool was_dep = dep_p (op0) || dep_p (op1);
+	const bool was_dep = (dependent_operand_p (op0)
+			      || dependent_operand_p (op1));
 	op0 = RECUR (op0);
 	op1 = RECUR (op1);
 
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 24cca99e909..2344b5eea00 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -1029,9 +1029,9 @@ maybe_warn_for_constant_evaluated (tree cond, bool constexpr_if)
    IF_STMT.  */
 
 tree
-finish_if_stmt_cond (tree cond, tree if_stmt)
+finish_if_stmt_cond (tree orig_cond, tree if_stmt)
 {
-  cond = maybe_convert_cond (cond);
+  tree cond = maybe_convert_cond (orig_cond);
   if (IF_STMT_CONSTEXPR_P (if_stmt)
       && !type_dependent_expression_p (cond)
       && require_constant_expression (cond)
@@ -1045,7 +1045,11 @@ finish_if_stmt_cond (tree cond, tree if_stmt)
       cond = cxx_constant_value (cond, NULL_TREE);
     }
   else
-    maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false);
+    {
+      maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false);
+      if (processing_template_decl)
+	cond = orig_cond;
+    }
   finish_cond (&IF_COND (if_stmt), cond);
   add_stmt (if_stmt);
   THEN_CLAUSE (if_stmt) = push_stmt_list ();
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C
new file mode 100644
index 00000000000..3e4fd446dca
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C
@@ -0,0 +1,16 @@
+// PR c++/94554
+// { dg-do compile { target c++17 } }
+// { dg-additional-options -Wall }
+
+int meow() { return 1; }
+void kitty(int);
+template <int (*F)()>
+void test() {
+    if constexpr (F) {
+        kitty(F());
+    } else {
+        kitty(2);
+    }
+}
+template void test<nullptr>();
+template void test<meow>();

base-commit: 6e4d5300c1f62c3f0cd1bf859b0ee6bb4e31e434
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-23 15:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-23 15:06 [pushed] c++: -Waddress and value-dependent expr [PR105885] Jason Merrill
2022-06-23 15:07 ` [pushed] c++: -Waddress and if constexpr [PR94554] Jason Merrill

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).