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 13
61 static ob_mstring posixlibs[NUMPOSIXLIBS] =
78 static ob_mstring stdlibs[NUMLIBS] =
99 static bool loadLCDFile (FILE * p_f, cstring p_name);
102 lcllib_isSkipHeader (cstring sname)
105 bool posixlib = FALSE;
110 llassert (cstring_isDefined (sname));
111 xname = fileLib_withoutExtension (sname, cstring_makeLiteralTemp (".h"));
114 llassert (cstring_isDefined (xname));
115 libname = strrchr (xname, CONNECTCHAR);
129 if (mstring_equal (libname, "varargs"))
131 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
135 message ("Include file <%s> is inconsistent with "
136 "ANSI library (should use <stdarg.h>)",
137 cstring_fromChars (libname)),
145 if (context_getFlag (FLG_SKIPANSIHEADERS)
146 && context_usingAnsiLibrary ())
149 for (i = 0; i < NUMLIBS; i++)
151 if (mstring_equal (libname, stdlibs[i]))
159 for (i = 0; i < NUMPOSIXLIBS; i++)
161 if (strchr (posixlibs[i], CONNECTCHAR) != NULL)
165 if ((ptr = strstr (xname, posixlibs[i])) != NULL)
167 if (ptr[strlen (posixlibs[i])] == '\0')
181 if (mstring_equal (libname, posixlibs[i]))
193 if (context_usingPosixLibrary ())
195 if (context_getFlag (FLG_SKIPPOSIXHEADERS))
203 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
207 message ("Include file <%s> matches the name of a "
208 "POSIX library, but the POSIX library is "
209 "not being used. Consider using +posixlib "
210 "or +posixstrictlib to select the POSIX "
211 "library, or -warnposix "
212 "to suppress this message.",
213 cstring_fromChars (matchname)),
220 cstring_free (xname);
221 /*@noaccess cstring@*/
225 static void printDot (void)
227 if (context_getFlag (FLG_SHOWSCAN))
229 (void) fflush (g_msgstream);
230 fprintf (stderr, ".");
231 (void) fflush (stderr);
236 dumpState (cstring cfname)
239 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
241 f = fopen (cstring_toCharsSafe (fname), "w");
243 if (context_getFlag (FLG_SHOWSCAN))
245 fprintf (stderr, "< Dumping to %s ", cstring_toCharsSafe (fname));
250 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
255 ** sequence is convulted --- must call usymtab_prepareDump before
256 ** dumping ctype table to convert type uid's
262 DPRINTF (("Before prepare dump:"));
264 DPRINTF (("Preparing dump..."));
267 usymtab_prepareDump ();
270 ** Be careful, these lines must match loadLCDFile checking.
273 fprintf (f, ";;LCLint Dump: %s\n", cstring_toCharsSafe (fname));
274 fprintf (f, ";;%s\n", LCL_VERSION);
275 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
276 fprintf (f, ";;ctTable\n");
278 DPRINTF (("Dumping types..."));
283 DPRINTF (("Dumping type sets..."));
284 fprintf (f, ";;tistable\n");
285 typeIdSet_dumpTable (f);
288 DPRINTF (("Dumping usymtab..."));
289 fprintf (f, ";;symTable\n");
293 DPRINTF (("Dumping modules..."));
294 fprintf (f, ";; Modules access\n");
295 context_dumpModuleAccess (f);
296 fprintf (f, ";;End\n");
297 check (fclose (f) == 0);
300 if (context_getFlag (FLG_SHOWSCAN))
302 fprintf (g_msgstream, " >\n");
305 cstring_free (fname);
314 cstring libname = fileLib_addExtension (context_selectedLibrary (),
315 cstring_makeLiteralTemp (DUMP_SUFFIX));
317 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
319 lldiagmsg (message ("Cannot find %sstandard library: %s",
320 cstring_makeLiteralTemp
321 (context_getFlag (FLG_STRICTLIB) ? "strict "
322 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
324 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
329 stdlib = fopen (cstring_toCharsSafe (fpath), "r");
333 lldiagmsg (message ("Cannot read standard library: %s",
335 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
341 if (context_getFlag (FLG_WHICHLIB))
343 char *t = mstring_create (MAX_NAME_LENGTH);
346 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
348 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
351 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
353 if (*t == ';' && *(t + 1) == ';')
361 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
368 tt = strrchr (t, '\n');
372 lldiagmsg (message ("Standard library: %s", fpath));
373 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
378 check (fclose (stdlib) == 0);
379 stdlib = fopen (cstring_toCharsSafe (fpath), "r");
382 llassert (stdlib != NULL);
384 fileloc_reallyFree (g_currentloc);
385 g_currentloc = fileloc_createLib (libname);
387 DPRINTF (("Loading: %s", fpath));
389 if (context_getDebug (FLG_SHOWSCAN))
391 fprintf (g_msgstream, "< loading standard library %s ",
392 cstring_toCharsSafe (fpath));
393 result = loadLCDFile (stdlib, fpath);
394 fprintf (g_msgstream, " >\n");
398 result = loadLCDFile (stdlib, fpath);
401 check (fclose (stdlib) == 0);
405 cstring_free (libname);
409 /*@constant int BUFLEN;@*/
413 loadLCDFile (FILE *f, cstring name)
418 ** Check version. Should be >= LCLINT_LIBVERSION
421 if (reader_readLine (f, buf, BUFLEN) == NULL
422 || !mstring_equalPrefix (buf, ";;LCLint Dump:"))
424 loadllmsg (message ("Load library %s is not in LCLint library format. Attempting "
425 "to continue without library.", name));
429 if (reader_readLine (f, buf, BUFLEN) != NULL)
431 if (!mstring_equalPrefix (buf, ";;"))
433 loadllmsg (message ("Load library %s is not in LCLint library format. Attempting "
434 "to continue without library.", name));
437 else if (mstring_equalPrefix (buf, ";;ctTable"))
439 loadllmsg (message ("Load library %s is in obsolete LCLint library format. Attempting "
440 "to continue anyway, but results may be incorrect. Rebuild "
441 "the library with this version of lclint.",
448 if (sscanf (buf, ";;LCLint %f", &version) != 1)
450 loadllmsg (message ("Load library %s is not in LCLint library format (missing version "
451 "number). Attempting "
452 "to continue without library.", name));
457 if ((LCLINT_LIBVERSION - version) >= FLT_EPSILON)
460 char *nl = strchr (buf, '\n');
464 vname = cstring_fromChars (buf + 9);
466 loadllmsg (message ("Load library %s is in obsolete LCLint library "
467 "format (version %s). Attempting "
468 "to continue anyway, but results may be incorrect. Rebuild "
469 "the library with this version of lclint.",
474 if (reader_readLine (f, buf, BUFLEN) == NULL)
476 loadllmsg (message ("Load library %s is not in LCLint library "
477 "format (missing library code). Attempting "
478 "to continue without library.", name));
485 if (sscanf (buf, ";;lib:%d", &lib) != 1)
487 loadllmsg (message ("Load library %s is not in LCLint library "
488 "format (missing library code). Attempting "
489 "to continue without library.", name));
494 flagcode code = (flagcode) lib;
496 if (flagcode_isLibraryFlag (code))
498 if (context_doMerge ())
500 context_setLibrary (code);
505 loadllmsg (message ("Load library %s has invalid library code (%s). "
506 "Attempting to continue without library.",
508 flagcode_unparse (code)));
520 loadllmsg (message ("Load library %s is not in LCLint library format (missing lines). "
521 "Attempting to continue without library.", name));
528 typeIdSet_loadTable (f);
534 context_loadModuleAccess (f);
541 ** load state from file created by dumpState
545 loadState (cstring cfname)
548 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
550 f = fopen (cstring_toCharsSafe (fname), "r");
554 if (context_getDebug (FLG_SHOWSCAN))
555 fprintf (g_msgstream, " >\n");
557 llfatalerror (message ("Cannot open dump file for loading: %s",
562 fileloc_reallyFree (g_currentloc);
563 g_currentloc = fileloc_createLib (cfname);
565 if (!loadLCDFile (f, cfname))
567 if (!loadStandardState ())
573 check (fclose (f) == 0);
576 /* usymtab_printAll (); */
577 cstring_free (fname);