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"
50 /*@-incondefs@*/ /*@-redecl@*/
51 extern /*:open:*/ /*@dependent@*/ FILE *yyin;
52 /*@=incondefs@*/ /*@=redecl@*/
54 /*@constant int NUMLIBS; @*/
57 /*@constant int NUMPOSIXLIBS; @*/
58 # define NUMPOSIXLIBS 18
60 static ob_mstring posixlibs[NUMPOSIXLIBS] =
71 "netinet/in", /* unix */
72 "sys/resource", /* unix */
73 "sys/socket", /* not posix */
74 "sys/syslog", /* not posix */
82 static ob_mstring stdlibs[NUMLIBS] =
104 "strings", /* some systems use this...they shouldn't */
111 static bool loadLCDFile (FILE * p_f, cstring p_name);
114 lcllib_isSkipHeader (cstring sname)
117 bool posixlib = FALSE;
122 llassert (cstring_isDefined (sname));
123 xname = fileLib_withoutExtension (sname, cstring_makeLiteralTemp (".h"));
125 DPRINTF (("Include? %s", sname));
128 llassert (cstring_isDefined (xname));
131 /* Posixlibs use forward slashes, so we use them here, too */
132 cstring_replaceAll (xname, '\\', '/');
133 libname = strrchr (xname, '/');
134 DPRINTF (("libname: %s", libname));
137 libname = strrchr (xname, CONNECTCHAR);
152 if (mstring_equal (libname, "varargs"))
154 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
158 message ("Include file <%s.h> is inconsistent with "
159 "ANSI library (should use <stdarg.h>)",
160 cstring_fromChars (libname)),
164 cstring_free (xname);
168 if (context_getFlag (FLG_SKIPISOHEADERS)
169 && context_usingAnsiLibrary ())
171 for (i = 0; i < NUMLIBS; i++)
173 if (mstring_equal (libname, stdlibs[i]))
181 for (i = 0; i < NUMPOSIXLIBS; i++)
183 if (strchr (posixlibs[i], CONNECTCHAR) != NULL
185 || strchr (posixlibs[i], ALTCONNECTCHAR) != NULL
191 DPRINTF (("xname: %s, posix: %s", xname, posixlibs[i]));
192 if ((ptr = strstr (xname, posixlibs[i])) != NULL)
194 if (ptr[strlen (posixlibs[i])] == '\0')
208 if (mstring_equal (libname, posixlibs[i]))
220 if (context_usingPosixLibrary ())
222 if (context_getFlag (FLG_SKIPPOSIXHEADERS))
224 cstring_free (xname);
230 the returned reference is possibly null,
231 but this should not change the null state of the parameter
237 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
241 message ("Include file <%s.h> matches the name of a "
242 "POSIX library, but the POSIX library is "
243 "not being used. Consider using +posixlib "
244 "or +posixstrictlib to select the POSIX "
245 "library, or -warnposix "
246 "to suppress this message.",
247 cstring_fromChars (matchname)),
254 cstring_free (xname);
255 /*@noaccess cstring@*/
256 /*@-nullstate@*/ /* same problem as above */
261 static void printDot (void)
263 displayScanContinue (cstring_makeLiteralTemp ("."));
267 dumpState (cstring cfname)
270 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
272 f = fileTable_openWriteFile (context_fileTable (), fname);
274 displayScanOpen (message ("Dumping to %s ", fname));
278 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
283 ** sequence is convulted --- must call usymtab_prepareDump before
284 ** dumping ctype table to convert type uid's
290 DPRINTF (("Before prepare dump:"));
292 DPRINTF (("Preparing dump..."));
295 usymtab_prepareDump ();
298 ** Be careful, these lines must match loadLCDFile checking.
301 fprintf (f, "%s %s\n", LIBRARY_MARKER, cstring_toCharsSafe (fname));
302 fprintf (f, ";;Splint %f\n", SPLINT_LIBVERSION);
303 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
304 fprintf (f, ";;ctTable\n");
306 DPRINTF (("Dumping types..."));
311 DPRINTF (("Dumping type sets..."));
312 fprintf (f, ";;tistable\n");
313 typeIdSet_dumpTable (f);
316 DPRINTF (("Dumping usymtab..."));
317 fprintf (f, ";;symTable\n");
321 DPRINTF (("Dumping modules..."));
322 fprintf (f, ";; Modules access\n");
323 context_dumpModuleAccess (f);
324 fprintf (f, ";;End\n");
325 check (fileTable_closeFile (context_fileTable (), f));
329 cstring_free (fname);
338 cstring libname = fileLib_addExtension (context_selectedLibrary (),
339 cstring_makeLiteralTemp (DUMP_SUFFIX));
341 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
343 lldiagmsg (message ("Cannot find %sstandard library: %s",
344 cstring_makeLiteralTemp
345 (context_getFlag (FLG_STRICTLIB) ? "strict "
346 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
348 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
353 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
357 lldiagmsg (message ("Cannot read standard library: %s",
359 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
365 if (context_getFlag (FLG_WHICHLIB))
367 char *t = mstring_create (MAX_NAME_LENGTH);
370 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
372 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
375 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
377 if (*t == ';' && *(t + 1) == ';')
385 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
392 tt = strrchr (t, '\n');
396 lldiagmsg (message ("Standard library: %s", fpath));
397 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
402 check (fileTable_closeFile (context_fileTable (), stdlib));
403 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
406 llassert (stdlib != NULL);
408 fileloc_reallyFree (g_currentloc);
409 g_currentloc = fileloc_createLib (libname);
411 DPRINTF (("Loading: %s", fpath));
413 displayScanOpen (message ("loading standard library %s ", fpath));
414 result = loadLCDFile (stdlib, fpath);
417 check (fileTable_closeFile (context_fileTable (), stdlib));
421 cstring_free (libname);
425 /*@constant int BUFLEN;@*/
429 loadLCDFile (FILE *f, cstring name)
434 ** Check version. Should be >= SPLINT_LIBVERSION
437 if (reader_readLine (f, buf, BUFLEN) == NULL
438 || !mstring_equalPrefix (buf, LIBRARY_MARKER))
440 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
441 "to continue without library.", name));
445 if (reader_readLine (f, buf, BUFLEN) != NULL)
447 if (!mstring_equalPrefix (buf, ";;"))
449 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
450 "to continue without library.", name));
453 else if (mstring_equalPrefix (buf, ";;ctTable"))
455 loadllmsg (message ("Load library %s is in obsolete Splint library format. Attempting "
456 "to continue anyway, but results may be incorrect. Rebuild "
457 "the library with this version of splint.",
464 if (sscanf (buf, ";;Splint %f", &version) != 1
465 && (sscanf (buf, ";;LCLint %f", &version) != 1))
467 loadllmsg (message ("Load library %s is not in Splint library format (missing version "
468 "number). Attempting "
469 "to continue without library.", name));
474 if ((SPLINT_LIBVERSION - version) >= FLT_EPSILON)
477 char *nl = strchr (buf, '\n');
481 vname = cstring_fromChars (buf + 9);
483 loadllmsg (message ("Load library %s is in obsolete Splint library "
484 "format (version %f (%s), expecting version %f). Attempting "
485 "to continue anyway, but results may be incorrect. Rebuild "
486 "the library with this version of splint.",
494 if (reader_readLine (f, buf, BUFLEN) == NULL)
496 loadllmsg (message ("Load library %s is not in Splint library "
497 "format (missing library code). Attempting "
498 "to continue without library.", name));
505 if (sscanf (buf, ";;lib:%d", &lib) != 1)
507 loadllmsg (message ("Load library %s is not in Splint library "
508 "format (missing library code). Attempting "
509 "to continue without library.", name));
514 flagcode code = (flagcode) lib;
516 if (flagcode_isLibraryFlag (code))
518 if (context_doMerge ())
520 context_setLibrary (code);
525 loadllmsg (message ("Load library %s has invalid library code (%s). "
526 "Attempting to continue without library.",
528 flagcode_unparse (code)));
540 loadllmsg (message ("Load library %s is not in Splint library format (missing lines). "
541 "Attempting to continue without library.", name));
548 typeIdSet_loadTable (f);
554 context_loadModuleAccess (f);
561 ** load state from file created by dumpState
565 loadState (cstring cfname)
568 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
570 f = fileTable_openReadFile (context_fileTable (), fname);
575 llfatalerror (message ("Cannot open dump file for loading: %s",
580 fileloc_reallyFree (g_currentloc);
581 g_currentloc = fileloc_createLib (cfname);
583 if (!loadLCDFile (f, cfname))
585 if (!loadStandardState ())
591 check (fileTable_closeFile (context_fileTable (), f));
594 /* usymtab_printAll (); */
595 cstring_free (fname);