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 { 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 { 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 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 s->nspace += constraintListBASESIZE;
73 newelements = (constraint *) dmalloc (sizeof (*newelements)
74 * (s->nelements + s->nspace));
76 for (i = 0; i < s->nelements; i++)
78 newelements[i] = s->elements[i];
82 s->elements = newelements;
87 constraintList_add (/*@returned@*/ constraintList s, /*@only@*/ constraint el)
90 // el = constraint_simplify (el);
91 if (constraintList_resolve (el, s) )
98 constraintList_grow (s);
101 s->elements[s->nelements] = el;
106 /* frees everything but actual constraints */
107 /* This function should only be used if you have
108 other references to unshared constraints
110 static void constraintList_freeShallow (/*@only@*/ constraintList c)
112 if (constraintList_isDefined(c) )
123 /*@only@*/ constraintList constraintList_addList (/*@only@*/ /*@returned@*/ constraintList s, /*@observer@*/ constraintList newList)
125 llassert(constraintList_isDefined(s) );
126 llassert(constraintList_isDefined(newList) );
128 if (newList == constraintList_undefined)
131 constraintList_elements (newList, elem)
133 s = constraintList_add (s, constraint_copy(elem) );
135 end_constraintList_elements;
140 constraintList constraintList_addListFree (/*@returned@*/ constraintList s, /*@only@*/ constraintList newList)
142 llassert(constraintList_isDefined(s) );
143 llassert(constraintList_isDefined(newList) );
145 if (constraintList_isUndefined(newList) )
148 constraintList_elements_private_only(newList, elem)
150 s = constraintList_add (s, elem);
152 end_constraintList_elements_private_only
154 constraintList_freeShallow(newList);
159 extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/
161 return (constraintList_print(s));
168 constraintList_print (/*@temp@*/ constraintList s) /*@*/
171 cstring st = cstring_undefined;
174 if (s->nelements == 0)
176 st = cstring_makeLiteral("<List Empty>");
180 for (i = 0; i < s->nelements; i++)
182 cstring type = cstring_undefined;
183 constraint current = s->elements[i];
185 if (constraint_isDefined(current) )
188 if ( context_getFlag (FLG_ORCONSTRAINT) )
189 temp1 = constraint_printOr(current);
191 temp1 = constraint_print(current);
192 type = message ("%q %q\n", type, temp1 );
202 st = message ("%q, %q", st, type);
209 void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
212 constraintList_elements (s, elem)
214 if (constraint_isDefined(elem))
216 constraint_printErrorPostCondition (elem, loc);
219 end_constraintList_elements;
223 void constraintList_printError (constraintList s, fileloc loc)
226 constraintList_elements (s, elem)
228 if (constraint_isDefined(elem) )
230 constraint_printError (elem, loc);
233 end_constraintList_elements;
239 constraintList_printDetailed (constraintList s)
242 cstring st = cstring_undefined;
245 if (s->nelements == 0)
247 st = cstring_makeLiteral("<List Empty>");
251 for (i = 0; i < s->nelements; i++)
253 cstring type = cstring_undefined;
254 constraint current = s->elements[i];
256 if (constraint_isDefined(current ) )
258 cstring temp1 = constraint_printDetailed (current);
259 type = message ("%s %s\n", type, temp1 );
271 st = message ("%q %q", st, type);
277 /*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
281 constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
285 DPRINTF ( (message ("Logical or on %s and %s",
286 constraintList_print(l1),
287 constraintList_print(l2)) ) );
289 ret = constraintList_makeNew();
290 constraintList_elements (l1, el)
292 temp = constraint_substitute (el, l2);
294 if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) )
295 { /*avoid redundant constraints*/
296 if (!constraintList_resolve (el, ret) )
299 temp2 = constraint_copy(el);
300 ret = constraintList_add (ret, temp2);
303 constraint_free(temp);
305 end_constraintList_elements;
307 constraintList_elements (l2, el)
309 temp = constraint_substitute (el, l1);
311 if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) )
313 /*avoid redundant constraints*/
314 if (!constraintList_resolve (el, ret) )
317 temp2 = constraint_copy(el);
318 ret = constraintList_add (ret, temp2);
321 constraint_free(temp);
323 end_constraintList_elements;
330 constraintList_free (/*@only@*/ constraintList s)
334 llassert(constraintList_isDefined(s) );
337 for (i = 0; i < s->nelements; i++)
339 constraint_free (s->elements[i]);
351 constraintList_copy (/*@oberserver@*/ /*@temp@*/ constraintList s)
353 constraintList ret = constraintList_makeNew ();
355 constraintList_elements (s, el)
357 ret = constraintList_add (ret, constraint_copy (el));
358 } end_constraintList_elements;
363 constraintList constraintList_preserveOrig (constraintList c)
365 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
367 constraintList_elements_private (c, el)
369 el = constraint_preserveOrig (el);
371 end_constraintList_elements_private;
375 constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@depenent@*/ /*@observer@*/ exprNode fcn)
377 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
379 constraintList_elements_private (c, el)
381 // el = constraint_preserveOrig (el);
382 el = constraint_setFcnPre(el);
383 el = constraint_origAddGeneratingExpr (el, fcn);
385 end_constraintList_elements_private;
391 constraintList constraintList_addGeneratingExpr (constraintList c,/*@dependent@*/ exprNode e)
393 DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) ));
395 constraintList_elements_private (c, el)
397 DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(el), exprNode_unparse(e) ) ));
398 el = constraint_addGeneratingExpr (el, e);
400 end_constraintList_elements_private;
404 /*@only@*/ constraintList constraintList_doFixResult (/*@only@*/constraintList postconditions, exprNode fcnCall)
407 ret = constraintList_makeNew();
408 constraintList_elements_private (postconditions, el)
410 ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) );
412 end_constraintList_elements_private;
414 constraintList_free(postconditions);
418 /*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, /*@temp@*/ /*@observer@*/ exprNodeList arglist)
421 ret = constraintList_makeNew();
423 constraintList_elements (preconditions, el)
425 ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
427 end_constraintList_elements;
429 constraintList_free (preconditions);
433 constraintList constraintList_doSRefFixBaseParam (constraintList preconditions, /*@observer@*/
434 exprNodeList arglist)
438 ret = constraintList_makeNew();
440 constraintList_elements (preconditions, el)
442 temp = constraint_copy(el);
443 ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) );
445 end_constraintList_elements;
450 constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
452 constraintList_elements_private (c, el)
454 el = constraint_togglePost(el);
455 if (constraint_hasOrig(el) )
457 el = constraint_togglePostOrig (el);
460 end_constraintList_elements_private;
464 /*@only@*/ constraintList constraintList_undump (FILE *f)
467 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
470 ret = constraintList_makeNew();
473 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
475 while (s != NULL && *s != ';')
480 c = reader_getWord(&s);
482 if (strcmp (c, "C") != 0)
484 llfatalbug(message("Error reading library. File may be corrupted"));
487 temp = constraint_undump (f);
488 ret = constraintList_add (ret, temp);
489 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
498 void constraintList_dump (/*@observer@*/ constraintList c, FILE *f)
500 constraintList_elements (c, el)
503 constraint_dump (el, f);
505 end_constraintList_elements; ;