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"
49 /*@constant int NUMLIBS; @*/
52 /*@constant int NUMPOSIXLIBS; @*/
53 # define NUMPOSIXLIBS 18
55 static ob_mstring posixlibs
[NUMPOSIXLIBS
] =
66 "netinet/in", /* unix */
67 "sys/resource", /* unix */
68 "sys/socket", /* not posix */
69 "sys/syslog", /* not posix */
77 static ob_mstring stdlibs
[NUMLIBS
] =
99 "strings", /* some systems use this...they shouldn't */
106 static bool loadLCDFile (FILE * p_f
, cstring p_name
);
109 lcllib_isSkipHeader (cstring sname
)
112 bool posixlib
= FALSE
;
117 llassert (cstring_isDefined (sname
));
118 xname
= fileLib_withoutExtension (sname
, cstring_makeLiteralTemp (".h"));
120 DPRINTF (("Include? %s", sname
));
123 llassert (cstring_isDefined (xname
));
126 /* Posixlibs use forward slashes, so we use them here, too */
127 cstring_replaceAll (xname
, '\\', '/');
128 libname
= strrchr (xname
, '/');
129 DPRINTF (("libname: %s", libname
));
132 libname
= strrchr (xname
, CONNECTCHAR
);
147 if (mstring_equal (libname
, "varargs"))
149 fileloc tmp
= fileloc_makePreprocPrevious (g_currentloc
);
153 message ("Include file <%s.h> is inconsistent with "
154 "ANSI library (should use <stdarg.h>)",
155 cstring_fromChars (libname
)),
159 cstring_free (xname
);
163 if (context_getFlag (FLG_SKIPISOHEADERS
)
164 && context_usingAnsiLibrary ())
166 for (i
= 0; i
< NUMLIBS
; i
++)
168 if (mstring_equal (libname
, stdlibs
[i
]))
176 for (i
= 0; i
< NUMPOSIXLIBS
; i
++)
178 if (strchr (posixlibs
[i
], CONNECTCHAR
) != NULL
180 || strchr (posixlibs
[i
], ALTCONNECTCHAR
) != NULL
186 DPRINTF (("xname: %s, posix: %s", xname
, posixlibs
[i
]));
187 if ((ptr
= strstr (xname
, posixlibs
[i
])) != NULL
)
189 if (ptr
[strlen (posixlibs
[i
])] == '\0')
203 if (mstring_equal (libname
, posixlibs
[i
]))
215 if (context_usingPosixLibrary ())
217 if (context_getFlag (FLG_SKIPPOSIXHEADERS
))
219 cstring_free (xname
);
225 the returned reference is possibly null,
226 but this should not change the null state of the parameter
232 fileloc tmp
= fileloc_makePreprocPrevious (g_currentloc
);
236 message ("Include file <%s.h> matches the name of a "
237 "POSIX library, but the POSIX library is "
238 "not being used. Consider using +posixlib "
239 "or +posixstrictlib to select the POSIX "
240 "library, or -warnposix "
241 "to suppress this message.",
242 cstring_fromChars (matchname
)),
249 cstring_free (xname
);
250 /*@noaccess cstring@*/
251 /*@-nullstate@*/ /* same problem as above */
256 static void printDot (void)
258 displayScanContinue (cstring_makeLiteralTemp ("."));
262 dumpState (cstring cfname
)
265 cstring fname
= fileLib_addExtension (cfname
, cstring_makeLiteralTemp (DUMP_SUFFIX
));
267 f
= fileTable_openWriteFile (context_fileTable (), fname
);
269 displayScanOpen (message ("Dumping to %s ", fname
));
273 lldiagmsg (message ("Cannot open dump file for writing: %s", fname
));
278 ** sequence is convulted --- must call usymtab_prepareDump before
279 ** dumping ctype table to convert type uid's
285 DPRINTF (("Before prepare dump:"));
287 DPRINTF (("Preparing dump..."));
290 usymtab_prepareDump ();
293 ** Be careful, these lines must match loadLCDFile checking.
296 fprintf (f
, "%s %s\n", LIBRARY_MARKER
, cstring_toCharsSafe (fname
));
297 fprintf (f
, ";;Splint %f\n", SPLINT_LIBVERSION
);
298 fprintf (f
, ";;lib:%d\n", (int) context_getLibrary ());
299 fprintf (f
, ";;ctTable\n");
301 DPRINTF (("Dumping types..."));
306 DPRINTF (("Dumping type sets..."));
307 fprintf (f
, ";;tistable\n");
308 typeIdSet_dumpTable (f
);
311 DPRINTF (("Dumping usymtab..."));
312 fprintf (f
, ";;symTable\n");
316 DPRINTF (("Dumping modules..."));
317 fprintf (f
, ";; Modules access\n");
318 context_dumpModuleAccess (f
);
319 fprintf (f
, ";;End\n");
320 check (fileTable_closeFile (context_fileTable (), f
));
324 cstring_free (fname
);
333 cstring libname
= fileLib_addExtension (context_selectedLibrary (),
334 cstring_makeLiteralTemp (DUMP_SUFFIX
));
336 if (osd_findOnLarchPath (libname
, &fpath
) != OSD_FILEFOUND
)
338 lldiagmsg (message ("Cannot find %sstandard library: %s",
339 cstring_makeLiteralTemp
340 (context_getFlag (FLG_STRICTLIB
) ? "strict "
341 : (context_getFlag (FLG_UNIXLIB
) ? "unix " : "")),
343 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
348 stdlib
= fileTable_openReadFile (context_fileTable (), fpath
);
352 lldiagmsg (message ("Cannot read standard library: %s",
354 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
360 if (context_getFlag (FLG_WHICHLIB
))
362 char *t
= mstring_create (MAX_NAME_LENGTH
);
365 if ((t
= reader_readLine (stdlib
, t
, MAX_NAME_LENGTH
)) == NULL
)
367 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
370 if ((t
= reader_readLine (stdlib
, t
, MAX_NAME_LENGTH
)) != NULL
)
372 if (*t
== ';' && *(t
+ 1) == ';')
380 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
387 tt
= strrchr (t
, '\n');
391 lldiagmsg (message ("Standard library: %s", fpath
));
392 /* evans 2004-01-13: removed this (it is the libversion which is confusing) */
393 /* lldiagmsg (message (" (created using %s)", cstring_fromChars (t))); */
398 check (fileTable_closeFile (context_fileTable (), stdlib
));
399 stdlib
= fileTable_openReadFile (context_fileTable (), fpath
);
402 llassert (stdlib
!= NULL
);
404 fileloc_reallyFree (g_currentloc
);
405 g_currentloc
= fileloc_createLib (libname
);
407 DPRINTF (("Loading: %s", fpath
));
409 displayScanOpen (message ("loading standard library %s ", fpath
));
410 result
= loadLCDFile (stdlib
, fpath
);
413 check (fileTable_closeFile (context_fileTable (), stdlib
));
417 cstring_free (libname
);
421 /*@constant int BUFLEN;@*/
425 loadLCDFile (FILE *f
, cstring name
)
430 ** Check version. Should be >= SPLINT_LIBVERSION
433 if (reader_readLine (f
, buf
, BUFLEN
) == NULL
434 || !mstring_equalPrefix (buf
, LIBRARY_MARKER
))
436 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
437 "to continue without library.", name
));
441 if (reader_readLine (f
, buf
, BUFLEN
) != NULL
)
443 if (!mstring_equalPrefix (buf
, ";;"))
445 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
446 "to continue without library.", name
));
449 else if (mstring_equalPrefix (buf
, ";;ctTable"))
451 loadllmsg (message ("Load library %s is in obsolete Splint library format. Attempting "
452 "to continue anyway, but results may be incorrect. Rebuild "
453 "the library with this version of splint.",
460 if (sscanf (buf
, ";;Splint %f", &version
) != 1
461 && (sscanf (buf
, ";;LCLint %f", &version
) != 1))
463 loadllmsg (message ("Load library %s is not in Splint library format (missing version "
464 "number). Attempting "
465 "to continue without library.", name
));
470 if ((SPLINT_LIBVERSION
- version
) >= FLT_EPSILON
)
473 char *nl
= strchr (buf
, '\n');
477 vname
= cstring_fromChars (buf
+ 9);
479 loadllmsg (message ("Load library %s is in obsolete Splint library "
480 "format (version %f (%s), expecting version %f). Attempting "
481 "to continue anyway, but results may be incorrect. Rebuild "
482 "the library with this version of splint.",
490 if (reader_readLine (f
, buf
, BUFLEN
) == NULL
)
492 loadllmsg (message ("Load library %s is not in Splint library "
493 "format (missing library code). Attempting "
494 "to continue without library.", name
));
501 if (sscanf (buf
, ";;lib:%d", &lib
) != 1)
503 loadllmsg (message ("Load library %s is not in Splint library "
504 "format (missing library code). Attempting "
505 "to continue without library.", name
));
510 flagcode code
= (flagcode
) lib
;
512 if (flagcode_isLibraryFlag (code
))
514 if (context_doMerge ())
516 context_setLibrary (code
);
521 loadllmsg (message ("Load library %s has invalid library code (%s). "
522 "Attempting to continue without library.",
524 flagcode_unparse (code
)));
536 loadllmsg (message ("Load library %s is not in Splint library format (missing lines). "
537 "Attempting to continue without library.", name
));
544 typeIdSet_loadTable (f
);
550 context_loadModuleAccess (f
);
557 ** load state from file created by dumpState
561 loadState (cstring cfname
)
564 cstring fname
= fileLib_addExtension (cfname
, cstring_makeLiteralTemp (DUMP_SUFFIX
));
566 f
= fileTable_openReadFile (context_fileTable (), fname
);
571 llfatalerror (message ("Cannot open dump file for loading: %s",
576 fileloc_reallyFree (g_currentloc
);
577 g_currentloc
= fileloc_createLib (cfname
);
579 if (!loadLCDFile (f
, cfname
))
581 if (!loadStandardState ())
587 check (fileTable_closeFile (context_fileTable (), f
));
590 /* usymtab_printAll (); */
591 cstring_free (fname
);