2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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 splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 # include "splintMacros.nf"
30 # include "cgrammar.h"
31 # include "cgrammar_tokens.h"
32 # include "transferChecks.h"
33 # include "exprChecks.h"
36 ** for now, allow exprChecks to access exprNode.
37 ** may remove this in future
42 static bool checkCallModifyAux (/*@exposed@*/ sRef p_s, exprNode p_f, sRef p_alias, exprNode p_err);
43 static bool checkModifyValAux (/*@exposed@*/ sRef p_s, exprNode p_f, sRef p_alias, exprNode p_err);
44 static bool checkModifyAux (/*@exposed@*/ sRef p_s, exprNode p_f, sRef p_alias, exprNode p_err);
45 static void checkSafeReturnExpr (/*@notnull@*/ exprNode p_e);
48 ** called at end of expression statement
50 ** of e->kind is not an assign, empty, body or modop
51 ** verify the the value is void
55 static int inCompoundStatementExpression = 0;
58 exprChecks_inCompoundStatementExpression (void)
60 inCompoundStatementExpression++;
64 exprChecks_leaveCompoundStatementExpression (void)
66 inCompoundStatementExpression--;
67 llassert (inCompoundStatementExpression >= 0);
71 exprChecks_checkStatementEffect (exprNode e)
73 bool hasError = FALSE;
75 if (inCompoundStatementExpression > 0)
78 ** Okay to have effectless statments in compound statement expression (should check
79 ** it is the last statement, but we don't for now).
85 if (!exprNode_isError (e))
87 exprKind ek = e->kind;
89 if (ek == XPR_CALL && !(ctype_isRealVoid (e->typ)))
91 if (ctype_isKnown (e->typ))
93 if (ctype_isManifestBool (ctype_realishType (e->typ)))
95 hasError = optgenerror
97 message ("Return value (type %t) ignored: %s",
99 exprNode_unparseFirst (e)),
102 else if (ctype_isDirectInt (e->typ))
104 hasError = optgenerror
106 message ("Return value (type %t) ignored: %s",
108 exprNode_unparseFirst (e)),
113 hasError = optgenerror
115 message ("Return value (type %t) ignored: %s",
117 exprNode_unparseFirst (e)),
123 if (!hasError && !(exprNode_mayEscape (e))
124 && !(e->canBreak)) /* control changes are effects too! */
126 if (sRefSet_hasRealElement (e->sets)
127 || sRefSet_hasRealElement (e->msets))
133 if (sRefSet_isEmpty (e->sets) && sRefSet_isEmpty (e->msets))
137 message ("Statement has no effect: %s",
138 exprNode_unparseFirst (e)),
143 if (context_maybeSet (FLG_NOEFFECTUNCON))
145 if (sRefSet_hasUnconstrained (e->sets))
149 message ("Statement has no effect (possible "
150 "undected modification through "
152 sRefSet_unparseUnconstrained (e->sets),
153 exprNode_unparseFirst (e)),
156 else if (sRefSet_hasUnconstrained (e->msets))
160 message ("Statement has no effect (possible "
161 "undected modification through "
163 sRefSet_unparseUnconstrained (e->msets),
164 exprNode_unparseFirst (e)),
169 ; /* statement has unknown modification */
179 checkRepExposed (sRef base, /*@notnull@*/ exprNode e, sRef alias,
180 /*@unused@*/ exprNode unused)
184 if (sRef_isInvalid (alias) || sRef_sameName (base, alias))
186 btype = sRef_getType (base);
188 if (ctype_isAbstract (btype) && ctype_isVisiblySharable (e->typ))
190 voptgenerror (FLG_RETEXPOSE,
191 message ("Return value exposes rep of %s: %s",
192 ctype_unparse (btype),
193 exprNode_unparse (e)),
200 sRef rbase = sRef_getRootBase (base);
201 btype = sRef_getType (rbase);
203 if (ctype_isAbstract (btype) && ctype_isVisiblySharable (e->typ))
207 message ("Return value may expose rep of %s through alias %q: %s",
208 ctype_unparse (btype),
209 sRef_unparse (rbase),
210 exprNode_unparse (e)),
220 checkRefGlobParam (sRef base, /*@notnull@*/ exprNode e,
221 sRef alias, /*@unused@*/ exprNode unused)
223 if (sRef_isInvalid (alias) || sRef_sameName (base, alias))
227 if (ctype_isUnknown (ct))
229 ct = sRef_getType (base);
232 if (ctype_isVisiblySharable (ct))
234 if (sRef_isFileOrGlobalScope (base))
236 uentry fcn = context_getHeader ();
237 bool noerror = FALSE;
239 if (uentry_isValid (fcn) && uentry_isFunction (fcn))
241 sRef res = uentry_getSref (fcn);
243 /* If result is dependent and global is owned, this is okay... */
244 if (sRef_isDependent (res)
245 && sRef_isOwned (base))
256 message ("Function returns reference to global %q: %s",
258 exprNode_unparse (e)),
264 else if (sRef_isAnyParam (base))
266 uentryList params = context_getParams ();
267 int paramno = sRef_getParam (base);
269 if (paramno < uentryList_size (params))
271 uentry arg = uentryList_getN (params, paramno);
272 sRef ref = uentry_getSref (arg);
274 if (uentry_isReturned (arg)
276 || sRef_isExposed (ref)
277 || sRef_isRefCounted (ref))
285 message ("Function returns reference to parameter %q: %s",
287 exprNode_unparse (e)),
293 llbuglit ("ret alias: bad paramno");
306 if (ctype_isVisiblySharable (e->typ))
308 if (sRef_isFileOrGlobalScope (base))
312 message ("Function may return reference to global %q through alias %q: %s",
313 sRef_unparse (alias),
315 exprNode_unparse (e)),
319 else if (sRef_isAnyParam (base) && !(sRef_isOnly (base)))
321 uentryList params = context_getParams ();
322 int paramno = sRef_getParam (base);
324 if (paramno < uentryList_size (params))
326 uentry arg = uentryList_getN (params, paramno);
328 if (!uentry_isReturned (arg))
333 ("Function may return reference to parameter %q through alias %q: %s",
335 sRef_unparse (alias),
336 exprNode_unparse (e)),
347 ("Function may return reference to parameter %q through alias %q: %s",
349 sRef_unparse (alias),
350 exprNode_unparse (e)),
367 exprNode_checkModify (exprNode e, exprNode err)
369 llassert (exprNode_isDefined (e));
371 DPRINTF (("Check modify: %s", exprNode_unparse (e)));
373 if (sRef_isValid (e->sref))
375 sRef_aliasCheckPred (checkModifyAux, sRef_isReference, e->sref, e, err);
380 exprNode_checkModifyVal (exprNode e, exprNode err)
382 llassert (exprNode_isDefined (e));
384 DPRINTF (("Check modify val: %s", exprNode_unparse (e)));
386 if (sRef_isValid (e->sref))
388 sRef_aliasCheckPred (checkModifyValAux, sRef_isReference, e->sref, e, err);
393 exprChecks_checkNullReturn (fileloc loc)
395 if (!context_inRealFunction ())
398 llmsg ("exprChecks_checkNullReturnExpr: not in function context");
404 if (ctype_isFunction (context_currentFunctionType ()))
406 ctype tr = ctype_getReturnType (context_currentFunctionType ());
408 if (!ctype_isFirstVoid (tr))
410 if (ctype_isUnknown (tr))
414 cstring_makeLiteral ("Empty return in function declared to implicitly return int"),
419 voptgenerror (FLG_EMPTYRETURN,
420 message ("Empty return in function declared to return %t", tr),
429 exprNode_checkReturn (exprNode e)
431 if (!exprNode_isError (e))
433 if (!context_inRealFunction ())
435 if (context_inMacro ())
437 llerror (FLG_MACRORETURN,
438 message ("Macro %s uses return (not functional)",
439 context_inFunctionName ()));
444 llbuglit ("exprNode_checkReturn: not in function context");
450 if (ctype_isFunction (context_currentFunctionType ()))
452 checkSafeReturnExpr (e);
463 exprNode_checkPred (cstring c, exprNode e)
467 if (exprNode_isError (e))
470 ct = exprNode_getType (e);
472 if (exprNode_isAssign (e))
476 message ("Test expression for %s is assignment expression: %s",
477 c, exprNode_unparse (e)),
481 if (ctype_isRealBool (ct) || ctype_isUnknown (ct))
482 /* evs 2000-12-20 added || ctype_isUnknown to avoid spurious messages */
486 else if (ctype_isRealPointer (ct))
490 message ("Test expression for %s not %s, type %t: %s", c,
491 context_printBoolName (),
492 ct, exprNode_unparse (e)),
495 else if (ctype_isRealInt (ct))
499 message ("Test expression for %s not %s, type %t: %s", c,
500 context_printBoolName (), ct, exprNode_unparse (e)),
507 message ("Test expression for %s not %s, type %t: %s", c,
508 context_printBoolName (), ct, exprNode_unparse (e)),
514 exprChecks_checkUsedGlobs (globSet decl, globSet used)
516 fileloc fl = uentry_whereSpecified (context_getHeader ());
518 if (fileloc_isUndefined (fl))
520 fl = uentry_whereDeclared (context_getHeader ());
523 globSet_allElements (decl, el)
525 if (!globSet_member (used, el))
527 if (sRef_isSpecInternalState (el)
528 || sRef_isNothing (el))
534 cstring sname = sRef_unparse (el);
536 if (fileloc_isLib (fl))
538 voptgenerror (FLG_USEALLGLOBS,
539 message ("Global %s listed (%q) but not used",
540 sname, fileloc_unparse (fl)),
545 voptgenerror (FLG_USEALLGLOBS,
546 message ("Global %s listed but not used", sname),
550 cstring_free (sname);
553 } end_globSet_allElements;
557 exprNode_checkAllMods (sRefSet mods, uentry ue)
559 bool realParams = FALSE;
560 uentry le = context_getHeader ();
561 fileloc fl = uentry_whereSpecified (le);
562 uentryList specParamNames = uentryList_undefined;
563 uentryList paramNames = context_getParams ();
565 if (uentry_isFunction (le))
567 specParamNames = uentry_getParams (le);
569 if (uentryList_isUndefined (specParamNames))
571 ; /* unknown params */
573 else if (uentryList_size (paramNames) != uentryList_size (specParamNames))
576 (message ("exprNode_checkAllMods: parameter lists have different sizes: "
578 uentryList_unparse (paramNames),
579 uentryList_size (paramNames),
580 uentryList_unparse (specParamNames),
581 uentryList_size (specParamNames)));
583 else if (uentryList_size (paramNames) > 0
584 && !uentry_hasRealName (uentryList_getN (specParamNames, 0)))
586 /* loaded from a library */
594 sRefSet_allElements (mods, sr)
596 if (sRef_isNothing (sr) || sRef_isSpecState (sr))
598 ; /* should report on anything? */
600 else if (sRef_isInternalState (sr))
602 if (!sRef_isModified (sr))
604 if (sRefSet_hasStatic (mods))
613 ("Function %s specified to modify internal state "
614 "but no internal state is modified",
615 uentry_rawName (ue)),
616 uentry_whereLast (ue)))
618 uentry_showWhereSpecified (le);
625 if (!sRef_isModified (sr))
627 cstring sname = realParams ? sRef_unparse (sr) : sRef_unparse (sr);
629 if (fileloc_isLib (fl) && !realParams)
633 message ("Suspect object listed (%q) in modifies "
634 "clause of %s not modified: %s",
635 fileloc_unparse (fl),
638 uentry_whereLast (ue));
644 message ("Suspect object listed in modifies of %s "
648 uentry_whereLast (ue)))
650 uentry_showWhereSpecified (le);
653 cstring_free (sname);
656 } end_sRefSet_allElements;
659 void exprNode_checkMacroBody (/*@only@*/ exprNode e)
661 if (!exprNode_isError (e))
665 if (!(context_inFunctionLike () || context_inMacroConstant ()
666 || context_inUnknownMacro ()))
670 ("exprNode_checkMacroBody: not in macro function or constant: %q",
671 context_unparse ()));
676 hdr = context_getHeader ();
678 if (e->kind == XPR_STMTLIST || e->kind == XPR_BODY)
683 ("Macro %q definition is statement list (recommend "
684 "do { ... } while (0) constuction to ensure multiple "
685 "statement macro is syntactic function)",
686 uentry_getName (hdr)),
687 fileloc_isDefined (e->loc) ? e->loc : g_currentloc);
690 if (context_inMacroConstant ())
692 ctype t = uentry_getType (hdr);
694 uentry_setDefined (hdr, e->loc);
696 if (!(exprNode_matchType (t, e)))
698 cstring uname = uentry_getName (hdr);
700 if (cstring_equal (uname, context_getTrueName ())
701 || cstring_equal (uname, context_getFalseName ()))
704 ** We need to do something special to allow FALSE and TRUE
705 ** to be defined without reporting errors. This is a tad
706 ** bogus, but otherwise lots of things would break.
710 llassert (ctype_isManifestBool (t));
711 /* Should also check type of e is a reasonable (?) bool type. */
718 ("Constant %q specified as %s, but defined as %s: %s",
719 uentry_getName (hdr),
721 ctype_unparse (e->typ),
722 exprNode_unparse (e)),
725 uentry_showWhereSpecified (hdr);
729 cstring_free (uname);
733 if (context_maybeSet (FLG_NULLSTATE)
735 && ctype_isRealPointer (t)
736 && exprNode_isNullValue (e))
738 uentry ue = usymtab_getTypeEntry (ctype_typeId (t));
739 sRef sr = uentry_getSref (ue);
741 if (!sRef_possiblyNull (sr))
745 message ("Constant %q of non-null type %s defined "
747 uentry_getName (hdr), ctype_unparse (t),
748 exprNode_unparse (e)),
749 message ("If %s can be null, add a /*@null@*/ "
750 "qualifer to its typedef.",
755 uentry_mergeConstantValue (hdr, e->val);
756 e->val = multiVal_undefined;
760 else if (context_inMacroFunction () || context_inUnknownMacro ())
762 ctype rettype = context_getRetType ();
764 if (context_isMacroMissingParams ())
766 llassert (context_inMacroFunction ());
769 ** # define newname oldname
771 ** newname is a function
772 ** specification of oldname should match
773 ** specification of newname.
776 if (!ctype_isFunction (e->typ))
780 message ("Function %s defined by unparameterized "
781 "macro not corresponding to function",
782 context_inFunctionName ()),
787 uentry ue = exprNode_getUentry (e);
789 if (uentry_isValid (ue))
792 ** Okay, for now --- should check for consistency
795 ** uentry oldue = usymtab_lookup (cfname);
798 /* check var conformance here! */
804 message ("Function %s defined by unparameterized "
805 "macro not corresponding to function",
806 context_inFunctionName ()),
810 e->typ = ctype_getReturnType (e->typ);
811 rettype = e->typ; /* avoid aditional errors */
815 if (ctype_isVoid (rettype) || ctype_isUnknown (rettype))
817 ; /* don't complain when void macros have values */
819 else if (!exprNode_matchType (rettype, e))
823 message ("Function %q specified to return %s, "
824 "implemented as macro having type %s: %s",
825 uentry_getName (hdr),
826 ctype_unparse (rettype), ctype_unparse (e->typ),
827 exprNode_unparse (e)),
830 uentry_showWhereSpecified (hdr);
837 /* these expressions have values: */
838 case XPR_PARENS: case XPR_ASSIGN:
839 case XPR_EMPTY: case XPR_VAR:
840 case XPR_OP: case XPR_POSTOP:
841 case XPR_PREOP: case XPR_CALL:
842 case XPR_SIZEOFT: case XPR_SIZEOF:
843 case XPR_ALIGNOFT: case XPR_ALIGNOF:
844 case XPR_CAST: case XPR_FETCH:
845 case XPR_COMMA: case XPR_COND:
846 case XPR_ARROW: case XPR_CONST:
847 case XPR_STRINGLITERAL: case XPR_NUMLIT:
848 case XPR_FACCESS: case XPR_OFFSETOF:
850 transferChecks_return (e, hdr);
853 /* these expressions don't */
855 case XPR_VAARG: case XPR_ITER:
856 case XPR_FOR: case XPR_FORPRED:
857 case XPR_GOTO: case XPR_CONTINUE:
858 case XPR_BREAK: case XPR_RETURN:
859 case XPR_NULLRETURN: case XPR_IF:
860 case XPR_IFELSE: case XPR_DOWHILE:
861 case XPR_WHILE: case XPR_STMT:
862 case XPR_STMTLIST: case XPR_SWITCH:
863 case XPR_INIT: case XPR_BODY:
864 case XPR_NODE: case XPR_ITERCALL:
865 case XPR_TOK: case XPR_CASE:
866 case XPR_FTCASE: case XPR_FTDEFAULT:
867 case XPR_DEFAULT: case XPR_WHILEPRED:
868 case XPR_BLOCK: case XPR_INITBLOCK:
871 message ("Function %q specified to return %s, "
872 "implemented as macro with no result: %s",
873 uentry_getName (hdr),
874 ctype_unparse (rettype),
875 exprNode_unparse (e)),
878 uentry_showWhereSpecified (hdr);
883 usymtab_checkFinalScope (FALSE);
887 llbug (message ("exprNode_checkMacroBody: not in macro function: %q", context_unparse ()));
893 context_exitFunction ();
897 void exprNode_checkFunctionBody (exprNode body)
899 if (!exprNode_isError (body))
901 bool noret = context_getFlag (FLG_NORETURN);
902 bool checkret = exprNode_mustEscape (body);
906 && !exprNode_errorEscape (body)
907 && context_inRealFunction ()
908 && ctype_isFunction (context_currentFunctionType ()))
910 ctype tr = ctype_getReturnType (context_currentFunctionType ());
912 if (!ctype_isFirstVoid (tr))
914 if (ctype_isUnknown (tr))
918 cstring_makeLiteral ("Path with no return in function declared to implicity return int"),
925 message ("Path with no return in function declared to return %t",
934 context_returnFunction ();
941 void exprNode_checkFunction (/*@unused@*/ uentry ue, /*@only@*/ exprNode body)
943 constraintList c, t, post;
944 constraintList c2, fix;
945 context_enterInnerContext ();
947 llassert (exprNode_isDefined (body));
949 DPRINTF (("Checking body: %s", exprNode_unparse (body)));
952 if we're not going to be printing any errors for buffer overflows
953 we can skip the checking to improve performance
955 FLG_DEBUGFUNCTIONCONSTRAINT controls wheather we perform the check anyway
956 in order to find potential problems like assert failures and seg faults...
959 if (!(context_getFlag (FLG_DEBUGFUNCTIONCONSTRAINT)
960 || context_getFlag (FLG_BOUNDSWRITE)
961 || context_getFlag (FLG_BOUNDSREAD)
962 || context_getFlag (FLG_LIKELYBOUNDSWRITE)
963 || context_getFlag (FLG_LIKELYBOUNDSREAD)
964 || context_getFlag (FLG_CHECKPOST)
965 || context_getFlag (FLG_ALLOCMISMATCH)))
967 exprNode_free (body);
968 context_exitInnerPlain();
972 exprNode_generateConstraints (body); /* evans 2002-03-02: this should not be declared to take a
973 dependent... fix it! */
975 c = uentry_getFcnPreconditions (ue);
977 if (constraintList_isDefined (c))
979 DPRINTF (("Function preconditions are %s", constraintList_unparseDetailed (c)));
981 body->requiresConstraints =
982 constraintList_reflectChangesFreePre (body->requiresConstraints, c);
984 c2 = constraintList_copy (c);
985 fix = constraintList_makeFixedArrayConstraints (body->uses);
986 c2 = constraintList_reflectChangesFreePre (c2, fix);
988 constraintList_free (fix);
990 if (context_getFlag (FLG_ORCONSTRAINT))
992 t = constraintList_reflectChangesOr (body->requiresConstraints, c2 );
996 t = constraintList_reflectChanges(body->requiresConstraints, c2);
999 constraintList_free (body->requiresConstraints);
1000 DPRINTF ((message ("The body has the required constraints: %s", constraintList_unparseDetailed (t) ) ) );
1002 body->requiresConstraints = t;
1004 t = constraintList_mergeEnsures (c, body->ensuresConstraints);
1005 constraintList_free(body->ensuresConstraints);
1007 body->ensuresConstraints = t;
1009 DPRINTF ((message ("The body has the ensures constraints: %s", constraintList_unparseDetailed (t) ) ) );
1010 constraintList_free(c2);
1013 if (constraintList_isDefined(c))
1015 DPRINTF ((message ("The Function %s has the preconditions %s",
1016 uentry_unparse(ue), constraintList_unparseDetailed(c))));
1020 DPRINTF((message ("The Function %s has no preconditions", uentry_unparse(ue))));
1023 if (context_getFlag (FLG_IMPBOUNDSCONSTRAINTS))
1025 constraintList implicitFcnConstraints = context_getImplicitFcnConstraints (ue);
1027 if (constraintList_isDefined (implicitFcnConstraints))
1029 DPRINTF (("Implicit constraints: %s", constraintList_unparse (implicitFcnConstraints)));
1031 body->requiresConstraints = constraintList_reflectChangesFreePre (body->requiresConstraints,
1032 implicitFcnConstraints);
1033 constraintList_free (implicitFcnConstraints);
1037 DPRINTF (("No implicit constraints"));
1041 body->requiresConstraints = constraintList_sort (body->requiresConstraints);
1043 constraintList_printError(body->requiresConstraints, g_currentloc);
1045 post = uentry_getFcnPostconditions (ue);
1047 if (context_getFlag (FLG_CHECKPOST))
1049 if (constraintList_isDefined (post))
1051 constraintList post2;
1053 DPRINTF ((message ("The declared function postconditions are %s \n\n\n\n\n",
1054 constraintList_unparseDetailed (post) ) ) );
1056 post = constraintList_reflectChangesFreePre (post, body->ensuresConstraints);
1058 post2 = constraintList_copy (post);
1059 fix = constraintList_makeFixedArrayConstraints (body->uses);
1060 post2 = constraintList_reflectChangesFreePre (post2, fix);
1061 constraintList_free(fix);
1062 if ( context_getFlag (FLG_ORCONSTRAINT) )
1064 t = constraintList_reflectChangesOr (post2, body->ensuresConstraints);
1068 t = constraintList_reflectChanges(post2, body->ensuresConstraints);
1071 constraintList_free(post2);
1072 constraintList_free(post);
1075 printf("Unresolved post conditions\n");
1076 constraintList_printErrorPostConditions(post, g_currentloc);
1080 if (constraintList_isDefined (post))
1082 constraintList_free (post);
1085 body->ensuresConstraints = constraintList_sort(body->ensuresConstraints);
1087 if ( context_getFlag (FLG_FUNCTIONPOST) )
1089 constraintList_printError(body->ensuresConstraints, g_currentloc);
1092 /* ConPrint (message ("Unable to resolve function constraints:\n%s", constraintList_printDetailed(body->requiresConstraints) ), g_currentloc);
1094 ConPrint (message ("Splint has found function post conditions:\n%s", constraintList_printDetailed(body->ensuresConstraints) ), g_currentloc);
1096 printf ("The required constraints are:\n%s", constraintList_printDetailed(body->requiresConstraints) );
1097 printf ("The ensures constraints are:\n%s", constraintList_unparseDetailed(body->ensuresConstraints) );
1100 if (constraintList_isDefined (c))
1101 constraintList_free(c);
1103 context_exitInnerPlain();
1105 /* is it okay not to free this? */
1106 DPRINTF (("Done checking constraints..."));
1107 exprNode_free (body);
1110 void exprChecks_checkEmptyMacroBody (void)
1114 if (!(context_inFunctionLike () || context_inMacroConstant ()
1115 || context_inUnknownMacro ()))
1118 (message ("exprNode_checkEmptyMacroBody: not in macro function or constant: %q",
1119 context_unparse ()));
1123 hdr = context_getHeader ();
1127 if (uentry_isFunction (hdr))
1132 ("Macro definition for %q is empty", uentry_getName (hdr)),
1135 usymtab_checkFinalScope (FALSE);
1138 context_exitFunction ();
1142 void exprNode_checkIterBody (/*@only@*/ exprNode body)
1144 context_exitAllClauses ();
1146 context_exitFunction ();
1147 exprNode_free (body);
1150 void exprNode_checkIterEnd (/*@only@*/ exprNode body)
1152 context_exitAllClauses ();
1153 context_exitFunction ();
1154 exprNode_free (body);
1158 bool checkModifyAuxAux (/*@exposed@*/ sRef s, exprNode f, sRef alias, exprNode err)
1160 bool hasMods = context_hasMods ();
1161 flagcode errCode = hasMods ? FLG_MODIFIES : FLG_MODNOMODS;
1163 if (exprNode_isDefined (f))
1165 f->sets = sRefSet_insert (f->sets, s);
1168 if (context_getFlag (FLG_MODIFIES)
1169 && (hasMods || context_getFlag (FLG_MODNOMODS)))
1171 sRefSet mods = context_modList ();
1173 if (!sRef_canModify (s, mods))
1175 sRef rb = sRef_getRootBase (s);
1178 if (sRef_isFileOrGlobalScope (rb))
1180 if (!context_checkGlobMod (rb))
1186 if (sRef_isInvalid (alias) || sRef_sameName (s, alias))
1188 if (sRef_isLocalVar (sRef_getRootBase (s)))
1193 ("Undocumented modification of internal state (%q): %s",
1194 sRef_unparse (s), exprNode_unparse (err)),
1195 exprNode_isDefined (f) ? f->loc : g_currentloc);
1199 if (sRef_isSystemState (s))
1201 if (errCode == FLG_MODNOMODS)
1203 if (context_getFlag (FLG_MODNOMODS))
1205 errCode = FLG_MODFILESYSTEM;
1210 errCode = FLG_MODFILESYSTEM;
1216 message ("Undocumented modification of %q: %s",
1217 sRef_unparse (s), exprNode_unparse (err)),
1218 exprNode_isDefined (f) ? f->loc : g_currentloc);
1225 if (sRef_isReference (s) && !sRef_isAddress (alias))
1230 ("Possible undocumented modification of %q through alias %q: %s",
1232 sRef_unparse (alias),
1233 exprNode_unparse (err)),
1234 exprNode_isDefined (f) ? f->loc : g_currentloc);
1242 if (context_maybeSet (FLG_MUSTMOD))
1244 (void) sRef_canModify (s, context_modList ());
1247 if (sRef_isRefsField (s))
1249 sRef_setModified (s);
1257 bool checkModifyAux (/*@exposed@*/ sRef s, exprNode f, sRef alias, exprNode err)
1259 DPRINTF (("Check modify aux: %s", sRef_unparseFull (s)));
1261 if (sRef_isReference (s) && sRef_isObserver (s)
1262 && context_maybeSet (FLG_MODOBSERVER))
1266 if (sRef_isPointer (s))
1268 sRef base = sRef_getBase (s);
1269 sname = sRef_unparse (base);
1273 if (sRef_isAddress (s))
1275 sRef p = sRef_constructPointer (s);
1276 sname = sRef_unparse (p);
1280 sname = sRef_unparse (s);
1284 if (!sRef_isValid (alias) || sRef_sameName (s, alias))
1286 if (sRef_isMeaningful (s))
1290 message ("Suspect modification of observer %s: %s",
1291 sname, exprNode_unparse (err)),
1292 exprNode_isDefined (f) ? f->loc : g_currentloc))
1294 sRef_showExpInfo (s);
1301 message ("Suspect modification of observer returned by "
1302 "function call: %s",
1303 exprNode_unparse (err)),
1304 exprNode_isDefined (f) ? f->loc : g_currentloc);
1311 message ("Suspect modification of observer %s through alias %q: %s",
1312 sname, sRef_unparse (alias), exprNode_unparse (err)),
1313 exprNode_isDefined (f) ? f->loc : g_currentloc))
1315 sRef_showExpInfo (s);
1319 cstring_free (sname);
1322 (void) checkModifyAuxAux (s, f, alias, err);
1327 bool checkModifyValAux (/*@exposed@*/ sRef s, exprNode f, sRef alias, exprNode err)
1329 (void) checkModifyAuxAux (s, f, alias, err);
1334 bool checkCallModifyAux (/*@exposed@*/ sRef s, exprNode f, sRef alias, exprNode err)
1336 bool result = FALSE;
1338 DPRINTF (("Check modify aux: %s / %s",
1339 sRef_unparse (s), sRef_unparse (alias)));
1341 if (sRef_isObserver (s) && context_maybeSet (FLG_MODOBSERVER))
1343 sRef p = sRef_isAddress (s) ? sRef_constructPointer (s) : s;
1344 cstring sname = sRef_unparse (p);
1346 if (!sRef_isValid (alias) || sRef_sameName (s, alias))
1348 if (sRef_isMeaningful (s))
1350 result = optgenerror
1352 message ("Suspect modification of observer %s: %s",
1353 sname, exprNode_unparse (err)),
1354 exprNode_isDefined (f) ? f->loc : g_currentloc);
1358 result = optgenerror
1360 message ("Suspect modification of observer returned by "
1361 "function call: %s",
1362 exprNode_unparse (err)),
1363 exprNode_isDefined (f) ? f->loc : g_currentloc);
1368 result = optgenerror
1371 ("Suspect modification of observer %s through alias %q: %s",
1372 sname, sRef_unparse (alias), exprNode_unparse (err)),
1373 exprNode_isDefined (f) ? f->loc : g_currentloc);
1376 cstring_free (sname);
1378 else if (context_maybeSet (FLG_MODIFIES))
1380 DPRINTF (("can modify: %s / %s",
1382 sRefSet_unparse (context_modList ())));
1384 if (!(sRef_canModifyVal (s, context_modList ())))
1386 sRef p = sRef_isAddress (s) ? sRef_constructPointer (s) : s;
1387 cstring sname = sRef_unparse (p);
1388 bool hasMods = context_hasMods ();
1389 sRef rb = sRef_getRootBase (s);
1390 flagcode errCode = hasMods ? FLG_MODIFIES : FLG_MODNOMODS;
1393 DPRINTF (("Can't modify! %s", sRef_unparse (s)));
1395 if (sRef_isFileOrGlobalScope (rb))
1397 uentry ue = sRef_getUentry (rb);
1399 /* be more specific here! */
1400 if (!uentry_isCheckedModify (ue))
1408 if (!sRef_isValid (alias) || sRef_sameName (s, alias))
1410 if (sRef_isLocalVar (sRef_getRootBase (s)))
1415 ("Undocumented modification of internal "
1416 "state (%q) through call to %s: %s",
1417 sRef_unparse (s), exprNode_unparse (f),
1418 exprNode_unparse (err)),
1419 exprNode_isDefined (f) ? f->loc : g_currentloc);
1423 if (sRef_isSystemState (s))
1425 if (errCode == FLG_MODNOMODS)
1427 if (context_getFlag (FLG_MODNOMODS))
1429 errCode = FLG_MODFILESYSTEM;
1434 errCode = FLG_MODFILESYSTEM;
1438 result = optgenerror
1440 message ("Undocumented modification of %s "
1441 "possible from call to %s: %s",
1443 exprNode_unparse (f),
1444 exprNode_unparse (err)),
1445 exprNode_isDefined (f) ? f->loc : g_currentloc);
1450 result = optgenerror
1452 message ("Undocumented modification of %s possible "
1453 "from call to %s (through alias %q): %s",
1455 exprNode_unparse (f),
1456 sRef_unparse (alias),
1457 exprNode_unparse (err)),
1458 exprNode_isDefined (f) ? f->loc : g_currentloc);
1461 cstring_free (sname);
1466 if (context_maybeSet (FLG_MUSTMOD))
1468 (void) sRef_canModifyVal (s, context_modList ());
1475 void exprNode_checkCallModifyVal (sRef s, exprNodeList args, exprNode f, exprNode err)
1477 s = sRef_fixBaseParam (s, args);
1478 DPRINTF (("Check call modify: %s", sRef_unparse (s)));
1479 sRef_aliasCheckPred (checkCallModifyAux, NULL, s, f, err);
1483 exprChecks_checkExport (uentry e)
1485 if (context_checkExport (e))
1487 fileloc fl = uentry_whereDeclared (e);
1489 if (fileloc_isHeader (fl) && !fileloc_isLib (fl)
1490 && !fileloc_isImport (fl) && !uentry_isStatic (e))
1492 if (uentry_isFunction (e) ||
1493 (uentry_isVariable (e) && ctype_isFunction (uentry_getType (e))))
1497 message ("Function exported, but not specified: %q",
1498 uentry_getName (e)),
1501 else if (uentry_isExpandedMacro (e))
1505 message ("Expanded macro exported, but not specified: %q",
1506 uentry_getName (e)),
1509 else if (uentry_isVariable (e) && !uentry_isParam (e))
1513 message ("Variable exported, but not specified: %q",
1514 uentry_getName (e)),
1517 else if (uentry_isEitherConstant (e))
1521 message ("Constant exported, but not specified: %q",
1522 uentry_getName (e)),
1525 else if (uentry_isIter (e) || uentry_isEndIter (e))
1529 message ("Iterator exported, but not specified: %q",
1530 uentry_getName (e)),
1534 else if (uentry_isDatatype (e))
1536 ; /* error already reported */
1546 static void checkSafeReturnExpr (/*@notnull@*/ exprNode e)
1548 ctype tr = ctype_getReturnType (context_currentFunctionType ());
1549 ctype te = exprNode_getType (e);
1551 /* evans 2001-08-21: added test to warn about void returns from void functions */
1552 if (ctype_isVoid (tr))
1555 (te, e, tr, exprNode_undefined,
1556 message ("Return expression from function declared void: %s", exprNode_unparse (e)),
1561 if (!ctype_forceMatch (tr, te) && !exprNode_matchLiteral (tr, e))
1564 (te, e, tr, exprNode_undefined,
1565 message ("Return value type %t does not match declared type %t: %s",
1566 te, tr, exprNode_unparse (e)),
1572 uentry rval = context_getHeader ();
1573 sRef resultref = uentry_getSref (rval);
1575 DPRINTF (("Check return: %s / %s / %s",
1576 exprNode_unparse (e),
1577 sRef_unparseFull (e->sref),
1578 uentry_unparse (rval)));
1580 transferChecks_return (e, rval);
1582 DPRINTF (("After return: %s / %s / %s",
1583 exprNode_unparse (e),
1584 sRef_unparseFull (e->sref),
1585 uentry_unparse (rval)));
1587 if (!(sRef_isExposed (uentry_getSref (context_getHeader ()))
1588 || sRef_isObserver (uentry_getSref (context_getHeader ())))
1589 && (context_getFlag (FLG_RETALIAS)
1590 || context_getFlag (FLG_RETEXPOSE)))
1592 sRef base = sRef_getRootBase (ret);
1593 ctype rtype = e->typ;
1595 if (ctype_isUnknown (rtype))
1600 if (ctype_isVisiblySharable (rtype))
1602 if (context_getFlag (FLG_RETALIAS))
1604 sRef_aliasCheckPred (checkRefGlobParam, NULL, base,
1605 e, exprNode_undefined);
1608 if (context_getFlag (FLG_RETEXPOSE) && sRef_isIReference (ret)
1609 && !sRef_isExposed (resultref) && !sRef_isObserver (resultref))
1611 sRef_aliasCheckPred (checkRepExposed, NULL, base, e,
1612 exprNode_undefined);