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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
27 ** Main module for Splint annotation-assisted program checker
33 ** Ensure that WIN32 and _WIN32 are both defined or both undefined.
38 # error "Inconsistent definitions."
42 # error "Inconsistent definitions."
51 # include "lclintMacros.nf"
58 # include "scanline.h"
59 # include "lclscanline.h"
60 # include "lclsyntable.h"
61 # include "lcltokentable.h"
62 # include "lslparse.h"
64 # include "syntable.h"
65 # include "tokentable.h"
73 # include "fileIdList.h"
75 # include "cgrammar.h"
80 extern /*@external@*/ int yydebug;
82 static void printMail (void);
83 static void printMaintainer (void);
84 static void printReferences (void);
85 static void printFlags (void);
86 static void printAnnotations (void);
87 static void printParseErrors (void);
88 static void printComments (void);
89 static void describePrefixCodes (void);
90 static void cleanupFiles (void);
91 static void showHelp (void);
92 static void interrupt (int p_i);
94 static bool readOptionsFile (cstring p_fname,
95 cstringSList *p_passThroughArgs,
97 /*@modifies fileSystem, internalState, *p_passThroughArgs@*/ ;
99 static void loadrc (FILE *p_rcfile, cstringSList *p_passThroughArgs)
100 /*@modifies *p_passThroughArgs, p_rcfile@*/
101 /*@ensures closed p_rcfile@*/ ;
103 static void describeVars (void);
104 static bool specialFlagsHelp (char *p_next);
105 static bool hasShownHerald = FALSE;
106 static char *specFullName (char *p_specfile, /*@out@*/ char **p_inpath)
107 /*@modifies *p_inpath@*/ ;
109 static bool anylcl = FALSE;
110 static clock_t inittime;
112 static /*@only@*/ /*@null@*/ inputStream initFile = inputStream_undefined;
114 static fileIdList preprocessFiles (fileIdList, bool)
115 /*@modifies fileSystem@*/ ;
120 void lslCleanup (void)
121 /*@globals killed g_symtab@*/
122 /*@modifies internalState, g_symtab@*/
125 ** Cleanup all the LCL/LSL.
128 static bool didCleanup = FALSE;
130 llassert (!didCleanup);
135 lsymbol_destroyMod ();
136 LCLSynTableCleanup ();
137 LCLTokenTableCleanup ();
138 LCLScanLineCleanup ();
141 /* clean up LSL parsing */
144 ltokenTableCleanup ();
148 symtable_free (g_symtab);
154 /*@globals undef g_symtab; @*/
155 /*@modifies g_symtab, internalState, fileSystem; @*/
158 ** Open init file provided by user, or use the default LCL init file
161 cstring larchpath = context_getLarchPath ();
162 inputStream LSLinitFile = inputStream_undefined;
166 if (inputStream_isUndefined (initFile))
168 initFile = inputStream_create (cstring_makeLiteral (INITFILENAME),
169 cstring_makeLiteralTemp (LCLINIT_SUFFIX),
172 if (!inputStream_getPath (larchpath, initFile))
174 lldiagmsg (message ("Continuing without LCL init file: %s",
175 inputStream_fileName (initFile)));
179 if (!inputStream_open (initFile))
181 lldiagmsg (message ("Continuing without LCL init file: %s",
182 inputStream_fileName (initFile)));
188 if (!inputStream_open (initFile))
190 lldiagmsg (message ("Continuing without LCL init file: %s",
191 inputStream_fileName (initFile)));
195 /* Initialize checker */
203 LCLTokenTableInit ();
215 /* need this to initialize LCL checker */
217 llassert (inputStream_isDefined (initFile));
218 if (inputStream_isOpen (initFile))
222 LCLScanReset (initFile);
223 LCLProcessInitFileInit ();
224 LCLProcessInitFileReset ();
227 LCLProcessInitFile ();
228 LCLProcessInitFileCleanup ();
231 check (inputStream_close (initFile));
234 /* Initialize LSL init files, for parsing LSL signatures from LSL */
236 LSLinitFile = inputStream_create (cstring_makeLiteral ("lslinit.lsi"),
237 cstring_makeLiteralTemp (".lsi"),
240 if (!inputStream_getPath (larchpath, LSLinitFile))
242 lldiagmsg (message ("Continuing without LSL init file: %s",
243 inputStream_fileName (LSLinitFile)));
247 if (!inputStream_open (LSLinitFile))
249 lldiagmsg (message ("Continuing without LSL init file: %s",
250 inputStream_fileName (LSLinitFile)));
266 if (inputStream_isOpen (LSLinitFile))
269 LSLScanReset (LSLinitFile);
270 LSLProcessInitFileInit ();
272 LSLProcessInitFile ();
274 check (inputStream_close (LSLinitFile));
277 inputStream_free (LSLinitFile);
282 (cstring_makeLiteral ("LSL init file error. Attempting to continue."));
286 g_symtab = symtable_new ();
289 ** sort_init must come after symtab has been initialized
298 ** Equivalent to importing old spec_csupport.lcl
299 ** define immutable LCL type "bool" and bool constants TRUE and FALSE
300 ** and initialized them to be equal to LSL's "true" and "false".
302 ** Reads in CTrait.syms (derived from CTrait.lsl) on LARCH_PATH.
306 LCLReportEolTokens (FALSE);
310 lslProcess (fileIdList lclfiles)
311 /*@globals undef g_currentSpec, undef g_currentSpecName, g_currentloc,
312 undef killed g_symtab; @*/
313 /*@modifies g_currentSpec, g_currentSpecName, g_currentloc, internalState, fileSystem; @*/
316 bool parser_status = FALSE;
317 bool overallStatus = FALSE;
321 context_resetSpecLines ();
323 fileIdList_elements (lclfiles, fid)
325 cstring actualName = cstring_undefined;
326 cstring fname = fileName (fid);
328 if (osd_getPath (cstring_fromChars (g_localSpecPath),
329 fname, &actualName) == OSD_FILENOTFOUND)
331 if (mstring_equal (g_localSpecPath, "."))
333 lldiagmsg (message ("Spec file not found: %q", osd_outputPath (fname)));
337 lldiagmsg (message ("Spec file not found: %q (on %s)",
338 osd_outputPath (fname),
339 cstring_fromChars (g_localSpecPath)));
344 inputStream specFile;
346 char *namePtr = actualName;
348 while (*namePtr == '.' && *(namePtr + 1) == CONNECTCHAR)
352 /*@noaccess cstring@*/
354 g_currentSpec = cstring_fromCharsNew (namePtr);
356 specFile = inputStream_create (cstring_copy (g_currentSpec),
357 LCL_EXTENSION, TRUE);
359 llassert (inputStream_isDefined (specFile));
361 g_currentSpecName = specFullName
362 (cstring_toCharsSafe (g_currentSpec),
367 if (context_getFlag (FLG_SHOWSCAN))
369 lldiagmsg (message ("< reading spec %s >", g_currentSpec));
372 /* Open source file */
374 if (!inputStream_open (specFile))
376 lldiagmsg (message ("Cannot open file: %q",
377 osd_outputPath (inputStream_fileName (specFile))));
378 inputStream_free (specFile);
382 scopeInfo dummy_scope = (scopeInfo) dmalloc (sizeof (*dummy_scope));
383 dummy_scope->kind = SPE_INVALID;
386 LCLScanReset (specFile);
389 ** Minor hacks to allow more than one LCL file to
390 ** be scanned, while keeping initializations
393 symtable_enterScope (g_symtab, dummy_scope);
394 resetImports (cstring_fromChars (g_currentSpecName));
395 context_enterLCLfile ();
396 (void) lclHadNewError ();
398 parser_status = (ylparse () != 0);
399 context_exitLCLfile ();
401 overallStatus = parser_status || lclHadNewError ();
403 if (context_getFlag (FLG_DOLCS))
407 outputLCSFile (path, "%FAILED Output from ",
412 outputLCSFile (path, "%PASSED Output from ",
417 (void) inputStream_close (specFile);
418 inputStream_free (specFile);
420 symtable_exitScope (g_symtab);
423 cstring_free (actualName);
424 } end_fileIdList_elements;
426 /* Can cleanup lsl stuff right away */
430 g_currentSpec = cstring_undefined;
431 g_currentSpecName = NULL;
435 static void handlePassThroughFlag (char *arg)
438 char *quotechar = strchr (curarg, '\"');
441 char *freearg = NULL;
443 while (quotechar != NULL)
445 if (*(quotechar - 1) == '\\')
447 char *tp = quotechar - 2;
458 curarg = quotechar + 1;
459 quotechar = strchr (curarg, '\"');
464 llassert (quotechar != NULL);
466 offset = (quotechar - arg) + 2;
470 arg = cstring_toCharsSafe
471 (message ("%s\"\'%s",
472 cstring_fromChars (arg),
473 cstring_fromChars (quotechar + 1)));
479 arg = cstring_toCharsSafe
480 (message ("%s\'\"%s",
481 cstring_fromChars (arg),
482 cstring_fromChars (quotechar + 1)));
487 curarg = arg + offset;
488 quotechar = strchr (curarg, '\"');
494 voptgenerror (FLG_BADFLAG,
495 message ("Unclosed quote in flag: %s",
496 cstring_fromChars (arg)),
505 ** If the value is surrounded by single quotes ('), remove
506 ** them. This is an artifact of UNIX command line?
509 def = osd_fixDefine (cstring_fromChars (arg + 1));
510 DPRINTF (("Do define: %s", def));
512 DPRINTF (("After define"));
514 } else if (arg[0] == 'U') {
515 cppDoUndefine (cstring_fromChars (arg + 1));
524 void showHerald (void)
526 if (hasShownHerald || context_getFlag (FLG_QUIET)) return;
530 fprintf (g_msgstream, "%s\n\n", SPLINT_VERSION);
531 hasShownHerald = TRUE;
536 static cstring findLarchPathFile (/*@temp@*/ cstring s)
541 status = osd_getPath (context_getLarchPath (), s, &pathName);
543 if (status == OSD_FILEFOUND)
547 else if (status == OSD_FILENOTFOUND)
550 lldiagmsg (message ("Cannot find file on LARCHPATH: %s", s));
552 else if (status == OSD_PATHTOOLONG)
554 /* Directory and filename are too long. Report error. */
555 llbuglit ("soure_getPath: Filename plus directory from search path too long");
562 return cstring_undefined;
565 static void addLarchPathFile (fileIdList files, /*@temp@*/ cstring s)
567 cstring pathName = findLarchPathFile (s);
569 if (cstring_isDefined (pathName))
571 if (fileTable_exists (context_fileTable (), pathName))
574 lldiagmsg (message ("File listed multiple times: %s", pathName));
575 cstring_free (pathName);
579 fileIdList_add (files, fileTable_addFileOnly (context_fileTable (), pathName));
584 static void addFile (fileIdList files, /*@only@*/ cstring s)
586 if (fileTable_exists (context_fileTable (), s))
589 lldiagmsg (message ("File listed multiple times: %s", s));
594 fileIdList_add (files, fileTable_addFileOnly (context_fileTable (), s));
598 static void addXHFile (fileIdList files, /*@temp@*/ cstring s)
600 cstring pathName = findLarchPathFile (s);
602 if (cstring_isDefined (pathName))
604 if (fileTable_exists (context_fileTable (), pathName))
607 lldiagmsg (message ("File listed multiple times: %s", s));
611 fileIdList_add (files, fileTable_addXHFile (context_fileTable (), pathName));
615 cstring_free (pathName);
619 ** Disable MSVC++ warning about return value. Methinks humbly lclint control
620 ** comments are a mite more legible.
624 # pragma warning (disable:4035)
627 int main (int argc, char *argv[])
629 /*@globals killed undef g_currentloc,
633 /*@modifies g_currentloc, fileSystem,
637 /*@globals killed undef g_currentloc,
638 killed undef initFile,
639 killed g_localSpecPath,
640 killed undef g_currentSpec,
641 killed undef g_currentSpecName,
645 /*@modifies g_currentloc, initFile,
646 g_localSpecPath, g_currentSpec, g_currentSpecName, fileSystem,
651 bool first_time = TRUE;
652 bool showhelp = FALSE;
655 inputStream sourceFile = inputStream_undefined;
657 fileIdList dercfiles;
658 cstringSList fl = cstringSList_undefined;
659 cstringSList passThroughArgs = cstringSList_undefined;
660 fileIdList cfiles, xfiles, lclfiles, mtfiles;
661 clock_t before, lcltime, libtime, pptime, cptime, rstime;
665 _wildcard (&argc, &argv);
668 g_msgstream = stdout;
670 (void) signal (SIGINT, interrupt);
671 (void) signal (SIGSEGV, interrupt);
673 cfiles = fileIdList_create ();
674 xfiles = fileIdList_create ();
675 lclfiles = fileIdList_create ();
676 mtfiles = fileIdList_create ();
679 clabstract_initMod ();
680 typeIdSet_initMod ();
681 cppReader_initMod ();
686 g_currentloc = fileloc_createBuiltin ();
691 context_setInCommandLine ();
703 ** Add include directories from environment.
707 cstring incval = cstring_copy (osd_getEnvironmentVariable (INCLUDEPATH_VAR));
708 cstring oincval = incval;
710 if (cstring_isDefined (incval))
713 ** Each directory on the include path is a system include directory.
716 DPRINTF (("include: %s", incval));
717 context_setString (FLG_SYSTEMDIRS, cstring_copy (incval));
719 while (cstring_isDefined (incval))
722 char *nextsep = strchr (incval, PATH_SEPARATOR);
728 dir = cstring_copy (incval);
730 if (cstring_length (dir) == 0
731 || !isalpha ((int) cstring_firstChar (dir)))
734 ** win32 environment values can have special values,
740 cppAddIncludeDir (dir);
743 *nextsep = PATH_SEPARATOR;
744 incval = cstring_fromChars (nextsep + 1);
752 /*@noaccess cstring@*/
755 else /* 2001-09-09: herbert */
757 /* Put C_INCLUDE_PATH directories in sysdirs */
758 cstring cincval = osd_getEnvironmentVariable (cstring_makeLiteralTemp ("C_INCLUDE_PATH"));
759 if (cstring_isDefined (cincval))
761 context_setString (FLG_SYSTEMDIRS, cstring_copy (cincval));
766 cstring_free (oincval);
770 ** check RCFILE for default flags
774 cstring home = osd_getHomeDir ();
775 cstring fname = cstring_undefined;
776 bool defaultf = TRUE;
779 for (i = 1; i < argc; i++)
784 if (*thisarg == '-' || *thisarg == '+')
786 bool set = (*thisarg == '+');
792 ** Don't report warnings this time
795 opt = flags_identifyFlagQuiet (cstring_fromChars (thisarg));
801 else if (opt == FLG_SHOWSCAN || opt == FLG_WARNRC)
804 ** Need to set it immediately, so rc file scan is displayed
807 context_userSetFlag (opt, set);
809 else if (opt == FLG_OPTF)
814 fname = cstring_fromChars (argv[i]);
815 (void) readOptionsFile (fname, &passThroughArgs, TRUE);
819 (cstring_makeLiteral ("Flag f to select options file "
820 "requires an argument"));
824 ; /* wait to process later */
831 if (!nof && defaultf)
834 ** No explicit rc file, first try reading ~/.splintrc
837 if (cstring_isUndefined (fname))
839 if (!cstring_isEmpty (home))
841 bool readhomerc, readaltrc;
842 cstring homename, altname;
844 homename = message ("%s%h%s", home, CONNECTCHAR,
845 cstring_fromChars (RCFILE));
846 readhomerc = readOptionsFile (homename, &passThroughArgs, FALSE);
849 ** Try ~/.lclintrc also for historical accuracy
852 altname = message ("%s%h%s", home, CONNECTCHAR,
853 cstring_fromChars (ALTRCFILE));
854 readaltrc = readOptionsFile (altname, &passThroughArgs, FALSE);
856 if (readhomerc && readaltrc)
861 message ("Found both %s and %s files. Using both files, "
862 "but recommend using only %s to avoid confusion.",
863 homename, altname, homename),
867 cstring_free (homename);
868 cstring_free (altname);
873 ** Next, read .splintrc in the current working directory
877 cstring rcname = message ("%s%s",osd_getCurrentDirectory (), cstring_fromChars (RCFILE));
878 cstring altname = message ("%s%s",osd_getCurrentDirectory (), cstring_fromChars (ALTRCFILE));
879 bool readrc, readaltrc;
881 readrc = readOptionsFile (rcname, &passThroughArgs, FALSE);
882 readaltrc = readOptionsFile (altname, &passThroughArgs, FALSE);
884 if (readrc && readaltrc)
886 voptgenerror (FLG_WARNRC,
887 message ("Found both %s and %s files. Using both files, "
888 "but recommend using only %s to avoid confusion.",
889 rcname, altname, rcname),
894 cstring_free (rcname);
895 cstring_free (altname);
902 for (i = 1; i < argc; i++)
918 if (*thisarg == '-' || *thisarg == '+')
920 thisarg++; /* skip '-' */
922 if (mstring_equal (thisarg, "modes"))
924 llmsg (describeModes ());
926 else if (mstring_equal (thisarg, "vars")
927 || mstring_equal (thisarg, "env"))
931 else if (mstring_equal (thisarg, "annotations"))
935 else if (mstring_equal (thisarg, "parseerrors"))
939 else if (mstring_equal (thisarg, "comments"))
943 else if (mstring_equal (thisarg, "prefixcodes"))
945 describePrefixCodes ();
947 else if (mstring_equal (thisarg, "references")
948 || mstring_equal (thisarg, "refs"))
952 else if (mstring_equal (thisarg, "mail"))
956 else if (mstring_equal (thisarg, "maintainer")
957 || mstring_equal (thisarg, "version"))
961 else if (mstring_equal (thisarg, "flags"))
965 char *next = argv[i + 1];
967 if (specialFlagsHelp (next))
973 flagkind k = identifyCategory (cstring_fromChars (next));
989 cstring s = describeFlag (cstring_fromChars (thisarg));
991 if (cstring_isDefined (s))
999 if (*thisarg == '-' || *thisarg == '+')
1001 bool set = (*thisarg == '+');
1004 thisarg++; /* skip '-' */
1005 flagname = cstring_fromChars (thisarg);
1007 DPRINTF (("Flag: %s", flagname));
1008 opt = flags_identifyFlag (flagname);
1009 DPRINTF (("Flag: %s", flagcode_unparse (opt)));
1011 if (flagcode_isSkip (opt) || opt == FLG_SHOWSCAN || opt == FLG_WARNRC)
1013 /* showscan already processed */
1014 DPRINTF (("Skipping!"));
1016 else if (flagcode_isInvalid (opt))
1018 DPRINTF (("Invalid: %s", flagname));
1020 if (isMode (flagname))
1022 context_setMode (flagname);
1026 DPRINTF (("Error!"));
1027 voptgenerror (FLG_BADFLAG,
1028 message ("Unrecognized option: %s",
1029 cstring_fromChars (thisarg)),
1035 context_userSetFlag (opt, set);
1037 if (flagcode_hasArgument (opt))
1039 if (opt == FLG_HELP)
1043 else if (flagcode_isPassThrough (opt)) /* -D or -U */
1045 passThroughArgs = cstringSList_add
1046 (passThroughArgs, cstring_fromChars (thisarg));
1048 else if (flagcode_hasNumber (opt))
1052 setValueFlag (opt, cstring_fromChars (argv[i]));
1058 ("Flag %s must be followed by a number",
1059 flagcode_unparse (opt)));
1062 else if (flagcode_hasChar (opt))
1066 setValueFlag (opt, cstring_fromChars (argv[i]));
1072 ("Flag %s must be followed by a character",
1073 flagcode_unparse (opt)));
1076 else if (opt == FLG_INCLUDEPATH || opt == FLG_SPECPATH)
1078 cstring dir = cstring_suffix (cstring_fromChars (thisarg), 1); /* skip over I */
1082 case FLG_INCLUDEPATH:
1083 cppAddIncludeDir (dir);
1084 /*@switchbreak@*/ break;
1087 g_localSpecPath = cstring_toCharsSafe
1089 cstring_fromChars (g_localSpecPath),
1093 /*@switchbreak@*/ break;
1097 else if (flagcode_hasString (opt)
1098 || opt == FLG_INIT || opt == FLG_OPTF)
1102 cstring arg = cstring_fromChars (argv[i]);
1104 if (opt == FLG_OPTF)
1106 ; /* -f already processed */
1108 else if (opt == FLG_INIT)
1111 initFile = inputStream_create
1113 cstring_makeLiteralTemp (LCLINIT_SUFFIX),
1120 DPRINTF (("String flag: %s / %s",
1121 flagcode_unparse (opt), arg));
1122 if (opt == FLG_MTSFILE)
1125 ** arg identifies mts files
1127 cstring tmp = message ("%s%s", arg, MTS_EXTENSION);
1128 addLarchPathFile (mtfiles, tmp);
1130 tmp = message ("%s%s", arg, XH_EXTENSION);
1131 addXHFile (xfiles, tmp);
1136 setStringFlag (opt, arg);
1144 ("Flag %s must be followed by a string",
1145 flagcode_unparse (opt)));
1155 else /* its a filename */
1157 DPRINTF (("Adding filename: %s", thisarg));
1158 fl = cstringSList_add (fl, cstring_fromChars (thisarg));
1167 ** create lists of C and LCL files
1170 cstringSList_elements (fl, current)
1172 cstring ext = fileLib_getExtension (current);
1174 if (cstring_isUndefined (ext))
1176 /* no extension --- both C and LCL with default extensions */
1178 addFile (cfiles, message ("%s%s", current, C_EXTENSION));
1179 addFile (lclfiles, message ("%s%s", current, LCL_EXTENSION));
1181 else if (cstring_equal (ext, XH_EXTENSION))
1183 addXHFile (xfiles, current);
1185 else if (cstring_equal (ext, PP_EXTENSION))
1187 if (!context_getFlag (FLG_NOPP))
1190 (FLG_FILEEXTENSIONS,
1191 message ("File extension %s used without +nopp flag (will be processed as C source code): %s",
1196 addFile (cfiles, cstring_copy (current));
1198 else if (cstring_equal (ext, LCL_EXTENSION))
1200 addFile (lclfiles, cstring_copy (current));
1202 else if (fileLib_isCExtension (ext))
1204 addFile (cfiles, cstring_copy (current));
1206 else if (cstring_equal (ext, MTS_EXTENSION))
1208 addLarchPathFile (mtfiles, current);
1213 (FLG_FILEEXTENSIONS,
1214 message ("Unrecognized file extension: %s (assuming %s is C source code)",
1218 addFile (cfiles, cstring_copy (current));
1220 } end_cstringSList_elements;
1228 fprintf (g_msgstream, "\n");
1230 fileIdList_free (cfiles);
1231 fileIdList_free (xfiles);
1232 fileIdList_free (lclfiles);
1241 inittime = clock ();
1243 context_resetErrors ();
1244 context_clearInCommandLine ();
1246 anylcl = !fileIdList_isEmpty (lclfiles);
1248 if (context_doMerge ())
1250 cstring m = context_getMerge ();
1252 if (context_getFlag (FLG_SHOWSCAN))
1254 fprintf (g_msgstream, "< loading %s ", cstring_toCharsSafe (m));
1259 if (context_getFlag (FLG_SHOWSCAN))
1261 fprintf (g_msgstream, " >\n");
1264 if (!usymtab_existsType (context_getBoolName ()))
1266 usymtab_initBool ();
1271 if (!context_getFlag (FLG_NOLIB) && loadStandardState ())
1280 /* setup bool type and constants */
1281 usymtab_initBool ();
1284 fileloc_free (g_currentloc);
1285 g_currentloc = fileloc_createBuiltin ();
1288 ** Read metastate files (must happen before loading libraries)
1291 fileIdList_elements (mtfiles, mtfile)
1293 context_setFileId (mtfile);
1295 if (context_getFlag (FLG_SHOWSCAN))
1297 lldiagmsg (message ("< processing %s >", rootFileName (mtfile)));
1300 mtreader_readFile (cstring_copy (fileName (mtfile)));
1301 } end_fileIdList_elements;
1308 llfatalerror (cstring_makeLiteral ("This version of Splint does not handle LCL files."));
1310 lslProcess (lclfiles);
1314 usymtab_initGlobalMarker ();
1319 ** call the pre-preprocessor and /lib/cpp to generate appropriate
1324 context_setInCommandLine ();
1326 DPRINTF (("Pass through: %s", cstringSList_unparse (passThroughArgs)));
1328 cstringSList_elements (passThroughArgs, thisarg) {
1329 handlePassThroughFlag (cstring_toCharsSafe (thisarg));
1330 } end_cstringSList_elements;
1332 cstringSList_free (passThroughArgs);
1336 DPRINTF (("Initializing cpp reader!"));
1337 cppReader_initialize ();
1338 cppReader_saveDefinitions ();
1340 context_clearInCommandLine ();
1342 if (!context_getFlag (FLG_NOPP))
1348 if (context_getFlag (FLG_SHOWSCAN))
1350 fprintf (stderr, "< preprocessing");
1355 context_setPreprocessing ();
1356 dercfiles = preprocessFiles (xfiles, TRUE);
1357 tfiles = preprocessFiles (cfiles, FALSE);
1358 dercfiles = fileIdList_append (dercfiles, tfiles);
1359 fileIdList_free (tfiles);
1361 context_clearPreprocessing ();
1363 fileIdList_free (cfiles);
1365 if (context_getFlag (FLG_SHOWSCAN))
1367 fprintf (stderr, " >\n");
1375 dercfiles = fileIdList_append (cfiles, xfiles);
1380 ** now, check all the corresponding C files
1382 ** (for now these are just <file>.c, but after pre-processing
1383 ** will be <tmpprefix>.<file>.c)
1388 int nfiles = /*@-unrecog@*/ _fcloseall (); /*@=unrecog@*/
1392 llbug (message ("Files unclosed: %d", nfiles));
1397 DPRINTF (("Initializing..."));
1399 exprNode_initMod ();
1401 DPRINTF (("Okay..."));
1403 fileIdList_elements (dercfiles, fid)
1405 sourceFile = inputStream_create (cstring_copy (fileName (fid)), C_EXTENSION, TRUE);
1406 context_setFileId (fid);
1408 /* Open source file */
1410 if (inputStream_isUndefined (sourceFile) || (!inputStream_open (sourceFile)))
1412 /* previously, this was ignored ?! */
1413 llbug (message ("Could not open temp file: %s", fileName (fid)));
1417 yyin = inputStream_getFile (sourceFile); /*< shared <- only */
1419 llassert (yyin != NULL);
1421 if (context_getFlag (FLG_SHOWSCAN))
1423 lldiagmsg (message ("< checking %q >", osd_outputPath (rootFileName (fid))));
1427 ** Every time, except the first time, through the loop,
1428 ** need to call yyrestart to clean up the parse buffer.
1433 (void) yyrestart (yyin);
1440 DPRINTF (("Entering..."));
1441 context_enterFile ();
1443 context_exitCFile ();
1445 (void) inputStream_close (sourceFile);
1447 } end_fileIdList_elements;
1451 /* process any leftover macros */
1453 context_processAllMacros ();
1455 /* check everything that was specified was defined */
1457 /* don't check if no c files were processed ?
1458 ** is this correct behaviour?
1461 if (context_getFlag (FLG_SHOWSCAN))
1463 lldiagmsg (cstring_makeLiteral ("< global checks >"));
1468 if (context_getLinesProcessed () > 0)
1470 usymtab_allDefined ();
1473 if (context_maybeSet (FLG_TOPUNUSED))
1475 uentry ue = usymtab_lookupSafe (cstring_makeLiteralTemp ("main"));
1477 if (uentry_isValid (ue))
1479 uentry_setUsed (ue, fileloc_observeBuiltin ());
1485 if (context_maybeSet (FLG_EXPORTLOCAL))
1487 usymtab_exportLocal ();
1491 if (context_maybeSet (FLG_EXPORTHEADER))
1493 usymtab_exportHeader ();
1496 if (context_getFlag (FLG_SHOWUSES))
1498 usymtab_displayAllUses ();
1501 context_checkSuppressCounts ();
1503 if (context_doDump ())
1505 cstring dump = context_getDump ();
1516 if (context_getFlag (FLG_SHOWSUMMARY))
1523 bool isQuiet = context_getFlag (FLG_QUIET);
1524 cstring specErrors = cstring_undefined;
1526 int nspecErrors = lclNumberErrors ();
1531 if (context_neednl ())
1532 fprintf (g_msgstream, "\n");
1535 if (nspecErrors > 0)
1537 if (nspecErrors == context_getLCLExpect ())
1540 message ("%d spec warning%&, as expected\n ",
1545 if (context_getLCLExpect () > 0)
1548 message ("%d spec warning%&, expected %d\n ",
1550 (int) context_getLCLExpect ());
1554 specErrors = message ("%d spec warning%&\n ",
1562 if (context_getLCLExpect () > 0)
1564 specErrors = message ("No spec warnings, expected %d\n ",
1565 (int) context_getLCLExpect ());
1571 if (context_anyErrors ())
1573 if (context_numErrors () == context_getExpect ())
1576 llmsg (message ("Finished checking --- "
1577 "%s%d code warning%&, as expected",
1578 specErrors, context_numErrors ()));
1583 if (context_getExpect () > 0)
1587 ("Finished checking --- "
1588 "%s%d code warning%&, expected %d",
1589 specErrors, context_numErrors (),
1590 (int) context_getExpect ()));
1600 llmsg (message ("Finished checking --- "
1601 "%s%d code warning%&",
1602 specErrors, context_numErrors ()));
1611 if (context_getExpect () > 0)
1615 ("Finished checking --- "
1616 "%sno code warnings, expected %d",
1618 (int) context_getExpect ()));
1625 if (context_getLinesProcessed () > 0)
1627 if (cstring_isEmpty (specErrors))
1631 llmsg (message ("Finished checking --- no warnings"));
1638 llmsg (message ("Finished checking --- %sno code warnings",
1646 llmsg (message ("Finished checking --- %sno code processed",
1653 cstring_free (specErrors);
1656 if (context_getFlag (FLG_STATS))
1658 clock_t ttime = clock () - before;
1659 int specLines = context_getSpecLinesProcessed ();
1665 fprintf (g_msgstream, "%d spec, ", specLines);
1668 # ifndef CLOCKS_PER_SEC
1669 fprintf (g_msgstream, "%d source lines in %ld time steps (steps/sec unknown)\n",
1670 context_getLinesProcessed (),
1673 fprintf (g_msgstream, "%d source lines in %.2f s.\n",
1674 context_getLinesProcessed (),
1675 (double) ttime / CLOCKS_PER_SEC);
1683 if (context_getFlag (FLG_TIMEDIST))
1685 clock_t ttime = clock () - before;
1689 char *msg = (char *) dmalloc (256 * sizeof (*msg));
1694 "Time distribution (percent): initialize %.2f / lcl %.2f / "
1695 "pre-process %.2f / c check %.2f / finalize %.2f \n",
1696 (100.0 * (double) (libtime - before) / ttime),
1697 (100.0 * (double) (lcltime - libtime) / ttime),
1698 (100.0 * (double) (pptime - lcltime) / ttime),
1699 (100.0 * (double) (cptime - pptime) / ttime),
1700 (100.0 * (double) (rstime - cptime) / ttime));
1705 "Time distribution (percent): initialize %.2f / "
1706 "pre-process %.2f / c check %.2f / finalize %.2f \n",
1707 (100.0 * (double) (libtime - before) / ttime),
1708 (100.0 * (double) (pptime - libtime) / ttime),
1709 (100.0 * (double) (cptime - pptime) / ttime),
1710 (100.0 * (double) (rstime - cptime) / ttime));
1713 llgenindentmsgnoloc (cstring_fromCharsO (msg));
1717 llexit (expsuccess ? LLSUCCESS : LLFAILURE);
1718 BADBRANCHRET (LLFAILURE);
1723 ** Reenable return value warnings.
1725 # pragma warning (default:4035)
1733 llmsg (message ("Source files are .c, .h and %s files. If there is no suffix,",
1735 llmsg (message (" Splint will look for <file>.c and <file>%s.", LCL_EXTENSION));
1737 llmsglit ("Use splint -help <topic or flag name> for more information");
1739 llmsglit ("Topics:");
1741 llmsglit (" annotations (describes source-code annotations)");
1742 llmsglit (" comments (describes control comments)");
1743 llmsglit (" flags (describes flag categories)");
1744 llmsglit (" flags <category> (describes flags in category)");
1745 llmsglit (" flags all (short description of all flags)");
1746 llmsglit (" flags alpha (list all flags alphabetically)");
1747 llmsglit (" flags full (full description of all flags)");
1748 llmsglit (" mail (information on mailing lists)");
1749 llmsglit (" modes (show mode settings)");
1750 llmsglit (" parseerrors (help on handling parser errors)");
1751 llmsglit (" prefixcodes (character codes in namespace prefixes)");
1752 llmsglit (" references (sources for more information)");
1753 llmsglit (" vars (environment variables)");
1754 llmsglit (" version (information on compilation, maintainer)");
1759 specialFlagsHelp (char *next)
1761 if ((next != NULL) && (*next != '-') && (*next != '+'))
1763 if (mstring_equal (next, "alpha"))
1768 else if (mstring_equal (next, "all"))
1770 printAllFlags (TRUE, FALSE);
1773 else if (mstring_equal (next, "categories")
1774 || mstring_equal (next, "cats"))
1776 listAllCategories ();
1779 else if (mstring_equal (next, "full"))
1781 printAllFlags (FALSE, TRUE);
1784 else if (mstring_equal (next, "manual"))
1801 printParseErrors (void)
1803 llmsglit ("Parse Errors");
1804 llmsglit ("------------");
1806 llmsglit ("LCLint will sometimes encounter a parse error for code that "
1807 "can be parsed with a local compiler. There are a few likely "
1808 "causes for this and a number of techniques that can be used "
1809 "to work around the problem.");
1811 llmsglit ("Compiler extensions --- compilers sometimes extend the C "
1812 "language with compiler-specific keywords and syntax. While "
1813 "it is not advisible to use these, oftentimes one has no choice "
1814 "when the system header files use compiler extensions. ");
1816 llmsglit ("Splint supports some of the GNU (gcc) compiler extensions, "
1817 "if the +gnuextensions flag is set. You may be able to workaround "
1818 "other compiler extensions by using a pre-processor define. "
1819 "Alternately, you can surround the unparseable code with");
1821 llmsglit (" # ifndef S_SPLINT_S");
1823 llmsglit (" # endif");
1825 /* evans 2000-12-21 fixed typo reported by Jeroen Ruigrok/Asmodai */
1826 llmsglit ("Missing type definitions --- an undefined type name will usually "
1827 "lead to a parse error. This often occurs when a standard header "
1828 "file defines some type that is not part of the standard library. ");
1829 llmsglit ("By default, Splint does not process the local files corresponding "
1830 "to standard library headers, but uses a library specification "
1831 "instead so dependencies on local system headers can be detected. "
1832 "If another system header file that does not correspond to a "
1833 "standard library header uses one of these superfluous types, "
1834 "a parse error will result.");
1836 llmsglit ("If the parse error is inside a posix standard header file, the "
1837 "first thing to try is +posixlib. This makes Splint use "
1838 "the posix library specification instead of reading the posix "
1841 llmsglit ("Otherwise, you may need to either manually define the problematic "
1842 "type (e.g., add -Dmlink_t=int to your .lclintrc file) or force "
1843 "lclint to process the header file that defines it. This is done "
1844 "by setting -skipansiheaders or -skipposixheaders before "
1845 "the file that defines the type is #include'd.");
1846 llmsglit ("(See lclint -help "
1847 "skipansiheaders and lclint -help skipposixheaders for a list of "
1848 "standard headers.) For example, if <sys/local.h> uses a type "
1849 "defined by posix header <sys/types.h> but not defined by the "
1850 "posix library, we might do: ");
1852 llmsglit (" /*@-skipposixheaders@*/");
1853 llmsglit (" # include <sys/types.h>");
1854 llmsglit (" /*@=skipposixheaders@*/");
1855 llmsglit (" # include <sys/local.h>");
1857 llmsglit ("to force Splint to process <sys/types.h>.");
1859 llmsglit ("At last resort, +trytorecover can be used to make Splint attempt "
1860 "to continue after a parse error. This is usually not successful "
1861 "and the author does not consider assertion failures when +trytorecover "
1862 "is used to be bugs.");
1866 printAnnotations (void)
1868 llmsglit ("Annotations");
1869 llmsglit ("-----------");
1871 llmsglit ("Annotations are semantic comments that document certain "
1872 "assumptions about functions, variables, parameters, and types. ");
1874 llmsglit ("They may be used to indicate where the representation of a "
1875 "user-defined type is hidden, to limit where a global variable may "
1876 "be used or modified, to constrain what a function implementation "
1877 "may do to its parameters, and to express checked assumptions about "
1878 "variables, types, structure fields, function parameters, and "
1879 "function results.");
1881 llmsglit ("Annotations are introduced by \"/*@\". The role of the @ may be "
1882 "played by any printable character, selected using -commentchar <char>.");
1884 llmsglit ("Consult the User's Guide for descriptions of checking associated with each annotation.");
1886 llmsglit ("Globals: (in function declarations)");
1887 llmsglit (" /*@globals <globitem>,+ @*/");
1888 llmsglit (" globitem is an identifier, internalState or fileSystem");
1890 llmsglit ("Modifies: (in function declarations)");
1891 llmsglit (" /*@modifies <moditem>,+ @*/");
1892 llmsglit (" moditem is an lvalue");
1893 llmsglit (" /*@modifies nothing @*/");
1894 llmsglit (" /*@*/ (Abbreviation for no globals and modifies nothing.)");
1896 llmsglit ("Iterators:");
1897 llmsglit (" /*@iter <identifier> (<parameter-type-list>) @*/ - declare an iterator");
1899 llmsglit ("Constants:");
1900 llmsglit (" /*@constant <declaration> @*/ - declares a constant");
1902 llmsglit ("Alternate Types:");
1903 llmsglit (" /*@alt <basic-type>,+ @*/");
1904 llmsglit (" (e.g., int /*@alt char@*/ is a type matching either int or char)");
1906 llmsglit ("Declarator Annotations");
1908 llmsglit ("Type Definitions:");
1909 llmsglit (" /*@abstract@*/ - representation is hidden from clients");
1910 llmsglit (" /*@concrete@*/ - representation is visible to clients");
1911 llmsglit (" /*@immutable@*/ - instances of the type cannot change value");
1912 llmsglit (" /*@mutable@*/ - instances of the type can change value");
1913 llmsglit (" /*@refcounted@*/ - reference counted type");
1915 llmsglit ("Global Variables:");
1916 llmsglit (" /*@unchecked@*/ - weakest checking for global use");
1917 llmsglit (" /*@checkmod@*/ - check modification by not use of global");
1918 llmsglit (" /*@checked@*/ - check use and modification of global");
1919 llmsglit (" /*@checkedstrict@*/ - check use of global strictly");
1921 llmsglit ("Memory Management:");
1922 llmsglit (" /*@dependent@*/ - a reference to externally-owned storage");
1923 llmsglit (" /*@keep@*/ - a parameter that is kept by the called function");
1924 llmsglit (" /*@killref@*/ - a refcounted parameter, killed by the call");
1925 llmsglit (" /*@only@*/ - an unshared reference");
1926 llmsglit (" /*@owned@*/ - owner of storage that may be shared by /*@dependent@*/ references");
1927 llmsglit (" /*@shared@*/ - shared reference that is never deallocated");
1928 llmsglit (" /*@temp@*/ - temporary parameter");
1930 llmsglit ("Aliasing:");
1931 llmsglit (" /*@unique@*/ - may not be aliased by any other visible reference");
1932 llmsglit (" /*@returned@*/ - may be aliased by the return value");
1934 llmsglit ("Exposure:");
1935 llmsglit (" /*@observer@*/ - reference that cannot be modified");
1936 llmsglit (" /*@exposed@*/ - exposed reference to storage in another object");
1938 llmsglit ("Definition State:");
1939 llmsglit (" /*@out@*/ - storage reachable from reference need not be defined");
1940 llmsglit (" /*@in@*/ - all storage reachable from reference must be defined");
1941 llmsglit (" /*@partial@*/ - partially defined, may have undefined fields");
1942 llmsglit (" /*@reldef@*/ - relax definition checking");
1944 llmsglit ("Global State: (for globals lists, no /*@, since list is already in /*@\'s)");
1945 llmsglit (" undef - variable is undefined before the call");
1946 llmsglit (" killed - variable is undefined after the call");
1948 llmsglit ("Null State:");
1949 llmsglit (" /*@null@*/ - possibly null pointer");
1950 llmsglit (" /*@notnull@*/ - definitely non-null pointer");
1951 llmsglit (" /*@relnull@*/ - relax null checking");
1953 llmsglit ("Null Predicates:");
1954 llmsglit (" /*@nullwhentrue@*/ - if result is TRUE, first parameter is NULL");
1955 llmsglit (" /*@falsewhennull@*/ - if result is TRUE, first parameter is not NULL");
1957 llmsglit ("Execution:");
1958 llmsglit (" /*@noreturn@*/ - function never returns");
1959 llmsglit (" /*@maynotreturn@*/ - function may or may not return");
1960 llmsglit (" /*@noreturnwhentrue@*/ - function does not return if first parameter is TRUE");
1961 llmsglit (" /*@noreturnwhenfalse@*/ - function does not return if first parameter if FALSE");
1962 llmsglit (" /*@alwaysreturns@*/ - function always returns");
1964 llmsglit ("Side-Effects:");
1965 llmsglit (" /*@sef@*/ - corresponding actual parameter has no side effects");
1967 llmsglit ("Declaration:");
1968 llmsglit (" /*@unused@*/ - need not be used (no unused errors reported)");
1969 llmsglit (" /*@external@*/ - defined externally (no undefined error reported)");
1972 llmsglit (" /*@fallthrough@*/ - fall-through case");
1974 llmsglit ("Break:");
1975 llmsglit (" /*@innerbreak@*/ - break is breaking an inner loop or switch");
1976 llmsglit (" /*@loopbreak@*/ - break is breaking a loop");
1977 llmsglit (" /*@switchbreak@*/ - break is breaking a switch");
1978 llmsglit (" /*@innercontinue@*/ - continue is continuing an inner loop");
1980 llmsglit ("Unreachable Code:");
1981 llmsglit (" /*@notreached@*/ - statement may be unreachable.");
1983 llmsglit ("Special Functions:");
1984 llmsglit (" /*@printflike@*/ - check variable arguments like printf");
1985 llmsglit (" /*@scanflike@*/ - check variable arguments like scanf");
1989 printComments (void)
1991 llmsglit ("Control Comments");
1992 llmsglit ("----------------");
1994 llmsglit ("Setting Flags");
1996 llmsglit ("Most flags (all except those characterized as \"globally-settable only\") can be set locally using control comments. A control comment can set flags locally to override the command line settings. The original flag settings are restored before processing the next file.");
1998 llmsglit ("The syntax for setting flags in control comments is the same as that of the command line, except that flags may also be preceded by = to restore their setting to the original command-line value. For instance,");
1999 llmsglit (" /*@+boolint -modifies =showfunc@*/");
2000 llmsglit ("sets boolint on (this makes bool and int indistinguishable types), sets modifies off (this prevents reporting of modification errors), and sets showfunc to its original setting (this controls whether or not the name of a function is displayed before a message).");
2002 llmsglit ("Error Suppression");
2004 llmsglit ("Several comments are provided for suppressing messages. In general, it is usually better to use specific flags to suppress a particular error permanently, but the general error suppression flags may be more convenient for quickly suppressing messages for code that will be corrected or documented later.");
2006 llmsglit ("/*@ignore@*/ ... /*@end@*/");
2008 (cstring_makeLiteral
2009 ("No errors will be reported in code regions between /*@ignore@*/ and /*@end@*/. These comments can be used to easily suppress an unlimited number of messages."));
2010 llmsglit ("/*@i@*/");
2012 (cstring_makeLiteral
2013 ("No errors will be reported from an /*@i@*/ comment to the end of the line."));
2014 llmsglit ("/*@i<n>@*/");
2016 (cstring_makeLiteral
2017 ("No errors will be reported from an /*@i<n>@*/ (e.g., /*@i3@*/) comment to the end of the line. If there are not exactly n errors suppressed from the comment point to the end of the line, Splint will report an error."));
2018 llmsglit ("/*@t@*/, /*@t<n>@*/");
2020 (cstring_makeLiteral
2021 ("Like i and i<n>, except controlled by +tmpcomments flag. These can be used to temporarily suppress certain errors. Then, -tmpcomments can be set to find them again."));
2023 llmsglit ("Type Access");
2025 llmsglit ("/*@access <type>@*/");
2026 llmsglit (" Allows the following code to access the representation of <type>");
2027 llmsglit ("/*@noaccess <type>@*/");
2028 llmsglit (" Hides the representation of <type>");
2030 llmsglit ("Macro Expansion");
2032 llmsglit ("/*@notfunction@*/");
2034 (cstring_makeLiteral
2035 ("Indicates that the next macro definition is not intended to be a "
2036 "function, and should be expanded in line instead of checked as a "
2037 "macro function definition."));
2044 llmsglit ("Flag Categories");
2045 llmsglit ("---------------");
2046 listAllCategories ();
2047 llmsglit ("\nTo see the flags in a flag category, do\n lclint -help flags <category>");
2048 llmsglit ("To see a list of all flags in alphabetical order, do\n lclint -help flags alpha");
2049 llmsglit ("To see a full description of all flags, do\n lclint -help flags full");
2053 printMaintainer (void)
2055 llmsg (message ("Maintainer: %s", cstring_makeLiteralTemp (LCLINT_MAINTAINER)));
2056 llmsglit (LCL_COMPILE);
2062 llmsglit ("Mailing Lists");
2063 llmsglit ("-------------");
2065 llmsglit ("There are two mailing lists associated with Splint: ");
2067 llmsglit (" lclint-announce@virginia.edu");
2069 llmsglit (" Reserved for announcements of new releases and bug fixes.");
2070 llmsglit (" To subscribe, send a message to majordomo@virginia.edu with body: ");
2071 llmsglit (" subscribe lclint-announce");
2073 llmsglit (" lclint-interest@virginia.edu");
2075 llmsglit (" Informal discussions on the use and development of lclint.");
2076 llmsglit (" To subscribe, send a message to majordomo@virginia.edu with body: ");
2077 llmsglit (" subscribe lclint-interest");
2081 printReferences (void)
2083 llmsglit ("References");
2084 llmsglit ("----------");
2086 llmsglit ("For more information, see the Splint web site: http://www.splint.org");
2090 describePrefixCodes (void)
2092 llmsglit ("Prefix Codes");
2093 llmsglit ("------------");
2095 llmsglit ("These characters have special meaning in name prefixes:");
2097 llmsg (message (" %h Any uppercase letter [A-Z]", PFX_UPPERCASE));
2098 llmsg (message (" %h Any lowercase letter [a-z]", PFX_LOWERCASE));
2099 llmsg (message (" %h Any character (valid in a C identifier)", PFX_ANY));
2100 llmsg (message (" %h Any digit [0-9]", PFX_DIGIT));
2101 llmsg (message (" %h Any non-uppercase letter [a-z0-9_]", PFX_NOTUPPER));
2102 llmsg (message (" %h Any non-lowercase letter [A-Z0-9_]", PFX_NOTLOWER));
2103 llmsg (message (" %h Any letter [A-Za-z]", PFX_ANYLETTER));
2104 llmsg (message (" %h Any letter or digit [A-Za-z0-9]", PFX_ANYLETTERDIGIT));
2105 llmsglit (" * Zero or more repetitions of the previous character class until the end of the name");
2114 eval = context_getLarchPath ();
2115 def = osd_getEnvironmentVariable (LARCH_PATH);
2117 if (cstring_isDefined (def) ||
2118 !cstring_equal (eval, cstring_fromChars (DEFAULT_LARCHPATH)))
2120 llmsg (message ("LARCH_PATH = %s", eval));
2124 llmsg (message ("LARCH_PATH = <not set> (default = %s)",
2125 cstring_fromChars (DEFAULT_LARCHPATH)));
2128 llmsglit (" --- path used to find larch initialization files and LSL traits");
2130 eval = context_getLCLImportDir ();
2131 def = osd_getEnvironmentVariable (cstring_makeLiteralTemp (LCLIMPORTDIR));
2133 if (cstring_isDefined (def) ||
2134 !cstring_equal (eval, cstring_fromChars (DEFAULT_LCLIMPORTDIR)))
2136 llmsg (message ("%q = %s", cstring_makeLiteral (LCLIMPORTDIR), eval));
2140 llmsg (message ("%s = <not set, default: %s>", cstring_makeLiteralTemp (LCLIMPORTDIR),
2141 cstring_makeLiteralTemp (DEFAULT_LCLIMPORTDIR)));
2144 llmsglit (" --- directory containing lcl standard library files "
2145 "(import with < ... >)");;
2148 ("include path = %q (set by environment variable %s and -I flags)",
2149 cppReader_getIncludePath (), INCLUDEPATH_VAR));
2151 llmsglit (" --- path used to find #include'd files");
2154 ("systemdirs = %s (set by -systemdirs or environment variable %s)", /*@i413223@*/
2155 context_getString (FLG_SYSTEMDIRS),
2158 llmsglit (" --- if file is found on this path, it is treated as a system file for error reporting");
2167 fprintf (stderr, "*** Interrupt\n");
2168 llexit (LLINTERRUPT);
2173 /* Cheat when there are parse errors */
2176 fprintf (stderr, "*** Segmentation Violation\n");
2178 /* Don't catch it if fileloc_unparse causes a signal */
2179 (void) signal (SIGSEGV, NULL);
2181 loc = fileloc_unparse (g_currentloc);
2183 fprintf (stderr, "*** Location (not trusted): %s\n",
2184 cstring_toCharsSafe (loc));
2187 fprintf (stderr, "*** Please report bug to %s\n", LCLINT_MAINTAINER);
2191 fprintf (stderr, "*** Signal: %d\n", i);
2193 fprintf (stderr, "*** Location (not trusted): %s\n",
2194 cstring_toCharsSafe (fileloc_unparse (g_currentloc)));
2197 fprintf (stderr, "*** Please report bug to %s ***\n", LCLINT_MAINTAINER);
2205 static bool doneCleanup = FALSE;
2207 /* make sure this is only called once! */
2209 if (doneCleanup) return;
2214 ** Close all open files
2215 ** (There should only be open files, if we exited after a fatal error.)
2218 fileTable_closeAll (context_fileTable ());
2220 if (context_getFlag (FLG_KEEP))
2222 check (fputs ("Temporary files kept:\n", stderr) != EOF);
2223 fileTable_printTemps (context_fileTable ());
2228 int nfiles = /*@-unrecog@*/ _fcloseall (); /*@=unrecog@*/
2232 llbug (message ("Files unclosed: %d", nfiles));
2235 fileTable_cleanup (context_fileTable ());
2242 ** cleans up temp files (if necessary) and exits
2248 DPRINTF (("llexit: %d", status));
2251 if (status == LLFAILURE)
2259 if (status != LLFAILURE)
2261 context_destroyMod ();
2262 exprNode_destroyMod ();
2265 uentry_destroyMod ();
2266 typeIdSet_destroyMod ();
2269 dmalloc_shutdown ();
2273 exit ((status == LLSUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE);
2276 bool readOptionsFile (cstring fname, cstringSList *passThroughArgs, bool report)
2280 if (fileTable_exists (context_fileTable (), fname))
2286 message ("Multiple attempts to read options file: %s", fname),
2292 FILE *innerf = fileTable_openFile (context_fileTable (), fname, "r");
2296 fileloc fc = g_currentloc;
2297 g_currentloc = fileloc_createRc (fname);
2299 if (context_getFlag (FLG_SHOWSCAN))
2301 lldiagmsg (message ("< reading options from %q >",
2302 fileloc_outputFilename (g_currentloc)));
2305 loadrc (innerf, passThroughArgs);
2306 fileloc_reallyFree (g_currentloc);
2316 message ("Cannot open options file: %s", fname),
2326 ** This shouldn't be necessary, but Apple Darwin can't handle '"''s.
2330 loadrc (/*:open:*/ FILE *rcfile, cstringSList *passThroughArgs)
2331 /*@modifies rcfile@*/
2332 /*@ensures closed rcfile@*/
2334 char *s = mstring_create (MAX_LINE_LENGTH);
2337 DPRINTF (("Pass through: %s", cstringSList_unparse (*passThroughArgs)));
2341 while (reader_readLine (rcfile, s, MAX_LINE_LENGTH) != NULL)
2348 DPRINTF (("Line: %s", s));
2349 DPRINTF (("Pass through: %s", cstringSList_unparse (*passThroughArgs)));
2351 while (*s == ' ' || *s == '\t')
2359 bool escaped = FALSE;
2360 bool quoted = FALSE;
2363 DPRINTF (("Process: %s", s));
2364 DPRINTF (("Pass through: %s", cstringSList_unparse (*passThroughArgs)));
2365 /* comment characters */
2366 if (c == '#' || c == ';' || c == '\n')
2372 if (c == '-' || c == '+')
2379 voptgenerror (FLG_BADFLAG,
2380 message ("Bad flag syntax (+ or - expected, "
2381 "+ is assumed): %s",
2382 cstring_fromChars (s)),
2393 while ((c = *s) != '\0')
2394 { /* remember to handle spaces and quotes in -D and -U ... */
2420 if (c == ' ' || c == '\t' || c == '\n')
2422 /*@innerbreak@*/ break;
2430 DPRINTF (("Nulling: %c", *s));
2433 if (mstring_isEmpty (thisflag))
2435 llfatalerror (message ("Missing flag: %s",
2436 cstring_fromChars (os)));
2439 DPRINTF (("Flag: %s", thisflag));
2441 opt = flags_identifyFlag (cstring_fromChars (thisflag));
2443 if (flagcode_isSkip (opt))
2447 else if (flagcode_isInvalid (opt))
2449 DPRINTF (("Invalid: %s", thisflag));
2451 if (isMode (cstring_fromChars (thisflag)))
2453 context_setMode (cstring_fromChars (thisflag));
2457 voptgenerror (FLG_BADFLAG,
2458 message ("Unrecognized option: %s",
2459 cstring_fromChars (thisflag)),
2465 context_userSetFlag (opt, set);
2467 if (flagcode_hasArgument (opt))
2469 if (opt == FLG_HELP)
2472 voptgenerror (FLG_BADFLAG,
2473 message ("Cannot use help in rc files"),
2476 else if (flagcode_isPassThrough (opt)) /* -D or -U */
2478 cstring arg = cstring_fromCharsNew (thisflag);
2479 cstring_markOwned (arg);
2480 *passThroughArgs = cstringSList_add (*passThroughArgs, arg);
2481 DPRINTF (("Pass through: %s",
2482 cstringSList_unparse (*passThroughArgs)));
2484 else if (opt == FLG_INCLUDEPATH
2485 || opt == FLG_SPECPATH)
2487 cstring dir = cstring_suffix (cstring_fromChars (thisflag), 1); /* skip over I/S */
2491 case FLG_INCLUDEPATH:
2492 cppAddIncludeDir (dir);
2493 /*@switchbreak@*/ break;
2496 g_localSpecPath = cstring_toCharsSafe
2497 (message ("%s:%s", cstring_fromChars (g_localSpecPath), dir));
2499 /*@switchbreak@*/ break;
2503 else if (flagcode_hasString (opt)
2504 || flagcode_hasNumber (opt)
2505 || flagcode_hasChar (opt)
2506 || opt == FLG_INIT || opt == FLG_OPTF)
2508 cstring extra = cstring_undefined;
2513 rest = mstring_copy (s);
2514 DPRINTF (("Here: rest = %s", rest));
2518 while ((rchar = *rest) != '\0'
2519 && (isspace ((int) rchar)))
2525 DPRINTF (("Yo: %s", rest));
2527 while ((rchar = *rest) != '\0'
2528 && !isspace ((int) rchar))
2530 extra = cstring_appendChar (extra, rchar);
2535 DPRINTF (("Yo: %s", extra));
2538 if (cstring_isUndefined (extra))
2544 ("Flag %s must be followed by an argument",
2545 flagcode_unparse (opt)),
2552 DPRINTF (("Here we are: %s", extra));
2554 if (flagcode_hasNumber (opt) || flagcode_hasChar (opt))
2556 DPRINTF (("Set value flag: %s", extra));
2557 setValueFlag (opt, extra);
2559 else if (opt == FLG_OPTF)
2561 (void) readOptionsFile (extra, passThroughArgs, TRUE);
2563 else if (opt == FLG_INIT)
2566 llassert (inputStream_isUndefined (initFile));
2568 initFile = inputStream_create
2569 (cstring_copy (extra),
2570 cstring_makeLiteralTemp (LCLINIT_SUFFIX),
2574 else if (flagcode_hasString (opt))
2576 DPRINTF (("Here: %s", extra));
2579 ** If it has "'s, we need to remove them.
2582 if (cstring_firstChar (extra) == '\"')
2584 if (cstring_lastChar (extra) == '\"')
2586 cstring unquoted = cstring_copyLength
2587 (cstring_toCharsSafe (cstring_suffix (extra, 1)),
2588 cstring_length (extra) - 2);
2590 DPRINTF (("string flag: %s -> %s", extra, unquoted));
2591 setStringFlag (opt, unquoted);
2592 cstring_free (extra);
2598 message ("Unmatched \" in option string: %s",
2601 setStringFlag (opt, extra);
2606 DPRINTF (("No quotes: %s", extra));
2607 setStringFlag (opt, extra);
2610 extra = cstring_undefined;
2618 cstring_free (extra);
2628 DPRINTF (("Pass through: %s", cstringSList_unparse (*passThroughArgs)));
2629 while ((c == ' ') || (c == '\t'))
2635 DPRINTF (("Pass through: %s", cstringSList_unparse (*passThroughArgs)));
2639 DPRINTF (("Pass through: %s", cstringSList_unparse (*passThroughArgs)));
2641 check (fileTable_closeFile (context_fileTable (), rcfile));
2644 static fileIdList preprocessFiles (fileIdList fl, bool xhfiles)
2645 /*@modifies fileSystem@*/
2647 bool msg = (context_getFlag (FLG_SHOWSCAN) && fileIdList_size (fl) > 10);
2648 int skip = (fileIdList_size (fl) / 5);
2649 int filesprocessed = 0;
2650 fileIdList dfiles = fileIdList_create ();
2652 fileloc_free (g_currentloc);
2653 g_currentloc = fileloc_createBuiltin ();
2655 fileIdList_elements (fl, fid)
2657 cstring ppfname = fileName (fid);
2659 if (!(osd_fileIsReadable (ppfname)))
2661 lldiagmsg (message ("Cannot open file: %q", osd_outputPath (ppfname)));
2662 ppfname = cstring_undefined;
2665 if (cstring_isDefined (ppfname))
2667 fileId dfile = fileTable_addCTempFile (context_fileTable (), fid);
2671 llassert (fileTable_isXHFile (context_fileTable (), dfile));
2674 llassert (cstring_isNonEmpty (ppfname));
2678 if ((filesprocessed % skip) == 0)
2680 if (filesprocessed == 0) {
2681 fprintf (stderr, " ");
2684 fprintf (stderr, ".");
2687 (void) fflush (stderr);
2692 if (cppProcess (ppfname, fileName (dfile)) != 0)
2694 llfatalerror (message ("Preprocessing error for file: %s",
2695 rootFileName (fid)));
2698 fileIdList_add (dfiles, dfile);
2700 } end_fileIdList_elements;
2705 /* This should be in an lclUtils.c file... */
2707 char *specFullName (char *specfile, /*@out@*/ char **inpath)
2709 /* extract the path and the specname associated with the given file */
2710 char *specname = (char *) dmalloc (sizeof (*specname)
2711 * (strlen (specfile) + 9));
2712 char *ospecname = specname;
2713 char *path = (char *) dmalloc (sizeof (*path) * (strlen (specfile)));
2717 /* initialized path to empty string or may have accidental garbage */
2720 /*@-mayaliasunique@*/
2721 strcpy (specname, specfile);
2722 /*@=mayaliasunique@*/
2724 /* trim off pathnames in specfile */
2725 size = strlen (specname);
2727 for (i = size_toInt (size) - 1; i >= 0; i--)
2729 if (specname[i] == CONNECTCHAR)
2731 /* strcpy (specname, (char *)specname+i+1); */
2732 for (j = 0; j <= i; j++) /* include '/' */
2734 path[j] = specname[j];
2744 ** also remove .lcl file extension, assume it's the last extension
2748 size = strlen (specname);
2750 for (i = size_toInt (size) - 1; i >= 0; i--)
2752 if (specname[i] == '.')
2762 ** If specname no longer points to the original char,
2763 ** we need to allocate a new pointer and copy the string.
2766 if (specname != ospecname) {
2767 char *rspecname = (char *) dmalloc (sizeof (*rspecname) * (strlen (specname) + 1));
2768 strcpy (rspecname, specname); /* evs 2000-05-16: Bug: was ospecname! */