2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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 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
27 ** based on list_template.c
29 ** where T has T_equal (or change this) and T_unparse
32 # include "lclintMacros.nf"
36 /*@iter constraintList_elements_private (sef constraintList x, yield constraint el); @*/
37 # define constraintList_elements_private(x, m_el) \
38 { int m_ind; constraint *m_elements = &((x)->elements[0]); \
39 for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
40 { constraint m_el = *(m_elements++);
42 # define end_constraintList_elements_private }}
45 constraintList constraintList_makeNew ()
47 constraintList s = (constraintList) dmalloc (sizeof (*s));
50 s->nspace = constraintListBASESIZE;
51 s->elements = (constraint *)
52 dmalloc (sizeof (*s->elements) * constraintListBASESIZE);
58 constraintList_grow (constraintList s)
61 constraint *newelements;
63 s->nspace += constraintListBASESIZE;
64 newelements = (constraint *) dmalloc (sizeof (*newelements)
65 * (s->nelements + s->nspace));
67 for (i = 0; i < s->nelements; i++)
69 newelements[i] = s->elements[i];
73 s->elements = newelements;
78 constraintList_add (/*@returned@*/ constraintList s, /*@only@*/ constraint el)
81 // el = constraint_simplify (el);
89 constraintList_grow (s);
92 s->elements[s->nelements] = el;
97 /* frees everything but actual constraints */
98 /* This function should only be used if you have
99 other references to unshared constraints
101 static void constraintList_freeShallow (/*@only@*/ constraintList c)
103 if (constraintList_isDefined(c) )
114 /*@only@*/ constraintList constraintList_addList (/*@returned@*/ constraintList s, /*@observer@*/ constraintList new)
116 llassert(constraintList_isDefined(s) );
117 llassert(constraintList_isDefined(new) );
119 if (new == constraintList_undefined)
122 constraintList_elements (new, elem)
124 s = constraintList_add (s, constraint_copy(elem) );
126 end_constraintList_elements;
131 /*@only@*/ constraintList constraintList_addListFree (/*@only@*/ constraintList s, /*@only@*/ constraintList new)
133 llassert(constraintList_isDefined(s) );
134 llassert(constraintList_isDefined(new) );
136 if (new == constraintList_undefined)
139 constraintList_elements_private(new, elem)
141 s = constraintList_add (s, elem);
143 end_constraintList_elements_private
145 constraintList_freeShallow(new);
150 extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/
152 return (constraintList_print(s));
159 constraintList_print (constraintList s) /*@*/
162 cstring st = cstring_undefined;
165 if (s->nelements == 0)
167 st = cstring_makeLiteral("<List Empty>");
171 for (i = 0; i < s->nelements; i++)
173 cstring type = cstring_undefined;
174 constraint current = s->elements[i];
179 if ( context_getFlag (FLG_ORCONSTRAINT) )
180 temp1 = constraint_printOr(current);
182 temp1 = constraint_print(current);
183 type = message ("%q %q\n", type, temp1 );
193 st = message ("%q, %q", st, type);
200 void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
203 constraintList_elements (s, elem)
207 constraint_printErrorPostCondition (elem, loc);
210 end_constraintList_elements;
214 void constraintList_printError (constraintList s, fileloc loc)
217 constraintList_elements (s, elem)
221 constraint_printError (elem, loc);
224 end_constraintList_elements;
230 constraintList_printDetailed (constraintList s)
233 cstring st = cstring_undefined;
236 if (s->nelements == 0)
238 st = cstring_makeLiteral("<List Empty>");
242 for (i = 0; i < s->nelements; i++)
244 cstring type = cstring_undefined;
245 constraint current = s->elements[i];
249 cstring temp1 = constraint_printDetailed (current);
250 type = message ("%s %s\n", type, temp1 );
262 st = message ("%q %q", st, type);
268 /*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
272 constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
276 DPRINTF ( (message ("Logical or on %s and %s",
277 constraintList_print(l1),
278 constraintList_print(l2)) ) );
280 ret = constraintList_makeNew();
281 constraintList_elements (l1, el)
283 temp = substitute (el, l2);
285 if (resolve (el, l2) || resolve(temp,l2) )
286 { /*avoid redundant constraints*/
287 if (!resolve (el, ret) )
290 temp2 = constraint_copy(el);
291 ret = constraintList_add (ret, temp2);
294 constraint_free(temp);
296 end_constraintList_elements;
298 constraintList_elements (l2, el)
300 temp = substitute (el, l1);
302 if (resolve (el, l1) || resolve(temp,l1) )
304 /*avoid redundant constraints*/
305 if (!resolve (el, ret) )
308 temp2 = constraint_copy(el);
309 ret = constraintList_add (ret, temp2);
312 constraint_free(temp);
314 end_constraintList_elements;
321 constraintList_free (/*@only@*/ constraintList s)
325 llassert(constraintList_isDefined(s) );
328 for (i = 0; i < s->nelements; i++)
330 constraint_free (s->elements[i]);
342 constraintList_copy (constraintList s)
344 constraintList ret = constraintList_makeNew ();
346 constraintList_elements (s, el)
348 ret = constraintList_add (ret, constraint_copy (el));
349 } end_constraintList_elements;
354 constraintList constraintList_preserveOrig (constraintList c)
356 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
358 constraintList_elements_private (c, el)
360 el = constraint_preserveOrig (el);
362 end_constraintList_elements_private;
366 constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c, exprNode fcn)
368 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
370 constraintList_elements_private (c, el)
372 // el = constraint_preserveOrig (el);
373 el = constraint_setFcnPre(el);
374 el = constraint_origAddGeneratingExpr (el, fcn);
376 end_constraintList_elements_private;
382 constraintList constraintList_addGeneratingExpr (constraintList c, exprNode e)
384 DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) ));
386 constraintList_elements_private (c, el)
388 DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(el), exprNode_unparse(e) ) ));
389 el = constraint_addGeneratingExpr (el, e);
391 end_constraintList_elements_private;
395 /*@only@*/ constraintList constraintList_doFixResult (/*@only@*/constraintList postconditions, exprNode fcnCall)
398 ret = constraintList_makeNew();
399 constraintList_elements_private (postconditions, el)
401 ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) );
403 end_constraintList_elements_private;
405 constraintList_free(postconditions);
409 /*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, exprNodeList arglist)
412 ret = constraintList_makeNew();
414 constraintList_elements (preconditions, el)
416 ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
418 end_constraintList_elements;
420 constraintList_free (preconditions);
424 constraintList constraintList_doSRefFixBaseParam (/*@observer@*/ constraintList preconditions, /*@observer@*/
425 exprNodeList arglist)
429 ret = constraintList_makeNew();
431 constraintList_elements (preconditions, el)
433 temp = constraint_copy(el);
434 ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) );
436 end_constraintList_elements;
441 constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
443 constraintList_elements_private (c, el)
445 el = constraint_togglePost(el);
446 if (constraint_hasOrig(el) )
448 el = constraint_togglePostOrig (el);
451 end_constraintList_elements_private;
455 /*@only@*/ constraintList constraintList_undump (FILE *f)
458 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
461 ret = constraintList_makeNew();
464 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
466 while (s != NULL && *s != ';')
473 if (strcmp (c, "C") != 0)
475 llfatalbug(message("Error reading library. File may be corrupted"));
478 temp = constraint_undump (f);
479 ret = constraintList_add (ret, temp);
480 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
489 void constraintList_dump (/*@observer@*/ constraintList c, FILE *f)
491 constraintList_elements (c, el)
494 constraint_dump (el, f);
496 end_constraintList_elements; ;