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