]> andersk Git - splint.git/blobdiff - src/constraintResolve.c
*** empty log message ***
[splint.git] / src / constraintResolve.c
index da5727cb2fd9947b1fa91e0a16ee4c1e425d4c5b..a79c3ff4003e450e1d44f7602c554fd7057f0279 100644 (file)
 # include "cgrammar_tokens.h"
 
 # include "exprChecks.h"
-# include "aliasChecks.h"
 # include "exprNodeSList.h"
-//# include "exprData.i"
 
 
 /*@access constraint, exprNode @*/
 
 
-constraint  inequalitySubstituteUnsound  (constraint c, constraintList p);
+static constraint  inequalitySubstitute  (/*@returned@*/ constraint p_c, constraintList p_p);
 
-static constraint constraint_searchandreplace (constraint c, constraintExpr old, constraintExpr new);
-//bool rangeCheck (arithType ar1, constraintExpr expr1, arithType ar2, constraintExpr expr2);
-//bool satifies (constraint pre, constraint post);
-//bool resolve (constraint c, constraintList p);
-//constraintList reflectChangesEnsures (constraintList pre2, constraintList post1);
-//constraint constraint_simplify (constraint c);
 
-//constraintList constraintList_fixConflicts (constraintList list1, constraintList list2);
+static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
 
-//constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2);
+static constraint  inequalitySubstituteUnsound  (/*@returned@*/ constraint p_c, constraintList p_p);
 
-//constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2);
-constraint  inequalitySubstitute  (constraint c, constraintList p);
+static constraint  inequalitySubstituteStrong  (/*@returned@*/ constraint p_c, constraintList p_p);
+
+static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
+
+
+static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
+
+static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
+
+static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
 
 /*********************************************/
 
+
+
+
+/*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
+{
+  constraintList ret;
+
+  ret = constraintList_mergeEnsures (list1, list2);
+
+  constraintList_free(list1);
+  return ret;
+}
                                            
 /*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
 {
@@ -54,7 +66,7 @@ constraint  inequalitySubstitute  (constraint c, constraintList p);
                     )));
   
   ret = constraintList_fixConflicts (list1, list2);
-  ret = reflectChangesEnsures (ret, list2);
+  ret = reflectChangesEnsuresFree1 (ret, list2);
   temp = constraintList_subsumeEnsures (ret, list2);
   constraintList_free(ret);
   ret = temp;
@@ -62,7 +74,8 @@ constraint  inequalitySubstitute  (constraint c, constraintList p);
   temp = constraintList_subsumeEnsures (list2, ret);
 
   temp = constraintList_addList (temp, ret);
-
+  constraintList_free(ret);
+  
   DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
                     constraintList_print(temp) )
                     ));
@@ -73,6 +86,18 @@ constraint  inequalitySubstitute  (constraint c, constraintList p);
   //return ret;
 }
 
+
+/*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
+{
+  constraintList ret;
+
+  ret = constraintList_mergeRequires(list1, list2);
+
+  constraintList_free(list1);
+
+  return ret;
+}
+
 /*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
 {
   constraintList ret;
@@ -81,92 +106,23 @@ constraint  inequalitySubstitute  (constraint c, constraintList p);
   DPRINTF((message ("constraintList_mergeRequires: merging  %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
 
   /* get constraints in list1 not satified by list2 */
-  temp = reflectChanges (list1, list2);
+  temp = constraintList_reflectChanges(list1, list2);
   DPRINTF((message ("constraintList_mergeRequires: temp = %s", constraintList_print(temp) ) ) );
 
 /*get constraints in list2 not satified by temp*/
-  ret = reflectChanges (list2, temp);
+  ret = constraintList_reflectChanges(list2, temp);
  
   DPRINTF((message ("constraintList_mergeRequires: ret =  %s", constraintList_print(ret) ) ) );
   
-  ret = constraintList_addList (ret, temp);
+  ret = constraintList_addListFree (ret, temp);
   
   DPRINTF((message ("constraintList_mergeRequires: returning  %s", constraintList_print(ret) ) ) );
 
   return ret;
 }
 
