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