]> andersk Git - splint.git/blob - src/constraintList.c
Temporarily set flags to splintme without warnings.
[splint.git] / src / constraintList.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
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.
10 ** 
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.
15 ** 
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.
19 **
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
23 */
24
25 /*
26 ** constraintList.c
27 **
28 ** based on list_template.c
29 **
30 ** where T has T_equal (or change this) and T_unparse
31 */
32
33 # include "splintMacros.nf"
34 # include "llbasic.h"
35
36 /*@-nullderef@*/ /* !!! DRL needs to fix this code! */
37 /*@-nullstate@*/ /* !!! DRL needs to fix this code! */
38 /*@-nullpass@*/ /* !!! DRL needs to fix this code! */
39 /*@-temptrans@*/ /* !!! DRL needs to fix this code! */
40
41 /*@iter constraintList_elements_private_only (sef constraintList x, yield only constraint el); @*/
42 # define constraintList_elements_private_only(x, m_el) \
43    { if (constraintList_isDefined (x)) { int m_ind; constraint *m_elements = &((x)->elements[0]); \
44      for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
45        { constraint m_el = *(m_elements++); 
46
47 # define end_constraintList_elements_private_only }}}
48
49
50 /*@iter constraintList_elements_private (sef constraintList x, yield  constraint el); @*/
51 # define constraintList_elements_private(x, m_el) \
52    { if (constraintList_isDefined (x)) { int m_ind; constraint *m_elements = &((x)->elements[0]); \
53      for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
54        { constraint m_el = *(m_elements++); 
55
56 # define end_constraintList_elements_private }}}
57
58
59 /*@only@*/ constraintList constraintList_makeNew ()
60 {
61   constraintList s = (constraintList) dmalloc (sizeof (*s));
62
63   s->nelements = 0;
64   s->nspace = constraintListBASESIZE;
65   s->elements = (constraint *)
66     dmalloc (sizeof (*s->elements) * constraintListBASESIZE);
67
68   return (s);
69 }
70
71 static void
72 constraintList_grow (constraintList s)
73 {
74   int i;
75   constraint *newelements; 
76
77   llassert (constraintList_isDefined (s));
78
79   s->nspace += constraintListBASESIZE;
80   newelements = (constraint *) dmalloc (sizeof (*newelements)
81                                      * (s->nelements + s->nspace));
82
83   for (i = 0; i < s->nelements; i++)
84     {
85       newelements[i] = s->elements[i]; 
86     }
87
88   sfree (s->elements); 
89   s->elements = newelements;
90 }
91
92
93 constraintList 
94 constraintList_add (/*@returned@*/ constraintList s, /*@only@*/ constraint el)
95 {
96   llassert (constraintList_isDefined (s));
97
98   /*drl7x */
99
100   if (constraintList_resolve (el, s) )
101     {
102       constraint_free (el);
103       return s;
104     }
105   
106   if (s->nspace <= 0)
107     constraintList_grow (s);
108
109   s->nspace--;
110   s->elements[s->nelements] = el;
111   s->nelements++;
112   return s;
113 }
114
115 /* frees everything but actual constraints */
116 /* This function should only be used if you have 
117    other references to unshared constraints 
118 */
119 static void constraintList_freeShallow (/*@only@*/ constraintList c)
120 {
121   if (constraintList_isDefined(c) )
122     {
123       free (c->elements);
124       c->elements = NULL;
125       c->nelements = -1;
126       c->nspace = -1;
127     }
128   free (c);
129   c = NULL;
130 }
131
132 /*@only@*/ constraintList constraintList_addList (/*@only@*/ /*@returned@*/ constraintList s, /*@observer@*/ /*@temp@*/ constraintList newList)
133 {
134   llassert(constraintList_isDefined(s) );
135   llassert(constraintList_isDefined(newList) );
136
137   if (newList == constraintList_undefined)
138     return s;
139   
140   constraintList_elements (newList, elem)
141     {
142     s = constraintList_add (s, constraint_copy(elem) );
143     }
144   end_constraintList_elements;
145
146   return s;
147 }
148
149 constraintList constraintList_addListFree (/*@returned@*/ constraintList s, /*@only@*/ constraintList newList)
150 {
151   llassert(constraintList_isDefined(s) );
152   llassert(constraintList_isDefined(newList) );
153
154   if (constraintList_isUndefined(newList) )
155     return s;
156   
157   constraintList_elements_private_only(newList, elem)
158     {
159     s = constraintList_add (s, elem);
160     }
161   end_constraintList_elements_private_only
162
163     constraintList_freeShallow(newList);
164     return s;
165 }
166
167
168 constraintList constraintList_removeSurpressed (/*@only@*/ constraintList s)
169 {
170   constraintList ret;
171   fileloc loc;
172   llassert(constraintList_isDefined(s) );
173
174   ret = constraintList_makeNew();
175   
176   constraintList_elements_private_only(s, elem)
177     {
178       loc = constraint_getFileloc(elem);
179
180       if (fileloc_isUndefined(loc))
181         {
182           ret = constraintList_add (ret, elem);
183         }
184       
185       else if (context_suppressFlagMsg(FLG_BOUNDSWRITE, loc) )
186         {
187           DPRINTF ((message ("constraintList_removeSurpressed getting rid of surpressed constraint %q", 
188                              constraint_print(elem))));
189           constraint_free(elem);
190         }
191       
192       else if (!constraint_hasMaxSet(elem) && context_suppressFlagMsg(FLG_BOUNDSREAD, loc))
193         {
194           DPRINTF ((message("constraintList_removeSurpressed getting rid of surpressed constraint %q", 
195                             constraint_print(elem))));
196           constraint_free(elem);
197         }
198       else
199         {
200           ret = constraintList_add (ret, elem);
201         } 
202       fileloc_free(loc);
203     } 
204   end_constraintList_elements_private_only;
205
206   constraintList_freeShallow(s);
207   
208   return ret;
209 }
210
211
212 extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/
213 {
214   return (constraintList_print(s));
215 }
216
217 # if 0
218 static /*@only@*/ cstring
219 constraintList_printLocation (/*@temp@*/ constraintList s) /*@*/
220 {
221   int i;
222   cstring st = cstring_undefined;
223   bool first = TRUE;
224   
225   if (!constraintList_isDefined (s))
226     {
227       return cstring_makeLiteral ("<undefined>");
228     }
229
230   if (s->nelements == 0)
231     {
232       st = cstring_makeLiteral("<List Empty>");
233       return st;
234     }
235
236   for (i = 0; i < s->nelements; i++)
237     {
238       cstring type = cstring_undefined;
239       constraint current = s->elements[i];
240
241       if (constraint_isDefined(current) )
242         {
243           cstring temp1;
244               temp1 = constraint_printLocation(current);
245           type = message ("%q %q\n", type, temp1 );
246         }
247
248       if (first)
249         {
250           st = type;
251           first = FALSE;
252         }
253       else
254         {
255           st = message ("%q, %q", st, type);
256         }
257     } 
258
259   return st;
260 }
261 # endif
262
263 /*@only@*/ cstring
264 constraintList_print (/*@temp@*/ constraintList s) /*@*/
265 {
266   int i;
267   cstring st = cstring_undefined;
268   bool first = TRUE;
269   
270   if (!constraintList_isDefined (s))
271     {
272       return cstring_makeLiteral ("<undefined>");
273     }
274
275   if (s->nelements == 0)
276     {
277       st = cstring_makeLiteral("<List Empty>");
278       return st;
279     }
280
281   for (i = 0; i < s->nelements; i++)
282     {
283       cstring type = cstring_undefined;
284       constraint current = s->elements[i];
285
286       if (constraint_isDefined(current) )
287         {
288           cstring temp1;
289             if ( context_getFlag (FLG_ORCONSTRAINT) )
290               temp1 = constraint_printOr(current);
291             else
292               temp1 = constraint_print(current);
293           type = message ("%q %q\n", type, temp1 );
294         }
295
296       if (first)
297         {
298           st = type;
299           first = FALSE;
300         }
301       else
302         {
303           st = message ("%q, %q", st, type);
304         }
305     } 
306
307   return st;
308 }
309
310 void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
311 {
312
313   constraintList_elements (s, elem)
314     {
315       if (constraint_isDefined(elem))
316         {
317           constraint_printErrorPostCondition (elem, loc);
318         }
319     }
320   end_constraintList_elements;
321   return;
322 }
323
324 void constraintList_printError (constraintList s, fileloc loc)
325 {
326
327   constraintList_elements (s, elem)
328     {
329       if (constraint_isDefined(elem) )
330         {
331           if (constraint_isPost(elem) )
332             constraint_printErrorPostCondition (elem, loc);
333           else
334             constraint_printError (elem, loc);
335         }
336     }
337   end_constraintList_elements;
338   return;
339 }
340
341
342 cstring
343 constraintList_printDetailed (constraintList s)
344 {
345   int i;
346   cstring st = cstring_undefined;
347   bool first = TRUE;
348
349   if (!constraintList_isDefined (s))
350     {
351       return cstring_makeLiteral ("<undefined>");
352     }
353
354   if (s->nelements == 0)
355     {
356       st = cstring_makeLiteral("<List Empty>");
357       return st;
358     }
359
360   for (i = 0; i < s->nelements; i++)
361     {
362       cstring type = cstring_undefined;
363       constraint current = s->elements[i];
364
365       if (constraint_isDefined(current ) )
366         {
367           cstring temp1 = constraint_printDetailed (current);
368           type = message ("%s %s\n", type, temp1 );
369           cstring_free(temp1);
370         }
371
372       if (first)
373         {
374           st = type;
375           first = FALSE;
376           type = NULL;
377         }
378       else
379         {
380           st = message ("%q %q", st, type);
381         }
382     }
383   return st;
384 }
385
386 /*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
387 } */
388
389 constraintList
390 constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
391 {
392   constraint temp;
393   constraintList ret;
394   DPRINTF ((message ("Logical or on %s and %s",
395                       constraintList_print(l1), 
396                       constraintList_print(l2)) ) );
397   
398   ret = constraintList_makeNew();
399   constraintList_elements (l1, el)
400     {
401       temp = constraint_substitute (el, l2);
402       
403       if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) )
404         {   /*avoid redundant constraints*/
405           if (!constraintList_resolve (el, ret) )
406             {
407               constraint temp2;
408               temp2 = constraint_copy(el);
409               ret = constraintList_add (ret, temp2);
410             }
411         }
412       constraint_free(temp);
413     }
414   end_constraintList_elements;
415
416    constraintList_elements (l2, el)
417     {
418       temp = constraint_substitute (el, l1);
419       
420       if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) )
421         {
422           /*avoid redundant constraints*/
423           if (!constraintList_resolve (el, ret) )
424             {
425               constraint temp2;
426               temp2 = constraint_copy(el);
427               ret = constraintList_add (ret, temp2);
428             }
429         }
430       constraint_free(temp);
431     }
432   end_constraintList_elements;
433
434   
435   return ret;
436 }
437
438 void
439 constraintList_free (/*@only@*/ constraintList s)
440 {
441   int i;
442
443   llassert(constraintList_isDefined(s) );
444
445   
446   for (i = 0; i < s->nelements; i++)
447     {
448       constraint_free (s->elements[i]); 
449     }
450
451   sfree (s->elements);
452   s->elements = NULL;
453   s->nelements = -1;
454   s->nspace = -1;
455   sfree (s);
456   s = NULL;
457 }
458
459 constraintList
460 constraintList_copy (/*@observer@*/ /*@temp@*/ constraintList s)
461 {
462   constraintList ret = constraintList_makeNew ();
463
464   constraintList_elements (s, el)
465     {
466       ret = constraintList_add (ret, constraint_copy (el));
467     } end_constraintList_elements;
468
469   return ret;
470 }
471
472 constraintList constraintList_preserveOrig (constraintList c)
473 {
474   DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
475
476   constraintList_elements_private (c, el)
477   {
478     el = constraint_preserveOrig (el);
479   }
480   end_constraintList_elements_private;
481   return c;
482 }
483
484 constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@dependent@*/ /*@observer@*/  exprNode fcn)
485 {
486   DPRINTF((message("constraintList_preserveCallInfo %s ", constraintList_print (c) ) ));
487
488   constraintList_elements_private (c, el)
489   {
490     el = constraint_setFcnPre(el);
491     el = constraint_origAddGeneratingExpr (el, fcn);
492   }
493   end_constraintList_elements_private;
494   return c;
495 }
496
497 constraintList constraintList_single (constraint c)
498 {
499   constraintList res;
500   res = constraintList_makeNew();
501   res = constraintList_add (res, c);
502   return res;
503 }
504
505 constraintList constraintList_addGeneratingExpr (constraintList c,/*@dependent@*/ exprNode e)
506 {
507   DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) ));
508   
509   constraintList_elements_private (c, el)
510   {
511     DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(el), exprNode_unparse(e) )  ));
512     el = constraint_addGeneratingExpr (el, e);
513   }
514   end_constraintList_elements_private;
515   return c;
516 }
517
518 /*@only@*/ constraintList constraintList_doFixResult (/*@only@*/constraintList postconditions, exprNode fcnCall)
519 {
520   constraintList ret;
521   ret = constraintList_makeNew();
522   constraintList_elements_private (postconditions, el)
523     {
524       ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) );
525     }
526   end_constraintList_elements_private;
527
528   constraintList_free(postconditions);
529   return ret;
530 }
531 /*
532 Commenting out because function is not yet stable
533   
534 / *@only@* / constraintList constraintList_doSRefFixStructConstraint(constraintList invars, sRef s, ctype ct )
535 {
536   constraintList ret;
537   ret = constraintList_makeNew();
538   
539   constraintList_elements (invars, el)
540     {
541       ret = constraintList_add(ret, constraint_doSRefFixInvarConstraint (el, s, ct) );
542     }
543   end_constraintList_elements;
544
545   / *  constraintList_free (invars);* /
546
547   return ret;
548 }
549 */
550
551 /*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, /*@temp@*/ /*@observer@*/ exprNodeList arglist)
552 {
553   constraintList ret;
554   ret = constraintList_makeNew();
555
556   constraintList_elements (preconditions, el)
557     {
558       ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
559     }
560   end_constraintList_elements;
561
562   constraintList_free (preconditions);
563
564   return ret;
565 }
566 constraintList constraintList_doSRefFixBaseParam (constraintList preconditions, /*@observer@*/
567                                                    exprNodeList arglist)
568 {
569   constraintList ret;
570   constraint temp;
571   ret = constraintList_makeNew();
572
573   constraintList_elements (preconditions, el)
574     {
575       temp = constraint_copy(el);
576       ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) );
577     }
578   end_constraintList_elements;
579
580   return ret;
581 }
582
583 constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
584 {
585   constraintList_elements_private (c, el)
586     {
587       el = constraint_togglePost(el);
588       if (constraint_hasOrig(el) )
589         {
590           el = constraint_togglePostOrig (el);
591         }
592     }
593   end_constraintList_elements_private;
594   return c;
595 }
596
597 /*@only@*/ constraintList constraintList_undump (FILE *f)
598 {
599   constraintList ret;
600   char *s;
601   char *os;
602   
603   ret = constraintList_makeNew();
604
605   os =  mstring_create (MAX_DUMP_LINE_LENGTH);
606   s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
607
608   while (s != NULL && *s != ';')
609     {
610       constraint temp;
611       char * c;
612
613       c =  reader_getWord(&s);
614       
615       if (strcmp (c, "C") != 0)
616         {
617           llfatalbug(message("Error reading library.  File may be corrupted"));
618         }
619
620       temp = constraint_undump (f);
621       ret = constraintList_add (ret, temp);
622       s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
623       free(c);
624     }
625   free(s);
626
627   return ret;
628 }
629
630
631 void constraintList_dump (/*@observer@*/ constraintList c,  FILE *f)
632 {
633   constraintList_elements (c, el)
634     {
635       fprintf(f, "C\n");
636       constraint_dump (el, f);
637     }
638   end_constraintList_elements; ;
639 }
640
641
642 constraintList constraintList_sort (/*@returned@*/ constraintList ret)
643 {
644   qsort (ret->elements, (size_t) ret->nelements,
645          (sizeof (*ret->elements)), 
646          (int (*)(const void *, const void *)) constraint_compare);
647   
648   DPRINTF((message("onstraint_sort returning") ));
649   return ret;
650 }
651
652
This page took 0.086895 seconds and 5 git commands to generate.