[PATCH] c++: Fix up diagnostics about taking address of an immediate member function [PR102753]

Jakub Jelinek jakub@redhat.com
Wed Nov 24 18:02:57 GMT 2021


On Wed, Nov 24, 2021 at 05:02:03PM +0100, Jakub Jelinek via Gcc-patches wrote:
> Unfortunately, the location wrappers are optimized away before we get a
> chance to use them in cp_fold_r.
> So, on the following patch, we get the location right on PTRMEM_CSTs not
> used inside of initializers, but for PTRMEM_CSTs in initializers we report
> them at UNKNOWN_LOCATION.
> As PTRMEM_CST is a C++ FE tree, I wonder if we couldn't instead of your
> patch or in addition to it do:
>  struct GTY(()) ptrmem_cst {
>    struct tree_common common;
>    tree member;
> +  location_t locus;
>  };
> #define PTRMEM_CST_LOCATION(NODE) \
>   (((ptrmem_cst_t)PTRMEM_CST_CHECK (NODE))->locus)
> and in make_ptrmem_cst set PTRMEM_CST_LOCATION to input_location.

Here is a full so far lightly tested patch that does this,
GXX_TESTSUITE_STDS=98,11,14,17,20,2b make check-g++ \
RUNTESTFLAGS="dg.exp='consteval* srcloc* is-constant-eval* spaceship-synth* constexpr-99287* feat-cxx* lambda-specifiers1*'"
which covers all tests that use consteval keyword in gcc/testsuite/.

Ok for trunk if it passes full bootstrap/regtest?

2021-11-24  Jakub Jelinek  <jakub@redhat.com>

	PR c++/102753
	* cp-tree.h (struct ptrmem_cst): Add locus member.
	(PTRMEM_CST_LOCATION): Define.
	* tree.c (make_ptrmem_cst): Set PTRMEM_CST_LOCATION to input_location.
	* typeck.c (cp_build_addr_expr_1): Don't diagnose taking address of
	immediate functions here.  Instead when taking their address make
	sure the returned ADDR_EXPR has EXPR_LOCATION set.
	* pt.c (tsubst_copy): Ensure ADDR_EXPR of immediate function has
	EXPR_LOCATION set.
	* cp-gimplify.c (cp_fold_r): Diagnose taking address of immediate
	functions here.  For consteval if don't walk THEN_CLAUSE.
	(cp_genericize_r): Don't assert calls to immediate functions must
	be source_location_current_p, instead only constant evaluate
	calls to source_location_current_p.

	* g++.dg/cpp2a/consteval20.C: Add some extra tests.
	* g++.dg/cpp2a/consteval23.C: Likewise.
	* g++.dg/cpp2a/consteval25.C: New test.
	* g++.dg/cpp2a/srcloc20.C: New test.

