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