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.
55 static constraintList fcnConstraints = NULL;
57 static constraintList fcnEnsuresConstraints = NULL;
60 //static constraintList fcnPreConditions = NULL;
63 static /*@only@*/ sRefSet fcnModifies = sRefSet_undefined;
64 static /*@only@*/ /*@null@*/ specialClauses specClauses = specialClauses_undefined;
65 static bool fcnNoGlobals = FALSE;
66 static bool ProcessingVars = FALSE;
67 static bool ProcessingParams = FALSE;
68 static bool ProcessingGlobals = FALSE;
69 static bool ProcessingTypedef = FALSE;
70 static bool ProcessingIterVars = FALSE;
71 static /*@only@*/ qtype processingType = qtype_undefined;
72 static uentry currentIter = uentry_undefined;
73 static globSet currentGlobals = globSet_undefined;
74 static /*@dependent@*/ uentryList saveParamList; /* for old style functions */
75 static /*@owned@*/ uentry saveFunction = uentry_undefined;
76 static int saveIterParamNo;
77 static idDecl fixStructDecl (/*@returned@*/ idDecl p_d);
78 static void checkTypeDecl (uentry p_e, ctype p_rep);
79 static /*@dependent@*/ fileloc saveStoreLoc = fileloc_undefined;
80 static storageClassCode storageClass = SCNONE;
81 static void declareEnumList (/*@temp@*/ enumNameList p_el, ctype p_c, fileloc p_loc);
82 static void resetGlobals (void);
83 static qual specialFunctionCode = QU_UNKNOWN;
84 static bool argsUsed = FALSE;
86 static bool hasSpecialCode (void)
88 return (specialFunctionCode != QU_UNKNOWN);
91 extern void setArgsUsed (void)
97 cstring_makeLiteral ("Multiple ARGSUSED comments for one function"),
104 static void reflectArgsUsed (uentry ue)
108 if (uentry_isFunction (ue))
110 uentryList params = uentry_getParams (ue);
112 uentryList_elements (params, el)
114 uentry_setUsed (el, fileloc_undefined);
115 } end_uentryList_elements ;
122 extern void setSpecialFunction (qual qu)
124 if (specialFunctionCode != QU_UNKNOWN)
126 voptgenerror (FLG_SYNTAX,
127 message ("Multiple special function codes: %s, %s "
128 "(first code is ignored)",
129 qual_unparse (specialFunctionCode),
134 specialFunctionCode = qu;
137 static void reflectSpecialCode (uentry ue)
139 switch (specialFunctionCode)
141 case QU_UNKNOWN: break;
143 uentry_setPrintfLike (ue);
146 uentry_setScanfLike (ue);
149 uentry_setMessageLike (ue);
154 specialFunctionCode = QU_UNKNOWN;
157 static void resetStorageClass (void)
159 qtype_free (processingType);
160 processingType = qtype_undefined;
161 storageClass = SCNONE;
164 static void reflectModGlobs (uentry ue)
168 llassert (globSet_isUndefined (currentGlobals));
170 uentry_setGlobals (ue, globSet_undefined);
171 fcnNoGlobals = FALSE;
173 else if (globSet_isDefined (currentGlobals))
175 uentry_setGlobals (ue, currentGlobals);
176 currentGlobals = globSet_undefined;
183 if (sRefSet_isDefined (fcnModifies))
185 uentry_setModifies (ue, fcnModifies);
186 fcnModifies = sRefSet_undefined;
191 uentry_setPreconditions (ue, fcnConstraints);
192 fcnConstraints = constraintList_undefined;
195 if (fcnEnsuresConstraints)
197 uentry_setPostconditions (ue, fcnEnsuresConstraints);
198 fcnEnsuresConstraints = constraintList_undefined;
202 if (uentry_isFunction (ue))
204 uentry_setSpecialClauses (ue, specClauses);
206 DPRINTF (("Done with spec clauses"));
210 static void reflectStorageClass (uentry u)
212 if (storageClass == SCSTATIC)
214 uentry_setStatic (u);
216 else if (storageClass == SCEXTERN)
218 uentry_setExtern (u);
222 ; /* no storage class */
229 saveStoreLoc = g_currentloc;
232 void setFunctionNoGlobals (void)
234 llassert (globSet_isUndefined (currentGlobals));
239 setFunctionStateSpecialClause (lltok stok, specialClauseKind kind,
241 /*@unused@*/ lltok etok)
243 int tok = lltok_getTok (stok);
248 specClauses = specialClauses_add (specClauses,
249 specialClause_create (TK_BEFORE, kind, s));
252 specClauses = specialClauses_add (specClauses,
253 specialClause_create (TK_AFTER, kind, s));
260 DPRINTF (("Added to specclauses: %s", specialClauses_unparse (specClauses)));
263 void setFunctionSpecialClause (lltok stok, sRefSet s,
264 /*@unused@*/ lltok etok)
266 int tok = lltok_getTok (stok);
271 specClauses = specialClauses_add (specClauses, specialClause_createUses (s));
274 specClauses = specialClauses_add (specClauses, specialClause_createDefines (s));
277 specClauses = specialClauses_add (specClauses, specialClause_createAllocates (s));
280 specClauses = specialClauses_add (specClauses, specialClause_createSets (s));
283 specClauses = specialClauses_add (specClauses, specialClause_createReleases (s));
290 DPRINTF (("Added to specclauses: %s", specialClauses_unparse (specClauses)));
295 constraintList getFunctionConstraints (void)
297 return constraintList_copy (fcnConstraints);
301 constraintList getEnsuresConstraints (void)
303 return constraintList_copy (fcnEnsuresConstraints);
306 void setEnsuresConstraints (constraintList c)
309 fcnEnsuresConstraints = constraintList_copy (c);
312 void setFunctionConstraints (constraintList c)
315 fcnConstraints = constraintList_copy (c);
319 void setFunctionModifies (sRefSet s)
321 sRefSet_free (fcnModifies);
325 static void reflectGlobalQualifiers (sRef sr, qualList quals)
327 qualList_elements (quals, qel)
329 if (qual_isGlobalQual (qel)) /* undef, killed */
331 sstate oldstate = sRef_getDefState (sr);
332 sstate defstate = sstate_fromQual (qel);
334 if ((oldstate == SS_UNDEFGLOB && defstate == SS_KILLED)
335 || (oldstate == SS_KILLED && defstate == SS_UNDEFGLOB))
337 defstate = SS_UNDEFKILLED;
344 sRef_setDefState (sr, defstate, fileloc_undefined);
346 else if (qual_isAllocQual (qel)) /* out, partial, reldef, etc. */
348 ctype realType = sRef_getType (sr);
349 sstate defstate = sstate_fromQual (qel);
351 if (qual_isRelDef (qel))
353 ; /* okay anywhere */
357 if (!ctype_isAP (realType)
358 && !ctype_isSU (realType)
359 && !ctype_isUnknown (realType)
360 && !ctype_isAbstract (sRef_getType (sr)))
364 message ("Qualifier %s used on non-pointer or struct: %q",
365 qual_unparse (qel), sRef_unparse (sr)));
370 sRef_setDefState (sr, defstate, fileloc_undefined);
372 else if (qual_isNull (qel))
374 sRef_setNullState (sr, NS_POSNULL, fileloc_undefined);
376 else if (qual_isRelNull (qel))
378 sRef_setNullState (sr, NS_RELNULL, fileloc_undefined);
380 else if (qual_isNotNull (qel))
382 sRef_setNullState (sr, NS_MNOTNULL, fileloc_undefined);
386 if (qual_isCQual (qel))
393 message ("Qualifier %s cannot be used in a globals list",
394 qual_unparse (qel)));
397 } end_qualList_elements;
400 void globListAdd (sRef sr, qualList quals)
402 if (sRef_isValid (sr))
404 sRef sc = sRef_copy (sr);
406 reflectGlobalQualifiers (sc, quals);
407 currentGlobals = globSet_insert (currentGlobals, sc);
411 extern void declareCIter (cstring name, /*@owned@*/ uentryList params)
415 ue = uentry_makeIter (name,
416 ctype_makeFunction (ctype_void, params),
417 fileloc_copy (g_currentloc));
419 usymtab_supEntry (uentry_makeEndIter (name, fileloc_copy (g_currentloc)));
421 reflectModGlobs (ue);
423 ue = usymtab_supGlobalEntryReturn (ue);
426 extern void nextIterParam (void)
428 llassert (ProcessingIterVars);
432 extern int iterParamNo (void)
434 llassert (ProcessingIterVars);
435 return saveIterParamNo;
439 ** yucky hacks to put it in the right place
443 makeCurrentParam (idDecl t)
447 saveStoreLoc = fileloc_undefined;
449 /* param number unknown */
451 ue = uentry_makeParam (t, 0);
456 declareUnnamedEnum (enumNameList el)
458 ctype ret = usymtab_enumEnumNameListType (el);
462 if (ctype_isDefined (ret))
465 e = uentry_makeEnumTagLoc (ctype_enumTag (rt), ret);
467 reflectStorageClass (e);
468 usymtab_supGlobalEntry (e);
470 declareEnumList (el, ret, g_currentloc);
471 enumNameList_free (el);
475 ctype ct = ctype_createEnum (fakeTag (), el);
477 e = uentry_makeEnumTagLoc (ctype_enumTag (ctype_realType (ct)), ct);
478 reflectStorageClass (e);
480 e = usymtab_supGlobalEntryReturn (e);
481 rt = uentry_getAbstractType (e);
482 declareEnumList (el, ct, g_currentloc);
489 declareEnum (cstring ename, enumNameList el)
494 llassert (cstring_isDefined (ename));
496 cet = ctype_createEnum (ename, el);
497 e = uentry_makeEnumTagLoc (ename, cet);
498 reflectStorageClass (e);
499 e = usymtab_supGlobalEntryReturn (e);
500 cet = uentry_getType (e);
501 declareEnumList (el, cet, uentry_whereLast (e));
502 return (uentry_getAbstractType (e));
506 declareEnumList (enumNameList el, ctype c, fileloc loc)
508 bool boolnames = FALSE;
509 bool othernames = FALSE;
511 (void) context_getSaveLocation (); /* undefine it */
513 if (context_maybeSet (FLG_NUMENUMMEMBERS))
515 int maxnum = context_getValue (FLG_NUMENUMMEMBERS);
516 int num = enumNameList_size (el);
522 message ("Enumerator %s declared with %d members (limit is set to %d)",
523 ctype_unparse (c), num, maxnum),
528 enumNameList_elements (el, e)
530 uentry ue = usymtab_lookupExposeGlob (e);
531 ctype ct = uentry_getType (ue);
533 llassert (uentry_isEnumConstant (ue));
535 if (ctype_isUnknown (ct))
537 uentry_setType (ue, c);
541 if (cstring_equal (e, context_getFalseName ())
542 || cstring_equal (e, context_getTrueName ()))
548 message ("Enumerator mixes boolean name (%s) with "
551 uentry_whereLast (ue)))
558 uentry_setType (ue, ctype_bool);
559 DPRINTF (("Set type: %s / %s",
560 uentry_unparse (ue), ctype_unparse (ctype_bool)));
568 message ("Enumerator mixes boolean names (%s, %s) with "
569 "non-boolean name: %s",
570 context_getTrueName (),
571 context_getFalseName (),
573 uentry_whereLast (ue)))
582 if (!ctype_match (c, ct))
584 if (ctype_isDirectBool (ct))
586 if (cstring_equal (e, context_getFalseName ())
587 || cstring_equal (e, context_getTrueName ()))
589 DPRINTF (("Here we are!"));
595 message ("Enumerator member %s declared with "
596 "inconsistent type: %s",
597 e, ctype_unparse (c)),
598 uentry_whereLast (ue)))
600 uentry_showWhereSpecifiedExtra
601 (ue, cstring_copy (ctype_unparse (ct)));
609 message ("Enumerator member %s declared with "
610 "inconsistent type: %s",
611 e, ctype_unparse (c)),
612 uentry_whereLast (ue)))
614 uentry_showWhereSpecifiedExtra
615 (ue, cstring_copy (ctype_unparse (ct)));
620 } end_enumNameList_elements;
623 static /*@dependent@*/ uentryList currentParamList;
625 void setCurrentParams (/*@dependent@*/ uentryList ue)
627 currentParamList = ue;
630 void clearCurrentParams (void)
632 currentParamList = uentryList_undefined;
636 ** requires: uentry_isFunction (e)
637 ** parameter names for current function are in currentParamList
640 static void enterFunctionParams (uentryList params)
644 uentryList_elements (params, current)
646 if (uentry_hasName (current))
648 uentry_setParamNo (current, paramno);
649 usymtab_supEntry (uentry_copy (current));
653 } end_uentryList_elements;
657 extern void enterParamsTemp (void)
659 usymtab_enterScope ();
660 enterFunctionParams (currentParamList);
663 extern void exitParamsTemp (void)
665 usymtab_quietPlainExitScope ();
668 static /*@exposed@*/ uentry globalDeclareFunction (idDecl tid)
670 ctype deftype = idDecl_getCtype (tid);
674 DPRINTF (("Global function: %s", idDecl_unparse (tid)));
676 if (ctype_isFunction (deftype))
678 rettype = ctype_returnValue (deftype);
682 rettype = ctype_unknown;
686 ** check has been moved here...
689 if (ctype_isFunction (idDecl_getCtype (tid)))
691 ue = uentry_makeIdFunction (tid);
692 reflectSpecialCode (ue);
693 reflectArgsUsed (ue);
697 llparseerror (message ("Inconsistent function declaration: %q",
698 idDecl_unparse (tid)));
700 tid = idDecl_replaceCtype
701 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
702 ue = uentry_makeIdFunction (tid);
705 reflectStorageClass (ue);
707 uentry_checkParams (ue);
708 reflectModGlobs (ue);
710 ue = usymtab_supGlobalEntryReturn (ue);
711 context_enterFunction (ue);
712 enterFunctionParams (uentry_getParams (ue));
714 resetStorageClass ();
719 ** for now, no type checking
720 ** (must check later though!)
723 static /*@only@*/ uentry globalDeclareOldStyleFunction (idDecl tid)
728 ** check has been moved here...
731 if (cstring_equalLit (idDecl_observeId (tid), "main"))
733 context_setFlagTemp (FLG_MAINTYPE, FALSE);
736 ue = uentry_makeIdFunction (tid);
737 reflectStorageClass (ue);
738 reflectSpecialCode (ue);
739 reflectArgsUsed (ue);
740 uentry_setDefined (ue, g_currentloc);
742 uentry_checkParams (ue);
744 if (ProcessingGlobals)
746 uentry_setGlobals (ue, currentGlobals);
749 resetStorageClass ();
753 static void oldStyleDeclareFunction (/*@only@*/ uentry e)
755 uentryList params = saveParamList;
756 ctype rt = uentry_getType (e);
758 llassert (ctype_isFunction (rt));
760 e = usymtab_supGlobalEntryReturn (e);
762 context_enterFunction (e);
763 enterFunctionParams (params);
764 saveParamList = uentryList_undefined;
765 resetStorageClass ();
768 void declareFunction (idDecl tid) /*@globals undef saveFunction; @*/
772 DPRINTF (("Declare function: %s", idDecl_unparse (tid)));
774 if (ProcessingParams)
776 ue = globalDeclareOldStyleFunction (tid);
781 saveFunction = uentry_undefined;
783 if (context_inRealFunction ())
785 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
787 llparseerror (message ("Function declared inside function: %q",
788 idDecl_unparse (tid)));
790 context_quietExitFunction ();
791 ue = usymtab_supEntryReturn (ue);
795 if (context_inInnerScope ())
797 llparseerror (message ("Declaration in inner context: %q",
798 idDecl_unparse (tid)));
800 sRef_setGlobalScope ();
801 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
803 ue = usymtab_supGlobalEntryReturn (ue);
804 sRef_clearGlobalScope ();
808 ue = globalDeclareFunction (tid);
815 resetStorageClass ();
819 void declareStaticFunction (idDecl tid) /*@globals undef saveFunction; @*/
823 DPRINTF (("Declare static funciton: %s", idDecl_unparse (tid)));
825 if (ProcessingParams)
827 ue = globalDeclareOldStyleFunction (tid);
832 saveFunction = uentry_undefined;
834 if (context_inRealFunction ())
836 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
838 llparseerror (message ("Function declared inside function: %q",
839 idDecl_unparse (tid)));
841 context_quietExitFunction ();
842 ue = usymtab_supEntryReturn (ue);
846 if (context_inInnerScope ())
848 llparseerror (message ("Declaration in inner context: %q",
849 idDecl_unparse (tid)));
851 sRef_setGlobalScope ();
852 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
854 ue = usymtab_supGlobalEntryReturn (ue);
855 sRef_clearGlobalScope ();
859 ctype deftype = idDecl_getCtype (tid);
862 if (ctype_isFunction (deftype))
864 rettype = ctype_returnValue (deftype);
868 rettype = ctype_unknown;
872 ** check has been moved here...
875 if (ctype_isFunction (idDecl_getCtype (tid)))
877 ue = uentry_makeIdFunction (tid);
878 reflectSpecialCode (ue);
879 reflectArgsUsed (ue);
883 llparseerror (message ("Inconsistent function declaration: %q",
884 idDecl_unparse (tid)));
886 tid = idDecl_replaceCtype
887 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
888 ue = uentry_makeIdFunction (tid);
891 reflectStorageClass (ue);
892 uentry_setStatic (ue);
894 uentry_checkParams (ue);
895 reflectModGlobs (ue);
897 DPRINTF (("Sub global entry: %s", uentry_unparse (ue)));
898 ue = usymtab_supGlobalEntryReturn (ue);
900 context_enterFunction (ue);
901 enterFunctionParams (uentry_getParams (ue));
902 resetStorageClass ();
909 resetStorageClass ();
914 checkTypeDecl (uentry e, ctype rep)
916 cstring n = uentry_getName (e);
918 DPRINTF (("Check type decl: %s", n));
920 if (cstring_equal (context_getBoolName (), n))
922 ctype rrep = ctype_realType (rep);
925 ** for abstract enum types, we need to fix the enum members:
926 ** they should have the abstract type, not the rep type.
929 if (ctype_isEnum (ctype_realType (rrep)))
931 enumNameList el = ctype_elist (rrep);
933 enumNameList_elements (el, ye)
935 if (usymtab_existsGlob (ye))
937 uentry ue = usymtab_lookupSafe (ye);
938 uentry_setType (ue, ctype_bool);
941 if (cstring_equal (context_getTrueName (), ye)
942 || cstring_equal (context_getFalseName (), ye))
950 message ("Member of boolean enumerated type definition "
951 "does not match name set to represent TRUE "
954 message ("Use -boolfalse and -booltrue to set the "
955 "name of false and true boolean values."),
956 uentry_whereDefined (e));
958 } end_enumNameList_elements;
962 if (usymtab_exists (n))
964 usymId llm = usymtab_getId (n);
965 uentry le = usymtab_getTypeEntry (llm);
967 uentry_setDeclared (e, g_currentloc);
968 uentry_setSref (e, sRef_makeGlobal (llm, uentry_getType (le)));
970 DPRINTF (("Here we are: %s / %s",
971 n, context_getBoolName ()));
973 if (uentry_isAbstractDatatype (le))
975 ctype rrep = ctype_realType (rep);
978 ** for abstract enum types, we need to fix the enum members:
979 ** they should have the abstract type, not the rep type.
982 if (ctype_isEnum (ctype_realType (rrep)))
984 ctype at = uentry_getAbstractType (le);
985 enumNameList el = ctype_elist (rrep);
987 enumNameList_elements (el, ye)
989 if (usymtab_existsGlob (ye))
991 uentry ue = usymtab_lookupSafe (ye);
993 llassert (uentry_isEitherConstant (ue));
994 llassertprint (ctype_match (uentry_getType (ue), rrep),
995 ("Bad enum: %s / %s",
997 ctype_unparse (rrep)));
999 uentry_setType (ue, at);
1001 } end_enumNameList_elements;
1004 if (uentry_isMutableDatatype (le))
1006 /* maybe more complicated if abstract and immutable ? */
1008 if (!ctype_isRealPointer (rep) && !ctype_isRealAbstract (rep))
1012 message ("Mutable abstract type %s declared without pointer "
1013 "indirection: %s (violates assignment semantics)",
1014 n, ctype_unparse (rep)),
1015 uentry_whereDefined (e));
1017 uentry_setMutable (e);
1024 fileloc fl = uentry_whereDeclared (e);
1026 if (context_getFlag (FLG_LIKELYBOOL)
1027 && !context_getFlag (FLG_BOOLINT))
1029 if ((cstring_equalLit (n, "BOOL")
1030 || cstring_equalLit (n, "Bool")
1031 || cstring_equalLit (n, "bool")
1032 || cstring_equalLit (n, "boolean")
1033 || cstring_equalLit (n, "Boolean")
1034 || cstring_equalLit (n, "BOOLEAN"))
1035 && !(cstring_equal (n, context_getBoolName ())))
1037 if (context_setBoolName ()) {
1040 message ("Type %s is probably meant as a boolean type, but does "
1041 "not match the boolean type name \"%s\".",
1043 context_getBoolName ()),
1048 message ("Type %s is probably meant as a boolean type, "
1049 "but the boolean type name is not set. "
1050 "Use -booltype %s to set it.",
1057 if (!uentry_isStatic (e)
1058 && !ctype_isFunction (uentry_getType (e))
1059 && !fileloc_isLib (fl)
1060 && !fileloc_isImport (fl)
1061 && fileloc_isHeader (fl))
1063 voptgenerror (FLG_EXPORTTYPE,
1064 message ("Type exported, but not specified: %s\n", n),
1073 fixUentryList (idDeclList tl, qtype q)
1075 uentryList f = uentryList_new ();
1077 idDeclList_elements (tl, i)
1079 if (idDecl_isDefined (i))
1085 (void) idDecl_fixBase (i, q);
1088 ** implicit annotations
1091 (void) fixStructDecl (i);
1093 ue = uentry_makeIdVariable (i);
1094 rt = ctype_realType (uentry_getType (ue));
1097 ** where is this here???
1099 if (ctype_isArray (rt) || ctype_isSU (rt))
1101 sRef_setAllocated (uentry_getSref (ue), uentry_whereDefined (ue));
1107 if (uentry_isValid (old = uentryList_lookupField (f, uentry_rawName (ue))))
1109 if (optgenerror (FLG_SYNTAX,
1110 message ("Field name reused: %s", uentry_rawName (ue)),
1111 uentry_whereDefined (ue)))
1113 llgenmsg (message ("Previous use of %s", uentry_rawName (ue)),
1114 uentry_whereDefined (old));
1118 f = uentryList_add (f, ue);
1120 } end_idDeclList_elements;
1122 idDeclList_free (tl);
1127 ** This is a hack to support unnamed struct/union fields as done by
1128 ** Microsoft VC++. It is not supported by the ANSI standard.
1130 ** The inner fields are added to the outer structure. This is meaningful
1131 ** for nesting structs inside unions, but lclint does no related
1136 fixUnnamedDecl (qtype q)
1138 ctype ct = ctype_realType (qtype_getType (q));
1140 if (ctype_isStruct (ct) || ctype_isUnion (ct))
1142 uentryList res = ctype_getFields (ct);
1144 return (uentryList_copy (res));
1151 return uentryList_undefined;
1154 void setStorageClass (storageClassCode sc)
1160 setProcessingIterVars (uentry iter)
1162 ProcessingIterVars = TRUE;
1164 saveIterParamNo = 0;
1168 setProcessingGlobalsList ()
1170 ProcessingGlobals = TRUE;
1172 llassert (globSet_isUndefined (currentGlobals));
1173 currentGlobals = globSet_undefined;
1175 llassert (sRefSet_isUndefined (fcnModifies));
1176 fcnModifies = sRefSet_undefined;
1179 ** No, special clauses might have been processed first!
1180 llassert (specialClauses_isUndefined (specClauses));
1181 specClauses = specialClauses_undefined;
1184 fcnNoGlobals = FALSE;
1187 static bool ProcessingGlobMods = FALSE;
1190 setProcessingGlobMods ()
1192 ProcessingGlobMods = TRUE;
1196 clearProcessingGlobMods ()
1198 ProcessingGlobMods = FALSE;
1202 isProcessingGlobMods ()
1204 return (ProcessingGlobMods);
1207 static void resetGlobals (void)
1209 ProcessingGlobals = FALSE;
1210 currentGlobals = globSet_undefined;
1211 llassert (sRefSet_isUndefined (fcnModifies));
1212 fcnModifies = sRefSet_undefined;
1213 fcnNoGlobals = FALSE;
1217 unsetProcessingGlobals ()
1219 ProcessingGlobals = FALSE;
1223 setProcessingVars (/*@only@*/ qtype q)
1225 ProcessingVars = TRUE;
1226 qtype_free (processingType);
1231 setGenericParamList (/*@dependent@*/ uentryList pm)
1233 ProcessingParams = TRUE;
1238 setProcessingTypedef (/*@only@*/ qtype q)
1240 ProcessingTypedef = TRUE;
1242 qtype_free (processingType);
1247 unsetProcessingVars ()
1249 resetStorageClass ();
1250 ProcessingVars = FALSE;
1256 if (ProcessingParams)
1258 if (uentry_isInvalid (saveFunction))
1260 llbuglit ("unsetProcessingVars: no saved function\n");
1262 if (sRefSet_isDefined (fcnModifies)) {
1263 sRefSet_free (fcnModifies);
1264 fcnModifies = sRefSet_undefined;
1269 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1270 uentryList params = uentryList_copy (saveParamList);
1271 ctype ct2 = ctype_makeFunction (ct, params);
1273 uentry_setType (saveFunction, ct2);
1274 ProcessingParams = FALSE;
1276 reflectModGlobs (saveFunction);
1277 oldStyleDeclareFunction (saveFunction);
1278 saveFunction = uentry_undefined;
1285 ** If the paramlist used a type name, we could be here.
1288 llfatalerror (message ("%q: Old-style function parameter list uses a "
1289 "type name.", fileloc_unparse (g_currentloc)));
1296 if (uentry_isValid (saveFunction))
1299 ** old style declaration
1302 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1305 uentryList_elements (saveParamList, current)
1307 uentry_setType (current, ctype_int); /* all params are ints */
1308 } end_uentryList_elements;
1310 ct2 = ctype_makeParamsFunction (ct, uentryList_copy (saveParamList));
1312 uentry_setType (saveFunction, ct2);
1313 ProcessingParams = FALSE;
1315 oldStyleDeclareFunction (saveFunction);
1316 saveFunction = uentry_undefined;
1321 unsetProcessingTypedef ()
1323 ProcessingTypedef = FALSE;
1326 void checkConstant (qtype t, idDecl id)
1330 id = idDecl_fixBase (id, t);
1331 e = uentry_makeIdConstant (id);
1333 reflectStorageClass (e);
1334 resetStorageClass ();
1336 usymtab_supGlobalEntry (e);
1339 void checkValueConstant (qtype t, idDecl id, exprNode e)
1343 id = idDecl_fixBase (id, t);
1344 ue = uentry_makeIdConstant (id);
1345 reflectStorageClass (ue);
1346 resetStorageClass ();
1348 if (exprNode_isDefined (e))
1350 if (!exprNode_matchType (uentry_getType (ue), e))
1353 (exprNode_getType (e), e,
1354 uentry_getType (ue), exprNode_undefined,
1355 message ("Constant %q initialized to type %t, expects %t: %s",
1356 uentry_getName (ue),
1357 exprNode_getType (e),
1358 uentry_getType (ue),
1359 exprNode_unparse (e)),
1364 if (exprNode_hasValue (e))
1366 uentry_mergeConstantValue (ue, multiVal_copy (exprNode_getValue (e)));
1371 usymtab_supGlobalEntry (ue);
1375 void processNamedDecl (idDecl t)
1377 if (qtype_isUndefined (processingType))
1379 llparseerror (message ("No type before declaration name: %q", idDecl_unparse (t)));
1381 processingType = qtype_create (ctype_unknown);
1384 t = idDecl_fixBase (t, processingType);
1386 DPRINTF (("Declare: %s", idDecl_unparse (t)));
1388 if (ProcessingGlobals)
1390 cstring id = idDecl_getName (t);
1391 uentry ue = usymtab_lookupSafe (id);
1393 if (!uentry_isValid (ue))
1395 llerror (FLG_UNRECOG,
1396 message ("Variable used in globals list is undeclared: %s", id));
1400 if (!ctype_match (uentry_getType (ue), idDecl_getCtype (t)))
1404 message ("Variable %s used in globals list declared %s, "
1406 id, ctype_unparse (uentry_getType (ue)),
1407 ctype_unparse (idDecl_getCtype (t))),
1412 sRef sr = sRef_copy (uentry_getSref (ue));
1414 reflectGlobalQualifiers (sr, idDecl_getQuals (t));
1416 currentGlobals = globSet_insert (currentGlobals, sr);
1420 else if (ProcessingVars)
1425 ct = ctype_realType (idDecl_getCtype (t));
1427 if (ProcessingParams)
1429 cstring id = idDecl_getName (t);
1430 int paramno = uentryList_lookupRealName (saveParamList, id);
1434 uentry cparam = uentryList_getN (saveParamList, paramno);
1436 uentry_setType (cparam, idDecl_getCtype (t));
1437 uentry_reflectQualifiers (cparam, idDecl_getQuals (t));
1438 uentry_setDeclaredOnly (cparam, context_getSaveLocation ());
1443 (message ("Old style declaration uses unlisted parameter: %s",
1451 if (context_inIterDef ())
1453 cstring pname = makeParam (idDecl_observeId (t));
1454 uentry p = usymtab_lookupSafe (pname);
1456 cstring_free (pname);
1458 if (uentry_isYield (p))
1460 e = uentry_makeParam (t, sRef_getParam (uentry_getSref (p)));
1462 uentry_checkYieldParam (p, e);
1464 usymtab_supEntrySref (e);
1469 if ((hasSpecialCode () || argsUsed)
1470 && ctype_isFunction (idDecl_getCtype (t)))
1472 e = uentry_makeIdFunction (t);
1473 reflectSpecialCode (e);
1474 reflectArgsUsed (e);
1478 e = uentry_makeIdVariable (t);
1481 loc = uentry_whereDeclared (e);
1484 if (context_inGlobalScope ())
1486 uentry_checkParams was here!
1490 if (ctype_isFunction (uentry_getType (e)))
1492 reflectModGlobs (e);
1496 llassert (!globSet_isDefined (currentGlobals)
1497 && !sRefSet_isDefined (fcnModifies));
1500 e = usymtab_supEntrySrefReturn (e);
1502 if (uentry_isExtern (e) && !context_inGlobalScope ())
1506 message ("Declaration using extern inside function scope: %q",
1507 uentry_unparse (e)),
1510 uentry_setDefined (e, fileloc_getExternal ());
1511 sRef_setDefined (uentry_getSref (e), fileloc_getExternal ());
1514 if (uentry_isFunction (e))
1516 uentry_checkParams (e);
1517 checkParamNames (e);
1520 if (uentry_isVar (e)
1521 && uentry_isCheckedUnknown (e))
1523 sRef sr = uentry_getSref (e);
1525 if (sRef_isLocalVar (sr))
1527 if (context_getFlag (FLG_IMPCHECKMODINTERNALS))
1529 uentry_setCheckMod (e);
1533 uentry_setUnchecked (e);
1536 else if (sRef_isFileStatic (sr))
1538 if (context_getFlag (FLG_IMPCHECKEDSTRICTSTATICS))
1540 uentry_setCheckedStrict (e);
1542 else if (context_getFlag (FLG_IMPCHECKEDSTATICS))
1544 uentry_setChecked (e);
1546 else if (context_getFlag (FLG_IMPCHECKMODSTATICS))
1548 uentry_setCheckMod (e);
1555 else /* real global */
1557 llassert (sRef_isRealGlobal (sr));
1559 if (context_getFlag (FLG_IMPCHECKEDSTRICTGLOBALS))
1561 uentry_setCheckedStrict (e);
1563 else if (context_getFlag (FLG_IMPCHECKEDGLOBALS))
1565 uentry_setChecked (e);
1567 else if (context_getFlag (FLG_IMPCHECKMODGLOBALS))
1569 uentry_setCheckMod (e);
1579 else if (ProcessingTypedef)
1581 ctype ct = idDecl_getCtype (t);
1584 DPRINTF (("Processing typedef: %s", ctype_unparse (ct)));
1586 e = uentry_makeIdDatatype (t);
1588 if (cstring_equal (idDecl_getName (t), context_getBoolName ())) {
1589 ctype rt = ctype_realType (ct);
1591 if (ctype_isEnum (rt)) {
1594 if (!(ctype_isInt (rt)
1595 || ctype_isUnknown (rt)
1596 || ctype_isChar (rt))) {
1599 message ("Boolean type %s defined using non-standard type %s (integral, char or enum type expected)",
1600 context_getBoolName (),
1601 ctype_unparse (ct)),
1602 uentry_whereLast (e));
1606 uentry_setType (e, ct);
1610 reflectStorageClass (e);
1611 checkTypeDecl (e, ct);
1613 e = usymtab_supReturnTypeEntry (e);
1615 if (uentry_isMaybeAbstract (e))
1617 if (context_getFlag (FLG_IMPABSTRACT))
1619 uentry_setAbstract (e);
1623 uentry_setConcrete (e);
1629 llparseerror (message ("Suspect missing struct or union keyword: %q",
1630 idDecl_unparse (t)));
1636 ** moved from grammar
1639 static idDecl fixStructDecl (/*@returned@*/ idDecl d)
1641 if (ctype_isVisiblySharable (idDecl_getCtype (d))
1642 && context_getFlag (FLG_STRUCTIMPONLY))
1644 if (!qualList_hasAliasQualifier (idDecl_getQuals (d)))
1646 if (qualList_hasExposureQualifier (idDecl_getQuals (d)))
1648 idDecl_addQual (d, qual_createDependent ());
1652 idDecl_addQual (d, qual_createImpOnly ());
1661 declareUnnamedStruct (/*@only@*/ uentryList f)
1663 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1665 int num = uentryList_size (f);
1666 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1671 (FLG_NUMSTRUCTFIELDS,
1672 message ("Structure declared with %d fields "
1673 "(limit is set to %d)",
1679 return (ctype_createUnnamedStruct (f));
1683 declareUnnamedUnion (/*@only@*/ uentryList f)
1685 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1687 int num = uentryList_size (f);
1688 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1693 (FLG_NUMSTRUCTFIELDS,
1694 message ("Union declared with %d fields "
1695 "(limit is set to %d)",
1701 return (ctype_createUnnamedUnion (f));
1704 ctype declareStruct (cstring id, /*@only@*/ uentryList f)
1708 int num = uentryList_size (f);
1710 ct = ctype_createStruct (cstring_copy (id), f);
1711 DPRINTF (("Declare struct: %s [%d]", ctype_unparse (ct), ct));
1712 ue = uentry_makeStructTagLoc (id, ct);
1714 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1716 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1721 (FLG_NUMSTRUCTFIELDS,
1722 message ("Structure %q declared with %d fields "
1723 "(limit is set to %d)",
1724 uentry_getName (ue), num, max),
1725 uentry_whereLast (ue));
1729 return (usymtab_supTypeEntry (ue));
1732 ctype declareUnion (cstring id, uentryList f)
1736 int num = uentryList_size (f);
1738 ct = ctype_createUnion (cstring_copy (id), f);
1739 ue = uentry_makeUnionTagLoc (id, ct);
1741 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1743 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1748 (FLG_NUMSTRUCTFIELDS,
1749 message ("Union %q declared with %d fields "
1750 "(limit is set to %d)",
1751 uentry_getName (ue), num, max),
1752 uentry_whereLast (ue));
1756 return (usymtab_supTypeEntry (ue));
1759 ctype handleStruct (/*@only@*/ cstring id)
1761 if (usymtab_existsStructTag (id))
1763 ctype ct = uentry_getAbstractType (usymtab_lookupStructTag (id));
1770 return (ctype_createForwardStruct (id));
1774 ctype handleUnion (/*@only@*/ cstring id)
1776 if (usymtab_existsUnionTag (id))
1778 ctype ret = uentry_getAbstractType (usymtab_lookupUnionTag (id));
1784 return (ctype_createForwardUnion (id));
1789 handleEnum (cstring id)
1791 if (usymtab_existsEnumTag (id))
1793 ctype ret = uentry_getAbstractType (usymtab_lookupEnumTag (id));
1799 return (declareEnum (id, enumNameList_new ()));
1803 bool processingIterVars (void)
1805 return ProcessingIterVars;
1808 uentry getCurrentIter (void)
1813 static bool flipOldStyle = FALSE;
1814 static bool flipNewStyle = TRUE;
1816 void setFlipOldStyle () { flipOldStyle = TRUE; }
1817 bool isFlipOldStyle () { return flipOldStyle; }
1818 bool isNewStyle () { return flipNewStyle; }
1819 void setNewStyle () { flipNewStyle = TRUE; }
1821 /*@dependent@*/ uentryList handleParamIdList (/*@dependent@*/ uentryList params)
1826 ** this is a really YUCKY hack to handle old style
1830 voptgenerror (FLG_OLDSTYLE,
1831 cstring_makeLiteral ("Old style function declaration"),
1834 uentryList_elements (params, current)
1836 uentry_setParam (current);
1837 uentry_setSref (current, sRef_makeParam (paramno, ctype_unknown));
1839 } end_uentryList_elements;
1841 setGenericParamList (params);
1842 g_expectingTypeName = TRUE;
1847 /*@dependent@*/ uentryList handleParamTypeList (/*@returned@*/ uentryList params)
1851 uentryList_fixMissingNames (params);
1853 voptgenerror (FLG_OLDSTYLE,
1854 cstring_makeLiteral ("Old style function declaration."),
1857 setGenericParamList (params);
1858 flipOldStyle = FALSE;
1859 g_expectingTypeName = TRUE;
1868 ctype c = ctype_unknown;
1869 cstring id = cstring_makeLiteral ("va_alist");
1872 if (ProcessingParams)
1874 int i = uentryList_lookupRealName (saveParamList, id);
1878 e = uentry_makeVariableSrefParam (id, c, sRef_makeParam (i, c));
1882 e = uentry_undefined; /* suppress gcc message */
1883 llfatalerrorLoc (cstring_makeLiteral ("va_dcl used without va_alist"));
1888 llerror (FLG_SYNTAX, cstring_makeLiteral ("va_dcl used outside of function declaration"));
1889 e = uentry_makeVariableLoc (id, c);
1893 uentry_setUsed (e, g_currentloc);
1894 usymtab_supEntrySref (e);
1897 /*@exposed@*/ sRef modListPointer (sRef s)
1899 ctype ct = sRef_getType (s);
1900 ctype rt = ctype_realType (ct);
1902 if (ctype_isAP (rt))
1904 if (context_inHeader () && ctype_isAbstract (ct))
1909 ("Modifies clause in header file dereferences abstract "
1910 "type %s (interface modifies clause should not depend "
1911 "on or expose type representation): %q",
1917 return (sRef_constructPointer (s));
1921 if (ctype_isKnown (rt))
1925 message ("Implementation modifies clause dereferences non-pointer (type %s): %q",
1935 /*@exposed@*/ sRef modListFieldAccess (sRef s, cstring f)
1937 ctype ct = sRef_getType (s);
1938 ctype rt = ctype_realType (ct);
1940 if (ctype_isStructorUnion (rt))
1942 uentry tf = uentryList_lookupField (ctype_getFields (rt), f);
1944 if (uentry_isUndefined (tf))
1946 voptgenerror (FLG_TYPE,
1947 message ("Modifies list accesses non-existent "
1948 "field %s of %t: %q", f, ct,
1953 return sRef_undefined;
1957 if (ctype_isAbstract (ct) && context_inHeader ())
1962 ("Modifies clause in header file accesses abstract "
1963 "type %s (interface modifies clause should not depend "
1964 "on or expose type representation): %q",
1971 cstring_markOwned (f);
1972 return (sRef_makeField (s, f));
1978 message ("Modifies clause dereferences non-pointer (type %s): %q",
1988 sRef globListUnrecognized (cstring s)
1990 if (cstring_equalLit (s, "nothing"))
1992 return sRef_makeNothing ();
1994 else if (cstring_equalLit (s, "internalState"))
1996 return sRef_makeInternalState ();
1998 else if (cstring_equalLit (s, "fileSystem")
1999 || cstring_equalLit (s, "systemState"))
2001 return sRef_makeSystemState ();
2007 message ("Unrecognized identifier in globals list: %s", s),
2010 return sRef_undefined;
2014 /*@exposed@*/ sRef modListArrowAccess (sRef s, cstring f)
2016 ctype ct = sRef_getType (s);
2017 ctype rt = ctype_realType (ct);
2019 if (ctype_isRealPointer (rt))
2021 ctype b = ctype_baseArrayPtr (rt);
2022 ctype rb = ctype_realType (b);
2024 if (ctype_isStructorUnion (rb))
2026 uentry tf = uentryList_lookupField (ctype_getFields (rb), f);
2028 if (uentry_isUndefined (tf))
2030 voptgenerror (FLG_TYPE,
2031 message ("Modifies list arrow accesses non-existent "
2032 "field %s of %t: %q", f, b,
2037 return sRef_undefined;
2041 if (context_inHeader ())
2043 if (ctype_isAbstract (b))
2048 ("Modifies clause in header file arrow accesses abstract "
2049 "type %s (interface modifies clause should not depend "
2050 "on or expose type representation): %q",
2058 if (ctype_isAbstract (ct))
2063 ("Modifies clause in header file arrow accesses abstract "
2064 "type %s (interface modifies clause should not depend "
2065 "on or expose type representation): %q",
2073 cstring_markOwned (f);
2074 return (sRef_makeArrow (s, f));
2080 message ("Modifies clause arrow accesses pointer to "
2081 "non-structure (type %s): %q",
2091 message ("Modifies clause arrow accesses non-pointer (type %s): %q",
2101 sRef checkSpecClausesId (uentry ue)
2103 cstring s = uentry_rawName (ue);
2105 if (sRef_isGlobal (uentry_getSref (ue)))
2109 message ("Global variable %s used special clause. (Global variables "
2110 "are not recognized in special clauses. If there is "
2111 "sufficient interest in support for this, it may be "
2112 "added to a future release. Send mail to "
2113 "lclint@cs.virginia.edu.)",
2117 return sRef_undefined;
2121 if (cstring_equalLit (s, "result"))
2125 message ("Special clause list uses %s which is a variable and has special "
2126 "meaning in a modifies list. (Special meaning assumed.)", s),
2129 uentry_showWhereDeclared (ue);
2133 return uentry_getSref (ue);
2139 don;t know what the real date is...
2145 based on checkSpecClausesId
2148 sRef checkbufferConstraintClausesId (uentry ue)
2150 cstring s = uentry_rawName (ue);
2151 if (cstring_equalLit (s, "result"))
2155 message ("Special clause list uses %s which is a variable and has special "
2156 "meaning in a modifies list. (Special meaning assumed.)", s),
2159 uentry_showWhereDeclared (ue);
2163 return uentry_getSref (ue);
2166 void checkModifiesId (uentry ue)
2168 cstring s = uentry_rawName (ue);
2170 if (cstring_equalLit (s, "nothing")
2171 || cstring_equalLit (s, "internalState")
2172 || cstring_equalLit (s, "systemState")
2173 || (cstring_equalLit (s, "fileSystem")))
2177 message ("Modifies list uses %s which is a variable and has special "
2178 "meaning in a modifies list. (Special meaning assumed.)", s),
2181 uentry_showWhereDeclared (ue);
2186 /*@exposed@*/ sRef fixModifiesId (cstring s)
2189 cstring pname = makeParam (s);
2190 uentry ue = usymtab_lookupSafe (pname);
2192 cstring_free (pname);
2194 if (cstring_equalLit (s, "nothing"))
2196 ret = sRef_makeNothing ();
2198 else if (cstring_equalLit (s, "internalState"))
2200 ret = sRef_makeInternalState ();
2202 else if (cstring_equalLit (s, "fileSystem")
2203 || cstring_equalLit (s, "systemState"))
2205 ret = sRef_makeSystemState ();
2209 ret = sRef_undefined;
2212 if (sRef_isValid (ret))
2214 if (uentry_isValid (ue))
2218 message ("Modifies list uses %s which is a parameter and has special "
2219 "meaning in a modifies list. (Special meaning assumed.)", s),
2225 if (uentry_isValid (ue))
2227 ret = uentry_getSref (ue);
2231 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2232 ret = sRef_undefined;
2236 message ("Unrecognized identifier in modifies comment: %s", s),
2246 sRef fixSpecClausesId (cstring s)
2249 cstring pname = makeParam (s);
2250 uentry ue = usymtab_lookupSafe (pname);
2252 cstring_free (pname);
2254 if (cstring_equalLit (s, "result"))
2256 ret = sRef_makeResult ();
2260 ret = sRef_undefined;
2263 if (sRef_isValid (ret))
2265 if (uentry_isValid (ue))
2269 message ("Special clause uses %s which is a parameter and has special "
2270 "meaning in a special clause. (Special meaning assumed.)", s),
2276 if (uentry_isValid (ue))
2278 ret = uentry_getSref (ue);
2280 if (sRef_isGlobal (ret))
2284 message ("Global variable %s used special clause. (Global variables "
2285 "are not recognized in special clauses. If there is "
2286 "sufficient interest in support for this, it may be "
2287 "added to a future release. Send mail to "
2288 "lclint@cs.virginia.edu.)",
2292 ret = sRef_undefined;
2297 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2298 ret = sRef_undefined;
2302 message ("Unrecognized identifier in special clause: %s", s),
2312 sRef modListArrayFetch (sRef s, /*@unused@*/ sRef mexp)
2314 ctype ct = sRef_getType (s);
2315 ctype rt = ctype_realType (ct);
2317 if (ctype_isAP (rt))
2319 if (context_inHeader () && ctype_isAbstract (ct))
2324 ("Modifies clause in header file indexes abstract "
2325 "type %s (interface modifies clause should not depend "
2326 "on or expose type representation): %q",
2332 return (sRef_makeAnyArrayFetch (s));
2339 ("Implementation modifies clause uses array fetch on non-array (type %s): %q",
2340 ctype_unparse (ct), sRef_unparse (s)),