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 fcnPreConditions = NULL;
60 static /*@only@*/ sRefSet fcnModifies = sRefSet_undefined;
61 static /*@only@*/ /*@null@*/ specialClauses specClauses = specialClauses_undefined;
62 static bool fcnNoGlobals = FALSE;
63 static bool ProcessingVars = FALSE;
64 static bool ProcessingParams = FALSE;
65 static bool ProcessingGlobals = FALSE;
66 static bool ProcessingTypedef = FALSE;
67 static bool ProcessingIterVars = FALSE;
68 static /*@only@*/ qtype processingType = qtype_undefined;
69 static uentry currentIter = uentry_undefined;
70 static globSet currentGlobals = globSet_undefined;
71 static /*@dependent@*/ uentryList saveParamList; /* for old style functions */
72 static /*@owned@*/ uentry saveFunction = uentry_undefined;
73 static int saveIterParamNo;
74 static idDecl fixStructDecl (/*@returned@*/ idDecl p_d);
75 static void checkTypeDecl (uentry p_e, ctype p_rep);
76 static /*@dependent@*/ fileloc saveStoreLoc = fileloc_undefined;
77 static storageClassCode storageClass = SCNONE;
78 static void declareEnumList (/*@temp@*/ enumNameList p_el, ctype p_c, fileloc p_loc);
79 static void resetGlobals (void);
80 static qual specialFunctionCode = QU_UNKNOWN;
81 static bool argsUsed = FALSE;
83 static bool hasSpecialCode (void)
85 return (specialFunctionCode != QU_UNKNOWN);
88 extern void setArgsUsed (void)
94 cstring_makeLiteral ("Multiple ARGSUSED comments for one function"),
101 static void reflectArgsUsed (uentry ue)
105 if (uentry_isFunction (ue))
107 uentryList params = uentry_getParams (ue);
109 uentryList_elements (params, el)
111 uentry_setUsed (el, fileloc_undefined);
112 } end_uentryList_elements ;
119 extern void setSpecialFunction (qual qu)
121 if (specialFunctionCode != QU_UNKNOWN)
123 voptgenerror (FLG_SYNTAX,
124 message ("Multiple special function codes: %s, %s "
125 "(first code is ignored)",
126 qual_unparse (specialFunctionCode),
131 specialFunctionCode = qu;
134 static void reflectSpecialCode (uentry ue)
136 switch (specialFunctionCode)
138 case QU_UNKNOWN: break;
140 uentry_setPrintfLike (ue);
143 uentry_setScanfLike (ue);
146 uentry_setMessageLike (ue);
151 specialFunctionCode = QU_UNKNOWN;
154 static void resetStorageClass (void)
156 qtype_free (processingType);
157 processingType = qtype_undefined;
158 storageClass = SCNONE;
161 static void reflectModGlobs (uentry ue)
165 llassert (globSet_isUndefined (currentGlobals));
167 uentry_setGlobals (ue, globSet_undefined);
168 fcnNoGlobals = FALSE;
170 else if (globSet_isDefined (currentGlobals))
172 uentry_setGlobals (ue, currentGlobals);
173 currentGlobals = globSet_undefined;
180 if (sRefSet_isDefined (fcnModifies))
182 uentry_setModifies (ue, fcnModifies);
183 fcnModifies = sRefSet_undefined;
188 uentry_setPreconditions (ue, fcnConstraints);
189 fcnConstraints = constraintList_undefined;
192 if (uentry_isFunction (ue))
194 uentry_setSpecialClauses (ue, specClauses);
196 DPRINTF (("Done with spec clauses"));
200 static void reflectStorageClass (uentry u)
202 if (storageClass == SCSTATIC)
204 uentry_setStatic (u);
206 else if (storageClass == SCEXTERN)
208 uentry_setExtern (u);
212 ; /* no storage class */
219 saveStoreLoc = g_currentloc;
222 void setFunctionNoGlobals (void)
224 llassert (globSet_isUndefined (currentGlobals));
229 setFunctionStateSpecialClause (lltok stok, specialClauseKind kind,
231 /*@unused@*/ lltok etok)
233 int tok = lltok_getTok (stok);
238 specClauses = specialClauses_add (specClauses,
239 specialClause_create (TK_BEFORE, kind, s));
242 specClauses = specialClauses_add (specClauses,
243 specialClause_create (TK_AFTER, kind, s));
250 DPRINTF (("Added to specclauses: %s", specialClauses_unparse (specClauses)));
253 void setFunctionSpecialClause (lltok stok, sRefSet s,
254 /*@unused@*/ lltok etok)
256 int tok = lltok_getTok (stok);
261 specClauses = specialClauses_add (specClauses, specialClause_createUses (s));
264 specClauses = specialClauses_add (specClauses, specialClause_createDefines (s));
267 specClauses = specialClauses_add (specClauses, specialClause_createAllocates (s));
270 specClauses = specialClauses_add (specClauses, specialClause_createSets (s));
273 specClauses = specialClauses_add (specClauses, specialClause_createReleases (s));
280 DPRINTF (("Added to specclauses: %s", specialClauses_unparse (specClauses)));
283 constraintList getFunctionConstraints (void)
285 return constraintList_copy (fcnConstraints);
288 void setFunctionConstraints (constraintList c)
291 fcnConstraints = constraintList_copy (c);
294 void setFunctionModifies (sRefSet s)
296 sRefSet_free (fcnModifies);
300 static void reflectGlobalQualifiers (sRef sr, qualList quals)
302 qualList_elements (quals, qel)
304 if (qual_isGlobalQual (qel)) /* undef, killed */
306 sstate oldstate = sRef_getDefState (sr);
307 sstate defstate = sstate_fromQual (qel);
309 if ((oldstate == SS_UNDEFGLOB && defstate == SS_KILLED)
310 || (oldstate == SS_KILLED && defstate == SS_UNDEFGLOB))
312 defstate = SS_UNDEFKILLED;
319 sRef_setDefState (sr, defstate, fileloc_undefined);
321 else if (qual_isAllocQual (qel)) /* out, partial, reldef, etc. */
323 ctype realType = sRef_getType (sr);
324 sstate defstate = sstate_fromQual (qel);
326 if (qual_isRelDef (qel))
328 ; /* okay anywhere */
332 if (!ctype_isAP (realType)
333 && !ctype_isSU (realType)
334 && !ctype_isUnknown (realType)
335 && !ctype_isAbstract (sRef_getType (sr)))
339 message ("Qualifier %s used on non-pointer or struct: %q",
340 qual_unparse (qel), sRef_unparse (sr)));
345 sRef_setDefState (sr, defstate, fileloc_undefined);
347 else if (qual_isNull (qel))
349 sRef_setNullState (sr, NS_POSNULL, fileloc_undefined);
351 else if (qual_isRelNull (qel))
353 sRef_setNullState (sr, NS_RELNULL, fileloc_undefined);
355 else if (qual_isNotNull (qel))
357 sRef_setNullState (sr, NS_MNOTNULL, fileloc_undefined);
361 if (qual_isCQual (qel))
368 message ("Qualifier %s cannot be used in a globals list",
369 qual_unparse (qel)));
372 } end_qualList_elements;
375 void globListAdd (sRef sr, qualList quals)
377 if (sRef_isValid (sr))
379 sRef sc = sRef_copy (sr);
381 reflectGlobalQualifiers (sc, quals);
382 currentGlobals = globSet_insert (currentGlobals, sc);
386 extern void declareCIter (cstring name, /*@owned@*/ uentryList params)
390 ue = uentry_makeIter (name,
391 ctype_makeFunction (ctype_void, params),
392 fileloc_copy (g_currentloc));
394 usymtab_supEntry (uentry_makeEndIter (name, fileloc_copy (g_currentloc)));
396 reflectModGlobs (ue);
398 ue = usymtab_supGlobalEntryReturn (ue);
401 extern void nextIterParam (void)
403 llassert (ProcessingIterVars);
407 extern int iterParamNo (void)
409 llassert (ProcessingIterVars);
410 return saveIterParamNo;
414 ** yucky hacks to put it in the right place
418 makeCurrentParam (idDecl t)
422 saveStoreLoc = fileloc_undefined;
424 /* param number unknown */
426 ue = uentry_makeParam (t, 0);
431 declareUnnamedEnum (enumNameList el)
433 ctype ret = usymtab_enumEnumNameListType (el);
437 if (ctype_isDefined (ret))
440 e = uentry_makeEnumTagLoc (ctype_enumTag (rt), ret);
442 reflectStorageClass (e);
443 usymtab_supGlobalEntry (e);
445 declareEnumList (el, ret, g_currentloc);
446 enumNameList_free (el);
450 ctype ct = ctype_createEnum (fakeTag (), el);
452 e = uentry_makeEnumTagLoc (ctype_enumTag (ctype_realType (ct)), ct);
453 reflectStorageClass (e);
455 e = usymtab_supGlobalEntryReturn (e);
456 rt = uentry_getAbstractType (e);
457 declareEnumList (el, ct, g_currentloc);
464 declareEnum (cstring ename, enumNameList el)
469 llassert (cstring_isDefined (ename));
471 cet = ctype_createEnum (ename, el);
472 e = uentry_makeEnumTagLoc (ename, cet);
473 reflectStorageClass (e);
474 e = usymtab_supGlobalEntryReturn (e);
475 cet = uentry_getType (e);
476 declareEnumList (el, cet, uentry_whereLast (e));
477 return (uentry_getAbstractType (e));
481 declareEnumList (enumNameList el, ctype c, fileloc loc)
483 bool boolnames = FALSE;
484 bool othernames = FALSE;
486 (void) context_getSaveLocation (); /* undefine it */
488 if (context_maybeSet (FLG_NUMENUMMEMBERS))
490 int maxnum = context_getValue (FLG_NUMENUMMEMBERS);
491 int num = enumNameList_size (el);
497 message ("Enumerator %s declared with %d members (limit is set to %d)",
498 ctype_unparse (c), num, maxnum),
503 enumNameList_elements (el, e)
505 uentry ue = usymtab_lookupExposeGlob (e);
506 ctype ct = uentry_getType (ue);
508 llassert (uentry_isEnumConstant (ue));
510 if (ctype_isUnknown (ct))
512 uentry_setType (ue, c);
516 if (cstring_equal (e, context_getFalseName ())
517 || cstring_equal (e, context_getTrueName ()))
523 message ("Enumerator mixes boolean name (%s) with "
526 uentry_whereLast (ue)))
533 uentry_setType (ue, ctype_bool);
534 DPRINTF (("Set type: %s / %s",
535 uentry_unparse (ue), ctype_unparse (ctype_bool)));
543 message ("Enumerator mixes boolean names (%s, %s) with "
544 "non-boolean name: %s",
545 context_getTrueName (),
546 context_getFalseName (),
548 uentry_whereLast (ue)))
557 if (!ctype_match (c, ct))
559 if (ctype_isDirectBool (ct))
561 if (cstring_equal (e, context_getFalseName ())
562 || cstring_equal (e, context_getTrueName ()))
564 DPRINTF (("Here we are!"));
570 message ("Enumerator member %s declared with "
571 "inconsistent type: %s",
572 e, ctype_unparse (c)),
573 uentry_whereLast (ue)))
575 uentry_showWhereSpecifiedExtra
576 (ue, cstring_copy (ctype_unparse (ct)));
584 message ("Enumerator member %s declared with "
585 "inconsistent type: %s",
586 e, ctype_unparse (c)),
587 uentry_whereLast (ue)))
589 uentry_showWhereSpecifiedExtra
590 (ue, cstring_copy (ctype_unparse (ct)));
595 } end_enumNameList_elements;
598 static /*@dependent@*/ uentryList currentParamList;
600 void setCurrentParams (/*@dependent@*/ uentryList ue)
602 currentParamList = ue;
605 void clearCurrentParams (void)
607 currentParamList = uentryList_undefined;
611 ** requires: uentry_isFunction (e)
612 ** parameter names for current function are in currentParamList
615 static void enterFunctionParams (uentryList params)
619 uentryList_elements (params, current)
621 if (uentry_hasName (current))
623 uentry_setParamNo (current, paramno);
624 usymtab_supEntry (uentry_copy (current));
628 } end_uentryList_elements;
632 extern void enterParamsTemp (void)
634 usymtab_enterScope ();
635 enterFunctionParams (currentParamList);
638 extern void exitParamsTemp (void)
640 usymtab_quietPlainExitScope ();
643 static /*@exposed@*/ uentry globalDeclareFunction (idDecl tid)
645 ctype deftype = idDecl_getCtype (tid);
649 DPRINTF (("Global function: %s", idDecl_unparse (tid)));
651 if (ctype_isFunction (deftype))
653 rettype = ctype_returnValue (deftype);
657 rettype = ctype_unknown;
661 ** check has been moved here...
664 if (ctype_isFunction (idDecl_getCtype (tid)))
666 ue = uentry_makeIdFunction (tid);
667 reflectSpecialCode (ue);
668 reflectArgsUsed (ue);
672 llparseerror (message ("Inconsistent function declaration: %q",
673 idDecl_unparse (tid)));
675 tid = idDecl_replaceCtype
676 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
677 ue = uentry_makeIdFunction (tid);
680 reflectStorageClass (ue);
682 uentry_checkParams (ue);
683 reflectModGlobs (ue);
685 ue = usymtab_supGlobalEntryReturn (ue);
686 context_enterFunction (ue);
687 enterFunctionParams (uentry_getParams (ue));
689 resetStorageClass ();
694 ** for now, no type checking
695 ** (must check later though!)
698 static /*@only@*/ uentry globalDeclareOldStyleFunction (idDecl tid)
703 ** check has been moved here...
706 if (cstring_equalLit (idDecl_observeId (tid), "main"))
708 context_setFlagTemp (FLG_MAINTYPE, FALSE);
711 ue = uentry_makeIdFunction (tid);
712 reflectStorageClass (ue);
713 reflectSpecialCode (ue);
714 reflectArgsUsed (ue);
715 uentry_setDefined (ue, g_currentloc);
717 uentry_checkParams (ue);
719 if (ProcessingGlobals)
721 uentry_setGlobals (ue, currentGlobals);
724 resetStorageClass ();
728 static void oldStyleDeclareFunction (/*@only@*/ uentry e)
730 uentryList params = saveParamList;
731 ctype rt = uentry_getType (e);
733 llassert (ctype_isFunction (rt));
735 e = usymtab_supGlobalEntryReturn (e);
737 context_enterFunction (e);
738 enterFunctionParams (params);
739 saveParamList = uentryList_undefined;
740 resetStorageClass ();
743 void declareFunction (idDecl tid) /*@globals undef saveFunction; @*/
747 DPRINTF (("Declare function: %s", idDecl_unparse (tid)));
749 if (ProcessingParams)
751 ue = globalDeclareOldStyleFunction (tid);
756 saveFunction = uentry_undefined;
758 if (context_inRealFunction ())
760 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
762 llparseerror (message ("Function declared inside function: %q",
763 idDecl_unparse (tid)));
765 context_quietExitFunction ();
766 ue = usymtab_supEntryReturn (ue);
770 if (context_inInnerScope ())
772 llparseerror (message ("Declaration in inner context: %q",
773 idDecl_unparse (tid)));
775 sRef_setGlobalScope ();
776 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
778 ue = usymtab_supGlobalEntryReturn (ue);
779 sRef_clearGlobalScope ();
783 ue = globalDeclareFunction (tid);
790 resetStorageClass ();
794 void declareStaticFunction (idDecl tid) /*@globals undef saveFunction; @*/
798 DPRINTF (("Declare static funciton: %s", idDecl_unparse (tid)));
800 if (ProcessingParams)
802 ue = globalDeclareOldStyleFunction (tid);
807 saveFunction = uentry_undefined;
809 if (context_inRealFunction ())
811 ue = uentry_makeVariableLoc (idDecl_observeId (tid), ctype_unknown);
813 llparseerror (message ("Function declared inside function: %q",
814 idDecl_unparse (tid)));
816 context_quietExitFunction ();
817 ue = usymtab_supEntryReturn (ue);
821 if (context_inInnerScope ())
823 llparseerror (message ("Declaration in inner context: %q",
824 idDecl_unparse (tid)));
826 sRef_setGlobalScope ();
827 ue = uentry_makeVariableLoc (idDecl_observeId (tid),
829 ue = usymtab_supGlobalEntryReturn (ue);
830 sRef_clearGlobalScope ();
834 ctype deftype = idDecl_getCtype (tid);
837 if (ctype_isFunction (deftype))
839 rettype = ctype_returnValue (deftype);
843 rettype = ctype_unknown;
847 ** check has been moved here...
850 if (ctype_isFunction (idDecl_getCtype (tid)))
852 ue = uentry_makeIdFunction (tid);
853 reflectSpecialCode (ue);
854 reflectArgsUsed (ue);
858 llparseerror (message ("Inconsistent function declaration: %q",
859 idDecl_unparse (tid)));
861 tid = idDecl_replaceCtype
862 (tid, ctype_makeFunction (ctype_unknown, uentryList_undefined));
863 ue = uentry_makeIdFunction (tid);
866 reflectStorageClass (ue);
867 uentry_setStatic (ue);
869 uentry_checkParams (ue);
870 reflectModGlobs (ue);
872 DPRINTF (("Sub global entry: %s", uentry_unparse (ue)));
873 ue = usymtab_supGlobalEntryReturn (ue);
875 context_enterFunction (ue);
876 enterFunctionParams (uentry_getParams (ue));
877 resetStorageClass ();
884 resetStorageClass ();
889 checkTypeDecl (uentry e, ctype rep)
891 cstring n = uentry_getName (e);
893 DPRINTF (("Check type decl: %s", n));
895 if (cstring_equal (context_getBoolName (), n))
897 ctype rrep = ctype_realType (rep);
900 ** for abstract enum types, we need to fix the enum members:
901 ** they should have the abstract type, not the rep type.
904 if (ctype_isEnum (ctype_realType (rrep)))
906 enumNameList el = ctype_elist (rrep);
908 enumNameList_elements (el, ye)
910 if (usymtab_existsGlob (ye))
912 uentry ue = usymtab_lookupSafe (ye);
913 uentry_setType (ue, ctype_bool);
916 if (cstring_equal (context_getTrueName (), ye)
917 || cstring_equal (context_getFalseName (), ye))
925 message ("Member of boolean enumerated type definition "
926 "does not match name set to represent TRUE "
929 message ("Use -boolfalse and -booltrue to set the "
930 "name of false and true boolean values."),
931 uentry_whereDefined (e));
933 } end_enumNameList_elements;
937 if (usymtab_exists (n))
939 usymId llm = usymtab_getId (n);
940 uentry le = usymtab_getTypeEntry (llm);
942 uentry_setDeclared (e, g_currentloc);
943 uentry_setSref (e, sRef_makeGlobal (llm, uentry_getType (le)));
945 DPRINTF (("Here we are: %s / %s",
946 n, context_getBoolName ()));
948 if (uentry_isAbstractDatatype (le))
950 ctype rrep = ctype_realType (rep);
953 ** for abstract enum types, we need to fix the enum members:
954 ** they should have the abstract type, not the rep type.
957 if (ctype_isEnum (ctype_realType (rrep)))
959 ctype at = uentry_getAbstractType (le);
960 enumNameList el = ctype_elist (rrep);
962 enumNameList_elements (el, ye)
964 if (usymtab_existsGlob (ye))
966 uentry ue = usymtab_lookupSafe (ye);
968 llassert (uentry_isEitherConstant (ue));
969 llassertprint (ctype_match (uentry_getType (ue), rrep),
970 ("Bad enum: %s / %s",
972 ctype_unparse (rrep)));
974 uentry_setType (ue, at);
976 } end_enumNameList_elements;
979 if (uentry_isMutableDatatype (le))
981 /* maybe more complicated if abstract and immutable ? */
983 if (!ctype_isRealPointer (rep) && !ctype_isRealAbstract (rep))
987 message ("Mutable abstract type %s declared without pointer "
988 "indirection: %s (violates assignment semantics)",
989 n, ctype_unparse (rep)),
990 uentry_whereDefined (e));
992 uentry_setMutable (e);
999 fileloc fl = uentry_whereDeclared (e);
1001 if (context_getFlag (FLG_LIKELYBOOL)
1002 && !context_getFlag (FLG_BOOLINT))
1004 if ((cstring_equalLit (n, "BOOL")
1005 || cstring_equalLit (n, "Bool")
1006 || cstring_equalLit (n, "bool")
1007 || cstring_equalLit (n, "boolean")
1008 || cstring_equalLit (n, "Boolean")
1009 || cstring_equalLit (n, "BOOLEAN"))
1010 && !(cstring_equal (n, context_getBoolName ())))
1012 if (context_setBoolName ()) {
1015 message ("Type %s is probably meant as a boolean type, but does "
1016 "not match the boolean type name \"%s\".",
1018 context_getBoolName ()),
1023 message ("Type %s is probably meant as a boolean type, "
1024 "but the boolean type name is not set. "
1025 "Use -booltype %s to set it.",
1032 if (!uentry_isStatic (e)
1033 && !ctype_isFunction (uentry_getType (e))
1034 && !fileloc_isLib (fl)
1035 && !fileloc_isImport (fl)
1036 && fileloc_isHeader (fl))
1038 voptgenerror (FLG_EXPORTTYPE,
1039 message ("Type exported, but not specified: %s\n", n),
1048 fixUentryList (idDeclList tl, qtype q)
1050 uentryList f = uentryList_new ();
1052 idDeclList_elements (tl, i)
1054 if (idDecl_isDefined (i))
1060 (void) idDecl_fixBase (i, q);
1063 ** implicit annotations
1066 (void) fixStructDecl (i);
1068 ue = uentry_makeIdVariable (i);
1069 rt = ctype_realType (uentry_getType (ue));
1072 ** where is this here???
1074 if (ctype_isArray (rt) || ctype_isSU (rt))
1076 sRef_setAllocated (uentry_getSref (ue), uentry_whereDefined (ue));
1082 if (uentry_isValid (old = uentryList_lookupField (f, uentry_rawName (ue))))
1084 if (optgenerror (FLG_SYNTAX,
1085 message ("Field name reused: %s", uentry_rawName (ue)),
1086 uentry_whereDefined (ue)))
1088 llgenmsg (message ("Previous use of %s", uentry_rawName (ue)),
1089 uentry_whereDefined (old));
1093 f = uentryList_add (f, ue);
1095 } end_idDeclList_elements;
1097 idDeclList_free (tl);
1102 ** This is a hack to support unnamed struct/union fields as done by
1103 ** Microsoft VC++. It is not supported by the ANSI standard.
1105 ** The inner fields are added to the outer structure. This is meaningful
1106 ** for nesting structs inside unions, but lclint does no related
1111 fixUnnamedDecl (qtype q)
1113 ctype ct = ctype_realType (qtype_getType (q));
1115 if (ctype_isStruct (ct) || ctype_isUnion (ct))
1117 uentryList res = ctype_getFields (ct);
1119 return (uentryList_copy (res));
1126 return uentryList_undefined;
1129 void setStorageClass (storageClassCode sc)
1135 setProcessingIterVars (uentry iter)
1137 ProcessingIterVars = TRUE;
1139 saveIterParamNo = 0;
1143 setProcessingGlobalsList ()
1145 ProcessingGlobals = TRUE;
1147 llassert (globSet_isUndefined (currentGlobals));
1148 currentGlobals = globSet_undefined;
1150 llassert (sRefSet_isUndefined (fcnModifies));
1151 fcnModifies = sRefSet_undefined;
1154 ** No, special clauses might have been processed first!
1155 llassert (specialClauses_isUndefined (specClauses));
1156 specClauses = specialClauses_undefined;
1159 fcnNoGlobals = FALSE;
1162 static bool ProcessingGlobMods = FALSE;
1165 setProcessingGlobMods ()
1167 ProcessingGlobMods = TRUE;
1171 clearProcessingGlobMods ()
1173 ProcessingGlobMods = FALSE;
1177 isProcessingGlobMods ()
1179 return (ProcessingGlobMods);
1182 static void resetGlobals (void)
1184 ProcessingGlobals = FALSE;
1185 currentGlobals = globSet_undefined;
1186 llassert (sRefSet_isUndefined (fcnModifies));
1187 fcnModifies = sRefSet_undefined;
1188 fcnNoGlobals = FALSE;
1192 unsetProcessingGlobals ()
1194 ProcessingGlobals = FALSE;
1198 setProcessingVars (/*@only@*/ qtype q)
1200 ProcessingVars = TRUE;
1201 qtype_free (processingType);
1206 setGenericParamList (/*@dependent@*/ uentryList pm)
1208 ProcessingParams = TRUE;
1213 setProcessingTypedef (/*@only@*/ qtype q)
1215 ProcessingTypedef = TRUE;
1217 qtype_free (processingType);
1222 unsetProcessingVars ()
1224 resetStorageClass ();
1225 ProcessingVars = FALSE;
1231 if (ProcessingParams)
1233 if (uentry_isInvalid (saveFunction))
1235 llbuglit ("unsetProcessingVars: no saved function\n");
1237 if (sRefSet_isDefined (fcnModifies)) {
1238 sRefSet_free (fcnModifies);
1239 fcnModifies = sRefSet_undefined;
1244 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1245 uentryList params = uentryList_copy (saveParamList);
1246 ctype ct2 = ctype_makeFunction (ct, params);
1248 uentry_setType (saveFunction, ct2);
1249 ProcessingParams = FALSE;
1251 reflectModGlobs (saveFunction);
1252 oldStyleDeclareFunction (saveFunction);
1253 saveFunction = uentry_undefined;
1260 ** If the paramlist used a type name, we could be here.
1263 llfatalerror (message ("%q: Old-style function parameter list uses a "
1264 "type name.", fileloc_unparse (g_currentloc)));
1271 if (uentry_isValid (saveFunction))
1274 ** old style declaration
1277 ctype ct = ctype_returnValue (uentry_getType (saveFunction));
1280 uentryList_elements (saveParamList, current)
1282 uentry_setType (current, ctype_int); /* all params are ints */
1283 } end_uentryList_elements;
1285 ct2 = ctype_makeParamsFunction (ct, uentryList_copy (saveParamList));
1287 uentry_setType (saveFunction, ct2);
1288 ProcessingParams = FALSE;
1290 oldStyleDeclareFunction (saveFunction);
1291 saveFunction = uentry_undefined;
1296 unsetProcessingTypedef ()
1298 ProcessingTypedef = FALSE;
1301 void checkConstant (qtype t, idDecl id)
1305 id = idDecl_fixBase (id, t);
1306 e = uentry_makeIdConstant (id);
1308 reflectStorageClass (e);
1309 resetStorageClass ();
1311 usymtab_supGlobalEntry (e);
1314 void checkValueConstant (qtype t, idDecl id, exprNode e)
1318 id = idDecl_fixBase (id, t);
1319 ue = uentry_makeIdConstant (id);
1320 reflectStorageClass (ue);
1321 resetStorageClass ();
1323 if (exprNode_isDefined (e))
1325 if (!exprNode_matchType (uentry_getType (ue), e))
1328 (exprNode_getType (e), e,
1329 uentry_getType (ue), exprNode_undefined,
1330 message ("Constant %q initialized to type %t, expects %t: %s",
1331 uentry_getName (ue),
1332 exprNode_getType (e),
1333 uentry_getType (ue),
1334 exprNode_unparse (e)),
1339 if (exprNode_hasValue (e))
1341 uentry_mergeConstantValue (ue, multiVal_copy (exprNode_getValue (e)));
1346 usymtab_supGlobalEntry (ue);
1350 void processNamedDecl (idDecl t)
1352 if (qtype_isUndefined (processingType))
1354 llparseerror (message ("No type before declaration name: %q", idDecl_unparse (t)));
1356 processingType = qtype_create (ctype_unknown);
1359 t = idDecl_fixBase (t, processingType);
1361 DPRINTF (("Declare: %s", idDecl_unparse (t)));
1363 if (ProcessingGlobals)
1365 cstring id = idDecl_getName (t);
1366 uentry ue = usymtab_lookupSafe (id);
1368 if (!uentry_isValid (ue))
1370 llerror (FLG_UNRECOG,
1371 message ("Variable used in globals list is undeclared: %s", id));
1375 if (!ctype_match (uentry_getType (ue), idDecl_getCtype (t)))
1379 message ("Variable %s used in globals list declared %s, "
1381 id, ctype_unparse (uentry_getType (ue)),
1382 ctype_unparse (idDecl_getCtype (t))),
1387 sRef sr = sRef_copy (uentry_getSref (ue));
1389 reflectGlobalQualifiers (sr, idDecl_getQuals (t));
1391 currentGlobals = globSet_insert (currentGlobals, sr);
1395 else if (ProcessingVars)
1400 ct = ctype_realType (idDecl_getCtype (t));
1402 if (ProcessingParams)
1404 cstring id = idDecl_getName (t);
1405 int paramno = uentryList_lookupRealName (saveParamList, id);
1409 uentry cparam = uentryList_getN (saveParamList, paramno);
1411 uentry_setType (cparam, idDecl_getCtype (t));
1412 uentry_reflectQualifiers (cparam, idDecl_getQuals (t));
1413 uentry_setDeclaredOnly (cparam, context_getSaveLocation ());
1418 (message ("Old style declaration uses unlisted parameter: %s",
1426 if (context_inIterDef ())
1428 cstring pname = makeParam (idDecl_observeId (t));
1429 uentry p = usymtab_lookupSafe (pname);
1431 cstring_free (pname);
1433 if (uentry_isYield (p))
1435 e = uentry_makeParam (t, sRef_getParam (uentry_getSref (p)));
1437 uentry_checkYieldParam (p, e);
1439 usymtab_supEntrySref (e);
1444 if ((hasSpecialCode () || argsUsed)
1445 && ctype_isFunction (idDecl_getCtype (t)))
1447 e = uentry_makeIdFunction (t);
1448 reflectSpecialCode (e);
1449 reflectArgsUsed (e);
1453 e = uentry_makeIdVariable (t);
1456 loc = uentry_whereDeclared (e);
1459 if (context_inGlobalScope ())
1461 uentry_checkParams was here!
1465 if (ctype_isFunction (uentry_getType (e)))
1467 reflectModGlobs (e);
1471 llassert (!globSet_isDefined (currentGlobals)
1472 && !sRefSet_isDefined (fcnModifies));
1475 e = usymtab_supEntrySrefReturn (e);
1477 if (uentry_isExtern (e) && !context_inGlobalScope ())
1481 message ("Declaration using extern inside function scope: %q",
1482 uentry_unparse (e)),
1485 uentry_setDefined (e, fileloc_getExternal ());
1486 sRef_setDefined (uentry_getSref (e), fileloc_getExternal ());
1489 if (uentry_isFunction (e))
1491 uentry_checkParams (e);
1492 checkParamNames (e);
1495 if (uentry_isVar (e)
1496 && uentry_isCheckedUnknown (e))
1498 sRef sr = uentry_getSref (e);
1500 if (sRef_isLocalVar (sr))
1502 if (context_getFlag (FLG_IMPCHECKMODINTERNALS))
1504 uentry_setCheckMod (e);
1508 uentry_setUnchecked (e);
1511 else if (sRef_isFileStatic (sr))
1513 if (context_getFlag (FLG_IMPCHECKEDSTRICTSTATICS))
1515 uentry_setCheckedStrict (e);
1517 else if (context_getFlag (FLG_IMPCHECKEDSTATICS))
1519 uentry_setChecked (e);
1521 else if (context_getFlag (FLG_IMPCHECKMODSTATICS))
1523 uentry_setCheckMod (e);
1530 else /* real global */
1532 llassert (sRef_isRealGlobal (sr));
1534 if (context_getFlag (FLG_IMPCHECKEDSTRICTGLOBALS))
1536 uentry_setCheckedStrict (e);
1538 else if (context_getFlag (FLG_IMPCHECKEDGLOBALS))
1540 uentry_setChecked (e);
1542 else if (context_getFlag (FLG_IMPCHECKMODGLOBALS))
1544 uentry_setCheckMod (e);
1554 else if (ProcessingTypedef)
1556 ctype ct = idDecl_getCtype (t);
1559 DPRINTF (("Processing typedef: %s", ctype_unparse (ct)));
1561 e = uentry_makeIdDatatype (t);
1563 if (cstring_equal (idDecl_getName (t), context_getBoolName ())) {
1564 ctype rt = ctype_realType (ct);
1566 if (ctype_isEnum (rt)) {
1569 if (!(ctype_isInt (rt)
1570 || ctype_isUnknown (rt)
1571 || ctype_isChar (rt))) {
1574 message ("Boolean type %s defined using non-standard type %s (integral, char or enum type expected)",
1575 context_getBoolName (),
1576 ctype_unparse (ct)),
1577 uentry_whereLast (e));
1581 uentry_setType (e, ct);
1585 reflectStorageClass (e);
1586 checkTypeDecl (e, ct);
1588 e = usymtab_supReturnTypeEntry (e);
1590 if (uentry_isMaybeAbstract (e))
1592 if (context_getFlag (FLG_IMPABSTRACT))
1594 uentry_setAbstract (e);
1598 uentry_setConcrete (e);
1604 llparseerror (message ("Suspect missing struct or union keyword: %q",
1605 idDecl_unparse (t)));
1611 ** moved from grammar
1614 static idDecl fixStructDecl (/*@returned@*/ idDecl d)
1616 if (ctype_isVisiblySharable (idDecl_getCtype (d))
1617 && context_getFlag (FLG_STRUCTIMPONLY))
1619 if (!qualList_hasAliasQualifier (idDecl_getQuals (d)))
1621 if (qualList_hasExposureQualifier (idDecl_getQuals (d)))
1623 idDecl_addQual (d, qual_createDependent ());
1627 idDecl_addQual (d, qual_createImpOnly ());
1636 declareUnnamedStruct (/*@only@*/ uentryList f)
1638 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1640 int num = uentryList_size (f);
1641 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1646 (FLG_NUMSTRUCTFIELDS,
1647 message ("Structure declared with %d fields "
1648 "(limit is set to %d)",
1654 return (ctype_createUnnamedStruct (f));
1658 declareUnnamedUnion (/*@only@*/ uentryList f)
1660 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1662 int num = uentryList_size (f);
1663 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1668 (FLG_NUMSTRUCTFIELDS,
1669 message ("Union declared with %d fields "
1670 "(limit is set to %d)",
1676 return (ctype_createUnnamedUnion (f));
1679 ctype declareStruct (cstring id, /*@only@*/ uentryList f)
1683 int num = uentryList_size (f);
1685 ct = ctype_createStruct (cstring_copy (id), f);
1686 DPRINTF (("Declare struct: %s [%d]", ctype_unparse (ct), ct));
1687 ue = uentry_makeStructTagLoc (id, ct);
1689 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1691 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1696 (FLG_NUMSTRUCTFIELDS,
1697 message ("Structure %q declared with %d fields "
1698 "(limit is set to %d)",
1699 uentry_getName (ue), num, max),
1700 uentry_whereLast (ue));
1704 return (usymtab_supTypeEntry (ue));
1707 ctype declareUnion (cstring id, uentryList f)
1711 int num = uentryList_size (f);
1713 ct = ctype_createUnion (cstring_copy (id), f);
1714 ue = uentry_makeUnionTagLoc (id, ct);
1716 if (context_maybeSet (FLG_NUMSTRUCTFIELDS))
1718 int max = context_getValue (FLG_NUMSTRUCTFIELDS);
1723 (FLG_NUMSTRUCTFIELDS,
1724 message ("Union %q declared with %d fields "
1725 "(limit is set to %d)",
1726 uentry_getName (ue), num, max),
1727 uentry_whereLast (ue));
1731 return (usymtab_supTypeEntry (ue));
1734 ctype handleStruct (/*@only@*/ cstring id)
1736 if (usymtab_existsStructTag (id))
1738 ctype ct = uentry_getAbstractType (usymtab_lookupStructTag (id));
1745 return (ctype_createForwardStruct (id));
1749 ctype handleUnion (/*@only@*/ cstring id)
1751 if (usymtab_existsUnionTag (id))
1753 ctype ret = uentry_getAbstractType (usymtab_lookupUnionTag (id));
1759 return (ctype_createForwardUnion (id));
1764 handleEnum (cstring id)
1766 if (usymtab_existsEnumTag (id))
1768 ctype ret = uentry_getAbstractType (usymtab_lookupEnumTag (id));
1774 return (declareEnum (id, enumNameList_new ()));
1778 bool processingIterVars (void)
1780 return ProcessingIterVars;
1783 uentry getCurrentIter (void)
1788 static bool flipOldStyle = FALSE;
1789 static bool flipNewStyle = TRUE;
1791 void setFlipOldStyle () { flipOldStyle = TRUE; }
1792 bool isFlipOldStyle () { return flipOldStyle; }
1793 bool isNewStyle () { return flipNewStyle; }
1794 void setNewStyle () { flipNewStyle = TRUE; }
1796 /*@dependent@*/ uentryList handleParamIdList (/*@dependent@*/ uentryList params)
1801 ** this is a really YUCKY hack to handle old style
1805 voptgenerror (FLG_OLDSTYLE,
1806 cstring_makeLiteral ("Old style function declaration"),
1809 uentryList_elements (params, current)
1811 uentry_setParam (current);
1812 uentry_setSref (current, sRef_makeParam (paramno, ctype_unknown));
1814 } end_uentryList_elements;
1816 setGenericParamList (params);
1817 g_expectingTypeName = TRUE;
1822 /*@dependent@*/ uentryList handleParamTypeList (/*@returned@*/ uentryList params)
1826 uentryList_fixMissingNames (params);
1828 voptgenerror (FLG_OLDSTYLE,
1829 cstring_makeLiteral ("Old style function declaration."),
1832 setGenericParamList (params);
1833 flipOldStyle = FALSE;
1834 g_expectingTypeName = TRUE;
1843 ctype c = ctype_unknown;
1844 cstring id = cstring_makeLiteral ("va_alist");
1847 if (ProcessingParams)
1849 int i = uentryList_lookupRealName (saveParamList, id);
1853 e = uentry_makeVariableSrefParam (id, c, sRef_makeParam (i, c));
1857 e = uentry_undefined; /* suppress gcc message */
1858 llfatalerrorLoc (cstring_makeLiteral ("va_dcl used without va_alist"));
1863 llerror (FLG_SYNTAX, cstring_makeLiteral ("va_dcl used outside of function declaration"));
1864 e = uentry_makeVariableLoc (id, c);
1868 uentry_setUsed (e, g_currentloc);
1869 usymtab_supEntrySref (e);
1872 /*@exposed@*/ sRef modListPointer (sRef s)
1874 ctype ct = sRef_getType (s);
1875 ctype rt = ctype_realType (ct);
1877 if (ctype_isAP (rt))
1879 if (context_inHeader () && ctype_isAbstract (ct))
1884 ("Modifies clause in header file dereferences abstract "
1885 "type %s (interface modifies clause should not depend "
1886 "on or expose type representation): %q",
1892 return (sRef_constructPointer (s));
1896 if (ctype_isKnown (rt))
1900 message ("Implementation modifies clause dereferences non-pointer (type %s): %q",
1910 /*@exposed@*/ sRef modListFieldAccess (sRef s, cstring f)
1912 ctype ct = sRef_getType (s);
1913 ctype rt = ctype_realType (ct);
1915 if (ctype_isStructorUnion (rt))
1917 uentry tf = uentryList_lookupField (ctype_getFields (rt), f);
1919 if (uentry_isUndefined (tf))
1921 voptgenerror (FLG_TYPE,
1922 message ("Modifies list accesses non-existent "
1923 "field %s of %t: %q", f, ct,
1928 return sRef_undefined;
1932 if (ctype_isAbstract (ct) && context_inHeader ())
1937 ("Modifies clause in header file accesses abstract "
1938 "type %s (interface modifies clause should not depend "
1939 "on or expose type representation): %q",
1946 cstring_markOwned (f);
1947 return (sRef_makeField (s, f));
1953 message ("Modifies clause dereferences non-pointer (type %s): %q",
1963 sRef globListUnrecognized (cstring s)
1965 if (cstring_equalLit (s, "nothing"))
1967 return sRef_makeNothing ();
1969 else if (cstring_equalLit (s, "internalState"))
1971 return sRef_makeInternalState ();
1973 else if (cstring_equalLit (s, "fileSystem")
1974 || cstring_equalLit (s, "systemState"))
1976 return sRef_makeSystemState ();
1982 message ("Unrecognized identifier in globals list: %s", s),
1985 return sRef_undefined;
1989 /*@exposed@*/ sRef modListArrowAccess (sRef s, cstring f)
1991 ctype ct = sRef_getType (s);
1992 ctype rt = ctype_realType (ct);
1994 if (ctype_isRealPointer (rt))
1996 ctype b = ctype_baseArrayPtr (rt);
1997 ctype rb = ctype_realType (b);
1999 if (ctype_isStructorUnion (rb))
2001 uentry tf = uentryList_lookupField (ctype_getFields (rb), f);
2003 if (uentry_isUndefined (tf))
2005 voptgenerror (FLG_TYPE,
2006 message ("Modifies list arrow accesses non-existent "
2007 "field %s of %t: %q", f, b,
2012 return sRef_undefined;
2016 if (context_inHeader ())
2018 if (ctype_isAbstract (b))
2023 ("Modifies clause in header file arrow accesses abstract "
2024 "type %s (interface modifies clause should not depend "
2025 "on or expose type representation): %q",
2033 if (ctype_isAbstract (ct))
2038 ("Modifies clause in header file arrow accesses abstract "
2039 "type %s (interface modifies clause should not depend "
2040 "on or expose type representation): %q",
2048 cstring_markOwned (f);
2049 return (sRef_makeArrow (s, f));
2055 message ("Modifies clause arrow accesses pointer to "
2056 "non-structure (type %s): %q",
2066 message ("Modifies clause arrow accesses non-pointer (type %s): %q",
2076 sRef checkSpecClausesId (uentry ue)
2078 cstring s = uentry_rawName (ue);
2080 if (sRef_isGlobal (uentry_getSref (ue)))
2084 message ("Global variable %s used special clause. (Global variables "
2085 "are not recognized in special clauses. If there is "
2086 "sufficient interest in support for this, it may be "
2087 "added to a future release. Send mail to "
2088 "lclint@cs.virginia.edu.)",
2092 return sRef_undefined;
2096 if (cstring_equalLit (s, "result"))
2100 message ("Special clause list uses %s which is a variable and has special "
2101 "meaning in a modifies list. (Special meaning assumed.)", s),
2104 uentry_showWhereDeclared (ue);
2108 return uentry_getSref (ue);
2113 void checkModifiesId (uentry ue)
2115 cstring s = uentry_rawName (ue);
2117 if (cstring_equalLit (s, "nothing")
2118 || cstring_equalLit (s, "internalState")
2119 || cstring_equalLit (s, "systemState")
2120 || (cstring_equalLit (s, "fileSystem")))
2124 message ("Modifies list uses %s which is a variable and has special "
2125 "meaning in a modifies list. (Special meaning assumed.)", s),
2128 uentry_showWhereDeclared (ue);
2133 /*@exposed@*/ sRef fixModifiesId (cstring s)
2136 cstring pname = makeParam (s);
2137 uentry ue = usymtab_lookupSafe (pname);
2139 cstring_free (pname);
2141 if (cstring_equalLit (s, "nothing"))
2143 ret = sRef_makeNothing ();
2145 else if (cstring_equalLit (s, "internalState"))
2147 ret = sRef_makeInternalState ();
2149 else if (cstring_equalLit (s, "fileSystem")
2150 || cstring_equalLit (s, "systemState"))
2152 ret = sRef_makeSystemState ();
2156 ret = sRef_undefined;
2159 if (sRef_isValid (ret))
2161 if (uentry_isValid (ue))
2165 message ("Modifies list uses %s which is a parameter and has special "
2166 "meaning in a modifies list. (Special meaning assumed.)", s),
2172 if (uentry_isValid (ue))
2174 ret = uentry_getSref (ue);
2178 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2179 ret = sRef_undefined;
2183 message ("Unrecognized identifier in modifies comment: %s", s),
2193 sRef fixSpecClausesId (cstring s)
2196 cstring pname = makeParam (s);
2197 uentry ue = usymtab_lookupSafe (pname);
2199 cstring_free (pname);
2201 if (cstring_equalLit (s, "result"))
2203 ret = sRef_makeResult ();
2207 ret = sRef_undefined;
2210 if (sRef_isValid (ret))
2212 if (uentry_isValid (ue))
2216 message ("Special clause uses %s which is a parameter and has special "
2217 "meaning in a special clause. (Special meaning assumed.)", s),
2223 if (uentry_isValid (ue))
2225 ret = uentry_getSref (ue);
2227 if (sRef_isGlobal (ret))
2231 message ("Global variable %s used special clause. (Global variables "
2232 "are not recognized in special clauses. If there is "
2233 "sufficient interest in support for this, it may be "
2234 "added to a future release. Send mail to "
2235 "lclint@cs.virginia.edu.)",
2239 ret = sRef_undefined;
2244 fileloc loc = fileloc_decColumn (g_currentloc, cstring_length (s));
2245 ret = sRef_undefined;
2249 message ("Unrecognized identifier in special clause: %s", s),
2259 sRef modListArrayFetch (sRef s, /*@unused@*/ sRef mexp)
2261 ctype ct = sRef_getType (s);
2262 ctype rt = ctype_realType (ct);
2264 if (ctype_isAP (rt))
2266 if (context_inHeader () && ctype_isAbstract (ct))
2271 ("Modifies clause in header file indexes abstract "
2272 "type %s (interface modifies clause should not depend "
2273 "on or expose type representation): %q",
2279 return (sRef_makeAnyArrayFetch (s));
2286 ("Implementation modifies clause uses array fetch on non-array (type %s): %q",
2287 ctype_unparse (ct), sRef_unparse (s)),