]> andersk Git - splint.git/blobdiff - src/ctype.c
Fixed processing of multi-dimensional arrays.
[splint.git] / src / ctype.c
index afc38f875f677a5725023be6005ed7975eeeec0f..a03d657e219e67f3668498586c2759eb9717afe5 100644 (file)
@@ -1,6 +1,6 @@
 /*
 ** Splint - annotation-assisted static program checker
-** Copyright (C) 1994-2001 University of Virginia,
+** Copyright (C) 1994-2003 University of Virginia,
 **         Massachusetts Institute of Technology
 **
 ** This program is free software; you can redistribute it and/or modify it
@@ -17,8 +17,8 @@
 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 ** MA 02111-1307, USA.
 **
-** For information on lclint: lclint-request@cs.virginia.edu
-** To report a bug: lclint-bug@cs.virginia.edu
+** For information on splint: info@splint.org
+** To report a bug: splint-bug@splint.org
 ** For more information: http://www.splint.org
 */
 /*
@@ -28,7 +28,7 @@
 ** They should probably be separated soon.
 */
 
-# include "lclintMacros.nf"
+# include "splintMacros.nf"
 # include "basic.h"
 # include "structNames.h"
 
@@ -52,12 +52,12 @@ static ctype ctype_getConjB (ctype p_c) /*@*/ ;
 
 static bool ctype_isComplex (ctype c)
 {
-  return (ctentry_isComplex (ctype_getCtentry(c)));
+  return (ctentry_isComplex (ctype_getCtentry (c)));
 }
 
 static bool ctype_isPlain (ctype c)
 {
-  return (ctentry_isPlain (ctype_getCtentry(c)));
+  return (ctentry_isPlain (ctype_getCtentry (c)));
 }
 
 static bool ctype_isBroken (ctype c)
@@ -154,9 +154,21 @@ ctype_createUser (typeId u)
 ctype
 ctype_createAbstract (typeId u)
 {
- /* requires: ctype_createAbstract (u) is never called more than once for any u. */
- /*           [ tested by cttable_addFullSafe, not really required ]            */
-  return (cttable_addFullSafe (ctentry_makeNew (CTK_PLAIN, ctbase_createAbstract (u))));
+  /* requires: ctype_createAbstract (u) is never called more than once for any u. */
+  /*           [ tested by cttable_addFullSafe, not really required ]            */
+
+  return (cttable_addFullSafe
+         (ctentry_makeNew (CTK_PLAIN, ctbase_createAbstract (u))));
+}
+
+ctype
+ctype_createNumAbstract (typeId u)
+{
+  /* requires: ctype_createAbstract (u) is never called more than once for any u. */
+  /*           [ tested by cttable_addFullSafe, not really required ]            */
+
+  return (cttable_addFullSafe 
+         (ctentry_makeNew (CTK_PLAIN, ctbase_createNumAbstract (u))));
 }
 
 int
@@ -194,7 +206,7 @@ ctype_realType (ctype c)
 bool
 ctype_isSimple (ctype c)
 {
-  return (!(ctype_isPointer (c) 
+  return (! (ctype_isPointer (c) 
            || ctype_isArray (c)
            || ctype_isFunction (c)));
 }
@@ -224,7 +236,7 @@ ctype_realishType (ctype c)
       else
        {
          ctype r = uentry_getRealType (usymtab_getTypeEntry 
-                                       (ctype_typeId (c)));
+                                        (ctype_typeId (c)));
          return (r);
        }
     }
@@ -235,7 +247,8 @@ ctype_realishType (ctype c)
 bool
 ctype_isUA (ctype c)
 {
-  return (!ctype_isUnknown (c) && ctbase_isUA (ctype_getCtbase (c)));
+  return (!ctype_isUnknown (c) 
+         && ctbase_isUA (ctype_getCtbase (c)));
 }
 
 bool
@@ -254,6 +267,16 @@ ctype_isAbstract (ctype c)
                || ctype_isAbstract (ctype_getConjB (c))))));
 }
 
+bool
+ctype_isNumAbstract (ctype c)
+{
+  return (!ctype_isUnknown (c) 
+         && ((ctype_isPlain (c) && ctbase_isNumAbstract (ctype_getCtbaseSafe (c))) ||
+             (ctype_isConj (c) &&
+              (ctype_isNumAbstract (ctype_getConjA (c)) 
+               || ctype_isNumAbstract (ctype_getConjB (c))))));
+}
+
 bool
 ctype_isImmutableAbstract (ctype t)
 {
@@ -269,6 +292,15 @@ ctype_isRealAbstract (ctype c)
            ctype_isRealAbstract (ctype_getConjB (c)))));
 }
 
