]> andersk Git - splint.git/blame - src/context.c
Added test case for pproblem with typedefs and fixed size arrays.
[splint.git] / src / context.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** context.c
26*/
27/*
28 * Modified by Herbert 04/19/97:
29 * - added include for new header portab.h containing OS dependent stuff.
30 * - changed occurrances of '/' as path delimiters to macro from portab.h
31 * - changed the handling of the tmp directory -- will no longer always be
32 * ".", if possible, environment variables "TMP" or, if not set, "TMP",
33 * or, if not set "." will be used.
34 */
35
1b8ae690 36# include "splintMacros.nf"
616915dd 37# include "llbasic.h"
38
39# ifndef NOLCL
40# include "usymtab_interface.h"
41# endif
42
43# include "exprChecks.h"
44# include "filelocStack.h"
45# include "fileIdList.h"
46# include "llmain.h"
47# include "intSet.h"
48# include "osd.h"
49# include "portab.h"
50
51extern /*@external@*/ int yydebug;
28bf4b0b 52extern /*@external@*/ int mtdebug;
616915dd 53
54typedef struct
55{
56 cstring file;
57 typeIdSet daccess ;
58} maccesst;
59
60typedef enum {
28bf4b0b 61 CX_ERROR,
62
616915dd 63 CX_GLOBAL, CX_INNER,
d9a28762 64 CX_FUNCTION, CX_FCNDECLARATION,
616915dd 65 CX_MACROFCN, CX_MACROCONST, CX_UNKNOWNMACRO,
7ebcc5bb 66 CX_ITERDEF, CX_ITEREND,
67 CX_OLDSTYLESCOPE, /* Parsing old-style parameter declarations */
28bf4b0b 68 CX_LCL, CX_LCLLIB, CX_MT
616915dd 69} kcontext;
70
28bf4b0b 71static struct
616915dd 72{
73 int linesprocessed;
74 int speclinesprocessed;
75
76 flagMarkerList markers;
77
78 /*
79 ** used to record state where a macro must match a function name
80 ** (i.e., no params were listed in the macro definition
81 */
82
83 bool macroMissingParams BOOLBITS;
84 bool preprocessing BOOLBITS;
85 bool incommandline BOOLBITS;
86 bool insuppressregion BOOLBITS;
87 bool inDerivedFile BOOLBITS;
88 bool instandardlib BOOLBITS;
89 bool inimport BOOLBITS;
90 bool inheader BOOLBITS;
91 bool inmacrocache BOOLBITS;
92 bool protectVars BOOLBITS;
93 bool neednl BOOLBITS;
94 bool showfunction BOOLBITS;
95 bool savedFlags BOOLBITS;
96 bool justpopped BOOLBITS;
97 bool anyExports BOOLBITS;
d9a28762 98 bool inFunctionHeader BOOLBITS;
616915dd 99
100 flagcode library;
101
102 ynm isNullGuarded;
103 fileloc saveloc;
104 fileloc pushloc;
105
106 clauseStack clauses;
107 clause inclause;
108
109 int numerrors;
110
111 filelocStack locstack;
112 fileTable ftab;
113 cstring msgAnnote;
114 /*@observer@*/ sRef aliasAnnote;
115 /*@observer@*/ sRef aliasAnnoteAls;
116 messageLog msgLog;
117
118 macrocache mc;
119 /*@observer@*/ sRefSet mods;
120
121 /* file access types */
122 typeIdSet facct;
123
124 /* local access types (this function) */
125 typeIdSet acct;
126
127 /* no access types (@noaccess) */
128 typeIdSet nacct;
129
130 /*@observer@*/ globSet globs;
131 /*@only@*/ globSet globs_used;
132
133 int nmods;
134 int maxmods;
135 /*@reldef@*/ maccesst *moduleaccess; /* Not defined is nmods == 0. */
136
137 kcontext kind;
138
139 ctype boolType;
140
141 bool flags[NUMFLAGS];
142 bool saveflags[NUMFLAGS];
143 bool setGlobally[NUMFLAGS];
144 bool setLocally[NUMFLAGS];
145
146 int values[NUMVALUEFLAGS];
147 int counters[NUMVALUEFLAGS];
148
149 o_cstring strings[NUMSTRINGFLAGS];
28bf4b0b 150 sRefSetList modrecs; /*@i32 ???? what is this for? */
616915dd 151
28bf4b0b 152 metaStateTable stateTable; /* User-defined state information. */
153 annotationTable annotTable; /* User-defined annotations table. */
b7e84605 154 union u_cont
616915dd 155 {
156 bool glob;
157 int cdepth;
158 /*@dependent@*/ /*@exposed@*/ uentry fcn;
159 } cont;
b7e84605 160
161 kcontext savekind;
162 union u_cont savecont;
616915dd 163} gc;
164
165static /*@exposed@*/ cstring context_exposeString (flagcode p_flag) ;
166static void context_restoreFlagSettings (void) /*@modifies gc@*/ ;
167static void context_saveFlagSettings (void) /*@modifies gc@*/ ;
168static void context_exitClauseAux (exprNode p_pred, exprNode p_tbranch)
169 /*@modifies gc@*/ ;
170static void context_exitClauseSimp (void) /*@modifies gc@*/ ;
171static void context_exitClausePlain (void) /*@modifies gc@*/ ;
172static void context_setJustPopped (void) /*@modifies gc.justpopped@*/ ;
173static void context_setValue (flagcode p_flag, int p_val) /*@modifies gc.flags@*/ ;
990ec868 174static void context_setFlag (flagcode p_f, bool p_b)
616915dd 175 /*@modifies gc.flags@*/ ;
176
177static void
178 context_setFlagAux (flagcode p_f, bool p_b, bool p_inFile, bool p_isRestore)
179 /*@modifies gc.flags@*/ ;
180
181static void context_restoreFlag (flagcode p_f) /*@modifies gc.flags@*/ ;
182
183/*@+enumindex@*/
184
185cstring context_unparseFlagMarkers ()
186{
187 return (flagMarkerList_unparse (gc.markers));
188}
189
190void context_setPreprocessing (void)
191{
192 llassert (!gc.preprocessing);
193 gc.preprocessing = TRUE;
194}
195
196void context_clearPreprocessing (void)
197{
198 llassert (gc.preprocessing);
199 gc.preprocessing = FALSE;
200}
201
202bool context_isPreprocessing (void)
203{
204 return gc.preprocessing;
205}
206
990ec868 207bool context_loadingLibrary (void)
208{
209 return (fileloc_isLib (g_currentloc));
210}
211
28bf4b0b 212bool context_inXHFile (void)
213{
214 return (fileloc_isXHFile (g_currentloc));
215}
216
616915dd 217void context_setInCommandLine (void)
218{
219 llassert (!gc.incommandline);
220 gc.incommandline = TRUE;
221}
222
223void context_clearInCommandLine (void)
224{
225 llassert (gc.incommandline);
226 gc.incommandline = FALSE;
227}
228
229bool context_isInCommandLine (void)
230{
231 return gc.incommandline;
232}
233
234static
235void pushClause (clause c) /*@modifies gc.clauses, gc.inclause@*/
236{
237 gc.inclause = c;
238 clauseStack_push (gc.clauses, c);
239
240 if (clause_isConditional (c)
241 && context_getFlag (FLG_CONTROLNESTDEPTH))
242 {
243 int maxdepth = context_getValue (FLG_CONTROLNESTDEPTH);
244 int depth = clauseStack_controlDepth (gc.clauses);
245
246 if (depth == maxdepth + 1)
247 {
248 voptgenerror
249 (FLG_CONTROLNESTDEPTH,
250 message ("Maximum control nesting depth "
251 "(%d) exceeded",
252 maxdepth),
253 g_currentloc);
254 }
255 }
256}
257
258static
259clause topClause (clauseStack s) /*@*/
260{
261 if (clauseStack_isEmpty (s)) return NOCLAUSE;
262 return ((clause) clauseStack_top (s));
263}
264
265void
266context_addMacroCache (/*@only@*/ cstring def)
267{
28bf4b0b 268 DPRINTF (("macro cache: %s", def));
269 macrocache_addEntry (gc.mc, fileloc_copy (g_currentloc), def);
616915dd 270}
271
272void
273context_addComment (/*@only@*/ cstring def)
274{
28bf4b0b 275 DPRINTF (("macro comment: %s", def));
276 macrocache_addComment (gc.mc, fileloc_copy (g_currentloc), def);
616915dd 277}
278
279/*
280** returns TRUE is fl is in ignore region, or region where -code
281**
282** the logic is fuzzy...
283*/
284
285static bool
286context_inSuppressFlagZone (fileloc fl, flagcode code)
287{
288 ynm ret = flagMarkerList_suppressError (gc.markers, code, fl);
289 bool res = FALSE;
290
291 if (ynm_isMaybe (ret))
292 {
293 /*
294 ** whas is dis?
295 */
296
297 if (gc.savedFlags)
298 {
299 res = !gc.saveflags[code];
300 }
301 else
302 {
303 res = !context_getFlag (code);
304 }
305 }
306 else
307 {
308 res = ynm_toBoolStrict (ret);
309 }
310
311 return res;
312}
313
314static bool
315context_suppressSystemMsg (fileloc fl)
316{
317 if (context_getFlag (FLG_SYSTEMDIRERRORS))
318 {
319 return FALSE;
320 }
321 else
322 {
323 return (fileloc_isSystemFile (fl));
324 }
325}
326
327bool
328context_suppressFlagMsg (flagcode flag, fileloc fl)
329{
330 if (context_suppressSystemMsg (fl))
331 {
332 return TRUE;
333 }
334
60eced23 335 DPRINTF (("Checking suppress: %s / %s", fileloc_unparse (fl), fileloc_unparse (g_currentloc)));
336
616915dd 337 /* want same object compare here */
338
339 if (fileloc_equal (fl, g_currentloc) || gc.inDerivedFile)
340 {
60eced23 341 DPRINTF (("In derived file: %s", bool_unparse (gc.inDerivedFile)));
342
616915dd 343 return (!context_getFlag (flag)
344 || context_inSuppressRegion ()
345 || context_inSuppressZone (fl)
60eced23 346 || (/*@!@@#@ gc.inDerivedFile && */ context_inSuppressFlagZone (fl, flag)));
616915dd 347 }
348 else
349 {
28bf4b0b 350 return (context_inSuppressFlagZone (fl, flag));
616915dd 351 }
352}
353
354bool
355context_suppressNotFlagMsg (flagcode flag, fileloc fl)
356{
357
358 if (context_suppressSystemMsg (fl))
359 {
360 return TRUE;
361 }
362
363 /*@access fileloc@*/
364 if (fl == g_currentloc)
365 /*@noaccess fileloc@*/
366 {
367 return (context_getFlag (flag) || context_inSuppressRegion ());
368 }
369 else
370 {
371 /* for now... */
372 return (context_getFlag (flag) || context_inSuppressRegion ());
373 }
374}
375
376bool
377context_inSuppressZone (fileloc fl)
378{
379 if (context_suppressSystemMsg (fl))
380 {
381 return TRUE;
382 }
383
384 return (flagMarkerList_inIgnore (gc.markers, fl));
385}
386
387bool
388context_inSuppressRegion (void)
389{
390 return (gc.insuppressregion);
391}
392
393void
394context_enterSuppressRegion (void)
395{
396 if (gc.insuppressregion)
397 {
398 gc.insuppressregion = FALSE; /* get this msg! */
399 llmsg (message
400 ("%q: New ignore errors region entered while in ignore errors region",
401 fileloc_unparse (g_currentloc)));
402 }
403
404 gc.insuppressregion = TRUE;
405 flagMarkerList_add (gc.markers, flagMarker_createIgnoreOn (g_currentloc));
406}
407
408static void
409context_addFlagMarker (flagcode code, ynm set)
410{
411 flagMarkerList_add (gc.markers,
412 flagMarker_createLocalSet (code, set, g_currentloc));
413}
414
415void
416context_enterSuppressLine (int count)
417{
418 fileloc nextline = fileloc_copy (g_currentloc);
419
420 flagMarkerList_add (gc.markers,
421 flagMarker_createIgnoreCount (count, g_currentloc));
422
423 fileloc_nextLine (nextline);
424 flagMarkerList_add (gc.markers,
425 flagMarker_createIgnoreOff (nextline));
426 fileloc_free (nextline);
427}
428
429void context_checkSuppressCounts (void)
430{
431 if (context_getFlag (FLG_SUPCOUNTS))
432 {
433 flagMarkerList_checkSuppressCounts (gc.markers);
434 }
435}
436
437void context_incLineno (void)
438{
439 gc.linesprocessed++;
440 incLine ();
441}
442
443void
444context_exitSuppressRegion (void)
445{
446 if (!gc.insuppressregion)
447 {
448 llerrorlit (FLG_SYNTAX,
449 "End ignore errors in region while not ignoring errors");
450 }
451
452 gc.insuppressregion = FALSE;
453 flagMarkerList_add (gc.markers, flagMarker_createIgnoreOff (g_currentloc));
454}
455
28bf4b0b 456void
457context_enterMTfile (void)
458{
459 gc.kind = CX_MT;
460}
461
462void
463context_exitMTfile (void)
464{
465 llassert (gc.kind == CX_MT);
466 gc.kind = CX_GLOBAL;
467}
468
616915dd 469# ifndef NOLCL
470void
471context_enterLCLfile (void)
472{
473 gc.kind = CX_LCL;
474 gc.facct = typeIdSet_emptySet ();
475}
476# endif
477
478static void
479addModuleAccess (/*@only@*/ cstring fname, typeIdSet mods)
480{
481 int i;
482
483 for (i = 0; i < gc.nmods; i++)
484 {
485 if (cstring_equal (gc.moduleaccess[i].file, fname))
486 {
487 gc.moduleaccess[i].daccess = typeIdSet_union (gc.moduleaccess[i].daccess, mods);
488 cstring_free (fname);
489 return;
490 }
491 }
492
493 if (gc.nmods == gc.maxmods)
494 {
495 maccesst *oldmods;
496
497 gc.maxmods = gc.maxmods + DEFAULTMAXMODS;
498 oldmods = gc.moduleaccess;
499
500 gc.moduleaccess = (maccesst *) dmalloc (sizeof (*gc.moduleaccess) * (gc.maxmods));
501
502 for (i = 0; i < gc.nmods; i++)
503 {
504 gc.moduleaccess[i] = oldmods[i];
505 }
506
507 sfree (oldmods);
508 }
509
510 gc.moduleaccess[gc.nmods].file = fname;
511 gc.moduleaccess[gc.nmods].daccess = mods;
512
513 gc.nmods++;
514}
515
516static void
517insertModuleAccess (cstring fname, typeId t)
518{
519 int i;
520
521 for (i = 0; i < gc.nmods; i++)
522 {
523 if (cstring_equal (gc.moduleaccess[i].file, fname))
524 {
525 gc.moduleaccess[i].daccess = typeIdSet_insert (gc.moduleaccess[i].daccess, t);
526 break;
527 }
528 }
529
530 addModuleAccess (cstring_copy (fname), typeIdSet_single (t));
531}
532
533# ifndef NOLCL
534void
535context_exitLCLfile (void)
536{
537 if (gc.kind != CX_LCLLIB)
538 {
28bf4b0b 539 cstring lclname =
4dd72714 540 fileLib_withoutExtension (fileTable_fileName (currentFile ()), LCL_EXTENSION);
616915dd 541
28bf4b0b 542 addModuleAccess (fileLib_removePath (lclname), gc.facct);
543 cstring_free (lclname);
616915dd 544 }
545
546 gc.kind = CX_LCL;
547 gc.kind = CX_GLOBAL;
548 gc.facct = typeIdSet_emptySet ();
549}
550# endif
551
552void
553context_dumpModuleAccess (FILE *fout)
554{
555 int i = 0;
556
557 for (i = 0; i < gc.nmods; i++)
558 {
559 cstring td = typeIdSet_dump (gc.moduleaccess[i].daccess);
560
561 fprintf (fout, "%s#%s@\n",
562 cstring_toCharsSafe (gc.moduleaccess[i].file),
563 cstring_toCharsSafe (td));
564
565 cstring_free (td);
566 }
567}
568
569bool context_usingPosixLibrary ()
570{
571 return (gc.library == FLG_POSIXLIB
572 || gc.library == FLG_POSIXSTRICTLIB
573 || gc.library == FLG_UNIXLIB
574 || gc.library == FLG_UNIXSTRICTLIB);
575}
576
577bool context_usingAnsiLibrary ()
578{
579 return (gc.library != FLG_NOLIB);
580}
581
582flagcode context_getLibrary ()
583{
584 return gc.library;
585}
586
587void context_setLibrary (flagcode code)
588{
589 gc.library = code;
590}
591
28bf4b0b 592/*@observer@*/ cstring context_selectedLibrary ()
616915dd 593{
594 switch (gc.library)
595 {
596 case FLG_STRICTLIB:
28bf4b0b 597 return cstring_makeLiteralTemp (LLSTRICTLIBS_NAME);
616915dd 598 case FLG_POSIXLIB:
28bf4b0b 599 return cstring_makeLiteralTemp (LLPOSIXLIBS_NAME);
616915dd 600 case FLG_POSIXSTRICTLIB:
28bf4b0b 601 return cstring_makeLiteralTemp (LLPOSIXSTRICTLIBS_NAME);
616915dd 602 case FLG_UNIXLIB:
28bf4b0b 603 return cstring_makeLiteralTemp (LLUNIXLIBS_NAME);
616915dd 604 case FLG_UNIXSTRICTLIB:
28bf4b0b 605 return cstring_makeLiteralTemp (LLUNIXSTRICTLIBS_NAME);
616915dd 606 case FLG_ANSILIB:
28bf4b0b 607 return cstring_makeLiteralTemp (LLSTDLIBS_NAME);
616915dd 608 BADDEFAULT;
609 }
610}
611
612
613void
614context_loadModuleAccess (FILE *in)
615{
616 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
617 char *lasts = s;
618 char *name = mstring_create (MAX_NAME_LENGTH);
619 char *oname = name;
620# ifndef NOFREE
621 char *os = s;
622# endif
623
28bf4b0b 624 while ((reader_readLine (in, s, MAX_DUMP_LINE_LENGTH) != NULL )
616915dd 625 && *s == ';')
626 {
627 ;
628 }
629
630 while (s != NULL && *s != ';' && *s != '\0')
631 {
632 name = oname;
633
634 while (*s != '#' && *s != '\0')
635 {
636 *name++ = *s++;
637 }
638
639 *name = '\0';
640
641 if (*s != '#')
642 {
643 llcontbug (message ("context_loadModuleAccess: bad library line: %s\n",
644 cstring_fromChars (s)));
645 break;
646 }
647
648 s++;
649
650 addModuleAccess (cstring_copy (cstring_fromChars (oname)),
651 typeIdSet_undump (&s));
652
28bf4b0b 653 (void) reader_readLine (in, s, MAX_DUMP_LINE_LENGTH);
616915dd 654 llassert (s != lasts);
655 lasts = s;
656 }
657
658 sfree (oname);
659# ifndef NOFREE
660 sfree (os);
661# endif
662}
663
664typeIdSet context_fileAccessTypes (void)
665{
666 return gc.facct;
667}
668
669void
670context_resetModeFlags (void)
671{
672
673 allFlagCodes (code)
674 {
675 if (flagcode_isModeFlag (code))
676 {
677 context_setFlag (code, FALSE);
678 }
982cc10b 679 } end_allFlagCodes;
680}
616915dd 681
682/*
683** resetAllFlags
684**
685** Set all flags to FALSE, except for a few which are
686** true by default.
687**
688** Set all values and strings to appropriate defaults.
689** Set all counters to 0.
690*/
691
692static void
693conext_resetAllCounters (void)
694{
695 int i;
696
697 for (i = 0; i < NUMVALUEFLAGS; i++)
698 {
699 gc.counters[i] = 0;
700 }
701}
702
703void
3120b462 704context_resetAllFlags (void)
616915dd 705{
982cc10b 706 DPRINTF (("******** Reset all flags"));
707
616915dd 708 allFlagCodes (code)
709 {
710 gc.flags[code] = FALSE;
711
4f43223c 712 if (flagcode_hasNumber (code))
616915dd 713 {
714 int val = 0;
715
716 /*@-loopswitchbreak@*/
717 switch (code)
718 {
719 case FLG_LIMIT:
720 val = DEFAULT_LIMIT; break;
28bf4b0b 721 case FLG_BUGSLIMIT:
722 val = DEFAULT_BUGSLIMIT; break;
616915dd 723 case FLG_LINELEN:
724 val = DEFAULT_LINELEN; break;
28bf4b0b 725 case FLG_INDENTSPACES:
726 val = DEFAULT_INDENTSPACES; break;
616915dd 727 case FLG_EXTERNALNAMELEN:
27c9e640 728 val = ISO99_EXTERNALNAMELEN; break;
616915dd 729 case FLG_INTERNALNAMELEN:
27c9e640 730 val = ISO99_INTERNALNAMELEN; break;
616915dd 731 case FLG_COMMENTCHAR:
732 val = (int) DEFAULT_COMMENTCHAR; break;
733 case FLG_CONTROLNESTDEPTH:
27c9e640 734 val = (int) ISO99_CONTROLNESTDEPTH; break;
616915dd 735 case FLG_STRINGLITERALLEN:
27c9e640 736 val = (int) ISO99_STRINGLITERALLEN; break;
616915dd 737 case FLG_INCLUDENEST:
27c9e640 738 val = (int) ISO99_INCLUDENEST; break;
616915dd 739 case FLG_NUMSTRUCTFIELDS:
27c9e640 740 val = (int) ISO99_NUMSTRUCTFIELDS; break;
616915dd 741 case FLG_NUMENUMMEMBERS:
27c9e640 742 val = (int) ISO99_NUMENUMMEMBERS; break;
616915dd 743 case FLG_EXPECT:
744 case FLG_LCLEXPECT:
745 break;
746 default:
747 llbug (message ("Bad value flag: %s", flagcode_unparse (code)));
748 }
749 /*@=loopswitchbreak@*/
750
982cc10b 751 DPRINTF (("Set value: [%s] / %d", flagcode_unparse (code), val));
616915dd 752 context_setValue (code, val);
982cc10b 753 DPRINTF (("Set value: [%s] / %d", flagcode_unparse (code), context_getValue (code)));
754 llassert (context_getValue (code) == val);
616915dd 755 }
4f43223c 756 else if (flagcode_hasChar (code))
757 {
758 llassert (code == FLG_COMMENTCHAR);
759 context_setCommentMarkerChar (DEFAULT_COMMENTCHAR);
4f43223c 760 }
616915dd 761 else if (flagcode_hasString (code))
762 {
763 cstring val = cstring_undefined;
764
765 switch (code)
766 { /*@-loopswitchbreak@*/
767 case FLG_LARCHPATH:
768 {
28bf4b0b 769 cstring larchpath = osd_getEnvironmentVariable (LARCH_PATH);
616915dd 770
28bf4b0b 771 if (cstring_isDefined (larchpath))
616915dd 772 {
28bf4b0b 773 val = cstring_copy (larchpath);
616915dd 774 }
775 else
776 {
777 val = cstring_makeLiteral (DEFAULT_LARCHPATH);
778 }
779
780 break;
781 }
782 case FLG_LCLIMPORTDIR:
783 {
28bf4b0b 784 val = cstring_copy (osd_getEnvironment (cstring_makeLiteralTemp (LCLIMPORTDIR), cstring_makeLiteralTemp (DEFAULT_LCLIMPORTDIR)));
616915dd 785 break;
786 }
787 case FLG_TMPDIR:
788# if defined(OS2) || defined(MSDOS) || defined(WIN32)
789 {
790 char *env = osd_getEnvironmentVariable ("TMP");
791
792 if (env == NULL)
793 {
794 env = osd_getEnvironmentVariable ("TEMP");
795 }
796
797 val = cstring_makeLiteral (env != NULL ? env : DEFAULT_TMPDIR);
798 }
799# else
800 val = cstring_makeLiteral (DEFAULT_TMPDIR);
801# endif /* !defined(OS2) && !defined(MSDOS) */
802
803 break;
804 case FLG_BOOLTYPE:
805 val = cstring_makeLiteral (DEFAULT_BOOLTYPE); break;
806 case FLG_BOOLFALSE:
807 val = cstring_makeLiteral ("FALSE"); break;
808 case FLG_BOOLTRUE:
809 val = cstring_makeLiteral ("TRUE"); break;
810 case FLG_MACROVARPREFIX:
811 val = cstring_makeLiteral ("m_"); break;
812 case FLG_SYSTEMDIRS:
813 val = cstring_makeLiteral (DEFAULT_SYSTEMDIR); break;
814 default:
815 break;
816 } /*@=loopswitchbreak@*/
817
818 context_setString (code, val);
819 }
820 else
821 {
822 ; /* nothing to set */
823 }
824 } end_allFlagCodes;
825
826 /*
827 ** These flags are true by default.
828 */
829
28bf4b0b 830 /*@i34 move this into flags.def */
831
3e3ec469 832 gc.flags[FLG_OBVIOUSLOOPEXEC] = TRUE;
616915dd 833 gc.flags[FLG_MODIFIES] = TRUE;
834 gc.flags[FLG_NESTCOMMENT] = TRUE;
835 gc.flags[FLG_GLOBALS] = TRUE;
836 gc.flags[FLG_FULLINITBLOCK] = TRUE;
7534721d 837 gc.flags[FLG_INITSIZE] = TRUE;
838 gc.flags[FLG_INITALLELEMENTS] = TRUE;
839
840 gc.flags[FLG_STRINGLITTOOLONG] = TRUE;
841
616915dd 842 gc.flags[FLG_LIKELYBOOL] = TRUE;
843 gc.flags[FLG_ZEROPTR] = TRUE;
844 gc.flags[FLG_NUMLITERAL] = TRUE;
845 gc.flags[FLG_DUPLICATEQUALS] = TRUE;
846 gc.flags[FLG_SKIPANSIHEADERS] = TRUE;
847 gc.flags[FLG_SKIPPOSIXHEADERS] = TRUE;
848 gc.flags[FLG_SYSTEMDIREXPAND] = TRUE;
849 gc.flags[FLG_UNRECOGCOMMENTS] = TRUE;
990ec868 850 gc.flags[FLG_UNRECOGFLAGCOMMENTS] = TRUE;
616915dd 851 gc.flags[FLG_CASTFCNPTR] = TRUE;
852 gc.flags[FLG_DOLCS] = TRUE;
853 gc.flags[FLG_USEVARARGS] = TRUE;
854 gc.flags[FLG_MAINTYPE] = TRUE;
855 gc.flags[FLG_SPECMACROS] = TRUE;
856 gc.flags[FLG_REDEF] = TRUE;
857 gc.flags[FLG_MACRONEXTLINE] = TRUE;
858
859 gc.flags[FLG_SIZEOFFORMALARRAY] = TRUE;
860 gc.flags[FLG_FIXEDFORMALARRAY] = TRUE;
861
4c9767b3 862 gc.flags[FLG_UNRECOGDIRECTIVE] = TRUE;
28bf4b0b 863 gc.flags[FLG_WARNUSE] = TRUE;
616915dd 864 gc.flags[FLG_PREDASSIGN] = TRUE;
865 gc.flags[FLG_MODOBSERVER] = TRUE;
866 gc.flags[FLG_MACROVARPREFIXEXCLUDE] = TRUE;
867 gc.flags[FLG_EXTERNALNAMECASEINSENSITIVE] = TRUE;
868
869 gc.flags[FLG_PARAMIMPTEMP] = TRUE;
870 gc.flags[FLG_RETIMPONLY] = TRUE;
871 gc.flags[FLG_GLOBIMPONLY] = TRUE;
872 gc.flags[FLG_STRUCTIMPONLY] = TRUE;
873 gc.flags[FLG_PREPROC] = TRUE;
874 gc.flags[FLG_NAMECHECKS] = TRUE;
875 gc.flags[FLG_FORMATCODE] = TRUE;
876 gc.flags[FLG_FORMATTYPE] = TRUE;
877 gc.flags[FLG_BADFLAG] = TRUE;
878 gc.flags[FLG_WARNFLAGS] = TRUE;
60868d40 879 gc.flags[FLG_WARNRC] = TRUE;
28bf4b0b 880 gc.flags[FLG_FILEEXTENSIONS] = TRUE;
616915dd 881 gc.flags[FLG_WARNUNIXLIB] = TRUE;
882 gc.flags[FLG_WARNPOSIX] = TRUE;
883 gc.flags[FLG_SHOWCOL] = TRUE;
884 gc.flags[FLG_SHOWFUNC] = TRUE;
885 gc.flags[FLG_SUPCOUNTS] = TRUE;
886 gc.flags[FLG_HINTS] = TRUE;
887 gc.flags[FLG_SYNTAX] = TRUE;
888 gc.flags[FLG_TYPE] = TRUE;
889 gc.flags[FLG_INCOMPLETETYPE] = TRUE;
890 gc.flags[FLG_ABSTRACT] = TRUE;
bb7c2085 891 gc.flags[FLG_ITERBALANCE] = TRUE;
892 gc.flags[FLG_ITERYIELD] = TRUE;
893 gc.flags[FLG_DUPLICATECASES] = TRUE;
894 gc.flags[FLG_ALWAYSEXITS] = TRUE;
895 gc.flags[FLG_EMPTYRETURN] = TRUE;
896 gc.flags[FLG_MACRORETURN] = TRUE;
616915dd 897 gc.flags[FLG_UNRECOG] = TRUE;
898 gc.flags[FLG_SYSTEMUNRECOG] = TRUE;
899 gc.flags[FLG_LINTCOMMENTS] = TRUE;
900 gc.flags[FLG_ACCESSCZECH] = TRUE;
901 gc.flags[FLG_ACCESSMODULE] = TRUE;
902 gc.flags[FLG_ACCESSFILE] = TRUE;
903 gc.flags[FLG_MACROVARPREFIX] = TRUE;
904
28bf4b0b 905 gc.flags[FLG_ANNOTATIONERROR] = TRUE;
906 gc.flags[FLG_COMMENTERROR] = TRUE;
907
616915dd 908 /*
909 ** Changed for version 2.4.
910 */
911
912 gc.flags[FLG_GNUEXTENSIONS] = TRUE;
913
4a689c7f 914 /*
915 Changed for 3.0.0.19
916 */
917 gc.flags[FLG_ORCONSTRAINT] = TRUE;
918 gc.flags[FLG_CONSTRAINTLOCATION] = TRUE;
e55c0c6d 919
920 /*drl 1/18/2002*/
921 gc.flags[FLG_WARNSYSFILES] = TRUE;
922
616915dd 923 /*
924 ** On by default for Win32, but not Unix (to support MS/VC++ error message format).
925 */
926
927# ifdef WIN32
928 gc.flags[FLG_PARENFILEFORMAT] = TRUE;
929# endif
930}
931
932/*
933** C is way-lame, and you can initialize an array to a constant except where
934** it is declared. Hence, a macro is used to set the modeflags.
935*/
936
937/*@notfunction@*/
938# define SETFLAGS() \
939 { int i = 0; while (modeflags[i] != INVALID_FLAG) { \
940 if (!flagcode_isModeFlag (modeflags[i])) \
941 { llbug (message ("not a mode flag: %s", \
942 flagcode_unparse (modeflags[i]))); } \
943 else { context_setFlag (modeflags[i], TRUE); } i++; }}
944
982cc10b 945static void context_setModeAux (cstring p_s, bool p_warn) ;
946
616915dd 947void
948context_setMode (cstring s)
982cc10b 949{
950 context_setModeAux (s, TRUE);
951}
952
953void
954context_setModeNoWarn (cstring s)
955{
956 context_setModeAux (s, FALSE);
957}
958
959void
960context_setModeAux (cstring s, bool warn)
616915dd 961{
962 intSet setflags = intSet_new ();
963
964 allFlagCodes (code)
965 {
966 if (flagcode_isModeFlag (code))
967 {
968 if (gc.setGlobally[code])
969 {
970 (void) intSet_insert (setflags, (int) code);
971 }
972 }
973 } end_allFlagCodes;
974
975 if (!intSet_isEmpty (setflags))
976 {
977 cstring rflags = cstring_undefined;
978 int num = 0;
979
980 intSet_elements (setflags, el)
981 {
982 if (cstring_isUndefined (rflags))
983 {
984 rflags = cstring_copy (flagcode_unparse ((flagcode) (el)));
985 }
986 else
987 {
988 rflags = message ("%q, %s", rflags,
989 flagcode_unparse ((flagcode) el));
990 }
991
992 num++;
993 if (num > 4 && intSet_size (setflags) > 6)
994 {
995 rflags = message ("%q, (%d others) ...", rflags,
996 intSet_size (setflags) - num);
997 break;
998 }
999 } end_intSet_elements ;
1000
982cc10b 1001 if (warn)
1002 {
1003 voptgenerror (FLG_WARNFLAGS,
1004 message ("Setting mode %s after setting mode flags will "
1005 "override set values of flags: %s",
1006 s, rflags),
1007 g_currentloc);
1008 }
616915dd 1009
1010 cstring_free (rflags);
1011 }
1012
1013 intSet_free (setflags);
1014
1015 context_resetModeFlags ();
1016
1017 if (cstring_equalLit (s, "standard"))
1018 {
1019 flagcode modeflags[] =
1020 {
1021 FLG_ENUMINT, FLG_MACROMATCHNAME,
7534721d 1022 FLG_STRINGLITNOROOM,
616915dd 1023 FLG_MACROUNDEF, FLG_RELAXQUALS,
1024 FLG_USEALLGLOBS, FLG_CHECKSTRICTGLOBALS,
1025 FLG_CHECKSTRICTGLOBALIAS,
1026 FLG_CHECKEDGLOBALIAS,
1027 FLG_CHECKMODGLOBALIAS,
1028 FLG_PREDBOOLOTHERS, FLG_PREDBOOLINT,
1d239d69 1029 FLG_UNSIGNEDCOMPARE,
616915dd 1030 FLG_PARAMUNUSED, FLG_VARUNUSED, FLG_FUNCUNUSED,
1031 FLG_TYPEUNUSED,
1032 FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED, FLG_FIELDUNUSED,
1d239d69 1033 FLG_PTRNUMCOMPARE, FLG_BOOLCOMPARE, FLG_UNSIGNEDCOMPARE,
1034 FLG_MUTREP, FLG_NOEFFECT, FLG_IMPTYPE,
616915dd 1035 FLG_RETVALOTHER, FLG_RETVALBOOL, FLG_RETVALINT,
28bf4b0b 1036 FLG_SPECUNDEF, FLG_INCONDEFS, FLG_INCONDEFSLIB, FLG_MISPLACEDSHAREQUAL,
616915dd 1037 FLG_MATCHFIELDS,
28bf4b0b 1038 FLG_FORMATCONST,
616915dd 1039 FLG_MACROPARAMS, FLG_MACROASSIGN, FLG_SEFPARAMS,
1040 FLG_MACROSTMT, FLG_MACROPARENS,
1041 FLG_MACROFCNDECL,
1042 FLG_MACROCONSTDECL,
1043 FLG_MACROREDEF, FLG_INFLOOPS, FLG_UNREACHABLE,
1044 FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE, FLG_USEDEF,
1045 FLG_FIRSTCASE,
1046 FLG_NESTEDEXTERN,
1047 FLG_NUMLITERAL,
1048 FLG_ZEROBOOL,
1049 /* memchecks flags */
1050 FLG_NULLDEREF,
1051 FLG_NULLSTATE, FLG_NULLASSIGN,
1052 FLG_NULLPASS, FLG_NULLRET,
1053
1054 FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF,
1055 FLG_RETSTACK,
1056
1057 /* memtrans flags */
1058 FLG_EXPOSETRANS,
1059 FLG_OBSERVERTRANS,
1060 FLG_DEPENDENTTRANS,
1061 FLG_NEWREFTRANS,
1062 FLG_ONLYTRANS,
1063 FLG_OWNEDTRANS,
1064 FLG_FRESHTRANS,
1065 FLG_SHAREDTRANS,
1066 FLG_TEMPTRANS,
1067 FLG_KEPTTRANS,
1068 FLG_REFCOUNTTRANS,
1069 FLG_STATICTRANS,
1070 FLG_UNKNOWNTRANS,
1071 FLG_KEEPTRANS,
1072 FLG_IMMEDIATETRANS,
1073
1074 FLG_EXPORTLOCAL,
1075
1076 FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
2e127cb8 1077 FLG_MUSTFREEONLY,
1078 FLG_MUSTFREEFRESH,
1079 FLG_MUSTDEFINE, FLG_GLOBSTATE,
616915dd 1080 FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1081 FLG_MEMIMPLICIT,
1082 FLG_BRANCHSTATE,
28bf4b0b 1083 FLG_STATETRANSFER, FLG_STATEMERGE,
616915dd 1084 FLG_EVALORDER, FLG_SHADOW, FLG_READONLYSTRINGS,
1085 FLG_EXITARG,
1086 FLG_IMPCHECKEDSPECGLOBALS,
1087 FLG_MODGLOBS, FLG_WARNLINTCOMMENTS,
1088 FLG_IFEMPTY, FLG_REALCOMPARE,
1089 FLG_BOOLOPS, FLG_PTRNEGATE,
a956d444 1090 FLG_SHIFTNEGATIVE,
1091 FLG_SHIFTIMPLEMENTATION,
28bf4b0b 1092 FLG_BUFFEROVERFLOWHIGH,
1093 FLG_BUFFEROVERFLOW,
1094 INVALID_FLAG
1095 } ;
616915dd 1096
1097 SETFLAGS ();
1098 }
1099 else if (cstring_equalLit (s, "weak"))
1100 {
1101 flagcode modeflags[] =
1102 {
1103 FLG_BOOLINT, FLG_CHARINT, FLG_FLOATDOUBLE,
1104 FLG_ENUMINT, FLG_RELAXQUALS, FLG_FORWARDDECL,
1105 FLG_CHARINDEX, FLG_ABSTVOIDP, FLG_USEALLGLOBS,
1106 FLG_CHARUNSIGNEDCHAR,
1107 FLG_PREDBOOLOTHERS,
1108 FLG_VARUNUSED, FLG_FUNCUNUSED,
1109 FLG_TYPEUNUSED,
1110 FLG_CHECKSTRICTGLOBALS, FLG_MACROMATCHNAME,
1111 FLG_RETVALOTHER,
1112 FLG_IFEMPTY,
1d239d69 1113 FLG_BUFFEROVERFLOWHIGH,
616915dd 1114 FLG_RETSTACK, FLG_PTRNEGATE,
28bf4b0b 1115 FLG_STATETRANSFER, FLG_STATEMERGE,
616915dd 1116 FLG_LONGUNSIGNEDINTEGRAL,
1117 FLG_LONGUNSIGNEDUNSIGNEDINTEGRAL,
1118 FLG_NUMLITERAL,
1119 FLG_CHARINTLITERAL,
1120 FLG_ZEROBOOL,
28bf4b0b 1121 FLG_BUFFEROVERFLOWHIGH,
616915dd 1122 INVALID_FLAG
1123 } ;
1124
1125 SETFLAGS ();
1126 }
1127 else if (cstring_equalLit (s, "checks"))
1128 {
1129 flagcode modeflags[] =
1130 {
1131 FLG_EXPORTLOCAL, FLG_IMPTYPE,
28bf4b0b 1132 FLG_STATETRANSFER, FLG_STATEMERGE,
616915dd 1133 FLG_CHECKSTRICTGLOBALIAS,
1134 FLG_CHECKEDGLOBALIAS,
1135 FLG_CHECKMODGLOBALIAS,
1136 FLG_UNCHECKEDGLOBALIAS,
28bf4b0b 1137 FLG_FORMATCONST,
7534721d 1138 FLG_STRINGLITNOROOM,
1810fe2a 1139 FLG_STRINGLITNOROOMFINALNULL,
7534721d 1140 FLG_STRINGLITSMALLER,
616915dd 1141 FLG_EXITARG, FLG_PTRNUMCOMPARE,
1d239d69 1142 FLG_BOOLCOMPARE, FLG_UNSIGNEDCOMPARE,
1143 FLG_MACROUNDEF, FLG_MUSTMOD, FLG_ALLGLOBALS,
616915dd 1144 FLG_PREDBOOLOTHERS, FLG_PREDBOOLPTR, FLG_PREDBOOLINT,
1145 FLG_USEALLGLOBS, FLG_MUTREP, FLG_RETALIAS,
1146 FLG_RETEXPOSE, FLG_ASSIGNEXPOSE, FLG_CASTEXPOSE,
1147 FLG_FUNCUNUSED, FLG_GLOBALSIMPMODIFIESNOTHING,
1148 FLG_TYPEUNUSED, FLG_FIELDUNUSED, FLG_PARAMUNUSED, FLG_VARUNUSED,
1149 FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED,
1150 FLG_NOEFFECT, FLG_EXPORTHEADER, FLG_EXPORTHEADERVAR,
1151 FLG_RETVALOTHER, FLG_RETVALBOOL, FLG_RETVALINT,
1152 FLG_SPECUNDEF, FLG_IMPCHECKMODINTERNALS,
1153 FLG_DECLUNDEF, FLG_INCONDEFS, FLG_INCONDEFSLIB,
28bf4b0b 1154 FLG_MISPLACEDSHAREQUAL, FLG_REDUNDANTSHAREQUAL,
616915dd 1155 FLG_MATCHFIELDS,
1156 FLG_MACROPARAMS,
1157 FLG_MACROASSIGN,
1158 FLG_DECLPARAMMATCH,
1159 FLG_FCNDEREF,
1160 FLG_FIRSTCASE,
1161 FLG_SEFPARAMS, FLG_SEFUNSPEC, FLG_MACROSTMT, FLG_MACROPARENS,
1162 FLG_MACROCONSTDECL,
1163 FLG_MACROFCNDECL,
1164 FLG_MACROREDEF,
1165 FLG_INFLOOPS, FLG_INFLOOPSUNCON,
1166 FLG_UNREACHABLE,
1167 FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE,
1168 FLG_EVALORDER, FLG_USEDEF,
616915dd 1169 FLG_NESTEDEXTERN,
1170
1d239d69 1171 /* warn use flags */
1172 FLG_MULTITHREADED, FLG_PORTABILITY, FLG_SUPERUSER, FLG_IMPLEMENTATIONOPTIONAL,
1173 FLG_BUFFEROVERFLOWHIGH,
1174
616915dd 1175 /* memchecks flags */
1176
1177 FLG_NULLSTATE, FLG_NULLDEREF, FLG_NULLASSIGN,
1178 FLG_NULLPASS, FLG_NULLRET,
1179
1180 FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF, FLG_RETSTACK,
1181
1182 /* memtrans flags */
1183 FLG_EXPOSETRANS,
1184 FLG_OBSERVERTRANS,
1185 FLG_DEPENDENTTRANS,
1186 FLG_NEWREFTRANS,
1187 FLG_ONLYTRANS,
1188 FLG_OWNEDTRANS,
1189 FLG_FRESHTRANS,
1190 FLG_SHAREDTRANS,
1191 FLG_TEMPTRANS,
1192 FLG_KEPTTRANS,
1193 FLG_REFCOUNTTRANS,
1194 FLG_STATICTRANS,
1195 FLG_UNKNOWNTRANS,
1196 FLG_STATICINITTRANS,
1197 FLG_UNKNOWNINITTRANS,
1198 FLG_KEEPTRANS,
1199 FLG_IMMEDIATETRANS,
1200 FLG_ONLYUNQGLOBALTRANS,
1201 FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
2e127cb8 1202 FLG_MUSTFREEONLY,
1203 FLG_MUSTFREEFRESH,
1204 FLG_MUSTDEFINE, FLG_GLOBSTATE,
616915dd 1205 FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1206 FLG_MEMIMPLICIT,
1207 FLG_BRANCHSTATE,
1208 FLG_NULLPOINTERARITH,
1209 FLG_SHADOW, FLG_DEPARRAYS,
1210 FLG_REDECL, FLG_READONLYSTRINGS, FLG_READONLYTRANS,
1211 FLG_LOOPLOOPBREAK, FLG_SWITCHLOOPBREAK, FLG_MODGLOBS,
1212 FLG_CHECKSTRICTGLOBALS, FLG_IMPCHECKEDSPECGLOBALS,
1213 FLG_MACROMATCHNAME, FLG_WARNLINTCOMMENTS,
1214 FLG_INCLUDENEST, FLG_ANSIRESERVED, FLG_CPPNAMES,
1215 FLG_NOPARAMS, FLG_IFEMPTY, FLG_WHILEEMPTY, FLG_REALCOMPARE,
a956d444 1216 FLG_BOOLOPS, FLG_SHIFTNEGATIVE,
1217 FLG_SHIFTIMPLEMENTATION,
28bf4b0b 1218 FLG_BUFFEROVERFLOWHIGH, FLG_BUFFEROVERFLOW,
616915dd 1219 INVALID_FLAG } ;
1220
1221 SETFLAGS ();
1222 }
1223 else if (cstring_equalLit (s, "strict"))
1224 {
1225 flagcode modeflags[] =
1226 {
1227 FLG_CHECKSTRICTGLOBALIAS,
1228 FLG_CHECKEDGLOBALIAS,
1229 FLG_CHECKMODGLOBALIAS,
1230 FLG_UNCHECKEDGLOBALIAS,
1231 FLG_MODFILESYSTEM,
1232 FLG_MACROMATCHNAME,
28bf4b0b 1233 FLG_FORMATCONST,
7534721d 1234 FLG_STRINGLITNOROOM,
1810fe2a 1235 FLG_STRINGLITNOROOMFINALNULL,
7534721d 1236 FLG_STRINGLITSMALLER,
28bf4b0b 1237 FLG_STATETRANSFER, FLG_STATEMERGE,
616915dd 1238 FLG_MACROUNDEF, FLG_MUTREP, FLG_MUSTMOD,
1239 FLG_ALLGLOBALS, FLG_IMPTYPE,
1240 FLG_MODNOMODS, FLG_MODGLOBSUNSPEC, FLG_MODSTRICTGLOBSUNSPEC,
1241 FLG_GLOBUNSPEC, FLG_SIZEOFTYPE,
1242 FLG_EXPORTHEADER, FLG_EXPORTHEADERVAR,
1243 FLG_NOPARAMS, FLG_OLDSTYLE, FLG_EXITARG,
1244 FLG_RETSTACK,
1245 FLG_FCNDEREF,
1246 FLG_ONLYUNQGLOBALTRANS,
1247 FLG_GLOBALSIMPMODIFIESNOTHING,
1248 FLG_PREDBOOLOTHERS, FLG_PREDBOOLPTR, FLG_PREDBOOLINT,
1249 FLG_INTERNALGLOBS, FLG_INTERNALGLOBSNOGLOBS,
1250 FLG_USEALLGLOBS, FLG_RETALIAS,
1251 FLG_MODGLOBS, FLG_MODGLOBSUNSPEC, FLG_MODGLOBSUNCHECKED,
1252 FLG_RETEXPOSE, FLG_ASSIGNEXPOSE, FLG_CASTEXPOSE,
1253 FLG_NOEFFECTUNCON, FLG_EVALORDERUNCON,
1254 FLG_FUNCUNUSED,
1255 FLG_EXPORTITER, FLG_EXPORTCONST,
1256 FLG_TYPEUNUSED, FLG_FIELDUNUSED, FLG_PARAMUNUSED, FLG_TOPUNUSED,
1257 FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED,
1258 FLG_VARUNUSED,
1259 FLG_NULLPOINTERARITH, FLG_POINTERARITH,
1260 FLG_PTRNUMCOMPARE,
1d239d69 1261 FLG_BOOLCOMPARE, FLG_UNSIGNEDCOMPARE,
1262 FLG_NOEFFECT, FLG_RETVALINT, FLG_RETVALBOOL, FLG_RETVALOTHER,
616915dd 1263 FLG_ANSIRESERVED, FLG_ANSIRESERVEDLOCAL, FLG_CPPNAMES,
1264 FLG_RETVALBOOL, FLG_RETVALINT, FLG_SPECUNDEF,
1265 FLG_DECLUNDEF, FLG_STRICTOPS, FLG_INCONDEFS,
28bf4b0b 1266 FLG_MISPLACEDSHAREQUAL, FLG_REDUNDANTSHAREQUAL,
616915dd 1267 FLG_INCONDEFSLIB, FLG_MATCHFIELDS, FLG_EXPORTMACRO, FLG_EXPORTVAR,
1268 FLG_EXPORTFCN, FLG_EXPORTTYPE, FLG_EXPORTLOCAL, FLG_MACROPARAMS,
1269 FLG_MACROASSIGN,
1270 FLG_SEFPARAMS, FLG_SEFUNSPEC, FLG_MACROSTMT, FLG_MACROPARENS,
1271 FLG_MACROFCNDECL,
1272 FLG_MACROCONSTDECL,
1273 FLG_MACROREDEF, FLG_MACROEMPTY,
1274 FLG_INFLOOPS, FLG_INFLOOPSUNCON,
1275 FLG_UNREACHABLE,
1276 FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE, FLG_USEDEF,
1277 FLG_EVALORDER,
1278 FLG_MODUNCON, FLG_MODUNCONNOMODS, FLG_MODINTERNALSTRICT,
1279 FLG_MODOBSERVERUNCON,
1280
1281 FLG_NESTEDEXTERN,
1282 FLG_FIRSTCASE,
1283
1d239d69 1284 /* warn use flags */
1285 FLG_MULTITHREADED, FLG_PORTABILITY, FLG_SUPERUSER, FLG_IMPLEMENTATIONOPTIONAL,
1286 FLG_BUFFEROVERFLOWHIGH,
1287 FLG_BUFFEROVERFLOW, FLG_TOCTOU,
1288
616915dd 1289 /* memchecks flags */
1290 FLG_NULLSTATE, FLG_NULLDEREF, FLG_NULLASSIGN,
1291 FLG_NULLPASS, FLG_NULLRET,
1292
1293 FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF,
1294
1295 /* memtrans flags */
1296 FLG_EXPOSETRANS,
1297 FLG_OBSERVERTRANS,
1298 FLG_DEPENDENTTRANS,
1299 FLG_NEWREFTRANS,
1300 FLG_ONLYTRANS,
1301 FLG_OWNEDTRANS,
1302 FLG_FRESHTRANS,
1303 FLG_SHAREDTRANS,
1304 FLG_TEMPTRANS,
1305 FLG_KEPTTRANS,
1306 FLG_REFCOUNTTRANS,
1307 FLG_STATICTRANS,
1308 FLG_UNKNOWNTRANS,
1309 FLG_KEEPTRANS,
1310 FLG_IMMEDIATETRANS,
1311 FLG_STATICINITTRANS,
1312 FLG_UNKNOWNINITTRANS,
1313
1314 FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
2e127cb8 1315 FLG_MUSTFREEONLY,
1316 FLG_MUSTFREEFRESH,
1317 FLG_MUSTDEFINE, FLG_GLOBSTATE,
616915dd 1318 FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1319 FLG_MEMIMPLICIT,
1320 FLG_BRANCHSTATE,
1321
1322 FLG_DECLPARAMNAME, FLG_DECLPARAMMATCH,
1323
1324 FLG_SHADOW, FLG_DEPARRAYS,
1325 FLG_STRICTDESTROY, FLG_STRICTUSERELEASED, FLG_STRICTBRANCHSTATE,
1326 FLG_REDECL, FLG_READONLYSTRINGS, FLG_READONLYTRANS,
1327 FLG_LOOPLOOPBREAK, FLG_LOOPSWITCHBREAK, FLG_SWITCHLOOPBREAK,
1328 FLG_SWITCHSWITCHBREAK, FLG_LOOPLOOPCONTINUE,
1329 FLG_CHECKSTRICTGLOBALS, FLG_IMPCHECKEDSPECGLOBALS,
1330 FLG_ALLGLOBALS, FLG_IMPCHECKEDSTRICTGLOBALS,
1331 FLG_IMPCHECKEDSTRICTSTATICS,
1332 FLG_IMPCHECKEDSTRICTSPECGLOBALS,
1333 FLG_IMPCHECKMODINTERNALS,
1334 FLG_WARNMISSINGGLOBALS, FLG_WARNMISSINGGLOBALSNOGLOBS,
1335 FLG_WARNLINTCOMMENTS, FLG_ANSIRESERVEDLOCAL,
1336 FLG_INCLUDENEST, FLG_STRINGLITERALLEN,
1337 FLG_NUMSTRUCTFIELDS, FLG_NUMENUMMEMBERS,
1338 FLG_CONTROLNESTDEPTH,
1339 FLG_FORBLOCK, FLG_WHILEBLOCK,
1340 FLG_FOREMPTY, FLG_WHILEEMPTY,
1341 FLG_IFEMPTY, FLG_IFBLOCK,
1342 FLG_ELSEIFCOMPLETE,
1343 FLG_REALCOMPARE, FLG_BOOLOPS,
1344 FLG_SYSTEMDIRERRORS, FLG_UNUSEDSPECIAL,
1345
a956d444 1346 FLG_SHIFTNEGATIVE,
1347 FLG_SHIFTIMPLEMENTATION,
1348 FLG_BITWISEOPS,
28bf4b0b 1349 FLG_BUFFEROVERFLOWHIGH, FLG_BUFFEROVERFLOW,
1350 INVALID_FLAG
1351 } ;
616915dd 1352
1353 SETFLAGS ();
1354 }
1355 else
1356 {
1357 llcontbug (message ("context_setMode: bad mode: %s", s));
1358 }
1359}
1360
1361bool
1362context_isSpecialFile (cstring fname)
1363{
28bf4b0b 1364 cstring ext = fileLib_getExtension (fname);
616915dd 1365
28bf4b0b 1366 return (cstring_equalLit (ext, ".y")
1367 || cstring_equalLit (ext, ".l")
616915dd 1368 || cstring_equalLit (fname, "lex.yy.c"));
1369}
1370
1371bool
1372context_isSystemDir (cstring dir)
1373{
68de3f33 1374 cstring thisdir = cstring_copy (context_getString (FLG_SYSTEMDIRS));
1375 cstring savedir = thisdir;
1376 cstring nextdir = cstring_afterChar (thisdir, PATH_SEPARATOR);
1377
1378 if (cstring_isDefined (nextdir))
616915dd 1379 {
68de3f33 1380 /*@access cstring@*/
1381 *nextdir = '\0'; /* closes thisdir */
616915dd 1382 nextdir += 1;
68de3f33 1383 /*@noaccess cstring@*/
616915dd 1384 }
1385
68de3f33 1386 /* 2001-09-09: added thisdir[0] != '\0'
1387 ** herbert: don't compare with an empty name!
1388 ** should return false for empty directory path
1389 */
1390
1391 while (!cstring_isEmpty (thisdir))
616915dd 1392 {
1393 DPRINTF (("Test: %s / %s", dir, thisdir));
1394
68de3f33 1395 if (osd_equalCanonicalPrefix (dir, thisdir))
616915dd 1396 {
68de3f33 1397 cstring_free (savedir);
616915dd 1398 return TRUE;
1399 }
1400
68de3f33 1401 if (cstring_isDefined (nextdir))
616915dd 1402 {
1403 thisdir = nextdir;
68de3f33 1404 nextdir = cstring_afterChar (thisdir, PATH_SEPARATOR);
616915dd 1405
68de3f33 1406 if (cstring_isDefined (nextdir))
616915dd 1407 {
68de3f33 1408 /*@access cstring@*/
616915dd 1409 *nextdir = '\0';
1410 nextdir += 1;
68de3f33 1411 /*@noaccess cstring@*/
616915dd 1412 }
1413 }
1414 else
1415 {
1416 break;
1417 }
1418 }
1419
1420 DPRINTF (("Returns FALSE"));
68de3f33 1421 cstring_free (savedir);
616915dd 1422 return FALSE;
1423}
1424
1425void
1426context_addFileAccessType (typeId t)
1427{
1428 cstring base;
1429
28bf4b0b 1430 if (context_inFunctionLike ())
616915dd 1431 {
1432 gc.acct = typeIdSet_insert (gc.acct, t);
1433 }
1434
1435 gc.facct = typeIdSet_insert (gc.facct, t);
1436
1437 base = fileloc_getBase (g_currentloc);
1438 insertModuleAccess (base, t);
1439 DPRINTF (("Add file access: %s / %s", typeIdSet_unparse (gc.facct),
1440 typeIdSet_unparse (gc.acct)));
1441}
1442
1443void
1444context_removeFileAccessType (typeId t)
1445{
1446 if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN
1447 || gc.kind == CX_UNKNOWNMACRO)
1448 {
1449 gc.acct = typeIdSet_removeFresh (gc.acct, t);
1450 }
1451
1452 gc.facct = typeIdSet_removeFresh (gc.facct, t);
1453 gc.nacct = typeIdSet_insert (gc.nacct, t);
1454}
1455
28bf4b0b 1456void context_enterFunctionHeader (void)
616915dd 1457{
b7e84605 1458 if (context_getFlag (FLG_GRAMMAR))
1459 {
1460 lldiagmsg (message ("Enter function header: %q", context_unparse ()));
1461 }
1462
ba45e1e4 1463 if (gc.kind != CX_GLOBAL)
1464 {
1465 llparseerror (cstring_makeLiteral
1466 ("Likely parse error. Function header outside global context."));
1467 }
1468 else
1469 {
1470 llassert (gc.kind == CX_GLOBAL);
1471 DPRINTF (("Enter function header!"));
1472 gc.inFunctionHeader = TRUE;
1473 }
616915dd 1474}
1475
28bf4b0b 1476void context_exitFunctionHeader (void)
616915dd 1477{
b7e84605 1478 if (context_getFlag (FLG_GRAMMAR))
1479 {
1480 lldiagmsg (message ("Exit function header: %q", context_unparse ()));
1481 }
1482
d9a28762 1483 DPRINTF (("Exit function header!"));
1484 gc.inFunctionHeader = FALSE;
616915dd 1485}
1486
28bf4b0b 1487bool context_inFunctionHeader (void)
1488{
d9a28762 1489 return (gc.inFunctionHeader);
28bf4b0b 1490}
1491
1492void context_enterFunctionDeclaration (uentry e)
1493{
b7e84605 1494 if (context_getFlag (FLG_GRAMMAR))
1495 {
1496 lldiagmsg (message ("Enter function declaration: %q", context_unparse ()));
1497 }
1498
28bf4b0b 1499 DPRINTF (("Enter function decl"));
1500 llassert (gc.savekind == CX_ERROR);
1501 gc.savekind = gc.kind;
b7e84605 1502 gc.savecont = gc.cont;
28bf4b0b 1503 gc.kind = CX_FCNDECLARATION;
1504 gc.cont.fcn = e;
1505}
1506
1507void context_exitFunctionDeclaration (void)
616915dd 1508{
b7e84605 1509 if (context_getFlag (FLG_GRAMMAR))
1510 {
1511 lldiagmsg (message ("Exit function declaration: %q", context_unparse ()));
1512 }
1513
28bf4b0b 1514 DPRINTF (("Exit function decl"));
1515 llassert (gc.savekind != CX_ERROR);
1516 llassert (gc.kind == CX_FCNDECLARATION);
1517 gc.kind = gc.savekind;
b7e84605 1518 gc.cont = gc.savecont;
1519
28bf4b0b 1520 gc.savekind = CX_ERROR;
b7e84605 1521
1522 if (context_getFlag (FLG_GRAMMAR))
1523 {
1524 lldiagmsg (message ("After exit function declaration: %q", context_unparse ()));
1525 }
616915dd 1526}
1527
28bf4b0b 1528bool context_inFunctionDeclaration (void)
1529{
1530 return (gc.kind == CX_FCNDECLARATION);
1531}
1532
1533
616915dd 1534void
1535context_enterMacro (/*@observer@*/ uentry e)
1536{
1537 context_enterFunction (e);
1538 gc.kind = CX_MACROFCN;
1539}
1540
1541void
1542context_enterUnknownMacro (/*@dependent@*/ uentry e)
1543{
1544 llassert (uentry_isFunction (e));
1545 context_enterFunction (e);
1546 gc.kind = CX_UNKNOWNMACRO;
1547}
1548
1549void context_enterAndClause (exprNode e)
1550{
1551
1552 usymtab_trueBranch (guardSet_copy (exprNode_getGuards (e)));
1553 pushClause (ANDCLAUSE);
1554}
1555
1556void context_enterOrClause (exprNode e)
1557{
1558 usymtab_trueBranch (guardSet_invert (exprNode_getGuards (e)));
1559 pushClause (ORCLAUSE);
1560}
1561
1562bool context_inDeepLoop (void)
1563{
1564 bool inLoop = FALSE;
1565
1566 clauseStack_elements (gc.clauses, el)
1567 {
1568 if (clause_isLoop (el))
1569 {
1570 if (inLoop)
1571 {
1572 return TRUE;
1573 }
1574
1575 inLoop = TRUE;
1576 }
1577 } end_clauseStack_elements;
1578
1579 return FALSE;
1580}
1581
1582bool context_inDeepSwitch (void)
1583{
1584 bool inLoop = FALSE;
1585
1586 clauseStack_elements (gc.clauses, el)
1587 {
1588 if (clause_isSwitch (el))
1589 {
1590 if (inLoop)
1591 {
1592 return TRUE;
1593 }
1594
1595 inLoop = TRUE;
1596 }
1597 } end_clauseStack_elements;
1598
1599 return FALSE;
1600}
1601
1602bool context_inDeepLoopSwitch (void)
1603{
1604 bool inLoop = FALSE;
1605
1606 clauseStack_elements (gc.clauses, el)
1607 {
1608 if (clause_isBreakable (el))
1609 {
1610 if (inLoop)
1611 {
1612 return TRUE;
1613 }
1614
1615 inLoop = TRUE;
1616 }
1617 } end_clauseStack_elements;
1618
1619 return FALSE;
1620}
1621
1622clause context_breakClause (void)
1623{
1624 clauseStack_elements (gc.clauses, el)
1625 {
1626 if (clause_isSwitch (el))
1627 {
1628 return el;
1629 }
1630 else if (clause_isLoop (el))
1631 {
1632 return el;
1633 }
1634 else
1635 {
1636 ;
1637 }
1638 } end_clauseStack_elements;
1639
1640 return NOCLAUSE;
1641}
1642
1643clause context_nextBreakClause (void)
1644{
1645 bool hasOne = FALSE;
1646
1647 clauseStack_elements (gc.clauses, el)
1648 {
1649 if (clause_isBreakable (el))
1650 {
1651 if (hasOne)
1652 {
1653 return el;
1654 }
1655 else
1656 {
1657 hasOne = TRUE;
1658 }
1659 }
1660 } end_clauseStack_elements;
1661
1662 return NOCLAUSE;
1663}
1664
1665bool context_inConditional (void)
1666{
1667 clauseStack_elements (gc.clauses, el)
1668 {
1669 /*
1670 ** Should also include TRUECLAUSE and FALSECLAUSE, but need
1671 ** to distinguish if from ? for this
1672 */
1673
1674 if (clause_isBreakable (el) && (el != DOWHILECLAUSE))
1675 {
1676 return TRUE;
1677 }
1678 } end_clauseStack_elements;
1679
1680 return FALSE;
1681}
1682
1683void context_exitAndClause (exprNode pred, exprNode tbranch)
1684{
1685 context_setJustPopped ();
1686
1687 llassert (gc.inclause == ANDCLAUSE);
1688
1689 usymtab_popAndBranch (pred, tbranch);
1690 clauseStack_pop (gc.clauses);
1691 gc.inclause = topClause (gc.clauses);
1692}
1693
1694void context_exitOrClause (exprNode pred, exprNode tbranch)
1695{
1696 context_setJustPopped ();
1697
1698 llassert (gc.inclause == ORCLAUSE);
1699
1700 usymtab_popOrBranch (pred, tbranch);
1701 clauseStack_pop (gc.clauses);
1702 gc.inclause = topClause (gc.clauses);
1703}
1704
1705static void context_enterCondClauseAux (clause cl)
1706 /*@modifies gc@*/
1707{
1708 pushClause (cl);
1709}
1710
1711static void context_enterTrueAux (exprNode e, clause cl)
1712 /*@modifies gc@*/
1713{
1714 usymtab_trueBranch (guardSet_copy (exprNode_getGuards (e)));
1715 pushClause (cl);
1716}
1717
1718void context_enterIterClause (void)
1719{
1720 context_enterTrueAux (exprNode_undefined, ITERCLAUSE);
1721}
1722
1723void context_enterDoWhileClause (void)
1724{
1725 pushClause (DOWHILECLAUSE);
1726}
1727
1728void context_enterWhileClause (exprNode e)
1729{
1730 context_enterTrueAux (e, WHILECLAUSE);
1731}
1732
1733void context_enterForClause (exprNode e)
1734{
1735 context_enterTrueAux (e, FORCLAUSE);
1736}
1737
1738void context_enterTrueClause (exprNode e)
1739{
1740 context_enterTrueAux (e, TRUECLAUSE);
1741}
1742
1743void context_enterSwitch (exprNode e)
1744{
1745
1746 usymtab_switchBranch (e);
1747 context_enterCondClauseAux (SWITCHCLAUSE);
1748}
1749
1750void context_exitSwitch (exprNode e, bool allpaths)
1751{
1752 usymtab_exitSwitch (e, allpaths);
1753
1754 while (clause_isCase (clauseStack_top (gc.clauses)))
1755 {
1756 clauseStack_pop (gc.clauses);
1757 }
1758
1759 context_exitClauseSimp ();
1760}
1761
1762void context_enterCaseClause (exprNode e)
1763{
1764 bool branch = FALSE;
1765
1766 DPRINTF (("Enter case clause!"));
1767
1768 branch = usymtab_newCase (exprNode_undefined, e);
1769
1770 if (branch)
1771 {
1772 context_enterCondClauseAux (CASECLAUSE);
1773 }
1774}
1775
1776static void context_enterFalseClauseAux (exprNode e, clause cl)
1777 /*@modifies gc@*/
1778{
1779 usymtab_altBranch (guardSet_invert (exprNode_getGuards (e)));
1780 gc.inclause = cl;
1781 clauseStack_switchTop (gc.clauses, cl);
1782}
1783
1784void context_enterFalseClause (exprNode e)
1785{
1786
1787 context_enterFalseClauseAux (e, FALSECLAUSE);
1788}
1789
1790void
1791context_enterConstantMacro (/*@exposed@*/ /*@dependent@*/ uentry e)
1792{
1793 gc.kind = CX_MACROCONST;
1794 gc.cont.fcn = e;
1795 gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1796
1797 gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)),
1798 gc.nacct);
1799
1800
1801 usymtab_enterScope ();
1802 sRef_enterFunctionScope ();
1803
1804 gc.globs = globSet_undefined;
1805 globSet_clear (gc.globs_used);
1806 gc.mods = sRefSet_undefined;
1807}
1808
1809uentry context_getHeader (void)
1810{
1811 if (!(context_inFunctionLike () || (gc.kind == CX_MACROCONST)))
1812 {
1813 llfatalbug (message ("context_getHeader: bad call: %q",
1814 context_unparse ()));
1815 }
1816
1817 return (gc.cont.fcn);
1818}
1819
1820void
1821context_setFunctionDefined (fileloc loc)
1822{
1823 switch (gc.kind)
1824 {
1825 case CX_UNKNOWNMACRO:
1826 case CX_FUNCTION:
1827 case CX_MACROFCN:
1828 uentry_setFunctionDefined (gc.cont.fcn, loc);
1829 break;
1830 default:
1831 /* (not a bug because of parse errors) */
1832 break;
1833 }
1834}
1835
1836void
1837context_enterFunction (/*@exposed@*/ uentry e)
1838{
1839 gc.kind = CX_FUNCTION;
1840 gc.cont.fcn = e;
1841
28bf4b0b 1842 DPRINTF (("Enter function: %s", uentry_unparse (e)));
1843
616915dd 1844 if (uentry_hasAccessType (e))
1845 {
1846 gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)),
1847 gc.nacct);
1848 }
1849 else
1850 {
1851 gc.acct = gc.facct;
1852 }
1853
1854 DPRINTF (("Enter function: %s / %s", uentry_unparse (e),
1855 typeIdSet_unparse (gc.acct)));
1856
1857 gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1858
1859 gc.globs = uentry_getGlobs (e);
1860 globSet_clear (gc.globs_used);
1861 gc.mods = uentry_getMods (e);
1862
1863 usymtab_enterFunctionScope (e);
1864 sRef_enterFunctionScope ();
1865}
1866
7ebcc5bb 1867void
1868context_enterOldStyleScope (void)
1869{
1870 gc.kind = CX_OLDSTYLESCOPE;
1871 DPRINTF (("Enter old style scope!"));
1872 usymtab_enterFunctionScope (uentry_undefined);
1873}
1874
1875void
1876context_completeOldStyleFunction (uentry e)
1877{
1878 llassert (gc.kind == CX_OLDSTYLESCOPE);
1879
1880 gc.kind = CX_FUNCTION;
1881 gc.cont.fcn = e;
1882
1883 DPRINTF (("Enter function: %s", uentry_unparse (e)));
1884
1885 if (uentry_hasAccessType (e))
1886 {
1887 gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)),
1888 gc.nacct);
1889 }
1890 else
1891 {
1892 gc.acct = gc.facct;
1893 }
1894
1895 DPRINTF (("Enter function: %s / %s", uentry_unparse (e),
1896 typeIdSet_unparse (gc.acct)));
1897
1898 gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1899
1900 if (!globSet_isEmpty (uentry_getGlobs (e)))
1901 {
1902 llfatalerror (message ("%q: Old-style function declaration uses a clause (rewrite with function parameters): %q",
1903 fileloc_unparse (g_currentloc), uentry_unparse (e)));
1904 }
1905
1906 gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1907
1908 gc.globs = uentry_getGlobs (e);
1909 globSet_clear (gc.globs_used);
1910
1911 gc.mods = uentry_getMods (e);
1912
1913 if (!sRefSet_isEmpty (gc.mods))
1914 {
1915 llfatalerror (message ("%q: Old-style function declaration uses a clause (rewrite with function parameters): %q",
1916 fileloc_unparse (g_currentloc), uentry_unparse (e)));
1917 }
1918
1919 sRef_enterFunctionScope ();
1920}
1921
616915dd 1922static bool context_checkStrictGlobals (void)
1923{
1924 return (context_getFlag (FLG_CHECKSTRICTGLOBALS));
1925}
1926
1927static bool context_hasGlobs (void)
1928{
1929 if (context_inFunctionLike ())
1930 {
1931 return (uentry_hasGlobs (gc.cont.fcn));
1932 }
1933 else
1934 {
1935 return (FALSE);
1936 }
1937}
1938
1939static bool context_checkCheckedGlobals (void)
1940{
1941 return (context_getFlag (FLG_GLOBALS)
1942 && (context_getFlag (FLG_GLOBUNSPEC)
1943 || context_hasGlobs ()));
1944}
1945
1946static bool context_checkUnknownGlobals (void)
1947{
1948 /* should be uentry_hasGlobs ? */
1949
1950 return (context_getFlag (FLG_ALLGLOBALS)
1951 && (context_getFlag (FLG_GLOBUNSPEC)
1952 || context_hasGlobs ()));
1953}
1954
1955static bool context_checkUncheckedGlobals (void)
1956{
1957 return (FALSE);
1958}
1959
1960bool
1961context_checkExport (uentry e)
1962{
1963 if (!gc.anyExports) return FALSE;
1964
1965 if (uentry_isFunction (e)
1966 || (uentry_isVariable (e) && ctype_isFunction (uentry_getType (e))))
1967 {
1968 return context_maybeSet (FLG_EXPORTFCN);
1969 }
1970 else if (uentry_isExpandedMacro (e))
1971 {
1972 return context_maybeSet (FLG_EXPORTMACRO);
1973 }
1974 else if (uentry_isVariable (e))
1975 {
1976 return context_maybeSet (FLG_EXPORTVAR);
1977 }
1978 else if (uentry_isEitherConstant (e))
1979 {
1980 return context_maybeSet (FLG_EXPORTCONST);
1981 }
1982 else if (uentry_isIter (e) || uentry_isEndIter (e))
1983 {
1984 return context_maybeSet (FLG_EXPORTITER);
1985 }
1986 else if (uentry_isDatatype (e))
1987 {
1988 return context_maybeSet (FLG_EXPORTTYPE);
1989 }
1990 else
1991 {
1992 BADEXIT;
1993 }
1994}
1995
1996bool
1997context_checkGlobUse (uentry glob)
1998{
1999
2000 if (uentry_isCheckedStrict (glob))
2001 {
2002 return context_checkStrictGlobals ();
2003 }
2004 else if (uentry_isChecked (glob))
2005 {
2006 return context_checkCheckedGlobals ();
2007 }
2008 else if (uentry_isCheckedUnknown (glob) || uentry_isCheckMod (glob))
2009 {
2010 return context_checkUnknownGlobals ();
2011 }
2012 else
2013 {
2014 llassert (uentry_isUnchecked (glob));
2015
2016 return context_checkUncheckedGlobals ();
2017 }
2018}
2019
2020bool
2021context_checkAliasGlob (uentry glob)
2022{
2023 if (uentry_isCheckedStrict (glob))
2024 {
2025 return gc.flags[FLG_CHECKSTRICTGLOBALIAS];
2026 }
2027 else if (uentry_isChecked (glob))
2028 {
2029 return gc.flags[FLG_CHECKEDGLOBALIAS];
2030 }
2031 else if (uentry_isCheckMod (glob))
2032 {
2033 return gc.flags[FLG_CHECKMODGLOBALIAS];
2034 }
2035 else
2036 {
2037 llassert (uentry_isUnchecked (glob) || uentry_isCheckedUnknown (glob));
2038
2039 return gc.flags[FLG_UNCHECKEDGLOBALIAS];
2040 }
2041}
2042
2043bool context_checkInternalUse (void)
2044{
2045 if (context_hasGlobs ())
2046 {
2047 return (gc.flags[FLG_INTERNALGLOBS]);
2048 }
2049 else
2050 {
2051 return (gc.flags[FLG_INTERNALGLOBSNOGLOBS]);
2052 }
2053}
2054
2055bool
2056context_checkGlobMod (sRef el)
2057{
2058 uentry ue = sRef_getUentry (el);
2059
28bf4b0b 2060 /* no: llassert (sRef_isFileOrGlobalScope (el)); also check local statics */
616915dd 2061
2062 if (uentry_isCheckedModify (ue)
2063 || (!uentry_isUnchecked (ue) && (gc.flags[FLG_ALLGLOBALS])))
2064 {
2065 if (context_hasMods ())
2066 {
2067 return (gc.flags[FLG_MODGLOBS]);
2068 }
2069 else
2070 {
2071 if (uentry_isCheckedStrict (ue))
2072 {
2073 return (gc.flags[FLG_MODGLOBSUNSPEC]);
2074 }
2075 else
2076 {
2077 return (gc.flags[FLG_MODSTRICTGLOBSUNSPEC]);
2078 }
2079 }
2080 }
2081 else
2082 {
2083 if (context_hasMods ())
2084 {
2085 return (gc.flags[FLG_MODGLOBSUNCHECKED]);
2086 }
2087 else
2088 {
2089 return FALSE;
2090 }
2091 }
2092}
2093
2094void
28bf4b0b 2095context_usedGlobal (/*@exposed@*/ sRef el)
616915dd 2096{
2097 if (!globSet_member (gc.globs_used, el))
2098 {
2099 /*
2100 ** The first time a global is used in a function, we need
2101 ** to clear the derived sRefs, since they were set for the
2102 ** previous function.
2103 */
2104
2105 sRef_clearDerived (el);
2106 gc.globs_used = globSet_insert (gc.globs_used, el);
2107 }
2108}
2109
2110/*@observer@*/ sRefSet
2111context_modList (void)
2112{
2113 return gc.mods;
2114}
2115
2116bool
2117context_globAccess (sRef s)
2118{
28bf4b0b 2119 llassert (sRef_isFileOrGlobalScope (s) || sRef_isKindSpecial (s));
616915dd 2120 return (globSet_member (gc.globs, s));
2121}
2122
2123bool
2124context_hasAccess (typeId t)
2125{
2126 if (context_inFunctionLike ())
2127 {
28bf4b0b 2128 DPRINTF (("Access %d / %s",
2129 t, typeIdSet_unparse (gc.acct)));
616915dd 2130 return (typeIdSet_member (gc.acct, t));
2131 }
2132 else
2133 {
2134 return (context_hasFileAccess (t));
2135 }
2136}
2137
2138bool
2139context_hasFileAccess (typeId t)
2140{
2141 return (typeIdSet_member (gc.facct, t));
2142}
2143
2144/*@only@*/ cstring
2145context_unparseAccess (void)
2146{
2147 return (message ("%q / %q", typeIdSet_unparse (gc.acct),
2148 typeIdSet_unparse (gc.facct)));
2149}
2150
2151/*@only@*/ cstring
2152context_unparseClauses (void)
2153{
2154 return (clauseStack_unparse (gc.clauses));
2155}
2156
2157bool
2158context_couldHaveAccess (typeId t)
2159{
2160 if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN || gc.kind == CX_UNKNOWNMACRO)
2161 {
2162 return (typeIdSet_member (gc.acct, t));
2163 }
2164 else
2165 {
2166 return (typeIdSet_member (gc.facct, t));
2167 }
2168}
2169
2170ctype
2171context_getRetType (void)
2172{
2173 ctype f = ctype_undefined;
2174
2175 if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN)
2176 {
2177 f = uentry_getType (gc.cont.fcn);
2178 }
2179 else if (gc.kind == CX_UNKNOWNMACRO)
2180 {
2181 return ctype_unknown;
2182 }
2183 else
2184 {
2185 llcontbuglit ("context_getRetType: not in a function context");
2186 return ctype_unknown;
2187 }
2188
2189 if (!ctype_isFunction (f))
2190 {
2191 if (ctype_isKnown (f))
2192 {
2193 llbuglit ("context_getRetType: not a function");
2194 }
2195
2196 return ctype_unknown;
2197 }
28bf4b0b 2198 return (ctype_getReturnType (f));
616915dd 2199}
2200
2201bool
2202context_hasMods (void)
2203{
2204 if (context_inFunctionLike ())
2205 {
2206 return (uentry_hasMods (gc.cont.fcn));
2207 }
2208 else
2209 {
2210 return FALSE;
2211 }
2212}
2213
2214void
2215context_exitAllClauses (void)
28bf4b0b 2216{
616915dd 2217 while (!clauseStack_isEmpty (gc.clauses))
2218 {
2219 clause el = clauseStack_top (gc.clauses);
616915dd 2220 gc.inclause = el;
2221
2222 if (clause_isNone (el))
2223 {
2224 usymtab_quietExitScope (g_currentloc);
2225 clauseStack_pop (gc.clauses);
2226 }
2227 else
2228 {
2229 context_exitClausePlain ();
2230 }
2231 }
2232
28bf4b0b 2233 clauseStack_clear (gc.clauses);
2234 gc.inclause = NOCLAUSE;
2235}
616915dd 2236
28bf4b0b 2237void
2238context_exitAllClausesQuiet (void)
2239{
2240 while (!clauseStack_isEmpty (gc.clauses))
2241 {
2242 clause el = clauseStack_top (gc.clauses);
2243 gc.inclause = el;
2244
2245 usymtab_quietExitScope (g_currentloc);
2246 clauseStack_pop (gc.clauses);
2247 }
2248
2249 clauseStack_clear (gc.clauses);
616915dd 2250 gc.inclause = NOCLAUSE;
2251}
2252
2253static
2254void context_exitClauseSimp (void)
2255{
2256
2257 context_setJustPopped ();
2258 clauseStack_pop (gc.clauses);
2259 gc.inclause = topClause (gc.clauses);
2260}
2261
2262static
2263void context_exitCaseClause (void)
2264{
2265 context_setJustPopped ();
2266 usymtab_popCaseBranch ();
2267 clauseStack_pop (gc.clauses);
2268 gc.inclause = topClause (gc.clauses);
2269}
2270
2271static
2272void context_exitClauseAux (exprNode pred, exprNode tbranch)
2273{
2274 context_setJustPopped ();
28bf4b0b 2275 /*@i32 was makeAlt */
616915dd 2276 usymtab_popTrueBranch (pred, tbranch, gc.inclause);
2277 clauseStack_pop (gc.clauses);
2278 gc.inclause = topClause (gc.clauses);
2279}
2280
2281void context_exitTrueClause (exprNode pred, exprNode tbranch)
2282{
28bf4b0b 2283 DPRINTF (("Exit true clause: %s", exprNode_unparse (tbranch)));
2284
616915dd 2285 if (gc.inclause != TRUECLAUSE)
2286 {
2287 llparseerror (cstring_makeLiteral
2288 ("Likely parse error. Conditional clauses are inconsistent."));
2289 return;
2290 }
28bf4b0b 2291
2292 context_setJustPopped ();
616915dd 2293 usymtab_popTrueBranch (pred, tbranch, TRUECLAUSE);
2294 clauseStack_pop (gc.clauses);
28bf4b0b 2295 gc.inclause = topClause (gc.clauses);
2296}
616915dd 2297
2298void context_exitIterClause (exprNode body)
2299{
2300 llassert (gc.inclause == ITERCLAUSE);
2301
2302 context_setJustPopped ();
2303
3e3ec469 2304 if (context_getFlag (FLG_ITERLOOPEXEC))
616915dd 2305 {
2306 usymtab_popTrueExecBranch (exprNode_undefined, body, ITERCLAUSE);
2307 }
2308 else
2309 {
2310 usymtab_popTrueBranch (exprNode_undefined, body, ITERCLAUSE);
2311 }
2312
2313 clauseStack_pop (gc.clauses);
2314 gc.inclause = topClause (gc.clauses);
2315}
2316
2317static void context_popCase (void) {
2318 /*
2319 ** If we are exiting an outer clause, sometimes still in a switch case.
2320 **
2321 ** e.g.:
2322 **
2323 ** switch(a)
2324 ** {
2325 ** case 1:
2326 ** while (c>3)
2327 ** {
2328 ** case 3: ++c;
2329 ** }
2330 ** }
2331 */
2332
2333 DPRINTF (("Popping case clause: %s",
2334 clauseStack_unparse (gc.clauses)));
28bf4b0b 2335
616915dd 2336 if (gc.inclause == CASECLAUSE) {
2337 context_exitCaseClause ();
2338 }
2339}
2340
2341void context_exitWhileClause (exprNode pred, exprNode body)
2342{
2343 guardSet invGuards = guardSet_invert (exprNode_getGuards (pred));
2344
2345 context_popCase ();
2346
2347 if (gc.inclause != WHILECLAUSE) {
2348 DPRINTF (("Clause: %s / %s", clause_unparse (gc.inclause),
2349 clauseStack_unparse (gc.clauses)));
2350 }
2351
2352 llassert (gc.inclause == WHILECLAUSE);
2353
2354 context_setJustPopped ();
2355
2356
2357 /*
2358 ** predicate must be false after while loop (unless there are breaks)
2359 */
2360
3e3ec469 2361 if (context_getFlag (FLG_WHILELOOPEXEC))
616915dd 2362 {
2363 usymtab_popTrueExecBranch (pred, body, WHILECLAUSE);
2364 }
2365 else
2366 {
2367 usymtab_popTrueBranch (pred, body, WHILECLAUSE);
2368 }
2369
2370
2371 usymtab_addGuards (invGuards);
2372 guardSet_free (invGuards);
2373
2374 clauseStack_pop (gc.clauses);
2375 gc.inclause = topClause (gc.clauses);
2376}
2377
2378void context_exitDoWhileClause (exprNode pred)
2379{
2380 guardSet invGuards = guardSet_invert (exprNode_getGuards (pred));
2381
2382 if (gc.inclause == CASECLAUSE) {
2383 /* handle Duff's device */
2384 clauseStack_pop (gc.clauses);
2385 gc.inclause = topClause (gc.clauses);
2386 }
2387
2388 llassert (gc.inclause == DOWHILECLAUSE);
2389
2390 context_setJustPopped ();
2391
2392
2393 usymtab_addGuards (invGuards);
2394 guardSet_free (invGuards);
2395
2396 clauseStack_pop (gc.clauses);
2397 gc.inclause = topClause (gc.clauses);
2398}
2399
2400void context_exitForClause (exprNode forPred, exprNode body)
2401{
2402 guardSet invGuards = guardSet_invert (exprNode_getForGuards (forPred));
2403
2404 llassert (gc.inclause == FORCLAUSE);
2405 context_setJustPopped ();
2406
3e3ec469 2407 DPRINTF (("Exit for: %s / %s", exprNode_unparse (forPred), exprNode_unparse (body)));
2408
616915dd 2409 /*
3e3ec469 2410 ** Predicate must be false after for loop (unless there are breaks)
616915dd 2411 */
2412
3e3ec469 2413 if (context_getFlag (FLG_FORLOOPEXEC))
616915dd 2414 {
3e3ec469 2415 DPRINTF (("Here: for loop exec"));
28bf4b0b 2416 usymtab_popTrueExecBranch (forPred, body, FORCLAUSE);
616915dd 2417 }
2418 else
2419 {
3e3ec469 2420 if (context_getFlag (FLG_OBVIOUSLOOPEXEC)
2421 && exprNode_loopMustExec (forPred))
2422 {
2423 DPRINTF (("Here: loop must exec"));
2424 usymtab_popTrueExecBranch (forPred, body, FORCLAUSE);
2425 }
2426 else
2427 {
2428 DPRINTF (("Pop true branch:"));
2429 usymtab_popTrueBranch (forPred, body, FORCLAUSE);
2430 }
616915dd 2431 }
2432
2433 usymtab_addGuards (invGuards);
2434 guardSet_free (invGuards);
2435 clauseStack_pop (gc.clauses);
2436 gc.inclause = topClause (gc.clauses);
2437}
2438
2439static void context_exitClausePlain (void)
2440{
2441 llassert (gc.inclause != NOCLAUSE);
2442
2443 if (gc.inclause == FALSECLAUSE)
2444 {
2445 context_exitClause (exprNode_undefined, exprNode_undefined, exprNode_undefined);
2446 }
2447 else
2448 {
2449 context_exitClauseAux (exprNode_undefined, exprNode_undefined);
2450 }
616915dd 2451}
2452
2453void context_exitClause (exprNode pred, exprNode tbranch, exprNode fbranch)
2454{
616915dd 2455 context_setJustPopped ();
28bf4b0b 2456
616915dd 2457 if (gc.inclause == FALSECLAUSE)
2458 {
2459 usymtab_popBranches (pred, tbranch, fbranch, FALSE, FALSECLAUSE);
28bf4b0b 2460
616915dd 2461 llassert (clauseStack_top (gc.clauses) == FALSECLAUSE);
2462
2463 clauseStack_pop (gc.clauses);
2464 gc.inclause = topClause (gc.clauses);
2465 }
2466 else
2467 {
28bf4b0b 2468 context_exitTrueClause (pred, tbranch);
616915dd 2469 }
2470}
2471
2472void
2473context_returnFunction (void)
2474{
2475 usymtab_checkFinalScope (TRUE);
2476}
2477
2478void
2479context_exitFunction (void)
2480{
b7e84605 2481 DPRINTF (("Exit function: %s", context_unparse ()));
2482
616915dd 2483 if (!context_inFunction () && !context_inMacroConstant ()
b7e84605 2484 && !context_inUnknownMacro ()
616915dd 2485 && !context_inIterDef () && !context_inIterEnd ())
2486 {
2487 /*
2488 ** not a bug because of parse errors
2489 */
b7e84605 2490
2491 BADBRANCH;
616915dd 2492 }
2493 else
2494 {
2495 if (context_inMacro () && usymtab_inFunctionScope ())
2496 {
2497 usymtab_exitScope (exprNode_undefined);
2498 }
2499
2500 if (uentry_hasGlobs (gc.cont.fcn))
2501 {
2502 exprChecks_checkUsedGlobs (gc.globs, gc.globs_used);
2503 }
2504
2505
2506 if (uentry_hasMods (gc.cont.fcn))
2507 {
2508 if (context_getFlag (FLG_MUSTMOD))
2509 {
2510 exprNode_checkAllMods (gc.mods, gc.cont.fcn);
2511 }
2512 }
2513
28bf4b0b 2514 DPRINTF (("Exit function: %s", uentry_unparse (gc.cont.fcn)));
2515
616915dd 2516 /*
2517 ** clear file static modifies
2518 */
2519
2520 /* do this first to get unused error messages */
2521
2522 usymtab_exitScope (exprNode_undefined);
2523 sRef_exitFunctionScope ();
2524
2525 gc.showfunction = FALSE;
2526 gc.kind = CX_GLOBAL;
2527 gc.cont.glob = TRUE;
2528 gc.acct = gc.facct;
2529 gc.globs = globSet_new ();
2530 globSet_clear (gc.globs_used);
2531 gc.mods = sRefSet_new ();
2532 }
2533
2534 llassert (clauseStack_isEmpty (gc.clauses));
2535 llassert (gc.inclause == NOCLAUSE);
b7e84605 2536 DPRINTF (("After exit function: %s", context_unparse ()));
616915dd 2537}
2538
2539void
2540context_quietExitFunction (void)
2541{
2542 while (gc.kind == CX_INNER)
2543 {
2544 context_exitInnerPlain ();
2545 }
2546
b7e84605 2547 if (!context_inFunction () && !context_inMacroConstant () && !context_inUnknownMacro ()
616915dd 2548 && !context_inIterDef () && !context_inIterEnd ())
2549 {
2550 }
2551 else
2552 {
2553 usymtab_quietExitScope (g_currentloc);
2554
2555 gc.showfunction = FALSE;
2556 gc.kind = CX_GLOBAL;
2557 gc.cont.glob = TRUE;
2558 gc.acct = gc.facct;
2559 gc.globs = globSet_new ();
2560 globSet_clear (gc.globs_used);
2561 gc.mods = sRefSet_new ();
2562
2563 sRef_exitFunctionScope ();
2564 }
2565}
2566
2567/*@observer@*/ uentryList
2568context_getParams (void)
2569{
2570 if (context_inFunctionLike ())
2571 {
2572 return (uentry_getParams (gc.cont.fcn));
2573 }
2574 else
2575 {
2576 llcontbug (message ("context_getParams: not in function: %q", context_unparse ()));
2577 return uentryList_undefined;
2578 }
2579}
2580
2581/*@observer@*/ globSet
2582context_getUsedGlobs (void)
2583{
2584 llassert (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN
2585 || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
2586
2587 return (gc.globs_used);
2588}
2589
2590cstring
2591context_moduleName ()
2592{
2593 return (fileloc_getBase (g_currentloc));
2594}
2595
2596/*@observer@*/ globSet
2597context_getGlobs (void)
2598{
2599 llassert (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN
2600 || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
2601
2602 return (gc.globs);
2603}
2604
2605void
2606context_addBoolAccess (void)
2607{
2608 cstring bname = context_getString (FLG_BOOLTYPE);
2609 typeIdSet boolt = typeIdSet_single (usymtab_getTypeId (bname));
2610
2611 addModuleAccess (cstring_copy (bname), boolt);
2612
2613 /* for sys/types (perhaps, this is bogus!) */
2614 addModuleAccess (cstring_makeLiteral ("types"), boolt);
2615}
2616
2617# if 0
2618bool
2619context_canAccessBool (void)
2620{
2621 return TRUE;
2622}
2623# endif
2624
2625/*
2626 static typeId boolType = typeId_invalid;
2627
2628 if (typeId_isInvalid (boolType))
2629 {
2630 boolType = usymtab_getTypeId (context_getBoolName ());
2631 }
2632
2633 if (typeId_isInvalid (boolType)) {
2634 return FALSE;
2635 } else {
2636 return (typeIdSet_member (gc.acct, boolType));
2637 }
2638}
2639*/
2640
2641/* evs 2000-07-25: old version - replaced */
2642
2643ctype
2644context_boolImplementationType () {
2645 /* For now, this is bogus! */
2646 return ctype_int;
2647}
2648
2649bool
2650context_canAccessBool (void)
2651{
2652 static typeId boolType = typeId_invalid;
2653
2654 if (typeId_isInvalid (boolType))
2655 {
2656 boolType = usymtab_getTypeId (context_getBoolName ());
2657 }
2658
2659 if (!typeId_isInvalid (boolType))
2660 {
2661 return context_hasAccess (boolType);
2662 }
2663 else
2664 {
2665 ;
2666 }
2667
2668 return FALSE;
2669}
2670
2671void
2672context_setMessageAnnote (/*@only@*/ cstring s)
2673{
2674 llassert (cstring_isUndefined (gc.msgAnnote));
2675 gc.msgAnnote = s;
2676}
2677
2678bool
2679context_hasMessageAnnote (void)
2680{
2681 return (cstring_isDefined (gc.msgAnnote));
2682}
2683
2684void
2685context_clearMessageAnnote (void)
2686{
2687 if (cstring_isDefined (gc.msgAnnote))
2688 {
2689 cstring_free (gc.msgAnnote);
2690 gc.msgAnnote = cstring_undefined;
2691 }
2692}
2693
2694/*@only@*/ cstring
2695context_getMessageAnnote (void)
2696{
2697 cstring st = gc.msgAnnote;
2698
2699 gc.msgAnnote = cstring_undefined;
2700 return st;
2701}
2702
2703void
2704context_setAliasAnnote (/*@observer@*/ sRef s, /*@observer@*/ sRef t)
2705{
2706 llassert (sRef_isInvalid (gc.aliasAnnote));
2707 llassert (!sRef_sameName (s, t));
2708 gc.aliasAnnote = s;
2709 gc.aliasAnnoteAls = t;
2710}
2711
2712bool
2713context_hasAliasAnnote (void)
2714{
2715 return (sRef_isValid (gc.aliasAnnote));
2716}
2717
2718void
2719context_clearAliasAnnote (void)
2720{
2721 gc.aliasAnnote = sRef_undefined;
2722}
2723
2724cstring
2725context_getAliasAnnote (void)
2726{
2727 sRef ret = gc.aliasAnnote;
2728 sRef als = gc.aliasAnnoteAls;
2729
2730 llassert (sRef_isValid (ret) && sRef_isValid (als));
2731
2732 gc.aliasAnnote = sRef_undefined;
2733 return (message ("%q aliases %q", sRef_unparse (als), sRef_unparse (ret)));
2734}
2735
2736void
2737context_recordFileModifies (sRefSet mods)
2738{
28bf4b0b 2739 gc.modrecs = sRefSetList_add (gc.modrecs, mods);
616915dd 2740}
2741
2742void
2743context_recordFileGlobals (globSet mods)
2744{
28bf4b0b 2745 DPRINTF (("Recording file globals: %s", globSet_unparse (mods)));
2746 /*@access globSet@*/ context_recordFileModifies (mods); /*@noaccess globSet@*/
616915dd 2747}
2748
2749void
2750context_setCommentMarkerChar (char c)
2751{
2752 llassert (c != '\0');
2753
2754 context_setValue (FLG_COMMENTCHAR, (int) c);
2755}
2756
2757char
2758context_getCommentMarkerChar (void)
2759{
2760 return ((char) context_getValue (FLG_COMMENTCHAR));
2761}
2762
2763static void
2764context_setValue (flagcode flag, int val)
2765{
2766 int index = flagcode_valueIndex (flag);
2767
2768 llassert (index >= 0 && index <= NUMVALUEFLAGS);
2769
28bf4b0b 2770 switch (flag)
616915dd 2771 {
28bf4b0b 2772 case FLG_LINELEN:
2773 if (val <= 0)
616915dd 2774 {
28bf4b0b 2775
abb1cb43 2776 llerror_flagWarning (message ("Value for %s must be a positive "
2777 "number (given %d)",
2778 flagcode_unparse (flag), val));
616915dd 2779 return;
616915dd 2780 }
28bf4b0b 2781 if (flag == FLG_LINELEN && val < MINLINELEN)
2782 {
abb1cb43 2783 llerror_flagWarning (message ("Value for %s must be at least %d (given %d)",
2784 flagcode_unparse (flag),
2785 MINLINELEN, val));
28bf4b0b 2786 val = MINLINELEN;
2787 }
2788 break;
616915dd 2789
28bf4b0b 2790 case FLG_INCLUDENEST:
2791 case FLG_CONTROLNESTDEPTH:
2792 case FLG_STRINGLITERALLEN:
2793 case FLG_NUMSTRUCTFIELDS:
2794 case FLG_NUMENUMMEMBERS:
2795 case FLG_INDENTSPACES:
2796 if (val < 0)
2797 {
abb1cb43 2798 llerror_flagWarning (message ("Value for %s must be a non-negative "
a956d444 2799 "number (given %d)",
2800 flagcode_unparse (flag), val));
28bf4b0b 2801 return;
2802 }
616915dd 2803
28bf4b0b 2804 break;
2805 default:
2806 break;
2807 }
982cc10b 2808
2809 DPRINTF (("Set value [%s] %d = %d", flagcode_unparse (flag), index, val));
28bf4b0b 2810 gc.values[index] = val;
616915dd 2811}
2812
2813void
2814context_setValueAndFlag (flagcode flag, int val)
2815{
2816 gc.flags[flag] = TRUE;
2817 context_setValue (flag, val);
2818}
2819
2820int
2821context_getValue (flagcode flag)
2822{
2823 int index = flagcode_valueIndex (flag);
2824
2825 llassert (index >= 0 && index <= NUMVALUEFLAGS);
982cc10b 2826 DPRINTF (("Get value [%s] %d = %d", flagcode_unparse (flag), index, gc.values[index]));
616915dd 2827 return (gc.values[index]);
2828}
2829
2830int
2831context_getCounter (flagcode flag)
2832{
2833 int index = flagcode_valueIndex (flag);
2834
2835 llassert (index >= 0 && index <= NUMVALUEFLAGS);
2836 return (gc.counters[index]);
2837}
2838
2839void
2840context_incCounter (flagcode flag)
2841{
2842 int index = flagcode_valueIndex (flag);
2843
2844 llassert (index >= 0 && index <= NUMVALUEFLAGS);
2845 /* check limit */
2846 gc.counters[index]++;
2847}
2848
2849void
2850context_decCounter (flagcode flag)
2851{
2852 int index = flagcode_valueIndex (flag);
2853
2854 llassert (index >= 0 && index <= NUMVALUEFLAGS);
2855 gc.counters[index]--;
2856}
2857
2858bool context_showFunction (void)
2859{
2860 return (gc.showfunction);
2861}
2862
2863void
2864context_setString (flagcode flag, cstring val)
2865{
2866 int index = flagcode_stringIndex (flag);
2867
2868 llassert (index >= 0 && index <= NUMSTRINGFLAGS);
2869
2870 if (flag == FLG_SYSTEMDIRS)
2871 {
2872 llassert (cstring_isDefined (val));
2873
2874 if (cstring_firstChar (val) == '\"')
2875 {
2876 cstring oval = val;
2877 cstring tval = cstring_copy (cstring_suffix (val, 1));
2878
2879 if (cstring_lastChar (tval) != '\"')
2880 {
2881 int n = cstring_length (tval) - 1;
2882
2883 while (isspace ((int) cstring_getChar (tval, n)))
2884 {
2885 n--;
2886 }
2887
2888 if (cstring_getChar (tval, n) != '\"')
2889 {
abb1cb43 2890 llerror_flagWarning (message ("Setting -systemdirs to string with unmatching quotes: %s", val));
616915dd 2891 }
2892 else
2893 {
2894 cstring otval = tval;
2895 tval = cstring_prefix (tval, n);
2896 cstring_free (otval);
2897 }
2898 }
2899
2900 val = cstring_copy (cstring_clip (tval, cstring_length (tval) - 1));
2901 DPRINTF (("val = %s", val));
2902 cstring_free (tval);
2903 cstring_free (oval);
2904 }
2905 }
2906
2907 if (flag == FLG_TMPDIR)
2908 {
2909 llassert (cstring_isDefined (val));
2910
2911 if (cstring_length (val) == 0)
2912 {
2913 cstring_free (val);
2914 val = message (".%s", cstring_makeLiteralTemp (CONNECTSTR));
2915 }
2916 else if (cstring_lastChar (val) != CONNECTCHAR)
2917 {
2918 val = cstring_appendChar (val, CONNECTCHAR);
2919 }
2920 else
2921 {
2922 ;
2923 }
2924 }
2925
2926 if (cstring_length (val) >= 1
ee229125 2927 && cstring_firstChar (val) == '\"')
616915dd 2928 {
abb1cb43 2929 llerror_flagWarning (message
2930 ("setting %s to string beginning with \". You probably "
2931 "don't meant to have the \"'s.",
2932 flagcode_unparse (flag)));
616915dd 2933 }
2934
2935 if (flag == FLG_BOOLTYPE)
2936 {
2937
2938 }
2939
2940 gc.strings[index] = val;
2941}
2942
2943static /*@exposed@*/ cstring
2944context_exposeString (flagcode flag)
2945{
2946 int index = flagcode_stringIndex (flag);
2947
2948 llassert (index >= 0 && index <= NUMSTRINGFLAGS);
2949 return (gc.strings[index]);
2950}
2951
2952cstring
2953context_getString (flagcode flag)
2954{
2955 return (context_exposeString (flag));
2956}
2957
2958void
2959context_resetErrors (void)
2960{
2961 gc.numerrors = 0;
2962}
2963
2964void context_initMod (void)
2965 /*@globals undef gc; @*/
2966{
2967 gc.kind = CX_GLOBAL;
b7e84605 2968
28bf4b0b 2969 gc.savekind = CX_ERROR;
b7e84605 2970 gc.savecont.glob = FALSE;
2971
616915dd 2972 gc.instandardlib = FALSE;
2973 gc.numerrors = 0;
2974 gc.neednl = FALSE;
2975 gc.linesprocessed = 0;
2976 gc.speclinesprocessed = 0;
2977 gc.insuppressregion = FALSE;
2978 gc.macroMissingParams = FALSE;
2979 gc.preprocessing = FALSE;
2980 gc.incommandline = FALSE;
2981 gc.mc = macrocache_create ();
2982 gc.nmods = 0;
2983 gc.maxmods = DEFAULTMAXMODS;
28bf4b0b 2984 gc.moduleaccess = (maccesst *) dmalloc (sizeof (*gc.moduleaccess) * (gc.maxmods));
2985
616915dd 2986 gc.library = FLG_ANSILIB;
2987
2988 gc.locstack = filelocStack_new ();
2989 gc.modrecs = sRefSetList_undefined;
2990 gc.anyExports = FALSE;
2991
2992 gc.ftab = fileTable_create ();
2993 gc.msgLog = messageLog_new ();
2994 gc.inimport = FALSE;
2995 gc.inDerivedFile = FALSE;
2996 gc.inheader = FALSE;
2997 gc.markers = flagMarkerList_new ();
2998 gc.cont.glob = TRUE;
2999 gc.showfunction = FALSE;
3000 gc.msgAnnote = cstring_undefined;
3001 gc.aliasAnnote = sRef_undefined;
3002 gc.aliasAnnoteAls = sRef_undefined;
3003 gc.boolType = ctype_bool;
3004 gc.mods = sRefSet_new ();
3005
3006 gc.saveloc = fileloc_undefined;
3007
3008 gc.inmacrocache = FALSE;
3009 gc.inclause = NOCLAUSE;
3010 gc.clauses = clauseStack_new ();
3011 gc.globs = globSet_new ();
3012 gc.nacct = typeIdSet_emptySet ();
3013 gc.acct = typeIdSet_emptySet ();
3014 gc.facct = typeIdSet_emptySet ();
3015 gc.savedFlags = FALSE;
3016 gc.pushloc = fileloc_undefined;
3017 gc.protectVars = FALSE;
3018 gc.justpopped = FALSE;
3019 gc.isNullGuarded = NO;
3020 gc.globs_used = globSet_undefined;
3021
3022 allFlagCodes (code)
3023 {
3024 gc.setGlobally[code] = FALSE;
3025 gc.setLocally[code] = FALSE;
982cc10b 3026 }
3027 end_allFlagCodes ;
3028
616915dd 3029 usymtab_initMod ();
3030
3031 context_resetAllFlags ();
982cc10b 3032
3120b462 3033 assertSet (gc.flags); /* Can't use global in defines */
3034 assertSet (gc.saveflags);
3035 assertSet (gc.values);
3036 assertSet (gc.strings);
982cc10b 3037
616915dd 3038 conext_resetAllCounters ();
3120b462 3039 assertSet (gc.counters);
3040
616915dd 3041 context_setMode (DEFAULT_MODE);
28bf4b0b 3042
3043 gc.stateTable = metaStateTable_create ();
3044 gc.annotTable = annotationTable_create ();
3045
d9a28762 3046 gc.inFunctionHeader = FALSE;
3047
28bf4b0b 3048 DPRINTF (("Annotations: \n%s",
3049 cstring_toCharsSafe (annotationTable_unparse (gc.annotTable))));
3050 DPRINTF (("State: \n%s",
3051 cstring_toCharsSafe (metaStateTable_unparse (gc.stateTable))));
3052
616915dd 3053}
3054
3055ctype
3056context_typeofZero (void)
3057{
3058 ctype ct = ctype_int;
3059
3060 if (context_getFlag (FLG_ZEROPTR))
3061 {
3062 ct = ctype_makeConj (ct, ctype_voidPointer);
3063 }
3064
3065 if (context_getFlag (FLG_ZEROBOOL)) {
3066 ct = ctype_makeConj (ct, ctype_bool);
3067 }
3068
3069 return ct;
3070}
3071
3072ctype
3073context_typeofOne (void)
3074{
3075 ctype ct = ctype_int;
3076
3077 /* 1 is on longer a bool (was before 2.4)
3078 if (!context_getFlag (FLG_ABSTRACTBOOL))
3079 {
3080 ct = ctype_makeConj (ct, ctype_bool);
3081 }
3082 */
3083
3084 return (ct);
3085}
3086
3087/*@only@*/ cstring
3088context_unparse (void)
3089{
3090 cstring s;
3091
3092 switch (gc.kind)
3093 {
3094 case CX_LCL:
3095 s = message ("LCL File: %q", fileloc_unparse (g_currentloc));
3096 break;
3097 case CX_LCLLIB:
3098 s = message ("LCL Lib File: %q", fileloc_unparse (g_currentloc));
3099 break;
3100 case CX_GLOBAL:
3101 s = message ("Global Context:%q", fileloc_unparse (g_currentloc));
3102 break;
3103 case CX_INNER:
b7e84605 3104 s = message ("Inner Context [%d] : %q",
3105 gc.cont.cdepth,
3106 fileloc_unparse (g_currentloc));
616915dd 3107 break;
3108 case CX_FUNCTION:
3109 s = message ("Function %q :%q \n\taccess %q\n\tmodifies %q",
3110 uentry_unparse (gc.cont.fcn),
3111 fileloc_unparse (g_currentloc),
3112 typeIdSet_unparse (gc.acct),
3113 sRefSet_unparse (gc.mods));
3114 break;
3115 case CX_MACROFCN:
3116 s = message ("Function Macro %q", uentry_unparse (gc.cont.fcn));
3117 break;
3118 case CX_UNKNOWNMACRO:
3119 s = message ("Forward Specified Macro %q", uentry_unparse (gc.cont.fcn));
3120 break;
3121 case CX_MACROCONST:
3122 s = message ("Constant Macro %q", uentry_unparse (gc.cont.fcn));
3123 break;
3124 case CX_ITERDEF:
3125 s = message ("Iter definition %q", uentry_unparse (gc.cont.fcn));
3126 break;
3127 case CX_ITEREND:
3128 s = message ("Iter end %q", uentry_unparse (gc.cont.fcn));
3129 break;
b7e84605 3130 case CX_FCNDECLARATION:
3131 s = message ("Function declaration %q", uentry_unparse (gc.cont.fcn));
3132 break;
616915dd 3133 default:
3134 s = message ("Un-unparseable context: %d", (int) gc.kind);
3135 break;
3136 }
3137
3138 s = message ("%q\naccess: %q", s, context_unparseAccess ());
3139 return (s);
3140}
3141
3142extern ctype
3143context_currentFunctionType (void)
3144{
3145 if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN)
3146 {
3147 return (uentry_getType (gc.cont.fcn));
3148 }
3149 else if (gc.kind == CX_INNER)
3150 {
3151 llcontbuglit ("context_currentFunctionType: inner context");
3152 do { context_exitInnerPlain (); } while (gc.kind == CX_INNER);
3153 return (context_currentFunctionType ());
3154 }
3155 else
3156 {
3157 llcontbuglit ("context_currentFunctionType: not in function");
3158 return (ctype_undefined);
3159 }
3160}
3161
3162void
3163context_enterInnerContext (void)
3164{
b7e84605 3165 if (context_getFlag (FLG_GRAMMAR))
3166 {
3167 lldiagmsg (message ("Enter inner context: %q", context_unparse ()));
3168 }
3169
616915dd 3170 if (gc.kind == CX_GLOBAL)
3171 {
3172 gc.kind = CX_INNER;
3173 gc.cont.cdepth = 1;
3174 }
3175 else if (gc.kind == CX_INNER)
3176 {
3177 gc.cont.cdepth++;
3178 }
3179 else
3180 {
3181 ;
3182 }
3183
616915dd 3184 usymtab_enterScope ();
3185 pushClause (NOCLAUSE);
3186}
3187
3188void
3189context_exitInnerPlain (void) /*@modifies gc;@*/
3190{
3191 context_exitInner (exprNode_undefined);
3192}
3193
3194void
3195context_exitInner (exprNode exp)
3196{
b7e84605 3197 if (context_getFlag (FLG_GRAMMAR))
3198 {
3199 lldiagmsg (message ("Enter inner context: %q", context_unparse ()));
3200 }
3201
616915dd 3202 llassertprint (gc.inclause == NOCLAUSE || gc.inclause == CASECLAUSE,
3203 ("inclause = %s", clause_nameTaken (gc.inclause)));
3204
3205 clauseStack_removeFirst (gc.clauses, NOCLAUSE);
3206 gc.inclause = topClause (gc.clauses);
3207
3208 if (gc.kind == CX_INNER)
3209 {
3210 if (--gc.cont.cdepth == 0)
3211 {
3212 gc.kind = CX_GLOBAL;
3213 gc.cont.glob = TRUE;
3214 }
3215 }
3216 else
3217 {
3218 if (gc.kind == CX_GLOBAL)
3219 {
3220 llcontbuglit ("Attempt to exit global context");
3221 return;
3222 }
3223 }
3224
3225 usymtab_exitScope (exp);
3226}
3227
3228
3229void
3230context_enterStructInnerContext (void)
3231{
b7e84605 3232 if (context_getFlag (FLG_GRAMMAR))
3233 {
3234 lldiagmsg (message ("Enter struct inner context: %q", context_unparse ()));
3235 }
3236
616915dd 3237 if (gc.kind == CX_GLOBAL)
3238 {
3239 gc.kind = CX_INNER;
3240 gc.cont.cdepth = 1;
3241 }
3242 else if (gc.kind == CX_INNER)
3243 {
3244 gc.cont.cdepth++;
3245 }
3246 else
3247 {
3248 ;
3249 }
3250
3251 usymtab_enterScope ();
b7e84605 3252
3253 if (context_getFlag (FLG_GRAMMAR))
3254 {
3255 lldiagmsg (message ("Enter struct inner context: %q", context_unparse ()));
3256 }
616915dd 3257}
3258
3259void
3260context_exitStructInnerContext (void)
3261{
b7e84605 3262 if (context_getFlag (FLG_GRAMMAR))
3263 {
3264 lldiagmsg (message ("Exit struct inner context: %q [%d]", context_unparse (), gc.cont.cdepth));
3265 }
3266
616915dd 3267 if (gc.kind == CX_INNER)
3268 {
b7e84605 3269 if (gc.cont.cdepth <= 0)
616915dd 3270 {
b7e84605 3271 llcontbuglit ("Attempt to exit inner context with no depth");
616915dd 3272 gc.kind = CX_GLOBAL;
3273 gc.cont.glob = TRUE;
b7e84605 3274 gc.cont.cdepth = 0;
616915dd 3275 }
b7e84605 3276 else {
3277 gc.cont.cdepth--;
3278
3279 if (gc.cont.cdepth == 0)
3280 {
3281 gc.kind = CX_GLOBAL;
3282 gc.cont.glob = TRUE;
3283 }
3284 }
616915dd 3285 }
3286 else
3287 {
3288 if (gc.kind == CX_GLOBAL)
3289 {
3290 llcontbuglit ("Attempt to exit global context");
3291 return;
3292 }
3293 }
3294
3295 usymtab_exitScope (exprNode_undefined);
b7e84605 3296
3297 if (context_getFlag (FLG_GRAMMAR))
3298 {
3299 lldiagmsg (message ("After exit struct inner context: %q [%d]", context_unparse (), gc.cont.cdepth));
3300 }
616915dd 3301}
3302
3303void
3304context_exitInnerSafe (void)
3305{
b7e84605 3306 if (context_getFlag (FLG_GRAMMAR))
3307 {
3308 lldiagmsg (message ("Exit inner safe: %q", context_unparse ()));
3309 }
3310
616915dd 3311 if (gc.kind == CX_INNER)
3312 {
b7e84605 3313 if (--gc.cont.cdepth <= 0)
616915dd 3314 {
b7e84605 3315 gc.cont.cdepth = 0;
616915dd 3316 }
3317 }
3318 else if (gc.kind == CX_GLOBAL)
3319 {
3320 llcontbuglit ("Attempt to exit global context");
3321 return;
3322 }
3323 else
3324 {
3325 if (usymtab_inDeepScope ())
3326 {
3327 usymtab_exitScope (exprNode_undefined);
3328 }
3329 }
3330}
3331
3332static
3333void setModuleAccess (void)
3334{
3335 gc.facct = typeIdSet_emptySet ();
3336
3337 if (fileId_isValid (currentFile ()))
3338 {
3339 cstring baseName = fileloc_getBase (g_currentloc);
3340
3341 if (context_getFlag (FLG_ACCESSFILE))
3342 {
3343 if (usymtab_existsType (baseName))
3344 {
3345 gc.facct = typeIdSet_insert (gc.facct,
3346 usymtab_getTypeId (baseName));
3347 }
3348 else
3349 {
3350 ;
3351 }
3352 }
3353
3354 if (context_getFlag (FLG_ACCESSMODULE))
3355 {
3356 int i;
3357 bool hasaccess = FALSE;
3358
3359 for (i = 0; i < gc.nmods; i++)
3360 {
3361 if (cstring_equal (baseName, gc.moduleaccess[i].file))
3362 {
3363 gc.facct = typeIdSet_union (gc.facct, gc.moduleaccess[i].daccess);
3364
3365 hasaccess = TRUE;
3366 break;
3367 }
3368 }
3369 }
3370
3371 gc.acct = gc.facct;
3372 gc.inheader = fileId_isHeader (currentFile ());
3373 }
3374 else
3375 {
3376 llcontbuglit ("Current file not defined\n");
3377 gc.facct = typeIdSet_emptySet ();
3378 gc.acct = gc.facct;
3379 gc.inheader = FALSE;
3380 }
3381
3382 /* 17 Jan 1995: forgot to clear nacct */
3383
3384 gc.nacct = typeIdSet_emptySet ();
3385}
3386
3387static void
3388context_enterFileAux (void)
3389{
3390 setModuleAccess ();
3391}
3392
3393void
3394context_enterFile (void)
3395{
3396 context_enterFileAux ();
3397 usymtab_enterFile ();
3398}
3399
3400void
3401context_enterMacroFile (void)
3402{
3403 context_enterFileAux ();
3404}
3405
3406bool
3407context_inFunction (void)
3408{
3409 kcontext ck = gc.kind;
3410
3411 return ((ck == CX_FUNCTION) || (ck == CX_MACROFCN) || (ck == CX_INNER));
3412}
3413
3414bool
3415context_inFunctionLike (void)
3416{
3417 return (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN
28bf4b0b 3418 || gc.kind == CX_FCNDECLARATION
616915dd 3419 || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
3420}
3421
3422bool
3423context_inRealFunction (void)
3424{
3425 kcontext ck = gc.kind;
3426
3427 return ((ck == CX_FUNCTION) || (ck == CX_MACROFCN));
3428}
3429
3430void
3431context_processAllMacros (void)
3432{
3433 usymtab_enterFile ();
3434
3435 gc.inmacrocache = TRUE;
3436 macrocache_processUndefinedElements (gc.mc);
3437 cleanupMessages ();
3438 usymtab_exitFile ();
3439
3440 gc.inmacrocache = FALSE;
3441 macrocache_finalize ();
3442}
3443
3444/*
3445** this happens once at the end of each C file
3446**
3447** check each Macro that was defined in current file.c or current file.h
3448**
3449*/
3450
3451static void
3452context_processMacros (void)
3453{
3454 if (fileId_isValid (currentFile ()))
3455 {
3456 fileloc lastfl;
4dd72714 3457 cstring cbase = fileLib_removePathFree (fileLib_removeAnyExtension (fileTable_fileName (currentFile ())));
616915dd 3458
3459 gc.inmacrocache = TRUE;
3460
3461 DPRINTF (("Processing macros: %s", cbase));
3462 lastfl = macrocache_processFileElements (gc.mc, cbase);
3463 DPRINTF (("Processing macros: %s", fileloc_unparse (lastfl)));
3464
3465 cstring_free (cbase);
3466
3467 if (fileloc_isDefined (lastfl))
3468 {
3469 g_currentloc = fileloc_update (g_currentloc, lastfl);
3470 cleanupMessages ();
3471 }
3472
3473 gc.inmacrocache = FALSE;
3474 }
3475}
3476
3477bool
3478context_processingMacros (void)
3479{
3480 return (gc.inmacrocache);
3481}
3482
3483void
28bf4b0b 3484context_exitCFile (void)
616915dd 3485{
3486 if (gc.kind != CX_GLOBAL)
3487 {
3488 llfatalerrorLoc
3489 (cstring_makeLiteral ("File ended outside global scope"));
3490 }
3491
3492 if (gc.insuppressregion)
3493 {
3494 /* gack...don't reverse the order of these lines! ;-> */
3495 gc.insuppressregion = FALSE;
3496 llerrorlit (FLG_SYNTAX,
3497 "File ended in ignore errors region, "
3498 "possible missing /*@end*/");
3499 }
3500
3501 /* fix up parse errors */
3502
3503 while (!usymtab_inFileScope ())
3504 {
3505 usymtab_quietExitScope (g_currentloc);
3506 }
3507
3508 /*
3509 ** Clear the file-specific modifies information.
3510 */
3511
3512 sRefSetList_elements (gc.modrecs, mods)
3513 {
3514 sRefSet_clearStatics (mods);
28bf4b0b 3515 } end_sRefSetList_elements ;
616915dd 3516
3517 sRefSetList_clear (gc.modrecs);
28bf4b0b 3518
616915dd 3519 context_processMacros ();
3520 cleanupMessages ();
28bf4b0b 3521
616915dd 3522 usymtab_exitFile ();
28bf4b0b 3523
616915dd 3524 gc.inDerivedFile = FALSE;
3525 filelocStack_clear (gc.locstack);
28bf4b0b 3526
616915dd 3527 gc.nacct = typeIdSet_emptySet (); /* empty noaccess */
28bf4b0b 3528
616915dd 3529 gc.cont.glob = TRUE;
3530
3531 if (gc.savedFlags)
3532 {
3533 context_restoreFlagSettings ();
3534 gc.savedFlags = FALSE;
3535 }
28bf4b0b 3536
3537 /*
3538 DPRINTF (("After exiting file: "));
3539 usymtab_printAll ();
3540 */
616915dd 3541}
3542
3543void
3544context_exitMacroCache (void)
3545{
3546 if (gc.kind != CX_GLOBAL)
3547 {
28bf4b0b 3548 if (context_inMacro ())
3549 /* this is okay, file could end without newline in macro */
616915dd 3550 {
28bf4b0b 3551 DPRINTF (("Still in macro: %s",
3552 context_unparse ()));
616915dd 3553 context_exitFunction ();
3554 }
3555 else
3556 {
3557 llcontbug (message ("context_exitMacroCache: outside global scope: %q",
3558 context_unparse ()));
3559 gc.kind = CX_GLOBAL;
3560 }
3561 }
3562
3563 /*
3564 ** no longer valid here
3565 ** if (gc.insuppressregion)
3566 ** {
3567 ** gc.insuppressregion = FALSE;
3568 ** llerror ("File ended in ignore errors region, possible missing @");
3569 ** }
3570 */
3571
3572 gc.cont.glob = TRUE;
3573}
3574
3575void
3576context_saveLocation (void)
3577{
3578 /* was llassert (fileloc_isUndefined (gc.saveloc)) */
6970c11b 3579 fileloc_free (gc.saveloc);
616915dd 3580 gc.saveloc = fileloc_copy (g_currentloc);
6970c11b 3581}
616915dd 3582
3583fileloc
3584context_getSaveLocation (void)
3585{
3586 fileloc fl = gc.saveloc;
6970c11b 3587 gc.saveloc = fileloc_undefined;
616915dd 3588 return fl;
3589}
3590
3591/*@observer@*/ cstring
3592context_inFunctionName (void)
3593{
3594 if (gc.kind == CX_FUNCTION
3595 || gc.kind == CX_MACROFCN || gc.kind == CX_UNKNOWNMACRO
3596 || gc.kind == CX_MACROCONST
3597 || gc.kind == CX_ITERDEF || gc.kind == CX_ITEREND)
3598 {
3599 return (uentry_rawName (gc.cont.fcn));
3600 }
3601 else
3602 {
3603 llcontbuglit ("context_inFunctionName: not in function");
3604 return (cstring_undefined);
3605 }
3606}
3607
3608void
3609context_userSetFlag (flagcode f, bool b)
3610{
28bf4b0b 3611 DPRINTF (("set flag: %s", flagcode_unparse (f)));
616915dd 3612
3613 if (f == FLG_NEVERINCLUDE && b)
3614 {
3615 if (gc.flags[FLG_EXPORTHEADER])
3616 {
abb1cb43 3617 llerror_flagWarning (cstring_makeLiteral
3618 ("setting +neverinclude after +exportheader. "
3619 "Turning off exportheader, since headers are not checked "
3620 "when +neverinclude is used."));
616915dd 3621
3622 gc.flags[FLG_EXPORTHEADER] = FALSE;
3623 }
3624 }
3625 else
3626 {
3627 if (f == FLG_EXPORTHEADER && b)
3628 {
3629 if (gc.flags[FLG_NEVERINCLUDE])
3630 {
abb1cb43 3631 llerror_flagWarning (cstring_makeLiteral
3632 ("setting +exportheader after +neverinclude. "
3633 "Not setting exportheader, since headers are not checked "
3634 "when +neverinclude is used."));
616915dd 3635 gc.flags[FLG_EXPORTHEADER] = FALSE;
3636 return;
3637 }
3638 }
3639 }
3640
3641 if (context_getFlag (FLG_WARNFLAGS) && f != FLG_NOF && f != FLG_OPTF)
3642 {
3643 bool lastsetting = context_getFlag (f);
3644
3645 if (bool_equal (lastsetting, b)
3646 && !flagcode_isSpecialFlag (f)
3647 && !flagcode_isIdemFlag (f)
3648 && !flagcode_hasArgument (f))
3649 {
abb1cb43 3650 llerror_flagWarning (message ("setting %s%s redundant with current value",
3651 cstring_makeLiteralTemp (b ? "+" : "-"),
3652 flagcode_unparse (f)));
28bf4b0b 3653 }
3654 }
3655
3656 if (flagcode_isWarnUseFlag (f) && b)
3657 {
3658 if (!context_getFlag (FLG_WARNUSE))
3659 {
abb1cb43 3660 llerror_flagWarning (message ("flag +%s is canceled by -warnuse",
3661 flagcode_unparse (f)));
28bf4b0b 3662
616915dd 3663 }
3664 }
3665
28bf4b0b 3666
616915dd 3667 if (flagcode_isLibraryFlag (f))
3668 {
3669 if (gc.library != FLG_ANSILIB
3670 && gc.library != f)
3671 {
abb1cb43 3672 llerror_flagWarning (message ("selecting library %s after library %s was "
3673 "selected (only one library may be used)",
3674 flagcode_unparse (f),
3675 flagcode_unparse (gc.library)));
616915dd 3676 }
3677
3678 if (f == FLG_UNIXLIB)
3679 {
3680 if (context_getFlag (FLG_WARNUNIXLIB))
3681 {
abb1cb43 3682 llerror_flagWarning (cstring_makeLiteral
3683 ("selecting unix library. Unix library is "
3684 "ad hoc addition to POSIX library. Recommend "
3685 "use +posixlib to select POSIX library instead. "
3686 "Use -warnunixlib to suppress this message."));
616915dd 3687 }
3688 }
3689
3690 gc.library = f;
3691 }
abb1cb43 3692
3693 if (flagcode_isNameChecksFlag (f) && b && !context_maybeSet (FLG_NAMECHECKS))
3694 {
3695 llerror_flagWarning (message
3696 ("setting +%s will not produce warnings with -namechecks. Must set +namechecks also.",
3697 flagcode_unparse (f)));
3698 }
3699
616915dd 3700 gc.setGlobally[f] = TRUE;
3701 context_setFlag (f, b);
3702}
3703
3704void
3705context_fileSetFlag (flagcode f, ynm set)
3706{
3707 if (!gc.savedFlags)
3708 {
3709 context_saveFlagSettings ();
3710 }
3711
3712 if (ynm_isOff (set))
3713 {
3714 context_setFlagAux (f, FALSE, TRUE, FALSE);
3715 }
3716 else if (ynm_isOn (set))
3717 {
3718 context_setFlagAux (f, TRUE, TRUE, FALSE);
3719 gc.setLocally[f] = TRUE;
3720 }
3721 else
3722 {
3723 context_restoreFlag (f);
3724 }
3725}
3726
3727static void
3728context_restoreFlag (flagcode f)
3729{
3730
3731 if (!gc.savedFlags)
3732 {
3733 voptgenerror
3734 (FLG_SYNTAX,
3735 message ("Attempt to restore flag %s when no file scope flags "
3736 "have been set.",
3737 flagcode_unparse (f)),
3738 g_currentloc);
3739 }
3740 else
3741 {
3742 context_addFlagMarker (f, MAYBE);
3743 context_setFlagAux (f, gc.saveflags[f], FALSE, TRUE);
3744 }
3745
3746 }
3747
28bf4b0b 3748static void
616915dd 3749context_setFlag (flagcode f, bool b)
3750{
3751 context_setFlagAux (f, b, FALSE, FALSE);
3752}
3753
3754void
3755context_setFlagTemp (flagcode f, bool b)
3756{
3757 DPRINTF (("Set flag temp: %s / %s", flagcode_unparse (f), bool_unparse (b)));
3758 gc.flags[f] = b;
3759}
3760
3761/*@notfunction@*/
3762# define DOSET(ff,b) \
3763 do { if (inFile) { gc.setLocally[ff] = TRUE; \
3764 context_addFlagMarker (ff, ynm_fromBool (b)); } \
28bf4b0b 3765 DPRINTF (("set flag: %s / %s", flagcode_unparse (ff), bool_unparse (b))); \
616915dd 3766 gc.flags[ff] = b; } while (FALSE)
3767
3768static void
3e3ec469 3769context_setFlagAux (flagcode f, bool b, bool inFile,
3770 /*@unused@*/ bool isRestore)
616915dd 3771{
3e3ec469 3772 DPRINTF (("Set flag: %s / %s", flagcode_unparse (f), bool_unparse (b)));
616915dd 3773
3774 if (f == FLG_USESTDERR)
3775 {
3776 if (b) {
3777 g_msgstream = stderr;
3778 } else {
3779 g_msgstream = stdout;
3780 }
3781 }
3782
28bf4b0b 3783 /*
3784 ** Removed test for special flags.
3785 */
616915dd 3786
28bf4b0b 3787 if (flagcode_isIdemFlag (f))
3788 {
3789 DOSET (f, TRUE);
3790 }
3791 else
3792 {
3793 DOSET (f, b);
3794 }
616915dd 3795
28bf4b0b 3796 if (f >= FLG_ITS4MOSTRISKY && f <= FLG_ITS4LOWRISK)
3797 {
3798 if (b) /* Turing higher level on, turns on all lower levels */
3799 {
3800 switch (f)
616915dd 3801 {
28bf4b0b 3802 case FLG_ITS4MOSTRISKY:
3803 DOSET (FLG_ITS4VERYRISKY, b);
3804 /*@fallthrough@*/
3805 case FLG_ITS4VERYRISKY:
3806 DOSET (FLG_ITS4RISKY, b);
3807 /*@fallthrough@*/
3808 case FLG_ITS4RISKY:
3809 DOSET (FLG_ITS4MODERATERISK, b);
3810 /*@fallthrough@*/
3811 case FLG_ITS4MODERATERISK:
3812 DOSET (FLG_ITS4LOWRISK, b);
3813 /*@fallthrough@*/
3814 case FLG_ITS4LOWRISK:
3815 break;
3816 BADDEFAULT;
616915dd 3817 }
28bf4b0b 3818 }
3819 else /* Turning level off, turns off all higher levels */
3820 {
3821 switch (f)
616915dd 3822 {
28bf4b0b 3823 case FLG_ITS4LOWRISK:
3824 DOSET (FLG_ITS4MODERATERISK, b);
3825 /*@fallthrough@*/
3826 case FLG_ITS4MODERATERISK:
3827 DOSET (FLG_ITS4RISKY, b);
3828 /*@fallthrough@*/
3829 case FLG_ITS4RISKY:
3830 DOSET (FLG_ITS4VERYRISKY, b);
3831 /*@fallthrough@*/
3832 case FLG_ITS4VERYRISKY:
3833 DOSET (FLG_ITS4MOSTRISKY, b);
3834 /*@fallthrough@*/
3835 case FLG_ITS4MOSTRISKY:
3836 break;
3837 BADDEFAULT;
616915dd 3838 }
616915dd 3839 }
3840 }
28bf4b0b 3841
3842 switch (f)
3843 {
3844 case FLG_ALLEMPTY:
3845 DOSET (FLG_ALLEMPTY, b);
3846 DOSET (FLG_IFEMPTY, b);
3847 DOSET (FLG_WHILEEMPTY, b);
3848 DOSET (FLG_FOREMPTY, b);
3849 break;
3850 case FLG_PREDBOOL:
3851 DOSET (FLG_PREDBOOL, b);
3852 DOSET (FLG_PREDBOOLINT, b);
3853 DOSET (FLG_PREDBOOLPTR, b);
3854 DOSET (FLG_PREDBOOLOTHERS, b);
3855 break;
3856 case FLG_GLOBALIAS:
3857 DOSET (FLG_CHECKSTRICTGLOBALIAS, b);
3858 DOSET (FLG_CHECKEDGLOBALIAS, b);
3859 DOSET (FLG_CHECKMODGLOBALIAS, b);
3860 DOSET (FLG_UNCHECKEDGLOBALIAS, b);
3861 break;
3862 case FLG_ALLBLOCK:
3863 DOSET (FLG_ALLBLOCK, b);
3864 DOSET (FLG_IFBLOCK, b);
3865 DOSET (FLG_WHILEBLOCK, b);
3866 DOSET (FLG_FORBLOCK, b);
3867 break;
3868 case FLG_GRAMMAR:
3869 if (b)
616915dd 3870 {
28bf4b0b 3871 yydebug = 1;
3872 mtdebug = 1;
616915dd 3873 }
3874 else
3875 {
28bf4b0b 3876 yydebug = 0;
3877 mtdebug = 0;
3878 }
3879
3880 DOSET (FLG_GRAMMAR, b);
3881 break;
3882 case FLG_CODEIMPONLY:
3883 DOSET (FLG_CODEIMPONLY, b);
3884 DOSET (FLG_GLOBIMPONLY, b);
3885 DOSET (FLG_RETIMPONLY, b);
3886 DOSET (FLG_STRUCTIMPONLY, b);
3887 break;
3888 case FLG_SPECALLIMPONLY:
3889 DOSET (FLG_SPECALLIMPONLY, b);
3890 DOSET (FLG_SPECGLOBIMPONLY, b);
3891 DOSET (FLG_SPECRETIMPONLY, b);
3892 DOSET (FLG_SPECSTRUCTIMPONLY, b);
3893 break;
3894 case FLG_ALLIMPONLY:
3895 DOSET (FLG_ALLIMPONLY, b);
3896 DOSET (FLG_GLOBIMPONLY, b);
3897 DOSET (FLG_RETIMPONLY, b);
3898 DOSET (FLG_STRUCTIMPONLY, b);
3899 DOSET (FLG_SPECGLOBIMPONLY, b);
3900 DOSET (FLG_SPECRETIMPONLY, b);
3901 DOSET (FLG_SPECSTRUCTIMPONLY, b);
3902 break;
27c9e640 3903 case FLG_ANSI89LIMITS:
3904 DOSET (FLG_ANSI89LIMITS, b);
28bf4b0b 3905 DOSET (FLG_CONTROLNESTDEPTH, b);
3906 DOSET (FLG_STRINGLITERALLEN, b);
3907 DOSET (FLG_INCLUDENEST, b);
3908 DOSET (FLG_NUMSTRUCTFIELDS, b);
3909 DOSET (FLG_NUMENUMMEMBERS, b);
3910
3911 if (b)
3912 {
27c9e640 3913 context_setValue (FLG_CONTROLNESTDEPTH, ANSI89_CONTROLNESTDEPTH);
3914 context_setValue (FLG_STRINGLITERALLEN, ANSI89_STRINGLITERALLEN);
3915 context_setValue (FLG_INCLUDENEST, ANSI89_INCLUDENEST);
3916 context_setValue (FLG_NUMSTRUCTFIELDS, ANSI89_NUMSTRUCTFIELDS);
3917 context_setValue (FLG_NUMENUMMEMBERS, ANSI89_NUMENUMMEMBERS);
3918 context_setValue (FLG_EXTERNALNAMELEN, ANSI89_EXTERNALNAMELEN);
3919 context_setValue (FLG_INTERNALNAMELEN, ANSI89_INTERNALNAMELEN);
3920 }
3921 break;
3922 case FLG_ISO99LIMITS:
3923 DOSET (FLG_ISO99LIMITS, b);
3924 DOSET (FLG_CONTROLNESTDEPTH, b);
3925 DOSET (FLG_STRINGLITERALLEN, b);
3926 DOSET (FLG_INCLUDENEST, b);
3927 DOSET (FLG_NUMSTRUCTFIELDS, b);
3928 DOSET (FLG_NUMENUMMEMBERS, b);
3929
3930 if (b)
3931 {
3932 context_setValue (FLG_CONTROLNESTDEPTH, ISO99_CONTROLNESTDEPTH);
3933 context_setValue (FLG_STRINGLITERALLEN, ISO99_STRINGLITERALLEN);
3934 context_setValue (FLG_INCLUDENEST, ISO99_INCLUDENEST);
3935 context_setValue (FLG_NUMSTRUCTFIELDS, ISO99_NUMSTRUCTFIELDS);
3936 context_setValue (FLG_NUMENUMMEMBERS, ISO99_NUMENUMMEMBERS);
3937 context_setValue (FLG_EXTERNALNAMELEN, ISO99_EXTERNALNAMELEN);
3938 context_setValue (FLG_INTERNALNAMELEN, ISO99_INTERNALNAMELEN);
28bf4b0b 3939 }
3940 break;
3941 case FLG_EXTERNALNAMELEN:
3942 DOSET (FLG_DISTINCTEXTERNALNAMES, TRUE);
3943 DOSET (FLG_EXTERNALNAMELEN, TRUE);
3944 break;
3945 case FLG_INTERNALNAMELEN:
3946 DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3947 DOSET (FLG_INTERNALNAMELEN, TRUE);
3948 break;
3949 case FLG_EXTERNALNAMECASEINSENSITIVE:
3950 DOSET (FLG_EXTERNALNAMECASEINSENSITIVE, b);
3951
3952 if (b && !gc.flags[FLG_DISTINCTEXTERNALNAMES])
3953 {
3954 DOSET (FLG_DISTINCTEXTERNALNAMES, TRUE);
3955 context_setValue (FLG_EXTERNALNAMELEN, 0);
3956 }
3957 break;
3958 case FLG_INTERNALNAMECASEINSENSITIVE:
3959 DOSET (FLG_INTERNALNAMECASEINSENSITIVE, b);
3960
3961 if (b && !gc.flags[FLG_DISTINCTINTERNALNAMES])
3962 {
3963 DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3964 context_setValue (FLG_INTERNALNAMELEN, 0);
3965 }
3966 break;
3967 case FLG_INTERNALNAMELOOKALIKE:
3968 DOSET (FLG_INTERNALNAMELOOKALIKE, b);
3969
3970 if (b && !gc.flags[FLG_DISTINCTINTERNALNAMES])
3971 {
3972 DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3973 context_setValue (FLG_INTERNALNAMELEN, 0);
616915dd 3974 }
28bf4b0b 3975 break;
3976 case FLG_MODUNSPEC:
3977 DOSET (FLG_MODNOMODS, b);
3978 DOSET (FLG_MODGLOBSUNSPEC, b);
3979 DOSET (FLG_MODSTRICTGLOBSUNSPEC, b);
3980 break;
3981 case FLG_EXPORTANY:
3982 DOSET (FLG_EXPORTVAR, b);
3983 DOSET (FLG_EXPORTFCN, b);
3984 DOSET (FLG_EXPORTTYPE, b);
3985 DOSET (FLG_EXPORTMACRO, b);
3986 DOSET (FLG_EXPORTCONST, b);
3987 gc.anyExports = TRUE;
3988 break;
3989 case FLG_REPEXPOSE:
3990 DOSET (FLG_RETEXPOSE, b);
3991 DOSET (FLG_ASSIGNEXPOSE, b);
3992 DOSET (FLG_CASTEXPOSE, b);
3993 break;
3994 case FLG_RETVAL:
3995 DOSET (FLG_RETVALBOOL, b);
3996 DOSET (FLG_RETVALINT, b);
3997 DOSET (FLG_RETVALOTHER, b);
3998 break;
3999 case FLG_PARTIAL:
4000 if (b)
4001 {
4002 DOSET (FLG_EXPORTLOCAL, FALSE);
4003 DOSET (FLG_DECLUNDEF, FALSE);
4004 DOSET (FLG_SPECUNDEF, FALSE);
4005 DOSET (FLG_TOPUNUSED, FALSE);
4006 }
4007 break;
4008 case FLG_DEEPBREAK:
4009 DOSET (FLG_LOOPLOOPBREAK, b);
4010 DOSET (FLG_LOOPSWITCHBREAK, b);
4011 DOSET (FLG_SWITCHLOOPBREAK, b);
4012 DOSET (FLG_SWITCHSWITCHBREAK, b);
4013 DOSET (FLG_LOOPLOOPCONTINUE, b);
4014 DOSET (FLG_DEEPBREAK, b);
4015 break;
3e3ec469 4016 case FLG_LOOPEXEC:
4017 DOSET (FLG_FORLOOPEXEC, b);
4018 DOSET (FLG_WHILELOOPEXEC, b);
4019 DOSET (FLG_ITERLOOPEXEC, b);
4020 break;
28bf4b0b 4021 case FLG_ACCESSALL:
4022 DOSET (FLG_ACCESSMODULE, b);
4023 DOSET (FLG_ACCESSFILE, b);
4024 DOSET (FLG_ACCESSCZECH, b);
4025 break;
4026 case FLG_ALLMACROS:
4027 DOSET (FLG_ALLMACROS, b);
4028 DOSET (FLG_FCNMACROS, b);
4029 DOSET (FLG_CONSTMACROS, b);
4030 break;
bb7c2085 4031 case FLG_BOUNDS:
4032 DOSET (FLG_BOUNDSREAD, b);
4033 DOSET (FLG_BOUNDSWRITE, b);
4034 break;
28bf4b0b 4035 case FLG_CZECH:
4036 if (b) { DOSET (FLG_ACCESSCZECH, b); }
4037 DOSET (FLG_CZECHFUNCTIONS, b);
4038 DOSET (FLG_CZECHVARS, b);
4039 DOSET (FLG_CZECHCONSTANTS, b);
4040 DOSET (FLG_CZECHTYPES, b);
4041 break;
4042 case FLG_SLOVAK:
4043 if (b) { DOSET (FLG_ACCESSSLOVAK, b); }
4044 DOSET (FLG_SLOVAKFUNCTIONS, b);
4045 DOSET (FLG_SLOVAKVARS, b);
4046 DOSET (FLG_SLOVAKCONSTANTS, b);
4047 DOSET (FLG_SLOVAKTYPES, b);
4048 break;
4049 case FLG_CZECHOSLOVAK:
4050 if (b) { DOSET (FLG_ACCESSCZECHOSLOVAK, b); }
4051 DOSET (FLG_CZECHOSLOVAKFUNCTIONS, b);
4052 DOSET (FLG_CZECHOSLOVAKVARS, b);
4053 DOSET (FLG_CZECHOSLOVAKCONSTANTS, b);
4054 DOSET (FLG_CZECHOSLOVAKTYPES, b);
4055 break;
4056 case FLG_NULL:
4057 DOSET (FLG_NULLSTATE, b);
4058 DOSET (FLG_NULLDEREF, b);
4059 DOSET (FLG_NULLASSIGN, b);
4060 DOSET (FLG_NULLPASS, b);
4061 DOSET (FLG_NULLRET, b);
4062 break;
2e127cb8 4063 case FLG_MUSTFREE:
4064 DOSET (FLG_MUSTFREEONLY, b);
4065 DOSET (FLG_MUSTFREEFRESH, b);
4066 break;
28bf4b0b 4067 case FLG_MEMCHECKS:
4068 DOSET (FLG_NULLSTATE, b);
4069 DOSET (FLG_NULLDEREF, b);
4070 DOSET (FLG_NULLASSIGN, b);
4071 DOSET (FLG_NULLPASS, b);
4072 DOSET (FLG_NULLRET, b);
4073 DOSET (FLG_COMPDEF, b);
4074 DOSET (FLG_COMPMEMPASS, b);
4075 DOSET (FLG_UNIONDEF, b);
4076 DOSET (FLG_MEMTRANS, b);
4077 DOSET (FLG_USERELEASED, b);
4078 DOSET (FLG_ALIASUNIQUE, b);
4079 DOSET (FLG_MAYALIASUNIQUE, b);
2e127cb8 4080 DOSET (FLG_MUSTFREEONLY, b);
4081 DOSET (FLG_MUSTFREEFRESH, b);
28bf4b0b 4082 DOSET (FLG_MUSTDEFINE, b);
4083 DOSET (FLG_GLOBSTATE, b);
4084 DOSET (FLG_COMPDESTROY, b);
4085 DOSET (FLG_MUSTNOTALIAS, b);
4086 DOSET (FLG_MEMIMPLICIT, b);
4087 DOSET (FLG_BRANCHSTATE, b);
4088 /*@fallthrough@*/ /* also sets memtrans flags */
4089 case FLG_MEMTRANS:
4090 DOSET (FLG_MEMTRANS, b);
4091 DOSET (FLG_EXPOSETRANS, b);
4092 DOSET (FLG_OBSERVERTRANS, b);
4093 DOSET (FLG_DEPENDENTTRANS, b);
4094 DOSET (FLG_NEWREFTRANS, b);
4095 DOSET (FLG_ONLYTRANS, b);
4096 DOSET (FLG_OWNEDTRANS, b);
4097 DOSET (FLG_FRESHTRANS, b);
4098 DOSET (FLG_SHAREDTRANS, b);
4099 DOSET (FLG_TEMPTRANS, b);
4100 DOSET (FLG_KEPTTRANS, b);
4101 DOSET (FLG_REFCOUNTTRANS, b);
4102 DOSET (FLG_STATICTRANS, b);
4103 DOSET (FLG_UNKNOWNTRANS, b);
4104 DOSET (FLG_KEEPTRANS, b);
4105 DOSET (FLG_IMMEDIATETRANS, b);
4106 break;
4107
4108 default:
4109 break;
4110 }
4111
4112 if (b && !gc.anyExports
4113 && (f == FLG_EXPORTVAR || f == FLG_EXPORTFCN
4114 || f == FLG_EXPORTTYPE || f == FLG_EXPORTMACRO
4115 || f == FLG_EXPORTCONST
4116 || f == FLG_EXPORTANY))
4117 {
4118 gc.anyExports = TRUE;
616915dd 4119 }
4120}
4121
4122bool
4123context_maybeSet (flagcode d)
4124{
4125 return (gc.flags[d] || gc.setLocally[d]);
4126}
4127
4128bool
4129context_getFlag (flagcode d)
4130{
4131 return (gc.flags[d]);
4132}
4133
28bf4b0b 4134bool
4135context_flagOn (flagcode f, fileloc loc)
4136{
4137 return (!context_suppressFlagMsg (f, loc));
4138}
4139
616915dd 4140static void context_saveFlagSettings (void)
4141{
4142 gc.savedFlags = TRUE;
4143 llassert (sizeof (gc.saveflags) == sizeof (gc.flags));
4144 memcpy (gc.saveflags, gc.flags, sizeof (gc.flags));
4145}
4146
4147static void context_restoreFlagSettings (void)
4148{
4149 llassert (sizeof (gc.saveflags) == sizeof (gc.flags));
4150 memcpy (gc.flags, gc.saveflags, sizeof (gc.flags));
4151 gc.savedFlags = FALSE;
4152}
4153
4154void context_setFilename (fileId fid, int lineno)
4155 /*@globals fileloc g_currentloc;@*/
4156 /*@modifies g_currentloc@*/
4157{
4158 if (fileId_baseEqual (currentFile (), fid))
4159 {
4160 setLine (lineno);
4161 return;
4162 }
4163 else
4164 {
4165 fileloc_setColumn (g_currentloc, 0);
4166
4167 if (fileloc_isSpecialFile (g_currentloc))
4168 {
4169 gc.inDerivedFile = TRUE;
4170 }
4171
4172 if (filelocStack_popPushFile (gc.locstack, g_currentloc))
4173 {
4174 int maxdepth = context_getValue (FLG_INCLUDENEST);
4175
4176 if (filelocStack_size (gc.locstack) > maxdepth)
4177 {
4178 int depth = filelocStack_includeDepth (gc.locstack);
4179
4180 if (depth > maxdepth)
4181 {
4182 if (optgenerror
4183 (FLG_INCLUDENEST,
4184 message ("Maximum include nesting depth "
4185 "(%d, current depth %d) exceeded",
4186 maxdepth,
4187 depth),
4188 filelocStack_nextTop (gc.locstack)))
4189 {
4190 filelocStack_printIncludes (gc.locstack);
4191 }
4192 }
4193 }
4194 }
4195
4196 g_currentloc = fileloc_create (fid, lineno, 1);
4197 gc.inheader = fileId_isHeader (currentFile ());
4198
4199 context_enterFileAux ();
4200 }
4201}
4202
4203void context_enterIterDef (/*@observer@*/ uentry le)
4204{
b7e84605 4205 context_enterMacro (le);
616915dd 4206 gc.acct = typeIdSet_subtract (gc.facct, gc.nacct);
4207 gc.kind = CX_ITERDEF;
4208}
4209
4210void context_enterIterEnd (/*@observer@*/ uentry le)
4211{
b7e84605 4212 context_enterMacro (le);
616915dd 4213 gc.kind = CX_ITEREND;
4214}
4215
4216void
4217context_destroyMod (void)
4218 /*@globals killed gc@*/
4219{
4220 setCodePoint ();
4221 ctype_destroyMod ();
4222 setCodePoint ();
4223 usymtab_free ();
4224 setCodePoint ();
4225 fileTable_free (gc.ftab);
4226 filelocStack_free (gc.locstack);
4227 setCodePoint ();
4228 gc.ftab = fileTable_undefined;
4229
4230 macrocache_free (gc.mc);
4231 sfree (gc.moduleaccess);
4232 setCodePoint ();
4233
4234 fileloc_free (gc.saveloc); gc.saveloc = fileloc_undefined;
4235 fileloc_free (gc.pushloc); gc.pushloc = fileloc_undefined;
4236
4237 setCodePoint ();
4238 sRefSetList_free (gc.modrecs);
4239 setCodePoint ();
4240 flagMarkerList_free (gc.markers);
4241 setCodePoint ();
4242 messageLog_free (gc.msgLog);
4243 setCodePoint ();
4244 clauseStack_free (gc.clauses);
4245 setCodePoint ();
4246
4247 cstring_free (gc.msgAnnote);
4248 globSet_free (gc.globs_used);
28bf4b0b 4249 metaStateTable_free (gc.stateTable);
4250 annotationTable_free (gc.annotTable);
4251}
616915dd 4252
4253/*
4254** Flag shortcuts.
4255*/
4256
4257bool context_msgBoolInt (void)
4258{
4259 return gc.flags [FLG_BOOLINT];
4260}
4261
4262bool context_msgCharInt (void)
4263{
4264 return gc.flags [FLG_CHARINT];
4265}
4266
4267bool context_msgEnumInt (void)
4268{
4269 return gc.flags [FLG_ENUMINT];
4270}
4271
4272bool context_msgPointerArith (void)
4273{
4274 return gc.flags [FLG_POINTERARITH];
4275}
4276
4277bool context_msgStrictOps (void)
4278{
4279 return gc.flags [FLG_STRICTOPS];
4280}
4281
4282# ifndef NOLCL
4283bool context_msgLh (void)
4284{
4285 return gc.flags [FLG_DOLH];
4286}
4287# endif
4288
4289void context_pushLoc (void)
4290{
4291 fileloc_free (gc.pushloc);
4292 gc.pushloc = gc.saveloc;
4293 gc.saveloc = fileloc_undefined;
4294}
4295
4296void context_popLoc (void)
4297{
4298 gc.saveloc = fileloc_update (gc.saveloc, gc.pushloc);
4299}
4300
4301bool context_inGlobalScope (void)
4302{
4303 return (usymtab_inFileScope() || usymtab_inGlobalScope ());
4304}
4305
4306bool context_inInnerScope (void)
4307{
4308 return (gc.kind == CX_INNER);
4309}
4310
4311void context_setProtectVars (void)
4312{
4313 gc.protectVars = TRUE;
4314}
4315
4316bool context_anyErrors (void)
4317{
4318 return (gc.numerrors > 0);
4319}
4320
4321void context_hasError (void)
4322{
4323 gc.numerrors++;
ccf0a4a8 4324 DPRINTF (("num errors: %d", gc.numerrors));
616915dd 4325}
4326
4327int context_numErrors (void)
4328{
4329 return gc.numerrors;
4330}
4331
4332bool context_neednl (void)
4333{
4334 return gc.neednl;
4335}
4336
4337void context_setNeednl (void)
4338{
4339 gc.neednl = TRUE;
4340}
4341
4342int context_getExpect (void)
4343{
4344 return (context_getValue (FLG_EXPECT));
4345}
4346
4347# ifndef NOLCL
4348int context_getLCLExpect (void)
4349{
4350 return (context_getValue (FLG_LCLEXPECT));
4351}
4352# endif
4353
4354int context_getLimit (void)
4355{
4356 return (context_getValue (FLG_LIMIT));
4357}
4358
4359bool context_unlimitedMessages (void)
4360{
4361 return (context_getLimit () < 0);
4362}
4363
4364void context_releaseVars (void)
4365{
4366 llassert (gc.protectVars);
4367 gc.protectVars = FALSE;
4368}
4369
4370void context_sizeofReleaseVars (void)
4371{
4372 /* If there is a nested sizeof, this might not hold:
4373 llassert (gc.protectVars);
4374 */
4375
4376 gc.protectVars = FALSE;
4377}
4378
4379bool context_inProtectVars (void)
4380{
4381 return (gc.protectVars);
4382}
4383
4384void context_hideShowscan (void)
4385{
4386 gc.flags[FLG_SHOWSCAN] = FALSE;
4387}
4388
4389void context_unhideShowscan (void)
4390{
4391 gc.flags[FLG_SHOWSCAN] = TRUE;
4392}
4393
4394bool context_inHeader (void)
4395{
4396 return (gc.inheader);
4397}
4398
4399fileTable context_fileTable (void)
4400{
4401 return gc.ftab;
4402}
4403
4404cstring context_tmpdir (void)
4405{
4406 return (context_getString (FLG_TMPDIR));
4407}
4408
4409messageLog context_messageLog (void)
4410{
4411 return gc.msgLog;
4412}
4413
4414bool context_inMacroFunction (void)
4415{
4416 return (gc.kind == CX_MACROFCN);
4417}
4418
4419bool context_inMacroConstant (void)
4420{
4421 return (gc.kind == CX_MACROCONST);
4422}
4423
b7e84605 4424bool context_inUnknownMacro (void)
616915dd 4425{
4426 return (gc.kind == CX_UNKNOWNMACRO);
4427}
4428
4429void context_setShownFunction (void)
4430{
4431 gc.showfunction = FALSE;
4432}
4433
4434bool context_doDump (void)
4435{
4436 return cstring_isNonEmpty (context_getString (FLG_DUMP));
4437}
4438
4439bool context_doMerge (void)
4440{
4441 return cstring_isNonEmpty (context_getString (FLG_MERGE));
4442}
4443
4444cstring context_getDump (void)
4445{
4446 return context_getString (FLG_DUMP);
4447}
4448
4449cstring context_getMerge (void)
4450{
4451 return context_getString (FLG_MERGE);
4452}
4453
4454# ifndef NOLCL
4455bool context_inLCLLib (void)
4456{
4457 return (gc.kind == CX_LCLLIB);
4458}
4459
4460bool context_inImport (void)
4461{
4462 return (gc.inimport);
4463}
4464
4465void context_enterImport (void)
4466{
4467 gc.inimport = TRUE;
4468}
4469
4470void context_leaveImport (void)
4471{
4472 gc.inimport = FALSE;
4473}
4474# endif
4475
4476bool context_inMacro (void)
4477{
4478 return (gc.kind == CX_MACROFCN || gc.kind == CX_MACROCONST
4479 || gc.kind == CX_UNKNOWNMACRO
4480 || gc.kind == CX_ITERDEF || gc.kind == CX_ITEREND);
4481}
4482
4483bool context_inIterDef (void)
4484{
4485 return (gc.kind == CX_ITERDEF);
4486}
4487
4488bool context_inIterEnd (void)
4489{
4490 return (gc.kind == CX_ITEREND);
4491}
4492
4493int context_getLinesProcessed (void)
4494{
4495 return (gc.linesprocessed);
4496}
4497
4498int context_getSpecLinesProcessed (void)
4499{
4500 return (gc.speclinesprocessed);
4501}
4502
4503# ifndef NOLCL
4504void context_processedSpecLine (void)
4505{
4506 gc.speclinesprocessed++;
4507}
4508
4509void context_resetSpecLines (void)
4510{
4511 gc.speclinesprocessed = 0;
4512}
4513# endif
4514
4515bool context_inGlobalContext (void)
4516{
4517 return (gc.kind == CX_GLOBAL);
4518}
4519
b7e84605 4520static void context_quietExitScopes (void)
4521{
4522 /*
4523 ** Try to restore the global scope (after an error).
4524 */
4525
4526 while (!usymtab_inFileScope ())
4527 {
4528 usymtab_quietExitScope (g_currentloc);
4529 }
4530
4531 gc.cont.glob = TRUE;
4532 gc.kind = CX_GLOBAL;
4533}
4534
4535void context_checkGlobalScope (void)
4536{
4537 if (gc.kind != CX_GLOBAL)
4538 {
a469ccf0 4539 if (context_inMacro ())
4540 {
4541 ; /* evans 2001-10-14: Okay to be in a macro here! */
4542 }
4543 else
4544 {
4545 llcontbug (message ("Not in global scope as expected: %q", context_unparse ()));
4546 context_quietExitScopes ();
4547 }
b7e84605 4548 }
4549}
4550
616915dd 4551void context_setFileId (fileId s)
4552{
4553 g_currentloc = fileloc_updateFileId (g_currentloc, s);
4554}
4555
4556bool context_setBoolName (void)
4557{
4558 return (!cstring_equalLit (context_getString (FLG_BOOLTYPE),
4559 DEFAULT_BOOLTYPE));
4560}
4561
4562cstring context_printBoolName (void)
4563{
4564 if (context_setBoolName ())
4565 {
4566 return context_getBoolName ();
4567 }
4568 else
4569 {
4570 return cstring_makeLiteralTemp ("boolean");
4571 }
4572}
4573
4574cstring context_getBoolName (void)
4575{
4576 return (context_getString (FLG_BOOLTYPE));
4577}
4578
4579cstring context_getFalseName (void)
4580{
4581 return (context_getString (FLG_BOOLFALSE));
4582}
4583
4584cstring context_getTrueName (void)
4585{
4586 return (context_getString (FLG_BOOLTRUE));
4587}
4588
4589cstring context_getLarchPath (void)
4590{
4591 return (context_getString (FLG_LARCHPATH));
4592}
4593
4594cstring context_getLCLImportDir (void)
4595{
4596 return (context_getString (FLG_LCLIMPORTDIR));
4597}
4598
4599static void context_setJustPopped (void)
4600{
4601 gc.justpopped = TRUE;
4602}
4603
4604void context_clearJustPopped (void)
4605{
4606 gc.justpopped = FALSE;
4607}
4608
4609bool context_justPopped (void)
4610{
4611 return (gc.justpopped);
4612}
4613
4614void context_setMacroMissingParams (void)
4615{
4616 gc.macroMissingParams = TRUE;
4617}
4618
4619void context_resetMacroMissingParams (void)
4620{
4621 gc.macroMissingParams = FALSE;
4622}
4623
4624bool context_isMacroMissingParams (void)
4625{
4626 return (gc.macroMissingParams);
4627}
4628
4629void context_showFilelocStack (void)
4630{
4631 filelocStack_printIncludes (gc.locstack);
4632}
4633
28bf4b0b 4634metaStateTable context_getMetaStateTable (void)
4635{
4636 return gc.stateTable;
4637}
4638
4639metaStateInfo context_lookupMetaStateInfo (cstring key)
4640{
4641 return metaStateTable_lookup (gc.stateTable, key);
4642}
4643
4644/*@null@*/ annotationInfo context_lookupAnnotation (cstring annot)
4645{
4646 annotationInfo ainfo;
4647
4648 ainfo = annotationTable_lookup (gc.annotTable, annot);
4649
4650 return ainfo;
4651}
4652
4653void context_addAnnotation (annotationInfo ainfo)
4654{
4655 if (annotationTable_contains (gc.annotTable, annotationInfo_getName (ainfo)))
4656 {
4657 voptgenerror
4658 (FLG_SYNTAX,
4659 message ("Duplicate annotation declaration: %s", annotationInfo_getName (ainfo)),
4660 annotationInfo_getLoc (ainfo));
4661
4662 annotationInfo_free (ainfo);
4663 }
4664 else
4665 {
4666 annotationTable_insert (gc.annotTable, ainfo);
4667 }
4668}
4669
4670void context_addMetaState (cstring mname, metaStateInfo msinfo)
4671{
4672 if (metaStateTable_contains (gc.stateTable, mname))
4673 {
4674 voptgenerror
4675 (FLG_SYNTAX,
4676 message ("Duplicate metastate declaration: %s", mname),
4677 metaStateInfo_getLoc (msinfo));
4678 cstring_free (mname);
4679 metaStateInfo_free (msinfo);
4680 }
4681 else
4682 {
990ec868 4683 DPRINTF (("Adding meta state: %s", mname));
28bf4b0b 4684 metaStateTable_insert (gc.stateTable, mname, msinfo);
4685 }
4686}
616915dd 4687
6970c11b 4688valueTable context_createValueTable (sRef s, stateInfo sinfo)
28bf4b0b 4689{
4690 if (metaStateTable_size (gc.stateTable) > 0)
4691 {
4692 valueTable res = valueTable_create (metaStateTable_size (gc.stateTable));
4693 /*@i32 should use smaller value... */
4694 DPRINTF (("Value table for: %s", sRef_unparse (s)));
990ec868 4695
28bf4b0b 4696 metaStateTable_elements (gc.stateTable, msname, msi)
4697 {
4698 mtContextNode context = metaStateInfo_getContext (msi);
616915dd 4699
28bf4b0b 4700 if (mtContextNode_matchesRefStrict (context, s))
4701 {
4702 DPRINTF (("Create: %s", metaStateInfo_unparse (msi)));
4703 llassert (cstring_equal (msname, metaStateInfo_getName (msi)));
4704
990ec868 4705 valueTable_insert
4706 (res,
4707 cstring_copy (metaStateInfo_getName (msi)),
6970c11b 4708 stateValue_createImplicit (metaStateInfo_getDefaultValue (msi, s),
4709 stateInfo_copy (sinfo)));
28bf4b0b 4710 }
4711 else
4712 {
4713 DPRINTF (("No match: %s", metaStateInfo_unparse (msi)));
4714 }
4715 }
4716 end_metaStateTable_elements ;
4717
6970c11b 4718 stateInfo_free (sinfo);
28bf4b0b 4719 DPRINTF (("Value table: %s", valueTable_unparse (res)));
4720 return res;
4721 }
4722 else
4723 {
6970c11b 4724 stateInfo_free (sinfo);
28bf4b0b 4725 return valueTable_undefined;
4726 }
4727}
616915dd 4728
6970c11b 4729valueTable context_createGlobalMarkerValueTable (stateInfo sinfo)
28bf4b0b 4730{
4731 if (metaStateTable_size (gc.stateTable) > 0)
4732 {
4733 valueTable res = valueTable_create (metaStateTable_size (gc.stateTable));
4734 /*@i32 should use smaller value... */
4735
4736 metaStateTable_elements (gc.stateTable, msname, msi)
4737 {
4738 /*@i23 only add global...*/
4739 DPRINTF (("Create: %s", metaStateInfo_unparse (msi)));
4740 llassert (cstring_equal (msname, metaStateInfo_getName (msi)));
4741
4742 valueTable_insert (res,
4743 cstring_copy (metaStateInfo_getName (msi)),
4744 stateValue_create (metaStateInfo_getDefaultGlobalValue (msi),
6970c11b 4745 stateInfo_copy (sinfo)));
28bf4b0b 4746 }
4747 end_metaStateTable_elements ;
4748
6970c11b 4749 stateInfo_free (sinfo);
28bf4b0b 4750 DPRINTF (("Value table: %s", valueTable_unparse (res)));
4751 return res;
4752 }
4753 else
4754 {
6970c11b 4755 stateInfo_free (sinfo);
28bf4b0b 4756 return valueTable_undefined;
4757 }
4758}
616915dd 4759
4760
4761
86d93ed3 4762/*drl 12/30/01 these are some ugly functions that were added to facilitate struct annotations */
4763
4764
4765/*drl added */
4766static ctype lastStruct;
4767
4768ctype context_setLastStruct (/*@returned@*/ ctype s) /*@globals lastStruct@*/
4769{
4770 lastStruct = s;
4771 return s;
4772}
4773
4774ctype context_getLastStruct (/*@returned@*/ /*ctype s*/) /*@globals lastStruct@*/
4775{
4776 return lastStruct;
4777}
4778
4779
4780/*@unused@*/ static int sInfoNum = 0;
4781
4782
4783struct getUe {
4784 /*@unused@*/ uentry ue;
4785 /*@unused@*/ sRef s;
4786};
4787
4788struct sInfo {
4789 /*@unused@*/ ctype ct;
4790 /*@unused@*/ constraintList inv;
4791 /*@unused@*/ int ngetUe;
4792 /*@unused@*/ struct getUe * t ;
4793};
4794
4795
4796static struct sInfo globalStructInfo;
4797
4798
4799/*drl 1/6/2001: I didn't think these functions were solid enough to include in the
4800 stable release of splint. I coomented them out so that they won't break anything
4801 but didn't delete them because they will be fixed and included later
4802*/
4803
4804/*
4805void setGlobalStructInfo(ctype ct, constraintList list)
4806{
4807 int i;
4808 uentryList f;
4809
4810 f = ctype_getFields (ct);
4811
4812 if (constraintList_isDefined(list) )
4813 {
4814 globalStructInfo.ct = ct;
4815 globalStructInfo.inv = list;
4816
4817 globalStructInfo.ngetUe = 0;
4818
4819 / *abstraction violation fix it * /
4820 globalStructInfo.t = dmalloc(f->nelements * sizeof(struct getUe) );
4821
4822 globalStructInfo.ngetUe = f->nelements;
4823
4824 i = 0;
4825
4826 uentryList_elements(f, ue)
4827 {
4828 globalStructInfo.t[i].ue = ue;
4829 globalStructInfo.t[i].s = uentry_getSref(ue);
4830 TPRINTF(( message(" setGlobalStructInfo:: adding ue=%s and sRef=%s",
4831 uentry_unparse(ue), sRef_unparse( uentry_getSref(ue) )
4832 )
4833 ));
4834 i++;
4835 }
4836 end_uentryList_elements;
4837 }
4838}
4839
4840*/
4841
4842bool hasInvariants (ctype ct) /*@*/
4843{
4844 if ( ctype_sameName(globalStructInfo.ct, ct) )
4845
4846 return TRUE;
4847
4848 else
4849
4850 return FALSE;
4851
4852}
4853
4854/*drl 1/6/2001: I didn't think these functions were solid enough to include in the
4855 stable release of splint. I coomented them out so that they won't break anything
4856 but didn't delete them because they will be fixed and included later
4857*/
4858
4859/*
4860constraintList getInvariants (ctype ct)
4861{
4862
4863 llassert(hasInvariants(ct) );
4864
4865 return globalStructInfo.inv;
4866}
4867*/
4868
4869/*
4870static int getSref (ctype ct, sRef s)
4871{
4872 int i;
4873
4874 i = 0;
4875
4876 / *
4877 DEBUGGIN INFO
4878
4879 fprintf(stderr, "getSref: ct = %s (%x)\n", ctype_unparse(ct), ct );
4880
4881 fprintf(stderr,"getSref: s = (%s) %X \n", sRef_unparse(s), s);
4882 * /
4883
4884 while (i < globalStructInfo.ngetUe)
4885 {
4886 DPRINTF(( message(" getSref:: comparing ue=%s and sRef=%s",
4887 uentry_unparse(globalStructInfo.t[i].ue),
4888 sRef_unparse(globalStructInfo.t[i].s)
4889 )
4890 ));
4891
4892 / *
4893 fprintf (stderr, " globalStructInfo.t[i].s = %x\n ",
4894 globalStructInfo.t[i].s );
4895 * /
4896
4897 if (sRef_same(globalStructInfo.t[i].s,s) )
4898 return i;
4899
4900 i++;
4901 }
4902 return -1;
4903}
4904
4905
4906sRef fixSref (ctype ct, sRef base, sRef fix)
4907{
4908 int index;
4909 uentry ue;
4910 cstring name;
4911 index = getSref(ct, fix);
4912
4913 if (index < 0)
4914 return fix;
4915
4916 ue = globalStructInfo.t[index].ue;
4917 name = uentry_getName(ue);
4918 fix = sRef_buildField(base, name );
4919 cstring_free(name);
4920 return fix;
4921}
4922
4923*/
This page took 0.960216 seconds and 5 git commands to generate.