This patch removes memset from m2spellcheck_InitCandidates.
It corrects a comment boiler plate and removes an unused local
variable.  Finally it frees up memory used by the candidates_array
in KillCandidates.

gcc/m2/ChangeLog:

        PR modula2/122333
        * gm2-compiler/M2MetaError.mod (JoinSentances): Remove
        unused variable.
        * gm2-gcc/m2spellcheck.cc (m2spellcheck_InitCandidates): Rewrite.
        (KillCandidates): Deallocate auto_vec candidates_array.
        (candidates_array_vec_t): New declaration.

Signed-off-by: Gaius Mulley <[email protected]>
---
 gcc/m2/gm2-compiler/M2MetaError.mod |  2 --
 gcc/m2/gm2-gcc/m2spellcheck.cc      | 15 ++++++++++-----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/gcc/m2/gm2-compiler/M2MetaError.mod 
b/gcc/m2/gm2-compiler/M2MetaError.mod
index 0ae919636c2..dc14e6b06be 100644
--- a/gcc/m2/gm2-compiler/M2MetaError.mod
+++ b/gcc/m2/gm2-compiler/M2MetaError.mod
@@ -1894,8 +1894,6 @@ END IsPunct ;
 *)

 PROCEDURE JoinSentances (VAR eb: errorBlock; s: String) ;
-VAR
-   i: INTEGER ;
 BEGIN
    IF (s # NIL) AND (Length (s) > 0)
    THEN
diff --git a/gcc/m2/gm2-gcc/m2spellcheck.cc b/gcc/m2/gm2-gcc/m2spellcheck.cc
index 22b77ed843d..c6d0c675a09 100644
--- a/gcc/m2/gm2-gcc/m2spellcheck.cc
+++ b/gcc/m2/gm2-gcc/m2spellcheck.cc
@@ -1,4 +1,4 @@
-/* m2spellcheck.cc provides an interface to GCC expression trees.
+/* m2spellcheck.cc provides an interface to the GCC spell checker.

 Copyright (C) 2025 Free Software Foundation, Inc.
 Contributed by Gaius Mulley <[email protected]>.
@@ -34,9 +34,10 @@ along with GNU Modula-2; see the file COPYING3.  If not see


 /* Define the hidden type Candidates declared in the definition module.  */
+typedef auto_vec<const char *> candidates_array_vec_t;

 typedef struct Candidates_t {
-  auto_vec<const char *> candidates_array;
+  candidates_array_vec_t candidates_array;
   struct Candidates_t *next;
 } Candidates;

@@ -57,12 +58,13 @@ m2spellcheck_InitCandidates (void)
       c = freeList;
       freeList = freeList->next;
     }
-  memset (c, 0, sizeof (Candidates));
+  :: new (&c->candidates_array) auto_vec<const char *> ();
+  c->next = NULL;
   return c;
 }

 /* Push a string to the Candidates array.
-   The candidates array will contain str at the end.  */
+   The candidates array will contain the string name at the end.  */

 static
 void
@@ -80,12 +82,15 @@ m2spellcheck_Push (void *cand, const char *name)
   Push (static_cast<Candidates *> (cand), name);
 }

+/* Return the Candidates structure to the freeList and deallocate
+   the auto_vec candidates_array.  */
+
 static
 void
 KillCandidates (Candidates **cand)
 {
-  // --fixme-- deallocate and zero the candidates_array.
   (*cand)->next = freeList;
+  (*cand)->candidates_array.~candidates_array_vec_t ();
   freeList = *cand;
   (*cand) = NULL;
 }
--
2.39.5

Reply via email to