Various minor fixes for compiler/linter (other then splint itself) warnings.
[splint-patched.git] / src / filelocStack.c
blob7174a1cf7e0961843f6921003c4ed23933262a54
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 ** filelocStack.c (from slist_template.c)
28 # include "splintMacros.nf"
29 # include "basic.h"
30 # include "filelocStack.h"
32 /*@constant int filelocStackBASESIZE;@*/
33 # define filelocStackBASESIZE MIDBASESIZE
35 static /*@notnull@*/ /*@only@*/ filelocStack
36 filelocStack_newEmpty (void)
38 filelocStack s = (filelocStack) dmalloc (sizeof (*s));
40 s->nelements = 0;
41 s->free = filelocStackBASESIZE;
42 s->elements = (fileloc *) dmalloc (sizeof (*s->elements) * filelocStackBASESIZE);
44 return (s);
47 filelocStack
48 filelocStack_new (void)
50 return (filelocStack_newEmpty ());
53 static void
54 filelocStack_grow (/*@notnull@*/ filelocStack s)
56 o_fileloc *oldelements = s->elements;
57 int i;
59 s->free += filelocStackBASESIZE;
60 s->elements = (fileloc *) dmalloc (sizeof (*s->elements)
61 * (s->nelements + s->free));
63 for (i = 0; i < s->nelements; i++)
65 s->elements[i] = oldelements[i];
68 sfree (oldelements);
71 static void
72 filelocStack_push (/*@returned@*/ filelocStack s, /*@keep@*/ fileloc el)
73 /*@modifies s@*/
75 llassert (filelocStack_isDefined (s));
77 if (s->free <= 0)
79 filelocStack_grow (s);
82 s->free--;
83 s->elements[s->nelements] = el;
84 s->nelements++;
87 fileloc filelocStack_nextTop (filelocStack s)
89 llassert (filelocStack_isDefined (s) && s->nelements > 1);
91 return (s->elements[s->nelements - 2]);
94 void filelocStack_clear (filelocStack s)
96 if (filelocStack_isDefined (s))
98 int i;
100 for (i = 0; i < s->nelements; i++)
102 fileloc_free (s->elements[i]);
105 s->free += s->nelements;
106 s->nelements = 0;
111 ** Returns TRUE of el is a new file.
114 bool filelocStack_popPushFile (filelocStack s, fileloc el)
116 int i;
118 llassert (filelocStack_isDefined (s));
120 for (i = s->nelements - 1; i >= 0; i--)
122 if (fileloc_sameBaseFile (s->elements[i], el))
124 int j;
126 for (j = i; j < s->nelements; j++)
128 fileloc_free (s->elements[j]);
130 s->elements[i] = el;
131 s->nelements = i + 1;
132 return FALSE;
136 filelocStack_push (s, el);
137 return TRUE;
140 # ifdef DEADCODE
141 /*@only@*/ cstring
142 filelocStack_unparse (filelocStack s)
144 int i;
145 cstring st = cstring_makeLiteral ("[");
147 if (filelocStack_isDefined (s))
149 for (i = s->nelements - 1; i >= 0; i--)
151 if (i == s->nelements - 1)
153 st = message ("%q %q", st, fileloc_unparse (s->elements[i]));
155 else
157 st = message ("%q, %q", st, fileloc_unparse (s->elements[i]));
162 st = message ("%q ]", st);
163 return st;
165 # endif /* DEADCODE */
167 int filelocStack_includeDepth (filelocStack s)
169 int depth = 0;
170 int i;
172 if (filelocStack_isDefined (s))
174 /* the zeroth element doesn't count! */
175 for (i = s->nelements - 1; i > 0; i--)
177 if (!fileloc_isSpecialFile (s->elements[i]))
179 depth++;
184 return depth;
187 void
188 filelocStack_printIncludes (filelocStack s)
190 if (filelocStack_isDefined (s))
192 int i;
193 bool prep = context_isPreprocessing ();
195 if (prep)
197 /* need to do this for messages */
198 context_clearPreprocessing ();
201 /* don't show last two files pushed */
202 for (i = s->nelements - 3; i >= 0; i--)
204 if (i == 0 || !fileloc_isSpecialFile (s->elements[i]))
206 llgenindentmsg (cstring_makeLiteral ("Include site"),
207 s->elements[i]);
211 if (prep)
213 context_setPreprocessing ();
218 void
219 filelocStack_free (/*@only@*/ filelocStack s)
221 if (filelocStack_isDefined (s))
223 int i;
224 for (i = 0; i < s->nelements; i++)
226 fileloc_free (s->elements[i]);
229 sfree (s->elements);
230 sfree (s);