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