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 ())
172 for (i = 0; i < NUMLIBS; i++)
174 if (mstring_equal (libname, stdlibs[i]))
182 for (i = 0; i < NUMPOSIXLIBS; i++)
184 if (strchr (posixlibs[i], CONNECTCHAR) != NULL
186 || strchr (posixlibs[i], ALTCONNECTCHAR) != NULL
192 DPRINTF (("xname: %s, posix: %s", xname, posixlibs[i]));
193 if ((ptr = strstr (xname, posixlibs[i])) != NULL)
195 if (ptr[strlen (posixlibs[i])] == '\0')
209 if (mstring_equal (libname, posixlibs[i]))
221 if (context_usingPosixLibrary ())
223 if (context_getFlag (FLG_SKIPPOSIXHEADERS))
225 cstring_free (xname);
231 the returned reference is possibly null,
232 but this should not change the null state of the parameter
238 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
242 message ("Include file <%s.h> matches the name of a "
243 "POSIX library, but the POSIX library is "
244 "not being used. Consider using +posixlib "
245 "or +posixstrictlib to select the POSIX "
246 "library, or -warnposix "
247 "to suppress this message.",
248 cstring_fromChars (matchname)),
255 cstring_free (xname);
256 /*@noaccess cstring@*/
257 /*@-nullstate@*/ /* same problem as above */
262 static void printDot (void)
264 displayScanContinue (cstring_makeLiteralTemp ("."));
268 dumpState (cstring cfname)
271 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
273 f = fileTable_openWriteFile (context_fileTable (), fname);
275 displayScanOpen (message ("Dumping to %s ", fname));
279 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
284 ** sequence is convulted --- must call usymtab_prepareDump before
285 ** dumping ctype table to convert type uid's
291 DPRINTF (("Before prepare dump:"));
293 DPRINTF (("Preparing dump..."));
296 usymtab_prepareDump ();
299 ** Be careful, these lines must match loadLCDFile checking.
302 fprintf (f, "%s %s\n", LIBRARY_MARKER, cstring_toCharsSafe (fname));
303 fprintf (f, ";;Splint %f\n", SPLINT_LIBVERSION);
304 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
305 fprintf (f, ";;ctTable\n");
307 DPRINTF (("Dumping types..."));
312 DPRINTF (("Dumping type sets..."));
313 fprintf (f, ";;tistable\n");
314 typeIdSet_dumpTable (f);
317 DPRINTF (("Dumping usymtab..."));
318 fprintf (f, ";;symTable\n");
322 DPRINTF (("Dumping modules..."));
323 fprintf (f, ";; Modules access\n");
324 context_dumpModuleAccess (f);
325 fprintf (f, ";;End\n");
326 check (fileTable_closeFile (context_fileTable (), f));
330 cstring_free (fname);
339 cstring libname = fileLib_addExtension (context_selectedLibrary (),
340 cstring_makeLiteralTemp (DUMP_SUFFIX));
342 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
344 lldiagmsg (message ("Cannot find %sstandard library: %s",
345 cstring_makeLiteralTemp
346 (context_getFlag (FLG_STRICTLIB) ? "strict "
347 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
349 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
354 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
358 lldiagmsg (message ("Cannot read standard library: %s",
360 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
366 if (context_getFlag (FLG_WHICHLIB))
368 char *t = mstring_create (MAX_NAME_LENGTH);
371 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
373 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
376 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
378 if (*t == ';' && *(t + 1) == ';')
386 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
393 tt = strrchr (t, '\n');
397 lldiagmsg (message ("Standard library: %s", fpath));
398 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
403 check (fileTable_closeFile (context_fileTable (), stdlib));
404 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
407 llassert (stdlib != NULL);
409 fileloc_reallyFree (g_currentloc);
410 g_currentloc = fileloc_createLib (libname);
412 DPRINTF (("Loading: %s", fpath));
414 displayScanOpen (message ("loading standard library %s ", fpath));
415 result = loadLCDFile (stdlib, fpath);
418 check (fileTable_closeFile (context_fileTable (), stdlib));
422 cstring_free (libname);
426 /*@constant int BUFLEN;@*/
430 loadLCDFile (FILE *f, cstring name)
435 ** Check version. Should be >= SPLINT_LIBVERSION
438 if (reader_readLine (f, buf, BUFLEN) == NULL
439 || !mstring_equalPrefix (buf, LIBRARY_MARKER))
441 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
442 "to continue without library.", name));
446 if (reader_readLine (f, buf, BUFLEN) != NULL)
448 if (!mstring_equalPrefix (buf, ";;"))
450 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
451 "to continue without library.", name));
454 else if (mstring_equalPrefix (buf, ";;ctTable"))
456 loadllmsg (message ("Load library %s is in obsolete Splint library format. Attempting "
457 "to continue anyway, but results may be incorrect. Rebuild "
458 "the library with this version of splint.",
465 if (sscanf (buf, ";;Splint %f", &version) != 1
466 && (sscanf (buf, ";;LCLint %f", &version) != 1))
468 loadllmsg (message ("Load library %s is not in Splint library format (missing version "
469 "number). Attempting "
470 "to continue without library.", name));
475 if ((SPLINT_LIBVERSION - version) >= FLT_EPSILON)
478 char *nl = strchr (buf, '\n');
482 vname = cstring_fromChars (buf + 9);
484 loadllmsg (message ("Load library %s is in obsolete Splint library "
485 "format (version %f (%s), expecting version %f). Attempting "
486 "to continue anyway, but results may be incorrect. Rebuild "
487 "the library with this version of splint.",
495 if (reader_readLine (f, buf, BUFLEN) == NULL)
497 loadllmsg (message ("Load library %s is not in Splint library "
498 "format (missing library code). Attempting "
499 "to continue without library.", name));
506 if (sscanf (buf, ";;lib:%d", &lib) != 1)
508 loadllmsg (message ("Load library %s is not in Splint library "
509 "format (missing library code). Attempting "
510 "to continue without library.", name));
515 flagcode code = (flagcode) lib;
517 if (flagcode_isLibraryFlag (code))
519 if (context_doMerge ())
521 context_setLibrary (code);
526 loadllmsg (message ("Load library %s has invalid library code (%s). "
527 "Attempting to continue without library.",
529 flagcode_unparse (code)));
541 loadllmsg (message ("Load library %s is not in Splint library format (missing lines). "
542 "Attempting to continue without library.", name));
549 typeIdSet_loadTable (f);
555 context_loadModuleAccess (f);
562 ** load state from file created by dumpState
566 loadState (cstring cfname)
569 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
571 f = fileTable_openReadFile (context_fileTable (), fname);
576 llfatalerror (message ("Cannot open dump file for loading: %s",
581 fileloc_reallyFree (g_currentloc);
582 g_currentloc = fileloc_createLib (cfname);
584 if (!loadLCDFile (f, cfname))
586 if (!loadStandardState ())
592 check (fileTable_closeFile (context_fileTable (), f));
595 /* usymtab_printAll (); */
596 cstring_free (fname);