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