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
28 ** based on list_template.c
30 ** where T has T_equal (or change this) and T_unparse
33 # include "lclintMacros.nf"
37 /*@iter constraintList_elements_private_only (sef constraintList x, yield only constraint el); @*/
38 # define constraintList_elements_private_only(x, m_el) \
39 { if (constraintList_isDefined (x)) { int m_ind; constraint *m_elements = &((x)->elements[0]); \
40 for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
41 { constraint m_el = *(m_elements++);
43 # define end_constraintList_elements_private_only }}}
46 /*@iter constraintList_elements_private (sef constraintList x, yield constraint el); @*/
47 # define constraintList_elements_private(x, m_el) \
48 { if (constraintList_isDefined (x)) { int m_ind; constraint *m_elements = &((x)->elements[0]); \
49 for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
50 { constraint m_el = *(m_elements++);
52 # define end_constraintList_elements_private }}}
55 /*@only@*/ constraintList constraintList_makeNew ()
57 constraintList s = (constraintList) dmalloc (sizeof (*s));
60 s->nspace = constraintListBASESIZE;
61 s->elements = (constraint *)
62 dmalloc (sizeof (*s->elements) * constraintListBASESIZE);
68 constraintList_grow (constraintList s)
71 constraint *newelements;
73 llassert (constraintList_isDefined (s));
75 s->nspace += constraintListBASESIZE;
76 newelements = (constraint *) dmalloc (sizeof (*newelements)
77 * (s->nelements + s->nspace));
79 for (i = 0; i < s->nelements; i++)
81 newelements[i] = s->elements[i];
85 s->elements = newelements;
90 constraintList_add (/*@returned@*/ constraintList s, /*@only@*/ constraint el)
92 llassert (constraintList_isDefined (s));
96 if (constraintList_resolve (el, s) )
103 constraintList_grow (s);
106 s->elements[s->nelements] = el;
111 /* frees everything but actual constraints */
112 /* This function should only be used if you have
113 other references to unshared constraints
115 static void constraintList_freeShallow (/*@only@*/ constraintList c)
117 if (constraintList_isDefined(c) )
128 /*@only@*/ constraintList constraintList_addList (/*@only@*/ /*@returned@*/ constraintList s, /*@observer@*/ constraintList newList)
130 llassert(constraintList_isDefined(s) );
131 llassert(constraintList_isDefined(newList) );
133 if (newList == constraintList_undefined)
136 constraintList_elements (newList, elem)
138 s = constraintList_add (s, constraint_copy(elem) );
140 end_constraintList_elements;
145 constraintList constraintList_addListFree (/*@returned@*/ constraintList s, /*@only@*/ constraintList newList)
147 llassert(constraintList_isDefined(s) );
148 llassert(constraintList_isDefined(newList) );
150 if (constraintList_isUndefined(newList) )
153 constraintList_elements_private_only(newList, elem)
155 s = constraintList_add (s, elem);
157 end_constraintList_elements_private_only
159 constraintList_freeShallow(newList);
164 constraintList constraintList_removeSurpressed (/*@only@*/ constraintList s)
168 llassert(constraintList_isDefined(s) );
170 ret = constraintList_makeNew();
172 constraintList_elements_private_only(s, elem)
174 loc = constraint_getFileloc(elem);
176 if(fileloc_isUndefined(loc) )
178 ret = constraintList_add (ret, elem);
181 else if (context_suppressFlagMsg(FLG_ARRAYBOUNDS, loc) )
183 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
184 constraint_free(elem);
187 else if ( (! constraint_hasMaxSet(elem) ) && context_suppressFlagMsg(FLG_ARRAYBOUNDSREAD, loc) )
189 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
190 constraint_free(elem);
194 ret = constraintList_add (ret, elem);
198 end_constraintList_elements_private_only;
200 constraintList_freeShallow(s);
206 extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/
208 return (constraintList_print(s));
212 static /*@only@*/ cstring
213 constraintList_printLocation (/*@temp@*/ constraintList s) /*@*/
216 cstring st = cstring_undefined;
219 if (!constraintList_isDefined (s))
221 return cstring_makeLiteral ("<undefined>");
224 if (s->nelements == 0)
226 st = cstring_makeLiteral("<List Empty>");
230 for (i = 0; i < s->nelements; i++)
232 cstring type = cstring_undefined;
233 constraint current = s->elements[i];
235 if (constraint_isDefined(current) )
238 temp1 = constraint_printLocation(current);
239 type = message ("%q %q\n", type, temp1 );
249 st = message ("%q, %q", st, type);
258 constraintList_print (/*@temp@*/ constraintList s) /*@*/
261 cstring st = cstring_undefined;
264 if (!constraintList_isDefined (s))
266 return cstring_makeLiteral ("<undefined>");
269 if (s->nelements == 0)
271 st = cstring_makeLiteral("<List Empty>");
275 for (i = 0; i < s->nelements; i++)
277 cstring type = cstring_undefined;
278 constraint current = s->elements[i];
280 if (constraint_isDefined(current) )
283 if ( context_getFlag (FLG_ORCONSTRAINT) )
284 temp1 = constraint_printOr(current);
286 temp1 = constraint_print(current);
287 type = message ("%q %q\n", type, temp1 );
297 st = message ("%q, %q", st, type);
304 void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
307 constraintList_elements (s, elem)
309 if (constraint_isDefined(elem))
311 constraint_printErrorPostCondition (elem, loc);
314 end_constraintList_elements;
318 void constraintList_printError (constraintList s, fileloc loc)
321 constraintList_elements (s, elem)
323 if (constraint_isDefined(elem) )
325 if (constraint_isPost(elem) )
326 constraint_printErrorPostCondition (elem, loc);
328 constraint_printError (elem, loc);
331 end_constraintList_elements;
337 constraintList_printDetailed (constraintList s)
340 cstring st = cstring_undefined;
343 if (!constraintList_isDefined (s))
345 return cstring_makeLiteral ("<undefined>");
348 if (s->nelements == 0)
350 st = cstring_makeLiteral("<List Empty>");
354 for (i = 0; i < s->nelements; i++)
356 cstring type = cstring_undefined;
357 constraint current = s->elements[i];
359 if (constraint_isDefined(current ) )
361 cstring temp1 = constraint_printDetailed (current);
362 type = message ("%s %s\n", type, temp1 );
374 st = message ("%q %q", st, type);
380 /*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
384 constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
388 DPRINTF ( (message ("Logical or on %s and %s",
389 constraintList_print(l1),
390 constraintList_print(l2)) ) );
392 ret = constraintList_makeNew();
393 constraintList_elements (l1, el)
395 temp = constraint_substitute (el, l2);
397 if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) )
398 { /*avoid redundant constraints*/
399 if (!constraintList_resolve (el, ret) )
402 temp2 = constraint_copy(el);
403 ret = constraintList_add (ret, temp2);
406 constraint_free(temp);
408 end_constraintList_elements;
410 constraintList_elements (l2, el)
412 temp = constraint_substitute (el, l1);
414 if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) )
416 /*avoid redundant constraints*/
417 if (!constraintList_resolve (el, ret) )
420 temp2 = constraint_copy(el);
421 ret = constraintList_add (ret, temp2);
424 constraint_free(temp);
426 end_constraintList_elements;
433 constraintList_free (/*@only@*/ constraintList s)
437 llassert(constraintList_isDefined(s) );
440 for (i = 0; i < s->nelements; i++)
442 constraint_free (s->elements[i]);
454 constraintList_copy (/*@observer@*/ /*@temp@*/ constraintList s)
456 constraintList ret = constraintList_makeNew ();
458 constraintList_elements (s, el)
460 ret = constraintList_add (ret, constraint_copy (el));
461 } end_constraintList_elements;
466 constraintList constraintList_preserveOrig (constraintList c)
468 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
470 constraintList_elements_private (c, el)
472 el = constraint_preserveOrig (el);
474 end_constraintList_elements_private;
478 constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@dependent@*/ /*@observer@*/ exprNode fcn)
480 DPRINTF((message("constraintList_preserveCallInfo %s ", constraintList_print (c) ) ));
482 constraintList_elements_private (c, el)
484 el = constraint_setFcnPre(el);
485 el = constraint_origAddGeneratingExpr (el, fcn);
487 end_constraintList_elements_private;
491 constraintList constraintList_single (constraint c)
494 res = constraintList_makeNew();
495 res = constraintList_add (res, c);
499 constraintList constraintList_addGeneratingExpr (constraintList c,/*@dependent@*/ exprNode e)
501 DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) ));
503 constraintList_elements_private (c, el)
505 DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(el), exprNode_unparse(e) ) ));
506 el = constraint_addGeneratingExpr (el, e);
508 end_constraintList_elements_private;
512 /*@only@*/ constraintList constraintList_doFixResult (/*@only@*/constraintList postconditions, exprNode fcnCall)
515 ret = constraintList_makeNew();
516 constraintList_elements_private (postconditions, el)
518 ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) );
520 end_constraintList_elements_private;
522 constraintList_free(postconditions);
526 /*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, /*@temp@*/ /*@observer@*/ exprNodeList arglist)
529 ret = constraintList_makeNew();
531 constraintList_elements (preconditions, el)
533 ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
535 end_constraintList_elements;
537 constraintList_free (preconditions);
541 constraintList constraintList_doSRefFixBaseParam (constraintList preconditions, /*@observer@*/
542 exprNodeList arglist)
546 ret = constraintList_makeNew();
548 constraintList_elements (preconditions, el)
550 temp = constraint_copy(el);
551 ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) );
553 end_constraintList_elements;
558 constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
560 constraintList_elements_private (c, el)
562 el = constraint_togglePost(el);
563 if (constraint_hasOrig(el) )
565 el = constraint_togglePostOrig (el);
568 end_constraintList_elements_private;
572 /*@only@*/ constraintList constraintList_undump (FILE *f)
575 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
578 ret = constraintList_makeNew();
581 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
583 while (s != NULL && *s != ';')
588 c = reader_getWord(&s);
590 if (strcmp (c, "C") != 0)
592 llfatalbug(message("Error reading library. File may be corrupted"));
595 temp = constraint_undump (f);
596 ret = constraintList_add (ret, temp);
597 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
606 void constraintList_dump (/*@observer@*/ constraintList c, FILE *f)
608 constraintList_elements (c, el)
611 constraint_dump (el, f);
613 end_constraintList_elements; ;
617 constraintList constraintList_sort (/*@returned@*/ constraintList ret)
619 qsort (ret->elements, (size_t) ret->nelements,
620 (sizeof (*ret->elements)),
621 (int (*)(const void *, const void *)) constraint_compare);
623 DPRINTF((message("onstraint_sort returning") ));