-void checkArgumentList (/*@out@*/ exprNode temp, exprNodeList arglist, fileloc sequencePoint)
-{
-  temp->requiresConstraints = constraintList_makeNew();
-  temp->ensuresConstraints = constraintList_makeNew();
-  temp->trueEnsuresConstraints = constraintList_makeNew();
-  temp->falseEnsuresConstraints = constraintList_makeNew();
-  
-  exprNodeList_elements (arglist, el)
-    {
-      exprNode_exprTraverse (el, FALSE, FALSE, sequencePoint);
-      el->requiresConstraints = exprNode_traversRequiresConstraints(el);
-      el->ensuresConstraints  = exprNode_traversEnsuresConstraints(el);
-
-      temp->requiresConstraints = constraintList_addList(temp->requiresConstraints,
-                                                           el->requiresConstraints);
-      
-      temp->ensuresConstraints = constraintList_addList(temp->ensuresConstraints,
-                                                          el->ensuresConstraints);
-    }
-  end_exprNodeList_elements;
-  
-}
-
-constraintList checkCall (exprNode fcn, exprNodeList arglist)
-{
-  constraintList preconditions;
-  uentry temp;
-  DPRINTF ( (message ("Got call that %s ( %s) ",  exprNode_unparse(fcn),   exprNodeList_unparse (arglist ) ) ) );
-
-  temp = exprNode_getUentry (fcn);
-
-  preconditions = uentry_getFcnPreconditions (temp);
-
-  if (preconditions != constraintList_undefined)
-    {
-      preconditions= constraintList_togglePost (preconditions);   
-      preconditions = constraintList_doSRefFixConstraintParam (preconditions, arglist);
-    }
-  else
-    {
-      if (preconditions == NULL)
-       preconditions = constraintList_makeNew();
-    }
-  
-  return preconditions;
-}
-
-constraintList getPostConditions (exprNode fcn, exprNodeList arglist, exprNode fcnCall)
-{
-  constraintList postconditions;
-  uentry temp;
-  DPRINTF ( (message ("Got call that %s ( %s) ",  exprNode_unparse(fcn),   exprNodeList_unparse (arglist ) ) ) );
-
-  temp = exprNode_getUentry (fcn);
-
-  postconditions = uentry_getFcnPostconditions (temp);
-
-  if (postconditions != constraintList_undefined)
-    {
-      postconditions = constraintList_doFixResult (postconditions, fcnCall);
-      postconditions = constraintList_doSRefFixConstraintParam (postconditions, arglist);
-    }
-  else
-    {
-      postconditions = constraintList_makeNew();
-    }
-  
-  return postconditions;
-}
-
-void mergeResolve (exprNode parent, exprNode child1, exprNode child2)
+/* old name mergeResolve renamed for czech naming convention */
+void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
 {
   constraintList temp, temp2;
 
@@ -215,9 +171,9 @@ void mergeResolve (exprNode parent, exprNode child1, exprNode child2)
   parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
 
   if ( context_getFlag (FLG_ORCONSTRAINT) )
-    temp = reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
+    temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
   else
-    temp = reflectChanges (child2->requiresConstraints, child1->ensuresConstraints);
+    temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
 
   temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
   constraintList_free(parent->requiresConstraints);
@@ -253,7 +209,7 @@ void mergeResolve (exprNode parent, exprNode child1, exprNode child2)
     {
       
       DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
-      if (!resolve (el, list2) )
+      if (!constraintList_resolve (el, list2) )
        {
          constraint temp;
          temp = constraint_copy(el);
@@ -269,37 +225,65 @@ void mergeResolve (exprNode parent, exprNode child1, exprNode child2)
 }
 
 
+
+/*used to be reflectChangesFreePre  renamed for Czech naming conventino*/
+/* tries to resolve constraints in list pre2 using post1 */
+/*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
+{
+  constraintList ret;
+  
+  ret = constraintList_reflectChanges(pre2, post1);
+
+  constraintList_free (pre2);
+  
+  return ret;
+}
+
+
+
 /* tries to resolve constraints in list pre2 using post1 */
-/*@only@*/ constraintList reflectChanges (constraintList pre2, constraintList post1)
+
+static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
 {
   
   constraintList ret;
   constraint temp;
+  constraint temp2;
+
+  llassert  (! context_getFlag (FLG_ORCONSTRAINT) );
+
   ret = constraintList_makeNew();
   DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
   
   constraintList_elements (pre2, el)
     {
-      if (!resolve (el, post1) )
+      if (!constraintList_resolve (el, post1) )
        {
-         temp = substitute (el, post1);
-         if (!resolve (temp, post1) )
+         temp = constraint_substitute (el, post1);
+         if (!constraintList_resolve (temp, post1) )
            {
              // try inequality substitution
-             constraint temp2;
+             //constraint temp2;
              
              // the inequality substitution may cause us to lose information
              //so we don't want to store the result but we do it anyway
              temp2 = constraint_copy (temp);
              //                  if (context_getFlag (FLG_ORCONSTRAINT) )
-                temp2 = inequalitySubstitute (temp, post1); 
-                     if (!resolve (temp2, post1) )
-                       {
-                         temp2 = inequalitySubstituteUnsound (temp2, post1); 
-                         if (!resolve (temp2, post1) )
-                             ret = constraintList_add (ret, temp2);
-                       }
+             temp2 = inequalitySubstitute (temp2, post1); 
+             if (!constraintList_resolve (temp2, post1) )
+               {
+                 temp2 = inequalitySubstituteUnsound (temp2, post1); 
+                 if (!constraintList_resolve (temp2, post1) )
+                   ret = constraintList_add (ret, temp2);
+                 else
+                   constraint_free(temp2);
+               }
+             else
+               {
+                 constraint_free(temp2);
+               }
            }
+         constraint_free(temp);
        }
     } end_constraintList_elements;
 
@@ -307,19 +291,33 @@ void mergeResolve (exprNode parent, exprNode child1, exprNode child2)
     return ret;
 }
 
+/* tries to resolve constraints in list pre2 using post1 */
+/*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
+{
+  constraintList temp;
+  
+  if ( context_getFlag (FLG_ORCONSTRAINT) )
+    
+    temp = constraintList_reflectChangesOr (pre2, post1);
+  else
+    temp = reflectChangesNoOr(pre2, post1);
+
+  return temp;                          
+}
 
-constraint constraint_addOr (constraint orig, constraint or)
+static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
 {
   constraint c;
   c = orig;
 
-  DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(or), constraint_printOr(orig) ) ));
+  DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
   
   while (c->or != NULL)
     {
       c = c->or;
     }
-  c->or = constraint_copy(or);
+  
+  c->or = constraint_copy(orConstr);
 
   DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
   
@@ -327,75 +325,138 @@ constraint constraint_addOr (constraint orig, constraint or)
 }
 
 
-bool resolveOr (constraint c, constraintList list)
+static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
 {
   constraint temp;
 
+  int numberOr;
+
+  numberOr = 0;
   DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
   temp = c;
 
   do
     {
-      if (resolve (temp, list) )
+      if (constraintList_resolve (temp, list) )
        return TRUE;
       temp = temp->or;
+      numberOr++;
+      llassert(numberOr <= 10);
     }
   while (constraint_isDefined(temp));
 
   return FALSE;
 }
 
+/*This is a "helper" function for doResolveOr */
 
-constraint doResolve (constraint c, constraintList post1, bool * resolved)
+static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
 {
   constraint temp;
+
+  llassert(constraint_isUndefined (c->or ) );
   
- if (!resolveOr (c, post1) )
+  if (!resolveOr (c, post1) )
+    {
+      
+      temp = constraint_substitute (c, post1);
+      
+      if (!resolveOr (temp, post1) )
        {
-         temp = substitute (c, post1);
-         if (!resolveOr (temp, post1) )
-           {
-             // try inequality substitution
-             constraint temp2;
-             
+         // try inequality substitution
+         constraint temp2;
+         
              // the inequality substitution may cause us to lose information
-             //so we don't want to store the result but we do it anyway
+             //so we don't want to store the result but we do  anyway
              temp2 = constraint_copy (c);
              //                  if (context_getFlag (FLG_ORCONSTRAINT) )
-             temp2 = inequalitySubstitute (temp2, post1); 
+             temp2 = inequalitySubstitute (temp2, post1);
+             if (!resolveOr (temp2, post1) )
+               {
+                 constraint temp3;
+                 temp3 = constraint_copy(temp2);
+                 
+                 temp3 = inequalitySubstituteStrong (temp3, post1);
+                 if (!resolveOr (temp3, post1) )
+                   {
+                     temp2 = inequalitySubstituteUnsound (temp2, post1); 
                      if (!resolveOr (temp2, post1) )
                        {
-                         temp2 = inequalitySubstituteUnsound (temp2, post1); 
-                         if (!resolveOr (temp2, post1) )
-                           {
-                             if (!constraint_same (temp, temp2) )
-                               temp = constraint_addOr (temp, temp2);
-                             *resolved = FALSE;
-                             return temp;
-                           }
+                         if (!constraint_same (temp, temp2) )
+                           temp = constraint_addOr (temp, temp2);
+
+                         if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
+                           temp = constraint_addOr (temp, temp3);
+                         
+                         *resolved = FALSE;
+                         
+                         constraint_free(temp2);
+                         constraint_free(temp3);
+                         constraint_free(c);
+                         
+                         return temp;
                        }
+                     constraint_free(temp2);
+                     constraint_free(temp3);
+                   }
+                 else
+                   {
+                     constraint_free(temp2);
+                     constraint_free(temp3);
+                   }
+               }
+             else
+               {
+                 constraint_free(temp2);
+               }                 
+
            }
+         constraint_free(temp);
        }
-
+ constraint_free(c);
  *resolved = TRUE;
  return NULL;
 
-
-
 }
 
-constraint doResolveOr (constraint c, constraintList post1, /*@out@*/bool * resolved)
+static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
 {
   constraint ret;
   constraint next;
   constraint curr;
+
+  
+  DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
+
+
   
   *resolved = FALSE;
+
+
   ret = constraint_copy(c);
+
+  if (constraintList_isEmpty(post1) )
+    {
+      return ret;
+    }
+  
   next = ret->or;
   ret->or = NULL;
 
   ret = doResolve (ret, post1, resolved);
+
+  if (*resolved)
+    {
+      if (next != NULL)
+       constraint_free(next);
+      
+      /*we don't need to free ret when resolved is false because ret is null*/
+      llassert(ret == NULL);
+      
+      return NULL;
+    }
+  
   while (next != NULL)
     {
       curr = next;
@@ -403,27 +464,32 @@ constraint doResolveOr (constraint c, constraintList post1, /*@out@*/bool * reso
       curr->or = NULL;
 
       curr = doResolve (curr, post1, resolved);
+      
       if (*resolved)
-       return NULL;
-            
+       {
+         /* curr is null so we don't try to free it*/
+         llassert(curr == NULL);
+         
+         if (next != NULL)
+           constraint_free(next);
+
+         constraint_free(ret);
+         return NULL;
+       }
       ret = constraint_addOr (ret, curr);
+      constraint_free(curr);
     }
-  
   return ret;
 }
 
-
-
-
-
 /* tries to resolve constraints in list pr2 using post1 */
-/*@only@*/ constraintList reflectChangesOr (constraintList pre2, constraintList post1)
+/*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
 {
   bool resolved;
   constraintList ret;
   constraint temp;
   ret = constraintList_makeNew();
-  DPRINTF((message ("reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
+  DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
   
   constraintList_elements (pre2, el)
     {
@@ -433,26 +499,35 @@ constraint doResolveOr (constraint c, constraintList post1, /*@out@*/bool * reso
        {
          ret = constraintList_add(ret, temp);
        }
+      else
+       {
+     /* we don't need to free temp when
+       resolved is false because temp is null */
+         llassert(temp == NULL);
+       }
+      
     } end_constraintList_elements;
 
-    DPRINTF((message ("reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
+  DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
     return ret;
 }
 
-/*@only@*/ constraintList reflectChangesEnsures (/*@only@*/ constraintList pre2, constraintList post1)
+static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
 {  
   constraintList ret;
   constraint temp;
   ret = constraintList_makeNew();
   constraintList_elements (pre2, el)
     {
-      if (!resolve (el, post1) )
+      if (!constraintList_resolve (el, post1) )
        {
-         temp = substitute (el, post1);
+         temp = constraint_substitute (el, post1);
          llassert (temp != NULL);
 
-         if (!resolve (temp, post1) )
+         if (!constraintList_resolve (temp, post1) )
            ret = constraintList_add (ret, temp);
+         else
+           constraint_free(temp);  
        }
       else
        {
@@ -464,7 +539,19 @@ constraint doResolveOr (constraint c, constraintList post1, /*@out@*/bool * reso
 }
 
 
-bool constraint_conflict (constraint c1, constraint c2)
+static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
+{
+  constraintList ret;
+
+  ret = reflectChangesEnsures (pre2, post1);
+  
+  constraintList_free(pre2);
+
+  return ret;
+}
+
+
+static bool constraint_conflict (constraint c1, constraint c2)
 {
   
   if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
@@ -477,13 +564,42 @@ bool constraint_conflict (constraint c1, constraint c2)
          }
     }  
 
+  // This is a slight kludg to prevent circular constraints like
+  // strlen(str) == maxRead(s) + strlen(str);
+
+  /*@i324234*/ //clean this up
+  
+  if (c1->ar == EQ)
+    if (c1->ar == c2->ar)
+      {
+       if (constraintExpr_search (c1->lexpr, c2->expr) )
+         if (constraintExpr_isTerm(c1->lexpr) )
+           {
+             constraintTerm term;
+             
+             term = constraintExpr_getTerm(c1->lexpr);
+
+             if (constraintTerm_isExprNode(term) )
+               {
+                 DPRINTF ( (message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
+                 return TRUE;
+               }
+           }
+      }
+
+  if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
+       {
+         DPRINTF ( (message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
+         return TRUE;
+       }
+  
   DPRINTF ( (message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
 
   return FALSE; 
 
 }
 
-void constraint_fixConflict ( constraint good, /*@observer@*/ constraint conflicting) /*@modifies good@*/
+static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
 {
   if (conflicting->ar ==EQ )
     {
@@ -494,7 +610,7 @@ void constraint_fixConflict ( constraint good, /*@observer@*/ constraint conflic
 
 }
 
-bool conflict (constraint c, constraintList list)
+static bool conflict (constraint c, constraintList list)
 {
 
   constraintList_elements (list, el)
@@ -530,25 +646,8 @@ constraintList constraintList_fixConflicts (constraintList list1, constraintList
     return ret;
 }
 
-bool resolve (/*@observer@*/ constraint c, /*@observer@*/ constraintList p)
-{
-  constraintList_elements (p, el)
-    {
-      if ( satifies (c, el) )
-       {
-         DPRINTF ( (message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
-         return TRUE;
-       }
-        DPRINTF ( (message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
-    }
-  end_constraintList_elements;
-  DPRINTF ( (message ("no constraints satify %s", constraint_print(c) ) ));
-  return FALSE;
-}
-
-
 /*returns true if constraint post satifies cosntriant pre */
-bool satifies (constraint pre, constraint post)
+static bool satifies (constraint pre, constraint post)
 {
   if (constraint_isAlwaysTrue (pre)  )
     return TRUE;
@@ -557,7 +656,7 @@ bool satifies (constraint pre, constraint post)
     {
       return FALSE;
     }
-  if (post->expr == NULL)
+  if (constraintExpr_isUndefined(post->expr))
     {
       llassert(FALSE);
       return FALSE;
@@ -566,7 +665,24 @@ bool satifies (constraint pre, constraint post)
   return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
 }
 
-bool arithType_canResolve (arithType ar1, arithType ar2)
+
+bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
+{
+  constraintList_elements (p, el)
+    {
+      if ( satifies (c, el) )
+       {
+         DPRINTF ( (message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
+         return TRUE;
+       }
+        DPRINTF ( (message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
+    }
+  end_constraintList_elements;
+  DPRINTF ( (message ("no constraints satify %s", constraint_print(c) ) ));
+  return FALSE;
+}
+
+static bool arithType_canResolve (arithType ar1, arithType ar2)
 {
   switch (ar1)
     {
@@ -597,11 +713,11 @@ bool arithType_canResolve (arithType ar1, arithType ar2)
 
 /* We look for constraint which are tautologies */
 
-bool constraint_isAlwaysTrue (constraint c)
+bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
 {
   constraintExpr l, r;
-  bool /*@unused@*/ lHasConstant, rHasConstant;
-  int /*@unused@*/ lConstant, rConstant;
+  bool rHasConstant;
+  int rConstant;
   
   l = c->lexpr;
   r = c->expr;
@@ -661,6 +777,10 @@ bool constraint_isAlwaysTrue (constraint c)
     {
       DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants  %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
       DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is  %d", rConstant ) ));
+      
+      constraintExpr_free(l);
+      constraintExpr_free(r);
+      
       switch (c->ar)
        {
        case EQ:
@@ -682,6 +802,8 @@ bool constraint_isAlwaysTrue (constraint c)
     }  
       else
       {
+       constraintExpr_free(l);
+       constraintExpr_free(r);
        DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
        return FALSE;
       }
@@ -689,10 +811,10 @@ bool constraint_isAlwaysTrue (constraint c)
   BADEXIT;
 }
 
-bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
+static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
 
 {
-  TPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
+  DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
 
   if (! arithType_canResolve (ar1, ar2) )
     return FALSE;
@@ -700,92 +822,115 @@ bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType a
   switch (ar1)
  {
  case GTE:
-      if (constraintExpr_similar (expr1, expr2) )
-        return TRUE;
-      /*@fallthrough@*/
- case GT:
-   if (!  (constraintExpr_canGetValue (expr1) &&
-          constraintExpr_canGetValue (expr2) ) )
-     {
-       constraintExpr e1, e2;
-       bool p1, p2;
-       int const1, const2;
-       
-       e1 = constraintExpr_copy(e1);
-       e2 = constraintExpr_copy(e2);
-
-       e1 = constraintExpr_propagateConstants (expr1, &p1, &const1);
-
-       e2 = constraintExpr_propagateConstants (expr2, &p2, &const2);
-
-       if (p1 && p2)
-        if (const1 <= const2)
-          if (constraintExpr_similar (e1, e2) )
-            {
-              constraintExpr_free(e1);
-              constraintExpr_free(e2);
-              return TRUE;
-            }
-       
-       DPRINTF( ("Can't Get value"));
-       
-       constraintExpr_free(e1);
-       constraintExpr_free(e2);
-       return FALSE;
-     }
-   
-   if (constraintExpr_compare (expr2, expr1) >= 0)
-     return TRUE;
-  
-  return FALSE;
- case EQ:
-   if (constraintExpr_similar (expr1, expr2) )
-        return TRUE;
-   
-   return FALSE;
- case LTE:
-   if (constraintExpr_similar (expr1, expr2) )
-        return TRUE;
-   /*@fallthrough@*/
- case LT:
+       if (constraintExpr_similar (expr1, expr2) )
+         return TRUE;
+       /*@fallthrough@*/
+  case GT:
     if (!  (constraintExpr_canGetValue (expr1) &&
-          constraintExpr_canGetValue (expr2) ) )
-     {
-      constraintExpr e1, e2;
-       bool p1, p2;
-       int const1, const2;
-       
-       e1 = constraintExpr_propagateConstants (expr1, &p1, &const1);
-
-       e2 = constraintExpr_propagateConstants (expr2, &p2, &const2);
-
-       if (p1 && p2)
-        if (const1 >= const2)
-          if (constraintExpr_similar (e1, e2) )
-            return TRUE;
-       
-       DPRINTF( ("Can't Get value"));
-       return FALSE;
-     }
-   
-   if (constraintExpr_compare (expr2, expr1) <= 0)
-     return TRUE;
+              constraintExpr_canGetValue (expr2) ) )
+           {
+                 constraintExpr e1, e2;
+                 bool p1, p2;
+                 int const1, const2;
+
+                 e1 = constraintExpr_copy(expr1);
+                 e2 = constraintExpr_copy(expr2);
+
+                 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
+
+                 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
+
+                 if (p1 || p2)
+                    {
+                     if (!p1)
+                          const1 = 0;
+
+                     if (!p2)
+                          const2 = 0;
+
+                     if (const1 <= const2)
+                          if (constraintExpr_similar (e1, e2) )
+                                 {
+                                        constraintExpr_free(e1);
+                                        constraintExpr_free(e2);
+                                        return TRUE;
+                                      }
+                     }
+                 DPRINTF( ("Can't Get value"));
+
+                 constraintExpr_free(e1);
+                 constraintExpr_free(e2);
+                 return FALSE;
+               }
+
+    if (constraintExpr_compare (expr2, expr1) >= 0)
+           return TRUE;
 
    return FALSE;
-   
- default:
-     llcontbug((message("Unhandled case in switch: %s", arithType_print(ar1) ) ) );
- }
+  case EQ:
+    if (constraintExpr_similar (expr1, expr2) )
+       return TRUE;
+
+    return FALSE;
+  case LTE:
+    if (constraintExpr_similar (expr1, expr2) )
+       return TRUE;
+    /*@fallthrough@*/
+  case LT:
+     if (!  (constraintExpr_canGetValue (expr1) &&
+               constraintExpr_canGetValue (expr2) ) )
+            {
+                 constraintExpr e1, e2;
+                  bool p1, p2;
+                  int const1, const2;
+
+                  e1 = constraintExpr_copy(expr1);
+                  e2 = constraintExpr_copy(expr2);
+
+                  e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
+
+                  e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
+
+                  if (p1 || p2)
+                     {
+                      if (!p1)
+                           const1 = 0;
+
+                      if (!p2)
+                           const2 = 0;
+
+                      if (const1 >= const2)
+                           if (constraintExpr_similar (e1, e2) )
+                                  {
+                                         constraintExpr_free(e1);
+                                         constraintExpr_free(e2);
+                                         return TRUE;
+                                       }
+                      }
+                  constraintExpr_free(e1);
+                  constraintExpr_free(e2);
+
+                  DPRINTF( ("Can't Get value"));
+                  return FALSE;
+                }
+
+    if (constraintExpr_compare (expr2, expr1) <= 0)
+           return TRUE;
+
+    return FALSE;
+
+  default:
+      llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
+  }
   BADEXIT;
 }
 
-
-constraint constraint_searchandreplace (constraint c, constraintExpr old, constraintExpr new)
+static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
 {
   DPRINTF (("Doing replace for lexpr") );
-  c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, new);
+  c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
   DPRINTF (("Doing replace for expr") );
-  c->expr = constraintExpr_searchandreplace (c->expr, old, new);
+  c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
   return c;
 }
 
@@ -800,7 +945,7 @@ bool constraint_search (constraint c, constraintExpr old) /*@*/
 }
 
 //adjust file locs and stuff
-constraint constraint_adjust (constraint substitute, constraint old)
+static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
 {
   fileloc loc1, loc2, loc3;
 
@@ -828,6 +973,10 @@ constraint constraint_adjust (constraint substitute, constraint old)
        substitute = constraint_simplify(substitute);
       }
 
+  fileloc_free (loc1);
+  fileloc_free (loc2);
+  fileloc_free (loc3);
+
   return substitute;
   
 }
@@ -838,32 +987,37 @@ constraint constraint_adjust (constraint substitute, constraint old)
 
    Warning this is sound but throws out information
  */
-constraint  inequalitySubstitute  (constraint c, constraintList p)
+
+constraint  inequalitySubstitute  (/*@returned@*/ constraint c, constraintList p)
 {
   if (c->ar != GTE)
     return c;
   
   constraintList_elements (p, el)
     {
-       if ( el->ar == LT)
+      if ( (el->ar == LT )  )
         //      if (!constraint_conflict (c, el) )
           {
-            constraint temp;
+            //constraint temp;
             constraintExpr  temp2;
             
-            temp = constraint_copy(el);
+            /*@i22*/
+
+            //temp = constraint_copy(el);
             
             //      temp = constraint_adjust(temp, c);
 
             if (constraintExpr_same (el->expr, c->expr) )
               {
-                DPRINTF((message ("inequalitySubstitute Replacing %s in %s with  %s",
+                DPRINTF((message ("inequalitySubstitute Replacing %q in %q with  %q",
                                   constraintExpr_print (c->expr),
                                   constraint_print (c),
                                   constraintExpr_print (el->expr) )
                          ));
                 temp2   = constraintExpr_copy (el->lexpr);
+                constraintExpr_free(c->expr);
                 c->expr =  constraintExpr_makeIncConstraintExpr (temp2);
+
               }
             
           }
@@ -874,6 +1028,70 @@ constraint  inequalitySubstitute  (constraint c, constraintList p)
   return c;
 }
 
+
+/* drl7x 7/26/001
+
+   THis function is like inequalitySubstitute but it adds the rule
+   added the rules x >= y &&  y <= b  ===> x >= b
+    x >= y &&  y < b  ===> x >= b + 1
+
+   This is sound but sonce it throws out additional information it should only one used
+   if we're oring constraints.
+ */
+
+static constraint  inequalitySubstituteStrong  (/*@returned@*/ constraint c, constraintList p)
+{
+  DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));      
+
+  if (c->ar != GTE)
+    return c;
+  
+  DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
+                     constraint_print(c), constraintList_print(p) ) ));      
+  constraintList_elements (p, el)
+    {
+      DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));      
+
+      if ( (el->ar == LT ) ||  (el->ar == LTE )  )
+        //      if (!constraint_conflict (c, el) )
+          {
+            //constraint temp;
+            constraintExpr  temp2;
+            
+            /*@i22*/
+
+            //temp = constraint_copy(el);
+            
+            //      temp = constraint_adjust(temp, c);
+
+            if (constraintExpr_same (el->lexpr, c->expr) )
+              {
+                DPRINTF((message ("inequalitySubstitute Replacing %s in %s with  %s",
+                                  constraintExpr_print (c->expr),
+                                  constraint_print (c),
+                                  constraintExpr_print (el->expr) )
+                         ));
+                temp2   = constraintExpr_copy (el->expr);
+                constraintExpr_free(c->expr);
+                if ( (el->ar == LTE ) )
+                  {
+                    c->expr = temp2;
+                  }
+                else
+                  {
+                    c->expr =  constraintExpr_makeIncConstraintExpr (temp2);
+                  }
+              }
+            
+          }
+    }
+  end_constraintList_elements;
+
+  c = constraint_simplify(c);
+  return c;
+}
+
+
 /* This function performs substitutions based on the rule:
    for a constraint of the form expr1 >= expr2;   a < b =>
    a = b -1 for all a in expr1.  This will work in most cases.
@@ -881,7 +1099,7 @@ constraint  inequalitySubstitute  (constraint c, constraintList p)
    Like inequalitySubstitute we're throwing away some information
 */
 
-constraint  inequalitySubstituteUnsound  (constraint c, constraintList p)
+static constraint  inequalitySubstituteUnsound  (/*@returned@*/ constraint c, constraintList p)
 {
   DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
   
@@ -894,10 +1112,10 @@ constraint  inequalitySubstituteUnsound  (constraint c, constraintList p)
        if ( ( el->ar == LTE) || (el->ar == LT) )
         //      if (!constraint_conflict (c, el) )
           {
-            constraint temp;
+            // constraint temp;
             constraintExpr  temp2;
             
-            temp = constraint_copy(el);
+            //temp = constraint_copy(el);
             
             //      temp = constraint_adjust(temp, c);
             temp2   = constraintExpr_copy (el->expr);
@@ -911,6 +1129,7 @@ constraint  inequalitySubstituteUnsound  (constraint c, constraintList p)
                               constraintExpr_print (temp2) ) ));
             
             c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
+            constraintExpr_free(temp2);
           }
     }
   end_constraintList_elements;
@@ -919,7 +1138,7 @@ constraint  inequalitySubstituteUnsound  (constraint c, constraintList p)
   return c;
 }
 
-/*@only@*/ constraint substitute (constraint c, constraintList p)
+/*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
 {
   constraint ret;
 
@@ -931,6 +1150,7 @@ constraint  inequalitySubstituteUnsound  (constraint c, constraintList p)
 
           {
             constraint temp;
+            
             temp = constraint_copy(el);
             
             temp = constraint_adjust(temp, ret);
@@ -942,17 +1162,31 @@ constraint  inequalitySubstituteUnsound  (constraint c, constraintList p)
          
             ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
             DPRINTF(( message ("The new constraint is %s", constraint_print (ret) ) ));
+            constraint_free(temp);
           }
     }
   end_constraintList_elements;
+  DPRINTF(( message ("The finial new constraint is %s", constraint_print (ret) ) ));
 
   ret = constraint_simplify(ret);
   return ret;
 }
 
+
+/*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
+{
+constraintList ret;
+
+ret = constraintList_substitute (target, subList);
+
+constraintList_free(target);
+
+return ret;
+}
+
 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
 
-constraintList constraintList_substitute (constraintList target, constraintList subList)
+/*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/  constraintList subList)
 {
 
   constraintList ret;
@@ -962,31 +1196,17 @@ constraintList constraintList_substitute (constraintList target, constraintList
   constraintList_elements(target, el)
   { 
     constraint temp;
-    #warning make sure this side effect is the right things
-    el = substitute(el, subList);
-    temp = constraint_copy (el);
+    //drl possible problem : warning make sure that a side effect is not expected
+
+    temp = constraint_substitute(el, subList);
     ret = constraintList_add (ret, temp);
   }
   end_constraintList_elements;
-#warning mem leak
-  return ret;
-}
-
-constraint constraint_solveWithFlag (constraint c, bool *b)
-{
 
-  *b = FALSE;
-  if (c->lexpr->kind == binaryexpr)
-    {
-      *b = TRUE;
-      DPRINTF( (message ("Solving %s\n", constraint_print(c) ) ) );
-      c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
-      DPRINTF( (message ("Solved and got %s\n", constraint_print(c) ) ) );
-    }
-  return c;
+  return ret;
 }
 
-constraint constraint_solve (constraint c)
+static constraint constraint_solve (/*@returned@*/ constraint c)
 {
   DPRINTF( (message ("Solving %s\n", constraint_print(c) ) ) );
   c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
@@ -1030,10 +1250,20 @@ static constraint  constraint_swapLeftRight (/*@returned@*/ constraint c)
 
 constraint constraint_simplify ( /*@returned@*/ constraint c)
 {
+
+  DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
+
+  if (constraint_tooDeep(c))
+    {
+        DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
+      return c;
+
+    }
+  
   c->lexpr = constraintExpr_simplify (c->lexpr);
   c->expr  = constraintExpr_simplify (c->expr);
 
-  if (c->lexpr->kind == binaryexpr)
+  if (constraintExpr_isBinaryExpr (c->lexpr) )
     {
       c = constraint_solve (c);
       
@@ -1047,6 +1277,9 @@ constraint constraint_simplify ( /*@returned@*/ constraint c)
       /*I don't think this will be an infinate loop*/
       c = constraint_simplify(c);
     }
+
+  DPRINTF(( message("constraint_simplify returning  %q ", constraint_print(c) ) ));
+
   return c;
 }
 
This page took 0.079963 seconds and 4 git commands to generate.