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