2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 # include <ctype.h> /* for isdigit */
29 # include "splintMacros.nf"
31 # include "cgrammar.h"
32 # include "cscanner.h"
33 # include "cscannerHelp.h"
34 # include "cgrammar_tokens.h"
36 # include "exprChecks.h"
37 # include "transferChecks.h"
38 # include "exprNodeSList.h"
40 static bool exprNode_sameStorage (exprNode p_e1, exprNode p_e2) /*@*/ ;
41 static bool exprNode_isEmptyStatement (exprNode p_e);
42 static /*@exposed@*/ exprNode exprNode_firstStatement (/*@returned@*/ exprNode p_e);
43 static bool exprNode_isFalseConstant (exprNode p_e) /*@*/ ;
44 static bool exprNode_isStatement (exprNode p_e);
45 static void checkGlobUse (uentry p_glob, bool p_isCall, /*@notnull@*/ exprNode p_e);
46 static void exprNode_addUse (exprNode p_e, /*@exposed@*/ sRef p_s);
47 static bool exprNode_matchArgType (ctype p_ct, exprNode p_e);
48 static exprNode exprNode_fakeCopy (exprNode p_e) /*@*/ ;
49 static exprNode exprNode_statementError (/*@only@*/ exprNode p_e, /*@only@*/ lltok p_t);
50 static bool exprNode_matchTypes (exprNode p_e1, exprNode p_e2);
51 static void checkUniqueParams (exprNode p_fcn,
52 /*@notnull@*/ exprNode p_current, exprNodeList p_args,
53 int p_paramno, uentry p_ucurrent);
54 static void updateAliases (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2);
55 static bool abstractOpError (ctype p_tr1, ctype p_tr2, lltok p_op,
56 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2,
57 fileloc p_loc1, fileloc p_loc2)
58 /*@modifies g_warningstream@*/ ;
59 static ctype checkNumerics (ctype p_tr1, ctype p_tr2, ctype p_te1, ctype p_te2,
60 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, lltok p_op);
61 static void doAssign (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, bool p_isInit);
62 static void checkSafeUse (exprNode p_e, /*@exposed@*/ sRef p_s);
63 static void reflectNullTest (/*@notnull@*/ exprNode p_e, bool p_isnull);
64 static void checkMacroParen (exprNode p_e);
65 static exprNodeSList exprNode_flatten (/*@dependent@*/ exprNode p_e);
66 static void exprNode_checkSetAny (exprNode p_e, /*@dependent@*/ cstring p_name);
67 static void exprNode_checkUse (exprNode p_e, /*@exposed@*/ sRef p_s, fileloc p_loc);
68 static void exprNode_mergeUSs (exprNode p_res, exprNode p_other);
69 static void exprNode_mergeCondUSs (exprNode p_res, exprNode p_other1, exprNode p_other2);
70 static /*@only@*/ /*@notnull@*/ exprNode exprNode_fromIdentifierAux (/*@observer@*/ uentry p_c);
71 static void checkAnyCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn,
72 /*@dependent@*/ cstring p_fname,
73 uentryList p_pn, exprNodeList p_args,
74 bool p_hasMods, sRefSet p_mods, bool p_isSpec,
76 static void checkOneArg (uentry p_ucurrent, /*@notnull@*/ exprNode p_current,
77 /*@dependent@*/ exprNode p_fcn, bool p_isSpec, int p_argno, int p_totargs);
79 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn, uentryList p_params, exprNodeList p_args);
81 static /*@only@*/ exprNode exprNode_effect (exprNode p_e)
82 /*@globals internalState@*/ ;
83 static /*@only@*/ cstring exprNode_doUnparse (exprNode p_e);
84 static /*@observer@*/ cstring exprNode_rootVarName (exprNode p_e);
85 static /*@exposed@*/ exprNode
86 exprNode_lastStatement (/*@returned@*/ exprNode p_e);
88 static /*@only@*/ exprNode s_mustExitNode = exprNode_undefined;
90 static int checkArgsReal (uentry p_fcn, /*@dependent@*/ exprNode p_f,
92 exprNodeList p_args, bool p_isIter, exprNode p_ret);
94 static bool inEffect = FALSE;
95 static int nowalloc = 0;
96 static int totalloc = 0;
97 static int maxalloc = 0;
99 static /*@only@*/ uentry regArg;
100 static /*@only@*/ uentry outArg;
101 static /*@only@*/ uentry outStringArg;
102 static /*@exposed@*/ sRef stdinRef;
103 static /*@exposed@*/ sRef stdoutRef;
104 static /*@only@*/ uentry csArg;
105 static /*@only@*/ uentry csOnlyArg;
106 static ctype cstringType;
107 static ctype ctypeType;
108 static ctype filelocType;
109 static bool initMod = FALSE;
111 /*@function void exprNode_swap (sef exprNode, sef exprNode)@*/
113 # define exprNode_swap(e1,e2) do { exprNode m_tmp = (e1); (e1) = (e2); (e2) = m_tmp; } while (FALSE)
116 static void exprNode_defineConstraints(/*@sef@*/ /*@special@*/ /*@notnull@*/ exprNode e)
117 /*@defines e->requiresConstraints, e->ensuresConstraints,
118 e->trueEnsuresConstraints, e->falseEnsuresConstraints @*/
120 e->requiresConstraints = constraintList_makeNew ();
121 e->ensuresConstraints = constraintList_makeNew ();
122 e->trueEnsuresConstraints = constraintList_makeNew ();
123 e->falseEnsuresConstraints = constraintList_makeNew ();
127 ** must occur after library has been read
130 void exprNode_initMod (void)
131 /*@globals undef regArg, undef outArg, undef outStringArg,
132 undef csOnlyArg, undef csArg;
139 cstringType = ctype_unknown;
140 ctypeType = ctype_unknown;
141 filelocType = ctype_unknown;
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 s_mustExitNode, initMod @*/
224 /* evans 2002-07-12: changed uentry_free to uentry_freeComplete */
225 uentry_freeComplete (regArg);
226 uentry_freeComplete (outArg);
227 uentry_freeComplete (outStringArg);
229 exprNode_free (s_mustExitNode);
236 static void exprNode_resetSref (/*@notnull@*/ exprNode e)
238 e->sref = sRef_undefined;
241 exprNode exprNode_fakeCopy (exprNode e)
243 /*@-temptrans@*/ /*@-retalias@*/
245 /*@=temptrans@*/ /*@=retalias@*/
248 static bool isFlagKey (char key)
250 return (key == '-' || key == '+' || key == ' ' || key == '#');
253 static void exprNode_combineControl (/*@notnull@*/ exprNode ret,
254 /*@notnull@*/ exprNode ifclause,
255 /*@notnull@*/ exprNode elseclause)
257 ret->canBreak = ifclause->canBreak || elseclause->canBreak;
260 (ifclause->mustBreak || exprNode_mustEscape (ifclause))
261 && (elseclause->mustBreak || exprNode_mustEscape (elseclause));
263 ret->exitCode = exitkind_combine (ifclause->exitCode,
264 elseclause->exitCode);
269 ** For exprNode's returned by exprNode_effect.
272 static bool shallowKind (exprKind kind)
274 return (kind == XPR_STRINGLITERAL
275 || kind == XPR_NUMLIT
278 || kind == XPR_NODE);
282 exprNode_freeIniter (/*@only@*/ exprNode e)
284 if (!exprNode_isError (e))
290 ** Its a fake copy, don't free the field->rec and field->field
295 sfree (e->edata->field);
301 exprNode_free (e->edata->op->b);
302 /*@-compdestroy@*/ sfree (e->edata->op); /*@=compdestroy@*/
306 llbug (message ("other: %s", exprNode_unparse (e)));
309 multiVal_free (e->val);
310 cstring_free (e->etext);
311 fileloc_free (e->loc);
312 sRefSet_free (e->uses);
313 sRefSet_free (e->sets);
314 sRefSet_free (e->msets);
315 guardSet_free (e->guards);
317 constraintList_free(e->requiresConstraints);
318 constraintList_free(e->ensuresConstraints);
319 constraintList_free(e->trueEnsuresConstraints);
320 constraintList_free(e->falseEnsuresConstraints);
322 e->requiresConstraints = NULL;
323 e->ensuresConstraints = NULL;
324 e->trueEnsuresConstraints = NULL;
325 e->falseEnsuresConstraints = NULL;
332 exprNode_freeShallow (/*@only@*/ exprNode e)
334 if (!exprNode_isError (e))
336 if (shallowKind (e->kind))
343 if (e->kind == XPR_EMPTY
344 || e->kind == XPR_BODY
345 || e->kind == XPR_STRINGLITERAL
346 || e->kind == XPR_NUMLIT
347 || e->kind == XPR_NODE
348 || e->kind == XPR_OFFSETOF
349 || e->kind == XPR_ALIGNOFT
350 || e->kind == XPR_ALIGNOF
351 || e->kind == XPR_SIZEOFT
352 || e->kind == XPR_SIZEOF)
354 /* don't free anything */
358 /* multiVal_free (e->val); */
359 cstring_free (e->etext);
360 fileloc_free (e->loc);
361 sRefSet_free (e->uses);
362 sRefSet_free (e->sets);
363 sRefSet_free (e->msets);
364 guardSet_free (e->guards);
365 exprData_freeShallow (e->edata, e->kind);
367 /*@-compdestroy@*/ sfree (e); /*@=compdestroy@*/
376 exprNode_free (exprNode e)
378 if (!exprNode_isError (e))
382 multiVal_free (e->val);
383 cstring_free (e->etext);
384 fileloc_free (e->loc);
385 sRefSet_free (e->uses);
386 sRefSet_free (e->sets);
387 sRefSet_free (e->msets);
388 guardSet_free (e->guards);
389 exprData_free (e->edata, e->kind);
391 constraintList_free(e->requiresConstraints);
392 constraintList_free(e->ensuresConstraints);
393 constraintList_free(e->trueEnsuresConstraints);
394 constraintList_free(e->falseEnsuresConstraints);
396 e->requiresConstraints = NULL;
397 e->ensuresConstraints = NULL;
398 e->trueEnsuresConstraints = NULL;
399 e->falseEnsuresConstraints = NULL;
409 exprNode_makeError ()
411 return exprNode_undefined;
414 static /*@out@*/ /*@only@*/ /*@notnull@*/ exprNode
417 exprNode ret = (exprNode) dmalloc (sizeof (*ret));
418 /* static int lastexpnodes = 0; */
423 if (nowalloc > maxalloc)
431 static /*@notnull@*/ /*@special@*/ exprNode
432 exprNode_createPlain (ctype c)
434 /*@ensures isnull result->edata, result->loc, result->val, result->guards,
435 result->uses, result->sets, result->msets, result->etext @*/
438 exprNode e = exprNode_new ();
442 e->val = multiVal_undefined;
443 e->sref = sRef_undefined;
444 e->etext = cstring_undefined;
445 e->loc = fileloc_undefined;
446 e->guards = guardSet_undefined;
447 e->uses = sRefSet_undefined;
448 e->sets = sRefSet_undefined;
449 e->msets = sRefSet_undefined;
450 e->edata = exprData_undefined;
451 e->exitCode = XK_NEVERESCAPE;
453 e->mustBreak = FALSE;
454 e->isJumpPoint = FALSE;
456 exprNode_defineConstraints(e);
461 /*@observer@*/ exprNode exprNode_makeMustExit (void)
463 if (exprNode_isUndefined (s_mustExitNode))
465 s_mustExitNode = exprNode_createPlain (ctype_unknown);
466 s_mustExitNode->exitCode = XK_MUSTEXIT;
469 return s_mustExitNode;
473 static /*@notnull@*/ /*@special@*/ exprNode exprNode_create (ctype c)
475 /*@post:isnull result->edata, result->guards, result->val,
476 result->uses, result->sets, result->msets@*/
479 exprNode e = exprNode_createPlain (c);
480 e->loc = fileloc_copy (g_currentloc);
484 static /*@notnull@*/ /*@special@*/ exprNode exprNode_createUnknown (void)
486 /*@post:isnull result->edata, result->guards,
487 result->uses, result->sets, result->msets@*/
490 return (exprNode_create (ctype_unknown));
493 static /*@notnull@*/ /*@special@*/ exprNode
494 exprNode_createLoc (ctype c, /*@keep@*/ fileloc loc)
496 /*@post:isnull result->edata, result->guards, result->val,
497 result->uses, result->sets, result->msets@*/
500 exprNode e = exprNode_createPlain (c);
506 exprNode_copySets (/*@special@*/ /*@notnull@*/ exprNode ret, exprNode e)
507 /*@defines ret->guards, ret->uses, ret->sets, ret->msets@*/
509 if (exprNode_isDefined (e))
511 ret->guards = guardSet_copy (e->guards);
512 ret->uses = sRefSet_newCopy (e->uses);
513 ret->sets = sRefSet_newCopy (e->sets);
514 ret->msets = sRefSet_newCopy (e->msets);
518 ret->guards = guardSet_undefined;
519 ret->uses = sRefSet_undefined;
520 ret->sets = sRefSet_undefined;
521 ret->msets = sRefSet_undefined;
525 static /*@notnull@*/ /*@special@*/ exprNode
526 exprNode_createPartialLocCopy (exprNode e, /*@only@*/ fileloc loc)
528 /*@post:isnull result->edata, result->etext@*/
531 exprNode ret = exprNode_new ();
533 if (exprNode_isError (e))
535 ret->typ = ctype_unknown;
536 ret->val = multiVal_undefined;
538 ret->guards = guardSet_undefined;
539 ret->uses = sRefSet_undefined;
540 ret->sets = sRefSet_undefined;
541 ret->msets = sRefSet_undefined;
546 ret->val = multiVal_copy (e->val);
548 ret->guards = guardSet_copy (e->guards);
549 ret->uses = sRefSet_newCopy (e->uses);
550 ret->sets = sRefSet_newCopy (e->sets);
551 ret->msets = sRefSet_newCopy (e->msets);
554 ret->kind = XPR_EMPTY;
555 ret->sref = sRef_undefined;
556 ret->etext = cstring_undefined;
557 ret->exitCode = XK_NEVERESCAPE;
558 ret->canBreak = FALSE;
559 ret->mustBreak = FALSE;
560 ret->isJumpPoint = FALSE;
561 ret->edata = exprData_undefined;
563 exprNode_defineConstraints(ret);
569 static /*@notnull@*/ /*@special@*/ exprNode
570 exprNode_createPartialCopy (exprNode e)
572 /*@post:isnull result->edata, result->etext@*/
575 return (exprNode_createPartialLocCopy (e, fileloc_copy (exprNode_loc (e))));
578 static /*@notnull@*/ /*@special@*/ exprNode
579 exprNode_createPartialNVCopy (exprNode e)
581 /*@post:isnull result->edata, result->etext, result->val @*/
584 exprNode ret = exprNode_new ();
586 if (exprNode_isError (e))
588 ret->typ = ctype_unknown;
589 ret->loc = fileloc_undefined;
590 ret->guards = guardSet_undefined;
591 ret->uses = sRefSet_undefined;
592 ret->sets = sRefSet_undefined;
593 ret->msets = sRefSet_undefined;
598 ret->loc = fileloc_copy (e->loc);
599 ret->guards = guardSet_copy (e->guards);
600 ret->uses = sRefSet_newCopy (e->uses);
601 ret->sets = sRefSet_newCopy (e->sets);
602 ret->msets = sRefSet_newCopy (e->msets);
605 ret->val = multiVal_undefined;
606 ret->kind = XPR_EMPTY;
607 ret->sref = sRef_undefined;
608 ret->etext = cstring_undefined;
609 ret->exitCode = XK_NEVERESCAPE;
610 ret->canBreak = FALSE;
611 ret->mustBreak = FALSE;
612 ret->isJumpPoint = FALSE;
613 ret->edata = exprData_undefined;
615 exprNode_defineConstraints(ret);
620 static /*@notnull@*/ /*@special@*/ exprNode
621 exprNode_createSemiCopy (exprNode e)
623 /*@post:isnull result->edata, result->etext, result->sets,
624 result->msets, result->uses, result->guards@*/
627 if (exprNode_isError (e))
629 return exprNode_createPlain (ctype_unknown);
633 exprNode ret = exprNode_new ();
636 ret->val = multiVal_copy (e->val);
637 ret->loc = fileloc_copy (e->loc);
638 ret->guards = guardSet_undefined;
639 ret->uses = sRefSet_undefined;
640 ret->sets = sRefSet_undefined;
641 ret->msets = sRefSet_undefined;
643 ret->kind = XPR_EMPTY;
644 ret->sref = sRef_undefined;
645 ret->etext = cstring_undefined;
646 ret->exitCode = XK_NEVERESCAPE;
647 ret->canBreak = FALSE;
648 ret->mustBreak = FALSE;
649 ret->isJumpPoint = FALSE;
650 ret->edata = exprData_undefined;
652 exprNode_defineConstraints(ret);
659 exprNode_isNullValue (exprNode e)
661 if (exprNode_isDefined (e))
663 multiVal m = exprNode_getValue (e);
665 if (multiVal_isInt (m))
667 return (multiVal_forceInt (m) == 0);
675 exprNode_isUnknownConstant (/*@notnull@*/ exprNode e)
677 while (e->kind == XPR_PARENS)
679 e = exprData_getUopNode (e->edata);
681 if (!exprNode_isDefined (e))
686 /* evans 2002-02-05: was llassert (exprNode_isDefined (e)); but this can fail */
689 if (e->kind == XPR_CONST)
691 multiVal m = exprNode_getValue (e);
693 if (multiVal_isUnknown (m))
703 exprNode_numLiteral (ctype c, /*@temp@*/ cstring t,
704 /*@only@*/ fileloc loc, long val)
706 exprNode e = exprNode_createLoc (c, loc);
708 e->kind = XPR_NUMLIT;
710 llassert (multiVal_isUndefined (e->val));
711 e->val = multiVal_makeInt (val);
712 e->edata = exprData_makeLiteral (cstring_copy (t));
716 e->sref = sRef_makeUnknown ();
717 sRef_setDefNull (e->sref, e->loc);
720 DPRINTF (("Num lit: %s / %s", exprNode_unparse (e), ctype_unparse (exprNode_getType (e))));
725 exprNode_charLiteral (char c, cstring text, /*@only@*/ fileloc loc)
727 exprNode e = exprNode_createLoc (ctype_char, loc);
729 if (context_getFlag (FLG_CHARINTLITERAL))
731 e->typ = ctype_makeConj (ctype_char, ctype_int);
734 e->kind = XPR_NUMLIT;
735 e->val = multiVal_makeChar (c);
737 e->edata = exprData_makeLiteral (cstring_copy (text));
742 exprNode_floatLiteral (double d, ctype ct, cstring text, /*@only@*/ fileloc loc)
744 exprNode e = exprNode_createLoc (ct, loc);
746 e->kind = XPR_NUMLIT;
747 e->val = multiVal_makeDouble (d);
748 e->edata = exprData_makeLiteral (cstring_copy (text));
752 multiVal exprNode_getValue (exprNode e)
754 while (exprNode_isInParens (e)) {
755 if (e->edata != NULL) {
756 e = exprData_getUopNode (e->edata);
762 if (exprNode_isDefined (e)) {
765 return multiVal_undefined;
770 exprNode_combineLiterals (exprNode e, exprNode rest)
774 /* Both must be string literals. */
776 if (exprNode_isUndefined (rest) || exprNode_isUndefined (e))
778 exprNode_free (rest);
782 if (!exprNode_isStringLiteral (e))
786 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e),
787 exprNode_unparse (rest)),
789 exprNode_free (rest);
793 if (!exprNode_isStringLiteral (rest))
797 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e), exprNode_unparse (rest)),
800 exprNode_free (rest);
804 ns = cstring_concat (multiVal_forceString (exprNode_getValue (e)),
805 multiVal_forceString (exprNode_getValue (rest)));
807 multiVal_free (e->val);
808 exprData_free (e->edata, e->kind);
809 e->edata = exprData_makeLiteral (cstring_copy (ns));
810 e->val = multiVal_makeString (ns);
811 exprNode_free (rest);
816 exprNode_rawStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
818 exprNode e = exprNode_createLoc (ctype_string, loc);
819 size_t len = cstring_length (t);
821 if (context_getFlag (FLG_STRINGLITERALLEN))
823 if (len > size_fromInt (context_getValue (FLG_STRINGLITERALLEN)))
825 voptgenerror (FLG_STRINGLITERALLEN,
827 ("String literal length (%d) exceeds maximum "
828 "length (%d): \"%s\"",
830 context_getValue (FLG_STRINGLITERALLEN),
836 e->kind = XPR_STRINGLITERAL;
837 e->val = multiVal_makeString (cstring_copy (t));
838 e->edata = exprData_makeLiteral (t);
839 e->sref = sRef_makeConst (ctype_string);
841 if (context_getFlag (FLG_READONLYSTRINGS))
843 sRef_setAliasKind (e->sref, AK_STATIC, fileloc_undefined);
844 sRef_setExKind (e->sref, XO_OBSERVER, loc);
848 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
851 return (e); /* s released */
855 exprNode_wideStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
857 exprNode res = exprNode_stringLiteral (t, loc);
858 res->typ = ctype_makeWideString ();
863 exprNode_stringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
865 size_t len = size_fromInt (size_toInt (cstring_length (t)) - 2);
866 char *ts = cstring_toCharsSafe (t);
867 char *s = cstring_toCharsSafe (cstring_create (len + 1));
869 llassert (*ts == '\"' && *(ts + len + 1) == '\"');
870 strncpy (s, ts+1, len);
873 return exprNode_rawStringLiteral (cstring_fromCharsO (s), loc);
876 exprNode exprNode_fromUIO (cstring c)
878 fileloc loc = context_getSaveLocation ();
879 exprNode e = exprNode_createPlain (ctype_unknown);
883 if (fileloc_isUndefined (loc))
885 loc = fileloc_copy (g_currentloc);
888 e->loc = loc; /* save loc was mangled */
889 e->sref = sRef_undefined;
891 if (usymtab_exists (c))
893 uentry ue = usymtab_lookupEither (c);
895 if (uentry_isDatatype (ue)
896 && uentry_isSpecified (ue))
899 (message ("%q: Specified datatype %s used in code, but not defined. "
900 "(Cannot continue reasonably from this error.)",
901 fileloc_unparse (e->loc), c));
909 llassertprint (!usymtab_exists (c), ("Entry exists: %s", c));
912 ** was supercedeGlobalEntry...is this better?
915 if (!context_inIterEnd ())
917 if (context_inMacro ())
919 if (context_getFlag (FLG_UNRECOG))
923 message ("Unrecognized identifier in macro definition: %s", c), e->loc);
927 flagcode_recordSuppressed (FLG_UNRECOG);
933 (FLG_UNRECOG, message ("Unrecognized identifier: %s", c), e->loc);
937 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
939 /* No alias errors for unrecognized identifiers */
940 sRef_setAliasKind (e->sref, AK_ERROR, loc);
945 exprNode exprNode_makeConstantString (cstring c, /*@only@*/ fileloc loc)
947 exprNode e = exprNode_createPlain (ctype_unknown);
950 e->sref = sRef_makeConst (ctype_string);
951 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
952 e->typ = ctype_string;
954 /* No alias errors for unrecognized identifiers */
955 sRef_setAliasKind (e->sref, AK_STATIC, loc);
956 sRef_setExKind (e->sref, XO_OBSERVER, loc);
961 exprNode exprNode_createId (/*@observer@*/ uentry c)
963 if (uentry_isValid (c))
965 exprNode e = exprNode_new ();
967 DPRINTF (("create id: %s", uentry_unparse (c)));
969 e->typ = uentry_getType (c);
971 if (uentry_isFunction (c)
972 && !sRef_isLocalVar (uentry_getSref (c)))
974 e->sref = sRef_undefined;
978 e->sref = uentry_getSref (c);
981 if (sRef_isStateUnknown (e->sref) && uentry_isNonLocal (c))
983 sRef_setDefined (e->sref, fileloc_undefined);
987 ** yoikes! leaving this out was a heinous bug...that would have been
988 ** caught if i had splint working first. gag!
991 e->etext = cstring_undefined;
993 if (uentry_isEitherConstant (c))
996 e->val = multiVal_copy (uentry_getConstantValue (c));
1001 e->val = multiVal_unknown ();
1004 e->edata = exprData_makeId (c);
1005 e->loc = context_getSaveLocation ();
1007 if (fileloc_isUndefined (e->loc))
1009 fileloc_free (e->loc);
1010 e->loc = fileloc_copy (g_currentloc);
1013 e->guards = guardSet_new ();
1014 e->sets = sRefSet_new ();
1015 e->msets = sRefSet_new ();
1016 e->uses = sRefSet_new ();
1018 /*> missing fields, detected by splint <*/
1019 e->exitCode = XK_NEVERESCAPE;
1020 e->isJumpPoint = FALSE;
1021 e->canBreak = FALSE;
1022 e->mustBreak = FALSE;
1024 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 (cscannerHelp_observeLastIdentifier ());
1042 if (uentry_isValid (ce))
1048 llbuglit ("Looks like Aunt Millie forgot to walk to dog again.");
1052 ret = exprNode_fromIdentifierAux (c);
1056 static void exprNode_checkStringLiteralLength (ctype t1, exprNode e2)
1058 multiVal mval = exprNode_getValue (e2);
1062 if (ctype_isFixedArray (t1))
1064 size_t nelements = ctype_getArraySize (t1);
1066 llassert (multiVal_isString (mval));
1067 slit = multiVal_forceString (mval);
1069 len = cstring_lengthExpandEscapes (slit);
1071 llassert (exprNode_isDefined (e2));
1073 if (len == nelements)
1077 temp = cstring_expandEscapes (slit);
1079 if (temp[len-1] == '\0')
1082 (FLG_STRINGLITNOROOMFINALNULL,
1083 message ("String literal with %d character%& "
1084 "is assigned to %s (no room for final null terminator): %s",
1085 size_toInt (len + 1),
1087 exprNode_unparse (e2)),
1093 (FLG_STRINGLITNOROOM,
1094 message ("String literal with %d character%& "
1095 "is assigned to %s (no room for null terminator): %s",
1096 size_toInt (len + 1),
1098 exprNode_unparse (e2)),
1102 else if (len > nelements)
1105 (FLG_STRINGLITTOOLONG,
1106 message ("String literal with %d character%& (counting null terminator) "
1107 "is assigned to %s (insufficient storage available): %s",
1108 size_toInt (len + 1),
1110 exprNode_unparse (e2)),
1113 else if (len < nelements - 1)
1116 (FLG_STRINGLITSMALLER,
1117 message ("String literal with %d character%& is assigned to %s (possible waste of storage): %s",
1118 size_toInt (len + 1),
1120 exprNode_unparse (e2)),
1130 static /*@only@*/ /*@notnull@*/ exprNode
1131 exprNode_fromIdentifierAux (/*@observer@*/ uentry c)
1133 exprNode e = exprNode_createId (c);
1136 uentry_setUsed (c, e->loc);
1138 if (uentry_isVar (c) && sRef_isFileOrGlobalScope (sr))
1140 checkGlobUse (c, FALSE, e);
1147 exprNode_isZero (exprNode e)
1149 if (exprNode_isDefined (e))
1151 multiVal m = exprNode_getValue (e);
1153 if (multiVal_isInt (m))
1155 return (multiVal_forceInt (m) == 0);
1163 exprNode_isNonNegative (exprNode e)
1165 if (exprNode_isDefined (e))
1167 multiVal m = exprNode_getValue (e);
1169 if (multiVal_isInt (m))
1171 return (multiVal_forceInt (m) >= 0);
1175 ** This is not always true if programmer defines enum
1176 ** values, but then the constant should be known.
1179 if (ctype_isEnum (ctype_realType (e->typ)))
1189 ** a[x] - uses a but NOT a[]
1190 ** result sref = a[] (set/use in assignment)
1192 ** The syntax x[a] is also legal in C, and has the same
1193 ** semantics. If ind is an array, and arr is an int, flip
1198 exprNode_arrayFetch (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
1201 ** error in arr, error propagates (no new messages)
1202 ** error in ind, assume valid and continue
1205 DPRINTF (("Array fetch: %s / %s",
1206 exprNode_unparse (e1), exprNode_unparse (e2)));
1208 if (exprNode_isError (e1))
1211 return (exprNode_makeError ());
1217 ctype carr = exprNode_getType (e1);
1218 ctype crarr = ctype_realType (carr);
1221 ** this sets up funny aliasing, that leads to spurious
1222 ** splint errors. Hence, the i2 comments.
1225 /* evans 2001-09-09 added ctype_isKnown so there is no swap when e1 type is unknown */
1226 if (ctype_isKnown (crarr)
1227 && !ctype_isRealArray (crarr)
1228 && ctype_isRealNumeric (crarr)
1229 && !exprNode_isError (e2)
1230 && ctype_isRealAP (exprNode_getType (e2))) /* fetch like 3[a] */
1235 carr = exprNode_getType (arr);
1236 crarr = ctype_realType (carr);
1244 DPRINTF (("arr: %s", exprNode_unparse (arr)));
1246 if (sRef_possiblyNull (arr->sref))
1248 if (!usymtab_isGuarded (arr->sref))
1250 if (optgenerror (FLG_NULLDEREF,
1251 message ("Index of %s pointer %q: %s",
1252 sRef_nullMessage (arr->sref),
1253 sRef_unparse (arr->sref),
1254 exprNode_unparse (arr)),
1257 DPRINTF (("ref: %s", sRef_unparseFull (arr->sref)));
1258 sRef_showNullInfo (arr->sref);
1260 /* suppress future messages */
1261 sRef_setNullError (arr->sref);
1266 if (exprNode_isError (ind))
1268 if ((ctype_isArrayPtr (crarr)
1269 && !ctype_isFunction (crarr))
1270 || ctype_isUnknown (carr))
1272 exprNode ret = exprNode_createPartialCopy (arr);
1274 if (ctype_isKnown (carr))
1276 ret->typ = ctype_baseArrayPtr (crarr);
1280 ret->typ = ctype_unknown;
1283 ret->sref = sRef_makeArrayFetch (arr->sref);
1285 ret->kind = XPR_FETCH;
1288 ** Because of funny aliasing (when arr and ind are
1289 ** flipped) spurious errors would be reported here.
1292 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1293 checkSafeUse (ret, arr->sref);
1298 voptgenerror (FLG_TYPE,
1299 message ("Array fetch from non-array (%t): %s[%s]", carr,
1300 exprNode_unparse (e1), exprNode_unparse (e2)),
1302 exprNode_free (arr);
1303 return (exprNode_makeError ());
1308 if (!ctype_isForceRealInt (&(ind->typ)))
1310 ctype rt = ctype_realType (ind->typ);
1312 if (ctype_isChar (rt))
1316 message ("Array fetch using non-integer, %t: %s[%s]",
1318 exprNode_unparse (e1), exprNode_unparse (e2)),
1321 else if (ctype_isEnum (rt))
1325 message ("Array fetch using non-integer, %t: %s[%s]",
1327 exprNode_unparse (e1), exprNode_unparse (e2)),
1334 message ("Array fetch using non-integer, %t: %s[%s]",
1336 exprNode_unparse (e1), exprNode_unparse (e2)),
1340 multiVal_free (ind->val);
1341 ind->val = multiVal_unknown ();
1344 if (ctype_isArrayPtr (crarr) && !ctype_isFunction (crarr))
1346 exprNode ret = exprNode_createSemiCopy (arr);
1347 multiVal m = exprNode_getValue (ind);
1349 ret->typ = ctype_baseArrayPtr (crarr);
1350 ret->kind = XPR_FETCH;
1352 if (multiVal_isInt (m))
1354 int i = (int) multiVal_forceInt (m);
1356 if (sRef_isValid (arr->sref)) {
1357 ret->sref = sRef_makeArrayFetchKnown (arr->sref, i);
1359 ret->sref = sRef_undefined;
1364 ret->sref = sRef_makeArrayFetch (arr->sref);
1367 ret->sets = sRefSet_realNewUnion (arr->sets, ind->sets);
1368 ret->msets = sRefSet_realNewUnion (arr->msets, ind->msets);
1369 ret->uses = sRefSet_realNewUnion (arr->uses, ind->uses);
1371 /* (see comment on spurious errors above) */
1372 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1374 exprNode_checkUse (ret, ind->sref, ind->loc);
1375 exprNode_checkUse (ret, arr->sref, arr->loc);
1381 if (ctype_isUnknown (carr))
1383 exprNode ret = exprNode_createPartialCopy (arr);
1385 ret->kind = XPR_FETCH;
1386 ret->typ = ctype_unknown;
1387 ret->sets = sRefSet_union (ret->sets, ind->sets);
1388 ret->msets = sRefSet_union (ret->msets, ind->msets);
1389 ret->uses = sRefSet_union (ret->uses, ind->uses);
1391 /* (see comment on spurious errors above) */
1392 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1394 exprNode_checkUse (ret, ind->sref, ind->loc);
1395 exprNode_checkUse (ret, arr->sref, arr->loc);
1402 message ("Array fetch from non-array (%t): %s[%s]", carr,
1403 exprNode_unparse (e1), exprNode_unparse (e2)),
1406 exprNode_free (arr);
1407 exprNode_free (ind);
1409 return (exprNode_makeError ());
1419 checkArgs (uentry fcn, /*@dependent@*/ exprNode f, ctype t,
1420 exprNodeList args, exprNode ret)
1422 return (checkArgsReal (fcn, f, ctype_argsFunction (t), args, FALSE, ret));
1426 ** checkPrintfArgs --- checks arguments for printf-like functions
1427 ** Arguments before ... have already been checked.
1428 ** The argument before the ... is a char *.
1429 ** argno is the format string argument.
1433 checkPrintfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1434 exprNodeList args, exprNode ret, int argno)
1437 ** the last argument before the elips is the format string
1442 int nargs = exprNodeList_size (args);
1443 uentryList params = uentry_getParams (fcn);
1447 ** These should be ensured by checkSpecialFunction
1450 llassert (uentryList_size (params) == argno + 1);
1451 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1453 a = exprNodeList_getN (args, argno - 1);
1454 formatloc = fileloc_copy (exprNode_loc (a));
1456 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1457 && exprNode_knownStringValue (a))
1459 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1460 char *code = format;
1463 nargs = exprNodeList_size (args);
1465 while ((code = strchr (code, '%')) != NULL)
1467 char *origcode = code;
1468 cstring codetext = cstring_newEmpty ();
1469 char key = *(++code);
1470 ctype modtype = ctype_int;
1471 bool modified = FALSE;
1473 fileloc_addColumn (formatloc, code - ocode);
1475 codetext = cstring_appendChar (codetext, key);
1478 while (isFlagKey (key))
1481 codetext = cstring_appendChar (codetext, key);
1482 fileloc_incColumn (formatloc);
1485 if (key == 'm') /* skipped in syslog */
1490 /* ignore field width */
1491 while (isdigit ((int) key) != 0)
1494 codetext = cstring_appendChar (codetext, key);
1495 fileloc_incColumn (formatloc);
1498 /* ignore precision */
1502 codetext = cstring_appendChar (codetext, key);
1503 fileloc_incColumn (formatloc);
1506 ** In printf, '*' means: read the next arg as an int for the
1507 ** field width. This seems to be missing from my copy of the
1508 ** standard x3.159-1989. Setion 4.9.6.1 refers to * (described
1509 ** later) but never does.
1514 ; /* don't do anything --- handle later */
1518 while (isdigit ((int) key) != 0)
1521 codetext = cstring_appendChar (codetext, key);
1522 fileloc_incColumn (formatloc);
1529 modtype = ctype_sint; /* short */
1531 codetext = cstring_appendChar (codetext, key);
1532 fileloc_incColumn (formatloc);
1534 else if (key == 'l' || key == 'L')
1536 modtype = ctype_lint; /* long */
1538 codetext = cstring_appendChar (codetext, key);
1539 fileloc_incColumn (formatloc);
1541 if (key == 'l' || key == 'L') {
1542 modtype = ctype_llint; /* long long */
1544 codetext = cstring_appendChar (codetext, key);
1545 fileloc_incColumn (formatloc);
1553 /* now, key = type of conversion to apply */
1555 fileloc_incColumn (formatloc);
1563 message ("No argument corresponding to %q format "
1564 "code %d (%%%s): \"%s\"",
1565 uentry_getName (fcn),
1567 cstring_fromChars (format)),
1570 if (fileloc_isDefined (formatloc)
1571 && context_getFlag (FLG_SHOWCOL))
1573 llgenindentmsg (cstring_makeLiteral ("Corresponding format code"),
1581 a = exprNodeList_getN (args, i);
1584 if (!exprNode_isError (a))
1590 case '*': /* int argument for fieldwidth */
1591 expecttype = ctype_int;
1592 *(--code) = '%'; /* convert it for next code */
1593 fileloc_subColumn (formatloc, 1);
1594 /*@switchbreak@*/ break;
1597 expecttype = ctype_combine (ctype_uint, modtype);
1598 /*@switchbreak@*/ break;
1600 case 'i': /* int argument */
1602 expecttype = ctype_combine (ctype_int, modtype);
1603 /*@switchbreak@*/ break;
1605 case 'x': /* unsigned int */
1607 expecttype = ctype_combine (ctype_uint, modtype);
1609 /*@switchbreak@*/ break;
1615 case 'f': /* double */
1616 expecttype = ctype_combine (ctype_double, modtype);
1617 /*@switchbreak@*/ break;
1619 case 'c': /* int converted to char (check its a char?) */
1620 expecttype = ctype_makeConj (ctype_int,
1621 ctype_makeConj (ctype_char,
1624 /* evans 2001-10-05 - changed to reflect correct ISO spec:
1625 int converted to char */
1627 /* expecttype = ctype_makeConj (ctype_char, ctype_uchar); */
1628 /*@switchbreak@*/ break;
1630 case 's': /* string */
1631 expecttype = ctype_string;
1632 /*@switchbreak@*/ break;
1635 while (((key = *(++code)) != ']')
1638 codetext = cstring_appendChar (codetext, key);
1639 fileloc_incColumn (formatloc);
1645 (message ("Bad character set format: %s",
1646 cstring_fromChars (origcode)));
1649 expecttype = ctype_string;
1650 /*@switchbreak@*/ break;
1652 case 'p': /* pointer */
1653 expecttype = ctype_makePointer (ctype_void);
1654 /* need not be defined */
1655 uentry_setDefState (regArg, SS_RELDEF);
1656 sRef_setPosNull (uentry_getSref (regArg),
1659 /*@switchbreak@*/ break;
1661 case 'n': /* pointer to int, modified by call! */
1662 expecttype = ctype_combine (ctype_makePointer (ctype_int), modtype);
1664 uentry_setDefState (regArg, SS_ALLOCATED); /* corresponds to out */
1665 /*@switchbreak@*/ break;
1667 case 'm': /* in a syslog, it doesn't consume an argument */
1668 /* should check we're really doing syslog */
1670 /*@switchbreak@*/ break;
1674 expecttype = ctype_unknown;
1678 message ("Unrecognized format code: %s",
1679 cstring_fromChars (origcode)),
1680 fileloc_isDefined (formatloc)
1681 ? formatloc : g_currentloc);
1683 /*@switchbreak@*/ break;
1686 if (!(exprNode_matchArgType (expecttype, a)))
1688 if (ctype_isVoidPointer (expecttype)
1689 && ctype_isRealAbstract (a->typ)
1690 && (context_getFlag (FLG_ABSTVOIDP)))
1696 if (llgenformattypeerror
1697 (expecttype, exprNode_undefined,
1699 message ("Format argument %d to %q (%%%s) expects "
1702 uentry_getName (fcn),
1705 a->typ, exprNode_unparse (a)),
1708 if (fileloc_isDefined (formatloc)
1709 && context_getFlag (FLG_SHOWCOL))
1712 (cstring_makeLiteral
1713 ("Corresponding format code"),
1720 uentry_setType (regArg, expecttype);
1721 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
1723 if (ctype_equal (expecttype, ctype_string))
1725 exprNode_checkUse (a, sRef_makePointer (a->sref), a->loc);
1728 uentry_setType (regArg, ctype_unknown);
1729 uentry_fixupSref (regArg);
1733 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1744 cstring_free (codetext);
1749 voptgenerror (FLG_TYPE,
1750 message ("Format string for %q has %d arg%&, given %d",
1751 uentry_getName (fcn), i - argno, nargs - argno),
1757 /* no checking possible for compile-time unknown format strings */
1758 if (exprNode_isDefined (a))
1762 message ("Format string parameter to %s is not a compile-time constant: %s",
1763 exprNode_unparse (f),
1764 exprNode_unparse (a)),
1769 fileloc_free (formatloc);
1773 checkScanfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1774 exprNodeList args, exprNode ret, int argno)
1778 int nargs = exprNodeList_size (args);
1779 uentryList params = uentry_getParams (fcn);
1783 ** These should be ensured by checkSpecialFunction
1786 llassert (uentryList_size (params) == argno + 1);
1787 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1789 a = exprNodeList_getN (args, argno - 1);
1790 formatloc = fileloc_copy (exprNode_loc (a));
1792 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1793 && exprNode_knownStringValue (a))
1795 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1796 char *code = format;
1799 nargs = exprNodeList_size (args);
1801 while ((code = strchr (code, '%')) != NULL)
1803 char *origcode = code;
1804 char key = *(++code);
1805 cstring codetext = cstring_newEmpty ();
1806 ctype modtype = ctype_int;
1807 char modifier = '\0';
1808 bool modified = TRUE;
1809 bool ignore = FALSE;
1811 codetext = cstring_appendChar (codetext, key);
1812 fileloc_addColumn (formatloc, code - ocode);
1815 ** this is based on ANSI standard library description of fscanf
1816 ** (from ANSI standard X3.159-1989, 4.9.6.1)
1819 /* '*' suppresses assignment (does not need match argument) */
1824 codetext = cstring_appendChar (codetext, key);
1827 fileloc_incColumn (formatloc);
1830 /* ignore field width */
1831 while (isdigit ((int) key) != 0)
1834 codetext = cstring_appendChar (codetext, key);
1835 fileloc_incColumn (formatloc);
1840 modtype = ctype_sint; /* short */
1842 codetext = cstring_appendChar (codetext, key);
1843 fileloc_incColumn (formatloc);
1845 else if (key == 'l' || key == 'L')
1847 modtype = ctype_lint; /* long */
1851 codetext = cstring_appendChar (codetext, key);
1853 fileloc_incColumn (formatloc);
1855 if (key == 'l' || key == 'L') {
1856 modtype = ctype_llint; /* long long */
1858 codetext = cstring_appendChar (codetext, key);
1859 fileloc_incColumn (formatloc);
1867 /* now, key = type of conversion to apply */
1869 fileloc_incColumn (formatloc);
1883 message ("No argument corresponding to %q format "
1884 "code %d (%%%s): \"%s\"",
1885 uentry_getName (fcn),
1887 cstring_fromChars (format)),
1890 if (fileloc_isDefined (formatloc)
1891 && context_getFlag (FLG_SHOWCOL))
1894 (cstring_makeLiteral ("Corresponding format code"),
1902 a = exprNodeList_getN (args, i);
1905 if (!exprNode_isError (a))
1911 case '*': /* int argument for fieldwidth */
1912 expecttype = ctype_makePointer (ctype_int);
1913 *(--code) = '%'; /* convert it for next code */
1914 fileloc_subColumn (formatloc, 1);
1915 /*@switchbreak@*/ break;
1918 expecttype = ctype_makePointer (ctype_combine (ctype_uint, modtype));
1919 /*@switchbreak@*/ break;
1924 case 'X': /* unsigned int */
1925 expecttype = ctype_makePointer (ctype_combine (ctype_int, modtype));
1926 /*@switchbreak@*/ break;
1933 /* printf is double, scanf is float! */
1935 if (modifier == 'l')
1937 expecttype = ctype_makePointer (ctype_double);
1939 else if (modifier == 'L')
1941 expecttype = ctype_makePointer (ctype_ldouble);
1945 llassert (modifier == '\0');
1946 expecttype = ctype_makePointer (ctype_float);
1948 /*@switchbreak@*/ break;
1950 case 'c': /* int converted to char (check its a char?) */
1951 expecttype = ctype_makePointer (ctype_makeConj (ctype_char, ctype_uchar));
1952 /*@switchbreak@*/ break;
1954 case 's': /* string */
1955 expecttype = ctype_string;
1956 /*@switchbreak@*/ break;
1960 while (((key = *(++code)) != ']')
1963 codetext = cstring_appendChar (codetext, key);
1964 fileloc_incColumn (formatloc);
1970 (message ("Bad character set format: %s",
1971 cstring_fromChars (origcode)));
1974 expecttype = ctype_string;
1975 /*@switchbreak@*/ break;
1978 case 'p': /* pointer */
1981 message ("Format code should not be used in scanf: %s",
1982 cstring_fromChars (origcode)),
1983 fileloc_isDefined (formatloc)
1984 ? formatloc : g_currentloc);
1986 expecttype = ctype_unknown;
1987 /*@switchbreak@*/ break;
1989 case 'n': /* pointer to int, modified by call! */
1990 expecttype = ctype_makePointer (ctype_int);
1991 /*@switchbreak@*/ break;
1994 expecttype = ctype_unknown;
1998 message ("Unrecognized format code: %s",
1999 cstring_fromChars (origcode)),
2000 fileloc_isDefined (formatloc)
2001 ? formatloc : g_currentloc);
2003 /*@switchbreak@*/ break;
2006 if (!(exprNode_matchArgType (expecttype, a)))
2008 if (ctype_isVoidPointer (expecttype)
2009 && ctype_isRealAbstract (a->typ)
2010 && (context_getFlag (FLG_ABSTVOIDP)))
2016 if (llgenformattypeerror
2017 (expecttype, exprNode_undefined,
2019 message ("Format argument %d to %q (%%%s) expects "
2022 uentry_getName (fcn),
2023 codetext, expecttype,
2024 a->typ, exprNode_unparse (a)),
2027 if (fileloc_isDefined (formatloc)
2028 && context_getFlag (FLG_SHOWCOL))
2031 (cstring_makeLiteral
2032 ("Corresponding format code"),
2039 uentry_setType (outArg, expecttype);
2040 checkOneArg (outArg, a, f, FALSE, i+1, nargs);
2041 uentry_setType (outArg, ctype_unknown);
2042 uentry_fixupSref (outArg);
2046 exprNode_checkCallModifyVal (a->sref, args, f, ret);
2051 /* a->sref = sRef_undefined; */
2058 cstring_free (codetext);
2063 voptgenerror (FLG_TYPE,
2064 message ("Format string for %q has %d arg%&, given %d",
2065 uentry_getName (fcn), i - argno, nargs - argno),
2071 /* no checking possible for compile-time unknown format strings */
2074 fileloc_free (formatloc);
2078 checkMessageArgs (/*@notnull@*/ /*@dependent@*/ exprNode f,
2081 /*@unused@*/ int argno)
2084 ** the last argument before the elips is the format string
2087 int nargs = exprNodeList_size (args);
2092 a = exprNodeList_getN (args, argno - 1);
2093 formatloc = fileloc_copy (exprNode_loc (a));
2095 if (ctype_isUnknown (cstringType)) {
2096 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
2098 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
2102 if (ctype_isUnknown (ctypeType)) {
2103 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
2105 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
2109 if (ctype_isUnknown (filelocType)) {
2110 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
2112 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
2116 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
2117 && exprNode_knownStringValue (a))
2119 cstring format = multiVal_forceString (exprNode_getValue (a));
2120 char *code = cstring_toCharsSafe (format);
2123 nargs = exprNodeList_size (args);
2125 while ((code = strchr (code, '%')) != NULL)
2127 char *origcode = code;
2128 char key = *(++code);
2129 cstring codetext = cstring_newEmpty ();
2130 bool isOnly = FALSE;
2132 codetext = cstring_appendChar (codetext, key);
2134 fileloc_addColumn (formatloc, code - ocode);
2136 while (key >= '0' && key <= '9')
2139 codetext = cstring_appendChar (codetext, key);
2140 fileloc_incColumn (formatloc);
2144 fileloc_incColumn (formatloc);
2148 if (key == '&') /* plural marker */
2157 message ("Message missing format arg %d (%%%s): \"%s\"",
2158 i + 1, codetext, format),
2164 a = exprNodeList_getN (args, i);
2168 if (!exprNode_isError (a))
2172 /*@-loopswitchbreak@*/
2178 expecttype = ctype_char; break;
2180 expecttype = cstringType; break;
2182 expecttype = cstringType; isOnly = TRUE; break;
2184 expecttype = cstringType; isOnly = TRUE; break;
2185 case 'd': expecttype = ctype_int; break;
2186 case 'u': expecttype = ctype_uint; break;
2187 case 'w': expecttype = ctype_ulint; break;
2188 case 'f': expecttype = ctype_float; break;
2189 case 'b': expecttype = ctype_bool; break;
2190 case 't': expecttype = ctypeType; break;
2192 expecttype = ctype_makePointer (ctype_void);
2193 /* need not be defined */
2194 uentry_setDefState (regArg, SS_RELDEF);
2195 sRef_setPosNull (uentry_getSref (regArg),
2198 /*@switchbreak@*/ break;
2199 case 'l': expecttype = filelocType; break;
2200 case '&': /* a wee bit of a hack methinks */
2201 expecttype = ctype_int;
2203 case 'r': expecttype = ctype_bool; break;
2205 expecttype = ctype_unknown;
2208 message ("Unrecognized format code: %s",
2209 cstring_fromChars (origcode)),
2210 fileloc_isDefined (formatloc)
2211 ? formatloc : g_currentloc);
2214 /*@=loopswitchbreak@*/
2216 if (!(exprNode_matchArgType (expecttype, a)))
2218 if (ctype_isVoidPointer (expecttype)
2219 && ctype_isRealAbstract (a->typ)
2220 && (context_getFlag (FLG_ABSTVOIDP)))
2226 if (llgenformattypeerror
2227 (expecttype, exprNode_undefined,
2229 message ("Format argument %d to %q (%%%s) expects "
2232 uentry_getName (fcn),
2233 codetext, expecttype,
2234 a->typ, exprNode_unparse (a)),
2237 if (fileloc_isDefined (formatloc)
2238 && context_getFlag (FLG_SHOWCOL))
2241 (cstring_makeLiteral
2242 ("Corresponding format code"),
2249 if (ctype_equal (expecttype, cstringType))
2253 checkOneArg (csOnlyArg, a, f, FALSE, i+1, nargs);
2254 uentry_fixupSref (csOnlyArg);
2258 checkOneArg (csArg, a, f, FALSE, i+1, nargs);
2259 uentry_fixupSref (csArg);
2264 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
2265 uentry_fixupSref (regArg);
2271 cstring_free (codetext);
2276 voptgenerror (FLG_TYPE,
2277 message ("Format string for %q has %d arg%&, given %d",
2278 uentry_getName (fcn), i - argno, nargs -argno),
2284 /* no checking possible for compile-time unknown format strings */
2287 fileloc_free (formatloc);
2291 checkExpressionDefinedAux (/*@notnull@*/ exprNode e1,
2292 /*@notnull@*/ exprNode e2,
2298 bool hadUncon = FALSE;
2300 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e1->sref)) &&
2301 sRefSet_hasUnconstrained (sets2))
2304 (FLG_EVALORDERUNCON,
2306 ("Expression may have undefined behavior (%q used in right operand "
2307 "may set global variable %q used in left operand): %s %s %s",
2308 sRefSet_unparseUnconstrained (sets2),
2309 sRef_unparse (sRef_getRootBase (e1->sref)),
2310 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2314 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e2->sref)) &&
2315 sRefSet_hasUnconstrained (sets1))
2318 (FLG_EVALORDERUNCON,
2320 ("Expression has undefined behavior (%q used in left operand "
2321 "may set global variable %q used in right operand): %s %s %s",
2322 sRefSet_unparseUnconstrained (sets1),
2323 sRef_unparse (e2->sref),
2324 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2328 sRefSet_realElements (e1->uses, sr)
2330 if (sRef_isMeaningful (sr) && sRefSet_member (sets2, sr))
2335 ("Expression has undefined behavior (left operand uses %q, "
2336 "modified by right operand): %s %s %s",
2338 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2341 } end_sRefSet_realElements;
2343 sRefSet_realElements (sets1, sr)
2345 if (sRef_isMeaningful (sr))
2347 if (sRef_same (sr, e2->sref))
2352 ("Expression has undefined behavior (value of right operand "
2353 "modified by left operand): %s %s %s",
2354 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2357 else if (sRefSet_member (e2->uses, sr))
2362 ("Expression has undefined behavior (left operand modifies %q, "
2363 "used by right operand): %s %s %s",
2365 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2370 if (sRefSet_member (sets2, sr))
2372 if (sRef_isUnconstrained (sr))
2380 hadUncon = optgenerror
2381 (FLG_EVALORDERUNCON,
2383 ("Expression may have undefined behavior. Left operand "
2384 "calls %q; right operand calls %q. The unconstrained "
2385 "functions may modify global state used by "
2386 "the other operand): %s %s %s",
2387 sRefSet_unparseUnconstrained (sets1),
2388 sRefSet_unparseUnconstrained (sets2),
2389 exprNode_unparse (e1), lltok_unparse (op),
2390 exprNode_unparse (e2)),
2399 ("Expression has undefined behavior (both "
2400 "operands modify %q): %s %s %s",
2402 exprNode_unparse (e1),
2403 lltok_unparse (op), exprNode_unparse (e2)),
2409 } end_sRefSet_realElements;
2412 static void checkExpressionDefined (exprNode e1, exprNode e2, lltok op)
2414 bool hasError = FALSE;
2416 if (exprNode_isError (e1) || exprNode_isError (e2))
2421 if (sRefSet_member (e2->sets, e1->sref))
2423 if (e2->kind == XPR_CALL)
2429 hasError = optgenerror
2431 message ("Expression has undefined behavior "
2432 "(value of left operand %s is modified "
2433 "by right operand %s): %s %s %s",
2434 exprNode_unparse (e1),
2435 exprNode_unparse (e2),
2436 exprNode_unparse (e1), lltok_unparse (op),
2437 exprNode_unparse (e2)),
2442 if (context_getFlag (FLG_EVALORDERUNCON))
2444 if (sRefSet_member (e2->msets, e1->sref))
2446 if (e2->kind == XPR_CALL)
2452 hasError = optgenerror
2455 ("Expression has undefined behavior (value of left "
2456 "operand may be modified by right operand): %s %s %s",
2457 exprNode_unparse (e1), lltok_unparse (op),
2458 exprNode_unparse (e2)),
2466 checkExpressionDefinedAux (e1, e2, e1->sets, e2->sets, op, FLG_EVALORDER);
2468 if (context_maybeSet (FLG_EVALORDERUNCON))
2470 checkExpressionDefinedAux (e1, e2, e1->msets,
2471 e2->msets, op, FLG_EVALORDERUNCON);
2476 static void checkSequencing (exprNode p_f, exprNodeList p_args);
2479 checkArgsReal (uentry fcn, /*@dependent@*/ exprNode f, uentryList cl,
2480 exprNodeList args, bool isIter, exprNode ret)
2484 if (!exprNode_isError (f))
2486 if (!uentryList_isMissingParams (cl))
2488 int nargs = exprNodeList_size (args);
2489 int expectargs = uentryList_size (cl);
2493 if (expectargs == 0)
2501 message ("Iter %q invoked with %d args, "
2503 uentry_getName (fcn),
2511 message ("Function %s called with %d args, "
2513 exprNode_unparse (f), nargs),
2520 last = uentry_getType (uentryList_getN (cl, expectargs - 1));
2522 exprNodeList_reset (args);
2524 uentryList_elements (cl, current)
2526 ctype ct = uentry_getType (current);
2529 if (ctype_isElips (ct))
2532 ** do special checking for printf/scanf library functions
2534 ** this is kludgey code, just for handling the special case
2538 if (uentry_isPrintfLike (fcn))
2540 checkPrintfArgs (f, fcn, args, ret, i);
2543 else if (uentry_isScanfLike (fcn))
2545 checkScanfArgs (f, fcn, args, ret, i);
2548 else if (uentry_isMessageLike (fcn))
2550 checkMessageArgs (f, fcn, args, i);
2555 llassert (!uentry_isSpecialFunction (fcn));
2558 nargs = expectargs; /* avoid errors */
2563 if (i >= nargs) break;
2565 a = exprNodeList_current (args);
2566 exprNodeList_advance (args);
2570 if (exprNode_isError (a))
2577 probably necessary? I'm not sure about this one
2578 checkMacroParen (a);
2581 f->guards = guardSet_union (f->guards, a->guards);
2583 DPRINTF (("match arg: %s / %s", ctype_unparse (ct), ctype_unparse (a->typ)));
2585 if (!(exprNode_matchArgType (ct, a)))
2587 DPRINTF (("Args mismatch!"));
2589 if (ctype_isVoidPointer (ct)
2590 && (ctype_isPointer (a->typ)
2591 && (ctype_isRealAbstract (ctype_baseArrayPtr (a->typ)))))
2596 ("Pointer to abstract type (%t) used "
2598 "(arg %d to %q): %s",
2600 uentry_getName (fcn),
2601 exprNode_unparse (a)),
2609 (ct, exprNode_undefined,
2612 ("Iter %q expects arg %d to "
2613 "be %t gets %t: %s",
2614 uentry_getName (fcn),
2615 i, ct, a->typ, exprNode_unparse (a)),
2626 ("Function %q expects arg %d to be %t gets %t: %s",
2627 uentry_getName (fcn),
2628 i, ct, a->typ, exprNode_unparse (a)),
2631 DPRINTF (("Types: %s / %s",
2633 ctype_unparse (a->typ)));
2637 ** Clear null marker for abstract types.
2638 ** (It is not revealed, so suppress future messages.)
2641 if (ctype_isAbstract (a->typ))
2643 sRef_setNullUnknown (exprNode_getSref (a), a->loc);
2650 } end_uentryList_elements ;
2653 if (expectargs != nargs) /* note: not != since we may have ... */
2655 if (ctype_isElips (last))
2659 message ("Function %s called with %d args, expects at least %d",
2660 exprNode_unparse (f),
2661 nargs, expectargs - 1),
2670 message ("Iter %q invoked with %d args, expects %d",
2671 uentry_getName (fcn), nargs, expectargs),
2678 message ("Function %s called with %d args, expects %d",
2679 exprNode_unparse (f),
2692 ** Check for undefined code sequences in function arguments:
2694 ** one parameter sets something used by another parameter
2695 ** one parameter sets something set by another parameter
2699 checkSequencingOne (exprNode f, exprNodeList args,
2700 /*@notnull@*/ exprNode el, int argno)
2703 ** Do second loop, iff +undefunspec
2707 int numloops = context_maybeSet (FLG_EVALORDERUNCON) ? 2 : 1;
2709 for (checkloop = 0; checkloop < numloops; checkloop++)
2715 thissets = el->sets;
2719 llassert (checkloop == 1);
2720 thissets = el->msets;
2723 sRefSet_realElements (thissets, thisset)
2727 /*@access exprNodeList@*/
2728 for (j = 0; j < args->nelements; j++)
2730 exprNode jl = args->elements[j];
2731 int thisargno = j + 1;
2733 if (thisargno != argno && exprNode_isDefined (jl))
2735 sRefSet otheruses = jl->uses;
2737 if (sRef_isFileOrGlobalScope (sRef_getRootBase (jl->sref)) &&
2738 sRefSet_hasUnconstrained (thissets))
2741 (FLG_EVALORDERUNCON,
2744 ("%q used in argument %d may set "
2745 "global variable %q used by argument %d: %s(%q)",
2746 cstring_capitalizeFree (sRefSet_unparseUnconstrained (thissets)),
2749 sRef_unparse (sRef_getRootBase (jl->sref)),
2751 exprNode_unparse (f), exprNodeList_unparse (args)),
2755 if (sRefSet_member (otheruses, thisset))
2757 if (sRef_isUnconstrained (thisset))
2760 (FLG_EVALORDERUNCON,
2762 ("Unconstrained functions used in arguments %d (%q) "
2763 "and %d (%s) may modify "
2764 "or use global state in undefined way: %s(%q)",
2766 sRefSet_unparseUnconstrainedPlain (otheruses),
2768 sRef_unconstrainedName (thisset),
2769 exprNode_unparse (f),
2770 exprNodeList_unparse (args)),
2778 ("Argument %d modifies %q, used by argument %d "
2779 "(order of evaluation of actual parameters is "
2780 "undefined): %s(%q)",
2781 argno, sRef_unparse (thisset), thisargno,
2782 exprNode_unparse (f), exprNodeList_unparse (args)),
2788 sRefSet othersets = jl->sets;
2790 if (sRefSet_member (othersets, thisset))
2792 if (sRef_isUnconstrained (thisset))
2795 (FLG_EVALORDERUNCON,
2797 ("Unconstrained functions used in "
2798 "arguments %d (%q) and %d (%s) may modify "
2799 "or use global state in undefined way: %s(%q)",
2801 sRefSet_unparseUnconstrainedPlain (othersets),
2803 sRef_unconstrainedName (thisset),
2804 exprNode_unparse (f), exprNodeList_unparse (args)),
2812 ("Argument %d modifies %q, set by argument %d (order of"
2813 " evaluation of actual parameters is undefined): %s(%q)",
2814 argno, sRef_unparse (thisset), thisargno,
2815 exprNode_unparse (f), exprNodeList_unparse (args)),
2822 /*@noaccess exprNodeList@*/
2823 } end_sRefSet_realElements;
2828 checkSequencing (exprNode f, exprNodeList args)
2830 if (exprNodeList_size (args) > 1)
2835 /*@access exprNodeList*/
2837 for (i = 0; i < args->nelements; i++)
2839 el = args->elements[i];
2841 if (!exprNode_isError (el))
2843 checkSequencingOne (f, args, el, i + 1);
2846 /*@noaccess exprNodeList*/
2851 ** requires le = exprNode_getUentry (f)
2855 checkGlobMods (/*@notnull@*/ /*@dependent@*/ exprNode f,
2856 uentry le, exprNodeList args,
2857 /*@notnull@*/ exprNode ret, int specialArgs)
2859 bool isSpec = FALSE;
2860 bool hasMods = FALSE;
2862 globSet usesGlobs = globSet_undefined;
2863 sRefSet mods = sRefSet_undefined;
2864 bool freshMods = FALSE;
2865 uentryList params = uentryList_undefined;
2867 DPRINTF (("Check glob mods: %s", exprNode_unparse (ret)));
2870 ** check globals and modifies
2875 if (!uentry_isValid (le))
2877 ctype fr = ctype_realType (f->typ);
2879 if (ctype_isFunction (fr))
2881 params = ctype_argsFunction (fr);
2885 params = uentryList_missingParams;
2888 if (!context_getFlag (FLG_MODNOMODS)
2889 && !context_getFlag (FLG_GLOBUNSPEC))
2891 checkUnspecCall (f, params, args);
2897 fname = uentry_rawName (le);
2901 if (uentry_isFunction (le))
2903 params = uentry_getParams (le);
2904 mods = uentry_getMods (le);
2905 hasMods = uentry_hasMods (le);
2906 usesGlobs = uentry_getGlobs (le);
2907 isSpec = uentry_isSpecified (le);
2909 else /* not a function */
2911 ctype ct = ctype_realType (uentry_getType (le));
2913 llassertprint (uentry_isVar (le) && ctype_isFunction (ct),
2914 ("checkModGlobs: uentry not a function: %s",
2915 uentry_unparse (le)));
2917 params = ctype_argsFunction (ct);
2918 return; /*@32 ! remove this? */
2927 globSet_allElements (usesGlobs, el)
2929 if (sRef_isValid (el))
2931 if (sRef_isInternalState (el) || sRef_isSystemState (el))
2933 context_usedGlobal (el);
2934 exprNode_checkUse (f, el, f->loc);
2936 if (context_checkInternalUse ())
2938 if (!context_globAccess (el))
2940 if (sRef_isSystemState (el)
2941 && !context_getFlag (FLG_MODFILESYSTEM))
2950 ("Called procedure %s may access %q, but "
2951 "globals list does not include globals %s",
2952 exprNode_unparse (f),
2954 cstring_makeLiteralTemp (sRef_isInternalState (el)
2962 else if (sRef_isNothing (el) || sRef_isSpecState (el))
2968 uentry gle = sRef_getUentry (el);
2969 sRef sr = sRef_updateSref (el);
2971 if (sRef_isUndefGlob (el))
2973 sRef_setDefined (sr, f->loc);
2974 exprNode_checkSet (f, sr);
2982 if (sRef_isAllocated (el))
2984 exprNode_checkSet (f, sr);
2988 if (sRef_isStateUndefined (sr))
2993 ("%s %q used by function undefined before call: %s",
2994 sRef_getScopeName (sr),
2996 exprNode_unparse (f)),
2998 sRef_setDefined (sr, f->loc);
3000 exprNode_checkUse (f, sr, f->loc);
3003 checkGlobUse (gle, TRUE, f);
3006 if (sRef_isKilledGlob (el))
3008 sRef_kill (sr, f->loc);
3009 context_usedGlobal (sr);
3013 } end_globSet_allElements;
3019 if (context_hasMods () || context_getFlag (FLG_MODNOMODS))
3021 sRefSet smods = sRefSet_undefined;
3024 ** NEED to check for modifies anything
3028 ** check each sRef that called function modifies (ml), is
3034 sRefSet_allElements (mods, s) /* s is something which may be modified */
3036 DPRINTF (("Check modify: %s", sRef_unparse (s)));
3038 if (sRef_isKindSpecial (s))
3040 if (sRef_isSpecInternalState (s))
3042 if (context_getFlag (FLG_MODINTERNALSTRICT))
3044 exprNode_checkCallModifyVal (s, args, f, ret);
3048 sRefSet mmods = context_modList ();
3050 sRefSet_allElements (mmods, el)
3052 if (sRef_isInternalState (el))
3054 sRef_setModified (el);
3056 } end_sRefSet_allElements ;
3061 exprNode_checkCallModifyVal (s, args, f, ret);
3066 sRef rb = sRef_getRootBase (s);
3068 if (sRef_isFileOrGlobalScope (rb))
3070 context_usedGlobal (rb);
3073 if (sRef_isFileStatic (s)
3074 && !fileId_equal (fileloc_fileId (f->loc),
3075 fileloc_fileId (uentry_whereDefined (le))))
3077 smods = sRefSet_insert (smods, s);
3081 exprNode_checkCallModifyVal (s, args, f, ret);
3084 } end_sRefSet_allElements;
3089 ** Static elements in modifies set can have nasty consequences.
3090 ** (I think...have not been able to reproduce a possible bug.)
3093 if (!sRefSet_isDefined (smods))
3095 mods = sRefSet_newCopy (mods);
3098 sRefSet_allElements (smods, el)
3100 bool res = sRefSet_delete (mods, el);
3103 } end_sRefSet_allElements;
3105 sRefSet_free (smods);
3110 else if (sRefSet_isDefined (mods))
3111 { /* just check observers */
3114 sRefSet_allElements (mods, s) /* s is something which may be modified */
3116 sRef rb = sRef_getRootBase (s);
3120 if (sRef_isParam (rb))
3122 sRef b = sRef_fixBaseParam (s, args);
3124 if (sRef_isObserver (b))
3126 exprNode e = exprNodeList_nth (args, sRef_getParam (rb));
3130 message ("Function call may modify observer%q: %s",
3131 sRef_unparsePreOpt (b), exprNode_unparse (e)),
3134 sRef_showExpInfo (b);
3138 } end_sRefSet_allElements;
3142 if (!hasMods) /* no specified modifications */
3144 if (context_getFlag (FLG_MODOBSERVERUNCON))
3146 exprNodeList_elements (args, e)
3148 if (exprNode_isDefined (e))
3150 sRef s = exprNode_getSref (e);
3152 if (sRef_isObserver (s)
3153 && ctype_isMutable (sRef_getType (s)))
3156 (FLG_MODOBSERVERUNCON,
3158 ("Call to unconstrained function %s may modify observer%q: %s",
3159 exprNode_unparse (f),
3160 sRef_unparsePreOpt (s), exprNode_unparse (e)),
3163 sRef_showExpInfo (s);
3167 } end_exprNodeList_elements;
3172 checkAnyCall (f, fname, params, args, hasMods, mods, isSpec, specialArgs);
3174 ret->uses = sRefSet_union (ret->uses, f->uses);
3175 ret->sets = sRefSet_union (ret->sets, f->sets);
3176 ret->msets = sRefSet_union (ret->msets, f->msets);
3181 ** Spurious errors reported, because splint can't tell
3182 ** mods must be fresh if freshMods is true.
3185 /*@i@*/ sRefSet_free (mods);
3191 void checkGlobUse (uentry glob, bool isCall, /*@notnull@*/ exprNode e)
3193 if (uentry_isVar (glob))
3195 if (context_inFunctionLike ())
3197 sRef sr = uentry_getSref (glob);
3199 context_usedGlobal (sr);
3201 if (context_checkGlobUse (glob))
3203 if (!context_globAccess (sr))
3209 message ("Called procedure %s may access %s %q",
3210 exprNode_unparse (e),
3211 sRef_unparseScope (sr),
3212 uentry_getName (glob)),
3219 message ("Undocumented use of %s %s",
3220 sRef_unparseScope (sr),
3221 exprNode_unparse (e)),
3230 llbug (message ("Global not variable: %q", uentry_unparse (glob)));
3235 reflectEnsuresClause (exprNode ret, uentry le, exprNode f, exprNodeList args)
3237 DPRINTF (("Reflect ensures clause: %s(%s) / %s / %s",
3238 exprNode_unparse (f), exprNodeList_unparse (args),
3239 uentry_unparseFull (le),
3240 stateClauseList_unparse (uentry_getStateClauseList (le))));
3242 if (uentry_isValid (le) && uentry_isFunction (le))
3244 stateClauseList sclauses = uentry_getStateClauseList (le);
3246 if (stateClauseList_isDefined (sclauses))
3248 DPRINTF (("Reflect ensures: %s / %s / %s",
3249 uentry_unparse (le),
3250 exprNode_unparse (f), exprNodeList_unparse (args)));
3252 stateClauseList_elements (sclauses, cl)
3254 if (stateClause_hasEnsures (cl))
3256 /* Same in usymtab.c:1904 */
3257 if (stateClause_setsMetaState (cl))
3259 qual q = stateClause_getMetaQual (cl);
3260 annotationInfo ainfo = qual_getAnnotationInfo (q);
3261 metaStateInfo minfo = annotationInfo_getState (ainfo);
3262 cstring key = metaStateInfo_getName (minfo);
3263 int mvalue = annotationInfo_getValue (ainfo);
3265 sRefSet osrs = sRefSet_undefined;
3268 if (stateClause_isGlobal (cl))
3270 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3275 srs = stateClause_getRefs (cl);
3278 DPRINTF (("Reflect ensures clause: %s", stateClause_unparse (cl)));
3281 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
3283 sRefSet_elements (srs, sel)
3287 if (sRef_isResult (sRef_getRootBase (sel)))
3289 s = exprNode_getSref (ret);
3293 s = sRef_fixBaseParam (sel, args);
3296 DPRINTF (("Reflecting state clause on: %s / %s",
3297 sRef_unparse (sel), sRef_unparse (s)));
3299 sRef_setMetaStateValueComplete (s, key, mvalue, exprNode_loc (f));
3300 } end_sRefSet_elements;
3302 sRefSet_free (osrs);
3306 sRefSet srs = stateClause_getRefs (cl);
3307 sRefModVal modf = stateClause_getEnsuresFunction (cl);
3308 int eparam = stateClause_getStateParameter (cl);
3310 llassert (modf != NULL);
3312 DPRINTF (("Reflect after clause: %s / %s",
3313 stateClause_unparse (cl),
3314 sRefSet_unparse (srs)));
3316 sRefSet_elements (srs, sel)
3320 DPRINTF (("elements: %s", sRef_unparse (sel)));
3321 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3323 if (sRef_isResult (sRef_getRootBase (sel)))
3325 DPRINTF (("Fix base: %s / %s",
3326 sRef_unparse (sel), sRef_unparse (exprNode_getSref (ret))));
3327 s = sRef_fixBase (sel, exprNode_getSref (ret));
3328 DPRINTF (("==> %s", sRef_unparseFull (s)));
3332 s = sRef_fixBaseParam (sel, args);
3335 DPRINTF (("elements: %s", sRef_unparse (s)));
3336 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3338 DPRINTF (("Reflecting state clause on: %s / %s",
3339 sRef_unparseFull (sel), sRef_unparseFull (s)));
3341 /* evans 2001-08-24 - added aliasSetCompleteParam */
3342 sRef_aliasSetCompleteParam (modf, s, eparam, exprNode_loc (f));
3344 DPRINTF (("After reflecting state clause on: %s / %s",
3345 sRef_unparseFull (sel), sRef_unparseFull (s)));
3346 } end_sRefSet_elements;
3349 } end_stateClauseList_elements ;
3352 DPRINTF (("Here: %s / %s",
3353 uentry_unparseFull (le),
3354 bool_unparse (uentry_hasMetaStateEnsures (le))));
3356 if (uentry_hasMetaStateEnsures (le))
3358 fileloc loc = exprNode_loc (f);
3360 metaStateConstraintList mscl = uentry_getMetaStateEnsures (le);
3362 metaStateConstraintList_elements (mscl, msc)
3364 metaStateSpecifier msspec = metaStateConstraint_getSpecifier (msc);
3365 metaStateInfo msinfo = metaStateSpecifier_getMetaStateInfo (msspec);
3366 metaStateExpression msexpr = metaStateConstraint_getExpression (msc);
3367 cstring key = metaStateInfo_getName (msinfo);
3368 sRef mlsr = metaStateSpecifier_getSref (msspec);
3370 sRef lastref = sRef_undefined;
3371 stateValue sval = stateValue_undefined;
3373 DPRINTF (("Meta state constraint for %s: %s", uentry_unparse (le),
3374 metaStateConstraint_unparse (msc)));
3375 DPRINTF (("Matches left: %s", sRef_unparseDebug (mlsr)));
3377 if (sRef_isResult (sRef_getRootBase (mlsr)))
3379 s = exprNode_getSref (ret);
3383 s = sRef_fixBaseParam (mlsr, args);
3386 DPRINTF (("Setting state: %s", sRef_unparseFull (s)));
3388 while (metaStateExpression_isDefined (msexpr))
3390 metaStateSpecifier ms = metaStateExpression_getSpecifier (msexpr);
3391 metaStateInfo msi = metaStateSpecifier_getMetaStateInfo (ms);
3394 DPRINTF (("Check expression: %s", metaStateExpression_unparse (msexpr)));
3396 if (metaStateExpression_isMerge (msexpr))
3398 msexpr = metaStateExpression_getRest (msexpr);
3402 msexpr = metaStateExpression_undefined;
3405 if (metaStateInfo_isDefined (msi))
3407 /* Must match lhs state */
3408 llassert (metaStateInfo_equal (msinfo, msi));
3411 if (metaStateSpecifier_isElipsis (ms))
3414 ** For elipsis, we need to merge all the relevant elipsis parameters
3418 uentryList params = uentry_getParams (le);
3419 int paramno = uentryList_size (params) - 1;
3421 if (!uentry_isElipsisMarker (uentryList_getN (params, paramno)))
3425 message ("Ensures clauses uses ... for function without ... in parameter list: %q",
3426 uentry_getName (le)),
3427 uentry_whereLast (le));
3428 /*@innerbreak@*/ break;
3431 while (paramno < exprNodeList_size (args))
3433 exprNode arg = exprNodeList_getN (args, paramno);
3434 fs = exprNode_getSref (arg);
3435 DPRINTF (("Merge arg: %s", exprNode_unparse (arg)));
3437 /* cut and pasted... gack*/
3438 if (stateValue_isDefined (sval))
3440 /* Use combination table to merge old state value with new one: */
3441 stateValue tval = sRef_getMetaStateValue (fs, key);
3443 if (stateValue_isDefined (tval))
3445 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3446 cstring msg = cstring_undefined;
3447 int nval = stateCombinationTable_lookup (sctable,
3448 stateValue_getValue (sval),
3449 stateValue_getValue (tval),
3451 DPRINTF (("Combining: %s + %s -> %d",
3452 stateValue_unparseValue (sval, msinfo),
3453 stateValue_unparseValue (tval, msinfo),
3456 if (nval == stateValue_error)
3461 ("Attributes merged in ensures clause in states that "
3462 "cannot be combined (%q is %q, %q is %q)%q",
3463 sRef_unparse (lastref),
3464 stateValue_unparseValue (sval, msinfo),
3466 stateValue_unparseValue (tval, msinfo),
3467 cstring_isDefined (msg) ?
3468 message (": %s", msg) : cstring_undefined),
3471 sRef_showMetaStateInfo (fs, key);
3475 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3476 loc = exprNode_loc (arg);
3480 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3485 sval = sRef_getMetaStateValue (fs, key);
3490 if (stateValue_isError (sval))
3492 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3501 msr = metaStateSpecifier_getSref (ms);
3504 llassert (sRef_isParam (sRef_getRootBase (msr)));
3505 fs = sRef_fixBaseParam (msr, args);
3507 if (stateValue_isDefined (sval))
3509 /* Use combination table to merge old state value with new one: */
3510 stateValue tval = sRef_getMetaStateValue (fs, key);
3512 if (stateValue_isDefined (tval))
3514 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3515 cstring msg = cstring_undefined;
3516 int nval = stateCombinationTable_lookup (sctable,
3517 stateValue_getValue (sval),
3518 stateValue_getValue (tval),
3520 DPRINTF (("Combining: %s + %s -> %d",
3521 stateValue_unparseValue (sval, msinfo),
3522 stateValue_unparseValue (tval, msinfo),
3525 if (nval == stateValue_error)
3530 ("Attributes merged in ensures clause in states that "
3531 "cannot be combined (%q is %q, %q is %q)%q",
3532 sRef_unparse (lastref),
3533 stateValue_unparseValue (sval, msinfo),
3535 stateValue_unparseValue (tval, msinfo),
3536 cstring_isDefined (msg)
3537 ? message (": %s", msg) : cstring_undefined),
3540 sRef_showMetaStateInfo (fs, key);
3544 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3548 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3553 sval = sRef_getMetaStateValue (fs, key);
3558 if (stateValue_isError (sval))
3560 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3565 DPRINTF (("Setting: %s:%s <- %s", sRef_unparse (s), key, stateValue_unparse (sval)));
3567 if (stateValue_isDefined (sval))
3569 sRef_setMetaStateValueComplete (s, key, stateValue_getValue (sval), loc);
3573 DPRINTF (("Undefined state: %s", cstring_toCharsSafe (sRef_unparse (s))));
3575 } end_metaStateConstraintList_elements ;
3577 metaStateConstraintList_free (mscl);
3583 checkRequiresClause (uentry le, exprNode f, exprNodeList args)
3585 DPRINTF (("Check requires clause: %s(%s) / %s / %s",
3586 exprNode_unparse (f), exprNodeList_unparse (args),
3587 uentry_unparseFull (le),
3588 stateClauseList_unparse (uentry_getStateClauseList (le))));
3590 if (uentry_isValid (le) && uentry_isFunction (le))
3592 stateClauseList sclauses = uentry_getStateClauseList (le);
3594 if (stateClauseList_isDefined (sclauses))
3596 DPRINTF (("Check requires: %s / %s / %s",
3597 uentry_unparse (le),
3598 exprNode_unparse (f), exprNodeList_unparse (args)));
3600 stateClauseList_elements (sclauses, cl)
3602 DPRINTF (("Check clause: %s / %s",
3603 stateClause_unparse (cl),
3604 bool_unparse (stateClause_hasRequires (cl))));
3606 if (stateClause_hasRequires (cl))
3608 sRefSet osrs = sRefSet_undefined;
3611 if (stateClause_isGlobal (cl))
3613 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3618 srs = stateClause_getRefs (cl);
3621 DPRINTF (("Refs: %s", sRefSet_unparse (srs)));
3623 if (stateClause_setsMetaState (cl))
3625 qual q = stateClause_getMetaQual (cl);
3626 annotationInfo ainfo = qual_getAnnotationInfo (q);
3627 metaStateInfo minfo = annotationInfo_getState (ainfo);
3628 cstring key = metaStateInfo_getName (minfo);
3629 int mvalue = annotationInfo_getValue (ainfo);
3631 DPRINTF (("Requires meta state! %s = %d", key, mvalue));
3633 sRefSet_elements (srs, sel)
3635 sRef s = sRef_fixBaseParam (sel, args);
3637 if (sRef_isResult (sRef_getRootBase (sel)))
3643 DPRINTF (("Checking state clause on: %s / %s / %s = %d",
3644 sRef_unparseFull (sel), sRef_unparseFull (s),
3647 if (!sRef_checkMetaStateValue (s, key, mvalue))
3649 DPRINTF (("HERE: %s", sRef_unparse (s)));
3653 ("Requires clause of called function %q not satisfied%q (state is %q): %q",
3654 uentry_getName (le),
3655 sRef_isGlobalMarker (s)
3657 : message (" by %q", sRef_unparse (s)),
3658 stateValue_unparseValue (sRef_getMetaStateValue (s, key),
3660 stateClause_unparse (cl)),
3663 sRef_showAliasInfo (s);
3667 DPRINTF (("Error supressed!"));
3668 DPRINTF (("Loc: %s", fileloc_unparse (exprNode_loc (f))));
3669 DPRINTF (("Context supress: %s",
3670 bool_unparse (context_suppressFlagMsg (FLG_STATETRANSFER, exprNode_loc (f)))));
3674 } end_sRefSet_elements;
3678 sRefModVal modf = stateClause_getRequiresBodyFunction (cl);
3679 int eparam = stateClause_getStateParameter (cl);
3681 DPRINTF (("Reflect after clause: %s / %s",
3682 stateClause_unparse (cl),
3683 sRefSet_unparse (srs)));
3685 llassert (modf != NULL);
3687 sRefSet_elements (srs, sel)
3691 DPRINTF (("elements: %s", sRef_unparse (sel)));
3692 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3694 s = sRef_fixBaseParam (sel, args);
3696 DPRINTF (("elements: %s", sRef_unparse (s)));
3697 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3699 if (sRef_isResult (sRef_getRootBase (sel)))
3701 ; /*@i423 what do we do about results */
3705 DPRINTF (("Reflecting state clause on: %s / %s",
3706 sRef_unparse (sel), sRef_unparse (s)));
3708 modf (s, eparam, exprNode_loc (f));
3710 } end_sRefSet_elements;
3713 sRefSet_free (osrs);
3715 } end_stateClauseList_elements ;
3720 static /*@only@*/ exprNode
3721 functionCallSafe (/*@only@*/ /*@notnull@*/ exprNode f,
3722 ctype t, /*@keep@*/ exprNodeList args)
3724 /* requires f is a non-error exprNode, with type function */
3725 cstring fname = exprNode_unparse (f);
3726 uentry le = exprNode_getUentry (f);
3727 exprNode ret = exprNode_createPartialCopy (f);
3732 DPRINTF (("Call: %s %s",exprNode_unparse (f), exprNodeList_unparse (args)));
3734 ret->typ = ctype_getReturnType (t);
3735 ret->kind = XPR_CALL;
3737 ret->edata = exprData_makeCall (f, args);
3740 ** Order of these steps is very important!
3742 ** Must check for argument dependencies before messing up uses and sets.
3745 if (context_getFlag (FLG_EVALORDER))
3747 exprNodeList_elements (args, current)
3749 if (exprNode_isDefined (current))
3751 exprNode_addUse (current, current->sref);
3753 } end_exprNodeList_elements;
3755 if (context_maybeSet (FLG_EVALORDER) || context_maybeSet (FLG_EVALORDERUNCON))
3757 checkSequencing (f, args);
3760 exprNodeList_elements (args, current)
3762 if (exprNode_isDefined (current) && sRef_isMeaningful (current->sref))
3764 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3766 } end_exprNodeList_elements ;
3769 special = checkArgs (le, f, t, args, ret);
3770 checkGlobMods (f, le, args, ret, special);
3771 checkRequiresClause (le, f, args);
3774 if (uentry_isValid (le)
3775 && (uentry_isFunction (le)
3776 || (uentry_isVariable (le)
3777 && ctype_isFunction (uentry_getType (le)))))
3779 exitkind exk = uentry_getExitCode (le);
3781 /* f->typ is already set to the return type */
3783 DPRINTF (("Function: %s", uentry_unparseFull (le)));
3784 ret->sref = uentry_returnedRef (le, args, exprNode_loc (f));
3785 DPRINTF (("Returned: %s / %s",
3786 uentry_unparseFull (le),
3787 sRef_unparseFull (ret->sref)));
3789 if (uentry_isFunction (le) && exprNodeList_size (args) >= 1)
3791 qual nullPred = uentry_nullPred (le);
3793 if (qual_isTrueNull (nullPred))
3795 exprNode arg = exprNodeList_head (args);
3797 if (exprNode_isDefined (arg))
3799 ret->guards = guardSet_addFalseGuard (ret->guards, arg->sref);
3802 else if (qual_isFalseNull (nullPred))
3804 exprNode arg = exprNodeList_head (args);
3806 if (exprNode_isDefined (arg))
3808 ret->guards = guardSet_addTrueGuard (ret->guards, arg->sref);
3813 llassert (qual_isUnknown (nullPred));
3817 if (exitkind_isConditionalExit (exk))
3821 ** if (arg0) then { exit! } else { ; }
3823 ** if (arg0) then { ; } else { exit! }
3828 llassert (!exprNodeList_isEmpty (args));
3829 firstArg = exprNodeList_head (args);
3831 if (exprNode_isDefined (firstArg)
3832 && !guardSet_isEmpty (firstArg->guards))
3834 usymtab_trueBranch (guardSet_undefined);
3835 usymtab_altBranch (guardSet_undefined);
3837 if (exitkind_isTrueExit (exk))
3839 usymtab_popBranches (firstArg,
3840 exprNode_makeMustExit (),
3842 TRUE, TRUEEXITCLAUSE);
3846 usymtab_popBranches (firstArg,
3848 exprNode_makeMustExit (),
3849 TRUE, FALSEEXITCLAUSE);
3853 ret->exitCode = XK_MAYEXIT;
3855 else if (exitkind_mustExit (exk))
3857 ret->exitCode = XK_MUSTEXIT;
3859 else if (exitkind_couldExit (exk))
3861 ret->exitCode = XK_MAYEXIT;
3868 if (cstring_equalLit (fname, "exit"))
3870 if (exprNodeList_size (args) == 1)
3872 exprNode arg = exprNodeList_head (args);
3874 if (exprNode_isDefined (arg) && exprNode_knownIntValue (arg))
3876 long int val = multiVal_forceInt (exprNode_getValue (arg));
3883 ("Argument to exit has implementation defined behavior: %s",
3884 exprNode_unparse (arg)),
3885 exprNode_loc (arg));
3893 ret->sref = sRef_undefined;
3894 exprNode_checkSetAny (ret, uentry_rawName (le));
3897 DPRINTF (("Before reflect: %s", sRef_unparseFull (ret->sref)));
3898 DPRINTF (("Reflect: %s", uentry_unparseFull (le)));
3899 reflectEnsuresClause (ret, le, f, args);
3902 DPRINTF (("Here: %s", sRef_unparseFull (ret->sref)));
3907 ** this is yucky! should keep the uentry as part of exprNode!
3910 uentry exprNode_getUentry (exprNode e)
3912 if (exprNode_isError (e))
3914 return uentry_undefined;
3918 cstring s = exprNode_rootVarName (e);
3919 uentry ue = usymtab_lookupSafe (s);
3926 ** Returns true iff e1 and e2 are both exactly the same storage
3930 static bool exprNode_sameStorage (exprNode e1, exprNode e2)
3932 sRef s1 = exprNode_getSref (e1);
3933 sRef s2 = exprNode_getSref (e2);
3935 return (sRef_realSame (s1, s2));
3939 exprNode_makeInitBlock (lltok brace, /*@only@*/ exprNodeList inits)
3941 exprNode ret = exprNode_createPlain (ctype_unknown);
3943 ret->kind = XPR_INITBLOCK;
3944 ret->edata = exprData_makeCall (exprNode_undefined, inits);
3945 ret->loc = fileloc_update (ret->loc, lltok_getLoc (brace));
3951 exprNode_functionCall (/*@only@*/ exprNode f, /*@only@*/ exprNodeList args)
3956 usymtab_checkAllValid ();
3959 if (exprNode_isUndefined (f))
3962 exprNodeList_free (args);
3963 return exprNode_undefined;
3966 t = exprNode_getType (f);
3968 if (sRef_isLocalVar (f->sref))
3970 exprNode_checkUse (f, f->sref, f->loc);
3972 if (sRef_possiblyNull (f->sref))
3974 if (!usymtab_isGuarded (f->sref))
3976 if (optgenerror (FLG_NULLDEREF,
3977 message ("Function call using %s pointer %q",
3978 sRef_nullMessage (f->sref),
3979 sRef_unparse (f->sref)),
3982 sRef_showNullInfo (f->sref);
3983 sRef_setNullError (f->sref);
3991 if (ctype_isRealFunction (t))
3993 exprNode ret = functionCallSafe (f, t, args);
3997 else if (ctype_isUnknown (t))
3999 exprNode ret = exprNode_createPartialCopy (f);
4005 exprNodeList_elements (args, current)
4007 if (exprNode_isDefined (current))
4009 exprNode_checkUse (ret, current->sref, ret->loc);
4012 ** also, anything derivable from current->sref may be used
4015 exprNode_addUse (ret, sRef_makeDerived (current->sref));
4016 exprNode_mergeUSs (ret, current);
4018 } end_exprNodeList_elements;
4020 ret->edata = exprData_makeCall (f, args);
4021 ret->kind = XPR_CALL;
4023 tstring = cstring_copy (exprNode_unparse (f));
4025 cstring_markOwned (tstring);
4026 exprNode_checkSetAny (ret, tstring);
4032 voptgenerror (FLG_TYPE,
4033 message ("Call to non-function (type %t): %s", t,
4034 exprNode_unparse (f)),
4037 exprNodeList_free (args);
4039 return (exprNode_makeError ());
4044 exprNode_fieldAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
4045 /*@only@*/ cstring f)
4047 exprNode ret = exprNode_createPartialCopy (s);
4049 ret->kind = XPR_FACCESS;
4051 if (exprNode_isError (s))
4053 ret->edata = exprData_makeField (s, f);
4058 ctype t = exprNode_getType (s);
4059 ctype tr = ctype_realType (t);
4061 checkMacroParen (s);
4063 ret->edata = exprData_makeField (s, f);
4065 if (ctype_isStructorUnion (tr))
4067 uentry tf = uentryList_lookupField (ctype_getFields (tr), f);
4069 if (uentry_isUndefined (tf))
4071 voptgenerror (FLG_TYPE,
4072 message ("Access non-existent field %s of %t: %s", f, t,
4073 exprNode_unparse (ret)),
4075 /*! cstring_free (f); */ /* evans 2001-03-25 self-detect */
4080 uentry_setUsed (tf, exprNode_loc (ret));
4082 ret->typ = uentry_getType (tf);
4083 checkSafeUse (ret, s->sref);
4085 ret->sref = sRef_makeField (s->sref, uentry_rawName (tf));
4086 /*!? exprNode_free (s); */ /* evans 2001-03-25 self-detect */
4090 else /* isStructorUnion */
4092 if (ctype_isRealAbstract (tr))
4096 message ("Access field of abstract type (%t): %s.%s",
4097 t, exprNode_unparse (s), f),
4099 ret->typ = ctype_unknown;
4103 if (ctype_isKnown (tr))
4108 ("Access field of non-struct or union (%t): %s.%s",
4109 t, exprNode_unparse (s), f),
4112 ret->typ = ctype_unknown;
4116 cstring sn = cstring_copy (f);
4118 checkSafeUse (ret, s->sref);
4119 cstring_markOwned (sn);
4120 ret->sref = sRef_makeField (s->sref, sn);
4132 exprNode_fieldAccess (/*@only@*/ exprNode s, /*@only@*/ lltok dot,
4133 /*@only@*/ cstring f)
4135 exprNode res = exprNode_fieldAccessAux (s, lltok_getLoc (dot), f);
4141 exprNode_addParens (/*@only@*/ lltok lpar, /*@only@*/ exprNode e)
4143 exprNode ret = exprNode_createPartialCopy (e);
4145 ret->loc = fileloc_update (ret->loc, lltok_getLoc (lpar));
4146 ret->kind = XPR_PARENS;
4147 ret->edata = exprData_makeUop (e, lpar);
4149 if (!exprNode_isError (e))
4151 ret->exitCode = e->exitCode;
4152 ret->canBreak = e->canBreak;
4153 ret->mustBreak = e->mustBreak;
4154 ret->isJumpPoint = e->isJumpPoint;
4155 ret->sref = e->sref;
4162 exprNode_arrowAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
4163 /*@only@*/ cstring f)
4165 exprNode ret = exprNode_createPartialCopy (s);
4167 ret->edata = exprData_makeField (s, f);
4168 ret->kind = XPR_ARROW;
4170 if (exprNode_isError (s))
4176 ctype t = exprNode_getType (s);
4177 ctype tr = ctype_realType (t);
4179 checkMacroParen (s);
4181 (void) ctype_fixArrayPtr (tr); /* REWRITE THIS */
4183 if (ctype_isRealPointer (tr))
4185 ctype b = ctype_realType (ctype_baseArrayPtr (tr));
4187 if (ctype_isStructorUnion (b))
4189 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4191 if (sRef_isKnown (s->sref) && sRef_possiblyNull (s->sref))
4193 if (!usymtab_isGuarded (s->sref) && !context_inProtectVars ())
4197 message ("Arrow access from %s pointer%q: %s",
4198 sRef_nullMessage (s->sref),
4199 sRef_unparsePreOpt (s->sref),
4200 exprNode_unparse (ret)),
4203 sRef_showNullInfo (s->sref);
4204 sRef_setNullError (s->sref);
4209 if (uentry_isUndefined (fentry))
4213 message ("Access non-existent field %s of %t: %s",
4214 f, t, exprNode_unparse (ret)),
4216 ret->typ = ctype_unknown;
4222 ** was safeUse: shouldn't be safe!
4225 ** rec must be defined,
4226 ** *rec must be allocated
4227 ** rec->field need only be defined it if is an rvalue
4230 uentry_setUsed (fentry, exprNode_loc (ret));
4231 ret->typ = uentry_getType (fentry);
4233 exprNode_checkUse (ret, s->sref, s->loc);
4235 /* exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc); */
4236 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4240 else /* Pointer to something that is not a struct or union*/
4242 if (ctype_isRealAbstract (tr))
4244 ctype xrt = ctype_forceRealType (tr);
4248 message ("Arrow access field of abstract type (%t): %s->%s",
4249 t, exprNode_unparse (s), f),
4253 ** Set the state correctly, as if the abstraction is broken.
4256 if (ctype_isRealPointer (xrt) &&
4257 (b = ctype_realType (ctype_baseArrayPtr (xrt)),
4258 ctype_isStructorUnion (b)))
4260 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4261 ret->typ = uentry_getType (fentry);
4262 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4266 ret->typ = ctype_unknown;
4267 ret->sref = sRef_undefined;
4270 else /* not a struct, union or abstract */
4272 if (ctype_isUnknown (tr)) {
4273 cstring sn = cstring_copy (f);
4275 DPRINTF (("Here: %s", exprNode_unparse (s)));
4277 exprNode_checkUse (ret, s->sref, s->loc);
4278 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4280 cstring_markOwned (sn);
4281 ret->sref = sRef_makeArrow (s->sref, sn);
4283 ret->kind = XPR_ARROW;
4288 message ("Arrow access field of non-struct or union "
4289 "pointer (%t): %s->%s",
4290 t, exprNode_unparse (s), f),
4293 ret->typ = ctype_unknown;
4294 ret->sref = sRef_undefined;
4299 else /* its not a pointer */
4301 if (!ctype_isUnknown (tr))
4305 message ("Arrow access of non-pointer (%t): %s->%s",
4306 t, exprNode_unparse (s), f),
4309 ret->typ = ctype_unknown;
4310 ret->sref = sRef_undefined;
4314 cstring sn = cstring_copy (f);
4316 DPRINTF (("Here: %s", exprNode_unparse (s)));
4318 exprNode_checkUse (ret, s->sref, s->loc);
4319 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4321 cstring_markOwned (sn);
4322 ret->sref = sRef_makeArrow (s->sref, sn);
4324 ret->kind = XPR_ARROW;
4335 exprNode_arrowAccess (/*@only@*/ exprNode s,
4336 /*@only@*/ lltok arrow,
4337 /*@only@*/ cstring f)
4339 exprNode res = exprNode_arrowAccessAux (s, lltok_getLoc (arrow), f);
4345 ** only postOp's in C: i++ and i--
4349 exprNode_postOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4351 /* check modification also */
4352 /* cstring opname = lltok_unparse (op);*/
4354 exprNode ret = exprNode_createPartialCopy (e);
4356 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4357 ret->kind = XPR_POSTOP;
4358 ret->edata = exprData_makeUop (e, op);
4360 if (!exprNode_isDefined (e))
4365 checkMacroParen (e);
4367 exprNode_checkUse (ret, e->sref, e->loc);
4368 exprNode_checkSet (ret, e->sref);
4370 t = exprNode_getType (e);
4372 if (sRef_isUnsafe (e->sref))
4374 voptgenerror (FLG_MACROPARAMS,
4375 message ("Operand of %s is macro parameter (non-functional): %s%s",
4376 lltok_unparse (op), exprNode_unparse (e), lltok_unparse (op)),
4378 sRef_makeSafe (e->sref);
4379 sRef_makeSafe (ret->sref);
4382 if (ctype_isForceRealNumeric (&t) || ctype_isRealAP (t))
4388 if (ctype_isRealAbstract (t))
4390 if (ctype_isRealNumAbstract (t)) {
4391 ; /* Allow operations on numabstract types */
4395 message ("Operand of %s is abstract type (%t): %s",
4396 lltok_unparse (op), t, exprNode_unparse (e)),
4404 message ("Operand of %s is non-numeric (%t): %s",
4405 lltok_unparse (op), t, exprNode_unparse (e)),
4408 ret->typ = ctype_unknown;
4411 /* if (ctype_isZero (t)) e->typ = ctype_int; */
4413 exprNode_checkModify (e, ret);
4415 /* added 7/11/2000 D.L */
4417 /*DRL 6/8/01 I decided to disable all Splint warnings here since the code
4418 probably needs a rewrite any way */
4423 /* updateEnvironmentForPostOp (e); */
4425 /* start modifications */
4426 /* added by Seejo on 4/16/2000 */
4428 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4430 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4432 ret->sref = sRef_copy (e->sref);
4435 if (lltok_getTok (op) == INC_OP) {
4436 if (sRef_getSize(e->sref) > 0) {
4438 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4440 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4441 /* Assumption: there is only 1 \0 in the buffer */
4442 /* This will not be correct if there are 2 \0's in the buffer */
4443 sRef_setNotNullTerminatedState(ret->sref);
4444 sRef_resetLen(ret->sref);
4446 sRef_setNullTerminatedState(ret->sref);
4447 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4449 if (sRef_isNullTerminated (ret->sref))
4450 printf ("ret->sref is Null Terminated\n");
4451 else if (sRef_isPossiblyNullTerminated (ret->sref))
4452 printf ("ret->sref is Possibly Null Terminated\n");
4453 else if (sRef_isNotNullTerminated (ret->sref))
4454 printf ("ret->sref is Not Null Terminated\n");
4459 if (lltok_getTok (op) == DEC_OP) {
4460 if (sRef_getSize(e->sref) >= 0) {
4461 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4462 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4467 /* end modifications */
4473 exprNode_preOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4475 bool checkMod = FALSE;
4477 int opid = lltok_getTok (op);
4478 exprNode ret = exprNode_createSemiCopy (e);
4480 exprNode_copySets (ret, e);
4482 multiVal_free (ret->val);
4483 ret->val = multiVal_undefined;
4484 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4485 ret->kind = XPR_PREOP;
4486 ret->edata = exprData_makeUop (e, op);
4488 if (exprNode_isError (e))
4493 checkMacroParen (e);
4495 te = exprNode_getType (e);
4496 tr = ctype_realType (te);
4498 if (opid != TAMPERSAND)
4500 exprNode_checkUse (ret, e->sref, e->loc);
4502 if (ctype_isRealAbstract (tr)
4503 && (!(ctype_isRealBool (te) && (opid == TEXCL))))
4505 if (ctype_isRealNumAbstract (tr))
4507 ; /* no warning for numabstract types */
4511 if (optgenerror (FLG_ABSTRACT,
4512 message ("Operand of %s is abstract type (%t): %s",
4513 lltok_unparse (op), tr,
4514 exprNode_unparse (ret)),
4517 tr = te = ctype_unknown;
4518 ret->typ = ctype_unknown;
4519 sRef_setNullError (e->sref);
4528 case DEC_OP: /* should also check modification! */
4529 if (sRef_isMacroParamRef (e->sref))
4533 message ("Operand of %s is macro parameter (non-functional): %s",
4534 lltok_unparse (op), exprNode_unparse (ret)),
4539 exprNode_checkSet (ret, e->sref);
4542 if (ctype_isForceRealNumeric (&tr) || ctype_isRealAP (tr))
4547 if (context_msgStrictOps ())
4551 message ("Operand of %s is non-numeric (%t): %s",
4552 lltok_unparse (op), te, exprNode_unparse (ret)),
4555 ret->typ = ctype_int;
4558 /* start modifications */
4559 /* added by Seejo on 4/16/2000 */
4561 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4563 if ((sRef_isPossiblyNullTerminated (e->sref))
4564 || (sRef_isNullTerminated(e->sref))) {
4565 ret->sref = sRef_copy (e->sref);
4568 if (lltok_getTok (op) == INC_OP) {
4569 if (sRef_getSize(e->sref) > 0) {
4571 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4573 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4574 /* Assumption: there is only 1 \0 in the buffer */
4575 /* This will not be correct if there are 2 \0's in the buffer */
4576 sRef_setNotNullTerminatedState(ret->sref);
4577 sRef_resetLen (ret->sref);
4579 sRef_setNullTerminatedState(ret->sref);
4580 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4586 if (lltok_getTok (op) == DEC_OP) {
4587 if (sRef_getSize(e->sref) >= 0) {
4588 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4589 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4594 /* end modifications */
4601 if (ctype_isForceRealNumeric (&tr))
4605 ret->val = multiVal_invert (exprNode_getValue (e));
4609 ret->val = multiVal_copy (exprNode_getValue (e));
4614 if (context_msgStrictOps ())
4618 message ("Operand of %s is non-numeric (%t): %s",
4619 lltok_unparse (op), te, exprNode_unparse (ret)),
4623 ret->typ = ctype_int;
4627 case TEXCL: /* maybe this should be restricted */
4628 guardSet_flip (ret->guards);
4630 if (ctype_isRealBool (te) || ctype_isUnknown (te))
4636 if (ctype_isRealPointer (tr))
4638 if (sRef_isKnown (e->sref))
4640 ret->guards = guardSet_addFalseGuard (ret->guards, e->sref);
4644 (FLG_BOOLOPS, FLG_PTRNEGATE,
4645 message ("Operand of %s is non-boolean (%t): %s",
4646 lltok_unparse (op), te, exprNode_unparse (ret)),
4653 message ("Operand of %s is non-boolean (%t): %s",
4654 lltok_unparse (op), te, exprNode_unparse (ret)),
4658 ret->typ = ctype_bool;
4663 if (ctype_isForceRealInt (&tr))
4668 if (context_msgStrictOps ())
4672 message ("Operand of %s is non-integer (%t): %s",
4673 lltok_unparse (op), te, exprNode_unparse (ret)),
4677 if (ctype_isInt (e->typ))
4683 ret->typ = ctype_int;
4689 ret->typ = ctype_makePointer (e->typ);
4691 if (sRef_isKnown (e->sref))
4693 ret->sref = sRef_makeAddress (e->sref);
4700 if (ctype_isAP (tr))
4702 ret->typ = ctype_baseArrayPtr (e->typ);
4706 if (ctype_isKnown (te))
4708 if (ctype_isFunction (te))
4714 message ("Dereference of function type (%t): %s",
4715 te, exprNode_unparse (ret)),
4720 voptgenerror (FLG_TYPE,
4721 message ("Dereference of non-pointer (%t): %s",
4722 te, exprNode_unparse (ret)),
4724 ret->typ = ctype_unknown;
4729 ret->typ = ctype_unknown;
4734 if (sRef_isKnown (e->sref))
4736 DPRINTF (("Checking possibly null: %s", sRef_unparseFull (e->sref)));
4738 if (sRef_possiblyNull (e->sref))
4740 DPRINTF (("Checking possibly null: %s", sRef_unparse (e->sref)));
4741 if (!usymtab_isGuarded (e->sref) && !context_inProtectVars ())
4745 message ("Dereference of %s pointer %q: %s",
4746 sRef_nullMessage (e->sref),
4747 sRef_unparse (e->sref),
4748 exprNode_unparse (ret)),
4751 sRef_showNullInfo (e->sref);
4752 sRef_setNotNull (e->sref, e->loc); /* suppress future messages */
4757 ret->sref = sRef_makePointer (e->sref);
4762 llbug (message ("exprNode_preOp: unhandled op: %s", lltok_unparse (op)));
4767 exprNode_checkModify (e, ret);
4774 ** any reason to disallow sizeof (abstract type) ?
4778 ** used by both sizeof
4782 ctype sizeof_resultType (void)
4784 static ctype sizet = ctype_unknown;
4786 if (ctype_isUnknown (sizet))
4788 if (usymtab_existsType (cstring_makeLiteralTemp ("size_t")))
4790 sizet = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("size_t")));
4794 sizet = ctype_ulint;
4801 exprNode_sizeofType (/*@only@*/ qtype qt)
4803 exprNode ret = exprNode_create (sizeof_resultType ());
4804 ctype ct = qtype_getType (qt);
4806 ret->kind = XPR_SIZEOFT;
4807 ret->edata = exprData_makeSizeofType (qt);
4809 voptgenerror (FLG_SIZEOFTYPE,
4810 message ("Parameter to sizeof is type %s: %s",
4812 exprNode_unparse (ret)),
4819 exprNode_alignofType (/*@only@*/ qtype qt)
4821 exprNode ret = exprNode_create (sizeof_resultType ());
4822 ctype ct = qtype_getType (qt);
4824 ret->kind = XPR_ALIGNOFT;
4825 ret->edata = exprData_makeSizeofType (qt);
4827 voptgenerror (FLG_SIZEOFTYPE,
4828 message ("Parameter to alignof is type %s: %s",
4830 exprNode_unparse (ret)),
4836 exprNode exprNode_offsetof (qtype qt, cstringList s)
4838 exprNode ret = exprNode_create (sizeof_resultType ());
4839 ctype ct = qtype_getType (qt);
4841 ret->kind = XPR_OFFSETOF;
4842 ret->edata = exprData_makeOffsetof (qt, s);
4844 if (!ctype_isRealSU (ct))
4846 voptgenerror (FLG_TYPE,
4847 message ("First parameter to offsetof is not a "
4848 "struct or union type (type %s): %s",
4850 exprNode_unparse (ret)),
4857 cstringList_elements (s, el) {
4861 if (ctype_isUndefined (lt))
4865 else if (!ctype_isRealSU (lt))
4867 voptgenerror (FLG_TYPE,
4868 message ("Inner offsetof type is not a "
4869 "struct or union type (type %s before field %s): %s",
4870 ctype_unparse (lt), el,
4871 exprNode_unparse (ret)),
4877 fields = ctype_getFields (ctype_realType (lt));
4878 fld = uentryList_lookupField (fields, el);
4879 DPRINTF (("Try: %s / %s", ctype_unparse (lt), el));
4881 if (uentry_isUndefined (fld))
4883 if (ctype_equal (lt, ct)) {
4884 voptgenerror (FLG_TYPE,
4885 message ("Field %s in offsetof is not the "
4886 "name of a field of %s: %s",
4889 exprNode_unparse (ret)),
4892 voptgenerror (FLG_TYPE,
4893 message ("Deep field %s in offsetof is not the "
4894 "name of a field of %s: %s",
4897 exprNode_unparse (ret)),
4903 lt = uentry_getType (fld);
4906 } end_cstringList_elements;
4908 /* Should report error if its a bit field - behavior is undefined! */
4915 exprNode_sizeofExpr (/*@only@*/ exprNode e)
4919 if (exprNode_isUndefined (e))
4921 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4922 ret->edata = exprData_makeSingle (e);
4923 ret->typ = sizeof_resultType ();
4924 ret->kind = XPR_SIZEOF;
4928 uentry u = exprNode_getUentry (e);
4930 ret = exprNode_createPartialCopy (e);
4931 ret->edata = exprData_makeSingle (e);
4933 ret->typ = sizeof_resultType ();
4934 ret->kind = XPR_SIZEOF;
4936 if (uentry_isValid (u)
4937 && uentry_isRefParam (u)
4938 && ctype_isRealArray (uentry_getType (u)))
4941 (FLG_SIZEOFFORMALARRAY,
4942 message ("Parameter to sizeof is an array-type function parameter: %s",
4943 exprNode_unparse (ret)),
4949 ** sizeof (x) doesn't "really" use x
4956 exprNode_alignofExpr (/*@only@*/ exprNode e)
4960 if (exprNode_isUndefined (e))
4962 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4966 ret = exprNode_createPartialCopy (e);
4969 ret->edata = exprData_makeSingle (e);
4970 ret->typ = sizeof_resultType ();
4971 ret->kind = XPR_ALIGNOF;
4974 ** sizeof (x) doesn't "really" use x
4981 exprNode_cast (/*@only@*/ lltok tok, /*@only@*/ exprNode e, /*@only@*/ qtype q)
4987 if (exprNode_isError (e))
4991 return exprNode_undefined;
4994 checkMacroParen (e);
4996 c = qtype_getType (q);
4997 t = exprNode_getType (e);
4999 ret = exprNode_createPartialCopy (e);
5001 ret->loc = fileloc_update (ret->loc, lltok_getLoc (tok));
5003 ret->kind = XPR_CAST;
5004 ret->edata = exprData_makeCast (tok, e, q);
5006 ret->sref = sRef_copy (e->sref);
5008 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
5010 if (!sRef_isConst (e->sref))
5012 usymtab_addForceMustAlias (ret->sref, e->sref);
5015 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
5016 sRef_setTypeFull (ret->sref, c);
5017 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
5023 ** void * <-> abstract * (if FLG_ABSTVOIDP)
5024 ** abstract * <-> void * (if FLG_ABSTVOIDP)
5027 if (ctype_isVoid (c)) /* cast to void is always okay --- discard value */
5029 /* evans 2002-07-19: added this warning */
5030 DPRINTF (("Checking: %s / %s", exprNode_unparse (ret), sRef_unparseFull (ret->sref)));
5031 if (sRef_isFresh (ret->sref))
5035 message ("New fresh storage %q(type %s) cast to void (not released): %s",
5036 sRef_unparseOpt (ret->sref),
5037 ctype_unparse (exprNode_getType (ret)),
5038 exprNode_unparse (ret)),
5039 exprNode_loc (ret));
5042 else if (ctype_isRealAP (c)) /* casting to array or pointer */
5044 ctype bc = ctype_getBaseType (c);
5045 ctype bt = ctype_getBaseType (t);
5046 ctype rt = ctype_realType (t);
5048 if (ctype_isFunction (ctype_baseArrayPtr (ctype_realType (c)))
5049 && (ctype_isArrayPtr (rt)
5050 && !ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
5054 message ("Cast from function pointer type (%t) to "
5055 "non-function pointer (%t): %s",
5056 c, t, exprNode_unparse (ret)),
5060 if (!ctype_isFunction (ctype_baseArrayPtr (c))
5061 && (ctype_isArrayPtr (rt)
5062 && ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
5066 message ("Cast from non-function pointer type (%t) to "
5067 "function pointer (%t): %s",
5068 c, t, exprNode_unparse (ret)),
5072 if (exprNode_isZero (e) && context_getFlag (FLG_ZEROPTR) &&
5073 !(ctype_isRealAbstract (bc)
5074 && context_hasAccess (ctype_typeId (bc))))
5076 ; /* okay to cast zero */
5080 if (ctype_isRealAbstract (bc)
5081 && !context_hasAccess (ctype_typeId (bc)))
5083 if (ctype_isVoidPointer (t) || ctype_isUnknown (t))
5087 message ("Cast to underlying abstract type %t: %s",
5088 c, exprNode_unparse (ret)),
5095 message ("Cast to underlying abstract type %t: %s",
5096 c, exprNode_unparse (ret)),
5101 if (ctype_isRealAbstract (bt)
5102 && !context_hasAccess (ctype_typeId (bt)))
5104 if (ctype_isUnknown (c) || ctype_isVoidPointer (c))
5108 message ("Cast from underlying abstract type %t: %s",
5109 t, exprNode_unparse (ret)),
5116 message ("Cast from underlying abstract type %t: %s",
5117 t, exprNode_unparse (ret)),
5125 ctype bt = ctype_realType (ctype_getBaseType (t));
5126 ctype bc = ctype_realType (ctype_getBaseType (c));
5128 if (ctype_isAbstract (bt) && !context_hasAccess (ctype_typeId (bt)))
5130 if (ctype_match (c, t))
5132 if (ctype_equal (c, t))
5136 message ("Redundant cast involving abstract type %t: %s",
5137 bt, exprNode_unparse (ret)),
5145 message ("Cast from abstract type %t: %s",
5146 bt, exprNode_unparse (ret)),
5151 if (ctype_isAbstract (bc)
5152 && !context_hasAccess (ctype_typeId (bc)))
5154 if (ctype_match (c, t))
5160 if (ctype_isNumAbstract (bc))
5162 if (exprNode_isNumLiteral (e))
5165 (FLG_NUMABSTRACTCAST,
5166 message ("Cast from literal to numabstract type %t: %s", bc,
5167 exprNode_unparse (ret)),
5174 message ("Cast to numabstract type %t: %s", bc,
5175 exprNode_unparse (ret)),
5181 DPRINTF (("No access to: %s / %d",
5182 ctype_unparse (bc), ctype_typeId (bc)));
5183 DPRINTF (("Context %s %s",
5184 bool_unparse (context_inFunctionLike ()),
5185 context_unparse ()));
5188 message ("Cast to abstract type %t: %s", bc,
5189 exprNode_unparse (ret)),
5196 if (ctype_isAbstract (c))
5198 if (sRef_isExposed (e->sref) || sRef_isOnly (e->sref))
5200 /* okay, cast exposed to abstract */
5201 sRef_clearExKindComplete (ret->sref, fileloc_undefined);
5205 if (ctype_isVisiblySharable (t)
5206 && sRef_isExternallyVisible (e->sref)
5207 && !(ctype_isAbstract (t)
5208 && context_hasAccess (ctype_typeId (t))))
5212 message ("Cast to abstract type from externally visible "
5213 "mutable storage exposes rep of %s: %s",
5215 exprNode_unparse (e)),
5225 evaluationOrderUndefined (lltok op)
5227 int opid = lltok_getTok (op);
5229 return (opid != AND_OP && opid != OR_OP);
5232 static bool checkIntegral (/*@notnull@*/ exprNode e1,
5233 /*@notnull@*/ exprNode e2,
5234 /*@notnull@*/ exprNode ret,
5239 ctype te1 = exprNode_getType (e1);
5240 ctype te2 = exprNode_getType (e2);
5242 ctype tr1 = ctype_realishType (te1);
5243 ctype tr2 = ctype_realishType (te2);
5245 if (ctype_isForceRealInt (&tr1) && ctype_isForceRealInt (&tr2))
5251 if (context_msgStrictOps ())
5253 if (!ctype_isInt (tr1) && !ctype_isInt (tr2))
5255 if (ctype_sameName (te1, te2))
5259 message ("Operands of %s are non-integer (%t): %s",
5260 lltok_unparse (op), te1,
5261 exprNode_unparse (ret)),
5268 message ("Operands of %s are non-integers (%t, %t): %s",
5269 lltok_unparse (op), te1, te2,
5270 exprNode_unparse (ret)),
5274 else if (!ctype_isInt (tr1))
5278 message ("Left operand of %s is non-integer (%t): %s",
5279 lltok_unparse (op), te1, exprNode_unparse (ret)),
5283 /* !ctype_isInt (te2) */
5287 message ("Right operand of %s is non-integer (%t): %s",
5288 lltok_unparse (op), te2, exprNode_unparse (ret)),
5298 ** returns exprNode representing e1 op e2
5300 ** uses msg if there are errors
5301 ** can be used for both assignment ops and regular ops
5306 static /*@only@*/ /*@notnull@*/ exprNode
5307 exprNode_makeOp (/*@keep@*/ exprNode e1, /*@keep@*/ exprNode e2,
5308 /*@keep@*/ lltok op)
5310 ctype te1, te2, tr1, tr2, tret;
5311 int opid = lltok_getTok (op);
5312 bool hasError = FALSE;
5315 if (exprNode_isError (e1))
5317 ret = exprNode_createPartialNVCopy (e2);
5321 ret = exprNode_createPartialNVCopy (e1);
5324 ret->val = multiVal_undefined;
5327 ret->edata = exprData_makeOp (e1, e2, op);
5329 if (exprNode_isError (e1) || exprNode_isError (e2))
5331 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5332 || opid == EQ_OP || opid == NE_OP
5333 || opid == AND_OP || opid == OR_OP)
5335 ret->typ = ctype_bool;
5338 if (exprNode_isDefined (e1))
5340 exprNode_checkUse (ret, e1->sref, e1->loc);
5343 if (exprNode_isDefined (e2))
5345 exprNode_mergeUSs (ret, e2);
5346 exprNode_checkUse (ret, e2->sref, e2->loc);
5352 tret = ctype_unknown;
5353 te1 = exprNode_getType (e1);
5355 DPRINTF (("te1 = %s / %s", exprNode_unparse (e1), ctype_unparse (te1)));
5357 te2 = exprNode_getType (e2);
5359 tr1 = ctype_realishType (te1);
5360 tr2 = ctype_realishType (te2);
5364 ret->guards = guardSet_or (ret->guards, e2->guards);
5366 else if (opid == AND_OP)
5368 ret->guards = guardSet_and (ret->guards, e2->guards);
5375 if (opid == EQ_OP || opid == NE_OP)
5377 exprNode temp1 = e1, temp2 = e2;
5379 /* could do NULL == x */
5381 if (exprNode_isNullValue (e1) || exprNode_isUnknownConstant (e1))
5383 temp1 = e2; temp2 = e1;
5386 if (exprNode_isNullValue (temp2) || exprNode_isUnknownConstant (temp2))
5388 reflectNullTest (temp1, (opid == NE_OP));
5389 guardSet_free (ret->guards);
5390 ret->guards = guardSet_copy (temp1->guards);
5394 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5395 || opid == EQ_OP || opid == NE_OP || opid == AND_OP || opid == OR_OP)
5400 if (anyAbstract (tr1, tr2) &&
5401 (!((ctype_isRealBool (te1) || ctype_isRealBool (te2)) &&
5402 (opid == AND_OP || opid == OR_OP
5403 || opid == EQ_OP || opid == NE_OP))))
5405 if (abstractOpError (tr1, tr2, op, e1, e2, e1->loc, e2->loc))
5407 tret = ctype_unknown;
5412 if (ctype_isUnknown (te1) || ctype_isUnknown (te2))
5414 /* unknown types, no comparisons possible */
5420 case TMULT: /* multiplication and division: */
5422 case MUL_ASSIGN: /* numeric, numeric -> numeric */
5423 case DIV_ASSIGN: /* */
5424 if (opid == TMULT || opid == MUL_ASSIGN)
5426 ret->val = multiVal_multiply (exprNode_getValue (e1),
5427 exprNode_getValue (e2));
5431 ret->val = multiVal_divide (exprNode_getValue (e1),
5432 exprNode_getValue (e2));
5435 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5438 case TPLUS: /* addition and subtraction: */
5439 case TMINUS: /* pointer, int -> pointer */
5440 case SUB_ASSIGN: /* int, pointer -> pointer */
5441 case ADD_ASSIGN: /* numeric, numeric -> numeric */
5442 if (opid == TPLUS || opid == ADD_ASSIGN)
5444 ret->val = multiVal_add (exprNode_getValue (e1),
5445 exprNode_getValue (e2));
5449 ret->val = multiVal_subtract (exprNode_getValue (e1),
5450 exprNode_getValue (e2));
5453 tr1 = ctype_fixArrayPtr (tr1);
5455 if ((ctype_isRealPointer (tr1) && !exprNode_isNullValue (e1))
5456 && (!ctype_isRealPointer (tr2) && ctype_isRealInt (tr2)))
5460 if (context_msgPointerArith ())
5464 message ("Pointer arithmetic (%t, %t): %s",
5465 te1, te2, exprNode_unparse (ret)),
5470 ** Swap terms so e1 is always the pointer
5473 if (ctype_isRealPointer (tr1))
5479 exprNode_swap (e1, e2);
5482 if (sRef_possiblyNull (e1->sref)
5483 && !usymtab_isGuarded (e1->sref))
5486 (FLG_NULLPOINTERARITH,
5487 message ("Pointer arithmetic involving possibly "
5488 "null pointer %s: %s",
5489 exprNode_unparse (e1),
5490 exprNode_unparse (ret)),
5494 ret->sref = sRef_copy (e1->sref);
5496 /* start modifications */
5497 /* added by Seejo on 4/16/2000 */
5499 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5501 if ((sRef_isPossiblyNullTerminated (e1->sref)) || (sRef_isNullTerminated(e1->sref))) {
5504 added ugly fixed to stop
5505 program from crashing on point + int +int
5506 one day I'll fix this or ask Seejo wtf the codes supposed to do. */
5508 if (!multiVal_isInt (e2->val) )
5512 val = (int) multiVal_forceInt (e2->val);
5514 /* Operator : + or += */
5515 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5516 if (sRef_getSize(e1->sref) >= val) {/* Incrementing the pointer by
5517 val should not result in a
5518 size < 0 (size = 0 is ok !) */
5520 sRef_setSize (ret->sref, sRef_getSize(e1->sref) - val);
5522 if (sRef_getLen(e1->sref) == val) { /* i.e. the character at posn val is \0 */
5523 sRef_setNotNullTerminatedState(ret->sref);
5524 sRef_resetLen (ret->sref);
5526 sRef_setNullTerminatedState(ret->sref);
5527 sRef_setLen (ret->sref, sRef_getLen(e1->sref) - val);
5532 /* Operator : - or -= */
5533 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5534 if (sRef_getSize(e1->sref) >= 0) {
5535 sRef_setSize (ret->sref, sRef_getSize(e1->sref) + val);
5536 sRef_setLen (ret->sref, sRef_getLen(e1->sref) + val);
5541 /* end modifications */
5543 sRef_setNullError (ret->sref);
5546 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5547 ** since is points to storage that should not be deallocated
5548 ** through this pointer.
5551 if (sRef_isOnly (ret->sref)
5552 || sRef_isFresh (ret->sref))
5554 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5559 else if ((!ctype_isRealPointer(tr1) && ctype_isRealInt (tr1))
5560 && (ctype_isRealPointer (tr2) && !exprNode_isNullValue (e2)))
5562 if (context_msgPointerArith ())
5566 message ("Pointer arithmetic (%t, %t): %s",
5567 te1, te2, exprNode_unparse (ret)),
5571 if (sRef_possiblyNull (e1->sref)
5572 && !usymtab_isGuarded (e1->sref))
5575 (FLG_NULLPOINTERARITH,
5576 message ("Pointer arithmetic involving possibly "
5577 "null pointer %s: %s",
5578 exprNode_unparse (e2),
5579 exprNode_unparse (ret)),
5583 ret->sref = sRef_copy (e2->sref);
5585 /* start modifications */
5586 /* added by Seejo on 4/16/2000 */
5588 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5591 if ((sRef_isPossiblyNullTerminated (e2->sref)) || (sRef_isNullTerminated(e2->sref))) {
5592 int val = (int) multiVal_forceInt (e1->val);
5594 /* Operator : + or += */
5595 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5596 if (sRef_getSize(e2->sref) >= val) {/* Incrementing the pointer by
5597 val should not result in a
5598 size < 0 (size = 0 is ok !) */
5600 sRef_setSize (ret->sref, sRef_getSize(e2->sref) - val);
5602 if (sRef_getLen(e2->sref) == val) { /* i.e. the character at posn val is \0 */
5603 sRef_setNotNullTerminatedState(ret->sref);
5604 sRef_resetLen (ret->sref);
5606 sRef_setNullTerminatedState(ret->sref);
5607 sRef_setLen (ret->sref, sRef_getLen(e2->sref) - val);
5612 /* Operator : - or -= */
5613 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5614 if (sRef_getSize(e2->sref) >= 0) {
5615 sRef_setSize (ret->sref, sRef_getSize(e2->sref) + val);
5616 sRef_setLen (ret->sref, sRef_getLen(e2->sref) + val);
5620 /* end modifications */
5622 sRef_setNullError (ret->sref);
5625 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5626 ** since is points to storage that should not be deallocated
5627 ** through this pointer.
5630 if (sRef_isOnly (ret->sref)
5631 || sRef_isFresh (ret->sref)) {
5632 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5636 ret->sref = e2->sref;
5640 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5649 case TAMPERSAND: /* bitwise & */
5651 case TCIRC: /* ^ (XOR) */
5656 bool reported = FALSE;
5662 if (opid == LEFT_OP || opid == LEFT_ASSIGN
5663 || opid == RIGHT_OP || opid == RIGHT_ASSIGN)
5666 ** evans 2002-01-01: fixed this to follow ISO 6.5.7.
5669 if (!ctype_isUnsigned (tr2)
5670 && !exprNode_isNonNegative (e2))
5672 reported = optgenerror
5674 message ("Right operand of %s may be negative (%t): %s",
5675 lltok_unparse (op), te2,
5676 exprNode_unparse (ret)),
5680 if (!ctype_isUnsigned (tr1)
5681 && !exprNode_isNonNegative (e1))
5683 reported = optgenerror
5684 (FLG_SHIFTIMPLEMENTATION,
5685 message ("Left operand of %s may be negative (%t): %s",
5686 lltok_unparse (op), te1,
5687 exprNode_unparse (ret)),
5692 ** Should check size of right operand also...
5698 if (!ctype_isUnsigned (tr1))
5700 if (exprNode_isNonNegative (e1)) {
5703 reported = optgenerror
5705 message ("Left operand of %s is not unsigned value (%t): %s",
5706 lltok_unparse (op), te1,
5707 exprNode_unparse (ret)),
5717 if (!ctype_isUnsigned (tr2))
5719 if (!exprNode_isNonNegative (e2)) {
5720 reported = optgenerror
5722 message ("Right operand of %s is not unsigned value (%t): %s",
5723 lltok_unparse (op), te2,
5724 exprNode_unparse (ret)),
5733 if (!checkIntegral (e1, e2, ret, op)) {
5734 te1 = ctype_unknown;
5738 DPRINTF (("Set: %s", ctype_unparse (te1)));
5741 ** tret is the widest type of te1 and te2
5744 tret = ctype_widest (te1, te2);
5749 if (checkIntegral (e1, e2, ret, op)) {
5752 tret = ctype_unknown;
5757 case TLT: /* comparisons */
5758 case TGT: /* numeric, numeric -> bool */
5760 DPRINTF (("Here we go: %s / %s",
5761 ctype_unparse (tr1), ctype_unparse (tr2)));
5763 if ((ctype_isReal (tr1) && !ctype_isInt (tr1))
5764 || (ctype_isReal (tr2) && !ctype_isInt (tr2)))
5767 bool fepsilon = FALSE;
5769 if (!ctype_isReal (rtype) || ctype_isInt (rtype))
5774 if (opid == TLT || opid == TGT)
5776 uentry ue1 = exprNode_getUentry (e1);
5777 uentry ue2 = exprNode_getUentry (e2);
5780 ** FLT_EPSILON, etc. really is a variable, not
5784 if (uentry_isVariable (ue1))
5786 cstring uname = uentry_rawName (ue1);
5788 if (cstring_equalLit (uname, "FLT_EPSILON")
5789 || cstring_equalLit (uname, "DBL_EPSILON")
5790 || cstring_equalLit (uname, "LDBL_EPSILON"))
5796 if (uentry_isVariable (ue2))
5798 cstring uname = uentry_rawName (ue2);
5800 if (cstring_equalLit (uname, "FLT_EPSILON")
5801 || cstring_equalLit (uname, "DBL_EPSILON")
5802 || cstring_equalLit (uname, "LDBL_EPSILON"))
5811 ; /* Don't complain. */
5817 message ("Dangerous comparison involving %s types: %s",
5818 ctype_unparse (rtype),
5819 exprNode_unparse (ret)),
5828 ** Types should match.
5831 DPRINTF (("Match types: %s / %s", exprNode_unparse (e1),
5832 exprNode_unparse (e2)));
5834 if (!exprNode_matchTypes (e1, e2))
5836 hasError = gentypeerror
5838 message ("Operands of %s have incompatible types (%t, %t): %s",
5839 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5845 || (ctype_isForceRealNumeric (&tr1)
5846 && ctype_isForceRealNumeric (&tr2)) ||
5847 (ctype_isRealPointer (tr1) && ctype_isRealPointer (tr2)))
5853 if ((ctype_isRealNumeric (tr1) && ctype_isRealPointer (tr2)) ||
5854 (ctype_isRealPointer (tr1) && ctype_isRealNumeric (tr2)))
5858 message ("Comparison of pointer and numeric (%t, %t): %s",
5859 te1, te2, exprNode_unparse (ret)),
5864 (void) checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5869 /* certain comparisons on unsigned's and zero look suspicious */
5871 if (opid == TLT || opid == LE_OP || opid == GE_OP)
5873 if ((ctype_isUnsigned (tr1) && exprNode_isZero (e2))
5874 || (ctype_isUnsigned (tr2) && exprNode_isZero (e1)))
5877 (FLG_UNSIGNEDCOMPARE,
5878 message ("Comparison of unsigned value involving zero: %s",
5879 exprNode_unparse (ret)),
5884 /* EQ_OP should NOT be used with booleans (unless one is FALSE) */
5886 if ((opid == EQ_OP || opid == NE_OP) &&
5887 ctype_isDirectBool (tr1) && ctype_isDirectBool (tr2))
5890 ** is one a variable?
5893 if (uentry_isVariable (exprNode_getUentry (e1))
5894 || uentry_isVariable (exprNode_getUentry (e2)))
5897 ** comparisons with FALSE are okay
5900 if (exprNode_isFalseConstant (e1)
5901 || exprNode_isFalseConstant (e2))
5910 ("Use of %q with %s variables (risks inconsistency because "
5911 "of multiple true values): %s",
5912 cstring_makeLiteral ((opid == EQ_OP) ? "==" : "!="),
5913 context_printBoolName (), exprNode_unparse (ret)),
5920 case AND_OP: /* bool, bool -> bool */
5922 if (ctype_isForceRealBool (&tr1) && ctype_isForceRealBool (&tr2))
5928 if (context_maybeSet (FLG_BOOLOPS))
5930 if (!ctype_isRealBool (te1) && !ctype_isRealBool (te2))
5932 if (ctype_sameName (te1, te2))
5936 message ("Operands of %s are non-boolean (%t): %s",
5937 lltok_unparse (op), te1,
5938 exprNode_unparse (ret)),
5946 ("Operands of %s are non-booleans (%t, %t): %s",
5947 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5951 else if (!ctype_isRealBool (te1))
5955 message ("Left operand of %s is non-boolean (%t): %s",
5956 lltok_unparse (op), te1, exprNode_unparse (ret)),
5959 else if (!ctype_isRealBool (te2))
5963 message ("Right operand of %s is non-boolean (%t): %s",
5964 lltok_unparse (op), te2, exprNode_unparse (ret)),
5977 (cstring_makeLiteral
5978 ("There has been a problem in the parser. This is believed to result "
5979 "from a problem with bison v. 1.25. Please try rebuidling Splint "
5980 "using the pre-compiled grammar files by commenting out the "
5981 "BISON= line in the top-level Makefile."));
5986 DPRINTF (("Return type %s: %s", exprNode_unparse (ret), ctype_unparse (tret)));
5988 exprNode_checkUse (ret, e1->sref, e1->loc);
5989 exprNode_mergeUSs (ret, e2);
5990 exprNode_checkUse (ret, e2->sref, e2->loc);
5996 exprNode_op (/*@only@*/ exprNode e1, /*@keep@*/ exprNode e2,
5997 /*@only@*/ lltok op)
6001 checkMacroParen (e1);
6002 checkMacroParen (e2);
6004 if (evaluationOrderUndefined (op) && context_maybeSet (FLG_EVALORDER))
6006 checkExpressionDefined (e1, e2, op);
6009 ret = exprNode_makeOp (e1, e2, op);
6014 void exprNode_checkAssignMod (exprNode e1, exprNode ret)
6017 ** This is somewhat bogus!
6019 ** Assigning to a nested observer in a non-observer datatype
6020 ** should not produce an error.
6023 sRef ref = exprNode_getSref (e1);
6025 DPRINTF (("Check assign mod: %s",
6026 sRef_unparseFull (ref)));
6028 if (sRef_isObserver (ref)
6029 || ((sRef_isFileStatic (ref) || sRef_isFileOrGlobalScope (ref))
6030 && ctype_isArray (ctype_realType (sRef_getType (ref)))))
6032 sRef base = sRef_getBase (ref);
6034 if (sRef_isValid (base) && sRef_isObserver (base))
6036 exprNode_checkModify (e1, ret);
6040 exprNode_checkModifyVal (e1, ret);
6045 exprNode_checkModify (e1, ret);
6050 exprNode_assign (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2, /*@only@*/ lltok op)
6052 bool isalloc = FALSE;
6053 bool isjustalloc = FALSE;
6054 bool noalias = FALSE;
6057 DPRINTF (("%s [%s] <- %s [%s]",
6058 exprNode_unparse (e1),
6059 ctype_unparse (e1->typ),
6060 exprNode_unparse (e2),
6061 ctype_unparse (e2->typ)));
6063 if (lltok_getTok (op) != TASSIGN)
6065 ret = exprNode_makeOp (e1, e2, op);
6067 DPRINTF (("Here goes: %s %s",
6068 ctype_unparse (e1->typ),
6069 ctype_unparse (e2->typ)));
6071 if (exprNode_isDefined (e1)
6072 && exprNode_isDefined (e2))
6074 if (ctype_isNumeric (e2->typ)
6075 || ctype_isNumeric (e1->typ))
6077 /* Its a pointer arithmetic expression like ptr += i */
6084 ret = exprNode_createPartialCopy (e1);
6085 ret->kind = XPR_ASSIGN;
6086 ret->edata = exprData_makeOp (e1, e2, op);
6088 if (!exprNode_isError (e2))
6090 ret->sets = sRefSet_union (ret->sets, e2->sets);
6091 ret->msets = sRefSet_union (ret->msets, e2->msets);
6092 ret->uses = sRefSet_union (ret->uses, e2->uses);
6096 checkExpressionDefined (e1, e2, op);
6098 if (exprNode_isError (e1))
6100 if (!exprNode_isError (e2))
6102 ret->loc = fileloc_update (ret->loc, e2->loc);
6106 ret->loc = fileloc_update (ret->loc, g_currentloc);
6110 if (!exprNode_isError (e2))
6112 checkMacroParen (e2);
6115 if (exprNode_isDefined (e1))
6117 if (sRef_isMacroParamRef (e1->sref))
6119 if (context_inIterDef ())
6121 uentry ue = sRef_getUentry (e1->sref);
6123 if (uentry_isYield (ue))
6129 if (fileloc_isDefined (e1->loc))
6133 message ("Assignment to non-yield iter parameter: %q",
6134 sRef_unparse (e1->sref)),
6141 message ("Assignment to non-yield iter parameter: %q",
6142 sRef_unparse (e1->sref)),
6149 if (fileloc_isDefined (e1->loc))
6153 message ("Assignment to macro parameter: %q",
6154 sRef_unparse (e1->sref)),
6161 message ("Assignment to macro parameter: %q",
6162 sRef_unparse (e1->sref)),
6166 exprNode_checkAssignMod (e1, ret); /* evans 2001-07-22 */
6171 exprNode_checkAssignMod (e1, ret);
6174 if (exprNode_isDefined (e2))
6176 if (lltok_getTok (op) == TASSIGN)
6178 ctype te1 = exprNode_getType (e1);
6179 ctype te2 = exprNode_getType (e2);
6181 if (ctype_isVoid (te2))
6185 message ("Assignment of void value to %t: %s %s %s",
6186 te1, exprNode_unparse (e1),
6188 exprNode_unparse (e2)),
6191 else if (!ctype_forceMatch (te1, te2))
6193 if (exprNode_matchLiteral (te1, e2))
6195 DPRINTF (("Literals match: %s / %s",
6196 ctype_unparse (te1), exprNode_unparse (e2)));
6197 if (ctype_isNumAbstract (te1)) {
6198 if (!context_flagOn (FLG_NUMABSTRACTLIT, e1->loc)) {
6199 (void) llgenhinterror
6202 ("Assignment of %t literal to numabstract type %t: %s %s %s",
6204 exprNode_unparse (e1),
6206 exprNode_unparse (e2)),
6208 ("Use +numabstractlit to allow numeric literals to be used as numabstract values"),
6217 message ("Assignment of %t to %t: %s %s %s",
6218 te2, te1, exprNode_unparse (e1),
6220 exprNode_unparse (e2)),
6226 /* Type checks okay */
6230 exprNode_mergeUSs (ret, e2);
6231 exprNode_checkUse (ret, e2->sref, e2->loc);
6233 DPRINTF (("Do assign! %s %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6240 doAssign (e1, e2, FALSE);
6243 ret->sref = e1->sref;
6247 if (exprNode_isDefined (e2))
6249 exprNode_mergeUSs (ret, e2);
6250 exprNode_checkUse (ret, e2->sref, e2->loc);
6254 if (sRef_isPointer (e1->sref) && !sRef_isMacroParamRef (e1->sref))
6256 exprNode_checkUse (ret, sRef_getBase (e1->sref), e1->loc);
6259 isjustalloc = sRef_isJustAllocated (e1->sref);
6260 isalloc = sRef_isAllocated (e1->sref);
6262 if (sRef_isField (e1->sref))
6264 sRef root = sRef_getRootBase (sRef_getBase (e1->sref));
6266 if (!sRef_isAllocated (root) && !sRef_isMacroParamRef (root))
6268 exprNode_checkUse (ret, root, e1->loc);
6274 ** be careful! this defines e1->sref.
6277 /* evans 2001-07-22: removed if (!sRef_isMacroParamRef (e1->sref)) */
6279 DPRINTF (("Setting: %s -> %s", exprNode_unparse (ret), sRef_unparse (e1->sref)));
6280 exprNode_checkSet (ret, e1->sref);
6284 sRef_setAllocatedComplete (e1->sref, exprNode_isDefined (e2)
6285 ? e2->loc : e1->loc);
6291 sRef_setAllocatedShallowComplete (e1->sref, exprNode_loc (e2));
6300 exprNode_cond (/*@keep@*/ exprNode pred, /*@keep@*/ exprNode ifclause,
6301 /*@keep@*/ exprNode elseclause)
6305 if (!exprNode_isError (pred))
6307 ret = exprNode_createPartialCopy (pred);
6308 checkMacroParen (pred);
6309 exprNode_checkPred (cstring_makeLiteralTemp ("conditional"), pred);
6311 if (!exprNode_isError (ifclause))
6313 checkMacroParen (ifclause); /* update macro counts! */
6315 if (!exprNode_isError (elseclause))
6317 checkMacroParen (elseclause);
6319 if (!exprNode_matchTypes (ifclause, elseclause))
6322 (exprNode_getType (ifclause),
6324 exprNode_getType (elseclause),
6326 message ("Conditional clauses are not of same type: "
6328 exprNode_unparse (ifclause),
6329 exprNode_getType (ifclause),
6330 exprNode_unparse (elseclause),
6331 exprNode_getType (elseclause)),
6334 ret->sref = sRef_undefined;
6335 ret->typ = ctype_unknown;
6340 /* for now...should merge the states */
6341 ret->sref = ifclause->sref;
6342 ret->typ = ifclause->typ;
6344 if (exprNode_isNullValue (ifclause))
6346 ret->typ = elseclause->typ;
6350 exprNode_checkUse (ret, pred->sref, pred->loc);
6351 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6352 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6354 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6359 ret->typ = ifclause->typ;
6361 exprNode_checkUse (pred, pred->sref, pred->loc);
6362 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6364 exprNode_mergeCondUSs (ret, ifclause, exprNode_undefined);
6369 if (!exprNode_isError (elseclause))
6371 ret->typ = elseclause->typ;
6373 exprNode_checkUse (pred, pred->sref, pred->loc);
6374 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6376 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6380 else /* pred is error */
6382 if (!exprNode_isError (ifclause))
6384 ret = exprNode_createSemiCopy (ifclause);
6386 checkMacroParen (ifclause); /* update macro counts! */
6388 if (!exprNode_isError (elseclause))
6390 checkMacroParen (elseclause);
6392 ret->typ = ifclause->typ;
6394 if (!ctype_forceMatch (ifclause->typ, elseclause->typ))
6397 (exprNode_getType (ifclause),
6399 exprNode_getType (elseclause),
6401 message ("Conditional clauses are not of same type: "
6403 exprNode_unparse (ifclause),
6404 exprNode_getType (ifclause),
6405 exprNode_unparse (elseclause),
6406 exprNode_getType (elseclause)),
6409 ret->typ = ctype_unknown;
6413 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6414 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6416 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6419 else if (!exprNode_isError (elseclause)) /* pred, if errors */
6421 ret = exprNode_createSemiCopy (ifclause);
6423 ret->typ = elseclause->typ;
6424 checkMacroParen (elseclause);
6426 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6427 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6429 else /* all errors! */
6431 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6435 ret->kind = XPR_COND;
6436 ret->edata = exprData_makeCond (pred, ifclause, elseclause);
6438 if (exprNode_isDefined (ifclause) && exprNode_isDefined (elseclause))
6440 exprNode_combineControl (ret, ifclause, elseclause);
6447 exprNode_vaArg (/*@only@*/ lltok tok, /*@only@*/ exprNode arg, /*@only@*/ qtype qt)
6449 ctype totype = qtype_getType (qt);
6451 exprNode_createPartialLocCopy (arg, fileloc_copy (lltok_getLoc (tok)));
6455 ** check use of va_arg : <valist>, type -> type
6458 if (exprNode_isError (arg))
6463 targ = exprNode_getType (arg);
6466 ** arg should have be a pointer
6469 if (!ctype_isUA (targ) ||
6470 (!usymId_equal (ctype_typeId (targ),
6471 usymtab_getTypeId (cstring_makeLiteralTemp ("va_list")))))
6475 message ("First argument to va_arg is not a va_list (type %t): %s",
6476 targ, exprNode_unparse (arg)),
6480 exprNode_checkSet (ret, arg->sref);
6484 ** return type is totype
6488 ret->kind = XPR_VAARG;
6489 ret->edata = exprData_makeCast (tok, arg, qt);
6494 exprNode exprNode_labelMarker (/*@only@*/ cstring label)
6496 exprNode ret = exprNode_createPlain (ctype_undefined);
6497 ret->kind = XPR_LABEL;
6498 ret->edata = exprData_makeLiteral (label);
6499 ret->isJumpPoint = TRUE;
6501 return (ret); /* for now, ignore label */
6504 exprNode exprNode_notReached (/*@returned@*/ exprNode stmt)
6506 if (exprNode_isDefined (stmt))
6508 stmt->isJumpPoint = TRUE;
6510 /* This prevent stray no return path errors, etc. */
6511 stmt->exitCode = XK_MUSTEXIT;
6517 bool exprNode_isDefaultMarker (exprNode e)
6519 if (exprNode_isDefined (e))
6521 return (e->kind == XPR_DEFAULT || e->kind == XPR_FTDEFAULT);
6527 bool exprNode_isCaseMarker (exprNode e)
6529 if (exprNode_isDefined (e))
6531 return (e->kind == XPR_FTCASE || e->kind == XPR_CASE);
6537 bool exprNode_isLabelMarker (exprNode e)
6539 if (exprNode_isDefined (e))
6541 return (e->kind == XPR_LABEL);
6547 exprNode exprNode_caseMarker (/*@only@*/ exprNode test, bool fallThrough)
6549 exprNode ret = exprNode_createPartialCopy (test);
6551 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6553 if (exprNode_isError (test)) {
6557 exprNode_checkUse (ret, test->sref, test->loc);
6559 usymtab_setExitCode (ret->exitCode);
6563 usymtab_setMustBreak ();
6566 ret->edata = exprData_makeSingle (test);
6567 ret->isJumpPoint = TRUE;
6573 exprNode exprNode_caseStatement (/*@only@*/ exprNode test, /*@only@*/ exprNode stmt, bool fallThrough)
6575 exprNode ret = exprNode_createPartialCopy (test);
6577 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6578 ret->edata = exprData_makePair (test, stmt);
6579 ret->isJumpPoint = TRUE;
6581 if (exprNode_isError (test))
6586 exprNode_checkUse (ret, test->sref, test->loc);
6588 if (exprNode_isError (stmt))
6593 exprNode_mergeUSs (ret, stmt);
6595 ret->exitCode = stmt->exitCode;
6596 ret->mustBreak = stmt->mustBreak;
6597 ret->canBreak = stmt->canBreak;
6599 usymtab_setExitCode (ret->exitCode);
6603 usymtab_setMustBreak ();
6610 /*@notnull@*/ /*@only@*/ exprNode
6611 exprNode_defaultMarker (/*@only@*/ lltok def, bool fallThrough)
6613 exprNode ret = exprNode_createTok (def);
6615 ret->isJumpPoint = TRUE;
6616 ret->kind = fallThrough ? XPR_FTDEFAULT : XPR_DEFAULT;
6621 exprNode_mayEscape (exprNode e)
6623 if (exprNode_isDefined (e))
6625 return exitkind_couldEscape (e->exitCode);
6631 exprNode_mustBreak (exprNode e)
6633 if (exprNode_isDefined (e))
6635 return e->mustBreak;
6642 exprNode_mustEscape (exprNode e)
6644 if (exprNode_isDefined (e))
6646 return exitkind_mustEscape (e->exitCode) || exprNode_mustBreak (e);
6653 exprNode_errorEscape (exprNode e)
6655 if (exprNode_isDefined (e))
6657 return exitkind_isError (e->exitCode);
6663 exprNode exprNode_concat (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
6665 exprNode ret = exprNode_createPartialCopy (e1);
6667 DPRINTF (("Concat: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6669 ret->edata = exprData_makePair (e1, e2);
6670 ret->kind = XPR_STMTLIST;
6672 if (exprNode_isDefined (e1))
6674 ret->isJumpPoint = e1->isJumpPoint;
6675 ret->canBreak = e1->canBreak;
6679 if (exprNode_isDefined (e2))
6681 ret->loc = fileloc_update (ret->loc, e2->loc);
6685 if (exprNode_isDefined (e2))
6687 ret->exitCode = e2->exitCode;
6688 ret->mustBreak = e2->mustBreak;
6689 if (e2->canBreak) ret->canBreak = TRUE;
6693 ** if e1 must return, then e2 is unreachable!
6696 if (exprNode_isDefined (e1) && exprNode_isDefined (e2))
6698 if ((exprNode_mustEscape (e1) || exprNode_mustBreak (e1))
6699 && !(e2->isJumpPoint))
6701 if (context_getFlag (FLG_UNREACHABLE))
6705 if (e2->kind == XPR_STMT)
6707 nr = exprData_getUopNode (e2->edata);
6710 if ((nr->kind == XPR_TOK
6711 && lltok_isSemi (exprData_getTok (nr->edata))))
6713 /* okay to have unreachable ";" */
6714 ret->exitCode = XK_MUSTEXIT;
6715 ret->canBreak = TRUE;
6719 if (optgenerror (FLG_UNREACHABLE,
6720 message ("Unreachable code: %s",
6721 exprNode_unparseFirst (nr)),
6724 ret->isJumpPoint = TRUE;
6725 ret->mustBreak = FALSE;
6726 ret->exitCode = XK_ERROR;
6727 DPRINTF (("Jump point: %s", exprNode_unparse (ret)));
6731 ret->exitCode = XK_MUSTEXIT;
6732 ret->canBreak = TRUE;
6740 if ((e2->kind == XPR_CASE || e2->kind == XPR_DEFAULT))
6743 ** We want a warning anytime we have:
6745 ** yyy; <<<- no break or return
6749 exprNode lastStmt = exprNode_lastStatement (e1);
6751 if (exprNode_isDefined (lastStmt)
6752 && !exprNode_mustEscape (lastStmt)
6753 && !exprNode_mustBreak (lastStmt)
6754 && !exprNode_isCaseMarker (lastStmt)
6755 && !exprNode_isDefaultMarker (lastStmt)
6756 && !exprNode_isLabelMarker (lastStmt))
6758 voptgenerror (FLG_CASEBREAK,
6760 ("Fall through case (no preceding break)"),
6767 exprNode_mergeUSs (ret, e2);
6769 usymtab_setExitCode (ret->exitCode);
6773 usymtab_setMustBreak ();
6776 DPRINTF (("==> %s", exprNode_unparse (ret)));
6780 exprNode exprNode_createTok (/*@only@*/ lltok t)
6782 exprNode ret; /*@i23 if on same line, bad things happen...!@*/
6783 ret = exprNode_create (ctype_unknown);
6784 ret->kind = XPR_TOK;
6785 ret->edata = exprData_makeTok (t);
6789 exprNode exprNode_statement (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6791 if (!exprNode_isError (e))
6793 exprChecks_checkStatementEffect(e);
6796 return (exprNode_statementError (e, t));
6799 static exprNode exprNode_statementError (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6801 exprNode ret = exprNode_createPartialCopy (e);
6803 if (!exprNode_isError (e))
6805 if (e->kind != XPR_ASSIGN)
6807 exprNode_checkUse (ret, e->sref, e->loc);
6810 ret->exitCode = e->exitCode;
6811 ret->canBreak = e->canBreak;
6812 ret->mustBreak = e->mustBreak;
6815 ret->edata = exprData_makeUop (e, t);
6816 ret->kind = XPR_STMT;
6821 exprNode exprNode_checkExpr (/*@returned@*/ exprNode e)
6823 if (!exprNode_isError (e))
6825 if (e->kind != XPR_ASSIGN)
6827 exprNode_checkUse (e, e->sref, e->loc);
6834 void exprNode_produceGuards (exprNode pred)
6836 if (!exprNode_isError (pred))
6838 if (ctype_isRealPointer (pred->typ))
6840 pred->guards = guardSet_addTrueGuard (pred->guards, pred->sref);
6843 exprNode_checkUse (pred, pred->sref, pred->loc);
6844 exprNode_resetSref (pred);
6848 exprNode exprNode_compoundStatementExpression (/*@only@*/ lltok tlparen, /*@only@*/ exprNode e)
6852 DPRINTF (("Compound: %s", exprNode_unparse (e)));
6854 if (!context_flagOn (FLG_GNUEXTENSIONS, exprNode_loc (e)))
6856 (void) llgenhinterror
6858 message ("Compound statement expressions is not supported by ISO C99"),
6859 message ("Use +gnuextensions to allow compound statement expressions (and other GNU language extensions) "
6860 "without this warning"),
6865 ** The type of a compoundStatementExpression is the type of the last statement
6868 llassert (exprNode_isBlock (e));
6869 laststmt = exprNode_lastStatement (e);
6871 DPRINTF (("Last statement: %s / %s", exprNode_unparse (laststmt), ctype_unparse (exprNode_getType (laststmt))));
6872 DPRINTF (("e: %s", exprNode_unparse (e)));
6873 e->typ = exprNode_getType (laststmt);
6874 return exprNode_addParens (tlparen, e);
6878 exprNode exprNode_makeBlock (/*@only@*/ exprNode e)
6880 exprNode ret = exprNode_createPartialCopy (e);
6882 if (!exprNode_isError (e))
6884 ret->exitCode = e->exitCode;
6885 ret->canBreak = e->canBreak;
6886 ret->mustBreak = e->mustBreak;
6889 DPRINTF (("Block e: %s", exprNode_unparse (e)));
6890 ret->edata = exprData_makeSingle (e);
6891 ret->kind = XPR_BLOCK;
6892 DPRINTF (("Block: %s", exprNode_unparse (ret)));
6896 bool exprNode_isBlock (exprNode e)
6898 return (exprNode_isDefined (e)
6899 && ((e)->kind == XPR_BLOCK));
6902 bool exprNode_isStatement (exprNode e)
6904 return (exprNode_isDefined (e)
6905 && ((e)->kind == XPR_STMT));
6908 bool exprNode_isAssign (exprNode e)
6910 if (exprNode_isDefined (e))
6912 return (e->kind == XPR_ASSIGN);
6918 bool exprNode_isEmptyStatement (exprNode e)
6920 return (exprNode_isDefined (e)
6921 && (e->kind == XPR_TOK)
6922 && (lltok_isSemi (exprData_getTok (e->edata))));
6925 bool exprNode_isMultiStatement (exprNode e)
6927 return (exprNode_isDefined (e)
6928 && ((e->kind == XPR_FOR)
6929 || (e->kind == XPR_FORPRED)
6930 || (e->kind == XPR_IF)
6931 || (e->kind == XPR_IFELSE)
6932 || (e->kind == XPR_WHILE)
6933 || (e->kind == XPR_WHILEPRED)
6934 || (e->kind == XPR_DOWHILE)
6935 || (e->kind == XPR_BLOCK)
6936 || (e->kind == XPR_STMT)
6937 || (e->kind == XPR_STMTLIST)
6938 || (e->kind == XPR_SWITCH)));
6941 void exprNode_checkIfPred (exprNode pred)
6943 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6946 exprNode exprNode_if (/*@only@*/ exprNode pred, /*@only@*/ exprNode tclause)
6949 bool emptyErr = FALSE;
6951 if (context_maybeSet (FLG_IFEMPTY))
6953 if (exprNode_isEmptyStatement (tclause))
6955 emptyErr = optgenerror (FLG_IFEMPTY,
6957 ("Body of if statement is empty"),
6958 exprNode_loc (tclause));
6962 if (!emptyErr && context_maybeSet (FLG_IFBLOCK))
6964 if (exprNode_isDefined (tclause)
6965 && !exprNode_isBlock (tclause))
6967 voptgenerror (FLG_IFBLOCK,
6969 ("Body of if statement is not a block: %s",
6970 exprNode_unparse (tclause)),
6971 exprNode_loc (tclause));
6975 if (exprNode_isError (pred))
6977 if (exprNode_isError (tclause))
6979 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6983 ret = exprNode_createPartialCopy (tclause);
6988 if (exprNode_mustEscape (pred))
6992 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6993 exprNode_loc (pred));
6996 /*! exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred); */ /*@i523@*/
6997 exprNode_checkUse (pred, pred->sref, pred->loc);
6999 if (!exprNode_isError (tclause))
7001 exprNode_mergeCondUSs (pred, tclause, exprNode_undefined);
7004 ret = exprNode_createPartialCopy (pred);
7008 ret->edata = exprData_makePair (pred, tclause);
7010 ret->exitCode = XK_UNKNOWN;
7012 if (exprNode_isDefined (tclause))
7014 ret->exitCode = exitkind_makeConditional (tclause->exitCode);
7015 ret->canBreak = tclause->canBreak;
7016 ret->sets = sRefSet_union (ret->sets, tclause->sets);
7017 ret->msets = sRefSet_union (ret->msets, tclause->msets);
7018 ret->uses = sRefSet_union (ret->uses, tclause->uses);
7021 ret->mustBreak = FALSE;
7026 exprNode exprNode_ifelse (/*@only@*/ exprNode pred,
7027 /*@only@*/ exprNode tclause,
7028 /*@only@*/ exprNode eclause)
7031 bool tEmptyErr = FALSE;
7032 bool eEmptyErr = FALSE;
7034 if (context_maybeSet (FLG_IFEMPTY))
7036 if (exprNode_isEmptyStatement (tclause))
7038 tEmptyErr = optgenerror
7041 ("Body of if clause of if statement is empty"),
7042 exprNode_loc (tclause));
7045 if (exprNode_isEmptyStatement (eclause))
7047 eEmptyErr = optgenerror
7050 ("Body of else clause of if statement is empty"),
7051 exprNode_loc (eclause));
7055 if (context_maybeSet (FLG_IFBLOCK))
7058 && exprNode_isDefined (tclause)
7059 && !exprNode_isBlock (tclause))
7061 voptgenerror (FLG_IFBLOCK,
7063 ("Body of if clause of if statement is not a block: %s",
7064 exprNode_unparse (tclause)),
7065 exprNode_loc (tclause));
7069 && exprNode_isDefined (eclause)
7070 && !exprNode_isBlock (eclause)
7071 && !(eclause->kind == XPR_IF)
7072 && !(eclause->kind == XPR_IFELSE))
7077 ("Body of else clause of if statement is not a block: %s",
7078 exprNode_unparse (eclause)),
7079 exprNode_loc (eclause));
7083 if (context_maybeSet (FLG_ELSEIFCOMPLETE))
7085 if (exprNode_isDefined (eclause)
7086 && (eclause->kind == XPR_IF))
7088 voptgenerror (FLG_ELSEIFCOMPLETE,
7089 message ("Incomplete else if logic (no final else): %s",
7090 exprNode_unparse (eclause)),
7091 exprNode_loc (eclause));
7095 if (exprNode_isError (pred))
7097 if (exprNode_isError (tclause))
7099 if (exprNode_isError (eclause))
7101 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7105 ret = exprNode_createPartialCopy (eclause);
7110 ret = exprNode_createPartialCopy (tclause);
7113 else /* pred is okay */
7115 ret = exprNode_createPartialCopy (pred);
7117 if (exprNode_mustEscape (pred))
7121 message ("Predicate always exits: %s", exprNode_unparse (pred)),
7122 exprNode_loc (pred));
7125 /*@i3423 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);*/
7126 exprNode_checkUse (ret, pred->sref, pred->loc);
7128 exprNode_mergeCondUSs (ret, tclause, eclause);
7131 ret->kind = XPR_IFELSE;
7132 ret->edata = exprData_makeCond (pred, tclause, eclause);
7134 if (exprNode_isDefined (tclause) && exprNode_isDefined (eclause))
7136 exprNode_combineControl (ret, tclause, eclause);
7137 ret->loc = fileloc_update (ret->loc, eclause->loc);
7144 ** *allpaths <- TRUE iff all executions paths must go through the switch
7148 checkSwitchExpr (exprNode test, /*@dependent@*/ exprNode e, /*@out@*/ bool *allpaths)
7150 exprNodeSList el = exprNode_flatten (e);
7151 bool mustReturn = TRUE; /* find a branch that doesn't */
7152 bool thisReturn = FALSE;
7153 bool hasDefault = FALSE;
7154 bool hasAllMembers = FALSE;
7155 bool inSwitch = FALSE;
7156 bool isEnumSwitch = FALSE;
7157 bool canBreak = FALSE;
7158 bool fallThrough = FALSE;
7159 ctype ct = ctype_unknown;
7160 enumNameSList usedEnums;
7163 if (exprNode_isDefined (test))
7168 ttype = ctype_realType (ct);
7170 if (ctype_isEnum (ttype))
7172 isEnumSwitch = TRUE;
7173 enums = ctype_elist (ttype);
7174 usedEnums = enumNameSList_new ();
7178 exprNodeSList_elements (el, current)
7181 DPRINTF ((message("checkSwitchExpr current = %s ", exprNode_unparse(current) ) ));
7183 if (exprNode_isDefined (current))
7185 switch (current->kind)
7192 (FLG_DUPLICATECASES,
7193 message ("Duplicate default cases in switch"),
7194 exprNode_loc (current));
7199 if (current->kind == XPR_DEFAULT || current->kind == XPR_FTDEFAULT)
7207 exprNode st = exprData_getSingle (current->edata);
7208 uentry ue = exprNode_getUentry (st);
7210 if (uentry_isValid (ue))
7212 cstring cname = uentry_rawName (ue);
7214 if (enumNameList_member (/*@-usedef@*/enums/*@=usedef@*/, cname))
7216 if (enumNameSList_member
7217 (/*@-usedef@*/usedEnums/*@=usedef@*/, cname))
7220 (FLG_DUPLICATECASES,
7221 message ("Duplicate case in switch: %s",
7227 enumNameSList_addh (usedEnums, cname);
7234 message ("Case in switch not %s member: %s",
7235 ctype_unparse (ct), cname),
7242 if (inSwitch && !fallThrough)
7244 if (!thisReturn || canBreak)
7254 /*@switchbreak@*/ break;
7256 thisReturn = thisReturn || exprNode_mustEscape (current);
7257 canBreak = canBreak || current->canBreak;
7258 if (canBreak) fallThrough = FALSE;
7261 } end_exprNodeSList_elements;
7263 if (inSwitch) /* check the last one! */
7265 if (!thisReturn || canBreak)
7274 && (enumNameSList_size (/*@-usedef@*/usedEnums/*@=usedef@*/) !=
7275 enumNameList_size (/*@-usedef@*/enums/*@=usedef@*/)))
7277 enumNameSList unused = enumNameSList_subtract (enums, usedEnums);
7279 voptgenerror (FLG_MISSCASE,
7280 message ("Missing case%s in switch: %q",
7281 cstring_makeLiteralTemp
7282 ((enumNameSList_size (unused) > 1) ? "s" : ""),
7283 enumNameSList_unparse (unused)),
7286 enumNameSList_free (unused);
7287 *allpaths = FALSE; /* evans 2002-01-01 */
7291 hasAllMembers = TRUE;
7295 enumNameSList_free (usedEnums);
7299 *allpaths = hasDefault;
7302 exprNodeSList_free (el);
7303 return ((hasDefault || hasAllMembers) && mustReturn);
7306 exprNode exprNode_switch (/*@only@*/ exprNode e, /*@only@*/ exprNode s)
7308 exprNode ret = exprNode_createPartialCopy (e);
7311 DPRINTF (("Switch: %s", exprNode_unparse (s)));
7313 ret->kind = XPR_SWITCH;
7314 ret->edata = exprData_makePair (e, s);
7316 if (!exprNode_isError (s))
7318 exprNode fs = exprNode_firstStatement (s);
7319 ret->loc = fileloc_update (ret->loc, s->loc);
7321 if (exprNode_isUndefined (fs)
7322 || exprNode_isCaseMarker (fs) || exprNode_isLabelMarker (fs)
7323 || exprNode_isDefaultMarker (fs)) {
7326 voptgenerror (FLG_FIRSTCASE,
7328 ("Statement after switch is not a case: %s", exprNode_unparse (fs)),
7333 if (!exprNode_isError (e))
7335 if (checkSwitchExpr (e, s, &allpaths))
7337 ret->exitCode = XK_MUSTRETURN;
7341 ret->exitCode = e->exitCode;
7344 ret->canBreak = e->canBreak;
7345 ret->mustBreak = e->mustBreak;
7349 ** exprNode.c:3883,32: Variable allpaths used before definition
7356 DPRINTF (("Context exit switch!"));
7357 context_exitSwitch (ret, allpaths);
7358 DPRINTF (("Context exit switch done!"));
7363 static void checkInfiniteLoop (/*@notnull@*/ exprNode test,
7364 /*@notnull@*/ exprNode body)
7366 sRefSet tuses = test->uses;
7368 if (!sRefSet_isEmpty (test->uses))
7370 sRefSet sets = sRefSet_newCopy (body->sets);
7371 bool hasError = TRUE;
7372 bool innerState = FALSE;
7373 sRefSet tuncon = sRefSet_undefined;
7375 sets = sRefSet_union (sets, test->sets);
7376 sets = sRefSet_union (sets, body->msets);
7377 sets = sRefSet_union (sets, test->msets);
7379 sRefSet_allElements (tuses, el)
7381 if (sRef_isUnconstrained (el))
7383 tuncon = sRefSet_insert (tuncon, el);
7387 if (sRefSet_member (sets, el))
7394 if (sRef_isInternalState (el)
7395 || sRef_isFileStatic (sRef_getRootBase (el)))
7399 } end_sRefSet_allElements ;
7403 sRefSet suncon = sRefSet_undefined;
7404 bool sinner = FALSE;
7406 sRefSet_allElements (sets, el)
7408 if (sRef_isUnconstrained (el))
7410 suncon = sRefSet_insert (suncon, el);
7412 else if (sRef_isInternalState (el))
7420 } end_sRefSet_allElements ;
7422 if (sinner && innerState)
7426 else if (sRefSet_isEmpty (tuncon)
7427 && sRefSet_isEmpty (suncon))
7432 ("Suspected infinite loop. No value used in loop test (%q) "
7433 "is modified by test or loop body.",
7434 sRefSet_unparsePlain (tuses)),
7439 if (sRefSet_isEmpty (tuncon))
7443 message ("Suspected infinite loop. No condition values "
7444 "modified. Modification possible through "
7445 "unconstrained calls: %q",
7446 sRefSet_unparsePlain (suncon)),
7453 message ("Suspected infinite loop. No condition values "
7454 "modified. Possible undetected dependency through "
7455 "unconstrained calls in loop test: %q",
7456 sRefSet_unparsePlain (tuncon)),
7462 sRefSet_free (sets);
7466 exprNode exprNode_while (/*@keep@*/ exprNode t, /*@keep@*/ exprNode b)
7469 bool emptyErr = FALSE;
7471 if (context_maybeSet (FLG_WHILEEMPTY))
7473 if (exprNode_isEmptyStatement (b))
7475 emptyErr = optgenerror
7478 ("Body of while statement is empty"),
7483 if (!emptyErr && context_maybeSet (FLG_WHILEBLOCK))
7485 if (exprNode_isDefined (b)
7486 && !exprNode_isBlock (b))
7488 if (context_inIterDef ()
7489 && (b->kind == XPR_STMTLIST
7490 || b->kind == XPR_TOK))
7496 voptgenerror (FLG_WHILEBLOCK,
7498 ("Body of while statement is not a block: %s",
7499 exprNode_unparse (b)),
7505 if (exprNode_isError (t))
7507 if (exprNode_isError (b))
7509 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7513 ret = exprNode_createPartialCopy (b);
7520 ret = exprNode_createPartialCopy (t);
7522 llassert (t->kind == XPR_WHILEPRED);
7524 test = exprData_getSingle (t->edata);
7526 if (!exprNode_isError (b) && exprNode_isDefined (test))
7528 if (context_maybeSet (FLG_INFLOOPS)
7529 || context_maybeSet (FLG_INFLOOPSUNCON))
7532 ** check that some variable in the predicate is set by the body
7533 ** if the predicate uses any variables
7536 checkInfiniteLoop (test, b);
7539 exprNode_mergeUSs (ret, b);
7541 if (exprNode_isDefined (b))
7543 ret->exitCode = exitkind_makeConditional (b->exitCode);
7548 ret->edata = exprData_makePair (t, b);
7549 ret->kind = XPR_WHILE;
7551 if (exprNode_isDefined (t) && exprNode_mustEscape (t))
7555 message ("Predicate always exits: %s", exprNode_unparse (t)),
7559 ret->exitCode = XK_NEVERESCAPE;
7562 ** If loop is infinite, and there is no break inside,
7563 ** exit code is never reach.
7566 if (exprNode_knownIntValue (t))
7568 if (!exprNode_isZero (t))
7570 if (exprNode_isDefined (b))
7574 /* Really, it means never reached. */
7575 ret->exitCode = XK_MUSTEXIT;
7585 ret->canBreak = FALSE;
7586 ret->mustBreak = FALSE;
7592 ** do { b } while (t);
7594 ** note: body passed as first argument
7597 exprNode exprNode_doWhile (/*@only@*/ exprNode b, /*@only@*/ exprNode t)
7601 DPRINTF (("Do while: %s / %s",
7602 exprNode_unparse (b), exprNode_unparse (t)));
7604 if (exprNode_isError (t))
7606 if (exprNode_isError (b))
7608 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7612 ret = exprNode_createPartialCopy (b);
7614 ret->exitCode = exitkind_makeConditional (b->exitCode);
7615 exprNode_checkUse (ret, b->sref, b->loc);
7616 ret->exitCode = b->exitCode;
7617 ret->canBreak = b->canBreak;
7618 ret->mustBreak = FALSE;
7623 DPRINTF (("Do while: %s / %s",
7624 exitkind_unparse (t->exitCode),
7625 exitkind_unparse (b->exitCode)));
7627 ret = exprNode_createPartialCopy (t);
7628 exprNode_checkPred (cstring_makeLiteralTemp ("while"), t);
7630 if (!exprNode_isError (b))
7633 ** forgot the copy's --- why wasn't this detected??
7636 ret->sets = sRefSet_copyInto (ret->sets, b->sets);
7637 ret->msets = sRefSet_copyInto (ret->msets, b->msets);
7638 ret->uses = sRefSet_copyInto (ret->uses, b->uses);
7640 /* left this out --- causes and aliasing bug (infinite loop)
7641 should be detected?? */
7643 exprNode_checkUse (ret, b->sref, b->loc);
7644 exprNode_mergeUSs (ret, t);
7645 exprNode_checkUse (ret, t->sref, t->loc);
7647 /* evans 2001-10-05: while loop can break */
7648 ret->exitCode = exitkind_makeConditional (b->exitCode);
7650 DPRINTF (("Do while: %s",
7651 exitkind_unparse (ret->exitCode)));
7653 ret->canBreak = b->canBreak;
7655 /* Always FALSE for doWhile loops - break's when test is false */
7656 ret->mustBreak = FALSE; /* b->mustBreak; */
7660 context_exitDoWhileClause (t);
7662 ret->kind = XPR_DOWHILE;
7663 ret->edata = exprData_makePair (t, b);
7667 bool exprNode_loopMustExec (exprNode forPred)
7670 ** Returns true if it is obvious that the loop always executes at least once
7672 ** For now, we only identify the most obvious cases. Should be true anytime
7673 ** we can prove init => !test.
7676 if (exprNode_isDefined (forPred))
7678 exprNode init, test, inc;
7681 llassert (forPred->kind == XPR_FORPRED);
7683 edata = forPred->edata;
7684 init = exprData_getTripleInit (edata);
7685 test = exprData_getTripleTest (edata);
7686 inc = exprData_getTripleInc (edata);
7688 if (exprNode_isAssign (init))
7690 exprNode loopVar = exprData_getOpA (init->edata);
7691 exprNode loopInit = exprData_getOpB (init->edata);
7693 if (exprNode_isDefined (test) && test->kind == XPR_OP)
7695 exprNode testVar = exprData_getOpA (test->edata);
7696 exprNode testVal = exprData_getOpB (test->edata);
7697 lltok comp = exprData_getOpTok (test->edata);
7698 int opid = lltok_getTok (comp);
7700 DPRINTF (("Same storage: %s / %s", exprNode_unparse (loopVar),
7701 exprNode_unparse (testVar)));
7703 if (exprNode_sameStorage (loopVar, testVar))
7705 multiVal valinit = exprNode_getValue (loopInit);
7706 multiVal valtest = exprNode_getValue (testVal);
7708 DPRINTF (("Values: %s / %s", multiVal_unparse (valinit),
7709 multiVal_unparse (valtest)));
7711 if (multiVal_isInt (valinit) && multiVal_isInt (valtest))
7713 long v1 = multiVal_forceInt (valinit);
7714 long v2 = multiVal_forceInt (valtest);
7716 DPRINTF (("Here: %ld %ld", v1, v2));
7718 if ((opid == EQ_OP && v1 < v2)
7719 || (opid == NE_OP && v1 != v2)
7720 || (opid == TLT && v1 <= v2)
7721 || (opid == TGT && v1 >= v2)
7722 || (opid == LE_OP && v1 < v2)
7723 || (opid == GE_OP && v1 > v2))
7725 DPRINTF (("mustexec if inc"));
7734 DPRINTF (("loop must exec: FALSE"));
7738 exprNode exprNode_for (/*@keep@*/ exprNode inc, /*@keep@*/ exprNode body)
7741 bool emptyErr = FALSE;
7743 if (context_maybeSet (FLG_FOREMPTY))
7745 if (exprNode_isEmptyStatement (body))
7747 emptyErr = optgenerror
7750 ("Body of for statement is empty"),
7751 exprNode_loc (body));
7755 if (!emptyErr && context_maybeSet (FLG_FORBLOCK))
7757 if (exprNode_isDefined (body)
7758 && !exprNode_isBlock (body))
7760 if (context_inIterDef ()
7761 && (body->kind == XPR_STMTLIST
7762 || body->kind == XPR_TOK))
7768 voptgenerror (FLG_FORBLOCK,
7770 ("Body of for statement is not a block: %s",
7771 exprNode_unparse (body)),
7772 exprNode_loc (body));
7778 ** for ud purposes: (alreadly) init -> test -> (now) LOOP: body + inc + test
7781 if (exprNode_isError (body))
7783 ret = exprNode_createPartialCopy (inc);
7787 ret = exprNode_createPartialCopy (body);
7789 ret->exitCode = exitkind_makeConditional (body->exitCode);
7791 exprNode_mergeUSs (inc, body);
7793 if (exprNode_isDefined (inc))
7797 context_setMessageAnnote (cstring_makeLiteral ("in post loop increment"));
7799 tmp = exprNode_effect (exprData_getTripleInc (inc->edata));
7800 exprNode_freeShallow (tmp);
7802 context_clearMessageAnnote ();
7803 context_setMessageAnnote (cstring_makeLiteral ("in post loop test"));
7805 tmp = exprNode_effect (exprData_getTripleTest (inc->edata));
7806 exprNode_freeShallow (tmp);
7808 context_clearMessageAnnote ();
7810 ret->uses = sRefSet_copyInto (ret->uses, inc->uses);
7811 ret->sets = sRefSet_copyInto (ret->sets, inc->sets);
7812 ret->msets = sRefSet_copyInto (ret->msets, inc->msets);
7816 ret->kind = XPR_FOR;
7817 ret->edata = exprData_makePair (inc, body);
7819 if (exprNode_isDefined (inc)) {
7820 exprNode test = exprData_getTripleTest (inc->edata);
7822 if (exprNode_isUndefined (test)) {
7823 if (exprNode_isDefined (body)) {
7824 if (!body->canBreak) {
7825 /* Really, it means never reached. */
7826 ret->exitCode = XK_MUSTEXIT;
7836 ** for (init; test; inc)
7839 ** while (test) { body; inc; }
7841 ** Now: check use of init (may set vars for test)
7842 ** check use of test
7846 /*@observer@*/ guardSet exprNode_getForGuards (exprNode pred)
7850 if (exprNode_isError (pred)) return guardSet_undefined;
7852 llassert (pred->kind == XPR_FORPRED);
7854 test = exprData_getTripleTest (pred->edata);
7856 if (!exprNode_isError (test))
7858 return (test->guards);
7861 return guardSet_undefined;
7864 exprNode exprNode_whilePred (/*@only@*/ exprNode test)
7866 exprNode ret = exprNode_createSemiCopy (test);
7868 if (exprNode_isDefined (test))
7870 exprNode_copySets (ret, test);
7871 exprNode_checkPred (cstring_makeLiteralTemp ("while"), test);
7872 exprNode_checkUse (ret, test->sref, test->loc);
7874 exprNode_produceGuards (test);
7876 ret->guards = guardSet_copy (test->guards);
7879 ret->edata = exprData_makeSingle (test);
7880 ret->kind = XPR_WHILEPRED;
7884 exprNode exprNode_forPred (/*@only@*/ exprNode init, /*@only@*/ exprNode test,
7885 /*@only@*/ exprNode inc)
7890 ** for ud purposes: init -> test -> LOOP: [ body, inc ]
7893 exprNode_checkPred (cstring_makeLiteralTemp ("for"), test);
7895 if (!exprNode_isError (inc))
7897 ret = exprNode_createPartialCopy (inc);
7901 if (!exprNode_isError (init))
7903 ret = exprNode_createPartialCopy (init);
7905 else if (!exprNode_isError (test))
7907 ret = exprNode_createPartialCopy (test);
7911 ret = exprNode_createUnknown ();
7915 exprNode_mergeUSs (ret, init);
7917 if (exprNode_isDefined (init))
7919 exprNode_checkUse (ret, init->sref, init->loc);
7922 exprNode_mergeUSs (ret, test);
7924 if (exprNode_isDefined (test))
7926 exprNode_checkUse (ret, test->sref, test->loc);
7929 ret->kind = XPR_FORPRED;
7930 ret->edata = exprData_makeFor (init, test, inc);
7934 /*@notnull@*/ /*@only@*/ exprNode exprNode_goto (/*@only@*/ cstring label)
7936 exprNode ret = exprNode_createUnknown ();
7938 if (context_inMacro ())
7940 voptgenerror (FLG_MACROSTMT,
7941 message ("Macro %s uses goto (not functional)",
7942 context_inFunctionName ()),
7946 ret->kind = XPR_GOTO;
7947 ret->edata = exprData_makeLiteral (label);
7948 ret->mustBreak = TRUE;
7949 ret->exitCode = XK_GOTO;
7950 ret->canBreak = TRUE;
7954 exprNode exprNode_continue (/*@only@*/ lltok l, int qcontinue)
7956 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7958 ret->kind = XPR_CONTINUE;
7959 ret->edata = exprData_makeTok (l);
7960 ret->canBreak = TRUE;
7961 ret->mustBreak = TRUE;
7963 if (qcontinue == QSAFEBREAK)
7967 else if (qcontinue == QINNERCONTINUE)
7969 if (!context_inDeepLoop ())
7972 (FLG_LOOPLOOPCONTINUE,
7973 cstring_makeLiteral ("Continue statement marked with innercontinue "
7974 "is not inside a nested loop"),
7975 exprNode_loc (ret));
7978 else if (qcontinue == BADTOK)
7980 if (context_inDeepLoop ())
7983 (FLG_LOOPLOOPCONTINUE,
7984 cstring_makeLiteral ("Continue statement in nested loop"),
7985 exprNode_loc (ret));
7990 llbuglit ("exprNode_continue: bad qcontinue");
7996 exprNode exprNode_break (/*@only@*/ lltok l, int bqual)
7998 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7999 clause breakClause = context_breakClause ();
8001 ret->kind = XPR_BREAK;
8002 ret->edata = exprData_makeTok (l);
8003 ret->canBreak = TRUE;
8004 ret->mustBreak = TRUE;
8006 if (breakClause == NOCLAUSE)
8010 cstring_makeLiteral ("Break not inside while, for or switch statement"),
8011 exprNode_loc (ret));
8015 if (bqual != BADTOK)
8022 if (breakClause == SWITCHCLAUSE)
8024 if (!context_inDeepSwitch ())
8026 voptgenerror (FLG_SYNTAX,
8028 ("Break preceded by innerbreak is not in a deep switch"),
8029 exprNode_loc (ret));
8034 if (!context_inDeepLoop ())
8036 voptgenerror (FLG_SYNTAX,
8038 ("Break preceded by innerbreak is not in a deep loop"),
8039 exprNode_loc (ret));
8044 if (breakClause == SWITCHCLAUSE)
8046 voptgenerror (FLG_SYNTAX,
8048 ("Break preceded by loopbreak is breaking a switch"),
8049 exprNode_loc (ret));
8053 if (breakClause != SWITCHCLAUSE)
8057 message ("Break preceded by switchbreak is breaking %s",
8058 cstring_makeLiteralTemp
8059 ((breakClause == WHILECLAUSE
8060 || breakClause == DOWHILECLAUSE) ? "a while loop"
8061 : (breakClause == FORCLAUSE) ? "a for loop"
8062 : (breakClause == ITERCLAUSE) ? "an iterator"
8064 exprNode_loc (ret));
8072 if (breakClause == SWITCHCLAUSE)
8074 clause nextBreakClause = context_nextBreakClause ();
8076 switch (nextBreakClause)
8078 case NOCLAUSE: break;
8084 (FLG_LOOPSWITCHBREAK,
8085 cstring_makeLiteral ("Break statement in switch inside loop"),
8086 exprNode_loc (ret));
8090 (FLG_SWITCHSWITCHBREAK,
8091 cstring_makeLiteral ("Break statement in switch inside switch"),
8092 exprNode_loc (ret));
8099 if (context_inDeepLoop ())
8103 cstring_makeLiteral ("Break statement in nested loop"),
8104 exprNode_loc (ret));
8108 if (context_inDeepLoopSwitch ())
8111 (FLG_SWITCHLOOPBREAK,
8112 cstring_makeLiteral ("Break statement in loop inside switch"),
8113 exprNode_loc (ret));
8123 exprNode exprNode_nullReturn (/*@only@*/ lltok t)
8125 fileloc loc = lltok_getLoc (t);
8126 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
8128 context_returnFunction ();
8129 exprChecks_checkNullReturn (loc);
8131 ret->kind = XPR_NULLRETURN;
8132 ret->edata = exprData_makeTok (t);
8133 ret->exitCode = XK_MUSTRETURN;
8137 exprNode exprNode_return (/*@only@*/ exprNode e)
8141 if (exprNode_isError (e))
8143 ret = exprNode_createUnknown ();
8147 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (e->loc));
8149 exprNode_checkUse (ret, e->sref, e->loc);
8150 exprNode_checkReturn (e);
8153 context_returnFunction ();
8154 ret->kind = XPR_RETURN;
8155 ret->edata = exprData_makeSingle (e);
8156 ret->exitCode = XK_MUSTRETURN;
8161 exprNode exprNode_comma (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
8165 if (exprNode_isError (e1))
8167 if (exprNode_isError (e2))
8169 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
8173 ret = exprNode_createPartialCopy (e2);
8174 exprNode_checkUse (ret, e2->sref, e2->loc);
8175 ret->sref = e2->sref;
8180 ret = exprNode_createPartialCopy (e1);
8182 exprNode_checkUse (ret, e1->sref, e1->loc);
8184 if (!exprNode_isError (e2))
8186 exprNode_mergeUSs (ret, e2);
8187 exprNode_checkUse (ret, e2->sref, e2->loc);
8188 ret->sref = e2->sref;
8192 ret->kind = XPR_COMMA;
8193 ret->edata = exprData_makePair (e1, e2);
8195 if (exprNode_isDefined (e1))
8197 if (exprNode_isDefined (e2))
8201 if (exprNode_mustEscape (e1) || e1->mustBreak)
8205 message ("Second clause of comma expression is unreachable: %s",
8206 exprNode_unparse (e2)),
8210 ret->exitCode = exitkind_combine (e1->exitCode, e2->exitCode);
8211 ret->mustBreak = e1->mustBreak || e2->mustBreak;
8212 ret->canBreak = e1->canBreak || e2->canBreak;
8216 if (exprNode_mustEscape (e1) || e1->mustBreak)
8220 message ("Second clause of comma expression is unreachable: %s",
8221 exprNode_unparse (e2)),
8225 ret->exitCode = e1->exitCode;
8226 ret->canBreak = e1->canBreak;
8231 if (exprNode_isDefined (e2))
8233 ret->exitCode = e2->exitCode;
8234 ret->mustBreak = e2->mustBreak;
8235 ret->canBreak = e2->canBreak;
8242 static bool exprNode_checkOneInit (/*@notnull@*/ exprNode el, exprNode val)
8244 ctype t1 = exprNode_getType (el);
8245 ctype t2 = exprNode_getType (val);
8246 bool hasError = FALSE;
8248 DPRINTF (("Check one init: %s / %s",
8249 exprNode_unparse (el),
8250 exprNode_unparse (val)));
8252 if (ctype_isUnknown (t1))
8254 voptgenerror (FLG_IMPTYPE,
8255 message ("Variable has unknown (implicitly int) type: %s",
8256 exprNode_unparse (el)),
8260 el->typ = ctype_int;
8263 if (exprNode_isDefined (val) && val->kind == XPR_INITBLOCK)
8265 exprNodeList vals = exprData_getArgs (val->edata);
8267 DPRINTF (("Check one init: %s", exprNodeList_unparse (vals)));
8268 DPRINTF (("Type: %s", ctype_unparse (t1)));
8270 if (ctype_isRealAP (t1))
8275 if (ctype_isFixedArray (t1))
8277 size_t nelements = ctype_getArraySize (t1);
8279 DPRINTF (("Checked array: %s / %d",
8280 ctype_unparse (t1), nelements));
8282 if (exprNode_isStringLiteral (val))
8284 exprNode_checkStringLiteralLength (t1, val);
8288 if (exprNodeList_size (vals) != size_toInt (nelements))
8290 hasError = optgenerror
8291 (exprNodeList_size (vals) > size_toInt (nelements)
8292 ? FLG_INITSIZE : FLG_INITALLELEMENTS,
8293 message ("Initializer block for "
8294 "%s has %d element%&, but declared as %s: %q",
8295 exprNode_unparse (el),
8296 exprNodeList_size (vals),
8298 exprNodeList_unparse (vals)),
8304 exprNodeList_elements (vals, oneval)
8306 cstring istring = message ("%d", i);
8309 (exprNode_fakeCopy (el),
8310 exprNode_numLiteral (ctype_int, istring,
8311 fileloc_copy (el->loc), i));
8313 if (exprNode_isDefined (newel))
8315 if (exprNodeList_size (vals) == 1
8316 && ctype_isString (exprNode_getType (oneval))
8317 && ctype_isChar (exprNode_getType (newel)))
8319 exprNode_freeIniter (newel);
8323 if (exprNode_checkOneInit (newel, oneval))
8328 if (nerrors > 3 && exprNodeList_size (vals) > 6)
8331 (message ("Additional initialization errors "
8332 "for %s not reported",
8333 exprNode_unparse (el)),
8335 exprNode_freeIniter (newel);
8340 exprNode_freeIniter (newel);
8345 exprNode_freeIniter (newel);
8350 cstring_free (istring);
8353 } end_exprNodeList_elements;
8356 else if (ctype_isStruct (ctype_realType (t1)))
8358 uentryList fields = ctype_getFields (t1);
8361 if (uentryList_size (fields) != exprNodeList_size (vals))
8363 if (uentryList_size (fields) > exprNodeList_size (vals))
8365 hasError = optgenerror
8367 message ("Initializer block for "
8368 "%s has %d field%&, but %s has %d field%&: %q",
8369 exprNode_unparse (el),
8370 exprNodeList_size (vals),
8372 uentryList_size (fields),
8373 exprNodeList_unparse (vals)),
8378 hasError = optgenerror
8380 message ("Initializer block for "
8381 "%s has %d field%&, but %s has %d field%&: %q",
8382 exprNode_unparse (el),
8383 exprNodeList_size (vals),
8385 uentryList_size (fields),
8386 exprNodeList_unparse (vals)),
8392 exprNodeList_elements (vals, oneval)
8394 uentry thisfield = uentryList_getN (fields, i);
8396 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8398 uentry_getName (thisfield));
8400 if (exprNode_isDefined (newel))
8402 if (exprNode_checkOneInit (newel, oneval))
8407 exprNode_freeIniter (newel);
8411 } end_exprNodeList_elements;
8414 /* evans 2001-12-30: added to fix bug reported by Jim Zelenka */
8415 else if (ctype_isUnion (ctype_realType (t1)))
8417 uentryList fields = ctype_getFields (t1);
8421 ** Union initializers set the first member always.
8424 DPRINTF (("Union initializer: %s / %s",
8425 exprNode_unparse (el), ctype_unparse (ctype_realType (t1))));
8427 if (exprNodeList_size (vals) != 1)
8429 hasError = optgenerror
8431 message ("Initializer block for union "
8432 "%s has %d elements, union initializers should have one element: %q",
8433 exprNode_unparse (el),
8434 exprNodeList_size (vals),
8435 exprNodeList_unparse (vals)),
8440 exprNode oneval = exprNodeList_head (vals);
8441 uentry thisfield = uentryList_getN (fields, i);
8443 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8445 uentry_getName (thisfield));
8447 if (exprNode_isDefined (newel))
8449 if (exprNode_checkOneInit (newel, oneval))
8454 exprNode_freeIniter (newel);
8460 hasError = optgenerror
8462 message ("Initializer block used for "
8463 "%s where %t is expected: %s",
8464 exprNode_unparse (el), t1, exprNode_unparse (val)),
8470 if (exprNode_isDefined (val))
8472 doAssign (el, val, TRUE);
8474 if (!exprNode_matchType (t1, val))
8476 hasError = gentypeerror
8478 message ("Initial value of %s is type %t, "
8480 exprNode_unparse (el),
8481 t2, t1, exprNode_unparse (val)),
8490 static /*@notnull@*/ exprNode
8491 exprNode_makeInitializationAux (/*@temp@*/ idDecl t)
8495 DPRINTF (("Initialization: %s", idDecl_unparse (t)));
8497 if (usymtab_exists (idDecl_observeId (t)))
8499 uentry ue = usymtab_lookup (idDecl_observeId (t));
8500 ret = exprNode_createId (ue);
8502 /*@i723 don't do this...but why? */
8504 ct = ctype_realishType (ret->typ);
8506 DPRINTF (("Type: %s", ctype_unparse (ret->typ)));
8508 if (ctype_isUnknown (ct))
8510 if (uentry_isAnyTag (ue))
8514 message ("%s used but not previously declared: %s",
8515 uentry_ekindName (ue),
8516 idDecl_getName (t)),
8524 message ("Variable has unknown (implicitly int) type: %s",
8525 idDecl_getName (t)),
8537 DPRINTF (("Unrecognized: %s", idDecl_unparse (t)));
8539 ue = uentry_makeUnrecognized (idDecl_observeId (t), fileloc_copy (g_currentloc));
8540 /*!! fileloc_copy (g_currentloc)); */
8541 /*@i32!!! should get error without this */
8543 ret = exprNode_fromIdentifierAux (ue);
8546 ** No error - this happens in old style declarations:
8550 message ("Unrecognized identifier in intializer: %s", idDecl_observeId (t)),
8557 exprData_free (ret->edata, ret->kind);
8558 ret->edata = exprData_undefined;
8559 ret->exitCode = XK_NEVERESCAPE;
8560 ret->mustBreak = FALSE;
8561 ret->kind = XPR_INIT;
8565 exprNode exprNode_makeEmptyInitialization (/*@only@*/ idDecl t)
8567 exprNode ret = exprNode_makeInitializationAux (t);
8568 llassert (ret->edata == exprData_undefined);
8569 ret->edata = exprData_makeInit (t, exprNode_undefined);
8573 exprNode exprNode_makeInitialization (/*@only@*/ idDecl t,
8574 /*@only@*/ exprNode e)
8576 uentry ue = usymtab_lookup (idDecl_observeId (t));
8577 exprNode ret = exprNode_makeInitializationAux (t);
8578 fileloc loc = exprNode_loc (e);
8580 DPRINTF (("initialization: %s = %s", idDecl_unparse (t), exprNode_unparse (e)));
8582 if (exprNode_isError (e))
8584 e = exprNode_createUnknown ();
8585 /* error: assume initializer is defined */
8586 sRef_setDefined (ret->sref, g_currentloc);
8587 ret->edata = exprData_makeInit (t, e);
8591 ctype ct = ctype_realishType (ret->typ);
8596 ** was addSafeUse --- what's the problem?
8598 ** int x = 3, y = x ?
8601 exprData_free (ret->edata, ret->kind);
8602 ret->edata = exprData_makeInit (t, e);
8603 DPRINTF (("ret: %s", exprNode_unparse (ret)));
8605 exprNode_checkUse (ret, e->sref, e->loc);
8607 if (ctype_isUnknown (e->typ) && uentry_isValid (ue))
8609 exprNode lhs = exprNode_createId (ue);
8612 ** static storage should be undefined before initializing
8615 if (uentry_isStatic (ue))
8617 sRef_setDefState (lhs->sref, SS_PARTIAL, fileloc_undefined);
8620 (void) exprNode_checkOneInit (lhs, e);
8622 if (uentry_isStatic (ue))
8624 sRef_setDefState (lhs->sref, SS_DEFINED, fileloc_undefined);
8627 exprNode_free (lhs);
8631 if (!exprNode_matchType (ct, e))
8633 if (exprNode_isZero (e) && ctype_isArrayPtr (ct))
8640 (exprNode_getType (e), e, exprNode_getType (ret), ret,
8642 ("Variable %q initialized to type %t, expects %t: %s",
8643 uentry_getName (ue), exprNode_getType (e),
8644 exprNode_getType (ret),
8645 exprNode_unparse (e)),
8651 if (uentry_isStatic (ue))
8653 sRef_setDefState (ret->sref, SS_PARTIAL, fileloc_undefined);
8656 if (exprNode_isStringLiteral (e)
8657 && (ctype_isArray (ct))
8658 && (ctype_isChar (ctype_realType (ctype_baseArrayPtr (ct)))))
8661 ** If t is a char [], the literal is copied.
8664 exprNode_checkStringLiteralLength (ct, e);
8665 sRef_setDefState (ret->sref, SS_DEFINED, e->loc);
8666 ret->val = multiVal_copy (e->val);
8668 sRef_setNullTerminatedState (ret->sref);
8670 if (multiVal_isDefined (e->val))
8672 cstring slit = multiVal_forceString (e->val);
8673 sRef_setLen (ret->sref, size_toInt (cstring_length (slit) + 1));
8676 if (ctype_isFixedArray (ct))
8678 sRef_setSize (ret->sref, size_toInt (ctype_getArraySize (ct)));
8683 doAssign (ret, e, TRUE);
8686 if (uentry_isStatic (ue))
8688 sRef_setDefState (ret->sref, SS_DEFINED, fileloc_undefined);
8692 if (context_inIterDef ())
8694 /* should check if it is yield */
8695 uentry_setUsed (ue, loc);
8702 exprNode_mergeUSs (ret, e);
8703 DPRINTF (("Ret: %s %p %p",
8704 exprNode_unparse (ret),
8705 ret->requiresConstraints,
8706 ret->ensuresConstraints));
8708 DPRINTF (("Ret: %s %s %s",
8709 exprNode_unparse (ret),
8710 constraintList_unparse (ret->requiresConstraints),
8711 constraintList_unparse (ret->ensuresConstraints)));
8715 exprNode exprNode_iter (/*@observer@*/ uentry name,
8716 /*@only@*/ exprNodeList alist,
8717 /*@only@*/ exprNode body,
8718 /*@observer@*/ uentry end)
8723 llassert (uentry_isValid (name));
8725 uentry_setUsed (name, exprNode_loc (body));
8727 ret = exprNode_createPartialCopy (body);
8728 iname = uentry_getName (name);
8730 if (uentry_isInvalid (end))
8732 llerror (FLG_ITERBALANCE,
8733 message ("Iter %s not balanced with end_%s", iname, iname));
8737 cstring ename = uentry_getName (end);
8739 if (!cstring_equalPrefixLit (ename, "end_"))
8741 llerror (FLG_ITERBALANCE, message ("Iter %s not balanced with end_%s: %s",
8742 iname, iname, ename));
8746 if (!cstring_equal (iname, cstring_suffix (ename, 4)))
8748 llerror (FLG_ITERBALANCE,
8749 message ("Iter %s not balanced with end_%s: %s",
8750 iname, iname, ename));
8754 cstring_free (ename);
8757 context_exitIterClause (body);
8759 ret->kind = XPR_ITER;
8760 ret->edata = exprData_makeIter (name, alist, body, end);
8762 if (uentry_isIter (name))
8764 (void) checkArgsReal (name, body,
8765 uentry_getParams (name), alist, TRUE, ret);
8768 cstring_free (iname);
8774 exprNode_iterNewId (/*@only@*/ cstring s)
8776 exprNode e = exprNode_new ();
8777 uentry ue = uentryList_getN (uentry_getParams (getCurrentIter ()), iterParamNo ());
8779 llassert (processingIterVars ());
8781 e->loc = context_getSaveLocation ();
8783 if (fileloc_isUndefined (e->loc))
8785 fileloc_free (e->loc);
8786 e->loc = fileloc_copy (g_currentloc);
8789 e->uses = sRefSet_new ();
8790 e->sets = sRefSet_new ();
8791 e->msets = sRefSet_new ();
8793 e->val = multiVal_unknown ();
8794 e->guards = guardSet_new ();
8795 e->sref = sRef_undefined;
8796 e->isJumpPoint = FALSE;
8797 e->exitCode = XK_NEVERESCAPE;
8799 /*> missing fields, detected by splint <*/
8800 e->canBreak = FALSE;
8801 e->mustBreak = FALSE;
8802 e->etext = cstring_undefined;
8804 if (uentry_isYield (ue))
8806 uentry uue = uentry_makeVariable (s, uentry_getType (ue),
8807 fileloc_copy (e->loc),
8811 uue = usymtab_supEntrySrefReturn (uue);
8813 sr = uentry_getSref (uue);
8814 sRef_mergeStateQuiet (sr, uentry_getSref (ue));
8815 sr = uentry_getSref (uue);
8816 sRef_setDefined (sr, e->loc);
8818 e->typ = uentry_getType (uue);
8820 e->edata = exprData_makeId (uue);
8821 uentry_setUsed (uue, g_currentloc);
8827 sRef_setGlobalScope ();
8828 uue = uentry_makeVariableLoc (s, ctype_unknown);
8830 e->typ = ctype_unknown;
8831 e->edata = exprData_makeId (uue);
8833 uentry_setUsed (uue, e->loc);
8834 uentry_setHasNameError (uue);
8836 if (context_getFlag (FLG_REPEATUNRECOG))
8838 uentry_markOwned (uue);
8842 usymtab_supGlobalEntry (uue);
8845 sRef_clearGlobalScope ();
8847 voptgenerror (FLG_UNRECOG, message ("Unrecognized identifier: %s", s),
8854 exprNode_defineConstraints(e);
8859 exprNode_iterExpr (/*@returned@*/ exprNode e)
8861 if (!processingIterVars ())
8863 llcontbuglit ("checkIterParam: not in iter");
8867 if (uentry_isYield (uentryList_getN (uentry_getParams (getCurrentIter ()),
8870 if (exprNode_isDefined (e))
8872 if (fileloc_isDefined (e->loc))
8876 message ("Yield parameter is not simple identifier: %s",
8877 exprNode_unparse (e)),
8884 message ("Yield parameter is not simple identifier: %s",
8885 exprNode_unparse (e)),
8895 exprNode_iterId (/*@observer@*/ uentry c)
8899 llassert (processingIterVars ());
8901 ue = uentryList_getN (uentry_getParams (getCurrentIter ()),
8904 if (uentry_isYield (ue))
8906 ctype ct = uentry_getType (ue);
8907 exprNode e = exprNode_createPlain (ct);
8908 cstring name = uentry_getName (c);
8909 uentry le = uentry_makeVariable (name, ct, fileloc_undefined, FALSE);
8911 uentry_setUsed (ue, g_currentloc);
8912 uentry_setHasNameError (ue);
8914 cstring_free (name);
8917 e->edata = exprData_makeId (le);
8918 e->loc = context_getSaveLocation ();
8919 e->sref = uentry_getSref (le);
8921 usymtab_supEntrySref (le);
8923 if (!context_inHeader ())
8927 message ("Yield parameter shadows local declaration: %q",
8928 uentry_getName (c)),
8929 fileloc_isDefined (e->loc) ? e->loc : g_currentloc))
8931 uentry_showWhereDeclared (c);
8938 return (exprNode_fromIdentifierAux (c));
8941 exprNode exprNode_iterStart (/*@observer@*/ uentry name, /*@only@*/ exprNodeList alist)
8943 exprNode ret = exprNode_create (ctype_unknown);
8945 ret->kind = XPR_ITERCALL;
8946 ret->edata = exprData_makeIterCall (name, alist);
8948 if (uentry_isIter (name))
8950 uentryList params = uentry_getParams (name);
8952 if (context_inIterDef ()
8953 && uentryList_size (params) == exprNodeList_size (alist))
8957 exprNodeList_elements (alist, arg)
8959 uentry parg = uentryList_getN (params, i);
8961 if (uentry_isYield (parg))
8963 uentry ue = exprNode_getUentry (arg);
8965 if (uentry_isValid (ue))
8972 } end_exprNodeList_elements;
8975 (void) checkArgsReal (name, ret, params, alist, TRUE, ret);
8976 checkUnspecCall (ret, params, alist);
8982 /*@exposed@*/ sRef exprNode_getSref (exprNode e)
8984 if (exprNode_isDefined (e))
8986 if (sRef_isInvalid (e->sref))
8989 e->sref = sRef_makeUnknown ();
8990 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
9001 return sRef_undefined;
9005 /*@observer@*/ cstring
9006 exprNode_unparseFirst (exprNode e)
9008 if (exprNode_isDefined (e))
9012 if (e->kind == XPR_STMTLIST
9013 || e->kind == XPR_COMMA || e->kind == XPR_COND)
9015 exprNode first = exprData_getPairA (e->edata);
9017 if (exprNode_isDefined (first))
9019 return (exprNode_unparseFirst (exprData_getPairA (e->edata)));
9023 return (cstring_makeLiteralTemp ("..."));
9027 ret = cstring_elide (exprNode_unparse (e), 20);
9028 cstring_markOwned (ret);
9034 return cstring_makeLiteralTemp ("<error>");
9038 /*@observer@*/ cstring
9039 exprNode_unparse (/*@temp@*/ exprNode e)
9041 if (exprNode_isError (e))
9043 return cstring_makeLiteralTemp ("<error>");
9046 if (cstring_isDefined (e->etext))
9052 cstring ret = exprNode_doUnparse (e);
9054 /*@-modifies@*/ /* benevolent */
9061 /*@observer@*/ fileloc
9062 exprNode_loc (exprNode e)
9064 if (exprNode_isError (e))
9066 return (g_currentloc);
9075 ** executes exprNode e
9076 ** recursively rexecutes as though in original parse using
9077 ** information in e->edata
9080 static /*@only@*/ exprNodeList exprNodeList_effect (exprNodeList e)
9082 exprNodeList ret = exprNodeList_new ();
9084 exprNodeList_elements (e, current)
9086 exprNodeList_addh (ret, exprNode_effect (current));
9087 } end_exprNodeList_elements;
9092 static /*@only@*/ exprNode exprNode_effect (exprNode e)
9093 /*@globals internalState@*/
9095 bool innerEffect = inEffect;
9101 context_clearJustPopped ();
9103 if (exprNode_isError (e))
9105 ret = exprNode_undefined;
9110 ** Turn off expose and dependent transfer checking.
9111 ** Need to pass exposed internal nodes,
9112 ** [ copying would be a waste! ]
9113 ** [ Actually, I think I wasted a lot more time than its worth ]
9114 ** [ trying to do this. ]
9118 /*@-observertrans@*/
9119 /*@-dependenttrans@*/
9126 ret = exprNode_addParens (exprData_getUopTok (data),
9127 exprNode_effect (exprData_getUopNode (data)));
9130 ret = exprNode_assign (exprNode_effect (exprData_getOpA (data)),
9131 exprNode_effect (exprData_getOpB (data)),
9132 exprData_getOpTok (data));
9135 ret = exprNode_undefined;
9138 ret = exprNode_functionCall (exprNode_effect (exprData_getFcn (data)),
9139 exprNodeList_effect (exprData_getArgs (data)));
9152 cstring id = exprData_getId (data);
9153 uentry ue = usymtab_lookupSafe (id);
9155 ret = exprNode_fromIdentifierAux (ue);
9156 ret->loc = fileloc_update (ret->loc, e->loc);
9163 ret = exprNode_arrayFetch (exprNode_effect (exprData_getPairA (data)),
9164 exprNode_effect (exprData_getPairB (data)));
9168 ** evans 2002-03-15: for && and ||, need to do the guards also
9169 ** this is what cgrammar.y does - should be
9170 ** able to avoid duplication, but need to
9171 ** time with grammar productions.
9174 DPRINTF (("Effect: %s", exprNode_unparse (e)));
9176 if (lltok_getTok (exprData_getOpTok (data)) == AND_OP)
9178 exprNode e1 = exprNode_effect (exprData_getOpA (data));
9180 exprNode_produceGuards (e1);
9181 context_enterAndClause (e1);
9182 e2 = exprNode_effect (exprData_getOpB (data));
9184 ret = exprNode_op (e1, e2,
9185 exprData_getOpTok (data));
9187 context_exitAndClause (ret, e2);
9189 else if (lltok_getTok (exprData_getOpTok (data)) == OR_OP)
9191 exprNode e1 = exprNode_effect (exprData_getOpA (data));
9193 exprNode_produceGuards (e1);
9194 context_enterOrClause (e1);
9195 e2 = exprNode_effect (exprData_getOpB (data));
9197 ret = exprNode_op (e1, e2,
9198 exprData_getOpTok (data));
9200 context_exitOrClause (ret, e2);
9204 ret = exprNode_op (exprNode_effect (exprData_getOpA (data)),
9205 exprNode_effect (exprData_getOpB (data)),
9206 exprData_getOpTok (data));
9211 ret = exprNode_postOp (exprNode_effect (exprData_getUopNode (data)),
9212 exprData_getUopTok (data));
9215 ret = exprNode_preOp (exprNode_effect (exprData_getUopNode (data)),
9216 exprData_getUopTok (data));
9228 ret = exprNode_vaArg (exprData_getCastTok (data),
9229 exprNode_effect (exprData_getCastNode (data)),
9230 exprData_getCastType (data));
9234 ret = exprNode_cast (exprData_getCastTok (data),
9235 exprNode_effect (exprData_getCastNode (data)),
9236 exprData_getCastType (data));
9239 ret = exprNode_iterStart (exprData_getIterCallIter (data),
9241 (exprData_getIterCallArgs (data)));
9245 ret = exprNode_iter (exprData_getIterSname (data),
9246 exprNodeList_effect (exprData_getIterAlist (data)),
9247 exprNode_effect (exprData_getIterBody (data)),
9248 exprData_getIterEname (data));
9252 ret = exprNode_for (exprNode_effect (exprData_getPairA (data)),
9253 exprNode_effect (exprData_getPairB (data)));
9257 ret = exprNode_forPred (exprNode_effect (exprData_getTripleInit (data)),
9258 exprNode_effect (exprData_getTripleTest (data)),
9259 exprNode_effect (exprData_getTripleInc (data)));
9263 ret = exprNode_createTok (exprData_getTok (data));
9267 ret = exprNode_goto (exprData_getLiteral (data));
9268 ret->loc = fileloc_update (ret->loc, e->loc);
9272 ret = exprNode_continue (exprData_getTok (data), QSAFEBREAK);
9276 ret = exprNode_break (exprData_getTok (data), QSAFEBREAK);
9280 ret = exprNode_return (exprNode_effect (exprData_getSingle (data)));
9283 case XPR_NULLRETURN:
9284 ret = exprNode_nullReturn (exprData_getTok (data));
9288 ret = exprNode_comma (exprNode_effect (exprData_getPairA (data)),
9289 exprNode_effect (exprData_getPairB (data)));
9293 ret = exprNode_cond (exprNode_effect (exprData_getTriplePred (data)),
9294 exprNode_effect (exprData_getTripleTrue (data)),
9295 exprNode_effect (exprData_getTripleFalse (data)));
9298 ret = exprNode_if (exprNode_effect (exprData_getPairA (data)),
9299 exprNode_effect (exprData_getPairB (data)));
9303 ret = exprNode_ifelse (exprNode_effect (exprData_getTriplePred (data)),
9304 exprNode_effect (exprData_getTripleTrue (data)),
9305 exprNode_effect (exprData_getTripleFalse (data)));
9308 ret = exprNode_whilePred (exprData_getSingle (data));
9312 ret = exprNode_while (exprNode_effect (exprData_getPairA (data)),
9313 exprNode_effect (exprData_getPairB (data)));
9317 ret = exprNode_doWhile (exprNode_effect (exprData_getPairA (data)),
9318 exprNode_effect (exprData_getPairB (data)));
9322 ret = exprNode_makeBlock (exprNode_effect (exprData_getSingle (data)));
9326 ret = exprNode_statement (exprNode_effect (exprData_getUopNode (data)),
9327 exprData_getUopTok (data));
9331 ret = exprNode_concat (exprNode_effect (exprData_getPairA (data)),
9332 exprNode_effect (exprData_getPairB (data)));
9337 ret = exprNode_caseMarker
9338 (exprNode_effect (exprData_getSingle (data)),
9344 ret = exprNode_createTok (exprData_getTok (data));
9348 ret = exprNode_switch (exprNode_effect (exprData_getPairA (data)),
9349 exprNode_effect (exprData_getPairB (data)));
9353 ret = exprNode_makeInitialization
9354 (exprData_getInitId (data),
9355 exprNode_effect (exprData_getInitNode (data)));
9359 ret = exprNode_fieldAccessAux
9360 (exprNode_effect (exprData_getFieldNode (data)),
9361 exprNode_loc (exprData_getFieldNode (data)),
9362 cstring_copy (exprData_getFieldName (data)));
9366 ret = exprNode_arrowAccessAux
9367 (exprNode_effect (exprData_getFieldNode (data)),
9368 exprNode_loc (exprData_getFieldNode (data)),
9369 cstring_copy (exprData_getFieldName (data)));
9372 case XPR_STRINGLITERAL:
9386 /*@=observertrans@*/
9388 /*@=dependenttrans@*/
9399 static /*@observer@*/ cstring exprNode_rootVarName (exprNode e)
9404 if (exprNode_isError (e))
9406 return cstring_undefined;
9414 ret = exprNode_rootVarName (exprData_getUopNode (data));
9417 ret = exprNode_rootVarName (exprData_getOpA (data));
9421 ret = exprData_getId (data);
9424 ret = idDecl_getName (exprData_getInitId (data));
9449 case XPR_NULLRETURN:
9471 case XPR_STRINGLITERAL:
9472 ret = cstring_undefined;
9479 static /*@only@*/ cstring exprNode_doUnparse (exprNode e)
9484 if (exprNode_isError (e))
9486 static /*@only@*/ cstring error = cstring_undefined;
9488 if (!cstring_isDefined (error))
9490 error = cstring_makeLiteral ("<error>");
9501 ret = message ("(%s)", exprNode_unparse (exprData_getUopNode (e->edata)));
9504 ret = message ("%s %s %s",
9505 exprNode_unparse (exprData_getOpA (data)),
9506 lltok_unparse (exprData_getOpTok (data)),
9507 exprNode_unparse (exprData_getOpB (data)));
9510 ret = message ("%s(%q)",
9511 exprNode_unparse (exprData_getFcn (data)),
9512 exprNodeList_unparse (exprData_getArgs (data)));
9515 ret = message ("{ %q }", exprNodeList_unparse (exprData_getArgs (data)));
9518 ret = cstring_undefined;
9521 ret = message ("%s:", exprData_getId (data));
9525 ret = cstring_copy (exprData_getId (data));
9528 ret = message ("%s[%s]", exprNode_unparse (exprData_getPairA (data)),
9529 exprNode_unparse (exprData_getPairB (data)));
9532 ret = message ("<body>");
9535 ret = message ("%s %s %s",
9536 exprNode_unparse (exprData_getOpA (data)),
9537 lltok_unparse (exprData_getOpTok (data)),
9538 exprNode_unparse (exprData_getOpB (data)));
9542 ret = message ("%s%s",
9543 lltok_unparse (exprData_getUopTok (data)),
9544 exprNode_unparse (exprData_getUopNode (data)));
9548 ret = message ("%s%s",
9549 exprNode_unparse (exprData_getUopNode (data)),
9550 lltok_unparse (exprData_getUopTok (data)));
9554 ret = message ("offsetof(%s,%q)",
9555 ctype_unparse (qtype_getType (exprData_getOffsetType (data))),
9556 cstringList_unparseSep (exprData_getOffsetName (data), cstring_makeLiteralTemp (".")));
9560 ret = message ("sizeof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9564 ret = message ("sizeof(%s)", exprNode_unparse (exprData_getSingle (data)));
9568 ret = message ("alignof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9572 ret = message ("alignof(%s)", exprNode_unparse (exprData_getSingle (data)));
9576 ret = message ("va_arg(%s, %q)",
9577 exprNode_unparse (exprData_getCastNode (data)),
9578 qtype_unparse (exprData_getCastType (data)));
9582 ret = message ("%q(%q)",
9583 uentry_getName (exprData_getIterCallIter (data)),
9584 exprNodeList_unparse (exprData_getIterCallArgs (data)));
9587 ret = message ("%q(%q) %s %q",
9588 uentry_getName (exprData_getIterSname (data)),
9589 exprNodeList_unparse (exprData_getIterAlist (data)),
9590 exprNode_unparse (exprData_getIterBody (data)),
9591 uentry_getName (exprData_getIterEname (data)));
9594 ret = message ("(%q)%s",
9595 qtype_unparse (exprData_getCastType (data)),
9596 exprNode_unparse (exprData_getCastNode (data)));
9600 ret = message ("%s %s",
9601 exprNode_unparse (exprData_getPairA (data)),
9602 exprNode_unparse (exprData_getPairB (data)));
9606 ret = message ("for (%s; %s; %s)",
9607 exprNode_unparse (exprData_getTripleInit (data)),
9608 exprNode_unparse (exprData_getTripleTest (data)),
9609 exprNode_unparse (exprData_getTripleInc (data)));
9613 ret = message ("goto %s", exprData_getLiteral (data));
9617 ret = cstring_makeLiteral ("continue");
9621 ret = cstring_makeLiteral ("break");
9625 ret = message ("return %s", exprNode_unparse (exprData_getSingle (data)));
9628 case XPR_NULLRETURN:
9629 ret = cstring_makeLiteral ("return");
9633 ret = message ("%s, %s",
9634 exprNode_unparse (exprData_getPairA (data)),
9635 exprNode_unparse (exprData_getPairB (data)));
9639 ret = message ("%s ? %s : %s",
9640 exprNode_unparse (exprData_getTriplePred (data)),
9641 exprNode_unparse (exprData_getTripleTrue (data)),
9642 exprNode_unparse (exprData_getTripleFalse (data)));
9645 ret = message ("if (%s) %s",
9646 exprNode_unparse (exprData_getPairA (data)),
9647 exprNode_unparse (exprData_getPairB (data)));
9651 ret = message ("if (%s) %s else %s",
9652 exprNode_unparse (exprData_getTriplePred (data)),
9653 exprNode_unparse (exprData_getTripleTrue (data)),
9654 exprNode_unparse (exprData_getTripleFalse (data)));
9657 ret = message ("while (%s) %s",
9658 exprNode_unparse (exprData_getPairA (data)),
9659 exprNode_unparse (exprData_getPairB (data)));
9663 ret = cstring_copy (exprNode_unparse (exprData_getSingle (data)));
9667 ret = cstring_copy (lltok_unparse (exprData_getTok (data)));
9671 ret = message ("do { %s } while (%s)",
9672 exprNode_unparse (exprData_getPairB (data)),
9673 exprNode_unparse (exprData_getPairA (data)));
9677 ret = message ("{ %s }", exprNode_unparse (exprData_getSingle (data)));
9678 /* evans 2002-02-20 was unparseFirst! */
9682 ret = message ("%s;", exprNode_unparse (exprData_getUopNode (data)));
9686 if (exprNode_isStatement (exprData_getPairA (data)))
9689 ** statement expressions already print the ;
9692 ret = message ("%s %s",
9693 exprNode_unparse (exprData_getPairA (data)),
9694 exprNode_unparse (exprData_getPairB (data)));
9698 ret = message ("%s; %s",
9699 exprNode_unparse (exprData_getPairA (data)),
9700 exprNode_unparse (exprData_getPairB (data)));
9706 ret = cstring_makeLiteral ("default:");
9710 ret = message ("switch (%s) %s",
9711 exprNode_unparse (exprData_getPairA (data)),
9712 exprNode_unparse (exprData_getPairB (data)));
9717 ret = message ("case %s:",
9718 exprNode_unparse (exprData_getSingle (data)));
9722 if (exprNode_isError (exprData_getInitNode (data)))
9724 ret = message ("%q", idDecl_unparseC (exprData_getInitId (data)));
9728 ret = message ("%q = %s",
9729 idDecl_unparseC (exprData_getInitId (data)),
9730 exprNode_unparse (exprData_getInitNode (data)));
9735 ret = message ("%s.%s",
9736 exprNode_unparse (exprData_getFieldNode (data)),
9737 exprData_getFieldName (data));
9741 ret = message ("%s->%s",
9742 exprNode_unparse (exprData_getFieldNode (data)),
9743 exprData_getFieldName (data));
9746 case XPR_STRINGLITERAL:
9747 if (ctype_isWideString (e->typ))
9749 ret = message ("L\"%s\"", exprData_getLiteral (data));
9753 ret = message ("\"%s\"", exprData_getLiteral (data));
9758 ret = cstring_copy (exprData_getLiteral (data));
9762 ret = cstring_makeLiteral ("<node>");
9770 exprNode_isInitializer (exprNode e)
9772 return (exprNode_isDefined (e)
9773 && e->kind == XPR_INIT);
9777 exprNode_isCharLiteral (exprNode e)
9779 if (exprNode_isDefined (e))
9781 return (multiVal_isChar (exprNode_getValue (e)));
9790 exprNode_isNumLiteral (exprNode e)
9792 if (exprNode_isDefined (e))
9794 return (multiVal_isInt (exprNode_getValue (e)));
9803 exprNode_isFalseConstant (exprNode e)
9805 if (exprNode_isDefined (e))
9807 cstring s = exprNode_rootVarName (e);
9809 if (cstring_equal (s, context_getFalseName ()))
9819 exprNode_matchLiteral (ctype expected, exprNode e)
9821 if (exprNode_isDefined (e))
9823 multiVal m = exprNode_getValue (e);
9825 if (multiVal_isDefined (m))
9827 if (multiVal_isInt (m))
9829 long int val = multiVal_forceInt (m);
9831 if (ctype_isNumAbstract (expected)
9832 && context_flagOn (FLG_NUMABSTRACTLIT, exprNode_loc (e)))
9837 if (ctype_isDirectBool (ctype_realishType (expected)))
9841 return FALSE; /* really?! return TRUE; allow use of 0 for FALSE */
9849 if (ctype_isRealInt (expected))
9852 ** unsigned <- [ constant >= 0 is okay ]
9855 if (ctype_isUnsigned (expected))
9864 ** No checks on sizes of integers...maybe add
9868 DPRINTF (("Here: %s => %s", exprNode_unparse (e), ctype_unparse (expected)));
9869 DPRINTF (("Type: %s / %s", ctype_unparse (exprNode_getType (e)),
9870 bool_unparse (ctype_isInt (exprNode_getType (e)))));
9872 if (context_getFlag (FLG_NUMLITERAL)
9873 && (ctype_isRegularInt (exprNode_getType (e)) || val == 0)) {
9879 return FALSE; /* evs 2000-05-17: previously, always returned TRUE */
9883 else if (ctype_isChar (expected))
9887 else if (ctype_isArrayPtr (expected))
9890 ** evans 2001-10-14: We allow 0 to match any pointer, but only if the type matches or is void *.
9895 if (ctype_match (exprNode_getType (e), expected)
9896 || ctype_isVoidPointer (exprNode_getType (e)))
9906 else if (ctype_isAnyFloat (expected))
9908 return (context_getFlag (FLG_NUMLITERAL));
9915 else if (multiVal_isDouble (m))
9917 if (ctype_isAnyFloat (expected))
9922 else if (multiVal_isChar (m))
9924 char val = multiVal_forceChar (m);
9926 if (ctype_isChar (expected))
9928 if (ctype_isUnsigned (expected) && ((int)val) < 0)
9949 exprNode_matchType (ctype expected, exprNode e)
9953 if (!exprNode_isDefined (e)) return TRUE;
9955 actual = ctype_realishType (exprNode_getType (e));
9957 if (ctype_match (ctype_realishType (expected), actual))
9962 llassert (!exprNode_isError (e));
9963 return (exprNode_matchLiteral (expected, e));
9967 exprNode_matchTypes (exprNode e1, exprNode e2)
9972 if (!exprNode_isDefined (e1)) return TRUE;
9973 if (!exprNode_isDefined (e2)) return TRUE;
9976 ** realish type --- keep bools, bools
9979 t1 = ctype_realishType (exprNode_getType (e1));
9980 t2 = ctype_realishType (exprNode_getType (e2));
9982 if (ctype_match (t1, t2))
9987 DPRINTF (("Matching literal! %s %s %s %s",
9988 ctype_unparse (t1), exprNode_unparse (e2),
9989 ctype_unparse (t2), exprNode_unparse (e1)));
9991 return (exprNode_matchLiteral (t1, e2) || exprNode_matchLiteral (t2, e1));
9999 exprNode_matchArgType (ctype ct, exprNode e)
10003 if (!exprNode_isDefined (e))
10008 et = ctype_realType (exprNode_getType (e));
10010 if (ctype_matchArg (ct, et)) return TRUE;
10012 llassert (!exprNode_isError (e));
10013 return (exprNode_matchLiteral (ct, e));
10016 static /*@only@*/ exprNodeSList
10017 exprNode_flatten (/*@dependent@*/ exprNode e) /*@*/
10019 if (exprNode_isDefined (e))
10021 if (e->kind == XPR_STMTLIST)
10023 return (exprNodeSList_append
10024 (exprNode_flatten (exprData_getPairA (e->edata)),
10025 exprNode_flatten (exprData_getPairB (e->edata))));
10027 else if (e->kind == XPR_BLOCK)
10029 return (exprNode_flatten (exprData_getSingle (e->edata)));
10033 return (exprNodeSList_singleton (e));
10037 return exprNodeSList_new ();
10040 static /*@exposed@*/ exprNode
10041 exprNode_lastStatement (/*@returned@*/ exprNode e)
10043 if (exprNode_isDefined (e))
10045 if (e->kind == XPR_STMTLIST)
10047 exprNode b = exprData_getPairB (e->edata);
10049 if (exprNode_isDefined (b))
10051 return exprNode_lastStatement (b);
10055 return exprNode_lastStatement (exprData_getPairA (e->edata));
10058 else if (e->kind == XPR_BLOCK)
10060 return (exprNode_lastStatement (exprData_getSingle (e->edata)));
10068 return exprNode_undefined;
10071 static /*@exposed@*/ exprNode
10072 exprNode_firstStatement (/*@returned@*/ exprNode e)
10074 if (exprNode_isDefined (e))
10076 if (e->kind == XPR_STMTLIST)
10078 exprNode b = exprData_getPairA (e->edata);
10080 if (exprNode_isDefined (b))
10082 return exprNode_firstStatement (b);
10086 return exprNode_firstStatement (exprData_getPairB (e->edata));
10089 else if (e->kind == XPR_BLOCK)
10091 return (exprNode_firstStatement (exprData_getSingle (e->edata)));
10099 return exprNode_undefined;
10103 exprNode_mergeUSs (exprNode res, exprNode other)
10105 if (exprNode_isDefined (res) && exprNode_isDefined (other))
10107 res->msets = sRefSet_union (res->msets, other->msets);
10108 res->sets = sRefSet_union (res->sets, other->sets);
10109 res->uses = sRefSet_union (res->uses, other->uses);
10114 exprNode_mergeCondUSs (exprNode res, exprNode other1, exprNode other2)
10116 if (exprNode_isDefined (res))
10118 if (exprNode_isDefined (other1))
10120 res->sets = sRefSet_union (res->sets, other1->sets);
10121 res->msets = sRefSet_union (res->msets, other1->msets);
10122 res->uses = sRefSet_union (res->uses, other1->uses);
10124 if (exprNode_isDefined (other2))
10126 res->sets = sRefSet_union (res->sets, other2->sets);
10127 res->msets = sRefSet_union (res->msets, other2->msets);
10128 res->uses = sRefSet_union (res->uses, other2->uses);
10134 ** modifies e->uses
10136 ** Reports errors is s is not defined.
10140 exprNode_addUse (exprNode e, /*@exposed@*/ sRef s)
10142 if (exprNode_isDefined (e))
10144 e->uses = sRefSet_insert (e->uses, s);
10149 exprNode_checkUse (exprNode e, /*@exposed@*/ sRef s, fileloc loc)
10151 if (sRef_isKnown (s) && !sRef_isConst (s))
10154 ** need to check all outer types are useable
10157 DPRINTF (("Check use: %s / %s",
10158 exprNode_unparse (e), sRef_unparse (s)));
10160 exprNode_addUse (e, s);
10162 if (!context_inProtectVars ())
10165 ** only report the deepest error
10168 sRef errorRef = sRef_undefined;
10169 sRef lastRef = sRef_undefined;
10170 bool deadRef = FALSE;
10171 bool unuseable = FALSE;
10172 bool errorMaybe = FALSE;
10174 while (sRef_isValid (s) && sRef_isKnown (s))
10176 ynm readable = sRef_isValidLvalue (s);
10178 DPRINTF (("Readable: %s / %s",
10179 sRef_unparseFull (s), ynm_unparse (readable)));
10181 if (!(ynm_toBoolStrict (readable)))
10183 if (ynm_isMaybe (readable))
10185 lastRef = errorRef;
10187 DPRINTF (("Setting ERROR: %s", sRef_unparseFull (s)));
10188 deadRef = sRef_isPossiblyDead (errorRef);
10189 unuseable = sRef_isUnuseable (errorRef);
10194 lastRef = errorRef;
10196 DPRINTF (("Setting ERROR: %s", sRef_unparseFull (s)));
10197 deadRef = sRef_isDead (errorRef);
10198 unuseable = sRef_isUnuseable (errorRef);
10199 errorMaybe = FALSE;
10203 if (!sRef_isPartial (s))
10205 DPRINTF (("Defining! %s", sRef_unparseFull (s)));
10206 sRef_setDefined (s, loc);
10207 DPRINTF (("Defining! %s", sRef_unparseFull (s)));
10212 s = sRef_getBaseSafe (s);
10215 if (sRef_isValid (errorRef))
10217 if (sRef_isValid (lastRef) && sRef_isField (lastRef)
10218 && sRef_isPointer (errorRef))
10220 errorRef = lastRef;
10221 DPRINTF (("errorRef: %s", sRef_unparseFull (errorRef)));
10226 if (sRef_isThroughArrayFetch (errorRef))
10229 (FLG_STRICTUSERELEASED,
10230 message ("%q %q may be used after being released",
10231 sRef_unparseKindNamePlain (errorRef),
10232 sRef_unparse (errorRef)),
10235 sRef_showRefKilled (errorRef);
10237 if (sRef_isKept (errorRef))
10239 sRef_clearAliasState (errorRef, loc);
10245 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
10249 message ("%q %q %qused after being released",
10250 sRef_unparseKindNamePlain (errorRef),
10251 sRef_unparse (errorRef),
10252 cstring_makeLiteral (errorMaybe
10253 ? "may be " : "")),
10256 sRef_showRefKilled (errorRef);
10258 if (sRef_isKept (errorRef))
10260 sRef_clearAliasState (errorRef, loc);
10265 else if (unuseable)
10269 message ("%q %q%qused in inconsistent state",
10270 sRef_unparseKindName (errorRef),
10271 sRef_unparseOpt (errorRef),
10272 cstring_makeLiteral (errorMaybe ? "may be " : "")),
10275 sRef_showStateInconsistent (errorRef);
10280 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
10284 message ("%q %q%qused before definition",
10285 sRef_unparseKindName (errorRef),
10286 sRef_unparseOpt (errorRef),
10287 cstring_makeLiteral (errorMaybe ? "may be " : "")),
10293 DPRINTF (("Error: %s", sRef_unparseFull (errorRef)));
10296 sRef_setDefined (errorRef, loc);
10298 if (sRef_isAddress (errorRef))
10300 sRef_setDefined (sRef_getRootBase (errorRef), loc);
10302 } /* end is error */
10310 checkSafeUse (exprNode e, /*@exposed@*/ sRef s)
10312 if (exprNode_isDefined (e) && sRef_isKnown (s))
10314 e->uses = sRefSet_insert (e->uses, s);
10319 exprNode_checkSetAny (exprNode e, /*@dependent@*/ cstring name)
10321 if (exprNode_isDefined (e))
10323 e->sets = sRefSet_insert (e->sets, sRef_makeUnconstrained (name));
10328 exprNode_checkSet (exprNode e, /*@exposed@*/ sRef s)
10330 sRef defines = sRef_undefined;
10332 if (sRef_isValid (s) && !sRef_isNothing (s))
10334 uentry ue = sRef_getBaseUentry (s);
10336 if (uentry_isValid (ue))
10338 uentry_setLset (ue);
10341 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
10343 voptgenerror (FLG_USEDEF,
10344 message ("Attempt to set unuseable storage: %q",
10349 if (sRef_isMeaningful (s))
10351 if (sRef_isDead (s))
10353 sRef base = sRef_getBaseSafe (s);
10355 if (sRef_isValid (base)
10356 && sRef_isDead (base))
10358 sRef_setPartial (s, exprNode_loc (e));
10361 defines = s; /* okay - modifies for only param */
10363 else if (sRef_isPartial (s))
10365 sRef eref = exprNode_getSref (e);
10367 if (!sRef_isPartial (eref))
10370 ** should do something different here???
10373 sRef_setDefinedComplete (eref, exprNode_loc (e));
10377 sRef_setPartialDefinedComplete (eref, exprNode_loc (e));
10380 if (sRef_isMeaningful (eref))
10389 else if (sRef_isAllocated (s))
10391 sRef eref = exprNode_getSref (e);
10394 if (!sRef_isAllocated (eref))
10396 sRef_setDefinedComplete (eref, exprNode_loc (e));
10400 sRef base = sRef_getBaseSafe (eref);
10402 if (sRef_isValid (base))
10404 sRef_setPdefined (base, exprNode_loc (e));
10412 sRef_setDefinedNCComplete (s, exprNode_loc (e));
10417 else /* not meaningful...but still need to insert it */
10423 if (exprNode_isDefined (e) && sRef_isValid (defines))
10425 e->sets = sRefSet_insert (e->sets, defines);
10430 exprNode_checkMSet (exprNode e, /*@exposed@*/ sRef s)
10432 if (sRef_isValid (s) && !sRef_isNothing (s))
10434 uentry ue = sRef_getBaseUentry (s);
10436 if (uentry_isValid (ue))
10438 uentry_setLset (ue);
10441 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
10443 voptgenerror (FLG_USEDEF,
10444 message ("Attempt to set unuseable storage: %q", sRef_unparse (s)),
10448 if (sRef_isMeaningful (s))
10450 sRef_setDefinedComplete (s, exprNode_loc (e));
10453 if (exprNode_isDefined (e))
10455 e->msets = sRefSet_insert (e->msets, s);
10461 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode fcn, uentryList params, exprNodeList args)
10463 checkAnyCall (fcn, cstring_undefined, params, args,
10464 FALSE, sRefSet_undefined, FALSE, 0);
10468 checkOneArg (uentry ucurrent, /*@notnull@*/ exprNode current,
10469 /*@dependent@*/ exprNode fcn, bool isSpec, int argno, int totargs)
10473 if (uentry_isYield (ucurrent))
10475 sRef_setDefined (exprNode_getSref (current), exprNode_loc (current));
10476 exprNode_checkSet (current, current->sref);
10480 if (uentry_isSefParam (ucurrent))
10482 sRefSet sets = current->sets;
10483 sRef ref = exprNode_getSref (current);
10485 if (sRef_isMacroParamRef (ref))
10487 uentry ue = sRef_getUentry (ref);
10489 if (!uentry_isSefParam (ue))
10494 ("Parameter %d to %s is declared sef, but "
10495 "the argument is a macro parameter declared "
10497 argno, exprNode_unparse (fcn),
10498 exprNode_unparse (current)),
10499 exprNode_loc (current));
10503 if (!sRefSet_isEmpty (sets))
10505 sRefSet reported = sRefSet_undefined;
10507 sRefSet_realElements (current->sets, el)
10509 if (sRefSet_isSameNameMember (reported, el))
10511 ; /* don't report again */
10515 if (sRef_isUnconstrained (el))
10520 ("Parameter %d to %s is declared sef, but "
10521 "the argument calls unconstrained function %s "
10522 "(no guarantee it will not modify something): %s",
10523 argno, exprNode_unparse (fcn),
10524 sRef_unconstrainedName (el),
10525 exprNode_unparse (current)),
10526 exprNode_loc (current));
10533 ("Parameter %d to %s is declared sef, but "
10534 "the argument may modify %q: %s",
10535 argno, exprNode_unparse (fcn),
10537 exprNode_unparse (current)),
10538 exprNode_loc (current));
10541 } end_sRefSet_realElements;
10545 transferChecks_passParam (current, ucurrent, isSpec, fcn, argno, totargs);
10546 exprNode_mergeUSs (fcn, current);
10551 checkAnyCall (/*@dependent@*/ exprNode fcn,
10552 /*@dependent@*/ cstring fname,
10555 bool hasMods, sRefSet mods,
10560 int nargs = exprNodeList_size (args);
10565 ** concat all args ud's to f, add each arg sref as a use unless
10566 ** it was specified as "out", in which case it is a def.
10569 uentryList_reset (pn);
10572 ** aliasing checks:
10574 ** if paramn is only or unique, no other arg may alias argn
10577 exprNodeList_elements (args, current)
10581 if (exprNode_isDefined (current))
10583 if ((!uentryList_isUndefined (pn) && !uentryList_isFinished (pn)))
10585 uentry ucurrent = uentryList_current (pn);
10587 if (specialArgs == 0
10588 || (paramno < specialArgs))
10590 checkOneArg (ucurrent, current, fcn, isSpec, paramno, nargs);
10592 if (context_maybeSet (FLG_ALIASUNIQUE))
10594 if (uentry_isOnly (ucurrent)
10595 || uentry_isUnique (ucurrent))
10597 checkUniqueParams (fcn, current, args,
10598 paramno, ucurrent);
10603 else /* uentry is undefined */
10605 if (specialArgs == 0)
10607 exprNode_checkUseParam (current);
10610 exprNode_mergeUSs (fcn, current);
10613 uentryList_advanceSafe (pn);
10614 } end_exprNodeList_elements;
10620 sRefSet_allElements (mods, s)
10623 sRef rb = sRef_getRootBase (s);
10625 if (sRef_isFileOrGlobalScope (rb))
10627 context_usedGlobal (rb);
10630 fb = sRef_fixBaseParam (s, args);
10632 if (!sRef_isMacroParamRef (fb))
10634 if (sRef_isNothing (fb))
10640 if (sRef_isValid (fb))
10642 uentry ue = sRef_getBaseUentry (s);
10644 if (uentry_isValid (ue))
10646 uentry_setLset (ue);
10650 fcn->sets = sRefSet_insert (fcn->sets, fb);
10653 sRef_clearDerivedComplete (s);
10654 } end_sRefSet_allElements;
10660 if (context_hasMods ())
10662 if (context_maybeSet (FLG_MODUNCON))
10666 message ("Undetected modification possible "
10667 "from call to unconstrained function %s: %s",
10669 exprNode_unparse (fcn)),
10670 exprNode_loc (fcn));
10675 if (context_maybeSet (FLG_MODUNCONNOMODS)
10676 && !(context_inIterDef () || context_inIterEnd ()))
10679 (FLG_MODUNCONNOMODS,
10680 message ("Undetected modification possible "
10681 "from call to unconstrained function %s: %s",
10683 exprNode_unparse (fcn)),
10684 exprNode_loc (fcn));
10688 exprNode_checkSetAny (fcn, fname);
10692 void exprNode_checkUseParam (exprNode current)
10694 if (exprNode_isDefined (current))
10696 exprNode_checkUse (current, current->sref, current->loc);
10701 checkNumerics (ctype tr1, ctype tr2, ctype te1, ctype te2,
10702 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10707 if (!ctype_match (tr1, tr2))
10709 if ((ctype_isRealInt (tr1) || ctype_isReal (tr1)) &&
10710 (ctype_isRealInt (tr2) || ctype_isReal (tr2)))
10712 DPRINTF (("No error: [%s] %s / [%s] %s",
10713 exprNode_unparse (e1), ctype_unparse (tr1),
10714 exprNode_unparse (e2), ctype_unparse (tr2)));
10716 ret = ctype_biggerType (tr1, tr2);
10720 if (ctype_isNumAbstract (tr1)
10721 && exprNode_isNumLiteral (e2)
10722 && context_flagOn (FLG_NUMABSTRACTLIT, e1->loc))
10724 ret = tr1; /* No error */
10726 else if (ctype_isNumAbstract (tr2)
10727 && exprNode_isNumLiteral (e1)
10728 && context_flagOn (FLG_NUMABSTRACTLIT, e1->loc))
10736 message ("Incompatible types for %s (%s, %s): %s %s %s",
10737 lltok_unparse (op),
10738 ctype_unparse (te1),
10739 ctype_unparse (te2),
10740 exprNode_unparse (e1), lltok_unparse (op),
10741 exprNode_unparse (e2)),
10744 ret = ctype_unknown;
10748 ret = ctype_biggerType (tr1, tr2);
10755 if (ctype_isNumAbstract (tr1))
10759 else if (ctype_isForceRealNumeric (&tr1)
10760 && ctype_isForceRealNumeric (&tr2))
10762 ret = ctype_resolveNumerics (tr1, tr2);
10764 else if (!context_msgStrictOps ())
10766 if (ctype_isPointer (tr1))
10768 if (ctype_isPointer (tr2) && !exprNode_isNullValue (e2))
10772 else if (ctype_isInt (tr2))
10778 ret = ctype_unknown;
10781 else if (ctype_isPointer (tr2))
10783 if (ctype_isPointer (tr1))
10787 else if (ctype_isInt (tr1))
10793 ret = ctype_unknown;
10798 ret = ctype_resolveNumerics (tr1, tr2);
10803 int opid = lltok_getTok (op);
10804 bool comparop = (opid == EQ_OP || opid == NE_OP
10805 || opid == TLT || opid == TGT
10806 || opid == LE_OP || opid == GE_OP);
10808 if (!ctype_isNumeric (tr1) && !ctype_isNumeric (tr2))
10811 && ((ctype_isEnum (tr1) && ctype_isEnum (tr2))
10812 || (ctype_isBool (tr1) && ctype_isBool (tr2))
10813 || (ctype_isChar (tr1) && ctype_isChar (tr2))))
10819 if (ctype_sameName (te1, te2))
10823 message ("Operands of %s are non-numeric (%t): %s %s %s",
10824 lltok_unparse (op), te1,
10825 exprNode_unparse (e1), lltok_unparse (op),
10826 exprNode_unparse (e2)),
10833 message ("Operands of %s are non-numerics (%t, %t): %s %s %s",
10834 lltok_unparse (op), te1, te2,
10835 exprNode_unparse (e1), lltok_unparse (op),
10836 exprNode_unparse (e2)),
10841 else if (!ctype_isNumeric (tr1))
10845 message ("Right operand of %s is non-numeric (%t): %s %s %s",
10846 lltok_unparse (op), te1,
10847 exprNode_unparse (e1), lltok_unparse (op),
10848 exprNode_unparse (e2)),
10853 if (!ctype_isNumeric (tr2))
10857 message ("Left operand of %s is non-numeric (%t): %s %s %s",
10858 lltok_unparse (op), te2,
10859 exprNode_unparse (e1), lltok_unparse (op),
10860 exprNode_unparse (e2)),
10865 ret = ctype_unknown;
10873 abstractOpError (ctype tr1, ctype tr2, lltok op,
10874 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10875 fileloc loc1, fileloc loc2)
10877 if (ctype_isRealAbstract (tr1) && ctype_isRealAbstract (tr2))
10879 if (ctype_match (tr1, tr2))
10881 if (ctype_isRealNumAbstract (tr1))
10883 ; /* No warning for numabstract types */
10887 if (lltok_isEqOp (op) || lltok_isNotEqOp (op))
10890 (FLG_ABSTRACTCOMPARE,
10891 message ("Object equality comparison (%s) on objects of abstract type (%t): %s %s %s",
10892 lltok_unparse (op), tr1,
10893 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10900 message ("Operands of %s are abstract type (%t): %s %s %s",
10901 lltok_unparse (op), tr1,
10902 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10909 if (ctype_isRealNumAbstract (tr1) && ctype_isRealNumAbstract (tr2))
10914 ("Operands of %s are different numabstract types (%t, %t): %s %s %s",
10915 lltok_unparse (op), tr1, tr2,
10916 exprNode_unparse (e1),
10917 lltok_unparse (op), exprNode_unparse (e2)),
10924 message ("Operands of %s are abstract types (%t, %t): %s %s %s",
10925 lltok_unparse (op), tr1, tr2,
10926 exprNode_unparse (e1), lltok_unparse (op),
10927 exprNode_unparse (e2)),
10932 else if (ctype_isRealAbstract (tr1) && !ctype_isRealNumAbstract (tr1))
10936 message ("Left operand of %s is abstract type (%t): %s %s %s",
10937 lltok_unparse (op), tr1,
10938 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10943 if (ctype_isRealAbstract (tr2) && !ctype_isRealNumAbstract (tr2))
10947 message ("Right operand of %s is abstract type (%t): %s %s %s",
10948 lltok_unparse (op), tr2,
10949 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10960 ** requies e1 and e2 and not error exprNode's.
10964 ** If e1 is a component of an abstract type, and e2 is mutable and client-visible,
10965 ** the rep of the abstract type is exposed.
10967 ** The order is very important:
10969 ** check rep expose (move into check transfer)
10975 ** This isn't really a sensible procedure, but the indententation
10976 ** was getting too deep.
10980 checkOneRepExpose (sRef ysr, sRef base,
10981 /*@notnull@*/ exprNode e1,
10982 /*@notnull@*/ exprNode e2, ctype ct,
10985 if (!(sRef_isOnly (ysr) || sRef_isKeep (ysr)
10986 || sRef_isOwned (ysr)
10987 || sRef_isExposed (ysr)))
10989 if (sRef_isAnyParam (base) && !sRef_isExposed (base)
10990 && !sRef_isObserver (base)) /* evans 2001-07-11: added isObserver */
10993 if (sRef_isIReference (ysr))
10995 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
11000 ("Assignment of mutable component of parameter %q "
11001 "to component of abstract "
11002 "type %s exposes rep: %s = %s",
11003 sRef_unparse (base),
11004 ctype_unparse (ct),
11005 exprNode_unparse (e1), exprNode_unparse (e2)),
11013 ("Assignment of mutable component of parameter %q "
11014 "(through alias %q) to component of abstract "
11015 "type %s exposes rep: %s = %s",
11016 sRef_unparse (base),
11017 sRef_unparse (e2->sref),
11018 ctype_unparse (ct),
11019 exprNode_unparse (e1), exprNode_unparse (e2)),
11025 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
11029 message ("Assignment of mutable parameter %q "
11030 "to component of abstract type %s "
11031 "exposes rep: %s = %s",
11032 sRef_unparse (base),
11033 ctype_unparse (ct),
11034 exprNode_unparse (e1),
11035 exprNode_unparse (e2)),
11042 message ("Assignment of mutable parameter %q "
11043 "(through alias %q) to "
11044 "component of abstract type %s exposes "
11046 sRef_unparse (base),
11047 sRef_unparse (e2->sref),
11048 ctype_unparse (ct),
11049 exprNode_unparse (e1),
11050 exprNode_unparse (e2)),
11056 if (sRef_isFileOrGlobalScope (s2b))
11058 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
11062 message ("Assignment of global %q "
11064 "abstract type %s exposes rep: %s = %s",
11065 sRef_unparse (base),
11066 ctype_unparse (ct),
11067 exprNode_unparse (e1), exprNode_unparse (e2)),
11074 message ("Assignment of global %q (through alias %q) "
11076 "abstract type %s exposes rep: %s = %s",
11077 sRef_unparse (base),
11078 sRef_unparse (e2->sref),
11079 ctype_unparse (ct),
11080 exprNode_unparse (e1), exprNode_unparse (e2)),
11088 doAssign (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2, bool isInit)
11090 DPRINTF (("Do assign: %s <- %s",
11091 exprNode_unparse (e1), exprNode_unparse (e2)));
11092 DPRINTF (("Ctype: %s", ctype_unparse (exprNode_getType (e1))));
11094 if (ctype_isRealFunction (exprNode_getType (e1))
11095 && !ctype_isRealPointer (exprNode_getType (e1)))
11099 message ("Invalid left-hand side of assignment (function type %s): %s",
11100 ctype_unparse (exprNode_getType (e1)),
11101 exprNode_unparse (e1)),
11105 if (context_getFlag (FLG_ASSIGNEXPOSE) && ctype_isMutable (e2->typ))
11107 ctype t2 = exprNode_getType (e2);
11108 sRef sr = sRef_getRootBase (e1->sref);
11109 ctype ct = sRef_getType (sr);
11111 if (ctype_isAbstract (t2)
11112 && !(uentry_isMutableDatatype (usymtab_getTypeEntry (ctype_typeId (t2)))))
11114 /* it is immutable, okay to reference */
11115 goto donerepexpose;
11118 if (ctype_isAbstract (ct) && sRef_isIReference (e1->sref))
11120 sRef s2b = sRef_getRootBase (e2->sref);
11121 sRef s1 = e1->sref;
11122 sRef s1b = sRef_getRootBase (s1);
11125 aliases = usymtab_canAlias (e2->sref);
11127 if (!sRef_similar (s2b, s1b)
11128 && !sRef_isExposed (s1)
11129 && !(sRef_isOnly (s2b) || sRef_isKeep (s2b) || sRef_isExposed (s2b)))
11131 if (sRef_isAnyParam (s2b) && !sRef_isOnly (s2b)
11132 && !sRef_isOwned (s2b) && !sRef_isKeep (s2b)
11133 && !sRef_isExposed (s2b))
11135 if (sRef_isIReference (e2->sref))
11140 ("Assignment of mutable component of parameter %q "
11141 "to component of abstract type %s exposes rep: %s = %s",
11142 sRef_unparse (s2b),
11143 ctype_unparse (ct),
11144 exprNode_unparse (e1), exprNode_unparse (e2)),
11151 message ("Assignment of mutable parameter %q to "
11152 "component of abstract type %s exposes rep: %s = %s",
11153 sRef_unparse (s2b),
11154 ctype_unparse (ct),
11155 exprNode_unparse (e1), exprNode_unparse (e2)),
11160 if (sRef_isFileOrGlobalScope (s2b))
11164 message ("Assignment of global %q to component of "
11165 "abstract type %s exposes rep: %s = %s",
11166 sRef_unparse (s2b),
11167 ctype_unparse (ct),
11168 exprNode_unparse (e1), exprNode_unparse (e2)),
11172 sRefSet_realElements (aliases, ysr)
11174 sRef base = sRef_getRootBase (ysr);
11176 if (sRef_similar (ysr, s2b) || sRef_similar (s1b, base)
11177 || sRef_sameName (base, s1b))
11179 ; /* error already reported or same sref */
11183 checkOneRepExpose (ysr, base, e1, e2, ct, s2b);
11185 } end_sRefSet_realElements;
11187 sRefSet_free (aliases);
11194 ** function variables don't really work...
11197 if (!ctype_isFunction (ctype_realType (e2->typ)))
11201 DPRINTF (("Check init: %s / %s",
11202 exprNode_unparse (e1), exprNode_unparse (e2)));
11203 transferChecks_initialization (e1, e2);
11207 transferChecks_assign (e1, e2);
11212 sRef fref = e2->sref;
11214 sRef_setDefState (e1->sref, sRef_getDefState (fref), e1->loc);
11215 sRef_setNullState (e1->sref, sRef_getNullState (fref), e1->loc);
11217 /* Need to typecheck the annotation on the parameters */
11219 if (ctype_isRealFunction (e1->typ)) {
11220 uentryList e1p = ctype_argsFunction (ctype_realType (e1->typ));
11221 uentryList e2p = ctype_argsFunction (ctype_realType (e2->typ));
11223 if (!uentryList_isMissingParams (e1p)
11224 && !uentryList_isMissingParams (e2p)
11225 && uentryList_size (e1p) > 0) {
11226 if (uentryList_size (e1p) == uentryList_size (e2p)) {
11229 uentryList_elements (e1p, el1) {
11232 el2 = uentryList_getN (e2p, n);
11234 uentry_checkMatchParam (el1, el2, n, e2);
11235 } end_uentryList_elements;
11241 if (exprNode_isStringLiteral (e2))
11243 exprNode_checkStringLiteralLength (exprNode_getType (e1), e2);
11246 if (isInit && sRef_isFileOrGlobalScope (e1->sref))
11252 DPRINTF (("Update aliases: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
11253 updateAliases (e1, e2);
11258 checkMacroParen (exprNode e)
11260 if (exprNode_isError (e) || e->kind == XPR_CAST)
11266 if (sRef_isUnsafe (e->sref) && !exprNode_isInParens (e))
11270 message ("Macro parameter used without parentheses: %s",
11271 exprNode_unparse (e)),
11278 reflectNullTest (/*@notnull@*/ exprNode e, bool isnull)
11282 e->guards = guardSet_addTrueGuard (e->guards, e->sref);
11286 e->guards = guardSet_addFalseGuard (e->guards, e->sref);
11293 ** if e2 is a parameter or global derived location which
11294 ** can be modified (that is, e2 is a mutable abstract type,
11295 ** or a derived pointer), then e1 can alias e2.
11297 ** e1 can alias everything which e2 can alias.
11299 ** Also, if e1 is guarded, remove from guard sets!
11302 static void updateAliases (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2)
11304 if (!context_inProtectVars ())
11307 ** depends on types of e1 and e2
11310 sRef s1 = e1->sref;
11311 sRef s2 = e2->sref;
11312 ctype t1 = exprNode_getType (e1);
11314 /* handle pointer sRefs, record fields, arrays, etc... */
11316 if (!ctype_isRealSU (t1))
11318 DPRINTF (("Copying real! %s", ctype_unparse (t1)));
11319 sRef_copyRealDerivedComplete (s1, s2);
11324 ** Fields should alias
11327 DPRINTF (("Not COPYING!: %s", ctype_unparse (t1)));
11330 if (ctype_isMutable (t1) && sRef_isKnown (s1))
11332 usymtab_clearAlias (s1);
11333 usymtab_addMustAlias (s1, s2);
11334 DPRINTF (("Add must alias: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
11338 DPRINTF (("Not mutable: %s", ctype_unparse (t1)));
11341 if (sRef_possiblyNull (s1) && usymtab_isGuarded (s1))
11343 usymtab_unguard (s1);
11348 exprNode exprNode_updateLocation (/*@returned@*/ exprNode e, /*@temp@*/ fileloc loc)
11350 if (exprNode_isDefined (e))
11352 e->loc = fileloc_update (e->loc, loc);
11356 e = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
11362 static void checkUniqueParams (exprNode fcn,
11363 /*@notnull@*/ exprNode current,
11365 int paramno, uentry ucurrent)
11368 sRef thisref = exprNode_getSref (current);
11371 ** Check if any argument could match this argument.
11374 exprNodeList_elements (args, icurrent)
11378 if (iparamno != paramno)
11380 sRef sr = exprNode_getSref (icurrent);
11382 if (sRef_similarRelaxed (thisref, sr))
11384 if (!sRef_isConst (thisref) && !sRef_isConst (sr))
11389 ("Parameter %d (%s) to function %s is declared %s but "
11390 "is aliased by parameter %d (%s)",
11392 exprNode_unparse (current),
11393 exprNode_unparse (fcn),
11394 alkind_unparse (uentry_getAliasKind (ucurrent)),
11395 iparamno, exprNode_unparse (icurrent)),
11401 sRefSet aliases = usymtab_canAlias (sr);
11403 sRefSet_allElements (aliases, asr)
11405 if (ctype_isUnknown (sRef_getType (thisref)))
11407 sRef_setType (thisref, uentry_getType (ucurrent));
11410 if (sRef_similarRelaxed (thisref, asr))
11412 if (sRef_isExternal (asr))
11414 if (sRef_isLocalState (thisref))
11420 sRef base = sRef_getRootBase (asr);
11422 if (!sRef_similar (sRef_getBase (asr), thisref))
11424 if (sRef_isUnique (base) || sRef_isOnly (base)
11425 || sRef_isKept (base)
11426 || (sRef_isAddress (asr) && sRef_isLocalVar (base))
11427 || (sRef_isAddress (thisref)
11428 && sRef_isLocalVar (sRef_getRootBase (thisref))))
11430 ; /* okay, no error */
11435 (FLG_MAYALIASUNIQUE,
11437 ("Parameter %d (%s) to function %s is declared %s but "
11438 "may be aliased externally by parameter %d (%s)",
11440 exprNode_unparse (current),
11441 exprNode_unparse (fcn),
11442 alkind_unparse (uentry_getAliasKind (ucurrent)),
11443 iparamno, exprNode_unparse (icurrent)),
11454 ("Parameter %d (%s) to function %s is declared %s but "
11455 "is aliased externally by parameter %d (%s) through "
11458 exprNode_unparse (current),
11459 exprNode_unparse (fcn),
11460 alkind_unparse (uentry_getAliasKind (ucurrent)),
11461 iparamno, exprNode_unparse (icurrent),
11462 sRef_unparse (asr)),
11466 } end_sRefSet_allElements;
11467 sRefSet_free (aliases);
11470 } end_exprNodeList_elements;
11473 long exprNode_getLongValue (exprNode e) {
11476 if (exprNode_hasValue (e)
11477 && multiVal_isInt (exprNode_getValue (e)))
11479 value = multiVal_forceInt (exprNode_getValue (e));
11490 /*@observer@*/ fileloc exprNode_getfileloc (exprNode p_e)
11492 if (exprNode_isDefined (p_e) )
11493 return ( p_e->loc );
11495 return fileloc_undefined;
11498 /*@only@*/ fileloc exprNode_getNextSequencePoint (exprNode e)
11501 ** Returns the location of the sequence point following e.
11503 ** Only works for statements (for now).
11506 if (exprNode_isDefined (e) && e->kind == XPR_STMT) {
11507 lltok t = exprData_getUopTok (e->edata);
11508 return fileloc_copy(lltok_getLoc (t));
11510 /* drl possible problem : warning fix
11511 llcontbug (message ("Cannot get next sequence point: %s", exprNode_unparse (e)));
11513 return fileloc_undefined;
11517 exprNode exprNode_createNew(ctype c)
11521 ret = exprNode_createPlain (c);
11526 bool exprNode_isInitBlock (exprNode e)
11528 return (exprNode_isDefined(e) && e->kind == XPR_INITBLOCK);