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