]> andersk Git - splint.git/blame - src/constraintList.c
Added support for or constraints.
[splint.git] / src / constraintList.c
CommitLineData
616915dd 1/*
2** LCLint - annotation-assisted static program checker
3** Copyright (C) 1994-2000 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** constraintList.c
26**
27** based on list_template.c
28**
29** where T has T_equal (or change this) and T_unparse
30*/
31
32# include "lclintMacros.nf"
33# include "llbasic.h"
34
35constraintList constraintList_new ()
36{
37 constraintList s = (constraintList) dmalloc (sizeof (*s));
38
39 s->nelements = 0;
40 s->nspace = constraintListBASESIZE;
41 s->elements = (constraint *)
42 dmalloc (sizeof (*s->elements) * constraintListBASESIZE);
43
44 return (s);
45}
46
47static void
48constraintList_grow (constraintList s)
49{
50 int i;
51 constraint *newelements;
52
53 s->nspace += constraintListBASESIZE;
54 newelements = (constraint *) dmalloc (sizeof (*newelements)
55 * (s->nelements + s->nspace));
56
57 for (i = 0; i < s->nelements; i++)
58 {
59 newelements[i] = s->elements[i];
60 }
61
62 sfree (s->elements);
63 s->elements = newelements;
64}
65
66/* void constraintList_exprNodemerge(void) */
67/* { */
68/* } */
69constraintList
70constraintList_add (constraintList s, constraint el)
71{
90bc41f7 72 el = constraint_simplify (el);
616915dd 73 if (resolve (el, s) )
74 return s;
75
76 if (s->nspace <= 0)
77 constraintList_grow (s);
78
79 s->nspace--;
80 s->elements[s->nelements] = el;
81 s->nelements++;
82 return s;
83}
84
85constraintList constraintList_addList (constraintList s, constraintList new)
86{
87 llassert(s);
88 llassert(new);
89
90 if (new == constraintList_undefined)
91 return s;
92
93 constraintList_elements(new, elem)
94 {
95 s = constraintList_add (s, elem);
96 }
97 end_constraintList_elements
98 return s;
99}
100
101cstring
102constraintList_print (constraintList s) /*@*/
103{
104 int i;
105 cstring st = cstring_undefined;
106 bool first = TRUE;
107
108 if (s->nelements == 0)
109 st = cstring_makeLiteral("<List Empty>");
110
111 for (i = 0; i < s->nelements; i++)
112 {
113 cstring type = cstring_undefined;
114 constraint current = s->elements[i];
115
116 if (current != NULL)
117 {
90bc41f7 118 cstring temp1;
119 if ( context_getFlag (FLG_ORCONSTRAINT) )
120 temp1 = constraint_printOr(current);
121 else
122 temp1 = constraint_print(current);
616915dd 123 type = message ("%q %q\n", type, temp1 );
124 }
125
126 if (first)
127 {
128 st = type;
129 first = FALSE;
130 }
131 else
132 {
133 st = message ("%q, %q", st, type);
134 }
135 }
136 return st;
137}
138
139void constraintList_printError (constraintList s, fileloc loc)
140{
141
142 constraintList_elements (s, elem)
143 {
144 if (elem != NULL)
145 {
146 constraint_printError (elem, loc);
147 }
148 }
149 end_constraintList_elements;
150 return;
151}
152
153cstring
154constraintList_printDetailed (constraintList s)
155{
156 int i;
157 cstring st = cstring_undefined;
158 bool first = TRUE;
159
160 if (s->nelements == 0)
161 st = cstring_makeLiteral("<List Empty>");
162
163 for (i = 0; i < s->nelements; i++)
164 {
165 cstring type = cstring_undefined;
166 constraint current = s->elements[i];
167
168 if (current != NULL)
169 {
170 cstring temp1 = constraint_printDetailed (current);
171 type = message ("%s %s\n", type, temp1 );
172 }
173
174 if (first)
175 {
176 st = type;
177 first = FALSE;
178 }
179 else
180 {
181 st = message ("%s %s", st, type);
182 }
183 }
184 return st;
185}
186
187/*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
188} */
189
190constraintList
191constraintList_logicalOr (constraintList l1, constraintList l2)
192{
193 constraint temp;
194 constraintList ret;
195 DPRINTF ( (message ("Logical of on %s and %s",
196 constraintList_print(l1),
197 constraintList_print(l2)) ) );
198
199 ret = constraintList_new();
200 constraintList_elements (l1, el)
201 {
202 temp = substitute (el, l2);
203
204 if (resolve (el, l2) || resolve(temp,l2) )
205 { /*avoid redundant constraints*/
206 if (!resolve (el, ret) )
207 ret = constraintList_add (ret, el);
208 }
209 }
210 end_constraintList_elements;
211
212 constraintList_elements (l2, el)
213 {
214 temp = substitute (el, l1);
215
216 if (resolve (el, l1) || resolve(temp,l1) )
217 {
218 /*avoid redundant constraints*/
219 if (!resolve (el, ret) )
220 ret = constraintList_add (ret, el);
221 }
222 }
223 end_constraintList_elements;
224
225
226 return ret;
227}
228
229void
230constraintList_free (constraintList s)
231{
232 int i;
233 for (i = 0; i < s->nelements; i++)
234 {
235 // constraint_free (s->elements[i]);
236 }
237
238 sfree (s->elements);
239 sfree (s);
240}
241
242constraintList
243constraintList_copy (constraintList s)
244{
245 constraintList ret = constraintList_new ();
246
247 constraintList_elements (s, el)
248 {
249 ret = constraintList_add (ret, constraint_copy (el));
250 } end_constraintList_elements;
251
252 return ret;
253}
254
255constraintList constraintList_preserveOrig (constraintList c)
256{
257 constraintList_elements (c, el)
258 {
259 el = constraint_preserveOrig (el);
260 }
261 end_constraintList_elements;
262 return c;
263}
264
9280addf 265constraintList constraintList_addGeneratingExpr (constraintList c, exprNode e)
266{
267 DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) ));
268
269 constraintList_elements (c, el)
270 {
271 DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(el), exprNode_unparse(e) ) ));
272 el = constraint_addGeneratingExpr (el, e);
273 }
274 end_constraintList_elements;
275 return c;
276}
277
616915dd 278constraintList constraintList_doFixResult (constraintList postconditions, exprNode fcnCall)
279{
280 constraintList ret;
281 ret = constraintList_new();
282 constraintList_elements (postconditions, el)
283 {
284 ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) );
285 }
286 end_constraintList_elements;
287
288 return ret;
289}
290
291constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, exprNodeList arglist)
292{
293 constraintList ret;
294 ret = constraintList_new();
295
296 constraintList_elements (preconditions, el)
297 {
298 ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
299 }
300 end_constraintList_elements;
301
302 return ret;
303}
304constraintList constraintList_doSRefFixBaseParam (constraintList preconditions,
305 exprNodeList arglist)
306{
307 constraintList ret;
308 ret = constraintList_new();
309
310 constraintList_elements (preconditions, el)
311 {
312 ret = constraintList_add(ret, constraint_doSRefFixBaseParam (el, arglist) );
313 }
314 end_constraintList_elements;
315
316 return ret;
317}
318
319constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
320{
321 constraintList_elements (c, el)
322 {
323 el->post = !el->post;
324 }
325 end_constraintList_elements;
326 return c;
327}
328
329
This page took 0.092747 seconds and 5 git commands to generate.