2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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"
53 /*@-incondefs@*/ /*@-redecl@*/
54 extern /*:open:*/ /*@dependent@*/ FILE *yyin;
55 /*@=incondefs@*/ /*@=redecl@*/
57 /*@constant int NUMLIBS; @*/
60 /*@constant int NUMPOSIXLIBS; @*/
61 # define NUMPOSIXLIBS 18
63 static ob_mstring posixlibs[NUMPOSIXLIBS] =
74 "netinet/in", /* unix */
75 "sys/resource", /* unix */
76 "sys/socket", /* not posix */
77 "sys/syslog", /* not posix */
85 static ob_mstring stdlibs[NUMLIBS] =
107 "strings", /* some systems use this...they shouldn't */
114 static bool loadLCDFile (FILE * p_f, cstring p_name);
117 lcllib_isSkipHeader (cstring sname)
120 bool posixlib = FALSE;
125 llassert (cstring_isDefined (sname));
126 xname = fileLib_withoutExtension (sname, cstring_makeLiteralTemp (".h"));
128 DPRINTF (("Include? %s", sname));
131 llassert (cstring_isDefined (xname));
134 /* Posixlibs use forward slashes, so we use them here, too */
135 cstring_replaceAll (xname, '\\', '/');
136 libname = strrchr (xname, '/');
137 DPRINTF (("libname: %s", libname));
140 libname = strrchr (xname, CONNECTCHAR);
155 if (mstring_equal (libname, "varargs"))
157 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
161 message ("Include file <%s.h> is inconsistent with "
162 "ANSI library (should use <stdarg.h>)",
163 cstring_fromChars (libname)),
167 cstring_free (xname);
171 if (context_getFlag (FLG_SKIPISOHEADERS)
172 && context_usingAnsiLibrary ())
175 for (i = 0; i < NUMLIBS; i++)
177 if (mstring_equal (libname, stdlibs[i]))
185 for (i = 0; i < NUMPOSIXLIBS; i++)
187 if (strchr (posixlibs[i], CONNECTCHAR) != NULL
189 || strchr (posixlibs[i], ALTCONNECTCHAR) != NULL
195 DPRINTF (("xname: %s, posix: %s", xname, posixlibs[i]));
196 if ((ptr = strstr (xname, posixlibs[i])) != NULL)
198 if (ptr[strlen (posixlibs[i])] == '\0')
212 if (mstring_equal (libname, posixlibs[i]))
224 if (context_usingPosixLibrary ())
226 if (context_getFlag (FLG_SKIPPOSIXHEADERS))
228 cstring_free (xname);
234 the returned reference is possibly null,
235 but this should not change the null state of the parameter
236 investigate this warning
242 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
246 message ("Include file <%s.h> matches the name of a "
247 "POSIX library, but the POSIX library is "
248 "not being used. Consider using +posixlib "
249 "or +posixstrictlib to select the POSIX "
250 "library, or -warnposix "
251 "to suppress this message.",
252 cstring_fromChars (matchname)),
259 cstring_free (xname);
260 /*@noaccess cstring@*/
261 /*@-nullstate@*/ /*@i233@*/ /* same problem as above */
266 static void printDot (void)
268 displayScanContinue (cstring_makeLiteralTemp ("."));
272 dumpState (cstring cfname)
275 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
277 f = fileTable_openWriteFile (context_fileTable (), fname);
279 displayScanOpen (message ("Dumping to %s ", fname));
283 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
288 ** sequence is convulted --- must call usymtab_prepareDump before
289 ** dumping ctype table to convert type uid's
295 DPRINTF (("Before prepare dump:"));
297 DPRINTF (("Preparing dump..."));
300 usymtab_prepareDump ();
303 ** Be careful, these lines must match loadLCDFile checking.
306 fprintf (f, "%s %s\n", LIBRARY_MARKER, cstring_toCharsSafe (fname));
307 fprintf (f, ";;%s\n", SPLINT_VERSION);
308 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
309 fprintf (f, ";;ctTable\n");
311 DPRINTF (("Dumping types..."));
316 DPRINTF (("Dumping type sets..."));
317 fprintf (f, ";;tistable\n");
318 typeIdSet_dumpTable (f);
321 DPRINTF (("Dumping usymtab..."));
322 fprintf (f, ";;symTable\n");
326 DPRINTF (("Dumping modules..."));
327 fprintf (f, ";; Modules access\n");
328 context_dumpModuleAccess (f);
329 fprintf (f, ";;End\n");
330 check (fileTable_closeFile (context_fileTable (), f));
334 cstring_free (fname);
343 cstring libname = fileLib_addExtension (context_selectedLibrary (),
344 cstring_makeLiteralTemp (DUMP_SUFFIX));
346 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
348 lldiagmsg (message ("Cannot find %sstandard library: %s",
349 cstring_makeLiteralTemp
350 (context_getFlag (FLG_STRICTLIB) ? "strict "
351 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
353 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
358 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
362 lldiagmsg (message ("Cannot read standard library: %s",
364 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
370 if (context_getFlag (FLG_WHICHLIB))
372 char *t = mstring_create (MAX_NAME_LENGTH);
375 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
377 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
380 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
382 if (*t == ';' && *(t + 1) == ';')
390 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
397 tt = strrchr (t, '\n');
401 lldiagmsg (message ("Standard library: %s", fpath));
402 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
407 check (fileTable_closeFile (context_fileTable (), stdlib));
408 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
411 llassert (stdlib != NULL);
413 fileloc_reallyFree (g_currentloc);
414 g_currentloc = fileloc_createLib (libname);
416 DPRINTF (("Loading: %s", fpath));
418 displayScanOpen (message ("loading standard library %s ", fpath));
419 result = loadLCDFile (stdlib, fpath);
422 check (fileTable_closeFile (context_fileTable (), stdlib));
426 cstring_free (libname);
430 /*@constant int BUFLEN;@*/
434 loadLCDFile (FILE *f, cstring name)
439 ** Check version. Should be >= SPLINT_LIBVERSION
442 if (reader_readLine (f, buf, BUFLEN) == NULL
443 || !mstring_equalPrefix (buf, LIBRARY_MARKER))
445 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
446 "to continue without library.", name));
450 if (reader_readLine (f, buf, BUFLEN) != NULL)
452 if (!mstring_equalPrefix (buf, ";;"))
454 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
455 "to continue without library.", name));
458 else if (mstring_equalPrefix (buf, ";;ctTable"))
460 loadllmsg (message ("Load library %s is in obsolete Splint library format. Attempting "
461 "to continue anyway, but results may be incorrect. Rebuild "
462 "the library with this version of splint.",
469 if (sscanf (buf, ";;Splint %f", &version) != 1
470 && (sscanf (buf, ";;LCLint %f", &version) != 1))
472 loadllmsg (message ("Load library %s is not in Splint library format (missing version "
473 "number). Attempting "
474 "to continue without library.", name));
479 if ((SPLINT_LIBVERSION - version) >= FLT_EPSILON)
482 char *nl = strchr (buf, '\n');
486 vname = cstring_fromChars (buf + 9);
488 loadllmsg (message ("Load library %s is in obsolete Splint library "
489 "format (version %s). Attempting "
490 "to continue anyway, but results may be incorrect. Rebuild "
491 "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);