2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
26 ** constraintGeneration.c
29 /* #define DEBUGPRINT 1 */
31 # include <ctype.h> /* for isdigit */
32 # include "splintMacros.nf"
35 # include "cgrammar_tokens.h"
37 # include "exprChecks.h"
38 # include "exprNodeSList.h"
40 /*drl We need to access the internal representation of exprNode
41 because these functions walk down the parse tree and need a richer
42 information than is accessible through the exprNode interface.*/
46 static /*@nullwhentrue@*/ bool exprNode_handleError (/*@temp@*/ exprNode p_e);
48 static void exprNode_stmt (/*@temp@*/ /*@temp@*/ exprNode p_e);
49 static void exprNode_multiStatement (/*@temp@*/ exprNode p_e);
51 static constraintList exprNode_traversTrueEnsuresConstraints (/*@temp@*/ exprNode p_e);
52 static constraintList exprNode_traversFalseEnsuresConstraints (/*@temp@*/ exprNode p_e);
54 static void checkArgumentList (/*@out@*/ exprNode p_temp, exprNodeList p_arglist, fileloc p_sequencePoint) /*@modifies p_temp @*/;
56 static constraintList checkCall (/*@temp@*/ exprNode p_fcn, exprNodeList p_arglist);
58 static bool exprNode_isUnhandled (/*@temp@*/ /*@observer@*/ exprNode e)
60 llassert(exprNode_isDefined(e));
86 DPRINTF((message ("Warning current constraint generation does not handle expression %s", exprNode_unparse(e))));
98 /*@nullwhentrue@*/ bool exprNode_handleError (exprNode e)
100 if (exprNode_isError (e) || exprNode_isUnhandled (e))
108 /* evans 2002-03-2 - parameter was dependent */
109 bool /*@alt void@*/ exprNode_generateConstraints (/*@temp@*/ exprNode e)
111 if (exprNode_isError (e))
114 if (exprNode_isUnhandled (e))
116 DPRINTF((message("Warning ignoring %s", exprNode_unparse (e))));
120 DPRINTF((message ("exprNode_generateConstraints Analysising %s at %s", exprNode_unparse(e),
121 fileloc_unparse(exprNode_getfileloc(e)))));
123 if (exprNode_isMultiStatement (e))
125 exprNode_multiStatement(e);
131 /* loc = exprNode_getNextSequencePoint(e); */
132 /* exprNode_exprTraverse(e, FALSE, FALSE, loc); */
134 /* fileloc_free(loc); */
144 c = constraintList_makeFixedArrayConstraints (e->uses);
145 e->requiresConstraints = constraintList_reflectChangesFreePre (e->requiresConstraints, c);
146 constraintList_free(c);
149 DPRINTF ((message ("e->requiresConstraints %s", constraintList_unparseDetailed (e->requiresConstraints))));
153 static void exprNode_stmt (/*@temp@*/ exprNode e)
159 if (exprNode_isError(e))
164 /*e->requiresConstraints = constraintList_makeNew();
165 e->ensuresConstraints = constraintList_makeNew(); */
167 DPRINTF(("expNode_stmt: STMT:"));
168 s = exprNode_unparse(e);
169 DPRINTF ((message("exprNode_stmt: STMT: %s ", s)));
171 if (e->kind == XPR_INIT)
173 constraintList tempList;
175 DPRINTF ((message ("%s ", exprNode_unparse (e))));
176 loc = exprNode_getNextSequencePoint(e); /* reduces to an expression */
177 exprNode_exprTraverse (e, FALSE, FALSE, loc);
180 tempList = e->requiresConstraints;
181 e->requiresConstraints = exprNode_traversRequiresConstraints(e);
182 constraintList_free(tempList);
184 tempList = e->ensuresConstraints;
185 e->ensuresConstraints = exprNode_traversEnsuresConstraints(e);
186 constraintList_free(tempList);
190 /*drl 2/13/002 patched bug so return statement will be checked*/
191 /*return is a stmt not not expression ...*/
192 if (e->kind == XPR_RETURN)
194 constraintList tempList;
196 loc = exprNode_getNextSequencePoint(e); /* reduces to an expression */
198 exprNode_exprTraverse (exprData_getSingle (e->edata), FALSE, TRUE, loc);
201 tempList = e->requiresConstraints;
202 e->requiresConstraints = exprNode_traversRequiresConstraints(e);
203 constraintList_free(tempList);
206 if (e->kind != XPR_STMT)
209 DPRINTF (("Not Stmt"));
210 DPRINTF ((message ("%s ", exprNode_unparse (e))));
212 if (exprNode_isMultiStatement (e))
214 exprNode_multiStatement (e); /* evans 2001-08-21: spurious return removed */
218 loc = exprNode_getNextSequencePoint(e); /* reduces to an expression */
220 exprNode_exprTraverse (e, FALSE, TRUE, loc);
228 DPRINTF ((message ("%s ", exprNode_unparse (e))));
230 snode = exprData_getUopNode (e->edata);
232 /* could be stmt involving multiple statements:
233 i.e. if, while for ect.
236 if (exprNode_isMultiStatement (snode))
238 exprNode_multiStatement (snode);
239 (void) exprNode_copyConstraints (e, snode);
243 loc = exprNode_getNextSequencePoint(e); /* reduces to an expression */
244 exprNode_exprTraverse (snode, FALSE, FALSE, loc);
248 constraintList_free (e->requiresConstraints);
249 e->requiresConstraints = exprNode_traversRequiresConstraints(snode);
251 constraintList_free (e->ensuresConstraints);
252 e->ensuresConstraints = exprNode_traversEnsuresConstraints(snode);
254 DPRINTF ((message ("smtlist constraints are: pre: %s \n and \t post %s\n",
255 constraintList_unparse(e->requiresConstraints),
256 constraintList_unparse(e->ensuresConstraints))));
261 static void exprNode_stmtList (/*@dependent@*/ exprNode e)
263 exprNode stmt1, stmt2;
264 if (exprNode_isError (e))
270 Handle case of stmtList with only one statement:
271 The parse tree stores this as stmt instead of stmtList
274 if (e->kind != XPR_STMTLIST)
279 llassert (e->kind == XPR_STMTLIST);
280 DPRINTF(("exprNode_stmtList STMTLIST:"));
281 DPRINTF ((cstring_toCharsSafe (exprNode_unparse(e))));
282 stmt1 = exprData_getPairA (e->edata);
283 stmt2 = exprData_getPairB (e->edata);
286 DPRINTF(("exprNode_stmtlist "));
287 DPRINTF ((message("XW%s | %s", exprNode_unparse(stmt1), exprNode_unparse(stmt2))));
289 exprNode_stmt (stmt1);
290 DPRINTF(("\nstmt after stmtList call "));
292 exprNode_stmt (stmt2);
293 exprNode_mergeResolve (e, stmt1, stmt2);
295 DPRINTF ((message ("smtlist constraints are: pre: %s \n and \t post %s\n",
296 constraintList_unparse(e->requiresConstraints),
297 constraintList_unparse(e->ensuresConstraints))));
301 static exprNode doIf (/*@returned@*/ exprNode e, /*@dependent@*/ exprNode test, /*@dependent@*/ exprNode body)
305 DPRINTF ((message ("doIf: %s ", exprNode_unparse(e))));
307 llassert(exprNode_isDefined(test));
308 llassert (exprNode_isDefined (e));
309 llassert (exprNode_isDefined (body));
312 DPRINTF((message ("ensures constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->ensuresConstraints))));
314 DPRINTF((message ("Requires constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->ensuresConstraints))));
316 DPRINTF((message ("trueEnsures constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->trueEnsuresConstraints))));
318 DPRINTF((message ("falseEnsures constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->falseEnsuresConstraints))));
322 DPRINTF((message ("ensures constraints for %s are %s", exprNode_unparse(test), constraintList_unparseDetailed(test->ensuresConstraints))));
324 DPRINTF((message ("Requires constraints for %s are %s", exprNode_unparse(test), constraintList_unparseDetailed(test->ensuresConstraints))));
326 DPRINTF((message ("trueEnsures constraints for %s are %s", exprNode_unparse(test), constraintList_unparseDetailed(test->trueEnsuresConstraints))));
328 DPRINTF((message ("falseEnsures constraints for %s are %s", exprNode_unparse(test), constraintList_unparseDetailed(test->falseEnsuresConstraints))));
332 temp = test->trueEnsuresConstraints;
333 test->trueEnsuresConstraints = exprNode_traversTrueEnsuresConstraints(test);
334 constraintList_free(temp);
336 temp = test->ensuresConstraints;
337 test->ensuresConstraints = exprNode_traversEnsuresConstraints (test);
338 constraintList_free(temp);
340 temp = test->requiresConstraints;
341 test->requiresConstraints = exprNode_traversRequiresConstraints (test);
342 constraintList_free(temp);
345 test->trueEnsuresConstraints = constraintList_substituteFreeTarget(test->trueEnsuresConstraints, test->ensuresConstraints);
347 DPRINTF ((message ("doIf: test ensures %s ", constraintList_unparse(test->ensuresConstraints))));
349 DPRINTF ((message ("doIf: test true ensures %s ", constraintList_unparse(test->trueEnsuresConstraints))));
351 constraintList_free(e->requiresConstraints);
354 e->requiresConstraints = constraintList_reflectChanges(body->requiresConstraints, test->trueEnsuresConstraints);
356 e->requiresConstraints = constraintList_reflectChangesFreePre (e->requiresConstraints,
357 test->ensuresConstraints);
358 temp = e->requiresConstraints;
359 e->requiresConstraints = constraintList_mergeRequires (e->requiresConstraints, test->requiresConstraints);
360 constraintList_free(temp);
363 /* drl possible problem : warning bad */
364 constraintList_free(e->ensuresConstraints);
365 e->ensuresConstraints = constraintList_copy (test->ensuresConstraints);
367 if (exprNode_mayEscape (body))
369 DPRINTF ((message("doIf: the if statement body %s returns or exits", exprNode_unparse(body))));
370 e->ensuresConstraints = constraintList_mergeEnsuresFreeFirst (e->ensuresConstraints,
371 test->falseEnsuresConstraints);
374 DPRINTF ((message ("doIf: if requiers %s ", constraintList_unparse(e->requiresConstraints))));
380 Also used for condition i.e. ?: operation
383 This function assumes that p, trueBranch, falseBranch have have all been traversed
384 for constraints i.e. we assume that exprNode_traversEnsuresConstraints,
385 exprNode_traversRequiresConstraints, exprNode_traversTrueEnsuresConstraints,
386 exprNode_traversFalseEnsuresConstraints have all been run
389 static exprNode doIfElse (/*@returned@*/ exprNode e, /*@dependent@*/ exprNode p, /*@dependent@*/ exprNode trueBranch, /*@dependent@*/ exprNode falseBranch)
391 constraintList c1, cons, t, t2, f, f2;
393 llassert (exprNode_isDefined (e));
394 llassert (exprNode_isDefined (p));
395 llassert (exprNode_isDefined (trueBranch));
396 llassert (exprNode_isDefined (falseBranch));
397 DPRINTF ((message ("doIfElse: %s ", exprNode_unparse(e))));
399 /* do requires clauses */
400 c1 = constraintList_copy (p->ensuresConstraints);
402 t = constraintList_reflectChanges(trueBranch->requiresConstraints, p->trueEnsuresConstraints);
403 t = constraintList_reflectChangesFreePre (t, p->ensuresConstraints);
405 cons = constraintList_reflectChanges(falseBranch->requiresConstraints, p->falseEnsuresConstraints);
406 cons = constraintList_reflectChangesFreePre (cons, c1);
408 constraintList_free (e->requiresConstraints);
409 e->requiresConstraints = constraintList_mergeRequiresFreeFirst (t, cons);
410 e->requiresConstraints = constraintList_mergeRequiresFreeFirst (e->requiresConstraints, p->requiresConstraints);
412 /* do ensures clauses
413 find the the ensures lists for each subbranch
416 t = constraintList_mergeEnsures (p->trueEnsuresConstraints, trueBranch->ensuresConstraints);
418 t = constraintList_mergeEnsures (p->ensuresConstraints, t);
419 constraintList_free(t2);
421 f = constraintList_mergeEnsures (p->falseEnsuresConstraints, falseBranch->ensuresConstraints);
423 f = constraintList_mergeEnsures (p->ensuresConstraints, f);
424 constraintList_free(f2);
426 /* find ensures for whole if/else statement */
428 constraintList_free(e->ensuresConstraints);
430 e->ensuresConstraints = constraintList_logicalOr (t, f);
432 constraintList_free(t);
433 constraintList_free(f);
434 constraintList_free(cons);
435 constraintList_free(c1);
437 DPRINTF ((message ("doIfElse: if requires %q ", constraintList_unparse(e->requiresConstraints))));
438 DPRINTF ((message ("doIfElse: if ensures %q ", constraintList_unparse(e->ensuresConstraints))));
443 static exprNode doWhile (/*@returned@*/ exprNode e, /*@dependent@*/ exprNode test, /*@dependent@*/ exprNode body)
445 DPRINTF ((message ("doWhile: %s ", exprNode_unparse(e))));
446 return doIf (e, test, body);
449 /*@only@*/ constraintList constraintList_makeFixedArrayConstraints (/*@observer@*/ sRefSet s)
453 ret = constraintList_makeNew();
455 sRefSet_elements (s, el)
457 if (sRef_isFixedArray(el))
460 DPRINTF((message("%s is a fixed array",
462 size = sRef_getArraySize(el);
463 DPRINTF((message("%s is a fixed array with size %d",
464 sRef_unparse(el), (int)size)));
465 con = constraint_makeSRefSetBufferSize (el, size_toLong (size - 1));
466 ret = constraintList_add(ret, con);
470 DPRINTF((message("%s is not a fixed array",
474 if (sRef_isExternallyVisible (el))
477 DPRINTF((message("%s is externally visible",
479 con = constraint_makeSRefWriteSafeInt(el, 0);
480 ret = constraintList_add(ret, con);
482 con = constraint_makeSRefReadSafeInt(el, 0);
484 ret = constraintList_add(ret, con);
489 end_sRefSet_elements ;
491 DPRINTF((message("constraintList_makeFixedArrayConstraints returning %s",
492 constraintList_unparse(ret))));
497 exprNode makeDataTypeConstraints (/*@returned@*/ exprNode e)
500 DPRINTF(("makeDataTypeConstraints"));
502 c = constraintList_makeFixedArrayConstraints (e->uses);
504 e->ensuresConstraints = constraintList_addListFree (e->ensuresConstraints, c);
510 static void doFor (/*@dependent@*/ exprNode e, /*@dependent@*/ exprNode forPred, /*@dependent@*/ exprNode forBody)
512 exprNode init, test, inc;
513 /* merge the constraints: modle as if statement */
520 llassert (exprNode_isDefined (e));
521 llassert (exprNode_isDefined (forPred));
522 llassert (exprNode_isDefined (forBody));
524 init = exprData_getTripleInit (forPred->edata);
525 test = exprData_getTripleTest (forPred->edata);
526 inc = exprData_getTripleInc (forPred->edata);
528 if (((exprNode_isError (test) /*|| (exprNode_isError(init))*/) || (exprNode_isError (inc))))
530 DPRINTF ((message ("strange for statement:%s, ignoring it", exprNode_unparse(e))));
534 exprNode_forLoopHeuristics(e, forPred, forBody);
536 constraintList_free(e->requiresConstraints);
537 e->requiresConstraints = constraintList_reflectChanges(forBody->requiresConstraints, test->ensuresConstraints);
538 e->requiresConstraints = constraintList_reflectChangesFreePre (e->requiresConstraints, test->trueEnsuresConstraints);
539 e->requiresConstraints = constraintList_reflectChangesFreePre (e->requiresConstraints, forPred->ensuresConstraints);
541 if (!forBody->canBreak)
543 e->ensuresConstraints = constraintList_addListFree(e->ensuresConstraints, constraintList_copy(forPred->ensuresConstraints));
544 e->ensuresConstraints = constraintList_addListFree(e->ensuresConstraints,constraintList_copy(test->falseEnsuresConstraints));
548 DPRINTF(("Can break"));
552 static /*@dependent@*/ exprNode exprNode_makeDependent(/*@returned@*/ exprNode e)
554 /* !!! DRL - this is ridiculous! Read the manual on memory annotations please! */
559 exprNode_doGenerateConstraintSwitch
560 (/*@dependent@*/ exprNode switchExpr,
561 /*@dependent@*/ exprNode body,
562 /*@special@*/ constraintList *currentRequires,
563 /*@special@*/ constraintList *currentEnsures,
564 /*@special@*/ constraintList *savedRequires,
565 /*@special@*/ constraintList *savedEnsures)
566 /*@post:only *currentRequires, *currentEnsures, *savedRequires, *savedEnsures @*/
567 /*@sets *currentRequires, *currentEnsures, *savedRequires, *savedEnsures @*/
569 exprNode stmt, stmtList;
571 DPRINTF((message("exprNode_doGenerateConstraintSwitch: (switch %s) %s",
572 exprNode_unparse(switchExpr), exprNode_unparse(body)
575 if (exprNode_isError(body))
577 *currentRequires = constraintList_makeNew ();
578 *currentEnsures = constraintList_makeNew ();
580 *savedRequires = constraintList_makeNew ();
581 *savedEnsures = constraintList_makeNew ();
587 if (body->kind != XPR_STMTLIST)
589 DPRINTF((message("exprNode_doGenerateConstraintSwitch: non stmtlist: %s",
590 exprNode_unparse(body))));
592 stmtList = exprNode_undefined;
593 stmt = exprNode_makeDependent(stmt);
594 stmtList = exprNode_makeDependent(stmtList);
598 stmt = exprData_getPairB(body->edata);
599 stmtList = exprData_getPairA(body->edata);
600 stmt = exprNode_makeDependent(stmt);
601 stmtList = exprNode_makeDependent(stmtList);
604 DPRINTF((message("exprNode_doGenerateConstraintSwitch: stmtlist: %s stmt: %s",
605 exprNode_unparse(stmtList), exprNode_unparse(stmt))
609 exprNode_doGenerateConstraintSwitch (switchExpr, stmtList, currentRequires, currentEnsures,
610 savedRequires, savedEnsures);
612 if (exprNode_isError(stmt))
619 switchExpr = exprNode_makeDependent (switchExpr);
621 if (! exprNode_isCaseMarker(stmt))
626 DPRINTF ((message("Got normal statement %s (requires %s ensures %s)", exprNode_unparse(stmt),
627 constraintList_unparse(stmt->requiresConstraints), constraintList_unparse(stmt->ensuresConstraints))));
629 temp = constraintList_reflectChanges (stmt->requiresConstraints,
632 *currentRequires = constraintList_mergeRequiresFreeFirst(
636 constraintList_free(temp);
638 *currentEnsures = constraintList_mergeEnsuresFreeFirst
640 stmt->ensuresConstraints);
641 DPRINTF((message("returning from exprNode_doGenerateConstraintSwitch: (switch %s) %s currentRequires:"
642 "%s currentEnsures:%s",
643 exprNode_unparse(switchExpr), exprNode_unparse(body),
644 constraintList_unparse(*currentRequires), constraintList_unparse(*currentEnsures)
652 if (exprNode_isCaseMarker(stmt) && exprNode_mustEscape(stmtList))
655 ** merge current and saved constraint with Logical Or...
656 ** make a constraint for ensures
662 DPRINTF ((message("Got case marker")));
664 if (constraintList_isUndefined(*savedEnsures) &&
665 constraintList_isUndefined(*savedRequires))
667 llassert(constraintList_isUndefined(*savedEnsures));
668 llassert(constraintList_isUndefined(*savedRequires));
669 *savedEnsures = constraintList_copy(*currentEnsures);
670 *savedRequires = constraintList_copy(*currentRequires);
674 DPRINTF ((message("Doing logical or")));
675 temp = constraintList_logicalOr (*savedEnsures, *currentEnsures);
676 constraintList_free (*savedEnsures);
677 *savedEnsures = temp;
679 *savedRequires = constraintList_mergeRequiresFreeFirst (*savedRequires, *currentRequires);
682 con = constraint_makeEnsureEqual (switchExpr, exprData_getSingle (stmt->edata), exprNode_loc (stmt));
684 constraintList_free (*currentEnsures);
685 *currentEnsures = constraintList_makeNew();
686 *currentEnsures = constraintList_add(*currentEnsures, con);
688 constraintList_free(*currentRequires);
689 *currentRequires = constraintList_makeNew();
690 DPRINTF (("exprNode_doGenerateConstraintSwitch: (switch %s) %s savedRequires:"
691 "%s savedEnsures:%s",
692 exprNode_unparse(switchExpr), exprNode_unparse(body),
693 constraintList_unparse(*savedRequires), constraintList_unparse(*savedEnsures)
696 else if (exprNode_isCaseMarker(stmt)) /* prior case has no break. */
699 We don't do anything to the sved constraints because the case hasn't ended
700 The new ensures constraints for the case will be:
701 the constraint for the case statement (CASE_LABEL == SWITCH_EXPR) logicalOr currentEnsures
706 constraintList ensuresTemp;
708 con = constraint_makeEnsureEqual (switchExpr, exprData_getSingle (stmt->edata), exprNode_loc (stmt));
710 ensuresTemp = constraintList_makeNew ();
711 ensuresTemp = constraintList_add (ensuresTemp, con);
713 if (exprNode_isError (stmtList))
715 constraintList_free (*currentEnsures);
716 *currentEnsures = constraintList_copy (ensuresTemp);
717 constraintList_free (ensuresTemp);
721 temp = constraintList_logicalOr (*currentEnsures, ensuresTemp);
722 constraintList_free (*currentEnsures);
723 constraintList_free (ensuresTemp);
724 *currentEnsures = temp;
727 constraintList_free (*currentRequires);
728 *currentRequires = constraintList_makeNew();
733 we handle the case of ! exprNode_isCaseMarker above
734 the else if clause should always be true.
739 DPRINTF (("returning from exprNode_doGenerateConstraintSwitch: (switch %s) %s currentRequires:"
740 "%s currentEnsures:%s",
741 exprNode_unparse(switchExpr), exprNode_unparse(body),
742 constraintList_unparse(*currentRequires), constraintList_unparse(*currentEnsures)
751 static void exprNode_generateConstraintSwitch (/*@notnull@*/ exprNode switchStmt)
753 constraintList constraintsRequires;
754 constraintList constraintsEnsures;
755 constraintList lastRequires;
756 constraintList lastEnsures;
761 switchExpr = exprData_getPairA (switchStmt->edata);
762 body = exprData_getPairB (switchStmt->edata);
764 if (!exprNode_isDefined (body))
769 DPRINTF((message("")));
771 if (body->kind == XPR_BLOCK)
772 body = exprData_getSingle(body->edata);
775 constraintsRequires = constraintList_undefined;
776 constraintsEnsures = constraintList_undefined;
778 lastRequires = constraintList_makeNew();
779 lastEnsures = constraintList_makeNew();
783 /* evans 2002-01-01: spurious warnings for these becuase of stack allocated storage */
784 exprNode_doGenerateConstraintSwitch (switchExpr, body, &lastRequires,
785 &lastEnsures, &constraintsRequires, &constraintsEnsures);
789 merge current and saved constraint with Logical Or...
790 make a constraint for ensures
793 constraintList_free(switchStmt->requiresConstraints);
794 constraintList_free(switchStmt->ensuresConstraints);
796 if (constraintList_isDefined(constraintsEnsures) && constraintList_isDefined(constraintsRequires))
798 switchStmt->ensuresConstraints = constraintList_logicalOr(constraintsEnsures, lastEnsures);
799 switchStmt->requiresConstraints = constraintList_mergeRequires(constraintsRequires, lastRequires);
800 constraintList_free (constraintsRequires);
801 constraintList_free (constraintsEnsures);
805 switchStmt->ensuresConstraints = constraintList_copy(lastEnsures);
806 switchStmt->requiresConstraints = constraintList_copy(lastRequires);
809 constraintList_free (lastRequires);
810 constraintList_free (lastEnsures);
812 DPRINTF(((message(" exprNode_generateConstraintSwitch returning requires: %s and ensures %s",
813 constraintList_unparse(switchStmt->requiresConstraints),
814 constraintList_unparse(switchStmt->ensuresConstraints)
819 static exprNode doSwitch (/*@returned@*/ /*@notnull@*/ exprNode e)
825 DPRINTF ((message ("doSwitch for: switch (%s) %s",
826 exprNode_unparse (exprData_getPairA (data)),
827 exprNode_unparse (exprData_getPairB (data)))));
829 body = exprData_getPairB (data);
830 exprNode_generateConstraintSwitch (e);
834 void exprNode_multiStatement (/*@dependent@*/ exprNode e)
840 exprNode p, trueBranch, falseBranch;
841 exprNode forPred, forBody;
846 DPRINTF((message ("exprNode_multistatement Analysising %s %s at", exprNode_unparse(e),
847 fileloc_unparse(exprNode_getfileloc(e)))));
849 if (exprNode_handleError (e))
862 forPred = exprData_getPairA (data);
863 forBody = exprData_getPairB (data);
865 /* First generate the constraints */
866 exprNode_generateConstraints (forPred);
867 exprNode_generateConstraints (forBody);
870 doFor (e, forPred, forBody);
875 exprNode_generateConstraints (exprData_getTripleInit (data));
876 test = exprData_getTripleTest (data);
877 exprNode_exprTraverse (test,FALSE, FALSE, exprNode_loc(e));
878 exprNode_generateConstraints (exprData_getTripleInc (data));
880 if (!exprNode_isError(test))
882 constraintList temp2;
883 temp2 = test->trueEnsuresConstraints;
884 test->trueEnsuresConstraints = exprNode_traversTrueEnsuresConstraints(test);
885 constraintList_free(temp2);
888 exprNode_generateConstraints (exprData_getTripleInc (data));
892 e1 = exprData_getPairA (data);
893 e2 = exprData_getPairB (data);
895 exprNode_exprTraverse (e1,
896 FALSE, FALSE, exprNode_loc(e1));
898 exprNode_generateConstraints (e2);
900 e = doWhile (e, e1, e2);
906 DPRINTF ((exprNode_unparse(e)));
907 e1 = exprData_getPairA (data);
908 e2 = exprData_getPairB (data);
910 exprNode_exprTraverse (e1, FALSE, FALSE, exprNode_loc(e1));
912 exprNode_generateConstraints (e2);
913 e = doIf (e, e1, e2);
917 DPRINTF(("Starting IFELSE"));
918 p = exprData_getTriplePred (data);
920 trueBranch = exprData_getTripleTrue (data);
921 falseBranch = exprData_getTripleFalse (data);
923 exprNode_exprTraverse (p,
924 FALSE, FALSE, exprNode_loc(p));
925 exprNode_generateConstraints (trueBranch);
926 exprNode_generateConstraints (falseBranch);
928 llassert (exprNode_isDefined (p));
929 temp = p->ensuresConstraints;
930 p->ensuresConstraints = exprNode_traversEnsuresConstraints (p);
931 constraintList_free(temp);
933 temp = p->requiresConstraints;
934 p->requiresConstraints = exprNode_traversRequiresConstraints (p);
935 constraintList_free(temp);
937 temp = p->trueEnsuresConstraints;
938 p->trueEnsuresConstraints = exprNode_traversTrueEnsuresConstraints(p);
939 constraintList_free(temp);
943 DPRINTF((message("p->trueEnsuresConstraints before substitue %s", constraintList_unparse(p->trueEnsuresConstraints) )
946 /*drl 10/10/2002 this is a bit of a hack but the reason why we do this is so that any function post conditions or similar things get applied correctly to each branch. e.g. in strlen(s) < 5 we want the trueEnsures to be maxRead(s) < 5*/
948 p->trueEnsuresConstraints = constraintList_substituteFreeTarget (p->trueEnsuresConstraints,
949 p->ensuresConstraints);
951 DPRINTF(( message ("p->trueEnsuresConstraints after substitue %s", constraintList_unparse(p->trueEnsuresConstraints) )
954 temp = p->falseEnsuresConstraints;
955 p->falseEnsuresConstraints = exprNode_traversFalseEnsuresConstraints(p);
956 constraintList_free(temp);
958 /*See comment on trueEnsures*/
959 p->falseEnsuresConstraints = constraintList_substituteFreeTarget (p->falseEnsuresConstraints,
960 p->ensuresConstraints);
962 e = doIfElse (e, p, trueBranch, falseBranch);
963 DPRINTF(("Done IFELSE"));
968 e2 = (exprData_getPairB (data));
969 e1 = (exprData_getPairA (data));
971 DPRINTF((message ("do { %s } while (%s)", exprNode_unparse(e2), exprNode_unparse(e1))));
972 exprNode_generateConstraints (e2);
973 exprNode_generateConstraints (e1);
974 e = exprNode_copyConstraints (e, e2);
975 DPRINTF ((message ("e = %s ", constraintList_unparse(e->requiresConstraints))));
983 tempExpr = exprData_getSingle (data);
985 exprNode_generateConstraints (tempExpr);
987 if (exprNode_isDefined(tempExpr) )
989 constraintList_free(e->requiresConstraints);
990 e->requiresConstraints = constraintList_copy (tempExpr->requiresConstraints);
991 constraintList_free(e->ensuresConstraints);
992 e->ensuresConstraints = constraintList_copy (tempExpr->ensuresConstraints);
1006 exprNode_stmtList (e);
1016 static bool lltok_isBoolean_Op (lltok tok)
1018 /*this should really be a switch statement but
1019 I don't want to violate the abstraction
1020 maybe this should go in lltok.c */
1022 if (lltok_isEqOp (tok))
1026 if (lltok_isAndOp (tok))
1032 if (lltok_isOrOp (tok))
1037 if (lltok_isGt_Op (tok))
1041 if (lltok_isLt_Op (tok))
1046 if (lltok_isLe_Op (tok))
1051 if (lltok_isGe_Op (tok))
1061 static void exprNode_booleanTraverse (/*@dependent@*/ exprNode e, /*@unused@*/ bool definatelv, /*@unused@*/ bool definaterv, fileloc sequencePoint)
1067 constraintList tempList, temp;
1069 if (exprNode_isUndefined(e) )
1071 llassert (exprNode_isDefined(e) );
1077 tok = exprData_getOpTok (data);
1078 t1 = exprData_getOpA (data);
1079 t2 = exprData_getOpB (data);
1081 /* drl 3/2/2003 we know this because of the type of expression*/
1082 llassert( exprNode_isDefined(t1) && exprNode_isDefined(t2) );
1085 tempList = constraintList_undefined;
1087 /* arithmetic tests */
1089 if (lltok_isEqOp (tok))
1091 cons = constraint_makeEnsureEqual (t1, t2, sequencePoint);
1092 e->trueEnsuresConstraints = constraintList_add(e->trueEnsuresConstraints, cons);
1096 if (lltok_isLt_Op (tok))
1098 cons = constraint_makeEnsureLessThan (t1, t2, sequencePoint);
1099 e->trueEnsuresConstraints = constraintList_add(e->trueEnsuresConstraints, cons);
1100 cons = constraint_makeEnsureGreaterThanEqual (t1, t2, sequencePoint);
1101 e->falseEnsuresConstraints = constraintList_add(e->falseEnsuresConstraints, cons);
1104 if (lltok_isGe_Op (tok))
1106 cons = constraint_makeEnsureGreaterThanEqual (t1, t2, sequencePoint);
1107 e->trueEnsuresConstraints = constraintList_add(e->trueEnsuresConstraints, cons);
1109 cons = constraint_makeEnsureLessThan (t1, t2, sequencePoint);
1110 e->falseEnsuresConstraints = constraintList_add(e->falseEnsuresConstraints, cons);
1113 if (lltok_isGt_Op (tok))
1115 cons = constraint_makeEnsureGreaterThan (t1, t2, sequencePoint);
1116 e->trueEnsuresConstraints = constraintList_add(e->trueEnsuresConstraints, cons);
1117 cons = constraint_makeEnsureLessThanEqual (t1, t2, sequencePoint);
1118 e->falseEnsuresConstraints = constraintList_add(e->falseEnsuresConstraints, cons);
1121 if (lltok_isLe_Op (tok))
1123 cons = constraint_makeEnsureLessThanEqual (t1, t2, sequencePoint);
1124 e->trueEnsuresConstraints = constraintList_add(e->trueEnsuresConstraints, cons);
1126 cons = constraint_makeEnsureGreaterThan (t1, t2, sequencePoint);
1127 e->falseEnsuresConstraints = constraintList_add(e->falseEnsuresConstraints, cons);
1130 /* Logical operations */
1132 if (lltok_isAndOp (tok))
1135 tempList = constraintList_copy (t1->trueEnsuresConstraints);
1136 tempList = constraintList_addList (tempList, t2->trueEnsuresConstraints);
1137 e->trueEnsuresConstraints = constraintList_addListFree(e->trueEnsuresConstraints, tempList);
1139 /* false ensures: fens t1 or tens t1 and fens t2 */
1140 tempList = constraintList_copy (t1->trueEnsuresConstraints);
1141 tempList = constraintList_addList (tempList, t2->falseEnsuresConstraints);
1143 tempList = constraintList_logicalOr (tempList, t1->falseEnsuresConstraints);
1144 constraintList_free (temp);
1146 /* evans - was constraintList_addList - memory leak detected by splint */
1147 e->falseEnsuresConstraints = constraintList_addListFree (e->falseEnsuresConstraints, tempList);
1149 else if (lltok_isOrOp (tok))
1152 tempList = constraintList_copy (t1->falseEnsuresConstraints);
1153 tempList = constraintList_addList (tempList, t2->falseEnsuresConstraints);
1154 e->falseEnsuresConstraints = constraintList_addListFree(e->falseEnsuresConstraints, tempList);
1156 /* true ensures: tens t1 or fens t1 and tens t2 */
1157 tempList = constraintList_copy (t1->falseEnsuresConstraints);
1158 tempList = constraintList_addList (tempList, t2->trueEnsuresConstraints);
1161 tempList = constraintList_logicalOr (tempList, t1->trueEnsuresConstraints);
1162 constraintList_free(temp);
1164 e->trueEnsuresConstraints = constraintList_addListFree(e->trueEnsuresConstraints, tempList);
1165 tempList = constraintList_undefined;
1169 DPRINTF((message("%s is not a boolean operation", lltok_unparse(tok))));
1173 void exprNode_exprTraverse (/*@dependent@*/ exprNode e, bool definatelv, bool definaterv, /*@observer@*/ /*@temp@*/ fileloc sequencePoint)
1175 exprNode t1, t2, fcn;
1177 bool handledExprNode;
1181 constraintList temp;
1183 if (exprNode_isError(e))
1188 DPRINTF((message ("exprNode_exprTraverset Analysising %s %s at", exprNode_unparse(e),
1189 fileloc_unparse(exprNode_getfileloc(e)))));
1191 /*e->requiresConstraints = constraintList_makeNew();
1192 e->ensuresConstraints = constraintList_makeNew();
1193 e->trueEnsuresConstraints = constraintList_makeNew();;
1194 e->falseEnsuresConstraints = constraintList_makeNew();;
1197 if (exprNode_isUnhandled (e))
1202 handledExprNode = TRUE;
1209 t1 = exprData_getSingle (data);
1210 exprNode_exprTraverse (t1, definatelv, definaterv, sequencePoint);
1211 e = exprNode_copyConstraints (e, t1);
1218 t1 = (exprData_getPairA (data));
1219 t2 = (exprData_getPairB (data));
1220 cons = constraint_makeWriteSafeExprNode (t1, t2);
1224 t1 = (exprData_getPairA (data));
1225 t2 = (exprData_getPairB (data));
1226 cons = constraint_makeReadSafeExprNode (t1, t2);
1229 e->requiresConstraints = constraintList_add(e->requiresConstraints, cons);
1230 cons = constraint_makeEnsureMaxReadAtLeast (t1, t2, sequencePoint);
1231 e->ensuresConstraints = constraintList_add(e->ensuresConstraints, cons);
1233 cons = constraint_makeEnsureLteMaxRead (t2, t1);
1234 e->trueEnsuresConstraints = constraintList_add(e->trueEnsuresConstraints, cons);
1236 exprNode_exprTraverse (exprData_getPairA (data), FALSE, TRUE, sequencePoint);
1237 exprNode_exprTraverse (exprData_getPairB (data), FALSE, TRUE, sequencePoint);
1242 exprNode_exprTraverse (exprData_getUopNode (e->edata), definatelv, definaterv, sequencePoint);
1252 t = exprData_getInitId (data);
1253 ue = usymtab_lookup (idDecl_observeId (t));
1254 lhs = exprNode_createId (ue);
1256 t2 = exprData_getInitNode (data);
1258 /* DPRINTF(((message("initialization: %s = %s",
1259 exprNode_unparse(lhs),
1260 exprNode_unparse(t2)
1264 exprNode_exprTraverse (t2, definatelv, TRUE, sequencePoint);
1266 /* this test is nessecary because some expressions generate a null expression node. function pointer do that -- drl */
1267 if ((!exprNode_isError (e)) && (!exprNode_isError(t2)))
1269 cons = constraint_makeEnsureEqual (e, t2, sequencePoint);
1270 e->ensuresConstraints = constraintList_add(e->ensuresConstraints, cons);
1276 t1 = exprData_getOpA (data);
1277 t2 = exprData_getOpB (data);
1278 exprNode_exprTraverse (t1, TRUE, definaterv, sequencePoint);
1279 exprNode_exprTraverse (t2, definatelv, TRUE, sequencePoint);
1281 /* this test is nessecary because some expressions generate a null expression node. function pointer do that -- drl */
1282 if ((!exprNode_isError (t1)) && (!exprNode_isError(t2)))
1284 cons = constraint_makeEnsureEqual (t1, t2, sequencePoint);
1285 e->ensuresConstraints = constraintList_add(e->ensuresConstraints, cons);
1289 t1 = exprData_getOpA (data);
1290 t2 = exprData_getOpB (data);
1291 tok = exprData_getOpTok (data);
1293 if (lltok_getTok (tok) == ADD_ASSIGN)
1295 exprNode_exprTraverse (t1, TRUE, definaterv, sequencePoint);
1296 exprNode_exprTraverse (t2, definatelv, TRUE, sequencePoint);
1298 cons = constraint_makeAddAssign (t1, t2, sequencePoint);
1299 e->ensuresConstraints = constraintList_add(e->ensuresConstraints, cons);
1301 else if (lltok_getTok (tok) == SUB_ASSIGN)
1303 exprNode_exprTraverse (t1, TRUE, definaterv, sequencePoint);
1304 exprNode_exprTraverse (t2, definatelv, TRUE, sequencePoint);
1306 cons = constraint_makeSubtractAssign (t1, t2, sequencePoint);
1307 e->ensuresConstraints = constraintList_add(e->ensuresConstraints, cons);
1311 exprNode_exprTraverse (t1, definatelv, definaterv, sequencePoint);
1312 exprNode_exprTraverse (t2, definatelv, definaterv, sequencePoint);
1315 if (lltok_isBoolean_Op (tok))
1316 exprNode_booleanTraverse (e, definatelv, definaterv, sequencePoint);
1320 /*drl 4-11-03 I think this is the same as the next case...*/
1326 C standard says operand to sizeof isn't evaluated unless
1327 its a variable length array. So we don't generate constraints.
1333 fcn = exprData_getFcn(data);
1335 exprNode_exprTraverse (fcn, definatelv, definaterv, sequencePoint);
1336 DPRINTF ((message ("Got call that %s (%s) ", exprNode_unparse(fcn), exprNodeList_unparse (exprData_getArgs (data)))));
1338 llassert( exprNode_isDefined(fcn) );
1340 fcn->requiresConstraints = constraintList_addListFree (fcn->requiresConstraints,
1341 checkCall (fcn, exprData_getArgs (data) ));
1343 fcn->ensuresConstraints = constraintList_addListFree (fcn->ensuresConstraints,
1344 exprNode_getPostConditions(fcn, exprData_getArgs (data),e ));
1346 t1 = exprNode_createNew (exprNode_getType (e));
1347 checkArgumentList (t1, exprData_getArgs(data), sequencePoint);
1348 exprNode_mergeResolve (e, t1, fcn);
1353 exprNode_exprTraverse (exprData_getSingle (data), definatelv, definaterv, sequencePoint);
1356 case XPR_NULLRETURN:
1362 exprNode_exprTraverse (exprData_getFieldNode (data), definatelv, definaterv, sequencePoint);
1366 exprNode_exprTraverse (exprData_getFieldNode (data), definatelv, definaterv, sequencePoint);
1369 case XPR_STRINGLITERAL:
1378 t1 = exprData_getUopNode(data);
1381 /* drl 3/2/2003 we know this because of the type of expression*/
1382 llassert( exprNode_isDefined(t1) );
1385 tok = (exprData_getUopTok (data));
1386 exprNode_exprTraverse (t1, definatelv, definaterv, sequencePoint);
1387 /*handle * pointer access */
1388 if (lltok_isIncOp (tok))
1390 DPRINTF(("doing ++(var)"));
1391 t1 = exprData_getUopNode (data);
1392 cons = constraint_makeMaxSetSideEffectPostIncrement (t1, sequencePoint);
1393 e->ensuresConstraints = constraintList_add (e->ensuresConstraints, cons);
1395 else if (lltok_isDecOp (tok))
1397 DPRINTF(("doing --(var)"));
1398 t1 = exprData_getUopNode (data);
1399 cons = constraint_makeMaxSetSideEffectPostDecrement (t1, sequencePoint);
1400 e->ensuresConstraints = constraintList_add (e->ensuresConstraints, cons);
1402 else if (lltok_isMult(tok ))
1406 cons = constraint_makeWriteSafeInt (t1, 0);
1410 cons = constraint_makeReadSafeInt (t1, 0);
1412 e->requiresConstraints = constraintList_add(e->requiresConstraints, cons);
1414 else if (lltok_isNotOp (tok))
1417 constraintList_free(e->trueEnsuresConstraints);
1419 e->trueEnsuresConstraints = constraintList_copy (t1->falseEnsuresConstraints);
1420 constraintList_free(e->falseEnsuresConstraints);
1421 e->falseEnsuresConstraints = constraintList_copy (t1->trueEnsuresConstraints);
1424 else if (lltok_isAmpersand_Op (tok))
1428 else if (lltok_isMinus_Op (tok))
1432 else if (lltok_isExcl_Op (tok))
1436 else if (lltok_isTilde_Op (tok))
1442 llcontbug (message("Unsupported preop in %s", exprNode_unparse(e)));
1448 exprNode_exprTraverse (exprData_getUopNode (data), TRUE,
1449 definaterv, sequencePoint);
1451 if (lltok_isIncOp (exprData_getUopTok (data)))
1453 DPRINTF(("doing ++"));
1454 t1 = exprData_getUopNode (data);
1455 cons = constraint_makeMaxSetSideEffectPostIncrement (t1, sequencePoint);
1456 e->ensuresConstraints = constraintList_add (e->ensuresConstraints, cons);
1458 if (lltok_isDecOp (exprData_getUopTok (data)))
1460 DPRINTF(("doing --"));
1461 t1 = exprData_getUopNode (data);
1462 cons = constraint_makeMaxSetSideEffectPostDecrement (t1, sequencePoint);
1463 e->ensuresConstraints = constraintList_add (e->ensuresConstraints, cons);
1468 t2 = exprData_getCastNode (data);
1469 DPRINTF ((message ("Examining cast (%q)%s",
1470 qtype_unparse (exprData_getCastType (data)),
1471 exprNode_unparse (t2))
1473 exprNode_exprTraverse (t2, definatelv, definaterv, sequencePoint);
1479 exprNode pred, trueBranch, falseBranch;
1481 pred = exprData_getTriplePred (data);
1482 trueBranch = exprData_getTripleTrue (data);
1483 falseBranch = exprData_getTripleFalse (data);
1485 llassert (exprNode_isDefined (pred));
1486 llassert (exprNode_isDefined (trueBranch));
1487 llassert (exprNode_isDefined (falseBranch));
1489 exprNode_exprTraverse (pred, FALSE, TRUE, sequencePoint);
1491 temp = pred->ensuresConstraints;
1492 pred->ensuresConstraints = exprNode_traversEnsuresConstraints(pred);
1493 constraintList_free(temp);
1495 temp = pred->requiresConstraints;
1496 pred->requiresConstraints = exprNode_traversRequiresConstraints(pred);
1497 constraintList_free(temp);
1499 temp = pred->trueEnsuresConstraints;
1500 pred->trueEnsuresConstraints = exprNode_traversTrueEnsuresConstraints(pred);
1501 constraintList_free(temp);
1503 temp = pred->falseEnsuresConstraints;
1504 pred->falseEnsuresConstraints = exprNode_traversFalseEnsuresConstraints(pred);
1505 constraintList_free(temp);
1507 exprNode_exprTraverse (trueBranch, FALSE, TRUE, sequencePoint);
1509 temp = trueBranch->ensuresConstraints;
1510 trueBranch->ensuresConstraints = exprNode_traversEnsuresConstraints(trueBranch);
1511 constraintList_free(temp);
1513 temp = trueBranch->requiresConstraints;
1514 trueBranch->requiresConstraints = exprNode_traversRequiresConstraints(trueBranch);
1515 constraintList_free(temp);
1518 temp = trueBranch->trueEnsuresConstraints;
1519 trueBranch->trueEnsuresConstraints = exprNode_traversTrueEnsuresConstraints(trueBranch);
1520 constraintList_free(temp);
1522 temp = trueBranch->falseEnsuresConstraints;
1523 trueBranch->falseEnsuresConstraints = exprNode_traversFalseEnsuresConstraints(trueBranch);
1524 constraintList_free(temp);
1526 exprNode_exprTraverse (falseBranch, FALSE, TRUE, sequencePoint);
1528 temp = falseBranch->ensuresConstraints;
1529 falseBranch->ensuresConstraints = exprNode_traversEnsuresConstraints(falseBranch);
1530 constraintList_free(temp);
1533 temp = falseBranch->requiresConstraints;
1534 falseBranch->requiresConstraints = exprNode_traversRequiresConstraints(falseBranch);
1535 constraintList_free(temp);
1537 temp = falseBranch->trueEnsuresConstraints;
1538 falseBranch->trueEnsuresConstraints = exprNode_traversTrueEnsuresConstraints(falseBranch);
1539 constraintList_free(temp);
1541 temp = falseBranch->falseEnsuresConstraints;
1542 falseBranch->falseEnsuresConstraints = exprNode_traversFalseEnsuresConstraints(falseBranch);
1543 constraintList_free(temp);
1545 /* if pred is true e equals true otherwise pred equals false */
1547 cons = constraint_makeEnsureEqual (e, trueBranch, sequencePoint);
1548 trueBranch->ensuresConstraints = constraintList_add(trueBranch->ensuresConstraints, cons);
1550 cons = constraint_makeEnsureEqual (e, trueBranch, sequencePoint);
1551 falseBranch->ensuresConstraints = constraintList_add(falseBranch->ensuresConstraints, cons);
1553 e = doIfElse (e, pred, trueBranch, falseBranch);
1558 t1 = exprData_getPairA (data);
1559 t2 = exprData_getPairB (data);
1560 /* we essiantially treat this like expr1; expr2
1561 of course sequencePoint isn't adjusted so this isn't completely accurate
1563 exprNode_exprTraverse (t1, FALSE, FALSE, sequencePoint);
1564 exprNode_exprTraverse (t2, definatelv, definaterv, sequencePoint);
1565 exprNode_mergeResolve (e, t1, t2);
1569 handledExprNode = FALSE;
1572 e->requiresConstraints = constraintList_preserveOrig (e->requiresConstraints);
1573 e->ensuresConstraints = constraintList_preserveOrig (e->ensuresConstraints);
1574 e->requiresConstraints = constraintList_addGeneratingExpr (e->requiresConstraints, e);
1576 e->ensuresConstraints = constraintList_addGeneratingExpr (e->ensuresConstraints, e);
1579 e->requiresConstraints = constraintList_removeSurpressed(e->requiresConstraints);
1581 DPRINTF((message ("ensures constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->ensuresConstraints))));
1583 DPRINTF((message ("Requires constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->ensuresConstraints))));
1585 DPRINTF((message ("trueEnsures constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->trueEnsuresConstraints))));
1587 DPRINTF((message ("falseEnsures constraints for %s are %s", exprNode_unparse(e), constraintList_unparseDetailed(e->falseEnsuresConstraints))));
1593 constraintList exprNode_traversTrueEnsuresConstraints (exprNode e)
1597 bool handledExprNode;
1601 if (exprNode_handleError (e))
1603 ret = constraintList_makeNew();
1607 ret = constraintList_copy (e->trueEnsuresConstraints);
1609 handledExprNode = TRUE;
1616 t1 = exprData_getSingle (data);
1617 ret = constraintList_addListFree (ret, exprNode_traversTrueEnsuresConstraints (t1));
1622 ret = constraintList_addListFree (ret,
1623 exprNode_traversTrueEnsuresConstraints
1624 (exprData_getPairA (data)));
1626 ret = constraintList_addListFree (ret,
1627 exprNode_traversTrueEnsuresConstraints
1628 (exprData_getPairB (data)));
1632 ret = constraintList_addListFree (ret,
1633 exprNode_traversTrueEnsuresConstraints
1634 (exprData_getUopNode (data)));
1638 ret = constraintList_addListFree (ret, exprNode_traversTrueEnsuresConstraints
1639 (exprData_getUopNode (data)));
1643 ret = constraintList_addListFree (ret,
1644 exprNode_traversTrueEnsuresConstraints
1645 (exprData_getInitNode (data)));
1650 ret = constraintList_addListFree (ret,
1651 exprNode_traversTrueEnsuresConstraints
1652 (exprData_getOpA (data)));
1654 ret = constraintList_addListFree (ret,
1655 exprNode_traversTrueEnsuresConstraints
1656 (exprData_getOpB (data)));
1659 ret = constraintList_addListFree (ret,
1660 exprNode_traversTrueEnsuresConstraints
1661 (exprData_getOpA (data)));
1663 ret = constraintList_addListFree (ret,
1664 exprNode_traversTrueEnsuresConstraints
1665 (exprData_getOpB (data)));
1672 ret = constraintList_addListFree (ret,
1673 exprNode_traversTrueEnsuresConstraints
1674 (exprData_getSingle (data)));
1678 ret = constraintList_addListFree (ret,
1679 exprNode_traversTrueEnsuresConstraints
1680 (exprData_getFcn (data)));
1684 ret = constraintList_addListFree (ret,
1685 exprNode_traversTrueEnsuresConstraints
1686 (exprData_getSingle (data)));
1689 case XPR_NULLRETURN:
1693 ret = constraintList_addListFree (ret,
1694 exprNode_traversTrueEnsuresConstraints
1695 (exprData_getFieldNode (data)));
1699 ret = constraintList_addListFree (ret,
1700 exprNode_traversTrueEnsuresConstraints
1701 (exprData_getFieldNode (data)));
1704 case XPR_STRINGLITERAL:
1711 ret = constraintList_addListFree (ret,
1712 exprNode_traversTrueEnsuresConstraints
1713 (exprData_getUopNode (data)));
1718 ret = constraintList_addListFree (ret,
1719 exprNode_traversTrueEnsuresConstraints
1720 (exprData_getCastNode (data)));
1730 constraintList exprNode_traversFalseEnsuresConstraints (exprNode e)
1733 bool handledExprNode;
1737 if (exprNode_handleError (e))
1739 ret = constraintList_makeNew();
1743 ret = constraintList_copy (e->falseEnsuresConstraints);
1744 handledExprNode = TRUE;
1750 t1 = exprData_getSingle (data);
1751 ret = constraintList_addListFree (ret,exprNode_traversFalseEnsuresConstraints (t1));
1756 ret = constraintList_addListFree (ret,
1757 exprNode_traversFalseEnsuresConstraints
1758 (exprData_getPairA (data)));
1760 ret = constraintList_addListFree (ret,
1761 exprNode_traversFalseEnsuresConstraints
1762 (exprData_getPairB (data)));
1766 ret = constraintList_addListFree (ret,
1767 exprNode_traversFalseEnsuresConstraints
1768 (exprData_getUopNode (data)));
1772 ret = constraintList_addListFree (ret, exprNode_traversFalseEnsuresConstraints
1773 (exprData_getUopNode (data)));
1776 ret = constraintList_addListFree (ret,
1777 exprNode_traversFalseEnsuresConstraints
1778 ( exprData_getInitNode (data)));
1782 ret = constraintList_addListFree (ret,
1783 exprNode_traversFalseEnsuresConstraints
1784 (exprData_getOpA (data)));
1786 ret = constraintList_addListFree (ret,
1787 exprNode_traversFalseEnsuresConstraints
1788 (exprData_getOpB (data)));
1791 ret = constraintList_addListFree (ret,
1792 exprNode_traversFalseEnsuresConstraints
1793 (exprData_getOpA (data)));
1795 ret = constraintList_addListFree (ret,
1796 exprNode_traversFalseEnsuresConstraints
1797 (exprData_getOpB (data)));
1804 ret = constraintList_addListFree (ret,
1805 exprNode_traversFalseEnsuresConstraints
1806 (exprData_getSingle (data)));
1810 ret = constraintList_addListFree (ret,
1811 exprNode_traversFalseEnsuresConstraints
1812 (exprData_getFcn (data)));
1816 ret = constraintList_addListFree (ret,
1817 exprNode_traversFalseEnsuresConstraints
1818 (exprData_getSingle (data)));
1821 case XPR_NULLRETURN:
1825 ret = constraintList_addListFree (ret,
1826 exprNode_traversFalseEnsuresConstraints
1827 (exprData_getFieldNode (data)));
1831 ret = constraintList_addListFree (ret,
1832 exprNode_traversFalseEnsuresConstraints
1833 (exprData_getFieldNode (data)));
1836 case XPR_STRINGLITERAL:
1843 ret = constraintList_addListFree (ret,
1844 exprNode_traversFalseEnsuresConstraints
1845 (exprData_getUopNode (data)));
1850 ret = constraintList_addListFree (ret,
1851 exprNode_traversFalseEnsuresConstraints
1852 (exprData_getCastNode (data)));
1863 /* walk down the tree and get all requires Constraints in each subexpression*/
1864 /*@only@*/ constraintList exprNode_traversRequiresConstraints (exprNode e)
1868 bool handledExprNode;
1872 if (exprNode_handleError (e))
1874 ret = constraintList_makeNew();
1878 ret = constraintList_copy (e->requiresConstraints);
1879 handledExprNode = TRUE;
1885 t1 = exprData_getSingle (data);
1886 ret = constraintList_addListFree (ret, exprNode_traversRequiresConstraints (t1));
1891 ret = constraintList_addListFree (ret,
1892 exprNode_traversRequiresConstraints
1893 (exprData_getPairA (data)));
1895 ret = constraintList_addListFree (ret,
1896 exprNode_traversRequiresConstraints
1897 (exprData_getPairB (data)));
1901 ret = constraintList_addListFree (ret,
1902 exprNode_traversRequiresConstraints
1903 (exprData_getUopNode (data)));
1907 ret = constraintList_addListFree (ret, exprNode_traversRequiresConstraints
1908 (exprData_getUopNode (data)));
1911 ret = constraintList_addListFree (ret,
1912 exprNode_traversRequiresConstraints
1913 (exprData_getInitNode (data)));
1917 ret = constraintList_addListFree (ret,
1918 exprNode_traversRequiresConstraints
1919 (exprData_getOpA (data)));
1921 ret = constraintList_addListFree (ret,
1922 exprNode_traversRequiresConstraints
1923 (exprData_getOpB (data)));
1926 ret = constraintList_addListFree (ret,
1927 exprNode_traversRequiresConstraints
1928 (exprData_getOpA (data)));
1930 ret = constraintList_addListFree (ret,
1931 exprNode_traversRequiresConstraints
1932 (exprData_getOpB (data)));
1939 ret = constraintList_addListFree (ret,
1940 exprNode_traversRequiresConstraints
1941 (exprData_getSingle (data)));
1945 ret = constraintList_addListFree (ret,
1946 exprNode_traversRequiresConstraints
1947 (exprData_getFcn (data)));
1951 ret = constraintList_addListFree (ret,
1952 exprNode_traversRequiresConstraints
1953 (exprData_getSingle (data)));
1956 case XPR_NULLRETURN:
1960 ret = constraintList_addListFree (ret,
1961 exprNode_traversRequiresConstraints
1962 (exprData_getFieldNode (data)));
1966 ret = constraintList_addListFree (ret,
1967 exprNode_traversRequiresConstraints
1968 (exprData_getFieldNode (data)));
1971 case XPR_STRINGLITERAL:
1978 ret = constraintList_addListFree (ret,
1979 exprNode_traversRequiresConstraints
1980 (exprData_getUopNode (data)));
1985 ret = constraintList_addListFree (ret,
1986 exprNode_traversRequiresConstraints
1987 (exprData_getCastNode (data)));
1998 /* walk down the tree and get all Ensures Constraints in each subexpression*/
1999 /*@only@*/ constraintList exprNode_traversEnsuresConstraints (exprNode e)
2003 bool handledExprNode;
2007 if (exprNode_handleError (e))
2009 ret = constraintList_makeNew();
2013 ret = constraintList_copy (e->ensuresConstraints);
2014 handledExprNode = TRUE;
2018 DPRINTF ((message ("exprnode_traversEnsuresConstraints call for %s with "
2019 "constraintList of %s",
2020 exprNode_unparse (e),
2021 constraintList_unparse(e->ensuresConstraints)
2029 t1 = exprData_getSingle (data);
2030 ret = constraintList_addListFree (ret,exprNode_traversEnsuresConstraints (t1));
2034 ret = constraintList_addListFree (ret,
2035 exprNode_traversEnsuresConstraints
2036 (exprData_getPairA (data)));
2038 ret = constraintList_addListFree (ret,
2039 exprNode_traversEnsuresConstraints
2040 (exprData_getPairB (data)));
2043 ret = constraintList_addListFree (ret,
2044 exprNode_traversEnsuresConstraints
2045 (exprData_getUopNode (data)));
2049 ret = constraintList_addListFree (ret, exprNode_traversEnsuresConstraints
2050 (exprData_getUopNode (data)));
2054 ret = constraintList_addListFree (ret,
2055 exprNode_traversEnsuresConstraints
2056 (exprData_getInitNode (data)));
2061 ret = constraintList_addListFree (ret,
2062 exprNode_traversEnsuresConstraints
2063 (exprData_getOpA (data)));
2065 ret = constraintList_addListFree (ret,
2066 exprNode_traversEnsuresConstraints
2067 (exprData_getOpB (data)));
2070 ret = constraintList_addListFree (ret,
2071 exprNode_traversEnsuresConstraints
2072 (exprData_getOpA (data)));
2074 ret = constraintList_addListFree (ret,
2075 exprNode_traversEnsuresConstraints
2076 (exprData_getOpB (data)));
2082 ret = constraintList_addListFree (ret,
2083 exprNode_traversEnsuresConstraints
2084 (exprData_getSingle (data)));
2087 ret = constraintList_addListFree (ret,
2088 exprNode_traversEnsuresConstraints
2089 (exprData_getFcn (data)));
2092 ret = constraintList_addListFree (ret,
2093 exprNode_traversEnsuresConstraints
2094 (exprData_getSingle (data)));
2096 case XPR_NULLRETURN:
2099 ret = constraintList_addListFree (ret,
2100 exprNode_traversEnsuresConstraints
2101 (exprData_getFieldNode (data)));
2104 ret = constraintList_addListFree (ret,
2105 exprNode_traversEnsuresConstraints
2106 (exprData_getFieldNode (data)));
2108 case XPR_STRINGLITERAL:
2113 ret = constraintList_addListFree (ret,
2114 exprNode_traversEnsuresConstraints
2115 (exprData_getUopNode (data)));
2118 ret = constraintList_addListFree (ret,
2119 exprNode_traversEnsuresConstraints
2120 (exprData_getCastNode (data)));
2126 DPRINTF((message ("exprnode_traversEnsuresConstraints call for %s with "
2127 "constraintList of is returning %s",
2128 exprNode_unparse (e),
2129 constraintList_unparse(ret))));
2134 /*drl moved out of constraintResolve.c 07-02-001 */
2135 void checkArgumentList (/*@out@*/ exprNode temp, exprNodeList arglist,
2136 fileloc sequencePoint)
2139 llassert(temp != NULL );
2141 temp->requiresConstraints = constraintList_makeNew();
2142 temp->ensuresConstraints = constraintList_makeNew();
2143 temp->trueEnsuresConstraints = constraintList_makeNew();
2144 temp->falseEnsuresConstraints = constraintList_makeNew();
2146 exprNodeList_elements (arglist, el)
2148 constraintList temp2;
2150 llassert(exprNode_isDefined(el) );
2152 exprNode_exprTraverse (el, FALSE, FALSE, sequencePoint);
2153 temp2 = el->requiresConstraints;
2154 el->requiresConstraints = exprNode_traversRequiresConstraints(el);
2155 constraintList_free(temp2);
2157 temp2 = el->ensuresConstraints;
2158 el->ensuresConstraints = exprNode_traversEnsuresConstraints(el);
2159 constraintList_free(temp2);
2161 temp->requiresConstraints = constraintList_addList(temp->requiresConstraints,
2162 el->requiresConstraints);
2164 temp->ensuresConstraints = constraintList_addList(temp->ensuresConstraints,
2165 el->ensuresConstraints);
2167 end_exprNodeList_elements;
2171 /*drl moved out of constraintResolve.c 07-03-001 */
2172 constraintList exprNode_getPostConditions (exprNode fcn, exprNodeList arglist, exprNode fcnCall)
2174 constraintList postconditions;
2176 DPRINTF((message ("Got call that %s (%s) ", exprNode_unparse(fcn), exprNodeList_unparse (arglist))));
2178 temp = exprNode_getUentry (fcn);
2180 postconditions = uentry_getFcnPostconditions (temp);
2182 if (constraintList_isDefined (postconditions))
2184 postconditions = constraintList_doSRefFixConstraintParam (postconditions, arglist);
2185 postconditions = constraintList_doFixResult (postconditions, fcnCall);
2189 postconditions = constraintList_makeNew();
2192 return postconditions;
2196 comment this out for now
2197 we'll include it in a production release when its stable...
2199 void findStructs (exprNodeList arglist)
2205 message("doing findStructs: %s", exprNodeList_unparse(arglist))
2209 exprNodeList_elements(arglist, expr)
2211 ct = exprNode_getType(expr);
2213 rt = ctype_realType (ct);
2215 if (ctype_isStruct (rt))
2216 TPRINTF((message("Found structure %s", exprNode_unparse(expr))
2218 if (hasInvariants(ct))
2220 constraintList invars;
2222 invars = getInvariants(ct);
2225 TPRINTF((message ("findStructs has invariants %s ", constraintList_unparse (invars))
2228 invars = constraintList_doSRefFixStructConstraint(invars, exprNode_getSref(expr), ct);
2231 TPRINTF((message ("findStructs finded invariants to be %s ", constraintList_unparse (invars))
2235 end_exprNodeList_elements;
2240 /*drl moved out of constraintResolve.c 07-02-001 */
2241 constraintList checkCall (/*@dependent@*/ exprNode fcn, exprNodeList arglist)
2243 constraintList preconditions;
2245 DPRINTF((message ("Got call that %s (%s) ", exprNode_unparse(fcn), exprNodeList_unparse (arglist))));
2247 temp = exprNode_getUentry (fcn);
2249 preconditions = uentry_getFcnPreconditions (temp);
2251 if (constraintList_isDefined(preconditions))
2253 preconditions = constraintList_togglePost (preconditions);
2254 preconditions = constraintList_preserveCallInfo(preconditions, fcn);
2255 preconditions = constraintList_doSRefFixConstraintParam (preconditions, arglist);
2259 if (constraintList_isUndefined(preconditions))
2260 preconditions = constraintList_makeNew();
2263 if (context_getFlag (FLG_IMPLICTCONSTRAINT) )
2267 uentryList_elements (arglist, el)
2270 TPRINTF((message("setImplictfcnConstraints doing: %s", uentry_unparse(el) ) ));
2272 s = uentry_getSref(el);
2273 if (sRef_isReference (s) )
2275 TPRINTF((message ("%s is a pointer", sRef_unparse(s) ) ));
2279 TPRINTF((message ("%s is NOT a pointer", sRef_unparse(s) ) ));
2282 //chagned this from MaxSet(s) == 0 to MaxSet(s) >= 0
2283 c = constraint_makeSRefWriteSafeInt (s, 0);
2285 implicitFcnConstraints = constraintList_add(implicitFcnConstraints , c);
2287 //drl 10/23/2002 added support for out
2288 if (!uentry_isOut(el) )
2290 c = constraint_makeSRefReadSafeInt (s, 0);
2291 implicitFcnConstraints = constraintList_add(implicitFcnConstraints , c);
2299 DPRINTF ((message("Done checkCall\n")));
2300 DPRINTF ((message("Returning list %q ", constraintList_unparseDetailed(preconditions))));
2303 drl we're going to comment this out for now
2304 we'll include it if we're sure it's working
2306 findStructs(arglist);
2309 return preconditions;
2312 /*drl added this function 10.29.001
2313 takes an exprNode of the form const + const
2317 I'm a bit nervous about modifying the exprNode
2318 but this is the easy way to do this
2319 If I have time I'd like to cause the exprNode to get created correctly in the first place */
2320 void exprNode_findValue(exprNode e)
2327 llassert(exprNode_isDefined(e) );
2331 if (exprNode_hasValue(e))
2334 if (e->kind == XPR_OP)
2336 t1 = exprData_getOpA (data);
2337 t2 = exprData_getOpB (data);
2338 tok = exprData_getOpTok (data);
2340 exprNode_findValue(t1);
2341 exprNode_findValue(t2);
2343 if (!(exprNode_knownIntValue(t1) && (exprNode_knownIntValue(t2))))
2346 if (lltok_isPlus_Op (tok))
2350 v1 = exprNode_getLongValue(t1);
2351 v2 = exprNode_getLongValue(t2);
2353 if (multiVal_isDefined(e->val))
2354 multiVal_free (e->val);
2356 e->val = multiVal_makeInt (v1 + v2);
2359 if (lltok_isMinus_Op (tok))
2363 v1 = exprNode_getLongValue(t1);
2364 v2 = exprNode_getLongValue(t2);
2366 if (multiVal_isDefined(e->val))
2368 multiVal_free (e->val);
2371 e->val = multiVal_makeInt (v1 - v2);
2374 /*drl I should really do * and / at some point */