Dear all,

while looking at the recent testcase gfortran.dg/pr115070.f90 with f951
running under valgrind, I noticed minor front-end memleaks of gfc_expr's
that are probably fallout from a code refactoring, which are fixed by
the attached.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald

From 87aaaf3b8614730d0f7ccfe29ee36f4921cf48d2 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Tue, 29 Oct 2024 21:52:27 +0100
Subject: [PATCH] Fortran: fix several front-end memleaks

gcc/fortran/ChangeLog:

	* trans-expr.cc (gfc_trans_class_init_assign): Free intermediate
	gfc_expr's.
	* trans.cc (get_final_proc_ref): Likewise.
	(get_elem_size): Likewise.
	(gfc_add_finalizer_call): Likewise.
---
 gcc/fortran/trans-expr.cc | 2 ++
 gcc/fortran/trans.cc      | 5 +++++
 2 files changed, 7 insertions(+)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index ff8cde93ef4..ddbb5ecf068 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -1890,6 +1890,8 @@ gfc_trans_class_init_assign (gfc_code *code)
     }

   gfc_add_expr_to_block (&block, tmp);
+  gfc_free_expr (lhs);
+  gfc_free_expr (rhs);

   return gfc_finish_block (&block);
 }
diff --git a/gcc/fortran/trans.cc b/gcc/fortran/trans.cc
index 58b93e233a1..1a0ba637058 100644
--- a/gcc/fortran/trans.cc
+++ b/gcc/fortran/trans.cc
@@ -1128,6 +1128,9 @@ get_final_proc_ref (gfc_se *se, gfc_expr *expr, tree class_container)

   if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
+
+  if (expr->ts.type != BT_DERIVED && !using_class_container)
+    gfc_free_expr (final_wrapper);
 }


@@ -1155,6 +1158,7 @@ get_elem_size (gfc_se *se, gfc_expr *expr, tree class_container)

       gfc_conv_expr (se, class_size);
       gcc_assert (se->post.head == NULL_TREE);
+      gfc_free_expr (class_size);
     }
 }

@@ -1467,6 +1471,7 @@ gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2,

   gfc_add_expr_to_block (block, tmp);
   gfc_add_block_to_block (block, &final_se.post);
+  gfc_free_expr (expr);

   return true;
 }
--
2.35.3

Reply via email to