2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 ** replaces filenamemap.c
28 ** based (loosely) on typeTable.c
30 ** entries in the fileTable are:
32 ** name - name of the file
33 ** type - kind of file (a temp file to be deleted?)
34 ** link - derived from this file
39 * - Added conditional stuff (macros OS2 and MSDOS) to make names of temporary
40 * files under Windows or OS/2 not larger than 8+3 characters to avoid
41 * trouble with FAT file systems or Novell Netware volumes.
42 * - Added include of new header file portab.h containing OS dependent stuff.
43 * - Changed occurance of '/' as path delimiter to a macro.
44 * - Added conditional stuff (#define and #include) for IBM's compiler.
47 # include "lclintMacros.nf"
52 # if defined(__IBMC__) && defined(OS2)
54 # define getpid _getpid
59 static bool fileTable_inRange (fileTable ft, fileId fid) /*@*/
61 return (fileTable_isDefined (ft) && (fid >= 0) && (fid < ft->nentries));
64 static fileId fileTable_internAddEntry (fileTable p_ft, /*@only@*/ ftentry p_e)
66 static /*@only@*/ cstring makeTempName (cstring p_dir, cstring p_pre, cstring p_suf);
68 static /*@only@*/ cstring
69 fileType_unparse (fileType ft)
73 case FILE_NORMAL: return cstring_makeLiteral ("normal");
74 case FILE_NODELETE: return cstring_makeLiteral ("normal");
75 case FILE_LSLTEMP: return cstring_makeLiteral ("ltemp");
76 case FILE_HEADER: return cstring_makeLiteral ("header");
77 case FILE_XH: return cstring_makeLiteral ("xh");
78 case FILE_MACROS: return cstring_makeLiteral ("macros");
79 case FILE_METASTATE: return cstring_makeLiteral ("metastate");
86 fileTable_getIndex (fileTable ft, cstring s)
88 if (ft == NULL) return NOT_FOUND;
89 return (cstringTable_lookup (ft->htable, s));
93 fileTable_unparse (fileTable ft)
95 cstring s = cstring_undefined;
98 if (fileTable_isUndefined (ft))
100 return (cstring_makeLiteral ("<fileTable undefined>"));
103 for (i = 0; i < ft->nentries; i++)
105 if (fileId_isValid (ft->elements[i]->fder))
107 s = message ("%s\n[%d] %s %q %d (%s)",
109 ft->elements[i]->fname,
110 fileType_unparse (ft->elements[i]->ftype),
111 ft->elements[i]->fder,
112 ft->elements[ft->elements[i]->fder]->fname);
116 s = message ("%s\n[%d] %s %q", s, i, ft->elements[i]->fname,
117 fileType_unparse (ft->elements[i]->ftype));
124 void fileTable_printTemps (fileTable ft)
126 if (fileTable_isDefined (ft))
130 for (i = 0; i < ft->nentries; i++)
132 if (ft->elements[i]->ftemp)
134 if (fileId_isValid (ft->elements[i]->fder))
136 fprintf (stderr, " %s:1\n\t%s:1\n",
137 cstring_toCharsSafe (ft->elements[ft->elements[i]->fder]->fname),
138 cstring_toCharsSafe (ft->elements[i]->fname));
142 fprintf (stderr, "[no file]\n\t%s:1\n",
143 cstring_toCharsSafe (ft->elements[i]->fname));
151 ** loads in fileTable from fileTable_dump
154 static /*@notnull@*/ ftentry
155 ftentry_create (/*@keep@*/ cstring tn, bool temp, fileType typ, fileId der)
157 ftentry t = (ftentry) dmalloc (sizeof (*t));
159 if (cstring_isUndefined (tn))
161 llbug (cstring_makeLiteral ("Undefined filename!"));
166 t->basename = cstring_undefined;
171 /* Don't set these until the basename is needed. */
179 ftentry_free (/*@only@*/ ftentry t)
181 cstring_free (t->fname);
182 cstring_free (t->basename);
186 /*@only@*/ /*@notnull@*/ fileTable
189 fileTable ft = (fileTable) dmalloc (sizeof (*ft));
192 ft->nspace = FTBASESIZE;
193 ft->elements = (ftentry *) dmalloc (FTBASESIZE * sizeof (*ft->elements));
194 ft->htable = cstringTable_create (FTHASHSIZE);
200 fileTable_grow (fileTable ft)
205 llassert (fileTable_isDefined (ft));
207 ft->nspace = FTBASESIZE;
209 newent = (ftentry *) dmalloc ((ft->nentries + ft->nspace) * sizeof (*newent));
211 for (i = 0; i < ft->nentries; i++)
213 newent[i] = ft->elements[i];
216 sfree (ft->elements);
217 ft->elements = newent;
221 fileTable_internAddEntry (fileTable ft, /*@only@*/ ftentry e)
223 llassert (fileTable_isDefined (ft));
230 cstringTable_insert (ft->htable, e->fname, ft->nentries);
231 ft->elements[ft->nentries] = e;
234 return (ft->nentries - 1);
237 void fileTable_noDelete (fileTable ft, cstring name)
239 fileId fid = fileTable_lookup (ft, name);
241 if (fileId_isValid (fid)) {
242 llassert (fileTable_isDefined (ft));
244 ft->elements[fid]->ftype = FILE_NODELETE;
249 fileTable_addFilePrim (fileTable ft, /*@only@*/ cstring name,
250 bool temp, fileType typ, fileId der)
253 int tindex = fileTable_getIndex (ft, name);
255 llassert (ft != fileTable_undefined);
257 if (tindex != NOT_FOUND)
259 llcontbug (message ("fileTable_addFilePrim: duplicate entry: %q", name));
265 ftentry e = ftentry_create (name, temp, typ, der);
267 if (der == fileId_invalid)
269 llassert (cstring_isUndefined (e->basename));
271 e->basename = fileLib_removePathFree (fileLib_removeAnyExtension (name));
272 e->fsystem = context_isSystemDir (name);
273 e->fspecial = context_isSpecialFile (name);
277 cstring srcname = cstring_concatFree1 (fileLib_removeAnyExtension (name),
279 fileId fid = fileTable_lookup (ft, srcname);
281 cstring_free (srcname);
283 if (fileId_isValid (fid))
285 fileId derid = ft->elements[fid]->fder;
287 ft->elements[fid]->fspecial = TRUE;
289 if (fileId_isValid (derid))
291 ft->elements[derid]->fspecial = TRUE;
298 ftentry de = ft->elements[der];
300 llassert (cstring_isUndefined (e->basename));
301 e->basename = cstring_copy (de->basename);
302 e->fsystem = de->fsystem;
303 e->fspecial = de->fspecial;
306 return (fileTable_internAddEntry (ft, e));
311 fileTable_addFile (fileTable ft, cstring name)
313 /* while (*name == '.' && *(name + 1) == '/') name += 2; */
315 return (fileTable_addFilePrim (ft, cstring_copy (name),
316 FALSE, FILE_NORMAL, fileId_invalid));
320 fileTable_addFileOnly (fileTable ft, /*@only@*/ cstring name)
322 return (fileTable_addFilePrim (ft, name, FALSE, FILE_NORMAL, fileId_invalid));
326 fileTable_addHeaderFile (fileTable ft, cstring name)
330 res = fileTable_addFilePrim (ft, cstring_copy (name), FALSE,
331 FILE_HEADER, fileId_invalid);
337 fileTable_isHeader (fileTable ft, fileId fid)
339 if (fileId_isInvalid (fid))
344 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
345 return (ft->elements[fid]->ftype == FILE_HEADER);
349 fileTable_isSystemFile (fileTable ft, fileId fid)
351 if (fileId_isInvalid (fid))
356 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
357 return (ft->elements[fid]->fsystem);
361 fileTable_isXHFile (fileTable ft, fileId fid)
363 if (fileId_isInvalid (fid))
368 if (!(fileTable_isDefined (ft) && fileTable_inRange (ft, fid)))
370 llcontbug (message ("Bad file table or id: %s %d", bool_unparse (fileTable_isDefined (ft)), fid));
375 return (ft->elements[fid]->ftype == FILE_XH);
380 fileTable_isSpecialFile (fileTable ft, fileId fid)
382 if (fileId_isInvalid (fid))
387 llassert (fileTable_isDefined (ft) && fileTable_inRange (ft, fid));
388 return (ft->elements[fid]->fspecial);
392 fileTable_addLibraryFile (fileTable ft, cstring name)
394 return (fileTable_addFilePrim (ft, cstring_copy (name),
395 FALSE, FILE_HEADER, fileId_invalid));
399 fileTable_addXHFile (fileTable ft, cstring name)
401 return (fileTable_addFilePrim (ft, cstring_copy (name),
402 FALSE, FILE_XH, fileId_invalid));
407 fileTable_addImportFile (fileTable ft, cstring name)
409 return (fileTable_addFilePrim (ft, cstring_copy (name),
410 FALSE, FILE_HEADER, fileId_invalid));
414 fileTable_addLCLFile (fileTable ft, cstring name)
416 return (fileTable_addFilePrim (ft, cstring_copy (name),
417 FALSE, FILE_HEADER, fileId_invalid));
422 static int tmpcounter = 0;
426 fileTable_addMacrosFile (fileTable ft)
429 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("lmx"),
430 cstring_makeLiteralTemp (".llm"));
432 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_MACROS, fileId_invalid));
436 fileTable_addMetastateFile (fileTable ft, cstring name)
438 return (fileTable_addFilePrim (ft, cstring_copy (name),
439 FALSE, FILE_METASTATE, fileId_invalid));
443 fileTable_addCTempFile (fileTable ft, fileId fid)
446 makeTempName (context_tmpdir (), cstring_makeLiteralTemp ("cl"),
449 llassert (fileTable_isDefined (ft));
451 if (!fileId_isValid (ft->elements[fid]->fder))
453 if (fileTable_isXHFile (ft, fid))
455 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_XH, fid));
459 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL, fid));
464 if (fileTable_isXHFile (ft, fid))
466 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_XH,
467 ft->elements[fid]->fder));
471 return (fileTable_addFilePrim (ft, newname, TRUE, FILE_NORMAL,
472 ft->elements[fid]->fder));
479 fileTable_addltemp (fileTable ft)
481 cstring newname = makeTempName (context_tmpdir (),
482 cstring_makeLiteralTemp ("ls"),
483 cstring_makeLiteralTemp (".lsl"));
486 if (cstring_hasNonAlphaNumBar (newname))
488 char *lastpath = (char *)NULL;
494 ("Operating system generates tmp filename containing invalid charater: %s",
496 lldiagmsg (cstring_makeLiteral
497 ("Try cleaning up the tmp directory. Attempting to continue."));
501 llassert (cstring_isDefined (newname));
502 lastpath = strrchr (newname, CONNECTCHAR); /* get the directory */
503 llassert (lastpath != NULL);
506 newname = message ("%q%hlsl%d.lsl",
510 /*@noaccess cstring@*/
515 ** this is kind of yucky...need to make the result of cstring_fromChars
516 ** refer to the same storage as its argument. Of course, this loses,
517 ** since cstring is abstract. Should make it an only?
520 ret = fileTable_addFilePrim (ft, cstring_copy (newname),
521 TRUE, FILE_LSLTEMP, fileId_invalid);
522 cstring_free (newname);
528 fileTable_exists (fileTable ft, cstring s)
530 int tindex = fileTable_getIndex (ft, s);
532 if (tindex == NOT_FOUND)
539 fileTable_lookup (fileTable ft, cstring s)
541 int tindex = fileTable_getIndex (ft, s);
543 if (tindex == NOT_FOUND)
545 return fileId_invalid;
554 ** This is pretty awkward --- when we find the real path of
555 ** a .xh file, we may need to change the recorded name. [Sigh]
559 fileTable_setFilePath (fileTable ft, fileId fid, cstring path)
561 llassert (fileId_isValid (fid));
562 llassert (fileTable_isDefined (ft));
563 /* Need to put new string in hash table */
564 cstringTable_insert (ft->htable, cstring_copy (path), fid);
565 ft->elements[fid]->fname = cstring_copy (path);
569 fileTable_lookupBase (fileTable ft, cstring base)
571 int tindex = fileTable_getIndex (ft, base);
573 if (tindex == NOT_FOUND)
575 return fileId_invalid;
581 llassert (fileTable_isDefined (ft));
583 der = ft->elements[tindex]->fder;
585 if (!fileId_isValid (der))
595 fileTable_getName (fileTable ft, fileId fid)
597 if (!fileId_isValid (fid))
600 (message ("fileTable_getName: called with invalid type id: %d", fid));
601 return cstring_makeLiteralTemp ("<invalid>");
604 llassert (fileTable_isDefined (ft));
605 return (ft->elements[fid]->fname);
609 fileTable_getRootName (fileTable ft, fileId fid)
613 if (!fileId_isValid (fid))
615 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
616 return cstring_makeLiteralTemp ("<invalid>");
619 if (!fileTable_isDefined (ft))
621 return cstring_makeLiteralTemp ("<no file table>");
624 fder = ft->elements[fid]->fder;
626 if (fileId_isValid (fder))
628 return (ft->elements[fder]->fname);
632 return (ft->elements[fid]->fname);
637 fileTable_getNameBase (fileTable ft, fileId fid)
639 if (!fileId_isValid (fid))
641 llcontbug (message ("fileTable_getName: called with invalid id: %d", fid));
642 return cstring_makeLiteralTemp ("<invalid>");
645 if (!fileTable_isDefined (ft))
647 return cstring_makeLiteralTemp ("<no file table>");
650 return (ft->elements[fid]->basename);
654 fileTable_sameBase (fileTable ft, fileId f1, fileId f2)
658 if (!fileId_isValid (f1))
663 if (!fileId_isValid (f2))
668 llassert (fileTable_isDefined (ft));
675 fd1 = ft->elements[f1]->fder;
677 if (!fileId_isValid (fd1))
682 fd2 = ft->elements[f2]->fder;
685 if (!fileId_isValid (fd2))
694 fileTable_cleanup (fileTable ft)
700 llassert (fileTable_isDefined (ft));
702 msg = ((ft->nentries > 40) && context_getFlag (FLG_SHOWSCAN));
703 skip = ft->nentries / 10;
707 (void) fflush (g_msgstream);
708 fprintf (stderr, "< cleaning");
711 for (i = 0; i < ft->nentries; i++)
713 ftentry fe = ft->elements[i];
717 /* let's be real careful now, hon! */
720 ** Make sure it is really a derived file
723 if (fe->ftype == FILE_LSLTEMP || fe->ftype == FILE_NODELETE)
725 ; /* already removed */
727 else if (fileId_isValid (fe->fder))
729 /*@i423 this should use close (fd) also... */
730 (void) osd_unlink (fe->fname);
732 else if (fe->ftype == FILE_MACROS)
734 (void) osd_unlink (fe->fname);
738 llbug (message ("Temporary file is not derivative: %s "
739 "(not deleted)", fe->fname));
747 if (msg && ((i % skip) == 0))
749 (void) fflush (g_msgstream);
752 fprintf (stderr, " ");
754 fprintf (stderr, ".");
757 (void) fflush (stderr);
763 fprintf (stderr, " >\n");
768 fileTable_free (/*@only@*/ fileTable f)
772 if (f == (fileTable)NULL)
777 while ( i < f->nentries )
779 ftentry_free (f->elements[i]);
783 cstringTable_free (f->htable);
789 ** unique temp filename are constructed from <dir><pre><pid><msg>.<suf>
790 ** requires: <dir> must end in '/'
793 static void nextMsg (char *msg)
823 llassertprint (FALSE, ("nextMsg: out of unique names!!!"));
831 static /*@only@*/ cstring makeTempName (cstring dir, cstring pre, cstring suf)
834 static /*@owned@*/ char *msg = NULL;
835 static /*@only@*/ cstring pidname = NULL;
839 llassert (cstring_length (pre) <= 3);
842 ** We limit the temp name to 8 characters:
850 msg = mstring_copy ("AAA"); /* there are 26^3 temp names */
855 /*@+matchanyintegral@*/
857 /*@=matchanyintegral@*/
860 if (cstring_isUndefined (pidname))
862 pidname = message ("%d", pid % 100);
865 maxlen = (cstring_length (dir) + cstring_length (pre) + mstring_length (msg)
866 + cstring_length (pidname) + cstring_length (suf) + 2);
868 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);
871 while (osd_fileExists (smsg))
874 smsg = message ("%s%s%s%s%s", dir, pre, pidname, cstring_fromChars (msg), suf);