]>
Commit | Line | Data |
---|---|---|
28bf4b0b | 1 | /* |
2 | ** LCLint - annotation-assisted static program checker | |
3 | ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu | |
21 | ** To report a bug: lclint-bug@cs.virginia.edu | |
22 | ** For more information: http://lclint.cs.virginia.edu | |
23 | */ | |
24 | /* | |
25 | ** stateCombinationTable.c | |
26 | ** | |
27 | ** A stateCombinationTable is a mapping from keys to value tables. | |
28 | */ | |
29 | ||
30 | # include "lclintMacros.nf" | |
31 | # include "llbasic.h" | |
32 | ||
33 | /* | |
34 | ** (key, value, value) => value | |
35 | */ | |
36 | ||
37 | static stateEntry stateEntry_create (void) | |
38 | { | |
39 | stateEntry res = (stateEntry) dmalloc (sizeof (*res)); | |
40 | res->value = 0; | |
41 | res->msg = cstring_undefined; | |
42 | return res; | |
43 | } | |
44 | ||
45 | static cstring stateEntry_unparse (stateEntry e) | |
46 | { | |
47 | if (cstring_isDefined (e->msg)) | |
48 | { | |
49 | return message ("[%d: %s]", e->value, e->msg); | |
50 | } | |
51 | else | |
52 | { | |
53 | return message ("%d", e->value); | |
54 | } | |
55 | } | |
56 | ||
57 | stateCombinationTable stateCombinationTable_create (int size) | |
58 | { | |
59 | stateCombinationTable res = (stateCombinationTable) dmalloc (sizeof (*res)); | |
60 | int i; | |
61 | ||
62 | res->size = size; | |
63 | res->rows = (stateRow *) dmalloc (sizeof (*(res->rows)) * size); | |
64 | ||
65 | for (i = 0; i < size; i++) | |
66 | { | |
67 | int j; | |
68 | ||
69 | res->rows[i] = (stateRow) dmalloc (sizeof (*res->rows[i])); | |
70 | res->rows[i]->size = size; | |
71 | ||
72 | /* Rows have an extra entry (for lose ref transfers) */ | |
73 | res->rows[i]->entries = (stateEntry *) | |
74 | dmalloc (sizeof (*res->rows[i]->entries) * (size + 1)); | |
75 | ||
76 | for (j = 0; j < size + 1; j++) | |
77 | { | |
78 | stateEntry s = stateEntry_create (); | |
79 | ||
80 | /* Default transfer changes no state and is permitted without error. */ | |
81 | ||
82 | s->value = i; | |
83 | llassert (cstring_isUndefined (s->msg)); | |
84 | ||
85 | res->rows[i]->entries[j] = s; | |
86 | } | |
87 | } | |
88 | ||
89 | /*@i32@*/ return res; | |
90 | } | |
91 | ||
92 | cstring stateCombinationTable_unparse (stateCombinationTable t) | |
93 | { | |
94 | int i; | |
95 | cstring res = cstring_newEmpty (); | |
96 | ||
97 | for (i = 0; i < t->size; i++) | |
98 | { | |
99 | int j; | |
100 | ||
101 | for (j = 0; j < (t->size + 1); j++) | |
102 | { | |
103 | if (j == 0) | |
104 | { | |
105 | res = message ("%q[%d: ] %q", res, i, | |
106 | stateEntry_unparse (t->rows[i]->entries[j])); | |
107 | } | |
108 | else | |
109 | { | |
110 | res = message ("%q . %q", res, | |
111 | stateEntry_unparse (t->rows[i]->entries[j])); | |
112 | } | |
113 | } | |
114 | ||
115 | res = cstring_appendChar (res, '\n'); | |
116 | } | |
117 | ||
118 | return res; | |
119 | } | |
120 | ||
121 | static void stateEntry_free (/*@only@*/ stateEntry s) | |
122 | { | |
123 | cstring_free (s->msg); | |
124 | sfree (s); | |
125 | } | |
126 | ||
127 | static void stateRow_free (/*@only@*/ stateRow r) | |
128 | { | |
129 | int i; | |
130 | ||
131 | for (i = 0; i < r->size + 1; i++) { | |
132 | /*@i32@*/ stateEntry_free (r->entries[i]); | |
133 | } | |
134 | ||
135 | sfree (r->entries); | |
136 | sfree (r); | |
137 | } | |
138 | ||
139 | void stateCombinationTable_free (/*@only@*/ stateCombinationTable t) | |
140 | { | |
141 | int i; | |
142 | ||
143 | for (i = 0; i < t->size; i++) { | |
144 | /*@i32@*/ stateRow_free (t->rows[i]); | |
145 | } | |
146 | ||
147 | sfree (t->rows); | |
148 | sfree (t); | |
149 | } | |
150 | ||
151 | static /*@exposed@*/ stateEntry | |
152 | stateCombintationTable_getEntry (stateCombinationTable h, | |
153 | int rkey, int ckey) | |
154 | { | |
155 | llassert (rkey < h->size); | |
156 | llassert (ckey < h->size + 1); | |
157 | ||
158 | return h->rows[rkey]->entries[ckey]; | |
159 | } | |
160 | ||
161 | void stateCombinationTable_set (stateCombinationTable h, | |
162 | int p_from, int p_to, | |
163 | int value, cstring msg) | |
164 | { | |
165 | stateEntry entry = stateCombintationTable_getEntry (h, p_from, p_to); | |
166 | ||
167 | llassert (entry != NULL); | |
168 | ||
169 | entry->value = value; | |
170 | llassert (cstring_isUndefined (entry->msg)); | |
171 | entry->msg = msg; | |
172 | ||
173 | DPRINTF (("Set entry: [%p] %d / %d => %s", entry, | |
174 | p_from, p_to, cstring_toCharsSafe (msg))); | |
175 | ||
176 | } | |
177 | ||
178 | /* | |
179 | ** Like set, but may already have value. | |
180 | ** (Only different is error checking.) | |
181 | */ | |
182 | ||
183 | void stateCombinationTable_update (stateCombinationTable h, | |
184 | int p_from, int p_to, | |
185 | int value, cstring msg) | |
186 | { | |
187 | stateEntry entry = stateCombintationTable_getEntry (h, p_from, p_to); | |
188 | ||
189 | llassert (entry != NULL); | |
190 | ||
191 | entry->value = value; | |
192 | cstring_free (entry->msg); | |
193 | entry->msg = msg; | |
194 | ||
195 | DPRINTF (("Update entry: [%p] %d / %d => %s", entry, | |
196 | p_from, p_to, cstring_toCharsSafe (msg))); | |
197 | } | |
198 | ||
199 | int stateCombinationTable_lookup (stateCombinationTable h, int p_from, int p_to, /*@out@*/ cstring *msg) | |
200 | { | |
201 | stateEntry entry; | |
202 | llassert (p_from != stateValue_error); | |
203 | llassert (p_to != stateValue_error); | |
204 | ||
205 | entry = stateCombintationTable_getEntry (h, p_from, p_to); | |
206 | llassert (entry != NULL); | |
207 | ||
208 | DPRINTF (("Lookup: %d / %d => %s [%p]", | |
209 | p_from, p_to, cstring_toCharsSafe (entry->msg), entry)); | |
210 | ||
211 | /*@i32@*/ *msg = entry->msg; | |
212 | return entry->value; | |
213 | } | |
214 | ||
215 | extern int | |
216 | stateCombinationTable_lookupLoseReference (stateCombinationTable h, int from, | |
217 | /*@out@*/ /*@observer@*/ cstring *msg) | |
218 | { | |
219 | return stateCombinationTable_lookup (h, from, h->size, msg); | |
220 | } | |
221 | ||
222 | ||
223 | ||
224 | ||
225 | ||
226 |