Some consistency changes to library & headers flags.
[splint-patched.git] / src / library.c
blob445601c52661d18b48d53ffe38baa54d16bb0775
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
5 **
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.
10 **
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.
15 **
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
25 ** library.c
27 ** routines for loading and creating library files
29 ** this is a brute-force implementation, a more efficient
30 ** representation should be designed.
34 # include <float.h>
36 # include "splintMacros.nf"
37 # include "basic.h"
38 # include "osd.h"
40 # include "libversion.h"
41 # include "library.h"
43 static void printDot (void)
45 displayScanContinue (cstring_makeLiteralTemp ("."));
48 static bool loadLCDFile (FILE * p_f, cstring p_name);
50 void
51 dumpState (cstring cfname)
53 FILE *f;
54 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
56 f = fileTable_openWriteFile (context_fileTable (), fname);
58 displayScanOpen (message ("Dumping to %s ", fname));
60 if (f == NULL)
62 lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
64 else
67 ** sequence is convulted --- must call usymtab_prepareDump before
68 ** dumping ctype table to convert type uid's
71 printDot ();
73 # if 0
74 DPRINTF (("Before prepare dump:"));
75 ctype_printTable ();
76 DPRINTF (("Preparing dump..."));
77 # endif
79 usymtab_prepareDump ();
82 ** Be careful, these lines must match loadLCDFile checking.
85 fprintf (f, "%s %s\n", LIBRARY_MARKER, cstring_toCharsSafe (fname));
86 fprintf (f, ";;Splint %f\n", LIBVERSION);
87 fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
88 fprintf (f, ";;ctTable\n");
90 DPRINTF (("Dumping types..."));
91 printDot ();
92 ctype_dumpTable (f);
93 printDot ();
95 DPRINTF (("Dumping type sets..."));
96 fprintf (f, ";;tistable\n");
97 typeIdSet_dumpTable (f);
98 printDot ();
100 DPRINTF (("Dumping usymtab..."));
101 fprintf (f, ";;symTable\n");
102 usymtab_dump (f);
103 printDot ();
105 DPRINTF (("Dumping modules..."));
106 fprintf (f, ";; Modules access\n");
107 context_dumpModuleAccess (f);
108 fprintf (f, ";;End\n");
109 check (fileTable_closeFile (context_fileTable (), f));
112 displayScanClose ();
113 cstring_free (fname);
116 bool
117 loadStandardState (void)
119 cstring fpath;
120 FILE *stdlib;
121 bool result;
122 cstring libname = fileLib_addExtension (context_selectedLibrary (),
123 cstring_makeLiteralTemp (DUMP_SUFFIX));
125 if (osd_getPath (context_getLarchPath (), libname, &fpath) != OSD_FILEFOUND)
127 lldiagmsg (message ("Cannot find standard library: %s", libname));
128 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
129 result = FALSE;
131 else
133 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
135 if (stdlib == NULL)
137 lldiagmsg (message ("Cannot read standard library: %s",
138 fpath));
139 lldiagmsg (cstring_makeLiteral (" Check LARCH_PATH environment variable."));
141 result = FALSE;
143 else
145 if (context_getFlag (FLG_WHICHLIB))
147 char *t = mstring_create (MAX_NAME_LENGTH);
148 char *ot = t;
150 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
152 llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
155 if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
157 if (*t == ';' && *(t + 1) == ';')
159 t += 2;
163 if (t == NULL)
165 lldiagmsg (message ("Standard library: %s <cannot read creation information>",
166 fpath));
168 else
170 char *tt;
172 tt = strrchr (t, '\n');
173 if (tt != NULL)
174 *tt = '\0';
176 lldiagmsg (message ("Standard library: %s", fpath));
177 /* evans 2004-01-13: removed this (it is the libversion which is confusing) */
178 /* lldiagmsg (message (" (created using %s)", cstring_fromChars (t))); */
181 sfree (ot);
183 check (fileTable_closeFile (context_fileTable (), stdlib));
184 stdlib = fileTable_openReadFile (context_fileTable (), fpath);
187 llassert (stdlib != NULL);
189 fileloc_reallyFree (g_currentloc);
190 g_currentloc = fileloc_createLib (libname, TRUE);
192 DPRINTF (("Loading: %s", fpath));
194 displayScanOpen (message ("loading standard library %s ", fpath));
195 result = loadLCDFile (stdlib, fpath);
196 displayScanClose ();
198 check (fileTable_closeFile (context_fileTable (), stdlib));
202 cstring_free (libname);
203 return result;
206 /*@constant int BUFLEN;@*/
207 # define BUFLEN 128
209 static bool
210 loadLCDFile (FILE *f, cstring name)
212 char buf[BUFLEN];
214 if (reader_readLine (f, buf, BUFLEN) == NULL
215 || !mstring_equalPrefix (buf, LIBRARY_MARKER))
217 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
218 "to continue without library.", name));
219 return FALSE;
222 if (reader_readLine (f, buf, BUFLEN) != NULL)
224 if (!mstring_equalPrefix (buf, ";;"))
226 loadllmsg (message ("Load library %s is not in Splint library format. Attempting "
227 "to continue without library.", name));
228 return FALSE;
230 else if (mstring_equalPrefix (buf, ";;ctTable"))
232 loadllmsg (message ("Load library %s is in obsolete Splint library format. Attempting "
233 "to continue anyway, but results may be incorrect. Rebuild "
234 "the library with this version of splint.",
235 name));
237 else
239 float version = 0.0;
241 if (sscanf (buf, ";;Splint %f", &version) != 1
242 && (sscanf (buf, ";;LCLint %f", &version) != 1))
244 loadllmsg (message ("Load library %s is not in Splint library format (missing version "
245 "number). Attempting to continue without library.", name));
246 return FALSE;
248 else
251 ** Check version. Should be >= LIBVERSION
254 if ((LIBVERSION - version) >= FLT_EPSILON)
256 cstring vname;
257 char *nl = strchr (buf, '\n');
259 *nl = '\0';
261 vname = cstring_fromChars (buf + 9);
263 loadllmsg (message ("Load library %s is in obsolete Splint library "
264 "format (version %f (%s), expecting version %f). Attempting "
265 "to continue anyway, but results may be incorrect. Rebuild "
266 "the library with this version of splint.",
267 name,
268 version,
269 vname,
270 LIBVERSION));
272 else
274 if (reader_readLine (f, buf, BUFLEN) == NULL)
276 loadllmsg (message ("Load library %s is not in Splint library "
277 "format (missing library code). Attempting "
278 "to continue without library.", name));
279 return FALSE;
281 else
283 int lib;
285 if (sscanf (buf, ";;lib:%d", &lib) != 1)
287 loadllmsg (message ("Load library %s is not in Splint library "
288 "format (missing library code). Attempting "
289 "to continue without library.", name));
290 return FALSE;
292 else
294 flagcode code = (flagcode) lib;
296 if (flagcode_isLibraryFlag (code))
298 if (context_doMerge ())
300 context_setLibrary (code);
303 else
305 loadllmsg (message ("Load library %s has invalid library code (%s). "
306 "Attempting to continue without library.",
307 name,
308 flagcode_unparse (code)));
310 return FALSE;
318 else
320 loadllmsg (message ("Load library %s is not in Splint library format (missing lines). "
321 "Attempting to continue without library.", name));
322 return FALSE;
325 ctype_loadTable (f);
326 printDot ();
328 typeIdSet_loadTable (f);
329 printDot ();
331 usymtab_load (f);
332 printDot ();
334 context_loadModuleAccess (f);
335 printDot ();
337 return TRUE;
341 ** load state from file created by dumpState
344 void
345 loadState (cstring cfname)
347 FILE *f;
348 cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
350 f = fileTable_openReadFile (context_fileTable (), fname);
352 if (f == NULL)
354 displayScanClose ();
355 llfatalerror (message ("Cannot open dump file for loading: %s",
356 fname));
358 else
360 fileloc_reallyFree (g_currentloc);
361 g_currentloc = fileloc_createLib (cfname, FALSE);
363 if (!loadLCDFile (f, cfname))
365 if (!loadStandardState ())
367 ctype_initTable ();
371 check (fileTable_closeFile (context_fileTable (), f));
374 /* usymtab_printAll (); */
375 cstring_free (fname);