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)));
294 constraintList getFunctionConstraints (void)
296 return constraintList_copy (fcnConstraints);
300 constraintList getEnsuresConstraints (void)
302 return constraintList_copy (fcnEnsuresConstraints);
305 void setEnsuresConstraints (constraintList c)
308 fcnEnsuresConstraints = constraintList_copy (c);
311 void setFunctionConstraints (constraintList c)
314 fcnConstraints = constraintList_copy (c);
318 void setFunctionModifies (sRefSet s)
320 sRefSet_free (fcnModifies);
324 static void reflectGlobalQualifiers (sRef sr, qualList quals)
326 qualList_elements (quals, qel)
328 if (qual_isGlobalQual (qel)) /* undef, killed */
330 sstate oldstate = sRef_getDefState (sr);
331 sstate defstate = sstate_fromQual (qel);
333 if ((oldstate == SS_UNDEFGLOB && defstate == SS_KILLED)
334 || (oldstate == SS_KILLED && defstate == SS_UNDEFGLOB))
336 defstate = SS_UNDEFKILLED;
343 sRef_setDefState (sr, defstate, fileloc_undefined);
345 else if (qual_isAllocQual (qel)) /* out, partial, reldef, etc. */
347 ctype realType = sRef_getType (sr);
348 sstate defstate = sstate_fromQual (qel);
350 if (qual_isRelDef (qel))
352 ; /* okay anywhere */
356 if (!ctype_isAP (realType)
357 && !ctype_isSU (realType)
358 && !ctype_isUnknown (realType)
359 && !ctype_isAbstract (sRef_getType (sr)))
363 message ("Qualifier %s used on non-pointer or struct: %q",
364 qual_unparse (qel), sRef_unparse (sr)));
369 sRef_setDefState (sr, defstate, fileloc_undefined);
371 else if (qual_isNull (qel))
373 sRef_setNullState (sr, NS_POSNULL, fileloc_undefined);
375 else if (qual_isRelNull (qel))
377 sRef_setNullState (sr, NS_RELNULL, fileloc_undefined);
379 else if (qual_isNotNull (qel))
381 sRef_setNullState (sr, NS_MNOTNULL, fileloc_undefined);
385 if (qual_isCQual (qel))
392 message ("Qualifier %s cannot be used in a globals list",
393 qual_unparse (qel)));
396 } end_qualList_elements;
399 void globListAdd (sRef sr, qualList quals)
401 if (sRef_isValid (sr))
403 sRef sc = sRef_copy (sr);
405 reflectGlobalQualifiers (sc, quals);
406 currentGlobals = globSet_insert (currentGlobals, sc);
410 extern void declareCIter (cstring name, /*@owned@*/ uentryList params)
414 ue = uentry_makeIter (name,
415 ctype_makeFunction (ctype_void, params),
416 fileloc_copy (g_currentloc));
418 usymtab_supEntry (uentry_makeEndIter (name, fileloc_copy (g_currentloc)));
420 reflectModGlobs (ue);
422 ue = usymtab_supGlobalEntryReturn (ue);
425 extern void nextIterParam (void)
427 llassert (ProcessingIterVars);
431 extern int iterParamNo (void)
433 llassert (ProcessingIterVars);
434 return saveIterParamNo;
438 ** yucky hacks to put it in the right place
442 makeCurrentParam (idDecl t)
446 saveStoreLoc = fileloc_undefined;
448 /* param number unknown */
450 ue = uentry_makeParam (t, 0);
455 declareUnnamedEnum (enumNameList el)
457 ctype ret = usymtab_enumEnumNameListType (el);
461 if (ctype_isDefined (ret))
464 e = uentry_makeEnumTagLoc (ctype_enumTag (rt), ret);
466 reflectStorageClass (e);
467 usymtab_supGlobalEntry (e);
469 declareEnumList (el, ret, g_currentloc);
470 enumNameList_free (el);
474 ctype ct = ctype_createEnum (fakeTag (), el);
476 e = uentry_makeEnumTagLoc (ctype_enumTag (ctype_realType (ct)), ct);
477 reflectStorageClass (e);
479 e = usymtab_supGlobalEntryReturn (e);
480 rt = uentry_getAbstractType (e);
481 declareEnumList (el, ct, g_currentloc);
488 declareEnum (cstring ename, enumNameList el)
493 llassert (cstring_isDefined (ename));
495 cet = ctype_createEnum (ename, el);
496 e = uentry_makeEnumTagLoc (ename, cet);
497 reflectStorageClass (e);
498 e = usymtab_supGlobalEntryReturn (e);
499 cet = uentry_getType (e);
500 declareEnumList (el, cet, uentry_whereLast (e));
501 return (uentry_getAbstractType (e));
505 declareEnumList (enumNameList el, ctype c, fileloc loc)
507 bool boolnames = FALSE;
508 bool othernames = FALSE;
510 (void) context_getSaveLocation (); /* undefine it */
512 if (context_maybeSet (FLG_NUMENUMMEMBERS))
514 int maxnum = context_getValue (FLG_NUMENUMMEMBERS);
515 int num = enumNameList_size (el);
521 message ("Enumerator %s declared with %d members (limit is set to %d)",
522 ctype_unparse (c), num, maxnum),
527 enumNameList_elements (el, e)
529 uentry ue = usymtab_lookupExposeGlob (e);
530 ctype ct = uentry_getType (ue);
532 llassert (uentry_isEnumConstant (ue));
534 if (ctype_isUnknown (ct))
536 uentry_setType (ue, c);
540 if (cstring_equal (e, context_getFalseName ())
541 || cstring_equal (e, context_getTrueName ()))
547 message ("Enumerator mixes boolean name (%s) with "
550 uentry_whereLast (ue)))
557 uentry_setType (ue, ctype_bool);
558 DPRINTF (("Set type: %s / %s",
559 uentry_unparse (ue), ctype_unparse (ctype_bool)));
567 message ("Enumerator mixes boolean names (%s, %s) with "
568 "non-boolean name: %s",
569 context_getTrueName (),
570 context_getFalseName (),
572 uentry_whereLast (ue)))
581 if (!ctype_match (c, ct))
583 if (ctype_isDirectBool (ct))
585 if (cstring_equal (e, context_getFalseName ())
586 || cstring_equal (e, context_getTrueName ()))
588 DPRINTF (("Here we are!"));
594 message ("Enumerator member %s declared with "
595 "inconsistent type: %s",
596 e, ctype_unparse (c)),
597 uentry_whereLast (ue)))
599 uentry_showWhereSpecifiedExtra
600 (ue, cstring_copy (ctype_unparse (ct)));
608 message ("Enumerator member %s declared with "
609 "inconsistent type: %s",
610 e, ctype_unparse (c)),
611 uentry_whereLast (ue)))
613 uentry_showWhereSpecifiedExtra
614 (ue, cstring_copy (ctype_unparse (ct)));
619 } end_enumNameList_elements;
622 static /*@dependent@*/ uentryList currentParamList;
624 void setCurrentParams (/*@dependent@*/ uentryList ue)
626 currentParamList = ue;
629 void clearCurrentParams (void)
631 currentParamList = uentryList_undefined;
635 ** requires: uentry_isFunction (e)
636 ** parameter names for current function are in currentParamList
639 static void enterFunctionParams (uentryList params)
643 uentryList_elements (params, current)
645 if (uentry_hasName (current))
647 uentry_setParamNo (current, paramno);
648 usymtab_supEntry (uentry_copy (current));
652 } end_uentryList_elements;
656 extern void enterParamsTemp (void)
658 usymtab_enterScope ();
659 enterFunctionParams (currentParamList);
662 extern void exitParamsTemp (void)
664 usymtab_quietPlainExitScope ();
667 static /*@exposed@*/ uentry globalDeclareFunction (idDecl tid)
669 ctype deftype = idDecl_getCtype (tid);
673 DPRINTF (("Global function: %s", idDecl_unparse (tid)));
675 if (ctype_isFunction (deftype))
677 rettype = ctype_returnValue (deftype);
681 rettype = ctype_unknown;
685 ** check has been moved here...
688 if (ctype_isFunction (idDecl_getCtype (tid)))
690 ue = uentry_makeIdFunction (tid);
691 reflectSpecialCode (ue);
692 reflectArgsUsed (ue);
696 llparseerror (message ("Inconsistent function declaration: %q",
697 idDecl_unparse (tid)));
699 tid = idDecl_replaceCtype
700 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
701 ue = uentry_makeIdFunction (tid);
704 reflectStorageClass (ue);
706 uentry_checkParams (ue);
707 reflectModGlobs (ue);
709 ue = usymtab_supGlobalEntryReturn (ue);
710 context_enterFunction (ue);
711 enterFunctionParams (uentry_getParams (ue));
713 resetStorageClass ();
718 ** for now, no type checking
719 ** (must check later though!)
722 static /*@only@*/ uentry globalDeclareOldStyleFunction (idDecl tid)
727 ** check has been moved here...
730 if (cstring_equalLit (idDecl_observeId (tid), "main"))
732 context_setFlagTemp (FLG_MAINTYPE, FALSE);
735 ue = uentry_makeIdFunction (tid);
736 reflectStorageClass (ue);
737 reflectSpecialCode (ue);
738 reflectArgsUsed (ue);
739 uentry_setDefined (ue, g_currentloc);
741 uentry_checkParams (ue);
743 if (ProcessingGlobals)
745 uentry_setGlobals (ue, currentGlobals);
748 resetStorageClass ();
752 static void oldStyleDeclareFunction (/*@only@*/ uentry e)
754 uentryList params = saveParamList;
755 ctype rt = uentry_getType (e);
757 llassert (ctype_isFunction (rt));
759 e = usymtab_supGlobalEntryReturn (e);
761 context_enterFunction (e);
762 enterFunctionParams (params);
763 saveParamList = uentryList_undefined;
764 resetStorageClass ();
767 void declareFunction (idDecl tid) /*@globals undef saveFunction; @*/
771 DPRINTF (("Declare function: %s", idDecl_unparse (tid)));
773 if (ProcessingParams)
775 ue = globalDeclareOldStyleFunction (tid);
780 saveFunction = uentry_undefined;
782 if (context_inRealFunction ())
784 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
786 llparseerror (message ("Function declared inside function: %q",
787 idDecl_unparse (tid)));
789 context_quietExitFunction ();
790 ue = usymtab_supEntryReturn (ue);
794 if (context_inInnerScope ())
796 llparseerror (message ("Declaration in inner context: %q",
797 idDecl_unparse (tid)));
799 sRef_setGlobalScope ();
800 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
802 ue = usymtab_supGlobalEntryReturn (ue);
803 sRef_clearGlobalScope ();
807 ue = globalDeclareFunction (tid);
814 resetStorageClass ();
818 void declareStaticFunction (idDecl tid) /*@globals undef saveFunction; @*/
822 DPRINTF (("Declare static funciton: %s", idDecl_unparse (tid)));
824 if (ProcessingParams)
826 ue = globalDeclareOldStyleFunction (tid);
831 saveFunction = uentry_undefined;
833 if (context_inRealFunction ())
835 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
837 llparseerror (message ("Function declared inside function: %q",
838 idDecl_unparse (tid)));
840 context_quietExitFunction ();
841 ue = usymtab_supEntryReturn (ue);
845 if (context_inInnerScope ())
847 llparseerror (message ("Declaration in inner context: %q",
848 idDecl_unparse (tid)));
850 sRef_setGlobalScope ();
851 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
853 ue = usymtab_supGlobalEntryReturn (ue);
854 sRef_clearGlobalScope ();
858 ctype deftype = idDecl_getCtype (tid);
861 if (ctype_isFunction (deftype))
863 rettype = ctype_returnValue (deftype);
867 rettype = ctype_unknown;
871 ** check has been moved here...
874 if (ctype_isFunction (idDecl_getCtype (tid)))
876 ue = uentry_makeIdFunction (tid);
877 reflectSpecialCode (ue);
878 reflectArgsUsed (ue);
882 llparseerror (message ("Inconsistent function declaration: %q",
883 idDecl_unparse (tid)));
885 tid = idDecl_replaceCtype
886 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
887 ue = uentry_makeIdFunction (tid);
890 reflectStorageClass (ue);
891 uentry_setStatic (ue);
893 uentry_checkParams (ue);
894 reflectModGlobs (ue);
896 DPRINTF (("Sub global entry: %s", uentry_unparse (ue)));
897 ue = usymtab_supGlobalEntryReturn (ue);
899 context_enterFunction (ue);
900 enterFunctionParams (uentry_getParams (ue));
901 resetStorageClass ();
908 resetStorageClass ();
913 checkTypeDecl (uentry e, ctype rep)
915 cstring n = uentry_getName (e);
917 DPRINTF (("Check type decl: %s", n));
919 if (cstring_equal (context_getBoolName (), n))
921 ctype rrep = ctype_realType (rep);
924 ** for abstract enum types, we need to fix the enum members:
925 ** they should have the abstract type, not the rep type.
928 if (ctype_isEnum (ctype_realType (rrep)))
930 enumNameList el = ctype_elist (rrep);
932 enumNameList_elements (el, ye)
934 if (usymtab_existsGlob (ye))
936 uentry ue = usymtab_lookupSafe (ye);
937 uentry_setType (ue, ctype_bool);
940 if (cstring_equal (context_getTrueName (), ye)
941 || cstring_equal (context_getFalseName (), ye))
949 message ("Member of boolean enumerated type definition "
950 "does not match name set to represent TRUE "
953 message ("Use -boolfalse and -booltrue to set the "
954 "name of false and true boolean values."),
955 uentry_whereDefined (e));
957 } end_enumNameList_elements;
961 if (usymtab_exists (n))
963 usymId llm = usymtab_getId (n);
964 uentry le = usymtab_getTypeEntry (llm);
966 uentry_setDeclared (e, g_currentloc);
967 uentry_setSref (e, sRef_makeGlobal (llm, uentry_getType (le)));
969 DPRINTF (("Here we are: %s / %s",
970 n, context_getBoolName ()));
972 if (uentry_isAbstractDatatype (le))
974 ctype rrep = ctype_realType (rep);
977 ** for abstract enum types, we need to fix the enum members:
978 ** they should have the abstract type, not the rep type.
981 if (ctype_isEnum (ctype_realType (rrep)))
983 ctype at = uentry_getAbstractType (le);
984 enumNameList el = ctype_elist (rrep);
986 enumNameList_elements (el, ye)
988 if (usymtab_existsGlob (ye))
990 uentry ue = usymtab_lookupSafe (ye);
992 llassert (uentry_isEitherConstant (ue));
993 llassertprint (ctype_match (uentry_getType (ue), rrep),
994 ("Bad enum: %s / %s",
996 ctype_unparse (rrep)));
998 uentry_setType (ue, at);
1000 } end_enumNameList_elements;
1003 if (uentry_isMutableDatatype (le))
1005 /* maybe more complicated if abstract and immutable ? */
1007 if (!ctype_isRealPointer (rep) && !ctype_isRealAbstract (rep))
1011 message ("Mutable abstract type %s declared without pointer "
1012 "indirection: %s (violates assignment semantics)",
1013 n, ctype_unparse (rep)),
1014 uentry_whereDefined (e));
1016 uentry_setMutable (e);
1023 fileloc fl = uentry_whereDeclared (e);
1025 if (context_getFlag (FLG_LIKELYBOOL)
1026 && !context_getFlag (FLG_BOOLINT))
1028 if ((cstring_equalLit (n, "BOOL")
1029 || cstring_equalLit (n, "Bool")
1030 || cstring_equalLit (n, "bool")
1031 || cstring_equalLit (n, "boolean")
1032 || cstring_equalLit (n, "Boolean")
1033 || cstring_equalLit (n, "BOOLEAN"))
1034 && !(cstring_equal (n, context_getBoolName ())))
1036 if (context_setBoolName ()) {
1039 message ("Type %s is probably meant as a boolean type, but does "
1040 "not match the boolean type name \"%s\".",
1042 context_getBoolName ()),
1047 message ("Type %s is probably meant as a boolean type, "
1048 "but the boolean type name is not set. "
1049 "Use -booltype %s to set it.",
1056 if (!uentry_isStatic (e)
1057 && !ctype_isFunction (uentry_getType (e))
1058 && !fileloc_isLib (fl)
1059 && !fileloc_isImport (fl)
1060 && fileloc_isHeader (fl))
1062 voptgenerror (FLG_EXPORTTYPE,
1063 message ("Type exported, but not specified: %s\n", n),
1072 fixUentryList (idDeclList tl, qtype q)
1074 uentryList f = uentryList_new ();
1076 idDeclList_elements (tl, i)
1078 if (idDecl_isDefined (i))
1084 (void) idDecl_fixBase (i, q);
1087 ** implicit annotations
1090 (void) fixStructDecl (i);
1092 ue = uentry_makeIdVariable (i);
1093 rt = ctype_realType (uentry_getType (ue));
1096 ** where is this here???
1098 if (ctype_isArray (rt) || ctype_isSU (rt))
1100 sRef_setAllocated (uentry_getSref (ue), uentry_whereDefined (ue));
1106 if (uentry_isValid (old = uentryList_lookupField (f, uentry_rawName (ue))))
1108 if (optgenerror (FLG_SYNTAX,
1109 message ("Field name reused: %s", uentry_rawName (ue)),
1110 uentry_whereDefined (ue)))
1112 llgenmsg (message ("Previous use of %s", uentry_rawName (ue)),
1113 uentry_whereDefined (old));
1117 f = uentryList_add (f, ue);
1119 } end_idDeclList_elements;
1121 idDeclList_free (tl);
1126 ** This is a hack to support unnamed struct/union fields as done by
1127 ** Microsoft VC++. It is not supported by the ANSI standard.
1129 ** The inner fields are added to the outer structure. This is meaningful
1130 ** for nesting structs inside unions, but lclint does no related
1135 fixUnnamedDecl (qtype q)
1137 ctype ct = ctype_realType (qtype_getType (q));
1139 if (ctype_isStruct (ct) || ctype_isUnion (ct))
1141 uentryList res = ctype_getFields (ct);
1143 return (uentryList_copy (res));
1150 return uentryList_undefined;
1153 void setStorageClass (storageClassCode sc)
1159 setProcessingIterVars (uentry iter)
1161 ProcessingIterVars = TRUE;
1163 saveIterParamNo = 0;
1167 setProcessingGlobalsList ()
1169 ProcessingGlobals = TRUE;
1171 llassert (globSet_isUndefined (currentGlobals));
1172 currentGlobals = globSet_undefined;
1174 llassert (sRefSet_isUndefined (fcnModifies));
1175 fcnModifies = sRefSet_undefined;
1178 ** No, special clauses might have been processed first!
1179 llassert (specialClauses_isUndefined (specClauses));
1180 specClauses = specialClauses_undefined;
1183 fcnNoGlobals = FALSE;
1186 static bool ProcessingGlobMods = FALSE;
1189 setProcessingGlobMods ()
1191 ProcessingGlobMods = TRUE;
1195 clearProcessingGlobMods ()
1197 ProcessingGlobMods = FALSE;
1201 isProcessingGlobMods ()
1203 return (ProcessingGlobMods);
1206 static void resetGlobals (void)
1208 ProcessingGlobals = FALSE;
1209 currentGlobals = globSet_undefined;
1210 llassert (sRefSet_isUndefined (fcnModifies));
1211 fcnModifies = sRefSet_undefined;
1212 fcnNoGlobals = FALSE;
1216 unsetProcessingGlobals ()
1218 ProcessingGlobals = FALSE;
1222 setProcessingVars (/*@only@*/ qtype q)
1224 ProcessingVars = TRUE;
1225 qtype_free (processingType);
1230 setGenericParamList (/*@dependent@*/ uentryList pm)
1232 ProcessingParams = TRUE;
1237 setProcessingTypedef (/*@only@*/ qtype q)
1239 ProcessingTypedef = TRUE;
1241 qtype_free (processingType);
1246 unsetProcessingVars ()
1248 resetStorageClass ();
1249 ProcessingVars = FALSE;
1255 if (ProcessingParams)
1257 if (uentry_isInvalid (saveFunction))
1259 llbuglit ("unsetProcessingVars: no saved function\n");
1261 if (sRefSet_isDefined (fcnModifies)) {
1262 sRefSet_free (fcnModifies);
1263 fcnModifies = sRefSet_undefined;
1268 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1269 uentryList params = uentryList_copy (saveParamList);
1270 ctype ct2 = ctype_makeFunction (ct, params);
1272 uentry_setType (saveFunction, ct2);
1273 ProcessingParams = FALSE;
1275 reflectModGlobs (saveFunction);
1276 oldStyleDeclareFunction (saveFunction);
1277 saveFunction = uentry_undefined;
1284 ** If the paramlist used a type name, we could be here.
1287 llfatalerror (message ("%q: Old-style function parameter list uses a "
1288 "type name.", fileloc_unparse (g_currentloc)));
1295 if (uentry_isValid (saveFunction))
1298 ** old style declaration
1301 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1304 uentryList_elements (saveParamList, current)
1306 uentry_setType (current, ctype_int); /* all params are ints */
1307 } end_uentryList_elements;
1309 ct2 = ctype_makeParamsFunction (ct, uentryList_copy (saveParamList));
1311 uentry_setType (saveFunction, ct2);
1312 ProcessingParams = FALSE;
1314 oldStyleDeclareFunction (saveFunction);
1315 saveFunction = uentry_undefined;
1320 unsetProcessingTypedef ()
1322 ProcessingTypedef = FALSE;
1325 void checkConstant (qtype t, idDecl id)
1329 id = idDecl_fixBase (id, t);
1330 e = uentry_makeIdConstant (id);
1332 reflectStorageClass (e);
1333 resetStorageClass ();
1335 usymtab_supGlobalEntry (e);
1338 void checkValueConstant (qtype t, idDecl id, exprNode e)
1342 id = idDecl_fixBase (id, t);
1343 ue = uentry_makeIdConstant (id);
1344 reflectStorageClass (ue);
1345 resetStorageClass ();
1347 if (exprNode_isDefined (e))
1349 if (!exprNode_matchType (uentry_getType (ue), e))
1352 (exprNode_getType (e), e,
1353 uentry_getType (ue), exprNode_undefined,
1354 message ("Constant %q initialized to type %t, expects %t: %s",
1355 uentry_getName (ue),
1356 exprNode_getType (e),
1357 uentry_getType (ue),
1358 exprNode_unparse (e)),
1363 if (exprNode_hasValue (e))
1365 uentry_mergeConstantValue (ue, multiVal_copy (exprNode_getValue (e)));
1370 usymtab_supGlobalEntry (ue);
1374 void processNamedDecl (idDecl t)
1376 if (qtype_isUndefined (processingType))
1378 llparseerror (message ("No type before declaration name: %q", idDecl_unparse (t)));
1380 processingType = qtype_create (ctype_unknown);
1383 t = idDecl_fixBase (t, processingType);
1385 DPRINTF (("Declare: %s", idDecl_unparse (t)));
1387 if (ProcessingGlobals)
1389 cstring id = idDecl_getName (t);
1390 uentry ue = usymtab_lookupSafe (id);
1392 if (!uentry_isValid (ue))
1394 llerror (FLG_UNRECOG,
1395 message ("Variable used in globals list is undeclared: %s", id));
1399 if (!ctype_match (uentry_getType (ue), idDecl_getCtype (t)))
1403 message ("Variable %s used in globals list declared %s, "
1405 id, ctype_unparse (uentry_getType (ue)),
1406 ctype_unparse (idDecl_getCtype (t))),
1411 sRef sr = sRef_copy (uentry_getSref (ue));
1413 reflectGlobalQualifiers (sr, idDecl_getQuals (t));
1415 currentGlobals = globSet_insert (currentGlobals, sr);
1419 else if (ProcessingVars)
1424 ct = ctype_realType (idDecl_getCtype (t));
1426 if (ProcessingParams)
1428 cstring id = idDecl_getName (t);
1429 int paramno = uentryList_lookupRealName (saveParamList, id);
1433 uentry cparam = uentryList_getN (saveParamList, paramno);
1435 uentry_setType (cparam, idDecl_getCtype (t));
1436 uentry_reflectQualifiers (cparam, idDecl_getQuals (t));
1437 uentry_setDeclaredOnly (cparam, context_getSaveLocation ());
1442 (message ("Old style declaration uses unlisted parameter: %s",
1450 if (context_inIterDef ())
1452 cstring pname = makeParam (idDecl_observeId (t));
1453 uentry p = usymtab_lookupSafe (pname);
1455 cstring_free (pname);
1457 if (uentry_isYield (p))
1459 e = uentry_makeParam (t, sRef_getParam (uentry_getSref (p)));
1461 uentry_checkYieldParam (p, e);
1463 usymtab_supEntrySref (e);
1468 if ((hasSpecialCode () || argsUsed)
1469 && ctype_isFunction (idDecl_getCtype (t)))
1471 e = uentry_makeIdFunction (t);
1472 reflectSpecialCode (e);
1473 reflectArgsUsed (e);
1477 e = uentry_makeIdVariable (t);
1480 loc = uentry_whereDeclared (e);
1483 if (context_inGlobalScope ())
1485 uentry_checkParams was here!
1489 if (ctype_isFunction (uentry_getType (e)))
1491 reflectModGlobs (e);
1495 llassert (!globSet_isDefined (currentGlobals)
1496 && !sRefSet_isDefined (fcnModifies));
1499 e = usymtab_supEntrySrefReturn (e);
1501 if (uentry_isExtern (e) && !context_inGlobalScope ())
1505 message ("Declaration using extern inside function scope: %q",
1506 uentry_unparse (e)),
1509 uentry_setDefined (e, fileloc_getExternal ());
1510 sRef_setDefined (uentry_getSref (e), fileloc_getExternal ());
1513 if (uentry_isFunction (e))
1515 uentry_checkParams (e);
1516 checkParamNames (e);
1519 if (uentry_isVar (e)
1520 && uentry_isCheckedUnknown (e))
1522 sRef sr = uentry_getSref (e);
1524 if (sRef_isLocalVar (sr))
1526 if (context_getFlag (FLG_IMPCHECKMODINTERNALS))
1528 uentry_setCheckMod (e);
1532 uentry_setUnchecked (e);
1535 else if (sRef_isFileStatic (sr))
1537 if (context_getFlag (FLG_IMPCHECKEDSTRICTSTATICS))
1539 uentry_setCheckedStrict (e);
1541 else if (context_getFlag (FLG_IMPCHECKEDSTATICS))
1543 uentry_setChecked (e);
1545 else if (context_getFlag (FLG_IMPCHECKMODSTATICS))
1547 uentry_setCheckMod (e);
1554 else /* real global */
1556 llassert (sRef_isRealGlobal (sr));
1558 if (context_getFlag (FLG_IMPCHECKEDSTRICTGLOBALS))
1560 uentry_setCheckedStrict (e);
1562 else if (context_getFlag (FLG_IMPCHECKEDGLOBALS))
1564 uentry_setChecked (e);
1566 else if (context_getFlag (FLG_IMPCHECKMODGLOBALS))
1568 uentry_setCheckMod (e);
1578 else if (ProcessingTypedef)
1580 ctype ct = idDecl_getCtype (t);
1583 DPRINTF (("Processing typedef: %s", ctype_unparse (ct)));
1585 e = uentry_makeIdDatatype (t);
1587 if (cstring_equal (idDecl_getName (t), context_getBoolName ())) {
1588 ctype rt = ctype_realType (ct);
1590 if (ctype_isEnum (rt)) {
1593 if (!(ctype_isInt (rt)
1594 || ctype_isUnknown (rt)
1595 || ctype_isChar (rt))) {
1598 message ("Boolean type %s defined using non-standard type %s (integral, char or enum type expected)",
1599 context_getBoolName (),
1600 ctype_unparse (ct)),
1601 uentry_whereLast (e));
1605 uentry_setType (e, ct);
1609 reflectStorageClass (e);
1610 checkTypeDecl (e, ct);
1612 e = usymtab_supReturnTypeEntry (e);
1614 if (uentry_isMaybeAbstract (e))
1616 if (context_getFlag (FLG_IMPABSTRACT))
1618 uentry_setAbstract (e);
1622 uentry_setConcrete (e);
1628 llparseerror (message ("Suspect missing struct or union keyword: %q",
1629 idDecl_unparse (t)));
1635 ** moved from grammar
1638 static idDecl fixStructDecl (/*@returned@*/ idDecl d)
1640 if (ctype_isVisiblySharable (idDecl_getCtype (d))
1641 && context_getFlag (FLG_STRUCTIMPONLY))
1643 if (!qualList_hasAliasQualifier (idDecl_getQuals (d)))
1645 if (qualList_hasExposureQualifier (idDecl_getQuals (d)))
1647 idDecl_addQual (d, qual_createDependent ());
1651 idDecl_addQual (d, qual_createImpOnly ());
1660 declareUnnamedStruct (/*@only@*/ uentryList f)
1662 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1664 int num = uentryList_size (f);
1665 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1670 (FLG_NUMSTRUCTFIELDS,
1671 message ("Structure declared with %d fields "
1672 "(limit is set to %d)",
1678 return (ctype_createUnnamedStruct (f));
1682 declareUnnamedUnion (/*@only@*/ uentryList f)
1684 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1686 int num = uentryList_size (f);
1687 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1692 (FLG_NUMSTRUCTFIELDS,
1693 message ("Union declared with %d fields "
1694 "(limit is set to %d)",
1700 return (ctype_createUnnamedUnion (f));
1703 ctype declareStruct (cstring id, /*@only@*/ uentryList f)
1707 int num = uentryList_size (f);
1709 ct = ctype_createStruct (cstring_copy (id), f);
1710 DPRINTF (("Declare struct: %s [%d]", ctype_unparse (ct), ct));
1711 ue = uentry_makeStructTagLoc (id, ct);
1713 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1715 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1720 (FLG_NUMSTRUCTFIELDS,
1721 message ("Structure %q declared with %d fields "
1722 "(limit is set to %d)",
1723 uentry_getName (ue), num, max),
1724 uentry_whereLast (ue));
1728 return (usymtab_supTypeEntry (ue));
1731 ctype declareUnion (cstring id, uentryList f)
1735 int num = uentryList_size (f);
1737 ct = ctype_createUnion (cstring_copy (id), f);
1738 ue = uentry_makeUnionTagLoc (id, ct);
1740 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1742 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1747 (FLG_NUMSTRUCTFIELDS,
1748 message ("Union %q declared with %d fields "
1749 "(limit is set to %d)",
1750 uentry_getName (ue), num, max),
1751 uentry_whereLast (ue));
1755 return (usymtab_supTypeEntry (ue));
1758 ctype handleStruct (/*@only@*/ cstring id)
1760 if (usymtab_existsStructTag (id))
1762 ctype ct = uentry_getAbstractType (usymtab_lookupStructTag (id));
1769 return (ctype_createForwardStruct (id));
1773 ctype handleUnion (/*@only@*/ cstring id)
1775 if (usymtab_existsUnionTag (id))
1777 ctype ret = uentry_getAbstractType (usymtab_lookupUnionTag (id));
1783 return (ctype_createForwardUnion (id));
1788 handleEnum (cstring id)
1790 if (usymtab_existsEnumTag (id))
1792 ctype ret = uentry_getAbstractType (usymtab_lookupEnumTag (id));
1798 return (declareEnum (id, enumNameList_new ()));
1802 bool processingIterVars (void)
1804 return ProcessingIterVars;
1807 uentry getCurrentIter (void)
1812 static bool flipOldStyle = FALSE;
1813 static bool flipNewStyle = TRUE;
1815 void setFlipOldStyle () { flipOldStyle = TRUE; }
1816 bool isFlipOldStyle () { return flipOldStyle; }
1817 bool isNewStyle () { return flipNewStyle; }
1818 void setNewStyle () { flipNewStyle = TRUE; }
1820 /*@dependent@*/ uentryList handleParamIdList (/*@dependent@*/ uentryList params)
1825 ** this is a really YUCKY hack to handle old style
1829 voptgenerror (FLG_OLDSTYLE,
1830 cstring_makeLiteral ("Old style function declaration"),
1833 uentryList_elements (params, current)
1835 uentry_setParam (current);
1836 uentry_setSref (current, sRef_makeParam (paramno, ctype_unknown));
1838 } end_uentryList_elements;
1840 setGenericParamList (params);
1841 g_expectingTypeName = TRUE;
1846 /*@dependent@*/ uentryList handleParamTypeList (/*@returned@*/ uentryList params)
1850 uentryList_fixMissingNames (params);
1852 voptgenerror (FLG_OLDSTYLE,
1853 cstring_makeLiteral ("Old style function declaration."),
1856 setGenericParamList (params);
1857 flipOldStyle = FALSE;
1858 g_expectingTypeName = TRUE;
1867 ctype c = ctype_unknown;
1868 cstring id = cstring_makeLiteral ("va_alist");
1871 if (ProcessingParams)
1873 int i = uentryList_lookupRealName (saveParamList, id);
1877 e = uentry_makeVariableSrefParam (id, c, sRef_makeParam (i, c));
1881 e = uentry_undefined; /* suppress gcc message */
1882 llfatalerrorLoc (cstring_makeLiteral ("va_dcl used without va_alist"));
1887 llerror (FLG_SYNTAX, cstring_makeLiteral ("va_dcl used outside of function declaration"));
1888 e = uentry_makeVariableLoc (id, c);
1892 uentry_setUsed (e, g_currentloc);
1893 usymtab_supEntrySref (e);
1896 /*@exposed@*/ sRef modListPointer (sRef s)
1898 ctype ct = sRef_getType (s);
1899 ctype rt = ctype_realType (ct);
1901 if (ctype_isAP (rt))
1903 if (context_inHeader () && ctype_isAbstract (ct))
1908 ("Modifies clause in header file dereferences abstract "
1909 "type %s (interface modifies clause should not depend "
1910 "on or expose type representation): %q",
1916 return (sRef_constructPointer (s));
1920 if (ctype_isKnown (rt))
1924 message ("Implementation modifies clause dereferences non-pointer (type %s): %q",
1934 /*@exposed@*/ sRef modListFieldAccess (sRef s, cstring f)
1936 ctype ct = sRef_getType (s);
1937 ctype rt = ctype_realType (ct);
1939 if (ctype_isStructorUnion (rt))
1941 uentry tf = uentryList_lookupField (ctype_getFields (rt), f);
1943 if (uentry_isUndefined (tf))
1945 voptgenerror (FLG_TYPE,
1946 message ("Modifies list accesses non-existent "
1947 "field %s of %t: %q", f, ct,
1952 return sRef_undefined;
1956 if (ctype_isAbstract (ct) && context_inHeader ())
1961 ("Modifies clause in header file accesses abstract "
1962 "type %s (interface modifies clause should not depend "
1963 "on or expose type representation): %q",
1970 cstring_markOwned (f);
1971 return (sRef_makeField (s, f));
1977 message ("Modifies clause dereferences non-pointer (type %s): %q",
1987 sRef globListUnrecognized (cstring s)
1989 if (cstring_equalLit (s, "nothing"))
1991 return sRef_makeNothing ();
1993 else if (cstring_equalLit (s, "internalState"))
1995 return sRef_makeInternalState ();
1997 else if (cstring_equalLit (s, "fileSystem")
1998 || cstring_equalLit (s, "systemState"))
2000 return sRef_makeSystemState ();
2006 message ("Unrecognized identifier in globals list: %s", s),
2009 return sRef_undefined;
2013 /*@exposed@*/ sRef modListArrowAccess (sRef s, cstring f)
2015 ctype ct = sRef_getType (s);
2016 ctype rt = ctype_realType (ct);
2018 if (ctype_isRealPointer (rt))
2020 ctype b = ctype_baseArrayPtr (rt);
2021 ctype rb = ctype_realType (b);
2023 if (ctype_isStructorUnion (rb))
2025 uentry tf = uentryList_lookupField (ctype_getFields (rb), f);
2027 if (uentry_isUndefined (tf))
2029 voptgenerror (FLG_TYPE,
2030 message ("Modifies list arrow accesses non-existent "
2031 "field %s of %t: %q", f, b,
2036 return sRef_undefined;
2040 if (context_inHeader ())
2042 if (ctype_isAbstract (b))
2047 ("Modifies clause in header file arrow accesses abstract "
2048 "type %s (interface modifies clause should not depend "
2049 "on or expose type representation): %q",
2057 if (ctype_isAbstract (ct))
2062 ("Modifies clause in header file arrow accesses abstract "
2063 "type %s (interface modifies clause should not depend "
2064 "on or expose type representation): %q",
2072 cstring_markOwned (f);
2073 return (sRef_makeArrow (s, f));
2079 message ("Modifies clause arrow accesses pointer to "
2080 "non-structure (type %s): %q",
2090 message ("Modifies clause arrow accesses non-pointer (type %s): %q",
2100 sRef checkSpecClausesId (uentry ue)
2102 cstring s = uentry_rawName (ue);
2104 if (sRef_isGlobal (uentry_getSref (ue)))
2108 message ("Global variable %s used special clause. (Global variables "
2109 "are not recognized in special clauses. If there is "
2110 "sufficient interest in support for this, it may be "
2111 "added to a future release. Send mail to "
2112 "lclint@cs.virginia.edu.)",
2116 return sRef_undefined;
2120 if (cstring_equalLit (s, "result"))
2124 message ("Special clause list uses %s which is a variable and has special "
2125 "meaning in a modifies list. (Special meaning assumed.)", s),
2128 uentry_showWhereDeclared (ue);
2132 return uentry_getSref (ue);
2138 based on checkSpecClausesId
2141 sRef checkbufferConstraintClausesId (uentry ue)
2143 cstring s = uentry_rawName (ue);
2144 if (cstring_equalLit (s, "result"))
2148 message ("Special clause list uses %s which is a variable and has special "
2149 "meaning in a modifies list. (Special meaning assumed.)", s),
2152 uentry_showWhereDeclared (ue);
2156 return uentry_getSref (ue);
2159 void checkModifiesId (uentry ue)
2161 cstring s = uentry_rawName (ue);
2163 if (cstring_equalLit (s, "nothing")
2164 || cstring_equalLit (s, "internalState")
2165 || cstring_equalLit (s, "systemState")
2166 || (cstring_equalLit (s, "fileSystem")))
2170 message ("Modifies list uses %s which is a variable and has special "
2171 "meaning in a modifies list. (Special meaning assumed.)", s),
2174 uentry_showWhereDeclared (ue);
2179 /*@exposed@*/ sRef fixModifiesId (cstring s)
2182 cstring pname = makeParam (s);
2183 uentry ue = usymtab_lookupSafe (pname);
2185 cstring_free (pname);
2187 if (cstring_equalLit (s, "nothing"))
2189 ret = sRef_makeNothing ();
2191 else if (cstring_equalLit (s, "internalState"))
2193 ret = sRef_makeInternalState ();
2195 else if (cstring_equalLit (s, "fileSystem")
2196 || cstring_equalLit (s, "systemState"))
2198 ret = sRef_makeSystemState ();
2202 ret = sRef_undefined;
2205 if (sRef_isValid (ret))
2207 if (uentry_isValid (ue))
2211 message ("Modifies list uses %s which is a parameter and has special "
2212 "meaning in a modifies list. (Special meaning assumed.)", s),
2218 if (uentry_isValid (ue))
2220 ret = uentry_getSref (ue);
2224 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2225 ret = sRef_undefined;
2229 message ("Unrecognized identifier in modifies comment: %s", s),
2239 sRef fixSpecClausesId (cstring s)
2242 cstring pname = makeParam (s);
2243 uentry ue = usymtab_lookupSafe (pname);
2245 cstring_free (pname);
2247 if (cstring_equalLit (s, "result"))
2249 ret = sRef_makeResult ();
2253 ret = sRef_undefined;
2256 if (sRef_isValid (ret))
2258 if (uentry_isValid (ue))
2262 message ("Special clause uses %s which is a parameter and has special "
2263 "meaning in a special clause. (Special meaning assumed.)", s),
2269 if (uentry_isValid (ue))
2271 ret = uentry_getSref (ue);
2273 if (sRef_isGlobal (ret))
2277 message ("Global variable %s used special clause. (Global variables "
2278 "are not recognized in special clauses. If there is "
2279 "sufficient interest in support for this, it may be "
2280 "added to a future release. Send mail to "
2281 "lclint@cs.virginia.edu.)",
2285 ret = sRef_undefined;
2290 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2291 ret = sRef_undefined;
2295 message ("Unrecognized identifier in special clause: %s", s),
2305 sRef modListArrayFetch (sRef s, /*@unused@*/ sRef mexp)
2307 ctype ct = sRef_getType (s);
2308 ctype rt = ctype_realType (ct);
2310 if (ctype_isAP (rt))
2312 if (context_inHeader () && ctype_isAbstract (ct))
2317 ("Modifies clause in header file indexes abstract "
2318 "type %s (interface modifies clause should not depend "
2319 "on or expose type representation): %q",
2325 return (sRef_makeAnyArrayFetch (s));
2332 ("Implementation modifies clause uses array fetch on non-array (type %s): %q",
2333 ctype_unparse (ct), sRef_unparse (s)),