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