]> andersk Git - splint.git/blame - src/constraintList.c
Committing after merging Evan's changes.
[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",
9a48d98c 183 constraint_unparse(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",
9a48d98c 190 constraint_unparse(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
15b3d2b2 206# if 0
f4ec8018 207static /*@only@*/ cstring
9a48d98c 208constraintList_unparseLocation (/*@temp@*/ constraintList s) /*@*/
f4ec8018 209{
210 int i;
211 cstring st = cstring_undefined;
212 bool first = TRUE;
213
214 if (!constraintList_isDefined (s))
215 {
216 return cstring_makeLiteral ("<undefined>");
217 }
218
219 if (s->nelements == 0)
220 {
221 st = cstring_makeLiteral("<List Empty>");
222 return st;
223 }
224
225 for (i = 0; i < s->nelements; i++)
226 {
227 cstring type = cstring_undefined;
228 constraint current = s->elements[i];
229
230 if (constraint_isDefined(current) )
231 {
232 cstring temp1;
9a48d98c 233 temp1 = constraint_unparseLocation(current);
f4ec8018 234 type = message ("%q %q\n", type, temp1 );
235 }
236
237 if (first)
238 {
239 st = type;
240 first = FALSE;
241 }
242 else
243 {
244 st = message ("%q, %q", st, type);
245 }
b7b694d6 246 }
f4ec8018 247
248 return st;
249}
15b3d2b2 250# endif
f4ec8018 251
920a3797 252/*@only@*/ cstring
9a48d98c 253constraintList_unparse (/*@temp@*/ constraintList s) /*@*/
616915dd 254{
255 int i;
256 cstring st = cstring_undefined;
257 bool first = TRUE;
60eced23 258
259 if (!constraintList_isDefined (s))
260 {
261 return cstring_makeLiteral ("<undefined>");
262 }
616915dd 263
264 if (s->nelements == 0)
d46ce6a4 265 {
266 st = cstring_makeLiteral("<List Empty>");
267 return st;
268 }
269
616915dd 270 for (i = 0; i < s->nelements; i++)
271 {
272 cstring type = cstring_undefined;
273 constraint current = s->elements[i];
274
28bf4b0b 275 if (constraint_isDefined(current) )
616915dd 276 {
90bc41f7 277 cstring temp1;
21f0106c 278
279 if (context_getFlag (FLG_ORCONSTRAINT))
280 {
2a6e9c30 281 temp1 = constraint_unparseOr (current);
21f0106c 282 }
283 else
284 {
9a48d98c 285 temp1 = constraint_unparse (current);
21f0106c 286 }
616915dd 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 }
b7b694d6 299 }
d46ce6a4 300
616915dd 301 return st;
302}
303
2a6e9c30 304void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
8f299805 305{
306
307 constraintList_elements (s, elem)
308 {
28bf4b0b 309 if (constraint_isDefined(elem))
8f299805 310 {
311 constraint_printErrorPostCondition (elem, loc);
312 }
313 }
314 end_constraintList_elements;
315 return;
316}
317
2a6e9c30 318void constraintList_printError (constraintList s, fileloc loc)
616915dd 319{
320
bb25bea6 321 constraintList_elements (s, elem)
616915dd 322 {
28bf4b0b 323 if (constraint_isDefined(elem) )
616915dd 324 {
84380658 325 if (constraint_isPost(elem) )
326 constraint_printErrorPostCondition (elem, loc);
327 else
328 constraint_printError (elem, loc);
616915dd 329 }
330 }
bb25bea6 331 end_constraintList_elements;
616915dd 332 return;
333}
334
8f299805 335
616915dd 336cstring
9a48d98c 337constraintList_unparseDetailed (constraintList s)
616915dd 338{
339 int i;
340 cstring st = cstring_undefined;
341 bool first = TRUE;
342
60eced23 343 if (!constraintList_isDefined (s))
344 {
345 return cstring_makeLiteral ("<undefined>");
346 }
347
616915dd 348 if (s->nelements == 0)
d46ce6a4 349 {
350 st = cstring_makeLiteral("<List Empty>");
351 return st;
352 }
353
616915dd 354 for (i = 0; i < s->nelements; i++)
355 {
356 cstring type = cstring_undefined;
357 constraint current = s->elements[i];
358
28bf4b0b 359 if (constraint_isDefined(current ) )
616915dd 360 {
9a48d98c 361 cstring temp1 = constraint_unparseDetailed (current);
616915dd 362 type = message ("%s %s\n", type, temp1 );
d46ce6a4 363 cstring_free(temp1);
616915dd 364 }
365
366 if (first)
367 {
368 st = type;
369 first = FALSE;
920a3797 370 type = NULL;
616915dd 371 }
372 else
373 {
920a3797 374 st = message ("%q %q", st, type);
616915dd 375 }
376 }
377 return st;
378}
379
380/*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
381} */
382
383constraintList
bb25bea6 384constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
616915dd 385{
386 constraint temp;
387 constraintList ret;
bb7c2085 388 DPRINTF ((message ("Logical or on %s and %s",
9a48d98c 389 constraintList_unparse(l1),
390 constraintList_unparse(l2)) ) );
616915dd 391
c3e695ff 392 ret = constraintList_makeNew();
bb25bea6 393 constraintList_elements (l1, el)
616915dd 394 {
28bf4b0b 395 temp = constraint_substitute (el, l2);
616915dd 396
28bf4b0b 397 if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) )
616915dd 398 { /*avoid redundant constraints*/
28bf4b0b 399 if (!constraintList_resolve (el, ret) )
bb25bea6 400 {
401 constraint temp2;
402 temp2 = constraint_copy(el);
403 ret = constraintList_add (ret, temp2);
404 }
616915dd 405 }
d46ce6a4 406 constraint_free(temp);
616915dd 407 }
bb25bea6 408 end_constraintList_elements;
616915dd 409
bb25bea6 410 constraintList_elements (l2, el)
616915dd 411 {
28bf4b0b 412 temp = constraint_substitute (el, l1);
616915dd 413
28bf4b0b 414 if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) )
616915dd 415 {
416 /*avoid redundant constraints*/
28bf4b0b 417 if (!constraintList_resolve (el, ret) )
bb25bea6 418 {
419 constraint temp2;
420 temp2 = constraint_copy(el);
421 ret = constraintList_add (ret, temp2);
422 }
616915dd 423 }
d46ce6a4 424 constraint_free(temp);
616915dd 425 }
bb25bea6 426 end_constraintList_elements;
616915dd 427
428
429 return ret;
430}
431
432void
bb25bea6 433constraintList_free (/*@only@*/ constraintList s)
616915dd 434{
9a48d98c 435 if (constraintList_isDefined (s))
616915dd 436 {
9a48d98c 437 int i;
438
439 for (i = 0; i < s->nelements; i++)
440 {
441 constraint_free (s->elements[i]);
442 }
443
444 sfree (s->elements);
445 s->elements = NULL;
446 s->nelements = -1;
447 s->nspace = -1;
448 sfree (s);
449 s = NULL;
616915dd 450 }
616915dd 451}
452
453constraintList
03d670b6 454constraintList_copy (/*@observer@*/ /*@temp@*/ constraintList s)
616915dd 455{
c3e695ff 456 constraintList ret = constraintList_makeNew ();
616915dd 457
bb25bea6 458 constraintList_elements (s, el)
616915dd 459 {
460 ret = constraintList_add (ret, constraint_copy (el));
bb25bea6 461 } end_constraintList_elements;
616915dd 462
463 return ret;
464}
465
466constraintList constraintList_preserveOrig (constraintList c)
467{
9a48d98c 468 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_unparse (c) ) ));
d46ce6a4 469
470 constraintList_elements_private (c, el)
616915dd 471 {
472 el = constraint_preserveOrig (el);
473 }
d46ce6a4 474 end_constraintList_elements_private;
616915dd 475 return c;
476}
477
03d670b6 478constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@dependent@*/ /*@observer@*/ exprNode fcn)
4ab867d6 479{
9a48d98c 480 DPRINTF((message("constraintList_preserveCallInfo %s ", constraintList_unparse (c) ) ));
4ab867d6 481
482 constraintList_elements_private (c, el)
483 {
4ab867d6 484 el = constraint_setFcnPre(el);
485 el = constraint_origAddGeneratingExpr (el, fcn);
486 }
487 end_constraintList_elements_private;
488 return c;
489}
490
3814599d 491constraintList constraintList_single (constraint c)
492{
493 constraintList res;
494 res = constraintList_makeNew();
495 res = constraintList_add (res, c);
496 return res;
497}
4ab867d6 498
28bf4b0b 499constraintList constraintList_addGeneratingExpr (constraintList c,/*@dependent@*/ exprNode e)
9280addf 500{
501 DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) ));
502
d46ce6a4 503 constraintList_elements_private (c, el)
9280addf 504 {
9a48d98c 505 DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_unparse(el), exprNode_unparse(e) ) ));
9280addf 506 el = constraint_addGeneratingExpr (el, e);
507 }
d46ce6a4 508 end_constraintList_elements_private;
9280addf 509 return c;
510}
511
d46ce6a4 512/*@only@*/ constraintList constraintList_doFixResult (/*@only@*/constraintList postconditions, exprNode fcnCall)
616915dd 513{
514 constraintList ret;
c3e695ff 515 ret = constraintList_makeNew();
d46ce6a4 516 constraintList_elements_private (postconditions, el)
616915dd 517 {
518 ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) );
519 }
d46ce6a4 520 end_constraintList_elements_private;
616915dd 521
d46ce6a4 522 constraintList_free(postconditions);
616915dd 523 return ret;
524}
86d93ed3 525/*
526Commenting out because function is not yet stable
527
528/ *@only@* / constraintList constraintList_doSRefFixStructConstraint(constraintList invars, sRef s, ctype ct )
529{
530 constraintList ret;
531 ret = constraintList_makeNew();
532
533 constraintList_elements (invars, el)
534 {
535 ret = constraintList_add(ret, constraint_doSRefFixInvarConstraint (el, s, ct) );
536 }
537 end_constraintList_elements;
538
539 / * constraintList_free (invars);* /
540
541 return ret;
542}
543*/
616915dd 544
28bf4b0b 545/*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, /*@temp@*/ /*@observer@*/ exprNodeList arglist)
616915dd 546{
547 constraintList ret;
c3e695ff 548 ret = constraintList_makeNew();
616915dd 549
bb25bea6 550 constraintList_elements (preconditions, el)
616915dd 551 {
552 ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
553 }
bb25bea6 554 end_constraintList_elements;
d46ce6a4 555
556 constraintList_free (preconditions);
616915dd 557
558 return ret;
559}
28bf4b0b 560constraintList constraintList_doSRefFixBaseParam (constraintList preconditions, /*@observer@*/
616915dd 561 exprNodeList arglist)
562{
563 constraintList ret;
bb25bea6 564 constraint temp;
c3e695ff 565 ret = constraintList_makeNew();
616915dd 566
bb25bea6 567 constraintList_elements (preconditions, el)
616915dd 568 {
bb25bea6 569 temp = constraint_copy(el);
570 ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) );
616915dd 571 }
bb25bea6 572 end_constraintList_elements;
616915dd 573
574 return ret;
575}
576
577constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
578{
d46ce6a4 579 constraintList_elements_private (c, el)
616915dd 580 {
84c9ffbf 581 el = constraint_togglePost(el);
2934b455 582 if (constraint_hasOrig(el) )
4ab867d6 583 {
2934b455 584 el = constraint_togglePostOrig (el);
4ab867d6 585 }
616915dd 586 }
d46ce6a4 587 end_constraintList_elements_private;
616915dd 588 return c;
589}
590
920a3797 591/*@only@*/ constraintList constraintList_undump (FILE *f)
592{
593 constraintList ret;
3be9a165 594 char *s;
920a3797 595 char *os;
596
597 ret = constraintList_makeNew();
598
3be9a165 599 os = mstring_create (MAX_DUMP_LINE_LENGTH);
920a3797 600 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
601
602 while (s != NULL && *s != ';')
603 {
604 constraint temp;
605 char * c;
606
28bf4b0b 607 c = reader_getWord(&s);
920a3797 608
2e4caa51 609 if (! mstring_isDefined(c) )
610 {
611 llfatalbug(message("Library file is corrupted") );
612 }
613
614
920a3797 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
631void 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
616915dd 641
02984642 642constraintList constraintList_sort (/*@returned@*/ constraintList ret)
643{
2e4caa51 644 if (constraintList_isUndefined(ret) )
645 {
646 llassert(FALSE);
647 return ret;
648 }
02984642 649 qsort (ret->elements, (size_t) ret->nelements,
15b3d2b2 650 (sizeof (*ret->elements)),
651 (int (*)(const void *, const void *)) constraint_compare);
652
653 DPRINTF((message("onstraint_sort returning") ));
02984642 654 return ret;
655}
f4ec8018 656
657
This page took 1.089281 seconds and 5 git commands to generate.