+bool
+ctype_isRealNumAbstract (ctype c)
+{
+  return (ctype_isNumAbstract (ctype_realType (c)) ||
+         (ctype_isConj (c) && 
+          (ctype_isRealNumAbstract (ctype_getConjA (c)) || 
+           ctype_isRealNumAbstract (ctype_getConjB (c)))));
+}
+
 /*
 ** primitive creators
 */
@@ -315,9 +347,85 @@ ctype_makePointer (ctype c)
     }
 }
 
-ctype ctype_makeFixedArray (ctype c, long size)
+ctype ctype_makeFixedArray (ctype c, size_t size)
+{
+  ctype res;
+  res = cttable_addDerived (CTK_ARRAY, ctbase_makeFixedArray (c, size), c);
+  return res;
+}
+
+/*
+** In C, array terms appear backwards:
+**
+**        int a[5][7]
+**
+** declares an array of 5 elements, each of which is
+** an array of 7 int's.
+**
+** We represent this as,
+**
+**        array (array (int, 7), 5)
+**
+** Hence, the rightmost declaration is the innermost type.
+*/
+
+ctype ctype_makeInnerFixedArray (ctype c, size_t size)
+{
+  ctype res;
+
+  DPRINTF (("makeinnerfixed: %s / %d", ctype_unparse (c), size));
+
+  if (ctype_isFixedArray (c))
+    {
+      ctype cb = ctype_baseArrayPtr (c);
+      size_t osize = ctype_getArraySize (c);
+      
+      res = ctype_makeFixedArray (ctype_makeInnerFixedArray (cb, size), osize);
+      DPRINTF (("res 1: %s", ctype_unparse (res)));
+    }
+  else if (ctype_isArray (c))
+    {
+      ctype cb = ctype_baseArrayPtr (c);
+
+      res = ctype_makeArray (ctype_makeInnerFixedArray (cb, size));
+      DPRINTF (("res 2: %s", ctype_unparse (res)));
+    }
+  else
+    {
+      res = ctype_makeFixedArray (c, size);
+      DPRINTF (("res 3: %s", ctype_unparse (res)));
+    }
+
+  DPRINTF (("Make inner fixed array: %s / base: %s", 
+           ctype_unparse (res), ctype_unparse (ctype_baseArrayPtr (res))));
+  return res;
+}
+
+ctype ctype_makeInnerArray (ctype c)
 {
-  return (cttable_addDerived (CTK_ARRAY, ctbase_makeFixedArray (c, size), c));
+  ctype res;
+
+  DPRINTF (("Make inner array: %s", ctype_unparse (c)));
+
+  if (ctype_isFixedArray (c))
+    {
+      ctype cb = ctype_baseArrayPtr (c);
+      size_t osize = ctype_getArraySize (c);
+      
+      res = ctype_makeFixedArray (ctype_makeInnerArray (cb), osize);
+    }
+  else if (ctype_isArray (c)) 
+    {
+      ctype cb = ctype_baseArrayPtr (c);
+      res = ctype_makeArray (ctype_makeInnerArray (cb));
+    }
+  else
+    {
+      res = ctype_makeArray (c);
+    }
+
+  DPRINTF (("Make inner array: %s", ctype_unparse (res)));
+  return res;
 }
 
 ctype
@@ -326,6 +434,8 @@ ctype_makeArray (ctype c)
   ctentry cte = ctype_getCtentry (c);
   ctype clp = ctentry_getArray (cte);
 
+  DPRINTF (("Make array: %s", ctype_unparse (c)));
+
   if /*@+enumint@*/ (clp == CTK_DNE) /*@=enumint@*/
     {
       ctype cnew = cttable_addDerived (CTK_ARRAY, ctbase_makeArray (c), c);
@@ -333,7 +443,9 @@ ctype_makeArray (ctype c)
       return (cnew);
     }
   else
-    return clp;
+    {
+      return clp;
+    }
 }
 
 /*
@@ -374,7 +486,8 @@ ctype_baseArrayPtr (ctype c)
 
       if (ctype_isBroken (clp))
        {
-         llbuglit ("ctype_baseArrayPtr: bogus ctype");
+         llcontbug (message ("ctype_baseArrayPtr: bogus ctype getting base of: %s", ctype_unparse (c)));
+         return ctype_unknown;
        }
 
       return clp;
@@ -626,6 +739,20 @@ ctype ctype_expectFunction (ctype c)
   return (cttable_addComplex (ctbase_expectFunction (c)));
 }
 
+ctype ctype_dontExpectFunction (ctype c)
+{
+  ctbase ctb = ctype_getCtbase (c);
+
+  /* what about this?
+  if (!ctype_isAP (c))
+    {
+      c = ctype_makePointer (c);
+    }
+  */
+
+  return (ctbase_getExpectFunction (ctb));
+}
+
 /*
 ** makeRealFunction: function returning base
 */
