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_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));
94 // el = constraint_simplify (el);
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@*/ 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 llassert(constraintList_isDefined(s) );
147 llassert(constraintList_isDefined(newList) );
149 if (constraintList_isUndefined(newList) )
152 constraintList_elements_private_only(newList, elem)
154 s = constraintList_add (s, elem);
156 end_constraintList_elements_private_only
158 constraintList_freeShallow(newList);
163 constraintList constraintList_removeSurpressed (/*@only@*/ constraintList s)
167 llassert(constraintList_isDefined(s) );
169 ret = constraintList_makeNew();
171 constraintList_elements_private_only(s, elem)
173 loc = constraint_getFileloc(elem);
175 if(fileloc_isUndefined(loc) )
177 ret = constraintList_add (ret, elem);
180 else if (context_suppressFlagMsg(FLG_ARRAYBOUNDS, loc) )
182 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
183 constraint_free(elem);
186 else if ( (! constraint_hasMaxSet(elem) ) && context_suppressFlagMsg(FLG_ARRAYBOUNDSREAD, loc) )
188 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
189 constraint_free(elem);
193 ret = constraintList_add (ret, elem);
197 end_constraintList_elements_private_only;
199 constraintList_freeShallow(s);
205 extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/
207 return (constraintList_print(s));
211 static /*@only@*/ cstring
212 constraintList_printLocation (/*@temp@*/ constraintList s) /*@*/
215 cstring st = cstring_undefined;
218 if (!constraintList_isDefined (s))
220 return cstring_makeLiteral ("<undefined>");
223 if (s->nelements == 0)
225 st = cstring_makeLiteral("<List Empty>");
229 for (i = 0; i < s->nelements; i++)
231 cstring type = cstring_undefined;
232 constraint current = s->elements[i];
234 if (constraint_isDefined(current) )
237 temp1 = constraint_printLocation(current);
238 type = message ("%q %q\n", type, temp1 );
248 st = message ("%q, %q", st, type);
257 constraintList_print (/*@temp@*/ constraintList s) /*@*/
260 cstring st = cstring_undefined;
263 if (!constraintList_isDefined (s))
265 return cstring_makeLiteral ("<undefined>");
268 if (s->nelements == 0)
270 st = cstring_makeLiteral("<List Empty>");
274 for (i = 0; i < s->nelements; i++)
276 cstring type = cstring_undefined;
277 constraint current = s->elements[i];
279 if (constraint_isDefined(current) )
282 if ( context_getFlag (FLG_ORCONSTRAINT) )
283 temp1 = constraint_printOr(current);
285 temp1 = constraint_print(current);
286 type = message ("%q %q\n", type, temp1 );
296 st = message ("%q, %q", st, type);
303 void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
306 constraintList_elements (s, elem)
308 if (constraint_isDefined(elem))
310 constraint_printErrorPostCondition (elem, loc);
313 end_constraintList_elements;
317 void constraintList_printError (constraintList s, fileloc loc)
320 constraintList_elements (s, elem)
322 if (constraint_isDefined(elem) )
324 if (constraint_isPost(elem) )
325 constraint_printErrorPostCondition (elem, loc);
327 constraint_printError (elem, loc);
330 end_constraintList_elements;
336 constraintList_printDetailed (constraintList s)
339 cstring st = cstring_undefined;
342 if (!constraintList_isDefined (s))
344 return cstring_makeLiteral ("<undefined>");
347 if (s->nelements == 0)
349 st = cstring_makeLiteral("<List Empty>");
353 for (i = 0; i < s->nelements; i++)
355 cstring type = cstring_undefined;
356 constraint current = s->elements[i];
358 if (constraint_isDefined(current ) )
360 cstring temp1 = constraint_printDetailed (current);
361 type = message ("%s %s\n", type, temp1 );
373 st = message ("%q %q", st, type);
379 /*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
383 constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
387 DPRINTF ( (message ("Logical or on %s and %s",
388 constraintList_print(l1),
389 constraintList_print(l2)) ) );
391 ret = constraintList_makeNew();
392 constraintList_elements (l1, el)
394 temp = constraint_substitute (el, l2);
396 if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) )
397 { /*avoid redundant constraints*/
398 if (!constraintList_resolve (el, ret) )
401 temp2 = constraint_copy(el);
402 ret = constraintList_add (ret, temp2);
405 constraint_free(temp);
407 end_constraintList_elements;
409 constraintList_elements (l2, el)
411 temp = constraint_substitute (el, l1);
413 if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) )
415 /*avoid redundant constraints*/
416 if (!constraintList_resolve (el, ret) )
419 temp2 = constraint_copy(el);
420 ret = constraintList_add (ret, temp2);
423 constraint_free(temp);
425 end_constraintList_elements;
432 constraintList_free (/*@only@*/ constraintList s)
436 llassert(constraintList_isDefined(s) );
439 for (i = 0; i < s->nelements; i++)
441 constraint_free (s->elements[i]);
453 constraintList_copy (/*@observer@*/ /*@temp@*/ constraintList s)
455 constraintList ret = constraintList_makeNew ();
457 constraintList_elements (s, el)
459 ret = constraintList_add (ret, constraint_copy (el));
460 } end_constraintList_elements;
465 constraintList constraintList_preserveOrig (constraintList c)
467 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
469 constraintList_elements_private (c, el)
471 el = constraint_preserveOrig (el);
473 end_constraintList_elements_private;
477 constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@dependent@*/ /*@observer@*/ exprNode fcn)
479 DPRINTF((message("constraintList_preserveCallInfo %s ", constraintList_print (c) ) ));
481 constraintList_elements_private (c, el)
483 // el = constraint_preserveOrig (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") ));