]>
Commit | Line | Data |
---|---|---|
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) \ | |
38 | { int m_ind; constraint *m_elements = &((x)->elements[0]); \ | |
39 | for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \ | |
40 | { constraint m_el = *(m_elements++); | |
41 | ||
42 | # define end_constraintList_elements_private_only }} | |
43 | ||
44 | ||
45 | /*@iter constraintList_elements_private (sef constraintList x, yield constraint el); @*/ | |
d46ce6a4 | 46 | # define constraintList_elements_private(x, m_el) \ |
47 | { int m_ind; constraint *m_elements = &((x)->elements[0]); \ | |
48 | for (m_ind = 0 ; m_ind < (x)->nelements; m_ind++) \ | |
49 | { constraint m_el = *(m_elements++); | |
50 | ||
51 | # define end_constraintList_elements_private }} | |
52 | ||
53 | ||
c3e695ff | 54 | 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 | ||
66 | static void | |
67 | constraintList_grow (constraintList s) | |
68 | { | |
69 | int i; | |
70 | constraint *newelements; | |
71 | ||
72 | s->nspace += constraintListBASESIZE; | |
73 | newelements = (constraint *) dmalloc (sizeof (*newelements) | |
74 | * (s->nelements + s->nspace)); | |
75 | ||
76 | for (i = 0; i < s->nelements; i++) | |
77 | { | |
78 | newelements[i] = s->elements[i]; | |
79 | } | |
80 | ||
81 | sfree (s->elements); | |
82 | s->elements = newelements; | |
83 | } | |
84 | ||
c3e695ff | 85 | |
616915dd | 86 | constraintList |
bb25bea6 | 87 | constraintList_add (/*@returned@*/ constraintList s, /*@only@*/ constraint el) |
616915dd | 88 | { |
c3e695ff | 89 | /*drl7x */ |
90 | // el = constraint_simplify (el); | |
28bf4b0b | 91 | if (constraintList_resolve (el, s) ) |
d46ce6a4 | 92 | { |
93 | constraint_free (el); | |
94 | return s; | |
95 | } | |
616915dd | 96 | |
97 | if (s->nspace <= 0) | |
98 | constraintList_grow (s); | |
99 | ||
100 | s->nspace--; | |
101 | s->elements[s->nelements] = el; | |
102 | s->nelements++; | |
103 | return s; | |
104 | } | |
105 | ||
d46ce6a4 | 106 | /* frees everything but actual constraints */ |
107 | /* This function should only be used if you have | |
108 | other references to unshared constraints | |
109 | */ | |
110 | static void constraintList_freeShallow (/*@only@*/ constraintList c) | |
111 | { | |
112 | if (constraintList_isDefined(c) ) | |
bb25bea6 | 113 | { |
114 | free (c->elements); | |
115 | c->elements = NULL; | |
116 | c->nelements = -1; | |
117 | c->nspace = -1; | |
118 | } | |
d46ce6a4 | 119 | free (c); |
bb25bea6 | 120 | c = NULL; |
121 | } | |
122 | ||
28bf4b0b | 123 | /*@only@*/ constraintList constraintList_addList (/*@only@*/ /*@returned@*/ constraintList s, /*@observer@*/ constraintList newList) |
bb25bea6 | 124 | { |
125 | llassert(constraintList_isDefined(s) ); | |
28bf4b0b | 126 | llassert(constraintList_isDefined(newList) ); |
bb25bea6 | 127 | |
28bf4b0b | 128 | if (newList == constraintList_undefined) |
bb25bea6 | 129 | return s; |
130 | ||
28bf4b0b | 131 | constraintList_elements (newList, elem) |
bb25bea6 | 132 | { |
133 | s = constraintList_add (s, constraint_copy(elem) ); | |
134 | } | |
135 | end_constraintList_elements; | |
136 | ||
137 | return s; | |
d46ce6a4 | 138 | } |
139 | ||
28bf4b0b | 140 | constraintList constraintList_addListFree (/*@returned@*/ constraintList s, /*@only@*/ constraintList newList) |
616915dd | 141 | { |
84c9ffbf | 142 | llassert(constraintList_isDefined(s) ); |
28bf4b0b | 143 | llassert(constraintList_isDefined(newList) ); |
616915dd | 144 | |
28bf4b0b | 145 | if (constraintList_isUndefined(newList) ) |
616915dd | 146 | return s; |
147 | ||
28bf4b0b | 148 | constraintList_elements_private_only(newList, elem) |
616915dd | 149 | { |
150 | s = constraintList_add (s, elem); | |
151 | } | |
28bf4b0b | 152 | end_constraintList_elements_private_only |
d46ce6a4 | 153 | |
28bf4b0b | 154 | constraintList_freeShallow(newList); |
616915dd | 155 | return s; |
156 | } | |
157 | ||
920a3797 | 158 | |
159 | extern /*@only@*/ cstring constraintList_unparse ( /*@observer@*/ constraintList s) /*@*/ | |
160 | { | |
161 | return (constraintList_print(s)); | |
162 | ||
163 | ||
164 | } | |
165 | ||
166 | ||
167 | /*@only@*/ cstring | |
28bf4b0b | 168 | constraintList_print (/*@temp@*/ constraintList s) /*@*/ |
616915dd | 169 | { |
170 | int i; | |
171 | cstring st = cstring_undefined; | |
172 | bool first = TRUE; | |
173 | ||
174 | if (s->nelements == 0) | |
d46ce6a4 | 175 | { |
176 | st = cstring_makeLiteral("<List Empty>"); | |
177 | return st; | |
178 | } | |
179 | ||
616915dd | 180 | for (i = 0; i < s->nelements; i++) |
181 | { | |
182 | cstring type = cstring_undefined; | |
183 | constraint current = s->elements[i]; | |
184 | ||
28bf4b0b | 185 | if (constraint_isDefined(current) ) |
616915dd | 186 | { |
90bc41f7 | 187 | cstring temp1; |
188 | if ( context_getFlag (FLG_ORCONSTRAINT) ) | |
189 | temp1 = constraint_printOr(current); | |
190 | else | |
191 | temp1 = constraint_print(current); | |
616915dd | 192 | type = message ("%q %q\n", type, temp1 ); |
193 | } | |
194 | ||
195 | if (first) | |
196 | { | |
197 | st = type; | |
198 | first = FALSE; | |
199 | } | |
200 | else | |
201 | { | |
202 | st = message ("%q, %q", st, type); | |
203 | } | |
d46ce6a4 | 204 | } //end for |
205 | ||
616915dd | 206 | return st; |
207 | } | |
208 | ||
8f299805 | 209 | void constraintList_printErrorPostConditions (constraintList s, fileloc loc) |
210 | { | |
211 | ||
212 | constraintList_elements (s, elem) | |
213 | { | |
28bf4b0b | 214 | if (constraint_isDefined(elem)) |
8f299805 | 215 | { |
216 | constraint_printErrorPostCondition (elem, loc); | |
217 | } | |
218 | } | |
219 | end_constraintList_elements; | |
220 | return; | |
221 | } | |
222 | ||
616915dd | 223 | void constraintList_printError (constraintList s, fileloc loc) |
224 | { | |
225 | ||
bb25bea6 | 226 | constraintList_elements (s, elem) |
616915dd | 227 | { |
28bf4b0b | 228 | if (constraint_isDefined(elem) ) |
616915dd | 229 | { |
230 | constraint_printError (elem, loc); | |
231 | } | |
232 | } | |
bb25bea6 | 233 | end_constraintList_elements; |
616915dd | 234 | return; |
235 | } | |
236 | ||
8f299805 | 237 | |
616915dd | 238 | cstring |
239 | constraintList_printDetailed (constraintList s) | |
240 | { | |
241 | int i; | |
242 | cstring st = cstring_undefined; | |
243 | bool first = TRUE; | |
244 | ||
245 | if (s->nelements == 0) | |
d46ce6a4 | 246 | { |
247 | st = cstring_makeLiteral("<List Empty>"); | |
248 | return st; | |
249 | } | |
250 | ||
616915dd | 251 | for (i = 0; i < s->nelements; i++) |
252 | { | |
253 | cstring type = cstring_undefined; | |
254 | constraint current = s->elements[i]; | |
255 | ||
28bf4b0b | 256 | if (constraint_isDefined(current ) ) |
616915dd | 257 | { |
258 | cstring temp1 = constraint_printDetailed (current); | |
259 | type = message ("%s %s\n", type, temp1 ); | |
d46ce6a4 | 260 | cstring_free(temp1); |
616915dd | 261 | } |
262 | ||
263 | if (first) | |
264 | { | |
265 | st = type; | |
266 | first = FALSE; | |
920a3797 | 267 | type = NULL; |
616915dd | 268 | } |
269 | else | |
270 | { | |
920a3797 | 271 | st = message ("%q %q", st, type); |
616915dd | 272 | } |
273 | } | |
274 | return st; | |
275 | } | |
276 | ||
277 | /*{ x: constraint | (x in l1 -> resolve (x, l2) || (x in l2 -> resolve (x, l1) | |
278 | } */ | |
279 | ||
280 | constraintList | |
bb25bea6 | 281 | constraintList_logicalOr (/*@observer@*/ constraintList l1, /*@observer@*/ constraintList l2) |
616915dd | 282 | { |
283 | constraint temp; | |
284 | constraintList ret; | |
bb25bea6 | 285 | DPRINTF ( (message ("Logical or on %s and %s", |
616915dd | 286 | constraintList_print(l1), |
287 | constraintList_print(l2)) ) ); | |
288 | ||
c3e695ff | 289 | ret = constraintList_makeNew(); |
bb25bea6 | 290 | constraintList_elements (l1, el) |
616915dd | 291 | { |
28bf4b0b | 292 | temp = constraint_substitute (el, l2); |
616915dd | 293 | |
28bf4b0b | 294 | if (constraintList_resolve (el, l2) || constraintList_resolve(temp,l2) ) |
616915dd | 295 | { /*avoid redundant constraints*/ |
28bf4b0b | 296 | if (!constraintList_resolve (el, ret) ) |
bb25bea6 | 297 | { |
298 | constraint temp2; | |
299 | temp2 = constraint_copy(el); | |
300 | ret = constraintList_add (ret, temp2); | |
301 | } | |
616915dd | 302 | } |
d46ce6a4 | 303 | constraint_free(temp); |
616915dd | 304 | } |
bb25bea6 | 305 | end_constraintList_elements; |
616915dd | 306 | |
bb25bea6 | 307 | constraintList_elements (l2, el) |
616915dd | 308 | { |
28bf4b0b | 309 | temp = constraint_substitute (el, l1); |
616915dd | 310 | |
28bf4b0b | 311 | if (constraintList_resolve (el, l1) || constraintList_resolve(temp,l1) ) |
616915dd | 312 | { |
313 | /*avoid redundant constraints*/ | |
28bf4b0b | 314 | if (!constraintList_resolve (el, ret) ) |
bb25bea6 | 315 | { |
316 | constraint temp2; | |
317 | temp2 = constraint_copy(el); | |
318 | ret = constraintList_add (ret, temp2); | |
319 | } | |
616915dd | 320 | } |
d46ce6a4 | 321 | constraint_free(temp); |
616915dd | 322 | } |
bb25bea6 | 323 | end_constraintList_elements; |
616915dd | 324 | |
325 | ||
326 | return ret; | |
327 | } | |
328 | ||
329 | void | |
bb25bea6 | 330 | constraintList_free (/*@only@*/ constraintList s) |
616915dd | 331 | { |
332 | int i; | |
bb25bea6 | 333 | |
334 | llassert(constraintList_isDefined(s) ); | |
335 | ||
336 | ||
616915dd | 337 | for (i = 0; i < s->nelements; i++) |
338 | { | |
bb25bea6 | 339 | constraint_free (s->elements[i]); |
616915dd | 340 | } |
341 | ||
342 | sfree (s->elements); | |
bb25bea6 | 343 | s->elements = NULL; |
344 | s->nelements = -1; | |
345 | s->nspace = -1; | |
616915dd | 346 | sfree (s); |
bb25bea6 | 347 | s = NULL; |
616915dd | 348 | } |
349 | ||
350 | constraintList | |
28bf4b0b | 351 | constraintList_copy (/*@oberserver@*/ /*@temp@*/ constraintList s) |
616915dd | 352 | { |
c3e695ff | 353 | constraintList ret = constraintList_makeNew (); |
616915dd | 354 | |
bb25bea6 | 355 | constraintList_elements (s, el) |
616915dd | 356 | { |
357 | ret = constraintList_add (ret, constraint_copy (el)); | |
bb25bea6 | 358 | } end_constraintList_elements; |
616915dd | 359 | |
360 | return ret; | |
361 | } | |
362 | ||
363 | constraintList constraintList_preserveOrig (constraintList c) | |
364 | { | |
d46ce6a4 | 365 | DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) )); |
366 | ||
367 | constraintList_elements_private (c, el) | |
616915dd | 368 | { |
369 | el = constraint_preserveOrig (el); | |
370 | } | |
d46ce6a4 | 371 | end_constraintList_elements_private; |
616915dd | 372 | return c; |
373 | } | |
374 | ||
28bf4b0b | 375 | constraintList constraintList_preserveCallInfo (/*@returned@*/ constraintList c,/*@observer@*/ /*@depenent@*/ /*@observer@*/ exprNode fcn) |
4ab867d6 | 376 | { |
377 | DPRINTF((message("constraintList_preserveOrig preserving the originial constraints for %s ", constraintList_print (c) ) )); | |
378 | ||
379 | constraintList_elements_private (c, el) | |
380 | { | |
381 | // el = constraint_preserveOrig (el); | |
382 | el = constraint_setFcnPre(el); | |
383 | el = constraint_origAddGeneratingExpr (el, fcn); | |
384 | } | |
385 | end_constraintList_elements_private; | |
386 | return c; | |
387 | } | |
388 | ||
389 | ||
390 | ||
28bf4b0b | 391 | constraintList constraintList_addGeneratingExpr (constraintList c,/*@dependent@*/ exprNode e) |
9280addf | 392 | { |
393 | DPRINTF ((message ("entering constraintList_addGeneratingExpr for %s ", exprNode_unparse(e) ) )); | |
394 | ||
d46ce6a4 | 395 | constraintList_elements_private (c, el) |
9280addf | 396 | { |
397 | DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(el), exprNode_unparse(e) ) )); | |
398 | el = constraint_addGeneratingExpr (el, e); | |
399 | } | |
d46ce6a4 | 400 | end_constraintList_elements_private; |
9280addf | 401 | return c; |
402 | } | |
403 | ||
d46ce6a4 | 404 | /*@only@*/ constraintList constraintList_doFixResult (/*@only@*/constraintList postconditions, exprNode fcnCall) |
616915dd | 405 | { |
406 | constraintList ret; | |
c3e695ff | 407 | ret = constraintList_makeNew(); |
d46ce6a4 | 408 | constraintList_elements_private (postconditions, el) |
616915dd | 409 | { |
410 | ret = constraintList_add (ret, constraint_doFixResult (el, fcnCall) ); | |
411 | } | |
d46ce6a4 | 412 | end_constraintList_elements_private; |
616915dd | 413 | |
d46ce6a4 | 414 | constraintList_free(postconditions); |
616915dd | 415 | return ret; |
416 | } | |
417 | ||
28bf4b0b | 418 | /*@only@*/ constraintList constraintList_doSRefFixConstraintParam (constraintList preconditions, /*@temp@*/ /*@observer@*/ exprNodeList arglist) |
616915dd | 419 | { |
420 | constraintList ret; | |
c3e695ff | 421 | ret = constraintList_makeNew(); |
616915dd | 422 | |
bb25bea6 | 423 | constraintList_elements (preconditions, el) |
616915dd | 424 | { |
425 | ret = constraintList_add(ret, constraint_doSRefFixConstraintParam (el, arglist) ); | |
426 | } | |
bb25bea6 | 427 | end_constraintList_elements; |
d46ce6a4 | 428 | |
429 | constraintList_free (preconditions); | |
616915dd | 430 | |
431 | return ret; | |
432 | } | |
28bf4b0b | 433 | constraintList constraintList_doSRefFixBaseParam (constraintList preconditions, /*@observer@*/ |
616915dd | 434 | exprNodeList arglist) |
435 | { | |
436 | constraintList ret; | |
bb25bea6 | 437 | constraint temp; |
c3e695ff | 438 | ret = constraintList_makeNew(); |
616915dd | 439 | |
bb25bea6 | 440 | constraintList_elements (preconditions, el) |
616915dd | 441 | { |
bb25bea6 | 442 | temp = constraint_copy(el); |
443 | ret = constraintList_add(ret, constraint_doSRefFixBaseParam (temp, arglist) ); | |
616915dd | 444 | } |
bb25bea6 | 445 | end_constraintList_elements; |
616915dd | 446 | |
447 | return ret; | |
448 | } | |
449 | ||
450 | constraintList constraintList_togglePost (/*@returned@*/ constraintList c) | |
451 | { | |
d46ce6a4 | 452 | constraintList_elements_private (c, el) |
616915dd | 453 | { |
84c9ffbf | 454 | el = constraint_togglePost(el); |
2934b455 | 455 | if (constraint_hasOrig(el) ) |
4ab867d6 | 456 | { |
2934b455 | 457 | el = constraint_togglePostOrig (el); |
4ab867d6 | 458 | } |
616915dd | 459 | } |
d46ce6a4 | 460 | end_constraintList_elements_private; |
616915dd | 461 | return c; |
462 | } | |
463 | ||
920a3797 | 464 | /*@only@*/ constraintList constraintList_undump (FILE *f) |
465 | { | |
466 | constraintList ret; | |
467 | char *s = mstring_create (MAX_DUMP_LINE_LENGTH); | |
468 | char *os; | |
469 | ||
470 | ret = constraintList_makeNew(); | |
471 | ||
472 | os = s; | |
473 | s = fgets (os, MAX_DUMP_LINE_LENGTH, f); | |
474 | ||
475 | while (s != NULL && *s != ';') | |
476 | { | |
477 | constraint temp; | |
478 | char * c; | |
479 | ||
28bf4b0b | 480 | c = reader_getWord(&s); |
920a3797 | 481 | |
482 | if (strcmp (c, "C") != 0) | |
483 | { | |
484 | llfatalbug(message("Error reading library. File may be corrupted")); | |
485 | } | |
486 | ||
487 | temp = constraint_undump (f); | |
488 | ret = constraintList_add (ret, temp); | |
489 | s = fgets (os, MAX_DUMP_LINE_LENGTH, f); | |
490 | free(c); | |
491 | } | |
492 | free(s); | |
493 | ||
494 | return ret; | |
495 | } | |
496 | ||
497 | ||
498 | void constraintList_dump (/*@observer@*/ constraintList c, FILE *f) | |
499 | { | |
500 | constraintList_elements (c, el) | |
501 | { | |
502 | fprintf(f, "C\n"); | |
503 | constraint_dump (el, f); | |
504 | } | |
505 | end_constraintList_elements; ; | |
506 | } | |
507 | ||
616915dd | 508 |