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"
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
234 investigate this warning
240 fileloc tmp = fileloc_makePreprocPrevious (g_currentloc);
244 message ("Include file <%s.h> matches the name of a "
245 "POSIX library, but the POSIX library is "
246 "not being used. Consider using +posixlib "
247 "or +posixstrictlib to select the POSIX "
248 "library, or -warnposix "
249 "to suppress this message.",
250 cstring_fromChars (matchname)),
257 cstring_free (xname);
258 /*@noaccess cstring@*/
259 /*@-nullstate@*/ /*@i233@*/ /* same problem as above */
264 static void printDot (void)
266 displayScanContinue (cstring_makeLiteralTemp ("."));
270 dumpState (cstring cfname)
273 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
275 f = fileTable_openWriteFile (context_fileTable (), fname);
277 displayScanOpen (message ("Dumping to %s ", fname));
281 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
286 ** sequence is convulted --- must call usymtab_prepareDump before
287 ** dumping ctype table to convert type uid's
293 DPRINTF (("Before prepare dump:"));
295 DPRINTF (("Preparing dump..."));
298 usymtab_prepareDump ();
301 ** Be careful, these lines must match loadLCDFile checking.
304 fprintf (f, "%s %s\n", LIBRARY_MARKER, cstring_toCharsSafe (fname));
305 fprintf (f, ";;%s\n", SPLINT_VERSION);
306 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
307 fprintf (f, ";;ctTable\n");
309 DPRINTF (("Dumping types..."));
314 DPRINTF (("Dumping type sets..."));
315 fprintf (f, ";;tistable\n");
316 typeIdSet_dumpTable (f);
319 DPRINTF (("Dumping usymtab..."));
320 fprintf (f, ";;symTable\n");
324 DPRINTF (("Dumping modules..."));
325 fprintf (f, ";; Modules access\n");
326 context_dumpModuleAccess (f);
327 fprintf (f, ";;End\n");
328 check (fileTable_closeFile (context_fileTable (), f));
332 cstring_free (fname);
341 cstring libname = fileLib_addExtension (context_selectedLibrary (),
342 cstring_makeLiteralTemp (DUMP_SUFFIX));
344 if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
346 lldiagmsg (message ("Cannot find %sstandard library: %s",
347 cstring_makeLiteralTemp
348 (context_getFlag (FLG_STRICTLIB) ? "strict "
349 : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
351 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
356 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
360 lldiagmsg (message ("Cannot read standard library: %s",
362 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
368 if (context_getFlag (FLG_WHICHLIB))
370 char *t = mstring_create (MAX_NAME_LENGTH);
373 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
375 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
378 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
380 if (*t == ';' && *(t + 1) == ';')
388 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
395 tt = strrchr (t, '\n');
399 lldiagmsg (message ("Standard library: %s", fpath));
400 lldiagmsg (message (" (created using %s)", cstring_fromChars (t)));
405 check (fileTable_closeFile (context_fileTable (), stdlib));
406 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
409 llassert (stdlib != NULL);
411 fileloc_reallyFree (g_currentloc);
412 g_currentloc = fileloc_createLib (libname);
414 DPRINTF (("Loading: %s", fpath));
416 displayScanOpen (message ("loading standard library %s ", fpath));
417 result = loadLCDFile (stdlib, fpath);
420 check (fileTable_closeFile (context_fileTable (), stdlib));
424 cstring_free (libname);
428 /*@constant int BUFLEN;@*/
432 loadLCDFile (FILE *f, cstring name)
437 ** Check version. Should be >= SPLINT_LIBVERSION
440 if (reader_readLine (f, buf, BUFLEN) == NULL
441 || !mstring_equalPrefix (buf, LIBRARY_MARKER))
443 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
444 "to continue without library.", name));
448 if (reader_readLine (f, buf, BUFLEN) != NULL)
450 if (!mstring_equalPrefix (buf, ";;"))
452 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
453 "to continue without library.", name));
456 else if (mstring_equalPrefix (buf, ";;ctTable"))
458 loadllmsg (message ("Load library %s is in obsolete Splint library format. Attempting "
459 "to continue anyway, but results may be incorrect. Rebuild "
460 "the library with this version of splint.",
467 if (sscanf (buf, ";;Splint %f", &version) != 1
468 && (sscanf (buf, ";;LCLint %f", &version) != 1))
470 loadllmsg (message ("Load library %s is not in Splint library format (missing version "
471 "number). Attempting "
472 "to continue without library.", name));
477 if ((SPLINT_LIBVERSION - version) >= FLT_EPSILON)
480 char *nl = strchr (buf, '\n');
484 vname = cstring_fromChars (buf + 9);
486 loadllmsg (message ("Load library %s is in obsolete Splint library "
487 "format (version %s). Attempting "
488 "to continue anyway, but results may be incorrect. Rebuild "
489 "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);