2 ** Splint - 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 splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 ** based on list_template.c
30 ** where T has T_equal (or change this) and T_unparse
33 # include "splintMacros.nf"
36 /*@iter constraintList_elements_private_only (sef constraintList x, yield only constraint el); @*/
37 # define constraintList_elements_private_only(x, m_el) \
38 { if (constraintList_isDefined (x)) { 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_only }}}
45 /*@iter constraintList_elements_private (sef constraintList x, yield constraint el); @*/
46 # define constraintList_elements_private(x, m_el) \
47 { if (constraintList_isDefined (x)) { int m_ind; constraint *m_elements = &((x)->elements[0]); \
48 for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
49 { constraint m_el = *(m_elements++);
51 # define end_constraintList_elements_private }}}
54 /*@only@*/ constraintList constraintList_makeNew ()
56 constraintList s = (constraintList) dmalloc (sizeof (*s));
59 s->nspace = constraintListBASESIZE;
60 s->elements = (constraint *)
61 dmalloc (sizeof (*s->elements) * constraintListBASESIZE);
67 constraintList_grow (constraintList s)
70 constraint *newelements;
72 llassert (constraintList_isDefined (s));
74 s->nspace += constraintListBASESIZE;
75 newelements = (constraint *) dmalloc (sizeof (*newelements)
76 * (s->nelements + s->nspace));
78 for (i = 0; i < s->nelements; i++)
80 newelements[i] = s->elements[i];
84 s->elements = newelements;
89 constraintList_add (/*@returned@*/ constraintList s, /*@only@*/ constraint el)
91 llassert (constraintList_isDefined (s));
95 if (constraintList_resolve (el, s) )
102 constraintList_grow (s);
105 s->elements[s->nelements] = el;
110 /* frees everything but actual constraints */
111 /* This function should only be used if you have
112 other references to unshared constraints
114 static void constraintList_freeShallow (/*@only@*/ constraintList c)
116 if (constraintList_isDefined(c) )
127 /*@only@*/ constraintList constraintList_addList (/*@only@*/ /*@returned@*/ constraintList s, /*@observer@*/ /*@temp@*/ constraintList newList)
129 llassert(constraintList_isDefined(s) );
130 llassert(constraintList_isDefined(newList) );
132 if (newList == constraintList_undefined)
135 constraintList_elements (newList, elem)
137 s = constraintList_add (s, constraint_copy(elem) );
139 end_constraintList_elements;
144 constraintList constraintList_addListFree (/*@returned@*/ constraintList s, /*@only@*/ constraintList newList)
146 if (constraintList_isUndefined (newList))
149 llassert (constraintList_isDefined (s));
150 llassert (constraintList_isDefined (newList));
152 constraintList_elements_private_only(newList, elem)
154 s = constraintList_add (s, elem);
155 } end_constraintList_elements_private_only;
157 constraintList_freeShallow (newList);
161 constraintList constraintList_removeSurpressed (/*@only@*/ constraintList s)
166 llassert (constraintList_isDefined (s));
167 ret = constraintList_makeNew();
169 constraintList_elements_private_only (s, elem)
171 loc = constraint_getFileloc(elem);
173 if (fileloc_isUndefined(loc))
175 ret = constraintList_add (ret, elem);
177 else if (context_suppressFlagMsg(FLG_BOUNDSWRITE, loc) )
179 DPRINTF ((message ("constraintList_removeSurpressed getting rid of surpressed constraint %q",
180 constraint_unparse(elem))));
181 constraint_free(elem);
183 else if (!constraint_hasMaxSet(elem) && context_suppressFlagMsg(FLG_BOUNDSREAD, loc))
185 DPRINTF ((message("constraintList_removeSurpressed getting rid of surpressed constraint %q",
186 constraint_unparse(elem))));
187 constraint_free(elem);
191 ret = constraintList_add (ret, elem);
194 } end_constraintList_elements_private_only;
196 constraintList_freeShallow(s);
201 static /*@only@*/ cstring
202 constraintList_unparseLocation (/*@temp@*/ constraintList s) /*@*/
205 cstring st = cstring_undefined;
208 if (!constraintList_isDefined (s))
210 return cstring_makeLiteral ("<undefined>");
213 if (s->nelements == 0)
215 st = cstring_makeLiteral("<List Empty>");
219 for (i = 0; i < s->nelements; i++)
221 cstring type = cstring_undefined;
222 constraint current = s->elements[i];
224 if (constraint_isDefined(current) )
227 temp1 = constraint_unparseLocation(current);
228 type = message ("%q %q\n", type, temp1 );
238 st = message ("%q, %q", st, type);
247 constraintList_unparse (/*@temp@*/ constraintList s) /*@*/
250 cstring st = cstring_undefined;
253 if (!constraintList_isDefined (s))
255 return cstring_makeLiteral ("<undefined>");
258 if (s->nelements == 0)
260 st = cstring_makeLiteral("<List Empty>");
264 for (i = 0; i < s->nelements; i++)
266 cstring type = cstring_undefined;
267 constraint current = s->elements[i];
269 if (constraint_isDefined(current) )
273 if (context_getFlag (FLG_ORCONSTRAINT))
275 temp1 = constraint_unparseOr (current);
279 temp1 = constraint_unparse (current);
281 type = message ("%q %q\n", type, temp1 );
291 st = message ("%q, %q", st, type);
298 void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
301 constraintList_elements (s, elem)
303 if (constraint_isDefined(elem))
305 constraint_printErrorPostCondition (elem, loc);
308 end_constraintList_elements;
312 void constraintList_printError (constraintList s, fileloc loc)
315 constraintList_elements (s, elem)
317 if (constraint_isDefined(elem) )
319 if (constraint_isPost(elem) )
320 constraint_printErrorPostCondition (elem, loc);
322 constraint_printError (elem, loc);
325 end_constraintList_elements;
331 constraintList_unparseDetailed (constraintList s)
334 cstring st = cstring_undefined;
337 if (!constraintList_isDefined (s))
339 return cstring_makeLiteral ("<undefined>");
342 if (s->nelements == 0)
344 st = cstring_makeLiteral("<List Empty>");
348 for (i = 0; i < s->nelements; i++)
350 cstring type = cstring_undefined;
351 constraint current = s->elements[i];
353 if (constraint_isDefined(current ) )
355 cstring temp1 = constraint_unparseDetailed (current);
356 type = message ("%s %s\n", type, temp1 );
368 st = message ("%q %q", st, type);
374 /*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
378 constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
382 DPRINTF ((message ("Logical or on %s and %s",
383 constraintList_unparse(l1),
384 constraintList_unparse(l2)) ) );
386 ret = constraintList_makeNew();
387 constraintList_elements (l1, el)
389 temp = constraint_substitute (el, l2);
391 if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) )
392 { /*avoid redundant constraints*/
393 if (!constraintList_resolve (el, ret) )
396 temp2 = constraint_copy(el);
397 ret = constraintList_add (ret, temp2);
400 constraint_free(temp);
402 end_constraintList_elements;
404 constraintList_elements (l2, el)
406 temp = constraint_substitute (el, l1);
408 if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) )
410 /*avoid redundant constraints*/
411 if (!constraintList_resolve (el, ret) )
414 temp2 = constraint_copy(el);
415 ret = constraintList_add (ret, temp2);
418 constraint_free(temp);
420 end_constraintList_elements;
427 constraintList_free (/*@only@*/ constraintList s)
429 if (constraintList_isDefined (s))
433 for (i = 0; i < s->nelements; i++)
435 constraint_free (s->elements[i]);
448 constraintList_copy (/*@observer@*/ /*@temp@*/ constraintList s)
450 constraintList ret = constraintList_makeNew ();
452 constraintList_elements (s, el)
454 ret = constraintList_add (ret, constraint_copy (el));
455 } end_constraintList_elements;
460 constraintList constraintList_preserveOrig (constraintList c)
462 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_unparse (c) ) ));
464 constraintList_elements_private (c, el)
466 el = constraint_preserveOrig (el);
468 end_constraintList_elements_private;
472 constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@dependent@*/ /*@observer@*/ exprNode fcn)
474 DPRINTF((message("constraintList_preserveCallInfo %s ", constraintList_unparse (c) ) ));
476 constraintList_elements_private (c, el)
478 el = constraint_setFcnPre(el);
479 el = constraint_origAddGeneratingExpr (el, fcn);
481 end_constraintList_elements_private;
485 constraintList constraintList_single (constraint c)
488 res = constraintList_makeNew();
489 res = constraintList_add (res, c);
493 constraintList constraintList_addGeneratingExpr (constraintList c,/*@dependent@*/ exprNode e)
495 DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) ));
497 constraintList_elements_private (c, el)
499 DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_unparse(el), exprNode_unparse(e) ) ));
500 el = constraint_addGeneratingExpr (el, e);
502 end_constraintList_elements_private;
506 /*@only@*/ constraintList constraintList_doFixResult (/*@only@*/constraintList postconditions, exprNode fcnCall)
509 ret = constraintList_makeNew();
510 constraintList_elements_private (postconditions, el)
512 ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) );
514 end_constraintList_elements_private;
516 constraintList_free(postconditions);
520 Commenting out because function is not yet stable
522 / *@only@* / constraintList constraintList_doSRefFixStructConstraint(constraintList invars, sRef s, ctype ct )
525 ret = constraintList_makeNew();
527 constraintList_elements (invars, el)
529 ret = constraintList_add(ret, constraint_doSRefFixInvarConstraint (el, s, ct) );
531 end_constraintList_elements;
533 / * constraintList_free (invars);* /
539 /*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, /*@temp@*/ /*@observer@*/ exprNodeList arglist)
542 ret = constraintList_makeNew();
544 constraintList_elements (preconditions, el)
546 ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
548 end_constraintList_elements;
550 constraintList_free (preconditions);
554 constraintList constraintList_doSRefFixBaseParam (constraintList preconditions, /*@observer@*/
555 exprNodeList arglist)
559 ret = constraintList_makeNew();
561 constraintList_elements (preconditions, el)
563 temp = constraint_copy(el);
564 ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) );
566 end_constraintList_elements;
571 constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
573 constraintList_elements_private (c, el)
575 el = constraint_togglePost(el);
576 if (constraint_hasOrig(el) )
578 el = constraint_togglePostOrig (el);
581 end_constraintList_elements_private;
585 /*@only@*/ constraintList constraintList_undump (FILE *f)
591 ret = constraintList_makeNew();
593 os = mstring_create (MAX_DUMP_LINE_LENGTH);
594 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
596 while (s != NULL && *s != ';')
601 c = reader_getWord(&s);
603 if (! mstring_isDefined(c) )
605 llfatalbug(message("Library file is corrupted") );
609 if (strcmp (c, "C") != 0)
611 llfatalbug(message("Error reading library. File may be corrupted"));
614 temp = constraint_undump (f);
615 ret = constraintList_add (ret, temp);
616 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
625 void constraintList_dump (/*@observer@*/ constraintList c, FILE *f)
627 constraintList_elements (c, el)
630 constraint_dump (el, f);
632 end_constraintList_elements; ;
636 constraintList constraintList_sort (/*@returned@*/ constraintList ret)
638 if (constraintList_isUndefined(ret) )
644 qsort (ret->elements, (size_t) ret->nelements,
645 (sizeof (*ret->elements)),
646 (int (*)(const void *, const void *)) constraint_compare);
648 DPRINTF((message("onstraint_sort returning") ));