]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
77d37419 | 3 | ** Copyright (C) 1994-2002 University of Virginia, |
616915dd | 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 | ** | |
1b8ae690 | 20 | ** For information on splint: splint@cs.virginia.edu |
21 | ** To report a bug: splint-bug@cs.virginia.edu | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | /* | |
25 | ** filelocList.c (from slist_template.c) | |
26 | */ | |
27 | ||
1b8ae690 | 28 | # include "splintMacros.nf" |
616915dd | 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 | int linelen = 0; | |
219 | int maxlen = context_getLineLen () - 3; | |
220 | cstring st = cstring_undefined; | |
221 | fileId lastFile = fileId_invalid; | |
222 | ||
223 | if (filelocList_isDefined (s)) | |
224 | { | |
225 | bool firstone = TRUE; | |
226 | ||
227 | for (i = 0; i < filelocList_size (s); i++) | |
228 | { | |
229 | if (fileloc_isDefined (s->elements[i])) | |
230 | { | |
231 | if (firstone) | |
232 | { | |
233 | st = fileloc_unparse (s->elements[i]); | |
234 | lastFile = fileloc_fileId (s->elements[i]); | |
235 | linelen = 3 + cstring_length (st); | |
236 | firstone = FALSE; | |
237 | } | |
238 | else | |
239 | { | |
240 | if (fileId_equal (fileloc_fileId (s->elements[i]), lastFile)) | |
241 | { | |
242 | if (linelen + 7 > maxlen) | |
243 | { | |
244 | st = message ("%q\n ", st); | |
245 | linelen = 6; | |
246 | } | |
247 | else | |
248 | { | |
249 | st = message ("%q, ", st); | |
250 | } | |
251 | ||
252 | st = message ("%q%d,%d", | |
253 | st, fileloc_lineno (s->elements[i]), | |
254 | fileloc_column (s->elements[i])); | |
255 | linelen += 3 + int_log (fileloc_lineno (s->elements[i])) | |
256 | + int_log (fileloc_column (s->elements[i])); | |
257 | } | |
258 | else | |
259 | { | |
260 | cstring fl = fileloc_unparse (s->elements[i]); | |
261 | st = message ("%q\n %s", st, fl); | |
262 | lastFile = fileloc_fileId (s->elements[i]); | |
263 | linelen = 3 + cstring_length (fl); | |
264 | cstring_free (fl); | |
265 | } | |
266 | } | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | return st; | |
272 | } | |
273 | ||
274 | void | |
275 | filelocList_free (/*@only@*/ filelocList s) | |
276 | { | |
277 | if (filelocList_isDefined (s)) | |
278 | { | |
279 | int i; | |
280 | for (i = 0; i < s->nelements; i++) | |
281 | { | |
282 | fileloc_free (s->elements[i]); | |
283 | } | |
284 | ||
285 | sfree (s->elements); | |
286 | sfree (s); | |
287 | } | |
288 | } | |
289 | ||
290 | ||
291 | ||
292 | ||
293 | ||
294 |