]> andersk Git - splint.git/blob - src/general.c
Fixed checking for shift expressions.
[splint.git] / src / general.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
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.
10 ** 
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.
15 ** 
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.
19 **
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
23 */
24 /*
25 ** general.c
26 */
27
28 # include "lclintMacros.nf"
29 # include "basic.h"
30 # undef malloc
31 # undef realloc
32 # undef calloc
33
34 # ifdef USEDMALLOC
35 # include "dmalloc.h"
36 # endif
37
38 # include "portab.h"
39
40 /*
41 ** redefine undef'd memory ops
42 */
43
44 # ifndef USEDMALLOC
45
46 /*@-mustdefine@*/
47
48 void sfree (void *x)
49 {
50   if (x != NULL)
51     {
52       /* fprintf (stderr, "Freeing: %p\n", x); */
53
54       /*
55       if ((unsigned long) x > 0xbf000000) {
56         fprintf (stderr, "Looks bad!\n");
57       }
58       */
59       
60       free (x);
61       
62       /* fprintf (stderr, "Done.\n"); */
63     }
64 }
65 # endif
66
67 void sfreeEventually (void *x)
68 {
69   if (x != NULL)
70     {
71       ; /* should keep in a table */
72     }
73 /*@-mustfree@*/
74 } /*@=mustfree@*/
75
76 /*
77 ** all memory should be allocated from dimalloc
78 */
79
80 static long unsigned size_toLongUnsigned (size_t x)
81 {
82   long unsigned res = (long unsigned) x;
83
84   llassert ((size_t) res == x);
85   return res;
86 }
87
88 /*@out@*/ void *dimalloc (size_t size, const char *name, int line)
89 {
90   /*
91   static void *lastaddr = 0;
92   static int numallocs = 0;
93   static int numbad = 0;
94   */
95
96   /* was malloc, use calloc to initialize to zero */
97   void *ret = (void *) calloc (1, size);
98
99   /*
100   numallocs++;
101
102   if (ret < lastaddr)
103     {
104       numbad++;
105       fprintf (stderr, "Bad alloc: %d / %d\n", numbad, numallocs);
106     }
107
108   lastaddr = ret;
109   */
110
111   if (ret == NULL)
112     {
113       if (size == 0)
114         {
115           llbug (message ("Zero allocation at %q.",
116                           fileloc_unparseRaw (cstring_fromChars (name), line)));
117         }
118       else
119         {
120           /* drl
121              fix this so message doesn't run out of
122              memory*/
123           
124           llbuglit("Out of memory");
125           
126           llfatalerrorLoc
127             (message ("Out of memory.  Allocating %w bytes at %s:%d.", 
128                       size_toLongUnsigned (size),
129                       cstring_fromChars (name), line));
130           
131         }
132     }
133       
134   /*@-null@*/ /* null okay for size = 0 */
135   /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size);  */
136   return ret; 
137   /*@=null@*/
138 }
139
140 void *dicalloc (size_t num, size_t size, const char *name, int line)
141 {
142   void *ret = (void *) calloc (num, size);
143
144   if (ret == NULL)
145     {
146       llfatalerrorLoc 
147         (message ("Out of memory.  Allocating %w bytes at %s:%d.", 
148                   size_toLongUnsigned (size),
149                   cstring_fromChars (name), line));
150     }
151   
152   return ret;
153 }
154
155 void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size, 
156                  char *name, int line)
157 {
158   void *ret;
159
160   if (x == NULL)
161     {                                  
162       ret = (void *) dmalloc (size);
163     }
164   else
165     {
166       ret = (void *) realloc (x, size);
167     }
168
169   if (ret == NULL)
170     {
171       llfatalerrorLoc
172         (message ("Out of memory.  Allocating %w bytes at %s:%d.", 
173                   size_toLongUnsigned (size),
174                   cstring_fromChars (name), line));
175     }
176   
177   return ret;
178 }
179
180 /*@=mustdefine@*/
181
182 # ifndef NOLCL
183 char *FormatInt (int i)
184 {
185   char temp[255]; /* assume the integer has at most 254 digits */
186   char *outs;
187
188   sprintf (temp, "%i", i);
189   outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
190   strcpy (outs, temp);
191
192   return (outs);
193 }
194 # endif
195
196 # ifndef NOLCL
197 bool firstWord (char *s, char *w)
198 {
199   llassert (s != NULL);
200   llassert (w != NULL);
201   
202   for (; *w != '\0'; w++, s++)
203     {
204       if (*w != *s || *s == '\0')
205         return FALSE;
206     }
207   return TRUE;
208 }
209 # endif
210
211 void mstring_markFree (char *s)
212 {
213   sfreeEventually (s);
214 }
215
216 char *mstring_spaces (int n)
217 {
218   int i;
219   char *ret;
220   char *ptr;
221
222   llassert (n >= 0);
223
224   ret = (char *) dmalloc (size_fromInt (n + 1));
225   ptr = ret;
226
227   for (i = 0; i < n; i++)
228     {
229       *ptr++ = ' ';
230     }
231
232   *ptr = '\0';
233
234   return ret;
235 }
236
237 bool mstring_containsChar (const char *s, char c)
238 {
239   if (mstring_isDefined (s))
240     {
241       return (strchr (s, c) != NULL);
242     }
243   else
244     {
245       return FALSE;
246     }
247 }
248
249 bool mstring_containsString (const char *s, const char *c)
250 {
251   if (mstring_isDefined (s))
252     {
253       return (strstr (s, c) != NULL);
254     }
255   else
256     {
257       return FALSE;
258     }
259 }
260  
261 char *mstring_concat (const char *s1, const char *s2)
262 {
263   char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
264   strcpy (s, s1);
265   strcat (s, s2);
266   return s;
267 }
268
269 extern /*@only@*/ char *
270 mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
271 {
272   /* like mstring_concat but deallocates old strings */
273   char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
274   strcpy (s, s1);
275   strcat (s, s2);
276
277   sfree (s1);
278   sfree (s2);
279   return s;
280 }
281
282 extern /*@only@*/ char *
283 mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
284 {
285   char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
286   strcpy (s, s1);
287   strcat (s, s2);
288   sfree (s1);
289
290   return s;
291 }
292
293 extern /*@only@*/ char *
294 mstring_append (/*@only@*/ char *s1, char c)
295 {
296   size_t l = strlen (s1);
297   char *s;
298
299   s = (char *) dmalloc (sizeof (*s) * (l + 2));
300
301   strcpy (s, s1);
302   *(s + l) = c;
303   *(s + l + 1) = '\0';
304   sfree (s1); 
305   return s;
306 }
307
308 extern 
309 char *mstring_copy (char *s1)
310 {
311   if (s1 == NULL)
312     {
313       return NULL;
314     }
315   else
316     {
317       char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
318       strcpy (s, s1);
319       return s;
320     }
321 }
322
323 extern
324 char *mstring_safePrint (char *s)
325 {
326   if (s == NULL)
327     {
328       return ("<undefined>");
329     }
330   else
331     {
332       return s;
333     }
334 }
335
336 extern
337 char *mstring_create (int n)
338 {
339   char *s;
340
341   s = dmalloc (sizeof (*s) * (n + 1));
342   *s = '\0';
343   return s;
344 }
345
346 void
347 fputline (FILE *out, char *s)
348 {
349   if (strlen (s) > 0) 
350     {
351       check (fputs (s, out) != EOF);
352     }
353
354   check (fputc ('\n', out) == (int) '\n');
355 }
356
357 unsigned int int_toNonNegative (int x)
358 {
359   llassert (x >= 0);
360   return (unsigned) x;
361 }
362
363 int int_log (int x)
364 {
365   int ret = 1;
366
367   while (x > 10)
368     {
369       ret++;
370       x /= 10;
371     }
372
373   return ret;
374 }
375
376 /*@-czechfcns@*/
377 long unsigned int 
378 longUnsigned_fromInt (int x)
379 {
380   llassert (x >= 0);
381   
382   return (long unsigned) x;
383 }
384
385 size_t size_fromInt (int x)
386 {
387   size_t res = (size_t) x;
388
389   llassert ((int) res == x);
390   return res;
391 }
392
393 int size_toInt (size_t x)
394 {
395   int res = (int) x;
396
397   llassert ((size_t) res == x);
398   return res;
399 }
400
401 long size_toLong (size_t x)
402 {
403   long res = (long) x;
404
405   llassert ((size_t) res == x);
406   return res;
407 }
408
409 /*@=czechfcns@*/
410
411 char
412 char_fromInt (int x)
413 {
414   /*
415   ** evans 2001-09-28 - changed assertion in response to Anthony Giorgio's comment 
416   ** that the old assertions failed for EBCDIC character set.  Now we just check 
417   ** that the result is equal.
418   */
419
420   char res = (char) x;
421   llassert ((int) res == x);
422   return res;
423 }
424
425 /*@-czechfcns@*/
426 int
427 longUnsigned_toInt (long unsigned int x)
428 {
429   int res = (int) x;
430
431   llassert ((long unsigned) res == x);
432   return res;
433 }
434
435 int
436 long_toInt (long int x)
437 {
438   int res = (int) x;
439
440   /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
441   return res;
442 }
443
444 /*@+czechfcns@*/
445
446 bool mstring_equalPrefix (const char *c1, const char *c2)
447 {
448   llassert (c1 != NULL);
449   llassert (c2 != NULL);
450
451   if (strncmp(c1, c2, strlen(c2)) == 0)
452     {
453       return TRUE;
454     }
455   else
456     {
457       return FALSE;
458     }
459 }
460
461 bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
462 {
463   if (s1 == NULL)
464     {
465       return (s2 == NULL);
466     }
467   else
468     {
469       if (s2 == NULL)
470         {
471           return FALSE;
472         }
473       else
474         {
475           return (strcmp(s1, s2) == 0);
476         }
477     }
478 }
479
This page took 0.067861 seconds and 5 git commands to generate.