--- gcc/cp/cp-tree.h.jj	2021-11-24 15:05:23.291928876 +0100
+++ gcc/cp/cp-tree.h	2021-11-24 17:08:19.507806769 +0100
@@ -703,6 +703,7 @@ struct GTY(()) template_parm_index {
 struct GTY(()) ptrmem_cst {
   struct tree_common common;
   tree member;
+  location_t locus;
 };
 typedef struct ptrmem_cst * ptrmem_cst_t;
 
@@ -4724,6 +4725,11 @@ more_aggr_init_expr_args_p (const aggr_i
 #define PTRMEM_CST_MEMBER(NODE) \
   (((ptrmem_cst_t)PTRMEM_CST_CHECK (NODE))->member)
 
+/* For a pointer-to-member constant `X::Y' this is a location where
+   the address of the member has been taken.  */
+#define PTRMEM_CST_LOCATION(NODE) \
+  (((ptrmem_cst_t)PTRMEM_CST_CHECK (NODE))->locus)
+
 /* The expression in question for a TYPEOF_TYPE.  */
 #define TYPEOF_TYPE_EXPR(NODE) (TYPE_VALUES_RAW (TYPEOF_TYPE_CHECK (NODE)))
 
--- gcc/cp/tree.c.jj	2021-11-24 15:05:23.371927735 +0100
+++ gcc/cp/tree.c	2021-11-24 17:09:05.348164621 +0100
@@ -5167,6 +5167,7 @@ make_ptrmem_cst (tree type, tree member)
   tree ptrmem_cst = make_node (PTRMEM_CST);
   TREE_TYPE (ptrmem_cst) = type;
   PTRMEM_CST_MEMBER (ptrmem_cst) = member;
+  PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
   return ptrmem_cst;
 }
 
--- gcc/cp/typeck.c.jj	2021-11-24 09:54:11.521738651 +0100
+++ gcc/cp/typeck.c	2021-11-24 17:09:43.620628485 +0100
@@ -6780,16 +6780,6 @@ cp_build_addr_expr_1 (tree arg, bool str
 	    return error_mark_node;
 	  }
 
-	if (TREE_CODE (t) == FUNCTION_DECL
-	    && DECL_IMMEDIATE_FUNCTION_P (t)
-	    && !in_immediate_context ())
-	  {
-	    if (complain & tf_error)
-	      error_at (loc, "taking address of an immediate function %qD",
-			t);
-	    return error_mark_node;
-	  }
-
 	type = build_ptrmem_type (context_for_name_lookup (t),
 				  TREE_TYPE (t));
 	t = make_ptrmem_cst (type, t);
@@ -6816,15 +6806,6 @@ cp_build_addr_expr_1 (tree arg, bool str
     {
       tree stripped_arg = tree_strip_any_location_wrapper (arg);
       if (TREE_CODE (stripped_arg) == FUNCTION_DECL
-	  && DECL_IMMEDIATE_FUNCTION_P (stripped_arg)
-	  && !in_immediate_context ())
-	{
-	  if (complain & tf_error)
-	    error_at (loc, "taking address of an immediate function %qD",
-		      stripped_arg);
-	  return error_mark_node;
-	}
-      if (TREE_CODE (stripped_arg) == FUNCTION_DECL
 	  && !mark_used (stripped_arg, complain) && !(complain & tf_error))
 	return error_mark_node;
       val = build_address (arg);
@@ -6865,6 +6846,13 @@ cp_build_addr_expr_1 (tree arg, bool str
 			      complain);
     }
 
+  /* For addresses of immediate functions ensure we have EXPR_LOCATION
+     set for possible later diagnostics.  */
+  if (TREE_CODE (val) == ADDR_EXPR
+      && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL
+      && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val, 0)))
+    SET_EXPR_LOCATION (val, input_location);
+
   return val;
 }
 
--- gcc/cp/pt.c.jj	2021-11-24 15:05:23.336928234 +0100
+++ gcc/cp/pt.c	2021-11-24 15:34:29.018014159 +0100
@@ -17012,6 +17012,12 @@ tsubst_copy (tree t, tree args, tsubst_f
 	r = build1 (code, type, op0);
 	if (code == ALIGNOF_EXPR)
 	  ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
+	/* For addresses of immediate functions ensure we have EXPR_LOCATION
+	   set for possible later diagnostics.  */
+	if (code == ADDR_EXPR
+	    && TREE_CODE (op0) == FUNCTION_DECL
+	    && DECL_IMMEDIATE_FUNCTION_P (op0))
+	  SET_EXPR_LOCATION (r, input_location);
 	return r;
       }
 
