2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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 splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 ** routines for loading and creating library files
29 ** this is a brute-force implementation, a more efficient
30 ** representation should be designed.
36 * - fixed the recognition of Posix headers for OS/2
39 # include "splintMacros.nf"
51 /*@-incondefs@*/ /*@-redecl@*/
52 extern /*:open:*/ /*@dependent@*/ FILE *yyin;
53 /*@=incondefs@*/ /*@=redecl@*/
55 /*@constant int NUMLIBS; @*/
58 /*@constant int NUMPOSIXLIBS; @*/
59 # define NUMPOSIXLIBS 18
61 static ob_mstring posixlibs[NUMPOSIXLIBS] =
72 "netinet/in", /* unix */
73 "sys/resource", /* unix */
74 "sys/socket", /* not posix */
75 "sys/syslog", /* not posix */
83 static ob_mstring stdlibs[NUMLIBS] =
105 "strings", /* some systems use this...they shouldn't */
112 static bool loadLCDFile (FILE * p_f, cstring p_name);
115 lcllib_isSkipHeader (cstring sname)
118 bool posixlib = FALSE;
123 llassert (cstring_isDefined (sname));
124 xname = fileLib_withoutExtension (sname, cstring_makeLiteralTemp (".h"));
126 DPRINTF (("Include? %s", sname));
129 llassert (cstring_isDefined (xname));
132 /* Posixlibs use forward slashes, so we use them here, too */
133 cstring_replaceAll (xname, '\\', '/');
134 libname = strrchr (xname, '/');
135 DPRINTF (("libname: %s", libname));
138 libname = strrchr (xname, CONNECTCHAR);
153 if (mstring_equal (libname, "varargs"))
155 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
159 message ("Include file <%s.h> is inconsistent with "
160 "ANSI library (should use <stdarg.h>)",
161 cstring_fromChars (libname)),
165 cstring_free (xname);
169 if (context_getFlag (FLG_SKIPISOHEADERS)
170 && context_usingAnsiLibrary ())
173 for (i = 0; i < NUMLIBS; i++)
175 if (mstring_equal (libname, stdlibs[i]))
183 for (i = 0; i < NUMPOSIXLIBS; i++)
185 if (strchr (posixlibs[i], CONNECTCHAR) != NULL
187 || strchr (posixlibs[i], ALTCONNECTCHAR) != NULL
193 DPRINTF (("xname: %s, posix: %s", xname, posixlibs[i]));
194 if ((ptr = strstr (xname, posixlibs[i])) != NULL)
196 if (ptr[strlen (posixlibs[i])] == '\0')
210 if (mstring_equal (libname, posixlibs[i]))
222 if (context_usingPosixLibrary ())
224 if (context_getFlag (FLG_SKIPPOSIXHEADERS))
226 cstring_free (xname);
232 the returned reference is possibly null,
233 but this should not change the null state of the parameter
239 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
243 message ("Include file <%s.h> matches the name of a "
244 "POSIX library, but the POSIX library is "
245 "not being used. Consider using +posixlib "
246 "or +posixstrictlib to select the POSIX "
247 "library, or -warnposix "
248 "to suppress this message.",
249 cstring_fromChars (matchname)),
256 cstring_free (xname);
257 /*@noaccess cstring@*/
258 /*@-nullstate@*/ /* same problem as above */
263 static void printDot (void)
265 displayScanContinue (cstring_makeLiteralTemp ("."));
269 dumpState (cstring cfname)
272 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
274 f = fileTable_openWriteFile (context_fileTable (), fname);
276 displayScanOpen (message ("Dumping to %s ", fname));
280 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
285 ** sequence is convulted --- must call usymtab_prepareDump before
286 ** dumping ctype table to convert type uid's
292 DPRINTF (("Before prepare dump:"));
294 DPRINTF (("Preparing dump..."));
297 usymtab_prepareDump ();
300 ** Be careful, these lines must match loadLCDFile checking.
303 fprintf (f, "%s %s\n", LIBRARY_MARKER, cstring_toCharsSafe (fname));
304 fprintf (f, ";;Splint %f\n", SPLINT_LIBVERSION);
305 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
306 fprintf (f, ";;ctTable\n");
308 DPRINTF (("Dumping types..."));
313 DPRINTF (("Dumping type sets..."));
314 fprintf (f, ";;tistable\n");
315 typeIdSet_dumpTable (f);
318 DPRINTF (("Dumping usymtab..."));
319 fprintf (f, ";;symTable\n");
323 DPRINTF (("Dumping modules..."));
324 fprintf (f, ";; Modules access\n");
325 context_dumpModuleAccess (f);
326 fprintf (f, ";;End\n");
327 check (fileTable_closeFile (context_fileTable (), f));
331 cstring_free (fname);
340 cstring libname = fileLib_addExtension (context_selectedLibrary (),
341 cstring_makeLiteralTemp (DUMP_SUFFIX));
343 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
345 lldiagmsg (message ("Cannot find %sstandard library: %s",
346 cstring_makeLiteralTemp
347 (context_getFlag (FLG_STRICTLIB) ? "strict "
348 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
350 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
355 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
359 lldiagmsg (message ("Cannot read standard library: %s",
361 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
367 if (context_getFlag (FLG_WHICHLIB))
369 char *t = mstring_create (MAX_NAME_LENGTH);
372 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
374 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
377 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
379 if (*t == ';' && *(t + 1) == ';')
387 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
394 tt = strrchr (t, '\n');
398 lldiagmsg (message ("Standard library: %s", fpath));
399 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
404 check (fileTable_closeFile (context_fileTable (), stdlib));
405 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
408 llassert (stdlib != NULL);
410 fileloc_reallyFree (g_currentloc);
411 g_currentloc = fileloc_createLib (libname);
413 DPRINTF (("Loading: %s", fpath));
415 displayScanOpen (message ("loading standard library %s ", fpath));
416 result = loadLCDFile (stdlib, fpath);
419 check (fileTable_closeFile (context_fileTable (), stdlib));
423 cstring_free (libname);
427 /*@constant int BUFLEN;@*/
431 loadLCDFile (FILE *f, cstring name)
436 ** Check version. Should be >= SPLINT_LIBVERSION
439 if (reader_readLine (f, buf, BUFLEN) == NULL
440 || !mstring_equalPrefix (buf, LIBRARY_MARKER))
442 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
443 "to continue without library.", name));
447 if (reader_readLine (f, buf, BUFLEN) != NULL)
449 if (!mstring_equalPrefix (buf, ";;"))
451 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
452 "to continue without library.", name));
455 else if (mstring_equalPrefix (buf, ";;ctTable"))
457 loadllmsg (message ("Load library %s is in obsolete Splint library format. Attempting "
458 "to continue anyway, but results may be incorrect. Rebuild "
459 "the library with this version of splint.",
466 if (sscanf (buf, ";;Splint %f", &version) != 1
467 && (sscanf (buf, ";;LCLint %f", &version) != 1))
469 loadllmsg (message ("Load library %s is not in Splint library format (missing version "
470 "number). Attempting "
471 "to continue without library.", name));
476 if ((SPLINT_LIBVERSION - version) >= FLT_EPSILON)
479 char *nl = strchr (buf, '\n');
483 vname = cstring_fromChars (buf + 9);
485 loadllmsg (message ("Load library %s is in obsolete Splint library "
486 "format (version %f (%s), expecting version %f). Attempting "
487 "to continue anyway, but results may be incorrect. Rebuild "
488 "the library with this version of splint.",
496 if (reader_readLine (f, buf, BUFLEN) == NULL)
498 loadllmsg (message ("Load library %s is not in Splint library "
499 "format (missing library code). Attempting "
500 "to continue without library.", name));
507 if (sscanf (buf, ";;lib:%d", &lib) != 1)
509 loadllmsg (message ("Load library %s is not in Splint library "
510 "format (missing library code). Attempting "
511 "to continue without library.", name));
516 flagcode code = (flagcode) lib;
518 if (flagcode_isLibraryFlag (code))
520 if (context_doMerge ())
522 context_setLibrary (code);
527 loadllmsg (message ("Load library %s has invalid library code (%s). "
528 "Attempting to continue without library.",
530 flagcode_unparse (code)));
542 loadllmsg (message ("Load library %s is not in Splint library format (missing lines). "
543 "Attempting to continue without library.", name));
550 typeIdSet_loadTable (f);
556 context_loadModuleAccess (f);
563 ** load state from file created by dumpState
567 loadState (cstring cfname)
570 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
572 f = fileTable_openReadFile (context_fileTable (), fname);
577 llfatalerror (message ("Cannot open dump file for loading: %s",
582 fileloc_reallyFree (g_currentloc);
583 g_currentloc = fileloc_createLib (cfname);
585 if (!loadLCDFile (f, cfname))
587 if (!loadStandardState ())
593 check (fileTable_closeFile (context_fileTable (), f));
596 /* usymtab_printAll (); */
597 cstring_free (fname);