]> andersk Git - splint.git/blame - src/llmain.c
Removed obsolete OS/2 test makefiles.
[splint.git] / src / llmain.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
c59f5181 3** Copyright (C) 1994-2003 University of Virginia,
616915dd 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.
19**
155af98d 20** For information on splint: info@splint.org
21** To report a bug: splint-bug@splint.org
11db3170 22** For more information: http://www.splint.org
616915dd 23*/
24/*
25** llmain.c
26**
11db3170 27** Main module for Splint annotation-assisted program checker
616915dd 28*/
29
30# include <signal.h>
12f2ffe9 31# include <time.h>
616915dd 32/*
33** Ensure that WIN32 and _WIN32 are both defined or both undefined.
34*/
35
36# ifdef WIN32
37# ifndef _WIN32
38# error "Inconsistent definitions."
39# endif
40# else
41# ifdef _WIN32
42# error "Inconsistent definitions."
43# endif
44# endif
45
46# ifdef WIN32
47# include <windows.h>
48# include <process.h>
49# endif
50
1b8ae690 51# include "splintMacros.nf"
b73d1009 52# include "basic.h"
616915dd 53# include "osd.h"
140c27a8 54# include "help.h"
616915dd 55# include "gram.h"
56# include "lclscan.h"
57# include "scanline.h"
58# include "lclscanline.h"
59# include "lclsyntable.h"
60# include "lcltokentable.h"
61# include "lslparse.h"
62# include "scan.h"
63# include "syntable.h"
64# include "tokentable.h"
65# include "lslinit.h"
66# include "lclinit.h"
67# include "lh.h"
68# include "imports.h"
616915dd 69
4dd72714 70# include "Headers/version.h" /* Visual C++ finds the wrong version.h */
616915dd 71# include "lcllib.h"
72# include "cgrammar.h"
4caf866b 73# include "rcfiles.h"
616915dd 74# include "llmain.h"
616915dd 75
76extern /*@external@*/ int yydebug;
616915dd 77static void cleanupFiles (void);
ce7034f0 78/*
79** evans 2002-07-03: renamed from interrupt to avoid conflict with WATCOM compiler keyword
80** (Suggested by Adam Clarke)
81*/
82
83static void llinterrupt (int p_i);
990ec868 84
616915dd 85static void describeVars (void);
86static bool specialFlagsHelp (char *p_next);
87static bool hasShownHerald = FALSE;
28bf4b0b 88static char *specFullName (char *p_specfile, /*@out@*/ char **p_inpath)
89 /*@modifies *p_inpath@*/ ;
616915dd 90
91static bool anylcl = FALSE;
92static clock_t inittime;
93
28bf4b0b 94static fileIdList preprocessFiles (fileIdList, bool)
616915dd 95 /*@modifies fileSystem@*/ ;
96
0e5499ac 97static void warnSysFiles(fileIdList p_files) /*@modifies fileSystem@*/;
e55c0c6d 98
616915dd 99static
100void lslCleanup (void)
101 /*@globals killed g_symtab@*/
102 /*@modifies internalState, g_symtab@*/
103{
104 /*
105 ** Cleanup all the LCL/LSL.
106 */
107
108 static bool didCleanup = FALSE;
109
110 llassert (!didCleanup);
111 llassert (anylcl);
112
113 didCleanup = TRUE;
114
115 lsymbol_destroyMod ();
116 LCLSynTableCleanup ();
117 LCLTokenTableCleanup ();
118 LCLScanLineCleanup ();
119 LCLScanCleanup ();
120
121 /* clean up LSL parsing */
122
123 lsynTableCleanup ();
124 ltokenTableCleanup ();
125 lscanLineCleanup ();
126 LSLScanCleanup ();
127
128 symtable_free (g_symtab);
129 sort_destroyMod ();
130}
131
616915dd 132static void
133lslProcess (fileIdList lclfiles)
134 /*@globals undef g_currentSpec, undef g_currentSpecName, g_currentloc,
135 undef killed g_symtab; @*/
136 /*@modifies g_currentSpec, g_currentSpecName, g_currentloc, internalState, fileSystem; @*/
137{
138 char *path = NULL;
139 bool parser_status = FALSE;
140 bool overallStatus = FALSE;
141
140c27a8 142 lslinit_process ();
143 inittime = clock ();
144
616915dd 145 context_resetSpecLines ();
146
147 fileIdList_elements (lclfiles, fid)
148 {
28bf4b0b 149 cstring actualName = cstring_undefined;
4dd72714 150 cstring fname = fileTable_fileName (fid);
616915dd 151
28bf4b0b 152 if (osd_getPath (cstring_fromChars (g_localSpecPath),
153 fname, &actualName) == OSD_FILENOTFOUND)
616915dd 154 {
155 if (mstring_equal (g_localSpecPath, "."))
156 {
53a89507 157 lldiagmsg (message ("Spec file not found: %q", osd_outputPath (fname)));
616915dd 158 }
159 else
160 {
53a89507 161 lldiagmsg (message ("Spec file not found: %q (on %s)",
162 osd_outputPath (fname),
616915dd 163 cstring_fromChars (g_localSpecPath)));
164 }
165 }
166 else
167 {
28bf4b0b 168 inputStream specFile;
169 /*@access cstring@*/
170 char *namePtr = actualName;
171
172 while (*namePtr == '.' && *(namePtr + 1) == CONNECTCHAR)
616915dd 173 {
28bf4b0b 174 namePtr += 2;
616915dd 175 }
28bf4b0b 176 /*@noaccess cstring@*/
177
178 g_currentSpec = cstring_fromCharsNew (namePtr);
179
180 specFile = inputStream_create (cstring_copy (g_currentSpec),
181 LCL_EXTENSION, TRUE);
616915dd 182
28bf4b0b 183 llassert (inputStream_isDefined (specFile));
616915dd 184
616915dd 185 g_currentSpecName = specFullName
186 (cstring_toCharsSafe (g_currentSpec),
187 &path);
188
189 setSpecFileId (fid);
190
80489f0a 191 displayScan (message ("reading spec %s", g_currentSpec));
616915dd 192
f7bbce9e 193 /* Open the source file */
616915dd 194
28bf4b0b 195 if (!inputStream_open (specFile))
616915dd 196 {
53a89507 197 lldiagmsg (message ("Cannot open file: %q",
198 osd_outputPath (inputStream_fileName (specFile))));
28bf4b0b 199 inputStream_free (specFile);
616915dd 200 }
201 else
202 {
203 scopeInfo dummy_scope = (scopeInfo) dmalloc (sizeof (*dummy_scope));
204 dummy_scope->kind = SPE_INVALID;
205
206 lhInit (specFile);
207 LCLScanReset (specFile);
208
209 /*
210 ** Minor hacks to allow more than one LCL file to
211 ** be scanned, while keeping initializations
212 */
213
214 symtable_enterScope (g_symtab, dummy_scope);
215 resetImports (cstring_fromChars (g_currentSpecName));
216 context_enterLCLfile ();
217 (void) lclHadNewError ();
218
219 parser_status = (ylparse () != 0);
220 context_exitLCLfile ();
221 lhCleanup ();
222 overallStatus = parser_status || lclHadNewError ();
223
224 if (context_getFlag (FLG_DOLCS))
225 {
226 if (overallStatus)
227 {
28bf4b0b 228 outputLCSFile (path, "%FAILED Output from ",
616915dd 229 g_currentSpecName);
230 }
231 else
232 {
28bf4b0b 233 outputLCSFile (path, "%PASSED Output from ",
616915dd 234 g_currentSpecName);
235 }
236 }
237
28bf4b0b 238 (void) inputStream_close (specFile);
239 inputStream_free (specFile);
616915dd 240
241 symtable_exitScope (g_symtab);
28bf4b0b 242 }
616915dd 243 }
28bf4b0b 244 cstring_free (actualName);
616915dd 245 } end_fileIdList_elements;
28bf4b0b 246
616915dd 247 /* Can cleanup lsl stuff right away */
28bf4b0b 248
249 lslCleanup ();
250
251 g_currentSpec = cstring_undefined;
252 g_currentSpecName = NULL;
616915dd 253}
616915dd 254
255static void handlePassThroughFlag (char *arg)
256{
257 char *curarg = arg;
258 char *quotechar = strchr (curarg, '\"');
259 int offset = 0;
260 bool open = FALSE;
b37cf05e 261 char *freearg = NULL;
616915dd 262
263 while (quotechar != NULL)
264 {
265 if (*(quotechar - 1) == '\\')
266 {
267 char *tp = quotechar - 2;
268 bool escape = TRUE;
269
270 while (*tp == '\\')
271 {
272 escape = !escape;
273 tp--;
274 }
275
276 if (escape)
277 {
278 curarg = quotechar + 1;
279 quotechar = strchr (curarg, '\"');
280 continue;
281 }
282 }
283
b37cf05e 284 llassert (quotechar != NULL);
616915dd 285 *quotechar = '\0';
286 offset = (quotechar - arg) + 2;
287
288 if (open)
289 {
290 arg = cstring_toCharsSafe
291 (message ("%s\"\'%s",
292 cstring_fromChars (arg),
293 cstring_fromChars (quotechar + 1)));
b37cf05e 294 freearg = arg;
616915dd 295 open = FALSE;
296 }
297 else
298 {
299 arg = cstring_toCharsSafe
300 (message ("%s\'\"%s",
301 cstring_fromChars (arg),
302 cstring_fromChars (quotechar + 1)));
b37cf05e 303 freearg = arg;
616915dd 304 open = TRUE;
305 }
306
307 curarg = arg + offset;
308 quotechar = strchr (curarg, '\"');
309 }
310
311 if (open)
312 {
313 showHerald ();
ccf0a4a8 314 voptgenerror (FLG_BADFLAG,
315 message ("Unclosed quote in flag: %s",
316 cstring_fromChars (arg)),
317 g_currentloc);
616915dd 318 }
319 else
320 {
321 if (arg[0] == 'D') {
322 cstring def;
323
324 /*
325 ** If the value is surrounded by single quotes ('), remove
326 ** them. This is an artifact of UNIX command line?
327 */
328
28bf4b0b 329 def = osd_fixDefine (cstring_fromChars (arg + 1));
616915dd 330 DPRINTF (("Do define: %s", def));
331 cppDoDefine (def);
332 DPRINTF (("After define"));
333 cstring_free (def);
334 } else if (arg[0] == 'U') {
335 cppDoUndefine (cstring_fromChars (arg + 1));
336 } else {
337 BADBRANCH;
338 }
339 }
b37cf05e 340
341 sfree (freearg);
616915dd 342}
343
344void showHerald (void)
345{
80489f0a 346 if (hasShownHerald || context_getFlag (FLG_QUIET))
347 {
348 return;
349 }
616915dd 350 else
351 {
80489f0a 352 fprintf (g_messagestream, "%s\n\n", SPLINT_VERSION);
616915dd 353 hasShownHerald = TRUE;
354 llflush ();
355 }
356}
357
616915dd 358/*
1b8ae690 359** Disable MSVC++ warning about return value. Methinks humbly splint control
616915dd 360** comments are a mite more legible.
361*/
362
363# ifdef WIN32
364# pragma warning (disable:4035)
365# endif
366
367int main (int argc, char *argv[])
616915dd 368 /*@globals killed undef g_currentloc,
616915dd 369 killed g_localSpecPath,
370 killed undef g_currentSpec,
371 killed undef g_currentSpecName,
372 killed undef yyin,
80489f0a 373 undef g_warningstream, g_messagestream, g_errorstream;
616915dd 374 @*/
140c27a8 375 /*@modifies g_currentloc, g_localSpecPath, g_currentSpec, g_currentSpecName,
376 fileSystem, yyin;
616915dd 377 @*/
616915dd 378{
379 bool first_time = TRUE;
616915dd 380 bool expsuccess;
28bf4b0b 381 inputStream sourceFile = inputStream_undefined;
616915dd 382
383 fileIdList dercfiles;
6fcd0b1e 384 cstringList passThroughArgs = cstringList_undefined;
28bf4b0b 385 fileIdList cfiles, xfiles, lclfiles, mtfiles;
616915dd 386 clock_t before, lcltime, libtime, pptime, cptime, rstime;
387 int i = 0;
388
22367f91 389# ifdef __EMX__
390 _wildcard (&argc, &argv);
391# endif
392
80489f0a 393 g_warningstream = stdout;
394 g_messagestream = stderr;
395 g_errorstream = stderr;
616915dd 396
ce7034f0 397 (void) signal (SIGINT, llinterrupt);
398 (void) signal (SIGSEGV, llinterrupt);
616915dd 399
616915dd 400 flags_initMod ();
6fcd0b1e 401 qual_initMod ();
28bf4b0b 402 clabstract_initMod ();
616915dd 403 typeIdSet_initMod ();
53a89507 404 osd_initMod ();
6fcd0b1e 405 cppReader_initMod ();
53a89507 406
616915dd 407 setCodePoint ();
28bf4b0b 408
616915dd 409 g_currentloc = fileloc_createBuiltin ();
80489f0a 410
616915dd 411 before = clock ();
412 context_initMod ();
28bf4b0b 413
616915dd 414 context_setInCommandLine ();
415
416 if (argc <= 1)
417 {
140c27a8 418 help_showAvailableHelp ();
aa9c1601 419 llexit (LLSUCCESS);
420 }
421
422 /* -help must be the first flag to get help */
140c27a8 423 if (flagcode_isHelpFlag (flags_identifyFlag (cstring_fromChars (argv[1]))))
aa9c1601 424 {
140c27a8 425 /*
426 ** Skip first flag and help flag
427 */
428
429 help_processFlags (argc - 2, argv + 2);
aa9c1601 430 llexit (LLSUCCESS);
616915dd 431 }
432
433 setCodePoint ();
434 yydebug = 0;
435
436 /*
437 ** Add include directories from environment.
438 */
439
440 {
7272a1c1 441 cstring incval = cstring_copy (osd_getEnvironmentVariable (INCLUDEPATH_VAR));
b37cf05e 442 cstring oincval = incval;
616915dd 443
28bf4b0b 444 if (cstring_isDefined (incval))
616915dd 445 {
446 /*
447 ** Each directory on the include path is a system include directory.
448 */
449
450 DPRINTF (("include: %s", incval));
28bf4b0b 451 context_setString (FLG_SYSTEMDIRS, cstring_copy (incval));
616915dd 452
28bf4b0b 453 while (cstring_isDefined (incval))
616915dd 454 {
28bf4b0b 455 /*@access cstring@*/
7272a1c1 456 char *nextsep = strchr (incval, PATH_SEPARATOR);
616915dd 457
458 if (nextsep != NULL)
459 {
460 cstring dir;
461 *nextsep = '\0';
28bf4b0b 462 dir = cstring_copy (incval);
616915dd 463
464 if (cstring_length (dir) == 0
465 || !isalpha ((int) cstring_firstChar (dir)))
466 {
467 /*
468 ** win32 environment values can have special values,
469 ** ignore them
470 */
471 }
472 else
473 {
616915dd 474 cppAddIncludeDir (dir);
475 }
476
7272a1c1 477 *nextsep = PATH_SEPARATOR;
28bf4b0b 478 incval = cstring_fromChars (nextsep + 1);
616915dd 479 cstring_free (dir);
480 }
481 else
482 {
483 break;
484 }
28bf4b0b 485
486 /*@noaccess cstring@*/
616915dd 487 }
488 }
68de3f33 489 else /* 2001-09-09: herbert */
490 {
491 /* Put C_INCLUDE_PATH directories in sysdirs */
492 cstring cincval = osd_getEnvironmentVariable (cstring_makeLiteralTemp ("C_INCLUDE_PATH"));
aa9c1601 493
68de3f33 494 if (cstring_isDefined (cincval))
495 {
496 context_setString (FLG_SYSTEMDIRS, cstring_copy (cincval));
497 }
498 }
499 /* /herbert */
28bf4b0b 500
b37cf05e 501 cstring_free (oincval);
616915dd 502 }
503
504 /*
505 ** check RCFILE for default flags
506 */
507
140c27a8 508 /*
509 ** Process command line message formatting flags before reading rc file
510 */
511
616915dd 512 {
28bf4b0b 513 cstring home = osd_getHomeDir ();
60868d40 514 cstring fname = cstring_undefined;
616915dd 515 bool defaultf = TRUE;
516 bool nof = FALSE;
517
518 for (i = 1; i < argc; i++)
519 {
520 char *thisarg;
521 thisarg = argv[i];
522
523 if (*thisarg == '-' || *thisarg == '+')
524 {
f2b6724f 525 bool set = (*thisarg == '+');
526 flagcode opt;
527
616915dd 528 thisarg++;
a956d444 529
530 /*
531 ** Don't report warnings this time
532 */
533
534 opt = flags_identifyFlagQuiet (cstring_fromChars (thisarg));
616915dd 535
f2b6724f 536 if (opt == FLG_NOF)
616915dd 537 {
538 nof = TRUE;
539 }
80489f0a 540 else if (flagcode_isMessageControlFlag (opt))
f2b6724f 541 {
542 /*
543 ** Need to set it immediately, so rc file scan is displayed
544 */
545
546 context_userSetFlag (opt, set);
80489f0a 547
548 if (flagcode_hasArgument (opt))
549 {
550 llassert (flagcode_hasString (opt));
551
552 if (++i < argc)
553 {
554 fname = cstring_fromChars (argv[i]);
16c024b5 555 flags_setStringFlag (opt, fname);
80489f0a 556 }
557 else
558 {
140c27a8 559 voptgenerror
560 (FLG_BADFLAG,
561 message
80489f0a 562 ("Flag %s must be followed by a string",
140c27a8 563 flagcode_unparse (opt)),
564 g_currentloc);
80489f0a 565 }
566 }
f2b6724f 567 }
568 else if (opt == FLG_OPTF)
616915dd 569 {
570 if (++i < argc)
571 {
572 defaultf = FALSE;
60868d40 573 fname = cstring_fromChars (argv[i]);
140c27a8 574 (void) rcfiles_read (fname, &passThroughArgs, TRUE);
616915dd 575 }
576 else
577 llfatalerror
578 (cstring_makeLiteral ("Flag f to select options file "
579 "requires an argument"));
580 }
581 else
582 {
583 ; /* wait to process later */
584 }
585 }
586 }
f2b6724f 587
616915dd 588 setCodePoint ();
589
590 if (!nof && defaultf)
591 {
60868d40 592 /*
f2b6724f 593 ** No explicit rc file, first try reading ~/.splintrc
60868d40 594 */
616915dd 595
f2b6724f 596 if (cstring_isUndefined (fname))
60868d40 597 {
f2b6724f 598 if (!cstring_isEmpty (home))
60868d40 599 {
f2b6724f 600 bool readhomerc, readaltrc;
601 cstring homename, altname;
602
603 homename = message ("%s%h%s", home, CONNECTCHAR,
604 cstring_fromChars (RCFILE));
140c27a8 605 readhomerc = rcfiles_read (homename, &passThroughArgs, FALSE);
60868d40 606
f2b6724f 607 /*
1b8ae690 608 ** Try ~/.splintrc also for historical accuracy
f2b6724f 609 */
610
611 altname = message ("%s%h%s", home, CONNECTCHAR,
612 cstring_fromChars (ALTRCFILE));
140c27a8 613 readaltrc = rcfiles_read (altname, &passThroughArgs, FALSE);
f2b6724f 614
615 if (readhomerc && readaltrc)
616 {
617
618 voptgenerror
619 (FLG_WARNRC,
620 message ("Found both %s and %s files. Using both files, "
621 "but recommend using only %s to avoid confusion.",
622 homename, altname, homename),
623 g_currentloc);
624 }
146e25eb 625
626 cstring_free (homename);
627 cstring_free (altname);
60868d40 628 }
629 }
630
f2b6724f 631 /*
632 ** Next, read .splintrc in the current working directory
633 */
634
635 {
636 cstring rcname = message ("%s%s",osd_getCurrentDirectory (), cstring_fromChars (RCFILE));
637 cstring altname = message ("%s%s",osd_getCurrentDirectory (), cstring_fromChars (ALTRCFILE));
638 bool readrc, readaltrc;
639
140c27a8 640 readrc = rcfiles_read (rcname, &passThroughArgs, FALSE);
641 readaltrc = rcfiles_read (altname, &passThroughArgs, FALSE);
f2b6724f 642
643 if (readrc && readaltrc)
644 {
645 voptgenerror (FLG_WARNRC,
646 message ("Found both %s and %s files. Using both files, "
647 "but recommend using only %s to avoid confusion.",
648 rcname, altname, rcname),
649 g_currentloc);
650
651 }
616915dd 652
f2b6724f 653 cstring_free (rcname);
654 cstring_free (altname);
655 }
616915dd 656 }
657 }
658
659 setCodePoint ();
140c27a8 660 llassert (fileloc_isBuiltin (g_currentloc));
661
662 cfiles = fileIdList_create ();
663 xfiles = fileIdList_create ();
664 lclfiles = fileIdList_create ();
665 mtfiles = fileIdList_create ();
28bf4b0b 666
aa9c1601 667 /* argv[0] is the program name, don't pass it to flags_processFlags */
140c27a8 668 flags_processFlags (TRUE, xfiles, cfiles,
669 lclfiles, mtfiles,
670 &passThroughArgs,
671 argc - 1, argv + 1);
616915dd 672
f2b6724f 673 showHerald ();
53306cab 674
675 if (context_getFlag (FLG_CSV)) {
676 cstring fname = context_getString (FLG_CSV);
677
678 if (cstring_isDefined (fname)) {
679 if (osd_fileExists (fname) && !context_getFlag (FLG_CSVOVERWRITE)) {
680 lldiagmsg (message ("Specified CSV output file already exists (use +csvoverwrite to automatically overwrite): %s",
681 fname));
682 } else {
683 g_csvstream = fopen (cstring_toCharsSafe (fname), "w");
684
685 DPRINTF (("Creating: %s", fname));
686 if (g_csvstream == NULL) {
687 lldiagmsg (message ("Cannot open file for CSV output: %s", fname));
688 } else {
689 displayScan (message ("Starting CSV output file: %s", context_getString (FLG_CSV)));
690 fprintf (g_csvstream,
691 "Warning, Flag Code, Flag Name, Priority, File, Line, Column, Warning Text, Additional Text\n");
692 }
693 }
694 }
695 }
696
616915dd 697# ifdef DOANNOTS
698 initAnnots ();
699# endif
700
701 inittime = clock ();
702
703 context_resetErrors ();
704 context_clearInCommandLine ();
705
706 anylcl = !fileIdList_isEmpty (lclfiles);
707
708 if (context_doMerge ())
709 {
710 cstring m = context_getMerge ();
711
80489f0a 712 displayScanOpen (message ("< loading %s ", m));
616915dd 713 loadState (m);
80489f0a 714 displayScanClose ();
616915dd 715
716 if (!usymtab_existsType (context_getBoolName ()))
717 {
718 usymtab_initBool ();
719 }
720 }
721 else
722 {
723 if (!context_getFlag (FLG_NOLIB) && loadStandardState ())
724 {
725 ;
726 }
727 else
728 {
729 ctype_initTable ();
730 }
731
732 /* setup bool type and constants */
733 usymtab_initBool ();
734 }
735
736 fileloc_free (g_currentloc);
737 g_currentloc = fileloc_createBuiltin ();
738
28bf4b0b 739 /*
740 ** Read metastate files (must happen before loading libraries)
741 */
742
743 fileIdList_elements (mtfiles, mtfile)
744 {
745 context_setFileId (mtfile);
80489f0a 746 displayScan (message ("processing %s", fileTable_rootFileName (mtfile)));
4dd72714 747 mtreader_readFile (cstring_copy (fileTable_fileName (mtfile)));
28bf4b0b 748 } end_fileIdList_elements;
749
616915dd 750 libtime = clock ();
28bf4b0b 751
616915dd 752 if (anylcl)
753 {
616915dd 754 lslProcess (lclfiles);
616915dd 755 }
756
28bf4b0b 757 usymtab_initGlobalMarker ();
758
616915dd 759 /*
760 ** pre-processing
761 **
762 ** call the pre-preprocessor and /lib/cpp to generate appropriate
763 ** files
764 **
765 */
766
767 context_setInCommandLine ();
140c27a8 768
616915dd 769 DPRINTF (("Pass through: %s", cstringSList_unparse (passThroughArgs)));
770
6fcd0b1e 771 cstringList_elements (passThroughArgs, thisarg)
140c27a8 772 {
773 handlePassThroughFlag (cstring_toCharsSafe (thisarg));
774 }
6fcd0b1e 775 end_cstringList_elements;
616915dd 776
6fcd0b1e 777 cstringList_free (passThroughArgs);
616915dd 778
779 cleanupMessages ();
780
28bf4b0b 781 DPRINTF (("Initializing cpp reader!"));
782 cppReader_initialize ();
616915dd 783 cppReader_saveDefinitions ();
784
785 context_clearInCommandLine ();
786
787 if (!context_getFlag (FLG_NOPP))
788 {
28bf4b0b 789 fileIdList tfiles;
790
616915dd 791 llflush ();
792
80489f0a 793 displayScanOpen (cstring_makeLiteral ("preprocessing"));
616915dd 794
795 lcltime = clock ();
796
797 context_setPreprocessing ();
28bf4b0b 798 dercfiles = preprocessFiles (xfiles, TRUE);
799 tfiles = preprocessFiles (cfiles, FALSE);
e55c0c6d 800 warnSysFiles(cfiles);
28bf4b0b 801 dercfiles = fileIdList_append (dercfiles, tfiles);
802 fileIdList_free (tfiles);
803
616915dd 804 context_clearPreprocessing ();
805
806 fileIdList_free (cfiles);
807
80489f0a 808 displayScanClose ();
616915dd 809 pptime = clock ();
810 }
811 else
812 {
813 lcltime = clock ();
28bf4b0b 814 dercfiles = fileIdList_append (cfiles, xfiles);
616915dd 815 pptime = clock ();
816 }
28bf4b0b 817
616915dd 818 /*
819 ** now, check all the corresponding C files
820 **
821 ** (for now these are just <file>.c, but after pre-processing
822 ** will be <tmpprefix>.<file>.c)
823 */
824
825 {
6ee276d2 826# if defined (WIN32) || defined (OS2) && defined (__IBMC__)
616915dd 827 int nfiles = /*@-unrecog@*/ _fcloseall (); /*@=unrecog@*/
828
829 if (nfiles != 0)
830 {
831 llbug (message ("Files unclosed: %d", nfiles));
832 }
833# endif
834 }
835
28bf4b0b 836 DPRINTF (("Initializing..."));
837
616915dd 838 exprNode_initMod ();
839
28bf4b0b 840 DPRINTF (("Okay..."));
841
616915dd 842 fileIdList_elements (dercfiles, fid)
843 {
4dd72714 844 sourceFile = inputStream_create (cstring_copy (fileTable_fileName (fid)), C_EXTENSION, TRUE);
616915dd 845 context_setFileId (fid);
846
847 /* Open source file */
848
28bf4b0b 849 if (inputStream_isUndefined (sourceFile) || (!inputStream_open (sourceFile)))
616915dd 850 {
851 /* previously, this was ignored ?! */
4dd72714 852 llbug (message ("Could not open temp file: %s", fileTable_fileName (fid)));
616915dd 853 }
854 else
855 {
28bf4b0b 856 yyin = inputStream_getFile (sourceFile); /*< shared <- only */
616915dd 857
858 llassert (yyin != NULL);
859
80489f0a 860 displayScan (message ("checking %q", osd_outputPath (fileTable_rootFileName (fid))));
616915dd 861
862 /*
863 ** Every time, except the first time, through the loop,
864 ** need to call yyrestart to clean up the parse buffer.
865 */
866
867 if (!first_time)
868 {
869 (void) yyrestart (yyin);
870 }
871 else
872 {
873 first_time = FALSE;
874 }
875
28bf4b0b 876 DPRINTF (("Entering..."));
616915dd 877 context_enterFile ();
878 (void) yyparse ();
28bf4b0b 879 context_exitCFile ();
616915dd 880
28bf4b0b 881 (void) inputStream_close (sourceFile);
882 }
6fcd0b1e 883
884 inputStream_free (sourceFile); /* evans 2002-07-12: why no warning without this?!! */
616915dd 885 } end_fileIdList_elements;
886
6fcd0b1e 887 fileIdList_free (dercfiles); /* evans 2002-07-12: why no warning without this?!! */
616915dd 888 cptime = clock ();
889
890 /* process any leftover macros */
891
892 context_processAllMacros ();
893
894 /* check everything that was specified was defined */
895
896 /* don't check if no c files were processed ?
897 ** is this correct behaviour?
898 */
899
80489f0a 900 displayScan (cstring_makeLiteral ("global checks"));
616915dd 901
902 cleanupMessages ();
903
904 if (context_getLinesProcessed () > 0)
905 {
906 usymtab_allDefined ();
907 }
908
909 if (context_maybeSet (FLG_TOPUNUSED))
910 {
911 uentry ue = usymtab_lookupSafe (cstring_makeLiteralTemp ("main"));
912
913 if (uentry_isValid (ue))
914 {
915 uentry_setUsed (ue, fileloc_observeBuiltin ());
916 }
917
918 usymtab_allUsed ();
919 }
920
921 if (context_maybeSet (FLG_EXPORTLOCAL))
922 {
923 usymtab_exportLocal ();
924 }
925
926
927 if (context_maybeSet (FLG_EXPORTHEADER))
928 {
929 usymtab_exportHeader ();
930 }
931
932 if (context_getFlag (FLG_SHOWUSES))
933 {
934 usymtab_displayAllUses ();
935 }
936
937 context_checkSuppressCounts ();
938
939 if (context_doDump ())
940 {
941 cstring dump = context_getDump ();
942
943 dumpState (dump);
944 }
945
946# ifdef DOANNOTS
947 printAnnots ();
948# endif
949
950 cleanupFiles ();
53306cab 951
952 if (g_csvstream != NULL) {
953 displayScan (message ("Closing CSV file: %s", context_getString (FLG_CSV)));
954 check (fclose (g_csvstream) == 0);
955 }
956
616915dd 957 if (context_getFlag (FLG_SHOWSUMMARY))
958 {
959 summarizeErrors ();
960 }
616915dd 961
962 {
963 bool isQuiet = context_getFlag (FLG_QUIET);
964 cstring specErrors = cstring_undefined;
616915dd 965 int nspecErrors = lclNumberErrors ();
616915dd 966
967 expsuccess = TRUE;
968
969 if (context_neednl ())
80489f0a 970 fprintf (g_warningstream, "\n");
616915dd 971
616915dd 972 if (nspecErrors > 0)
973 {
974 if (nspecErrors == context_getLCLExpect ())
975 {
976 specErrors =
11db3170 977 message ("%d spec warning%&, as expected\n ",
616915dd 978 nspecErrors);
979 }
980 else
981 {
982 if (context_getLCLExpect () > 0)
983 {
984 specErrors =
11db3170 985 message ("%d spec warning%&, expected %d\n ",
616915dd 986 nspecErrors,
987 (int) context_getLCLExpect ());
988 }
989 else
990 {
2e127cb8 991 specErrors = message ("%d spec warning%&\n ",
616915dd 992 nspecErrors);
993 expsuccess = FALSE;
994 }
995 }
996 }
997 else
998 {
999 if (context_getLCLExpect () > 0)
1000 {
11db3170 1001 specErrors = message ("No spec warnings, expected %d\n ",
616915dd 1002 (int) context_getLCLExpect ());
1003 expsuccess = FALSE;
1004 }
1005 }
616915dd 1006
1007 if (context_anyErrors ())
1008 {
1009 if (context_numErrors () == context_getExpect ())
1010 {
1011 if (!isQuiet) {
11db3170 1012 llmsg (message ("Finished checking --- "
1013 "%s%d code warning%&, as expected",
616915dd 1014 specErrors, context_numErrors ()));
1015 }
1016 }
1017 else
1018 {
1019 if (context_getExpect () > 0)
1020 {
1021 if (!isQuiet) {
1022 llmsg (message
11db3170 1023 ("Finished checking --- "
1024 "%s%d code warning%&, expected %d",
616915dd 1025 specErrors, context_numErrors (),
1026 (int) context_getExpect ()));
1027 }
1028
1029 expsuccess = FALSE;
1030 }
1031 else
1032 {
1033
28bf4b0b 1034 if (!isQuiet)
1035 {
11db3170 1036 llmsg (message ("Finished checking --- "
2e127cb8 1037 "%s%d code warning%&",
28bf4b0b 1038 specErrors, context_numErrors ()));
1039 }
616915dd 1040
1041 expsuccess = FALSE;
1042 }
1043 }
1044 }
1045 else
1046 {
1047 if (context_getExpect () > 0)
1048 {
1049 if (!isQuiet) {
1050 llmsg (message
11db3170 1051 ("Finished checking --- "
1052 "%sno code warnings, expected %d",
616915dd 1053 specErrors,
1054 (int) context_getExpect ()));
1055 }
1056
1057 expsuccess = FALSE;
1058 }
1059 else
1060 {
1061 if (context_getLinesProcessed () > 0)
1062 {
11db3170 1063 if (cstring_isEmpty (specErrors))
1064 {
1065 if (!isQuiet)
1066 {
1067 llmsg (message ("Finished checking --- no warnings"));
1068 }
1069 }
1070 else
146e25eb 1071 {
11db3170 1072 if (!isQuiet)
1073 {
1074 llmsg (message ("Finished checking --- %sno code warnings",
1075 specErrors));
1076 }
146e25eb 1077 }
616915dd 1078 }
1079 else
1080 {
1081 if (!isQuiet) {
11db3170 1082 llmsg (message ("Finished checking --- %sno code processed",
616915dd 1083 specErrors));
1084 }
1085 }
1086 }
1087 }
1088
1089 cstring_free (specErrors);
909cf5eb 1090
1091 if (context_numBugs () > 0) {
1092 expsuccess = FALSE;
1093 if (!isQuiet) {
1094 llmsg (message (" %d internal bugs reported", context_numBugs ()));
1095 }
1096 }
616915dd 1097 }
1098
1099 if (context_getFlag (FLG_STATS))
1100 {
1101 clock_t ttime = clock () - before;
1102 int specLines = context_getSpecLinesProcessed ();
140c27a8 1103 cstring specmsg = cstring_undefined;
909cf5eb 1104
616915dd 1105 rstime = clock ();
1106
1107 if (specLines > 0)
1108 {
140c27a8 1109 specmsg = message ("%d spec, ", specLines);
616915dd 1110 }
1111
6fcd0b1e 1112 /* The clock might wrap around, not platform-independent easy way to deal with this... */
1113 if (ttime > 0)
1114 {
616915dd 1115# ifndef CLOCKS_PER_SEC
6fcd0b1e 1116 lldiagmsg (message ("%s%d source lines in %d time steps (steps/sec unknown)\n",
1117 specmsg,
1118 context_getLinesProcessed (),
1119 (int) ttime));
616915dd 1120# else
6fcd0b1e 1121 lldiagmsg (message ("%s%d source lines in %f s.\n",
1122 specmsg,
1123 context_getLinesProcessed (),
1124 (double) ttime / CLOCKS_PER_SEC));
1125 DPRINTF (("Time: %ld [%ld - %ld]", ttime, rstime, before));
616915dd 1126# endif
6fcd0b1e 1127 }
1128 else
1129 {
1130 lldiagmsg (message ("%s%d source lines\n",
1131 specmsg,
1132 context_getLinesProcessed ()));
1133 }
1134
616915dd 1135 }
1136 else
1137 {
1138 rstime = clock ();
1139 }
1140
1141 if (context_getFlag (FLG_TIMEDIST))
1142 {
1143 clock_t ttime = clock () - before;
1144
1145 if (ttime > 0)
1146 {
1147 char *msg = (char *) dmalloc (256 * sizeof (*msg));
1148
1149 if (anylcl)
1150 {
e5081f8c 1151 /* Gack: really should figure out how to make configure find snprintf... */
1152# ifdef WIN32
1153 (void) _snprintf (msg, 256,
1154# else
f9264521 1155 (void) snprintf (msg, 256,
e5081f8c 1156# endif
f9264521 1157 "Time distribution (percent): initialize %.2f / lcl %.2f / "
1158 "pre-process %.2f / c check %.2f / finalize %.2f \n",
1159 (100.0 * (double) (libtime - before) / ttime),
1160 (100.0 * (double) (lcltime - libtime) / ttime),
1161 (100.0 * (double) (pptime - lcltime) / ttime),
1162 (100.0 * (double) (cptime - pptime) / ttime),
1163 (100.0 * (double) (rstime - cptime) / ttime));
616915dd 1164 }
1165 else
1166 {
e5081f8c 1167# ifdef WIN32
1168 (void) _snprintf (msg, 256,
1169# else
f9264521 1170 (void) snprintf (msg, 256,
e5081f8c 1171# endif
f9264521 1172 "Time distribution (percent): initialize %.2f / "
1173 "pre-process %.2f / c check %.2f / finalize %.2f \n",
1174 (100.0 * (double) (libtime - before) / ttime),
1175 (100.0 * (double) (pptime - libtime) / ttime),
1176 (100.0 * (double) (cptime - pptime) / ttime),
1177 (100.0 * (double) (rstime - cptime) / ttime));
616915dd 1178 }
1179
1180 llgenindentmsgnoloc (cstring_fromCharsO (msg));
1181 }
1182 }
1183
1184 llexit (expsuccess ? LLSUCCESS : LLFAILURE);
8250fa4a 1185 BADBRANCHRET (LLFAILURE);
616915dd 1186}
1187
28bf4b0b 1188# ifdef WIN32
616915dd 1189/*
1190** Reenable return value warnings.
1191*/
28bf4b0b 1192# pragma warning (default:4035)
1193# endif
616915dd 1194
616915dd 1195void
ce7034f0 1196llinterrupt (int i)
616915dd 1197{
1198 switch (i)
1199 {
1200 case SIGINT:
80489f0a 1201 fprintf (g_errorstream, "*** Interrupt\n");
616915dd 1202 llexit (LLINTERRUPT);
1203 case SIGSEGV:
1204 {
1205 cstring loc;
1206
1207 /* Cheat when there are parse errors */
1208 checkParseError ();
1209
80489f0a 1210 fprintf (g_errorstream, "*** Segmentation Violation\n");
616915dd 1211
1212 /* Don't catch it if fileloc_unparse causes a signal */
1213 (void) signal (SIGSEGV, NULL);
1214
1215 loc = fileloc_unparse (g_currentloc);
1216
80489f0a 1217 fprintf (g_errorstream, "*** Location (not trusted): %s\n",
616915dd 1218 cstring_toCharsSafe (loc));
1219 cstring_free (loc);
1220 printCodePoint ();
80489f0a 1221 fprintf (g_errorstream, "*** Please report bug to %s\n", SPLINT_MAINTAINER);
616915dd 1222 exit (LLGIVEUP);
1223 }
1224 default:
80489f0a 1225 fprintf (g_errorstream, "*** Signal: %d\n", i);
616915dd 1226 /*@-mustfree@*/
80489f0a 1227 fprintf (g_errorstream, "*** Location (not trusted): %s\n",
616915dd 1228 cstring_toCharsSafe (fileloc_unparse (g_currentloc)));
1229 /*@=mustfree@*/
1230 printCodePoint ();
80489f0a 1231 fprintf (g_errorstream, "*** Please report bug to %s ***\n", SPLINT_MAINTAINER);
616915dd 1232 exit (LLGIVEUP);
1233 }
1234}
1235
1236void
1237cleanupFiles (void)
1238{
1239 static bool doneCleanup = FALSE;
1240
1241 /* make sure this is only called once! */
1242
1243 if (doneCleanup) return;
1244
1245 setCodePoint ();
1246
dfd82dce 1247 /*
1248 ** Close all open files
1249 ** (There should only be open files, if we exited after a fatal error.)
1250 */
1251
1252 fileTable_closeAll (context_fileTable ());
1253
616915dd 1254 if (context_getFlag (FLG_KEEP))
1255 {
80489f0a 1256 check (fputs ("Temporary files kept:\n", g_messagestream) != EOF);
616915dd 1257 fileTable_printTemps (context_fileTable ());
1258 }
1259 else
1260 {
6ee276d2 1261# if defined (WIN32) || defined (OS2) && defined (__IBMC__)
616915dd 1262 int nfiles = /*@-unrecog@*/ _fcloseall (); /*@=unrecog@*/
1263
1264 if (nfiles != 0)
1265 {
1266 llbug (message ("Files unclosed: %d", nfiles));
1267 }
1268# endif
1269 fileTable_cleanup (context_fileTable ());
1270 }
1271
1272 doneCleanup = TRUE;
1273}
1274
1275/*
11db3170 1276** cleans up temp files (if necessary) and exits
616915dd 1277*/
1278
27c9e640 1279/*@noreturn@*/ void
616915dd 1280llexit (int status)
1281{
1282 DPRINTF (("llexit: %d", status));
1283
6ee276d2 1284# if defined (WIN32) || defined (OS2) && defined (__IBMC__)
616915dd 1285 if (status == LLFAILURE)
1286 {
1287 _fcloseall ();
1288 }
1289# endif
1290
1291 cleanupFiles ();
1292
1293 if (status != LLFAILURE)
1294 {
6fcd0b1e 1295 usymtab_destroyMod ();
66a0043d 1296
1297 /*drl I'm commenting this line out
1298 because it is causing Splint to crash when built with
1299 2.95 I'm not sure if this is a compiler bug or if if has to do with bool
1300 Any way if we're going to exist the program why do we bother freeing stuff...
1301 */
1302 /* context_destroyMod (); */
616915dd 1303 exprNode_destroyMod ();
6fcd0b1e 1304 cppReader_destroyMod ();
616915dd 1305 sRef_destroyMod ();
1306 uentry_destroyMod ();
1307 typeIdSet_destroyMod ();
6fcd0b1e 1308 qual_destroyMod ();
1309 osd_destroyMod ();
1310 fileloc_destroyMod ();
616915dd 1311# ifdef USEDMALLOC
1312 dmalloc_shutdown ();
1313# endif
1314 }
1315
1316 exit ((status == LLSUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE);
1317}
1318
28bf4b0b 1319static fileIdList preprocessFiles (fileIdList fl, bool xhfiles)
616915dd 1320 /*@modifies fileSystem@*/
1321{
1322 bool msg = (context_getFlag (FLG_SHOWSCAN) && fileIdList_size (fl) > 10);
1323 int skip = (fileIdList_size (fl) / 5);
1324 int filesprocessed = 0;
1325 fileIdList dfiles = fileIdList_create ();
1326
1327 fileloc_free (g_currentloc);
1328 g_currentloc = fileloc_createBuiltin ();
1329
1330 fileIdList_elements (fl, fid)
1331 {
4dd72714 1332 cstring ppfname = fileTable_fileName (fid);
616915dd 1333
53a89507 1334 if (!(osd_fileIsReadable (ppfname)))
616915dd 1335 {
146e25eb 1336 lldiagmsg (message ("Cannot open file: %q", osd_outputPath (ppfname)));
53a89507 1337 ppfname = cstring_undefined;
28bf4b0b 1338 }
1339
1340 if (cstring_isDefined (ppfname))
1341 {
1342 fileId dfile = fileTable_addCTempFile (context_fileTable (), fid);
53a89507 1343
1344 if (xhfiles)
1345 {
1346 llassert (fileTable_isXHFile (context_fileTable (), dfile));
1347 }
1348
28bf4b0b 1349 llassert (cstring_isNonEmpty (ppfname));
616915dd 1350
1351 if (msg)
1352 {
1353 if ((filesprocessed % skip) == 0)
1354 {
1355 if (filesprocessed == 0) {
ce7034f0 1356 displayScanContinue (cstring_makeLiteral (" "));
616915dd 1357 }
1358 else {
ce7034f0 1359 displayScanContinue (cstring_makeLiteral ("."));
616915dd 1360 }
616915dd 1361 }
1362 filesprocessed++;
1363 }
1364
4dd72714 1365 DPRINTF (("outfile: %s", fileTable_fileName (dfile)));
1366
1367 if (cppProcess (ppfname, fileTable_fileName (dfile)) != 0)
616915dd 1368 {
1369 llfatalerror (message ("Preprocessing error for file: %s",
4dd72714 1370 fileTable_rootFileName (fid)));
616915dd 1371 }
1372
1373 fileIdList_add (dfiles, dfile);
1374 }
1375 } end_fileIdList_elements;
6fcd0b1e 1376
1377 return dfiles;
616915dd 1378}
28bf4b0b 1379
1380/* This should be in an lclUtils.c file... */
28bf4b0b 1381char *specFullName (char *specfile, /*@out@*/ char **inpath)
1382{
1383 /* extract the path and the specname associated with the given file */
1384 char *specname = (char *) dmalloc (sizeof (*specname)
1385 * (strlen (specfile) + 9));
1386 char *ospecname = specname;
1387 char *path = (char *) dmalloc (sizeof (*path) * (strlen (specfile)));
1388 size_t size;
1389 long int i, j;
1390
1391 /* initialized path to empty string or may have accidental garbage */
1392 *path = '\0';
1393
1394 /*@-mayaliasunique@*/
1395 strcpy (specname, specfile);
1396 /*@=mayaliasunique@*/
1397
1398 /* trim off pathnames in specfile */
1399 size = strlen (specname);
1400
1401 for (i = size_toInt (size) - 1; i >= 0; i--)
1402 {
1403 if (specname[i] == CONNECTCHAR)
1404 {
1405 /* strcpy (specname, (char *)specname+i+1); */
1406 for (j = 0; j <= i; j++) /* include '/' */
1407 {
1408 path[j] = specname[j];
1409 }
1410
1411 path[i + 1] = '\0';
1412 specname += i + 1;
1413 break;
1414 }
1415 }
1416
1417 /*
1418 ** also remove .lcl file extension, assume it's the last extension
1419 ** of the file name
1420 */
1421
1422 size = strlen (specname);
1423
1424 for (i = size_toInt (size) - 1; i >= 0; i--)
1425 {
1426 if (specname[i] == '.')
1427 {
1428 specname[i] = '\0';
1429 break;
1430 }
1431 }
1432
1433 *inpath = path;
1434
1435 /*
1436 ** If specname no longer points to the original char,
1437 ** we need to allocate a new pointer and copy the string.
1438 */
1439
1440 if (specname != ospecname) {
1441 char *rspecname = (char *) dmalloc (sizeof (*rspecname) * (strlen (specname) + 1));
1442 strcpy (rspecname, specname); /* evs 2000-05-16: Bug: was ospecname! */
1443 sfree (ospecname);
1444 return rspecname;
1445 }
1446
1447 return specname;
1448}
e55c0c6d 1449
1450void warnSysFiles(fileIdList files)
1451{
1452 fileIdList_elements (files, file)
1453 {
1454
1455 if (fileTable_isSystemFile (context_fileTable (), file) )
1456 {
d0fa7de4 1457 if (!context_getFlag( FLG_SYSTEMDIRERRORS ) )
1458 {
1459 voptgenerror (FLG_WARNSYSFILES, message ("Warning %s is a considered a system file. No errors in this file will be reported.", fileTable_rootFileName (file) ), g_currentloc);
1460 }
e55c0c6d 1461 }
1462 }
1463 end_fileIdList_elements;
1464}
This page took 0.384644 seconds and 5 git commands to generate.