2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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.
20 ** For information on splint: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
27 ** NOTE: This is not a stand-alone source file, but is included in ctype.c.
28 ** (This is necessary becuase there is no other way in C to have a
29 ** hidden scope, besides at the file level.)
35 ** type definitions and forward declarations in ctbase.i
39 ctentry_free (/*@only@*/ ctentry c)
41 ctbase_free (c->ctbase);
42 cstring_free (c->unparse);
46 static void cttable_reset (void)
50 if (cttab.entries != NULL)
54 for (i = 0; i < cttab.size; i++)
56 ctentry_free (cttab.entries[i]);
60 sfree (cttab.entries);
71 ctentry_makeNew (ctkind ctk, /*@only@*/ ctbase c)
74 return (ctentry_make (ctk, c, ctype_dne, ctype_dne, ctype_dne, cstring_undefined));
77 static /*@only@*/ ctentry
78 ctentry_make (ctkind ctk,
79 /*@keep@*/ ctbase c, ctype base,
80 ctype ptr, ctype array,
81 /*@keep@*/ cstring unparse) /*@*/
83 ctentry cte = (ctentry) dmalloc (sizeof (*cte));
89 cte->unparse = unparse;
94 ctentry_unparse (ctentry c)
97 ("%20s [%d] [%d] [%d]",
98 (cstring_isDefined (c->unparse) ? c->unparse : cstring_makeLiteral ("<no name>")),
105 ctentry_isInteresting (ctentry c)
107 return (cstring_isNonEmpty (c->unparse));
110 static /*@only@*/ cstring
111 ctentry_dump (ctentry c)
113 DPRINTF (("Dumping: %s", ctentry_unparse (c)));
115 if (c->ptr == ctype_dne
116 && c->array == ctype_dne
117 && c->base == ctype_dne)
119 return (message ("%d %q&",
120 ctkind_toInt (c->kind),
121 ctbase_dump (c->ctbase)));
123 else if (c->base == ctype_undefined
124 && c->array == ctype_dne)
126 if (c->ptr == ctype_dne)
128 return (message ("%d %q!",
129 ctkind_toInt (c->kind),
130 ctbase_dump (c->ctbase)));
134 return (message ("%d %q^%d",
135 ctkind_toInt (c->kind),
136 ctbase_dump (c->ctbase),
140 else if (c->ptr == ctype_dne
141 && c->array == ctype_dne)
143 return (message ("%d %q%d&",
144 ctkind_toInt (c->kind),
145 ctbase_dump (c->ctbase),
150 return (message ("%d %q%d %d %d",
151 ctkind_toInt (c->kind),
152 ctbase_dump (c->ctbase),
153 c->base, c->ptr, c->array));
158 static /*@only@*/ ctentry
159 ctentry_undump (/*@dependent@*/ char *s)
161 int base, ptr, array;
165 kind = ctkind_fromInt (reader_getInt (&s));
166 ct = ctbase_undump (&s);
168 if (reader_optCheckChar (&s, '&'))
174 else if (reader_optCheckChar (&s, '!'))
176 base = ctype_undefined;
180 else if (reader_optCheckChar (&s, '^'))
182 base = ctype_undefined;
183 ptr = reader_getInt (&s);
188 base = reader_getInt (&s);
190 if (reader_optCheckChar (&s, '&'))
197 ptr = reader_getInt (&s);
198 array = reader_getInt (&s);
202 /* can't unparse w/o typeTable */
203 return (ctentry_make (kind, ct, base, ptr, array, cstring_undefined));
206 static /*@observer@*/ cstring
207 ctentry_doUnparse (ctentry c) /*@modifies c@*/
209 if (cstring_isDefined (c->unparse))
215 cstring s = ctbase_unparse (c->ctbase);
217 if (!cstring_isEmpty (s) && !cstring_containsChar (s, '<'))
223 cstring_markOwned (s);
230 static /*@observer@*/ cstring
231 ctentry_doUnparseDeep (ctentry c)
233 if (cstring_isDefined (c->unparse))
239 c->unparse = ctbase_unparseDeep (c->ctbase);
245 ** cttable operations
248 static /*@only@*/ cstring
249 cttable_unparse (void)
252 cstring s = cstring_undefined;
255 for (i = 0; i < cttab.size; i++)
257 ctentry cte = cttab.entries[i];
258 if (ctentry_isInteresting (cte))
260 if (ctbase_isUA (cte->ctbase))
262 s = message ("%s%d\t%q [%d]\n", s, i, ctentry_unparse (cttab.entries[i]),
263 cte->ctbase->contents.tid);
267 s = message ("%s%d\t%q\n", s, i, ctentry_unparse (cttab.entries[i]));
271 /*@noaccess ctbase@*/
281 for (i = 0; i < cttab.size; i++)
283 ctentry cte = cttab.entries[i];
285 if (TRUE) /* ctentry_isInteresting (cte)) */
287 if (ctbase_isUA (cte->ctbase))
289 fprintf (g_msgstream, "%3d: %s [%d]\n", i,
290 cstring_toCharsSafe (ctentry_doUnparse (cttab.entries[i])),
291 cte->ctbase->contents.tid);
295 fprintf (g_msgstream, "%3d: %s\n", i,
296 cstring_toCharsSafe (ctentry_doUnparse (cttab.entries[i])));
301 /* fprintf (g_msgstream, "%3d: <no name>\n", i); */
304 /*@noaccess ctbase@*/
310 ** Output cttable for dump file
314 cttable_dump (FILE *fout)
316 bool showdots = FALSE;
317 int showdotstride = 0;
320 if (context_getFlag (FLG_SHOWSCAN) && cttab.size > 5000)
322 fprintf (g_msgstream, " >\n"); /* end dumping to */
323 fprintf (g_msgstream, "< Dumping type table (%d types) ", cttab.size);
324 showdotstride = cttab.size / 5;
329 DPRINTF (("Dumping cttable: "));
333 for (i = 0; i < cttab.size; i++)
337 s = ctentry_dump (cttab.entries[i]);
338 DPRINTF (("[%d] = %s", i, ctentry_unparse (cttab.entries[i])));
339 llassert (cstring_length (s) < MAX_DUMP_LINE_LENGTH);
340 fputline (fout, cstring_toCharsSafe (s));
343 if (showdots && (i != 0 && ((i - 1) % showdotstride == 0)))
345 (void) fflush (g_msgstream);
346 fprintf (stderr, ".");
347 (void) fflush (stderr);
353 fprintf (stderr, " >\n< Continuing dump ");
359 ** load cttable from init file
362 static void cttable_load (FILE *f)
364 /*@modifies cttab, f @*/
366 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
372 DPRINTF (("Loading cttable: "));
376 while (reader_readLine (f, s, MAX_DUMP_LINE_LENGTH) != NULL && *s == ';')
381 if (mstring_length (s) == (MAX_DUMP_LINE_LENGTH - 1))
383 llbug (message ("Library line too long: %s\n", cstring_fromChars (s)));
386 while (s != NULL && *s != ';' && *s != '\0')
390 cte = ctentry_undump (s);
391 ct = cttable_addFull (cte);
393 DPRINTF (("Type: %d: %s", ct, ctype_unparse (ct)));
395 if (ctbase_isConj (cte->ctbase)
396 && !(ctbase_isExplicitConj (cte->ctbase)))
398 ctype_recordConj (ct);
401 (void) reader_readLine (f, s, MAX_DUMP_LINE_LENGTH);
407 DPRINTF (("Done loading cttable: "));
415 ** fill up the cttable with basic types, and first order derivations.
416 ** this is done without using our constructors for efficiency reasons
422 static void cttable_init (void)
423 /*@globals cttab@*/ /*@modifies cttab@*/
427 ctbase ct = ctbase_undefined;
429 llassert (cttab.size == 0);
431 /* do for plain, pointer, arrayof */
432 for (i = CTK_PLAIN; i <= CTK_ARRAY; i++)
434 for (j = CTX_UNKNOWN; j <= CTX_LAST; j++)
440 ct = ctbase_createBool (); /* why different? */
442 else if (j == CTX_UNKNOWN)
444 ct = ctbase_createUnknown ();
448 ct = ctbase_createPrim ((cprim)j);
451 (void) cttable_addFull
452 (ctentry_make (CTK_PLAIN,
454 j + CTK_PREDEFINED, j + CTK_PREDEFINED2,
455 ctbase_unparse (ct)));
462 ct = ctbase_makePointer (j);
463 /*@switchbreak@*/ break;
465 ct = ctbase_makeArray (j);
466 /*@switchbreak@*/ break;
468 llbugexitlit ("cttable_init: base case");
471 (void) cttable_addDerived (i, ct, j);
476 /**** reserve a slot for userBool ****/
477 (void) cttable_addFull (ctentry_make (CTK_INVALID, ctbase_undefined,
478 ctype_undefined, ctype_dne, ctype_dne,
488 o_ctentry *newentries ;
490 cttab.nspace = CTK_BASESIZE;
491 newentries = (ctentry *) dmalloc (sizeof (*newentries) * (cttab.size + cttab.nspace));
493 if (newentries == NULL)
495 llfatalerror (message ("cttable_grow: out of memory. Size: %d",
499 for (i = 0; i < cttab.size; i++)
501 newentries[i] = cttab.entries[i];
505 sfree (cttab.entries);
508 cttab.entries = newentries;
513 cttable_addDerived (ctkind ctk, /*@keep@*/ ctbase cnew, ctype base)
515 if (cttab.nspace == 0)
518 cttab.entries[cttab.size] =
519 ctentry_make (ctk, cnew, base, ctype_dne, ctype_dne, cstring_undefined);
523 return (cttab.size++);
527 cttable_addComplex (/*@only@*/ ctbase cnew)
528 /*@modifies cttab; @*/
531 if (cnew->type != CT_FCN && cnew->type != CT_EXPFCN)
534 int ctstop = cttable_lastIndex () - DEFAULT_OPTLEVEL;
536 if (ctstop < LAST_PREDEFINED)
538 ctstop = LAST_PREDEFINED;
541 for (i = cttable_lastIndex (); i >= ctstop; i--) /* better to go from back... */
545 ctb = ctype_getCtbase (i);
547 /*@i32 is this optimization really worthwhile??? */
549 if (ctbase_isDefined (ctb) && ctbase_equivStrict (cnew, ctb))
551 DPRINTF (("EQUIV!! %s / %s",
552 ctbase_unparse (cnew),
553 ctbase_unparse (ctb)));
561 if (cttab.nspace == 0)
564 cttab.entries[cttab.size] = ctentry_make (CTK_COMPLEX, cnew, ctype_undefined,
565 ctype_dne, ctype_dne,
569 return (cttab.size++);
570 /*@noaccess ctbase@*/
574 cttable_addFull (ctentry cnew)
576 if (cttab.nspace == 0)
581 cttab.entries[cttab.size] = cnew;
584 return (cttab.size++);
588 cttable_addFullSafe (/*@only@*/ ctentry cnew)
591 ctbase cnewbase = cnew->ctbase;
593 llassert (ctbase_isDefined (cnewbase));
595 for (i = cttable_lastIndex (); i >= CT_FIRST; i--)
597 ctbase ctb = ctype_getCtbase (i);
599 if (ctbase_isDefined (ctb)
600 && ctbase_equiv (cnewbase, ctype_getCtbaseSafe (i)))
607 if (cttab.nspace == 0)
610 cttab.entries[cttab.size] = cnew;
614 return (cttab.size++);