Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

filelocStack.c

Go to the documentation of this file.
00001 /*
00002 ** LCLint - annotation-assisted static program checker
00003 ** Copyright (C) 1994-2000 University of Virginia,
00004 **         Massachusetts Institute of Technology
00005 **
00006 ** This program is free software; you can redistribute it and/or modify it
00007 ** under the terms of the GNU General Public License as published by the
00008 ** Free Software Foundation; either version 2 of the License, or (at your
00009 ** option) any later version.
00010 ** 
00011 ** This program is distributed in the hope that it will be useful, but
00012 ** WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 ** General Public License for more details.
00015 ** 
00016 ** The GNU General Public License is available from http://www.gnu.org/ or
00017 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00018 ** MA 02111-1307, USA.
00019 **
00020 ** For information on lclint: lclint-request@cs.virginia.edu
00021 ** To report a bug: lclint-bug@cs.virginia.edu
00022 ** For more information: http://lclint.cs.virginia.edu
00023 */
00024 /*
00025 ** filelocStack.c (from slist_template.c)
00026 */
00027 
00028 # include "lclintMacros.nf"
00029 # include "basic.h"
00030 # include "filelocStack.h"
00031 
00032 static /*@notnull@*/ /*@only@*/ filelocStack
00033 filelocStack_newEmpty (void)
00034 {
00035   filelocStack s = (filelocStack) dmalloc (sizeof (*s));
00036   
00037   s->nelements = 0;
00038   s->free = filelocStackBASESIZE;
00039   s->elements = (fileloc *) dmalloc (sizeof (*s->elements) * filelocStackBASESIZE);
00040 
00041   return (s);
00042 }
00043 
00044 filelocStack
00045 filelocStack_new ()
00046 {
00047   return (filelocStack_newEmpty ());
00048 }
00049 
00050 static void
00051 filelocStack_grow (/*@notnull@*/ filelocStack s)
00052 {
00053   o_fileloc *oldelements = s->elements;
00054   int i;
00055   
00056   s->free += filelocStackBASESIZE; 
00057   s->elements = (fileloc *) dmalloc (sizeof (*s->elements) 
00058                                      * (s->nelements + s->free));
00059     
00060   for (i = 0; i < s->nelements; i++)
00061     {
00062       s->elements[i] = oldelements[i];
00063     }
00064   
00065   sfree (oldelements);
00066 }
00067 
00068 static void 
00069   filelocStack_push (/*@returned@*/ filelocStack s, /*@keep@*/ fileloc el)
00070   /*@modifies s@*/
00071 {
00072   llassert (filelocStack_isDefined (s));
00073 
00074   if (s->free <= 0)
00075     {
00076       filelocStack_grow (s);
00077     }
00078   
00079   s->free--;
00080   s->elements[s->nelements] = el;
00081   s->nelements++;
00082 }
00083 
00084 fileloc filelocStack_nextTop (filelocStack s)
00085 {
00086   llassert (filelocStack_isDefined (s) && s->nelements > 1);
00087 
00088   return (s->elements[s->nelements - 2]);
00089 }
00090 
00091 void filelocStack_clear (filelocStack s)
00092 {
00093   if (filelocStack_isDefined (s))
00094     {
00095       int i;
00096 
00097       for (i = 0; i < s->nelements; i++)
00098         {
00099           fileloc_free (s->elements[i]);
00100         }
00101 
00102       s->free += s->nelements;
00103       s->nelements = 0;
00104     }
00105 }
00106 
00107 /*
00108 ** Returns TRUE of el is a new file.
00109 */
00110 
00111 bool filelocStack_popPushFile (filelocStack s, fileloc el)
00112 {
00113   int i;
00114 
00115   llassert (filelocStack_isDefined (s));
00116 
00117   for (i = s->nelements - 1; i >= 0; i--)
00118     {
00119       if (fileloc_sameBaseFile (s->elements[i], el))
00120         {
00121           int j;
00122           
00123           for (j = i; j < s->nelements; j++)
00124             {
00125               fileloc_free (s->elements[j]);
00126             }
00127 
00128           s->elements[i] = el;
00129           s->nelements = i + 1;
00130           return FALSE;
00131         }
00132     }
00133 
00134   filelocStack_push (s, el);
00135   return TRUE;
00136 }
00137 
00138 /*@only@*/ cstring
00139 filelocStack_unparse (filelocStack s)
00140 {
00141    int i;
00142    cstring st = cstring_makeLiteral ("[");
00143 
00144    if (filelocStack_isDefined (s))
00145      {
00146        for (i = s->nelements - 1; i >= 0; i--)
00147          {
00148            if (i == s->nelements - 1)
00149              {
00150                st = message ("%q %q", st, fileloc_unparse (s->elements[i]));
00151              }
00152            else
00153              {
00154                st = message ("%q, %q", st, fileloc_unparse (s->elements[i]));
00155              }
00156          }
00157      }
00158    
00159    st = message ("%q ]", st);
00160    return st;
00161 }
00162 
00163 int filelocStack_includeDepth (filelocStack s)
00164 {
00165   int depth = 0;
00166   int i;
00167 
00168   if (filelocStack_isDefined (s))
00169     {
00170       /* the zeroth element doesn't count! */
00171       for (i = s->nelements - 1; i > 0; i--)
00172         {
00173           if (!fileloc_isSpecialFile (s->elements[i]))
00174             {
00175               depth++;
00176             }
00177         }
00178     }
00179 
00180   return depth;
00181 }
00182 
00183 void
00184 filelocStack_printIncludes (filelocStack s)
00185 {
00186   if (filelocStack_isDefined (s))
00187     {
00188       int i;
00189       bool prep = context_isPreprocessing ();
00190       
00191       if (prep)
00192         {
00193           /* need to do this for messages */
00194           context_clearPreprocessing ();
00195         }
00196 
00197       /* don't show last two files pushed */
00198       for (i = s->nelements - 3; i >= 0; i--)
00199         {
00200           if (i == 0 || !fileloc_isSpecialFile (s->elements[i]))
00201             {
00202               llgenindentmsg (cstring_makeLiteral ("Include site"),
00203                               s->elements[i]);
00204             }
00205         }
00206 
00207       if (prep)
00208         {
00209           context_setPreprocessing ();
00210         }
00211     }
00212 }
00213 
00214 void
00215 filelocStack_free (/*@only@*/ filelocStack s)
00216 {
00217   if (filelocStack_isDefined (s))
00218     {
00219       int i;
00220       for (i = 0; i < s->nelements; i++)
00221         {
00222           fileloc_free (s->elements[i]); 
00223         }
00224       
00225       sfree (s->elements); 
00226       sfree (s);
00227     }
00228 }
00229 
00230 
00231 
00232 
00233 
00234 

Generated at Fri Nov 3 18:57:41 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000