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"
49 /*@-incondefs@*/ /*@-redecl@*/
50 extern /*:open:*/ /*@dependent@*/ FILE *yyin;
51 /*@=incondefs@*/ /*@=redecl@*/
53 /*@constant int NUMLIBS; @*/
56 /*@constant int NUMPOSIXLIBS; @*/
57 # define NUMPOSIXLIBS 18
59 static ob_mstring posixlibs[NUMPOSIXLIBS] =
70 "netinet/in", /* unix */
71 "sys/resource", /* unix */
72 "sys/socket", /* not posix */
73 "sys/syslog", /* not posix */
81 static ob_mstring stdlibs[NUMLIBS] =
102 static bool loadLCDFile (FILE * p_f, cstring p_name);
105 lcllib_isSkipHeader (cstring sname)
108 bool posixlib = FALSE;
113 llassert (cstring_isDefined (sname));
114 xname = fileLib_withoutExtension (sname, cstring_makeLiteralTemp (".h"));
116 DPRINTF (("Include? %s", sname));
119 llassert (cstring_isDefined (xname));
120 libname = strrchr (xname, CONNECTCHAR);
134 if (mstring_equal (libname, "varargs"))
136 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
140 message ("Include file <%s.h> is inconsistent with "
141 "ANSI library (should use <stdarg.h>)",
142 cstring_fromChars (libname)),
146 cstring_free (xname);
150 if (context_getFlag (FLG_SKIPANSIHEADERS)
151 && context_usingAnsiLibrary ())
154 for (i = 0; i < NUMLIBS; i++)
156 if (mstring_equal (libname, stdlibs[i]))
164 for (i = 0; i < NUMPOSIXLIBS; i++)
166 if (strchr (posixlibs[i], CONNECTCHAR) != NULL)
170 if ((ptr = strstr (xname, posixlibs[i])) != NULL)
172 if (ptr[strlen (posixlibs[i])] == '\0')
186 if (mstring_equal (libname, posixlibs[i]))
198 if (context_usingPosixLibrary ())
200 if (context_getFlag (FLG_SKIPPOSIXHEADERS))
202 cstring_free (xname);
208 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
212 message ("Include file <%s.h> matches the name of a "
213 "POSIX library, but the POSIX library is "
214 "not being used. Consider using +posixlib "
215 "or +posixstrictlib to select the POSIX "
216 "library, or -warnposix "
217 "to suppress this message.",
218 cstring_fromChars (matchname)),
225 cstring_free (xname);
226 /*@noaccess cstring@*/
230 static void printDot (void)
232 if (context_getFlag (FLG_SHOWSCAN))
234 (void) fflush (g_msgstream);
235 fprintf (stderr, ".");
236 (void) fflush (stderr);
241 dumpState (cstring cfname)
244 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
246 f = fileTable_openFile (context_fileTable (), fname, "w");
248 if (context_getFlag (FLG_SHOWSCAN))
250 fprintf (stderr, "< Dumping to %s ", cstring_toCharsSafe (fname));
255 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
260 ** sequence is convulted --- must call usymtab_prepareDump before
261 ** dumping ctype table to convert type uid's
267 DPRINTF (("Before prepare dump:"));
269 DPRINTF (("Preparing dump..."));
272 usymtab_prepareDump ();
275 ** Be careful, these lines must match loadLCDFile checking.
278 fprintf (f, ";;LCLint Dump: %s\n", cstring_toCharsSafe (fname));
279 fprintf (f, ";;%s\n", LCL_VERSION);
280 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
281 fprintf (f, ";;ctTable\n");
283 DPRINTF (("Dumping types..."));
288 DPRINTF (("Dumping type sets..."));
289 fprintf (f, ";;tistable\n");
290 typeIdSet_dumpTable (f);
293 DPRINTF (("Dumping usymtab..."));
294 fprintf (f, ";;symTable\n");
298 DPRINTF (("Dumping modules..."));
299 fprintf (f, ";; Modules access\n");
300 context_dumpModuleAccess (f);
301 fprintf (f, ";;End\n");
302 check (fileTable_closeFile (context_fileTable (), f));
305 if (context_getFlag (FLG_SHOWSCAN))
307 fprintf (g_msgstream, " >\n");
310 cstring_free (fname);
319 cstring libname = fileLib_addExtension (context_selectedLibrary (),
320 cstring_makeLiteralTemp (DUMP_SUFFIX));
322 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
324 lldiagmsg (message ("Cannot find %sstandard library: %s",
325 cstring_makeLiteralTemp
326 (context_getFlag (FLG_STRICTLIB) ? "strict "
327 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
329 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
334 stdlib = fileTable_openFile (context_fileTable (), fpath, "r");
338 lldiagmsg (message ("Cannot read standard library: %s",
340 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
346 if (context_getFlag (FLG_WHICHLIB))
348 char *t = mstring_create (MAX_NAME_LENGTH);
351 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
353 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
356 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
358 if (*t == ';' && *(t + 1) == ';')
366 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
373 tt = strrchr (t, '\n');
377 lldiagmsg (message ("Standard library: %s", fpath));
378 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
383 check (fileTable_closeFile (context_fileTable (), stdlib));
384 stdlib = fileTable_openFile (context_fileTable (), fpath, "r");
387 llassert (stdlib != NULL);
389 fileloc_reallyFree (g_currentloc);
390 g_currentloc = fileloc_createLib (libname);
392 DPRINTF (("Loading: %s", fpath));
394 if (context_getDebug (FLG_SHOWSCAN))
396 fprintf (g_msgstream, "< loading standard library %s ",
397 cstring_toCharsSafe (fpath));
398 result = loadLCDFile (stdlib, fpath);
399 fprintf (g_msgstream, " >\n");
403 result = loadLCDFile (stdlib, fpath);
406 check (fileTable_closeFile (context_fileTable (), stdlib));
410 cstring_free (libname);
414 /*@constant int BUFLEN;@*/
418 loadLCDFile (FILE *f, cstring name)
423 ** Check version. Should be >= LCLINT_LIBVERSION
426 if (reader_readLine (f, buf, BUFLEN) == NULL
427 || !mstring_equalPrefix (buf, ";;LCLint Dump:"))
429 loadllmsg (message ("Load library %s is not in LCLint library format. Attempting "
430 "to continue without library.", name));
434 if (reader_readLine (f, buf, BUFLEN) != NULL)
436 if (!mstring_equalPrefix (buf, ";;"))
438 loadllmsg (message ("Load library %s is not in LCLint library format. Attempting "
439 "to continue without library.", name));
442 else if (mstring_equalPrefix (buf, ";;ctTable"))
444 loadllmsg (message ("Load library %s is in obsolete LCLint library format. Attempting "
445 "to continue anyway, but results may be incorrect. Rebuild "
446 "the library with this version of lclint.",
453 if (sscanf (buf, ";;LCLint %f", &version) != 1)
455 loadllmsg (message ("Load library %s is not in LCLint library format (missing version "
456 "number). Attempting "
457 "to continue without library.", name));
462 if ((LCLINT_LIBVERSION - version) >= FLT_EPSILON)
465 char *nl = strchr (buf, '\n');
469 vname = cstring_fromChars (buf + 9);
471 loadllmsg (message ("Load library %s is in obsolete LCLint library "
472 "format (version %s). Attempting "
473 "to continue anyway, but results may be incorrect. Rebuild "
474 "the library with this version of lclint.",
479 if (reader_readLine (f, buf, BUFLEN) == NULL)
481 loadllmsg (message ("Load library %s is not in LCLint library "
482 "format (missing library code). Attempting "
483 "to continue without library.", name));
490 if (sscanf (buf, ";;lib:%d", &lib) != 1)
492 loadllmsg (message ("Load library %s is not in LCLint library "
493 "format (missing library code). Attempting "
494 "to continue without library.", name));
499 flagcode code = (flagcode) lib;
501 if (flagcode_isLibraryFlag (code))
503 if (context_doMerge ())
505 context_setLibrary (code);
510 loadllmsg (message ("Load library %s has invalid library code (%s). "
511 "Attempting to continue without library.",
513 flagcode_unparse (code)));
525 loadllmsg (message ("Load library %s is not in LCLint library format (missing lines). "
526 "Attempting to continue without library.", name));
533 typeIdSet_loadTable (f);
539 context_loadModuleAccess (f);
546 ** load state from file created by dumpState
550 loadState (cstring cfname)
553 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
555 f = fileTable_openFile (context_fileTable (), fname, "r");
559 if (context_getDebug (FLG_SHOWSCAN))
560 fprintf (g_msgstream, " >\n");
562 llfatalerror (message ("Cannot open dump file for loading: %s",
567 fileloc_reallyFree (g_currentloc);
568 g_currentloc = fileloc_createLib (cfname);
570 if (!loadLCDFile (f, cfname))
572 if (!loadStandardState ())
578 check (fileTable_closeFile (context_fileTable (), f));
581 /* usymtab_printAll (); */
582 cstring_free (fname);