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