]> andersk Git - splint.git/blob - src/filelocStack.c
Merged code tree with Dave Evans's version. Many changes to numberous to list....
[splint.git] / src / filelocStack.c
1 /*
2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
23 */
24 /*
25 ** filelocStack.c (from slist_template.c)
26 */
27
28 # include "lclintMacros.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       s->elements[i] = oldelements[i];
63     }
64   
65   sfree (oldelements);
66 }
67
68 static void 
69   filelocStack_push (/*@returned@*/ filelocStack s, /*@keep@*/ fileloc el)
70   /*@modifies s@*/
71 {
72   llassert (filelocStack_isDefined (s));
73
74   if (s->free <= 0)
75     {
76       filelocStack_grow (s);
77     }
78   
79   s->free--;
80   s->elements[s->nelements] = el;
81   s->nelements++;
82 }
83
84 fileloc filelocStack_nextTop (filelocStack s)
85 {
86   llassert (filelocStack_isDefined (s) && s->nelements > 1);
87
88   return (s->elements[s->nelements - 2]);
89 }
90
91 void filelocStack_clear (filelocStack s)
92 {
93   if (filelocStack_isDefined (s))
94     {
95       int i;
96
97       for (i = 0; i < s->nelements; i++)
98         {
99           fileloc_free (s->elements[i]);
100         }
101
102       s->free += s->nelements;
103       s->nelements = 0;
104     }
105 }
106
107 /*
108 ** Returns TRUE of el is a new file.
109 */
110
111 bool filelocStack_popPushFile (filelocStack s, fileloc el)
112 {
113   int i;
114
115   llassert (filelocStack_isDefined (s));
116
117   for (i = s->nelements - 1; i >= 0; i--)
118     {
119       if (fileloc_sameBaseFile (s->elements[i], el))
120         {
121           int j;
122           
123           for (j = i; j < s->nelements; j++)
124             {
125               fileloc_free (s->elements[j]);
126             }
127
128           s->elements[i] = el;
129           s->nelements = i + 1;
130           return FALSE;
131         }
132     }
133
134   filelocStack_push (s, el);
135   return TRUE;
136 }
137
138 /*@only@*/ cstring
139 filelocStack_unparse (filelocStack s)
140 {
141    int i;
142    cstring st = cstring_makeLiteral ("[");
143
144    if (filelocStack_isDefined (s))
145      {
146        for (i = s->nelements - 1; i >= 0; i--)
147          {
148            if (i == s->nelements - 1)
149              {
150                st = message ("%q %q", st, fileloc_unparse (s->elements[i]));
151              }
152            else
153              {
154                st = message ("%q, %q", st, fileloc_unparse (s->elements[i]));
155              }
156          }
157      }
158    
159    st = message ("%q ]", st);
160    return st;
161 }
162
163 int filelocStack_includeDepth (filelocStack s)
164 {
165   int depth = 0;
166   int i;
167
168   if (filelocStack_isDefined (s))
169     {
170       /* the zeroth element doesn't count! */
171       for (i = s->nelements - 1; i > 0; i--)
172         {
173           if (!fileloc_isSpecialFile (s->elements[i]))
174             {
175               depth++;
176             }
177         }
178     }
179
180   return depth;
181 }
182
183 void
184 filelocStack_printIncludes (filelocStack s)
185 {
186   if (filelocStack_isDefined (s))
187     {
188       int i;
189       bool prep = context_isPreprocessing ();
190       
191       if (prep)
192         {
193           /* need to do this for messages */
194           context_clearPreprocessing ();
195         }
196
197       /* don't show last two files pushed */
198       for (i = s->nelements - 3; i >= 0; i--)
199         {
200           if (i == 0 || !fileloc_isSpecialFile (s->elements[i]))
201             {
202               llgenindentmsg (cstring_makeLiteral ("Include site"),
203                               s->elements[i]);
204             }
205         }
206
207       if (prep)
208         {
209           context_setPreprocessing ();
210         }
211     }
212 }
213
214 void
215 filelocStack_free (/*@only@*/ filelocStack s)
216 {
217   if (filelocStack_isDefined (s))
218     {
219       int i;
220       for (i = 0; i < s->nelements; i++)
221         {
222           fileloc_free (s->elements[i]); 
223         }
224       
225       sfree (s->elements); 
226       sfree (s);
227     }
228 }
229
230
231
232
233
234
This page took 1.056715 seconds and 5 git commands to generate.