Line data Source code
1 : /* m2spellcheck.cc provides an interface to the GCC spell checker.
2 :
3 : Copyright (C) 2025-2026 Free Software Foundation, Inc.
4 : Contributed by Gaius Mulley <gaiusmod2@gmail.com>.
5 :
6 : This file is part of GNU Modula-2.
7 :
8 : GNU Modula-2 is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GNU Modula-2 is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GNU Modula-2; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "gcc-consolidation.h"
23 :
24 : #include "../gm2-lang.h"
25 : #include "../m2-tree.h"
26 :
27 : #define m2spellcheck_c
28 : #include "m2assert.h"
29 : #include "m2builtins.h"
30 : #include "m2convert.h"
31 : #include "m2decl.h"
32 : #include "m2expr.h"
33 : #include "m2spellcheck.h"
34 :
35 :
36 : /* Define the hidden type Candidates declared in the definition module. */
37 : typedef auto_vec<const char *> candidates_array_vec_t;
38 :
39 : typedef struct Candidates_t {
40 : candidates_array_vec_t candidates_array;
41 : struct Candidates_t *next;
42 : } Candidates;
43 :
44 :
45 : static Candidates *freeList = NULL;
46 :
47 :
48 : /* InitCandidates create an empty candidate array. */
49 :
50 : void *
51 230 : m2spellcheck_InitCandidates (void)
52 : {
53 230 : Candidates *c = NULL;
54 230 : if (freeList == NULL)
55 175 : c = (Candidates *) xmalloc (sizeof (Candidates));
56 : else
57 : {
58 55 : c = freeList;
59 55 : freeList = freeList->next;
60 : }
61 230 : :: new (&c->candidates_array) auto_vec<const char *> ();
62 230 : c->next = NULL;
63 230 : return c;
64 : }
65 :
66 : /* Push a string to the Candidates array.
67 : The candidates array will contain the string name at the end. */
68 :
69 : static
70 : void
71 3127 : Push (Candidates *cand, const char *name)
72 : {
73 0 : cand->candidates_array.safe_push (name);
74 0 : }
75 :
76 : /* Push a string to the Candidates array.
77 : The candidates array will contain str at the end. */
78 :
79 : void
80 3127 : m2spellcheck_Push (void *cand, const char *name)
81 : {
82 3127 : Push (static_cast<Candidates *> (cand), name);
83 3127 : }
84 :
85 : /* Return the Candidates structure to the freeList and deallocate
86 : the auto_vec candidates_array. */
87 :
88 : static
89 : void
90 230 : KillCandidates (Candidates **cand)
91 : {
92 230 : (*cand)->next = freeList;
93 230 : (*cand)->candidates_array.~candidates_array_vec_t ();
94 230 : freeList = *cand;
95 230 : (*cand) = NULL;
96 230 : }
97 :
98 : /* KillCandidates deallocates the candidates array and set (*cand) to NULL.
99 : (*cand) is placed into the m2spellcheck module freeList. */
100 :
101 : void
102 230 : m2spellcheck_KillCandidates (void **cand)
103 : {
104 230 : KillCandidates (reinterpret_cast<Candidates **> (cand));
105 230 : }
106 :
107 : /* FindClosestCharStar return the closest match to name found within
108 : the candidates_array. NULL is returned if no close match is found. */
109 :
110 : const char*
111 230 : FindClosestCharStar (Candidates *cand, const char *name)
112 : {
113 230 : return find_closest_string (name, &cand->candidates_array);
114 : }
115 :
116 : const char*
117 230 : m2spellcheck_FindClosestCharStar (void *cand, const char *name)
118 : {
119 230 : return FindClosestCharStar (static_cast<Candidates *> (cand),
120 230 : name);
121 : }
|