2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
27 ** Pre-processor hash table. Derived from gnu cpp.
30 /* Part of CPP library. (Macro hash table support.)
31 Copyright (C) 1986, 87, 89, 92-95, 1996 Free Software Foundation, Inc.
32 Written by Per Bothner, 1994.
33 Based on CCCP program by by Paul Rubin, June 1986
34 Adapted to ANSI C, Richard Stallman, Jan 1987
36 This program is free software; you can redistribute it and/or modify it
37 under the terms of the GNU General Public License as published by the
38 Free Software Foundation; either version 2, or (at your option) any
41 This program is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 GNU General Public License for more details.
46 You should have received a copy of the GNU General Public License
47 along with this program; if not, write to the Free Software
48 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50 In other words, you are welcome to use, share and improve this program.
51 You are forbidden to forbid anyone else to use, share and improve
52 what you give them. Help stamp out software-hoarding! */
54 # include "lclintMacros.nf"
61 typedef /*@only@*/ HASHNODE *o_HASHNODE;
63 static o_HASHNODE hashtab[CPP_HASHSIZE];
64 static o_HASHNODE ohashtab[CPP_HASHSIZE];
66 static void HashNode_delete (/*@null@*/ /*@only@*/ HASHNODE *);
68 /* p_prev need not be defined, but isn't defined by HashNode_copy */
70 /*@function static unsigned int hashStep (unsigned, char) modifies nothing ; @*/
71 # define hashStep(old, c) (((old) << 2) + (unsigned int) (c))
73 /*@function static unsigned int makePositive (unsigned int) modifies nothing ; @*/
74 # define makePositive(v) ((v) & 0x7fffffff) /* make number positive */
76 static /*@null@*/ HASHNODE *
77 HashNode_copy (/*@null@*/ HASHNODE *,
78 /*@dependent@*/ HASHNODE **p_hdr,
79 /*@dependent@*/ /*@null@*/ /*@special@*/ HASHNODE *p_prev)
82 void cppReader_saveHashtab ()
86 for (i = 0; i < CPP_HASHSIZE; i++)
88 ohashtab[i] = HashNode_copy (hashtab[i], &ohashtab[i], NULL);
92 void cppReader_restoreHashtab ()
96 for (i = 0; i < CPP_HASHSIZE; i++) {
97 /* HashNode_delete (hashtab[i]); */
98 hashtab[i] = HashNode_copy (ohashtab[i], &hashtab[i], NULL);
102 static void HashNode_delete (/*@only@*/ /*@null@*/ HASHNODE *node)
110 HashNode_delete (node->next);
112 if (node->type == T_MACRO)
114 DEFINITION *d = node->value.defn;
115 struct reflist *ap, *nextap;
117 for (ap = d->pattern; ap != NULL; ap = nextap)
125 sfree (d->args.argnames);
131 cstring_free (node->name);
136 /*@null@*/ HASHNODE *HashNode_copy (HASHNODE *node, HASHNODE **hdr,
137 /*@dependent@*/ HASHNODE *prev)
145 HASHNODE *res = dmalloc (sizeof (*res));
147 res->next = HashNode_copy (node->next, hdr, res);
150 res->bucket_hdr = hdr;
151 res->type = node->type;
152 res->length = node->length;
153 res->name = cstring_copy (node->name);
155 if (node->type == T_MACRO)
157 DEFINITION *d = node->value.defn;
158 DEFINITION *nd = dmalloc (sizeof (*nd));
160 res->value.defn = nd;
161 nd->nargs = d->nargs;
163 nd->length = d->length;
164 nd->predefined = d->predefined;
165 nd->expansion = d->expansion;
169 if (d->pattern != NULL)
171 struct reflist *ap, *nextap;
172 struct reflist **last = &nd->pattern;
174 for (ap = d->pattern; ap != NULL; ap = nextap)
176 struct reflist *npattern = dmalloc (sizeof (*(nd->pattern)));
180 if (ap == d->pattern)
184 } /*@=branchstate@*/ /* npattern is propagated through loop */
186 last = &(npattern->next);
187 npattern->next = NULL; /* will get filled in */
188 npattern->stringify = d->pattern->stringify;
189 npattern->raw_before = d->pattern->raw_before;
190 npattern->raw_after = d->pattern->raw_after;
191 npattern->rest_args = d->pattern->rest_args;
192 npattern->argno = d->pattern->argno;
204 llassert (d->args.argnames != NULL);
206 nd->args.argnames = mstring_copy (d->args.argnames);
211 ** This fix found by:
213 ** Date: Mon, 31 May 1999 15:10:50 +0900 (JST)
214 ** From: "N.Komazaki" <koma@focs.sei.co.jp>
217 /*! why doesn't lclint report an error for this? */
218 nd->args.argnames = mstring_createEmpty ();
223 if (node->type == T_CONST)
225 res->value.ival = node->value.ival;
227 else if (node->type == T_PCSTRING)
229 res->value.cpval = mstring_copy (node->value.cpval);
230 llassert (res->value.cpval != NULL);
234 res->value = node->value;
238 /*@-uniondef@*/ /*@-compdef@*/ /* res->prev is not defined */
240 /*@=uniondef@*/ /*@=compdef@*/
244 /* Return hash function on name. must be compatible with the one
245 computed a step at a time, elsewhere */
248 hashf (const char *name, int len, int hashsize)
254 r = hashStep (r, *name++);
257 return (int) (makePositive (r) % hashsize);
261 ** Find the most recent hash node for name name (ending with first
262 ** non-identifier char) cppReader_installed by install
264 ** If LEN is >= 0, it is the length of the name.
265 ** Otherwise, compute the length by scanning the entire name.
267 ** If HASH is >= 0, it is the precomputed hash code.
268 ** Otherwise, compute the hash code.
271 /*@null@*/ HASHNODE *cppReader_lookup (char *name, int len, int hash)
278 for (bp = name; isIdentifierChar (*bp); bp++)
288 hash = hashf (name, len, CPP_HASHSIZE);
291 bucket = hashtab[hash];
293 while (bucket != NULL)
295 if (bucket->length == len &&
296 cstring_equalLen (bucket->name, cstring_fromChars (name), len))
301 bucket = bucket->next;
307 /*@null@*/ HASHNODE *cppReader_lookupExpand (char *name, int len, int hash)
309 HASHNODE *node = cppReader_lookup (name, len, hash);
311 DPRINTF (("Lookup expand: %s", name));
315 if (node->type == T_MACRO)
317 DEFINITION *defn = (DEFINITION *) node->value.defn;
319 DPRINTF (("Check macro..."));
321 if (defn->noExpand) {
322 DPRINTF (("No expand!"));
332 * Delete a hash node. Some weirdness to free junk from macros.
333 * More such weirdness will have to be added if you define more hash
334 * types that need it.
337 /* Note that the DEFINITION of a macro is removed from the hash table
338 but its storage is not freed. This would be a storage leak
339 except that it is not reasonable to keep undefining and redefining
340 large numbers of macros many times.
341 In any case, this is necessary, because a macro can be #undef'd
342 in the middle of reading the arguments to a call to it.
343 If #undef freed the DEFINITION, that would crash. */
346 cppReader_deleteMacro (HASHNODE *hp)
348 if (hp->prev != NULL)
351 hp->prev->next = hp->next;
356 if (hp->next != NULL)
358 hp->next->prev = hp->prev;
361 /* make sure that the bucket chain header that
362 the deleted guy was on points to the right thing afterwards. */
363 if (hp == *hp->bucket_hdr) {
364 *hp->bucket_hdr = hp->next;
367 if (hp->type == T_MACRO)
369 DEFINITION *d = hp->value.defn;
370 struct reflist *ap, *nextap;
372 for (ap = d->pattern; ap != NULL; ap = nextap)
380 sfree (d->args.argnames);
384 /*@-dependenttrans@*/ /*@-exposetrans@*/ /*@-compdestroy@*/
386 /*@=dependenttrans@*/ /*@=exposetrans@*/ /*@=compdestroy@*/
389 /* Install a name in the main hash table, even if it is already there.
390 name stops with first non alphanumeric, except leading '#'.
391 caller must check against redefinition if that is desired.
392 cppReader_deleteMacro () removes things installed by install () in fifo order.
393 this is important because of the `defined' special symbol used
394 in #if, and also if pushdef/popdef directives are ever implemented.
396 If LEN is >= 0, it is the length of the name.
397 Otherwise, compute the length by scanning the entire name.
399 If HASH is >= 0, it is the precomputed hash code.
400 Otherwise, compute the hash code. */
402 HASHNODE *cppReader_install (char *name, int len, enum node_type type,
403 int ivalue, char *value, int hash)
412 while (isIdentifierChar (*p))
422 hash = hashf (name, len, CPP_HASHSIZE);
425 i = sizeof (*hp) + len + 1;
428 hp = (HASHNODE *) dmalloc (size_fromInt (i));
430 hp->bucket_hdr = &hashtab[bucket];
432 hp->next = hashtab[bucket];
435 if (hp->next != NULL)
440 hashtab[bucket] = hp;
445 if (hp->type == T_CONST)
447 hp->value.ival = ivalue;
448 llassert (value == NULL);
452 hp->value.cpval = value;
456 char *tmp = ((char *) hp) + sizeof (*hp);
460 for (i = 0; i < len; i++)
466 hp->name = cstring_fromChars (tmp);
469 /*@-mustfree@*/ /*@-uniondef@*/ /*@-compdef@*/
471 /*@=mustfree@*/ /*@=uniondef@*/ /*@=compdef@*/
474 HASHNODE *cppReader_installMacro (char *name, int len,
475 struct definition *defn, int hash)
477 return cppReader_install (name, len, T_MACRO, 0, (char *) defn, hash);
481 cppReader_hashCleanup (void)
485 for (i = CPP_HASHSIZE; --i >= 0; )
487 while (hashtab[i] != NULL)
489 cppReader_deleteMacro (hashtab[i]);