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