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 ** routines for loading and creating library files
29 ** this is a brute-force implementation, a more efficient
30 ** representation should be designed.
34 # include "lclintMacros.nf"
51 /*@-incondefs@*/ /*@-redecl@*/
52 extern /*@open@*/ /*@dependent@*/ FILE *yyin;
53 /*@=incondefs@*/ /*@=redecl@*/
55 /*@constant int NUMLIBS; @*/
58 /*@constant int NUMPOSIXLIBS; @*/
59 # define NUMPOSIXLIBS 12
61 static ob_mstring posixlibs[NUMPOSIXLIBS] =
77 static ob_mstring stdlibs[NUMLIBS] =
98 static bool loadLCDFile (FILE * p_f, cstring p_name);
101 lcllib_isSkipHeader (cstring sname)
104 bool posixlib = FALSE;
109 llassert (cstring_isDefined (sname));
110 xname = fileLib_withoutExtension (sname, cstring_makeLiteralTemp (".h"));
113 llassert (cstring_isDefined (xname));
114 libname = strrchr (xname, CONNECTCHAR);
128 if (mstring_equal (libname, "varargs"))
130 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
134 message ("Include file <%s> is inconsistent with "
135 "ANSI library (should use <stdarg.h>)",
136 cstring_fromChars (libname)),
144 if (context_getFlag (FLG_SKIPANSIHEADERS)
145 && context_usingAnsiLibrary ())
148 for (i = 0; i < NUMLIBS; i++)
150 if (mstring_equal (libname, stdlibs[i]))
158 for (i = 0; i < NUMPOSIXLIBS; i++)
160 if (strchr (posixlibs[i], CONNECTCHAR) != NULL)
164 if ((ptr = strstr (xname, posixlibs[i])) != NULL)
166 if (ptr[strlen (posixlibs[i])] == '\0')
180 if (mstring_equal (libname, posixlibs[i]))
192 if (context_usingPosixLibrary ())
194 if (context_getFlag (FLG_SKIPPOSIXHEADERS))
202 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
206 message ("Include file <%s> matches the name of a "
207 "POSIX library, but the POSIX library is "
208 "not being used. Consider using +posixlib "
209 "or +posixstrictlib to select the POSIX "
210 "library, or -warnposix "
211 "to suppress this message.",
212 cstring_fromChars (matchname)),
219 cstring_free (xname);
220 /*@noaccess cstring@*/
224 static void printDot (void)
226 if (context_getFlag (FLG_SHOWSCAN))
228 (void) fflush (g_msgstream);
229 fprintf (stderr, ".");
230 (void) fflush (stderr);
235 dumpState (cstring cfname)
238 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
240 f = fopen (cstring_toCharsSafe (fname), "w");
242 if (context_getFlag (FLG_SHOWSCAN))
244 fprintf (stderr, "< Dumping to %s ", cstring_toCharsSafe (fname));
249 llgloberror (message ("Cannot open dump file for writing: %s", fname));
254 ** sequence is convulted --- must call usymtab_prepareDump before
255 ** dumping ctype table to convert type uid's
261 DPRINTF (("Before prepare dump:"));
263 DPRINTF (("Preparing dump..."));
266 usymtab_prepareDump ();
269 ** Be careful, these lines must match loadLCDFile checking.
272 fprintf (f, ";;LCLint Dump: %s\n", cstring_toCharsSafe (fname));
273 fprintf (f, ";;%s\n", LCL_VERSION);
274 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
275 fprintf (f, ";;ctTable\n");
277 DPRINTF (("Dumping types..."));
282 DPRINTF (("Dumping type sets..."));
283 fprintf (f, ";;tistable\n");
284 typeIdSet_dumpTable (f);
287 DPRINTF (("Dumping usymtab..."));
288 fprintf (f, ";;symTable\n");
292 DPRINTF (("Dumping modules..."));
293 fprintf (f, ";; Modules access\n");
294 context_dumpModuleAccess (f);
295 fprintf (f, ";;End\n");
296 check (fclose (f) == 0);
299 if (context_getFlag (FLG_SHOWSCAN))
301 fprintf (g_msgstream, " >\n");
304 cstring_free (fname);
313 cstring libname = fileLib_addExtension (context_selectedLibrary (),
314 cstring_makeLiteralTemp (DUMP_SUFFIX));
316 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
318 lldiagmsg (message ("Cannot find %sstandard library: %s",
319 cstring_makeLiteralTemp
320 (context_getFlag (FLG_STRICTLIB) ? "strict "
321 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
323 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
328 stdlib = fopen (cstring_toCharsSafe (fpath), "r");
332 lldiagmsg (message ("Cannot read standard library: %s",
334 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
340 if (context_getFlag (FLG_WHICHLIB))
342 char *t = mstring_create (MAX_NAME_LENGTH);
345 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
347 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
350 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
352 if (*t == ';' && *(t + 1) == ';')
360 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
367 tt = strrchr (t, '\n');
371 lldiagmsg (message ("Standard library: %s", fpath));
372 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
377 check (fclose (stdlib) == 0);
378 stdlib = fopen (cstring_toCharsSafe (fpath), "r");
381 llassert (stdlib != NULL);
383 fileloc_reallyFree (g_currentloc);
384 g_currentloc = fileloc_createLib (libname);
386 DPRINTF (("Loading: %s", fpath));
388 if (context_getDebug (FLG_SHOWSCAN))
390 fprintf (g_msgstream, "< loading standard library %s ",
391 cstring_toCharsSafe (fpath));
392 result = loadLCDFile (stdlib, fpath);
393 fprintf (g_msgstream, " >\n");
397 result = loadLCDFile (stdlib, fpath);
400 check (fclose (stdlib) == 0);
404 cstring_free (libname);
408 /*@constant int BUFLEN;@*/
412 loadLCDFile (FILE *f, cstring name)
417 ** Check version. Should be >= LCLINT_LIBVERSION
420 if (reader_readLine (f, buf, BUFLEN) == NULL
421 || !mstring_equalPrefix (buf, ";;LCLint Dump:"))
423 loadllmsg (message ("Load library %s is not in LCLint library format. Attempting "
424 "to continue without library.", name));
428 if (reader_readLine (f, buf, BUFLEN) != NULL)
430 if (!mstring_equalPrefix (buf, ";;"))
432 loadllmsg (message ("Load library %s is not in LCLint library format. Attempting "
433 "to continue without library.", name));
436 else if (mstring_equalPrefix (buf, ";;ctTable"))
438 loadllmsg (message ("Load library %s is in obsolete LCLint library format. Attempting "
439 "to continue anyway, but results may be incorrect. Rebuild "
440 "the library with this version of lclint.",
447 if (sscanf (buf, ";;LCLint %f", &version) != 1)
449 loadllmsg (message ("Load library %s is not in LCLint library format (missing version "
450 "number). Attempting "
451 "to continue without library.", name));
456 if ((LCLINT_LIBVERSION - version) >= FLT_EPSILON)
459 char *nl = strchr (buf, '\n');
463 vname = cstring_fromChars (buf + 9);
465 loadllmsg (message ("Load library %s is in obsolete LCLint library "
466 "format (version %s). Attempting "
467 "to continue anyway, but results may be incorrect. Rebuild "
468 "the library with this version of lclint.",
473 if (reader_readLine (f, buf, BUFLEN) == NULL)
475 loadllmsg (message ("Load library %s is not in LCLint library "
476 "format (missing library code). Attempting "
477 "to continue without library.", name));
484 if (sscanf (buf, ";;lib:%d", &lib) != 1)
486 loadllmsg (message ("Load library %s is not in LCLint library "
487 "format (missing library code). Attempting "
488 "to continue without library.", name));
493 flagcode code = (flagcode) lib;
495 if (flagcode_isLibraryFlag (code))
497 if (context_doMerge ())
499 context_setLibrary (code);
504 loadllmsg (message ("Load library %s has invalid library code (%s). "
505 "Attempting to continue without library.",
507 flagcode_unparse (code)));
519 loadllmsg (message ("Load library %s is not in LCLint library format (missing lines). "
520 "Attempting to continue without library.", name));
527 typeIdSet_loadTable (f);
533 context_loadModuleAccess (f);
540 ** load state from file created by dumpState
544 loadState (cstring cfname)
547 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
549 f = fopen (cstring_toCharsSafe (fname), "r");
553 if (context_getDebug (FLG_SHOWSCAN))
554 fprintf (g_msgstream, " >\n");
556 llfatalerror (message ("Cannot open dump file for loading: %s",
561 fileloc_reallyFree (g_currentloc);
562 g_currentloc = fileloc_createLib (cfname);
564 if (!loadLCDFile (f, cfname))
566 if (!loadStandardState ())
572 check (fclose (f) == 0);
575 /* usymtab_printAll (); */
576 cstring_free (fname);