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