2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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.
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
31 # include "lclintMacros.nf"
33 # include "cgrammar.h"
36 # include "usymtab_interface.h"
39 # include "structNames.h"
40 # include "nameChecks.h"
43 # include "sgrammar_tokens.h"
45 # include "cgrammar_tokens.h"
49 ** Lots of variables are needed because of interactions with the
50 ** parser. This is easier than restructuring the grammar so the
51 ** right values are available in the right place.
54 static /*@only@*/ sRefSet fcnModifies = sRefSet_undefined;
55 static /*@only@*/ /*@null@*/ specialClauses specClauses = specialClauses_undefined;
56 static bool fcnNoGlobals = FALSE;
57 static bool ProcessingVars = FALSE;
58 static bool ProcessingParams = FALSE;
59 static bool ProcessingGlobals = FALSE;
60 static bool ProcessingTypedef = FALSE;
61 static bool ProcessingIterVars = FALSE;
62 static /*@only@*/ qtype processingType = qtype_undefined;
63 static uentry currentIter = uentry_undefined;
64 static globSet currentGlobals = globSet_undefined;
65 static /*@dependent@*/ uentryList saveParamList; /* for old style functions */
66 static /*@owned@*/ uentry saveFunction = uentry_undefined;
67 static int saveIterParamNo;
68 static idDecl fixStructDecl (/*@returned@*/ idDecl p_d);
69 static void checkTypeDecl (uentry p_e, ctype p_rep);
70 static /*@dependent@*/ fileloc saveStoreLoc = fileloc_undefined;
71 static storageClassCode storageClass = SCNONE;
72 static void declareEnumList (/*@temp@*/ enumNameList p_el, ctype p_c, fileloc p_loc);
73 static void resetGlobals (void);
74 static qual specialFunctionCode = QU_UNKNOWN;
75 static bool argsUsed = FALSE;
77 static bool hasSpecialCode (void)
79 return (specialFunctionCode != QU_UNKNOWN);
82 extern void setArgsUsed (void)
88 cstring_makeLiteral ("Multiple ARGSUSED comments for one function"),
95 static void reflectArgsUsed (uentry ue)
99 if (uentry_isFunction (ue))
101 uentryList params = uentry_getParams (ue);
103 uentryList_elements (params, el)
105 uentry_setUsed (el, fileloc_undefined);
106 } end_uentryList_elements ;
113 extern void setSpecialFunction (qual qu)
115 if (specialFunctionCode != QU_UNKNOWN)
117 voptgenerror (FLG_SYNTAX,
118 message ("Multiple special function codes: %s, %s "
119 "(first code is ignored)",
120 qual_unparse (specialFunctionCode),
125 specialFunctionCode = qu;
128 static void reflectSpecialCode (uentry ue)
130 switch (specialFunctionCode)
132 case QU_UNKNOWN: break;
134 uentry_setPrintfLike (ue);
137 uentry_setScanfLike (ue);
140 uentry_setMessageLike (ue);
145 specialFunctionCode = QU_UNKNOWN;
148 static void resetStorageClass (void)
150 qtype_free (processingType);
151 processingType = qtype_undefined;
152 storageClass = SCNONE;
155 static void reflectModGlobs (uentry ue)
159 llassert (globSet_isUndefined (currentGlobals));
161 uentry_setGlobals (ue, globSet_undefined);
162 fcnNoGlobals = FALSE;
164 else if (globSet_isDefined (currentGlobals))
166 uentry_setGlobals (ue, currentGlobals);
167 currentGlobals = globSet_undefined;
174 if (sRefSet_isDefined (fcnModifies))
176 uentry_setModifies (ue, fcnModifies);
177 fcnModifies = sRefSet_undefined;
180 if (uentry_isFunction (ue))
182 uentry_setSpecialClauses (ue, specClauses);
184 DPRINTF (("Done with spec clauses"));
188 static void reflectStorageClass (uentry u)
190 if (storageClass == SCSTATIC)
192 uentry_setStatic (u);
194 else if (storageClass == SCEXTERN)
196 uentry_setExtern (u);
200 ; /* no storage class */
207 saveStoreLoc = g_currentloc;
210 void setFunctionNoGlobals (void)
212 llassert (globSet_isUndefined (currentGlobals));
217 setFunctionStateSpecialClause (lltok stok, specialClauseKind kind,
219 /*@unused@*/ lltok etok)
221 int tok = lltok_getTok (stok);
226 specClauses = specialClauses_add (specClauses,
227 specialClause_create (TK_BEFORE, kind, s));
230 specClauses = specialClauses_add (specClauses,
231 specialClause_create (TK_AFTER, kind, s));
238 DPRINTF (("Added to specclauses: %s", specialClauses_unparse (specClauses)));
241 void setFunctionSpecialClause (lltok stok, sRefSet s,
242 /*@unused@*/ lltok etok)
244 int tok = lltok_getTok (stok);
249 specClauses = specialClauses_add (specClauses, specialClause_createUses (s));
252 specClauses = specialClauses_add (specClauses, specialClause_createDefines (s));
255 specClauses = specialClauses_add (specClauses, specialClause_createAllocates (s));
258 specClauses = specialClauses_add (specClauses, specialClause_createSets (s));
261 specClauses = specialClauses_add (specClauses, specialClause_createReleases (s));
268 DPRINTF (("Added to specclauses: %s", specialClauses_unparse (specClauses)));
271 void setFunctionModifies (sRefSet s)
273 sRefSet_free (fcnModifies);
277 static void reflectGlobalQualifiers (sRef sr, qualList quals)
279 qualList_elements (quals, qel)
281 if (qual_isGlobalQual (qel)) /* undef, killed */
283 sstate oldstate = sRef_getDefState (sr);
284 sstate defstate = sstate_fromQual (qel);
286 if ((oldstate == SS_UNDEFGLOB && defstate == SS_KILLED)
287 || (oldstate == SS_KILLED && defstate == SS_UNDEFGLOB))
289 defstate = SS_UNDEFKILLED;
296 sRef_setDefState (sr, defstate, fileloc_undefined);
298 else if (qual_isAllocQual (qel)) /* out, partial, reldef, etc. */
300 ctype realType = sRef_getType (sr);
301 sstate defstate = sstate_fromQual (qel);
303 if (qual_isRelDef (qel))
305 ; /* okay anywhere */
309 if (!ctype_isAP (realType)
310 && !ctype_isSU (realType)
311 && !ctype_isUnknown (realType)
312 && !ctype_isAbstract (sRef_getType (sr)))
316 message ("Qualifier %s used on non-pointer or struct: %q",
317 qual_unparse (qel), sRef_unparse (sr)));
322 sRef_setDefState (sr, defstate, fileloc_undefined);
324 else if (qual_isNull (qel))
326 sRef_setNullState (sr, NS_POSNULL, fileloc_undefined);
328 else if (qual_isRelNull (qel))
330 sRef_setNullState (sr, NS_RELNULL, fileloc_undefined);
332 else if (qual_isNotNull (qel))
334 sRef_setNullState (sr, NS_MNOTNULL, fileloc_undefined);
338 if (qual_isCQual (qel))
345 message ("Qualifier %s cannot be used in a globals list",
346 qual_unparse (qel)));
349 } end_qualList_elements;
352 void globListAdd (sRef sr, qualList quals)
354 if (sRef_isValid (sr))
356 sRef sc = sRef_copy (sr);
358 reflectGlobalQualifiers (sc, quals);
359 currentGlobals = globSet_insert (currentGlobals, sc);
363 extern void declareCIter (cstring name, /*@owned@*/ uentryList params)
367 ue = uentry_makeIter (name,
368 ctype_makeFunction (ctype_void, params),
369 fileloc_copy (g_currentloc));
371 usymtab_supEntry (uentry_makeEndIter (name, fileloc_copy (g_currentloc)));
373 reflectModGlobs (ue);
375 ue = usymtab_supGlobalEntryReturn (ue);
378 extern void nextIterParam (void)
380 llassert (ProcessingIterVars);
384 extern int iterParamNo (void)
386 llassert (ProcessingIterVars);
387 return saveIterParamNo;
391 ** yucky hacks to put it in the right place
395 makeCurrentParam (idDecl t)
399 saveStoreLoc = fileloc_undefined;
401 /* param number unknown */
403 ue = uentry_makeParam (t, 0);
408 declareUnnamedEnum (enumNameList el)
410 ctype ret = usymtab_enumEnumNameListType (el);
414 if (ctype_isDefined (ret))
417 e = uentry_makeEnumTagLoc (ctype_enumTag (rt), ret);
419 reflectStorageClass (e);
420 usymtab_supGlobalEntry (e);
422 declareEnumList (el, ret, g_currentloc);
423 enumNameList_free (el);
427 ctype ct = ctype_createEnum (fakeTag (), el);
429 e = uentry_makeEnumTagLoc (ctype_enumTag (ctype_realType (ct)), ct);
430 reflectStorageClass (e);
432 e = usymtab_supGlobalEntryReturn (e);
433 rt = uentry_getAbstractType (e);
434 declareEnumList (el, ct, g_currentloc);
441 declareEnum (cstring ename, enumNameList el)
446 llassert (cstring_isDefined (ename));
448 cet = ctype_createEnum (ename, el);
449 e = uentry_makeEnumTagLoc (ename, cet);
450 reflectStorageClass (e);
451 e = usymtab_supGlobalEntryReturn (e);
452 cet = uentry_getType (e);
453 declareEnumList (el, cet, uentry_whereLast (e));
454 return (uentry_getAbstractType (e));
458 declareEnumList (enumNameList el, ctype c, fileloc loc)
460 bool boolnames = FALSE;
461 bool othernames = FALSE;
463 (void) context_getSaveLocation (); /* undefine it */
465 if (context_maybeSet (FLG_NUMENUMMEMBERS))
467 int maxnum = context_getValue (FLG_NUMENUMMEMBERS);
468 int num = enumNameList_size (el);
474 message ("Enumerator %s declared with %d members (limit is set to %d)",
475 ctype_unparse (c), num, maxnum),
480 enumNameList_elements (el, e)
482 uentry ue = usymtab_lookupExposeGlob (e);
483 ctype ct = uentry_getType (ue);
485 llassert (uentry_isEnumConstant (ue));
487 if (ctype_isUnknown (ct))
489 uentry_setType (ue, c);
493 if (cstring_equal (e, context_getFalseName ())
494 || cstring_equal (e, context_getTrueName ()))
500 message ("Enumerator mixes boolean name (%s) with "
503 uentry_whereLast (ue)))
510 uentry_setType (ue, ctype_bool);
511 DPRINTF (("Set type: %s / %s",
512 uentry_unparse (ue), ctype_unparse (ctype_bool)));
520 message ("Enumerator mixes boolean names (%s, %s) with "
521 "non-boolean name: %s",
522 context_getTrueName (),
523 context_getFalseName (),
525 uentry_whereLast (ue)))
534 if (!ctype_match (c, ct))
536 if (ctype_isDirectBool (ct))
538 if (cstring_equal (e, context_getFalseName ())
539 || cstring_equal (e, context_getTrueName ()))
541 DPRINTF (("Here we are!"));
547 message ("Enumerator member %s declared with "
548 "inconsistent type: %s",
549 e, ctype_unparse (c)),
550 uentry_whereLast (ue)))
552 uentry_showWhereSpecifiedExtra
553 (ue, cstring_copy (ctype_unparse (ct)));
561 message ("Enumerator member %s declared with "
562 "inconsistent type: %s",
563 e, ctype_unparse (c)),
564 uentry_whereLast (ue)))
566 uentry_showWhereSpecifiedExtra
567 (ue, cstring_copy (ctype_unparse (ct)));
572 } end_enumNameList_elements;
575 static /*@dependent@*/ uentryList currentParamList;
577 void setCurrentParams (/*@dependent@*/ uentryList ue)
579 currentParamList = ue;
582 void clearCurrentParams (void)
584 currentParamList = uentryList_undefined;
588 ** requires: uentry_isFunction (e)
589 ** parameter names for current function are in currentParamList
592 static void enterFunctionParams (uentryList params)
596 uentryList_elements (params, current)
598 if (uentry_hasName (current))
600 uentry_setParamNo (current, paramno);
601 usymtab_supEntry (uentry_copy (current));
605 } end_uentryList_elements;
609 extern void enterParamsTemp (void)
611 usymtab_enterScope ();
612 enterFunctionParams (currentParamList);
615 extern void exitParamsTemp (void)
617 usymtab_quietPlainExitScope ();
620 static /*@exposed@*/ uentry globalDeclareFunction (idDecl tid)
622 ctype deftype = idDecl_getCtype (tid);
626 DPRINTF (("Global function: %s", idDecl_unparse (tid)));
628 if (ctype_isFunction (deftype))
630 rettype = ctype_returnValue (deftype);
634 rettype = ctype_unknown;
638 ** check has been moved here...
641 if (ctype_isFunction (idDecl_getCtype (tid)))
643 ue = uentry_makeIdFunction (tid);
644 reflectSpecialCode (ue);
645 reflectArgsUsed (ue);
649 llparseerror (message ("Inconsistent function declaration: %q",
650 idDecl_unparse (tid)));
652 tid = idDecl_replaceCtype
653 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
654 ue = uentry_makeIdFunction (tid);
657 reflectStorageClass (ue);
659 uentry_checkParams (ue);
660 reflectModGlobs (ue);
662 ue = usymtab_supGlobalEntryReturn (ue);
663 context_enterFunction (ue);
664 enterFunctionParams (uentry_getParams (ue));
666 resetStorageClass ();
671 ** for now, no type checking
672 ** (must check later though!)
675 static /*@only@*/ uentry globalDeclareOldStyleFunction (idDecl tid)
680 ** check has been moved here...
683 if (cstring_equalLit (idDecl_observeId (tid), "main"))
685 context_setFlagTemp (FLG_MAINTYPE, FALSE);
688 ue = uentry_makeIdFunction (tid);
689 reflectStorageClass (ue);
690 reflectSpecialCode (ue);
691 reflectArgsUsed (ue);
692 uentry_setDefined (ue, g_currentloc);
694 uentry_checkParams (ue);
696 if (ProcessingGlobals)
698 uentry_setGlobals (ue, currentGlobals);
701 resetStorageClass ();
705 static void oldStyleDeclareFunction (/*@only@*/ uentry e)
707 uentryList params = saveParamList;
708 ctype rt = uentry_getType (e);
710 llassert (ctype_isFunction (rt));
712 e = usymtab_supGlobalEntryReturn (e);
714 context_enterFunction (e);
715 enterFunctionParams (params);
716 saveParamList = uentryList_undefined;
717 resetStorageClass ();
720 void declareFunction (idDecl tid) /*@globals undef saveFunction; @*/
724 DPRINTF (("Declare function: %s", idDecl_unparse (tid)));
726 if (ProcessingParams)
728 ue = globalDeclareOldStyleFunction (tid);
733 saveFunction = uentry_undefined;
735 if (context_inRealFunction ())
737 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
739 llparseerror (message ("Function declared inside function: %q",
740 idDecl_unparse (tid)));
742 context_quietExitFunction ();
743 ue = usymtab_supEntryReturn (ue);
747 if (context_inInnerScope ())
749 llparseerror (message ("Declaration in inner context: %q",
750 idDecl_unparse (tid)));
752 sRef_setGlobalScope ();
753 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
755 ue = usymtab_supGlobalEntryReturn (ue);
756 sRef_clearGlobalScope ();
760 ue = globalDeclareFunction (tid);
767 resetStorageClass ();
771 void declareStaticFunction (idDecl tid) /*@globals undef saveFunction; @*/
775 DPRINTF (("Declare static funciton: %s", idDecl_unparse (tid)));
777 if (ProcessingParams)
779 ue = globalDeclareOldStyleFunction (tid);
784 saveFunction = uentry_undefined;
786 if (context_inRealFunction ())
788 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
790 llparseerror (message ("Function declared inside function: %q",
791 idDecl_unparse (tid)));
793 context_quietExitFunction ();
794 ue = usymtab_supEntryReturn (ue);
798 if (context_inInnerScope ())
800 llparseerror (message ("Declaration in inner context: %q",
801 idDecl_unparse (tid)));
803 sRef_setGlobalScope ();
804 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
806 ue = usymtab_supGlobalEntryReturn (ue);
807 sRef_clearGlobalScope ();
811 ctype deftype = idDecl_getCtype (tid);
814 if (ctype_isFunction (deftype))
816 rettype = ctype_returnValue (deftype);
820 rettype = ctype_unknown;
824 ** check has been moved here...
827 if (ctype_isFunction (idDecl_getCtype (tid)))
829 ue = uentry_makeIdFunction (tid);
830 reflectSpecialCode (ue);
831 reflectArgsUsed (ue);
835 llparseerror (message ("Inconsistent function declaration: %q",
836 idDecl_unparse (tid)));
838 tid = idDecl_replaceCtype
839 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
840 ue = uentry_makeIdFunction (tid);
843 reflectStorageClass (ue);
844 uentry_setStatic (ue);
846 uentry_checkParams (ue);
847 reflectModGlobs (ue);
849 DPRINTF (("Sub global entry: %s", uentry_unparse (ue)));
850 ue = usymtab_supGlobalEntryReturn (ue);
852 context_enterFunction (ue);
853 enterFunctionParams (uentry_getParams (ue));
854 resetStorageClass ();
861 resetStorageClass ();
866 checkTypeDecl (uentry e, ctype rep)
868 cstring n = uentry_getName (e);
870 DPRINTF (("Check type decl: %s", n));
872 if (cstring_equal (context_getBoolName (), n))
874 ctype rrep = ctype_realType (rep);
877 ** for abstract enum types, we need to fix the enum members:
878 ** they should have the abstract type, not the rep type.
881 if (ctype_isEnum (ctype_realType (rrep)))
883 enumNameList el = ctype_elist (rrep);
885 enumNameList_elements (el, ye)
887 if (usymtab_existsGlob (ye))
889 uentry ue = usymtab_lookupSafe (ye);
890 uentry_setType (ue, ctype_bool);
893 if (cstring_equal (context_getTrueName (), ye)
894 || cstring_equal (context_getFalseName (), ye))
902 message ("Member of boolean enumerated type definition "
903 "does not match name set to represent TRUE "
906 message ("Use -boolfalse and -booltrue to set the "
907 "name of false and true boolean values."),
908 uentry_whereDefined (e));
910 } end_enumNameList_elements;
914 if (usymtab_exists (n))
916 usymId llm = usymtab_getId (n);
917 uentry le = usymtab_getTypeEntry (llm);
919 uentry_setDeclared (e, g_currentloc);
920 uentry_setSref (e, sRef_makeGlobal (llm, uentry_getType (le)));
922 DPRINTF (("Here we are: %s / %s",
923 n, context_getBoolName ()));
925 if (uentry_isAbstractDatatype (le))
927 ctype rrep = ctype_realType (rep);
930 ** for abstract enum types, we need to fix the enum members:
931 ** they should have the abstract type, not the rep type.
934 if (ctype_isEnum (ctype_realType (rrep)))
936 ctype at = uentry_getAbstractType (le);
937 enumNameList el = ctype_elist (rrep);
939 enumNameList_elements (el, ye)
941 if (usymtab_existsGlob (ye))
943 uentry ue = usymtab_lookupSafe (ye);
945 llassert (uentry_isEitherConstant (ue));
946 llassertprint (ctype_match (uentry_getType (ue), rrep),
947 ("Bad enum: %s / %s",
949 ctype_unparse (rrep)));
951 uentry_setType (ue, at);
953 } end_enumNameList_elements;
956 if (uentry_isMutableDatatype (le))
958 /* maybe more complicated if abstract and immutable ? */
960 if (!ctype_isRealPointer (rep) && !ctype_isRealAbstract (rep))
964 message ("Mutable abstract type %s declared without pointer "
965 "indirection: %s (violates assignment semantics)",
966 n, ctype_unparse (rep)),
967 uentry_whereDefined (e));
969 uentry_setMutable (e);
976 fileloc fl = uentry_whereDeclared (e);
978 if (context_getFlag (FLG_LIKELYBOOL)
979 && !context_getFlag (FLG_BOOLINT))
981 if ((cstring_equalLit (n, "BOOL")
982 || cstring_equalLit (n, "Bool")
983 || cstring_equalLit (n, "bool")
984 || cstring_equalLit (n, "boolean")
985 || cstring_equalLit (n, "Boolean")
986 || cstring_equalLit (n, "BOOLEAN"))
987 && !(cstring_equal (n, context_getBoolName ())))
989 if (context_setBoolName ()) {
992 message ("Type %s is probably meant as a boolean type, but does "
993 "not match the boolean type name \"%s\".",
995 context_getBoolName ()),
1000 message ("Type %s is probably meant as a boolean type, "
1001 "but the boolean type name is not set. "
1002 "Use -booltype %s to set it.",
1009 if (!uentry_isStatic (e)
1010 && !ctype_isFunction (uentry_getType (e))
1011 && !fileloc_isLib (fl)
1012 && !fileloc_isImport (fl)
1013 && fileloc_isHeader (fl))
1015 voptgenerror (FLG_EXPORTTYPE,
1016 message ("Type exported, but not specified: %s\n", n),
1025 fixUentryList (idDeclList tl, qtype q)
1027 uentryList f = uentryList_new ();
1029 idDeclList_elements (tl, i)
1031 if (idDecl_isDefined (i))
1037 (void) idDecl_fixBase (i, q);
1040 ** implicit annotations
1043 (void) fixStructDecl (i);
1045 ue = uentry_makeIdVariable (i);
1046 rt = ctype_realType (uentry_getType (ue));
1049 ** where is this here???
1051 if (ctype_isArray (rt) || ctype_isSU (rt))
1053 sRef_setAllocated (uentry_getSref (ue), uentry_whereDefined (ue));
1059 if (uentry_isValid (old = uentryList_lookupField (f, uentry_rawName (ue))))
1061 if (optgenerror (FLG_SYNTAX,
1062 message ("Field name reused: %s", uentry_rawName (ue)),
1063 uentry_whereDefined (ue)))
1065 llgenmsg (message ("Previous use of %s", uentry_rawName (ue)),
1066 uentry_whereDefined (old));
1070 f = uentryList_add (f, ue);
1072 } end_idDeclList_elements;
1074 idDeclList_free (tl);
1079 ** This is a hack to support unnamed struct/union fields as done by
1080 ** Microsoft VC++. It is not supported by the ANSI standard.
1082 ** The inner fields are added to the outer structure. This is meaningful
1083 ** for nesting structs inside unions, but lclint does no related
1088 fixUnnamedDecl (qtype q)
1090 ctype ct = ctype_realType (qtype_getType (q));
1092 if (ctype_isStruct (ct) || ctype_isUnion (ct))
1094 uentryList res = ctype_getFields (ct);
1096 return (uentryList_copy (res));
1103 return uentryList_undefined;
1106 void setStorageClass (storageClassCode sc)
1112 setProcessingIterVars (uentry iter)
1114 ProcessingIterVars = TRUE;
1116 saveIterParamNo = 0;
1120 setProcessingGlobalsList ()
1122 ProcessingGlobals = TRUE;
1124 llassert (globSet_isUndefined (currentGlobals));
1125 currentGlobals = globSet_undefined;
1127 llassert (sRefSet_isUndefined (fcnModifies));
1128 fcnModifies = sRefSet_undefined;
1131 ** No, special clauses might have been processed first!
1132 llassert (specialClauses_isUndefined (specClauses));
1133 specClauses = specialClauses_undefined;
1136 fcnNoGlobals = FALSE;
1139 static bool ProcessingGlobMods = FALSE;
1142 setProcessingGlobMods ()
1144 ProcessingGlobMods = TRUE;
1148 clearProcessingGlobMods ()
1150 ProcessingGlobMods = FALSE;
1154 isProcessingGlobMods ()
1156 return (ProcessingGlobMods);
1159 static void resetGlobals (void)
1161 ProcessingGlobals = FALSE;
1162 currentGlobals = globSet_undefined;
1163 llassert (sRefSet_isUndefined (fcnModifies));
1164 fcnModifies = sRefSet_undefined;
1165 fcnNoGlobals = FALSE;
1169 unsetProcessingGlobals ()
1171 ProcessingGlobals = FALSE;
1175 setProcessingVars (/*@only@*/ qtype q)
1177 ProcessingVars = TRUE;
1178 qtype_free (processingType);
1183 setGenericParamList (/*@dependent@*/ uentryList pm)
1185 ProcessingParams = TRUE;
1190 setProcessingTypedef (/*@only@*/ qtype q)
1192 ProcessingTypedef = TRUE;
1194 qtype_free (processingType);
1199 unsetProcessingVars ()
1201 resetStorageClass ();
1202 ProcessingVars = FALSE;
1208 if (ProcessingParams)
1210 if (uentry_isInvalid (saveFunction))
1212 llbuglit ("unsetProcessingVars: no saved function\n");
1214 if (sRefSet_isDefined (fcnModifies)) {
1215 sRefSet_free (fcnModifies);
1216 fcnModifies = sRefSet_undefined;
1221 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1222 uentryList params = uentryList_copy (saveParamList);
1223 ctype ct2 = ctype_makeFunction (ct, params);
1225 uentry_setType (saveFunction, ct2);
1226 ProcessingParams = FALSE;
1228 reflectModGlobs (saveFunction);
1229 oldStyleDeclareFunction (saveFunction);
1230 saveFunction = uentry_undefined;
1237 ** If the paramlist used a type name, we could be here.
1240 llfatalerror (message ("%q: Old-style function parameter list uses a "
1241 "type name.", fileloc_unparse (g_currentloc)));
1248 if (uentry_isValid (saveFunction))
1251 ** old style declaration
1254 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1257 uentryList_elements (saveParamList, current)
1259 uentry_setType (current, ctype_int); /* all params are ints */
1260 } end_uentryList_elements;
1262 ct2 = ctype_makeParamsFunction (ct, uentryList_copy (saveParamList));
1264 uentry_setType (saveFunction, ct2);
1265 ProcessingParams = FALSE;
1267 oldStyleDeclareFunction (saveFunction);
1268 saveFunction = uentry_undefined;
1273 unsetProcessingTypedef ()
1275 ProcessingTypedef = FALSE;
1278 void checkConstant (qtype t, idDecl id)
1282 id = idDecl_fixBase (id, t);
1283 e = uentry_makeIdConstant (id);
1285 reflectStorageClass (e);
1286 resetStorageClass ();
1288 usymtab_supGlobalEntry (e);
1291 void checkValueConstant (qtype t, idDecl id, exprNode e)
1295 id = idDecl_fixBase (id, t);
1296 ue = uentry_makeIdConstant (id);
1297 reflectStorageClass (ue);
1298 resetStorageClass ();
1300 if (exprNode_isDefined (e))
1302 if (!exprNode_matchType (uentry_getType (ue), e))
1305 (exprNode_getType (e), e,
1306 uentry_getType (ue), exprNode_undefined,
1307 message ("Constant %q initialized to type %t, expects %t: %s",
1308 uentry_getName (ue),
1309 exprNode_getType (e),
1310 uentry_getType (ue),
1311 exprNode_unparse (e)),
1316 if (exprNode_hasValue (e))
1318 uentry_mergeConstantValue (ue, multiVal_copy (exprNode_getValue (e)));
1323 usymtab_supGlobalEntry (ue);
1327 void processNamedDecl (idDecl t)
1329 if (qtype_isUndefined (processingType))
1331 llparseerror (message ("No type before declaration name: %q", idDecl_unparse (t)));
1333 processingType = qtype_create (ctype_unknown);
1336 t = idDecl_fixBase (t, processingType);
1338 DPRINTF (("Declare: %s", idDecl_unparse (t)));
1340 if (ProcessingGlobals)
1342 cstring id = idDecl_getName (t);
1343 uentry ue = usymtab_lookupSafe (id);
1345 if (!uentry_isValid (ue))
1347 llerror (FLG_UNRECOG,
1348 message ("Variable used in globals list is undeclared: %s", id));
1352 if (!ctype_match (uentry_getType (ue), idDecl_getCtype (t)))
1356 message ("Variable %s used in globals list declared %s, "
1358 id, ctype_unparse (uentry_getType (ue)),
1359 ctype_unparse (idDecl_getCtype (t))),
1364 sRef sr = sRef_copy (uentry_getSref (ue));
1366 reflectGlobalQualifiers (sr, idDecl_getQuals (t));
1368 currentGlobals = globSet_insert (currentGlobals, sr);
1372 else if (ProcessingVars)
1377 ct = ctype_realType (idDecl_getCtype (t));
1379 if (ProcessingParams)
1381 cstring id = idDecl_getName (t);
1382 int paramno = uentryList_lookupRealName (saveParamList, id);
1386 uentry cparam = uentryList_getN (saveParamList, paramno);
1388 uentry_setType (cparam, idDecl_getCtype (t));
1389 uentry_reflectQualifiers (cparam, idDecl_getQuals (t));
1390 uentry_setDeclaredOnly (cparam, context_getSaveLocation ());
1395 (message ("Old style declaration uses unlisted parameter: %s",
1403 if (context_inIterDef ())
1405 cstring pname = makeParam (idDecl_observeId (t));
1406 uentry p = usymtab_lookupSafe (pname);
1408 cstring_free (pname);
1410 if (uentry_isYield (p))
1412 e = uentry_makeParam (t, sRef_getParam (uentry_getSref (p)));
1414 uentry_checkYieldParam (p, e);
1416 usymtab_supEntrySref (e);
1421 if ((hasSpecialCode () || argsUsed)
1422 && ctype_isFunction (idDecl_getCtype (t)))
1424 e = uentry_makeIdFunction (t);
1425 reflectSpecialCode (e);
1426 reflectArgsUsed (e);
1430 e = uentry_makeIdVariable (t);
1433 loc = uentry_whereDeclared (e);
1436 if (context_inGlobalScope ())
1438 uentry_checkParams was here!
1442 if (ctype_isFunction (uentry_getType (e)))
1444 reflectModGlobs (e);
1448 llassert (!globSet_isDefined (currentGlobals)
1449 && !sRefSet_isDefined (fcnModifies));
1452 e = usymtab_supEntrySrefReturn (e);
1454 if (uentry_isExtern (e) && !context_inGlobalScope ())
1458 message ("Declaration using extern inside function scope: %q",
1459 uentry_unparse (e)),
1462 uentry_setDefined (e, fileloc_getExternal ());
1463 sRef_setDefined (uentry_getSref (e), fileloc_getExternal ());
1466 if (uentry_isFunction (e))
1468 uentry_checkParams (e);
1469 checkParamNames (e);
1472 if (uentry_isVar (e)
1473 && uentry_isCheckedUnknown (e))
1475 sRef sr = uentry_getSref (e);
1477 if (sRef_isLocalVar (sr))
1479 if (context_getFlag (FLG_IMPCHECKMODINTERNALS))
1481 uentry_setCheckMod (e);
1485 uentry_setUnchecked (e);
1488 else if (sRef_isFileStatic (sr))
1490 if (context_getFlag (FLG_IMPCHECKEDSTRICTSTATICS))
1492 uentry_setCheckedStrict (e);
1494 else if (context_getFlag (FLG_IMPCHECKEDSTATICS))
1496 uentry_setChecked (e);
1498 else if (context_getFlag (FLG_IMPCHECKMODSTATICS))
1500 uentry_setCheckMod (e);
1507 else /* real global */
1509 llassert (sRef_isRealGlobal (sr));
1511 if (context_getFlag (FLG_IMPCHECKEDSTRICTGLOBALS))
1513 uentry_setCheckedStrict (e);
1515 else if (context_getFlag (FLG_IMPCHECKEDGLOBALS))
1517 uentry_setChecked (e);
1519 else if (context_getFlag (FLG_IMPCHECKMODGLOBALS))
1521 uentry_setCheckMod (e);
1531 else if (ProcessingTypedef)
1533 ctype ct = idDecl_getCtype (t);
1536 DPRINTF (("Processing typedef: %s", ctype_unparse (ct)));
1538 e = uentry_makeIdDatatype (t);
1540 if (cstring_equal (idDecl_getName (t), context_getBoolName ())) {
1541 ctype rt = ctype_realType (ct);
1543 if (ctype_isEnum (rt)) {
1546 if (!(ctype_isInt (rt)
1547 || ctype_isUnknown (rt)
1548 || ctype_isChar (rt))) {
1551 message ("Boolean type %s defined using non-standard type %s (integral, char or enum type expected)",
1552 context_getBoolName (),
1553 ctype_unparse (ct)),
1554 uentry_whereLast (e));
1558 uentry_setType (e, ct);
1562 reflectStorageClass (e);
1563 checkTypeDecl (e, ct);
1565 e = usymtab_supReturnTypeEntry (e);
1567 if (uentry_isMaybeAbstract (e))
1569 if (context_getFlag (FLG_IMPABSTRACT))
1571 uentry_setAbstract (e);
1575 uentry_setConcrete (e);
1581 llparseerror (message ("Suspect missing struct or union keyword: %q",
1582 idDecl_unparse (t)));
1588 ** moved from grammar
1591 static idDecl fixStructDecl (/*@returned@*/ idDecl d)
1593 if (ctype_isVisiblySharable (idDecl_getCtype (d))
1594 && context_getFlag (FLG_STRUCTIMPONLY))
1596 if (!qualList_hasAliasQualifier (idDecl_getQuals (d)))
1598 if (qualList_hasExposureQualifier (idDecl_getQuals (d)))
1600 idDecl_addQual (d, qual_createDependent ());
1604 idDecl_addQual (d, qual_createImpOnly ());
1613 declareUnnamedStruct (/*@only@*/ uentryList f)
1615 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1617 int num = uentryList_size (f);
1618 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1623 (FLG_NUMSTRUCTFIELDS,
1624 message ("Structure declared with %d fields "
1625 "(limit is set to %d)",
1631 return (ctype_createUnnamedStruct (f));
1635 declareUnnamedUnion (/*@only@*/ uentryList f)
1637 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1639 int num = uentryList_size (f);
1640 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1645 (FLG_NUMSTRUCTFIELDS,
1646 message ("Union declared with %d fields "
1647 "(limit is set to %d)",
1653 return (ctype_createUnnamedUnion (f));
1656 ctype declareStruct (cstring id, /*@only@*/ uentryList f)
1660 int num = uentryList_size (f);
1662 ct = ctype_createStruct (cstring_copy (id), f);
1663 DPRINTF (("Declare struct: %s [%d]", ctype_unparse (ct), ct));
1664 ue = uentry_makeStructTagLoc (id, ct);
1666 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1668 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1673 (FLG_NUMSTRUCTFIELDS,
1674 message ("Structure %q declared with %d fields "
1675 "(limit is set to %d)",
1676 uentry_getName (ue), num, max),
1677 uentry_whereLast (ue));
1681 return (usymtab_supTypeEntry (ue));
1684 ctype declareUnion (cstring id, uentryList f)
1688 int num = uentryList_size (f);
1690 ct = ctype_createUnion (cstring_copy (id), f);
1691 ue = uentry_makeUnionTagLoc (id, ct);
1693 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1695 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1700 (FLG_NUMSTRUCTFIELDS,
1701 message ("Union %q declared with %d fields "
1702 "(limit is set to %d)",
1703 uentry_getName (ue), num, max),
1704 uentry_whereLast (ue));
1708 return (usymtab_supTypeEntry (ue));
1711 ctype handleStruct (/*@only@*/ cstring id)
1713 if (usymtab_existsStructTag (id))
1715 ctype ct = uentry_getAbstractType (usymtab_lookupStructTag (id));
1722 return (ctype_createForwardStruct (id));
1726 ctype handleUnion (/*@only@*/ cstring id)
1728 if (usymtab_existsUnionTag (id))
1730 ctype ret = uentry_getAbstractType (usymtab_lookupUnionTag (id));
1736 return (ctype_createForwardUnion (id));
1741 handleEnum (cstring id)
1743 if (usymtab_existsEnumTag (id))
1745 ctype ret = uentry_getAbstractType (usymtab_lookupEnumTag (id));
1751 return (declareEnum (id, enumNameList_new ()));
1755 bool processingIterVars (void)
1757 return ProcessingIterVars;
1760 uentry getCurrentIter (void)
1765 static bool flipOldStyle = FALSE;
1766 static bool flipNewStyle = TRUE;
1768 void setFlipOldStyle () { flipOldStyle = TRUE; }
1769 bool isFlipOldStyle () { return flipOldStyle; }
1770 bool isNewStyle () { return flipNewStyle; }
1771 void setNewStyle () { flipNewStyle = TRUE; }
1773 /*@dependent@*/ uentryList handleParamIdList (/*@dependent@*/ uentryList params)
1778 ** this is a really YUCKY hack to handle old style
1782 voptgenerror (FLG_OLDSTYLE,
1783 cstring_makeLiteral ("Old style function declaration"),
1786 uentryList_elements (params, current)
1788 uentry_setParam (current);
1789 uentry_setSref (current, sRef_makeParam (paramno, ctype_unknown));
1791 } end_uentryList_elements;
1793 setGenericParamList (params);
1794 g_expectingTypeName = TRUE;
1799 /*@dependent@*/ uentryList handleParamTypeList (/*@returned@*/ uentryList params)
1803 uentryList_fixMissingNames (params);
1805 voptgenerror (FLG_OLDSTYLE,
1806 cstring_makeLiteral ("Old style function declaration."),
1809 setGenericParamList (params);
1810 flipOldStyle = FALSE;
1811 g_expectingTypeName = TRUE;
1820 ctype c = ctype_unknown;
1821 cstring id = cstring_makeLiteral ("va_alist");
1824 if (ProcessingParams)
1826 int i = uentryList_lookupRealName (saveParamList, id);
1830 e = uentry_makeVariableSrefParam (id, c, sRef_makeParam (i, c));
1834 e = uentry_undefined; /* suppress gcc message */
1835 llfatalerrorLoc (cstring_makeLiteral ("va_dcl used without va_alist"));
1840 llerror (FLG_SYNTAX, cstring_makeLiteral ("va_dcl used outside of function declaration"));
1841 e = uentry_makeVariableLoc (id, c);
1845 uentry_setUsed (e, g_currentloc);
1846 usymtab_supEntrySref (e);
1849 /*@exposed@*/ sRef modListPointer (sRef s)
1851 ctype ct = sRef_getType (s);
1852 ctype rt = ctype_realType (ct);
1854 if (ctype_isAP (rt))
1856 if (context_inHeader () && ctype_isAbstract (ct))
1861 ("Modifies clause in header file dereferences abstract "
1862 "type %s (interface modifies clause should not depend "
1863 "on or expose type representation): %q",
1869 return (sRef_constructPointer (s));
1873 if (ctype_isKnown (rt))
1877 message ("Implementation modifies clause dereferences non-pointer (type %s): %q",
1887 /*@exposed@*/ sRef modListFieldAccess (sRef s, cstring f)
1889 ctype ct = sRef_getType (s);
1890 ctype rt = ctype_realType (ct);
1892 if (ctype_isStructorUnion (rt))
1894 uentry tf = uentryList_lookupField (ctype_getFields (rt), f);
1896 if (uentry_isUndefined (tf))
1898 voptgenerror (FLG_TYPE,
1899 message ("Modifies list accesses non-existent "
1900 "field %s of %t: %q", f, ct,
1905 return sRef_undefined;
1909 if (ctype_isAbstract (ct) && context_inHeader ())
1914 ("Modifies clause in header file accesses abstract "
1915 "type %s (interface modifies clause should not depend "
1916 "on or expose type representation): %q",
1923 cstring_markOwned (f);
1924 return (sRef_makeField (s, f));
1930 message ("Modifies clause dereferences non-pointer (type %s): %q",
1940 sRef globListUnrecognized (cstring s)
1942 if (cstring_equalLit (s, "nothing"))
1944 return sRef_makeNothing ();
1946 else if (cstring_equalLit (s, "internalState"))
1948 return sRef_makeInternalState ();
1950 else if (cstring_equalLit (s, "fileSystem")
1951 || cstring_equalLit (s, "systemState"))
1953 return sRef_makeSystemState ();
1959 message ("Unrecognized identifier in globals list: %s", s),
1962 return sRef_undefined;
1966 /*@exposed@*/ sRef modListArrowAccess (sRef s, cstring f)
1968 ctype ct = sRef_getType (s);
1969 ctype rt = ctype_realType (ct);
1971 if (ctype_isRealPointer (rt))
1973 ctype b = ctype_baseArrayPtr (rt);
1974 ctype rb = ctype_realType (b);
1976 if (ctype_isStructorUnion (rb))
1978 uentry tf = uentryList_lookupField (ctype_getFields (rb), f);
1980 if (uentry_isUndefined (tf))
1982 voptgenerror (FLG_TYPE,
1983 message ("Modifies list arrow accesses non-existent "
1984 "field %s of %t: %q", f, b,
1989 return sRef_undefined;
1993 if (context_inHeader ())
1995 if (ctype_isAbstract (b))
2000 ("Modifies clause in header file arrow accesses abstract "
2001 "type %s (interface modifies clause should not depend "
2002 "on or expose type representation): %q",
2010 if (ctype_isAbstract (ct))
2015 ("Modifies clause in header file arrow accesses abstract "
2016 "type %s (interface modifies clause should not depend "
2017 "on or expose type representation): %q",
2025 cstring_markOwned (f);
2026 return (sRef_makeArrow (s, f));
2032 message ("Modifies clause arrow accesses pointer to "
2033 "non-structure (type %s): %q",
2043 message ("Modifies clause arrow accesses non-pointer (type %s): %q",
2053 sRef checkSpecClausesId (uentry ue)
2055 cstring s = uentry_rawName (ue);
2057 if (sRef_isGlobal (uentry_getSref (ue)))
2061 message ("Global variable %s used special clause. (Global variables "
2062 "are not recognized in special clauses. If there is "
2063 "sufficient interest in support for this, it may be "
2064 "added to a future release. Send mail to "
2065 "lclint@cs.virginia.edu.)",
2069 return sRef_undefined;
2073 if (cstring_equalLit (s, "result"))
2077 message ("Special clause list uses %s which is a variable and has special "
2078 "meaning in a modifies list. (Special meaning assumed.)", s),
2081 uentry_showWhereDeclared (ue);
2085 return uentry_getSref (ue);
2090 void checkModifiesId (uentry ue)
2092 cstring s = uentry_rawName (ue);
2094 if (cstring_equalLit (s, "nothing")
2095 || cstring_equalLit (s, "internalState")
2096 || cstring_equalLit (s, "systemState")
2097 || (cstring_equalLit (s, "fileSystem")))
2101 message ("Modifies list uses %s which is a variable and has special "
2102 "meaning in a modifies list. (Special meaning assumed.)", s),
2105 uentry_showWhereDeclared (ue);
2110 /*@exposed@*/ sRef fixModifiesId (cstring s)
2113 cstring pname = makeParam (s);
2114 uentry ue = usymtab_lookupSafe (pname);
2116 cstring_free (pname);
2118 if (cstring_equalLit (s, "nothing"))
2120 ret = sRef_makeNothing ();
2122 else if (cstring_equalLit (s, "internalState"))
2124 ret = sRef_makeInternalState ();
2126 else if (cstring_equalLit (s, "fileSystem")
2127 || cstring_equalLit (s, "systemState"))
2129 ret = sRef_makeSystemState ();
2133 ret = sRef_undefined;
2136 if (sRef_isValid (ret))
2138 if (uentry_isValid (ue))
2142 message ("Modifies list uses %s which is a parameter and has special "
2143 "meaning in a modifies list. (Special meaning assumed.)", s),
2149 if (uentry_isValid (ue))
2151 ret = uentry_getSref (ue);
2155 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2156 ret = sRef_undefined;
2160 message ("Unrecognized identifier in modifies comment: %s", s),
2170 sRef fixSpecClausesId (cstring s)
2173 cstring pname = makeParam (s);
2174 uentry ue = usymtab_lookupSafe (pname);
2176 cstring_free (pname);
2178 if (cstring_equalLit (s, "result"))
2180 ret = sRef_makeResult ();
2184 ret = sRef_undefined;
2187 if (sRef_isValid (ret))
2189 if (uentry_isValid (ue))
2193 message ("Special clause uses %s which is a parameter and has special "
2194 "meaning in a special clause. (Special meaning assumed.)", s),
2200 if (uentry_isValid (ue))
2202 ret = uentry_getSref (ue);
2204 if (sRef_isGlobal (ret))
2208 message ("Global variable %s used special clause. (Global variables "
2209 "are not recognized in special clauses. If there is "
2210 "sufficient interest in support for this, it may be "
2211 "added to a future release. Send mail to "
2212 "lclint@cs.virginia.edu.)",
2216 ret = sRef_undefined;
2221 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2222 ret = sRef_undefined;
2226 message ("Unrecognized identifier in special clause: %s", s),
2236 sRef modListArrayFetch (sRef s, /*@unused@*/ sRef mexp)
2238 ctype ct = sRef_getType (s);
2239 ctype rt = ctype_realType (ct);
2241 if (ctype_isAP (rt))
2243 if (context_inHeader () && ctype_isAbstract (ct))
2248 ("Modifies clause in header file indexes abstract "
2249 "type %s (interface modifies clause should not depend "
2250 "on or expose type representation): %q",
2256 return (sRef_makeAnyArrayFetch (s));
2263 ("Implementation modifies clause uses array fetch on non-array (type %s): %q",
2264 ctype_unparse (ct), sRef_unparse (s)),