]> andersk Git - splint.git/blame - src/constraintList.c
Manual flags.
[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**
20** For information on lclint: lclint-request@cs.virginia.edu
21** To report a bug: lclint-bug@cs.virginia.edu
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
33# include "lclintMacros.nf"
34# include "llbasic.h"
35
d46ce6a4 36
28bf4b0b 37/*@iter constraintList_elements_private_only (sef constraintList x, yield only constraint el); @*/
38# define constraintList_elements_private_only(x, m_el) \
60eced23 39 { if (constraintList_isDefined (x)) { int m_ind; constraint *m_elements = &((x)->elements[0]); \
28bf4b0b 40 for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
41 { constraint m_el = *(m_elements++);
42
60eced23 43# define end_constraintList_elements_private_only }}}
28bf4b0b 44
45
60eced23 46/*@iter constraintList_elements_private (sef constraintList x, yield constraint el); @*/
d46ce6a4 47# define constraintList_elements_private(x, m_el) \
60eced23 48 { if (constraintList_isDefined (x)) { int m_ind; constraint *m_elements = &((x)->elements[0]); \
d46ce6a4 49 for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \
50 { constraint m_el = *(m_elements++);
51
60eced23 52# define end_constraintList_elements_private }}}
d46ce6a4 53
54
03d670b6 55/*@only@*/ constraintList constraintList_makeNew ()
616915dd 56{
57 constraintList s = (constraintList) dmalloc (sizeof (*s));
58
59 s->nelements = 0;
60 s->nspace = constraintListBASESIZE;
61 s->elements = (constraint *)
62 dmalloc (sizeof (*s->elements) * constraintListBASESIZE);
63
64 return (s);
65}
66
67static void
68constraintList_grow (constraintList s)
69{
70 int i;
71 constraint *newelements;
72
60eced23 73 llassert (constraintList_isDefined (s));
74
616915dd 75 s->nspace += constraintListBASESIZE;
76 newelements = (constraint *) dmalloc (sizeof (*newelements)
77 * (s->nelements + s->nspace));
78
79 for (i = 0; i < s->nelements; i++)
80 {
81 newelements[i] = s->elements[i];
82 }
83
84 sfree (s->elements);
85 s->elements = newelements;
86}
87
c3e695ff 88
616915dd 89constraintList
bb25bea6 90constraintList_add (/*@returned@*/ constraintList s, /*@only@*/ constraint el)
616915dd 91{
60eced23 92 llassert (constraintList_isDefined (s));
93
c3e695ff 94 /*drl7x */
b7b694d6 95
28bf4b0b 96 if (constraintList_resolve (el, s) )
d46ce6a4 97 {
98 constraint_free (el);
99 return s;
100 }
616915dd 101
102 if (s->nspace <= 0)
103 constraintList_grow (s);
104
105 s->nspace--;
106 s->elements[s->nelements] = el;
107 s->nelements++;
108 return s;
109}
110
d46ce6a4 111/* frees everything but actual constraints */
112/* This function should only be used if you have
113 other references to unshared constraints
114*/
115static void constraintList_freeShallow (/*@only@*/ constraintList c)
116{
117 if (constraintList_isDefined(c) )
bb25bea6 118 {
119 free (c->elements);
120 c->elements = NULL;
121 c->nelements = -1;
122 c->nspace = -1;
123 }
d46ce6a4 124 free (c);
bb25bea6 125 c = NULL;
126}
127
28bf4b0b 128/*@only@*/ constraintList constraintList_addList (/*@only@*/ /*@returned@*/ constraintList s, /*@observer@*/ constraintList newList)
bb25bea6 129{
130 llassert(constraintList_isDefined(s) );
28bf4b0b 131 llassert(constraintList_isDefined(newList) );
bb25bea6 132
28bf4b0b 133 if (newList == constraintList_undefined)
bb25bea6 134 return s;
135
28bf4b0b 136 constraintList_elements (newList, elem)
bb25bea6 137 {
138 s = constraintList_add (s, constraint_copy(elem) );
139 }
140 end_constraintList_elements;
141
142 return s;
d46ce6a4 143}
144
28bf4b0b 145constraintList constraintList_addListFree (/*@returned@*/ constraintList s, /*@only@*/ constraintList newList)
616915dd 146{
84c9ffbf 147 llassert(constraintList_isDefined(s) );
28bf4b0b 148 llassert(constraintList_isDefined(newList) );
616915dd 149
28bf4b0b 150 if (constraintList_isUndefined(newList) )
616915dd 151 return s;
152
28bf4b0b 153 constraintList_elements_private_only(newList, elem)
616915dd 154 {
155 s = constraintList_add (s, elem);
156 }
28bf4b0b 157 end_constraintList_elements_private_only
d46ce6a4 158
28bf4b0b 159 constraintList_freeShallow(newList);
616915dd 160 return s;
161}
162
920a3797 163
7edb30e6 164constraintList constraintList_removeSurpressed (/*@only@*/ constraintList s)
165{
166 constraintList ret;
167 fileloc loc;
168 llassert(constraintList_isDefined(s) );
169
170 ret = constraintList_makeNew();
171
172 constraintList_elements_private_only(s, elem)
173 {
174 loc = constraint_getFileloc(elem);
175
176 if(fileloc_isUndefined(loc) )
177 {
178 ret = constraintList_add (ret, elem);
179 }
180
181 else if (context_suppressFlagMsg(FLG_ARRAYBOUNDS, loc) )
182 {
183 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
184 constraint_free(elem);
185 }
186
187 else if ( (! constraint_hasMaxSet(elem) ) && context_suppressFlagMsg(FLG_ARRAYBOUNDSREAD, loc) )
188 {
189 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
190 constraint_free(elem);
191 }
192 else
193 {
194 ret = constraintList_add (ret, elem);
195 }
196 fileloc_free(loc);
197 }
198 end_constraintList_elements_private_only;
199
200 constraintList_freeShallow(s);
201
202 return ret;
203}
204
205
920a3797 206extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/
207{
208 return (constraintList_print(s));
920a3797 209}
210
15b3d2b2 211# if 0
f4ec8018 212static /*@only@*/ cstring
213constraintList_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 }
b7b694d6 251 }
f4ec8018 252
253 return st;
254}
15b3d2b2 255# endif
f4ec8018 256
920a3797 257/*@only@*/ cstring
28bf4b0b 258constraintList_print (/*@temp@*/ constraintList s) /*@*/
616915dd 259{
260 int i;
261 cstring st = cstring_undefined;
262 bool first = TRUE;
60eced23 263
264 if (!constraintList_isDefined (s))
265 {
266 return cstring_makeLiteral ("<undefined>");
267 }
616915dd 268
269 if (s->nelements == 0)
d46ce6a4 270 {
271 st = cstring_makeLiteral("<List Empty>");
272 return st;
273 }
274
616915dd 275 for (i = 0; i < s->nelements; i++)
276 {
277 cstring type = cstring_undefined;
278 constraint current = s->elements[i];
279
28bf4b0b 280 if (constraint_isDefined(current) )
616915dd 281 {
90bc41f7 282 cstring temp1;
283 if ( context_getFlag (FLG_ORCONSTRAINT) )
284 temp1 = constraint_printOr(current);
285 else
286 temp1 = constraint_print(current);
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
8f299805 304void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
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
616915dd 318void constraintList_printError (constraintList s, fileloc loc)
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
337constraintList_printDetailed (constraintList s)
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 {
361 cstring temp1 = constraint_printDetailed (current);
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;
bb25bea6 388 DPRINTF ( (message ("Logical or on %s and %s",
616915dd 389 constraintList_print(l1),
390 constraintList_print(l2)) ) );
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{
435 int i;
bb25bea6 436
437 llassert(constraintList_isDefined(s) );
438
439
616915dd 440 for (i = 0; i < s->nelements; i++)
441 {
bb25bea6 442 constraint_free (s->elements[i]);
616915dd 443 }
444
445 sfree (s->elements);
bb25bea6 446 s->elements = NULL;
447 s->nelements = -1;
448 s->nspace = -1;
616915dd 449 sfree (s);
bb25bea6 450 s = NULL;
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{
d46ce6a4 468 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
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{
dc7f6a51 480 DPRINTF((message("constraintList_preserveCallInfo %s ", constraintList_print (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 {
505 DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(el), exprNode_unparse(e) ) ));
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}
525
28bf4b0b 526/*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, /*@temp@*/ /*@observer@*/ exprNodeList arglist)
616915dd 527{
528 constraintList ret;
c3e695ff 529 ret = constraintList_makeNew();
616915dd 530
bb25bea6 531 constraintList_elements (preconditions, el)
616915dd 532 {
533 ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) );
534 }
bb25bea6 535 end_constraintList_elements;
d46ce6a4 536
537 constraintList_free (preconditions);
616915dd 538
539 return ret;
540}
28bf4b0b 541constraintList constraintList_doSRefFixBaseParam (constraintList preconditions, /*@observer@*/
616915dd 542 exprNodeList arglist)
543{
544 constraintList ret;
bb25bea6 545 constraint temp;
c3e695ff 546 ret = constraintList_makeNew();
616915dd 547
bb25bea6 548 constraintList_elements (preconditions, el)
616915dd 549 {
bb25bea6 550 temp = constraint_copy(el);
551 ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) );
616915dd 552 }
bb25bea6 553 end_constraintList_elements;
616915dd 554
555 return ret;
556}
557
558constraintList constraintList_togglePost (/*@returned@*/ constraintList c)
559{
d46ce6a4 560 constraintList_elements_private (c, el)
616915dd 561 {
84c9ffbf 562 el = constraint_togglePost(el);
2934b455 563 if (constraint_hasOrig(el) )
4ab867d6 564 {
2934b455 565 el = constraint_togglePostOrig (el);
4ab867d6 566 }
616915dd 567 }
d46ce6a4 568 end_constraintList_elements_private;
616915dd 569 return c;
570}
571
920a3797 572/*@only@*/ constraintList constraintList_undump (FILE *f)
573{
574 constraintList ret;
3be9a165 575 char *s;
920a3797 576 char *os;
577
578 ret = constraintList_makeNew();
579
3be9a165 580 os = mstring_create (MAX_DUMP_LINE_LENGTH);
920a3797 581 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
582
583 while (s != NULL && *s != ';')
584 {
585 constraint temp;
586 char * c;
587
28bf4b0b 588 c = reader_getWord(&s);
920a3797 589
590 if (strcmp (c, "C") != 0)
591 {
592 llfatalbug(message("Error reading library. File may be corrupted"));
593 }
594
595 temp = constraint_undump (f);
596 ret = constraintList_add (ret, temp);
597 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
598 free(c);
599 }
600 free(s);
601
602 return ret;
603}
604
605
606void constraintList_dump (/*@observer@*/ constraintList c, FILE *f)
607{
608 constraintList_elements (c, el)
609 {
610 fprintf(f, "C\n");
611 constraint_dump (el, f);
612 }
613 end_constraintList_elements; ;
614}
615
616915dd 616
02984642 617constraintList constraintList_sort (/*@returned@*/ constraintList ret)
618{
619 qsort (ret->elements, (size_t) ret->nelements,
15b3d2b2 620 (sizeof (*ret->elements)),
621 (int (*)(const void *, const void *)) constraint_compare);
622
623 DPRINTF((message("onstraint_sort returning") ));
02984642 624 return ret;
625}
f4ec8018 626
627
This page took 1.503421 seconds and 5 git commands to generate.