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