@@ -988,11 +1115,11 @@ ctype_isDirectInt (ctype c)
 static bool
   ctype_isForcePred (ctype * c, bool (pred) (ctype))
 {
-  if (ctype_isConj (*c))
+  /*drl bee: pbr */  if (ctype_isConj (*c))
     {
       ctype cbr = ctype_getConjA (*c);
 
-      if ((*pred) (cbr))
+       if ((*pred) (cbr))
        {
          if ((*pred) (ctype_getConjB (*c)))
            {
@@ -1069,7 +1196,11 @@ ctype_makeConjAux (ctype c1, ctype c2, bool isExplicit)
 ctype
 ctype_makeExplicitConj (ctype c1, ctype c2)
 {
-  if (ctype_isFunction (c1) && !ctype_isFunction (c2))
+  if (ctype_isAnytype (c1) || ctype_isAnytype (c2))
+    {
+      return ctype_makeAnytype ();
+    }
+  else if (ctype_isFunction (c1) && !ctype_isFunction (c2))
     {
       ctype ret = ctype_makeExplicitConj (ctype_getReturnType (c1), c2);
 
@@ -1098,6 +1229,27 @@ static ctype ivb = ctype_unknown;  /* int | void * | bool */
 static ctype ivbf = ctype_unknown; /* int | void * | bool | float */
 static ctype cuc = ctype_unknown;  /* char | unsigned char */
 
+static ctype cany = ctype_unknown;
+
+ctype 
+ctype_makeAnytype ()
+{
+  if (cany == ctype_unknown)
+    {
+      cany = ctype_makeConj (ctype_unknown, ctype_dne);
+      llassert (ctype_isAnytype (cany));
+    }
+
+  DPRINTF (("make anytype: %s", ctype_unparse (cany)));
+  return cany;
+}
+
+bool 
+ctype_isAnytype (ctype c)
+{
+  return (c == cany);
+} 
+
 static void
 ctype_recordConj (ctype c)
 {
@@ -1218,7 +1370,15 @@ ctype_makeConj (ctype c1, ctype c2)
 
   DPRINTF (("Make conj: %s / %s", ctype_unparse (c1), ctype_unparse (c2)));
 
-  if (ctype_isUnknown (c1)) 
+  if (ctype_isAnytype (c1))
+    {
+      return c1;
+    }
+  else if (ctype_isAnytype (c2))
+    {
+      return c2;
+    }
+  else if (ctype_isUnknown (c1)) 
     {
       return c2;
     }
@@ -1310,7 +1470,7 @@ ctype_makeConj (ctype c1, ctype c2)
                  if (iv == ctype_unknown)
                    {
                      iv = cttable_addComplex
-                       (ctbase_makeConj (ctype_int, 
+                        (ctbase_makeConj (ctype_int, 
                                          ctype_voidPointer,
                                          FALSE));
                    }
@@ -1409,7 +1569,6 @@ ctype_makeConj (ctype c1, ctype c2)
        {
          ;
        }
-
       
       return (cttable_addComplex (ctbase_makeConj (c1, c2, FALSE)));
     }
@@ -1700,14 +1859,19 @@ ctype_isArray (ctype c)
 
 bool ctype_isIncompleteArray (ctype c)
 {
-  return (ctype_isArray (c) && !ctype_isFixedArray (c));
-}
-
-bool ctype_isFixedArray (ctype c)
-{
-  if (ctype_isElips (c)) return FALSE;
+  if (ctype_isArray (c)) 
+    {
+      if (ctype_isFixedArray (c)) 
+       {
+         return ctype_isIncompleteArray (ctype_baseArrayPtr (c));
+       }
+      else 
+       {
+         return TRUE;
+       }
+    }
 
-  return (ctbase_isFixedArray (ctype_getCtbaseSafe (c)));
+  return FALSE;
 }
 
 bool
@@ -1748,6 +1912,10 @@ ctype_unparse (ctype c)
     {
       return cstring_makeLiteralTemp ("-");
     }
+  else if (ctype_isAnytype (c))
+    {
+      return cstring_makeLiteralTemp ("<any>");
+    }
   else if (ctype_isUnknown (c))
     {
       return cstring_makeLiteralTemp ("?");
@@ -1825,8 +1993,7 @@ ctype_dump (ctype c)
   
   if (ctype_isUA (c))
     {
-      cstring tname = usymtab_getTypeEntryName 
-       (usymtab_convertId (ctype_typeId (c)));
+      cstring tname = usymtab_getTypeEntryName (usymtab_convertTypeId (ctype_typeId (c)));
       
       if (cstring_equal (tname, context_getBoolName ()))
        {
@@ -1871,6 +2038,7 @@ ctype_getBaseType (ctype c)
              case CT_ENUMLIST:
              case CT_BOOL:
              case CT_ABST:
+             case CT_NUMABST:
              case CT_FCN:
              case CT_STRUCT:
              case CT_UNION:
@@ -1899,13 +2067,14 @@ ctype_getBaseType (ctype c)
 }
 
 ctype
-ctype_adjustPointers (int np, ctype c)
+ctype_adjustPointers (pointers p, ctype c)
 {
-  
+  int np = pointers_depth (p);
+
   if (ctype_isFunction (c))
     {
       c = ctype_makeParamsFunction
-       (ctype_adjustPointers (np, ctype_getReturnType (c)),
+        (ctype_adjustPointers (p, ctype_getReturnType (c)),
         uentryList_copy (ctype_argsFunction (c)));
     }
   else
@@ -2113,6 +2282,16 @@ ctype_createForwardUnion (cstring n)
   return (ct);
 }
 
+ctype
+ctype_createForwardEnum (cstring n)
+{
+  uentry ue  = uentry_makeEnumTag (n, ctype_unknown, fileloc_undefined);
+  ctype ct = usymtab_supForwardTypeEntry (ue);
+
+  cstring_free (n);
+  return (ct);
+}
+
 ctype
 ctype_removePointers (ctype c)
 {
@@ -2155,7 +2334,10 @@ bool ctype_isRefCounted (ctype t)
 
 bool ctype_isVisiblySharable (ctype t)
 {
-  if (ctype_isUnknown (t)) return TRUE;
+  if (ctype_isUnknown (t))
+    {
+      return TRUE;
+    }
 
   if (ctype_isConj (t))
     {
@@ -2171,7 +2353,14 @@ bool ctype_isVisiblySharable (ctype t)
 
          if (rt == t)
            {
-             return TRUE;
+             if (ctype_isNumAbstract (t))
+               {
+                 return FALSE;
+               }
+             else
+               {
+                 return TRUE;
+               }
            }
          else
            {
@@ -2470,7 +2659,12 @@ ctype ctype_combine (ctype dominant, ctype modifier)
   
 ctype ctype_resolve (ctype c)
 {
-  if (ctype_isUnknown (c)) return ctype_int;
+  if (ctype_isUnknown (c) && !ctype_isAnytype (c))
+    {
+      DPRINTF (("Resolving to int: %s", ctype_unparse (c)));
+      return ctype_int;
+    }
+
   return c;
 }
 
@@ -2651,20 +2845,28 @@ ctype_getCtentry (ctype c)
   /*@=enumint@*/
 }
 
+
+bool ctype_isFixedArray (ctype c)
+{
+  if (ctype_isElips (c)) return FALSE;
+
+  return (ctbase_isFixedArray (ctype_getCtbaseSafe (c)));
+}
+
+
 /*drl 11/28/2000 */
 /* requires that the type is an fixed array */
 /* return the size of the array */
 
-long int ctype_getArraySize (ctype c)
+size_t ctype_getArraySize (ctype c)
 {
-  long int size;
-  ctentry cte = ctype_getCtentry (c);
+  size_t size;
+
   ctbase ctb;
 
   llassert (ctype_isFixedArray (c));
-  llassert ((ctentry_getKind (cte) ==  CTK_COMPLEX) || (ctentry_getKind(cte) == CTK_ARRAY));
 
-  ctb = cte->ctbase;
+  ctb = ctype_getCtbaseSafe(c);
   size = ctbase_getArraySize (ctb);
 
   DPRINTF ((message ("ctype_getArraySize: got fixed array size of %s / %d ",
@@ -2673,3 +2875,19 @@ long int ctype_getArraySize (ctype c)
   return size;
 }
 
+ctype ctype_biggerType (ctype c1, ctype c2)
+{
+  if (ctbase_isBigger (ctype_getCtbaseSafe (c2), ctype_getCtbaseSafe (c1)))
+    {
+      return c2;
+    }
+  else
+    {
+      return c1;
+    }
+}
+
+int ctype_getSize (ctype c)
+{
+  return ctbase_getSize (ctype_getCtbaseSafe (ctype_realType (c)));
+}
This page took 0.086051 seconds and 4 git commands to generate.