--- gcc/cp/cp-gimplify.c.jj	2021-11-19 10:04:54.343314733 +0100
+++ gcc/cp/cp-gimplify.c	2021-11-24 18:39:46.080233582 +0100
@@ -900,8 +900,58 @@ struct cp_genericize_data
 static tree
 cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data)
 {
-  tree stmt;
-  enum tree_code code;
+  tree stmt = *stmt_p;
+  enum tree_code code = TREE_CODE (stmt);
+
+  switch (code)
+    {
+    case PTRMEM_CST:
+      if (TREE_CODE (PTRMEM_CST_MEMBER (stmt)) == FUNCTION_DECL
+	  && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (stmt)))
+	{
+	  if (!((hash_set<tree> *) data)->add (stmt))
+	    error_at (PTRMEM_CST_LOCATION (stmt),
+		      "taking address of an immediate function %qD",
+		      PTRMEM_CST_MEMBER (stmt));
+	  stmt = *stmt_p = build_zero_cst (TREE_TYPE (stmt));
+	  break;
+	}
+      break;
+
+    case ADDR_EXPR:
+      if (TREE_CODE (TREE_OPERAND (stmt, 0)) == FUNCTION_DECL
+	  && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (stmt, 0)))
+	{
+	  /* Direct calls to std::source_location::current() are
+	     only evaluated during genericization, so we need
+	     to make an exception for them.  But we shouldn't
+	     allow those outside of direct calls.  */
+	  if (source_location_current_p (TREE_OPERAND (stmt, 0))
+	      && ((hash_set<tree> *) data)->contains (stmt))
+	    break;
+	  error_at (EXPR_LOCATION (stmt),
+		    "taking address of an immediate function %qD",
+		    TREE_OPERAND (stmt, 0));
+	  stmt = *stmt_p = build_zero_cst (TREE_TYPE (stmt));
+	  break;
+	}
+      break;
+
+    case CALL_EXPR:
+      if (tree fndecl = cp_get_callee_fndecl_nofold (stmt))
+	if (DECL_IMMEDIATE_FUNCTION_P (fndecl)
+	    && source_location_current_p (fndecl))
+	  {
+	    tree fn = cp_get_callee (stmt);
+	    STRIP_NOPS (fn);
+	    if (TREE_CODE (fn) == ADDR_EXPR)
+	      ((hash_set<tree> *) data)->add (fn);
+	  }
+      break;
+
+    default:
+      break;
+    }
 
   *stmt_p = stmt = cp_fold (*stmt_p);
 
@@ -917,12 +967,16 @@ cp_fold_r (tree *stmt_p, int *walk_subtr
     }
 
   code = TREE_CODE (stmt);
-  if (code == OMP_FOR || code == OMP_SIMD || code == OMP_DISTRIBUTE
-      || code == OMP_LOOP || code == OMP_TASKLOOP || code == OACC_LOOP)
+  switch (code)
     {
       tree x;
       int i, n;
-
+    case OMP_FOR:
+    case OMP_SIMD:
+    case OMP_DISTRIBUTE:
+    case OMP_LOOP:
+    case OMP_TASKLOOP:
+    case OACC_LOOP:
       cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL);
       cp_walk_tree (&OMP_FOR_CLAUSES (stmt), cp_fold_r, data, NULL);
       cp_walk_tree (&OMP_FOR_INIT (stmt), cp_fold_r, data, NULL);
@@ -961,6 +1015,22 @@ cp_fold_r (tree *stmt_p, int *walk_subtr
 	}
       cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_fold_r, data, NULL);
       *walk_subtrees = 0;
+      return NULL;
+
+    case IF_STMT:
+      if (IF_STMT_CONSTEVAL_P (stmt))
+	{
+	  /* Don't walk THEN_CLAUSE (stmt) for consteval if.  IF_COND is always
+	     boolean_false_node.  */
+	  cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_r, data, NULL);
+	  cp_walk_tree (&IF_SCOPE (stmt), cp_fold_r, data, NULL);
+	  *walk_subtrees = 0;
+	  return NULL;
+	}
+      break;
+
+    default:
+      break;
     }
 
   return NULL;
@@ -1477,9 +1547,9 @@ cp_genericize_r (tree *stmt_p, int *walk
 	}
 
       if (tree fndecl = cp_get_callee_fndecl_nofold (stmt))
-	if (DECL_IMMEDIATE_FUNCTION_P (fndecl))
+	if (DECL_IMMEDIATE_FUNCTION_P (fndecl)
+	    && source_location_current_p (fndecl))
 	  {
-	    gcc_assert (source_location_current_p (fndecl));
 	    *stmt_p = cxx_constant_value (stmt);
 	    break;
 	  }
