1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/*
** Splint - annotation-assisted static program checker
** Copyright (C) 1994-2003 University of Virginia,
** Massachusetts Institute of Technology
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2 of the License, or (at your
** option) any later version.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** The GNU General Public License is available from http://www.gnu.org/ or
** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
** MA 02111-1307, USA.
**
** For information on splint: info@splint.org
** To report a bug: splint-bug@splint.org
** For more information: http://www.splint.org
*/
/*
** macrocache.c
**
** rep Invariant:
** no two fileloc's may be equal
**
*/
# include "splintMacros.nf"
# include "basic.h"
# include "llmain.h"
/*@constant int MCEBASESIZE;@*/
# define MCEBASESIZE 8
/*@constant int DNE;@*/
# define DNE -1
/*
** Temporary file used for processing macros.
*/
static /*:open:*/ /*@null@*/ FILE *s_macFile = NULL;
/*
** mcDisable is set to TRUE when a macro is being processed, so
** its contents are not added to the macrocache again, creating
** a nasty infinite loop.
*/
static bool mcDisable = TRUE;
static void macrocache_grow (macrocache p_s);
static int macrocache_exists (macrocache p_s, fileloc p_fl);
static void macrocache_processMacro (macrocache p_m, int p_i);
static /*@only@*/ mce
mce_create (/*@only@*/ fileloc fl, /*@only@*/ cstring def, bool comment)
{
mce m = (mce) dmalloc (sizeof (*m));
m->fl = fl;
m->def = def; /*< had a copy here! check this carefully */
m->defined = FALSE;
m->scomment = comment;
return m;
}
static void mce_free (/*@only@*/ mce m)
{
fileloc_free (m->fl);
cstring_free (m->def);
sfree (m);
}
/*@only@*/ macrocache
macrocache_create (void)
{
macrocache s = (macrocache) dmalloc (sizeof (*s));
s->entries = 0;
s->nspace = MCEBASESIZE;
s->contents = (mce *) dmalloc (sizeof (*s->contents) * MCEBASESIZE);
mcDisable = FALSE;
return (s);
}
void
macrocache_free (macrocache s)
{
int i;
llassert (s_macFile == NULL);
for (i = 0; i < s->entries; i++)
{
mce_free (s->contents[i]);
}
sfree (s->contents);
sfree (s);
}
static void
macrocache_grow (macrocache s)
{
int i;
o_mce *oldcontents = s->contents;
s->nspace = MCEBASESIZE;
s->contents = (mce *) dmalloc (sizeof (*s->contents) * (s->entries + s->nspace));
for (i = 0; i < s->entries; i++)
{
s->contents[i] = oldcontents[i];
}
sfree (oldcontents);
}
static void
macrocache_addGenEntry (macrocache s, /*@only@*/ fileloc fl,
/*@only@*/ cstring def, bool sup)
{
int i;
if (mcDisable)
{
fileloc_free (fl);
cstring_free (def);
return;
}
if ((i = macrocache_exists (s, fl)) != DNE)
{
if (cstring_equal (def, s->contents[i]->def))
{
fileloc_free (fl);
cstring_free (def);
return;
}
else
{
/*
** macro definition contained macro that is expanded
** replace with def
**
** how do we know which is better??
*/
cstring_free (s->contents[i]->def);
s->contents[i]->def = def;
fileloc_free (fl);
return;
}
}
if (s->nspace <= 0) {
macrocache_grow (s);
}
s->nspace--;
s->contents[s->entries] = mce_create (fl, def, sup);
s->entries++;
}
void
macrocache_addEntry (macrocache s, /*@only@*/ fileloc fl, /*@only@*/ cstring def)
{
macrocache_addGenEntry (s, fl, def, FALSE);
}
void
macrocache_addComment (macrocache s, /*@only@*/ fileloc fl, /*@only@*/ cstring def)
{
DPRINTF (("Macrocache add comment: %s / %s", fileloc_unparse (fl), def));
macrocache_addGenEntry (s, fl, def, TRUE);
}
static int
macrocache_exists (macrocache s, fileloc fl)
{
int i;
for (i = 0; i < s->entries; i++)
{
if (fileloc_equal (s->contents[i]->fl, fl))
return (i);
}
return (DNE);
}
/*@only@*/ cstring
macrocache_unparse (macrocache m)
{
cstring s = cstring_undefined;
int i;
for (i = 0; i < m->entries; i++)
{
fileloc fl = m->contents[i]->fl;
cstring def = m->contents[i]->def;
bool defined = m->contents[i]->defined;
s = message ("%q%q: %s [%s]\n", s, fileloc_unparse (fl), def,
bool_unparse (defined));
}
return (s);
}
/*
** needs to call lex by hand...yuk!
**
** modifies gc fileloc!
*/
/*
** there's gotta be a better way of doing this!
*/
static void pushString (/*@only@*/ cstring s)
{
static fileId mtid = fileId_invalid;
long floc;
if (s_macFile == NULL)
{
cstring fname;
mtid = fileTable_addMacrosFile (context_fileTable ());
fname = fileTable_fileName (mtid);
s_macFile = fileTable_createMacrosFile (context_fileTable (), fname); /* , "wb+"); ? **/
if (s_macFile == NULL)
{
llcontbug (message ("Cannot open tmp file %s needed to process macro: %s",
fname, s));
cstring_free (s);
return;
}
}
llassert (s_macFile != NULL);
/* SunOS, others? don't define SEEK_CUR and SEEK_SET */
# ifndef SEEK_CUR
# define SEEK_CUR 1
# endif
check (fseek (s_macFile, 0, SEEK_CUR) == 0);
floc = ftell (s_macFile);
if (cstring_length (s) > 0) {
check (fputs (cstring_toCharsSafe (s), s_macFile) != EOF);
}
check (fputc ('\n', s_macFile) == (int) '\n');
# ifndef SEEK_SET
# define SEEK_SET 0
# endif
check (fseek (s_macFile, floc, SEEK_SET) == 0);
yyin = s_macFile;
(void) yyrestart (yyin);
cstring_free (s);
}
static void
macrocache_processMacro (macrocache m, int i)
{
fileloc fl = m->contents[i]->fl;
m->contents[i]->defined = TRUE;
if (!fileId_equal (currentFile (), fileloc_fileId (fl)))
{
g_currentloc = fileloc_update (g_currentloc, fl);
context_enterMacroFile ();
}
else
{
setLine (fileloc_lineno (fl));
}
beginLine ();
DPRINTF (("Process macro: %s", m->contents[i]->def));
if (m->contents[i]->scomment)
{
pushString (message ("%s%s%s",
cstring_fromChars (BEFORE_COMMENT_MARKER),
m->contents[i]->def,
cstring_fromChars (AFTER_COMMENT_MARKER)));
(void) yyparse ();
}
else
{
bool insup = context_inSuppressRegion ();
pushString (message ("%s %s",
cstring_makeLiteralTemp (PPMRCODE),
m->contents[i]->def));
(void) yyparse ();
if (context_inSuppressRegion () && !insup)
{
voptgenerror
(FLG_SYNTAX,
message ("Macro ends in ignore region: %s", m->contents[i]->def),
fl);
}
}
incLine ();
context_exitAllClauses ();
context_exitMacroCache ();
}
extern void macrocache_processUndefinedElements (macrocache m)
{
fileloc lastfl = fileloc_undefined;
int i;
mcDisable = TRUE;
DPRINTF (("Processing undefined elements"));
if (!context_getFlag (FLG_PARTIAL))
{
for (i = 0; i < m->entries; i++)
{
if (m->contents[i]->defined)
{
;
}
else
{
fileloc fl = m->contents[i]->fl;
if (fileloc_isDefined (lastfl) && fileloc_sameFile (fl, lastfl))
{
;
}
else
{
if (!fileloc_isLib (fl))
{
displayScan (message ("checking macros %q",
fileloc_outputFilename (fl)));
}
lastfl = fl;
cleanupMessages ();
}
macrocache_processMacro (m, i);
}
}
}
mcDisable = FALSE;
}
extern /*@observer@*/ fileloc macrocache_processFileElements (macrocache m, cstring base)
{
fileloc lastfl = fileloc_undefined;
int i;
mcDisable = TRUE;
for (i = 0; i < m->entries; i++)
{
if (m->contents[i]->defined)
{
;
}
else
{
fileloc fl = m->contents[i]->fl; /* should be dependent! */
cstring fb = fileloc_getBase (fl);
if (cstring_equal (fb, base))
{
lastfl = fl;
macrocache_processMacro (m, i);
}
}
}
mcDisable = FALSE;
return lastfl;
}
void macrocache_finalize (void)
{
if (s_macFile != NULL)
{
check (fileTable_closeFile (context_fileTable (), s_macFile));
s_macFile = NULL;
}
}
|