]> andersk Git - splint.git/blobdiff - src/ctbase.i
Fixed processing of multi-dimensional arrays.
[splint.git] / src / ctbase.i
index 49f3e257644cdb715de398c679f36ef6d0421ed6..c256432cb7e972ab558318d7973a222ece092acb 100644 (file)
@@ -40,7 +40,7 @@ abst_typedef /*@null@*/ struct s_ctbase *ctbase;
    ((cid) == CT_ABST || (cid) == CT_USER || (cid) == CT_NUMABST)
 /*@=macrofcndecl@*/ /*@=macroparams@*/
 
-/*@private@*/ typedef struct {
+/*:private:*/ typedef struct {
   ctkind kind;
   ctbase ctbase; 
   ctype base;     /* type I point to (or element of array) */
@@ -501,11 +501,67 @@ ctbase_unparse (ctbase c)
          return (message ("%t *", c->contents.base));
        }
     case CT_FIXEDARRAY:
-      return (message ("%t [%d]", 
-                      c->contents.farray->base, 
-                      (int) c->contents.farray->size));
+      /*
+      ** C prints out array declarations backwards, if
+      ** base is an array need to print out in reverse order.
+      */
+
+      if (ctype_isArray (c->contents.farray->base)) 
+       {
+         ctype base = c->contents.farray->base;
+         cstring res = message ("[%d]", (int) c->contents.farray->size);
+
+         while (ctype_isArray (base)) 
+           {
+             if (ctype_isFixedArray (base)) 
+               {
+                 res = message ("%q[%d]", 
+                                res, (int) ctype_getArraySize (base));
+               }
+             else
+               {
+                 res = message ("%q[]", res);
+               }
+
+             base = ctype_baseArrayPtr (base);
+           }
+
+         return (message ("%t %q", base, res));
+       } 
+      else 
+       {
+         return (message ("%t [%d]", 
+                          c->contents.farray->base, 
+                          (int) c->contents.farray->size));
+       }
     case CT_ARRAY:
-      return (message ("%t []", c->contents.base));
+      if (ctype_isArray (c->contents.base)) 
+       {
+         ctype base = c->contents.base;
+         cstring res = cstring_makeLiteral ("[]");
+
+         while (ctype_isArray (base)) 
+           {
+             if (ctype_isFixedArray (base)) 
+               {
+                 res = message ("%q[%d]", 
+                                res, (int) ctype_getArraySize (base));
+               }
+             else
+               {
+                 res = message ("%q[]", res);
+               }
+
+             base = ctype_baseArrayPtr (base);
+           }
+
+         return (message ("%t %q", base, res));
+
+       }
+      else
+       {
+         return (message ("%t []", c->contents.base));
+       }
     case CT_FCN:
       return (message ("[function (%q) returns %t]",
                       uentryList_unparseParams (c->contents.fcn->params),
@@ -2627,3 +2683,54 @@ bool ctbase_isBigger (ctbase ct1, ctbase ct2)
       return FALSE;
     }
 }
+
+int ctbase_getSize (ctbase ct)
+{
+  if (ct == NULL) 
+    {
+      return 0;
+    }
+  
+  switch (ct->type) 
+    {
+    case CT_UNKNOWN:
+    case CT_BOOL:
+    case CT_PRIM:
+      {
+       cprim cp = ct->contents.prim;
+       int nbits = cprim_getExpectedBits (cp);
+       return nbits;
+      }
+    case CT_USER:
+    case CT_ABST:
+    case CT_NUMABST:
+    case CT_EXPFCN:
+      {
+       return 0;
+      }
+    case CT_PTR:
+      {
+       /* Malloc returns void *, but they are bytes.  Normal void * is pointer size. */
+       if (ctype_isVoid (ct->contents.base)) 
+         {
+           return 8;
+         }
+       else
+         {
+           return ctype_getSize (ct->contents.base);
+         }
+      }
+    case CT_FIXEDARRAY: 
+    case CT_ARRAY:
+    case CT_FCN:
+    case CT_STRUCT:
+    case CT_UNION:
+    case CT_ENUM:
+    case CT_CONJ:
+      break;
+      BADDEFAULT;
+    }
+
+  return 0;
+      
+}
This page took 0.035682 seconds and 4 git commands to generate.