--- gcc/testsuite/g++.dg/cpp2a/consteval20.C.jj	2021-10-27 09:03:07.576043195 +0200
+++ gcc/testsuite/g++.dg/cpp2a/consteval20.C	2021-11-24 16:49:40.481679281 +0100
@@ -10,10 +10,14 @@ constexpr S s;
 int
 bar ()
 {
+  auto c = &S::foo;			// { dg-error "taking address of an immediate function" }
+  constexpr auto d = &S::foo;		// { dg-error "taking address of an immediate function" }
+  static auto e = &S::foo;		// { dg-error "taking address of an immediate function" }
   return (s.*&S::foo) ();		// { dg-error "taking address of an immediate function" }
 }
 
 constexpr auto a = &S::foo;		// { dg-error "taking address of an immediate function" }
+auto b = &S::foo;			// { dg-error "taking address of an immediate function" }
 
 consteval int
 baz ()
--- gcc/testsuite/g++.dg/cpp2a/consteval23.C.jj	2021-10-27 09:03:07.576043195 +0200
+++ gcc/testsuite/g++.dg/cpp2a/consteval23.C	2021-11-24 18:49:34.721877913 +0100
@@ -2,6 +2,7 @@
 // { dg-do compile { target c++20 } }
 
 consteval int foo () { return 42; }
+constexpr auto baz (int (*fn) ()) { return fn; }
 
 consteval int
 bar (int (*fn) () = foo)
@@ -11,3 +12,6 @@ bar (int (*fn) () = foo)
 
 static_assert (bar () == 42);
 static_assert (bar (foo) == 42);
+static_assert (bar (&foo) == 42);
+static_assert (bar (baz (foo)) == 42);
+static_assert (bar (baz (&foo)) == 42);
--- gcc/testsuite/g++.dg/cpp2a/consteval25.C.jj	2021-11-24 18:49:28.321968743 +0100
+++ gcc/testsuite/g++.dg/cpp2a/consteval25.C	2021-11-24 18:50:41.790926102 +0100
@@ -0,0 +1,17 @@
+// PR c++/102753
+// { dg-do compile { target c++20 } }
+// { dg-options "" }
+
+consteval int foo () { return 42; }
+
+consteval int
+bar (int (*fn) ())
+{
+  return fn ();
+}
+
+void
+baz ()
+{
+  static_assert (bar (({ constexpr auto a = 1; foo; })) == 42);
+}
--- gcc/testsuite/g++.dg/cpp2a/srcloc20.C.jj	2021-11-24 18:45:35.810268971 +0100
+++ gcc/testsuite/g++.dg/cpp2a/srcloc20.C	2021-11-24 18:48:01.280204003 +0100
@@ -0,0 +1,44 @@
+// { dg-do compile { target c++20 } }
+
+namespace std {
+  struct source_location {
+    struct __impl {
+      const char *_M_file_name;
+      const char *_M_function_name;
+      unsigned int _M_line, _M_column;
+    };
+    const __impl *__ptr;
+    constexpr source_location () : __ptr (nullptr) {}
+    static consteval source_location
+    current (const void *__p = __builtin_source_location ()) {
+      source_location __ret;
+      __ret.__ptr = static_cast <const __impl *> (__p);
+      return __ret;
+    }
+    constexpr const char *file_name () const {
+      return __ptr ? __ptr->_M_file_name : "";
+    }
+    constexpr const char *function_name () const {
+      return __ptr ? __ptr->_M_function_name : "";
+    }
+    constexpr unsigned line () const {
+      return __ptr ? __ptr->_M_line : 0;
+    }
+    constexpr unsigned column () const {
+      return __ptr ? __ptr->_M_column : 0;
+    }
+  };
+}
+
+using namespace std;
+
+auto a = source_location::current;		// { dg-error "taking address of an immediate function" }
+constexpr auto b = &source_location::current;	// { dg-error "taking address of an immediate function" }
+
+void
+foo ()
+{
+  auto c = &source_location::current;		// { dg-error "taking address of an immediate function" }
+  constexpr auto d = source_location::current;	// { dg-error "taking address of an immediate function" }
+  static auto e = source_location::current;	// { dg-error "taking address of an immediate function" }
+}


	Jakub



More information about the Gcc-patches mailing list