2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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
29 /*This file was formerly called forjunk.c C
30 renamed Oct 8, 2001 - DRL
33 /* #define DEBUGPRINT 1 */
35 # include <ctype.h> /* for isdigit */
36 # include "splintMacros.nf"
38 # include "cgrammar.h"
39 # include "cgrammar_tokens.h"
41 # include "exprChecks.h"
42 # include "exprNodeSList.h"
44 # include "exprDataQuite.i"
46 /*@access constraint, exprNode @*/ /* !!! NO! */
48 /*@access constraintExpr @*/ /* !!! NO! */
50 /*@-nullderef@*/ /* !!! DRL needs to fix this code! */
51 /*@-nullstate@*/ /* !!! DRL needs to fix this code! */
52 /*@-nullpass@*/ /* !!! DRL needs to fix this code! */
53 /*@-temptrans@*/ /* !!! DRL needs to fix this code! */
55 static bool isInc (/*@observer@*/ constraintExpr c) /*@*/
58 llassert (constraintExpr_isDefined (c) );
59 if (c->kind == binaryexpr )
61 constraintExprBinaryOpKind binOP;
62 constraintExpr t1, t2;
63 t1 = constraintExprData_binaryExprGetExpr1 (c->data);
64 t2 = constraintExprData_binaryExprGetExpr2 (c->data);
66 binOP = constraintExprData_binaryExprGetOp (c->data);
67 if (binOP == BINARYOP_PLUS)
68 if (constraintExpr_isLit (t2) && constraintExpr_getValue (t2) == 1 )
77 /* look for constraints like cexrp = cexrp + 1 */
78 static bool incVar (/*@notnull@*/ constraint c) /*@*/
85 if (! isInc (c->expr ) )
88 llassert (constraintExpr_isDefined (c->expr) );
89 llassert (c->expr->kind == binaryexpr);
91 t1 = constraintExprData_binaryExprGetExpr1 (c->expr->data);
92 if (constraintExpr_similar (c->lexpr, t1) )
97 /*@noaccess constraintExpr @*/
100 static bool increments (/*@observer@*/ constraint c,
101 /*@observer@*/ constraintExpr var)
103 llassert (constraint_isDefined (c) );
105 if (constraint_isUndefined (c) )
110 llassert (incVar (c));
111 if (constraintExpr_similar (c->lexpr, var) )
117 static bool canGetForTimes (/*@notnull@*/ exprNode forPred, /*@notnull@*/ exprNode forBody)
120 exprNode init, test, inc, t1, t2;
123 llassert (exprNode_isDefined (forPred) );
124 llassert (exprNode_isDefined (forBody) );
126 init = exprData_getTripleInit (forPred->edata);
127 test = exprData_getTripleTest (forPred->edata);
128 inc = exprData_getTripleInc (forPred->edata);
130 llassert (exprNode_isDefined (test) );
132 if (exprNode_isUndefined (test) )
137 llassert (exprNode_isDefined (inc) );
139 if (exprNode_isUndefined (inc) )
144 if (test->kind != XPR_PREOP)
147 tok = (exprData_getUopTok (test->edata));
148 if (!lltok_isMult (tok) )
153 /* should check preop too */
154 if (inc->kind != XPR_POSTOP)
159 tok = (exprData_getUopTok (inc->edata));
160 if (lltok_isInc_Op (tok) )
162 t1 = exprData_getUopNode (test->edata);
163 t2 = exprData_getUopNode (inc->edata);
164 llassert (exprNode_isDefined (t2) && exprNode_isDefined (t2) );
166 if (exprNode_isUndefined (t1) || exprNode_isUndefined (t2) )
171 if (sRef_sameName (t1->sref, t2->sref) )
179 static /*@only@*/ constraintList getLessThanConstraints (/*@observer@*/ constraintList c)
183 ret = constraintList_makeNew ();
184 constraintList_elements (c, el)
186 llassert (constraint_isDefined (el));
187 if ( constraint_isUndefined (el) )
190 if (el->ar == LT || el->ar == LTE)
193 temp = constraint_copy (el);
195 ret = constraintList_add (ret, temp);
198 end_constraintList_elements;
203 static /*@only@*/ constraintList getIncConstraints (/*@observer@*/ constraintList c)
207 ret = constraintList_makeNew ();
208 constraintList_elements (c, el)
210 llassert (constraint_isDefined (el));
215 temp = constraint_copy (el);
216 ret = constraintList_add (ret, temp);
219 end_constraintList_elements;
224 static /*@only@*/ constraintExpr getForTimes (/*@notnull@*/ exprNode forPred, /*@notnull@*/ exprNode forBody)
227 exprNode init, test, inc, t1, t2;
228 constraintList ltCon;
229 constraintList incCon;
234 init = exprData_getTripleInit (forPred->edata);
235 test = exprData_getTripleTest (forPred->edata);
236 inc = exprData_getTripleInc (forPred->edata);
238 llassert (exprNode_isDefined (test) );
239 llassert (exprNode_isDefined (inc) );
241 ltCon = getLessThanConstraints (test->trueEnsuresConstraints);
242 incCon = getIncConstraints (inc->ensuresConstraints);
244 DPRINTF (( message ("getForTimes: ltCon: %s from %s", constraintList_print (ltCon), constraintList_print (test->trueEnsuresConstraints) ) ));
246 DPRINTF (( message ("getForTimes: incCon: %s from %s", constraintList_print (incCon), constraintList_print (inc->ensuresConstraints) ) ));
248 constraintList_elements (ltCon, el)
250 constraintList_elements (incCon, el2)
252 if ( increments (el2, el->lexpr) )
254 DPRINTF (( message ("getForTimes: %s increments %s", constraint_print (el2), constraint_print (el) ) ));
255 ret = constraintExpr_copy (el->expr);
256 constraintList_free (ltCon);
257 constraintList_free (incCon);
264 DPRINTF (( message ("getForTimes: %s doesn't increment %s", constraint_print (el2), constraint_print (el) ) ));
267 end_constraintList_elements;
270 end_constraintList_elements;
272 constraintList_free (ltCon);
273 constraintList_free (incCon);
275 DPRINTF (( message ("getForTimes: %s %s resorting to ugly hack", exprNode_unparse (forPred), exprNode_unparse (forBody) ) ));
276 if (! canGetForTimes (forPred, forBody) )
282 if (test->kind != XPR_PREOP)
285 tok = (exprData_getUopTok (test->edata));
286 if (!lltok_isMult (tok) )
291 /* should check preop too */
292 if (inc->kind != XPR_POSTOP)
297 tok = (exprData_getUopTok (inc->edata));
298 if (lltok_isInc_Op (tok) )
300 t1 = exprData_getUopNode (test->edata);
301 t2 = exprData_getUopNode (inc->edata);
302 if (sRef_sameName (t1->sref, t2->sref) )
304 return (constraintExpr_makeMaxSetExpr (t1) );
311 /*@access constraintExpr @*/
313 static /*@only@*/ constraintExpr constraintExpr_searchAndAdd (/*@only@*/ constraintExpr c, /*@observer@*/ constraintExpr find, /*@observer@*/ constraintExpr add)
315 constraintExprKind kind;
318 DPRINTF (( message ("Doing constraintExpr_searchAndAdd %s %s %s ",
319 constraintExpr_unparse (c), constraintExpr_unparse (find), constraintExpr_unparse (add) ) ) );
321 if ( constraintExpr_similar (c, find) )
324 constraintExpr newExpr;
328 cPrint = constraintExpr_unparse (c);
331 newExpr = constraintExpr_makeAddExpr (c, constraintExpr_copy (add) );
333 DPRINTF ((message ("Replacing %q with %q",
334 cPrint, constraintExpr_unparse (newExpr)
346 temp = constraintExprData_unaryExprGetExpr (c->data);
347 temp = constraintExpr_searchAndAdd (constraintExpr_copy (temp), find, add);
348 c->data = constraintExprData_unaryExprSetExpr (c->data, temp);
352 temp = constraintExprData_binaryExprGetExpr1 (c->data);
353 temp = constraintExpr_searchAndAdd (constraintExpr_copy (temp), find, add);
354 c->data = constraintExprData_binaryExprSetExpr1 (c->data, temp);
356 temp = constraintExprData_binaryExprGetExpr2 (c->data);
357 temp = constraintExpr_searchAndAdd (constraintExpr_copy (temp), find, add);
358 c->data = constraintExprData_binaryExprSetExpr2 (c->data, temp);
367 /*@noaccess constraintExpr @*/
369 static constraint constraint_searchAndAdd (/*@returned@*/ constraint c, /*@observer@*/ constraintExpr find, /*@observer@*/ constraintExpr add)
372 llassert (constraint_search (c, find) );
373 DPRINTF (( message ("Doing constraint_searchAndAdd %s %s %s ",
374 constraint_print (c), constraintExpr_unparse (find), constraintExpr_unparse (add) ) ) );
376 c->lexpr = constraintExpr_searchAndAdd (c->lexpr, find, add);
377 c->expr = constraintExpr_searchAndAdd (c->expr, find, add);
379 c = constraint_simplify (c);
380 c = constraint_simplify (c);
386 static constraintList constraintList_searchAndAdd (/*@returned@*/ constraintList list,
387 /*@observer@*/ constraintExpr find, /*@observer@*/ constraintExpr add)
389 constraintList newConstraints;
392 newConstraints = constraintList_makeNew ();
394 constraintList_elements (list, el)
396 if (constraint_search (el, find) )
399 newExpr = constraint_copy (el);
401 newExpr = constraint_searchAndAdd (newExpr, find, add);
402 DPRINTF (( (message ("Adding constraint %s ", constraint_print (newExpr)) ) ));
403 newConstraints = constraintList_add (newConstraints, newExpr);
407 end_constraintList_elements;
409 ret = constraintList_addListFree (list, newConstraints);
413 static void doAdjust (/*@unused@*/ exprNode e, /*@unused@*/ exprNode forPred, /*@observer@*/ exprNode forBody, /*@observer@*/ constraintExpr iterations)
416 constraintList_elements (forBody->ensuresConstraints, el)
418 /* look for var = var + 1 */
421 DPRINTF ((message ("Found inc variable constraint : %s", constraint_print (el) ) ));
422 forBody->requiresConstraints = constraintList_searchAndAdd (forBody->requiresConstraints, el->lexpr, iterations);
425 end_constraintList_elements;
428 void exprNode_forLoopHeuristics ( exprNode e, exprNode forPred, exprNode forBody)
430 exprNode init, test, inc;
432 constraintExpr iterations;
434 init = exprData_getTripleInit (forPred->edata);
435 test = exprData_getTripleTest (forPred->edata);
436 inc = exprData_getTripleInc (forPred->edata);
438 if (exprNode_isError (test) || exprNode_isError (inc) )
441 iterations = getForTimes (forPred, forBody );
443 if (constraintExpr_isDefined (iterations) )
445 doAdjust ( e, forPred, forBody, iterations);
446 constraintExpr_free (iterations);
453 /* DPRINTF (("Can't get for time ")); */
456 /* if (exprNode_isError (init) ) */
461 /* if (init->kind == XPR_ASSIGN) */
463 /* t1 = exprData_getOpA (init->edata); */
464 /* t2 = exprData_getOpB (init->edata); */
466 /* if (! (t1->kind == XPR_VAR) ) */
472 /* if (test->kind == XPR_FETCH) */
474 /* t3 = exprData_getPairA (test->edata); */
475 /* t4 = exprData_getPairB (test->edata); */
477 /* if (sRef_sameName (t1->sref, t4->sref) ) */
479 /* DPRINTF ((message ("Found a for loop matching heuristic:%s", exprNode_unparse (forPred) ) )); */
480 /* con = constraint_makeEnsureLteMaxRead (t1, t3); */
481 /* forPred->ensuresConstraints = constraintList_add (forPred->ensuresConstraints, con); */
485 /* DPRINTF ((message ("Didn't Find a for loop matching heuristic:%s %s and %s differ", exprNode_unparse (forPred), exprNode_unparse (t1), exprNode_unparse (t3) ) )); */