2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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 <ctype.h> /* for isdigit */
29 # include "splintMacros.nf"
31 # include "cgrammar.h"
32 # include "cgrammar_tokens.h"
34 # include "exprChecks.h"
35 # include "transferChecks.h"
36 # include "exprNodeSList.h"
38 static bool exprNode_sameStorage (exprNode p_e1, exprNode p_e2) /*@*/ ;
39 static bool exprNode_isEmptyStatement (exprNode p_e);
40 static /*@exposed@*/ exprNode exprNode_firstStatement (/*@returned@*/ exprNode p_e);
41 static bool exprNode_isFalseConstant (exprNode p_e) /*@*/ ;
42 static bool exprNode_isStatement (exprNode p_e);
43 static void checkGlobUse (uentry p_glob, bool p_isCall, /*@notnull@*/ exprNode p_e);
44 static void exprNode_addUse (exprNode p_e, /*@exposed@*/ sRef p_s);
45 static bool exprNode_matchArgType (ctype p_ct, exprNode p_e);
46 static exprNode exprNode_fakeCopy (exprNode p_e) /*@*/ ;
47 static exprNode exprNode_statementError (/*@only@*/ exprNode p_e, /*@only@*/ lltok p_t);
48 static bool exprNode_matchTypes (exprNode p_e1, exprNode p_e2);
49 static void checkUniqueParams (exprNode p_fcn,
50 /*@notnull@*/ exprNode p_current, exprNodeList p_args,
51 int p_paramno, uentry p_ucurrent);
52 static void updateAliases (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2);
53 static void abstractOpError (ctype p_tr1, ctype p_tr2, lltok p_op,
54 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2,
55 fileloc p_loc1, fileloc p_loc2);
56 static ctype checkNumerics (ctype p_tr1, ctype p_tr2, ctype p_te1, ctype p_te2,
57 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, lltok p_op);
58 static void doAssign (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, bool p_isInit);
59 static void checkSafeUse (exprNode p_e, /*@exposed@*/ sRef p_s);
60 static void reflectNullTest (/*@notnull@*/ exprNode p_e, bool p_isnull);
61 static void checkMacroParen (exprNode p_e);
62 static exprNodeSList exprNode_flatten (/*@dependent@*/ exprNode p_e);
63 static void exprNode_checkSetAny (exprNode p_e, /*@dependent@*/ cstring p_name);
64 static void exprNode_checkUse (exprNode p_e, /*@exposed@*/ sRef p_s, fileloc p_loc);
65 static void exprNode_mergeUSs (exprNode p_res, exprNode p_other);
66 static void exprNode_mergeCondUSs (exprNode p_res, exprNode p_other1, exprNode p_other2);
67 static /*@only@*/ /*@notnull@*/ exprNode exprNode_fromIdentifierAux (/*@observer@*/ uentry p_c);
68 static void checkAnyCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn,
69 /*@dependent@*/ cstring p_fname,
70 uentryList p_pn, exprNodeList p_args,
71 bool p_hasMods, sRefSet p_mods, bool p_isSpec,
73 static void checkOneArg (uentry p_ucurrent, /*@notnull@*/ exprNode p_current,
74 /*@dependent@*/ exprNode p_fcn, bool p_isSpec, int p_argno, int p_totargs);
76 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn, uentryList p_params, exprNodeList p_args);
78 static /*@only@*/ exprNode exprNode_effect (exprNode p_e)
79 /*@globals internalState@*/ ;
80 static /*@only@*/ cstring exprNode_doUnparse (exprNode p_e);
81 static /*@observer@*/ cstring exprNode_rootVarName (exprNode p_e);
82 static /*@exposed@*/ exprNode
83 exprNode_lastStatement (/*@returned@*/ exprNode p_e);
85 static /*@null@*/ sRef defref = sRef_undefined;
86 static /*@only@*/ exprNode mustExitNode = exprNode_undefined;
88 static int checkArgsReal (uentry p_fcn, /*@dependent@*/ exprNode p_f,
90 exprNodeList p_args, bool p_isIter, exprNode p_ret);
92 static bool inEffect = FALSE;
93 static int nowalloc = 0;
94 static int totalloc = 0;
95 static int maxalloc = 0;
97 static /*@only@*/ uentry regArg;
98 static /*@only@*/ uentry outArg;
99 static /*@only@*/ uentry outStringArg;
100 static /*@exposed@*/ sRef stdinRef;
101 static /*@exposed@*/ sRef stdoutRef;
102 static /*@only@*/ uentry csArg;
103 static /*@only@*/ uentry csOnlyArg;
104 static ctype cstringType;
105 static ctype ctypeType;
106 static ctype filelocType;
107 static bool initMod = FALSE;
109 /*@function void exprNode_swap (sef exprNode, sef exprNode)@*/
111 # define exprNode_swap(e1,e2) do { exprNode m_tmp = (e1); (e1) = (e2); (e2) = m_tmp; } while (FALSE)
114 static void exprNode_defineConstraints(/*@sef@*/ /*@special@*/ /*@notnull@*/ exprNode e)
115 /*@defines e->requiresConstraints, e->ensuresConstraints,
116 e->trueEnsuresConstraints, e->falseEnsuresConstraints @*/
118 e->requiresConstraints = constraintList_makeNew ();
119 e->ensuresConstraints = constraintList_makeNew ();
120 e->trueEnsuresConstraints = constraintList_makeNew ();
121 e->falseEnsuresConstraints = constraintList_makeNew ();
125 ** must occur after library has been read
128 void exprNode_initMod (void)
129 /*@globals undef regArg, undef outArg, undef outStringArg,
130 undef csOnlyArg, undef csArg;
137 cstringType = ctype_unknown;
138 ctypeType = ctype_unknown;
139 filelocType = ctype_unknown;
141 defref = sRef_undefined;
143 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
145 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
148 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
150 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
153 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
155 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
158 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdin")))
160 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdin"));
162 else /* define stdin */
164 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdin"),
166 fileloc_getBuiltin (),
168 uentry_setHasNameError (ue);
169 ue = usymtab_supGlobalEntryReturn (ue);
172 stdinRef = sRef_makePointer (uentry_getSref (ue));
174 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdout")))
176 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdout"));
180 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdout"),
182 fileloc_getBuiltin (),
184 uentry_setHasNameError (ue);
185 ue = usymtab_supGlobalEntryReturn (ue);
188 stdoutRef = sRef_makePointer (uentry_getSref (ue));
190 tmp = idDecl_create (cstring_undefined, qtype_create (ctype_unknown));
192 regArg = uentry_makeParam (tmp, PARAMUNKNOWN);
195 qtype_addQual (qtype_create (ctype_makePointer (ctype_unknown)),
198 outArg = uentry_makeParam (tmp, PARAMUNKNOWN);
200 idDecl_setTyp (tmp, qtype_addQual (qtype_create (ctype_string),
203 outStringArg = uentry_makeParam (tmp, PARAMUNKNOWN);
205 idDecl_setTyp (tmp, qtype_addQual (qtype_addQual (qtype_create (cstringType),
207 qual_createNull ()));
209 csOnlyArg = uentry_makeParam (tmp, PARAMUNKNOWN);
211 idDecl_setTyp (tmp, qtype_addQual (qtype_create (cstringType), qual_createNull ()));
212 csArg = uentry_makeParam (tmp, PARAMUNKNOWN);
218 exprNode_destroyMod (void)
219 /*@globals killed regArg, killed outArg, killed outStringArg,
220 killed mustExitNode, initMod @*/
224 uentry_free (regArg);
225 uentry_free (outArg);
226 uentry_free (outStringArg);
228 exprNode_free (mustExitNode);
235 static void exprNode_resetSref (/*@notnull@*/ exprNode e)
240 exprNode exprNode_fakeCopy (exprNode e)
242 /*@-temptrans@*/ /*@-retalias@*/
244 /*@=temptrans@*/ /*@=retalias@*/
247 static bool isFlagKey (char key)
249 return (key == '-' || key == '+' || key == ' ' || key == '#');
252 static void exprNode_combineControl (/*@notnull@*/ exprNode ret,
253 /*@notnull@*/ exprNode ifclause,
254 /*@notnull@*/ exprNode elseclause)
256 ret->canBreak = ifclause->canBreak || elseclause->canBreak;
259 (ifclause->mustBreak || exprNode_mustEscape (ifclause))
260 && (elseclause->mustBreak || exprNode_mustEscape (elseclause));
262 ret->exitCode = exitkind_combine (ifclause->exitCode,
263 elseclause->exitCode);
268 ** For exprNode's returned by exprNode_effect.
271 static bool shallowKind (exprKind kind)
273 return (kind == XPR_STRINGLITERAL
274 || kind == XPR_NUMLIT
277 || kind == XPR_NODE);
281 exprNode_freeIniter (/*@only@*/ exprNode e)
283 if (!exprNode_isError (e))
289 ** Its a fake copy, don't free the field->rec and field->field
294 sfree (e->edata->field);
300 exprNode_free (e->edata->op->b);
301 /*@-compdestroy@*/ sfree (e->edata->op); /*@=compdestroy@*/
305 llbug (message ("other: %s", exprNode_unparse (e)));
308 multiVal_free (e->val);
309 cstring_free (e->etext);
310 fileloc_free (e->loc);
311 sRefSet_free (e->uses);
312 sRefSet_free (e->sets);
313 sRefSet_free (e->msets);
314 guardSet_free (e->guards);
316 constraintList_free(e->requiresConstraints);
317 constraintList_free(e->ensuresConstraints);
318 constraintList_free(e->trueEnsuresConstraints);
319 constraintList_free(e->falseEnsuresConstraints);
321 e->requiresConstraints = NULL;
322 e->ensuresConstraints = NULL;
323 e->trueEnsuresConstraints = NULL;
324 e->falseEnsuresConstraints = NULL;
331 exprNode_freeShallow (/*@only@*/ exprNode e)
333 if (!exprNode_isError (e))
335 if (shallowKind (e->kind))
342 if (e->kind == XPR_EMPTY
343 || e->kind == XPR_BODY
344 || e->kind == XPR_STRINGLITERAL
345 || e->kind == XPR_NUMLIT
346 || e->kind == XPR_NODE
347 || e->kind == XPR_OFFSETOF
348 || e->kind == XPR_ALIGNOFT
349 || e->kind == XPR_ALIGNOF
350 || e->kind == XPR_SIZEOFT
351 || e->kind == XPR_SIZEOF)
353 /* don't free anything */
357 /* multiVal_free (e->val); */
358 cstring_free (e->etext);
359 fileloc_free (e->loc);
360 sRefSet_free (e->uses);
361 sRefSet_free (e->sets);
362 sRefSet_free (e->msets);
363 guardSet_free (e->guards);
364 exprData_freeShallow (e->edata, e->kind);
366 /*@-compdestroy@*/ sfree (e); /*@=compdestroy@*/
375 exprNode_free (exprNode e)
377 if (!exprNode_isError (e))
381 multiVal_free (e->val);
382 cstring_free (e->etext);
383 fileloc_free (e->loc);
384 sRefSet_free (e->uses);
385 sRefSet_free (e->sets);
386 sRefSet_free (e->msets);
387 guardSet_free (e->guards);
388 exprData_free (e->edata, e->kind);
390 constraintList_free(e->requiresConstraints);
391 constraintList_free(e->ensuresConstraints);
392 constraintList_free(e->trueEnsuresConstraints);
393 constraintList_free(e->falseEnsuresConstraints);
395 e->requiresConstraints = NULL;
396 e->ensuresConstraints = NULL;
397 e->trueEnsuresConstraints = NULL;
398 e->falseEnsuresConstraints = NULL;
408 exprNode_makeError ()
410 return exprNode_undefined;
413 static /*@out@*/ /*@only@*/ /*@notnull@*/ exprNode
416 exprNode ret = (exprNode) dmalloc (sizeof (*ret));
417 /* static int lastexpnodes = 0; */
422 if (nowalloc > maxalloc)
430 static /*@notnull@*/ /*@special@*/ exprNode
431 exprNode_createPlain (ctype c)
433 /*@ensures isnull result->edata, result->loc, result->val, result->guards,
434 result->uses, result->sets, result->msets, result->etext @*/
437 exprNode e = exprNode_new ();
441 e->val = multiVal_undefined;
443 e->etext = cstring_undefined;
444 e->loc = fileloc_undefined;
445 e->guards = guardSet_undefined;
446 e->uses = sRefSet_undefined;
447 e->sets = sRefSet_undefined;
448 e->msets = sRefSet_undefined;
449 e->edata = exprData_undefined;
450 e->exitCode = XK_NEVERESCAPE;
452 e->mustBreak = FALSE;
453 e->isJumpPoint = FALSE;
455 exprNode_defineConstraints(e);
460 /*@observer@*/ exprNode exprNode_makeMustExit (void)
462 if (exprNode_isUndefined (mustExitNode))
464 mustExitNode = exprNode_createPlain (ctype_unknown);
465 mustExitNode->exitCode = XK_MUSTEXIT;
472 static /*@notnull@*/ /*@special@*/ exprNode exprNode_create (ctype c)
474 /*@post:isnull result->edata, result->guards, result->val,
475 result->uses, result->sets, result->msets@*/
478 exprNode e = exprNode_createPlain (c);
479 e->loc = fileloc_copy (g_currentloc);
483 static /*@notnull@*/ /*@special@*/ exprNode exprNode_createUnknown (void)
485 /*@post:isnull result->edata, result->guards,
486 result->uses, result->sets, result->msets@*/
489 return (exprNode_create (ctype_unknown));
492 static /*@notnull@*/ /*@special@*/ exprNode
493 exprNode_createLoc (ctype c, /*@keep@*/ fileloc loc)
495 /*@post:isnull result->edata, result->guards, result->val,
496 result->uses, result->sets, result->msets@*/
499 exprNode e = exprNode_createPlain (c);
505 exprNode_copySets (/*@special@*/ /*@notnull@*/ exprNode ret, exprNode e)
506 /*@defines ret->guards, ret->uses, ret->sets, ret->msets@*/
508 if (exprNode_isDefined (e))
510 ret->guards = guardSet_copy (e->guards);
511 ret->uses = sRefSet_newCopy (e->uses);
512 ret->sets = sRefSet_newCopy (e->sets);
513 ret->msets = sRefSet_newCopy (e->msets);
517 ret->guards = guardSet_undefined;
518 ret->uses = sRefSet_undefined;
519 ret->sets = sRefSet_undefined;
520 ret->msets = sRefSet_undefined;
524 static /*@notnull@*/ /*@special@*/ exprNode
525 exprNode_createPartialLocCopy (exprNode e, /*@only@*/ fileloc loc)
527 /*@post:isnull result->edata, result->etext@*/
530 exprNode ret = exprNode_new ();
532 if (exprNode_isError (e))
534 ret->typ = ctype_unknown;
535 ret->val = multiVal_undefined;
537 ret->guards = guardSet_undefined;
538 ret->uses = sRefSet_undefined;
539 ret->sets = sRefSet_undefined;
540 ret->msets = sRefSet_undefined;
545 ret->val = multiVal_copy (e->val);
547 ret->guards = guardSet_copy (e->guards);
548 ret->uses = sRefSet_newCopy (e->uses);
549 ret->sets = sRefSet_newCopy (e->sets);
550 ret->msets = sRefSet_newCopy (e->msets);
553 ret->kind = XPR_EMPTY;
555 ret->etext = cstring_undefined;
556 ret->exitCode = XK_NEVERESCAPE;
557 ret->canBreak = FALSE;
558 ret->mustBreak = FALSE;
559 ret->isJumpPoint = FALSE;
560 ret->edata = exprData_undefined;
562 exprNode_defineConstraints(ret);
568 static /*@notnull@*/ /*@special@*/ exprNode
569 exprNode_createPartialCopy (exprNode e)
571 /*@post:isnull result->edata, result->etext@*/
574 return (exprNode_createPartialLocCopy (e, fileloc_copy (exprNode_loc (e))));
577 static /*@notnull@*/ /*@special@*/ exprNode
578 exprNode_createPartialNVCopy (exprNode e)
580 /*@post:isnull result->edata, result->etext, result->val @*/
583 exprNode ret = exprNode_new ();
585 if (exprNode_isError (e))
587 ret->typ = ctype_unknown;
588 ret->loc = fileloc_undefined;
589 ret->guards = guardSet_undefined;
590 ret->uses = sRefSet_undefined;
591 ret->sets = sRefSet_undefined;
592 ret->msets = sRefSet_undefined;
597 ret->loc = fileloc_copy (e->loc);
598 ret->guards = guardSet_copy (e->guards);
599 ret->uses = sRefSet_newCopy (e->uses);
600 ret->sets = sRefSet_newCopy (e->sets);
601 ret->msets = sRefSet_newCopy (e->msets);
604 ret->val = multiVal_undefined;
605 ret->kind = XPR_EMPTY;
607 ret->etext = cstring_undefined;
608 ret->exitCode = XK_NEVERESCAPE;
609 ret->canBreak = FALSE;
610 ret->mustBreak = FALSE;
611 ret->isJumpPoint = FALSE;
612 ret->edata = exprData_undefined;
614 exprNode_defineConstraints(ret);
619 static /*@notnull@*/ /*@special@*/ exprNode
620 exprNode_createSemiCopy (exprNode e)
622 /*@post:isnull result->edata, result->etext, result->sets,
623 result->msets, result->uses, result->guards@*/
626 if (exprNode_isError (e))
628 return exprNode_createPlain (ctype_unknown);
632 exprNode ret = exprNode_new ();
635 ret->val = multiVal_copy (e->val);
636 ret->loc = fileloc_copy (e->loc);
637 ret->guards = guardSet_undefined;
638 ret->uses = sRefSet_undefined;
639 ret->sets = sRefSet_undefined;
640 ret->msets = sRefSet_undefined;
642 ret->kind = XPR_EMPTY;
644 ret->etext = cstring_undefined;
645 ret->exitCode = XK_NEVERESCAPE;
646 ret->canBreak = FALSE;
647 ret->mustBreak = FALSE;
648 ret->isJumpPoint = FALSE;
649 ret->edata = exprData_undefined;
651 exprNode_defineConstraints(ret);
658 exprNode_isNullValue (exprNode e)
660 if (exprNode_isDefined (e))
662 multiVal m = exprNode_getValue (e);
664 if (multiVal_isInt (m))
666 return (multiVal_forceInt (m) == 0);
674 exprNode_isUnknownConstant (/*@notnull@*/ exprNode e)
676 while (e->kind == XPR_PARENS)
678 e = exprData_getUopNode (e->edata);
680 if (!exprNode_isDefined (e))
685 /* evans 2002-02-05: was llassert (exprNode_isDefined (e)); but this can fail */
688 if (e->kind == XPR_CONST)
690 multiVal m = exprNode_getValue (e);
692 if (multiVal_isUnknown (m))
702 exprNode_numLiteral (ctype c, /*@temp@*/ cstring t,
703 /*@only@*/ fileloc loc, long val)
705 exprNode e = exprNode_createLoc (c, loc);
707 e->kind = XPR_NUMLIT;
709 llassert (multiVal_isUndefined (e->val));
710 e->val = multiVal_makeInt (val);
711 e->edata = exprData_makeLiteral (cstring_copy (t));
715 e->sref = sRef_makeUnknown ();
716 sRef_setDefNull (e->sref, e->loc);
719 DPRINTF (("Num lit: %s / %s", exprNode_unparse (e), ctype_unparse (exprNode_getType (e))));
724 exprNode_charLiteral (char c, cstring text, /*@only@*/ fileloc loc)
726 exprNode e = exprNode_createLoc (ctype_char, loc);
728 if (context_getFlag (FLG_CHARINTLITERAL))
730 e->typ = ctype_makeConj (ctype_char, ctype_int);
733 e->kind = XPR_NUMLIT;
734 e->val = multiVal_makeChar (c);
736 e->edata = exprData_makeLiteral (cstring_copy (text));
741 exprNode_floatLiteral (double d, ctype ct, cstring text, /*@only@*/ fileloc loc)
743 exprNode e = exprNode_createLoc (ct, loc);
745 e->kind = XPR_NUMLIT;
746 e->val = multiVal_makeDouble (d);
747 e->edata = exprData_makeLiteral (cstring_copy (text));
751 multiVal exprNode_getValue (exprNode e)
753 while (exprNode_isInParens (e)) {
754 if (e->edata != NULL) {
755 e = exprData_getUopNode (e->edata);
761 if (exprNode_isDefined (e)) {
764 return multiVal_undefined;
769 exprNode_combineLiterals (exprNode e, exprNode rest)
773 /* Both must be string literals. */
775 if (exprNode_isUndefined (rest) || exprNode_isUndefined (e))
777 exprNode_free (rest);
781 if (!exprNode_isStringLiteral (e))
785 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e),
786 exprNode_unparse (rest)),
788 exprNode_free (rest);
792 if (!exprNode_isStringLiteral (rest))
796 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e), exprNode_unparse (rest)),
799 exprNode_free (rest);
803 ns = cstring_concat (multiVal_forceString (exprNode_getValue (e)),
804 multiVal_forceString (exprNode_getValue (rest)));
806 multiVal_free (e->val);
807 exprData_free (e->edata, e->kind);
808 e->edata = exprData_makeLiteral (cstring_copy (ns));
809 e->val = multiVal_makeString (ns);
810 exprNode_free (rest);
815 exprNode_rawStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
817 exprNode e = exprNode_createLoc (ctype_string, loc);
818 size_t len = cstring_length (t);
820 if (context_getFlag (FLG_STRINGLITERALLEN))
822 if (len > size_fromInt (context_getValue (FLG_STRINGLITERALLEN)))
824 voptgenerror (FLG_STRINGLITERALLEN,
826 ("String literal length (%d) exceeds maximum "
827 "length (%d): \"%s\"",
829 context_getValue (FLG_STRINGLITERALLEN),
835 e->kind = XPR_STRINGLITERAL;
836 e->val = multiVal_makeString (cstring_copy (t));
837 e->edata = exprData_makeLiteral (t);
838 e->sref = sRef_makeConst (ctype_string);
840 if (context_getFlag (FLG_READONLYSTRINGS))
842 sRef_setAliasKind (e->sref, AK_STATIC, fileloc_undefined);
843 sRef_setExKind (e->sref, XO_OBSERVER, loc);
847 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
850 return (e); /* s released */
854 exprNode_wideStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
856 exprNode res = exprNode_stringLiteral (t, loc);
857 res->typ = ctype_makeWideString ();
862 exprNode_stringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
864 size_t len = size_fromInt (cstring_length (t) - 2);
865 char *ts = cstring_toCharsSafe (t);
866 char *s = cstring_toCharsSafe (cstring_create (len + 1));
868 llassert (*ts == '\"' && *(ts + len + 1) == '\"');
869 strncpy (s, ts+1, len);
872 return exprNode_rawStringLiteral (cstring_fromCharsO (s), loc);
875 exprNode exprNode_fromUIO (cstring c)
877 fileloc loc = context_getSaveLocation ();
878 exprNode e = exprNode_createPlain (ctype_unknown);
882 if (fileloc_isUndefined (loc))
884 loc = fileloc_copy (g_currentloc);
887 e->loc = loc; /* save loc was mangled */
890 if (usymtab_exists (c))
892 uentry ue = usymtab_lookupEither (c);
894 if (uentry_isDatatype (ue)
895 && uentry_isSpecified (ue))
898 (message ("%q: Specified datatype %s used in code, but not defined. "
899 "(Cannot continue reasonably from this error.)",
900 fileloc_unparse (e->loc), c));
908 llassertprint (!usymtab_exists (c), ("Entry exists: %s", c));
911 ** was supercedeGlobalEntry...is this better?
914 if (!context_inIterEnd ())
916 if (context_inMacro ())
918 if (context_getFlag (FLG_UNRECOG))
922 message ("Unrecognized identifier in macro definition: %s", c), e->loc);
926 flagcode_recordSuppressed (FLG_UNRECOG);
932 (FLG_UNRECOG, message ("Unrecognized identifier: %s", c), e->loc);
936 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
938 /* No alias errors for unrecognized identifiers */
939 sRef_setAliasKind (e->sref, AK_ERROR, loc);
944 exprNode exprNode_makeConstantString (cstring c, /*@only@*/ fileloc loc)
946 exprNode e = exprNode_createPlain (ctype_unknown);
949 e->sref = sRef_makeConst (ctype_string);
950 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
951 e->typ = ctype_string;
953 /* No alias errors for unrecognized identifiers */
954 sRef_setAliasKind (e->sref, AK_STATIC, loc);
955 sRef_setExKind (e->sref, XO_OBSERVER, loc);
960 exprNode exprNode_createId (/*@observer@*/ uentry c)
962 if (uentry_isValid (c))
964 exprNode e = exprNode_new ();
966 DPRINTF (("create id: %s", uentry_unparse (c)));
968 e->typ = uentry_getType (c);
970 if (uentry_isFunction (c)
971 && !sRef_isLocalVar (uentry_getSref (c)))
973 e->sref = sRef_undefined;
977 e->sref = uentry_getSref (c);
980 if (sRef_isStateUnknown (e->sref) && uentry_isNonLocal (c))
982 sRef_setDefined (e->sref, fileloc_undefined);
986 ** yoikes! leaving this out was a heinous bug...that would have been
987 ** caught if i had splint working first. gag!
990 e->etext = cstring_undefined;
992 if (uentry_isEitherConstant (c))
995 e->val = multiVal_copy (uentry_getConstantValue (c));
1000 e->val = multiVal_unknown ();
1003 e->edata = exprData_makeId (c);
1004 e->loc = context_getSaveLocation ();
1006 if (fileloc_isUndefined (e->loc))
1008 fileloc_free (e->loc);
1009 e->loc = fileloc_copy (g_currentloc);
1012 e->guards = guardSet_new ();
1013 e->sets = sRefSet_new ();
1014 e->msets = sRefSet_new ();
1015 e->uses = sRefSet_new ();
1017 /*> missing fields, detected by splint <*/
1018 e->exitCode = XK_NEVERESCAPE;
1019 e->isJumpPoint = FALSE;
1020 e->canBreak = FALSE;
1021 e->mustBreak = FALSE;
1023 exprNode_defineConstraints(e);
1029 return exprNode_createUnknown ();
1033 /*@notnull@*/ exprNode
1034 exprNode_fromIdentifier (/*@observer@*/ uentry c)
1038 if (context_justPopped ()) /* watch out! c could be dead */
1040 uentry ce = usymtab_lookupSafe (LastIdentifier ());
1042 if (uentry_isValid (ce))
1048 llbuglit ("Looks like Aunt Millie forgot to walk to dog again.");
1052 ret = exprNode_fromIdentifierAux (c);
1057 static void exprNode_checkStringLiteralLength (ctype t1, exprNode e2)
1059 multiVal mval = exprNode_getValue (e2);
1063 if (ctype_isFixedArray (t1))
1065 int nelements = long_toInt (ctype_getArraySize (t1));
1067 llassert (multiVal_isString (mval));
1068 slit = multiVal_forceString (mval);
1070 len = cstring_lengthExpandEscapes (slit);
1072 llassert (exprNode_isDefined (e2));
1074 if (len == nelements)
1078 temp = cstring_expandEscapes (slit);
1080 if (temp[len-1] == '\0')
1083 (FLG_STRINGLITNOROOMFINALNULL,
1084 message ("String literal with %d character%& "
1085 "is assigned to %s (no room for final null terminator): %s",
1088 exprNode_unparse (e2)),
1094 (FLG_STRINGLITNOROOM,
1095 message ("String literal with %d character%& "
1096 "is assigned to %s (no room for null terminator): %s",
1099 exprNode_unparse (e2)),
1103 else if (len > nelements)
1106 (FLG_STRINGLITTOOLONG,
1107 message ("String literal with %d character%& (counting null terminator) "
1108 "is assigned to %s (insufficient storage available): %s",
1111 exprNode_unparse (e2)),
1114 else if (len < nelements - 1)
1117 (FLG_STRINGLITSMALLER,
1118 message ("String literal with %d character%& is assigned to %s (possible waste of storage): %s",
1121 exprNode_unparse (e2)),
1131 static /*@only@*/ /*@notnull@*/ exprNode
1132 exprNode_fromIdentifierAux (/*@observer@*/ uentry c)
1134 exprNode e = exprNode_createId (c);
1137 uentry_setUsed (c, e->loc);
1139 if (uentry_isVar (c) && sRef_isFileOrGlobalScope (sr))
1141 checkGlobUse (c, FALSE, e);
1148 exprNode_isZero (exprNode e)
1150 if (exprNode_isDefined (e))
1152 multiVal m = exprNode_getValue (e);
1154 if (multiVal_isInt (m))
1156 return (multiVal_forceInt (m) == 0);
1164 exprNode_isNonNegative (exprNode e)
1166 if (exprNode_isDefined (e))
1168 multiVal m = exprNode_getValue (e);
1170 if (multiVal_isInt (m))
1172 return (multiVal_forceInt (m) >= 0);
1176 ** This is not always true if programmer defines enum
1177 ** values, but then the constant should be known.
1180 if (ctype_isEnum (ctype_realType (e->typ)))
1190 ** a[x] - uses a but NOT a[]
1191 ** result sref = a[] (set/use in assignment)
1193 ** The syntax x[a] is also legal in C, and has the same
1194 ** semantics. If ind is an array, and arr is an int, flip
1199 exprNode_arrayFetch (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
1202 ** error in arr, error propagates (no new messages)
1203 ** error in ind, assume valid and continue
1206 DPRINTF (("Array fetch: %s / %s",
1207 exprNode_unparse (e1), exprNode_unparse (e2)));
1209 if (exprNode_isError (e1))
1212 return (exprNode_makeError ());
1218 ctype carr = exprNode_getType (e1);
1219 ctype crarr = ctype_realType (carr);
1222 ** this sets up funny aliasing, that leads to spurious
1223 ** splint errors. Hence, the i2 comments.
1226 /* evans 2001-09-09 added ctype_isKnown so there is no swap when e1 type is unknown */
1227 if (ctype_isKnown (crarr)
1228 && !ctype_isRealArray (crarr)
1229 && ctype_isRealNumeric (crarr)
1230 && !exprNode_isError (e2)
1231 && ctype_isRealAP (exprNode_getType (e2))) /* fetch like 3[a] */
1236 carr = exprNode_getType (arr);
1237 crarr = ctype_realType (carr);
1245 DPRINTF (("arr: %s", exprNode_unparse (arr)));
1247 if (sRef_possiblyNull (arr->sref))
1249 if (!usymtab_isGuarded (arr->sref))
1251 if (optgenerror (FLG_NULLDEREF,
1252 message ("Index of %s pointer %q: %s",
1253 sRef_nullMessage (arr->sref),
1254 sRef_unparse (arr->sref),
1255 exprNode_unparse (arr)),
1258 DPRINTF (("ref: %s", sRef_unparseFull (arr->sref)));
1259 sRef_showNullInfo (arr->sref);
1261 /* suppress future messages */
1262 sRef_setNullError (arr->sref);
1267 if (exprNode_isError (ind))
1269 if ((ctype_isArrayPtr (crarr)
1270 && !ctype_isFunction (crarr))
1271 || ctype_isUnknown (carr))
1273 exprNode ret = exprNode_createPartialCopy (arr);
1275 if (ctype_isKnown (carr))
1277 ret->typ = ctype_baseArrayPtr (crarr);
1281 ret->typ = ctype_unknown;
1284 ret->sref = sRef_makeArrayFetch (arr->sref);
1286 ret->kind = XPR_FETCH;
1289 ** Because of funny aliasing (when arr and ind are
1290 ** flipped) spurious errors would be reported here.
1293 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1294 checkSafeUse (ret, arr->sref);
1299 voptgenerror (FLG_TYPE,
1300 message ("Array fetch from non-array (%t): %s[%s]", carr,
1301 exprNode_unparse (e1), exprNode_unparse (e2)),
1303 exprNode_free (arr);
1304 return (exprNode_makeError ());
1309 if (!ctype_isForceRealInt (&(ind->typ)))
1311 ctype rt = ctype_realType (ind->typ);
1313 if (ctype_isChar (rt))
1317 message ("Array fetch using non-integer, %t: %s[%s]",
1319 exprNode_unparse (e1), exprNode_unparse (e2)),
1322 else if (ctype_isEnum (rt))
1326 message ("Array fetch using non-integer, %t: %s[%s]",
1328 exprNode_unparse (e1), exprNode_unparse (e2)),
1335 message ("Array fetch using non-integer, %t: %s[%s]",
1337 exprNode_unparse (e1), exprNode_unparse (e2)),
1341 multiVal_free (ind->val);
1342 ind->val = multiVal_unknown ();
1345 if (ctype_isArrayPtr (crarr) && !ctype_isFunction (crarr))
1347 exprNode ret = exprNode_createSemiCopy (arr);
1348 multiVal m = exprNode_getValue (ind);
1350 ret->typ = ctype_baseArrayPtr (crarr);
1351 ret->kind = XPR_FETCH;
1353 if (multiVal_isInt (m))
1355 int i = (int) multiVal_forceInt (m);
1357 if (sRef_isValid (arr->sref)) {
1358 ret->sref = sRef_makeArrayFetchKnown (arr->sref, i);
1360 ret->sref = sRef_undefined;
1365 ret->sref = sRef_makeArrayFetch (arr->sref);
1368 ret->sets = sRefSet_realNewUnion (arr->sets, ind->sets);
1369 ret->msets = sRefSet_realNewUnion (arr->msets, ind->msets);
1370 ret->uses = sRefSet_realNewUnion (arr->uses, ind->uses);
1372 /* (see comment on spurious errors above) */
1373 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1375 exprNode_checkUse (ret, ind->sref, ind->loc);
1376 exprNode_checkUse (ret, arr->sref, arr->loc);
1382 if (ctype_isUnknown (carr))
1384 exprNode ret = exprNode_createPartialCopy (arr);
1386 ret->kind = XPR_FETCH;
1387 ret->typ = ctype_unknown;
1388 ret->sets = sRefSet_union (ret->sets, ind->sets);
1389 ret->msets = sRefSet_union (ret->msets, ind->msets);
1390 ret->uses = sRefSet_union (ret->uses, ind->uses);
1392 /* (see comment on spurious errors above) */
1393 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1395 exprNode_checkUse (ret, ind->sref, ind->loc);
1396 exprNode_checkUse (ret, arr->sref, arr->loc);
1403 message ("Array fetch from non-array (%t): %s[%s]", carr,
1404 exprNode_unparse (e1), exprNode_unparse (e2)),
1407 exprNode_free (arr);
1408 exprNode_free (ind);
1410 return (exprNode_makeError ());
1420 checkArgs (uentry fcn, /*@dependent@*/ exprNode f, ctype t,
1421 exprNodeList args, exprNode ret)
1423 return (checkArgsReal (fcn, f, ctype_argsFunction (t), args, FALSE, ret));
1427 ** checkPrintfArgs --- checks arguments for printf-like functions
1428 ** Arguments before ... have already been checked.
1429 ** The argument before the ... is a char *.
1430 ** argno is the format string argument.
1434 checkPrintfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1435 exprNodeList args, exprNode ret, int argno)
1438 ** the last argument before the elips is the format string
1443 int nargs = exprNodeList_size (args);
1444 uentryList params = uentry_getParams (fcn);
1448 ** These should be ensured by checkSpecialFunction
1451 llassert (uentryList_size (params) == argno + 1);
1452 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1454 a = exprNodeList_getN (args, argno - 1);
1455 formatloc = fileloc_copy (exprNode_loc (a));
1457 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1458 && exprNode_knownStringValue (a))
1460 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1461 char *code = format;
1464 nargs = exprNodeList_size (args);
1466 while ((code = strchr (code, '%')) != NULL)
1468 char *origcode = code;
1469 cstring codetext = cstring_newEmpty ();
1470 char key = *(++code);
1471 ctype modtype = ctype_int;
1472 bool modified = FALSE;
1474 fileloc_addColumn (formatloc, code - ocode);
1476 codetext = cstring_appendChar (codetext, key);
1479 while (isFlagKey (key))
1482 codetext = cstring_appendChar (codetext, key);
1483 fileloc_incColumn (formatloc);
1486 if (key == 'm') /* skipped in syslog */
1491 /* ignore field width */
1492 while (isdigit ((int) key) != 0)
1495 codetext = cstring_appendChar (codetext, key);
1496 fileloc_incColumn (formatloc);
1499 /* ignore precision */
1503 codetext = cstring_appendChar (codetext, key);
1504 fileloc_incColumn (formatloc);
1507 ** In printf, '*' means: read the next arg as an int for the
1508 ** field width. This seems to be missing from my copy of the
1509 ** standard x3.159-1989. Setion 4.9.6.1 refers to * (described
1510 ** later) but never does.
1515 ; /* don't do anything --- handle later */
1519 while (isdigit ((int) key) != 0)
1522 codetext = cstring_appendChar (codetext, key);
1523 fileloc_incColumn (formatloc);
1530 modtype = ctype_sint; /* short */
1532 codetext = cstring_appendChar (codetext, key);
1533 fileloc_incColumn (formatloc);
1535 else if (key == 'l' || key == 'L')
1537 modtype = ctype_lint; /* long */
1539 codetext = cstring_appendChar (codetext, key);
1540 fileloc_incColumn (formatloc);
1542 if (key == 'l' || key == 'L') {
1543 modtype = ctype_llint; /* long long */
1545 codetext = cstring_appendChar (codetext, key);
1546 fileloc_incColumn (formatloc);
1554 /* now, key = type of conversion to apply */
1556 fileloc_incColumn (formatloc);
1564 message ("No argument corresponding to %q format "
1565 "code %d (%%%s): \"%s\"",
1566 uentry_getName (fcn),
1568 cstring_fromChars (format)),
1571 if (fileloc_isDefined (formatloc)
1572 && context_getFlag (FLG_SHOWCOL))
1574 llgenindentmsg (cstring_makeLiteral ("Corresponding format code"),
1582 a = exprNodeList_getN (args, i);
1585 if (!exprNode_isError (a))
1591 case '*': /* int argument for fieldwidth */
1592 expecttype = ctype_int;
1593 *(--code) = '%'; /* convert it for next code */
1594 fileloc_subColumn (formatloc, 1);
1595 /*@switchbreak@*/ break;
1598 expecttype = ctype_combine (ctype_uint, modtype);
1599 /*@switchbreak@*/ break;
1601 case 'i': /* int argument */
1603 expecttype = ctype_combine (ctype_int, modtype);
1604 /*@switchbreak@*/ break;
1606 case 'x': /* unsigned int */
1608 expecttype = ctype_combine (ctype_uint, modtype);
1610 /*@switchbreak@*/ break;
1616 case 'f': /* double */
1617 expecttype = ctype_combine (ctype_double, modtype);
1618 /*@switchbreak@*/ break;
1620 case 'c': /* int converted to char (check its a char?) */
1621 expecttype = ctype_makeConj (ctype_int,
1622 ctype_makeConj (ctype_char,
1625 /* evans 2001-10-05 - changed to reflect correct ISO spec:
1626 int converted to char */
1628 /* expecttype = ctype_makeConj (ctype_char, ctype_uchar); */
1629 /*@switchbreak@*/ break;
1631 case 's': /* string */
1632 expecttype = ctype_string;
1633 /*@switchbreak@*/ break;
1636 while (((key = *(++code)) != ']')
1639 codetext = cstring_appendChar (codetext, key);
1640 fileloc_incColumn (formatloc);
1646 (message ("Bad character set format: %s",
1647 cstring_fromChars (origcode)));
1650 expecttype = ctype_string;
1651 /*@switchbreak@*/ break;
1653 case 'p': /* pointer */
1654 expecttype = ctype_makePointer (ctype_void);
1655 uentry_setDefState (regArg, SS_RELDEF); /* need not be defined */
1656 sRef_setPosNull (uentry_getSref (regArg), fileloc_undefined); /* could be null */
1657 /*@switchbreak@*/ break;
1659 case 'n': /* pointer to int, modified by call! */
1660 expecttype = ctype_combine (ctype_makePointer (ctype_int), modtype);
1662 uentry_setDefState (regArg, SS_ALLOCATED); /* corresponds to out */
1663 /*@switchbreak@*/ break;
1665 case 'm': /* in a syslog, it doesn't consume an argument */
1666 /* should check we're really doing syslog */
1668 /*@switchbreak@*/ break;
1672 expecttype = ctype_unknown;
1676 message ("Unrecognized format code: %s",
1677 cstring_fromChars (origcode)),
1678 fileloc_isDefined (formatloc)
1679 ? formatloc : g_currentloc);
1681 /*@switchbreak@*/ break;
1684 if (!(exprNode_matchArgType (expecttype, a)))
1686 if (ctype_isVoidPointer (expecttype)
1687 && ctype_isRealAbstract (a->typ)
1688 && (context_getFlag (FLG_ABSTVOIDP)))
1694 if (llgenformattypeerror
1695 (expecttype, exprNode_undefined,
1697 message ("Format argument %d to %q (%%%s) expects "
1700 uentry_getName (fcn),
1703 a->typ, exprNode_unparse (a)),
1706 if (fileloc_isDefined (formatloc)
1707 && context_getFlag (FLG_SHOWCOL))
1710 (cstring_makeLiteral
1711 ("Corresponding format code"),
1718 uentry_setType (regArg, expecttype);
1719 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
1721 if (ctype_equal (expecttype, ctype_string))
1723 exprNode_checkUse (a, sRef_makePointer (a->sref), a->loc);
1726 uentry_setType (regArg, ctype_unknown);
1727 uentry_fixupSref (regArg);
1731 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1742 cstring_free (codetext);
1747 voptgenerror (FLG_TYPE,
1748 message ("Format string for %q has %d arg%&, given %d",
1749 uentry_getName (fcn), i - argno, nargs - argno),
1755 /* no checking possible for compile-time unknown format strings */
1756 if (exprNode_isDefined (a))
1760 message ("Format string parameter to %s is not a compile-time constant: %s",
1761 exprNode_unparse (f),
1762 exprNode_unparse (a)),
1767 fileloc_free (formatloc);
1771 checkScanfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1772 exprNodeList args, exprNode ret, int argno)
1776 int nargs = exprNodeList_size (args);
1777 uentryList params = uentry_getParams (fcn);
1781 ** These should be ensured by checkSpecialFunction
1784 llassert (uentryList_size (params) == argno + 1);
1785 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1787 a = exprNodeList_getN (args, argno - 1);
1788 formatloc = fileloc_copy (exprNode_loc (a));
1790 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1791 && exprNode_knownStringValue (a))
1793 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1794 char *code = format;
1797 nargs = exprNodeList_size (args);
1799 while ((code = strchr (code, '%')) != NULL)
1801 char *origcode = code;
1802 char key = *(++code);
1803 cstring codetext = cstring_newEmpty ();
1804 ctype modtype = ctype_int;
1805 char modifier = '\0';
1806 bool modified = TRUE;
1807 bool ignore = FALSE;
1809 codetext = cstring_appendChar (codetext, key);
1810 fileloc_addColumn (formatloc, code - ocode);
1813 ** this is based on ANSI standard library description of fscanf
1814 ** (from ANSI standard X3.159-1989, 4.9.6.1)
1817 /* '*' suppresses assignment (does not need match argument) */
1822 codetext = cstring_appendChar (codetext, key);
1825 fileloc_incColumn (formatloc);
1828 /* ignore field width */
1829 while (isdigit ((int) key) != 0)
1832 codetext = cstring_appendChar (codetext, key);
1833 fileloc_incColumn (formatloc);
1838 modtype = ctype_sint; /* short */
1840 codetext = cstring_appendChar (codetext, key);
1841 fileloc_incColumn (formatloc);
1843 else if (key == 'l' || key == 'L')
1845 modtype = ctype_lint; /* long */
1849 codetext = cstring_appendChar (codetext, key);
1851 fileloc_incColumn (formatloc);
1853 if (key == 'l' || key == 'L') {
1854 modtype = ctype_llint; /* long long */
1856 codetext = cstring_appendChar (codetext, key);
1857 fileloc_incColumn (formatloc);
1865 /* now, key = type of conversion to apply */
1867 fileloc_incColumn (formatloc);
1881 message ("No argument corresponding to %q format "
1882 "code %d (%%%s): \"%s\"",
1883 uentry_getName (fcn),
1885 cstring_fromChars (format)),
1888 if (fileloc_isDefined (formatloc)
1889 && context_getFlag (FLG_SHOWCOL))
1892 (cstring_makeLiteral ("Corresponding format code"),
1900 a = exprNodeList_getN (args, i);
1903 if (!exprNode_isError (a))
1909 case '*': /* int argument for fieldwidth */
1910 expecttype = ctype_makePointer (ctype_int);
1911 *(--code) = '%'; /* convert it for next code */
1912 fileloc_subColumn (formatloc, 1);
1913 /*@switchbreak@*/ break;
1916 expecttype = ctype_makePointer (ctype_combine (ctype_uint, modtype));
1917 /*@switchbreak@*/ break;
1922 case 'X': /* unsigned int */
1923 expecttype = ctype_makePointer (ctype_combine (ctype_int, modtype));
1924 /*@switchbreak@*/ break;
1931 /* printf is double, scanf is float! */
1933 if (modifier == 'l')
1935 expecttype = ctype_makePointer (ctype_double);
1937 else if (modifier == 'L')
1939 expecttype = ctype_makePointer (ctype_ldouble);
1943 llassert (modifier == '\0');
1944 expecttype = ctype_makePointer (ctype_float);
1946 /*@switchbreak@*/ break;
1948 case 'c': /* int converted to char (check its a char?) */
1949 expecttype = ctype_makePointer (ctype_makeConj (ctype_char, ctype_uchar));
1950 /*@switchbreak@*/ break;
1952 case 's': /* string */
1953 expecttype = ctype_string;
1954 /*@switchbreak@*/ break;
1958 while (((key = *(++code)) != ']')
1961 codetext = cstring_appendChar (codetext, key);
1962 fileloc_incColumn (formatloc);
1968 (message ("Bad character set format: %s",
1969 cstring_fromChars (origcode)));
1972 expecttype = ctype_string;
1973 /*@switchbreak@*/ break;
1976 case 'p': /* pointer */
1979 message ("Format code should not be used in scanf: %s",
1980 cstring_fromChars (origcode)),
1981 fileloc_isDefined (formatloc)
1982 ? formatloc : g_currentloc);
1984 expecttype = ctype_unknown;
1985 /*@switchbreak@*/ break;
1987 case 'n': /* pointer to int, modified by call! */
1988 expecttype = ctype_makePointer (ctype_int);
1989 /*@switchbreak@*/ break;
1992 expecttype = ctype_unknown;
1996 message ("Unrecognized format code: %s",
1997 cstring_fromChars (origcode)),
1998 fileloc_isDefined (formatloc)
1999 ? formatloc : g_currentloc);
2001 /*@switchbreak@*/ break;
2004 if (!(exprNode_matchArgType (expecttype, a)))
2006 if (ctype_isVoidPointer (expecttype)
2007 && ctype_isRealAbstract (a->typ)
2008 && (context_getFlag (FLG_ABSTVOIDP)))
2014 if (llgenformattypeerror
2015 (expecttype, exprNode_undefined,
2017 message ("Format argument %d to %q (%%%s) expects "
2020 uentry_getName (fcn),
2021 codetext, expecttype,
2022 a->typ, exprNode_unparse (a)),
2025 if (fileloc_isDefined (formatloc)
2026 && context_getFlag (FLG_SHOWCOL))
2029 (cstring_makeLiteral
2030 ("Corresponding format code"),
2037 uentry_setType (outArg, expecttype);
2038 checkOneArg (outArg, a, f, FALSE, i+1, nargs);
2039 uentry_setType (outArg, ctype_unknown);
2040 uentry_fixupSref (outArg);
2044 exprNode_checkCallModifyVal (a->sref, args, f, ret);
2049 /* a->sref = defref; */
2056 cstring_free (codetext);
2061 voptgenerror (FLG_TYPE,
2062 message ("Format string for %q has %d arg%&, given %d",
2063 uentry_getName (fcn), i - argno, nargs - argno),
2069 /* no checking possible for compile-time unknown format strings */
2072 fileloc_free (formatloc);
2076 checkMessageArgs (/*@notnull@*/ /*@dependent@*/ exprNode f,
2079 /*@unused@*/ int argno)
2082 ** the last argument before the elips is the format string
2085 int nargs = exprNodeList_size (args);
2090 a = exprNodeList_getN (args, argno - 1);
2091 formatloc = fileloc_copy (exprNode_loc (a));
2093 if (ctype_isUnknown (cstringType)) {
2094 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
2096 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
2100 if (ctype_isUnknown (ctypeType)) {
2101 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
2103 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
2107 if (ctype_isUnknown (filelocType)) {
2108 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
2110 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
2114 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
2115 && exprNode_knownStringValue (a))
2117 cstring format = multiVal_forceString (exprNode_getValue (a));
2118 char *code = cstring_toCharsSafe (format);
2121 nargs = exprNodeList_size (args);
2123 while ((code = strchr (code, '%')) != NULL)
2125 char *origcode = code;
2126 char key = *(++code);
2127 cstring codetext = cstring_newEmpty ();
2128 bool isOnly = FALSE;
2130 codetext = cstring_appendChar (codetext, key);
2132 fileloc_addColumn (formatloc, code - ocode);
2134 while (key >= '0' && key <= '9')
2137 codetext = cstring_appendChar (codetext, key);
2138 fileloc_incColumn (formatloc);
2142 fileloc_incColumn (formatloc);
2146 if (key == '&') /* plural marker */
2155 message ("Message missing format arg %d (%%%s): \"%s\"",
2156 i + 1, codetext, format),
2162 a = exprNodeList_getN (args, i);
2166 if (!exprNode_isError (a))
2170 /*@-loopswitchbreak@*/
2176 expecttype = ctype_char; break;
2178 expecttype = cstringType; break;
2180 expecttype = cstringType; isOnly = TRUE; break;
2182 expecttype = cstringType; isOnly = TRUE; break;
2183 case 'd': expecttype = ctype_int; break;
2184 case 'u': expecttype = ctype_uint; break;
2185 case 'w': expecttype = ctype_ulint; break;
2186 case 'f': expecttype = ctype_float; break;
2187 case 'b': expecttype = ctype_bool; break;
2188 case 't': expecttype = ctypeType; break;
2189 case 'l': expecttype = filelocType; break;
2190 case '&': /* a wee bit of a hack methinks */
2191 expecttype = ctype_int;
2193 case 'r': expecttype = ctype_bool; break;
2195 expecttype = ctype_unknown;
2198 message ("Unrecognized format code: %s",
2199 cstring_fromChars (origcode)),
2200 fileloc_isDefined (formatloc)
2201 ? formatloc : g_currentloc);
2204 /*@=loopswitchbreak@*/
2206 if (!(exprNode_matchArgType (expecttype, a)))
2208 if (ctype_isVoidPointer (expecttype)
2209 && ctype_isRealAbstract (a->typ)
2210 && (context_getFlag (FLG_ABSTVOIDP)))
2216 if (llgenformattypeerror
2217 (expecttype, exprNode_undefined,
2219 message ("Format argument %d to %q (%%%s) expects "
2222 uentry_getName (fcn),
2223 codetext, expecttype,
2224 a->typ, exprNode_unparse (a)),
2227 if (fileloc_isDefined (formatloc)
2228 && context_getFlag (FLG_SHOWCOL))
2231 (cstring_makeLiteral
2232 ("Corresponding format code"),
2239 if (ctype_equal (expecttype, cstringType))
2243 checkOneArg (csOnlyArg, a, f, FALSE, i+1, nargs);
2244 uentry_fixupSref (csOnlyArg);
2248 checkOneArg (csArg, a, f, FALSE, i+1, nargs);
2249 uentry_fixupSref (csArg);
2254 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
2255 uentry_fixupSref (regArg);
2261 cstring_free (codetext);
2266 voptgenerror (FLG_TYPE,
2267 message ("Format string for %q has %d arg%&, given %d",
2268 uentry_getName (fcn), i - argno, nargs -argno),
2274 /* no checking possible for compile-time unknown format strings */
2277 fileloc_free (formatloc);
2281 checkExpressionDefinedAux (/*@notnull@*/ exprNode e1,
2282 /*@notnull@*/ exprNode e2,
2288 bool hadUncon = FALSE;
2290 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e1->sref)) &&
2291 sRefSet_hasUnconstrained (sets2))
2294 (FLG_EVALORDERUNCON,
2296 ("Expression may have undefined behavior (%q used in right operand "
2297 "may set global variable %q used in left operand): %s %s %s",
2298 sRefSet_unparseUnconstrained (sets2),
2299 sRef_unparse (sRef_getRootBase (e1->sref)),
2300 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2304 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e2->sref)) &&
2305 sRefSet_hasUnconstrained (sets1))
2308 (FLG_EVALORDERUNCON,
2310 ("Expression has undefined behavior (%q used in left operand "
2311 "may set global variable %q used in right operand): %s %s %s",
2312 sRefSet_unparseUnconstrained (sets1),
2313 sRef_unparse (e2->sref),
2314 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2318 sRefSet_realElements (e1->uses, sr)
2320 if (sRef_isMeaningful (sr) && sRefSet_member (sets2, sr))
2325 ("Expression has undefined behavior (left operand uses %q, "
2326 "modified by right operand): %s %s %s",
2328 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2331 } end_sRefSet_realElements;
2333 sRefSet_realElements (sets1, sr)
2335 if (sRef_isMeaningful (sr))
2337 if (sRef_same (sr, e2->sref))
2342 ("Expression has undefined behavior (value of right operand "
2343 "modified by left operand): %s %s %s",
2344 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2347 else if (sRefSet_member (e2->uses, sr))
2352 ("Expression has undefined behavior (left operand modifies %q, "
2353 "used by right operand): %s %s %s",
2355 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2360 if (sRefSet_member (sets2, sr))
2362 if (sRef_isUnconstrained (sr))
2370 hadUncon = optgenerror
2371 (FLG_EVALORDERUNCON,
2373 ("Expression may have undefined behavior. Left operand "
2374 "calls %q; right operand calls %q. The unconstrained "
2375 "functions may modify global state used by "
2376 "the other operand): %s %s %s",
2377 sRefSet_unparseUnconstrained (sets1),
2378 sRefSet_unparseUnconstrained (sets2),
2379 exprNode_unparse (e1), lltok_unparse (op),
2380 exprNode_unparse (e2)),
2389 ("Expression has undefined behavior (both "
2390 "operands modify %q): %s %s %s",
2392 exprNode_unparse (e1),
2393 lltok_unparse (op), exprNode_unparse (e2)),
2399 } end_sRefSet_realElements;
2402 static void checkExpressionDefined (exprNode e1, exprNode e2, lltok op)
2404 bool hasError = FALSE;
2406 if (exprNode_isError (e1) || exprNode_isError (e2))
2411 if (sRefSet_member (e2->sets, e1->sref))
2413 if (e2->kind == XPR_CALL)
2419 hasError = optgenerror
2421 message ("Expression has undefined behavior "
2422 "(value of left operand %s is modified "
2423 "by right operand %s): %s %s %s",
2424 exprNode_unparse (e1),
2425 exprNode_unparse (e2),
2426 exprNode_unparse (e1), lltok_unparse (op),
2427 exprNode_unparse (e2)),
2432 if (context_getFlag (FLG_EVALORDERUNCON))
2434 if (sRefSet_member (e2->msets, e1->sref))
2436 if (e2->kind == XPR_CALL)
2442 hasError = optgenerror
2445 ("Expression has undefined behavior (value of left "
2446 "operand may be modified by right operand): %s %s %s",
2447 exprNode_unparse (e1), lltok_unparse (op),
2448 exprNode_unparse (e2)),
2456 checkExpressionDefinedAux (e1, e2, e1->sets, e2->sets, op, FLG_EVALORDER);
2458 if (context_maybeSet (FLG_EVALORDERUNCON))
2460 checkExpressionDefinedAux (e1, e2, e1->msets,
2461 e2->msets, op, FLG_EVALORDERUNCON);
2466 static void checkSequencing (exprNode p_f, exprNodeList p_args);
2469 checkArgsReal (uentry fcn, /*@dependent@*/ exprNode f, uentryList cl,
2470 exprNodeList args, bool isIter, exprNode ret)
2474 if (!exprNode_isError (f))
2476 if (!uentryList_isMissingParams (cl))
2478 int nargs = exprNodeList_size (args);
2479 int expectargs = uentryList_size (cl);
2483 if (expectargs == 0)
2491 message ("Iter %q invoked with %d args, "
2493 uentry_getName (fcn),
2501 message ("Function %s called with %d args, "
2503 exprNode_unparse (f), nargs),
2510 last = uentry_getType (uentryList_getN (cl, expectargs - 1));
2512 exprNodeList_reset (args);
2514 uentryList_elements (cl, current)
2516 ctype ct = uentry_getType (current);
2519 if (ctype_isElips (ct))
2522 ** do special checking for printf/scanf library functions
2524 ** this is kludgey code, just for handling the special case
2528 if (uentry_isPrintfLike (fcn))
2530 checkPrintfArgs (f, fcn, args, ret, i);
2533 else if (uentry_isScanfLike (fcn))
2535 checkScanfArgs (f, fcn, args, ret, i);
2538 else if (uentry_isMessageLike (fcn))
2540 checkMessageArgs (f, fcn, args, i);
2545 llassert (!uentry_isSpecialFunction (fcn));
2548 nargs = expectargs; /* avoid errors */
2553 if (i >= nargs) break;
2555 a = exprNodeList_current (args);
2556 exprNodeList_advance (args);
2560 if (exprNode_isError (a))
2567 probably necessary? I'm not sure about this one
2568 checkMacroParen (a);
2571 f->guards = guardSet_union (f->guards, a->guards);
2573 DPRINTF (("match arg: %s / %s", ctype_unparse (ct), ctype_unparse (a->typ)));
2575 if (!(exprNode_matchArgType (ct, a)))
2577 DPRINTF (("Args mismatch!"));
2579 if (ctype_isVoidPointer (ct)
2580 && (ctype_isPointer (a->typ)
2581 && (ctype_isRealAbstract (ctype_baseArrayPtr (a->typ)))))
2586 ("Pointer to abstract type (%t) used "
2588 "(arg %d to %q): %s",
2590 uentry_getName (fcn),
2591 exprNode_unparse (a)),
2599 (ct, exprNode_undefined,
2602 ("Iter %q expects arg %d to "
2603 "be %t gets %t: %s",
2604 uentry_getName (fcn),
2605 i, ct, a->typ, exprNode_unparse (a)),
2616 ("Function %q expects arg %d to be %t gets %t: %s",
2617 uentry_getName (fcn),
2618 i, ct, a->typ, exprNode_unparse (a)),
2621 DPRINTF (("Types: %s / %s",
2623 ctype_unparse (a->typ)));
2627 ** Clear null marker for abstract types.
2628 ** (It is not revealed, so suppress future messages.)
2631 if (ctype_isAbstract (a->typ))
2633 sRef_setNullUnknown (exprNode_getSref (a), a->loc);
2640 } end_uentryList_elements ;
2643 if (expectargs != nargs) /* note: not != since we may have ... */
2645 if (ctype_isElips (last))
2649 message ("Function %s called with %d args, expects at least %d",
2650 exprNode_unparse (f),
2651 nargs, expectargs - 1),
2660 message ("Iter %q invoked with %d args, expects %d",
2661 uentry_getName (fcn), nargs, expectargs),
2668 message ("Function %s called with %d args, expects %d",
2669 exprNode_unparse (f),
2682 ** Check for undefined code sequences in function arguments:
2684 ** one parameter sets something used by another parameter
2685 ** one parameter sets something set by another parameter
2689 checkSequencingOne (exprNode f, exprNodeList args,
2690 /*@notnull@*/ exprNode el, int argno)
2693 ** Do second loop, iff +undefunspec
2697 int numloops = context_maybeSet (FLG_EVALORDERUNCON) ? 2 : 1;
2699 for (checkloop = 0; checkloop < numloops; checkloop++)
2705 thissets = el->sets;
2709 llassert (checkloop == 1);
2710 thissets = el->msets;
2713 sRefSet_realElements (thissets, thisset)
2717 /*@access exprNodeList@*/
2718 for (j = 0; j < args->nelements; j++)
2720 exprNode jl = args->elements[j];
2721 int thisargno = j + 1;
2723 if (thisargno != argno && exprNode_isDefined (jl))
2725 sRefSet otheruses = jl->uses;
2727 if (sRef_isFileOrGlobalScope (sRef_getRootBase (jl->sref)) &&
2728 sRefSet_hasUnconstrained (thissets))
2731 (FLG_EVALORDERUNCON,
2734 ("%q used in argument %d may set "
2735 "global variable %q used by argument %d: %s(%q)",
2736 cstring_capitalizeFree (sRefSet_unparseUnconstrained (thissets)),
2739 sRef_unparse (sRef_getRootBase (jl->sref)),
2741 exprNode_unparse (f), exprNodeList_unparse (args)),
2745 if (sRefSet_member (otheruses, thisset))
2747 if (sRef_isUnconstrained (thisset))
2750 (FLG_EVALORDERUNCON,
2752 ("Unconstrained functions used in arguments %d (%q) "
2753 "and %d (%s) may modify "
2754 "or use global state in undefined way: %s(%q)",
2756 sRefSet_unparseUnconstrainedPlain (otheruses),
2758 sRef_unconstrainedName (thisset),
2759 exprNode_unparse (f),
2760 exprNodeList_unparse (args)),
2768 ("Argument %d modifies %q, used by argument %d "
2769 "(order of evaluation of actual parameters is "
2770 "undefined): %s(%q)",
2771 argno, sRef_unparse (thisset), thisargno,
2772 exprNode_unparse (f), exprNodeList_unparse (args)),
2778 sRefSet othersets = jl->sets;
2780 if (sRefSet_member (othersets, thisset))
2782 if (sRef_isUnconstrained (thisset))
2785 (FLG_EVALORDERUNCON,
2787 ("Unconstrained functions used in "
2788 "arguments %d (%q) and %d (%s) may modify "
2789 "or use global state in undefined way: %s(%q)",
2791 sRefSet_unparseUnconstrainedPlain (othersets),
2793 sRef_unconstrainedName (thisset),
2794 exprNode_unparse (f), exprNodeList_unparse (args)),
2802 ("Argument %d modifies %q, set by argument %d (order of"
2803 " evaluation of actual parameters is undefined): %s(%q)",
2804 argno, sRef_unparse (thisset), thisargno,
2805 exprNode_unparse (f), exprNodeList_unparse (args)),
2812 /*@noaccess exprNodeList@*/
2813 } end_sRefSet_realElements;
2818 checkSequencing (exprNode f, exprNodeList args)
2820 if (exprNodeList_size (args) > 1)
2825 /*@access exprNodeList*/
2827 for (i = 0; i < args->nelements; i++)
2829 el = args->elements[i];
2831 if (!exprNode_isError (el))
2833 checkSequencingOne (f, args, el, i + 1);
2836 /*@noaccess exprNodeList*/
2841 ** requires le = exprNode_getUentry (f)
2845 checkGlobMods (/*@notnull@*/ /*@dependent@*/ exprNode f,
2846 uentry le, exprNodeList args,
2847 /*@notnull@*/ exprNode ret, int specialArgs)
2849 bool isSpec = FALSE;
2850 bool hasMods = FALSE;
2852 globSet usesGlobs = globSet_undefined;
2853 sRefSet mods = sRefSet_undefined;
2854 bool freshMods = FALSE;
2855 uentryList params = uentryList_undefined;
2857 DPRINTF (("Check glob mods: %s", exprNode_unparse (ret)));
2860 ** check globals and modifies
2865 if (!uentry_isValid (le))
2867 ctype fr = ctype_realType (f->typ);
2869 if (ctype_isFunction (fr))
2871 params = ctype_argsFunction (fr);
2875 params = uentryList_missingParams;
2878 if (!context_getFlag (FLG_MODNOMODS)
2879 && !context_getFlag (FLG_GLOBUNSPEC))
2881 checkUnspecCall (f, params, args);
2887 fname = uentry_rawName (le);
2891 if (uentry_isFunction (le))
2893 params = uentry_getParams (le);
2894 mods = uentry_getMods (le);
2895 hasMods = uentry_hasMods (le);
2896 usesGlobs = uentry_getGlobs (le);
2897 isSpec = uentry_isSpecified (le);
2899 else /* not a function */
2901 ctype ct = ctype_realType (uentry_getType (le));
2903 llassertprint (uentry_isVar (le) && ctype_isFunction (ct),
2904 ("checkModGlobs: uentry not a function: %s",
2905 uentry_unparse (le)));
2907 params = ctype_argsFunction (ct);
2908 return; /*@32 ! remove this? */
2917 globSet_allElements (usesGlobs, el)
2919 if (sRef_isValid (el))
2921 if (sRef_isInternalState (el) || sRef_isSystemState (el))
2923 context_usedGlobal (el);
2924 exprNode_checkUse (f, el, f->loc);
2926 if (context_checkInternalUse ())
2928 if (!context_globAccess (el))
2930 if (sRef_isSystemState (el)
2931 && !context_getFlag (FLG_MODFILESYSTEM))
2940 ("Called procedure %s may access %q, but "
2941 "globals list does not include globals %s",
2942 exprNode_unparse (f),
2944 cstring_makeLiteralTemp (sRef_isInternalState (el)
2952 else if (sRef_isNothing (el) || sRef_isSpecState (el))
2958 uentry gle = sRef_getUentry (el);
2959 sRef sr = sRef_updateSref (el);
2961 if (sRef_isUndefGlob (el))
2963 sRef_setDefined (sr, f->loc);
2964 exprNode_checkSet (f, sr);
2972 if (sRef_isAllocated (el))
2974 exprNode_checkSet (f, sr);
2978 if (sRef_isStateUndefined (sr))
2983 ("%s %q used by function undefined before call: %s",
2984 sRef_getScopeName (sr),
2986 exprNode_unparse (f)),
2988 sRef_setDefined (sr, f->loc);
2990 exprNode_checkUse (f, sr, f->loc);
2993 checkGlobUse (gle, TRUE, f);
2996 if (sRef_isKilledGlob (el))
2998 sRef_kill (sr, f->loc);
2999 context_usedGlobal (sr);
3003 } end_globSet_allElements;
3009 if (context_hasMods () || context_getFlag (FLG_MODNOMODS))
3011 sRefSet smods = sRefSet_undefined;
3014 ** NEED to check for modifies anything
3018 ** check each sRef that called function modifies (ml), is
3024 sRefSet_allElements (mods, s) /* s is something which may be modified */
3026 DPRINTF (("Check modify: %s", sRef_unparse (s)));
3028 if (sRef_isKindSpecial (s))
3030 if (sRef_isSpecInternalState (s))
3032 if (context_getFlag (FLG_MODINTERNALSTRICT))
3034 exprNode_checkCallModifyVal (s, args, f, ret);
3038 sRefSet mmods = context_modList ();
3040 sRefSet_allElements (mmods, el)
3042 if (sRef_isInternalState (el))
3044 sRef_setModified (el);
3046 } end_sRefSet_allElements ;
3051 exprNode_checkCallModifyVal (s, args, f, ret);
3056 sRef rb = sRef_getRootBase (s);
3058 if (sRef_isFileOrGlobalScope (rb))
3060 context_usedGlobal (rb);
3063 if (sRef_isFileStatic (s)
3064 && !fileId_equal (fileloc_fileId (f->loc),
3065 fileloc_fileId (uentry_whereDefined (le))))
3067 smods = sRefSet_insert (smods, s);
3071 exprNode_checkCallModifyVal (s, args, f, ret);
3074 } end_sRefSet_allElements;
3079 ** Static elements in modifies set can have nasty consequences.
3080 ** (I think...have not been able to reproduce a possible bug.)
3083 if (!sRefSet_isDefined (smods))
3085 mods = sRefSet_newCopy (mods);
3088 sRefSet_allElements (smods, el)
3090 bool res = sRefSet_delete (mods, el);
3093 } end_sRefSet_allElements;
3095 sRefSet_free (smods);
3100 else if (sRefSet_isDefined (mods))
3101 { /* just check observers */
3104 sRefSet_allElements (mods, s) /* s is something which may be modified */
3106 sRef rb = sRef_getRootBase (s);
3110 if (sRef_isParam (rb))
3112 sRef b = sRef_fixBaseParam (s, args);
3114 if (sRef_isObserver (b))
3116 exprNode e = exprNodeList_nth (args, sRef_getParam (rb));
3120 message ("Function call may modify observer%q: %s",
3121 sRef_unparsePreOpt (b), exprNode_unparse (e)),
3124 sRef_showExpInfo (b);
3128 } end_sRefSet_allElements;
3132 if (!hasMods) /* no specified modifications */
3134 if (context_getFlag (FLG_MODOBSERVERUNCON))
3136 exprNodeList_elements (args, e)
3138 if (exprNode_isDefined (e))
3140 sRef s = exprNode_getSref (e);
3142 if (sRef_isObserver (s)
3143 && ctype_isMutable (sRef_getType (s)))
3146 (FLG_MODOBSERVERUNCON,
3148 ("Call to unconstrained function %s may modify observer%q: %s",
3149 exprNode_unparse (f),
3150 sRef_unparsePreOpt (s), exprNode_unparse (e)),
3153 sRef_showExpInfo (s);
3157 } end_exprNodeList_elements;
3162 checkAnyCall (f, fname, params, args, hasMods, mods, isSpec, specialArgs);
3164 ret->uses = sRefSet_union (ret->uses, f->uses);
3165 ret->sets = sRefSet_union (ret->sets, f->sets);
3166 ret->msets = sRefSet_union (ret->msets, f->msets);
3171 ** Spurious errors reported, because splint can't tell
3172 ** mods must be fresh if freshMods is true.
3175 /*@i@*/ sRefSet_free (mods);
3181 void checkGlobUse (uentry glob, bool isCall, /*@notnull@*/ exprNode e)
3183 if (uentry_isVar (glob))
3185 if (context_inFunctionLike ())
3187 sRef sr = uentry_getSref (glob);
3189 context_usedGlobal (sr);
3191 if (context_checkGlobUse (glob))
3193 if (!context_globAccess (sr))
3199 message ("Called procedure %s may access %s %q",
3200 exprNode_unparse (e),
3201 sRef_unparseScope (sr),
3202 uentry_getName (glob)),
3209 message ("Undocumented use of %s %s",
3210 sRef_unparseScope (sr),
3211 exprNode_unparse (e)),
3220 llbug (message ("Global not variable: %q", uentry_unparse (glob)));
3225 reflectEnsuresClause (exprNode ret, uentry le, exprNode f, exprNodeList args)
3227 DPRINTF (("Reflect ensures clause: %s(%s) / %s / %s",
3228 exprNode_unparse (f), exprNodeList_unparse (args),
3229 uentry_unparseFull (le),
3230 stateClauseList_unparse (uentry_getStateClauseList (le))));
3232 if (uentry_isValid (le) && uentry_isFunction (le))
3234 stateClauseList sclauses = uentry_getStateClauseList (le);
3236 if (stateClauseList_isDefined (sclauses))
3238 DPRINTF (("Reflect ensures: %s / %s / %s",
3239 uentry_unparse (le),
3240 exprNode_unparse (f), exprNodeList_unparse (args)));
3242 stateClauseList_elements (sclauses, cl)
3244 if (stateClause_hasEnsures (cl))
3246 /* Same in usymtab.c:1904 */
3247 if (stateClause_setsMetaState (cl))
3249 qual q = stateClause_getMetaQual (cl);
3250 annotationInfo ainfo = qual_getAnnotationInfo (q);
3251 metaStateInfo minfo = annotationInfo_getState (ainfo);
3252 cstring key = metaStateInfo_getName (minfo);
3253 int mvalue = annotationInfo_getValue (ainfo);
3255 sRefSet osrs = sRefSet_undefined;
3258 if (stateClause_isGlobal (cl))
3260 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3265 srs = stateClause_getRefs (cl);
3268 DPRINTF (("Reflect ensures clause: %s", stateClause_unparse (cl)));
3271 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
3273 sRefSet_elements (srs, sel)
3277 if (sRef_isResult (sRef_getRootBase (sel)))
3279 s = exprNode_getSref (ret);
3283 s = sRef_fixBaseParam (sel, args);
3286 DPRINTF (("Reflecting state clause on: %s / %s",
3287 sRef_unparse (sel), sRef_unparse (s)));
3289 sRef_setMetaStateValueComplete (s, key, mvalue, exprNode_loc (f));
3290 } end_sRefSet_elements;
3292 sRefSet_free (osrs);
3296 sRefSet srs = stateClause_getRefs (cl);
3297 sRefModVal modf = stateClause_getEnsuresFunction (cl);
3298 int eparam = stateClause_getStateParameter (cl);
3300 llassert (modf != NULL);
3302 DPRINTF (("Reflect after clause: %s / %s",
3303 stateClause_unparse (cl),
3304 sRefSet_unparse (srs)));
3306 sRefSet_elements (srs, sel)
3310 DPRINTF (("elements: %s", sRef_unparse (sel)));
3311 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3313 if (sRef_isResult (sRef_getRootBase (sel)))
3315 DPRINTF (("Fix base: %s / %s",
3316 sRef_unparse (sel), sRef_unparse (exprNode_getSref (ret))));
3317 s = sRef_fixBase (sel, exprNode_getSref (ret));
3318 DPRINTF (("==> %s", sRef_unparseFull (s)));
3322 s = sRef_fixBaseParam (sel, args);
3325 DPRINTF (("elements: %s", sRef_unparse (s)));
3326 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3328 DPRINTF (("Reflecting state clause on: %s / %s",
3329 sRef_unparseFull (sel), sRef_unparseFull (s)));
3331 /* evans 2001-08-24 - added aliasSetCompleteParam */
3332 sRef_aliasSetCompleteParam (modf, s, eparam, exprNode_loc (f));
3334 DPRINTF (("After reflecting state clause on: %s / %s",
3335 sRef_unparseFull (sel), sRef_unparseFull (s)));
3336 } end_sRefSet_elements;
3339 } end_stateClauseList_elements ;
3342 DPRINTF (("Here: %s / %s",
3343 uentry_unparseFull (le),
3344 bool_unparse (uentry_hasMetaStateEnsures (le))));
3346 if (uentry_hasMetaStateEnsures (le))
3348 fileloc loc = exprNode_loc (f);
3350 metaStateConstraintList mscl = uentry_getMetaStateEnsures (le);
3352 metaStateConstraintList_elements (mscl, msc)
3354 metaStateSpecifier msspec = metaStateConstraint_getSpecifier (msc);
3355 metaStateInfo msinfo = metaStateSpecifier_getMetaStateInfo (msspec);
3356 metaStateExpression msexpr = metaStateConstraint_getExpression (msc);
3357 cstring key = metaStateInfo_getName (msinfo);
3358 sRef mlsr = metaStateSpecifier_getSref (msspec);
3360 sRef lastref = sRef_undefined;
3361 stateValue sval = stateValue_undefined;
3363 DPRINTF (("Meta state constraint for %s: %s", uentry_unparse (le),
3364 metaStateConstraint_unparse (msc)));
3365 DPRINTF (("Matches left: %s", sRef_unparseDebug (mlsr)));
3367 if (sRef_isResult (sRef_getRootBase (mlsr)))
3369 s = exprNode_getSref (ret);
3373 s = sRef_fixBaseParam (mlsr, args);
3376 DPRINTF (("Setting state: %s", sRef_unparseFull (s)));
3378 while (metaStateExpression_isDefined (msexpr))
3380 metaStateSpecifier ms = metaStateExpression_getSpecifier (msexpr);
3381 metaStateInfo msi = metaStateSpecifier_getMetaStateInfo (ms);
3384 DPRINTF (("Check expression: %s", metaStateExpression_unparse (msexpr)));
3386 if (metaStateExpression_isMerge (msexpr))
3388 msexpr = metaStateExpression_getRest (msexpr);
3392 msexpr = metaStateExpression_undefined;
3395 if (metaStateInfo_isDefined (msi))
3397 /* Must match lhs state */
3398 llassert (metaStateInfo_equal (msinfo, msi));
3401 if (metaStateSpecifier_isElipsis (ms))
3404 ** For elipsis, we need to merge all the relevant elipsis parameters
3408 uentryList params = uentry_getParams (le);
3409 int paramno = uentryList_size (params) - 1;
3411 if (!uentry_isElipsisMarker (uentryList_getN (params, paramno)))
3415 message ("Ensures clauses uses ... for function without ... in parameter list: %q",
3416 uentry_getName (le)),
3417 uentry_whereLast (le));
3418 /*@innerbreak@*/ break;
3421 while (paramno < exprNodeList_size (args))
3423 exprNode arg = exprNodeList_getN (args, paramno);
3424 fs = exprNode_getSref (arg);
3425 DPRINTF (("Merge arg: %s", exprNode_unparse (arg)));
3427 /* cut and pasted... gack*/
3428 if (stateValue_isDefined (sval))
3430 /* Use combination table to merge old state value with new one: */
3431 stateValue tval = sRef_getMetaStateValue (fs, key);
3433 if (stateValue_isDefined (tval))
3435 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3436 cstring msg = cstring_undefined;
3437 int nval = stateCombinationTable_lookup (sctable,
3438 stateValue_getValue (sval),
3439 stateValue_getValue (tval),
3441 DPRINTF (("Combining: %s + %s -> %d",
3442 stateValue_unparseValue (sval, msinfo),
3443 stateValue_unparseValue (tval, msinfo),
3446 if (nval == stateValue_error)
3451 ("Attributes merged in ensures clause in states that "
3452 "cannot be combined (%q is %q, %q is %q)%q",
3453 sRef_unparse (lastref),
3454 stateValue_unparseValue (sval, msinfo),
3456 stateValue_unparseValue (tval, msinfo),
3457 cstring_isDefined (msg) ?
3458 message (": %s", msg) : cstring_undefined),
3461 sRef_showMetaStateInfo (fs, key);
3465 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3466 loc = exprNode_loc (arg);
3470 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3475 sval = sRef_getMetaStateValue (fs, key);
3480 if (stateValue_isError (sval))
3482 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3491 msr = metaStateSpecifier_getSref (ms);
3494 llassert (sRef_isParam (sRef_getRootBase (msr)));
3495 fs = sRef_fixBaseParam (msr, args);
3497 if (stateValue_isDefined (sval))
3499 /* Use combination table to merge old state value with new one: */
3500 stateValue tval = sRef_getMetaStateValue (fs, key);
3502 if (stateValue_isDefined (tval))
3504 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3505 cstring msg = cstring_undefined;
3506 int nval = stateCombinationTable_lookup (sctable,
3507 stateValue_getValue (sval),
3508 stateValue_getValue (tval),
3510 DPRINTF (("Combining: %s + %s -> %d",
3511 stateValue_unparseValue (sval, msinfo),
3512 stateValue_unparseValue (tval, msinfo),
3515 if (nval == stateValue_error)
3520 ("Attributes merged in ensures clause in states that "
3521 "cannot be combined (%q is %q, %q is %q)%q",
3522 sRef_unparse (lastref),
3523 stateValue_unparseValue (sval, msinfo),
3525 stateValue_unparseValue (tval, msinfo),
3526 cstring_isDefined (msg)
3527 ? message (": %s", msg) : cstring_undefined),
3530 sRef_showMetaStateInfo (fs, key);
3534 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3538 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3543 sval = sRef_getMetaStateValue (fs, key);
3548 if (stateValue_isError (sval))
3550 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3555 DPRINTF (("Setting: %s:%s <- %s", sRef_unparse (s), key, stateValue_unparse (sval)));
3557 if (stateValue_isDefined (sval))
3559 sRef_setMetaStateValueComplete (s, key, stateValue_getValue (sval), loc);
3563 DPRINTF (("Undefined state: %s", cstring_toCharsSafe (sRef_unparse (s))));
3565 } end_metaStateConstraintList_elements ;
3567 metaStateConstraintList_free (mscl);
3573 checkRequiresClause (uentry le, exprNode f, exprNodeList args)
3575 DPRINTF (("Check requires clause: %s(%s) / %s / %s",
3576 exprNode_unparse (f), exprNodeList_unparse (args),
3577 uentry_unparseFull (le),
3578 stateClauseList_unparse (uentry_getStateClauseList (le))));
3580 if (uentry_isValid (le) && uentry_isFunction (le))
3582 stateClauseList sclauses = uentry_getStateClauseList (le);
3584 if (stateClauseList_isDefined (sclauses))
3586 DPRINTF (("Check requires: %s / %s / %s",
3587 uentry_unparse (le),
3588 exprNode_unparse (f), exprNodeList_unparse (args)));
3590 stateClauseList_elements (sclauses, cl)
3592 DPRINTF (("Check clause: %s / %s",
3593 stateClause_unparse (cl),
3594 bool_unparse (stateClause_hasRequires (cl))));
3596 if (stateClause_hasRequires (cl))
3598 sRefSet osrs = sRefSet_undefined;
3601 if (stateClause_isGlobal (cl))
3603 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3608 srs = stateClause_getRefs (cl);
3611 DPRINTF (("Refs: %s", sRefSet_unparse (srs)));
3613 if (stateClause_setsMetaState (cl))
3615 qual q = stateClause_getMetaQual (cl);
3616 annotationInfo ainfo = qual_getAnnotationInfo (q);
3617 metaStateInfo minfo = annotationInfo_getState (ainfo);
3618 cstring key = metaStateInfo_getName (minfo);
3619 int mvalue = annotationInfo_getValue (ainfo);
3621 DPRINTF (("Requires meta state! %s = %d", key, mvalue));
3623 sRefSet_elements (srs, sel)
3625 sRef s = sRef_fixBaseParam (sel, args);
3627 if (sRef_isResult (sRef_getRootBase (sel)))
3633 DPRINTF (("Checking state clause on: %s / %s / %s = %d",
3634 sRef_unparseFull (sel), sRef_unparseFull (s),
3637 if (!sRef_checkMetaStateValue (s, key, mvalue))
3639 DPRINTF (("HERE: %s", sRef_unparse (s)));
3643 ("Requires clause of called function %q not satisfied%q (state is %q): %q",
3644 uentry_getName (le),
3645 sRef_isGlobalMarker (s)
3647 : message (" by %q", sRef_unparse (s)),
3648 stateValue_unparseValue (sRef_getMetaStateValue (s, key),
3650 stateClause_unparse (cl)),
3653 sRef_showAliasInfo (s);
3657 DPRINTF (("Error supressed!"));
3658 DPRINTF (("Loc: %s", fileloc_unparse (exprNode_loc (f))));
3659 DPRINTF (("Context supress: %s",
3660 bool_unparse (context_suppressFlagMsg (FLG_STATETRANSFER, exprNode_loc (f)))));
3664 } end_sRefSet_elements;
3668 sRefModVal modf = stateClause_getRequiresBodyFunction (cl);
3669 int eparam = stateClause_getStateParameter (cl);
3671 DPRINTF (("Reflect after clause: %s / %s",
3672 stateClause_unparse (cl),
3673 sRefSet_unparse (srs)));
3675 llassert (modf != NULL);
3677 sRefSet_elements (srs, sel)
3681 DPRINTF (("elements: %s", sRef_unparse (sel)));
3682 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3684 s = sRef_fixBaseParam (sel, args);
3686 DPRINTF (("elements: %s", sRef_unparse (s)));
3687 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3689 if (sRef_isResult (sRef_getRootBase (sel)))
3691 ; /*@i423 what do we do about results */
3695 DPRINTF (("Reflecting state clause on: %s / %s",
3696 sRef_unparse (sel), sRef_unparse (s)));
3698 modf (s, eparam, exprNode_loc (f));
3700 } end_sRefSet_elements;
3703 sRefSet_free (osrs);
3705 } end_stateClauseList_elements ;
3710 static /*@only@*/ exprNode
3711 functionCallSafe (/*@only@*/ /*@notnull@*/ exprNode f,
3712 ctype t, /*@keep@*/ exprNodeList args)
3714 /* requires f is a non-error exprNode, with type function */
3715 cstring fname = exprNode_unparse (f);
3716 uentry le = exprNode_getUentry (f);
3717 exprNode ret = exprNode_createPartialCopy (f);
3722 DPRINTF (("Call: %s %s",exprNode_unparse (f), exprNodeList_unparse (args)));
3724 ret->typ = ctype_getReturnType (t);
3725 ret->kind = XPR_CALL;
3727 ret->edata = exprData_makeCall (f, args);
3730 ** Order of these steps is very important!
3732 ** Must check for argument dependencies before messing up uses and sets.
3735 if (context_getFlag (FLG_EVALORDER))
3737 exprNodeList_elements (args, current)
3739 if (exprNode_isDefined (current))
3741 exprNode_addUse (current, current->sref);
3743 } end_exprNodeList_elements;
3745 if (context_maybeSet (FLG_EVALORDER) || context_maybeSet (FLG_EVALORDERUNCON))
3747 checkSequencing (f, args);
3750 exprNodeList_elements (args, current)
3752 if (exprNode_isDefined (current) && sRef_isMeaningful (current->sref))
3754 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3756 } end_exprNodeList_elements ;
3759 special = checkArgs (le, f, t, args, ret);
3760 checkGlobMods (f, le, args, ret, special);
3761 checkRequiresClause (le, f, args);
3764 if (uentry_isValid (le)
3765 && (uentry_isFunction (le)
3766 || (uentry_isVariable (le)
3767 && ctype_isFunction (uentry_getType (le)))))
3769 exitkind exk = uentry_getExitCode (le);
3771 /* f->typ is already set to the return type */
3773 DPRINTF (("Function: %s", uentry_unparseFull (le)));
3774 ret->sref = uentry_returnedRef (le, args);
3775 DPRINTF (("Returned: %s / %s",
3776 uentry_unparseFull (le),
3777 sRef_unparseFull (ret->sref)));
3779 if (uentry_isFunction (le) && exprNodeList_size (args) >= 1)
3781 qual nullPred = uentry_nullPred (le);
3783 if (qual_isTrueNull (nullPred))
3785 exprNode arg = exprNodeList_head (args);
3787 if (exprNode_isDefined (arg))
3789 ret->guards = guardSet_addFalseGuard (ret->guards, arg->sref);
3792 else if (qual_isFalseNull (nullPred))
3794 exprNode arg = exprNodeList_head (args);
3796 if (exprNode_isDefined (arg))
3798 ret->guards = guardSet_addTrueGuard (ret->guards, arg->sref);
3803 llassert (qual_isUnknown (nullPred));
3807 if (exitkind_isConditionalExit (exk))
3811 ** if (arg0) then { exit! } else { ; }
3813 ** if (arg0) then { ; } else { exit! }
3818 llassert (!exprNodeList_isEmpty (args));
3819 firstArg = exprNodeList_head (args);
3821 if (exprNode_isDefined (firstArg)
3822 && !guardSet_isEmpty (firstArg->guards))
3824 usymtab_trueBranch (guardSet_undefined);
3825 usymtab_altBranch (guardSet_undefined);
3827 if (exitkind_isTrueExit (exk))
3829 usymtab_popBranches (firstArg,
3830 exprNode_makeMustExit (),
3832 TRUE, TRUEEXITCLAUSE);
3836 usymtab_popBranches (firstArg,
3838 exprNode_makeMustExit (),
3839 TRUE, FALSEEXITCLAUSE);
3843 ret->exitCode = XK_MAYEXIT;
3845 else if (exitkind_mustExit (exk))
3847 ret->exitCode = XK_MUSTEXIT;
3849 else if (exitkind_couldExit (exk))
3851 ret->exitCode = XK_MAYEXIT;
3858 if (cstring_equalLit (fname, "exit"))
3860 if (exprNodeList_size (args) == 1)
3862 exprNode arg = exprNodeList_head (args);
3864 if (exprNode_isDefined (arg) && exprNode_knownIntValue (arg))
3866 long int val = multiVal_forceInt (exprNode_getValue (arg));
3873 ("Argument to exit has implementation defined behavior: %s",
3874 exprNode_unparse (arg)),
3875 exprNode_loc (arg));
3884 exprNode_checkSetAny (ret, uentry_rawName (le));
3887 DPRINTF (("Before reflect: %s", sRef_unparseFull (ret->sref)));
3888 DPRINTF (("Reflect: %s", uentry_unparseFull (le)));
3889 reflectEnsuresClause (ret, le, f, args);
3896 ** this is yucky! should keep the uentry as part of exprNode!
3899 uentry exprNode_getUentry (exprNode e)
3901 if (exprNode_isError (e))
3903 return uentry_undefined;
3907 cstring s = exprNode_rootVarName (e);
3908 uentry ue = usymtab_lookupSafe (s);
3915 ** Returns true iff e1 and e2 are both exactly the same storage
3919 static bool exprNode_sameStorage (exprNode e1, exprNode e2)
3921 sRef s1 = exprNode_getSref (e1);
3922 sRef s2 = exprNode_getSref (e2);
3924 return (sRef_realSame (s1, s2));
3928 exprNode_makeInitBlock (lltok brace, /*@only@*/ exprNodeList inits)
3930 exprNode ret = exprNode_createPlain (ctype_unknown);
3932 ret->kind = XPR_INITBLOCK;
3933 ret->edata = exprData_makeCall (exprNode_undefined, inits);
3934 ret->loc = fileloc_update (ret->loc, lltok_getLoc (brace));
3940 exprNode_functionCall (/*@only@*/ exprNode f, /*@only@*/ exprNodeList args)
3946 if (exprNode_isUndefined (f))
3949 exprNodeList_free (args);
3950 return exprNode_undefined;
3953 t = exprNode_getType (f);
3955 if (sRef_isLocalVar (f->sref))
3957 exprNode_checkUse (f, f->sref, f->loc);
3959 if (sRef_possiblyNull (f->sref))
3961 if (!usymtab_isGuarded (f->sref))
3963 if (optgenerror (FLG_NULLDEREF,
3964 message ("Function call using %s pointer %q",
3965 sRef_nullMessage (f->sref),
3966 sRef_unparse (f->sref)),
3969 sRef_showNullInfo (f->sref);
3970 sRef_setNullError (f->sref);
3978 if (ctype_isRealFunction (t))
3980 exprNode ret = functionCallSafe (f, t, args);
3984 else if (ctype_isUnknown (t))
3986 exprNode ret = exprNode_createPartialCopy (f);
3992 exprNodeList_elements (args, current)
3994 if (exprNode_isDefined (current))
3996 exprNode_checkUse (ret, current->sref, ret->loc);
3999 ** also, anything derivable from current->sref may be used
4002 exprNode_addUse (ret, sRef_makeDerived (current->sref));
4003 exprNode_mergeUSs (ret, current);
4005 } end_exprNodeList_elements;
4007 ret->edata = exprData_makeCall (f, args);
4008 ret->kind = XPR_CALL;
4010 tstring = cstring_copy (exprNode_unparse (f));
4012 cstring_markOwned (tstring);
4013 exprNode_checkSetAny (ret, tstring);
4019 voptgenerror (FLG_TYPE,
4020 message ("Call to non-function (type %t): %s", t,
4021 exprNode_unparse (f)),
4024 exprNodeList_free (args);
4026 return (exprNode_makeError ());
4031 exprNode_fieldAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
4032 /*@only@*/ cstring f)
4034 exprNode ret = exprNode_createPartialCopy (s);
4036 ret->kind = XPR_FACCESS;
4038 if (exprNode_isError (s))
4040 ret->edata = exprData_makeField (s, f);
4045 ctype t = exprNode_getType (s);
4046 ctype tr = ctype_realType (t);
4048 checkMacroParen (s);
4050 ret->edata = exprData_makeField (s, f);
4052 if (ctype_isStructorUnion (tr))
4054 uentry tf = uentryList_lookupField (ctype_getFields (tr), f);
4056 if (uentry_isUndefined (tf))
4058 voptgenerror (FLG_TYPE,
4059 message ("Access non-existent field %s of %t: %s", f, t,
4060 exprNode_unparse (ret)),
4062 /*! cstring_free (f); */ /* evans 2001-03-25 self-detect */
4067 uentry_setUsed (tf, exprNode_loc (ret));
4069 ret->typ = uentry_getType (tf);
4070 checkSafeUse (ret, s->sref);
4072 ret->sref = sRef_makeField (s->sref, uentry_rawName (tf));
4073 /*!? exprNode_free (s); */ /* evans 2001-03-25 self-detect */
4077 else /* isStructorUnion */
4079 if (ctype_isRealAbstract (tr))
4083 message ("Access field of abstract type (%t): %s.%s",
4084 t, exprNode_unparse (s), f),
4086 ret->typ = ctype_unknown;
4090 if (ctype_isKnown (tr))
4095 ("Access field of non-struct or union (%t): %s.%s",
4096 t, exprNode_unparse (s), f),
4099 ret->typ = ctype_unknown;
4103 cstring sn = cstring_copy (f);
4105 checkSafeUse (ret, s->sref);
4106 cstring_markOwned (sn);
4107 ret->sref = sRef_makeField (s->sref, sn);
4119 exprNode_fieldAccess (/*@only@*/ exprNode s, /*@only@*/ lltok dot,
4120 /*@only@*/ cstring f)
4122 exprNode res = exprNode_fieldAccessAux (s, lltok_getLoc (dot), f);
4123 lltok_release (dot);
4128 exprNode_addParens (/*@only@*/ lltok lpar, /*@only@*/ exprNode e)
4130 exprNode ret = exprNode_createPartialCopy (e);
4132 ret->loc = fileloc_update (ret->loc, lltok_getLoc (lpar));
4133 ret->kind = XPR_PARENS;
4134 ret->edata = exprData_makeUop (e, lpar);
4136 if (!exprNode_isError (e))
4138 ret->exitCode = e->exitCode;
4139 ret->canBreak = e->canBreak;
4140 ret->mustBreak = e->mustBreak;
4141 ret->isJumpPoint = e->isJumpPoint;
4142 ret->sref = e->sref;
4149 exprNode_arrowAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
4150 /*@only@*/ cstring f)
4152 exprNode ret = exprNode_createPartialCopy (s);
4154 ret->edata = exprData_makeField (s, f);
4155 ret->kind = XPR_ARROW;
4157 if (exprNode_isError (s))
4163 ctype t = exprNode_getType (s);
4164 ctype tr = ctype_realType (t);
4166 checkMacroParen (s);
4168 (void) ctype_fixArrayPtr (tr); /* REWRITE THIS */
4170 if (ctype_isRealPointer (tr))
4172 ctype b = ctype_realType (ctype_baseArrayPtr (tr));
4174 if (ctype_isStructorUnion (b))
4176 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4178 if (sRef_isKnown (s->sref) && sRef_possiblyNull (s->sref))
4180 if (!usymtab_isGuarded (s->sref) && !context_inProtectVars ())
4184 message ("Arrow access from %s pointer%q: %s",
4185 sRef_nullMessage (s->sref),
4186 sRef_unparsePreOpt (s->sref),
4187 exprNode_unparse (ret)),
4190 sRef_showNullInfo (s->sref);
4191 sRef_setNullError (s->sref);
4196 if (uentry_isUndefined (fentry))
4200 message ("Access non-existent field %s of %t: %s",
4201 f, t, exprNode_unparse (ret)),
4203 ret->typ = ctype_unknown;
4209 ** was safeUse: shouldn't be safe!
4212 ** rec must be defined,
4213 ** *rec must be allocated
4214 ** rec->field need only be defined it if is an rvalue
4217 uentry_setUsed (fentry, exprNode_loc (ret));
4218 ret->typ = uentry_getType (fentry);
4220 exprNode_checkUse (ret, s->sref, s->loc);
4222 /* exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc); */
4223 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4227 else /* Pointer to something that is not a struct or union*/
4229 if (ctype_isRealAbstract (tr))
4231 ctype xrt = ctype_forceRealType (tr);
4235 message ("Arrow access field of abstract type (%t): %s->%s",
4236 t, exprNode_unparse (s), f),
4240 ** Set the state correctly, as if the abstraction is broken.
4243 if (ctype_isRealPointer (xrt) &&
4244 (b = ctype_realType (ctype_baseArrayPtr (xrt)),
4245 ctype_isStructorUnion (b)))
4247 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4248 ret->typ = uentry_getType (fentry);
4249 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4253 ret->typ = ctype_unknown;
4254 ret->sref = sRef_undefined;
4257 else /* not a struct, union or abstract */
4259 if (ctype_isUnknown (tr)) {
4260 cstring sn = cstring_copy (f);
4262 DPRINTF (("Here: %s", exprNode_unparse (s)));
4264 exprNode_checkUse (ret, s->sref, s->loc);
4265 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4267 cstring_markOwned (sn);
4268 ret->sref = sRef_makeArrow (s->sref, sn);
4270 ret->kind = XPR_ARROW;
4275 message ("Arrow access field of non-struct or union "
4276 "pointer (%t): %s->%s",
4277 t, exprNode_unparse (s), f),
4280 ret->typ = ctype_unknown;
4281 ret->sref = sRef_undefined;
4286 else /* its not a pointer */
4288 if (!ctype_isUnknown (tr))
4292 message ("Arrow access of non-pointer (%t): %s->%s",
4293 t, exprNode_unparse (s), f),
4296 ret->typ = ctype_unknown;
4297 ret->sref = sRef_undefined;
4301 cstring sn = cstring_copy (f);
4303 DPRINTF (("Here: %s", exprNode_unparse (s)));
4305 exprNode_checkUse (ret, s->sref, s->loc);
4306 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4308 cstring_markOwned (sn);
4309 ret->sref = sRef_makeArrow (s->sref, sn);
4311 ret->kind = XPR_ARROW;
4322 exprNode_arrowAccess (/*@only@*/ exprNode s,
4323 /*@only@*/ lltok arrow,
4324 /*@only@*/ cstring f)
4326 exprNode res = exprNode_arrowAccessAux (s, lltok_getLoc (arrow), f);
4327 lltok_release (arrow);
4332 ** only postOp's in C: i++ and i--
4336 exprNode_postOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4338 /* check modification also */
4339 /* cstring opname = lltok_unparse (op);*/
4341 exprNode ret = exprNode_createPartialCopy (e);
4343 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4344 ret->kind = XPR_POSTOP;
4345 ret->edata = exprData_makeUop (e, op);
4347 if (!exprNode_isDefined (e))
4352 checkMacroParen (e);
4354 exprNode_checkUse (ret, e->sref, e->loc);
4355 exprNode_checkSet (ret, e->sref);
4357 t = exprNode_getType (e);
4359 if (sRef_isUnsafe (e->sref))
4361 voptgenerror (FLG_MACROPARAMS,
4362 message ("Operand of %s is macro parameter (non-functional): %s%s",
4363 lltok_unparse (op), exprNode_unparse (e), lltok_unparse (op)),
4365 sRef_makeSafe (e->sref);
4366 sRef_makeSafe (ret->sref);
4369 if (ctype_isForceRealNumeric (&t) || ctype_isRealAP (t))
4375 if (ctype_isRealAbstract (t))
4379 message ("Operand of %s is abstract type (%t): %s",
4380 lltok_unparse (op), t, exprNode_unparse (e)),
4387 message ("Operand of %s is non-numeric (%t): %s",
4388 lltok_unparse (op), t, exprNode_unparse (e)),
4391 ret->typ = ctype_unknown;
4394 /* if (ctype_isZero (t)) e->typ = ctype_int; */
4396 exprNode_checkModify (e, ret);
4398 /* added 7/11/2000 D.L */
4400 /*DRL 6/8/01 I decided to disable all Splint warnings here since the code
4401 probably needs a rewrite any way */
4406 /* updateEnvironmentForPostOp (e); */
4408 /* start modifications */
4409 /* added by Seejo on 4/16/2000 */
4411 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4413 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4415 ret->sref = sRef_copy (e->sref);
4418 if (lltok_getTok (op) == INC_OP) {
4419 if (sRef_getSize(e->sref) > 0) {
4421 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4423 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4424 /* Assumption: there is only 1 \0 in the buffer */
4425 /* This will not be correct if there are 2 \0's in the buffer */
4426 sRef_setNotNullTerminatedState(ret->sref);
4427 sRef_resetLen(ret->sref);
4429 sRef_setNullTerminatedState(ret->sref);
4430 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4432 if (sRef_isNullTerminated (ret->sref))
4433 printf ("ret->sref is Null Terminated\n");
4434 else if (sRef_isPossiblyNullTerminated (ret->sref))
4435 printf ("ret->sref is Possibly Null Terminated\n");
4436 else if (sRef_isNotNullTerminated (ret->sref))
4437 printf ("ret->sref is Not Null Terminated\n");
4442 if (lltok_getTok (op) == DEC_OP) {
4443 if (sRef_getSize(e->sref) >= 0) {
4444 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4445 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4450 /* end modifications */
4456 exprNode_preOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4458 bool checkMod = FALSE;
4460 int opid = lltok_getTok (op);
4461 exprNode ret = exprNode_createSemiCopy (e);
4463 exprNode_copySets (ret, e);
4465 multiVal_free (ret->val);
4466 ret->val = multiVal_undefined;
4467 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4468 ret->kind = XPR_PREOP;
4469 ret->edata = exprData_makeUop (e, op);
4471 if (exprNode_isError (e))
4476 checkMacroParen (e);
4478 te = exprNode_getType (e);
4479 tr = ctype_realType (te);
4481 if (opid != TAMPERSAND)
4483 exprNode_checkUse (ret, e->sref, e->loc);
4485 if (ctype_isRealAbstract (tr)
4486 && (!(ctype_isRealBool (te) && (opid == TEXCL))))
4488 if (optgenerror (FLG_ABSTRACT,
4489 message ("Operand of %s is abstract type (%t): %s",
4490 lltok_unparse (op), tr,
4491 exprNode_unparse (ret)),
4494 tr = te = ctype_unknown;
4495 ret->typ = ctype_unknown;
4496 sRef_setNullError (e->sref);
4504 case DEC_OP: /* should also check modification! */
4505 if (sRef_isMacroParamRef (e->sref))
4509 message ("Operand of %s is macro parameter (non-functional): %s",
4510 lltok_unparse (op), exprNode_unparse (ret)),
4515 exprNode_checkSet (ret, e->sref);
4518 if (ctype_isForceRealNumeric (&tr) || ctype_isRealAP (tr))
4523 if (context_msgStrictOps ())
4527 message ("Operand of %s is non-numeric (%t): %s",
4528 lltok_unparse (op), te, exprNode_unparse (ret)),
4531 ret->typ = ctype_int;
4534 /* start modifications */
4535 /* added by Seejo on 4/16/2000 */
4537 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4539 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4541 ret->sref = sRef_copy (e->sref);
4544 if (lltok_getTok (op) == INC_OP) {
4545 if (sRef_getSize(e->sref) > 0) {
4547 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4549 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4550 /* Assumption: there is only 1 \0 in the buffer */
4551 /* This will not be correct if there are 2 \0's in the buffer */
4552 sRef_setNotNullTerminatedState(ret->sref);
4553 sRef_resetLen (ret->sref);
4555 sRef_setNullTerminatedState(ret->sref);
4556 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4562 if (lltok_getTok (op) == DEC_OP) {
4563 if (sRef_getSize(e->sref) >= 0) {
4564 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4565 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4570 /* end modifications */
4577 if (ctype_isForceRealNumeric (&tr))
4581 ret->val = multiVal_invert (exprNode_getValue (e));
4585 ret->val = multiVal_copy (exprNode_getValue (e));
4590 if (context_msgStrictOps ())
4594 message ("Operand of %s is non-numeric (%t): %s",
4595 lltok_unparse (op), te, exprNode_unparse (ret)),
4599 ret->typ = ctype_int;
4603 case TEXCL: /* maybe this should be restricted */
4604 guardSet_flip (ret->guards);
4606 if (ctype_isRealBool (te) || ctype_isUnknown (te))
4612 if (ctype_isRealPointer (tr))
4614 if (sRef_isKnown (e->sref))
4616 ret->guards = guardSet_addFalseGuard (ret->guards, e->sref);
4620 (FLG_BOOLOPS, FLG_PTRNEGATE,
4621 message ("Operand of %s is non-boolean (%t): %s",
4622 lltok_unparse (op), te, exprNode_unparse (ret)),
4629 message ("Operand of %s is non-boolean (%t): %s",
4630 lltok_unparse (op), te, exprNode_unparse (ret)),
4634 ret->typ = ctype_bool;
4639 if (ctype_isForceRealInt (&tr))
4644 if (context_msgStrictOps ())
4648 message ("Operand of %s is non-integer (%t): %s",
4649 lltok_unparse (op), te, exprNode_unparse (ret)),
4653 if (ctype_isInt (e->typ))
4659 ret->typ = ctype_int;
4665 ret->typ = ctype_makePointer (e->typ);
4667 if (sRef_isKnown (e->sref))
4669 ret->sref = sRef_makeAddress (e->sref);
4676 if (ctype_isAP (tr))
4678 ret->typ = ctype_baseArrayPtr (e->typ);
4682 if (ctype_isKnown (te))
4684 if (ctype_isFunction (te))
4690 message ("Dereference of function type (%t): %s",
4691 te, exprNode_unparse (ret)),
4696 voptgenerror (FLG_TYPE,
4697 message ("Dereference of non-pointer (%t): %s",
4698 te, exprNode_unparse (ret)),
4700 ret->typ = ctype_unknown;
4705 ret->typ = ctype_unknown;
4710 if (sRef_isKnown (e->sref))
4712 DPRINTF (("Checking possibly null: %s", sRef_unparseFull (e->sref)));
4714 if (sRef_possiblyNull (e->sref))
4716 DPRINTF (("Checking possibly null: %s", sRef_unparse (e->sref)));
4717 if (!usymtab_isGuarded (e->sref) && !context_inProtectVars ())
4721 message ("Dereference of %s pointer %q: %s",
4722 sRef_nullMessage (e->sref),
4723 sRef_unparse (e->sref),
4724 exprNode_unparse (ret)),
4727 sRef_showNullInfo (e->sref);
4728 sRef_setNotNull (e->sref, e->loc); /* suppress future messages */
4733 ret->sref = sRef_makePointer (e->sref);
4738 llbug (message ("exprNode_preOp: unhandled op: %s", lltok_unparse (op)));
4743 exprNode_checkModify (e, ret);
4750 ** any reason to disallow sizeof (abstract type) ?
4754 ** used by both sizeof
4758 ctype sizeof_resultType (void)
4760 static ctype sizet = ctype_unknown;
4762 if (ctype_isUnknown (sizet))
4764 if (usymtab_existsType (cstring_makeLiteralTemp ("size_t")))
4766 sizet = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("size_t")));
4770 sizet = ctype_ulint;
4777 exprNode_sizeofType (/*@only@*/ qtype qt)
4779 exprNode ret = exprNode_create (sizeof_resultType ());
4780 ctype ct = qtype_getType (qt);
4782 ret->kind = XPR_SIZEOFT;
4783 ret->edata = exprData_makeSizeofType (qt);
4785 voptgenerror (FLG_SIZEOFTYPE,
4786 message ("Parameter to sizeof is type %s: %s",
4788 exprNode_unparse (ret)),
4795 exprNode_alignofType (/*@only@*/ qtype qt)
4797 exprNode ret = exprNode_create (sizeof_resultType ());
4798 ctype ct = qtype_getType (qt);
4800 ret->kind = XPR_ALIGNOFT;
4801 ret->edata = exprData_makeSizeofType (qt);
4803 voptgenerror (FLG_SIZEOFTYPE,
4804 message ("Parameter to alignof is type %s: %s",
4806 exprNode_unparse (ret)),
4812 exprNode exprNode_offsetof (qtype qt, cstringList s)
4814 exprNode ret = exprNode_create (sizeof_resultType ());
4815 ctype ct = qtype_getType (qt);
4817 ret->kind = XPR_OFFSETOF;
4818 ret->edata = exprData_makeOffsetof (qt, s);
4820 if (!ctype_isRealSU (ct))
4822 voptgenerror (FLG_TYPE,
4823 message ("First parameter to offsetof is not a "
4824 "struct or union type (type %s): %s",
4826 exprNode_unparse (ret)),
4833 cstringList_elements (s, el) {
4837 if (ctype_isUndefined (lt))
4841 else if (!ctype_isRealSU (lt))
4843 voptgenerror (FLG_TYPE,
4844 message ("Inner offsetof type is not a "
4845 "struct or union type (type %s before field %s): %s",
4846 ctype_unparse (lt), el,
4847 exprNode_unparse (ret)),
4853 fields = ctype_getFields (ctype_realType (lt));
4854 fld = uentryList_lookupField (fields, el);
4855 DPRINTF (("Try: %s / %s", ctype_unparse (lt), el));
4857 if (uentry_isUndefined (fld))
4859 if (ctype_equal (lt, ct)) {
4860 voptgenerror (FLG_TYPE,
4861 message ("Field %s in offsetof is not the "
4862 "name of a field of %s: %s",
4865 exprNode_unparse (ret)),
4868 voptgenerror (FLG_TYPE,
4869 message ("Deep field %s in offsetof is not the "
4870 "name of a field of %s: %s",
4873 exprNode_unparse (ret)),
4879 lt = uentry_getType (fld);
4882 } end_cstringList_elements;
4884 /* Should report error if its a bit field - behavior is undefined! */
4891 exprNode_sizeofExpr (/*@only@*/ exprNode e)
4895 if (exprNode_isUndefined (e))
4897 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4898 ret->edata = exprData_makeSingle (e);
4899 ret->typ = sizeof_resultType ();
4900 ret->kind = XPR_SIZEOF;
4904 uentry u = exprNode_getUentry (e);
4906 ret = exprNode_createPartialCopy (e);
4907 ret->edata = exprData_makeSingle (e);
4909 ret->typ = sizeof_resultType ();
4910 ret->kind = XPR_SIZEOF;
4912 if (uentry_isValid (u)
4913 && uentry_isRefParam (u)
4914 && ctype_isRealArray (uentry_getType (u)))
4917 (FLG_SIZEOFFORMALARRAY,
4918 message ("Parameter to sizeof is an array-type function parameter: %s",
4919 exprNode_unparse (ret)),
4925 ** sizeof (x) doesn't "really" use x
4932 exprNode_alignofExpr (/*@only@*/ exprNode e)
4936 if (exprNode_isUndefined (e))
4938 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4942 ret = exprNode_createPartialCopy (e);
4945 ret->edata = exprData_makeSingle (e);
4946 ret->typ = sizeof_resultType ();
4947 ret->kind = XPR_ALIGNOF;
4950 ** sizeof (x) doesn't "really" use x
4957 exprNode_cast (/*@only@*/ lltok tok, /*@only@*/ exprNode e, /*@only@*/ qtype q)
4963 if (exprNode_isError (e))
4966 lltok_release (tok);
4967 return exprNode_undefined;
4970 checkMacroParen (e);
4972 c = qtype_getType (q);
4973 t = exprNode_getType (e);
4975 ret = exprNode_createPartialCopy (e);
4977 ret->loc = fileloc_update (ret->loc, lltok_getLoc (tok));
4979 ret->kind = XPR_CAST;
4980 ret->edata = exprData_makeCast (tok, e, q);
4982 if (ctype_isRealSU (ctype_getBaseType (sRef_getType (e->sref))))
4985 ** This is a bit of a hack to avoid a problem
4986 ** when the code does,
4987 ** (some other struct) x
4992 ret->sref = sRef_copy (e->sref);
4993 usymtab_addForceMustAlias (ret->sref, e->sref);
4994 sRef_setTypeFull (ret->sref, c);
4995 DPRINTF (("Cast: %s -> %s", sRef_unparseFull (e->sref),
4996 sRef_unparseFull (ret->sref)));
5000 ret->sref = e->sref;
5001 sRef_setTypeFull (ret->sref, c);
5002 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
5009 ** void * <-> abstract * (if FLG_ABSTVOIDP)
5010 ** abstract * <-> void * (if FLG_ABSTVOIDP)
5013 if (ctype_isVoid (c)) /* cast to void is always okay --- discard value */
5017 else if (ctype_isRealAP (c)) /* casting to array or pointer */
5019 ctype bc = ctype_getBaseType (c);
5020 ctype bt = ctype_getBaseType (t);
5021 ctype rt = ctype_realType (t);
5023 if (ctype_isFunction (ctype_baseArrayPtr (ctype_realType (c)))
5024 && (ctype_isArrayPtr (rt)
5025 && !ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
5029 message ("Cast from function pointer type (%t) to "
5030 "non-function pointer (%t): %s",
5031 c, t, exprNode_unparse (ret)),
5035 if (!ctype_isFunction (ctype_baseArrayPtr (c))
5036 && (ctype_isArrayPtr (rt)
5037 && ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
5041 message ("Cast from non-function pointer type (%t) to "
5042 "function pointer (%t): %s",
5043 c, t, exprNode_unparse (ret)),
5047 if (exprNode_isZero (e) && context_getFlag (FLG_ZEROPTR) &&
5048 !(ctype_isRealAbstract (bc)
5049 && context_hasAccess (ctype_typeId (bc))))
5051 ; /* okay to cast zero */
5055 if (ctype_isRealAbstract (bc)
5056 && !context_hasAccess (ctype_typeId (bc)))
5058 if (ctype_isVoidPointer (t) || ctype_isUnknown (t))
5062 message ("Cast to underlying abstract type %t: %s",
5063 c, exprNode_unparse (ret)),
5070 message ("Cast to underlying abstract type %t: %s",
5071 c, exprNode_unparse (ret)),
5076 if (ctype_isRealAbstract (bt)
5077 && !context_hasAccess (ctype_typeId (bt)))
5079 if (ctype_isUnknown (c) || ctype_isVoidPointer (c))
5083 message ("Cast from underlying abstract type %t: %s",
5084 t, exprNode_unparse (ret)),
5091 message ("Cast from underlying abstract type %t: %s",
5092 t, exprNode_unparse (ret)),
5100 ctype bt = ctype_realType (ctype_getBaseType (t));
5101 ctype bc = ctype_realType (ctype_getBaseType (c));
5103 if (ctype_isAbstract (bt) && !context_hasAccess (ctype_typeId (bt)))
5105 if (ctype_match (c, t))
5107 if (ctype_equal (c, t))
5111 message ("Redundant cast involving abstract type %t: %s",
5112 bt, exprNode_unparse (ret)),
5120 message ("Cast from abstract type %t: %s",
5121 bt, exprNode_unparse (ret)),
5126 if (ctype_isAbstract (bc)
5127 && !context_hasAccess (ctype_typeId (bc)))
5129 if (ctype_match (c, t))
5135 DPRINTF (("No access to: %s / %d",
5136 ctype_unparse (bc), ctype_typeId (bc)));
5137 DPRINTF (("Context %s %s",
5138 bool_unparse (context_inFunctionLike ()),
5139 context_unparse ()));
5142 message ("Cast to abstract type %t: %s", bc,
5143 exprNode_unparse (ret)),
5149 if (ctype_isAbstract (c))
5151 if (sRef_isExposed (e->sref) || sRef_isOnly (e->sref))
5153 /* okay, cast exposed to abstract */
5154 sRef_clearExKindComplete (ret->sref, fileloc_undefined);
5158 if (ctype_isVisiblySharable (t)
5159 && sRef_isExternallyVisible (e->sref)
5160 && !(ctype_isAbstract (t)
5161 && context_hasAccess (ctype_typeId (t))))
5165 message ("Cast to abstract type from externally visible "
5166 "mutable storage exposes rep of %s: %s",
5168 exprNode_unparse (e)),
5178 evaluationOrderUndefined (lltok op)
5180 int opid = lltok_getTok (op);
5182 return (opid != AND_OP && opid != OR_OP);
5185 static bool checkIntegral (/*@notnull@*/ exprNode e1,
5186 /*@notnull@*/ exprNode e2,
5187 /*@notnull@*/ exprNode ret,
5192 ctype te1 = exprNode_getType (e1);
5193 ctype te2 = exprNode_getType (e2);
5195 ctype tr1 = ctype_realishType (te1);
5196 ctype tr2 = ctype_realishType (te2);
5198 if (ctype_isForceRealInt (&tr1) && ctype_isForceRealInt (&tr2))
5204 if (context_msgStrictOps ())
5206 if (!ctype_isInt (tr1) && !ctype_isInt (tr2))
5208 if (ctype_sameName (te1, te2))
5212 message ("Operands of %s are non-integer (%t): %s",
5213 lltok_unparse (op), te1,
5214 exprNode_unparse (ret)),
5221 message ("Operands of %s are non-integers (%t, %t): %s",
5222 lltok_unparse (op), te1, te2,
5223 exprNode_unparse (ret)),
5227 else if (!ctype_isInt (tr1))
5231 message ("Left operand of %s is non-integer (%t): %s",
5232 lltok_unparse (op), te1, exprNode_unparse (ret)),
5236 /* !ctype_isInt (te2) */
5240 message ("Right operand of %s is non-integer (%t): %s",
5241 lltok_unparse (op), te2, exprNode_unparse (ret)),
5251 ** returns exprNode representing e1 op e2
5253 ** uses msg if there are errors
5254 ** can be used for both assignment ops and regular ops
5259 static /*@only@*/ /*@notnull@*/ exprNode
5260 exprNode_makeOp (/*@keep@*/ exprNode e1, /*@keep@*/ exprNode e2,
5261 /*@keep@*/ lltok op)
5263 ctype te1, te2, tr1, tr2, tret;
5264 int opid = lltok_getTok (op);
5265 bool hasError = FALSE;
5268 if (exprNode_isError (e1))
5270 ret = exprNode_createPartialNVCopy (e2);
5274 ret = exprNode_createPartialNVCopy (e1);
5277 ret->val = multiVal_undefined;
5280 ret->edata = exprData_makeOp (e1, e2, op);
5282 if (exprNode_isError (e1) || exprNode_isError (e2))
5284 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5285 || opid == EQ_OP || opid == NE_OP
5286 || opid == AND_OP || opid == OR_OP)
5288 ret->typ = ctype_bool;
5291 if (exprNode_isDefined (e1))
5293 exprNode_checkUse (ret, e1->sref, e1->loc);
5296 if (exprNode_isDefined (e2))
5298 exprNode_mergeUSs (ret, e2);
5299 exprNode_checkUse (ret, e2->sref, e2->loc);
5305 tret = ctype_unknown;
5306 te1 = exprNode_getType (e1);
5308 DPRINTF (("te1 = %s / %s", exprNode_unparse (e1), ctype_unparse (te1)));
5310 te2 = exprNode_getType (e2);
5312 tr1 = ctype_realishType (te1);
5313 tr2 = ctype_realishType (te2);
5317 ret->guards = guardSet_or (ret->guards, e2->guards);
5319 else if (opid == AND_OP)
5321 ret->guards = guardSet_and (ret->guards, e2->guards);
5328 if (opid == EQ_OP || opid == NE_OP)
5330 exprNode temp1 = e1, temp2 = e2;
5332 /* could do NULL == x */
5334 if (exprNode_isNullValue (e1) || exprNode_isUnknownConstant (e1))
5336 temp1 = e2; temp2 = e1;
5339 if (exprNode_isNullValue (temp2) || exprNode_isUnknownConstant (temp2))
5341 reflectNullTest (temp1, (opid == NE_OP));
5342 guardSet_free (ret->guards);
5343 ret->guards = guardSet_copy (temp1->guards);
5347 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5348 || opid == EQ_OP || opid == NE_OP || opid == AND_OP || opid == OR_OP)
5353 if (anyAbstract (tr1, tr2) &&
5354 (!((ctype_isRealBool (te1) || ctype_isRealBool (te2)) &&
5355 (opid == AND_OP || opid == OR_OP
5356 || opid == EQ_OP || opid == NE_OP))))
5358 abstractOpError (tr1, tr2, op, e1, e2, e1->loc, e2->loc);
5360 else if (ctype_isUnknown (te1) || ctype_isUnknown (te2))
5362 /* unknown types, no comparisons possible */
5368 case TMULT: /* multiplication and division: */
5370 case MUL_ASSIGN: /* numeric, numeric -> numeric */
5371 case DIV_ASSIGN: /* */
5373 if (opid == TMULT || opid == MUL_ASSIGN)
5375 ret->val = multiVal_multiply (exprNode_getValue (e1),
5376 exprNode_getValue (e2));
5380 ret->val = multiVal_divide (exprNode_getValue (e1),
5381 exprNode_getValue (e2));
5384 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5387 case TPLUS: /* addition and subtraction: */
5388 case TMINUS: /* pointer, int -> pointer */
5389 case SUB_ASSIGN: /* int, pointer -> pointer */
5390 case ADD_ASSIGN: /* numeric, numeric -> numeric */
5392 if (opid == TPLUS || opid == ADD_ASSIGN)
5394 ret->val = multiVal_add (exprNode_getValue (e1),
5395 exprNode_getValue (e2));
5399 ret->val = multiVal_subtract (exprNode_getValue (e1),
5400 exprNode_getValue (e2));
5403 tr1 = ctype_fixArrayPtr (tr1);
5405 if ((ctype_isRealPointer (tr1) && !exprNode_isNullValue (e1))
5406 && (!ctype_isRealPointer (tr2) && ctype_isRealInt (tr2)))
5410 if (context_msgPointerArith ())
5414 message ("Pointer arithmetic (%t, %t): %s",
5415 te1, te2, exprNode_unparse (ret)),
5420 ** Swap terms so e1 is always the pointer
5423 if (ctype_isRealPointer (tr1))
5429 exprNode_swap (e1, e2);
5433 if (sRef_possiblyNull (e1->sref)
5434 && !usymtab_isGuarded (e1->sref))
5437 (FLG_NULLPOINTERARITH,
5438 message ("Pointer arithmetic involving possibly "
5439 "null pointer %s: %s",
5440 exprNode_unparse (e1),
5441 exprNode_unparse (ret)),
5445 ret->sref = sRef_copy (e1->sref);
5447 /* start modifications */
5448 /* added by Seejo on 4/16/2000 */
5450 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5452 if ((sRef_isPossiblyNullTerminated (e1->sref)) || (sRef_isNullTerminated(e1->sref))) {
5455 added ugly fixed to stop
5456 program from crashing on point + int +int
5457 one day I'll fix this or ask Seejo wtf the codes supposed to do. */
5459 if (!multiVal_isInt (e2->val) )
5463 val = (int) multiVal_forceInt (e2->val);
5465 /* Operator : + or += */
5466 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5467 if (sRef_getSize(e1->sref) >= val) {/* Incrementing the pointer by
5468 val should not result in a
5469 size < 0 (size = 0 is ok !) */
5471 sRef_setSize (ret->sref, sRef_getSize(e1->sref) - val);
5473 if (sRef_getLen(e1->sref) == val) { /* i.e. the character at posn val is \0 */
5474 sRef_setNotNullTerminatedState(ret->sref);
5475 sRef_resetLen (ret->sref);
5477 sRef_setNullTerminatedState(ret->sref);
5478 sRef_setLen (ret->sref, sRef_getLen(e1->sref) - val);
5483 /* Operator : - or -= */
5484 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5485 if (sRef_getSize(e1->sref) >= 0) {
5486 sRef_setSize (ret->sref, sRef_getSize(e1->sref) + val);
5487 sRef_setLen (ret->sref, sRef_getLen(e1->sref) + val);
5492 /* end modifications */
5494 sRef_setNullError (ret->sref);
5497 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5498 ** since is points to storage that should not be deallocated
5499 ** through this pointer.
5502 if (sRef_isOnly (ret->sref)
5503 || sRef_isFresh (ret->sref))
5505 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5510 else if ((!ctype_isRealPointer(tr1) && ctype_isRealInt (tr1))
5511 && (ctype_isRealPointer (tr2) && !exprNode_isNullValue (e2)))
5513 if (context_msgPointerArith ())
5517 message ("Pointer arithmetic (%t, %t): %s",
5518 te1, te2, exprNode_unparse (ret)),
5522 if (sRef_possiblyNull (e1->sref)
5523 && !usymtab_isGuarded (e1->sref))
5526 (FLG_NULLPOINTERARITH,
5527 message ("Pointer arithmetic involving possibly "
5528 "null pointer %s: %s",
5529 exprNode_unparse (e2),
5530 exprNode_unparse (ret)),
5534 ret->sref = sRef_copy (e2->sref);
5536 /* start modifications */
5537 /* added by Seejo on 4/16/2000 */
5539 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5542 if ((sRef_isPossiblyNullTerminated (e2->sref)) || (sRef_isNullTerminated(e2->sref))) {
5543 int val = (int) multiVal_forceInt (e1->val);
5545 /* Operator : + or += */
5546 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5547 if (sRef_getSize(e2->sref) >= val) {/* Incrementing the pointer by
5548 val should not result in a
5549 size < 0 (size = 0 is ok !) */
5551 sRef_setSize (ret->sref, sRef_getSize(e2->sref) - val);
5553 if (sRef_getLen(e2->sref) == val) { /* i.e. the character at posn val is \0 */
5554 sRef_setNotNullTerminatedState(ret->sref);
5555 sRef_resetLen (ret->sref);
5557 sRef_setNullTerminatedState(ret->sref);
5558 sRef_setLen (ret->sref, sRef_getLen(e2->sref) - val);
5563 /* Operator : - or -= */
5564 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5565 if (sRef_getSize(e2->sref) >= 0) {
5566 sRef_setSize (ret->sref, sRef_getSize(e2->sref) + val);
5567 sRef_setLen (ret->sref, sRef_getLen(e2->sref) + val);
5571 /* end modifications */
5573 sRef_setNullError (ret->sref);
5576 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5577 ** since is points to storage that should not be deallocated
5578 ** through this pointer.
5581 if (sRef_isOnly (ret->sref)
5582 || sRef_isFresh (ret->sref)) {
5583 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5587 ret->sref = e2->sref;
5591 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5600 case TAMPERSAND: /* bitwise & */
5602 case TCIRC: /* ^ (XOR) */
5607 bool reported = FALSE;
5613 if (opid == LEFT_OP || opid == LEFT_ASSIGN
5614 || opid == RIGHT_OP || opid == RIGHT_ASSIGN)
5617 ** evans 2002-01-01: fixed this to follow ISO 6.5.7.
5620 if (!ctype_isUnsigned (tr2)
5621 && !exprNode_isNonNegative (e2))
5623 reported = optgenerror
5625 message ("Right operand of %s may be negative (%t): %s",
5626 lltok_unparse (op), te2,
5627 exprNode_unparse (ret)),
5631 if (!ctype_isUnsigned (tr1)
5632 && !exprNode_isNonNegative (e1))
5634 reported = optgenerror
5635 (FLG_SHIFTIMPLEMENTATION,
5636 message ("Left operand of %s may be negative (%t): %s",
5637 lltok_unparse (op), te1,
5638 exprNode_unparse (ret)),
5643 ** Should check size of right operand also...
5649 if (!ctype_isUnsigned (tr1))
5651 if (exprNode_isNonNegative (e1)) {
5654 reported = optgenerror
5656 message ("Left operand of %s is not unsigned value (%t): %s",
5657 lltok_unparse (op), te1,
5658 exprNode_unparse (ret)),
5668 if (!ctype_isUnsigned (tr2))
5670 if (!exprNode_isNonNegative (e2)) {
5671 reported = optgenerror
5673 message ("Right operand of %s is not unsigned value (%t): %s",
5674 lltok_unparse (op), te2,
5675 exprNode_unparse (ret)),
5684 if (!checkIntegral (e1, e2, ret, op)) {
5685 te1 = ctype_unknown;
5689 DPRINTF (("Set: %s", ctype_unparse (te1)));
5692 ** tret is the widest type of te1 and te2
5695 tret = ctype_widest (te1, te2);
5700 if (checkIntegral (e1, e2, ret, op)) {
5703 tret = ctype_unknown;
5708 case TLT: /* comparisons */
5709 case TGT: /* numeric, numeric -> bool */
5711 DPRINTF (("Here we go: %s / %s",
5712 ctype_unparse (tr1), ctype_unparse (tr2)));
5714 if ((ctype_isReal (tr1) && !ctype_isInt (tr1))
5715 || (ctype_isReal (tr2) && !ctype_isInt (tr2)))
5718 bool fepsilon = FALSE;
5720 if (!ctype_isReal (rtype) || ctype_isInt (rtype))
5725 if (opid == TLT || opid == TGT)
5727 uentry ue1 = exprNode_getUentry (e1);
5728 uentry ue2 = exprNode_getUentry (e2);
5731 ** FLT_EPSILON, etc. really is a variable, not
5735 if (uentry_isVariable (ue1))
5737 cstring uname = uentry_rawName (ue1);
5739 if (cstring_equalLit (uname, "FLT_EPSILON")
5740 || cstring_equalLit (uname, "DBL_EPSILON")
5741 || cstring_equalLit (uname, "LDBL_EPSILON"))
5747 if (uentry_isVariable (ue2))
5749 cstring uname = uentry_rawName (ue2);
5751 if (cstring_equalLit (uname, "FLT_EPSILON")
5752 || cstring_equalLit (uname, "DBL_EPSILON")
5753 || cstring_equalLit (uname, "LDBL_EPSILON"))
5762 ; /* Don't complain. */
5768 message ("Dangerous comparison involving %s types: %s",
5769 ctype_unparse (rtype),
5770 exprNode_unparse (ret)),
5779 ** Types should match.
5782 DPRINTF (("Match types: %s / %s", exprNode_unparse (e1),
5783 exprNode_unparse (e2)));
5785 if (!exprNode_matchTypes (e1, e2))
5787 hasError = gentypeerror
5789 message ("Operands of %s have incompatible types (%t, %t): %s",
5790 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5796 || (ctype_isForceRealNumeric (&tr1)
5797 && ctype_isForceRealNumeric (&tr2)) ||
5798 (ctype_isRealPointer (tr1) && ctype_isRealPointer (tr2)))
5804 if ((ctype_isRealNumeric (tr1) && ctype_isRealPointer (tr2)) ||
5805 (ctype_isRealPointer (tr1) && ctype_isRealNumeric (tr2)))
5809 message ("Comparison of pointer and numeric (%t, %t): %s",
5810 te1, te2, exprNode_unparse (ret)),
5815 (void) checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5820 /* certain comparisons on unsigned's and zero look suspicious */
5822 if (opid == TLT || opid == LE_OP || opid == GE_OP)
5824 if ((ctype_isUnsigned (tr1) && exprNode_isZero (e2))
5825 || (ctype_isUnsigned (tr2) && exprNode_isZero (e1)))
5828 (FLG_UNSIGNEDCOMPARE,
5829 message ("Comparison of unsigned value involving zero: %s",
5830 exprNode_unparse (ret)),
5835 /* EQ_OP should NOT be used with booleans (unless one is FALSE) */
5837 if ((opid == EQ_OP || opid == NE_OP) &&
5838 ctype_isDirectBool (tr1) && ctype_isDirectBool (tr2))
5841 ** is one a variable?
5844 if (uentry_isVariable (exprNode_getUentry (e1))
5845 || uentry_isVariable (exprNode_getUentry (e2)))
5848 ** comparisons with FALSE are okay
5851 if (exprNode_isFalseConstant (e1)
5852 || exprNode_isFalseConstant (e2))
5861 ("Use of %q with %s variables (risks inconsistency because "
5862 "of multiple true values): %s",
5863 cstring_makeLiteral ((opid == EQ_OP) ? "==" : "!="),
5864 context_printBoolName (), exprNode_unparse (ret)),
5871 case AND_OP: /* bool, bool -> bool */
5874 if (ctype_isForceRealBool (&tr1) && ctype_isForceRealBool (&tr2))
5880 if (context_maybeSet (FLG_BOOLOPS))
5882 if (!ctype_isRealBool (te1) && !ctype_isRealBool (te2))
5884 if (ctype_sameName (te1, te2))
5888 message ("Operands of %s are non-boolean (%t): %s",
5889 lltok_unparse (op), te1,
5890 exprNode_unparse (ret)),
5898 ("Operands of %s are non-booleans (%t, %t): %s",
5899 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5903 else if (!ctype_isRealBool (te1))
5907 message ("Left operand of %s is non-boolean (%t): %s",
5908 lltok_unparse (op), te1, exprNode_unparse (ret)),
5911 else if (!ctype_isRealBool (te2))
5915 message ("Right operand of %s is non-boolean (%t): %s",
5916 lltok_unparse (op), te2, exprNode_unparse (ret)),
5929 (cstring_makeLiteral
5930 ("There has been a problem in the parser. This is believed to result "
5931 "from a problem with bison v. 1.25. Please try rebuidling Splint "
5932 "using the pre-compiled grammar files by commenting out the "
5933 "BISON= line in the top-level Makefile."));
5938 DPRINTF (("Return type: %s", ctype_unparse (tret)));
5941 exprNode_checkUse (ret, e1->sref, e1->loc);
5942 exprNode_mergeUSs (ret, e2);
5943 exprNode_checkUse (ret, e2->sref, e2->loc);
5949 exprNode_op (/*@only@*/ exprNode e1, /*@keep@*/ exprNode e2,
5950 /*@only@*/ lltok op)
5954 checkMacroParen (e1);
5955 checkMacroParen (e2);
5957 if (evaluationOrderUndefined (op) && context_maybeSet (FLG_EVALORDER))
5959 checkExpressionDefined (e1, e2, op);
5962 ret = exprNode_makeOp (e1, e2, op);
5967 void exprNode_checkAssignMod (exprNode e1, exprNode ret)
5970 ** This is somewhat bogus!
5972 ** Assigning to a nested observer in a non-observer datatype
5973 ** should not produce an error.
5976 sRef ref = exprNode_getSref (e1);
5978 DPRINTF (("Check assign mod: %s",
5979 sRef_unparseFull (ref)));
5981 if (sRef_isObserver (ref)
5982 || ((sRef_isFileStatic (ref) || sRef_isFileOrGlobalScope (ref))
5983 && ctype_isArray (ctype_realType (sRef_getType (ref)))))
5985 sRef base = sRef_getBase (ref);
5987 if (sRef_isValid (base) && sRef_isObserver (base))
5989 exprNode_checkModify (e1, ret);
5993 exprNode_checkModifyVal (e1, ret);
5998 exprNode_checkModify (e1, ret);
6003 exprNode_assign (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2, /*@only@*/ lltok op)
6005 bool isalloc = FALSE;
6006 bool isjustalloc = FALSE;
6007 bool noalias = FALSE;
6010 DPRINTF (("%s [%s] <- %s [%s]",
6011 exprNode_unparse (e1),
6012 ctype_unparse (e1->typ),
6013 exprNode_unparse (e2),
6014 ctype_unparse (e2->typ)));
6016 if (lltok_getTok (op) != TASSIGN)
6018 ret = exprNode_makeOp (e1, e2, op);
6020 DPRINTF (("Here goes: %s %s",
6021 ctype_unparse (e1->typ),
6022 ctype_unparse (e2->typ)));
6024 if (exprNode_isDefined (e1)
6025 && exprNode_isDefined (e2))
6027 if (ctype_isNumeric (e2->typ)
6028 || ctype_isNumeric (e1->typ))
6030 /* Its a pointer arithmetic expression like ptr += i */
6037 ret = exprNode_createPartialCopy (e1);
6038 ret->kind = XPR_ASSIGN;
6039 ret->edata = exprData_makeOp (e1, e2, op);
6041 if (!exprNode_isError (e2))
6043 ret->sets = sRefSet_union (ret->sets, e2->sets);
6044 ret->msets = sRefSet_union (ret->msets, e2->msets);
6045 ret->uses = sRefSet_union (ret->uses, e2->uses);
6049 checkExpressionDefined (e1, e2, op);
6051 if (exprNode_isError (e1))
6053 if (!exprNode_isError (e2))
6055 ret->loc = fileloc_update (ret->loc, e2->loc);
6059 ret->loc = fileloc_update (ret->loc, g_currentloc);
6063 if (!exprNode_isError (e2))
6065 checkMacroParen (e2);
6068 if (exprNode_isDefined (e1))
6070 if (sRef_isMacroParamRef (e1->sref))
6072 if (context_inIterDef ())
6074 uentry ue = sRef_getUentry (e1->sref);
6076 if (uentry_isYield (ue))
6082 if (fileloc_isDefined (e1->loc))
6086 message ("Assignment to non-yield iter parameter: %q",
6087 sRef_unparse (e1->sref)),
6094 message ("Assignment to non-yield iter parameter: %q",
6095 sRef_unparse (e1->sref)),
6102 if (fileloc_isDefined (e1->loc))
6106 message ("Assignment to macro parameter: %q",
6107 sRef_unparse (e1->sref)),
6114 message ("Assignment to macro parameter: %q",
6115 sRef_unparse (e1->sref)),
6119 exprNode_checkAssignMod (e1, ret); /* evans 2001-07-22 */
6124 exprNode_checkAssignMod (e1, ret);
6127 if (exprNode_isDefined (e2))
6129 if (lltok_getTok (op) == TASSIGN)
6131 ctype te1 = exprNode_getType (e1);
6132 ctype te2 = exprNode_getType (e2);
6134 if (ctype_isVoid (te2))
6138 message ("Assignment of void value to %t: %s %s %s",
6139 te1, exprNode_unparse (e1),
6141 exprNode_unparse (e2)),
6144 else if (!ctype_forceMatch (te1, te2))
6146 if (exprNode_matchLiteral (te1, e2))
6154 message ("Assignment of %t to %t: %s %s %s",
6155 te2, te1, exprNode_unparse (e1),
6157 exprNode_unparse (e2)),
6163 /* Type checks okay */
6167 exprNode_mergeUSs (ret, e2);
6168 exprNode_checkUse (ret, e2->sref, e2->loc);
6170 DPRINTF (("Do assign! %s %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6177 doAssign (e1, e2, FALSE);
6180 ret->sref = e1->sref;
6184 if (exprNode_isDefined (e2))
6186 exprNode_mergeUSs (ret, e2);
6187 exprNode_checkUse (ret, e2->sref, e2->loc);
6191 if (sRef_isPointer (e1->sref) && !sRef_isMacroParamRef (e1->sref))
6193 exprNode_checkUse (ret, sRef_getBase (e1->sref), e1->loc);
6196 isjustalloc = sRef_isJustAllocated (e1->sref);
6197 isalloc = sRef_isAllocated (e1->sref);
6199 if (sRef_isField (e1->sref))
6201 sRef root = sRef_getRootBase (sRef_getBase (e1->sref));
6203 if (!sRef_isAllocated (root) && !sRef_isMacroParamRef (root))
6205 exprNode_checkUse (ret, root, e1->loc);
6211 ** be careful! this defines e1->sref.
6214 /* evans 2001-07-22: removed if (!sRef_isMacroParamRef (e1->sref)) */
6216 DPRINTF (("Setting: %s -> %s", exprNode_unparse (ret), sRef_unparse (e1->sref)));
6217 exprNode_checkSet (ret, e1->sref);
6221 sRef_setAllocatedComplete (e1->sref, exprNode_isDefined (e2)
6222 ? e2->loc : e1->loc);
6228 sRef_setAllocatedShallowComplete (e1->sref, exprNode_loc (e2));
6237 exprNode_cond (/*@keep@*/ exprNode pred, /*@keep@*/ exprNode ifclause,
6238 /*@keep@*/ exprNode elseclause)
6242 if (!exprNode_isError (pred))
6244 ret = exprNode_createPartialCopy (pred);
6245 checkMacroParen (pred);
6246 exprNode_checkPred (cstring_makeLiteralTemp ("conditional"), pred);
6248 if (!exprNode_isError (ifclause))
6250 checkMacroParen (ifclause); /* update macro counts! */
6252 if (!exprNode_isError (elseclause))
6254 checkMacroParen (elseclause);
6256 if (!exprNode_matchTypes (ifclause, elseclause))
6259 (exprNode_getType (ifclause),
6261 exprNode_getType (elseclause),
6263 message ("Conditional clauses are not of same type: "
6265 exprNode_unparse (ifclause),
6266 exprNode_getType (ifclause),
6267 exprNode_unparse (elseclause),
6268 exprNode_getType (elseclause)),
6271 ret->sref = sRef_undefined;
6272 ret->typ = ctype_unknown;
6277 /* for now...should merge the states */
6278 ret->sref = ifclause->sref;
6279 ret->typ = ifclause->typ;
6281 if (exprNode_isNullValue (ifclause))
6283 ret->typ = elseclause->typ;
6287 exprNode_checkUse (ret, pred->sref, pred->loc);
6288 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6289 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6291 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6296 ret->typ = ifclause->typ;
6298 exprNode_checkUse (pred, pred->sref, pred->loc);
6299 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6301 exprNode_mergeCondUSs (ret, ifclause, exprNode_undefined);
6306 if (!exprNode_isError (elseclause))
6308 ret->typ = elseclause->typ;
6310 exprNode_checkUse (pred, pred->sref, pred->loc);
6311 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6313 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6317 else /* pred is error */
6319 if (!exprNode_isError (ifclause))
6321 ret = exprNode_createSemiCopy (ifclause);
6323 checkMacroParen (ifclause); /* update macro counts! */
6325 if (!exprNode_isError (elseclause))
6327 checkMacroParen (elseclause);
6329 ret->typ = ifclause->typ;
6331 if (!ctype_forceMatch (ifclause->typ, elseclause->typ))
6334 (exprNode_getType (ifclause),
6336 exprNode_getType (elseclause),
6338 message ("Conditional clauses are not of same type: "
6340 exprNode_unparse (ifclause),
6341 exprNode_getType (ifclause),
6342 exprNode_unparse (elseclause),
6343 exprNode_getType (elseclause)),
6346 ret->typ = ctype_unknown;
6350 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6351 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6353 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6356 else if (!exprNode_isError (elseclause)) /* pred, if errors */
6358 ret = exprNode_createSemiCopy (ifclause);
6360 ret->typ = elseclause->typ;
6361 checkMacroParen (elseclause);
6363 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6364 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6366 else /* all errors! */
6368 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6372 ret->kind = XPR_COND;
6373 ret->edata = exprData_makeCond (pred, ifclause, elseclause);
6375 if (exprNode_isDefined (ifclause) && exprNode_isDefined (elseclause))
6377 exprNode_combineControl (ret, ifclause, elseclause);
6384 exprNode_vaArg (/*@only@*/ lltok tok, /*@only@*/ exprNode arg, /*@only@*/ qtype qt)
6386 ctype totype = qtype_getType (qt);
6388 exprNode_createPartialLocCopy (arg, fileloc_copy (lltok_getLoc (tok)));
6392 ** check use of va_arg : <valist>, type -> type
6395 if (exprNode_isError (arg))
6400 targ = exprNode_getType (arg);
6403 ** arg should have be a pointer
6406 if (!ctype_isUA (targ) ||
6407 (!usymId_equal (ctype_typeId (targ),
6408 usymtab_getTypeId (cstring_makeLiteralTemp ("va_list")))))
6412 message ("First argument to va_arg is not a va_list (type %t): %s",
6413 targ, exprNode_unparse (arg)),
6417 exprNode_checkSet (ret, arg->sref);
6421 ** return type is totype
6425 ret->kind = XPR_VAARG;
6426 ret->edata = exprData_makeCast (tok, arg, qt);
6431 exprNode exprNode_labelMarker (/*@only@*/ cstring label)
6433 exprNode ret = exprNode_createPlain (ctype_undefined);
6434 ret->kind = XPR_LABEL;
6435 ret->edata = exprData_makeLiteral (label);
6436 ret->isJumpPoint = TRUE;
6438 return (ret); /* for now, ignore label */
6441 exprNode exprNode_notReached (/*@returned@*/ exprNode stmt)
6443 if (exprNode_isDefined (stmt))
6445 stmt->isJumpPoint = TRUE;
6447 /* This prevent stray no return path errors, etc. */
6448 stmt->exitCode = XK_MUSTEXIT;
6454 bool exprNode_isDefaultMarker (exprNode e)
6456 if (exprNode_isDefined (e))
6458 return (e->kind == XPR_DEFAULT || e->kind == XPR_FTDEFAULT);
6464 bool exprNode_isCaseMarker (exprNode e)
6466 if (exprNode_isDefined (e))
6468 return (e->kind == XPR_FTCASE || e->kind == XPR_CASE);
6474 bool exprNode_isLabelMarker (exprNode e)
6476 if (exprNode_isDefined (e))
6478 return (e->kind == XPR_LABEL);
6484 exprNode exprNode_caseMarker (/*@only@*/ exprNode test, bool fallThrough)
6486 exprNode ret = exprNode_createPartialCopy (test);
6488 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6490 if (exprNode_isError (test)) {
6494 exprNode_checkUse (ret, test->sref, test->loc);
6496 usymtab_setExitCode (ret->exitCode);
6500 usymtab_setMustBreak ();
6503 ret->edata = exprData_makeSingle (test);
6504 ret->isJumpPoint = TRUE;
6510 exprNode exprNode_caseStatement (/*@only@*/ exprNode test, /*@only@*/ exprNode stmt, bool fallThrough)
6512 exprNode ret = exprNode_createPartialCopy (test);
6514 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6515 ret->edata = exprData_makePair (test, stmt);
6516 ret->isJumpPoint = TRUE;
6518 if (exprNode_isError (test))
6523 exprNode_checkUse (ret, test->sref, test->loc);
6525 if (exprNode_isError (stmt))
6530 exprNode_mergeUSs (ret, stmt);
6532 ret->exitCode = stmt->exitCode;
6533 ret->mustBreak = stmt->mustBreak;
6534 ret->canBreak = stmt->canBreak;
6536 usymtab_setExitCode (ret->exitCode);
6540 usymtab_setMustBreak ();
6547 /*@notnull@*/ /*@only@*/ exprNode
6548 exprNode_defaultMarker (/*@only@*/ lltok def, bool fallThrough)
6550 exprNode ret = exprNode_createTok (def);
6552 ret->isJumpPoint = TRUE;
6553 ret->kind = fallThrough ? XPR_FTDEFAULT : XPR_DEFAULT;
6558 exprNode_mayEscape (exprNode e)
6560 if (exprNode_isDefined (e))
6562 return exitkind_couldEscape (e->exitCode);
6568 exprNode_mustBreak (exprNode e)
6570 if (exprNode_isDefined (e))
6572 return e->mustBreak;
6579 exprNode_mustEscape (exprNode e)
6581 if (exprNode_isDefined (e))
6583 return exitkind_mustEscape (e->exitCode) || exprNode_mustBreak (e);
6590 exprNode_errorEscape (exprNode e)
6592 if (exprNode_isDefined (e))
6594 return exitkind_isError (e->exitCode);
6600 exprNode exprNode_concat (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
6602 exprNode ret = exprNode_createPartialCopy (e1);
6604 DPRINTF (("Concat: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6606 ret->edata = exprData_makePair (e1, e2);
6607 ret->kind = XPR_STMTLIST;
6609 if (exprNode_isDefined (e1))
6611 ret->isJumpPoint = e1->isJumpPoint;
6612 ret->canBreak = e1->canBreak;
6616 if (exprNode_isDefined (e2))
6618 ret->loc = fileloc_update (ret->loc, e2->loc);
6622 if (exprNode_isDefined (e2))
6624 ret->exitCode = e2->exitCode;
6625 ret->mustBreak = e2->mustBreak;
6626 if (e2->canBreak) ret->canBreak = TRUE;
6630 ** if e1 must return, then e2 is unreachable!
6633 if (exprNode_isDefined (e1) && exprNode_isDefined (e2))
6635 if ((exprNode_mustEscape (e1) || exprNode_mustBreak (e1))
6636 && !(e2->isJumpPoint))
6638 if (context_getFlag (FLG_UNREACHABLE))
6642 if (e2->kind == XPR_STMT)
6644 nr = exprData_getUopNode (e2->edata);
6647 if ((nr->kind == XPR_TOK
6648 && lltok_isSemi (exprData_getTok (nr->edata))))
6650 /* okay to have unreachable ";" */
6651 ret->exitCode = XK_MUSTEXIT;
6652 ret->canBreak = TRUE;
6656 if (optgenerror (FLG_UNREACHABLE,
6657 message ("Unreachable code: %s",
6658 exprNode_unparseFirst (nr)),
6661 ret->isJumpPoint = TRUE;
6662 ret->mustBreak = FALSE;
6663 ret->exitCode = XK_ERROR;
6664 DPRINTF (("Jump point: %s", exprNode_unparse (ret)));
6668 ret->exitCode = XK_MUSTEXIT;
6669 ret->canBreak = TRUE;
6677 if ((e2->kind == XPR_CASE || e2->kind == XPR_DEFAULT))
6680 ** We want a warning anytime we have:
6682 ** yyy; <<<- no break or return
6686 exprNode lastStmt = exprNode_lastStatement (e1);
6688 if (exprNode_isDefined (lastStmt)
6689 && !exprNode_mustEscape (lastStmt)
6690 && !exprNode_mustBreak (lastStmt)
6691 && !exprNode_isCaseMarker (lastStmt)
6692 && !exprNode_isDefaultMarker (lastStmt)
6693 && !exprNode_isLabelMarker (lastStmt))
6695 voptgenerror (FLG_CASEBREAK,
6697 ("Fall through case (no preceding break)"),
6704 exprNode_mergeUSs (ret, e2);
6706 usymtab_setExitCode (ret->exitCode);
6710 usymtab_setMustBreak ();
6713 DPRINTF (("==> %s", exprNode_unparse (ret)));
6717 exprNode exprNode_createTok (/*@only@*/ lltok t)
6719 exprNode ret; /*@i23 if on same line, bad things happen...!@*/
6720 ret = exprNode_create (ctype_unknown);
6721 ret->kind = XPR_TOK;
6722 ret->edata = exprData_makeTok (t);
6726 exprNode exprNode_statement (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6728 if (!exprNode_isError (e))
6730 exprChecks_checkStatementEffect(e);
6733 return (exprNode_statementError (e, t));
6736 static exprNode exprNode_statementError (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6738 exprNode ret = exprNode_createPartialCopy (e);
6740 if (!exprNode_isError (e))
6742 if (e->kind != XPR_ASSIGN)
6744 exprNode_checkUse (ret, e->sref, e->loc);
6747 ret->exitCode = e->exitCode;
6748 ret->canBreak = e->canBreak;
6749 ret->mustBreak = e->mustBreak;
6752 ret->edata = exprData_makeUop (e, t);
6753 ret->kind = XPR_STMT;
6758 exprNode exprNode_checkExpr (/*@returned@*/ exprNode e)
6760 if (!exprNode_isError (e))
6762 if (e->kind != XPR_ASSIGN)
6764 exprNode_checkUse (e, e->sref, e->loc);
6771 void exprNode_produceGuards (exprNode pred)
6773 if (!exprNode_isError (pred))
6775 if (ctype_isRealPointer (pred->typ))
6777 pred->guards = guardSet_addTrueGuard (pred->guards, pred->sref);
6780 exprNode_checkUse (pred, pred->sref, pred->loc);
6781 exprNode_resetSref (pred);
6785 exprNode exprNode_compoundStatementExpression (/*@only@*/ lltok tlparen, /*@only@*/ exprNode e)
6789 DPRINTF (("Compound: %s", exprNode_unparse (e)));
6791 if (!context_flagOn (FLG_GNUEXTENSIONS, exprNode_loc (e)))
6793 (void) llgenhinterror
6795 message ("Compound statement expressions is not supported by ISO C99"),
6796 message ("Use +gnuextensions to allow compound statement expressions (and other GNU language extensions) "
6797 "without this warning"),
6802 ** The type of a compoundStatementExpression is the type of the last statement
6805 llassert (exprNode_isBlock (e));
6806 laststmt = exprNode_lastStatement (e);
6808 DPRINTF (("Last statement: %s / %s", exprNode_unparse (laststmt), ctype_unparse (exprNode_getType (laststmt))));
6809 DPRINTF (("e: %s", exprNode_unparse (e)));
6810 e->typ = exprNode_getType (laststmt);
6811 return exprNode_addParens (tlparen, e);
6815 exprNode exprNode_makeBlock (/*@only@*/ exprNode e)
6817 exprNode ret = exprNode_createPartialCopy (e);
6819 if (!exprNode_isError (e))
6821 ret->exitCode = e->exitCode;
6822 ret->canBreak = e->canBreak;
6823 ret->mustBreak = e->mustBreak;
6826 DPRINTF (("Block e: %s", exprNode_unparse (e)));
6827 ret->edata = exprData_makeSingle (e);
6828 ret->kind = XPR_BLOCK;
6829 DPRINTF (("Block: %s", exprNode_unparse (ret)));
6833 bool exprNode_isBlock (exprNode e)
6835 return (exprNode_isDefined (e)
6836 && ((e)->kind == XPR_BLOCK));
6839 bool exprNode_isStatement (exprNode e)
6841 return (exprNode_isDefined (e)
6842 && ((e)->kind == XPR_STMT));
6845 bool exprNode_isAssign (exprNode e)
6847 if (exprNode_isDefined (e))
6849 return (e->kind == XPR_ASSIGN);
6855 bool exprNode_isEmptyStatement (exprNode e)
6857 return (exprNode_isDefined (e)
6858 && (e->kind == XPR_TOK)
6859 && (lltok_isSemi (exprData_getTok (e->edata))));
6862 bool exprNode_isMultiStatement (exprNode e)
6864 return (exprNode_isDefined (e)
6865 && ((e->kind == XPR_FOR)
6866 || (e->kind == XPR_FORPRED)
6867 || (e->kind == XPR_IF)
6868 || (e->kind == XPR_IFELSE)
6869 || (e->kind == XPR_WHILE)
6870 || (e->kind == XPR_WHILEPRED)
6871 || (e->kind == XPR_DOWHILE)
6872 || (e->kind == XPR_BLOCK)
6873 || (e->kind == XPR_STMT)
6874 || (e->kind == XPR_STMTLIST)
6875 || (e->kind == XPR_SWITCH)));
6878 void exprNode_checkIfPred (exprNode pred)
6880 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6883 exprNode exprNode_if (/*@only@*/ exprNode pred, /*@only@*/ exprNode tclause)
6886 bool emptyErr = FALSE;
6888 if (context_maybeSet (FLG_IFEMPTY))
6890 if (exprNode_isEmptyStatement (tclause))
6892 emptyErr = optgenerror (FLG_IFEMPTY,
6894 ("Body of if statement is empty"),
6895 exprNode_loc (tclause));
6899 if (!emptyErr && context_maybeSet (FLG_IFBLOCK))
6901 if (exprNode_isDefined (tclause)
6902 && !exprNode_isBlock (tclause))
6904 voptgenerror (FLG_IFBLOCK,
6906 ("Body of if statement is not a block: %s",
6907 exprNode_unparse (tclause)),
6908 exprNode_loc (tclause));
6912 if (exprNode_isError (pred))
6914 if (exprNode_isError (tclause))
6916 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6920 ret = exprNode_createPartialCopy (tclause);
6925 if (exprNode_mustEscape (pred))
6929 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6930 exprNode_loc (pred));
6933 /*! exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred); */ /*@i523@*/
6934 exprNode_checkUse (pred, pred->sref, pred->loc);
6936 if (!exprNode_isError (tclause))
6938 exprNode_mergeCondUSs (pred, tclause, exprNode_undefined);
6941 ret = exprNode_createPartialCopy (pred);
6945 ret->edata = exprData_makePair (pred, tclause);
6947 ret->exitCode = XK_UNKNOWN;
6949 if (exprNode_isDefined (tclause))
6951 ret->exitCode = exitkind_makeConditional (tclause->exitCode);
6952 ret->canBreak = tclause->canBreak;
6953 ret->sets = sRefSet_union (ret->sets, tclause->sets);
6954 ret->msets = sRefSet_union (ret->msets, tclause->msets);
6955 ret->uses = sRefSet_union (ret->uses, tclause->uses);
6958 ret->mustBreak = FALSE;
6963 exprNode exprNode_ifelse (/*@only@*/ exprNode pred,
6964 /*@only@*/ exprNode tclause,
6965 /*@only@*/ exprNode eclause)
6968 bool tEmptyErr = FALSE;
6969 bool eEmptyErr = FALSE;
6971 if (context_maybeSet (FLG_IFEMPTY))
6973 if (exprNode_isEmptyStatement (tclause))
6975 tEmptyErr = optgenerror
6978 ("Body of if clause of if statement is empty"),
6979 exprNode_loc (tclause));
6982 if (exprNode_isEmptyStatement (eclause))
6984 eEmptyErr = optgenerror
6987 ("Body of else clause of if statement is empty"),
6988 exprNode_loc (eclause));
6992 if (context_maybeSet (FLG_IFBLOCK))
6995 && exprNode_isDefined (tclause)
6996 && !exprNode_isBlock (tclause))
6998 voptgenerror (FLG_IFBLOCK,
7000 ("Body of if clause of if statement is not a block: %s",
7001 exprNode_unparse (tclause)),
7002 exprNode_loc (tclause));
7006 && exprNode_isDefined (eclause)
7007 && !exprNode_isBlock (eclause)
7008 && !(eclause->kind == XPR_IF)
7009 && !(eclause->kind == XPR_IFELSE))
7014 ("Body of else clause of if statement is not a block: %s",
7015 exprNode_unparse (eclause)),
7016 exprNode_loc (eclause));
7020 if (context_maybeSet (FLG_ELSEIFCOMPLETE))
7022 if (exprNode_isDefined (eclause)
7023 && (eclause->kind == XPR_IF))
7025 voptgenerror (FLG_ELSEIFCOMPLETE,
7026 message ("Incomplete else if logic (no final else): %s",
7027 exprNode_unparse (eclause)),
7028 exprNode_loc (eclause));
7032 if (exprNode_isError (pred))
7034 if (exprNode_isError (tclause))
7036 if (exprNode_isError (eclause))
7038 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7042 ret = exprNode_createPartialCopy (eclause);
7047 ret = exprNode_createPartialCopy (tclause);
7050 else /* pred is okay */
7052 ret = exprNode_createPartialCopy (pred);
7054 if (exprNode_mustEscape (pred))
7058 message ("Predicate always exits: %s", exprNode_unparse (pred)),
7059 exprNode_loc (pred));
7062 /*@i3423 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);*/
7063 exprNode_checkUse (ret, pred->sref, pred->loc);
7065 exprNode_mergeCondUSs (ret, tclause, eclause);
7068 ret->kind = XPR_IFELSE;
7069 ret->edata = exprData_makeCond (pred, tclause, eclause);
7071 if (exprNode_isDefined (tclause) && exprNode_isDefined (eclause))
7073 exprNode_combineControl (ret, tclause, eclause);
7074 ret->loc = fileloc_update (ret->loc, eclause->loc);
7081 ** *allpaths <- TRUE iff all executions paths must go through the switch
7085 checkSwitchExpr (exprNode test, /*@dependent@*/ exprNode e, /*@out@*/ bool *allpaths)
7087 exprNodeSList el = exprNode_flatten (e);
7088 bool mustReturn = TRUE; /* find a branch that doesn't */
7089 bool thisReturn = FALSE;
7090 bool hasDefault = FALSE;
7091 bool hasAllMembers = FALSE;
7092 bool inSwitch = FALSE;
7093 bool isEnumSwitch = FALSE;
7094 bool canBreak = FALSE;
7095 bool fallThrough = FALSE;
7096 ctype ct = ctype_unknown;
7097 enumNameSList usedEnums;
7100 if (exprNode_isDefined (test))
7105 ttype = ctype_realType (ct);
7107 if (ctype_isEnum (ttype))
7109 isEnumSwitch = TRUE;
7110 enums = ctype_elist (ttype);
7111 usedEnums = enumNameSList_new ();
7115 exprNodeSList_elements (el, current)
7118 DPRINTF ((message("checkSwitchExpr current = %s ", exprNode_unparse(current) ) ));
7120 if (exprNode_isDefined (current))
7122 switch (current->kind)
7129 (FLG_DUPLICATECASES,
7130 message ("Duplicate default cases in switch"),
7131 exprNode_loc (current));
7136 if (current->kind == XPR_DEFAULT || current->kind == XPR_FTDEFAULT)
7144 exprNode st = exprData_getSingle (current->edata);
7145 uentry ue = exprNode_getUentry (st);
7147 if (uentry_isValid (ue))
7149 cstring cname = uentry_rawName (ue);
7151 if (enumNameList_member (/*@-usedef@*/enums/*@=usedef@*/, cname))
7153 if (enumNameSList_member
7154 (/*@-usedef@*/usedEnums/*@=usedef@*/, cname))
7157 (FLG_DUPLICATECASES,
7158 message ("Duplicate case in switch: %s",
7164 enumNameSList_addh (usedEnums, cname);
7171 message ("Case in switch not %s member: %s",
7172 ctype_unparse (ct), cname),
7179 if (inSwitch && !fallThrough)
7181 if (!thisReturn || canBreak)
7191 /*@switchbreak@*/ break;
7193 thisReturn = thisReturn || exprNode_mustEscape (current);
7194 canBreak = canBreak || current->canBreak;
7195 if (canBreak) fallThrough = FALSE;
7198 } end_exprNodeSList_elements;
7200 if (inSwitch) /* check the last one! */
7202 if (!thisReturn || canBreak)
7211 && (enumNameSList_size (/*@-usedef@*/usedEnums/*@=usedef@*/) !=
7212 enumNameList_size (/*@-usedef@*/enums/*@=usedef@*/)))
7214 enumNameSList unused = enumNameSList_subtract (enums, usedEnums);
7216 voptgenerror (FLG_MISSCASE,
7217 message ("Missing case%s in switch: %q",
7218 cstring_makeLiteralTemp
7219 ((enumNameSList_size (unused) > 1) ? "s" : ""),
7220 enumNameSList_unparse (unused)),
7223 enumNameSList_free (unused);
7224 *allpaths = FALSE; /* evans 2002-01-01 */
7228 hasAllMembers = TRUE;
7232 enumNameSList_free (usedEnums);
7236 *allpaths = hasDefault;
7239 exprNodeSList_free (el);
7240 return ((hasDefault || hasAllMembers) && mustReturn);
7243 exprNode exprNode_switch (/*@only@*/ exprNode e, /*@only@*/ exprNode s)
7245 exprNode ret = exprNode_createPartialCopy (e);
7248 DPRINTF (("Switch: %s", exprNode_unparse (s)));
7250 ret->kind = XPR_SWITCH;
7251 ret->edata = exprData_makePair (e, s);
7253 if (!exprNode_isError (s))
7255 exprNode fs = exprNode_firstStatement (s);
7256 ret->loc = fileloc_update (ret->loc, s->loc);
7258 if (exprNode_isUndefined (fs)
7259 || exprNode_isCaseMarker (fs) || exprNode_isLabelMarker (fs)
7260 || exprNode_isDefaultMarker (fs)) {
7263 voptgenerror (FLG_FIRSTCASE,
7265 ("Statement after switch is not a case: %s", exprNode_unparse (fs)),
7270 if (!exprNode_isError (e))
7272 if (checkSwitchExpr (e, s, &allpaths))
7274 ret->exitCode = XK_MUSTRETURN;
7278 ret->exitCode = e->exitCode;
7281 ret->canBreak = e->canBreak;
7282 ret->mustBreak = e->mustBreak;
7286 ** exprNode.c:3883,32: Variable allpaths used before definition
7293 DPRINTF (("Context exit switch!"));
7294 context_exitSwitch (ret, allpaths);
7295 DPRINTF (("Context exit switch done!"));
7300 static void checkInfiniteLoop (/*@notnull@*/ exprNode test,
7301 /*@notnull@*/ exprNode body)
7303 sRefSet tuses = test->uses;
7305 if (!sRefSet_isEmpty (test->uses))
7307 sRefSet sets = sRefSet_newCopy (body->sets);
7308 bool hasError = TRUE;
7309 bool innerState = FALSE;
7310 sRefSet tuncon = sRefSet_undefined;
7312 sets = sRefSet_union (sets, test->sets);
7313 sets = sRefSet_union (sets, body->msets);
7314 sets = sRefSet_union (sets, test->msets);
7316 sRefSet_allElements (tuses, el)
7318 if (sRef_isUnconstrained (el))
7320 tuncon = sRefSet_insert (tuncon, el);
7324 if (sRefSet_member (sets, el))
7331 if (sRef_isInternalState (el)
7332 || sRef_isFileStatic (sRef_getRootBase (el)))
7336 } end_sRefSet_allElements ;
7340 sRefSet suncon = sRefSet_undefined;
7341 bool sinner = FALSE;
7343 sRefSet_allElements (sets, el)
7345 if (sRef_isUnconstrained (el))
7347 suncon = sRefSet_insert (suncon, el);
7349 else if (sRef_isInternalState (el))
7357 } end_sRefSet_allElements ;
7359 if (sinner && innerState)
7363 else if (sRefSet_isEmpty (tuncon)
7364 && sRefSet_isEmpty (suncon))
7369 ("Suspected infinite loop. No value used in loop test (%q) "
7370 "is modified by test or loop body.",
7371 sRefSet_unparsePlain (tuses)),
7376 if (sRefSet_isEmpty (tuncon))
7380 message ("Suspected infinite loop. No condition values "
7381 "modified. Modification possible through "
7382 "unconstrained calls: %q",
7383 sRefSet_unparsePlain (suncon)),
7390 message ("Suspected infinite loop. No condition values "
7391 "modified. Possible undetected dependency through "
7392 "unconstrained calls in loop test: %q",
7393 sRefSet_unparsePlain (tuncon)),
7399 sRefSet_free (sets);
7403 exprNode exprNode_while (/*@keep@*/ exprNode t, /*@keep@*/ exprNode b)
7406 bool emptyErr = FALSE;
7408 if (context_maybeSet (FLG_WHILEEMPTY))
7410 if (exprNode_isEmptyStatement (b))
7412 emptyErr = optgenerror
7415 ("Body of while statement is empty"),
7420 if (!emptyErr && context_maybeSet (FLG_WHILEBLOCK))
7422 if (exprNode_isDefined (b)
7423 && !exprNode_isBlock (b))
7425 if (context_inIterDef ()
7426 && (b->kind == XPR_STMTLIST
7427 || b->kind == XPR_TOK))
7433 voptgenerror (FLG_WHILEBLOCK,
7435 ("Body of while statement is not a block: %s",
7436 exprNode_unparse (b)),
7442 if (exprNode_isError (t))
7444 if (exprNode_isError (b))
7446 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7450 ret = exprNode_createPartialCopy (b);
7457 ret = exprNode_createPartialCopy (t);
7459 llassert (t->kind == XPR_WHILEPRED);
7461 test = exprData_getSingle (t->edata);
7463 if (!exprNode_isError (b) && exprNode_isDefined (test))
7465 if (context_maybeSet (FLG_INFLOOPS)
7466 || context_maybeSet (FLG_INFLOOPSUNCON))
7469 ** check that some variable in the predicate is set by the body
7470 ** if the predicate uses any variables
7473 checkInfiniteLoop (test, b);
7476 exprNode_mergeUSs (ret, b);
7478 if (exprNode_isDefined (b))
7480 ret->exitCode = exitkind_makeConditional (b->exitCode);
7485 ret->edata = exprData_makePair (t, b);
7486 ret->kind = XPR_WHILE;
7488 if (exprNode_isDefined (t) && exprNode_mustEscape (t))
7492 message ("Predicate always exits: %s", exprNode_unparse (t)),
7496 ret->exitCode = XK_NEVERESCAPE;
7499 ** If loop is infinite, and there is no break inside,
7500 ** exit code is never reach.
7503 if (exprNode_knownIntValue (t))
7505 if (!exprNode_isZero (t))
7507 if (exprNode_isDefined (b))
7511 /* Really, it means never reached. */
7512 ret->exitCode = XK_MUSTEXIT;
7522 ret->canBreak = FALSE;
7523 ret->mustBreak = FALSE;
7529 ** do { b } while (t);
7531 ** note: body passed as first argument
7534 exprNode exprNode_doWhile (/*@only@*/ exprNode b, /*@only@*/ exprNode t)
7538 DPRINTF (("Do while: %s / %s",
7539 exprNode_unparse (b), exprNode_unparse (t)));
7541 if (exprNode_isError (t))
7543 if (exprNode_isError (b))
7545 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7549 ret = exprNode_createPartialCopy (b);
7551 ret->exitCode = exitkind_makeConditional (b->exitCode);
7552 exprNode_checkUse (ret, b->sref, b->loc);
7553 ret->exitCode = b->exitCode;
7554 ret->canBreak = b->canBreak;
7555 ret->mustBreak = FALSE;
7560 DPRINTF (("Do while: %s / %s",
7561 exitkind_unparse (t->exitCode),
7562 exitkind_unparse (b->exitCode)));
7564 ret = exprNode_createPartialCopy (t);
7565 exprNode_checkPred (cstring_makeLiteralTemp ("while"), t);
7567 if (!exprNode_isError (b))
7570 ** forgot the copy's --- why wasn't this detected??
7573 ret->sets = sRefSet_copyInto (ret->sets, b->sets);
7574 ret->msets = sRefSet_copyInto (ret->msets, b->msets);
7575 ret->uses = sRefSet_copyInto (ret->uses, b->uses);
7577 /* left this out --- causes and aliasing bug (infinite loop)
7578 should be detected?? */
7580 exprNode_checkUse (ret, b->sref, b->loc);
7581 exprNode_mergeUSs (ret, t);
7582 exprNode_checkUse (ret, t->sref, t->loc);
7584 /* evans 2001-10-05: while loop can break */
7585 ret->exitCode = exitkind_makeConditional (b->exitCode);
7587 DPRINTF (("Do while: %s",
7588 exitkind_unparse (ret->exitCode)));
7590 ret->canBreak = b->canBreak;
7592 /* Always FALSE for doWhile loops - break's when test is false */
7593 ret->mustBreak = FALSE; /* b->mustBreak; */
7597 context_exitDoWhileClause (t);
7599 ret->kind = XPR_DOWHILE;
7600 ret->edata = exprData_makePair (t, b);
7604 bool exprNode_loopMustExec (exprNode forPred)
7607 ** Returns true if it is obvious that the loop always executes at least once
7609 ** For now, we only identify the most obvious cases. Should be true anytime
7610 ** we can prove init => !test.
7613 if (exprNode_isDefined (forPred))
7615 exprNode init, test, inc;
7618 llassert (forPred->kind == XPR_FORPRED);
7620 edata = forPred->edata;
7621 init = exprData_getTripleInit (edata);
7622 test = exprData_getTripleTest (edata);
7623 inc = exprData_getTripleInc (edata);
7625 if (exprNode_isAssign (init))
7627 exprNode loopVar = exprData_getOpA (init->edata);
7628 exprNode loopInit = exprData_getOpB (init->edata);
7630 if (exprNode_isDefined (test) && test->kind == XPR_OP)
7632 exprNode testVar = exprData_getOpA (test->edata);
7633 exprNode testVal = exprData_getOpB (test->edata);
7634 lltok comp = exprData_getOpTok (test->edata);
7635 int opid = lltok_getTok (comp);
7637 DPRINTF (("Same storage: %s / %s", exprNode_unparse (loopVar),
7638 exprNode_unparse (testVar)));
7640 if (exprNode_sameStorage (loopVar, testVar))
7642 multiVal valinit = exprNode_getValue (loopInit);
7643 multiVal valtest = exprNode_getValue (testVal);
7645 DPRINTF (("Values: %s / %s", multiVal_unparse (valinit),
7646 multiVal_unparse (valtest)));
7648 if (multiVal_isInt (valinit) && multiVal_isInt (valtest))
7650 long v1 = multiVal_forceInt (valinit);
7651 long v2 = multiVal_forceInt (valtest);
7653 DPRINTF (("Here: %ld %ld", v1, v2));
7655 if ((opid == EQ_OP && v1 < v2)
7656 || (opid == NE_OP && v1 != v2)
7657 || (opid == TLT && v1 <= v2)
7658 || (opid == TGT && v1 >= v2)
7659 || (opid == LE_OP && v1 < v2)
7660 || (opid == GE_OP && v1 > v2))
7662 DPRINTF (("mustexec if inc"));
7671 DPRINTF (("loop must exec: FALSE"));
7675 exprNode exprNode_for (/*@keep@*/ exprNode inc, /*@keep@*/ exprNode body)
7678 bool emptyErr = FALSE;
7680 if (context_maybeSet (FLG_FOREMPTY))
7682 if (exprNode_isEmptyStatement (body))
7684 emptyErr = optgenerror
7687 ("Body of for statement is empty"),
7688 exprNode_loc (body));
7692 if (!emptyErr && context_maybeSet (FLG_FORBLOCK))
7694 if (exprNode_isDefined (body)
7695 && !exprNode_isBlock (body))
7697 if (context_inIterDef ()
7698 && (body->kind == XPR_STMTLIST
7699 || body->kind == XPR_TOK))
7705 voptgenerror (FLG_FORBLOCK,
7707 ("Body of for statement is not a block: %s",
7708 exprNode_unparse (body)),
7709 exprNode_loc (body));
7715 ** for ud purposes: (alreadly) init -> test -> (now) LOOP: body + inc + test
7718 if (exprNode_isError (body))
7720 ret = exprNode_createPartialCopy (inc);
7724 ret = exprNode_createPartialCopy (body);
7726 ret->exitCode = exitkind_makeConditional (body->exitCode);
7728 exprNode_mergeUSs (inc, body);
7730 if (exprNode_isDefined (inc))
7734 context_setMessageAnnote (cstring_makeLiteral ("in post loop increment"));
7736 tmp = exprNode_effect (exprData_getTripleInc (inc->edata));
7737 exprNode_freeShallow (tmp);
7739 context_clearMessageAnnote ();
7740 context_setMessageAnnote (cstring_makeLiteral ("in post loop test"));
7742 tmp = exprNode_effect (exprData_getTripleTest (inc->edata));
7743 exprNode_freeShallow (tmp);
7745 context_clearMessageAnnote ();
7747 ret->uses = sRefSet_copyInto (ret->uses, inc->uses);
7748 ret->sets = sRefSet_copyInto (ret->sets, inc->sets);
7749 ret->msets = sRefSet_copyInto (ret->msets, inc->msets);
7753 ret->kind = XPR_FOR;
7754 ret->edata = exprData_makePair (inc, body);
7756 if (exprNode_isDefined (inc)) {
7757 exprNode test = exprData_getTripleTest (inc->edata);
7759 if (exprNode_isUndefined (test)) {
7760 if (exprNode_isDefined (body)) {
7761 if (!body->canBreak) {
7762 /* Really, it means never reached. */
7763 ret->exitCode = XK_MUSTEXIT;
7773 ** for (init; test; inc)
7776 ** while (test) { body; inc; }
7778 ** Now: check use of init (may set vars for test)
7779 ** check use of test
7783 /*@observer@*/ guardSet exprNode_getForGuards (exprNode pred)
7787 if (exprNode_isError (pred)) return guardSet_undefined;
7789 llassert (pred->kind == XPR_FORPRED);
7791 test = exprData_getTripleTest (pred->edata);
7793 if (!exprNode_isError (test))
7795 return (test->guards);
7798 return guardSet_undefined;
7801 exprNode exprNode_whilePred (/*@only@*/ exprNode test)
7803 exprNode ret = exprNode_createSemiCopy (test);
7805 if (exprNode_isDefined (test))
7807 exprNode_copySets (ret, test);
7808 exprNode_checkPred (cstring_makeLiteralTemp ("while"), test);
7809 exprNode_checkUse (ret, test->sref, test->loc);
7811 exprNode_produceGuards (test);
7813 ret->guards = guardSet_copy (test->guards);
7816 ret->edata = exprData_makeSingle (test);
7817 ret->kind = XPR_WHILEPRED;
7821 exprNode exprNode_forPred (/*@only@*/ exprNode init, /*@only@*/ exprNode test,
7822 /*@only@*/ exprNode inc)
7827 ** for ud purposes: init -> test -> LOOP: [ body, inc ]
7830 exprNode_checkPred (cstring_makeLiteralTemp ("for"), test);
7832 if (!exprNode_isError (inc))
7834 ret = exprNode_createPartialCopy (inc);
7838 if (!exprNode_isError (init))
7840 ret = exprNode_createPartialCopy (init);
7842 else if (!exprNode_isError (test))
7844 ret = exprNode_createPartialCopy (test);
7848 ret = exprNode_createUnknown ();
7852 exprNode_mergeUSs (ret, init);
7854 if (exprNode_isDefined (init))
7856 exprNode_checkUse (ret, init->sref, init->loc);
7859 exprNode_mergeUSs (ret, test);
7861 if (exprNode_isDefined (test))
7863 exprNode_checkUse (ret, test->sref, test->loc);
7866 ret->kind = XPR_FORPRED;
7867 ret->edata = exprData_makeFor (init, test, inc);
7871 /*@notnull@*/ /*@only@*/ exprNode exprNode_goto (/*@only@*/ cstring label)
7873 exprNode ret = exprNode_createUnknown ();
7875 if (context_inMacro ())
7877 voptgenerror (FLG_MACROSTMT,
7878 message ("Macro %s uses goto (not functional)",
7879 context_inFunctionName ()),
7883 ret->kind = XPR_GOTO;
7884 ret->edata = exprData_makeLiteral (label);
7885 ret->mustBreak = TRUE;
7886 ret->exitCode = XK_GOTO;
7887 ret->canBreak = TRUE;
7891 exprNode exprNode_continue (/*@only@*/ lltok l, int qcontinue)
7893 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7895 ret->kind = XPR_CONTINUE;
7896 ret->edata = exprData_makeTok (l);
7897 ret->canBreak = TRUE;
7898 ret->mustBreak = TRUE;
7900 if (qcontinue == QSAFEBREAK)
7904 else if (qcontinue == QINNERCONTINUE)
7906 if (!context_inDeepLoop ())
7909 (FLG_LOOPLOOPCONTINUE,
7910 cstring_makeLiteral ("Continue statement marked with innercontinue "
7911 "is not inside a nested loop"),
7912 exprNode_loc (ret));
7915 else if (qcontinue == BADTOK)
7917 if (context_inDeepLoop ())
7920 (FLG_LOOPLOOPCONTINUE,
7921 cstring_makeLiteral ("Continue statement in nested loop"),
7922 exprNode_loc (ret));
7927 llbuglit ("exprNode_continue: bad qcontinue");
7933 exprNode exprNode_break (/*@only@*/ lltok l, int bqual)
7935 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7936 clause breakClause = context_breakClause ();
7938 ret->kind = XPR_BREAK;
7939 ret->edata = exprData_makeTok (l);
7940 ret->canBreak = TRUE;
7941 ret->mustBreak = TRUE;
7943 if (breakClause == NOCLAUSE)
7947 cstring_makeLiteral ("Break not inside while, for or switch statement"),
7948 exprNode_loc (ret));
7952 if (bqual != BADTOK)
7959 if (breakClause == SWITCHCLAUSE)
7961 if (!context_inDeepSwitch ())
7963 voptgenerror (FLG_SYNTAX,
7965 ("Break preceded by innerbreak is not in a deep switch"),
7966 exprNode_loc (ret));
7971 if (!context_inDeepLoop ())
7973 voptgenerror (FLG_SYNTAX,
7975 ("Break preceded by innerbreak is not in a deep loop"),
7976 exprNode_loc (ret));
7981 if (breakClause == SWITCHCLAUSE)
7983 voptgenerror (FLG_SYNTAX,
7985 ("Break preceded by loopbreak is breaking a switch"),
7986 exprNode_loc (ret));
7990 if (breakClause != SWITCHCLAUSE)
7994 message ("Break preceded by switchbreak is breaking %s",
7995 cstring_makeLiteralTemp
7996 ((breakClause == WHILECLAUSE
7997 || breakClause == DOWHILECLAUSE) ? "a while loop"
7998 : (breakClause == FORCLAUSE) ? "a for loop"
7999 : (breakClause == ITERCLAUSE) ? "an iterator"
8001 exprNode_loc (ret));
8009 if (breakClause == SWITCHCLAUSE)
8011 clause nextBreakClause = context_nextBreakClause ();
8013 switch (nextBreakClause)
8015 case NOCLAUSE: break;
8021 (FLG_LOOPSWITCHBREAK,
8022 cstring_makeLiteral ("Break statement in switch inside loop"),
8023 exprNode_loc (ret));
8027 (FLG_SWITCHSWITCHBREAK,
8028 cstring_makeLiteral ("Break statement in switch inside switch"),
8029 exprNode_loc (ret));
8036 if (context_inDeepLoop ())
8040 cstring_makeLiteral ("Break statement in nested loop"),
8041 exprNode_loc (ret));
8045 if (context_inDeepLoopSwitch ())
8048 (FLG_SWITCHLOOPBREAK,
8049 cstring_makeLiteral ("Break statement in loop inside switch"),
8050 exprNode_loc (ret));
8060 exprNode exprNode_nullReturn (/*@only@*/ lltok t)
8062 fileloc loc = lltok_getLoc (t);
8063 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
8065 context_returnFunction ();
8066 exprChecks_checkNullReturn (loc);
8068 ret->kind = XPR_NULLRETURN;
8069 ret->edata = exprData_makeTok (t);
8070 ret->exitCode = XK_MUSTRETURN;
8074 exprNode exprNode_return (/*@only@*/ exprNode e)
8078 if (exprNode_isError (e))
8080 ret = exprNode_createUnknown ();
8084 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (e->loc));
8086 exprNode_checkUse (ret, e->sref, e->loc);
8087 exprNode_checkReturn (e);
8090 context_returnFunction ();
8091 ret->kind = XPR_RETURN;
8092 ret->edata = exprData_makeSingle (e);
8093 ret->exitCode = XK_MUSTRETURN;
8098 exprNode exprNode_comma (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
8102 if (exprNode_isError (e1))
8104 if (exprNode_isError (e2))
8106 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
8110 ret = exprNode_createPartialCopy (e2);
8111 exprNode_checkUse (ret, e2->sref, e2->loc);
8112 ret->sref = e2->sref;
8117 ret = exprNode_createPartialCopy (e1);
8119 exprNode_checkUse (ret, e1->sref, e1->loc);
8121 if (!exprNode_isError (e2))
8123 exprNode_mergeUSs (ret, e2);
8124 exprNode_checkUse (ret, e2->sref, e2->loc);
8125 ret->sref = e2->sref;
8129 ret->kind = XPR_COMMA;
8130 ret->edata = exprData_makePair (e1, e2);
8132 if (exprNode_isDefined (e1))
8134 if (exprNode_isDefined (e2))
8138 if (exprNode_mustEscape (e1) || e1->mustBreak)
8142 message ("Second clause of comma expression is unreachable: %s",
8143 exprNode_unparse (e2)),
8147 ret->exitCode = exitkind_combine (e1->exitCode, e2->exitCode);
8148 ret->mustBreak = e1->mustBreak || e2->mustBreak;
8149 ret->canBreak = e1->canBreak || e2->canBreak;
8153 if (exprNode_mustEscape (e1) || e1->mustBreak)
8157 message ("Second clause of comma expression is unreachable: %s",
8158 exprNode_unparse (e2)),
8162 ret->exitCode = e1->exitCode;
8163 ret->canBreak = e1->canBreak;
8168 if (exprNode_isDefined (e2))
8170 ret->exitCode = e2->exitCode;
8171 ret->mustBreak = e2->mustBreak;
8172 ret->canBreak = e2->canBreak;
8179 static bool exprNode_checkOneInit (/*@notnull@*/ exprNode el, exprNode val)
8181 ctype t1 = exprNode_getType (el);
8182 ctype t2 = exprNode_getType (val);
8183 bool hasError = FALSE;
8185 DPRINTF (("Check one init: %s / %s",
8186 exprNode_unparse (el),
8187 exprNode_unparse (val)));
8189 if (ctype_isUnknown (t1))
8191 voptgenerror (FLG_IMPTYPE,
8192 message ("Variable has unknown (implicitly int) type: %s",
8193 exprNode_unparse (el)),
8197 el->typ = ctype_int;
8200 if (exprNode_isDefined (val) && val->kind == XPR_INITBLOCK)
8202 exprNodeList vals = exprData_getArgs (val->edata);
8204 DPRINTF (("Check one init: %s", exprNodeList_unparse (vals)));
8205 DPRINTF (("Type: %s", ctype_unparse (t1)));
8207 if (ctype_isRealAP (t1))
8212 if (ctype_isFixedArray (t1))
8214 int nelements = long_toInt (ctype_getArraySize (t1));
8216 DPRINTF (("Checked array: %s / %d",
8217 ctype_unparse (t1), nelements));
8219 if (exprNode_isStringLiteral (val))
8221 exprNode_checkStringLiteralLength (t1, val);
8225 if (exprNodeList_size (vals) != nelements)
8227 hasError = optgenerror
8228 (exprNodeList_size (vals) > nelements ? FLG_INITSIZE : FLG_INITALLELEMENTS,
8229 message ("Initializer block for "
8230 "%s has %d element%&, but declared as %s: %q",
8231 exprNode_unparse (el),
8232 exprNodeList_size (vals),
8234 exprNodeList_unparse (vals)),
8240 exprNodeList_elements (vals, oneval)
8242 cstring istring = message ("%d", i);
8245 (exprNode_fakeCopy (el),
8246 exprNode_numLiteral (ctype_int, istring,
8247 fileloc_copy (el->loc), i));
8249 if (exprNode_isDefined (newel))
8251 if (exprNodeList_size (vals) == 1
8252 && ctype_isString (exprNode_getType (oneval))
8253 && ctype_isChar (exprNode_getType (newel)))
8255 exprNode_freeIniter (newel);
8259 if (exprNode_checkOneInit (newel, oneval))
8264 if (nerrors > 3 && exprNodeList_size (vals) > 6)
8267 (message ("Additional initialization errors "
8268 "for %s not reported",
8269 exprNode_unparse (el)),
8271 exprNode_freeIniter (newel);
8276 exprNode_freeIniter (newel);
8281 exprNode_freeIniter (newel);
8286 cstring_free (istring);
8289 } end_exprNodeList_elements;
8292 else if (ctype_isStruct (ctype_realType (t1)))
8294 uentryList fields = ctype_getFields (t1);
8297 if (uentryList_size (fields) != exprNodeList_size (vals))
8299 if (uentryList_size (fields) > exprNodeList_size (vals))
8301 hasError = optgenerror
8303 message ("Initializer block for "
8304 "%s has %d field%&, but %s has %d field%&: %q",
8305 exprNode_unparse (el),
8306 exprNodeList_size (vals),
8308 uentryList_size (fields),
8309 exprNodeList_unparse (vals)),
8314 hasError = optgenerror
8316 message ("Initializer block for "
8317 "%s has %d field%&, but %s has %d field%&: %q",
8318 exprNode_unparse (el),
8319 exprNodeList_size (vals),
8321 uentryList_size (fields),
8322 exprNodeList_unparse (vals)),
8328 exprNodeList_elements (vals, oneval)
8330 uentry thisfield = uentryList_getN (fields, i);
8332 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8334 uentry_getName (thisfield));
8336 if (exprNode_isDefined (newel))
8338 if (exprNode_checkOneInit (newel, oneval))
8343 exprNode_freeIniter (newel);
8347 } end_exprNodeList_elements;
8350 /* evans 2001-12-30: added to fix bug reported by Jim Zelenka */
8351 else if (ctype_isUnion (ctype_realType (t1)))
8353 uentryList fields = ctype_getFields (t1);
8357 ** Union initializers set the first member always.
8360 DPRINTF (("Union initializer: %s / %s",
8361 exprNode_unparse (el), ctype_unparse (ctype_realType (t1))));
8363 if (exprNodeList_size (vals) != 1)
8365 hasError = optgenerror
8367 message ("Initializer block for union "
8368 "%s has %d elements, union initializers should have one element: %q",
8369 exprNode_unparse (el),
8370 exprNodeList_size (vals),
8371 exprNodeList_unparse (vals)),
8376 exprNode oneval = exprNodeList_head (vals);
8377 uentry thisfield = uentryList_getN (fields, i);
8379 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8381 uentry_getName (thisfield));
8383 if (exprNode_isDefined (newel))
8385 if (exprNode_checkOneInit (newel, oneval))
8390 exprNode_freeIniter (newel);
8396 hasError = optgenerror
8398 message ("Initializer block used for "
8399 "%s where %t is expected: %s",
8400 exprNode_unparse (el), t1, exprNode_unparse (val)),
8406 if (exprNode_isDefined (val))
8408 doAssign (el, val, TRUE);
8410 if (!exprNode_matchType (t1, val))
8412 hasError = gentypeerror
8414 message ("Initial value of %s is type %t, "
8416 exprNode_unparse (el),
8417 t2, t1, exprNode_unparse (val)),
8426 static /*@notnull@*/ exprNode
8427 exprNode_makeInitializationAux (/*@temp@*/ idDecl t)
8431 DPRINTF (("Initialization: %s", idDecl_unparse (t)));
8433 if (usymtab_exists (idDecl_observeId (t)))
8435 uentry ue = usymtab_lookup (idDecl_observeId (t));
8436 ret = exprNode_createId (ue);
8438 /*@i723 don't do this...but why? */
8440 ct = ctype_realishType (ret->typ);
8442 DPRINTF (("Type: %s", ctype_unparse (ret->typ)));
8444 if (ctype_isUnknown (ct))
8446 if (uentry_isAnyTag (ue))
8450 message ("%s used but not previously declared: %s",
8451 uentry_ekindName (ue),
8452 idDecl_getName (t)),
8460 message ("Variable has unknown (implicitly int) type: %s",
8461 idDecl_getName (t)),
8473 DPRINTF (("Unrecognized: %s", idDecl_unparse (t)));
8475 ue = uentry_makeUnrecognized (idDecl_observeId (t), fileloc_copy (g_currentloc));
8476 /*!! fileloc_copy (g_currentloc)); */
8477 /*@i32!!! should get error without this */
8479 ret = exprNode_fromIdentifierAux (ue);
8482 ** No error - this happens in old style declarations:
8486 message ("Unrecognized identifier in intializer: %s", idDecl_observeId (t)),
8493 exprData_free (ret->edata, ret->kind);
8494 ret->edata = exprData_undefined;
8496 ret->exitCode = XK_NEVERESCAPE;
8497 ret->mustBreak = FALSE;
8498 ret->kind = XPR_INIT;
8502 exprNode exprNode_makeEmptyInitialization (/*@only@*/ idDecl t)
8504 exprNode ret = exprNode_makeInitializationAux (t);
8505 llassert (ret->edata == exprData_undefined);
8506 ret->edata = exprData_makeInit (t, exprNode_undefined);
8510 exprNode exprNode_makeInitialization (/*@only@*/ idDecl t,
8511 /*@only@*/ exprNode e)
8513 uentry ue = usymtab_lookup (idDecl_observeId (t));
8514 exprNode ret = exprNode_makeInitializationAux (t);
8515 fileloc loc = exprNode_loc (e);
8517 if (exprNode_isError (e))
8519 e = exprNode_createUnknown ();
8522 /* error: assume initializer is defined */
8523 sRef_setDefined (ret->sref, g_currentloc);
8527 ctype ct = ctype_realishType (ret->typ);
8532 ** was addSafeUse --- what's the problem?
8534 ** int x = 3, y = x ?
8537 exprData_free (ret->edata, ret->kind);
8538 ret->edata = exprData_makeInit (t, e);
8540 exprNode_checkUse (ret, e->sref, e->loc);
8542 if (ctype_isUnknown (e->typ) && uentry_isValid (ue))
8544 exprNode lhs = exprNode_createId (ue);
8547 ** static storage should be undefined before initializing
8550 if (uentry_isStatic (ue))
8552 sRef_setDefState (lhs->sref, SS_PARTIAL, fileloc_undefined);
8555 (void) exprNode_checkOneInit (lhs, e);
8557 if (uentry_isStatic (ue))
8559 sRef_setDefState (lhs->sref, SS_DEFINED, fileloc_undefined);
8562 exprNode_free (lhs);
8566 if (!exprNode_matchType (ct, e))
8568 if (exprNode_isZero (e) && ctype_isArrayPtr (ct))
8575 (exprNode_getType (e), e, exprNode_getType (ret), ret,
8577 ("Variable %q initialized to type %t, expects %t: %s",
8578 uentry_getName (ue), exprNode_getType (e),
8579 exprNode_getType (ret),
8580 exprNode_unparse (e)),
8586 if (uentry_isStatic (ue))
8588 sRef_setDefState (ret->sref, SS_PARTIAL, fileloc_undefined);
8591 doAssign (ret, e, TRUE);
8593 if (uentry_isStatic (ue))
8595 sRef_setDefState (ret->sref, SS_DEFINED, fileloc_undefined);
8599 if (context_inIterDef ())
8601 /* should check if it is yield */
8602 uentry_setUsed (ue, loc);
8609 exprNode_mergeUSs (ret, e);
8613 exprNode exprNode_iter (/*@observer@*/ uentry name,
8614 /*@only@*/ exprNodeList alist,
8615 /*@only@*/ exprNode body,
8616 /*@observer@*/ uentry end)
8621 llassert (uentry_isValid (name));
8623 uentry_setUsed (name, exprNode_loc (body));
8625 ret = exprNode_createPartialCopy (body);
8626 iname = uentry_getName (name);
8628 if (uentry_isInvalid (end))
8630 llerror (FLG_ITERBALANCE,
8631 message ("Iter %s not balanced with end_%s", iname, iname));
8635 cstring ename = uentry_getName (end);
8637 if (!cstring_equalPrefixLit (ename, "end_"))
8639 llerror (FLG_ITERBALANCE, message ("Iter %s not balanced with end_%s: %s",
8640 iname, iname, ename));
8644 if (!cstring_equal (iname, cstring_suffix (ename, 4)))
8646 llerror (FLG_ITERBALANCE,
8647 message ("Iter %s not balanced with end_%s: %s",
8648 iname, iname, ename));
8652 cstring_free (ename);
8655 context_exitIterClause (body);
8657 ret->kind = XPR_ITER;
8658 ret->edata = exprData_makeIter (name, alist, body, end);
8660 if (uentry_isIter (name))
8662 (void) checkArgsReal (name, body,
8663 uentry_getParams (name), alist, TRUE, ret);
8666 cstring_free (iname);
8672 exprNode_iterNewId (/*@only@*/ cstring s)
8674 exprNode e = exprNode_new ();
8675 uentry ue = uentryList_getN (uentry_getParams (getCurrentIter ()), iterParamNo ());
8677 llassert (processingIterVars ());
8679 e->loc = context_getSaveLocation ();
8681 if (fileloc_isUndefined (e->loc))
8683 fileloc_free (e->loc);
8684 e->loc = fileloc_copy (g_currentloc);
8687 e->uses = sRefSet_new ();
8688 e->sets = sRefSet_new ();
8689 e->msets = sRefSet_new ();
8691 e->val = multiVal_unknown ();
8692 e->guards = guardSet_new ();
8694 e->isJumpPoint = FALSE;
8695 e->exitCode = XK_NEVERESCAPE;
8697 /*> missing fields, detected by splint <*/
8698 e->canBreak = FALSE;
8699 e->mustBreak = FALSE;
8700 e->etext = cstring_undefined;
8702 if (uentry_isYield (ue))
8704 uentry uue = uentry_makeVariable (s, uentry_getType (ue),
8705 fileloc_copy (e->loc),
8709 uue = usymtab_supEntrySrefReturn (uue);
8711 sr = uentry_getSref (uue);
8712 sRef_mergeStateQuiet (sr, uentry_getSref (ue));
8713 sr = uentry_getSref (uue);
8714 sRef_setDefined (sr, e->loc);
8716 e->typ = uentry_getType (uue);
8718 e->edata = exprData_makeId (uue);
8719 uentry_setUsed (uue, g_currentloc);
8725 sRef_setGlobalScope ();
8726 uue = uentry_makeVariableLoc (s, ctype_unknown);
8728 e->typ = ctype_unknown;
8729 e->edata = exprData_makeId (uue);
8731 uentry_setUsed (uue, e->loc);
8732 uentry_setHasNameError (uue);
8734 if (context_getFlag (FLG_REPEATUNRECOG))
8736 uentry_markOwned (uue);
8740 usymtab_supGlobalEntry (uue);
8743 sRef_clearGlobalScope ();
8745 voptgenerror (FLG_UNRECOG, message ("Unrecognized identifier: %s", s),
8752 exprNode_defineConstraints(e);
8757 exprNode_iterExpr (/*@returned@*/ exprNode e)
8759 if (!processingIterVars ())
8761 llcontbuglit ("checkIterParam: not in iter");
8765 if (uentry_isYield (uentryList_getN (uentry_getParams (getCurrentIter ()),
8768 if (exprNode_isDefined (e))
8770 if (fileloc_isDefined (e->loc))
8774 message ("Yield parameter is not simple identifier: %s",
8775 exprNode_unparse (e)),
8782 message ("Yield parameter is not simple identifier: %s",
8783 exprNode_unparse (e)),
8793 exprNode_iterId (/*@observer@*/ uentry c)
8797 llassert (processingIterVars ());
8799 ue = uentryList_getN (uentry_getParams (getCurrentIter ()),
8802 if (uentry_isYield (ue))
8804 ctype ct = uentry_getType (ue);
8805 exprNode e = exprNode_createPlain (ct);
8806 cstring name = uentry_getName (c);
8807 uentry le = uentry_makeVariable (name, ct, fileloc_undefined, FALSE);
8809 uentry_setUsed (ue, g_currentloc);
8810 uentry_setHasNameError (ue);
8812 cstring_free (name);
8815 e->edata = exprData_makeId (le);
8816 e->loc = context_getSaveLocation ();
8817 e->sref = uentry_getSref (le);
8819 usymtab_supEntrySref (le);
8821 if (!context_inHeader ())
8825 message ("Yield parameter shadows local declaration: %q",
8826 uentry_getName (c)),
8827 fileloc_isDefined (e->loc) ? e->loc : g_currentloc))
8829 uentry_showWhereDeclared (c);
8836 return (exprNode_fromIdentifierAux (c));
8839 exprNode exprNode_iterStart (/*@observer@*/ uentry name, /*@only@*/ exprNodeList alist)
8841 exprNode ret = exprNode_create (ctype_unknown);
8843 ret->kind = XPR_ITERCALL;
8844 ret->edata = exprData_makeIterCall (name, alist);
8846 if (uentry_isIter (name))
8848 uentryList params = uentry_getParams (name);
8850 if (context_inIterDef ()
8851 && uentryList_size (params) == exprNodeList_size (alist))
8855 exprNodeList_elements (alist, arg)
8857 uentry parg = uentryList_getN (params, i);
8859 if (uentry_isYield (parg))
8861 uentry ue = exprNode_getUentry (arg);
8863 if (uentry_isValid (ue))
8870 } end_exprNodeList_elements;
8873 (void) checkArgsReal (name, ret, params, alist, TRUE, ret);
8874 checkUnspecCall (ret, params, alist);
8880 /*@exposed@*/ sRef exprNode_getSref (exprNode e)
8882 if (exprNode_isDefined (e))
8885 if (e->sref == defref) /*@noaccess sRef@*/
8888 e->sref = sRef_makeUnknown ();
8889 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
8900 return sRef_undefined;
8904 /*@observer@*/ cstring
8905 exprNode_unparseFirst (exprNode e)
8907 if (exprNode_isDefined (e))
8911 if (e->kind == XPR_STMTLIST
8912 || e->kind == XPR_COMMA || e->kind == XPR_COND)
8914 exprNode first = exprData_getPairA (e->edata);
8916 if (exprNode_isDefined (first))
8918 return (exprNode_unparseFirst (exprData_getPairA (e->edata)));
8922 return (cstring_makeLiteralTemp ("..."));
8926 ret = cstring_elide (exprNode_unparse (e), 20);
8927 cstring_markOwned (ret);
8933 return cstring_makeLiteralTemp ("<error>");
8937 /*@observer@*/ cstring
8938 exprNode_unparse (/*@temp@*/ exprNode e)
8940 if (exprNode_isError (e))
8942 return cstring_makeLiteralTemp ("<error>");
8945 if (cstring_isDefined (e->etext))
8951 cstring ret = exprNode_doUnparse (e);
8953 /*@-modifies@*/ /* benevolent */
8960 /*@observer@*/ fileloc
8961 exprNode_loc (exprNode e)
8963 if (exprNode_isError (e))
8965 return (g_currentloc);
8974 ** executes exprNode e
8975 ** recursively rexecutes as though in original parse using
8976 ** information in e->edata
8979 static /*@only@*/ exprNodeList exprNodeList_effect (exprNodeList e)
8981 exprNodeList ret = exprNodeList_new ();
8983 exprNodeList_elements (e, current)
8985 exprNodeList_addh (ret, exprNode_effect (current));
8986 } end_exprNodeList_elements;
8991 static /*@only@*/ exprNode exprNode_effect (exprNode e)
8992 /*@globals internalState@*/
8994 bool innerEffect = inEffect;
9000 context_clearJustPopped ();
9002 if (exprNode_isError (e))
9004 ret = exprNode_undefined;
9009 ** Turn off expose and dependent transfer checking.
9010 ** Need to pass exposed internal nodes,
9011 ** [ copying would be a waste! ]
9012 ** [ Actually, I think I wasted a lot more time than its worth ]
9013 ** [ trying to do this. ]
9017 /*@-observertrans@*/
9018 /*@-dependenttrans@*/
9025 ret = exprNode_addParens (exprData_getUopTok (data),
9026 exprNode_effect (exprData_getUopNode (data)));
9029 ret = exprNode_assign (exprNode_effect (exprData_getOpA (data)),
9030 exprNode_effect (exprData_getOpB (data)),
9031 exprData_getOpTok (data));
9034 ret = exprNode_undefined;
9037 ret = exprNode_functionCall (exprNode_effect (exprData_getFcn (data)),
9038 exprNodeList_effect (exprData_getArgs (data)));
9051 cstring id = exprData_getId (data);
9052 uentry ue = usymtab_lookupSafe (id);
9054 ret = exprNode_fromIdentifierAux (ue);
9055 ret->loc = fileloc_update (ret->loc, e->loc);
9062 ret = exprNode_arrayFetch (exprNode_effect (exprData_getPairA (data)),
9063 exprNode_effect (exprData_getPairB (data)));
9066 ret = exprNode_op (exprNode_effect (exprData_getOpA (data)),
9067 exprNode_effect (exprData_getOpB (data)),
9068 exprData_getOpTok (data));
9072 ret = exprNode_postOp (exprNode_effect (exprData_getUopNode (data)),
9073 exprData_getUopTok (data));
9076 ret = exprNode_preOp (exprNode_effect (exprData_getUopNode (data)),
9077 exprData_getUopTok (data));
9089 ret = exprNode_vaArg (exprData_getCastTok (data),
9090 exprNode_effect (exprData_getCastNode (data)),
9091 exprData_getCastType (data));
9095 ret = exprNode_cast (exprData_getCastTok (data),
9096 exprNode_effect (exprData_getCastNode (data)),
9097 exprData_getCastType (data));
9100 ret = exprNode_iterStart (exprData_getIterCallIter (data),
9102 (exprData_getIterCallArgs (data)));
9106 ret = exprNode_iter (exprData_getIterSname (data),
9107 exprNodeList_effect (exprData_getIterAlist (data)),
9108 exprNode_effect (exprData_getIterBody (data)),
9109 exprData_getIterEname (data));
9113 ret = exprNode_for (exprNode_effect (exprData_getPairA (data)),
9114 exprNode_effect (exprData_getPairB (data)));
9118 ret = exprNode_forPred (exprNode_effect (exprData_getTripleInit (data)),
9119 exprNode_effect (exprData_getTripleTest (data)),
9120 exprNode_effect (exprData_getTripleInc (data)));
9124 ret = exprNode_createTok (exprData_getTok (data));
9128 ret = exprNode_goto (exprData_getLiteral (data));
9129 ret->loc = fileloc_update (ret->loc, e->loc);
9133 ret = exprNode_continue (exprData_getTok (data), QSAFEBREAK);
9137 ret = exprNode_break (exprData_getTok (data), QSAFEBREAK);
9141 ret = exprNode_return (exprNode_effect (exprData_getSingle (data)));
9144 case XPR_NULLRETURN:
9145 ret = exprNode_nullReturn (exprData_getTok (data));
9149 ret = exprNode_comma (exprNode_effect (exprData_getPairA (data)),
9150 exprNode_effect (exprData_getPairB (data)));
9154 ret = exprNode_cond (exprNode_effect (exprData_getTriplePred (data)),
9155 exprNode_effect (exprData_getTripleTrue (data)),
9156 exprNode_effect (exprData_getTripleFalse (data)));
9159 ret = exprNode_if (exprNode_effect (exprData_getPairA (data)),
9160 exprNode_effect (exprData_getPairB (data)));
9164 ret = exprNode_ifelse (exprNode_effect (exprData_getTriplePred (data)),
9165 exprNode_effect (exprData_getTripleTrue (data)),
9166 exprNode_effect (exprData_getTripleFalse (data)));
9169 ret = exprNode_whilePred (exprData_getSingle (data));
9173 ret = exprNode_while (exprNode_effect (exprData_getPairA (data)),
9174 exprNode_effect (exprData_getPairB (data)));
9178 ret = exprNode_doWhile (exprNode_effect (exprData_getPairA (data)),
9179 exprNode_effect (exprData_getPairB (data)));
9183 ret = exprNode_makeBlock (exprNode_effect (exprData_getSingle (data)));
9187 ret = exprNode_statement (exprNode_effect (exprData_getUopNode (data)),
9188 exprData_getUopTok (data));
9192 ret = exprNode_concat (exprNode_effect (exprData_getPairA (data)),
9193 exprNode_effect (exprData_getPairB (data)));
9198 ret = exprNode_caseMarker
9199 (exprNode_effect (exprData_getSingle (data)),
9205 ret = exprNode_createTok (exprData_getTok (data));
9209 ret = exprNode_switch (exprNode_effect (exprData_getPairA (data)),
9210 exprNode_effect (exprData_getPairB (data)));
9214 ret = exprNode_makeInitialization
9215 (exprData_getInitId (data),
9216 exprNode_effect (exprData_getInitNode (data)));
9220 ret = exprNode_fieldAccessAux
9221 (exprNode_effect (exprData_getFieldNode (data)),
9222 exprNode_loc (exprData_getFieldNode (data)),
9223 cstring_copy (exprData_getFieldName (data)));
9227 ret = exprNode_arrowAccessAux
9228 (exprNode_effect (exprData_getFieldNode (data)),
9229 exprNode_loc (exprData_getFieldNode (data)),
9230 cstring_copy (exprData_getFieldName (data)));
9233 case XPR_STRINGLITERAL:
9247 /*@=observertrans@*/
9249 /*@=dependenttrans@*/
9260 static /*@observer@*/ cstring exprNode_rootVarName (exprNode e)
9265 if (exprNode_isError (e))
9267 return cstring_undefined;
9275 ret = exprNode_rootVarName (exprData_getUopNode (data));
9278 ret = exprNode_rootVarName (exprData_getOpA (data));
9282 ret = exprData_getId (data);
9285 ret = idDecl_getName (exprData_getInitId (data));
9310 case XPR_NULLRETURN:
9332 case XPR_STRINGLITERAL:
9333 ret = cstring_undefined;
9340 static /*@only@*/ cstring exprNode_doUnparse (exprNode e)
9345 if (exprNode_isError (e))
9347 static /*@only@*/ cstring error = cstring_undefined;
9349 if (!cstring_isDefined (error))
9351 error = cstring_makeLiteral ("<error>");
9362 ret = message ("(%s)", exprNode_unparse (exprData_getUopNode (e->edata)));
9365 ret = message ("%s %s %s",
9366 exprNode_unparse (exprData_getOpA (data)),
9367 lltok_unparse (exprData_getOpTok (data)),
9368 exprNode_unparse (exprData_getOpB (data)));
9371 ret = message ("%s(%q)",
9372 exprNode_unparse (exprData_getFcn (data)),
9373 exprNodeList_unparse (exprData_getArgs (data)));
9376 ret = message ("{ %q }", exprNodeList_unparse (exprData_getArgs (data)));
9379 ret = cstring_undefined;
9382 ret = message ("%s:", exprData_getId (data));
9386 ret = cstring_copy (exprData_getId (data));
9389 ret = message ("%s[%s]", exprNode_unparse (exprData_getPairA (data)),
9390 exprNode_unparse (exprData_getPairB (data)));
9393 ret = message ("<body>");
9396 ret = message ("%s %s %s",
9397 exprNode_unparse (exprData_getOpA (data)),
9398 lltok_unparse (exprData_getOpTok (data)),
9399 exprNode_unparse (exprData_getOpB (data)));
9403 ret = message ("%s%s",
9404 lltok_unparse (exprData_getUopTok (data)),
9405 exprNode_unparse (exprData_getUopNode (data)));
9409 ret = message ("%s%s",
9410 exprNode_unparse (exprData_getUopNode (data)),
9411 lltok_unparse (exprData_getUopTok (data)));
9415 ret = message ("offsetof(%s,%q)",
9416 ctype_unparse (qtype_getType (exprData_getOffsetType (data))),
9417 cstringList_unparseSep (exprData_getOffsetName (data), cstring_makeLiteralTemp (".")));
9421 ret = message ("sizeof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9425 ret = message ("sizeof(%s)", exprNode_unparse (exprData_getSingle (data)));
9429 ret = message ("alignof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9433 ret = message ("alignof(%s)", exprNode_unparse (exprData_getSingle (data)));
9437 ret = message ("va_arg(%s, %q)",
9438 exprNode_unparse (exprData_getCastNode (data)),
9439 qtype_unparse (exprData_getCastType (data)));
9443 ret = message ("%q(%q)",
9444 uentry_getName (exprData_getIterCallIter (data)),
9445 exprNodeList_unparse (exprData_getIterCallArgs (data)));
9448 ret = message ("%q(%q) %s %q",
9449 uentry_getName (exprData_getIterSname (data)),
9450 exprNodeList_unparse (exprData_getIterAlist (data)),
9451 exprNode_unparse (exprData_getIterBody (data)),
9452 uentry_getName (exprData_getIterEname (data)));
9455 ret = message ("(%q)%s",
9456 qtype_unparse (exprData_getCastType (data)),
9457 exprNode_unparse (exprData_getCastNode (data)));
9461 ret = message ("%s %s",
9462 exprNode_unparse (exprData_getPairA (data)),
9463 exprNode_unparse (exprData_getPairB (data)));
9467 ret = message ("for (%s; %s; %s)",
9468 exprNode_unparse (exprData_getTripleInit (data)),
9469 exprNode_unparse (exprData_getTripleTest (data)),
9470 exprNode_unparse (exprData_getTripleInc (data)));
9474 ret = message ("goto %s", exprData_getLiteral (data));
9478 ret = cstring_makeLiteral ("continue");
9482 ret = cstring_makeLiteral ("break");
9486 ret = message ("return %s", exprNode_unparse (exprData_getSingle (data)));
9489 case XPR_NULLRETURN:
9490 ret = cstring_makeLiteral ("return");
9494 ret = message ("%s, %s",
9495 exprNode_unparse (exprData_getPairA (data)),
9496 exprNode_unparse (exprData_getPairB (data)));
9500 ret = message ("%s ? %s : %s",
9501 exprNode_unparse (exprData_getTriplePred (data)),
9502 exprNode_unparse (exprData_getTripleTrue (data)),
9503 exprNode_unparse (exprData_getTripleFalse (data)));
9506 ret = message ("if (%s) %s",
9507 exprNode_unparse (exprData_getPairA (data)),
9508 exprNode_unparse (exprData_getPairB (data)));
9512 ret = message ("if (%s) %s else %s",
9513 exprNode_unparse (exprData_getTriplePred (data)),
9514 exprNode_unparse (exprData_getTripleTrue (data)),
9515 exprNode_unparse (exprData_getTripleFalse (data)));
9518 ret = message ("while (%s) %s",
9519 exprNode_unparse (exprData_getPairA (data)),
9520 exprNode_unparse (exprData_getPairB (data)));
9524 ret = cstring_copy (exprNode_unparse (exprData_getSingle (data)));
9528 ret = cstring_copy (lltok_unparse (exprData_getTok (data)));
9532 ret = message ("do { %s } while (%s)",
9533 exprNode_unparse (exprData_getPairB (data)),
9534 exprNode_unparse (exprData_getPairA (data)));
9538 ret = message ("{ %s }", exprNode_unparse (exprData_getSingle (data)));
9539 /* evans 2002-02-20 was unparseFirst! */
9543 ret = message ("%s;", exprNode_unparse (exprData_getUopNode (data)));
9547 if (exprNode_isStatement (exprData_getPairA (data)))
9550 ** statement expressions already print the ;
9553 ret = message ("%s %s",
9554 exprNode_unparse (exprData_getPairA (data)),
9555 exprNode_unparse (exprData_getPairB (data)));
9559 ret = message ("%s; %s",
9560 exprNode_unparse (exprData_getPairA (data)),
9561 exprNode_unparse (exprData_getPairB (data)));
9567 ret = cstring_makeLiteral ("default:");
9571 ret = message ("switch (%s) %s",
9572 exprNode_unparse (exprData_getPairA (data)),
9573 exprNode_unparse (exprData_getPairB (data)));
9578 ret = message ("case %s:",
9579 exprNode_unparse (exprData_getSingle (data)));
9583 if (exprNode_isError (exprData_getInitNode (data)))
9585 ret = message ("%q", idDecl_unparseC (exprData_getInitId (data)));
9589 ret = message ("%q = %s",
9590 idDecl_unparseC (exprData_getInitId (data)),
9591 exprNode_unparse (exprData_getInitNode (data)));
9596 ret = message ("%s.%s",
9597 exprNode_unparse (exprData_getFieldNode (data)),
9598 exprData_getFieldName (data));
9602 ret = message ("%s->%s",
9603 exprNode_unparse (exprData_getFieldNode (data)),
9604 exprData_getFieldName (data));
9607 case XPR_STRINGLITERAL:
9608 if (ctype_isWideString (e->typ))
9610 ret = message ("L\"%s\"", exprData_getLiteral (data));
9614 ret = message ("\"%s\"", exprData_getLiteral (data));
9619 ret = cstring_copy (exprData_getLiteral (data));
9623 ret = cstring_makeLiteral ("<node>");
9631 exprNode_isInitializer (exprNode e)
9633 return (exprNode_isDefined (e)
9634 && e->kind == XPR_INIT);
9638 exprNode_isCharLit (exprNode e)
9640 if (exprNode_isDefined (e))
9642 return (multiVal_isChar (exprNode_getValue (e)));
9651 exprNode_isNumLit (exprNode e)
9653 if (exprNode_isDefined (e))
9655 return (multiVal_isInt (exprNode_getValue (e)));
9664 exprNode_isFalseConstant (exprNode e)
9666 if (exprNode_isDefined (e))
9668 cstring s = exprNode_rootVarName (e);
9670 if (cstring_equal (s, context_getFalseName ()))
9680 exprNode_matchLiteral (ctype expected, exprNode e)
9682 if (exprNode_isDefined (e))
9684 multiVal m = exprNode_getValue (e);
9686 if (multiVal_isDefined (m))
9688 if (multiVal_isInt (m))
9690 long int val = multiVal_forceInt (m);
9692 if (ctype_isDirectBool (ctype_realishType (expected)))
9696 return FALSE; /* really?! return TRUE; allow use of 0 for FALSE */
9704 if (ctype_isRealInt (expected))
9707 ** unsigned <- [ constant >= 0 is okay ]
9710 if (ctype_isUnsigned (expected))
9719 ** No checks on sizes of integers...maybe add
9723 DPRINTF (("Here: %s => %s", exprNode_unparse (e), ctype_unparse (expected)));
9724 DPRINTF (("Type: %s / %s", ctype_unparse (exprNode_getType (e)),
9725 bool_unparse (ctype_isInt (exprNode_getType (e)))));
9727 if (context_getFlag (FLG_NUMLITERAL)
9728 && (ctype_isRegularInt (exprNode_getType (e)) || val == 0)) {
9734 return FALSE; /* evs 2000-05-17: previously, always returned TRUE */
9738 else if (ctype_isChar (expected))
9742 else if (ctype_isArrayPtr (expected))
9745 ** evans 2001-10-14: We allow 0 to match any pointer, but only if the type matches or is void *.
9750 if (ctype_match (exprNode_getType (e), expected)
9751 || ctype_isVoidPointer (exprNode_getType (e)))
9761 else if (ctype_isAnyFloat (expected))
9763 return (context_getFlag (FLG_NUMLITERAL));
9770 else if (multiVal_isDouble (m))
9772 if (ctype_isAnyFloat (expected))
9777 else if (multiVal_isChar (m))
9779 char val = multiVal_forceChar (m);
9781 if (ctype_isChar (expected))
9783 if (ctype_isUnsigned (expected) && ((int)val) < 0)
9804 exprNode_matchType (ctype expected, exprNode e)
9808 if (!exprNode_isDefined (e)) return TRUE;
9810 actual = ctype_realishType (exprNode_getType (e));
9812 if (ctype_match (ctype_realishType (expected), actual))
9817 llassert (!exprNode_isError (e));
9818 return (exprNode_matchLiteral (expected, e));
9822 exprNode_matchTypes (exprNode e1, exprNode e2)
9827 if (!exprNode_isDefined (e1)) return TRUE;
9828 if (!exprNode_isDefined (e2)) return TRUE;
9831 ** realish type --- keep bools, bools
9834 t1 = ctype_realishType (exprNode_getType (e1));
9835 t2 = ctype_realishType (exprNode_getType (e2));
9837 if (ctype_match (t1, t2))
9842 DPRINTF (("Matching literal! %s %s %s %s",
9843 ctype_unparse (t1), exprNode_unparse (e2),
9844 ctype_unparse (t2), exprNode_unparse (e1)));
9846 return (exprNode_matchLiteral (t1, e2) || exprNode_matchLiteral (t2, e1));
9854 exprNode_matchArgType (ctype ct, exprNode e)
9858 if (!exprNode_isDefined (e))
9863 et = ctype_realType (exprNode_getType (e));
9865 if (ctype_matchArg (ct, et)) return TRUE;
9867 llassert (!exprNode_isError (e));
9868 return (exprNode_matchLiteral (ct, e));
9871 static /*@only@*/ exprNodeSList
9872 exprNode_flatten (/*@dependent@*/ exprNode e) /*@*/
9874 if (exprNode_isDefined (e))
9876 if (e->kind == XPR_STMTLIST)
9878 return (exprNodeSList_append
9879 (exprNode_flatten (exprData_getPairA (e->edata)),
9880 exprNode_flatten (exprData_getPairB (e->edata))));
9882 else if (e->kind == XPR_BLOCK)
9884 return (exprNode_flatten (exprData_getSingle (e->edata)));
9888 return (exprNodeSList_singleton (e));
9892 return exprNodeSList_new ();
9895 static /*@exposed@*/ exprNode
9896 exprNode_lastStatement (/*@returned@*/ exprNode e)
9898 if (exprNode_isDefined (e))
9900 if (e->kind == XPR_STMTLIST)
9902 exprNode b = exprData_getPairB (e->edata);
9904 if (exprNode_isDefined (b))
9906 return exprNode_lastStatement (b);
9910 return exprNode_lastStatement (exprData_getPairA (e->edata));
9913 else if (e->kind == XPR_BLOCK)
9915 return (exprNode_lastStatement (exprData_getSingle (e->edata)));
9923 return exprNode_undefined;
9926 static /*@exposed@*/ exprNode
9927 exprNode_firstStatement (/*@returned@*/ exprNode e)
9929 if (exprNode_isDefined (e))
9931 if (e->kind == XPR_STMTLIST)
9933 exprNode b = exprData_getPairA (e->edata);
9935 if (exprNode_isDefined (b))
9937 return exprNode_firstStatement (b);
9941 return exprNode_firstStatement (exprData_getPairB (e->edata));
9944 else if (e->kind == XPR_BLOCK)
9946 return (exprNode_firstStatement (exprData_getSingle (e->edata)));
9954 return exprNode_undefined;
9958 exprNode_mergeUSs (exprNode res, exprNode other)
9960 if (exprNode_isDefined (res) && exprNode_isDefined (other))
9962 res->msets = sRefSet_union (res->msets, other->msets);
9963 res->sets = sRefSet_union (res->sets, other->sets);
9964 res->uses = sRefSet_union (res->uses, other->uses);
9969 exprNode_mergeCondUSs (exprNode res, exprNode other1, exprNode other2)
9971 if (exprNode_isDefined (res))
9973 if (exprNode_isDefined (other1))
9975 res->sets = sRefSet_union (res->sets, other1->sets);
9976 res->msets = sRefSet_union (res->msets, other1->msets);
9977 res->uses = sRefSet_union (res->uses, other1->uses);
9979 if (exprNode_isDefined (other2))
9981 res->sets = sRefSet_union (res->sets, other2->sets);
9982 res->msets = sRefSet_union (res->msets, other2->msets);
9983 res->uses = sRefSet_union (res->uses, other2->uses);
9991 ** Reports errors is s is not defined.
9995 exprNode_addUse (exprNode e, /*@exposed@*/ sRef s)
9997 if (exprNode_isDefined (e))
9999 e->uses = sRefSet_insert (e->uses, s);
10004 exprNode_checkUse (exprNode e, /*@exposed@*/ sRef s, fileloc loc)
10006 if (sRef_isKnown (s) && !sRef_isConst (s))
10009 ** need to check all outer types are useable
10012 DPRINTF (("Check use: %s / %s",
10013 exprNode_unparse (e), sRef_unparse (s)));
10015 exprNode_addUse (e, s);
10017 if (!context_inProtectVars ())
10020 ** only report the deepest error
10023 sRef errorRef = sRef_undefined;
10024 sRef lastRef = sRef_undefined;
10025 bool deadRef = FALSE;
10026 bool unuseable = FALSE;
10027 bool errorMaybe = FALSE;
10029 while (sRef_isValid (s) && sRef_isKnown (s))
10031 ynm readable = sRef_isValidLvalue (s);
10033 DPRINTF (("Readable: %s / %s",
10034 sRef_unparseFull (s), ynm_unparse (readable)));
10036 if (!(ynm_toBoolStrict (readable)))
10038 if (ynm_isMaybe (readable))
10040 lastRef = errorRef;
10042 DPRINTF (("Setting ERROR: %s", sRef_unparseFull (s)));
10043 deadRef = sRef_isPossiblyDead (errorRef);
10044 unuseable = sRef_isUnuseable (errorRef);
10049 lastRef = errorRef;
10051 deadRef = sRef_isDead (errorRef);
10052 unuseable = sRef_isUnuseable (errorRef);
10053 errorMaybe = FALSE;
10056 if (!sRef_isPartial (s))
10058 DPRINTF (("Defining! %s", sRef_unparseFull (s)));
10059 sRef_setDefined (s, fileloc_undefined);
10063 s = sRef_getBaseSafe (s);
10066 if (sRef_isValid (errorRef))
10068 if (sRef_isValid (lastRef) && sRef_isField (lastRef)
10069 && sRef_isPointer (errorRef))
10071 errorRef = lastRef;
10076 if (sRef_isThroughArrayFetch (errorRef))
10079 (FLG_STRICTUSERELEASED,
10080 message ("%q %q may be used after being released",
10081 sRef_unparseKindNamePlain (errorRef),
10082 sRef_unparse (errorRef)),
10085 sRef_showRefKilled (errorRef);
10087 if (sRef_isKept (errorRef))
10089 sRef_clearAliasState (errorRef, loc);
10095 DPRINTF (("HERE: %s", sRef_unparse (errorRef)));
10099 message ("%q %q %qused after being released",
10100 sRef_unparseKindNamePlain (errorRef),
10101 sRef_unparse (errorRef),
10102 cstring_makeLiteral (errorMaybe
10103 ? "may be " : "")),
10106 sRef_showRefKilled (errorRef);
10108 if (sRef_isKept (errorRef))
10110 sRef_clearAliasState (errorRef, loc);
10115 else if (unuseable)
10119 message ("%q %q%qused in inconsistent state",
10120 sRef_unparseKindName (errorRef),
10121 sRef_unparseOpt (errorRef),
10122 cstring_makeLiteral (errorMaybe ? "may be " : "")),
10125 sRef_showStateInconsistent (errorRef);
10130 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
10134 message ("%q %q%qused before definition",
10135 sRef_unparseKindName (errorRef),
10136 sRef_unparseOpt (errorRef),
10137 cstring_makeLiteral (errorMaybe ? "may be " : "")),
10140 DPRINTF (("Error: %s", sRef_unparseFull (errorRef)));
10143 sRef_setDefined (errorRef, loc);
10145 if (sRef_isAddress (errorRef))
10147 sRef_setDefined (sRef_getRootBase (errorRef), loc);
10149 } /* end is error */
10157 checkSafeUse (exprNode e, /*@exposed@*/ sRef s)
10159 if (exprNode_isDefined (e) && sRef_isKnown (s))
10161 e->uses = sRefSet_insert (e->uses, s);
10166 exprNode_checkSetAny (exprNode e, /*@dependent@*/ cstring name)
10168 if (exprNode_isDefined (e))
10170 e->sets = sRefSet_insert (e->sets, sRef_makeUnconstrained (name));
10175 exprNode_checkSet (exprNode e, /*@exposed@*/ sRef s)
10177 sRef defines = sRef_undefined;
10179 if (sRef_isValid (s) && !sRef_isNothing (s))
10181 uentry ue = sRef_getBaseUentry (s);
10183 if (uentry_isValid (ue))
10185 uentry_setLset (ue);
10188 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
10190 voptgenerror (FLG_USEDEF,
10191 message ("Attempt to set unuseable storage: %q",
10196 if (sRef_isMeaningful (s))
10198 if (sRef_isDead (s))
10200 sRef base = sRef_getBaseSafe (s);
10202 if (sRef_isValid (base)
10203 && sRef_isDead (base))
10205 sRef_setPartial (s, exprNode_loc (e));
10208 defines = s; /* okay - modifies for only param */
10210 else if (sRef_isPartial (s))
10212 sRef eref = exprNode_getSref (e);
10214 if (!sRef_isPartial (eref))
10217 ** should do something different here???
10220 sRef_setDefinedComplete (eref, exprNode_loc (e));
10224 sRef_setPartialDefinedComplete (eref, exprNode_loc (e));
10227 if (sRef_isMeaningful (eref))
10236 else if (sRef_isAllocated (s))
10238 sRef eref = exprNode_getSref (e);
10241 if (!sRef_isAllocated (eref))
10243 sRef_setDefinedComplete (eref, exprNode_loc (e));
10247 sRef base = sRef_getBaseSafe (eref);
10249 if (sRef_isValid (base))
10251 sRef_setPdefined (base, exprNode_loc (e));
10259 sRef_setDefinedNCComplete (s, exprNode_loc (e));
10264 else /* not meaningful...but still need to insert it */
10270 if (exprNode_isDefined (e) && sRef_isValid (defines))
10272 e->sets = sRefSet_insert (e->sets, defines);
10277 exprNode_checkMSet (exprNode e, /*@exposed@*/ sRef s)
10279 if (sRef_isValid (s) && !sRef_isNothing (s))
10281 uentry ue = sRef_getBaseUentry (s);
10283 if (uentry_isValid (ue))
10285 uentry_setLset (ue);
10288 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
10290 voptgenerror (FLG_USEDEF,
10291 message ("Attempt to set unuseable storage: %q", sRef_unparse (s)),
10295 if (sRef_isMeaningful (s))
10297 sRef_setDefinedComplete (s, exprNode_loc (e));
10300 if (exprNode_isDefined (e))
10302 e->msets = sRefSet_insert (e->msets, s);
10308 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode fcn, uentryList params, exprNodeList args)
10310 checkAnyCall (fcn, cstring_undefined, params, args,
10311 FALSE, sRefSet_undefined, FALSE, 0);
10315 checkOneArg (uentry ucurrent, /*@notnull@*/ exprNode current,
10316 /*@dependent@*/ exprNode fcn, bool isSpec, int argno, int totargs)
10320 if (uentry_isYield (ucurrent))
10322 sRef_setDefined (exprNode_getSref (current), exprNode_loc (current));
10323 exprNode_checkSet (current, current->sref);
10327 if (uentry_isSefParam (ucurrent))
10329 sRefSet sets = current->sets;
10330 sRef ref = exprNode_getSref (current);
10332 if (sRef_isMacroParamRef (ref))
10334 uentry ue = sRef_getUentry (ref);
10336 if (!uentry_isSefParam (ue))
10341 ("Parameter %d to %s is declared sef, but "
10342 "the argument is a macro parameter declared "
10344 argno, exprNode_unparse (fcn),
10345 exprNode_unparse (current)),
10346 exprNode_loc (current));
10350 if (!sRefSet_isEmpty (sets))
10352 sRefSet reported = sRefSet_undefined;
10354 sRefSet_realElements (current->sets, el)
10356 if (sRefSet_isSameNameMember (reported, el))
10358 ; /* don't report again */
10362 if (sRef_isUnconstrained (el))
10367 ("Parameter %d to %s is declared sef, but "
10368 "the argument calls unconstrained function %s "
10369 "(no guarantee it will not modify something): %s",
10370 argno, exprNode_unparse (fcn),
10371 sRef_unconstrainedName (el),
10372 exprNode_unparse (current)),
10373 exprNode_loc (current));
10380 ("Parameter %d to %s is declared sef, but "
10381 "the argument may modify %q: %s",
10382 argno, exprNode_unparse (fcn),
10384 exprNode_unparse (current)),
10385 exprNode_loc (current));
10388 } end_sRefSet_realElements;
10392 transferChecks_passParam (current, ucurrent, isSpec, fcn, argno, totargs);
10393 exprNode_mergeUSs (fcn, current);
10398 checkAnyCall (/*@dependent@*/ exprNode fcn,
10399 /*@dependent@*/ cstring fname,
10402 bool hasMods, sRefSet mods,
10407 int nargs = exprNodeList_size (args);
10412 ** concat all args ud's to f, add each arg sref as a use unless
10413 ** it was specified as "out", in which case it is a def.
10416 uentryList_reset (pn);
10419 ** aliasing checks:
10421 ** if paramn is only or unique, no other arg may alias argn
10424 exprNodeList_elements (args, current)
10428 if (exprNode_isDefined (current))
10430 if ((!uentryList_isUndefined (pn) && !uentryList_isFinished (pn)))
10432 uentry ucurrent = uentryList_current (pn);
10434 if (specialArgs == 0
10435 || (paramno < specialArgs))
10437 checkOneArg (ucurrent, current, fcn, isSpec, paramno, nargs);
10439 if (context_maybeSet (FLG_ALIASUNIQUE))
10441 if (uentry_isOnly (ucurrent)
10442 || uentry_isUnique (ucurrent))
10444 checkUniqueParams (fcn, current, args,
10445 paramno, ucurrent);
10450 else /* uentry is undefined */
10452 if (specialArgs == 0)
10454 exprNode_checkUseParam (current);
10457 exprNode_mergeUSs (fcn, current);
10460 uentryList_advanceSafe (pn);
10461 } end_exprNodeList_elements;
10467 sRefSet_allElements (mods, s)
10470 sRef rb = sRef_getRootBase (s);
10472 if (sRef_isFileOrGlobalScope (rb))
10474 context_usedGlobal (rb);
10477 fb = sRef_fixBaseParam (s, args);
10479 if (!sRef_isMacroParamRef (fb))
10481 if (sRef_isNothing (fb))
10487 if (sRef_isValid (fb))
10489 uentry ue = sRef_getBaseUentry (s);
10491 if (uentry_isValid (ue))
10493 uentry_setLset (ue);
10497 fcn->sets = sRefSet_insert (fcn->sets, fb);
10500 sRef_clearDerivedComplete (s);
10501 } end_sRefSet_allElements;
10507 if (context_hasMods ())
10509 if (context_maybeSet (FLG_MODUNCON))
10513 message ("Undetected modification possible "
10514 "from call to unconstrained function %s: %s",
10516 exprNode_unparse (fcn)),
10517 exprNode_loc (fcn));
10522 if (context_maybeSet (FLG_MODUNCONNOMODS)
10523 && !(context_inIterDef () || context_inIterEnd ()))
10526 (FLG_MODUNCONNOMODS,
10527 message ("Undetected modification possible "
10528 "from call to unconstrained function %s: %s",
10530 exprNode_unparse (fcn)),
10531 exprNode_loc (fcn));
10535 exprNode_checkSetAny (fcn, fname);
10539 void exprNode_checkUseParam (exprNode current)
10541 if (exprNode_isDefined (current))
10543 exprNode_checkUse (current, current->sref, current->loc);
10548 checkNumerics (ctype tr1, ctype tr2, ctype te1, ctype te2,
10549 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10554 if (!ctype_match (tr1, tr2))
10556 if ((ctype_isRealInt (tr1) || ctype_isReal (tr1)) &&
10557 (ctype_isRealInt (tr2) || ctype_isReal (tr2)))
10559 DPRINTF (("No error: [%s] %s / [%s] %s",
10560 exprNode_unparse (e1), ctype_unparse (tr1),
10561 exprNode_unparse (e2), ctype_unparse (tr2)));
10565 (void) gentypeerror
10567 message ("Incompatible types for %s (%s, %s): %s %s %s",
10568 lltok_unparse (op),
10569 ctype_unparse (te1),
10570 ctype_unparse (te2),
10571 exprNode_unparse (e1), lltok_unparse (op),
10572 exprNode_unparse (e2)),
10575 ret = ctype_unknown;
10579 if (ctype_isForceRealNumeric (&tr1) && ctype_isForceRealNumeric (&tr2))
10581 ret = ctype_resolveNumerics (tr1, tr2);
10583 else if (!context_msgStrictOps ())
10585 if (ctype_isPointer (tr1))
10587 if (ctype_isPointer (tr2) && !exprNode_isNullValue (e2))
10591 else if (ctype_isInt (tr2))
10597 ret = ctype_unknown;
10600 else if (ctype_isPointer (tr2))
10602 if (ctype_isPointer (tr1))
10606 else if (ctype_isInt (tr1))
10612 ret = ctype_unknown;
10617 ret = ctype_resolveNumerics (tr1, tr2);
10622 int opid = lltok_getTok (op);
10623 bool comparop = (opid == EQ_OP || opid == NE_OP
10624 || opid == TLT || opid == TGT
10625 || opid == LE_OP || opid == GE_OP);
10627 if (!ctype_isNumeric (tr1) && !ctype_isNumeric (tr2))
10630 && ((ctype_isEnum (tr1) && ctype_isEnum (tr2))
10631 || (ctype_isBool (tr1) && ctype_isBool (tr2))
10632 || (ctype_isChar (tr1) && ctype_isChar (tr2))))
10638 if (ctype_sameName (te1, te2))
10642 message ("Operands of %s are non-numeric (%t): %s %s %s",
10643 lltok_unparse (op), te1,
10644 exprNode_unparse (e1), lltok_unparse (op),
10645 exprNode_unparse (e2)),
10652 message ("Operands of %s are non-numerics (%t, %t): %s %s %s",
10653 lltok_unparse (op), te1, te2,
10654 exprNode_unparse (e1), lltok_unparse (op),
10655 exprNode_unparse (e2)),
10660 else if (!ctype_isNumeric (tr1))
10664 message ("Right operand of %s is non-numeric (%t): %s %s %s",
10665 lltok_unparse (op), te1,
10666 exprNode_unparse (e1), lltok_unparse (op),
10667 exprNode_unparse (e2)),
10672 if (!ctype_isNumeric (tr2))
10676 message ("Left operand of %s is non-numeric (%t): %s %s %s",
10677 lltok_unparse (op), te2,
10678 exprNode_unparse (e1), lltok_unparse (op),
10679 exprNode_unparse (e2)),
10684 ret = ctype_unknown;
10692 abstractOpError (ctype tr1, ctype tr2, lltok op,
10693 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10694 fileloc loc1, fileloc loc2)
10696 if (ctype_isRealAbstract (tr1) && ctype_isRealAbstract (tr2))
10698 if (ctype_match (tr1, tr2))
10702 message ("Operands of %s are abstract type (%t): %s %s %s",
10703 lltok_unparse (op), tr1,
10704 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10711 message ("Operands of %s are abstract types (%t, %t): %s %s %s",
10712 lltok_unparse (op), tr1, tr2,
10713 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10717 else if (ctype_isRealAbstract (tr1))
10721 message ("Left operand of %s is abstract type (%t): %s %s %s",
10722 lltok_unparse (op), tr1,
10723 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10728 if (ctype_isRealAbstract (tr2))
10732 message ("Right operand of %s is abstract type (%t): %s %s %s",
10733 lltok_unparse (op), tr2,
10734 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10743 ** requies e1 and e2 and not error exprNode's.
10747 ** If e1 is a component of an abstract type, and e2 is mutable and client-visible,
10748 ** the rep of the abstract type is exposed.
10750 ** The order is very important:
10752 ** check rep expose (move into check transfer)
10758 ** This isn't really a sensible procedure, but the indententation
10759 ** was getting too deep.
10763 checkOneRepExpose (sRef ysr, sRef base,
10764 /*@notnull@*/ exprNode e1,
10765 /*@notnull@*/ exprNode e2, ctype ct,
10768 if (!(sRef_isOnly (ysr) || sRef_isKeep (ysr)
10769 || sRef_isOwned (ysr)
10770 || sRef_isExposed (ysr)))
10772 if (sRef_isAnyParam (base) && !sRef_isExposed (base)
10773 && !sRef_isObserver (base)) /* evans 2001-07-11: added isObserver */
10776 if (sRef_isIReference (ysr))
10778 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10783 ("Assignment of mutable component of parameter %q "
10784 "to component of abstract "
10785 "type %s exposes rep: %s = %s",
10786 sRef_unparse (base),
10787 ctype_unparse (ct),
10788 exprNode_unparse (e1), exprNode_unparse (e2)),
10796 ("Assignment of mutable component of parameter %q "
10797 "(through alias %q) to component of abstract "
10798 "type %s exposes rep: %s = %s",
10799 sRef_unparse (base),
10800 sRef_unparse (e2->sref),
10801 ctype_unparse (ct),
10802 exprNode_unparse (e1), exprNode_unparse (e2)),
10808 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10812 message ("Assignment of mutable parameter %q "
10813 "to component of abstract type %s "
10814 "exposes rep: %s = %s",
10815 sRef_unparse (base),
10816 ctype_unparse (ct),
10817 exprNode_unparse (e1),
10818 exprNode_unparse (e2)),
10825 message ("Assignment of mutable parameter %q "
10826 "(through alias %q) to "
10827 "component of abstract type %s exposes "
10829 sRef_unparse (base),
10830 sRef_unparse (e2->sref),
10831 ctype_unparse (ct),
10832 exprNode_unparse (e1),
10833 exprNode_unparse (e2)),
10839 if (sRef_isFileOrGlobalScope (s2b))
10841 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10845 message ("Assignment of global %q "
10847 "abstract type %s exposes rep: %s = %s",
10848 sRef_unparse (base),
10849 ctype_unparse (ct),
10850 exprNode_unparse (e1), exprNode_unparse (e2)),
10857 message ("Assignment of global %q (through alias %q) "
10859 "abstract type %s exposes rep: %s = %s",
10860 sRef_unparse (base),
10861 sRef_unparse (e2->sref),
10862 ctype_unparse (ct),
10863 exprNode_unparse (e1), exprNode_unparse (e2)),
10871 doAssign (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2, bool isInit)
10873 DPRINTF (("Do assign: %s <- %s",
10874 exprNode_unparse (e1), exprNode_unparse (e2)));
10875 DPRINTF (("Ctype: %s", ctype_unparse (exprNode_getType (e1))));
10877 if (ctype_isRealFunction (exprNode_getType (e1))
10878 && !ctype_isRealPointer (exprNode_getType (e1)))
10882 message ("Invalid left-hand side of assignment (function type %s): %s",
10883 ctype_unparse (exprNode_getType (e1)),
10884 exprNode_unparse (e1)),
10888 if (context_getFlag (FLG_ASSIGNEXPOSE) && ctype_isMutable (e2->typ))
10890 ctype t2 = exprNode_getType (e2);
10891 sRef sr = sRef_getRootBase (e1->sref);
10892 ctype ct = sRef_getType (sr);
10894 if (ctype_isAbstract (t2)
10895 && !(uentry_isMutableDatatype (usymtab_getTypeEntry (ctype_typeId (t2)))))
10897 /* it is immutable, okay to reference */
10898 goto donerepexpose;
10901 if (ctype_isAbstract (ct) && sRef_isIReference (e1->sref))
10903 sRef s2b = sRef_getRootBase (e2->sref);
10904 sRef s1 = e1->sref;
10905 sRef s1b = sRef_getRootBase (s1);
10908 aliases = usymtab_canAlias (e2->sref);
10910 if (!sRef_similar (s2b, s1b)
10911 && !sRef_isExposed (s1)
10912 && !(sRef_isOnly (s2b) || sRef_isKeep (s2b) || sRef_isExposed (s2b)))
10914 if (sRef_isAnyParam (s2b) && !sRef_isOnly (s2b)
10915 && !sRef_isOwned (s2b) && !sRef_isKeep (s2b)
10916 && !sRef_isExposed (s2b))
10918 if (sRef_isIReference (e2->sref))
10923 ("Assignment of mutable component of parameter %q "
10924 "to component of abstract type %s exposes rep: %s = %s",
10925 sRef_unparse (s2b),
10926 ctype_unparse (ct),
10927 exprNode_unparse (e1), exprNode_unparse (e2)),
10934 message ("Assignment of mutable parameter %q to "
10935 "component of abstract type %s exposes rep: %s = %s",
10936 sRef_unparse (s2b),
10937 ctype_unparse (ct),
10938 exprNode_unparse (e1), exprNode_unparse (e2)),
10943 if (sRef_isFileOrGlobalScope (s2b))
10947 message ("Assignment of global %q to component of "
10948 "abstract type %s exposes rep: %s = %s",
10949 sRef_unparse (s2b),
10950 ctype_unparse (ct),
10951 exprNode_unparse (e1), exprNode_unparse (e2)),
10955 sRefSet_realElements (aliases, ysr)
10957 sRef base = sRef_getRootBase (ysr);
10959 if (sRef_similar (ysr, s2b) || sRef_similar (s1b, base)
10960 || sRef_sameName (base, s1b))
10962 ; /* error already reported or same sref */
10966 checkOneRepExpose (ysr, base, e1, e2, ct, s2b);
10968 } end_sRefSet_realElements;
10970 sRefSet_free (aliases);
10977 ** function variables don't really work...
10980 if (!ctype_isFunction (ctype_realType (e2->typ)))
10984 DPRINTF (("Check init: %s / %s",
10985 exprNode_unparse (e1), exprNode_unparse (e2)));
10986 transferChecks_initialization (e1, e2);
10990 transferChecks_assign (e1, e2);
10995 sRef fref = e2->sref;
10997 sRef_setDefState (e1->sref, sRef_getDefState (fref), e1->loc);
10998 sRef_setNullState (e1->sref, sRef_getNullState (fref), e1->loc);
11000 /* Need to typecheck the annotation on the parameters */
11002 if (ctype_isRealFunction (e1->typ)) {
11003 uentryList e1p = ctype_argsFunction (ctype_realType (e1->typ));
11004 uentryList e2p = ctype_argsFunction (ctype_realType (e2->typ));
11006 if (!uentryList_isMissingParams (e1p)
11007 && !uentryList_isMissingParams (e2p)
11008 && uentryList_size (e1p) > 0) {
11009 if (uentryList_size (e1p) == uentryList_size (e2p)) {
11012 uentryList_elements (e1p, el1) {
11015 el2 = uentryList_getN (e2p, n);
11017 uentry_checkMatchParam (el1, el2, n, e2);
11018 } end_uentryList_elements;
11024 if (exprNode_isStringLiteral (e2))
11026 exprNode_checkStringLiteralLength (exprNode_getType (e1), e2);
11029 if (isInit && sRef_isFileOrGlobalScope (e1->sref))
11035 DPRINTF (("Update aliases: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
11036 updateAliases (e1, e2);
11041 checkMacroParen (exprNode e)
11043 if (exprNode_isError (e) || e->kind == XPR_CAST)
11049 if (sRef_isUnsafe (e->sref) && !exprNode_isInParens (e))
11053 message ("Macro parameter used without parentheses: %s",
11054 exprNode_unparse (e)),
11061 reflectNullTest (/*@notnull@*/ exprNode e, bool isnull)
11065 e->guards = guardSet_addTrueGuard (e->guards, e->sref);
11069 e->guards = guardSet_addFalseGuard (e->guards, e->sref);
11076 ** if e2 is a parameter or global derived location which
11077 ** can be modified (that is, e2 is a mutable abstract type,
11078 ** or a derived pointer), then e1 can alias e2.
11080 ** e1 can alias everything which e2 can alias.
11082 ** Also, if e1 is guarded, remove from guard sets!
11085 static void updateAliases (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2)
11087 if (!context_inProtectVars ())
11090 ** depends on types of e1 and e2
11093 sRef s1 = e1->sref;
11094 sRef s2 = e2->sref;
11095 ctype t1 = exprNode_getType (e1);
11097 /* handle pointer sRefs, record fields, arrays, etc... */
11099 if (!ctype_isRealSU (t1))
11101 DPRINTF (("Copying real! %s", ctype_unparse (t1)));
11102 sRef_copyRealDerivedComplete (s1, s2);
11107 ** Fields should alias
11110 DPRINTF (("Not COPYING!: %s", ctype_unparse (t1)));
11113 if (ctype_isMutable (t1) && sRef_isKnown (s1))
11115 usymtab_clearAlias (s1);
11116 usymtab_addMustAlias (s1, s2);
11117 DPRINTF (("Add must alias: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
11121 DPRINTF (("Not mutable: %s", ctype_unparse (t1)));
11124 if (sRef_possiblyNull (s1) && usymtab_isGuarded (s1))
11126 usymtab_unguard (s1);
11131 exprNode exprNode_updateLocation (/*@returned@*/ exprNode e, /*@temp@*/ fileloc loc)
11133 if (exprNode_isDefined (e))
11135 e->loc = fileloc_update (e->loc, loc);
11139 e = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
11145 static void checkUniqueParams (exprNode fcn,
11146 /*@notnull@*/ exprNode current,
11148 int paramno, uentry ucurrent)
11151 sRef thisref = exprNode_getSref (current);
11154 ** Check if any argument could match this argument.
11157 exprNodeList_elements (args, icurrent)
11161 if (iparamno != paramno)
11163 sRef sr = exprNode_getSref (icurrent);
11165 if (sRef_similarRelaxed (thisref, sr))
11167 if (!sRef_isConst (thisref) && !sRef_isConst (sr))
11172 ("Parameter %d (%s) to function %s is declared %s but "
11173 "is aliased by parameter %d (%s)",
11175 exprNode_unparse (current),
11176 exprNode_unparse (fcn),
11177 alkind_unparse (uentry_getAliasKind (ucurrent)),
11178 iparamno, exprNode_unparse (icurrent)),
11184 sRefSet aliases = usymtab_canAlias (sr);
11186 sRefSet_allElements (aliases, asr)
11188 if (ctype_isUnknown (sRef_getType (thisref)))
11190 sRef_setType (thisref, uentry_getType (ucurrent));
11193 if (sRef_similarRelaxed (thisref, asr))
11195 if (sRef_isExternal (asr))
11197 if (sRef_isLocalState (thisref))
11203 sRef base = sRef_getRootBase (asr);
11205 if (!sRef_similar (sRef_getBase (asr), thisref))
11207 if (sRef_isUnique (base) || sRef_isOnly (base)
11208 || sRef_isKept (base)
11209 || (sRef_isAddress (asr) && sRef_isLocalVar (base))
11210 || (sRef_isAddress (thisref)
11211 && sRef_isLocalVar (sRef_getRootBase (thisref))))
11213 ; /* okay, no error */
11218 (FLG_MAYALIASUNIQUE,
11220 ("Parameter %d (%s) to function %s is declared %s but "
11221 "may be aliased externally by parameter %d (%s)",
11223 exprNode_unparse (current),
11224 exprNode_unparse (fcn),
11225 alkind_unparse (uentry_getAliasKind (ucurrent)),
11226 iparamno, exprNode_unparse (icurrent)),
11237 ("Parameter %d (%s) to function %s is declared %s but "
11238 "is aliased externally by parameter %d (%s) through "
11241 exprNode_unparse (current),
11242 exprNode_unparse (fcn),
11243 alkind_unparse (uentry_getAliasKind (ucurrent)),
11244 iparamno, exprNode_unparse (icurrent),
11245 sRef_unparse (asr)),
11249 } end_sRefSet_allElements;
11250 sRefSet_free (aliases);
11253 } end_exprNodeList_elements;
11256 long exprNode_getLongValue (exprNode e) {
11259 if (exprNode_hasValue (e)
11260 && multiVal_isInt (exprNode_getValue (e)))
11262 value = multiVal_forceInt (exprNode_getValue (e));
11273 /*@observer@*/ fileloc exprNode_getfileloc (exprNode p_e)
11275 if (exprNode_isDefined (p_e) )
11276 return ( p_e->loc );
11278 return fileloc_undefined;
11281 /*@only@*/ fileloc exprNode_getNextSequencePoint (exprNode e)
11284 ** Returns the location of the sequence point following e.
11286 ** Only works for statements (for now).
11289 if (exprNode_isDefined (e) && e->kind == XPR_STMT) {
11290 lltok t = exprData_getUopTok (e->edata);
11291 return fileloc_copy(lltok_getLoc (t));
11293 /* drl possible problem : warning fix
11294 llcontbug (message ("Cannot get next sequence point: %s", exprNode_unparse (e)));
11296 return fileloc_undefined;
11300 exprNode exprNode_createNew(ctype c)
11304 ret = exprNode_createPlain (c);
11309 bool exprNode_isInitBlock (exprNode e)
11311 return (exprNode_isDefined(e) && e->kind == XPR_INITBLOCK);