Don't try calling lsl when file is missing.
[splint-patched.git] / src / filelocList.c
blobe58c0f05149e8d425d4a289c359d2e2e9e2d388e
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.
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
25 ** filelocList.c (from slist_template.c)
28 # include "splintMacros.nf"
29 # include "basic.h"
31 /*@constant int filelocListBASESIZE;@*/
32 # define filelocListBASESIZE MIDBASESIZE
35 ** Invariant: If any member of the list is fileloc_undefined, then
36 ** the 0th member is fileloc_undefined.
39 filelocList
40 filelocList_new (void)
42 return (filelocList_undefined);
45 static /*@notnull@*/ /*@only@*/ filelocList
46 filelocList_newEmpty (void)
48 filelocList s = (filelocList) dmalloc (sizeof (*s));
50 s->nelements = 0;
51 s->free = filelocListBASESIZE;
52 s->elements = (fileloc *) dmalloc (sizeof (*s->elements) * filelocListBASESIZE);
54 return (s);
57 static void
58 filelocList_grow (/*@notnull@*/ filelocList s)
60 int i;
61 o_fileloc *oldelements = s->elements;
63 s->free += filelocListBASESIZE;
64 s->elements = (fileloc *) dmalloc (sizeof (*s->elements)
65 * (s->nelements + s->free));
67 for (i = 0; i < s->nelements; i++)
69 s->elements[i] = oldelements[i];
72 sfree (oldelements);
75 filelocList
76 filelocList_append (/*@returned@*/ filelocList s, /*@only@*/ filelocList t)
78 llassert (NOALIAS (s, t));
80 if (filelocList_isUndefined (t) || filelocList_isEmpty (t)) return s;
82 if (filelocList_isUndefined (s))
84 s = filelocList_newEmpty ();
87 filelocList_elements (t, fl)
89 /* Okay to use exposed storage here, t is begin eaten. */
91 /*@-exposetrans@*/ /*@-dependenttrans@*/
92 s = filelocList_add (s, fl);
93 /*@=exposetrans@*/ /*@=dependenttrans@*/
94 } end_filelocList_elements;
96 sfree (t->elements);
97 sfree (t);
99 return s;
102 filelocList
103 filelocList_addUndefined (/*@returned@*/ filelocList s)
105 if (filelocList_isUndefined (s)
106 || s->nelements == 0
107 || fileloc_isDefined (s->elements[0]))
109 return (filelocList_add (s, fileloc_undefined));
111 else
113 return s;
117 static bool filelocList_hasUndefinedLoc (filelocList s)
119 return (filelocList_isDefined (s)
120 && s->nelements > 0
121 && fileloc_isUndefined (s->elements[0]));
124 filelocList
125 filelocList_addDifferentFile (/*@returned@*/ filelocList s,
126 fileloc where,
127 fileloc loc)
129 if (filelocList_hasUndefinedLoc (s) || filelocList_size (s) >= 2)
131 return s;
133 else
135 if (fileloc_sameModule (where, loc))
137 if (filelocList_isEmpty (s))
139 return filelocList_add (s, fileloc_copy (loc));
141 else
143 return s;
146 else
148 return filelocList_addUndefined (s);
153 filelocList
154 filelocList_add (/*@returned@*/ filelocList s, /*@only@*/ fileloc el)
156 if (filelocList_isUndefined (s))
158 s = filelocList_newEmpty ();
161 if (s->free <= 0)
163 filelocList_grow (s);
166 s->free--;
167 s->elements[s->nelements] = el;
169 if (fileloc_isUndefined (el))
171 s->elements[s->nelements] = s->elements[0];
172 s->elements[0] = fileloc_undefined;
175 s->nelements++;
176 return s;
179 # ifdef DEADCODE
180 /*@only@*/ cstring
181 filelocList_unparse (filelocList s)
183 int i;
184 cstring st = cstring_makeLiteral ("[");
186 if (filelocList_isDefined (s))
188 for (i = 0; i < filelocList_size (s); i++)
190 if (i == 0)
192 st = message ("%q %q", st, fileloc_unparse (s->elements[i]));
194 else
195 st = message ("%q, %q", st, fileloc_unparse (s->elements[i]));
199 st = message ("%q ]", st);
200 return st;
202 # endif /* DEADCODE */
204 int filelocList_realSize (filelocList s)
206 int size = 0;
208 filelocList_elements (s, el)
210 if (fileloc_isDefined (el))
212 size++;
214 } end_filelocList_elements;
216 return size;
219 cstring filelocList_unparseUses (filelocList s)
221 int i;
222 size_t linelen = 0;
223 int maxlen = context_getLineLen () - 3;
224 cstring st = cstring_undefined;
225 fileId lastFile = fileId_invalid;
226 bool parenFormat = context_getFlag (FLG_PARENFILEFORMAT);
228 if (filelocList_isDefined (s))
230 bool firstone = TRUE;
232 for (i = 0; i < filelocList_size (s); i++)
234 if (fileloc_isDefined (s->elements[i]))
236 if (firstone)
238 st = fileloc_unparse (s->elements[i]);
239 lastFile = fileloc_fileId (s->elements[i]);
240 linelen = 3 + cstring_length (st);
241 firstone = FALSE;
243 else
245 if (fileId_equal (fileloc_fileId (s->elements[i]), lastFile))
247 if (linelen + 7 > size_fromInt (maxlen))
249 st = message ("%q\n ", st);
250 linelen = 6;
252 else
254 st = message ("%q, ", st);
257 if (parenFormat)
259 st = message ("%q(%d,%d)",
260 st, fileloc_lineno (s->elements[i]),
261 fileloc_column (s->elements[i]));
263 else
265 st = message ("%q%d:%d",
266 st, fileloc_lineno (s->elements[i]),
267 fileloc_column (s->elements[i]));
270 linelen += 3 + int_log (fileloc_lineno (s->elements[i]))
271 + int_log (fileloc_column (s->elements[i]));
273 else
275 cstring fl = fileloc_unparse (s->elements[i]);
276 st = message ("%q\n %s", st, fl);
277 lastFile = fileloc_fileId (s->elements[i]);
278 linelen = 3 + cstring_length (fl);
279 cstring_free (fl);
286 return st;
289 void
290 filelocList_free (/*@only@*/ filelocList s)
292 if (filelocList_isDefined (s))
294 int i;
295 for (i = 0; i < s->nelements; i++)
297 fileloc_free (s->elements[i]);
300 sfree (s->elements);
301 sfree (s);