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