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 /*@access constraint, exprNode @*/ /* !!! NO! */
46 /*@access constraintExpr @*/ /* !!! NO! */
48 /*@-nullderef@*/ /* !!! DRL needs to fix this code! */
49 /*@-nullstate@*/ /* !!! DRL needs to fix this code! */
50 /*@-nullpass@*/ /* !!! DRL needs to fix this code! */
51 /*@-temptrans@*/ /* !!! DRL needs to fix this code! */
53 static bool isInc (/*@observer@*/ constraintExpr c) /*@*/
56 llassert (constraintExpr_isDefined (c) );
57 if (c->kind == binaryexpr )
59 constraintExprBinaryOpKind binOP;
60 constraintExpr t1, t2;
61 t1 = constraintExprData_binaryExprGetExpr1 (c->data);
62 t2 = constraintExprData_binaryExprGetExpr2 (c->data);
64 binOP = constraintExprData_binaryExprGetOp (c->data);
65 if (binOP == BINARYOP_PLUS)
66 if (constraintExpr_isLit (t2) && constraintExpr_getValue (t2) == 1 )
75 /* look for constraints like cexrp = cexrp + 1 */
76 static bool incVar (/*@notnull@*/ constraint c) /*@*/
83 if (! isInc (c->expr ) )
86 llassert (constraintExpr_isDefined (c->expr) );
87 llassert (c->expr->kind == binaryexpr);
89 t1 = constraintExprData_binaryExprGetExpr1 (c->expr->data);
90 if (constraintExpr_similar (c->lexpr, t1) )
95 /*@noaccess constraintExpr @*/
98 static bool increments (/*@observer@*/ constraint c,
99 /*@observer@*/ constraintExpr var)
101 llassert (constraint_isDefined (c) );
103 if (constraint_isUndefined (c) )
108 llassert (incVar (c));
109 if (constraintExpr_similar (c->lexpr, var) )
115 static bool canGetForTimes (/*@notnull@*/ exprNode forPred, /*@notnull@*/ exprNode forBody)
118 exprNode init, test, inc, t1, t2;
121 llassert (exprNode_isDefined (forPred) );
122 llassert (exprNode_isDefined (forBody) );
124 init = exprData_getTripleInit (forPred->edata);
125 test = exprData_getTripleTest (forPred->edata);
126 inc = exprData_getTripleInc (forPred->edata);
128 llassert (exprNode_isDefined (test) );
130 if (exprNode_isUndefined (test) )
135 llassert (exprNode_isDefined (inc) );
137 if (exprNode_isUndefined (inc) )
142 if (test->kind != XPR_PREOP)
145 tok = (exprData_getUopTok (test->edata));
146 if (!lltok_isMult (tok) )
151 /* should check preop too */
152 if (inc->kind != XPR_POSTOP)
157 tok = (exprData_getUopTok (inc->edata));
158 if (lltok_isInc_Op (tok) )
160 t1 = exprData_getUopNode (test->edata);
161 t2 = exprData_getUopNode (inc->edata);
162 llassert (exprNode_isDefined (t2) && exprNode_isDefined (t2) );
164 if (exprNode_isUndefined (t1) || exprNode_isUndefined (t2) )
169 if (sRef_sameName (t1->sref, t2->sref) )
177 static /*@only@*/ constraintList getLessThanConstraints (/*@observer@*/ constraintList c)
181 ret = constraintList_makeNew ();
182 constraintList_elements (c, el)
184 llassert (constraint_isDefined (el));
185 if ( constraint_isUndefined (el) )
188 if (el->ar == LT || el->ar == LTE)
191 temp = constraint_copy (el);
193 ret = constraintList_add (ret, temp);
196 end_constraintList_elements;
201 static /*@only@*/ constraintList getIncConstraints (/*@observer@*/ constraintList c)
205 ret = constraintList_makeNew ();
206 constraintList_elements (c, el)
208 llassert (constraint_isDefined (el));
213 temp = constraint_copy (el);
214 ret = constraintList_add (ret, temp);
217 end_constraintList_elements;
222 static /*@only@*/ constraintExpr getForTimes (/*@notnull@*/ exprNode forPred, /*@notnull@*/ exprNode forBody)
225 exprNode init, test, inc, t1, t2;
226 constraintList ltCon;
227 constraintList incCon;
232 init = exprData_getTripleInit (forPred->edata);
233 test = exprData_getTripleTest (forPred->edata);
234 inc = exprData_getTripleInc (forPred->edata);
236 llassert (exprNode_isDefined (test) );
237 llassert (exprNode_isDefined (inc) );
239 ltCon = getLessThanConstraints (test->trueEnsuresConstraints);
240 incCon = getIncConstraints (inc->ensuresConstraints);
242 DPRINTF (( message ("getForTimes: ltCon: %s from %s", constraintList_print (ltCon), constraintList_print (test->trueEnsuresConstraints) ) ));
244 DPRINTF (( message ("getForTimes: incCon: %s from %s", constraintList_print (incCon), constraintList_print (inc->ensuresConstraints) ) ));
246 constraintList_elements (ltCon, el)
248 constraintList_elements (incCon, el2)
250 if ( increments (el2, el->lexpr) )
252 DPRINTF (( message ("getForTimes: %s increments %s", constraint_print (el2), constraint_print (el) ) ));
253 ret = constraintExpr_copy (el->expr);
254 constraintList_free (ltCon);
255 constraintList_free (incCon);
262 DPRINTF (( message ("getForTimes: %s doesn't increment %s", constraint_print (el2), constraint_print (el) ) ));
265 end_constraintList_elements;
268 end_constraintList_elements;
270 constraintList_free (ltCon);
271 constraintList_free (incCon);
273 DPRINTF (( message ("getForTimes: %s %s resorting to ugly hack", exprNode_unparse (forPred), exprNode_unparse (forBody) ) ));
274 if (! canGetForTimes (forPred, forBody) )
280 if (test->kind != XPR_PREOP)
283 tok = (exprData_getUopTok (test->edata));
284 if (!lltok_isMult (tok) )
289 /* should check preop too */
290 if (inc->kind != XPR_POSTOP)
295 tok = (exprData_getUopTok (inc->edata));
296 if (lltok_isInc_Op (tok) )
298 t1 = exprData_getUopNode (test->edata);
299 t2 = exprData_getUopNode (inc->edata);
300 if (sRef_sameName (t1->sref, t2->sref) )
302 return (constraintExpr_makeMaxSetExpr (t1) );
309 /*@access constraintExpr @*/
311 static /*@only@*/ constraintExpr constraintExpr_searchAndAdd (/*@only@*/ constraintExpr c, /*@observer@*/ constraintExpr find, /*@observer@*/ constraintExpr add)
313 constraintExprKind kind;
316 DPRINTF (( message ("Doing constraintExpr_searchAndAdd %s %s %s ",
317 constraintExpr_unparse (c), constraintExpr_unparse (find), constraintExpr_unparse (add) ) ) );
319 if ( constraintExpr_similar (c, find) )
322 constraintExpr newExpr;
326 cPrint = constraintExpr_unparse (c);
329 newExpr = constraintExpr_makeAddExpr (c, constraintExpr_copy (add) );
331 DPRINTF ((message ("Replacing %q with %q",
332 cPrint, constraintExpr_unparse (newExpr)
344 temp = constraintExprData_unaryExprGetExpr (c->data);
345 temp = constraintExpr_searchAndAdd (constraintExpr_copy (temp), find, add);
346 c->data = constraintExprData_unaryExprSetExpr (c->data, temp);
350 temp = constraintExprData_binaryExprGetExpr1 (c->data);
351 temp = constraintExpr_searchAndAdd (constraintExpr_copy (temp), find, add);
352 c->data = constraintExprData_binaryExprSetExpr1 (c->data, temp);
354 temp = constraintExprData_binaryExprGetExpr2 (c->data);
355 temp = constraintExpr_searchAndAdd (constraintExpr_copy (temp), find, add);
356 c->data = constraintExprData_binaryExprSetExpr2 (c->data, temp);
365 /*@noaccess constraintExpr @*/
367 static constraint constraint_searchAndAdd (/*@returned@*/ constraint c, /*@observer@*/ constraintExpr find, /*@observer@*/ constraintExpr add)
370 llassert (constraint_search (c, find) );
371 DPRINTF (( message ("Doing constraint_searchAndAdd %s %s %s ",
372 constraint_print (c), constraintExpr_unparse (find), constraintExpr_unparse (add) ) ) );
374 c->lexpr = constraintExpr_searchAndAdd (c->lexpr, find, add);
375 c->expr = constraintExpr_searchAndAdd (c->expr, find, add);
377 c = constraint_simplify (c);
378 c = constraint_simplify (c);
384 static constraintList constraintList_searchAndAdd (/*@returned@*/ constraintList list,
385 /*@observer@*/ constraintExpr find, /*@observer@*/ constraintExpr add)
387 constraintList newConstraints;
390 newConstraints = constraintList_makeNew ();
392 constraintList_elements (list, el)
394 if (constraint_search (el, find) )
397 newExpr = constraint_copy (el);
399 newExpr = constraint_searchAndAdd (newExpr, find, add);
400 DPRINTF (( (message ("Adding constraint %s ", constraint_print (newExpr)) ) ));
401 newConstraints = constraintList_add (newConstraints, newExpr);
405 end_constraintList_elements;
407 ret = constraintList_addListFree (list, newConstraints);
411 static void doAdjust (/*@unused@*/ exprNode e, /*@unused@*/ exprNode forPred, /*@observer@*/ exprNode forBody, /*@observer@*/ constraintExpr iterations)
414 constraintList_elements (forBody->ensuresConstraints, el)
416 /* look for var = var + 1 */
419 DPRINTF ((message ("Found inc variable constraint : %s", constraint_print (el) ) ));
420 forBody->requiresConstraints = constraintList_searchAndAdd (forBody->requiresConstraints, el->lexpr, iterations);
423 end_constraintList_elements;
426 void exprNode_forLoopHeuristics ( exprNode e, exprNode forPred, exprNode forBody)
428 exprNode init, test, inc;
430 constraintExpr iterations;
432 init = exprData_getTripleInit (forPred->edata);
433 test = exprData_getTripleTest (forPred->edata);
434 inc = exprData_getTripleInc (forPred->edata);
436 if (exprNode_isError (test) || exprNode_isError (inc) )
439 iterations = getForTimes (forPred, forBody );
441 if (constraintExpr_isDefined (iterations) )
443 doAdjust ( e, forPred, forBody, iterations);
444 constraintExpr_free (iterations);
451 /* DPRINTF (("Can't get for time ")); */
454 /* if (exprNode_isError (init) ) */
459 /* if (init->kind == XPR_ASSIGN) */
461 /* t1 = exprData_getOpA (init->edata); */
462 /* t2 = exprData_getOpB (init->edata); */
464 /* if (! (t1->kind == XPR_VAR) ) */
470 /* if (test->kind == XPR_FETCH) */
472 /* t3 = exprData_getPairA (test->edata); */
473 /* t4 = exprData_getPairB (test->edata); */
475 /* if (sRef_sameName (t1->sref, t4->sref) ) */
477 /* DPRINTF ((message ("Found a for loop matching heuristic:%s", exprNode_unparse (forPred) ) )); */
478 /* con = constraint_makeEnsureLteMaxRead (t1, t3); */
479 /* forPred->ensuresConstraints = constraintList_add (forPred->ensuresConstraints, con); */
483 /* DPRINTF ((message ("Didn't Find a for loop matching heuristic:%s %s and %s differ", exprNode_unparse (forPred), exprNode_unparse (t1), exprNode_unparse (t3) ) )); */