From 68b0447425a91665be609906c7722b9f036b56db Mon Sep 17 00:00:00 2001
From: Costas Argyris <costas.argyris@gmail.com>
Date: Sat, 2 Dec 2023 17:39:13 +0000
Subject: [PATCH] driver: Call finalize method from main.

The driver::finalize() method frees up memory.

Signed-off-by: Costas Argyris <costas.argyris@gmail.com>
---
 gcc/gcc-main.cc |  7 ++++---
 gcc/gcc.cc      | 10 ++++++----
 gcc/gcc.h       |  3 ++-
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/gcc/gcc-main.cc b/gcc/gcc-main.cc
index 66588f50276..fa63a8f47d4 100644
--- a/gcc/gcc-main.cc
+++ b/gcc/gcc-main.cc
@@ -41,8 +41,9 @@ extern int main (int, char **);
 int
 main (int argc, char **argv)
 {
-  driver d (false, /* can_finalize */
+  driver d (false, /* can_restore_env */
 	    false); /* debug */
-
-  return d.main (argc, argv);
+  int ret = d.main (argc, argv);
+  d.finalize();
+  return ret;
 }
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 9f21ad9453e..e16f9208f90 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -8231,11 +8231,12 @@ compare_files (char *cmpfile[])
   return ret;
 }
 
-driver::driver (bool can_finalize, bool debug) :
+driver::driver (bool can_restore_env, bool debug) :
   explicit_link_files (NULL),
-  decoded_options (NULL)
+  decoded_options (NULL),
+  can_restore_env (can_restore_env)
 {
-  env.init (can_finalize, debug);
+  env.init (can_restore_env, debug);
 }
 
 driver::~driver ()
@@ -11208,7 +11209,8 @@ quote_spec_arg (char *orig)
 void
 driver::finalize ()
 {
-  env.restore ();
+  if (can_restore_env)
+    env.restore ();
   diagnostic_finish (global_dc);
 
   is_cpp_driver = 0;
diff --git a/gcc/gcc.h b/gcc/gcc.h
index 19a61b373ee..e377f2be741 100644
--- a/gcc/gcc.h
+++ b/gcc/gcc.h
@@ -30,7 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 class driver
 {
  public:
-  driver (bool can_finalize, bool debug);
+  driver (bool can_restore_env, bool debug);
   ~driver ();
   int main (int argc, char **argv);
   void finalize ();
@@ -59,6 +59,7 @@ class driver
   struct cl_decoded_option *decoded_options;
   unsigned int decoded_options_count;
   option_proposer m_option_proposer;
+  bool can_restore_env;
 };
 
 /* The mapping of a spec function name to the C function that
-- 
2.30.2

