]> andersk Git - splint.git/blob - src/lclsyntable.c
e3e038a0d8449346164218246100c019b040baf2
[splint.git] / src / lclsyntable.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 ** 
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** General Public License for more details.
15 ** 
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
19 **
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
23 */
24 /*
25 ** syntable.c
26 **
27 ** Larch/C Interface language synonym table
28 **
29 **      This table stores synonyms for the Larch/C Interface Language.  It is
30 **      essentially an array of token-handles indexed by string-handles.
31 **      Therefore, synonyms (strings) can be converted to the actual token.
32 **
33 **  AUTHORS:
34 **      J.P. Wild
35 **
36 **
37 **  CREATION DATE:  90.08.10
38 */
39
40 # include "splintMacros.nf"
41 # include "basic.h"
42 # include "lcltokentable.h"
43 # include "lclsyntable.h"
44
45 static long unsigned MaxSyn;    /* size of SynTable[]              */
46 static /*@only@*/ /*@reldef@*/ /*@null@*/ lsymbol *SynTable;
47 static void
48   AllocSynTable (void)
49   /*@globals SynTable, MaxSyn@*/
50   /*@modifies *SynTable, MaxSyn@*/;
51
52 void
53 LCLAddSyn (lsymbol ntok, lsymbol otok)
54 {
55   while (otok >= MaxSyn)
56     {
57       /* No more space available.  Allocate more. */
58       AllocSynTable ();
59     }
60
61   llassert (SynTable != NULL);
62
63   if (SynTable[ntok] == 0)
64     {
65      /* Entry is empty. Fill it in. */
66       SynTable[ntok] = otok;
67
68       /* Mark oldToken as having a synonym. */
69       LCLSetTokenHasSyn (otok, TRUE);
70     }
71   else
72     {
73       llbuglit ("LCLAddSyn: invalid argument");
74     }
75 }
76
77 /*@exposed@*/ ltoken
78 LCLGetTokenForSyn (lsymbol ntok)
79 {
80   llassert (SynTable != NULL);
81
82   if (!((ntok < MaxSyn) || (SynTable[ntok] != 0)))
83     llbuglit ("LCLGetSyn: bad argument");
84
85   return LCLGetToken (SynTable[ntok]);
86 }
87
88 bool
89 LCLIsSyn (lsymbol str)
90 {
91   if (MaxSyn == 0)
92     {
93       return FALSE;
94     }
95   else
96     {
97       llassert (SynTable != NULL);
98
99       if (str < MaxSyn)
100         {
101           /* Check for synonym entry in table. */
102           return (SynTable[str] != 0);
103         }
104       else
105         {
106           /* No token for synonym.  Return FALSE. */
107           return FALSE;
108         }
109     }
110 }
111   
112 static void
113 AllocSynTable (void) /*@globals SynTable; @*/
114 {
115   long unsigned newSize, oldSize;
116   long unsigned int i;
117
118   oldSize = MaxSyn;
119
120   if (oldSize == 0)
121     {
122       /* First time SynTable allocated.  Set initial size. */
123       newSize = INITSYNTABLE;
124       SynTable = (lsymbol *) dmalloc (newSize * sizeof (*SynTable));
125     }
126   else
127     {
128       lsymbol *oldSynTable = SynTable; 
129
130       llassert (oldSynTable != NULL);
131
132       /* Synonym table already allocated.  Calulate extension size. */
133       newSize = (unsigned long) (DELTASYNTABLE * oldSize);
134       SynTable = (lsymbol *) dmalloc (newSize * sizeof (*SynTable));
135
136       for (i = 0; i < oldSize; i++)
137         {
138           SynTable[i] = oldSynTable[i];
139         }
140       
141       sfree (oldSynTable);
142     }
143
144   /* Zero out new allocated space.  Need to detect when cells are empty     */
145   /* and do this by checking that SynTable[x] == 0.                         */
146
147   /* ###  Should the "for" loop be replaced with the following?             */
148   /* #if VMS                                                                */
149   /* # include <string.h>;                                                  */
150   /* #else                                                                  */
151   /* # include <memory.h>;                                                  */
152   /*                                                                        */
153   /* memset (SynTable[oldSize], 0,                                          */
154   /*          (newSize - oldSize) * sizeof (*SynTable));                            */
155   
156   for (i = oldSize; i < newSize; i++)
157     {
158       SynTable[i] = 0;
159     }
160
161   MaxSyn = newSize;
162 }
163
164
165 void
166 LCLSynTableInit (void)
167 {
168   MaxSyn = 0;
169 }
170
171 void
172 LCLSynTableReset (void)
173 {
174 }
175
176 void
177 LCLSynTableCleanup (void)
178 {
179   sfree (SynTable);
180   SynTable = NULL;
181 }
This page took 0.350124 seconds and 3 git commands to generate.