]> andersk Git - splint.git/blame - src/constraintList.c
*** empty log message ***
[splint.git] / src / constraintList.c
CommitLineData
616915dd 1/*
2** LCLint - annotation-assisted static program checker
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
22** For more information: http://lclint.cs.virginia.edu
23*/
24/*
25** constraintList.c
26**
27** based on list_template.c
28**
29** where T has T_equal (or change this) and T_unparse
30*/
31
32# include "lclintMacros.nf"
33# include "llbasic.h"
34
d46ce6a4 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);
62
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 */
94 // el = constraint_simplify (el);
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
28bf4b0b 127/*@only@*/ constraintList constraintList_addList (/*@only@*/ /*@returned@*/ constraintList s, /*@observer@*/ 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
175 if(fileloc_isUndefined(loc) )
176 {
177 ret = constraintList_add (ret, elem);
178 }
179
180 else if (context_suppressFlagMsg(FLG_ARRAYBOUNDS, loc) )
181 {
182 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
183 constraint_free(elem);
184 }
185
186 else if ( (! constraint_hasMaxSet(elem) ) && context_suppressFlagMsg(FLG_ARRAYBOUNDSREAD, loc) )
187 {
188 DPRINTF(( message("constraintList_removeSurpressed getting rid of surpressed constraint %q", constraint_print(elem) ) ));
189 constraint_free(elem);
190 }
191 else
192 {
193 ret = constraintList_add (ret, elem);
194 }
195 fileloc_free(loc);
196 }
197 end_constraintList_elements_private_only;
198
199 constraintList_freeShallow(s);
200
201 return ret;
202}
203
204
920a3797 205extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/
206{
207 return (constraintList_print(s));
920a3797 208}
209
15b3d2b2 210# if 0
f4ec8018 211static /*@only@*/ cstring
212constraintList_printLocation (/*@temp@*/ constraintList s) /*@*/
213{
214 int i;
215 cstring st = cstring_undefined;
216 bool first = TRUE;
217
218 if (!constraintList_isDefined (s))
219 {
220 return cstring_makeLiteral ("<undefined>");
221 }
222
223 if (s->nelements == 0)
224 {
225 st = cstring_makeLiteral("<List Empty>");
226 return st;
227 }
228
229 for (i = 0; i < s->nelements; i++)
230 {
231 cstring type = cstring_undefined;
232 constraint current = s->elements[i];
233
234 if (constraint_isDefined(current) )
235 {
236 cstring temp1;
237 temp1 = constraint_printLocation(current);
238 type = message ("%q %q\n", type, temp1 );
239 }
240
241 if (first)
242 {
243 st = type;
244 first = FALSE;
245 }
246 else
247 {
248 st = message ("%q, %q", st, type);
249 }
250 } //end for
251
252 return st;
253}
15b3d2b2 254# endif
f4ec8018 255
920a3797 256/*@only@*/ cstring
28bf4b0b 257constraintList_print (/*@temp@*/ constraintList s) /*@*/
616915dd 258{
259 int i;
260 cstring st = cstring_undefined;
261 bool first = TRUE;
60eced23 262
263 if (!constraintList_isDefined (s))
264 {
265 return cstring_makeLiteral ("<undefined>");
266 }
616915dd 267
268 if (s->nelements == 0)
d46ce6a4 269 {
270 st = cstring_makeLiteral("<List Empty>");
271 return st;
272 }
273
616915dd 274 for (i = 0; i < s->nelements; i++)
275 {
276 cstring type = cstring_undefined;
277 constraint current = s->elements[i];
278
28bf4b0b 279 if (constraint_isDefined(current) )
616915dd 280 {
90bc41f7 281 cstring temp1;
282 if ( context_getFlag (FLG_ORCONSTRAINT) )
283 temp1 = constraint_printOr(current);
284 else
285 temp1 = constraint_print(current);
616915dd 286 type = message ("%q %q\n", type, temp1 );
287 }
288
289 if (first)
290 {
291 st = type;
292 first = FALSE;
293 }
294 else
295 {
296 st = message ("%q, %q", st, type);
297 }
d46ce6a4 298 } //end for
299
616915dd 300 return st;
301}
302
8f299805 303void constraintList_printErrorPostConditions (constraintList s, fileloc loc)
304{
305
306 constraintList_elements (s, elem)
307 {
28bf4b0b 308 if (constraint_isDefined(elem))
8f299805 309 {
310 constraint_printErrorPostCondition (elem, loc);
311 }
312 }
313 end_constraintList_elements;
314 return;
315}
316
616915dd 317void constraintList_printError (constraintList s, fileloc loc)
318{
319
bb25bea6 320 constraintList_elements (s, elem)
616915dd 321 {
28bf4b0b 322 if (constraint_isDefined(elem) )
616915dd 323 {
84380658 324 if (constraint_isPost(elem) )
325 constraint_printErrorPostCondition (elem, loc);
326 else
327 constraint_printError (elem, loc);
616915dd 328 }
329 }
bb25bea6 330 end_constraintList_elements;
616915dd 331 return;
332}
333
8f299805 334
616915dd 335cstring
336constraintList_printDetailed (constraintList s)
337{
338 int i;
339 cstring st = cstring_undefined;
340 bool first = TRUE;
341
60eced23 342 if (!constraintList_isDefined (s))
343 {
344 return cstring_makeLiteral ("<undefined>");
345 }
346
616915dd 347 if (s->nelements == 0)
d46ce6a4 348 {
349 st = cstring_makeLiteral("<List Empty>");
350 return st;
351 }
352
616915dd 353 for (i = 0; i < s->nelements; i++)
354 {
355 cstring type = cstring_undefined;
356 constraint current = s->elements[i];
357
28bf4b0b 358 if (constraint_isDefined(current ) )
616915dd 359 {
360 cstring temp1 = constraint_printDetailed (current);
361 type = message ("%s %s\n", type, temp1 );
d46ce6a4 362 cstring_free(temp1);
616915dd 363 }
364
365 if (first)
366 {
367 st = type;
368 first = FALSE;
920a3797 369 type = NULL;
616915dd 370 }
371 else
372 {
920a3797 373 st = message ("%q %q", st, type);
616915dd 374 }
375 }
376 return st;
377}
378
379/*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1)
380} */
381
382constraintList
bb25bea6 383constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2)
616915dd 384{
385 constraint temp;
386 constraintList ret;
bb25bea6 387 DPRINTF ( (message ("Logical or on %s and %s",
616915dd 388 constraintList_print(l1),
389 constraintList_print(l2)) ) );
390
c3e695ff 391 ret = constraintList_makeNew();
bb25bea6 392 constraintList_elements (l1, el)
616915dd 393 {
28bf4b0b 394 temp = constraint_substitute (el, l2);
616915dd 395
28bf4b0b 396 if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) )
616915dd 397 { /*avoid redundant constraints*/
28bf4b0b 398 if (!constraintList_resolve (el, ret) )
bb25bea6 399 {
400 constraint temp2;
401 temp2 = constraint_copy(el);
402 ret = constraintList_add (ret, temp2);
403 }
616915dd 404 }
d46ce6a4 405 constraint_free(temp);
616915dd 406 }
bb25bea6 407 end_constraintList_elements;
616915dd 408
bb25bea6 409 constraintList_elements (l2, el)
616915dd 410 {
28bf4b0b 411 temp = constraint_substitute (el, l1);
616915dd 412
28bf4b0b 413 if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) )
616915dd 414 {
415 /*avoid redundant constraints*/
28bf4b0b 416 if (!constraintList_resolve (el, ret) )
bb25bea6 417 {
418 constraint temp2;
419 temp2 = constraint_copy(el);
420 ret = constraintList_add (ret, temp2);
421 }
616915dd 422 }
d46ce6a4 423 constraint_free(temp);
616915dd 424 }
bb25bea6 425 end_constraintList_elements;
616915dd 426
427
428 return ret;
429}
430
431void
bb25bea6 432constraintList_free (/*@only@*/ constraintList s)
616915dd 433{
434 int i;
bb25bea6 435
436 llassert(constraintList_isDefined(s) );
437
438
616915dd 439 for (i = 0; i < s->nelements; i++)
440 {
bb25bea6 441 constraint_free (s->elements[i]);
616915dd 442 }
443
444 sfree (s->elements);
bb25bea6 445 s->elements = NULL;
446 s->nelements = -1;
447 s->nspace = -1;
616915dd 448 sfree (s);
bb25bea6 449 s = NULL;
616915dd 450}
451
452constraintList
03d670b6 453constraintList_copy (/*@observer@*/ /*@temp@*/ constraintList s)
616915dd 454{
c3e695ff 455 constraintList ret = constraintList_makeNew ();
616915dd 456
bb25bea6 457 constraintList_elements (s, el)
616915dd 458 {
459 ret = constraintList_add (ret, constraint_copy (el));
bb25bea6 460 } end_constraintList_elements;
616915dd 461
462 return ret;
463}
464
465constraintList constraintList_preserveOrig (constraintList c)
466{
d46ce6a4 467 DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) ));
468
469 constraintList_elements_private (c, el)
616915dd 470 {
471 el = constraint_preserveOrig (el);
472 }
d46ce6a4 473 end_constraintList_elements_private;
616915dd 474 return c;
475}
476
03d670b6 477constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@dependent@*/ /*@observer@*/ exprNode fcn)
4ab867d6 478{
dc7f6a51 479 DPRINTF((message("constraintList_preserveCallInfo %s ", constraintList_print (c) ) ));
4ab867d6 480
481 constraintList_elements_private (c, el)
482 {
483 // el = constraint_preserveOrig (el);
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;
575 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
576 char *os;
577
578 ret = constraintList_makeNew();
579
580 os = s;
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 0.153479 seconds and 5 git commands to generate.