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