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