public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [FYI] c++: check for trying to cache non-constant expressions
Date: Mon, 17 Jul 2023 19:57:11 -0400	[thread overview]
Message-ID: <20230717235711.972199-1-jason@redhat.com> (raw)
In-Reply-To: <20230717211808.946183-1-jason@redhat.com>

Just for reference, not applying.

-- 8< --

This is what I used to check for what non-constant results we were
previously caching.  After the previous two patches it's just address
of a local and a partially-uninitialized COMPLEX_EXPR.

gcc/cp/ChangeLog:

	* constexpr.cc (cxx_eval_call_expression): Add checking.
---
 gcc/cp/constexpr.cc | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 6e8f1c2b61e..ed4e6a3acf9 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -3206,6 +3206,11 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
 		      cacheable = false;
 		      break;
 		    }
+	      /* Also don't cache a call that returns a deallocated pointer.  */
+	      if (cacheable && CHECKING_P
+		  && (cp_walk_tree_without_duplicates
+		      (&result, find_heap_var_refs, NULL)))
+		cacheable = false;
 	    }
 
 	    /* Rewrite all occurrences of the function's RESULT_DECL with the
@@ -3215,10 +3220,6 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
 		&& !is_empty_class (TREE_TYPE (res)))
 	      if (replace_decl (&result, res, ctx->object))
 		cacheable = false;
-
-	  /* Only cache a permitted result of a constant expression.  */
-	  if (cacheable && !reduced_constant_expression_p (result))
-	    cacheable = false;
 	}
       else
 	/* Couldn't get a function copy to evaluate.  */
@@ -3230,6 +3231,18 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
 	result = error_mark_node;
       else if (!result)
 	result = void_node;
+
+      /* Only cache a permitted result of a constant expression.  */
+      if (cacheable && !reduced_constant_expression_p (result))
+	{
+	  cacheable = false;
+	  if (CHECKING_P
+	      && result != void_node && result != error_mark_node
+	      && !(TREE_CODE (result) == CONSTRUCTOR
+		   && CONSTRUCTOR_NO_CLEARING (result)))
+	    internal_error ("caching %qE", result);
+	}
+
       if (entry)
 	entry->result = cacheable ? result : error_mark_node;
     }

base-commit: c7ac1de5f5c561b2d90c084a638c232d322d54e6
-- 
2.39.3


      reply	other threads:[~2023-07-17 23:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-17 21:18 [pushed] c++: only cache constexpr calls that are constant exprs Jason Merrill
2023-07-17 23:57 ` Jason Merrill [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230717235711.972199-1-jason@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).