]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
c59f5181 | 3 | ** Copyright (C) 1994-2003 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 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | /* | |
25 | ** filelocStack.c (from slist_template.c) | |
26 | */ | |
27 | ||
1b8ae690 | 28 | # include "splintMacros.nf" |
616915dd | 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 | { | |
8fd556fb | 62 | s->elements[i] = oldelements[i]; |
616915dd | 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--; | |
8fd556fb | 80 | s->elements[s->nelements] = el; |
616915dd | 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 | { | |
8fd556fb | 99 | fileloc_free (s->elements[i]); |
616915dd | 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 | { | |
8fd556fb | 119 | if (fileloc_sameBaseFile (s->elements[i], el)) |
616915dd | 120 | { |
121 | int j; | |
122 | ||
123 | for (j = i; j < s->nelements; j++) | |
124 | { | |
8fd556fb | 125 | fileloc_free (s->elements[j]); |
616915dd | 126 | } |
8fd556fb | 127 | s->elements[i] = el; |
616915dd | 128 | s->nelements = i + 1; |
129 | return FALSE; | |
130 | } | |
131 | } | |
132 | ||
133 | filelocStack_push (s, el); | |
134 | return TRUE; | |
135 | } | |
136 | ||
137 | /*@only@*/ cstring | |
138 | filelocStack_unparse (filelocStack s) | |
139 | { | |
140 | int i; | |
141 | cstring st = cstring_makeLiteral ("["); | |
142 | ||
143 | if (filelocStack_isDefined (s)) | |
144 | { | |
145 | for (i = s->nelements - 1; i >= 0; i--) | |
146 | { | |
147 | if (i == s->nelements - 1) | |
148 | { | |
8fd556fb | 149 | st = message ("%q %q", st, fileloc_unparse (s->elements[i])); |
616915dd | 150 | } |
151 | else | |
152 | { | |
8fd556fb | 153 | st = message ("%q, %q", st, fileloc_unparse (s->elements[i])); |
616915dd | 154 | } |
155 | } | |
156 | } | |
157 | ||
158 | st = message ("%q ]", st); | |
159 | return st; | |
160 | } | |
161 | ||
162 | int filelocStack_includeDepth (filelocStack s) | |
163 | { | |
164 | int depth = 0; | |
165 | int i; | |
166 | ||
167 | if (filelocStack_isDefined (s)) | |
168 | { | |
169 | /* the zeroth element doesn't count! */ | |
170 | for (i = s->nelements - 1; i > 0; i--) | |
171 | { | |
8fd556fb | 172 | if (!fileloc_isSpecialFile (s->elements[i])) |
616915dd | 173 | { |
174 | depth++; | |
175 | } | |
176 | } | |
177 | } | |
178 | ||
179 | return depth; | |
180 | } | |
181 | ||
182 | void | |
183 | filelocStack_printIncludes (filelocStack s) | |
184 | { | |
185 | if (filelocStack_isDefined (s)) | |
186 | { | |
187 | int i; | |
188 | bool prep = context_isPreprocessing (); | |
189 | ||
190 | if (prep) | |
191 | { | |
192 | /* need to do this for messages */ | |
193 | context_clearPreprocessing (); | |
194 | } | |
195 | ||
196 | /* don't show last two files pushed */ | |
197 | for (i = s->nelements - 3; i >= 0; i--) | |
198 | { | |
8fd556fb | 199 | if (i == 0 || !fileloc_isSpecialFile (s->elements[i])) |
616915dd | 200 | { |
8fd556fb | 201 | llgenindentmsg (cstring_makeLiteral ("Include site"), |
616915dd | 202 | s->elements[i]); |
203 | } | |
204 | } | |
205 | ||
206 | if (prep) | |
207 | { | |
208 | context_setPreprocessing (); | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | void | |
214 | filelocStack_free (/*@only@*/ filelocStack s) | |
215 | { | |
216 | if (filelocStack_isDefined (s)) | |
217 | { | |
218 | int i; | |
219 | for (i = 0; i < s->nelements; i++) | |
220 | { | |
8fd556fb | 221 | fileloc_free (s->elements[i]); |
616915dd | 222 | } |
223 | ||
224 | sfree (s->elements); | |
225 | sfree (s); | |
226 | } | |
227 | } | |
228 | ||
229 | ||
230 | ||
231 | ||
232 | ||
233 |