]> andersk Git - splint.git/blob - src/filelocStack.c
c6670321ee205b6d40a3ea9195a7578bad76cf81
[splint.git] / src / filelocStack.c
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 ** filelocStack.c (from slist_template.c)
26 */
27
28 # include "splintMacros.nf"
29 # include "basic.h"
30 # include "filelocStack.h"
31
32 static /*@notnull@*/ /*@only@*/ filelocStack
33 filelocStack_newEmpty (void)
34 {
35   filelocStack s = (filelocStack) dmalloc (sizeof (*s));
36   
37   s->nelements = 0;
38   s->free = filelocStackBASESIZE;
39   s->elements = (fileloc *) dmalloc (sizeof (*s->elements) * filelocStackBASESIZE);
40
41   return (s);
42 }
43
44 filelocStack
45 filelocStack_new ()
46 {
47   return (filelocStack_newEmpty ());
48 }
49
50 static void
51 filelocStack_grow (/*@notnull@*/ filelocStack s)
52 {
53   o_fileloc *oldelements = s->elements;
54   int i;
55   
56   s->free += filelocStackBASESIZE; 
57   s->elements = (fileloc *) dmalloc (sizeof (*s->elements) 
58                                      * (s->nelements + s->free));
59     
60   for (i = 0; i < s->nelements; i++)
61     {
62       /*drl bee: si*/
63       /*drl bee: si*/
64       s->elements[i] = oldelements[i];
65     }
66   
67   sfree (oldelements);
68 }
69
70 static void 
71   filelocStack_push (/*@returned@*/ filelocStack s, /*@keep@*/ fileloc el)
72   /*@modifies s@*/
73 {
74   llassert (filelocStack_isDefined (s));
75
76   if (s->free <= 0)
77     {
78       filelocStack_grow (s);
79     }
80   
81   s->free--;
82   /*drl bee: si*/
83   s->elements[s->nelements] = el;
84   s->nelements++;
85 }
86
87 fileloc filelocStack_nextTop (filelocStack s)
88 {
89   llassert (filelocStack_isDefined (s) && s->nelements > 1);
90
91   return (s->elements[s->nelements - 2]);
92 }
93
94 void filelocStack_clear (filelocStack s)
95 {
96   if (filelocStack_isDefined (s))
97     {
98       int i;
99
100       for (i = 0; i < s->nelements; i++)
101         {
102                 /*drl bee: si*/
103           fileloc_free (s->elements[i]);
104         }
105
106       s->free += s->nelements;
107       s->nelements = 0;
108     }
109 }
110
111 /*
112 ** Returns TRUE of el is a new file.
113 */
114
115 bool filelocStack_popPushFile (filelocStack s, fileloc el)
116 {
117   int i;
118
119   llassert (filelocStack_isDefined (s));
120
121   for (i = s->nelements - 1; i >= 0; i--)
122     {
123             /*drl bee: si*/
124       if (fileloc_sameBaseFile (s->elements[i], el))
125         {
126           int j;
127           
128           for (j = i; j < s->nelements; j++)
129             {
130                     /*drl bee: si*/
131               fileloc_free (s->elements[j]);
132             }
133       /*drl bee: si*/
134           s->elements[i] = el;
135           s->nelements = i + 1;
136           return FALSE;
137         }
138     }
139
140   filelocStack_push (s, el);
141   return TRUE;
142 }
143
144 /*@only@*/ cstring
145 filelocStack_unparse (filelocStack s)
146 {
147    int i;
148    cstring st = cstring_makeLiteral ("[");
149
150    if (filelocStack_isDefined (s))
151      {
152        for (i = s->nelements - 1; i >= 0; i--)
153          {
154            if (i == s->nelements - 1)
155              {
156                      /*drl bee: si*/
157                st = message ("%q %q", st, fileloc_unparse (s->elements[i]));
158              }
159            else
160              {
161                      /*drl bee: si*/
162                st = message ("%q, %q", st, fileloc_unparse (s->elements[i]));
163              }
164          }
165      }
166    
167    st = message ("%q ]", st);
168    return st;
169 }
170
171 int filelocStack_includeDepth (filelocStack s)
172 {
173   int depth = 0;
174   int i;
175
176   if (filelocStack_isDefined (s))
177     {
178       /* the zeroth element doesn't count! */
179       for (i = s->nelements - 1; i > 0; i--)
180         {
181                 /*drl bee: si*/
182           if (!fileloc_isSpecialFile (s->elements[i]))
183             {
184               depth++;
185             }
186         }
187     }
188
189   return depth;
190 }
191
192 void
193 filelocStack_printIncludes (filelocStack s)
194 {
195   if (filelocStack_isDefined (s))
196     {
197       int i;
198       bool prep = context_isPreprocessing ();
199       
200       if (prep)
201         {
202           /* need to do this for messages */
203           context_clearPreprocessing ();
204         }
205
206       /* don't show last two files pushed */
207       for (i = s->nelements - 3; i >= 0; i--)
208         {
209                 /*drl bee: si*/
210           if (i == 0 || !fileloc_isSpecialFile (s->elements[i]))
211             {
212                     /*drl bee: si*/
213               llgenindentmsg (cstring_makeLiteral ("Include site"),
214                               s->elements[i]);
215             }
216         }
217
218       if (prep)
219         {
220           context_setPreprocessing ();
221         }
222     }
223 }
224
225 void
226 filelocStack_free (/*@only@*/ filelocStack s)
227 {
228   if (filelocStack_isDefined (s))
229     {
230       int i;
231       for (i = 0; i < s->nelements; i++)
232         {
233                 /*drl bee: si*/
234           fileloc_free (s->elements[i]); 
235         }
236       
237       sfree (s->elements); 
238       sfree (s);
239     }
240 }
241
242
243
244
245
246
This page took 0.041403 seconds and 3 git commands to generate.