2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 # include <ctype.h> /* for isdigit */
29 # include "splintMacros.nf"
31 # include "cgrammar.h"
32 # include "cscanner.h"
33 # include "cgrammar_tokens.h"
35 # include "exprChecks.h"
36 # include "transferChecks.h"
37 # include "exprNodeSList.h"
39 static bool exprNode_sameStorage (exprNode p_e1, exprNode p_e2) /*@*/ ;
40 static bool exprNode_isEmptyStatement (exprNode p_e);
41 static /*@exposed@*/ exprNode exprNode_firstStatement (/*@returned@*/ exprNode p_e);
42 static bool exprNode_isFalseConstant (exprNode p_e) /*@*/ ;
43 static bool exprNode_isStatement (exprNode p_e);
44 static void checkGlobUse (uentry p_glob, bool p_isCall, /*@notnull@*/ exprNode p_e);
45 static void exprNode_addUse (exprNode p_e, /*@exposed@*/ sRef p_s);
46 static bool exprNode_matchArgType (ctype p_ct, exprNode p_e);
47 static exprNode exprNode_fakeCopy (exprNode p_e) /*@*/ ;
48 static exprNode exprNode_statementError (/*@only@*/ exprNode p_e, /*@only@*/ lltok p_t);
49 static bool exprNode_matchTypes (exprNode p_e1, exprNode p_e2);
50 static void checkUniqueParams (exprNode p_fcn,
51 /*@notnull@*/ exprNode p_current, exprNodeList p_args,
52 int p_paramno, uentry p_ucurrent);
53 static void updateAliases (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2);
54 static void abstractOpError (ctype p_tr1, ctype p_tr2, lltok p_op,
55 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2,
56 fileloc p_loc1, fileloc p_loc2);
57 static ctype checkNumerics (ctype p_tr1, ctype p_tr2, ctype p_te1, ctype p_te2,
58 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, lltok p_op);
59 static void doAssign (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, bool p_isInit);
60 static void checkSafeUse (exprNode p_e, /*@exposed@*/ sRef p_s);
61 static void reflectNullTest (/*@notnull@*/ exprNode p_e, bool p_isnull);
62 static void checkMacroParen (exprNode p_e);
63 static exprNodeSList exprNode_flatten (/*@dependent@*/ exprNode p_e);
64 static void exprNode_checkSetAny (exprNode p_e, /*@dependent@*/ cstring p_name);
65 static void exprNode_checkUse (exprNode p_e, /*@exposed@*/ sRef p_s, fileloc p_loc);
66 static void exprNode_mergeUSs (exprNode p_res, exprNode p_other);
67 static void exprNode_mergeCondUSs (exprNode p_res, exprNode p_other1, exprNode p_other2);
68 static /*@only@*/ /*@notnull@*/ exprNode exprNode_fromIdentifierAux (/*@observer@*/ uentry p_c);
69 static void checkAnyCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn,
70 /*@dependent@*/ cstring p_fname,
71 uentryList p_pn, exprNodeList p_args,
72 bool p_hasMods, sRefSet p_mods, bool p_isSpec,
74 static void checkOneArg (uentry p_ucurrent, /*@notnull@*/ exprNode p_current,
75 /*@dependent@*/ exprNode p_fcn, bool p_isSpec, int p_argno, int p_totargs);
77 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn, uentryList p_params, exprNodeList p_args);
79 static /*@only@*/ exprNode exprNode_effect (exprNode p_e)
80 /*@globals internalState@*/ ;
81 static /*@only@*/ cstring exprNode_doUnparse (exprNode p_e);
82 static /*@observer@*/ cstring exprNode_rootVarName (exprNode p_e);
83 static /*@exposed@*/ exprNode
84 exprNode_lastStatement (/*@returned@*/ exprNode p_e);
86 static /*@only@*/ exprNode s_mustExitNode = exprNode_undefined;
88 static int checkArgsReal (uentry p_fcn, /*@dependent@*/ exprNode p_f,
90 exprNodeList p_args, bool p_isIter, exprNode p_ret);
92 static bool inEffect = FALSE;
93 static int nowalloc = 0;
94 static int totalloc = 0;
95 static int maxalloc = 0;
97 static /*@only@*/ uentry regArg;
98 static /*@only@*/ uentry outArg;
99 static /*@only@*/ uentry outStringArg;
100 static /*@exposed@*/ sRef stdinRef;
101 static /*@exposed@*/ sRef stdoutRef;
102 static /*@only@*/ uentry csArg;
103 static /*@only@*/ uentry csOnlyArg;
104 static ctype cstringType;
105 static ctype ctypeType;
106 static ctype filelocType;
107 static bool initMod = FALSE;
109 /*@function void exprNode_swap (sef exprNode, sef exprNode)@*/
111 # define exprNode_swap(e1,e2) do { exprNode m_tmp = (e1); (e1) = (e2); (e2) = m_tmp; } while (FALSE)
114 static void exprNode_defineConstraints(/*@sef@*/ /*@special@*/ /*@notnull@*/ exprNode e)
115 /*@defines e->requiresConstraints, e->ensuresConstraints,
116 e->trueEnsuresConstraints, e->falseEnsuresConstraints @*/
118 e->requiresConstraints = constraintList_makeNew ();
119 e->ensuresConstraints = constraintList_makeNew ();
120 e->trueEnsuresConstraints = constraintList_makeNew ();
121 e->falseEnsuresConstraints = constraintList_makeNew ();
125 ** must occur after library has been read
128 void exprNode_initMod (void)
129 /*@globals undef regArg, undef outArg, undef outStringArg,
130 undef csOnlyArg, undef csArg;
137 cstringType = ctype_unknown;
138 ctypeType = ctype_unknown;
139 filelocType = ctype_unknown;
141 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
143 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
146 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
148 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
151 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
153 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
156 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdin")))
158 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdin"));
160 else /* define stdin */
162 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdin"),
164 fileloc_getBuiltin (),
166 uentry_setHasNameError (ue);
167 ue = usymtab_supGlobalEntryReturn (ue);
170 stdinRef = sRef_makePointer (uentry_getSref (ue));
172 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdout")))
174 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdout"));
178 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdout"),
180 fileloc_getBuiltin (),
182 uentry_setHasNameError (ue);
183 ue = usymtab_supGlobalEntryReturn (ue);
186 stdoutRef = sRef_makePointer (uentry_getSref (ue));
188 tmp = idDecl_create (cstring_undefined, qtype_create (ctype_unknown));
190 regArg = uentry_makeParam (tmp, PARAMUNKNOWN);
193 qtype_addQual (qtype_create (ctype_makePointer (ctype_unknown)),
196 outArg = uentry_makeParam (tmp, PARAMUNKNOWN);
198 idDecl_setTyp (tmp, qtype_addQual (qtype_create (ctype_string),
201 outStringArg = uentry_makeParam (tmp, PARAMUNKNOWN);
203 idDecl_setTyp (tmp, qtype_addQual (qtype_addQual (qtype_create (cstringType),
205 qual_createNull ()));
207 csOnlyArg = uentry_makeParam (tmp, PARAMUNKNOWN);
209 idDecl_setTyp (tmp, qtype_addQual (qtype_create (cstringType), qual_createNull ()));
210 csArg = uentry_makeParam (tmp, PARAMUNKNOWN);
216 exprNode_destroyMod (void)
217 /*@globals killed regArg, killed outArg, killed outStringArg,
218 killed s_mustExitNode, initMod @*/
222 /* evans 2002-07-12: changed uentry_free to uentry_freeComplete */
223 uentry_freeComplete (regArg);
224 uentry_freeComplete (outArg);
225 uentry_freeComplete (outStringArg);
227 exprNode_free (s_mustExitNode);
234 static void exprNode_resetSref (/*@notnull@*/ exprNode e)
236 e->sref = sRef_undefined;
239 exprNode exprNode_fakeCopy (exprNode e)
241 /*@-temptrans@*/ /*@-retalias@*/
243 /*@=temptrans@*/ /*@=retalias@*/
246 static bool isFlagKey (char key)
248 return (key == '-' || key == '+' || key == ' ' || key == '#');
251 static void exprNode_combineControl (/*@notnull@*/ exprNode ret,
252 /*@notnull@*/ exprNode ifclause,
253 /*@notnull@*/ exprNode elseclause)
255 ret->canBreak = ifclause->canBreak || elseclause->canBreak;
258 (ifclause->mustBreak || exprNode_mustEscape (ifclause))
259 && (elseclause->mustBreak || exprNode_mustEscape (elseclause));
261 ret->exitCode = exitkind_combine (ifclause->exitCode,
262 elseclause->exitCode);
267 ** For exprNode's returned by exprNode_effect.
270 static bool shallowKind (exprKind kind)
272 return (kind == XPR_STRINGLITERAL
273 || kind == XPR_NUMLIT
276 || kind == XPR_NODE);
280 exprNode_freeIniter (/*@only@*/ exprNode e)
282 if (!exprNode_isError (e))
288 ** Its a fake copy, don't free the field->rec and field->field
293 sfree (e->edata->field);
299 exprNode_free (e->edata->op->b);
300 /*@-compdestroy@*/ sfree (e->edata->op); /*@=compdestroy@*/
304 llbug (message ("other: %s", exprNode_unparse (e)));
307 multiVal_free (e->val);
308 cstring_free (e->etext);
309 fileloc_free (e->loc);
310 sRefSet_free (e->uses);
311 sRefSet_free (e->sets);
312 sRefSet_free (e->msets);
313 guardSet_free (e->guards);
315 constraintList_free(e->requiresConstraints);
316 constraintList_free(e->ensuresConstraints);
317 constraintList_free(e->trueEnsuresConstraints);
318 constraintList_free(e->falseEnsuresConstraints);
320 e->requiresConstraints = NULL;
321 e->ensuresConstraints = NULL;
322 e->trueEnsuresConstraints = NULL;
323 e->falseEnsuresConstraints = NULL;
330 exprNode_freeShallow (/*@only@*/ exprNode e)
332 if (!exprNode_isError (e))
334 if (shallowKind (e->kind))
341 if (e->kind == XPR_EMPTY
342 || e->kind == XPR_BODY
343 || e->kind == XPR_STRINGLITERAL
344 || e->kind == XPR_NUMLIT
345 || e->kind == XPR_NODE
346 || e->kind == XPR_OFFSETOF
347 || e->kind == XPR_ALIGNOFT
348 || e->kind == XPR_ALIGNOF
349 || e->kind == XPR_SIZEOFT
350 || e->kind == XPR_SIZEOF)
352 /* don't free anything */
356 /* multiVal_free (e->val); */
357 cstring_free (e->etext);
358 fileloc_free (e->loc);
359 sRefSet_free (e->uses);
360 sRefSet_free (e->sets);
361 sRefSet_free (e->msets);
362 guardSet_free (e->guards);
363 exprData_freeShallow (e->edata, e->kind);
365 /*@-compdestroy@*/ sfree (e); /*@=compdestroy@*/
374 exprNode_free (exprNode e)
376 if (!exprNode_isError (e))
380 multiVal_free (e->val);
381 cstring_free (e->etext);
382 fileloc_free (e->loc);
383 sRefSet_free (e->uses);
384 sRefSet_free (e->sets);
385 sRefSet_free (e->msets);
386 guardSet_free (e->guards);
387 exprData_free (e->edata, e->kind);
389 constraintList_free(e->requiresConstraints);
390 constraintList_free(e->ensuresConstraints);
391 constraintList_free(e->trueEnsuresConstraints);
392 constraintList_free(e->falseEnsuresConstraints);
394 e->requiresConstraints = NULL;
395 e->ensuresConstraints = NULL;
396 e->trueEnsuresConstraints = NULL;
397 e->falseEnsuresConstraints = NULL;
407 exprNode_makeError ()
409 return exprNode_undefined;
412 static /*@out@*/ /*@only@*/ /*@notnull@*/ exprNode
415 exprNode ret = (exprNode) dmalloc (sizeof (*ret));
416 /* static int lastexpnodes = 0; */
421 if (nowalloc > maxalloc)
429 static /*@notnull@*/ /*@special@*/ exprNode
430 exprNode_createPlain (ctype c)
432 /*@ensures isnull result->edata, result->loc, result->val, result->guards,
433 result->uses, result->sets, result->msets, result->etext @*/
436 exprNode e = exprNode_new ();
440 e->val = multiVal_undefined;
441 e->sref = sRef_undefined;
442 e->etext = cstring_undefined;
443 e->loc = fileloc_undefined;
444 e->guards = guardSet_undefined;
445 e->uses = sRefSet_undefined;
446 e->sets = sRefSet_undefined;
447 e->msets = sRefSet_undefined;
448 e->edata = exprData_undefined;
449 e->exitCode = XK_NEVERESCAPE;
451 e->mustBreak = FALSE;
452 e->isJumpPoint = FALSE;
454 exprNode_defineConstraints(e);
459 /*@observer@*/ exprNode exprNode_makeMustExit (void)
461 if (exprNode_isUndefined (s_mustExitNode))
463 s_mustExitNode = exprNode_createPlain (ctype_unknown);
464 s_mustExitNode->exitCode = XK_MUSTEXIT;
467 return s_mustExitNode;
471 static /*@notnull@*/ /*@special@*/ exprNode exprNode_create (ctype c)
473 /*@post:isnull result->edata, result->guards, result->val,
474 result->uses, result->sets, result->msets@*/
477 exprNode e = exprNode_createPlain (c);
478 e->loc = fileloc_copy (g_currentloc);
482 static /*@notnull@*/ /*@special@*/ exprNode exprNode_createUnknown (void)
484 /*@post:isnull result->edata, result->guards,
485 result->uses, result->sets, result->msets@*/
488 return (exprNode_create (ctype_unknown));
491 static /*@notnull@*/ /*@special@*/ exprNode
492 exprNode_createLoc (ctype c, /*@keep@*/ fileloc loc)
494 /*@post:isnull result->edata, result->guards, result->val,
495 result->uses, result->sets, result->msets@*/
498 exprNode e = exprNode_createPlain (c);
504 exprNode_copySets (/*@special@*/ /*@notnull@*/ exprNode ret, exprNode e)
505 /*@defines ret->guards, ret->uses, ret->sets, ret->msets@*/
507 if (exprNode_isDefined (e))
509 ret->guards = guardSet_copy (e->guards);
510 ret->uses = sRefSet_newCopy (e->uses);
511 ret->sets = sRefSet_newCopy (e->sets);
512 ret->msets = sRefSet_newCopy (e->msets);
516 ret->guards = guardSet_undefined;
517 ret->uses = sRefSet_undefined;
518 ret->sets = sRefSet_undefined;
519 ret->msets = sRefSet_undefined;
523 static /*@notnull@*/ /*@special@*/ exprNode
524 exprNode_createPartialLocCopy (exprNode e, /*@only@*/ fileloc loc)
526 /*@post:isnull result->edata, result->etext@*/
529 exprNode ret = exprNode_new ();
531 if (exprNode_isError (e))
533 ret->typ = ctype_unknown;
534 ret->val = multiVal_undefined;
536 ret->guards = guardSet_undefined;
537 ret->uses = sRefSet_undefined;
538 ret->sets = sRefSet_undefined;
539 ret->msets = sRefSet_undefined;
544 ret->val = multiVal_copy (e->val);
546 ret->guards = guardSet_copy (e->guards);
547 ret->uses = sRefSet_newCopy (e->uses);
548 ret->sets = sRefSet_newCopy (e->sets);
549 ret->msets = sRefSet_newCopy (e->msets);
552 ret->kind = XPR_EMPTY;
553 ret->sref = sRef_undefined;
554 ret->etext = cstring_undefined;
555 ret->exitCode = XK_NEVERESCAPE;
556 ret->canBreak = FALSE;
557 ret->mustBreak = FALSE;
558 ret->isJumpPoint = FALSE;
559 ret->edata = exprData_undefined;
561 exprNode_defineConstraints(ret);
567 static /*@notnull@*/ /*@special@*/ exprNode
568 exprNode_createPartialCopy (exprNode e)
570 /*@post:isnull result->edata, result->etext@*/
573 return (exprNode_createPartialLocCopy (e, fileloc_copy (exprNode_loc (e))));
576 static /*@notnull@*/ /*@special@*/ exprNode
577 exprNode_createPartialNVCopy (exprNode e)
579 /*@post:isnull result->edata, result->etext, result->val @*/
582 exprNode ret = exprNode_new ();
584 if (exprNode_isError (e))
586 ret->typ = ctype_unknown;
587 ret->loc = fileloc_undefined;
588 ret->guards = guardSet_undefined;
589 ret->uses = sRefSet_undefined;
590 ret->sets = sRefSet_undefined;
591 ret->msets = sRefSet_undefined;
596 ret->loc = fileloc_copy (e->loc);
597 ret->guards = guardSet_copy (e->guards);
598 ret->uses = sRefSet_newCopy (e->uses);
599 ret->sets = sRefSet_newCopy (e->sets);
600 ret->msets = sRefSet_newCopy (e->msets);
603 ret->val = multiVal_undefined;
604 ret->kind = XPR_EMPTY;
605 ret->sref = sRef_undefined;
606 ret->etext = cstring_undefined;
607 ret->exitCode = XK_NEVERESCAPE;
608 ret->canBreak = FALSE;
609 ret->mustBreak = FALSE;
610 ret->isJumpPoint = FALSE;
611 ret->edata = exprData_undefined;
613 exprNode_defineConstraints(ret);
618 static /*@notnull@*/ /*@special@*/ exprNode
619 exprNode_createSemiCopy (exprNode e)
621 /*@post:isnull result->edata, result->etext, result->sets,
622 result->msets, result->uses, result->guards@*/
625 if (exprNode_isError (e))
627 return exprNode_createPlain (ctype_unknown);
631 exprNode ret = exprNode_new ();
634 ret->val = multiVal_copy (e->val);
635 ret->loc = fileloc_copy (e->loc);
636 ret->guards = guardSet_undefined;
637 ret->uses = sRefSet_undefined;
638 ret->sets = sRefSet_undefined;
639 ret->msets = sRefSet_undefined;
641 ret->kind = XPR_EMPTY;
642 ret->sref = sRef_undefined;
643 ret->etext = cstring_undefined;
644 ret->exitCode = XK_NEVERESCAPE;
645 ret->canBreak = FALSE;
646 ret->mustBreak = FALSE;
647 ret->isJumpPoint = FALSE;
648 ret->edata = exprData_undefined;
650 exprNode_defineConstraints(ret);
657 exprNode_isNullValue (exprNode e)
659 if (exprNode_isDefined (e))
661 multiVal m = exprNode_getValue (e);
663 if (multiVal_isInt (m))
665 return (multiVal_forceInt (m) == 0);
673 exprNode_isUnknownConstant (/*@notnull@*/ exprNode e)
675 while (e->kind == XPR_PARENS)
677 e = exprData_getUopNode (e->edata);
679 if (!exprNode_isDefined (e))
684 /* evans 2002-02-05: was llassert (exprNode_isDefined (e)); but this can fail */
687 if (e->kind == XPR_CONST)
689 multiVal m = exprNode_getValue (e);
691 if (multiVal_isUnknown (m))
701 exprNode_numLiteral (ctype c, /*@temp@*/ cstring t,
702 /*@only@*/ fileloc loc, long val)
704 exprNode e = exprNode_createLoc (c, loc);
706 e->kind = XPR_NUMLIT;
708 llassert (multiVal_isUndefined (e->val));
709 e->val = multiVal_makeInt (val);
710 e->edata = exprData_makeLiteral (cstring_copy (t));
714 e->sref = sRef_makeUnknown ();
715 sRef_setDefNull (e->sref, e->loc);
718 DPRINTF (("Num lit: %s / %s", exprNode_unparse (e), ctype_unparse (exprNode_getType (e))));
723 exprNode_charLiteral (char c, cstring text, /*@only@*/ fileloc loc)
725 exprNode e = exprNode_createLoc (ctype_char, loc);
727 if (context_getFlag (FLG_CHARINTLITERAL))
729 e->typ = ctype_makeConj (ctype_char, ctype_int);
732 e->kind = XPR_NUMLIT;
733 e->val = multiVal_makeChar (c);
735 e->edata = exprData_makeLiteral (cstring_copy (text));
740 exprNode_floatLiteral (double d, ctype ct, cstring text, /*@only@*/ fileloc loc)
742 exprNode e = exprNode_createLoc (ct, loc);
744 e->kind = XPR_NUMLIT;
745 e->val = multiVal_makeDouble (d);
746 e->edata = exprData_makeLiteral (cstring_copy (text));
750 multiVal exprNode_getValue (exprNode e)
752 while (exprNode_isInParens (e)) {
753 if (e->edata != NULL) {
754 e = exprData_getUopNode (e->edata);
760 if (exprNode_isDefined (e)) {
763 return multiVal_undefined;
768 exprNode_combineLiterals (exprNode e, exprNode rest)
772 /* Both must be string literals. */
774 if (exprNode_isUndefined (rest) || exprNode_isUndefined (e))
776 exprNode_free (rest);
780 if (!exprNode_isStringLiteral (e))
784 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e),
785 exprNode_unparse (rest)),
787 exprNode_free (rest);
791 if (!exprNode_isStringLiteral (rest))
795 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e), exprNode_unparse (rest)),
798 exprNode_free (rest);
802 ns = cstring_concat (multiVal_forceString (exprNode_getValue (e)),
803 multiVal_forceString (exprNode_getValue (rest)));
805 multiVal_free (e->val);
806 exprData_free (e->edata, e->kind);
807 e->edata = exprData_makeLiteral (cstring_copy (ns));
808 e->val = multiVal_makeString (ns);
809 exprNode_free (rest);
814 exprNode_rawStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
816 exprNode e = exprNode_createLoc (ctype_string, loc);
817 size_t len = cstring_length (t);
819 if (context_getFlag (FLG_STRINGLITERALLEN))
821 if (len > size_fromInt (context_getValue (FLG_STRINGLITERALLEN)))
823 voptgenerror (FLG_STRINGLITERALLEN,
825 ("String literal length (%d) exceeds maximum "
826 "length (%d): \"%s\"",
828 context_getValue (FLG_STRINGLITERALLEN),
834 e->kind = XPR_STRINGLITERAL;
835 e->val = multiVal_makeString (cstring_copy (t));
836 e->edata = exprData_makeLiteral (t);
837 e->sref = sRef_makeConst (ctype_string);
839 if (context_getFlag (FLG_READONLYSTRINGS))
841 sRef_setAliasKind (e->sref, AK_STATIC, fileloc_undefined);
842 sRef_setExKind (e->sref, XO_OBSERVER, loc);
846 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
849 return (e); /* s released */
853 exprNode_wideStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
855 exprNode res = exprNode_stringLiteral (t, loc);
856 res->typ = ctype_makeWideString ();
861 exprNode_stringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
863 size_t len = size_fromInt (cstring_length (t) - 2);
864 char *ts = cstring_toCharsSafe (t);
865 char *s = cstring_toCharsSafe (cstring_create (len + 1));
867 llassert (*ts == '\"' && *(ts + len + 1) == '\"');
868 strncpy (s, ts+1, len);
871 return exprNode_rawStringLiteral (cstring_fromCharsO (s), loc);
874 exprNode exprNode_fromUIO (cstring c)
876 fileloc loc = context_getSaveLocation ();
877 exprNode e = exprNode_createPlain (ctype_unknown);
881 if (fileloc_isUndefined (loc))
883 loc = fileloc_copy (g_currentloc);
886 e->loc = loc; /* save loc was mangled */
887 e->sref = sRef_undefined;
889 if (usymtab_exists (c))
891 uentry ue = usymtab_lookupEither (c);
893 if (uentry_isDatatype (ue)
894 && uentry_isSpecified (ue))
897 (message ("%q: Specified datatype %s used in code, but not defined. "
898 "(Cannot continue reasonably from this error.)",
899 fileloc_unparse (e->loc), c));
907 llassertprint (!usymtab_exists (c), ("Entry exists: %s", c));
910 ** was supercedeGlobalEntry...is this better?
913 if (!context_inIterEnd ())
915 if (context_inMacro ())
917 if (context_getFlag (FLG_UNRECOG))
921 message ("Unrecognized identifier in macro definition: %s", c), e->loc);
925 flagcode_recordSuppressed (FLG_UNRECOG);
931 (FLG_UNRECOG, message ("Unrecognized identifier: %s", c), e->loc);
935 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
937 /* No alias errors for unrecognized identifiers */
938 sRef_setAliasKind (e->sref, AK_ERROR, loc);
943 exprNode exprNode_makeConstantString (cstring c, /*@only@*/ fileloc loc)
945 exprNode e = exprNode_createPlain (ctype_unknown);
948 e->sref = sRef_makeConst (ctype_string);
949 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
950 e->typ = ctype_string;
952 /* No alias errors for unrecognized identifiers */
953 sRef_setAliasKind (e->sref, AK_STATIC, loc);
954 sRef_setExKind (e->sref, XO_OBSERVER, loc);
959 exprNode exprNode_createId (/*@observer@*/ uentry c)
961 if (uentry_isValid (c))
963 exprNode e = exprNode_new ();
965 DPRINTF (("create id: %s", uentry_unparse (c)));
967 e->typ = uentry_getType (c);
969 if (uentry_isFunction (c)
970 && !sRef_isLocalVar (uentry_getSref (c)))
972 e->sref = sRef_undefined;
976 e->sref = uentry_getSref (c);
979 if (sRef_isStateUnknown (e->sref) && uentry_isNonLocal (c))
981 sRef_setDefined (e->sref, fileloc_undefined);
985 ** yoikes! leaving this out was a heinous bug...that would have been
986 ** caught if i had splint working first. gag!
989 e->etext = cstring_undefined;
991 if (uentry_isEitherConstant (c))
994 e->val = multiVal_copy (uentry_getConstantValue (c));
999 e->val = multiVal_unknown ();
1002 e->edata = exprData_makeId (c);
1003 e->loc = context_getSaveLocation ();
1005 if (fileloc_isUndefined (e->loc))
1007 fileloc_free (e->loc);
1008 e->loc = fileloc_copy (g_currentloc);
1011 e->guards = guardSet_new ();
1012 e->sets = sRefSet_new ();
1013 e->msets = sRefSet_new ();
1014 e->uses = sRefSet_new ();
1016 /*> missing fields, detected by splint <*/
1017 e->exitCode = XK_NEVERESCAPE;
1018 e->isJumpPoint = FALSE;
1019 e->canBreak = FALSE;
1020 e->mustBreak = FALSE;
1022 exprNode_defineConstraints (e);
1027 return exprNode_createUnknown ();
1031 /*@notnull@*/ exprNode
1032 exprNode_fromIdentifier (/*@observer@*/ uentry c)
1036 if (context_justPopped ()) /* watch out! c could be dead */
1038 uentry ce = usymtab_lookupSafe (cscanner_observeLastIdentifier ());
1040 if (uentry_isValid (ce))
1046 llbuglit ("Looks like Aunt Millie forgot to walk to dog again.");
1050 ret = exprNode_fromIdentifierAux (c);
1054 static void exprNode_checkStringLiteralLength (ctype t1, exprNode e2)
1056 multiVal mval = exprNode_getValue (e2);
1060 if (ctype_isFixedArray (t1))
1062 size_t nelements = ctype_getArraySize (t1);
1064 llassert (multiVal_isString (mval));
1065 slit = multiVal_forceString (mval);
1067 len = cstring_lengthExpandEscapes (slit);
1069 llassert (exprNode_isDefined (e2));
1071 if (len == nelements)
1075 temp = cstring_expandEscapes (slit);
1077 if (temp[len-1] == '\0')
1080 (FLG_STRINGLITNOROOMFINALNULL,
1081 message ("String literal with %d character%& "
1082 "is assigned to %s (no room for final null terminator): %s",
1085 exprNode_unparse (e2)),
1091 (FLG_STRINGLITNOROOM,
1092 message ("String literal with %d character%& "
1093 "is assigned to %s (no room for null terminator): %s",
1096 exprNode_unparse (e2)),
1100 else if (len > nelements)
1103 (FLG_STRINGLITTOOLONG,
1104 message ("String literal with %d character%& (counting null terminator) "
1105 "is assigned to %s (insufficient storage available): %s",
1108 exprNode_unparse (e2)),
1111 else if (len < nelements - 1)
1114 (FLG_STRINGLITSMALLER,
1115 message ("String literal with %d character%& is assigned to %s (possible waste of storage): %s",
1118 exprNode_unparse (e2)),
1128 static /*@only@*/ /*@notnull@*/ exprNode
1129 exprNode_fromIdentifierAux (/*@observer@*/ uentry c)
1131 exprNode e = exprNode_createId (c);
1134 uentry_setUsed (c, e->loc);
1136 if (uentry_isVar (c) && sRef_isFileOrGlobalScope (sr))
1138 checkGlobUse (c, FALSE, e);
1145 exprNode_isZero (exprNode e)
1147 if (exprNode_isDefined (e))
1149 multiVal m = exprNode_getValue (e);
1151 if (multiVal_isInt (m))
1153 return (multiVal_forceInt (m) == 0);
1161 exprNode_isNonNegative (exprNode e)
1163 if (exprNode_isDefined (e))
1165 multiVal m = exprNode_getValue (e);
1167 if (multiVal_isInt (m))
1169 return (multiVal_forceInt (m) >= 0);
1173 ** This is not always true if programmer defines enum
1174 ** values, but then the constant should be known.
1177 if (ctype_isEnum (ctype_realType (e->typ)))
1187 ** a[x] - uses a but NOT a[]
1188 ** result sref = a[] (set/use in assignment)
1190 ** The syntax x[a] is also legal in C, and has the same
1191 ** semantics. If ind is an array, and arr is an int, flip
1196 exprNode_arrayFetch (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
1199 ** error in arr, error propagates (no new messages)
1200 ** error in ind, assume valid and continue
1203 DPRINTF (("Array fetch: %s / %s",
1204 exprNode_unparse (e1), exprNode_unparse (e2)));
1206 if (exprNode_isError (e1))
1209 return (exprNode_makeError ());
1215 ctype carr = exprNode_getType (e1);
1216 ctype crarr = ctype_realType (carr);
1219 ** this sets up funny aliasing, that leads to spurious
1220 ** splint errors. Hence, the i2 comments.
1223 /* evans 2001-09-09 added ctype_isKnown so there is no swap when e1 type is unknown */
1224 if (ctype_isKnown (crarr)
1225 && !ctype_isRealArray (crarr)
1226 && ctype_isRealNumeric (crarr)
1227 && !exprNode_isError (e2)
1228 && ctype_isRealAP (exprNode_getType (e2))) /* fetch like 3[a] */
1233 carr = exprNode_getType (arr);
1234 crarr = ctype_realType (carr);
1242 DPRINTF (("arr: %s", exprNode_unparse (arr)));
1244 if (sRef_possiblyNull (arr->sref))
1246 if (!usymtab_isGuarded (arr->sref))
1248 if (optgenerror (FLG_NULLDEREF,
1249 message ("Index of %s pointer %q: %s",
1250 sRef_nullMessage (arr->sref),
1251 sRef_unparse (arr->sref),
1252 exprNode_unparse (arr)),
1255 DPRINTF (("ref: %s", sRef_unparseFull (arr->sref)));
1256 sRef_showNullInfo (arr->sref);
1258 /* suppress future messages */
1259 sRef_setNullError (arr->sref);
1264 if (exprNode_isError (ind))
1266 if ((ctype_isArrayPtr (crarr)
1267 && !ctype_isFunction (crarr))
1268 || ctype_isUnknown (carr))
1270 exprNode ret = exprNode_createPartialCopy (arr);
1272 if (ctype_isKnown (carr))
1274 ret->typ = ctype_baseArrayPtr (crarr);
1278 ret->typ = ctype_unknown;
1281 ret->sref = sRef_makeArrayFetch (arr->sref);
1283 ret->kind = XPR_FETCH;
1286 ** Because of funny aliasing (when arr and ind are
1287 ** flipped) spurious errors would be reported here.
1290 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1291 checkSafeUse (ret, arr->sref);
1296 voptgenerror (FLG_TYPE,
1297 message ("Array fetch from non-array (%t): %s[%s]", carr,
1298 exprNode_unparse (e1), exprNode_unparse (e2)),
1300 exprNode_free (arr);
1301 return (exprNode_makeError ());
1306 if (!ctype_isForceRealInt (&(ind->typ)))
1308 ctype rt = ctype_realType (ind->typ);
1310 if (ctype_isChar (rt))
1314 message ("Array fetch using non-integer, %t: %s[%s]",
1316 exprNode_unparse (e1), exprNode_unparse (e2)),
1319 else if (ctype_isEnum (rt))
1323 message ("Array fetch using non-integer, %t: %s[%s]",
1325 exprNode_unparse (e1), exprNode_unparse (e2)),
1332 message ("Array fetch using non-integer, %t: %s[%s]",
1334 exprNode_unparse (e1), exprNode_unparse (e2)),
1338 multiVal_free (ind->val);
1339 ind->val = multiVal_unknown ();
1342 if (ctype_isArrayPtr (crarr) && !ctype_isFunction (crarr))
1344 exprNode ret = exprNode_createSemiCopy (arr);
1345 multiVal m = exprNode_getValue (ind);
1347 ret->typ = ctype_baseArrayPtr (crarr);
1348 ret->kind = XPR_FETCH;
1350 if (multiVal_isInt (m))
1352 int i = (int) multiVal_forceInt (m);
1354 if (sRef_isValid (arr->sref)) {
1355 ret->sref = sRef_makeArrayFetchKnown (arr->sref, i);
1357 ret->sref = sRef_undefined;
1362 ret->sref = sRef_makeArrayFetch (arr->sref);
1365 ret->sets = sRefSet_realNewUnion (arr->sets, ind->sets);
1366 ret->msets = sRefSet_realNewUnion (arr->msets, ind->msets);
1367 ret->uses = sRefSet_realNewUnion (arr->uses, ind->uses);
1369 /* (see comment on spurious errors above) */
1370 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1372 exprNode_checkUse (ret, ind->sref, ind->loc);
1373 exprNode_checkUse (ret, arr->sref, arr->loc);
1379 if (ctype_isUnknown (carr))
1381 exprNode ret = exprNode_createPartialCopy (arr);
1383 ret->kind = XPR_FETCH;
1384 ret->typ = ctype_unknown;
1385 ret->sets = sRefSet_union (ret->sets, ind->sets);
1386 ret->msets = sRefSet_union (ret->msets, ind->msets);
1387 ret->uses = sRefSet_union (ret->uses, ind->uses);
1389 /* (see comment on spurious errors above) */
1390 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1392 exprNode_checkUse (ret, ind->sref, ind->loc);
1393 exprNode_checkUse (ret, arr->sref, arr->loc);
1400 message ("Array fetch from non-array (%t): %s[%s]", carr,
1401 exprNode_unparse (e1), exprNode_unparse (e2)),
1404 exprNode_free (arr);
1405 exprNode_free (ind);
1407 return (exprNode_makeError ());
1417 checkArgs (uentry fcn, /*@dependent@*/ exprNode f, ctype t,
1418 exprNodeList args, exprNode ret)
1420 return (checkArgsReal (fcn, f, ctype_argsFunction (t), args, FALSE, ret));
1424 ** checkPrintfArgs --- checks arguments for printf-like functions
1425 ** Arguments before ... have already been checked.
1426 ** The argument before the ... is a char *.
1427 ** argno is the format string argument.
1431 checkPrintfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1432 exprNodeList args, exprNode ret, int argno)
1435 ** the last argument before the elips is the format string
1440 int nargs = exprNodeList_size (args);
1441 uentryList params = uentry_getParams (fcn);
1445 ** These should be ensured by checkSpecialFunction
1448 llassert (uentryList_size (params) == argno + 1);
1449 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1451 a = exprNodeList_getN (args, argno - 1);
1452 formatloc = fileloc_copy (exprNode_loc (a));
1454 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1455 && exprNode_knownStringValue (a))
1457 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1458 char *code = format;
1461 nargs = exprNodeList_size (args);
1463 while ((code = strchr (code, '%')) != NULL)
1465 char *origcode = code;
1466 cstring codetext = cstring_newEmpty ();
1467 char key = *(++code);
1468 ctype modtype = ctype_int;
1469 bool modified = FALSE;
1471 fileloc_addColumn (formatloc, code - ocode);
1473 codetext = cstring_appendChar (codetext, key);
1476 while (isFlagKey (key))
1479 codetext = cstring_appendChar (codetext, key);
1480 fileloc_incColumn (formatloc);
1483 if (key == 'm') /* skipped in syslog */
1488 /* ignore field width */
1489 while (isdigit ((int) key) != 0)
1492 codetext = cstring_appendChar (codetext, key);
1493 fileloc_incColumn (formatloc);
1496 /* ignore precision */
1500 codetext = cstring_appendChar (codetext, key);
1501 fileloc_incColumn (formatloc);
1504 ** In printf, '*' means: read the next arg as an int for the
1505 ** field width. This seems to be missing from my copy of the
1506 ** standard x3.159-1989. Setion 4.9.6.1 refers to * (described
1507 ** later) but never does.
1512 ; /* don't do anything --- handle later */
1516 while (isdigit ((int) key) != 0)
1519 codetext = cstring_appendChar (codetext, key);
1520 fileloc_incColumn (formatloc);
1527 modtype = ctype_sint; /* short */
1529 codetext = cstring_appendChar (codetext, key);
1530 fileloc_incColumn (formatloc);
1532 else if (key == 'l' || key == 'L')
1534 modtype = ctype_lint; /* long */
1536 codetext = cstring_appendChar (codetext, key);
1537 fileloc_incColumn (formatloc);
1539 if (key == 'l' || key == 'L') {
1540 modtype = ctype_llint; /* long long */
1542 codetext = cstring_appendChar (codetext, key);
1543 fileloc_incColumn (formatloc);
1551 /* now, key = type of conversion to apply */
1553 fileloc_incColumn (formatloc);
1561 message ("No argument corresponding to %q format "
1562 "code %d (%%%s): \"%s\"",
1563 uentry_getName (fcn),
1565 cstring_fromChars (format)),
1568 if (fileloc_isDefined (formatloc)
1569 && context_getFlag (FLG_SHOWCOL))
1571 llgenindentmsg (cstring_makeLiteral ("Corresponding format code"),
1579 a = exprNodeList_getN (args, i);
1582 if (!exprNode_isError (a))
1588 case '*': /* int argument for fieldwidth */
1589 expecttype = ctype_int;
1590 *(--code) = '%'; /* convert it for next code */
1591 fileloc_subColumn (formatloc, 1);
1592 /*@switchbreak@*/ break;
1595 expecttype = ctype_combine (ctype_uint, modtype);
1596 /*@switchbreak@*/ break;
1598 case 'i': /* int argument */
1600 expecttype = ctype_combine (ctype_int, modtype);
1601 /*@switchbreak@*/ break;
1603 case 'x': /* unsigned int */
1605 expecttype = ctype_combine (ctype_uint, modtype);
1607 /*@switchbreak@*/ break;
1613 case 'f': /* double */
1614 expecttype = ctype_combine (ctype_double, modtype);
1615 /*@switchbreak@*/ break;
1617 case 'c': /* int converted to char (check its a char?) */
1618 expecttype = ctype_makeConj (ctype_int,
1619 ctype_makeConj (ctype_char,
1622 /* evans 2001-10-05 - changed to reflect correct ISO spec:
1623 int converted to char */
1625 /* expecttype = ctype_makeConj (ctype_char, ctype_uchar); */
1626 /*@switchbreak@*/ break;
1628 case 's': /* string */
1629 expecttype = ctype_string;
1630 /*@switchbreak@*/ break;
1633 while (((key = *(++code)) != ']')
1636 codetext = cstring_appendChar (codetext, key);
1637 fileloc_incColumn (formatloc);
1643 (message ("Bad character set format: %s",
1644 cstring_fromChars (origcode)));
1647 expecttype = ctype_string;
1648 /*@switchbreak@*/ break;
1650 case 'p': /* pointer */
1651 expecttype = ctype_makePointer (ctype_void);
1652 /* need not be defined */
1653 uentry_setDefState (regArg, SS_RELDEF);
1654 sRef_setPosNull (uentry_getSref (regArg),
1657 /*@switchbreak@*/ break;
1659 case 'n': /* pointer to int, modified by call! */
1660 expecttype = ctype_combine (ctype_makePointer (ctype_int), modtype);
1662 uentry_setDefState (regArg, SS_ALLOCATED); /* corresponds to out */
1663 /*@switchbreak@*/ break;
1665 case 'm': /* in a syslog, it doesn't consume an argument */
1666 /* should check we're really doing syslog */
1668 /*@switchbreak@*/ break;
1672 expecttype = ctype_unknown;
1676 message ("Unrecognized format code: %s",
1677 cstring_fromChars (origcode)),
1678 fileloc_isDefined (formatloc)
1679 ? formatloc : g_currentloc);
1681 /*@switchbreak@*/ break;
1684 if (!(exprNode_matchArgType (expecttype, a)))
1686 if (ctype_isVoidPointer (expecttype)
1687 && ctype_isRealAbstract (a->typ)
1688 && (context_getFlag (FLG_ABSTVOIDP)))
1694 if (llgenformattypeerror
1695 (expecttype, exprNode_undefined,
1697 message ("Format argument %d to %q (%%%s) expects "
1700 uentry_getName (fcn),
1703 a->typ, exprNode_unparse (a)),
1706 if (fileloc_isDefined (formatloc)
1707 && context_getFlag (FLG_SHOWCOL))
1710 (cstring_makeLiteral
1711 ("Corresponding format code"),
1718 uentry_setType (regArg, expecttype);
1719 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
1721 if (ctype_equal (expecttype, ctype_string))
1723 exprNode_checkUse (a, sRef_makePointer (a->sref), a->loc);
1726 uentry_setType (regArg, ctype_unknown);
1727 uentry_fixupSref (regArg);
1731 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1742 cstring_free (codetext);
1747 voptgenerror (FLG_TYPE,
1748 message ("Format string for %q has %d arg%&, given %d",
1749 uentry_getName (fcn), i - argno, nargs - argno),
1755 /* no checking possible for compile-time unknown format strings */
1756 if (exprNode_isDefined (a))
1760 message ("Format string parameter to %s is not a compile-time constant: %s",
1761 exprNode_unparse (f),
1762 exprNode_unparse (a)),
1767 fileloc_free (formatloc);
1771 checkScanfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1772 exprNodeList args, exprNode ret, int argno)
1776 int nargs = exprNodeList_size (args);
1777 uentryList params = uentry_getParams (fcn);
1781 ** These should be ensured by checkSpecialFunction
1784 llassert (uentryList_size (params) == argno + 1);
1785 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1787 a = exprNodeList_getN (args, argno - 1);
1788 formatloc = fileloc_copy (exprNode_loc (a));
1790 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1791 && exprNode_knownStringValue (a))
1793 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1794 char *code = format;
1797 nargs = exprNodeList_size (args);
1799 while ((code = strchr (code, '%')) != NULL)
1801 char *origcode = code;
1802 char key = *(++code);
1803 cstring codetext = cstring_newEmpty ();
1804 ctype modtype = ctype_int;
1805 char modifier = '\0';
1806 bool modified = TRUE;
1807 bool ignore = FALSE;
1809 codetext = cstring_appendChar (codetext, key);
1810 fileloc_addColumn (formatloc, code - ocode);
1813 ** this is based on ANSI standard library description of fscanf
1814 ** (from ANSI standard X3.159-1989, 4.9.6.1)
1817 /* '*' suppresses assignment (does not need match argument) */
1822 codetext = cstring_appendChar (codetext, key);
1825 fileloc_incColumn (formatloc);
1828 /* ignore field width */
1829 while (isdigit ((int) key) != 0)
1832 codetext = cstring_appendChar (codetext, key);
1833 fileloc_incColumn (formatloc);
1838 modtype = ctype_sint; /* short */
1840 codetext = cstring_appendChar (codetext, key);
1841 fileloc_incColumn (formatloc);
1843 else if (key == 'l' || key == 'L')
1845 modtype = ctype_lint; /* long */
1849 codetext = cstring_appendChar (codetext, key);
1851 fileloc_incColumn (formatloc);
1853 if (key == 'l' || key == 'L') {
1854 modtype = ctype_llint; /* long long */
1856 codetext = cstring_appendChar (codetext, key);
1857 fileloc_incColumn (formatloc);
1865 /* now, key = type of conversion to apply */
1867 fileloc_incColumn (formatloc);
1881 message ("No argument corresponding to %q format "
1882 "code %d (%%%s): \"%s\"",
1883 uentry_getName (fcn),
1885 cstring_fromChars (format)),
1888 if (fileloc_isDefined (formatloc)
1889 && context_getFlag (FLG_SHOWCOL))
1892 (cstring_makeLiteral ("Corresponding format code"),
1900 a = exprNodeList_getN (args, i);
1903 if (!exprNode_isError (a))
1909 case '*': /* int argument for fieldwidth */
1910 expecttype = ctype_makePointer (ctype_int);
1911 *(--code) = '%'; /* convert it for next code */
1912 fileloc_subColumn (formatloc, 1);
1913 /*@switchbreak@*/ break;
1916 expecttype = ctype_makePointer (ctype_combine (ctype_uint, modtype));
1917 /*@switchbreak@*/ break;
1922 case 'X': /* unsigned int */
1923 expecttype = ctype_makePointer (ctype_combine (ctype_int, modtype));
1924 /*@switchbreak@*/ break;
1931 /* printf is double, scanf is float! */
1933 if (modifier == 'l')
1935 expecttype = ctype_makePointer (ctype_double);
1937 else if (modifier == 'L')
1939 expecttype = ctype_makePointer (ctype_ldouble);
1943 llassert (modifier == '\0');
1944 expecttype = ctype_makePointer (ctype_float);
1946 /*@switchbreak@*/ break;
1948 case 'c': /* int converted to char (check its a char?) */
1949 expecttype = ctype_makePointer (ctype_makeConj (ctype_char, ctype_uchar));
1950 /*@switchbreak@*/ break;
1952 case 's': /* string */
1953 expecttype = ctype_string;
1954 /*@switchbreak@*/ break;
1958 while (((key = *(++code)) != ']')
1961 codetext = cstring_appendChar (codetext, key);
1962 fileloc_incColumn (formatloc);
1968 (message ("Bad character set format: %s",
1969 cstring_fromChars (origcode)));
1972 expecttype = ctype_string;
1973 /*@switchbreak@*/ break;
1976 case 'p': /* pointer */
1979 message ("Format code should not be used in scanf: %s",
1980 cstring_fromChars (origcode)),
1981 fileloc_isDefined (formatloc)
1982 ? formatloc : g_currentloc);
1984 expecttype = ctype_unknown;
1985 /*@switchbreak@*/ break;
1987 case 'n': /* pointer to int, modified by call! */
1988 expecttype = ctype_makePointer (ctype_int);
1989 /*@switchbreak@*/ break;
1992 expecttype = ctype_unknown;
1996 message ("Unrecognized format code: %s",
1997 cstring_fromChars (origcode)),
1998 fileloc_isDefined (formatloc)
1999 ? formatloc : g_currentloc);
2001 /*@switchbreak@*/ break;
2004 if (!(exprNode_matchArgType (expecttype, a)))
2006 if (ctype_isVoidPointer (expecttype)
2007 && ctype_isRealAbstract (a->typ)
2008 && (context_getFlag (FLG_ABSTVOIDP)))
2014 if (llgenformattypeerror
2015 (expecttype, exprNode_undefined,
2017 message ("Format argument %d to %q (%%%s) expects "
2020 uentry_getName (fcn),
2021 codetext, expecttype,
2022 a->typ, exprNode_unparse (a)),
2025 if (fileloc_isDefined (formatloc)
2026 && context_getFlag (FLG_SHOWCOL))
2029 (cstring_makeLiteral
2030 ("Corresponding format code"),
2037 uentry_setType (outArg, expecttype);
2038 checkOneArg (outArg, a, f, FALSE, i+1, nargs);
2039 uentry_setType (outArg, ctype_unknown);
2040 uentry_fixupSref (outArg);
2044 exprNode_checkCallModifyVal (a->sref, args, f, ret);
2049 /* a->sref = sRef_undefined; */
2056 cstring_free (codetext);
2061 voptgenerror (FLG_TYPE,
2062 message ("Format string for %q has %d arg%&, given %d",
2063 uentry_getName (fcn), i - argno, nargs - argno),
2069 /* no checking possible for compile-time unknown format strings */
2072 fileloc_free (formatloc);
2076 checkMessageArgs (/*@notnull@*/ /*@dependent@*/ exprNode f,
2079 /*@unused@*/ int argno)
2082 ** the last argument before the elips is the format string
2085 int nargs = exprNodeList_size (args);
2090 a = exprNodeList_getN (args, argno - 1);
2091 formatloc = fileloc_copy (exprNode_loc (a));
2093 if (ctype_isUnknown (cstringType)) {
2094 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
2096 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
2100 if (ctype_isUnknown (ctypeType)) {
2101 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
2103 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
2107 if (ctype_isUnknown (filelocType)) {
2108 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
2110 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
2114 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
2115 && exprNode_knownStringValue (a))
2117 cstring format = multiVal_forceString (exprNode_getValue (a));
2118 char *code = cstring_toCharsSafe (format);
2121 nargs = exprNodeList_size (args);
2123 while ((code = strchr (code, '%')) != NULL)
2125 char *origcode = code;
2126 char key = *(++code);
2127 cstring codetext = cstring_newEmpty ();
2128 bool isOnly = FALSE;
2130 codetext = cstring_appendChar (codetext, key);
2132 fileloc_addColumn (formatloc, code - ocode);
2134 while (key >= '0' && key <= '9')
2137 codetext = cstring_appendChar (codetext, key);
2138 fileloc_incColumn (formatloc);
2142 fileloc_incColumn (formatloc);
2146 if (key == '&') /* plural marker */
2155 message ("Message missing format arg %d (%%%s): \"%s\"",
2156 i + 1, codetext, format),
2162 a = exprNodeList_getN (args, i);
2166 if (!exprNode_isError (a))
2170 /*@-loopswitchbreak@*/
2176 expecttype = ctype_char; break;
2178 expecttype = cstringType; break;
2180 expecttype = cstringType; isOnly = TRUE; break;
2182 expecttype = cstringType; isOnly = TRUE; break;
2183 case 'd': expecttype = ctype_int; break;
2184 case 'u': expecttype = ctype_uint; break;
2185 case 'w': expecttype = ctype_ulint; break;
2186 case 'f': expecttype = ctype_float; break;
2187 case 'b': expecttype = ctype_bool; break;
2188 case 't': expecttype = ctypeType; break;
2190 expecttype = ctype_makePointer (ctype_void);
2191 /* need not be defined */
2192 uentry_setDefState (regArg, SS_RELDEF);
2193 sRef_setPosNull (uentry_getSref (regArg),
2196 /*@switchbreak@*/ break;
2197 case 'l': expecttype = filelocType; break;
2198 case '&': /* a wee bit of a hack methinks */
2199 expecttype = ctype_int;
2201 case 'r': expecttype = ctype_bool; break;
2203 expecttype = ctype_unknown;
2206 message ("Unrecognized format code: %s",
2207 cstring_fromChars (origcode)),
2208 fileloc_isDefined (formatloc)
2209 ? formatloc : g_currentloc);
2212 /*@=loopswitchbreak@*/
2214 if (!(exprNode_matchArgType (expecttype, a)))
2216 if (ctype_isVoidPointer (expecttype)
2217 && ctype_isRealAbstract (a->typ)
2218 && (context_getFlag (FLG_ABSTVOIDP)))
2224 if (llgenformattypeerror
2225 (expecttype, exprNode_undefined,
2227 message ("Format argument %d to %q (%%%s) expects "
2230 uentry_getName (fcn),
2231 codetext, expecttype,
2232 a->typ, exprNode_unparse (a)),
2235 if (fileloc_isDefined (formatloc)
2236 && context_getFlag (FLG_SHOWCOL))
2239 (cstring_makeLiteral
2240 ("Corresponding format code"),
2247 if (ctype_equal (expecttype, cstringType))
2251 checkOneArg (csOnlyArg, a, f, FALSE, i+1, nargs);
2252 uentry_fixupSref (csOnlyArg);
2256 checkOneArg (csArg, a, f, FALSE, i+1, nargs);
2257 uentry_fixupSref (csArg);
2262 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
2263 uentry_fixupSref (regArg);
2269 cstring_free (codetext);
2274 voptgenerror (FLG_TYPE,
2275 message ("Format string for %q has %d arg%&, given %d",
2276 uentry_getName (fcn), i - argno, nargs -argno),
2282 /* no checking possible for compile-time unknown format strings */
2285 fileloc_free (formatloc);
2289 checkExpressionDefinedAux (/*@notnull@*/ exprNode e1,
2290 /*@notnull@*/ exprNode e2,
2296 bool hadUncon = FALSE;
2298 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e1->sref)) &&
2299 sRefSet_hasUnconstrained (sets2))
2302 (FLG_EVALORDERUNCON,
2304 ("Expression may have undefined behavior (%q used in right operand "
2305 "may set global variable %q used in left operand): %s %s %s",
2306 sRefSet_unparseUnconstrained (sets2),
2307 sRef_unparse (sRef_getRootBase (e1->sref)),
2308 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2312 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e2->sref)) &&
2313 sRefSet_hasUnconstrained (sets1))
2316 (FLG_EVALORDERUNCON,
2318 ("Expression has undefined behavior (%q used in left operand "
2319 "may set global variable %q used in right operand): %s %s %s",
2320 sRefSet_unparseUnconstrained (sets1),
2321 sRef_unparse (e2->sref),
2322 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2326 sRefSet_realElements (e1->uses, sr)
2328 if (sRef_isMeaningful (sr) && sRefSet_member (sets2, sr))
2333 ("Expression has undefined behavior (left operand uses %q, "
2334 "modified by right operand): %s %s %s",
2336 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2339 } end_sRefSet_realElements;
2341 sRefSet_realElements (sets1, sr)
2343 if (sRef_isMeaningful (sr))
2345 if (sRef_same (sr, e2->sref))
2350 ("Expression has undefined behavior (value of right operand "
2351 "modified by left operand): %s %s %s",
2352 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2355 else if (sRefSet_member (e2->uses, sr))
2360 ("Expression has undefined behavior (left operand modifies %q, "
2361 "used by right operand): %s %s %s",
2363 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2368 if (sRefSet_member (sets2, sr))
2370 if (sRef_isUnconstrained (sr))
2378 hadUncon = optgenerror
2379 (FLG_EVALORDERUNCON,
2381 ("Expression may have undefined behavior. Left operand "
2382 "calls %q; right operand calls %q. The unconstrained "
2383 "functions may modify global state used by "
2384 "the other operand): %s %s %s",
2385 sRefSet_unparseUnconstrained (sets1),
2386 sRefSet_unparseUnconstrained (sets2),
2387 exprNode_unparse (e1), lltok_unparse (op),
2388 exprNode_unparse (e2)),
2397 ("Expression has undefined behavior (both "
2398 "operands modify %q): %s %s %s",
2400 exprNode_unparse (e1),
2401 lltok_unparse (op), exprNode_unparse (e2)),
2407 } end_sRefSet_realElements;
2410 static void checkExpressionDefined (exprNode e1, exprNode e2, lltok op)
2412 bool hasError = FALSE;
2414 if (exprNode_isError (e1) || exprNode_isError (e2))
2419 if (sRefSet_member (e2->sets, e1->sref))
2421 if (e2->kind == XPR_CALL)
2427 hasError = optgenerror
2429 message ("Expression has undefined behavior "
2430 "(value of left operand %s is modified "
2431 "by right operand %s): %s %s %s",
2432 exprNode_unparse (e1),
2433 exprNode_unparse (e2),
2434 exprNode_unparse (e1), lltok_unparse (op),
2435 exprNode_unparse (e2)),
2440 if (context_getFlag (FLG_EVALORDERUNCON))
2442 if (sRefSet_member (e2->msets, e1->sref))
2444 if (e2->kind == XPR_CALL)
2450 hasError = optgenerror
2453 ("Expression has undefined behavior (value of left "
2454 "operand may be modified by right operand): %s %s %s",
2455 exprNode_unparse (e1), lltok_unparse (op),
2456 exprNode_unparse (e2)),
2464 checkExpressionDefinedAux (e1, e2, e1->sets, e2->sets, op, FLG_EVALORDER);
2466 if (context_maybeSet (FLG_EVALORDERUNCON))
2468 checkExpressionDefinedAux (e1, e2, e1->msets,
2469 e2->msets, op, FLG_EVALORDERUNCON);
2474 static void checkSequencing (exprNode p_f, exprNodeList p_args);
2477 checkArgsReal (uentry fcn, /*@dependent@*/ exprNode f, uentryList cl,
2478 exprNodeList args, bool isIter, exprNode ret)
2482 if (!exprNode_isError (f))
2484 if (!uentryList_isMissingParams (cl))
2486 int nargs = exprNodeList_size (args);
2487 int expectargs = uentryList_size (cl);
2491 if (expectargs == 0)
2499 message ("Iter %q invoked with %d args, "
2501 uentry_getName (fcn),
2509 message ("Function %s called with %d args, "
2511 exprNode_unparse (f), nargs),
2518 last = uentry_getType (uentryList_getN (cl, expectargs - 1));
2520 exprNodeList_reset (args);
2522 uentryList_elements (cl, current)
2524 ctype ct = uentry_getType (current);
2527 if (ctype_isElips (ct))
2530 ** do special checking for printf/scanf library functions
2532 ** this is kludgey code, just for handling the special case
2536 if (uentry_isPrintfLike (fcn))
2538 checkPrintfArgs (f, fcn, args, ret, i);
2541 else if (uentry_isScanfLike (fcn))
2543 checkScanfArgs (f, fcn, args, ret, i);
2546 else if (uentry_isMessageLike (fcn))
2548 checkMessageArgs (f, fcn, args, i);
2553 llassert (!uentry_isSpecialFunction (fcn));
2556 nargs = expectargs; /* avoid errors */
2561 if (i >= nargs) break;
2563 a = exprNodeList_current (args);
2564 exprNodeList_advance (args);
2568 if (exprNode_isError (a))
2575 probably necessary? I'm not sure about this one
2576 checkMacroParen (a);
2579 f->guards = guardSet_union (f->guards, a->guards);
2581 DPRINTF (("match arg: %s / %s", ctype_unparse (ct), ctype_unparse (a->typ)));
2583 if (!(exprNode_matchArgType (ct, a)))
2585 DPRINTF (("Args mismatch!"));
2587 if (ctype_isVoidPointer (ct)
2588 && (ctype_isPointer (a->typ)
2589 && (ctype_isRealAbstract (ctype_baseArrayPtr (a->typ)))))
2594 ("Pointer to abstract type (%t) used "
2596 "(arg %d to %q): %s",
2598 uentry_getName (fcn),
2599 exprNode_unparse (a)),
2607 (ct, exprNode_undefined,
2610 ("Iter %q expects arg %d to "
2611 "be %t gets %t: %s",
2612 uentry_getName (fcn),
2613 i, ct, a->typ, exprNode_unparse (a)),
2624 ("Function %q expects arg %d to be %t gets %t: %s",
2625 uentry_getName (fcn),
2626 i, ct, a->typ, exprNode_unparse (a)),
2629 DPRINTF (("Types: %s / %s",
2631 ctype_unparse (a->typ)));
2635 ** Clear null marker for abstract types.
2636 ** (It is not revealed, so suppress future messages.)
2639 if (ctype_isAbstract (a->typ))
2641 sRef_setNullUnknown (exprNode_getSref (a), a->loc);
2648 } end_uentryList_elements ;
2651 if (expectargs != nargs) /* note: not != since we may have ... */
2653 if (ctype_isElips (last))
2657 message ("Function %s called with %d args, expects at least %d",
2658 exprNode_unparse (f),
2659 nargs, expectargs - 1),
2668 message ("Iter %q invoked with %d args, expects %d",
2669 uentry_getName (fcn), nargs, expectargs),
2676 message ("Function %s called with %d args, expects %d",
2677 exprNode_unparse (f),
2690 ** Check for undefined code sequences in function arguments:
2692 ** one parameter sets something used by another parameter
2693 ** one parameter sets something set by another parameter
2697 checkSequencingOne (exprNode f, exprNodeList args,
2698 /*@notnull@*/ exprNode el, int argno)
2701 ** Do second loop, iff +undefunspec
2705 int numloops = context_maybeSet (FLG_EVALORDERUNCON) ? 2 : 1;
2707 for (checkloop = 0; checkloop < numloops; checkloop++)
2713 thissets = el->sets;
2717 llassert (checkloop == 1);
2718 thissets = el->msets;
2721 sRefSet_realElements (thissets, thisset)
2725 /*@access exprNodeList@*/
2726 for (j = 0; j < args->nelements; j++)
2728 exprNode jl = args->elements[j];
2729 int thisargno = j + 1;
2731 if (thisargno != argno && exprNode_isDefined (jl))
2733 sRefSet otheruses = jl->uses;
2735 if (sRef_isFileOrGlobalScope (sRef_getRootBase (jl->sref)) &&
2736 sRefSet_hasUnconstrained (thissets))
2739 (FLG_EVALORDERUNCON,
2742 ("%q used in argument %d may set "
2743 "global variable %q used by argument %d: %s(%q)",
2744 cstring_capitalizeFree (sRefSet_unparseUnconstrained (thissets)),
2747 sRef_unparse (sRef_getRootBase (jl->sref)),
2749 exprNode_unparse (f), exprNodeList_unparse (args)),
2753 if (sRefSet_member (otheruses, thisset))
2755 if (sRef_isUnconstrained (thisset))
2758 (FLG_EVALORDERUNCON,
2760 ("Unconstrained functions used in arguments %d (%q) "
2761 "and %d (%s) may modify "
2762 "or use global state in undefined way: %s(%q)",
2764 sRefSet_unparseUnconstrainedPlain (otheruses),
2766 sRef_unconstrainedName (thisset),
2767 exprNode_unparse (f),
2768 exprNodeList_unparse (args)),
2776 ("Argument %d modifies %q, used by argument %d "
2777 "(order of evaluation of actual parameters is "
2778 "undefined): %s(%q)",
2779 argno, sRef_unparse (thisset), thisargno,
2780 exprNode_unparse (f), exprNodeList_unparse (args)),
2786 sRefSet othersets = jl->sets;
2788 if (sRefSet_member (othersets, thisset))
2790 if (sRef_isUnconstrained (thisset))
2793 (FLG_EVALORDERUNCON,
2795 ("Unconstrained functions used in "
2796 "arguments %d (%q) and %d (%s) may modify "
2797 "or use global state in undefined way: %s(%q)",
2799 sRefSet_unparseUnconstrainedPlain (othersets),
2801 sRef_unconstrainedName (thisset),
2802 exprNode_unparse (f), exprNodeList_unparse (args)),
2810 ("Argument %d modifies %q, set by argument %d (order of"
2811 " evaluation of actual parameters is undefined): %s(%q)",
2812 argno, sRef_unparse (thisset), thisargno,
2813 exprNode_unparse (f), exprNodeList_unparse (args)),
2820 /*@noaccess exprNodeList@*/
2821 } end_sRefSet_realElements;
2826 checkSequencing (exprNode f, exprNodeList args)
2828 if (exprNodeList_size (args) > 1)
2833 /*@access exprNodeList*/
2835 for (i = 0; i < args->nelements; i++)
2837 el = args->elements[i];
2839 if (!exprNode_isError (el))
2841 checkSequencingOne (f, args, el, i + 1);
2844 /*@noaccess exprNodeList*/
2849 ** requires le = exprNode_getUentry (f)
2853 checkGlobMods (/*@notnull@*/ /*@dependent@*/ exprNode f,
2854 uentry le, exprNodeList args,
2855 /*@notnull@*/ exprNode ret, int specialArgs)
2857 bool isSpec = FALSE;
2858 bool hasMods = FALSE;
2860 globSet usesGlobs = globSet_undefined;
2861 sRefSet mods = sRefSet_undefined;
2862 bool freshMods = FALSE;
2863 uentryList params = uentryList_undefined;
2865 DPRINTF (("Check glob mods: %s", exprNode_unparse (ret)));
2868 ** check globals and modifies
2873 if (!uentry_isValid (le))
2875 ctype fr = ctype_realType (f->typ);
2877 if (ctype_isFunction (fr))
2879 params = ctype_argsFunction (fr);
2883 params = uentryList_missingParams;
2886 if (!context_getFlag (FLG_MODNOMODS)
2887 && !context_getFlag (FLG_GLOBUNSPEC))
2889 checkUnspecCall (f, params, args);
2895 fname = uentry_rawName (le);
2899 if (uentry_isFunction (le))
2901 params = uentry_getParams (le);
2902 mods = uentry_getMods (le);
2903 hasMods = uentry_hasMods (le);
2904 usesGlobs = uentry_getGlobs (le);
2905 isSpec = uentry_isSpecified (le);
2907 else /* not a function */
2909 ctype ct = ctype_realType (uentry_getType (le));
2911 llassertprint (uentry_isVar (le) && ctype_isFunction (ct),
2912 ("checkModGlobs: uentry not a function: %s",
2913 uentry_unparse (le)));
2915 params = ctype_argsFunction (ct);
2916 return; /*@32 ! remove this? */
2925 globSet_allElements (usesGlobs, el)
2927 if (sRef_isValid (el))
2929 if (sRef_isInternalState (el) || sRef_isSystemState (el))
2931 context_usedGlobal (el);
2932 exprNode_checkUse (f, el, f->loc);
2934 if (context_checkInternalUse ())
2936 if (!context_globAccess (el))
2938 if (sRef_isSystemState (el)
2939 && !context_getFlag (FLG_MODFILESYSTEM))
2948 ("Called procedure %s may access %q, but "
2949 "globals list does not include globals %s",
2950 exprNode_unparse (f),
2952 cstring_makeLiteralTemp (sRef_isInternalState (el)
2960 else if (sRef_isNothing (el) || sRef_isSpecState (el))
2966 uentry gle = sRef_getUentry (el);
2967 sRef sr = sRef_updateSref (el);
2969 if (sRef_isUndefGlob (el))
2971 sRef_setDefined (sr, f->loc);
2972 exprNode_checkSet (f, sr);
2980 if (sRef_isAllocated (el))
2982 exprNode_checkSet (f, sr);
2986 if (sRef_isStateUndefined (sr))
2991 ("%s %q used by function undefined before call: %s",
2992 sRef_getScopeName (sr),
2994 exprNode_unparse (f)),
2996 sRef_setDefined (sr, f->loc);
2998 exprNode_checkUse (f, sr, f->loc);
3001 checkGlobUse (gle, TRUE, f);
3004 if (sRef_isKilledGlob (el))
3006 sRef_kill (sr, f->loc);
3007 context_usedGlobal (sr);
3011 } end_globSet_allElements;
3017 if (context_hasMods () || context_getFlag (FLG_MODNOMODS))
3019 sRefSet smods = sRefSet_undefined;
3022 ** NEED to check for modifies anything
3026 ** check each sRef that called function modifies (ml), is
3032 sRefSet_allElements (mods, s) /* s is something which may be modified */
3034 DPRINTF (("Check modify: %s", sRef_unparse (s)));
3036 if (sRef_isKindSpecial (s))
3038 if (sRef_isSpecInternalState (s))
3040 if (context_getFlag (FLG_MODINTERNALSTRICT))
3042 exprNode_checkCallModifyVal (s, args, f, ret);
3046 sRefSet mmods = context_modList ();
3048 sRefSet_allElements (mmods, el)
3050 if (sRef_isInternalState (el))
3052 sRef_setModified (el);
3054 } end_sRefSet_allElements ;
3059 exprNode_checkCallModifyVal (s, args, f, ret);
3064 sRef rb = sRef_getRootBase (s);
3066 if (sRef_isFileOrGlobalScope (rb))
3068 context_usedGlobal (rb);
3071 if (sRef_isFileStatic (s)
3072 && !fileId_equal (fileloc_fileId (f->loc),
3073 fileloc_fileId (uentry_whereDefined (le))))
3075 smods = sRefSet_insert (smods, s);
3079 exprNode_checkCallModifyVal (s, args, f, ret);
3082 } end_sRefSet_allElements;
3087 ** Static elements in modifies set can have nasty consequences.
3088 ** (I think...have not been able to reproduce a possible bug.)
3091 if (!sRefSet_isDefined (smods))
3093 mods = sRefSet_newCopy (mods);
3096 sRefSet_allElements (smods, el)
3098 bool res = sRefSet_delete (mods, el);
3101 } end_sRefSet_allElements;
3103 sRefSet_free (smods);
3108 else if (sRefSet_isDefined (mods))
3109 { /* just check observers */
3112 sRefSet_allElements (mods, s) /* s is something which may be modified */
3114 sRef rb = sRef_getRootBase (s);
3118 if (sRef_isParam (rb))
3120 sRef b = sRef_fixBaseParam (s, args);
3122 if (sRef_isObserver (b))
3124 exprNode e = exprNodeList_nth (args, sRef_getParam (rb));
3128 message ("Function call may modify observer%q: %s",
3129 sRef_unparsePreOpt (b), exprNode_unparse (e)),
3132 sRef_showExpInfo (b);
3136 } end_sRefSet_allElements;
3140 if (!hasMods) /* no specified modifications */
3142 if (context_getFlag (FLG_MODOBSERVERUNCON))
3144 exprNodeList_elements (args, e)
3146 if (exprNode_isDefined (e))
3148 sRef s = exprNode_getSref (e);
3150 if (sRef_isObserver (s)
3151 && ctype_isMutable (sRef_getType (s)))
3154 (FLG_MODOBSERVERUNCON,
3156 ("Call to unconstrained function %s may modify observer%q: %s",
3157 exprNode_unparse (f),
3158 sRef_unparsePreOpt (s), exprNode_unparse (e)),
3161 sRef_showExpInfo (s);
3165 } end_exprNodeList_elements;
3170 checkAnyCall (f, fname, params, args, hasMods, mods, isSpec, specialArgs);
3172 ret->uses = sRefSet_union (ret->uses, f->uses);
3173 ret->sets = sRefSet_union (ret->sets, f->sets);
3174 ret->msets = sRefSet_union (ret->msets, f->msets);
3179 ** Spurious errors reported, because splint can't tell
3180 ** mods must be fresh if freshMods is true.
3183 /*@i@*/ sRefSet_free (mods);
3189 void checkGlobUse (uentry glob, bool isCall, /*@notnull@*/ exprNode e)
3191 if (uentry_isVar (glob))
3193 if (context_inFunctionLike ())
3195 sRef sr = uentry_getSref (glob);
3197 context_usedGlobal (sr);
3199 if (context_checkGlobUse (glob))
3201 if (!context_globAccess (sr))
3207 message ("Called procedure %s may access %s %q",
3208 exprNode_unparse (e),
3209 sRef_unparseScope (sr),
3210 uentry_getName (glob)),
3217 message ("Undocumented use of %s %s",
3218 sRef_unparseScope (sr),
3219 exprNode_unparse (e)),
3228 llbug (message ("Global not variable: %q", uentry_unparse (glob)));
3233 reflectEnsuresClause (exprNode ret, uentry le, exprNode f, exprNodeList args)
3235 DPRINTF (("Reflect ensures clause: %s(%s) / %s / %s",
3236 exprNode_unparse (f), exprNodeList_unparse (args),
3237 uentry_unparseFull (le),
3238 stateClauseList_unparse (uentry_getStateClauseList (le))));
3240 if (uentry_isValid (le) && uentry_isFunction (le))
3242 stateClauseList sclauses = uentry_getStateClauseList (le);
3244 if (stateClauseList_isDefined (sclauses))
3246 DPRINTF (("Reflect ensures: %s / %s / %s",
3247 uentry_unparse (le),
3248 exprNode_unparse (f), exprNodeList_unparse (args)));
3250 stateClauseList_elements (sclauses, cl)
3252 if (stateClause_hasEnsures (cl))
3254 /* Same in usymtab.c:1904 */
3255 if (stateClause_setsMetaState (cl))
3257 qual q = stateClause_getMetaQual (cl);
3258 annotationInfo ainfo = qual_getAnnotationInfo (q);
3259 metaStateInfo minfo = annotationInfo_getState (ainfo);
3260 cstring key = metaStateInfo_getName (minfo);
3261 int mvalue = annotationInfo_getValue (ainfo);
3263 sRefSet osrs = sRefSet_undefined;
3266 if (stateClause_isGlobal (cl))
3268 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3273 srs = stateClause_getRefs (cl);
3276 DPRINTF (("Reflect ensures clause: %s", stateClause_unparse (cl)));
3279 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
3281 sRefSet_elements (srs, sel)
3285 if (sRef_isResult (sRef_getRootBase (sel)))
3287 s = exprNode_getSref (ret);
3291 s = sRef_fixBaseParam (sel, args);
3294 DPRINTF (("Reflecting state clause on: %s / %s",
3295 sRef_unparse (sel), sRef_unparse (s)));
3297 sRef_setMetaStateValueComplete (s, key, mvalue, exprNode_loc (f));
3298 } end_sRefSet_elements;
3300 sRefSet_free (osrs);
3304 sRefSet srs = stateClause_getRefs (cl);
3305 sRefModVal modf = stateClause_getEnsuresFunction (cl);
3306 int eparam = stateClause_getStateParameter (cl);
3308 llassert (modf != NULL);
3310 DPRINTF (("Reflect after clause: %s / %s",
3311 stateClause_unparse (cl),
3312 sRefSet_unparse (srs)));
3314 sRefSet_elements (srs, sel)
3318 DPRINTF (("elements: %s", sRef_unparse (sel)));
3319 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3321 if (sRef_isResult (sRef_getRootBase (sel)))
3323 DPRINTF (("Fix base: %s / %s",
3324 sRef_unparse (sel), sRef_unparse (exprNode_getSref (ret))));
3325 s = sRef_fixBase (sel, exprNode_getSref (ret));
3326 DPRINTF (("==> %s", sRef_unparseFull (s)));
3330 s = sRef_fixBaseParam (sel, args);
3333 DPRINTF (("elements: %s", sRef_unparse (s)));
3334 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3336 DPRINTF (("Reflecting state clause on: %s / %s",
3337 sRef_unparseFull (sel), sRef_unparseFull (s)));
3339 /* evans 2001-08-24 - added aliasSetCompleteParam */
3340 sRef_aliasSetCompleteParam (modf, s, eparam, exprNode_loc (f));
3342 DPRINTF (("After reflecting state clause on: %s / %s",
3343 sRef_unparseFull (sel), sRef_unparseFull (s)));
3344 } end_sRefSet_elements;
3347 } end_stateClauseList_elements ;
3350 DPRINTF (("Here: %s / %s",
3351 uentry_unparseFull (le),
3352 bool_unparse (uentry_hasMetaStateEnsures (le))));
3354 if (uentry_hasMetaStateEnsures (le))
3356 fileloc loc = exprNode_loc (f);
3358 metaStateConstraintList mscl = uentry_getMetaStateEnsures (le);
3360 metaStateConstraintList_elements (mscl, msc)
3362 metaStateSpecifier msspec = metaStateConstraint_getSpecifier (msc);
3363 metaStateInfo msinfo = metaStateSpecifier_getMetaStateInfo (msspec);
3364 metaStateExpression msexpr = metaStateConstraint_getExpression (msc);
3365 cstring key = metaStateInfo_getName (msinfo);
3366 sRef mlsr = metaStateSpecifier_getSref (msspec);
3368 sRef lastref = sRef_undefined;
3369 stateValue sval = stateValue_undefined;
3371 DPRINTF (("Meta state constraint for %s: %s", uentry_unparse (le),
3372 metaStateConstraint_unparse (msc)));
3373 DPRINTF (("Matches left: %s", sRef_unparseDebug (mlsr)));
3375 if (sRef_isResult (sRef_getRootBase (mlsr)))
3377 s = exprNode_getSref (ret);
3381 s = sRef_fixBaseParam (mlsr, args);
3384 DPRINTF (("Setting state: %s", sRef_unparseFull (s)));
3386 while (metaStateExpression_isDefined (msexpr))
3388 metaStateSpecifier ms = metaStateExpression_getSpecifier (msexpr);
3389 metaStateInfo msi = metaStateSpecifier_getMetaStateInfo (ms);
3392 DPRINTF (("Check expression: %s", metaStateExpression_unparse (msexpr)));
3394 if (metaStateExpression_isMerge (msexpr))
3396 msexpr = metaStateExpression_getRest (msexpr);
3400 msexpr = metaStateExpression_undefined;
3403 if (metaStateInfo_isDefined (msi))
3405 /* Must match lhs state */
3406 llassert (metaStateInfo_equal (msinfo, msi));
3409 if (metaStateSpecifier_isElipsis (ms))
3412 ** For elipsis, we need to merge all the relevant elipsis parameters
3416 uentryList params = uentry_getParams (le);
3417 int paramno = uentryList_size (params) - 1;
3419 if (!uentry_isElipsisMarker (uentryList_getN (params, paramno)))
3423 message ("Ensures clauses uses ... for function without ... in parameter list: %q",
3424 uentry_getName (le)),
3425 uentry_whereLast (le));
3426 /*@innerbreak@*/ break;
3429 while (paramno < exprNodeList_size (args))
3431 exprNode arg = exprNodeList_getN (args, paramno);
3432 fs = exprNode_getSref (arg);
3433 DPRINTF (("Merge arg: %s", exprNode_unparse (arg)));
3435 /* cut and pasted... gack*/
3436 if (stateValue_isDefined (sval))
3438 /* Use combination table to merge old state value with new one: */
3439 stateValue tval = sRef_getMetaStateValue (fs, key);
3441 if (stateValue_isDefined (tval))
3443 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3444 cstring msg = cstring_undefined;
3445 int nval = stateCombinationTable_lookup (sctable,
3446 stateValue_getValue (sval),
3447 stateValue_getValue (tval),
3449 DPRINTF (("Combining: %s + %s -> %d",
3450 stateValue_unparseValue (sval, msinfo),
3451 stateValue_unparseValue (tval, msinfo),
3454 if (nval == stateValue_error)
3459 ("Attributes merged in ensures clause in states that "
3460 "cannot be combined (%q is %q, %q is %q)%q",
3461 sRef_unparse (lastref),
3462 stateValue_unparseValue (sval, msinfo),
3464 stateValue_unparseValue (tval, msinfo),
3465 cstring_isDefined (msg) ?
3466 message (": %s", msg) : cstring_undefined),
3469 sRef_showMetaStateInfo (fs, key);
3473 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3474 loc = exprNode_loc (arg);
3478 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3483 sval = sRef_getMetaStateValue (fs, key);
3488 if (stateValue_isError (sval))
3490 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3499 msr = metaStateSpecifier_getSref (ms);
3502 llassert (sRef_isParam (sRef_getRootBase (msr)));
3503 fs = sRef_fixBaseParam (msr, args);
3505 if (stateValue_isDefined (sval))
3507 /* Use combination table to merge old state value with new one: */
3508 stateValue tval = sRef_getMetaStateValue (fs, key);
3510 if (stateValue_isDefined (tval))
3512 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3513 cstring msg = cstring_undefined;
3514 int nval = stateCombinationTable_lookup (sctable,
3515 stateValue_getValue (sval),
3516 stateValue_getValue (tval),
3518 DPRINTF (("Combining: %s + %s -> %d",
3519 stateValue_unparseValue (sval, msinfo),
3520 stateValue_unparseValue (tval, msinfo),
3523 if (nval == stateValue_error)
3528 ("Attributes merged in ensures clause in states that "
3529 "cannot be combined (%q is %q, %q is %q)%q",
3530 sRef_unparse (lastref),
3531 stateValue_unparseValue (sval, msinfo),
3533 stateValue_unparseValue (tval, msinfo),
3534 cstring_isDefined (msg)
3535 ? message (": %s", msg) : cstring_undefined),
3538 sRef_showMetaStateInfo (fs, key);
3542 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3546 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3551 sval = sRef_getMetaStateValue (fs, key);
3556 if (stateValue_isError (sval))
3558 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3563 DPRINTF (("Setting: %s:%s <- %s", sRef_unparse (s), key, stateValue_unparse (sval)));
3565 if (stateValue_isDefined (sval))
3567 sRef_setMetaStateValueComplete (s, key, stateValue_getValue (sval), loc);
3571 DPRINTF (("Undefined state: %s", cstring_toCharsSafe (sRef_unparse (s))));
3573 } end_metaStateConstraintList_elements ;
3575 metaStateConstraintList_free (mscl);
3581 checkRequiresClause (uentry le, exprNode f, exprNodeList args)
3583 DPRINTF (("Check requires clause: %s(%s) / %s / %s",
3584 exprNode_unparse (f), exprNodeList_unparse (args),
3585 uentry_unparseFull (le),
3586 stateClauseList_unparse (uentry_getStateClauseList (le))));
3588 if (uentry_isValid (le) && uentry_isFunction (le))
3590 stateClauseList sclauses = uentry_getStateClauseList (le);
3592 if (stateClauseList_isDefined (sclauses))
3594 DPRINTF (("Check requires: %s / %s / %s",
3595 uentry_unparse (le),
3596 exprNode_unparse (f), exprNodeList_unparse (args)));
3598 stateClauseList_elements (sclauses, cl)
3600 DPRINTF (("Check clause: %s / %s",
3601 stateClause_unparse (cl),
3602 bool_unparse (stateClause_hasRequires (cl))));
3604 if (stateClause_hasRequires (cl))
3606 sRefSet osrs = sRefSet_undefined;
3609 if (stateClause_isGlobal (cl))
3611 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3616 srs = stateClause_getRefs (cl);
3619 DPRINTF (("Refs: %s", sRefSet_unparse (srs)));
3621 if (stateClause_setsMetaState (cl))
3623 qual q = stateClause_getMetaQual (cl);
3624 annotationInfo ainfo = qual_getAnnotationInfo (q);
3625 metaStateInfo minfo = annotationInfo_getState (ainfo);
3626 cstring key = metaStateInfo_getName (minfo);
3627 int mvalue = annotationInfo_getValue (ainfo);
3629 DPRINTF (("Requires meta state! %s = %d", key, mvalue));
3631 sRefSet_elements (srs, sel)
3633 sRef s = sRef_fixBaseParam (sel, args);
3635 if (sRef_isResult (sRef_getRootBase (sel)))
3641 DPRINTF (("Checking state clause on: %s / %s / %s = %d",
3642 sRef_unparseFull (sel), sRef_unparseFull (s),
3645 if (!sRef_checkMetaStateValue (s, key, mvalue))
3647 DPRINTF (("HERE: %s", sRef_unparse (s)));
3651 ("Requires clause of called function %q not satisfied%q (state is %q): %q",
3652 uentry_getName (le),
3653 sRef_isGlobalMarker (s)
3655 : message (" by %q", sRef_unparse (s)),
3656 stateValue_unparseValue (sRef_getMetaStateValue (s, key),
3658 stateClause_unparse (cl)),
3661 sRef_showAliasInfo (s);
3665 DPRINTF (("Error supressed!"));
3666 DPRINTF (("Loc: %s", fileloc_unparse (exprNode_loc (f))));
3667 DPRINTF (("Context supress: %s",
3668 bool_unparse (context_suppressFlagMsg (FLG_STATETRANSFER, exprNode_loc (f)))));
3672 } end_sRefSet_elements;
3676 sRefModVal modf = stateClause_getRequiresBodyFunction (cl);
3677 int eparam = stateClause_getStateParameter (cl);
3679 DPRINTF (("Reflect after clause: %s / %s",
3680 stateClause_unparse (cl),
3681 sRefSet_unparse (srs)));
3683 llassert (modf != NULL);
3685 sRefSet_elements (srs, sel)
3689 DPRINTF (("elements: %s", sRef_unparse (sel)));
3690 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3692 s = sRef_fixBaseParam (sel, args);
3694 DPRINTF (("elements: %s", sRef_unparse (s)));
3695 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3697 if (sRef_isResult (sRef_getRootBase (sel)))
3699 ; /*@i423 what do we do about results */
3703 DPRINTF (("Reflecting state clause on: %s / %s",
3704 sRef_unparse (sel), sRef_unparse (s)));
3706 modf (s, eparam, exprNode_loc (f));
3708 } end_sRefSet_elements;
3711 sRefSet_free (osrs);
3713 } end_stateClauseList_elements ;
3718 static /*@only@*/ exprNode
3719 functionCallSafe (/*@only@*/ /*@notnull@*/ exprNode f,
3720 ctype t, /*@keep@*/ exprNodeList args)
3722 /* requires f is a non-error exprNode, with type function */
3723 cstring fname = exprNode_unparse (f);
3724 uentry le = exprNode_getUentry (f);
3725 exprNode ret = exprNode_createPartialCopy (f);
3730 DPRINTF (("Call: %s %s",exprNode_unparse (f), exprNodeList_unparse (args)));
3732 ret->typ = ctype_getReturnType (t);
3733 ret->kind = XPR_CALL;
3735 ret->edata = exprData_makeCall (f, args);
3738 ** Order of these steps is very important!
3740 ** Must check for argument dependencies before messing up uses and sets.
3743 if (context_getFlag (FLG_EVALORDER))
3745 exprNodeList_elements (args, current)
3747 if (exprNode_isDefined (current))
3749 exprNode_addUse (current, current->sref);
3751 } end_exprNodeList_elements;
3753 if (context_maybeSet (FLG_EVALORDER) || context_maybeSet (FLG_EVALORDERUNCON))
3755 checkSequencing (f, args);
3758 exprNodeList_elements (args, current)
3760 if (exprNode_isDefined (current) && sRef_isMeaningful (current->sref))
3762 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3764 } end_exprNodeList_elements ;
3767 special = checkArgs (le, f, t, args, ret);
3768 checkGlobMods (f, le, args, ret, special);
3769 checkRequiresClause (le, f, args);
3772 if (uentry_isValid (le)
3773 && (uentry_isFunction (le)
3774 || (uentry_isVariable (le)
3775 && ctype_isFunction (uentry_getType (le)))))
3777 exitkind exk = uentry_getExitCode (le);
3779 /* f->typ is already set to the return type */
3781 DPRINTF (("Function: %s", uentry_unparseFull (le)));
3782 ret->sref = uentry_returnedRef (le, args, exprNode_loc (f));
3783 DPRINTF (("Returned: %s / %s",
3784 uentry_unparseFull (le),
3785 sRef_unparseFull (ret->sref)));
3787 if (uentry_isFunction (le) && exprNodeList_size (args) >= 1)
3789 qual nullPred = uentry_nullPred (le);
3791 if (qual_isTrueNull (nullPred))
3793 exprNode arg = exprNodeList_head (args);
3795 if (exprNode_isDefined (arg))
3797 ret->guards = guardSet_addFalseGuard (ret->guards, arg->sref);
3800 else if (qual_isFalseNull (nullPred))
3802 exprNode arg = exprNodeList_head (args);
3804 if (exprNode_isDefined (arg))
3806 ret->guards = guardSet_addTrueGuard (ret->guards, arg->sref);
3811 llassert (qual_isUnknown (nullPred));
3815 if (exitkind_isConditionalExit (exk))
3819 ** if (arg0) then { exit! } else { ; }
3821 ** if (arg0) then { ; } else { exit! }
3826 llassert (!exprNodeList_isEmpty (args));
3827 firstArg = exprNodeList_head (args);
3829 if (exprNode_isDefined (firstArg)
3830 && !guardSet_isEmpty (firstArg->guards))
3832 usymtab_trueBranch (guardSet_undefined);
3833 usymtab_altBranch (guardSet_undefined);
3835 if (exitkind_isTrueExit (exk))
3837 usymtab_popBranches (firstArg,
3838 exprNode_makeMustExit (),
3840 TRUE, TRUEEXITCLAUSE);
3844 usymtab_popBranches (firstArg,
3846 exprNode_makeMustExit (),
3847 TRUE, FALSEEXITCLAUSE);
3851 ret->exitCode = XK_MAYEXIT;
3853 else if (exitkind_mustExit (exk))
3855 ret->exitCode = XK_MUSTEXIT;
3857 else if (exitkind_couldExit (exk))
3859 ret->exitCode = XK_MAYEXIT;
3866 if (cstring_equalLit (fname, "exit"))
3868 if (exprNodeList_size (args) == 1)
3870 exprNode arg = exprNodeList_head (args);
3872 if (exprNode_isDefined (arg) && exprNode_knownIntValue (arg))
3874 long int val = multiVal_forceInt (exprNode_getValue (arg));
3881 ("Argument to exit has implementation defined behavior: %s",
3882 exprNode_unparse (arg)),
3883 exprNode_loc (arg));
3891 ret->sref = sRef_undefined;
3892 exprNode_checkSetAny (ret, uentry_rawName (le));
3895 DPRINTF (("Before reflect: %s", sRef_unparseFull (ret->sref)));
3896 DPRINTF (("Reflect: %s", uentry_unparseFull (le)));
3897 reflectEnsuresClause (ret, le, f, args);
3900 DPRINTF (("Here: %s", sRef_unparseFull (ret->sref)));
3905 ** this is yucky! should keep the uentry as part of exprNode!
3908 uentry exprNode_getUentry (exprNode e)
3910 if (exprNode_isError (e))
3912 return uentry_undefined;
3916 cstring s = exprNode_rootVarName (e);
3917 uentry ue = usymtab_lookupSafe (s);
3924 ** Returns true iff e1 and e2 are both exactly the same storage
3928 static bool exprNode_sameStorage (exprNode e1, exprNode e2)
3930 sRef s1 = exprNode_getSref (e1);
3931 sRef s2 = exprNode_getSref (e2);
3933 return (sRef_realSame (s1, s2));
3937 exprNode_makeInitBlock (lltok brace, /*@only@*/ exprNodeList inits)
3939 exprNode ret = exprNode_createPlain (ctype_unknown);
3941 ret->kind = XPR_INITBLOCK;
3942 ret->edata = exprData_makeCall (exprNode_undefined, inits);
3943 ret->loc = fileloc_update (ret->loc, lltok_getLoc (brace));
3949 exprNode_functionCall (/*@only@*/ exprNode f, /*@only@*/ exprNodeList args)
3954 usymtab_checkAllValid ();
3957 if (exprNode_isUndefined (f))
3960 exprNodeList_free (args);
3961 return exprNode_undefined;
3964 t = exprNode_getType (f);
3966 if (sRef_isLocalVar (f->sref))
3968 exprNode_checkUse (f, f->sref, f->loc);
3970 if (sRef_possiblyNull (f->sref))
3972 if (!usymtab_isGuarded (f->sref))
3974 if (optgenerror (FLG_NULLDEREF,
3975 message ("Function call using %s pointer %q",
3976 sRef_nullMessage (f->sref),
3977 sRef_unparse (f->sref)),
3980 sRef_showNullInfo (f->sref);
3981 sRef_setNullError (f->sref);
3989 if (ctype_isRealFunction (t))
3991 exprNode ret = functionCallSafe (f, t, args);
3995 else if (ctype_isUnknown (t))
3997 exprNode ret = exprNode_createPartialCopy (f);
4003 exprNodeList_elements (args, current)
4005 if (exprNode_isDefined (current))
4007 exprNode_checkUse (ret, current->sref, ret->loc);
4010 ** also, anything derivable from current->sref may be used
4013 exprNode_addUse (ret, sRef_makeDerived (current->sref));
4014 exprNode_mergeUSs (ret, current);
4016 } end_exprNodeList_elements;
4018 ret->edata = exprData_makeCall (f, args);
4019 ret->kind = XPR_CALL;
4021 tstring = cstring_copy (exprNode_unparse (f));
4023 cstring_markOwned (tstring);
4024 exprNode_checkSetAny (ret, tstring);
4030 voptgenerror (FLG_TYPE,
4031 message ("Call to non-function (type %t): %s", t,
4032 exprNode_unparse (f)),
4035 exprNodeList_free (args);
4037 return (exprNode_makeError ());
4042 exprNode_fieldAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
4043 /*@only@*/ cstring f)
4045 exprNode ret = exprNode_createPartialCopy (s);
4047 ret->kind = XPR_FACCESS;
4049 if (exprNode_isError (s))
4051 ret->edata = exprData_makeField (s, f);
4056 ctype t = exprNode_getType (s);
4057 ctype tr = ctype_realType (t);
4059 checkMacroParen (s);
4061 ret->edata = exprData_makeField (s, f);
4063 if (ctype_isStructorUnion (tr))
4065 uentry tf = uentryList_lookupField (ctype_getFields (tr), f);
4067 if (uentry_isUndefined (tf))
4069 voptgenerror (FLG_TYPE,
4070 message ("Access non-existent field %s of %t: %s", f, t,
4071 exprNode_unparse (ret)),
4073 /*! cstring_free (f); */ /* evans 2001-03-25 self-detect */
4078 uentry_setUsed (tf, exprNode_loc (ret));
4080 ret->typ = uentry_getType (tf);
4081 checkSafeUse (ret, s->sref);
4083 ret->sref = sRef_makeField (s->sref, uentry_rawName (tf));
4084 /*!? exprNode_free (s); */ /* evans 2001-03-25 self-detect */
4088 else /* isStructorUnion */
4090 if (ctype_isRealAbstract (tr))
4094 message ("Access field of abstract type (%t): %s.%s",
4095 t, exprNode_unparse (s), f),
4097 ret->typ = ctype_unknown;
4101 if (ctype_isKnown (tr))
4106 ("Access field of non-struct or union (%t): %s.%s",
4107 t, exprNode_unparse (s), f),
4110 ret->typ = ctype_unknown;
4114 cstring sn = cstring_copy (f);
4116 checkSafeUse (ret, s->sref);
4117 cstring_markOwned (sn);
4118 ret->sref = sRef_makeField (s->sref, sn);
4130 exprNode_fieldAccess (/*@only@*/ exprNode s, /*@only@*/ lltok dot,
4131 /*@only@*/ cstring f)
4133 exprNode res = exprNode_fieldAccessAux (s, lltok_getLoc (dot), f);
4139 exprNode_addParens (/*@only@*/ lltok lpar, /*@only@*/ exprNode e)
4141 exprNode ret = exprNode_createPartialCopy (e);
4143 ret->loc = fileloc_update (ret->loc, lltok_getLoc (lpar));
4144 ret->kind = XPR_PARENS;
4145 ret->edata = exprData_makeUop (e, lpar);
4147 if (!exprNode_isError (e))
4149 ret->exitCode = e->exitCode;
4150 ret->canBreak = e->canBreak;
4151 ret->mustBreak = e->mustBreak;
4152 ret->isJumpPoint = e->isJumpPoint;
4153 ret->sref = e->sref;
4160 exprNode_arrowAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
4161 /*@only@*/ cstring f)
4163 exprNode ret = exprNode_createPartialCopy (s);
4165 ret->edata = exprData_makeField (s, f);
4166 ret->kind = XPR_ARROW;
4168 if (exprNode_isError (s))
4174 ctype t = exprNode_getType (s);
4175 ctype tr = ctype_realType (t);
4177 checkMacroParen (s);
4179 (void) ctype_fixArrayPtr (tr); /* REWRITE THIS */
4181 if (ctype_isRealPointer (tr))
4183 ctype b = ctype_realType (ctype_baseArrayPtr (tr));
4185 if (ctype_isStructorUnion (b))
4187 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4189 if (sRef_isKnown (s->sref) && sRef_possiblyNull (s->sref))
4191 if (!usymtab_isGuarded (s->sref) && !context_inProtectVars ())
4195 message ("Arrow access from %s pointer%q: %s",
4196 sRef_nullMessage (s->sref),
4197 sRef_unparsePreOpt (s->sref),
4198 exprNode_unparse (ret)),
4201 sRef_showNullInfo (s->sref);
4202 sRef_setNullError (s->sref);
4207 if (uentry_isUndefined (fentry))
4211 message ("Access non-existent field %s of %t: %s",
4212 f, t, exprNode_unparse (ret)),
4214 ret->typ = ctype_unknown;
4220 ** was safeUse: shouldn't be safe!
4223 ** rec must be defined,
4224 ** *rec must be allocated
4225 ** rec->field need only be defined it if is an rvalue
4228 uentry_setUsed (fentry, exprNode_loc (ret));
4229 ret->typ = uentry_getType (fentry);
4231 exprNode_checkUse (ret, s->sref, s->loc);
4233 /* exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc); */
4234 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4238 else /* Pointer to something that is not a struct or union*/
4240 if (ctype_isRealAbstract (tr))
4242 ctype xrt = ctype_forceRealType (tr);
4246 message ("Arrow access field of abstract type (%t): %s->%s",
4247 t, exprNode_unparse (s), f),
4251 ** Set the state correctly, as if the abstraction is broken.
4254 if (ctype_isRealPointer (xrt) &&
4255 (b = ctype_realType (ctype_baseArrayPtr (xrt)),
4256 ctype_isStructorUnion (b)))
4258 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4259 ret->typ = uentry_getType (fentry);
4260 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4264 ret->typ = ctype_unknown;
4265 ret->sref = sRef_undefined;
4268 else /* not a struct, union or abstract */
4270 if (ctype_isUnknown (tr)) {
4271 cstring sn = cstring_copy (f);
4273 DPRINTF (("Here: %s", exprNode_unparse (s)));
4275 exprNode_checkUse (ret, s->sref, s->loc);
4276 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4278 cstring_markOwned (sn);
4279 ret->sref = sRef_makeArrow (s->sref, sn);
4281 ret->kind = XPR_ARROW;
4286 message ("Arrow access field of non-struct or union "
4287 "pointer (%t): %s->%s",
4288 t, exprNode_unparse (s), f),
4291 ret->typ = ctype_unknown;
4292 ret->sref = sRef_undefined;
4297 else /* its not a pointer */
4299 if (!ctype_isUnknown (tr))
4303 message ("Arrow access of non-pointer (%t): %s->%s",
4304 t, exprNode_unparse (s), f),
4307 ret->typ = ctype_unknown;
4308 ret->sref = sRef_undefined;
4312 cstring sn = cstring_copy (f);
4314 DPRINTF (("Here: %s", exprNode_unparse (s)));
4316 exprNode_checkUse (ret, s->sref, s->loc);
4317 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4319 cstring_markOwned (sn);
4320 ret->sref = sRef_makeArrow (s->sref, sn);
4322 ret->kind = XPR_ARROW;
4333 exprNode_arrowAccess (/*@only@*/ exprNode s,
4334 /*@only@*/ lltok arrow,
4335 /*@only@*/ cstring f)
4337 exprNode res = exprNode_arrowAccessAux (s, lltok_getLoc (arrow), f);
4343 ** only postOp's in C: i++ and i--
4347 exprNode_postOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4349 /* check modification also */
4350 /* cstring opname = lltok_unparse (op);*/
4352 exprNode ret = exprNode_createPartialCopy (e);
4354 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4355 ret->kind = XPR_POSTOP;
4356 ret->edata = exprData_makeUop (e, op);
4358 if (!exprNode_isDefined (e))
4363 checkMacroParen (e);
4365 exprNode_checkUse (ret, e->sref, e->loc);
4366 exprNode_checkSet (ret, e->sref);
4368 t = exprNode_getType (e);
4370 if (sRef_isUnsafe (e->sref))
4372 voptgenerror (FLG_MACROPARAMS,
4373 message ("Operand of %s is macro parameter (non-functional): %s%s",
4374 lltok_unparse (op), exprNode_unparse (e), lltok_unparse (op)),
4376 sRef_makeSafe (e->sref);
4377 sRef_makeSafe (ret->sref);
4380 if (ctype_isForceRealNumeric (&t) || ctype_isRealAP (t))
4386 if (ctype_isRealAbstract (t))
4390 message ("Operand of %s is abstract type (%t): %s",
4391 lltok_unparse (op), t, exprNode_unparse (e)),
4398 message ("Operand of %s is non-numeric (%t): %s",
4399 lltok_unparse (op), t, exprNode_unparse (e)),
4402 ret->typ = ctype_unknown;
4405 /* if (ctype_isZero (t)) e->typ = ctype_int; */
4407 exprNode_checkModify (e, ret);
4409 /* added 7/11/2000 D.L */
4411 /*DRL 6/8/01 I decided to disable all Splint warnings here since the code
4412 probably needs a rewrite any way */
4417 /* updateEnvironmentForPostOp (e); */
4419 /* start modifications */
4420 /* added by Seejo on 4/16/2000 */
4422 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4424 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4426 ret->sref = sRef_copy (e->sref);
4429 if (lltok_getTok (op) == INC_OP) {
4430 if (sRef_getSize(e->sref) > 0) {
4432 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4434 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4435 /* Assumption: there is only 1 \0 in the buffer */
4436 /* This will not be correct if there are 2 \0's in the buffer */
4437 sRef_setNotNullTerminatedState(ret->sref);
4438 sRef_resetLen(ret->sref);
4440 sRef_setNullTerminatedState(ret->sref);
4441 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4443 if (sRef_isNullTerminated (ret->sref))
4444 printf ("ret->sref is Null Terminated\n");
4445 else if (sRef_isPossiblyNullTerminated (ret->sref))
4446 printf ("ret->sref is Possibly Null Terminated\n");
4447 else if (sRef_isNotNullTerminated (ret->sref))
4448 printf ("ret->sref is Not Null Terminated\n");
4453 if (lltok_getTok (op) == DEC_OP) {
4454 if (sRef_getSize(e->sref) >= 0) {
4455 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4456 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4461 /* end modifications */
4467 exprNode_preOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4469 bool checkMod = FALSE;
4471 int opid = lltok_getTok (op);
4472 exprNode ret = exprNode_createSemiCopy (e);
4474 exprNode_copySets (ret, e);
4476 multiVal_free (ret->val);
4477 ret->val = multiVal_undefined;
4478 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4479 ret->kind = XPR_PREOP;
4480 ret->edata = exprData_makeUop (e, op);
4482 if (exprNode_isError (e))
4487 checkMacroParen (e);
4489 te = exprNode_getType (e);
4490 tr = ctype_realType (te);
4492 if (opid != TAMPERSAND)
4494 exprNode_checkUse (ret, e->sref, e->loc);
4496 if (ctype_isRealAbstract (tr)
4497 && (!(ctype_isRealBool (te) && (opid == TEXCL))))
4499 if (optgenerror (FLG_ABSTRACT,
4500 message ("Operand of %s is abstract type (%t): %s",
4501 lltok_unparse (op), tr,
4502 exprNode_unparse (ret)),
4505 tr = te = ctype_unknown;
4506 ret->typ = ctype_unknown;
4507 sRef_setNullError (e->sref);
4515 case DEC_OP: /* should also check modification! */
4516 if (sRef_isMacroParamRef (e->sref))
4520 message ("Operand of %s is macro parameter (non-functional): %s",
4521 lltok_unparse (op), exprNode_unparse (ret)),
4526 exprNode_checkSet (ret, e->sref);
4529 if (ctype_isForceRealNumeric (&tr) || ctype_isRealAP (tr))
4534 if (context_msgStrictOps ())
4538 message ("Operand of %s is non-numeric (%t): %s",
4539 lltok_unparse (op), te, exprNode_unparse (ret)),
4542 ret->typ = ctype_int;
4545 /* start modifications */
4546 /* added by Seejo on 4/16/2000 */
4548 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4550 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4552 ret->sref = sRef_copy (e->sref);
4555 if (lltok_getTok (op) == INC_OP) {
4556 if (sRef_getSize(e->sref) > 0) {
4558 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4560 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4561 /* Assumption: there is only 1 \0 in the buffer */
4562 /* This will not be correct if there are 2 \0's in the buffer */
4563 sRef_setNotNullTerminatedState(ret->sref);
4564 sRef_resetLen (ret->sref);
4566 sRef_setNullTerminatedState(ret->sref);
4567 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4573 if (lltok_getTok (op) == DEC_OP) {
4574 if (sRef_getSize(e->sref) >= 0) {
4575 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4576 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4581 /* end modifications */
4588 if (ctype_isForceRealNumeric (&tr))
4592 ret->val = multiVal_invert (exprNode_getValue (e));
4596 ret->val = multiVal_copy (exprNode_getValue (e));
4601 if (context_msgStrictOps ())
4605 message ("Operand of %s is non-numeric (%t): %s",
4606 lltok_unparse (op), te, exprNode_unparse (ret)),
4610 ret->typ = ctype_int;
4614 case TEXCL: /* maybe this should be restricted */
4615 guardSet_flip (ret->guards);
4617 if (ctype_isRealBool (te) || ctype_isUnknown (te))
4623 if (ctype_isRealPointer (tr))
4625 if (sRef_isKnown (e->sref))
4627 ret->guards = guardSet_addFalseGuard (ret->guards, e->sref);
4631 (FLG_BOOLOPS, FLG_PTRNEGATE,
4632 message ("Operand of %s is non-boolean (%t): %s",
4633 lltok_unparse (op), te, exprNode_unparse (ret)),
4640 message ("Operand of %s is non-boolean (%t): %s",
4641 lltok_unparse (op), te, exprNode_unparse (ret)),
4645 ret->typ = ctype_bool;
4650 if (ctype_isForceRealInt (&tr))
4655 if (context_msgStrictOps ())
4659 message ("Operand of %s is non-integer (%t): %s",
4660 lltok_unparse (op), te, exprNode_unparse (ret)),
4664 if (ctype_isInt (e->typ))
4670 ret->typ = ctype_int;
4676 ret->typ = ctype_makePointer (e->typ);
4678 if (sRef_isKnown (e->sref))
4680 ret->sref = sRef_makeAddress (e->sref);
4687 if (ctype_isAP (tr))
4689 ret->typ = ctype_baseArrayPtr (e->typ);
4693 if (ctype_isKnown (te))
4695 if (ctype_isFunction (te))
4701 message ("Dereference of function type (%t): %s",
4702 te, exprNode_unparse (ret)),
4707 voptgenerror (FLG_TYPE,
4708 message ("Dereference of non-pointer (%t): %s",
4709 te, exprNode_unparse (ret)),
4711 ret->typ = ctype_unknown;
4716 ret->typ = ctype_unknown;
4721 if (sRef_isKnown (e->sref))
4723 DPRINTF (("Checking possibly null: %s", sRef_unparseFull (e->sref)));
4725 if (sRef_possiblyNull (e->sref))
4727 DPRINTF (("Checking possibly null: %s", sRef_unparse (e->sref)));
4728 if (!usymtab_isGuarded (e->sref) && !context_inProtectVars ())
4732 message ("Dereference of %s pointer %q: %s",
4733 sRef_nullMessage (e->sref),
4734 sRef_unparse (e->sref),
4735 exprNode_unparse (ret)),
4738 sRef_showNullInfo (e->sref);
4739 sRef_setNotNull (e->sref, e->loc); /* suppress future messages */
4744 ret->sref = sRef_makePointer (e->sref);
4749 llbug (message ("exprNode_preOp: unhandled op: %s", lltok_unparse (op)));
4754 exprNode_checkModify (e, ret);
4761 ** any reason to disallow sizeof (abstract type) ?
4765 ** used by both sizeof
4769 ctype sizeof_resultType (void)
4771 static ctype sizet = ctype_unknown;
4773 if (ctype_isUnknown (sizet))
4775 if (usymtab_existsType (cstring_makeLiteralTemp ("size_t")))
4777 sizet = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("size_t")));
4781 sizet = ctype_ulint;
4788 exprNode_sizeofType (/*@only@*/ qtype qt)
4790 exprNode ret = exprNode_create (sizeof_resultType ());
4791 ctype ct = qtype_getType (qt);
4793 ret->kind = XPR_SIZEOFT;
4794 ret->edata = exprData_makeSizeofType (qt);
4796 voptgenerror (FLG_SIZEOFTYPE,
4797 message ("Parameter to sizeof is type %s: %s",
4799 exprNode_unparse (ret)),
4806 exprNode_alignofType (/*@only@*/ qtype qt)
4808 exprNode ret = exprNode_create (sizeof_resultType ());
4809 ctype ct = qtype_getType (qt);
4811 ret->kind = XPR_ALIGNOFT;
4812 ret->edata = exprData_makeSizeofType (qt);
4814 voptgenerror (FLG_SIZEOFTYPE,
4815 message ("Parameter to alignof is type %s: %s",
4817 exprNode_unparse (ret)),
4823 exprNode exprNode_offsetof (qtype qt, cstringList s)
4825 exprNode ret = exprNode_create (sizeof_resultType ());
4826 ctype ct = qtype_getType (qt);
4828 ret->kind = XPR_OFFSETOF;
4829 ret->edata = exprData_makeOffsetof (qt, s);
4831 if (!ctype_isRealSU (ct))
4833 voptgenerror (FLG_TYPE,
4834 message ("First parameter to offsetof is not a "
4835 "struct or union type (type %s): %s",
4837 exprNode_unparse (ret)),
4844 cstringList_elements (s, el) {
4848 if (ctype_isUndefined (lt))
4852 else if (!ctype_isRealSU (lt))
4854 voptgenerror (FLG_TYPE,
4855 message ("Inner offsetof type is not a "
4856 "struct or union type (type %s before field %s): %s",
4857 ctype_unparse (lt), el,
4858 exprNode_unparse (ret)),
4864 fields = ctype_getFields (ctype_realType (lt));
4865 fld = uentryList_lookupField (fields, el);
4866 DPRINTF (("Try: %s / %s", ctype_unparse (lt), el));
4868 if (uentry_isUndefined (fld))
4870 if (ctype_equal (lt, ct)) {
4871 voptgenerror (FLG_TYPE,
4872 message ("Field %s in offsetof is not the "
4873 "name of a field of %s: %s",
4876 exprNode_unparse (ret)),
4879 voptgenerror (FLG_TYPE,
4880 message ("Deep field %s in offsetof is not the "
4881 "name of a field of %s: %s",
4884 exprNode_unparse (ret)),
4890 lt = uentry_getType (fld);
4893 } end_cstringList_elements;
4895 /* Should report error if its a bit field - behavior is undefined! */
4902 exprNode_sizeofExpr (/*@only@*/ exprNode e)
4906 if (exprNode_isUndefined (e))
4908 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4909 ret->edata = exprData_makeSingle (e);
4910 ret->typ = sizeof_resultType ();
4911 ret->kind = XPR_SIZEOF;
4915 uentry u = exprNode_getUentry (e);
4917 ret = exprNode_createPartialCopy (e);
4918 ret->edata = exprData_makeSingle (e);
4920 ret->typ = sizeof_resultType ();
4921 ret->kind = XPR_SIZEOF;
4923 if (uentry_isValid (u)
4924 && uentry_isRefParam (u)
4925 && ctype_isRealArray (uentry_getType (u)))
4928 (FLG_SIZEOFFORMALARRAY,
4929 message ("Parameter to sizeof is an array-type function parameter: %s",
4930 exprNode_unparse (ret)),
4936 ** sizeof (x) doesn't "really" use x
4943 exprNode_alignofExpr (/*@only@*/ exprNode e)
4947 if (exprNode_isUndefined (e))
4949 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4953 ret = exprNode_createPartialCopy (e);
4956 ret->edata = exprData_makeSingle (e);
4957 ret->typ = sizeof_resultType ();
4958 ret->kind = XPR_ALIGNOF;
4961 ** sizeof (x) doesn't "really" use x
4968 exprNode_cast (/*@only@*/ lltok tok, /*@only@*/ exprNode e, /*@only@*/ qtype q)
4974 if (exprNode_isError (e))
4978 return exprNode_undefined;
4981 checkMacroParen (e);
4983 c = qtype_getType (q);
4984 t = exprNode_getType (e);
4986 ret = exprNode_createPartialCopy (e);
4988 ret->loc = fileloc_update (ret->loc, lltok_getLoc (tok));
4990 ret->kind = XPR_CAST;
4991 ret->edata = exprData_makeCast (tok, e, q);
4993 ret->sref = sRef_copy (e->sref);
4995 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
4997 if (!sRef_isConst (e->sref))
4999 usymtab_addForceMustAlias (ret->sref, e->sref);
5002 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
5003 sRef_setTypeFull (ret->sref, c);
5004 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
5010 ** void * <-> abstract * (if FLG_ABSTVOIDP)
5011 ** abstract * <-> void * (if FLG_ABSTVOIDP)
5014 if (ctype_isVoid (c)) /* cast to void is always okay --- discard value */
5016 /* evans 2002-07-19: added this warning */
5017 DPRINTF (("Checking: %s / %s", exprNode_unparse (ret), sRef_unparseFull (ret->sref)));
5018 if (sRef_isFresh (ret->sref))
5022 message ("New fresh storage %q(type %s) cast to void (not released): %s",
5023 sRef_unparseOpt (ret->sref),
5024 ctype_unparse (exprNode_getType (ret)),
5025 exprNode_unparse (ret)),
5026 exprNode_loc (ret));
5029 else if (ctype_isRealAP (c)) /* casting to array or pointer */
5031 ctype bc = ctype_getBaseType (c);
5032 ctype bt = ctype_getBaseType (t);
5033 ctype rt = ctype_realType (t);
5035 if (ctype_isFunction (ctype_baseArrayPtr (ctype_realType (c)))
5036 && (ctype_isArrayPtr (rt)
5037 && !ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
5041 message ("Cast from function pointer type (%t) to "
5042 "non-function pointer (%t): %s",
5043 c, t, exprNode_unparse (ret)),
5047 if (!ctype_isFunction (ctype_baseArrayPtr (c))
5048 && (ctype_isArrayPtr (rt)
5049 && ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
5053 message ("Cast from non-function pointer type (%t) to "
5054 "function pointer (%t): %s",
5055 c, t, exprNode_unparse (ret)),
5059 if (exprNode_isZero (e) && context_getFlag (FLG_ZEROPTR) &&
5060 !(ctype_isRealAbstract (bc)
5061 && context_hasAccess (ctype_typeId (bc))))
5063 ; /* okay to cast zero */
5067 if (ctype_isRealAbstract (bc)
5068 && !context_hasAccess (ctype_typeId (bc)))
5070 if (ctype_isVoidPointer (t) || ctype_isUnknown (t))
5074 message ("Cast to underlying abstract type %t: %s",
5075 c, exprNode_unparse (ret)),
5082 message ("Cast to underlying abstract type %t: %s",
5083 c, exprNode_unparse (ret)),
5088 if (ctype_isRealAbstract (bt)
5089 && !context_hasAccess (ctype_typeId (bt)))
5091 if (ctype_isUnknown (c) || ctype_isVoidPointer (c))
5095 message ("Cast from underlying abstract type %t: %s",
5096 t, exprNode_unparse (ret)),
5103 message ("Cast from underlying abstract type %t: %s",
5104 t, exprNode_unparse (ret)),
5112 ctype bt = ctype_realType (ctype_getBaseType (t));
5113 ctype bc = ctype_realType (ctype_getBaseType (c));
5115 if (ctype_isAbstract (bt) && !context_hasAccess (ctype_typeId (bt)))
5117 if (ctype_match (c, t))
5119 if (ctype_equal (c, t))
5123 message ("Redundant cast involving abstract type %t: %s",
5124 bt, exprNode_unparse (ret)),
5132 message ("Cast from abstract type %t: %s",
5133 bt, exprNode_unparse (ret)),
5138 if (ctype_isAbstract (bc)
5139 && !context_hasAccess (ctype_typeId (bc)))
5141 if (ctype_match (c, t))
5147 DPRINTF (("No access to: %s / %d",
5148 ctype_unparse (bc), ctype_typeId (bc)));
5149 DPRINTF (("Context %s %s",
5150 bool_unparse (context_inFunctionLike ()),
5151 context_unparse ()));
5154 message ("Cast to abstract type %t: %s", bc,
5155 exprNode_unparse (ret)),
5161 if (ctype_isAbstract (c))
5163 if (sRef_isExposed (e->sref) || sRef_isOnly (e->sref))
5165 /* okay, cast exposed to abstract */
5166 sRef_clearExKindComplete (ret->sref, fileloc_undefined);
5170 if (ctype_isVisiblySharable (t)
5171 && sRef_isExternallyVisible (e->sref)
5172 && !(ctype_isAbstract (t)
5173 && context_hasAccess (ctype_typeId (t))))
5177 message ("Cast to abstract type from externally visible "
5178 "mutable storage exposes rep of %s: %s",
5180 exprNode_unparse (e)),
5190 evaluationOrderUndefined (lltok op)
5192 int opid = lltok_getTok (op);
5194 return (opid != AND_OP && opid != OR_OP);
5197 static bool checkIntegral (/*@notnull@*/ exprNode e1,
5198 /*@notnull@*/ exprNode e2,
5199 /*@notnull@*/ exprNode ret,
5204 ctype te1 = exprNode_getType (e1);
5205 ctype te2 = exprNode_getType (e2);
5207 ctype tr1 = ctype_realishType (te1);
5208 ctype tr2 = ctype_realishType (te2);
5210 if (ctype_isForceRealInt (&tr1) && ctype_isForceRealInt (&tr2))
5216 if (context_msgStrictOps ())
5218 if (!ctype_isInt (tr1) && !ctype_isInt (tr2))
5220 if (ctype_sameName (te1, te2))
5224 message ("Operands of %s are non-integer (%t): %s",
5225 lltok_unparse (op), te1,
5226 exprNode_unparse (ret)),
5233 message ("Operands of %s are non-integers (%t, %t): %s",
5234 lltok_unparse (op), te1, te2,
5235 exprNode_unparse (ret)),
5239 else if (!ctype_isInt (tr1))
5243 message ("Left operand of %s is non-integer (%t): %s",
5244 lltok_unparse (op), te1, exprNode_unparse (ret)),
5248 /* !ctype_isInt (te2) */
5252 message ("Right operand of %s is non-integer (%t): %s",
5253 lltok_unparse (op), te2, exprNode_unparse (ret)),
5263 ** returns exprNode representing e1 op e2
5265 ** uses msg if there are errors
5266 ** can be used for both assignment ops and regular ops
5271 static /*@only@*/ /*@notnull@*/ exprNode
5272 exprNode_makeOp (/*@keep@*/ exprNode e1, /*@keep@*/ exprNode e2,
5273 /*@keep@*/ lltok op)
5275 ctype te1, te2, tr1, tr2, tret;
5276 int opid = lltok_getTok (op);
5277 bool hasError = FALSE;
5280 if (exprNode_isError (e1))
5282 ret = exprNode_createPartialNVCopy (e2);
5286 ret = exprNode_createPartialNVCopy (e1);
5289 ret->val = multiVal_undefined;
5292 ret->edata = exprData_makeOp (e1, e2, op);
5294 if (exprNode_isError (e1) || exprNode_isError (e2))
5296 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5297 || opid == EQ_OP || opid == NE_OP
5298 || opid == AND_OP || opid == OR_OP)
5300 ret->typ = ctype_bool;
5303 if (exprNode_isDefined (e1))
5305 exprNode_checkUse (ret, e1->sref, e1->loc);
5308 if (exprNode_isDefined (e2))
5310 exprNode_mergeUSs (ret, e2);
5311 exprNode_checkUse (ret, e2->sref, e2->loc);
5317 tret = ctype_unknown;
5318 te1 = exprNode_getType (e1);
5320 DPRINTF (("te1 = %s / %s", exprNode_unparse (e1), ctype_unparse (te1)));
5322 te2 = exprNode_getType (e2);
5324 tr1 = ctype_realishType (te1);
5325 tr2 = ctype_realishType (te2);
5329 ret->guards = guardSet_or (ret->guards, e2->guards);
5331 else if (opid == AND_OP)
5333 ret->guards = guardSet_and (ret->guards, e2->guards);
5340 if (opid == EQ_OP || opid == NE_OP)
5342 exprNode temp1 = e1, temp2 = e2;
5344 /* could do NULL == x */
5346 if (exprNode_isNullValue (e1) || exprNode_isUnknownConstant (e1))
5348 temp1 = e2; temp2 = e1;
5351 if (exprNode_isNullValue (temp2) || exprNode_isUnknownConstant (temp2))
5353 reflectNullTest (temp1, (opid == NE_OP));
5354 guardSet_free (ret->guards);
5355 ret->guards = guardSet_copy (temp1->guards);
5359 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5360 || opid == EQ_OP || opid == NE_OP || opid == AND_OP || opid == OR_OP)
5365 if (anyAbstract (tr1, tr2) &&
5366 (!((ctype_isRealBool (te1) || ctype_isRealBool (te2)) &&
5367 (opid == AND_OP || opid == OR_OP
5368 || opid == EQ_OP || opid == NE_OP))))
5370 abstractOpError (tr1, tr2, op, e1, e2, e1->loc, e2->loc);
5372 else if (ctype_isUnknown (te1) || ctype_isUnknown (te2))
5374 /* unknown types, no comparisons possible */
5380 case TMULT: /* multiplication and division: */
5382 case MUL_ASSIGN: /* numeric, numeric -> numeric */
5383 case DIV_ASSIGN: /* */
5385 if (opid == TMULT || opid == MUL_ASSIGN)
5387 ret->val = multiVal_multiply (exprNode_getValue (e1),
5388 exprNode_getValue (e2));
5392 ret->val = multiVal_divide (exprNode_getValue (e1),
5393 exprNode_getValue (e2));
5396 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5399 case TPLUS: /* addition and subtraction: */
5400 case TMINUS: /* pointer, int -> pointer */
5401 case SUB_ASSIGN: /* int, pointer -> pointer */
5402 case ADD_ASSIGN: /* numeric, numeric -> numeric */
5404 if (opid == TPLUS || opid == ADD_ASSIGN)
5406 ret->val = multiVal_add (exprNode_getValue (e1),
5407 exprNode_getValue (e2));
5411 ret->val = multiVal_subtract (exprNode_getValue (e1),
5412 exprNode_getValue (e2));
5415 tr1 = ctype_fixArrayPtr (tr1);
5417 if ((ctype_isRealPointer (tr1) && !exprNode_isNullValue (e1))
5418 && (!ctype_isRealPointer (tr2) && ctype_isRealInt (tr2)))
5422 if (context_msgPointerArith ())
5426 message ("Pointer arithmetic (%t, %t): %s",
5427 te1, te2, exprNode_unparse (ret)),
5432 ** Swap terms so e1 is always the pointer
5435 if (ctype_isRealPointer (tr1))
5441 exprNode_swap (e1, e2);
5445 if (sRef_possiblyNull (e1->sref)
5446 && !usymtab_isGuarded (e1->sref))
5449 (FLG_NULLPOINTERARITH,
5450 message ("Pointer arithmetic involving possibly "
5451 "null pointer %s: %s",
5452 exprNode_unparse (e1),
5453 exprNode_unparse (ret)),
5457 ret->sref = sRef_copy (e1->sref);
5459 /* start modifications */
5460 /* added by Seejo on 4/16/2000 */
5462 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5464 if ((sRef_isPossiblyNullTerminated (e1->sref)) || (sRef_isNullTerminated(e1->sref))) {
5467 added ugly fixed to stop
5468 program from crashing on point + int +int
5469 one day I'll fix this or ask Seejo wtf the codes supposed to do. */
5471 if (!multiVal_isInt (e2->val) )
5475 val = (int) multiVal_forceInt (e2->val);
5477 /* Operator : + or += */
5478 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5479 if (sRef_getSize(e1->sref) >= val) {/* Incrementing the pointer by
5480 val should not result in a
5481 size < 0 (size = 0 is ok !) */
5483 sRef_setSize (ret->sref, sRef_getSize(e1->sref) - val);
5485 if (sRef_getLen(e1->sref) == val) { /* i.e. the character at posn val is \0 */
5486 sRef_setNotNullTerminatedState(ret->sref);
5487 sRef_resetLen (ret->sref);
5489 sRef_setNullTerminatedState(ret->sref);
5490 sRef_setLen (ret->sref, sRef_getLen(e1->sref) - val);
5495 /* Operator : - or -= */
5496 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5497 if (sRef_getSize(e1->sref) >= 0) {
5498 sRef_setSize (ret->sref, sRef_getSize(e1->sref) + val);
5499 sRef_setLen (ret->sref, sRef_getLen(e1->sref) + val);
5504 /* end modifications */
5506 sRef_setNullError (ret->sref);
5509 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5510 ** since is points to storage that should not be deallocated
5511 ** through this pointer.
5514 if (sRef_isOnly (ret->sref)
5515 || sRef_isFresh (ret->sref))
5517 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5522 else if ((!ctype_isRealPointer(tr1) && ctype_isRealInt (tr1))
5523 && (ctype_isRealPointer (tr2) && !exprNode_isNullValue (e2)))
5525 if (context_msgPointerArith ())
5529 message ("Pointer arithmetic (%t, %t): %s",
5530 te1, te2, exprNode_unparse (ret)),
5534 if (sRef_possiblyNull (e1->sref)
5535 && !usymtab_isGuarded (e1->sref))
5538 (FLG_NULLPOINTERARITH,
5539 message ("Pointer arithmetic involving possibly "
5540 "null pointer %s: %s",
5541 exprNode_unparse (e2),
5542 exprNode_unparse (ret)),
5546 ret->sref = sRef_copy (e2->sref);
5548 /* start modifications */
5549 /* added by Seejo on 4/16/2000 */
5551 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5554 if ((sRef_isPossiblyNullTerminated (e2->sref)) || (sRef_isNullTerminated(e2->sref))) {
5555 int val = (int) multiVal_forceInt (e1->val);
5557 /* Operator : + or += */
5558 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5559 if (sRef_getSize(e2->sref) >= val) {/* Incrementing the pointer by
5560 val should not result in a
5561 size < 0 (size = 0 is ok !) */
5563 sRef_setSize (ret->sref, sRef_getSize(e2->sref) - val);
5565 if (sRef_getLen(e2->sref) == val) { /* i.e. the character at posn val is \0 */
5566 sRef_setNotNullTerminatedState(ret->sref);
5567 sRef_resetLen (ret->sref);
5569 sRef_setNullTerminatedState(ret->sref);
5570 sRef_setLen (ret->sref, sRef_getLen(e2->sref) - val);
5575 /* Operator : - or -= */
5576 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5577 if (sRef_getSize(e2->sref) >= 0) {
5578 sRef_setSize (ret->sref, sRef_getSize(e2->sref) + val);
5579 sRef_setLen (ret->sref, sRef_getLen(e2->sref) + val);
5583 /* end modifications */
5585 sRef_setNullError (ret->sref);
5588 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5589 ** since is points to storage that should not be deallocated
5590 ** through this pointer.
5593 if (sRef_isOnly (ret->sref)
5594 || sRef_isFresh (ret->sref)) {
5595 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5599 ret->sref = e2->sref;
5603 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5612 case TAMPERSAND: /* bitwise & */
5614 case TCIRC: /* ^ (XOR) */
5619 bool reported = FALSE;
5625 if (opid == LEFT_OP || opid == LEFT_ASSIGN
5626 || opid == RIGHT_OP || opid == RIGHT_ASSIGN)
5629 ** evans 2002-01-01: fixed this to follow ISO 6.5.7.
5632 if (!ctype_isUnsigned (tr2)
5633 && !exprNode_isNonNegative (e2))
5635 reported = optgenerror
5637 message ("Right operand of %s may be negative (%t): %s",
5638 lltok_unparse (op), te2,
5639 exprNode_unparse (ret)),
5643 if (!ctype_isUnsigned (tr1)
5644 && !exprNode_isNonNegative (e1))
5646 reported = optgenerror
5647 (FLG_SHIFTIMPLEMENTATION,
5648 message ("Left operand of %s may be negative (%t): %s",
5649 lltok_unparse (op), te1,
5650 exprNode_unparse (ret)),
5655 ** Should check size of right operand also...
5661 if (!ctype_isUnsigned (tr1))
5663 if (exprNode_isNonNegative (e1)) {
5666 reported = optgenerror
5668 message ("Left operand of %s is not unsigned value (%t): %s",
5669 lltok_unparse (op), te1,
5670 exprNode_unparse (ret)),
5680 if (!ctype_isUnsigned (tr2))
5682 if (!exprNode_isNonNegative (e2)) {
5683 reported = optgenerror
5685 message ("Right operand of %s is not unsigned value (%t): %s",
5686 lltok_unparse (op), te2,
5687 exprNode_unparse (ret)),
5696 if (!checkIntegral (e1, e2, ret, op)) {
5697 te1 = ctype_unknown;
5701 DPRINTF (("Set: %s", ctype_unparse (te1)));
5704 ** tret is the widest type of te1 and te2
5707 tret = ctype_widest (te1, te2);
5712 if (checkIntegral (e1, e2, ret, op)) {
5715 tret = ctype_unknown;
5720 case TLT: /* comparisons */
5721 case TGT: /* numeric, numeric -> bool */
5723 DPRINTF (("Here we go: %s / %s",
5724 ctype_unparse (tr1), ctype_unparse (tr2)));
5726 if ((ctype_isReal (tr1) && !ctype_isInt (tr1))
5727 || (ctype_isReal (tr2) && !ctype_isInt (tr2)))
5730 bool fepsilon = FALSE;
5732 if (!ctype_isReal (rtype) || ctype_isInt (rtype))
5737 if (opid == TLT || opid == TGT)
5739 uentry ue1 = exprNode_getUentry (e1);
5740 uentry ue2 = exprNode_getUentry (e2);
5743 ** FLT_EPSILON, etc. really is a variable, not
5747 if (uentry_isVariable (ue1))
5749 cstring uname = uentry_rawName (ue1);
5751 if (cstring_equalLit (uname, "FLT_EPSILON")
5752 || cstring_equalLit (uname, "DBL_EPSILON")
5753 || cstring_equalLit (uname, "LDBL_EPSILON"))
5759 if (uentry_isVariable (ue2))
5761 cstring uname = uentry_rawName (ue2);
5763 if (cstring_equalLit (uname, "FLT_EPSILON")
5764 || cstring_equalLit (uname, "DBL_EPSILON")
5765 || cstring_equalLit (uname, "LDBL_EPSILON"))
5774 ; /* Don't complain. */
5780 message ("Dangerous comparison involving %s types: %s",
5781 ctype_unparse (rtype),
5782 exprNode_unparse (ret)),
5791 ** Types should match.
5794 DPRINTF (("Match types: %s / %s", exprNode_unparse (e1),
5795 exprNode_unparse (e2)));
5797 if (!exprNode_matchTypes (e1, e2))
5799 hasError = gentypeerror
5801 message ("Operands of %s have incompatible types (%t, %t): %s",
5802 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5808 || (ctype_isForceRealNumeric (&tr1)
5809 && ctype_isForceRealNumeric (&tr2)) ||
5810 (ctype_isRealPointer (tr1) && ctype_isRealPointer (tr2)))
5816 if ((ctype_isRealNumeric (tr1) && ctype_isRealPointer (tr2)) ||
5817 (ctype_isRealPointer (tr1) && ctype_isRealNumeric (tr2)))
5821 message ("Comparison of pointer and numeric (%t, %t): %s",
5822 te1, te2, exprNode_unparse (ret)),
5827 (void) checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5832 /* certain comparisons on unsigned's and zero look suspicious */
5834 if (opid == TLT || opid == LE_OP || opid == GE_OP)
5836 if ((ctype_isUnsigned (tr1) && exprNode_isZero (e2))
5837 || (ctype_isUnsigned (tr2) && exprNode_isZero (e1)))
5840 (FLG_UNSIGNEDCOMPARE,
5841 message ("Comparison of unsigned value involving zero: %s",
5842 exprNode_unparse (ret)),
5847 /* EQ_OP should NOT be used with booleans (unless one is FALSE) */
5849 if ((opid == EQ_OP || opid == NE_OP) &&
5850 ctype_isDirectBool (tr1) && ctype_isDirectBool (tr2))
5853 ** is one a variable?
5856 if (uentry_isVariable (exprNode_getUentry (e1))
5857 || uentry_isVariable (exprNode_getUentry (e2)))
5860 ** comparisons with FALSE are okay
5863 if (exprNode_isFalseConstant (e1)
5864 || exprNode_isFalseConstant (e2))
5873 ("Use of %q with %s variables (risks inconsistency because "
5874 "of multiple true values): %s",
5875 cstring_makeLiteral ((opid == EQ_OP) ? "==" : "!="),
5876 context_printBoolName (), exprNode_unparse (ret)),
5883 case AND_OP: /* bool, bool -> bool */
5886 if (ctype_isForceRealBool (&tr1) && ctype_isForceRealBool (&tr2))
5892 if (context_maybeSet (FLG_BOOLOPS))
5894 if (!ctype_isRealBool (te1) && !ctype_isRealBool (te2))
5896 if (ctype_sameName (te1, te2))
5900 message ("Operands of %s are non-boolean (%t): %s",
5901 lltok_unparse (op), te1,
5902 exprNode_unparse (ret)),
5910 ("Operands of %s are non-booleans (%t, %t): %s",
5911 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5915 else if (!ctype_isRealBool (te1))
5919 message ("Left operand of %s is non-boolean (%t): %s",
5920 lltok_unparse (op), te1, exprNode_unparse (ret)),
5923 else if (!ctype_isRealBool (te2))
5927 message ("Right operand of %s is non-boolean (%t): %s",
5928 lltok_unparse (op), te2, exprNode_unparse (ret)),
5941 (cstring_makeLiteral
5942 ("There has been a problem in the parser. This is believed to result "
5943 "from a problem with bison v. 1.25. Please try rebuidling Splint "
5944 "using the pre-compiled grammar files by commenting out the "
5945 "BISON= line in the top-level Makefile."));
5950 DPRINTF (("Return type: %s", ctype_unparse (tret)));
5953 exprNode_checkUse (ret, e1->sref, e1->loc);
5954 exprNode_mergeUSs (ret, e2);
5955 exprNode_checkUse (ret, e2->sref, e2->loc);
5961 exprNode_op (/*@only@*/ exprNode e1, /*@keep@*/ exprNode e2,
5962 /*@only@*/ lltok op)
5966 checkMacroParen (e1);
5967 checkMacroParen (e2);
5969 if (evaluationOrderUndefined (op) && context_maybeSet (FLG_EVALORDER))
5971 checkExpressionDefined (e1, e2, op);
5974 ret = exprNode_makeOp (e1, e2, op);
5979 void exprNode_checkAssignMod (exprNode e1, exprNode ret)
5982 ** This is somewhat bogus!
5984 ** Assigning to a nested observer in a non-observer datatype
5985 ** should not produce an error.
5988 sRef ref = exprNode_getSref (e1);
5990 DPRINTF (("Check assign mod: %s",
5991 sRef_unparseFull (ref)));
5993 if (sRef_isObserver (ref)
5994 || ((sRef_isFileStatic (ref) || sRef_isFileOrGlobalScope (ref))
5995 && ctype_isArray (ctype_realType (sRef_getType (ref)))))
5997 sRef base = sRef_getBase (ref);
5999 if (sRef_isValid (base) && sRef_isObserver (base))
6001 exprNode_checkModify (e1, ret);
6005 exprNode_checkModifyVal (e1, ret);
6010 exprNode_checkModify (e1, ret);
6015 exprNode_assign (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2, /*@only@*/ lltok op)
6017 bool isalloc = FALSE;
6018 bool isjustalloc = FALSE;
6019 bool noalias = FALSE;
6022 DPRINTF (("%s [%s] <- %s [%s]",
6023 exprNode_unparse (e1),
6024 ctype_unparse (e1->typ),
6025 exprNode_unparse (e2),
6026 ctype_unparse (e2->typ)));
6028 if (lltok_getTok (op) != TASSIGN)
6030 ret = exprNode_makeOp (e1, e2, op);
6032 DPRINTF (("Here goes: %s %s",
6033 ctype_unparse (e1->typ),
6034 ctype_unparse (e2->typ)));
6036 if (exprNode_isDefined (e1)
6037 && exprNode_isDefined (e2))
6039 if (ctype_isNumeric (e2->typ)
6040 || ctype_isNumeric (e1->typ))
6042 /* Its a pointer arithmetic expression like ptr += i */
6049 ret = exprNode_createPartialCopy (e1);
6050 ret->kind = XPR_ASSIGN;
6051 ret->edata = exprData_makeOp (e1, e2, op);
6053 if (!exprNode_isError (e2))
6055 ret->sets = sRefSet_union (ret->sets, e2->sets);
6056 ret->msets = sRefSet_union (ret->msets, e2->msets);
6057 ret->uses = sRefSet_union (ret->uses, e2->uses);
6061 checkExpressionDefined (e1, e2, op);
6063 if (exprNode_isError (e1))
6065 if (!exprNode_isError (e2))
6067 ret->loc = fileloc_update (ret->loc, e2->loc);
6071 ret->loc = fileloc_update (ret->loc, g_currentloc);
6075 if (!exprNode_isError (e2))
6077 checkMacroParen (e2);
6080 if (exprNode_isDefined (e1))
6082 if (sRef_isMacroParamRef (e1->sref))
6084 if (context_inIterDef ())
6086 uentry ue = sRef_getUentry (e1->sref);
6088 if (uentry_isYield (ue))
6094 if (fileloc_isDefined (e1->loc))
6098 message ("Assignment to non-yield iter parameter: %q",
6099 sRef_unparse (e1->sref)),
6106 message ("Assignment to non-yield iter parameter: %q",
6107 sRef_unparse (e1->sref)),
6114 if (fileloc_isDefined (e1->loc))
6118 message ("Assignment to macro parameter: %q",
6119 sRef_unparse (e1->sref)),
6126 message ("Assignment to macro parameter: %q",
6127 sRef_unparse (e1->sref)),
6131 exprNode_checkAssignMod (e1, ret); /* evans 2001-07-22 */
6136 exprNode_checkAssignMod (e1, ret);
6139 if (exprNode_isDefined (e2))
6141 if (lltok_getTok (op) == TASSIGN)
6143 ctype te1 = exprNode_getType (e1);
6144 ctype te2 = exprNode_getType (e2);
6146 if (ctype_isVoid (te2))
6150 message ("Assignment of void value to %t: %s %s %s",
6151 te1, exprNode_unparse (e1),
6153 exprNode_unparse (e2)),
6156 else if (!ctype_forceMatch (te1, te2))
6158 if (exprNode_matchLiteral (te1, e2))
6166 message ("Assignment of %t to %t: %s %s %s",
6167 te2, te1, exprNode_unparse (e1),
6169 exprNode_unparse (e2)),
6175 /* Type checks okay */
6179 exprNode_mergeUSs (ret, e2);
6180 exprNode_checkUse (ret, e2->sref, e2->loc);
6182 DPRINTF (("Do assign! %s %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6189 doAssign (e1, e2, FALSE);
6192 ret->sref = e1->sref;
6196 if (exprNode_isDefined (e2))
6198 exprNode_mergeUSs (ret, e2);
6199 exprNode_checkUse (ret, e2->sref, e2->loc);
6203 if (sRef_isPointer (e1->sref) && !sRef_isMacroParamRef (e1->sref))
6205 exprNode_checkUse (ret, sRef_getBase (e1->sref), e1->loc);
6208 isjustalloc = sRef_isJustAllocated (e1->sref);
6209 isalloc = sRef_isAllocated (e1->sref);
6211 if (sRef_isField (e1->sref))
6213 sRef root = sRef_getRootBase (sRef_getBase (e1->sref));
6215 if (!sRef_isAllocated (root) && !sRef_isMacroParamRef (root))
6217 exprNode_checkUse (ret, root, e1->loc);
6223 ** be careful! this defines e1->sref.
6226 /* evans 2001-07-22: removed if (!sRef_isMacroParamRef (e1->sref)) */
6228 DPRINTF (("Setting: %s -> %s", exprNode_unparse (ret), sRef_unparse (e1->sref)));
6229 exprNode_checkSet (ret, e1->sref);
6233 sRef_setAllocatedComplete (e1->sref, exprNode_isDefined (e2)
6234 ? e2->loc : e1->loc);
6240 sRef_setAllocatedShallowComplete (e1->sref, exprNode_loc (e2));
6249 exprNode_cond (/*@keep@*/ exprNode pred, /*@keep@*/ exprNode ifclause,
6250 /*@keep@*/ exprNode elseclause)
6254 if (!exprNode_isError (pred))
6256 ret = exprNode_createPartialCopy (pred);
6257 checkMacroParen (pred);
6258 exprNode_checkPred (cstring_makeLiteralTemp ("conditional"), pred);
6260 if (!exprNode_isError (ifclause))
6262 checkMacroParen (ifclause); /* update macro counts! */
6264 if (!exprNode_isError (elseclause))
6266 checkMacroParen (elseclause);
6268 if (!exprNode_matchTypes (ifclause, elseclause))
6271 (exprNode_getType (ifclause),
6273 exprNode_getType (elseclause),
6275 message ("Conditional clauses are not of same type: "
6277 exprNode_unparse (ifclause),
6278 exprNode_getType (ifclause),
6279 exprNode_unparse (elseclause),
6280 exprNode_getType (elseclause)),
6283 ret->sref = sRef_undefined;
6284 ret->typ = ctype_unknown;
6289 /* for now...should merge the states */
6290 ret->sref = ifclause->sref;
6291 ret->typ = ifclause->typ;
6293 if (exprNode_isNullValue (ifclause))
6295 ret->typ = elseclause->typ;
6299 exprNode_checkUse (ret, pred->sref, pred->loc);
6300 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6301 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6303 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6308 ret->typ = ifclause->typ;
6310 exprNode_checkUse (pred, pred->sref, pred->loc);
6311 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6313 exprNode_mergeCondUSs (ret, ifclause, exprNode_undefined);
6318 if (!exprNode_isError (elseclause))
6320 ret->typ = elseclause->typ;
6322 exprNode_checkUse (pred, pred->sref, pred->loc);
6323 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6325 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6329 else /* pred is error */
6331 if (!exprNode_isError (ifclause))
6333 ret = exprNode_createSemiCopy (ifclause);
6335 checkMacroParen (ifclause); /* update macro counts! */
6337 if (!exprNode_isError (elseclause))
6339 checkMacroParen (elseclause);
6341 ret->typ = ifclause->typ;
6343 if (!ctype_forceMatch (ifclause->typ, elseclause->typ))
6346 (exprNode_getType (ifclause),
6348 exprNode_getType (elseclause),
6350 message ("Conditional clauses are not of same type: "
6352 exprNode_unparse (ifclause),
6353 exprNode_getType (ifclause),
6354 exprNode_unparse (elseclause),
6355 exprNode_getType (elseclause)),
6358 ret->typ = ctype_unknown;
6362 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6363 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6365 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6368 else if (!exprNode_isError (elseclause)) /* pred, if errors */
6370 ret = exprNode_createSemiCopy (ifclause);
6372 ret->typ = elseclause->typ;
6373 checkMacroParen (elseclause);
6375 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6376 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6378 else /* all errors! */
6380 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6384 ret->kind = XPR_COND;
6385 ret->edata = exprData_makeCond (pred, ifclause, elseclause);
6387 if (exprNode_isDefined (ifclause) && exprNode_isDefined (elseclause))
6389 exprNode_combineControl (ret, ifclause, elseclause);
6396 exprNode_vaArg (/*@only@*/ lltok tok, /*@only@*/ exprNode arg, /*@only@*/ qtype qt)
6398 ctype totype = qtype_getType (qt);
6400 exprNode_createPartialLocCopy (arg, fileloc_copy (lltok_getLoc (tok)));
6404 ** check use of va_arg : <valist>, type -> type
6407 if (exprNode_isError (arg))
6412 targ = exprNode_getType (arg);
6415 ** arg should have be a pointer
6418 if (!ctype_isUA (targ) ||
6419 (!usymId_equal (ctype_typeId (targ),
6420 usymtab_getTypeId (cstring_makeLiteralTemp ("va_list")))))
6424 message ("First argument to va_arg is not a va_list (type %t): %s",
6425 targ, exprNode_unparse (arg)),
6429 exprNode_checkSet (ret, arg->sref);
6433 ** return type is totype
6437 ret->kind = XPR_VAARG;
6438 ret->edata = exprData_makeCast (tok, arg, qt);
6443 exprNode exprNode_labelMarker (/*@only@*/ cstring label)
6445 exprNode ret = exprNode_createPlain (ctype_undefined);
6446 ret->kind = XPR_LABEL;
6447 ret->edata = exprData_makeLiteral (label);
6448 ret->isJumpPoint = TRUE;
6450 return (ret); /* for now, ignore label */
6453 exprNode exprNode_notReached (/*@returned@*/ exprNode stmt)
6455 if (exprNode_isDefined (stmt))
6457 stmt->isJumpPoint = TRUE;
6459 /* This prevent stray no return path errors, etc. */
6460 stmt->exitCode = XK_MUSTEXIT;
6466 bool exprNode_isDefaultMarker (exprNode e)
6468 if (exprNode_isDefined (e))
6470 return (e->kind == XPR_DEFAULT || e->kind == XPR_FTDEFAULT);
6476 bool exprNode_isCaseMarker (exprNode e)
6478 if (exprNode_isDefined (e))
6480 return (e->kind == XPR_FTCASE || e->kind == XPR_CASE);
6486 bool exprNode_isLabelMarker (exprNode e)
6488 if (exprNode_isDefined (e))
6490 return (e->kind == XPR_LABEL);
6496 exprNode exprNode_caseMarker (/*@only@*/ exprNode test, bool fallThrough)
6498 exprNode ret = exprNode_createPartialCopy (test);
6500 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6502 if (exprNode_isError (test)) {
6506 exprNode_checkUse (ret, test->sref, test->loc);
6508 usymtab_setExitCode (ret->exitCode);
6512 usymtab_setMustBreak ();
6515 ret->edata = exprData_makeSingle (test);
6516 ret->isJumpPoint = TRUE;
6522 exprNode exprNode_caseStatement (/*@only@*/ exprNode test, /*@only@*/ exprNode stmt, bool fallThrough)
6524 exprNode ret = exprNode_createPartialCopy (test);
6526 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6527 ret->edata = exprData_makePair (test, stmt);
6528 ret->isJumpPoint = TRUE;
6530 if (exprNode_isError (test))
6535 exprNode_checkUse (ret, test->sref, test->loc);
6537 if (exprNode_isError (stmt))
6542 exprNode_mergeUSs (ret, stmt);
6544 ret->exitCode = stmt->exitCode;
6545 ret->mustBreak = stmt->mustBreak;
6546 ret->canBreak = stmt->canBreak;
6548 usymtab_setExitCode (ret->exitCode);
6552 usymtab_setMustBreak ();
6559 /*@notnull@*/ /*@only@*/ exprNode
6560 exprNode_defaultMarker (/*@only@*/ lltok def, bool fallThrough)
6562 exprNode ret = exprNode_createTok (def);
6564 ret->isJumpPoint = TRUE;
6565 ret->kind = fallThrough ? XPR_FTDEFAULT : XPR_DEFAULT;
6570 exprNode_mayEscape (exprNode e)
6572 if (exprNode_isDefined (e))
6574 return exitkind_couldEscape (e->exitCode);
6580 exprNode_mustBreak (exprNode e)
6582 if (exprNode_isDefined (e))
6584 return e->mustBreak;
6591 exprNode_mustEscape (exprNode e)
6593 if (exprNode_isDefined (e))
6595 return exitkind_mustEscape (e->exitCode) || exprNode_mustBreak (e);
6602 exprNode_errorEscape (exprNode e)
6604 if (exprNode_isDefined (e))
6606 return exitkind_isError (e->exitCode);
6612 exprNode exprNode_concat (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
6614 exprNode ret = exprNode_createPartialCopy (e1);
6616 DPRINTF (("Concat: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6618 ret->edata = exprData_makePair (e1, e2);
6619 ret->kind = XPR_STMTLIST;
6621 if (exprNode_isDefined (e1))
6623 ret->isJumpPoint = e1->isJumpPoint;
6624 ret->canBreak = e1->canBreak;
6628 if (exprNode_isDefined (e2))
6630 ret->loc = fileloc_update (ret->loc, e2->loc);
6634 if (exprNode_isDefined (e2))
6636 ret->exitCode = e2->exitCode;
6637 ret->mustBreak = e2->mustBreak;
6638 if (e2->canBreak) ret->canBreak = TRUE;
6642 ** if e1 must return, then e2 is unreachable!
6645 if (exprNode_isDefined (e1) && exprNode_isDefined (e2))
6647 if ((exprNode_mustEscape (e1) || exprNode_mustBreak (e1))
6648 && !(e2->isJumpPoint))
6650 if (context_getFlag (FLG_UNREACHABLE))
6654 if (e2->kind == XPR_STMT)
6656 nr = exprData_getUopNode (e2->edata);
6659 if ((nr->kind == XPR_TOK
6660 && lltok_isSemi (exprData_getTok (nr->edata))))
6662 /* okay to have unreachable ";" */
6663 ret->exitCode = XK_MUSTEXIT;
6664 ret->canBreak = TRUE;
6668 if (optgenerror (FLG_UNREACHABLE,
6669 message ("Unreachable code: %s",
6670 exprNode_unparseFirst (nr)),
6673 ret->isJumpPoint = TRUE;
6674 ret->mustBreak = FALSE;
6675 ret->exitCode = XK_ERROR;
6676 DPRINTF (("Jump point: %s", exprNode_unparse (ret)));
6680 ret->exitCode = XK_MUSTEXIT;
6681 ret->canBreak = TRUE;
6689 if ((e2->kind == XPR_CASE || e2->kind == XPR_DEFAULT))
6692 ** We want a warning anytime we have:
6694 ** yyy; <<<- no break or return
6698 exprNode lastStmt = exprNode_lastStatement (e1);
6700 if (exprNode_isDefined (lastStmt)
6701 && !exprNode_mustEscape (lastStmt)
6702 && !exprNode_mustBreak (lastStmt)
6703 && !exprNode_isCaseMarker (lastStmt)
6704 && !exprNode_isDefaultMarker (lastStmt)
6705 && !exprNode_isLabelMarker (lastStmt))
6707 voptgenerror (FLG_CASEBREAK,
6709 ("Fall through case (no preceding break)"),
6716 exprNode_mergeUSs (ret, e2);
6718 usymtab_setExitCode (ret->exitCode);
6722 usymtab_setMustBreak ();
6725 DPRINTF (("==> %s", exprNode_unparse (ret)));
6729 exprNode exprNode_createTok (/*@only@*/ lltok t)
6731 exprNode ret; /*@i23 if on same line, bad things happen...!@*/
6732 ret = exprNode_create (ctype_unknown);
6733 ret->kind = XPR_TOK;
6734 ret->edata = exprData_makeTok (t);
6738 exprNode exprNode_statement (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6740 if (!exprNode_isError (e))
6742 exprChecks_checkStatementEffect(e);
6745 return (exprNode_statementError (e, t));
6748 static exprNode exprNode_statementError (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6750 exprNode ret = exprNode_createPartialCopy (e);
6752 if (!exprNode_isError (e))
6754 if (e->kind != XPR_ASSIGN)
6756 exprNode_checkUse (ret, e->sref, e->loc);
6759 ret->exitCode = e->exitCode;
6760 ret->canBreak = e->canBreak;
6761 ret->mustBreak = e->mustBreak;
6764 ret->edata = exprData_makeUop (e, t);
6765 ret->kind = XPR_STMT;
6770 exprNode exprNode_checkExpr (/*@returned@*/ exprNode e)
6772 if (!exprNode_isError (e))
6774 if (e->kind != XPR_ASSIGN)
6776 exprNode_checkUse (e, e->sref, e->loc);
6783 void exprNode_produceGuards (exprNode pred)
6785 if (!exprNode_isError (pred))
6787 if (ctype_isRealPointer (pred->typ))
6789 pred->guards = guardSet_addTrueGuard (pred->guards, pred->sref);
6792 exprNode_checkUse (pred, pred->sref, pred->loc);
6793 exprNode_resetSref (pred);
6797 exprNode exprNode_compoundStatementExpression (/*@only@*/ lltok tlparen, /*@only@*/ exprNode e)
6801 DPRINTF (("Compound: %s", exprNode_unparse (e)));
6803 if (!context_flagOn (FLG_GNUEXTENSIONS, exprNode_loc (e)))
6805 (void) llgenhinterror
6807 message ("Compound statement expressions is not supported by ISO C99"),
6808 message ("Use +gnuextensions to allow compound statement expressions (and other GNU language extensions) "
6809 "without this warning"),
6814 ** The type of a compoundStatementExpression is the type of the last statement
6817 llassert (exprNode_isBlock (e));
6818 laststmt = exprNode_lastStatement (e);
6820 DPRINTF (("Last statement: %s / %s", exprNode_unparse (laststmt), ctype_unparse (exprNode_getType (laststmt))));
6821 DPRINTF (("e: %s", exprNode_unparse (e)));
6822 e->typ = exprNode_getType (laststmt);
6823 return exprNode_addParens (tlparen, e);
6827 exprNode exprNode_makeBlock (/*@only@*/ exprNode e)
6829 exprNode ret = exprNode_createPartialCopy (e);
6831 if (!exprNode_isError (e))
6833 ret->exitCode = e->exitCode;
6834 ret->canBreak = e->canBreak;
6835 ret->mustBreak = e->mustBreak;
6838 DPRINTF (("Block e: %s", exprNode_unparse (e)));
6839 ret->edata = exprData_makeSingle (e);
6840 ret->kind = XPR_BLOCK;
6841 DPRINTF (("Block: %s", exprNode_unparse (ret)));
6845 bool exprNode_isBlock (exprNode e)
6847 return (exprNode_isDefined (e)
6848 && ((e)->kind == XPR_BLOCK));
6851 bool exprNode_isStatement (exprNode e)
6853 return (exprNode_isDefined (e)
6854 && ((e)->kind == XPR_STMT));
6857 bool exprNode_isAssign (exprNode e)
6859 if (exprNode_isDefined (e))
6861 return (e->kind == XPR_ASSIGN);
6867 bool exprNode_isEmptyStatement (exprNode e)
6869 return (exprNode_isDefined (e)
6870 && (e->kind == XPR_TOK)
6871 && (lltok_isSemi (exprData_getTok (e->edata))));
6874 bool exprNode_isMultiStatement (exprNode e)
6876 return (exprNode_isDefined (e)
6877 && ((e->kind == XPR_FOR)
6878 || (e->kind == XPR_FORPRED)
6879 || (e->kind == XPR_IF)
6880 || (e->kind == XPR_IFELSE)
6881 || (e->kind == XPR_WHILE)
6882 || (e->kind == XPR_WHILEPRED)
6883 || (e->kind == XPR_DOWHILE)
6884 || (e->kind == XPR_BLOCK)
6885 || (e->kind == XPR_STMT)
6886 || (e->kind == XPR_STMTLIST)
6887 || (e->kind == XPR_SWITCH)));
6890 void exprNode_checkIfPred (exprNode pred)
6892 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6895 exprNode exprNode_if (/*@only@*/ exprNode pred, /*@only@*/ exprNode tclause)
6898 bool emptyErr = FALSE;
6900 if (context_maybeSet (FLG_IFEMPTY))
6902 if (exprNode_isEmptyStatement (tclause))
6904 emptyErr = optgenerror (FLG_IFEMPTY,
6906 ("Body of if statement is empty"),
6907 exprNode_loc (tclause));
6911 if (!emptyErr && context_maybeSet (FLG_IFBLOCK))
6913 if (exprNode_isDefined (tclause)
6914 && !exprNode_isBlock (tclause))
6916 voptgenerror (FLG_IFBLOCK,
6918 ("Body of if statement is not a block: %s",
6919 exprNode_unparse (tclause)),
6920 exprNode_loc (tclause));
6924 if (exprNode_isError (pred))
6926 if (exprNode_isError (tclause))
6928 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6932 ret = exprNode_createPartialCopy (tclause);
6937 if (exprNode_mustEscape (pred))
6941 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6942 exprNode_loc (pred));
6945 /*! exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred); */ /*@i523@*/
6946 exprNode_checkUse (pred, pred->sref, pred->loc);
6948 if (!exprNode_isError (tclause))
6950 exprNode_mergeCondUSs (pred, tclause, exprNode_undefined);
6953 ret = exprNode_createPartialCopy (pred);
6957 ret->edata = exprData_makePair (pred, tclause);
6959 ret->exitCode = XK_UNKNOWN;
6961 if (exprNode_isDefined (tclause))
6963 ret->exitCode = exitkind_makeConditional (tclause->exitCode);
6964 ret->canBreak = tclause->canBreak;
6965 ret->sets = sRefSet_union (ret->sets, tclause->sets);
6966 ret->msets = sRefSet_union (ret->msets, tclause->msets);
6967 ret->uses = sRefSet_union (ret->uses, tclause->uses);
6970 ret->mustBreak = FALSE;
6975 exprNode exprNode_ifelse (/*@only@*/ exprNode pred,
6976 /*@only@*/ exprNode tclause,
6977 /*@only@*/ exprNode eclause)
6980 bool tEmptyErr = FALSE;
6981 bool eEmptyErr = FALSE;
6983 if (context_maybeSet (FLG_IFEMPTY))
6985 if (exprNode_isEmptyStatement (tclause))
6987 tEmptyErr = optgenerror
6990 ("Body of if clause of if statement is empty"),
6991 exprNode_loc (tclause));
6994 if (exprNode_isEmptyStatement (eclause))
6996 eEmptyErr = optgenerror
6999 ("Body of else clause of if statement is empty"),
7000 exprNode_loc (eclause));
7004 if (context_maybeSet (FLG_IFBLOCK))
7007 && exprNode_isDefined (tclause)
7008 && !exprNode_isBlock (tclause))
7010 voptgenerror (FLG_IFBLOCK,
7012 ("Body of if clause of if statement is not a block: %s",
7013 exprNode_unparse (tclause)),
7014 exprNode_loc (tclause));
7018 && exprNode_isDefined (eclause)
7019 && !exprNode_isBlock (eclause)
7020 && !(eclause->kind == XPR_IF)
7021 && !(eclause->kind == XPR_IFELSE))
7026 ("Body of else clause of if statement is not a block: %s",
7027 exprNode_unparse (eclause)),
7028 exprNode_loc (eclause));
7032 if (context_maybeSet (FLG_ELSEIFCOMPLETE))
7034 if (exprNode_isDefined (eclause)
7035 && (eclause->kind == XPR_IF))
7037 voptgenerror (FLG_ELSEIFCOMPLETE,
7038 message ("Incomplete else if logic (no final else): %s",
7039 exprNode_unparse (eclause)),
7040 exprNode_loc (eclause));
7044 if (exprNode_isError (pred))
7046 if (exprNode_isError (tclause))
7048 if (exprNode_isError (eclause))
7050 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7054 ret = exprNode_createPartialCopy (eclause);
7059 ret = exprNode_createPartialCopy (tclause);
7062 else /* pred is okay */
7064 ret = exprNode_createPartialCopy (pred);
7066 if (exprNode_mustEscape (pred))
7070 message ("Predicate always exits: %s", exprNode_unparse (pred)),
7071 exprNode_loc (pred));
7074 /*@i3423 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);*/
7075 exprNode_checkUse (ret, pred->sref, pred->loc);
7077 exprNode_mergeCondUSs (ret, tclause, eclause);
7080 ret->kind = XPR_IFELSE;
7081 ret->edata = exprData_makeCond (pred, tclause, eclause);
7083 if (exprNode_isDefined (tclause) && exprNode_isDefined (eclause))
7085 exprNode_combineControl (ret, tclause, eclause);
7086 ret->loc = fileloc_update (ret->loc, eclause->loc);
7093 ** *allpaths <- TRUE iff all executions paths must go through the switch
7097 checkSwitchExpr (exprNode test, /*@dependent@*/ exprNode e, /*@out@*/ bool *allpaths)
7099 exprNodeSList el = exprNode_flatten (e);
7100 bool mustReturn = TRUE; /* find a branch that doesn't */
7101 bool thisReturn = FALSE;
7102 bool hasDefault = FALSE;
7103 bool hasAllMembers = FALSE;
7104 bool inSwitch = FALSE;
7105 bool isEnumSwitch = FALSE;
7106 bool canBreak = FALSE;
7107 bool fallThrough = FALSE;
7108 ctype ct = ctype_unknown;
7109 enumNameSList usedEnums;
7112 if (exprNode_isDefined (test))
7117 ttype = ctype_realType (ct);
7119 if (ctype_isEnum (ttype))
7121 isEnumSwitch = TRUE;
7122 enums = ctype_elist (ttype);
7123 usedEnums = enumNameSList_new ();
7127 exprNodeSList_elements (el, current)
7130 DPRINTF ((message("checkSwitchExpr current = %s ", exprNode_unparse(current) ) ));
7132 if (exprNode_isDefined (current))
7134 switch (current->kind)
7141 (FLG_DUPLICATECASES,
7142 message ("Duplicate default cases in switch"),
7143 exprNode_loc (current));
7148 if (current->kind == XPR_DEFAULT || current->kind == XPR_FTDEFAULT)
7156 exprNode st = exprData_getSingle (current->edata);
7157 uentry ue = exprNode_getUentry (st);
7159 if (uentry_isValid (ue))
7161 cstring cname = uentry_rawName (ue);
7163 if (enumNameList_member (/*@-usedef@*/enums/*@=usedef@*/, cname))
7165 if (enumNameSList_member
7166 (/*@-usedef@*/usedEnums/*@=usedef@*/, cname))
7169 (FLG_DUPLICATECASES,
7170 message ("Duplicate case in switch: %s",
7176 enumNameSList_addh (usedEnums, cname);
7183 message ("Case in switch not %s member: %s",
7184 ctype_unparse (ct), cname),
7191 if (inSwitch && !fallThrough)
7193 if (!thisReturn || canBreak)
7203 /*@switchbreak@*/ break;
7205 thisReturn = thisReturn || exprNode_mustEscape (current);
7206 canBreak = canBreak || current->canBreak;
7207 if (canBreak) fallThrough = FALSE;
7210 } end_exprNodeSList_elements;
7212 if (inSwitch) /* check the last one! */
7214 if (!thisReturn || canBreak)
7223 && (enumNameSList_size (/*@-usedef@*/usedEnums/*@=usedef@*/) !=
7224 enumNameList_size (/*@-usedef@*/enums/*@=usedef@*/)))
7226 enumNameSList unused = enumNameSList_subtract (enums, usedEnums);
7228 voptgenerror (FLG_MISSCASE,
7229 message ("Missing case%s in switch: %q",
7230 cstring_makeLiteralTemp
7231 ((enumNameSList_size (unused) > 1) ? "s" : ""),
7232 enumNameSList_unparse (unused)),
7235 enumNameSList_free (unused);
7236 *allpaths = FALSE; /* evans 2002-01-01 */
7240 hasAllMembers = TRUE;
7244 enumNameSList_free (usedEnums);
7248 *allpaths = hasDefault;
7251 exprNodeSList_free (el);
7252 return ((hasDefault || hasAllMembers) && mustReturn);
7255 exprNode exprNode_switch (/*@only@*/ exprNode e, /*@only@*/ exprNode s)
7257 exprNode ret = exprNode_createPartialCopy (e);
7260 DPRINTF (("Switch: %s", exprNode_unparse (s)));
7262 ret->kind = XPR_SWITCH;
7263 ret->edata = exprData_makePair (e, s);
7265 if (!exprNode_isError (s))
7267 exprNode fs = exprNode_firstStatement (s);
7268 ret->loc = fileloc_update (ret->loc, s->loc);
7270 if (exprNode_isUndefined (fs)
7271 || exprNode_isCaseMarker (fs) || exprNode_isLabelMarker (fs)
7272 || exprNode_isDefaultMarker (fs)) {
7275 voptgenerror (FLG_FIRSTCASE,
7277 ("Statement after switch is not a case: %s", exprNode_unparse (fs)),
7282 if (!exprNode_isError (e))
7284 if (checkSwitchExpr (e, s, &allpaths))
7286 ret->exitCode = XK_MUSTRETURN;
7290 ret->exitCode = e->exitCode;
7293 ret->canBreak = e->canBreak;
7294 ret->mustBreak = e->mustBreak;
7298 ** exprNode.c:3883,32: Variable allpaths used before definition
7305 DPRINTF (("Context exit switch!"));
7306 context_exitSwitch (ret, allpaths);
7307 DPRINTF (("Context exit switch done!"));
7312 static void checkInfiniteLoop (/*@notnull@*/ exprNode test,
7313 /*@notnull@*/ exprNode body)
7315 sRefSet tuses = test->uses;
7317 if (!sRefSet_isEmpty (test->uses))
7319 sRefSet sets = sRefSet_newCopy (body->sets);
7320 bool hasError = TRUE;
7321 bool innerState = FALSE;
7322 sRefSet tuncon = sRefSet_undefined;
7324 sets = sRefSet_union (sets, test->sets);
7325 sets = sRefSet_union (sets, body->msets);
7326 sets = sRefSet_union (sets, test->msets);
7328 sRefSet_allElements (tuses, el)
7330 if (sRef_isUnconstrained (el))
7332 tuncon = sRefSet_insert (tuncon, el);
7336 if (sRefSet_member (sets, el))
7343 if (sRef_isInternalState (el)
7344 || sRef_isFileStatic (sRef_getRootBase (el)))
7348 } end_sRefSet_allElements ;
7352 sRefSet suncon = sRefSet_undefined;
7353 bool sinner = FALSE;
7355 sRefSet_allElements (sets, el)
7357 if (sRef_isUnconstrained (el))
7359 suncon = sRefSet_insert (suncon, el);
7361 else if (sRef_isInternalState (el))
7369 } end_sRefSet_allElements ;
7371 if (sinner && innerState)
7375 else if (sRefSet_isEmpty (tuncon)
7376 && sRefSet_isEmpty (suncon))
7381 ("Suspected infinite loop. No value used in loop test (%q) "
7382 "is modified by test or loop body.",
7383 sRefSet_unparsePlain (tuses)),
7388 if (sRefSet_isEmpty (tuncon))
7392 message ("Suspected infinite loop. No condition values "
7393 "modified. Modification possible through "
7394 "unconstrained calls: %q",
7395 sRefSet_unparsePlain (suncon)),
7402 message ("Suspected infinite loop. No condition values "
7403 "modified. Possible undetected dependency through "
7404 "unconstrained calls in loop test: %q",
7405 sRefSet_unparsePlain (tuncon)),
7411 sRefSet_free (sets);
7415 exprNode exprNode_while (/*@keep@*/ exprNode t, /*@keep@*/ exprNode b)
7418 bool emptyErr = FALSE;
7420 if (context_maybeSet (FLG_WHILEEMPTY))
7422 if (exprNode_isEmptyStatement (b))
7424 emptyErr = optgenerror
7427 ("Body of while statement is empty"),
7432 if (!emptyErr && context_maybeSet (FLG_WHILEBLOCK))
7434 if (exprNode_isDefined (b)
7435 && !exprNode_isBlock (b))
7437 if (context_inIterDef ()
7438 && (b->kind == XPR_STMTLIST
7439 || b->kind == XPR_TOK))
7445 voptgenerror (FLG_WHILEBLOCK,
7447 ("Body of while statement is not a block: %s",
7448 exprNode_unparse (b)),
7454 if (exprNode_isError (t))
7456 if (exprNode_isError (b))
7458 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7462 ret = exprNode_createPartialCopy (b);
7469 ret = exprNode_createPartialCopy (t);
7471 llassert (t->kind == XPR_WHILEPRED);
7473 test = exprData_getSingle (t->edata);
7475 if (!exprNode_isError (b) && exprNode_isDefined (test))
7477 if (context_maybeSet (FLG_INFLOOPS)
7478 || context_maybeSet (FLG_INFLOOPSUNCON))
7481 ** check that some variable in the predicate is set by the body
7482 ** if the predicate uses any variables
7485 checkInfiniteLoop (test, b);
7488 exprNode_mergeUSs (ret, b);
7490 if (exprNode_isDefined (b))
7492 ret->exitCode = exitkind_makeConditional (b->exitCode);
7497 ret->edata = exprData_makePair (t, b);
7498 ret->kind = XPR_WHILE;
7500 if (exprNode_isDefined (t) && exprNode_mustEscape (t))
7504 message ("Predicate always exits: %s", exprNode_unparse (t)),
7508 ret->exitCode = XK_NEVERESCAPE;
7511 ** If loop is infinite, and there is no break inside,
7512 ** exit code is never reach.
7515 if (exprNode_knownIntValue (t))
7517 if (!exprNode_isZero (t))
7519 if (exprNode_isDefined (b))
7523 /* Really, it means never reached. */
7524 ret->exitCode = XK_MUSTEXIT;
7534 ret->canBreak = FALSE;
7535 ret->mustBreak = FALSE;
7541 ** do { b } while (t);
7543 ** note: body passed as first argument
7546 exprNode exprNode_doWhile (/*@only@*/ exprNode b, /*@only@*/ exprNode t)
7550 DPRINTF (("Do while: %s / %s",
7551 exprNode_unparse (b), exprNode_unparse (t)));
7553 if (exprNode_isError (t))
7555 if (exprNode_isError (b))
7557 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7561 ret = exprNode_createPartialCopy (b);
7563 ret->exitCode = exitkind_makeConditional (b->exitCode);
7564 exprNode_checkUse (ret, b->sref, b->loc);
7565 ret->exitCode = b->exitCode;
7566 ret->canBreak = b->canBreak;
7567 ret->mustBreak = FALSE;
7572 DPRINTF (("Do while: %s / %s",
7573 exitkind_unparse (t->exitCode),
7574 exitkind_unparse (b->exitCode)));
7576 ret = exprNode_createPartialCopy (t);
7577 exprNode_checkPred (cstring_makeLiteralTemp ("while"), t);
7579 if (!exprNode_isError (b))
7582 ** forgot the copy's --- why wasn't this detected??
7585 ret->sets = sRefSet_copyInto (ret->sets, b->sets);
7586 ret->msets = sRefSet_copyInto (ret->msets, b->msets);
7587 ret->uses = sRefSet_copyInto (ret->uses, b->uses);
7589 /* left this out --- causes and aliasing bug (infinite loop)
7590 should be detected?? */
7592 exprNode_checkUse (ret, b->sref, b->loc);
7593 exprNode_mergeUSs (ret, t);
7594 exprNode_checkUse (ret, t->sref, t->loc);
7596 /* evans 2001-10-05: while loop can break */
7597 ret->exitCode = exitkind_makeConditional (b->exitCode);
7599 DPRINTF (("Do while: %s",
7600 exitkind_unparse (ret->exitCode)));
7602 ret->canBreak = b->canBreak;
7604 /* Always FALSE for doWhile loops - break's when test is false */
7605 ret->mustBreak = FALSE; /* b->mustBreak; */
7609 context_exitDoWhileClause (t);
7611 ret->kind = XPR_DOWHILE;
7612 ret->edata = exprData_makePair (t, b);
7616 bool exprNode_loopMustExec (exprNode forPred)
7619 ** Returns true if it is obvious that the loop always executes at least once
7621 ** For now, we only identify the most obvious cases. Should be true anytime
7622 ** we can prove init => !test.
7625 if (exprNode_isDefined (forPred))
7627 exprNode init, test, inc;
7630 llassert (forPred->kind == XPR_FORPRED);
7632 edata = forPred->edata;
7633 init = exprData_getTripleInit (edata);
7634 test = exprData_getTripleTest (edata);
7635 inc = exprData_getTripleInc (edata);
7637 if (exprNode_isAssign (init))
7639 exprNode loopVar = exprData_getOpA (init->edata);
7640 exprNode loopInit = exprData_getOpB (init->edata);
7642 if (exprNode_isDefined (test) && test->kind == XPR_OP)
7644 exprNode testVar = exprData_getOpA (test->edata);
7645 exprNode testVal = exprData_getOpB (test->edata);
7646 lltok comp = exprData_getOpTok (test->edata);
7647 int opid = lltok_getTok (comp);
7649 DPRINTF (("Same storage: %s / %s", exprNode_unparse (loopVar),
7650 exprNode_unparse (testVar)));
7652 if (exprNode_sameStorage (loopVar, testVar))
7654 multiVal valinit = exprNode_getValue (loopInit);
7655 multiVal valtest = exprNode_getValue (testVal);
7657 DPRINTF (("Values: %s / %s", multiVal_unparse (valinit),
7658 multiVal_unparse (valtest)));
7660 if (multiVal_isInt (valinit) && multiVal_isInt (valtest))
7662 long v1 = multiVal_forceInt (valinit);
7663 long v2 = multiVal_forceInt (valtest);
7665 DPRINTF (("Here: %ld %ld", v1, v2));
7667 if ((opid == EQ_OP && v1 < v2)
7668 || (opid == NE_OP && v1 != v2)
7669 || (opid == TLT && v1 <= v2)
7670 || (opid == TGT && v1 >= v2)
7671 || (opid == LE_OP && v1 < v2)
7672 || (opid == GE_OP && v1 > v2))
7674 DPRINTF (("mustexec if inc"));
7683 DPRINTF (("loop must exec: FALSE"));
7687 exprNode exprNode_for (/*@keep@*/ exprNode inc, /*@keep@*/ exprNode body)
7690 bool emptyErr = FALSE;
7692 if (context_maybeSet (FLG_FOREMPTY))
7694 if (exprNode_isEmptyStatement (body))
7696 emptyErr = optgenerror
7699 ("Body of for statement is empty"),
7700 exprNode_loc (body));
7704 if (!emptyErr && context_maybeSet (FLG_FORBLOCK))
7706 if (exprNode_isDefined (body)
7707 && !exprNode_isBlock (body))
7709 if (context_inIterDef ()
7710 && (body->kind == XPR_STMTLIST
7711 || body->kind == XPR_TOK))
7717 voptgenerror (FLG_FORBLOCK,
7719 ("Body of for statement is not a block: %s",
7720 exprNode_unparse (body)),
7721 exprNode_loc (body));
7727 ** for ud purposes: (alreadly) init -> test -> (now) LOOP: body + inc + test
7730 if (exprNode_isError (body))
7732 ret = exprNode_createPartialCopy (inc);
7736 ret = exprNode_createPartialCopy (body);
7738 ret->exitCode = exitkind_makeConditional (body->exitCode);
7740 exprNode_mergeUSs (inc, body);
7742 if (exprNode_isDefined (inc))
7746 context_setMessageAnnote (cstring_makeLiteral ("in post loop increment"));
7748 tmp = exprNode_effect (exprData_getTripleInc (inc->edata));
7749 exprNode_freeShallow (tmp);
7751 context_clearMessageAnnote ();
7752 context_setMessageAnnote (cstring_makeLiteral ("in post loop test"));
7754 tmp = exprNode_effect (exprData_getTripleTest (inc->edata));
7755 exprNode_freeShallow (tmp);
7757 context_clearMessageAnnote ();
7759 ret->uses = sRefSet_copyInto (ret->uses, inc->uses);
7760 ret->sets = sRefSet_copyInto (ret->sets, inc->sets);
7761 ret->msets = sRefSet_copyInto (ret->msets, inc->msets);
7765 ret->kind = XPR_FOR;
7766 ret->edata = exprData_makePair (inc, body);
7768 if (exprNode_isDefined (inc)) {
7769 exprNode test = exprData_getTripleTest (inc->edata);
7771 if (exprNode_isUndefined (test)) {
7772 if (exprNode_isDefined (body)) {
7773 if (!body->canBreak) {
7774 /* Really, it means never reached. */
7775 ret->exitCode = XK_MUSTEXIT;
7785 ** for (init; test; inc)
7788 ** while (test) { body; inc; }
7790 ** Now: check use of init (may set vars for test)
7791 ** check use of test
7795 /*@observer@*/ guardSet exprNode_getForGuards (exprNode pred)
7799 if (exprNode_isError (pred)) return guardSet_undefined;
7801 llassert (pred->kind == XPR_FORPRED);
7803 test = exprData_getTripleTest (pred->edata);
7805 if (!exprNode_isError (test))
7807 return (test->guards);
7810 return guardSet_undefined;
7813 exprNode exprNode_whilePred (/*@only@*/ exprNode test)
7815 exprNode ret = exprNode_createSemiCopy (test);
7817 if (exprNode_isDefined (test))
7819 exprNode_copySets (ret, test);
7820 exprNode_checkPred (cstring_makeLiteralTemp ("while"), test);
7821 exprNode_checkUse (ret, test->sref, test->loc);
7823 exprNode_produceGuards (test);
7825 ret->guards = guardSet_copy (test->guards);
7828 ret->edata = exprData_makeSingle (test);
7829 ret->kind = XPR_WHILEPRED;
7833 exprNode exprNode_forPred (/*@only@*/ exprNode init, /*@only@*/ exprNode test,
7834 /*@only@*/ exprNode inc)
7839 ** for ud purposes: init -> test -> LOOP: [ body, inc ]
7842 exprNode_checkPred (cstring_makeLiteralTemp ("for"), test);
7844 if (!exprNode_isError (inc))
7846 ret = exprNode_createPartialCopy (inc);
7850 if (!exprNode_isError (init))
7852 ret = exprNode_createPartialCopy (init);
7854 else if (!exprNode_isError (test))
7856 ret = exprNode_createPartialCopy (test);
7860 ret = exprNode_createUnknown ();
7864 exprNode_mergeUSs (ret, init);
7866 if (exprNode_isDefined (init))
7868 exprNode_checkUse (ret, init->sref, init->loc);
7871 exprNode_mergeUSs (ret, test);
7873 if (exprNode_isDefined (test))
7875 exprNode_checkUse (ret, test->sref, test->loc);
7878 ret->kind = XPR_FORPRED;
7879 ret->edata = exprData_makeFor (init, test, inc);
7883 /*@notnull@*/ /*@only@*/ exprNode exprNode_goto (/*@only@*/ cstring label)
7885 exprNode ret = exprNode_createUnknown ();
7887 if (context_inMacro ())
7889 voptgenerror (FLG_MACROSTMT,
7890 message ("Macro %s uses goto (not functional)",
7891 context_inFunctionName ()),
7895 ret->kind = XPR_GOTO;
7896 ret->edata = exprData_makeLiteral (label);
7897 ret->mustBreak = TRUE;
7898 ret->exitCode = XK_GOTO;
7899 ret->canBreak = TRUE;
7903 exprNode exprNode_continue (/*@only@*/ lltok l, int qcontinue)
7905 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7907 ret->kind = XPR_CONTINUE;
7908 ret->edata = exprData_makeTok (l);
7909 ret->canBreak = TRUE;
7910 ret->mustBreak = TRUE;
7912 if (qcontinue == QSAFEBREAK)
7916 else if (qcontinue == QINNERCONTINUE)
7918 if (!context_inDeepLoop ())
7921 (FLG_LOOPLOOPCONTINUE,
7922 cstring_makeLiteral ("Continue statement marked with innercontinue "
7923 "is not inside a nested loop"),
7924 exprNode_loc (ret));
7927 else if (qcontinue == BADTOK)
7929 if (context_inDeepLoop ())
7932 (FLG_LOOPLOOPCONTINUE,
7933 cstring_makeLiteral ("Continue statement in nested loop"),
7934 exprNode_loc (ret));
7939 llbuglit ("exprNode_continue: bad qcontinue");
7945 exprNode exprNode_break (/*@only@*/ lltok l, int bqual)
7947 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7948 clause breakClause = context_breakClause ();
7950 ret->kind = XPR_BREAK;
7951 ret->edata = exprData_makeTok (l);
7952 ret->canBreak = TRUE;
7953 ret->mustBreak = TRUE;
7955 if (breakClause == NOCLAUSE)
7959 cstring_makeLiteral ("Break not inside while, for or switch statement"),
7960 exprNode_loc (ret));
7964 if (bqual != BADTOK)
7971 if (breakClause == SWITCHCLAUSE)
7973 if (!context_inDeepSwitch ())
7975 voptgenerror (FLG_SYNTAX,
7977 ("Break preceded by innerbreak is not in a deep switch"),
7978 exprNode_loc (ret));
7983 if (!context_inDeepLoop ())
7985 voptgenerror (FLG_SYNTAX,
7987 ("Break preceded by innerbreak is not in a deep loop"),
7988 exprNode_loc (ret));
7993 if (breakClause == SWITCHCLAUSE)
7995 voptgenerror (FLG_SYNTAX,
7997 ("Break preceded by loopbreak is breaking a switch"),
7998 exprNode_loc (ret));
8002 if (breakClause != SWITCHCLAUSE)
8006 message ("Break preceded by switchbreak is breaking %s",
8007 cstring_makeLiteralTemp
8008 ((breakClause == WHILECLAUSE
8009 || breakClause == DOWHILECLAUSE) ? "a while loop"
8010 : (breakClause == FORCLAUSE) ? "a for loop"
8011 : (breakClause == ITERCLAUSE) ? "an iterator"
8013 exprNode_loc (ret));
8021 if (breakClause == SWITCHCLAUSE)
8023 clause nextBreakClause = context_nextBreakClause ();
8025 switch (nextBreakClause)
8027 case NOCLAUSE: break;
8033 (FLG_LOOPSWITCHBREAK,
8034 cstring_makeLiteral ("Break statement in switch inside loop"),
8035 exprNode_loc (ret));
8039 (FLG_SWITCHSWITCHBREAK,
8040 cstring_makeLiteral ("Break statement in switch inside switch"),
8041 exprNode_loc (ret));
8048 if (context_inDeepLoop ())
8052 cstring_makeLiteral ("Break statement in nested loop"),
8053 exprNode_loc (ret));
8057 if (context_inDeepLoopSwitch ())
8060 (FLG_SWITCHLOOPBREAK,
8061 cstring_makeLiteral ("Break statement in loop inside switch"),
8062 exprNode_loc (ret));
8072 exprNode exprNode_nullReturn (/*@only@*/ lltok t)
8074 fileloc loc = lltok_getLoc (t);
8075 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
8077 context_returnFunction ();
8078 exprChecks_checkNullReturn (loc);
8080 ret->kind = XPR_NULLRETURN;
8081 ret->edata = exprData_makeTok (t);
8082 ret->exitCode = XK_MUSTRETURN;
8086 exprNode exprNode_return (/*@only@*/ exprNode e)
8090 if (exprNode_isError (e))
8092 ret = exprNode_createUnknown ();
8096 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (e->loc));
8098 exprNode_checkUse (ret, e->sref, e->loc);
8099 exprNode_checkReturn (e);
8102 context_returnFunction ();
8103 ret->kind = XPR_RETURN;
8104 ret->edata = exprData_makeSingle (e);
8105 ret->exitCode = XK_MUSTRETURN;
8110 exprNode exprNode_comma (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
8114 if (exprNode_isError (e1))
8116 if (exprNode_isError (e2))
8118 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
8122 ret = exprNode_createPartialCopy (e2);
8123 exprNode_checkUse (ret, e2->sref, e2->loc);
8124 ret->sref = e2->sref;
8129 ret = exprNode_createPartialCopy (e1);
8131 exprNode_checkUse (ret, e1->sref, e1->loc);
8133 if (!exprNode_isError (e2))
8135 exprNode_mergeUSs (ret, e2);
8136 exprNode_checkUse (ret, e2->sref, e2->loc);
8137 ret->sref = e2->sref;
8141 ret->kind = XPR_COMMA;
8142 ret->edata = exprData_makePair (e1, e2);
8144 if (exprNode_isDefined (e1))
8146 if (exprNode_isDefined (e2))
8150 if (exprNode_mustEscape (e1) || e1->mustBreak)
8154 message ("Second clause of comma expression is unreachable: %s",
8155 exprNode_unparse (e2)),
8159 ret->exitCode = exitkind_combine (e1->exitCode, e2->exitCode);
8160 ret->mustBreak = e1->mustBreak || e2->mustBreak;
8161 ret->canBreak = e1->canBreak || e2->canBreak;
8165 if (exprNode_mustEscape (e1) || e1->mustBreak)
8169 message ("Second clause of comma expression is unreachable: %s",
8170 exprNode_unparse (e2)),
8174 ret->exitCode = e1->exitCode;
8175 ret->canBreak = e1->canBreak;
8180 if (exprNode_isDefined (e2))
8182 ret->exitCode = e2->exitCode;
8183 ret->mustBreak = e2->mustBreak;
8184 ret->canBreak = e2->canBreak;
8191 static bool exprNode_checkOneInit (/*@notnull@*/ exprNode el, exprNode val)
8193 ctype t1 = exprNode_getType (el);
8194 ctype t2 = exprNode_getType (val);
8195 bool hasError = FALSE;
8197 DPRINTF (("Check one init: %s / %s",
8198 exprNode_unparse (el),
8199 exprNode_unparse (val)));
8201 if (ctype_isUnknown (t1))
8203 voptgenerror (FLG_IMPTYPE,
8204 message ("Variable has unknown (implicitly int) type: %s",
8205 exprNode_unparse (el)),
8209 el->typ = ctype_int;
8212 if (exprNode_isDefined (val) && val->kind == XPR_INITBLOCK)
8214 exprNodeList vals = exprData_getArgs (val->edata);
8216 DPRINTF (("Check one init: %s", exprNodeList_unparse (vals)));
8217 DPRINTF (("Type: %s", ctype_unparse (t1)));
8219 if (ctype_isRealAP (t1))
8224 if (ctype_isFixedArray (t1))
8226 size_t nelements = ctype_getArraySize (t1);
8228 DPRINTF (("Checked array: %s / %d",
8229 ctype_unparse (t1), nelements));
8231 if (exprNode_isStringLiteral (val))
8233 exprNode_checkStringLiteralLength (t1, val);
8237 if (exprNodeList_size (vals) != size_toInt (nelements))
8239 hasError = optgenerror
8240 (exprNodeList_size (vals) > size_toInt (nelements)
8241 ? FLG_INITSIZE : FLG_INITALLELEMENTS,
8242 message ("Initializer block for "
8243 "%s has %d element%&, but declared as %s: %q",
8244 exprNode_unparse (el),
8245 exprNodeList_size (vals),
8247 exprNodeList_unparse (vals)),
8253 exprNodeList_elements (vals, oneval)
8255 cstring istring = message ("%d", i);
8258 (exprNode_fakeCopy (el),
8259 exprNode_numLiteral (ctype_int, istring,
8260 fileloc_copy (el->loc), i));
8262 if (exprNode_isDefined (newel))
8264 if (exprNodeList_size (vals) == 1
8265 && ctype_isString (exprNode_getType (oneval))
8266 && ctype_isChar (exprNode_getType (newel)))
8268 exprNode_freeIniter (newel);
8272 if (exprNode_checkOneInit (newel, oneval))
8277 if (nerrors > 3 && exprNodeList_size (vals) > 6)
8280 (message ("Additional initialization errors "
8281 "for %s not reported",
8282 exprNode_unparse (el)),
8284 exprNode_freeIniter (newel);
8289 exprNode_freeIniter (newel);
8294 exprNode_freeIniter (newel);
8299 cstring_free (istring);
8302 } end_exprNodeList_elements;
8305 else if (ctype_isStruct (ctype_realType (t1)))
8307 uentryList fields = ctype_getFields (t1);
8310 if (uentryList_size (fields) != exprNodeList_size (vals))
8312 if (uentryList_size (fields) > exprNodeList_size (vals))
8314 hasError = optgenerror
8316 message ("Initializer block for "
8317 "%s has %d field%&, but %s has %d field%&: %q",
8318 exprNode_unparse (el),
8319 exprNodeList_size (vals),
8321 uentryList_size (fields),
8322 exprNodeList_unparse (vals)),
8327 hasError = optgenerror
8329 message ("Initializer block for "
8330 "%s has %d field%&, but %s has %d field%&: %q",
8331 exprNode_unparse (el),
8332 exprNodeList_size (vals),
8334 uentryList_size (fields),
8335 exprNodeList_unparse (vals)),
8341 exprNodeList_elements (vals, oneval)
8343 uentry thisfield = uentryList_getN (fields, i);
8345 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8347 uentry_getName (thisfield));
8349 if (exprNode_isDefined (newel))
8351 if (exprNode_checkOneInit (newel, oneval))
8356 exprNode_freeIniter (newel);
8360 } end_exprNodeList_elements;
8363 /* evans 2001-12-30: added to fix bug reported by Jim Zelenka */
8364 else if (ctype_isUnion (ctype_realType (t1)))
8366 uentryList fields = ctype_getFields (t1);
8370 ** Union initializers set the first member always.
8373 DPRINTF (("Union initializer: %s / %s",
8374 exprNode_unparse (el), ctype_unparse (ctype_realType (t1))));
8376 if (exprNodeList_size (vals) != 1)
8378 hasError = optgenerror
8380 message ("Initializer block for union "
8381 "%s has %d elements, union initializers should have one element: %q",
8382 exprNode_unparse (el),
8383 exprNodeList_size (vals),
8384 exprNodeList_unparse (vals)),
8389 exprNode oneval = exprNodeList_head (vals);
8390 uentry thisfield = uentryList_getN (fields, i);
8392 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8394 uentry_getName (thisfield));
8396 if (exprNode_isDefined (newel))
8398 if (exprNode_checkOneInit (newel, oneval))
8403 exprNode_freeIniter (newel);
8409 hasError = optgenerror
8411 message ("Initializer block used for "
8412 "%s where %t is expected: %s",
8413 exprNode_unparse (el), t1, exprNode_unparse (val)),
8419 if (exprNode_isDefined (val))
8421 doAssign (el, val, TRUE);
8423 if (!exprNode_matchType (t1, val))
8425 hasError = gentypeerror
8427 message ("Initial value of %s is type %t, "
8429 exprNode_unparse (el),
8430 t2, t1, exprNode_unparse (val)),
8439 static /*@notnull@*/ exprNode
8440 exprNode_makeInitializationAux (/*@temp@*/ idDecl t)
8444 DPRINTF (("Initialization: %s", idDecl_unparse (t)));
8446 if (usymtab_exists (idDecl_observeId (t)))
8448 uentry ue = usymtab_lookup (idDecl_observeId (t));
8449 ret = exprNode_createId (ue);
8451 /*@i723 don't do this...but why? */
8453 ct = ctype_realishType (ret->typ);
8455 DPRINTF (("Type: %s", ctype_unparse (ret->typ)));
8457 if (ctype_isUnknown (ct))
8459 if (uentry_isAnyTag (ue))
8463 message ("%s used but not previously declared: %s",
8464 uentry_ekindName (ue),
8465 idDecl_getName (t)),
8473 message ("Variable has unknown (implicitly int) type: %s",
8474 idDecl_getName (t)),
8486 DPRINTF (("Unrecognized: %s", idDecl_unparse (t)));
8488 ue = uentry_makeUnrecognized (idDecl_observeId (t), fileloc_copy (g_currentloc));
8489 /*!! fileloc_copy (g_currentloc)); */
8490 /*@i32!!! should get error without this */
8492 ret = exprNode_fromIdentifierAux (ue);
8495 ** No error - this happens in old style declarations:
8499 message ("Unrecognized identifier in intializer: %s", idDecl_observeId (t)),
8506 exprData_free (ret->edata, ret->kind);
8507 ret->edata = exprData_undefined;
8508 ret->exitCode = XK_NEVERESCAPE;
8509 ret->mustBreak = FALSE;
8510 ret->kind = XPR_INIT;
8514 exprNode exprNode_makeEmptyInitialization (/*@only@*/ idDecl t)
8516 exprNode ret = exprNode_makeInitializationAux (t);
8517 llassert (ret->edata == exprData_undefined);
8518 ret->edata = exprData_makeInit (t, exprNode_undefined);
8522 exprNode exprNode_makeInitialization (/*@only@*/ idDecl t,
8523 /*@only@*/ exprNode e)
8525 uentry ue = usymtab_lookup (idDecl_observeId (t));
8526 exprNode ret = exprNode_makeInitializationAux (t);
8527 fileloc loc = exprNode_loc (e);
8529 DPRINTF (("initialization: %s = %s", idDecl_unparse (t), exprNode_unparse (e)));
8531 if (exprNode_isError (e))
8533 e = exprNode_createUnknown ();
8534 /* error: assume initializer is defined */
8535 sRef_setDefined (ret->sref, g_currentloc);
8536 ret->edata = exprData_makeInit (t, e);
8540 ctype ct = ctype_realishType (ret->typ);
8545 ** was addSafeUse --- what's the problem?
8547 ** int x = 3, y = x ?
8550 exprData_free (ret->edata, ret->kind);
8551 ret->edata = exprData_makeInit (t, e);
8552 DPRINTF (("ret: %s", exprNode_unparse (ret)));
8554 exprNode_checkUse (ret, e->sref, e->loc);
8556 if (ctype_isUnknown (e->typ) && uentry_isValid (ue))
8558 exprNode lhs = exprNode_createId (ue);
8561 ** static storage should be undefined before initializing
8564 if (uentry_isStatic (ue))
8566 sRef_setDefState (lhs->sref, SS_PARTIAL, fileloc_undefined);
8569 (void) exprNode_checkOneInit (lhs, e);
8571 if (uentry_isStatic (ue))
8573 sRef_setDefState (lhs->sref, SS_DEFINED, fileloc_undefined);
8576 exprNode_free (lhs);
8580 if (!exprNode_matchType (ct, e))
8582 if (exprNode_isZero (e) && ctype_isArrayPtr (ct))
8589 (exprNode_getType (e), e, exprNode_getType (ret), ret,
8591 ("Variable %q initialized to type %t, expects %t: %s",
8592 uentry_getName (ue), exprNode_getType (e),
8593 exprNode_getType (ret),
8594 exprNode_unparse (e)),
8600 if (uentry_isStatic (ue))
8602 sRef_setDefState (ret->sref, SS_PARTIAL, fileloc_undefined);
8605 if (exprNode_isStringLiteral (e)
8606 && (ctype_isArray (ct))
8607 && (ctype_isChar (ctype_realType (ctype_baseArrayPtr (ct)))))
8610 ** If t is a char [], the literal is copied.
8613 exprNode_checkStringLiteralLength (ct, e);
8614 sRef_setDefState (ret->sref, SS_DEFINED, e->loc);
8615 ret->val = multiVal_copy (e->val);
8617 sRef_setNullTerminatedState (ret->sref);
8619 if (multiVal_isDefined (e->val))
8621 cstring slit = multiVal_forceString (e->val);
8622 sRef_setLen (ret->sref, cstring_length (slit) + 1);
8625 if (ctype_isFixedArray (ct))
8627 sRef_setSize (ret->sref, size_toInt (ctype_getArraySize (ct)));
8632 doAssign (ret, e, TRUE);
8635 if (uentry_isStatic (ue))
8637 sRef_setDefState (ret->sref, SS_DEFINED, fileloc_undefined);
8641 if (context_inIterDef ())
8643 /* should check if it is yield */
8644 uentry_setUsed (ue, loc);
8651 exprNode_mergeUSs (ret, e);
8652 DPRINTF (("Ret: %s %p %p",
8653 exprNode_unparse (ret),
8654 ret->requiresConstraints,
8655 ret->ensuresConstraints));
8657 DPRINTF (("Ret: %s %s %s",
8658 exprNode_unparse (ret),
8659 constraintList_unparse (ret->requiresConstraints),
8660 constraintList_unparse (ret->ensuresConstraints)));
8664 exprNode exprNode_iter (/*@observer@*/ uentry name,
8665 /*@only@*/ exprNodeList alist,
8666 /*@only@*/ exprNode body,
8667 /*@observer@*/ uentry end)
8672 llassert (uentry_isValid (name));
8674 uentry_setUsed (name, exprNode_loc (body));
8676 ret = exprNode_createPartialCopy (body);
8677 iname = uentry_getName (name);
8679 if (uentry_isInvalid (end))
8681 llerror (FLG_ITERBALANCE,
8682 message ("Iter %s not balanced with end_%s", iname, iname));
8686 cstring ename = uentry_getName (end);
8688 if (!cstring_equalPrefixLit (ename, "end_"))
8690 llerror (FLG_ITERBALANCE, message ("Iter %s not balanced with end_%s: %s",
8691 iname, iname, ename));
8695 if (!cstring_equal (iname, cstring_suffix (ename, 4)))
8697 llerror (FLG_ITERBALANCE,
8698 message ("Iter %s not balanced with end_%s: %s",
8699 iname, iname, ename));
8703 cstring_free (ename);
8706 context_exitIterClause (body);
8708 ret->kind = XPR_ITER;
8709 ret->edata = exprData_makeIter (name, alist, body, end);
8711 if (uentry_isIter (name))
8713 (void) checkArgsReal (name, body,
8714 uentry_getParams (name), alist, TRUE, ret);
8717 cstring_free (iname);
8723 exprNode_iterNewId (/*@only@*/ cstring s)
8725 exprNode e = exprNode_new ();
8726 uentry ue = uentryList_getN (uentry_getParams (getCurrentIter ()), iterParamNo ());
8728 llassert (processingIterVars ());
8730 e->loc = context_getSaveLocation ();
8732 if (fileloc_isUndefined (e->loc))
8734 fileloc_free (e->loc);
8735 e->loc = fileloc_copy (g_currentloc);
8738 e->uses = sRefSet_new ();
8739 e->sets = sRefSet_new ();
8740 e->msets = sRefSet_new ();
8742 e->val = multiVal_unknown ();
8743 e->guards = guardSet_new ();
8744 e->sref = sRef_undefined;
8745 e->isJumpPoint = FALSE;
8746 e->exitCode = XK_NEVERESCAPE;
8748 /*> missing fields, detected by splint <*/
8749 e->canBreak = FALSE;
8750 e->mustBreak = FALSE;
8751 e->etext = cstring_undefined;
8753 if (uentry_isYield (ue))
8755 uentry uue = uentry_makeVariable (s, uentry_getType (ue),
8756 fileloc_copy (e->loc),
8760 uue = usymtab_supEntrySrefReturn (uue);
8762 sr = uentry_getSref (uue);
8763 sRef_mergeStateQuiet (sr, uentry_getSref (ue));
8764 sr = uentry_getSref (uue);
8765 sRef_setDefined (sr, e->loc);
8767 e->typ = uentry_getType (uue);
8769 e->edata = exprData_makeId (uue);
8770 uentry_setUsed (uue, g_currentloc);
8776 sRef_setGlobalScope ();
8777 uue = uentry_makeVariableLoc (s, ctype_unknown);
8779 e->typ = ctype_unknown;
8780 e->edata = exprData_makeId (uue);
8782 uentry_setUsed (uue, e->loc);
8783 uentry_setHasNameError (uue);
8785 if (context_getFlag (FLG_REPEATUNRECOG))
8787 uentry_markOwned (uue);
8791 usymtab_supGlobalEntry (uue);
8794 sRef_clearGlobalScope ();
8796 voptgenerror (FLG_UNRECOG, message ("Unrecognized identifier: %s", s),
8803 exprNode_defineConstraints(e);
8808 exprNode_iterExpr (/*@returned@*/ exprNode e)
8810 if (!processingIterVars ())
8812 llcontbuglit ("checkIterParam: not in iter");
8816 if (uentry_isYield (uentryList_getN (uentry_getParams (getCurrentIter ()),
8819 if (exprNode_isDefined (e))
8821 if (fileloc_isDefined (e->loc))
8825 message ("Yield parameter is not simple identifier: %s",
8826 exprNode_unparse (e)),
8833 message ("Yield parameter is not simple identifier: %s",
8834 exprNode_unparse (e)),
8844 exprNode_iterId (/*@observer@*/ uentry c)
8848 llassert (processingIterVars ());
8850 ue = uentryList_getN (uentry_getParams (getCurrentIter ()),
8853 if (uentry_isYield (ue))
8855 ctype ct = uentry_getType (ue);
8856 exprNode e = exprNode_createPlain (ct);
8857 cstring name = uentry_getName (c);
8858 uentry le = uentry_makeVariable (name, ct, fileloc_undefined, FALSE);
8860 uentry_setUsed (ue, g_currentloc);
8861 uentry_setHasNameError (ue);
8863 cstring_free (name);
8866 e->edata = exprData_makeId (le);
8867 e->loc = context_getSaveLocation ();
8868 e->sref = uentry_getSref (le);
8870 usymtab_supEntrySref (le);
8872 if (!context_inHeader ())
8876 message ("Yield parameter shadows local declaration: %q",
8877 uentry_getName (c)),
8878 fileloc_isDefined (e->loc) ? e->loc : g_currentloc))
8880 uentry_showWhereDeclared (c);
8887 return (exprNode_fromIdentifierAux (c));
8890 exprNode exprNode_iterStart (/*@observer@*/ uentry name, /*@only@*/ exprNodeList alist)
8892 exprNode ret = exprNode_create (ctype_unknown);
8894 ret->kind = XPR_ITERCALL;
8895 ret->edata = exprData_makeIterCall (name, alist);
8897 if (uentry_isIter (name))
8899 uentryList params = uentry_getParams (name);
8901 if (context_inIterDef ()
8902 && uentryList_size (params) == exprNodeList_size (alist))
8906 exprNodeList_elements (alist, arg)
8908 uentry parg = uentryList_getN (params, i);
8910 if (uentry_isYield (parg))
8912 uentry ue = exprNode_getUentry (arg);
8914 if (uentry_isValid (ue))
8921 } end_exprNodeList_elements;
8924 (void) checkArgsReal (name, ret, params, alist, TRUE, ret);
8925 checkUnspecCall (ret, params, alist);
8931 /*@exposed@*/ sRef exprNode_getSref (exprNode e)
8933 if (exprNode_isDefined (e))
8935 if (sRef_isInvalid (e->sref))
8938 e->sref = sRef_makeUnknown ();
8939 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
8950 return sRef_undefined;
8954 /*@observer@*/ cstring
8955 exprNode_unparseFirst (exprNode e)
8957 if (exprNode_isDefined (e))
8961 if (e->kind == XPR_STMTLIST
8962 || e->kind == XPR_COMMA || e->kind == XPR_COND)
8964 exprNode first = exprData_getPairA (e->edata);
8966 if (exprNode_isDefined (first))
8968 return (exprNode_unparseFirst (exprData_getPairA (e->edata)));
8972 return (cstring_makeLiteralTemp ("..."));
8976 ret = cstring_elide (exprNode_unparse (e), 20);
8977 cstring_markOwned (ret);
8983 return cstring_makeLiteralTemp ("<error>");
8987 /*@observer@*/ cstring
8988 exprNode_unparse (/*@temp@*/ exprNode e)
8990 if (exprNode_isError (e))
8992 return cstring_makeLiteralTemp ("<error>");
8995 if (cstring_isDefined (e->etext))
9001 cstring ret = exprNode_doUnparse (e);
9003 /*@-modifies@*/ /* benevolent */
9010 /*@observer@*/ fileloc
9011 exprNode_loc (exprNode e)
9013 if (exprNode_isError (e))
9015 return (g_currentloc);
9024 ** executes exprNode e
9025 ** recursively rexecutes as though in original parse using
9026 ** information in e->edata
9029 static /*@only@*/ exprNodeList exprNodeList_effect (exprNodeList e)
9031 exprNodeList ret = exprNodeList_new ();
9033 exprNodeList_elements (e, current)
9035 exprNodeList_addh (ret, exprNode_effect (current));
9036 } end_exprNodeList_elements;
9041 static /*@only@*/ exprNode exprNode_effect (exprNode e)
9042 /*@globals internalState@*/
9044 bool innerEffect = inEffect;
9050 context_clearJustPopped ();
9052 if (exprNode_isError (e))
9054 ret = exprNode_undefined;
9059 ** Turn off expose and dependent transfer checking.
9060 ** Need to pass exposed internal nodes,
9061 ** [ copying would be a waste! ]
9062 ** [ Actually, I think I wasted a lot more time than its worth ]
9063 ** [ trying to do this. ]
9067 /*@-observertrans@*/
9068 /*@-dependenttrans@*/
9075 ret = exprNode_addParens (exprData_getUopTok (data),
9076 exprNode_effect (exprData_getUopNode (data)));
9079 ret = exprNode_assign (exprNode_effect (exprData_getOpA (data)),
9080 exprNode_effect (exprData_getOpB (data)),
9081 exprData_getOpTok (data));
9084 ret = exprNode_undefined;
9087 ret = exprNode_functionCall (exprNode_effect (exprData_getFcn (data)),
9088 exprNodeList_effect (exprData_getArgs (data)));
9101 cstring id = exprData_getId (data);
9102 uentry ue = usymtab_lookupSafe (id);
9104 ret = exprNode_fromIdentifierAux (ue);
9105 ret->loc = fileloc_update (ret->loc, e->loc);
9112 ret = exprNode_arrayFetch (exprNode_effect (exprData_getPairA (data)),
9113 exprNode_effect (exprData_getPairB (data)));
9117 ** evans 2002-03-15: for && and ||, need to do the guards also
9118 ** this is what cgrammar.y does - should be
9119 ** able to avoid duplication, but need to
9120 ** time with grammar productions.
9123 DPRINTF (("Effect: %s", exprNode_unparse (e)));
9125 if (lltok_getTok (exprData_getOpTok (data)) == AND_OP)
9127 exprNode e1 = exprNode_effect (exprData_getOpA (data));
9129 exprNode_produceGuards (e1);
9130 context_enterAndClause (e1);
9131 e2 = exprNode_effect (exprData_getOpB (data));
9133 ret = exprNode_op (e1, e2,
9134 exprData_getOpTok (data));
9136 context_exitAndClause (ret, e2);
9138 else if (lltok_getTok (exprData_getOpTok (data)) == OR_OP)
9140 exprNode e1 = exprNode_effect (exprData_getOpA (data));
9142 exprNode_produceGuards (e1);
9143 context_enterOrClause (e1);
9144 e2 = exprNode_effect (exprData_getOpB (data));
9146 ret = exprNode_op (e1, e2,
9147 exprData_getOpTok (data));
9149 context_exitOrClause (ret, e2);
9153 ret = exprNode_op (exprNode_effect (exprData_getOpA (data)),
9154 exprNode_effect (exprData_getOpB (data)),
9155 exprData_getOpTok (data));
9160 ret = exprNode_postOp (exprNode_effect (exprData_getUopNode (data)),
9161 exprData_getUopTok (data));
9164 ret = exprNode_preOp (exprNode_effect (exprData_getUopNode (data)),
9165 exprData_getUopTok (data));
9177 ret = exprNode_vaArg (exprData_getCastTok (data),
9178 exprNode_effect (exprData_getCastNode (data)),
9179 exprData_getCastType (data));
9183 ret = exprNode_cast (exprData_getCastTok (data),
9184 exprNode_effect (exprData_getCastNode (data)),
9185 exprData_getCastType (data));
9188 ret = exprNode_iterStart (exprData_getIterCallIter (data),
9190 (exprData_getIterCallArgs (data)));
9194 ret = exprNode_iter (exprData_getIterSname (data),
9195 exprNodeList_effect (exprData_getIterAlist (data)),
9196 exprNode_effect (exprData_getIterBody (data)),
9197 exprData_getIterEname (data));
9201 ret = exprNode_for (exprNode_effect (exprData_getPairA (data)),
9202 exprNode_effect (exprData_getPairB (data)));
9206 ret = exprNode_forPred (exprNode_effect (exprData_getTripleInit (data)),
9207 exprNode_effect (exprData_getTripleTest (data)),
9208 exprNode_effect (exprData_getTripleInc (data)));
9212 ret = exprNode_createTok (exprData_getTok (data));
9216 ret = exprNode_goto (exprData_getLiteral (data));
9217 ret->loc = fileloc_update (ret->loc, e->loc);
9221 ret = exprNode_continue (exprData_getTok (data), QSAFEBREAK);
9225 ret = exprNode_break (exprData_getTok (data), QSAFEBREAK);
9229 ret = exprNode_return (exprNode_effect (exprData_getSingle (data)));
9232 case XPR_NULLRETURN:
9233 ret = exprNode_nullReturn (exprData_getTok (data));
9237 ret = exprNode_comma (exprNode_effect (exprData_getPairA (data)),
9238 exprNode_effect (exprData_getPairB (data)));
9242 ret = exprNode_cond (exprNode_effect (exprData_getTriplePred (data)),
9243 exprNode_effect (exprData_getTripleTrue (data)),
9244 exprNode_effect (exprData_getTripleFalse (data)));
9247 ret = exprNode_if (exprNode_effect (exprData_getPairA (data)),
9248 exprNode_effect (exprData_getPairB (data)));
9252 ret = exprNode_ifelse (exprNode_effect (exprData_getTriplePred (data)),
9253 exprNode_effect (exprData_getTripleTrue (data)),
9254 exprNode_effect (exprData_getTripleFalse (data)));
9257 ret = exprNode_whilePred (exprData_getSingle (data));
9261 ret = exprNode_while (exprNode_effect (exprData_getPairA (data)),
9262 exprNode_effect (exprData_getPairB (data)));
9266 ret = exprNode_doWhile (exprNode_effect (exprData_getPairA (data)),
9267 exprNode_effect (exprData_getPairB (data)));
9271 ret = exprNode_makeBlock (exprNode_effect (exprData_getSingle (data)));
9275 ret = exprNode_statement (exprNode_effect (exprData_getUopNode (data)),
9276 exprData_getUopTok (data));
9280 ret = exprNode_concat (exprNode_effect (exprData_getPairA (data)),
9281 exprNode_effect (exprData_getPairB (data)));
9286 ret = exprNode_caseMarker
9287 (exprNode_effect (exprData_getSingle (data)),
9293 ret = exprNode_createTok (exprData_getTok (data));
9297 ret = exprNode_switch (exprNode_effect (exprData_getPairA (data)),
9298 exprNode_effect (exprData_getPairB (data)));
9302 ret = exprNode_makeInitialization
9303 (exprData_getInitId (data),
9304 exprNode_effect (exprData_getInitNode (data)));
9308 ret = exprNode_fieldAccessAux
9309 (exprNode_effect (exprData_getFieldNode (data)),
9310 exprNode_loc (exprData_getFieldNode (data)),
9311 cstring_copy (exprData_getFieldName (data)));
9315 ret = exprNode_arrowAccessAux
9316 (exprNode_effect (exprData_getFieldNode (data)),
9317 exprNode_loc (exprData_getFieldNode (data)),
9318 cstring_copy (exprData_getFieldName (data)));
9321 case XPR_STRINGLITERAL:
9335 /*@=observertrans@*/
9337 /*@=dependenttrans@*/
9348 static /*@observer@*/ cstring exprNode_rootVarName (exprNode e)
9353 if (exprNode_isError (e))
9355 return cstring_undefined;
9363 ret = exprNode_rootVarName (exprData_getUopNode (data));
9366 ret = exprNode_rootVarName (exprData_getOpA (data));
9370 ret = exprData_getId (data);
9373 ret = idDecl_getName (exprData_getInitId (data));
9398 case XPR_NULLRETURN:
9420 case XPR_STRINGLITERAL:
9421 ret = cstring_undefined;
9428 static /*@only@*/ cstring exprNode_doUnparse (exprNode e)
9433 if (exprNode_isError (e))
9435 static /*@only@*/ cstring error = cstring_undefined;
9437 if (!cstring_isDefined (error))
9439 error = cstring_makeLiteral ("<error>");
9450 ret = message ("(%s)", exprNode_unparse (exprData_getUopNode (e->edata)));
9453 ret = message ("%s %s %s",
9454 exprNode_unparse (exprData_getOpA (data)),
9455 lltok_unparse (exprData_getOpTok (data)),
9456 exprNode_unparse (exprData_getOpB (data)));
9459 ret = message ("%s(%q)",
9460 exprNode_unparse (exprData_getFcn (data)),
9461 exprNodeList_unparse (exprData_getArgs (data)));
9464 ret = message ("{ %q }", exprNodeList_unparse (exprData_getArgs (data)));
9467 ret = cstring_undefined;
9470 ret = message ("%s:", exprData_getId (data));
9474 ret = cstring_copy (exprData_getId (data));
9477 ret = message ("%s[%s]", exprNode_unparse (exprData_getPairA (data)),
9478 exprNode_unparse (exprData_getPairB (data)));
9481 ret = message ("<body>");
9484 ret = message ("%s %s %s",
9485 exprNode_unparse (exprData_getOpA (data)),
9486 lltok_unparse (exprData_getOpTok (data)),
9487 exprNode_unparse (exprData_getOpB (data)));
9491 ret = message ("%s%s",
9492 lltok_unparse (exprData_getUopTok (data)),
9493 exprNode_unparse (exprData_getUopNode (data)));
9497 ret = message ("%s%s",
9498 exprNode_unparse (exprData_getUopNode (data)),
9499 lltok_unparse (exprData_getUopTok (data)));
9503 ret = message ("offsetof(%s,%q)",
9504 ctype_unparse (qtype_getType (exprData_getOffsetType (data))),
9505 cstringList_unparseSep (exprData_getOffsetName (data), cstring_makeLiteralTemp (".")));
9509 ret = message ("sizeof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9513 ret = message ("sizeof(%s)", exprNode_unparse (exprData_getSingle (data)));
9517 ret = message ("alignof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9521 ret = message ("alignof(%s)", exprNode_unparse (exprData_getSingle (data)));
9525 ret = message ("va_arg(%s, %q)",
9526 exprNode_unparse (exprData_getCastNode (data)),
9527 qtype_unparse (exprData_getCastType (data)));
9531 ret = message ("%q(%q)",
9532 uentry_getName (exprData_getIterCallIter (data)),
9533 exprNodeList_unparse (exprData_getIterCallArgs (data)));
9536 ret = message ("%q(%q) %s %q",
9537 uentry_getName (exprData_getIterSname (data)),
9538 exprNodeList_unparse (exprData_getIterAlist (data)),
9539 exprNode_unparse (exprData_getIterBody (data)),
9540 uentry_getName (exprData_getIterEname (data)));
9543 ret = message ("(%q)%s",
9544 qtype_unparse (exprData_getCastType (data)),
9545 exprNode_unparse (exprData_getCastNode (data)));
9549 ret = message ("%s %s",
9550 exprNode_unparse (exprData_getPairA (data)),
9551 exprNode_unparse (exprData_getPairB (data)));
9555 ret = message ("for (%s; %s; %s)",
9556 exprNode_unparse (exprData_getTripleInit (data)),
9557 exprNode_unparse (exprData_getTripleTest (data)),
9558 exprNode_unparse (exprData_getTripleInc (data)));
9562 ret = message ("goto %s", exprData_getLiteral (data));
9566 ret = cstring_makeLiteral ("continue");
9570 ret = cstring_makeLiteral ("break");
9574 ret = message ("return %s", exprNode_unparse (exprData_getSingle (data)));
9577 case XPR_NULLRETURN:
9578 ret = cstring_makeLiteral ("return");
9582 ret = message ("%s, %s",
9583 exprNode_unparse (exprData_getPairA (data)),
9584 exprNode_unparse (exprData_getPairB (data)));
9588 ret = message ("%s ? %s : %s",
9589 exprNode_unparse (exprData_getTriplePred (data)),
9590 exprNode_unparse (exprData_getTripleTrue (data)),
9591 exprNode_unparse (exprData_getTripleFalse (data)));
9594 ret = message ("if (%s) %s",
9595 exprNode_unparse (exprData_getPairA (data)),
9596 exprNode_unparse (exprData_getPairB (data)));
9600 ret = message ("if (%s) %s else %s",
9601 exprNode_unparse (exprData_getTriplePred (data)),
9602 exprNode_unparse (exprData_getTripleTrue (data)),
9603 exprNode_unparse (exprData_getTripleFalse (data)));
9606 ret = message ("while (%s) %s",
9607 exprNode_unparse (exprData_getPairA (data)),
9608 exprNode_unparse (exprData_getPairB (data)));
9612 ret = cstring_copy (exprNode_unparse (exprData_getSingle (data)));
9616 ret = cstring_copy (lltok_unparse (exprData_getTok (data)));
9620 ret = message ("do { %s } while (%s)",
9621 exprNode_unparse (exprData_getPairB (data)),
9622 exprNode_unparse (exprData_getPairA (data)));
9626 ret = message ("{ %s }", exprNode_unparse (exprData_getSingle (data)));
9627 /* evans 2002-02-20 was unparseFirst! */
9631 ret = message ("%s;", exprNode_unparse (exprData_getUopNode (data)));
9635 if (exprNode_isStatement (exprData_getPairA (data)))
9638 ** statement expressions already print the ;
9641 ret = message ("%s %s",
9642 exprNode_unparse (exprData_getPairA (data)),
9643 exprNode_unparse (exprData_getPairB (data)));
9647 ret = message ("%s; %s",
9648 exprNode_unparse (exprData_getPairA (data)),
9649 exprNode_unparse (exprData_getPairB (data)));
9655 ret = cstring_makeLiteral ("default:");
9659 ret = message ("switch (%s) %s",
9660 exprNode_unparse (exprData_getPairA (data)),
9661 exprNode_unparse (exprData_getPairB (data)));
9666 ret = message ("case %s:",
9667 exprNode_unparse (exprData_getSingle (data)));
9671 if (exprNode_isError (exprData_getInitNode (data)))
9673 ret = message ("%q", idDecl_unparseC (exprData_getInitId (data)));
9677 ret = message ("%q = %s",
9678 idDecl_unparseC (exprData_getInitId (data)),
9679 exprNode_unparse (exprData_getInitNode (data)));
9684 ret = message ("%s.%s",
9685 exprNode_unparse (exprData_getFieldNode (data)),
9686 exprData_getFieldName (data));
9690 ret = message ("%s->%s",
9691 exprNode_unparse (exprData_getFieldNode (data)),
9692 exprData_getFieldName (data));
9695 case XPR_STRINGLITERAL:
9696 if (ctype_isWideString (e->typ))
9698 ret = message ("L\"%s\"", exprData_getLiteral (data));
9702 ret = message ("\"%s\"", exprData_getLiteral (data));
9707 ret = cstring_copy (exprData_getLiteral (data));
9711 ret = cstring_makeLiteral ("<node>");
9719 exprNode_isInitializer (exprNode e)
9721 return (exprNode_isDefined (e)
9722 && e->kind == XPR_INIT);
9726 exprNode_isCharLit (exprNode e)
9728 if (exprNode_isDefined (e))
9730 return (multiVal_isChar (exprNode_getValue (e)));
9739 exprNode_isNumLit (exprNode e)
9741 if (exprNode_isDefined (e))
9743 return (multiVal_isInt (exprNode_getValue (e)));
9752 exprNode_isFalseConstant (exprNode e)
9754 if (exprNode_isDefined (e))
9756 cstring s = exprNode_rootVarName (e);
9758 if (cstring_equal (s, context_getFalseName ()))
9768 exprNode_matchLiteral (ctype expected, exprNode e)
9770 if (exprNode_isDefined (e))
9772 multiVal m = exprNode_getValue (e);
9774 if (multiVal_isDefined (m))
9776 if (multiVal_isInt (m))
9778 long int val = multiVal_forceInt (m);
9780 if (ctype_isDirectBool (ctype_realishType (expected)))
9784 return FALSE; /* really?! return TRUE; allow use of 0 for FALSE */
9792 if (ctype_isRealInt (expected))
9795 ** unsigned <- [ constant >= 0 is okay ]
9798 if (ctype_isUnsigned (expected))
9807 ** No checks on sizes of integers...maybe add
9811 DPRINTF (("Here: %s => %s", exprNode_unparse (e), ctype_unparse (expected)));
9812 DPRINTF (("Type: %s / %s", ctype_unparse (exprNode_getType (e)),
9813 bool_unparse (ctype_isInt (exprNode_getType (e)))));
9815 if (context_getFlag (FLG_NUMLITERAL)
9816 && (ctype_isRegularInt (exprNode_getType (e)) || val == 0)) {
9822 return FALSE; /* evs 2000-05-17: previously, always returned TRUE */
9826 else if (ctype_isChar (expected))
9830 else if (ctype_isArrayPtr (expected))
9833 ** evans 2001-10-14: We allow 0 to match any pointer, but only if the type matches or is void *.
9838 if (ctype_match (exprNode_getType (e), expected)
9839 || ctype_isVoidPointer (exprNode_getType (e)))
9849 else if (ctype_isAnyFloat (expected))
9851 return (context_getFlag (FLG_NUMLITERAL));
9858 else if (multiVal_isDouble (m))
9860 if (ctype_isAnyFloat (expected))
9865 else if (multiVal_isChar (m))
9867 char val = multiVal_forceChar (m);
9869 if (ctype_isChar (expected))
9871 if (ctype_isUnsigned (expected) && ((int)val) < 0)
9892 exprNode_matchType (ctype expected, exprNode e)
9896 if (!exprNode_isDefined (e)) return TRUE;
9898 actual = ctype_realishType (exprNode_getType (e));
9900 if (ctype_match (ctype_realishType (expected), actual))
9905 llassert (!exprNode_isError (e));
9906 return (exprNode_matchLiteral (expected, e));
9910 exprNode_matchTypes (exprNode e1, exprNode e2)
9915 if (!exprNode_isDefined (e1)) return TRUE;
9916 if (!exprNode_isDefined (e2)) return TRUE;
9919 ** realish type --- keep bools, bools
9922 t1 = ctype_realishType (exprNode_getType (e1));
9923 t2 = ctype_realishType (exprNode_getType (e2));
9925 if (ctype_match (t1, t2))
9930 DPRINTF (("Matching literal! %s %s %s %s",
9931 ctype_unparse (t1), exprNode_unparse (e2),
9932 ctype_unparse (t2), exprNode_unparse (e1)));
9934 return (exprNode_matchLiteral (t1, e2) || exprNode_matchLiteral (t2, e1));
9942 exprNode_matchArgType (ctype ct, exprNode e)
9946 if (!exprNode_isDefined (e))
9951 et = ctype_realType (exprNode_getType (e));
9953 if (ctype_matchArg (ct, et)) return TRUE;
9955 llassert (!exprNode_isError (e));
9956 return (exprNode_matchLiteral (ct, e));
9959 static /*@only@*/ exprNodeSList
9960 exprNode_flatten (/*@dependent@*/ exprNode e) /*@*/
9962 if (exprNode_isDefined (e))
9964 if (e->kind == XPR_STMTLIST)
9966 return (exprNodeSList_append
9967 (exprNode_flatten (exprData_getPairA (e->edata)),
9968 exprNode_flatten (exprData_getPairB (e->edata))));
9970 else if (e->kind == XPR_BLOCK)
9972 return (exprNode_flatten (exprData_getSingle (e->edata)));
9976 return (exprNodeSList_singleton (e));
9980 return exprNodeSList_new ();
9983 static /*@exposed@*/ exprNode
9984 exprNode_lastStatement (/*@returned@*/ exprNode e)
9986 if (exprNode_isDefined (e))
9988 if (e->kind == XPR_STMTLIST)
9990 exprNode b = exprData_getPairB (e->edata);
9992 if (exprNode_isDefined (b))
9994 return exprNode_lastStatement (b);
9998 return exprNode_lastStatement (exprData_getPairA (e->edata));
10001 else if (e->kind == XPR_BLOCK)
10003 return (exprNode_lastStatement (exprData_getSingle (e->edata)));
10011 return exprNode_undefined;
10014 static /*@exposed@*/ exprNode
10015 exprNode_firstStatement (/*@returned@*/ exprNode e)
10017 if (exprNode_isDefined (e))
10019 if (e->kind == XPR_STMTLIST)
10021 exprNode b = exprData_getPairA (e->edata);
10023 if (exprNode_isDefined (b))
10025 return exprNode_firstStatement (b);
10029 return exprNode_firstStatement (exprData_getPairB (e->edata));
10032 else if (e->kind == XPR_BLOCK)
10034 return (exprNode_firstStatement (exprData_getSingle (e->edata)));
10042 return exprNode_undefined;
10046 exprNode_mergeUSs (exprNode res, exprNode other)
10048 if (exprNode_isDefined (res) && exprNode_isDefined (other))
10050 res->msets = sRefSet_union (res->msets, other->msets);
10051 res->sets = sRefSet_union (res->sets, other->sets);
10052 res->uses = sRefSet_union (res->uses, other->uses);
10057 exprNode_mergeCondUSs (exprNode res, exprNode other1, exprNode other2)
10059 if (exprNode_isDefined (res))
10061 if (exprNode_isDefined (other1))
10063 res->sets = sRefSet_union (res->sets, other1->sets);
10064 res->msets = sRefSet_union (res->msets, other1->msets);
10065 res->uses = sRefSet_union (res->uses, other1->uses);
10067 if (exprNode_isDefined (other2))
10069 res->sets = sRefSet_union (res->sets, other2->sets);
10070 res->msets = sRefSet_union (res->msets, other2->msets);
10071 res->uses = sRefSet_union (res->uses, other2->uses);
10077 ** modifies e->uses
10079 ** Reports errors is s is not defined.
10083 exprNode_addUse (exprNode e, /*@exposed@*/ sRef s)
10085 if (exprNode_isDefined (e))
10087 e->uses = sRefSet_insert (e->uses, s);
10092 exprNode_checkUse (exprNode e, /*@exposed@*/ sRef s, fileloc loc)
10094 if (sRef_isKnown (s) && !sRef_isConst (s))
10097 ** need to check all outer types are useable
10100 DPRINTF (("Check use: %s / %s",
10101 exprNode_unparse (e), sRef_unparse (s)));
10103 exprNode_addUse (e, s);
10105 if (!context_inProtectVars ())
10108 ** only report the deepest error
10111 sRef errorRef = sRef_undefined;
10112 sRef lastRef = sRef_undefined;
10113 bool deadRef = FALSE;
10114 bool unuseable = FALSE;
10115 bool errorMaybe = FALSE;
10117 while (sRef_isValid (s) && sRef_isKnown (s))
10119 ynm readable = sRef_isValidLvalue (s);
10121 DPRINTF (("Readable: %s / %s",
10122 sRef_unparseFull (s), ynm_unparse (readable)));
10124 if (!(ynm_toBoolStrict (readable)))
10126 if (ynm_isMaybe (readable))
10128 lastRef = errorRef;
10130 DPRINTF (("Setting ERROR: %s", sRef_unparseFull (s)));
10131 deadRef = sRef_isPossiblyDead (errorRef);
10132 unuseable = sRef_isUnuseable (errorRef);
10137 lastRef = errorRef;
10139 deadRef = sRef_isDead (errorRef);
10140 unuseable = sRef_isUnuseable (errorRef);
10141 errorMaybe = FALSE;
10144 if (!sRef_isPartial (s))
10146 DPRINTF (("Defining! %s", sRef_unparseFull (s)));
10147 sRef_setDefined (s, fileloc_undefined);
10151 s = sRef_getBaseSafe (s);
10154 if (sRef_isValid (errorRef))
10156 if (sRef_isValid (lastRef) && sRef_isField (lastRef)
10157 && sRef_isPointer (errorRef))
10159 errorRef = lastRef;
10164 if (sRef_isThroughArrayFetch (errorRef))
10167 (FLG_STRICTUSERELEASED,
10168 message ("%q %q may be used after being released",
10169 sRef_unparseKindNamePlain (errorRef),
10170 sRef_unparse (errorRef)),
10173 sRef_showRefKilled (errorRef);
10175 if (sRef_isKept (errorRef))
10177 sRef_clearAliasState (errorRef, loc);
10183 DPRINTF (("HERE: %s", sRef_unparse (errorRef)));
10187 message ("%q %q %qused after being released",
10188 sRef_unparseKindNamePlain (errorRef),
10189 sRef_unparse (errorRef),
10190 cstring_makeLiteral (errorMaybe
10191 ? "may be " : "")),
10194 sRef_showRefKilled (errorRef);
10196 if (sRef_isKept (errorRef))
10198 sRef_clearAliasState (errorRef, loc);
10203 else if (unuseable)
10207 message ("%q %q%qused in inconsistent state",
10208 sRef_unparseKindName (errorRef),
10209 sRef_unparseOpt (errorRef),
10210 cstring_makeLiteral (errorMaybe ? "may be " : "")),
10213 sRef_showStateInconsistent (errorRef);
10218 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
10222 message ("%q %q%qused before definition",
10223 sRef_unparseKindName (errorRef),
10224 sRef_unparseOpt (errorRef),
10225 cstring_makeLiteral (errorMaybe ? "may be " : "")),
10228 DPRINTF (("Error: %s", sRef_unparseFull (errorRef)));
10231 sRef_setDefined (errorRef, loc);
10233 if (sRef_isAddress (errorRef))
10235 sRef_setDefined (sRef_getRootBase (errorRef), loc);
10237 } /* end is error */
10245 checkSafeUse (exprNode e, /*@exposed@*/ sRef s)
10247 if (exprNode_isDefined (e) && sRef_isKnown (s))
10249 e->uses = sRefSet_insert (e->uses, s);
10254 exprNode_checkSetAny (exprNode e, /*@dependent@*/ cstring name)
10256 if (exprNode_isDefined (e))
10258 e->sets = sRefSet_insert (e->sets, sRef_makeUnconstrained (name));
10263 exprNode_checkSet (exprNode e, /*@exposed@*/ sRef s)
10265 sRef defines = sRef_undefined;
10267 if (sRef_isValid (s) && !sRef_isNothing (s))
10269 uentry ue = sRef_getBaseUentry (s);
10271 if (uentry_isValid (ue))
10273 uentry_setLset (ue);
10276 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
10278 voptgenerror (FLG_USEDEF,
10279 message ("Attempt to set unuseable storage: %q",
10284 if (sRef_isMeaningful (s))
10286 if (sRef_isDead (s))
10288 sRef base = sRef_getBaseSafe (s);
10290 if (sRef_isValid (base)
10291 && sRef_isDead (base))
10293 sRef_setPartial (s, exprNode_loc (e));
10296 defines = s; /* okay - modifies for only param */
10298 else if (sRef_isPartial (s))
10300 sRef eref = exprNode_getSref (e);
10302 if (!sRef_isPartial (eref))
10305 ** should do something different here???
10308 sRef_setDefinedComplete (eref, exprNode_loc (e));
10312 sRef_setPartialDefinedComplete (eref, exprNode_loc (e));
10315 if (sRef_isMeaningful (eref))
10324 else if (sRef_isAllocated (s))
10326 sRef eref = exprNode_getSref (e);
10329 if (!sRef_isAllocated (eref))
10331 sRef_setDefinedComplete (eref, exprNode_loc (e));
10335 sRef base = sRef_getBaseSafe (eref);
10337 if (sRef_isValid (base))
10339 sRef_setPdefined (base, exprNode_loc (e));
10347 sRef_setDefinedNCComplete (s, exprNode_loc (e));
10352 else /* not meaningful...but still need to insert it */
10358 if (exprNode_isDefined (e) && sRef_isValid (defines))
10360 e->sets = sRefSet_insert (e->sets, defines);
10365 exprNode_checkMSet (exprNode e, /*@exposed@*/ sRef s)
10367 if (sRef_isValid (s) && !sRef_isNothing (s))
10369 uentry ue = sRef_getBaseUentry (s);
10371 if (uentry_isValid (ue))
10373 uentry_setLset (ue);
10376 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
10378 voptgenerror (FLG_USEDEF,
10379 message ("Attempt to set unuseable storage: %q", sRef_unparse (s)),
10383 if (sRef_isMeaningful (s))
10385 sRef_setDefinedComplete (s, exprNode_loc (e));
10388 if (exprNode_isDefined (e))
10390 e->msets = sRefSet_insert (e->msets, s);
10396 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode fcn, uentryList params, exprNodeList args)
10398 checkAnyCall (fcn, cstring_undefined, params, args,
10399 FALSE, sRefSet_undefined, FALSE, 0);
10403 checkOneArg (uentry ucurrent, /*@notnull@*/ exprNode current,
10404 /*@dependent@*/ exprNode fcn, bool isSpec, int argno, int totargs)
10408 if (uentry_isYield (ucurrent))
10410 sRef_setDefined (exprNode_getSref (current), exprNode_loc (current));
10411 exprNode_checkSet (current, current->sref);
10415 if (uentry_isSefParam (ucurrent))
10417 sRefSet sets = current->sets;
10418 sRef ref = exprNode_getSref (current);
10420 if (sRef_isMacroParamRef (ref))
10422 uentry ue = sRef_getUentry (ref);
10424 if (!uentry_isSefParam (ue))
10429 ("Parameter %d to %s is declared sef, but "
10430 "the argument is a macro parameter declared "
10432 argno, exprNode_unparse (fcn),
10433 exprNode_unparse (current)),
10434 exprNode_loc (current));
10438 if (!sRefSet_isEmpty (sets))
10440 sRefSet reported = sRefSet_undefined;
10442 sRefSet_realElements (current->sets, el)
10444 if (sRefSet_isSameNameMember (reported, el))
10446 ; /* don't report again */
10450 if (sRef_isUnconstrained (el))
10455 ("Parameter %d to %s is declared sef, but "
10456 "the argument calls unconstrained function %s "
10457 "(no guarantee it will not modify something): %s",
10458 argno, exprNode_unparse (fcn),
10459 sRef_unconstrainedName (el),
10460 exprNode_unparse (current)),
10461 exprNode_loc (current));
10468 ("Parameter %d to %s is declared sef, but "
10469 "the argument may modify %q: %s",
10470 argno, exprNode_unparse (fcn),
10472 exprNode_unparse (current)),
10473 exprNode_loc (current));
10476 } end_sRefSet_realElements;
10480 transferChecks_passParam (current, ucurrent, isSpec, fcn, argno, totargs);
10481 exprNode_mergeUSs (fcn, current);
10486 checkAnyCall (/*@dependent@*/ exprNode fcn,
10487 /*@dependent@*/ cstring fname,
10490 bool hasMods, sRefSet mods,
10495 int nargs = exprNodeList_size (args);
10500 ** concat all args ud's to f, add each arg sref as a use unless
10501 ** it was specified as "out", in which case it is a def.
10504 uentryList_reset (pn);
10507 ** aliasing checks:
10509 ** if paramn is only or unique, no other arg may alias argn
10512 exprNodeList_elements (args, current)
10516 if (exprNode_isDefined (current))
10518 if ((!uentryList_isUndefined (pn) && !uentryList_isFinished (pn)))
10520 uentry ucurrent = uentryList_current (pn);
10522 if (specialArgs == 0
10523 || (paramno < specialArgs))
10525 checkOneArg (ucurrent, current, fcn, isSpec, paramno, nargs);
10527 if (context_maybeSet (FLG_ALIASUNIQUE))
10529 if (uentry_isOnly (ucurrent)
10530 || uentry_isUnique (ucurrent))
10532 checkUniqueParams (fcn, current, args,
10533 paramno, ucurrent);
10538 else /* uentry is undefined */
10540 if (specialArgs == 0)
10542 exprNode_checkUseParam (current);
10545 exprNode_mergeUSs (fcn, current);
10548 uentryList_advanceSafe (pn);
10549 } end_exprNodeList_elements;
10555 sRefSet_allElements (mods, s)
10558 sRef rb = sRef_getRootBase (s);
10560 if (sRef_isFileOrGlobalScope (rb))
10562 context_usedGlobal (rb);
10565 fb = sRef_fixBaseParam (s, args);
10567 if (!sRef_isMacroParamRef (fb))
10569 if (sRef_isNothing (fb))
10575 if (sRef_isValid (fb))
10577 uentry ue = sRef_getBaseUentry (s);
10579 if (uentry_isValid (ue))
10581 uentry_setLset (ue);
10585 fcn->sets = sRefSet_insert (fcn->sets, fb);
10588 sRef_clearDerivedComplete (s);
10589 } end_sRefSet_allElements;
10595 if (context_hasMods ())
10597 if (context_maybeSet (FLG_MODUNCON))
10601 message ("Undetected modification possible "
10602 "from call to unconstrained function %s: %s",
10604 exprNode_unparse (fcn)),
10605 exprNode_loc (fcn));
10610 if (context_maybeSet (FLG_MODUNCONNOMODS)
10611 && !(context_inIterDef () || context_inIterEnd ()))
10614 (FLG_MODUNCONNOMODS,
10615 message ("Undetected modification possible "
10616 "from call to unconstrained function %s: %s",
10618 exprNode_unparse (fcn)),
10619 exprNode_loc (fcn));
10623 exprNode_checkSetAny (fcn, fname);
10627 void exprNode_checkUseParam (exprNode current)
10629 if (exprNode_isDefined (current))
10631 exprNode_checkUse (current, current->sref, current->loc);
10636 checkNumerics (ctype tr1, ctype tr2, ctype te1, ctype te2,
10637 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10642 if (!ctype_match (tr1, tr2))
10644 if ((ctype_isRealInt (tr1) || ctype_isReal (tr1)) &&
10645 (ctype_isRealInt (tr2) || ctype_isReal (tr2)))
10647 DPRINTF (("No error: [%s] %s / [%s] %s",
10648 exprNode_unparse (e1), ctype_unparse (tr1),
10649 exprNode_unparse (e2), ctype_unparse (tr2)));
10653 (void) gentypeerror
10655 message ("Incompatible types for %s (%s, %s): %s %s %s",
10656 lltok_unparse (op),
10657 ctype_unparse (te1),
10658 ctype_unparse (te2),
10659 exprNode_unparse (e1), lltok_unparse (op),
10660 exprNode_unparse (e2)),
10663 ret = ctype_unknown;
10667 if (ctype_isForceRealNumeric (&tr1) && ctype_isForceRealNumeric (&tr2))
10669 ret = ctype_resolveNumerics (tr1, tr2);
10671 else if (!context_msgStrictOps ())
10673 if (ctype_isPointer (tr1))
10675 if (ctype_isPointer (tr2) && !exprNode_isNullValue (e2))
10679 else if (ctype_isInt (tr2))
10685 ret = ctype_unknown;
10688 else if (ctype_isPointer (tr2))
10690 if (ctype_isPointer (tr1))
10694 else if (ctype_isInt (tr1))
10700 ret = ctype_unknown;
10705 ret = ctype_resolveNumerics (tr1, tr2);
10710 int opid = lltok_getTok (op);
10711 bool comparop = (opid == EQ_OP || opid == NE_OP
10712 || opid == TLT || opid == TGT
10713 || opid == LE_OP || opid == GE_OP);
10715 if (!ctype_isNumeric (tr1) && !ctype_isNumeric (tr2))
10718 && ((ctype_isEnum (tr1) && ctype_isEnum (tr2))
10719 || (ctype_isBool (tr1) && ctype_isBool (tr2))
10720 || (ctype_isChar (tr1) && ctype_isChar (tr2))))
10726 if (ctype_sameName (te1, te2))
10730 message ("Operands of %s are non-numeric (%t): %s %s %s",
10731 lltok_unparse (op), te1,
10732 exprNode_unparse (e1), lltok_unparse (op),
10733 exprNode_unparse (e2)),
10740 message ("Operands of %s are non-numerics (%t, %t): %s %s %s",
10741 lltok_unparse (op), te1, te2,
10742 exprNode_unparse (e1), lltok_unparse (op),
10743 exprNode_unparse (e2)),
10748 else if (!ctype_isNumeric (tr1))
10752 message ("Right operand of %s is non-numeric (%t): %s %s %s",
10753 lltok_unparse (op), te1,
10754 exprNode_unparse (e1), lltok_unparse (op),
10755 exprNode_unparse (e2)),
10760 if (!ctype_isNumeric (tr2))
10764 message ("Left operand of %s is non-numeric (%t): %s %s %s",
10765 lltok_unparse (op), te2,
10766 exprNode_unparse (e1), lltok_unparse (op),
10767 exprNode_unparse (e2)),
10772 ret = ctype_unknown;
10780 abstractOpError (ctype tr1, ctype tr2, lltok op,
10781 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10782 fileloc loc1, fileloc loc2)
10784 if (ctype_isRealAbstract (tr1) && ctype_isRealAbstract (tr2))
10786 if (ctype_match (tr1, tr2))
10790 message ("Operands of %s are abstract type (%t): %s %s %s",
10791 lltok_unparse (op), tr1,
10792 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10799 message ("Operands of %s are abstract types (%t, %t): %s %s %s",
10800 lltok_unparse (op), tr1, tr2,
10801 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10805 else if (ctype_isRealAbstract (tr1))
10809 message ("Left operand of %s is abstract type (%t): %s %s %s",
10810 lltok_unparse (op), tr1,
10811 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10816 if (ctype_isRealAbstract (tr2))
10820 message ("Right operand of %s is abstract type (%t): %s %s %s",
10821 lltok_unparse (op), tr2,
10822 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10831 ** requies e1 and e2 and not error exprNode's.
10835 ** If e1 is a component of an abstract type, and e2 is mutable and client-visible,
10836 ** the rep of the abstract type is exposed.
10838 ** The order is very important:
10840 ** check rep expose (move into check transfer)
10846 ** This isn't really a sensible procedure, but the indententation
10847 ** was getting too deep.
10851 checkOneRepExpose (sRef ysr, sRef base,
10852 /*@notnull@*/ exprNode e1,
10853 /*@notnull@*/ exprNode e2, ctype ct,
10856 if (!(sRef_isOnly (ysr) || sRef_isKeep (ysr)
10857 || sRef_isOwned (ysr)
10858 || sRef_isExposed (ysr)))
10860 if (sRef_isAnyParam (base) && !sRef_isExposed (base)
10861 && !sRef_isObserver (base)) /* evans 2001-07-11: added isObserver */
10864 if (sRef_isIReference (ysr))
10866 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10871 ("Assignment of mutable component of parameter %q "
10872 "to component of abstract "
10873 "type %s exposes rep: %s = %s",
10874 sRef_unparse (base),
10875 ctype_unparse (ct),
10876 exprNode_unparse (e1), exprNode_unparse (e2)),
10884 ("Assignment of mutable component of parameter %q "
10885 "(through alias %q) to component of abstract "
10886 "type %s exposes rep: %s = %s",
10887 sRef_unparse (base),
10888 sRef_unparse (e2->sref),
10889 ctype_unparse (ct),
10890 exprNode_unparse (e1), exprNode_unparse (e2)),
10896 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10900 message ("Assignment of mutable parameter %q "
10901 "to component of abstract type %s "
10902 "exposes rep: %s = %s",
10903 sRef_unparse (base),
10904 ctype_unparse (ct),
10905 exprNode_unparse (e1),
10906 exprNode_unparse (e2)),
10913 message ("Assignment of mutable parameter %q "
10914 "(through alias %q) to "
10915 "component of abstract type %s exposes "
10917 sRef_unparse (base),
10918 sRef_unparse (e2->sref),
10919 ctype_unparse (ct),
10920 exprNode_unparse (e1),
10921 exprNode_unparse (e2)),
10927 if (sRef_isFileOrGlobalScope (s2b))
10929 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10933 message ("Assignment of global %q "
10935 "abstract type %s exposes rep: %s = %s",
10936 sRef_unparse (base),
10937 ctype_unparse (ct),
10938 exprNode_unparse (e1), exprNode_unparse (e2)),
10945 message ("Assignment of global %q (through alias %q) "
10947 "abstract type %s exposes rep: %s = %s",
10948 sRef_unparse (base),
10949 sRef_unparse (e2->sref),
10950 ctype_unparse (ct),
10951 exprNode_unparse (e1), exprNode_unparse (e2)),
10959 doAssign (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2, bool isInit)
10961 DPRINTF (("Do assign: %s <- %s",
10962 exprNode_unparse (e1), exprNode_unparse (e2)));
10963 DPRINTF (("Ctype: %s", ctype_unparse (exprNode_getType (e1))));
10965 if (ctype_isRealFunction (exprNode_getType (e1))
10966 && !ctype_isRealPointer (exprNode_getType (e1)))
10970 message ("Invalid left-hand side of assignment (function type %s): %s",
10971 ctype_unparse (exprNode_getType (e1)),
10972 exprNode_unparse (e1)),
10976 if (context_getFlag (FLG_ASSIGNEXPOSE) && ctype_isMutable (e2->typ))
10978 ctype t2 = exprNode_getType (e2);
10979 sRef sr = sRef_getRootBase (e1->sref);
10980 ctype ct = sRef_getType (sr);
10982 if (ctype_isAbstract (t2)
10983 && !(uentry_isMutableDatatype (usymtab_getTypeEntry (ctype_typeId (t2)))))
10985 /* it is immutable, okay to reference */
10986 goto donerepexpose;
10989 if (ctype_isAbstract (ct) && sRef_isIReference (e1->sref))
10991 sRef s2b = sRef_getRootBase (e2->sref);
10992 sRef s1 = e1->sref;
10993 sRef s1b = sRef_getRootBase (s1);
10996 aliases = usymtab_canAlias (e2->sref);
10998 if (!sRef_similar (s2b, s1b)
10999 && !sRef_isExposed (s1)
11000 && !(sRef_isOnly (s2b) || sRef_isKeep (s2b) || sRef_isExposed (s2b)))
11002 if (sRef_isAnyParam (s2b) && !sRef_isOnly (s2b)
11003 && !sRef_isOwned (s2b) && !sRef_isKeep (s2b)
11004 && !sRef_isExposed (s2b))
11006 if (sRef_isIReference (e2->sref))
11011 ("Assignment of mutable component of parameter %q "
11012 "to component of abstract type %s exposes rep: %s = %s",
11013 sRef_unparse (s2b),
11014 ctype_unparse (ct),
11015 exprNode_unparse (e1), exprNode_unparse (e2)),
11022 message ("Assignment of mutable parameter %q to "
11023 "component of abstract type %s exposes rep: %s = %s",
11024 sRef_unparse (s2b),
11025 ctype_unparse (ct),
11026 exprNode_unparse (e1), exprNode_unparse (e2)),
11031 if (sRef_isFileOrGlobalScope (s2b))
11035 message ("Assignment of global %q to component of "
11036 "abstract type %s exposes rep: %s = %s",
11037 sRef_unparse (s2b),
11038 ctype_unparse (ct),
11039 exprNode_unparse (e1), exprNode_unparse (e2)),
11043 sRefSet_realElements (aliases, ysr)
11045 sRef base = sRef_getRootBase (ysr);
11047 if (sRef_similar (ysr, s2b) || sRef_similar (s1b, base)
11048 || sRef_sameName (base, s1b))
11050 ; /* error already reported or same sref */
11054 checkOneRepExpose (ysr, base, e1, e2, ct, s2b);
11056 } end_sRefSet_realElements;
11058 sRefSet_free (aliases);
11065 ** function variables don't really work...
11068 if (!ctype_isFunction (ctype_realType (e2->typ)))
11072 DPRINTF (("Check init: %s / %s",
11073 exprNode_unparse (e1), exprNode_unparse (e2)));
11074 transferChecks_initialization (e1, e2);
11078 transferChecks_assign (e1, e2);
11083 sRef fref = e2->sref;
11085 sRef_setDefState (e1->sref, sRef_getDefState (fref), e1->loc);
11086 sRef_setNullState (e1->sref, sRef_getNullState (fref), e1->loc);
11088 /* Need to typecheck the annotation on the parameters */
11090 if (ctype_isRealFunction (e1->typ)) {
11091 uentryList e1p = ctype_argsFunction (ctype_realType (e1->typ));
11092 uentryList e2p = ctype_argsFunction (ctype_realType (e2->typ));
11094 if (!uentryList_isMissingParams (e1p)
11095 && !uentryList_isMissingParams (e2p)
11096 && uentryList_size (e1p) > 0) {
11097 if (uentryList_size (e1p) == uentryList_size (e2p)) {
11100 uentryList_elements (e1p, el1) {
11103 el2 = uentryList_getN (e2p, n);
11105 uentry_checkMatchParam (el1, el2, n, e2);
11106 } end_uentryList_elements;
11112 if (exprNode_isStringLiteral (e2))
11114 exprNode_checkStringLiteralLength (exprNode_getType (e1), e2);
11117 if (isInit && sRef_isFileOrGlobalScope (e1->sref))
11123 DPRINTF (("Update aliases: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
11124 updateAliases (e1, e2);
11129 checkMacroParen (exprNode e)
11131 if (exprNode_isError (e) || e->kind == XPR_CAST)
11137 if (sRef_isUnsafe (e->sref) && !exprNode_isInParens (e))
11141 message ("Macro parameter used without parentheses: %s",
11142 exprNode_unparse (e)),
11149 reflectNullTest (/*@notnull@*/ exprNode e, bool isnull)
11153 e->guards = guardSet_addTrueGuard (e->guards, e->sref);
11157 e->guards = guardSet_addFalseGuard (e->guards, e->sref);
11164 ** if e2 is a parameter or global derived location which
11165 ** can be modified (that is, e2 is a mutable abstract type,
11166 ** or a derived pointer), then e1 can alias e2.
11168 ** e1 can alias everything which e2 can alias.
11170 ** Also, if e1 is guarded, remove from guard sets!
11173 static void updateAliases (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2)
11175 if (!context_inProtectVars ())
11178 ** depends on types of e1 and e2
11181 sRef s1 = e1->sref;
11182 sRef s2 = e2->sref;
11183 ctype t1 = exprNode_getType (e1);
11185 /* handle pointer sRefs, record fields, arrays, etc... */
11187 if (!ctype_isRealSU (t1))
11189 DPRINTF (("Copying real! %s", ctype_unparse (t1)));
11190 sRef_copyRealDerivedComplete (s1, s2);
11195 ** Fields should alias
11198 DPRINTF (("Not COPYING!: %s", ctype_unparse (t1)));
11201 if (ctype_isMutable (t1) && sRef_isKnown (s1))
11203 usymtab_clearAlias (s1);
11204 usymtab_addMustAlias (s1, s2);
11205 DPRINTF (("Add must alias: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
11209 DPRINTF (("Not mutable: %s", ctype_unparse (t1)));
11212 if (sRef_possiblyNull (s1) && usymtab_isGuarded (s1))
11214 usymtab_unguard (s1);
11219 exprNode exprNode_updateLocation (/*@returned@*/ exprNode e, /*@temp@*/ fileloc loc)
11221 if (exprNode_isDefined (e))
11223 e->loc = fileloc_update (e->loc, loc);
11227 e = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
11233 static void checkUniqueParams (exprNode fcn,
11234 /*@notnull@*/ exprNode current,
11236 int paramno, uentry ucurrent)
11239 sRef thisref = exprNode_getSref (current);
11242 ** Check if any argument could match this argument.
11245 exprNodeList_elements (args, icurrent)
11249 if (iparamno != paramno)
11251 sRef sr = exprNode_getSref (icurrent);
11253 if (sRef_similarRelaxed (thisref, sr))
11255 if (!sRef_isConst (thisref) && !sRef_isConst (sr))
11260 ("Parameter %d (%s) to function %s is declared %s but "
11261 "is aliased by parameter %d (%s)",
11263 exprNode_unparse (current),
11264 exprNode_unparse (fcn),
11265 alkind_unparse (uentry_getAliasKind (ucurrent)),
11266 iparamno, exprNode_unparse (icurrent)),
11272 sRefSet aliases = usymtab_canAlias (sr);
11274 sRefSet_allElements (aliases, asr)
11276 if (ctype_isUnknown (sRef_getType (thisref)))
11278 sRef_setType (thisref, uentry_getType (ucurrent));
11281 if (sRef_similarRelaxed (thisref, asr))
11283 if (sRef_isExternal (asr))
11285 if (sRef_isLocalState (thisref))
11291 sRef base = sRef_getRootBase (asr);
11293 if (!sRef_similar (sRef_getBase (asr), thisref))
11295 if (sRef_isUnique (base) || sRef_isOnly (base)
11296 || sRef_isKept (base)
11297 || (sRef_isAddress (asr) && sRef_isLocalVar (base))
11298 || (sRef_isAddress (thisref)
11299 && sRef_isLocalVar (sRef_getRootBase (thisref))))
11301 ; /* okay, no error */
11306 (FLG_MAYALIASUNIQUE,
11308 ("Parameter %d (%s) to function %s is declared %s but "
11309 "may be aliased externally by parameter %d (%s)",
11311 exprNode_unparse (current),
11312 exprNode_unparse (fcn),
11313 alkind_unparse (uentry_getAliasKind (ucurrent)),
11314 iparamno, exprNode_unparse (icurrent)),
11325 ("Parameter %d (%s) to function %s is declared %s but "
11326 "is aliased externally by parameter %d (%s) through "
11329 exprNode_unparse (current),
11330 exprNode_unparse (fcn),
11331 alkind_unparse (uentry_getAliasKind (ucurrent)),
11332 iparamno, exprNode_unparse (icurrent),
11333 sRef_unparse (asr)),
11337 } end_sRefSet_allElements;
11338 sRefSet_free (aliases);
11341 } end_exprNodeList_elements;
11344 long exprNode_getLongValue (exprNode e) {
11347 if (exprNode_hasValue (e)
11348 && multiVal_isInt (exprNode_getValue (e)))
11350 value = multiVal_forceInt (exprNode_getValue (e));
11361 /*@observer@*/ fileloc exprNode_getfileloc (exprNode p_e)
11363 if (exprNode_isDefined (p_e) )
11364 return ( p_e->loc );
11366 return fileloc_undefined;
11369 /*@only@*/ fileloc exprNode_getNextSequencePoint (exprNode e)
11372 ** Returns the location of the sequence point following e.
11374 ** Only works for statements (for now).
11377 if (exprNode_isDefined (e) && e->kind == XPR_STMT) {
11378 lltok t = exprData_getUopTok (e->edata);
11379 return fileloc_copy(lltok_getLoc (t));
11381 /* drl possible problem : warning fix
11382 llcontbug (message ("Cannot get next sequence point: %s", exprNode_unparse (e)));
11384 return fileloc_undefined;
11388 exprNode exprNode_createNew(ctype c)
11392 ret = exprNode_createPlain (c);
11397 bool exprNode_isInitBlock (exprNode e)
11399 return (exprNode_isDefined(e) && e->kind == XPR_INITBLOCK);