public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/redhat/heads/gcc-9-branch)] c++: Fix cast to pointer to VLA.
@ 2020-03-17 19:18 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2020-03-17 19:18 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f137a7c6b122e524294fb792bb97d5f3b0600c4f

commit f137a7c6b122e524294fb792bb97d5f3b0600c4f
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Mar 2 14:42:47 2020 -0500

    c++: Fix cast to pointer to VLA.
    
    The C front-end fixed this issue in r257620 by adding a DECL_EXPR from
    grokdeclarator.  We don't have an easy way to do that in the C++ front-end,
    but it works fine to create and prepend a DECL_EXPR when we are genericizing
    the NOP_EXPR for the cast.
    
    The C patch wraps the DECL_EXPR in a BIND_EXPR, but that seems unnecessary
    in C++; this is just a hook to run gimplify_type_sizes, we aren't actually
    declaring anything that we need to worry about scoping for.
    
    gcc/cp/ChangeLog
    2020-03-02  Jason Merrill  <jason@redhat.com>
    
            PR c++/88256
            * cp-gimplify.c (predeclare_vla): New.
            (cp_genericize_r) [NOP_EXPR]: Call it.

Diff:
---
 gcc/cp/ChangeLog                                   |  6 +++++
 gcc/cp/cp-gimplify.c                               | 31 ++++++++++++++++++++++
 .../compile => c-c++-common}/pr84305.c             |  2 ++
 3 files changed, 39 insertions(+)

diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index bf1791d11a5..5a908241c6a 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2020-03-02  Jason Merrill  <jason@redhat.com>
+
+	PR c++/88256
+	* cp-gimplify.c (predeclare_vla): New.
+	(cp_genericize_r) [NOP_EXPR]: Call it.
+
 2020-03-02  Jason Merrill  <jason@redhat.com>
 
 	PR c++/93442
diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 90a315003d4..4be45abca88 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -1131,6 +1131,36 @@ cp_fold_function (tree fndecl)
   cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &pset, NULL);
 }
 
+/* If EXPR involves an anonymous VLA type, prepend a DECL_EXPR for that type
+   to trigger gimplify_type_sizes; otherwise a cast to pointer-to-VLA confuses
+   the middle-end (c++/88256).  */
+
+static tree
+predeclare_vla (tree expr)
+{
+  tree type = TREE_TYPE (expr);
+  if (type == error_mark_node)
+    return expr;
+
+  /* We need to strip pointers for gimplify_type_sizes.  */
+  tree vla = type;
+  while (POINTER_TYPE_P (vla))
+    {
+      if (TYPE_NAME (vla))
+	return expr;
+      vla = TREE_TYPE (vla);
+    }
+  if (TYPE_NAME (vla) || !variably_modified_type_p (vla, NULL_TREE))
+    return expr;
+
+  tree decl = build_decl (input_location, TYPE_DECL, NULL_TREE, vla);
+  DECL_ARTIFICIAL (decl) = 1;
+  TYPE_NAME (vla) = decl;
+  tree dexp = build_stmt (input_location, DECL_EXPR, decl);
+  expr = build2 (COMPOUND_EXPR, type, dexp, expr);
+  return expr;
+}
+
 /* Perform any pre-gimplification lowering of C++ front end trees to
    GENERIC.  */
 
@@ -1584,6 +1614,7 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
       break;
 
     case NOP_EXPR:
+      *stmt_p = predeclare_vla (*stmt_p);
       if (!wtd->no_sanitize_p
 	  && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)
 	  && TYPE_REF_P (TREE_TYPE (stmt)))
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr84305.c b/gcc/testsuite/c-c++-common/pr84305.c
similarity index 78%
rename from gcc/testsuite/gcc.c-torture/compile/pr84305.c
rename to gcc/testsuite/c-c++-common/pr84305.c
index 374fa67f593..27150dd5328 100644
--- a/gcc/testsuite/gcc.c-torture/compile/pr84305.c
+++ b/gcc/testsuite/c-c++-common/pr84305.c
@@ -1,3 +1,5 @@
+// { dg-additional-options -O3 }
+
 int res, a, b;
 void *foo;
 static void f2 (int arg) { res = ((int (*)[arg][b]) foo)[0][0][0]; }


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

only message in thread, other threads:[~2020-03-17 19:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 19:18 [gcc(refs/vendors/redhat/heads/gcc-9-branch)] c++: Fix cast to pointer to VLA 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).