]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ** Splint - annotation-assisted static program checker | |
3 | ** Copyright (C) 1994-2003 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 | ** filelocList.c (from slist_template.c) | |
26 | */ | |
27 | ||
28 | # include "splintMacros.nf" | |
29 | # include "basic.h" | |
30 | # include "filelocList.h" | |
31 | ||
32 | /* | |
33 | ** Invariant: If any member of the list is fileloc_undefined, then | |
34 | ** the 0th member is fileloc_undefined. | |
35 | */ | |
36 | ||
37 | filelocList | |
38 | filelocList_new () | |
39 | { | |
40 | return (filelocList_undefined); | |
41 | } | |
42 | ||
43 | static /*@notnull@*/ /*@only@*/ filelocList | |
44 | filelocList_newEmpty (void) | |
45 | { | |
46 | filelocList s = (filelocList) dmalloc (sizeof (*s)); | |
47 | ||
48 | s->nelements = 0; | |
49 | s->free = filelocListBASESIZE; | |
50 | s->elements = (fileloc *) dmalloc (sizeof (*s->elements) * filelocListBASESIZE); | |
51 | ||
52 | return (s); | |
53 | } | |
54 | ||
55 | static void | |
56 | filelocList_grow (/*@notnull@*/ filelocList s) | |
57 | { | |
58 | int i; | |
59 | o_fileloc *oldelements = s->elements; | |
60 | ||
61 | s->free += filelocListBASESIZE; | |
62 | s->elements = (fileloc *) dmalloc (sizeof (*s->elements) | |
63 | * (s->nelements + s->free)); | |
64 | ||
65 | for (i = 0; i < s->nelements; i++) | |
66 | { | |
67 | s->elements[i] = oldelements[i]; | |
68 | } | |
69 | ||
70 | sfree (oldelements); | |
71 | } | |
72 | ||
73 | filelocList | |
74 | filelocList_append (/*@returned@*/ filelocList s, /*@only@*/ filelocList t) | |
75 | { | |
76 | llassert (NOALIAS (s, t)); | |
77 | ||
78 | if (filelocList_isUndefined (t) || filelocList_isEmpty (t)) return s; | |
79 | ||
80 | if (filelocList_isUndefined (s)) | |
81 | { | |
82 | s = filelocList_newEmpty (); | |
83 | } | |
84 | ||
85 | filelocList_elements (t, fl) | |
86 | { | |
87 | /* Okay to use exposed storage here, t is begin eaten. */ | |
88 | ||
89 | /*@-exposetrans@*/ /*@-dependenttrans@*/ | |
90 | s = filelocList_add (s, fl); | |
91 | /*@=exposetrans@*/ /*@=dependenttrans@*/ | |
92 | } end_filelocList_elements; | |
93 | ||
94 | sfree (t->elements); | |
95 | sfree (t); | |
96 | ||
97 | return s; | |
98 | } | |
99 | ||
100 | filelocList | |
101 | filelocList_addUndefined (/*@returned@*/ filelocList s) | |
102 | { | |
103 | if (filelocList_isUndefined (s) | |
104 | || s->nelements == 0 | |
105 | || fileloc_isDefined (s->elements[0])) | |
106 | { | |
107 | return (filelocList_add (s, fileloc_undefined)); | |
108 | } | |
109 | else | |
110 | { | |
111 | return s; | |
112 | } | |
113 | } | |
114 | ||
115 | static bool filelocList_hasUndefinedLoc (filelocList s) | |
116 | { | |
117 | return (filelocList_isDefined (s) | |
118 | && s->nelements > 0 | |
119 | && fileloc_isUndefined (s->elements[0])); | |
120 | } | |
121 | ||
122 | filelocList | |
123 | filelocList_addDifferentFile (/*@returned@*/ filelocList s, | |
124 | fileloc where, | |
125 | fileloc loc) | |
126 | { | |
127 | if (filelocList_hasUndefinedLoc (s) || filelocList_size (s) >= 2) | |
128 | { | |
129 | return s; | |
130 | } | |
131 | else | |
132 | { | |
133 | if (fileloc_sameModule (where, loc)) | |
134 | { | |
135 | if (filelocList_isEmpty (s)) | |
136 | { | |
137 | return filelocList_add (s, fileloc_copy (loc)); | |
138 | } | |
139 | else | |
140 | { | |
141 | return s; | |
142 | } | |
143 | } | |
144 | else | |
145 | { | |
146 | return filelocList_addUndefined (s); | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | filelocList | |
152 | filelocList_add (/*@returned@*/ filelocList s, /*@only@*/ fileloc el) | |
153 | { | |
154 | if (filelocList_isUndefined (s)) | |
155 | { | |
156 | s = filelocList_newEmpty (); | |
157 | } | |
158 | ||
159 | if (s->free <= 0) | |
160 | { | |
161 | filelocList_grow (s); | |
162 | } | |
163 | ||
164 | s->free--; | |
165 | s->elements[s->nelements] = el; | |
166 | ||
167 | if (fileloc_isUndefined (el)) | |
168 | { | |
169 | s->elements[s->nelements] = s->elements[0]; | |
170 | s->elements[0] = fileloc_undefined; | |
171 | } | |
172 | ||
173 | s->nelements++; | |
174 | return s; | |
175 | } | |
176 | ||
177 | /*@only@*/ cstring | |
178 | filelocList_unparse (filelocList s) | |
179 | { | |
180 | int i; | |
181 | cstring st = cstring_makeLiteral ("["); | |
182 | ||
183 | if (filelocList_isDefined (s)) | |
184 | { | |
185 | for (i = 0; i < filelocList_size (s); i++) | |
186 | { | |
187 | if (i == 0) | |
188 | { | |
189 | st = message ("%q %q", st, fileloc_unparse (s->elements[i])); | |
190 | } | |
191 | else | |
192 | st = message ("%q, %q", st, fileloc_unparse (s->elements[i])); | |
193 | } | |
194 | } | |
195 | ||
196 | st = message ("%q ]", st); | |
197 | return st; | |
198 | } | |
199 | ||
200 | int filelocList_realSize (filelocList s) | |
201 | { | |
202 | int size = 0; | |
203 | ||
204 | filelocList_elements (s, el) | |
205 | { | |
206 | if (fileloc_isDefined (el)) | |
207 | { | |
208 | size++; | |
209 | } | |
210 | } end_filelocList_elements; | |
211 | ||
212 | return size; | |
213 | } | |
214 | ||
215 | cstring filelocList_unparseUses (filelocList s) | |
216 | { | |
217 | int i; | |
218 | size_t linelen = 0; | |
219 | int maxlen = context_getLineLen () - 3; | |
220 | cstring st = cstring_undefined; | |
221 | fileId lastFile = fileId_invalid; | |
222 | bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT); | |
223 | ||
224 | if (filelocList_isDefined (s)) | |
225 | { | |
226 | bool firstone = TRUE; | |
227 | ||
228 | for (i = 0; i < filelocList_size (s); i++) | |
229 | { | |
230 | if (fileloc_isDefined (s->elements[i])) | |
231 | { | |
232 | if (firstone) | |
233 | { | |
234 | st = fileloc_unparse (s->elements[i]); | |
235 | lastFile = fileloc_fileId (s->elements[i]); | |
236 | linelen = 3 + cstring_length (st); | |
237 | firstone = FALSE; | |
238 | } | |
239 | else | |
240 | { | |
241 | if (fileId_equal (fileloc_fileId (s->elements[i]), lastFile)) | |
242 | { | |
243 | if (linelen + 7 > size_fromInt (maxlen)) | |
244 | { | |
245 | st = message ("%q\n ", st); | |
246 | linelen = 6; | |
247 | } | |
248 | else | |
249 | { | |
250 | st = message ("%q, ", st); | |
251 | } | |
252 | ||
253 | if (parenFormat) | |
254 | { | |
255 | st = message ("%q(%d,%d)", | |
256 | st, fileloc_lineno (s->elements[i]), | |
257 | fileloc_column (s->elements[i])); | |
258 | } | |
259 | else | |
260 | { | |
261 | st = message ("%q%d:%d", | |
262 | st, fileloc_lineno (s->elements[i]), | |
263 | fileloc_column (s->elements[i])); | |
264 | } | |
265 | ||
266 | linelen += 3 + int_log (fileloc_lineno (s->elements[i])) | |
267 | + int_log (fileloc_column (s->elements[i])); | |
268 | } | |
269 | else | |
270 | { | |
271 | cstring fl = fileloc_unparse (s->elements[i]); | |
272 | st = message ("%q\n %s", st, fl); | |
273 | lastFile = fileloc_fileId (s->elements[i]); | |
274 | linelen = 3 + cstring_length (fl); | |
275 | cstring_free (fl); | |
276 | } | |
277 | } | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | return st; | |
283 | } | |
284 | ||
285 | void | |
286 | filelocList_free (/*@only@*/ filelocList s) | |
287 | { | |
288 | if (filelocList_isDefined (s)) | |
289 | { | |
290 | int i; | |
291 | for (i = 0; i < s->nelements; i++) | |
292 | { | |
293 | fileloc_free (s->elements[i]); | |
294 | } | |
295 | ||
296 | sfree (s->elements); | |
297 | sfree (s); | |
298 | } | |
299 | } | |
300 | ||
301 | ||
302 | ||
303 | ||
304 | ||
305 |