2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
28 # include <ctype.h> /* for isdigit */
29 # include "lclintMacros.nf"
31 # include "cgrammar.h"
32 # include "cgrammar_tokens.h"
34 # include "exprChecks.h"
35 # include "transferChecks.h"
36 # include "exprNodeSList.h"
38 static bool exprNode_isEmptyStatement (exprNode p_e);
39 static /*@exposed@*/ exprNode exprNode_firstStatement (/*@returned@*/ exprNode p_e);
40 static bool exprNode_isFalseConstant (exprNode p_e) /*@*/ ;
41 static bool exprNode_isBlock (exprNode p_e);
42 static void checkGlobUse (uentry p_glob, bool p_isCall, /*@notnull@*/ exprNode p_e);
43 static void exprNode_addUse (exprNode p_e, /*@exposed@*/ sRef p_s);
44 static bool exprNode_matchArgType (ctype p_ct, exprNode p_e);
45 static exprNode exprNode_fakeCopy (exprNode p_e) /*@*/ ;
46 static exprNode exprNode_statementError (/*@only@*/ exprNode p_e, /*@only@*/ lltok p_t);
47 static bool exprNode_matchTypes (exprNode p_e1, exprNode p_e2);
48 static void checkUniqueParams (exprNode p_fcn,
49 /*@notnull@*/ exprNode p_current, exprNodeList p_args,
50 int p_paramno, uentry p_ucurrent);
51 static void updateAliases (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2);
52 static void abstractOpError (ctype p_tr1, ctype p_tr2, lltok p_op,
53 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2,
54 fileloc p_loc1, fileloc p_loc2);
55 static ctype checkNumerics (ctype p_tr1, ctype p_tr2, ctype p_te1, ctype p_te2,
56 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, lltok p_op);
57 static void doAssign (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, bool p_isInit);
58 static void checkSafeUse (exprNode p_e, /*@exposed@*/ sRef p_s);
59 static void reflectNullTest (/*@notnull@*/ exprNode p_e, bool p_isnull);
60 static void checkMacroParen (exprNode p_e);
61 static exprNodeSList exprNode_flatten (/*@dependent@*/ exprNode p_e);
62 static void exprNode_checkSetAny (exprNode p_e, /*@dependent@*/ cstring p_name);
63 static void exprNode_checkUse (exprNode p_e, /*@exposed@*/ sRef p_s, fileloc p_loc);
64 static void exprNode_mergeUSs (exprNode p_res, exprNode p_other);
65 static void exprNode_mergeCondUSs (exprNode p_res, exprNode p_other1, exprNode p_other2);
66 static /*@only@*/ /*@notnull@*/ exprNode exprNode_fromIdentifierAux (/*@observer@*/ uentry p_c);
67 static void checkAnyCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn,
68 /*@dependent@*/ cstring p_fname,
69 uentryList p_pn, exprNodeList p_args,
70 bool p_hasMods, sRefSet p_mods, bool p_isSpec,
72 static void checkOneArg (uentry p_ucurrent, /*@notnull@*/ exprNode p_current,
73 /*@dependent@*/ exprNode p_fcn, bool p_isSpec, int p_argno, int p_totargs);
75 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn, uentryList p_params, exprNodeList p_args);
77 static /*@only@*/ exprNode exprNode_effect (exprNode p_e)
78 /*@globals internalState@*/ ;
79 static /*@only@*/ cstring exprNode_doUnparse (exprNode p_e);
80 static /*@observer@*/ cstring exprNode_rootVarName (exprNode p_e);
81 static /*@exposed@*/ exprNode
82 exprNode_lastStatement (/*@returned@*/ exprNode p_e);
84 static /*@null@*/ sRef defref = sRef_undefined;
85 static /*@only@*/ exprNode mustExitNode = exprNode_undefined;
87 static int checkArgsReal (uentry p_fcn, /*@dependent@*/ exprNode p_f,
89 exprNodeList p_args, bool p_isIter, exprNode p_ret);
91 static bool inEffect = FALSE;
92 static int nowalloc = 0;
93 static int totalloc = 0;
94 static int maxalloc = 0;
96 static /*@only@*/ uentry regArg;
97 static /*@only@*/ uentry outArg;
98 static /*@only@*/ uentry outStringArg;
99 static /*@exposed@*/ sRef stdinRef;
100 static /*@exposed@*/ sRef stdoutRef;
101 static /*@only@*/ uentry csArg;
102 static /*@only@*/ uentry csOnlyArg;
103 static ctype cstringType;
104 static ctype ctypeType;
105 static ctype filelocType;
106 static bool initMod = FALSE;
108 /*@function void exprNode_swap (sef exprNode, sef exprNode)@*/
110 # define exprNode_swap(e1,e2) do { exprNode m_tmp = (e1); (e1) = (e2); (e2) = m_tmp; } while (FALSE)
113 static void exprNode_defineConstraints(/*@sef@*/ /*@special@*/ /*@notnull@*/ exprNode e)
114 /*@defines e->requiresConstraints, e->ensuresConstraints,
115 e->trueEnsuresConstraints, e->falseEnsuresConstraints @*/
117 e->requiresConstraints = constraintList_makeNew ();
118 e->ensuresConstraints = constraintList_makeNew ();
119 e->trueEnsuresConstraints = constraintList_makeNew ();
120 e->falseEnsuresConstraints = constraintList_makeNew ();
124 ** must occur after library has been read
127 void exprNode_initMod (void)
128 /*@globals undef regArg, undef outArg, undef outStringArg,
129 undef csOnlyArg, undef csArg;
136 cstringType = ctype_unknown;
137 ctypeType = ctype_unknown;
138 filelocType = ctype_unknown;
140 defref = sRef_undefined;
142 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
144 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
147 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
149 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
152 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
154 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
157 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdin")))
159 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdin"));
161 else /* define stdin */
163 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdin"),
165 fileloc_getBuiltin (),
167 uentry_setHasNameError (ue);
168 ue = usymtab_supGlobalEntryReturn (ue);
171 stdinRef = sRef_makePointer (uentry_getSref (ue));
173 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdout")))
175 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdout"));
179 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdout"),
181 fileloc_getBuiltin (),
183 uentry_setHasNameError (ue);
184 ue = usymtab_supGlobalEntryReturn (ue);
187 stdoutRef = sRef_makePointer (uentry_getSref (ue));
189 tmp = idDecl_create (cstring_undefined, qtype_create (ctype_unknown));
191 regArg = uentry_makeParam (tmp, PARAMUNKNOWN);
194 qtype_addQual (qtype_create (ctype_makePointer (ctype_unknown)),
197 outArg = uentry_makeParam (tmp, PARAMUNKNOWN);
199 idDecl_setTyp (tmp, qtype_addQual (qtype_create (ctype_string),
202 outStringArg = uentry_makeParam (tmp, PARAMUNKNOWN);
204 idDecl_setTyp (tmp, qtype_addQual (qtype_addQual (qtype_create (cstringType),
206 qual_createNull ()));
208 csOnlyArg = uentry_makeParam (tmp, PARAMUNKNOWN);
210 idDecl_setTyp (tmp, qtype_addQual (qtype_create (cstringType), qual_createNull ()));
211 csArg = uentry_makeParam (tmp, PARAMUNKNOWN);
217 exprNode_destroyMod (void)
218 /*@globals killed regArg, killed outArg, killed outStringArg,
219 killed mustExitNode, initMod @*/
223 uentry_free (regArg);
224 uentry_free (outArg);
225 uentry_free (outStringArg);
227 exprNode_free (mustExitNode);
234 static void exprNode_resetSref (/*@notnull@*/ exprNode e)
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;
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 (mustExitNode))
463 mustExitNode = exprNode_createPlain (ctype_unknown);
464 mustExitNode->exitCode = XK_MUSTEXIT;
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;
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;
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;
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);
678 llassert (exprNode_isDefined (e));
681 if (e->kind == XPR_CONST)
683 multiVal m = exprNode_getValue (e);
685 if (multiVal_isUnknown (m))
695 exprNode_numLiteral (ctype c, /*@temp@*/ cstring t,
696 /*@only@*/ fileloc loc, long val)
698 exprNode e = exprNode_createLoc (c, loc);
700 e->kind = XPR_NUMLIT;
702 llassert (multiVal_isUndefined (e->val));
703 e->val = multiVal_makeInt (val);
704 e->edata = exprData_makeLiteral (cstring_copy (t));
708 e->sref = sRef_makeUnknown ();
709 sRef_setDefNull (e->sref, e->loc);
712 DPRINTF (("Num lit: %s / %s", exprNode_unparse (e), ctype_unparse (exprNode_getType (e))));
717 exprNode_charLiteral (char c, cstring text, /*@only@*/ fileloc loc)
719 exprNode e = exprNode_createLoc (ctype_char, loc);
721 if (context_getFlag (FLG_CHARINTLITERAL))
723 e->typ = ctype_makeConj (ctype_char, ctype_int);
726 e->kind = XPR_NUMLIT;
727 e->val = multiVal_makeChar (c);
729 e->edata = exprData_makeLiteral (cstring_copy (text));
734 exprNode_floatLiteral (double d, ctype ct, cstring text, /*@only@*/ fileloc loc)
736 exprNode e = exprNode_createLoc (ct, loc);
738 e->kind = XPR_NUMLIT;
739 e->val = multiVal_makeDouble (d);
740 e->edata = exprData_makeLiteral (cstring_copy (text));
744 multiVal exprNode_getValue (exprNode e)
746 while (exprNode_isInParens (e)) {
747 if (e->edata != NULL) {
748 e = exprData_getUopNode (e->edata);
754 if (exprNode_isDefined (e)) {
757 return multiVal_undefined;
762 exprNode_combineLiterals (exprNode e, exprNode rest)
766 /* Both must be string literals. */
768 if (exprNode_isUndefined (rest) || exprNode_isUndefined (e))
770 exprNode_free (rest);
774 if (!exprNode_isStringLiteral (e))
778 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e),
779 exprNode_unparse (rest)),
781 exprNode_free (rest);
785 if (!exprNode_isStringLiteral (rest))
789 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e), exprNode_unparse (rest)),
792 exprNode_free (rest);
796 ns = cstring_concat (multiVal_forceString (exprNode_getValue (e)),
797 multiVal_forceString (exprNode_getValue (rest)));
799 multiVal_free (e->val);
800 exprData_free (e->edata, e->kind);
801 e->edata = exprData_makeLiteral (cstring_copy (ns));
802 e->val = multiVal_makeString (ns);
803 exprNode_free (rest);
808 exprNode_rawStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
810 exprNode e = exprNode_createLoc (ctype_string, loc);
811 int len = cstring_length (t);
813 if (context_getFlag (FLG_STRINGLITERALLEN))
815 if (len > context_getValue (FLG_STRINGLITERALLEN))
817 voptgenerror (FLG_STRINGLITERALLEN,
819 ("String literal length (%d) exceeds maximum "
820 "length (%d): \"%s\"",
822 context_getValue (FLG_STRINGLITERALLEN),
828 e->kind = XPR_STRINGLITERAL;
829 e->val = multiVal_makeString (cstring_copy (t));
830 e->edata = exprData_makeLiteral (t);
831 e->sref = sRef_makeConst (ctype_string);
833 if (context_getFlag (FLG_READONLYSTRINGS))
835 sRef_setAliasKind (e->sref, AK_STATIC, fileloc_undefined);
836 sRef_setExKind (e->sref, XO_OBSERVER, loc);
840 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
843 return (e); /* s released */
847 exprNode_stringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
849 int len = cstring_length (t) - 2;
850 char *ts = cstring_toCharsSafe (t);
851 char *s = cstring_toCharsSafe (cstring_create (len + 1));
853 llassert (*ts == '\"' && *(ts + len + 1) == '\"');
854 strncpy (s, ts+1, size_fromInt (len));
857 return exprNode_rawStringLiteral (cstring_fromCharsO (s), loc);
860 exprNode exprNode_fromUIO (cstring c)
862 fileloc loc = context_getSaveLocation ();
863 exprNode e = exprNode_createPlain (ctype_unknown);
867 if (fileloc_isUndefined (loc))
869 loc = fileloc_copy (g_currentloc);
872 e->loc = loc; /* save loc was mangled */
875 if (usymtab_exists (c))
877 uentry ue = usymtab_lookupEither (c);
879 if (uentry_isDatatype (ue)
880 && uentry_isSpecified (ue))
883 (message ("%q: Specified datatype %s used in code, but not defined. "
884 "(Cannot continue reasonably from this error.)",
885 fileloc_unparse (e->loc), c));
893 llassertprint (!usymtab_exists (c), ("Entry exists: %s", c));
896 ** was supercedeGlobalEntry...is this better?
899 if (!context_inIterEnd ())
901 if (context_inMacro ())
903 if (context_getFlag (FLG_UNRECOG))
907 message ("Unrecognized identifier in macro definition: %s", c), e->loc);
911 flagcode_recordSuppressed (FLG_UNRECOG);
917 (FLG_UNRECOG, message ("Unrecognized identifier: %s", c), e->loc);
921 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
923 /* No alias errors for unrecognized identifiers */
924 sRef_setAliasKind (e->sref, AK_ERROR, loc);
929 exprNode exprNode_createId (/*@observer@*/ uentry c)
931 if (uentry_isValid (c))
933 exprNode e = exprNode_new ();
935 e->typ = uentry_getType (c);
937 if (uentry_isFunction (c)
938 && !sRef_isLocalVar (uentry_getSref (c)))
940 e->sref = sRef_undefined;
944 e->sref = uentry_getSref (c);
947 if (sRef_isStateUnknown (e->sref) && uentry_isNonLocal (c))
949 sRef_setDefined (e->sref, fileloc_undefined);
953 ** yoikes! leaving this out was a heinous bug...that would have been
954 ** caught if i had lclint working first. gag!
957 e->etext = cstring_undefined;
959 if (uentry_isEitherConstant (c))
962 e->val = multiVal_copy (uentry_getConstantValue (c));
967 e->val = multiVal_unknown ();
970 e->edata = exprData_makeId (c);
971 e->loc = context_getSaveLocation ();
973 if (fileloc_isUndefined (e->loc))
975 fileloc_free (e->loc);
976 e->loc = fileloc_copy (g_currentloc);
979 e->guards = guardSet_new ();
980 e->sets = sRefSet_new ();
981 e->msets = sRefSet_new ();
982 e->uses = sRefSet_new ();
984 /*> missing fields, detected by lclint <*/
985 e->exitCode = XK_NEVERESCAPE;
986 e->isJumpPoint = FALSE;
988 e->mustBreak = FALSE;
990 exprNode_defineConstraints(e);
996 return exprNode_createUnknown ();
1000 /*@notnull@*/ exprNode
1001 exprNode_fromIdentifier (/*@observer@*/ uentry c)
1005 if (context_justPopped ()) /* watch out! c could be dead */
1007 uentry ce = usymtab_lookupSafe (LastIdentifier ());
1009 if (uentry_isValid (ce))
1015 llbuglit ("Looks like Aunt Millie forgot to walk to dog again.");
1019 ret = exprNode_fromIdentifierAux (c);
1025 static /*@only@*/ /*@notnull@*/ exprNode
1026 exprNode_fromIdentifierAux (/*@observer@*/ uentry c)
1028 exprNode e = exprNode_createId (c);
1031 uentry_setUsed (c, e->loc);
1033 if (uentry_isVar (c) && sRef_isFileOrGlobalScope (sr))
1035 checkGlobUse (c, FALSE, e);
1042 exprNode_isZero (exprNode e)
1044 if (exprNode_isDefined (e))
1046 multiVal m = exprNode_getValue (e);
1048 if (multiVal_isInt (m))
1050 return (multiVal_forceInt (m) == 0);
1058 exprNode_isNonNegative (exprNode e)
1060 if (exprNode_isDefined (e))
1062 multiVal m = exprNode_getValue (e);
1064 if (multiVal_isInt (m))
1066 return (multiVal_forceInt (m) >= 0);
1074 ** a[x] - uses a but NOT a[]
1075 ** result sref = a[] (set/use in assignment)
1077 ** The syntax x[a] is also legal in C, and has the same
1078 ** semantics. If ind is an array, and arr is an int, flip
1083 exprNode_arrayFetch (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
1086 ** error in arr, error propagates (no new messages)
1087 ** error in ind, assume valid and continue
1090 if (exprNode_isError (e1))
1093 return (exprNode_makeError ());
1099 ctype carr = exprNode_getType (e1);
1100 ctype crarr = ctype_realType (carr);
1103 ** this sets up funny aliasing, that leads to spurious
1104 ** lclint errors. Hence, the i2 comments.
1107 if (!ctype_isRealArray (crarr)
1108 && ctype_isRealNumeric (crarr)
1109 && !exprNode_isError (e2)
1110 && ctype_isRealAP (exprNode_getType (e2))) /* fetch like 3[a] */
1115 carr = exprNode_getType (arr);
1116 crarr = ctype_realType (carr);
1124 if (sRef_possiblyNull (arr->sref))
1126 if (!usymtab_isGuarded (arr->sref))
1128 if (optgenerror (FLG_NULLDEREF,
1129 message ("Index of %s pointer %q: %s",
1130 sRef_nullMessage (arr->sref),
1131 sRef_unparse (arr->sref),
1132 exprNode_unparse (arr)),
1135 sRef_showNullInfo (arr->sref);
1137 /* suppress future messages */
1138 sRef_setNullError (arr->sref);
1143 if (exprNode_isError (ind))
1145 if ((ctype_isArrayPtr (crarr)
1146 && !ctype_isFunction (crarr))
1147 || ctype_isUnknown (carr))
1149 exprNode ret = exprNode_createPartialCopy (arr);
1151 if (ctype_isKnown (carr))
1153 ret->typ = ctype_baseArrayPtr (crarr);
1157 ret->typ = ctype_unknown;
1160 ret->sref = sRef_makeArrayFetch (arr->sref);
1162 ret->kind = XPR_FETCH;
1165 ** Because of funny aliasing (when arr and ind are
1166 ** flipped) spurious errors would be reported here.
1169 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1170 checkSafeUse (ret, arr->sref);
1175 voptgenerror (FLG_TYPE,
1176 message ("Array fetch from non-array (%t): %s[%s]", carr,
1177 exprNode_unparse (e1), exprNode_unparse (e2)),
1179 exprNode_free (arr);
1180 return (exprNode_makeError ());
1185 if (!ctype_isForceRealInt (&(ind->typ)))
1187 ctype rt = ctype_realType (ind->typ);
1189 if (ctype_isChar (rt))
1193 message ("Array fetch using non-integer, %t: %s[%s]",
1195 exprNode_unparse (e1), exprNode_unparse (e2)),
1198 else if (ctype_isEnum (rt))
1202 message ("Array fetch using non-integer, %t: %s[%s]",
1204 exprNode_unparse (e1), exprNode_unparse (e2)),
1211 message ("Array fetch using non-integer, %t: %s[%s]",
1213 exprNode_unparse (e1), exprNode_unparse (e2)),
1217 multiVal_free (ind->val);
1218 ind->val = multiVal_unknown ();
1221 if (ctype_isArrayPtr (crarr) && !ctype_isFunction (crarr))
1223 exprNode ret = exprNode_createSemiCopy (arr);
1224 multiVal m = exprNode_getValue (ind);
1226 ret->typ = ctype_baseArrayPtr (crarr);
1227 ret->kind = XPR_FETCH;
1229 if (multiVal_isInt (m))
1231 int i = (int) multiVal_forceInt (m);
1233 if (sRef_isValid (arr->sref)) {
1234 ret->sref = sRef_makeArrayFetchKnown (arr->sref, i);
1236 ret->sref = sRef_undefined;
1241 ret->sref = sRef_makeArrayFetch (arr->sref);
1244 ret->sets = sRefSet_realNewUnion (arr->sets, ind->sets);
1245 ret->msets = sRefSet_realNewUnion (arr->msets, ind->msets);
1246 ret->uses = sRefSet_realNewUnion (arr->uses, ind->uses);
1248 /* (see comment on spurious errors above) */
1249 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1251 exprNode_checkUse (ret, ind->sref, ind->loc);
1252 exprNode_checkUse (ret, arr->sref, arr->loc);
1258 if (ctype_isUnknown (carr))
1260 exprNode ret = exprNode_createPartialCopy (arr);
1262 ret->kind = XPR_FETCH;
1263 ret->typ = ctype_unknown;
1264 ret->sets = sRefSet_union (ret->sets, ind->sets);
1265 ret->msets = sRefSet_union (ret->msets, ind->msets);
1266 ret->uses = sRefSet_union (ret->uses, ind->uses);
1268 /* (see comment on spurious errors above) */
1269 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1271 exprNode_checkUse (ret, ind->sref, ind->loc);
1272 exprNode_checkUse (ret, arr->sref, arr->loc);
1279 message ("Array fetch from non-array (%t): %s[%s]", carr,
1280 exprNode_unparse (e1), exprNode_unparse (e2)),
1283 exprNode_free (arr);
1284 exprNode_free (ind);
1286 return (exprNode_makeError ());
1296 checkArgs (uentry fcn, /*@dependent@*/ exprNode f, ctype t,
1297 exprNodeList args, exprNode ret)
1299 return (checkArgsReal (fcn, f, ctype_argsFunction (t), args, FALSE, ret));
1303 ** checkPrintfArgs --- checks arguments for printf-like functions
1304 ** Arguments before ... have already been checked.
1305 ** The argument before the ... is a char *.
1306 ** argno is the format string argument.
1310 checkPrintfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1311 exprNodeList args, exprNode ret, int argno)
1314 ** the last argument before the elips is the format string
1319 int nargs = exprNodeList_size (args);
1320 uentryList params = uentry_getParams (fcn);
1324 ** These should be ensured by checkSpecialFunction
1327 llassert (uentryList_size (params) == argno + 1);
1328 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1330 a = exprNodeList_getN (args, argno - 1);
1331 formatloc = fileloc_copy (exprNode_loc (a));
1333 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1334 && exprNode_knownStringValue (a))
1336 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1337 char *code = format;
1340 nargs = exprNodeList_size (args);
1342 while ((code = strchr (code, '%')) != NULL)
1344 char *origcode = code;
1345 cstring codetext = cstring_newEmpty ();
1346 char key = *(++code);
1347 ctype modtype = ctype_int;
1348 bool modified = FALSE;
1350 fileloc_addColumn (formatloc, code - ocode);
1352 codetext = cstring_appendChar (codetext, key);
1355 while (isFlagKey (key))
1358 codetext = cstring_appendChar (codetext, key);
1359 fileloc_incColumn (formatloc);
1362 if (key == 'm') /* skipped in syslog */
1367 /* ignore field width */
1368 while (isdigit ((int) key) != 0)
1371 codetext = cstring_appendChar (codetext, key);
1372 fileloc_incColumn (formatloc);
1375 /* ignore precision */
1379 codetext = cstring_appendChar (codetext, key);
1380 fileloc_incColumn (formatloc);
1383 ** In printf, '*' means: read the next arg as an int for the
1384 ** field width. This seems to be missing from my copy of the
1385 ** standard x3.159-1989. Setion 4.9.6.1 refers to * (described
1386 ** later) but never does.
1391 ; /* don't do anything --- handle later */
1395 while (isdigit ((int) key) != 0)
1398 codetext = cstring_appendChar (codetext, key);
1399 fileloc_incColumn (formatloc);
1406 modtype = ctype_sint; /* short */
1408 codetext = cstring_appendChar (codetext, key);
1409 fileloc_incColumn (formatloc);
1411 else if (key == 'l' || key == 'L')
1413 modtype = ctype_lint; /* long */
1415 codetext = cstring_appendChar (codetext, key);
1416 fileloc_incColumn (formatloc);
1418 if (key == 'l' || key == 'L') {
1419 modtype = ctype_llint; /* long long */
1421 codetext = cstring_appendChar (codetext, key);
1422 fileloc_incColumn (formatloc);
1430 /* now, key = type of conversion to apply */
1432 fileloc_incColumn (formatloc);
1440 message ("No argument corresponding to %q format "
1441 "code %d (%%%s): \"%s\"",
1442 uentry_getName (fcn),
1444 cstring_fromChars (format)),
1447 if (fileloc_isDefined (formatloc)
1448 && context_getFlag (FLG_SHOWCOL))
1450 llgenindentmsg (cstring_makeLiteral ("Corresponding format code"),
1458 a = exprNodeList_getN (args, i);
1461 if (!exprNode_isError (a))
1467 case '*': /* int argument for fieldwidth */
1468 expecttype = ctype_int;
1469 *(--code) = '%'; /* convert it for next code */
1470 fileloc_subColumn (formatloc, 1);
1471 /*@switchbreak@*/ break;
1474 expecttype = ctype_combine (ctype_uint, modtype);
1475 /*@switchbreak@*/ break;
1477 case 'i': /* int argument */
1479 expecttype = ctype_combine (ctype_int, modtype);
1480 /*@switchbreak@*/ break;
1482 case 'x': /* unsigned int */
1484 expecttype = ctype_combine (ctype_uint, modtype);
1486 /*@switchbreak@*/ break;
1492 case 'f': /* double */
1493 expecttype = ctype_combine (ctype_double, modtype);
1494 /*@switchbreak@*/ break;
1496 case 'c': /* int converted to char (check its a char?) */
1497 expecttype = ctype_makeConj (ctype_char, ctype_uchar);
1498 /*@switchbreak@*/ break;
1500 case 's': /* string */
1501 expecttype = ctype_string;
1502 /*@switchbreak@*/ break;
1505 while (((key = *(++code)) != ']')
1508 codetext = cstring_appendChar (codetext, key);
1509 fileloc_incColumn (formatloc);
1515 (message ("Bad character set format: %s",
1516 cstring_fromChars (origcode)));
1519 expecttype = ctype_string;
1520 /*@switchbreak@*/ break;
1522 case 'p': /* pointer */
1523 expecttype = ctype_makePointer (ctype_void);
1524 uentry_setDefState (regArg, SS_RELDEF); /* need not be defined */
1525 sRef_setPosNull (uentry_getSref (regArg), fileloc_undefined); /* could be null */
1526 /*@switchbreak@*/ break;
1528 case 'n': /* pointer to int, modified by call! */
1529 expecttype = ctype_combine (ctype_makePointer (ctype_int), modtype);
1531 uentry_setDefState (regArg, SS_ALLOCATED); /* corresponds to out */
1532 /*@switchbreak@*/ break;
1534 case 'm': /* in a syslog, it doesn't consume an argument */
1535 /* should check we're really doing syslog */
1537 /*@switchbreak@*/ break;
1541 expecttype = ctype_unknown;
1545 message ("Unrecognized format code: %s",
1546 cstring_fromChars (origcode)),
1547 fileloc_isDefined (formatloc)
1548 ? formatloc : g_currentloc);
1550 /*@switchbreak@*/ break;
1553 if (!(exprNode_matchArgType (expecttype, a)))
1555 if (ctype_isVoidPointer (expecttype)
1556 && ctype_isRealAbstract (a->typ)
1557 && (context_getFlag (FLG_ABSTVOIDP)))
1563 if (llgenformattypeerror
1564 (expecttype, exprNode_undefined,
1566 message ("Format argument %d to %q (%%%s) expects "
1569 uentry_getName (fcn),
1572 a->typ, exprNode_unparse (a)),
1575 if (fileloc_isDefined (formatloc)
1576 && context_getFlag (FLG_SHOWCOL))
1579 (cstring_makeLiteral
1580 ("Corresponding format code"),
1587 uentry_setType (regArg, expecttype);
1588 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
1590 if (ctype_equal (expecttype, ctype_string))
1592 exprNode_checkUse (a, sRef_makePointer (a->sref), a->loc);
1595 uentry_setType (regArg, ctype_unknown);
1596 uentry_fixupSref (regArg);
1600 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1611 cstring_free (codetext);
1616 voptgenerror (FLG_TYPE,
1617 message ("Format string for %q has %d arg%&, given %d",
1618 uentry_getName (fcn), i - argno, nargs - argno),
1624 /* no checking possible for compile-time unknown format strings */
1625 if (exprNode_isDefined (a))
1629 message ("Format string parameter to %s is not a compile-time constant: %s",
1630 exprNode_unparse (f),
1631 exprNode_unparse (a)),
1636 fileloc_free (formatloc);
1640 checkScanfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1641 exprNodeList args, exprNode ret, int argno)
1645 int nargs = exprNodeList_size (args);
1646 uentryList params = uentry_getParams (fcn);
1650 ** These should be ensured by checkSpecialFunction
1653 llassert (uentryList_size (params) == argno + 1);
1654 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1656 a = exprNodeList_getN (args, argno - 1);
1657 formatloc = fileloc_copy (exprNode_loc (a));
1659 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1660 && exprNode_knownStringValue (a))
1662 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1663 char *code = format;
1666 nargs = exprNodeList_size (args);
1668 while ((code = strchr (code, '%')) != NULL)
1670 char *origcode = code;
1671 char key = *(++code);
1672 cstring codetext = cstring_newEmpty ();
1673 ctype modtype = ctype_int;
1674 char modifier = '\0';
1675 bool modified = TRUE;
1676 bool ignore = FALSE;
1678 codetext = cstring_appendChar (codetext, key);
1679 fileloc_addColumn (formatloc, code - ocode);
1682 ** this is based on ANSI standard library description of fscanf
1683 ** (from ANSI standard X3.159-1989, 4.9.6.1)
1686 /* '*' suppresses assignment (does not need match argument) */
1691 codetext = cstring_appendChar (codetext, key);
1694 fileloc_incColumn (formatloc);
1697 /* ignore field width */
1698 while (isdigit ((int) key) != 0)
1701 codetext = cstring_appendChar (codetext, key);
1702 fileloc_incColumn (formatloc);
1707 modtype = ctype_sint; /* short */
1709 codetext = cstring_appendChar (codetext, key);
1710 fileloc_incColumn (formatloc);
1712 else if (key == 'l' || key == 'L')
1714 modtype = ctype_lint; /* long */
1718 codetext = cstring_appendChar (codetext, key);
1720 fileloc_incColumn (formatloc);
1722 if (key == 'l' || key == 'L') {
1723 modtype = ctype_llint; /* long long */
1725 codetext = cstring_appendChar (codetext, key);
1726 fileloc_incColumn (formatloc);
1734 /* now, key = type of conversion to apply */
1736 fileloc_incColumn (formatloc);
1750 message ("No argument corresponding to %q format "
1751 "code %d (%%%s): \"%s\"",
1752 uentry_getName (fcn),
1754 cstring_fromChars (format)),
1757 if (fileloc_isDefined (formatloc)
1758 && context_getFlag (FLG_SHOWCOL))
1761 (cstring_makeLiteral ("Corresponding format code"),
1769 a = exprNodeList_getN (args, i);
1772 if (!exprNode_isError (a))
1778 case '*': /* int argument for fieldwidth */
1779 expecttype = ctype_makePointer (ctype_int);
1780 *(--code) = '%'; /* convert it for next code */
1781 fileloc_subColumn (formatloc, 1);
1782 /*@switchbreak@*/ break;
1785 expecttype = ctype_makePointer (ctype_combine (ctype_uint, modtype));
1786 /*@switchbreak@*/ break;
1791 case 'X': /* unsigned int */
1792 expecttype = ctype_makePointer (ctype_combine (ctype_int, modtype));
1793 /*@switchbreak@*/ break;
1800 /* printf is double, scanf is float! */
1802 if (modifier == 'l')
1804 expecttype = ctype_makePointer (ctype_double);
1806 else if (modifier == 'L')
1808 expecttype = ctype_makePointer (ctype_ldouble);
1812 llassert (modifier == '\0');
1813 expecttype = ctype_makePointer (ctype_float);
1815 /*@switchbreak@*/ break;
1817 case 'c': /* int converted to char (check its a char?) */
1818 expecttype = ctype_makePointer (ctype_makeConj (ctype_char, ctype_uchar));
1819 /*@switchbreak@*/ break;
1821 case 's': /* string */
1822 expecttype = ctype_string;
1823 /*@switchbreak@*/ break;
1827 while (((key = *(++code)) != ']')
1830 codetext = cstring_appendChar (codetext, key);
1831 fileloc_incColumn (formatloc);
1837 (message ("Bad character set format: %s",
1838 cstring_fromChars (origcode)));
1841 expecttype = ctype_string;
1842 /*@switchbreak@*/ break;
1845 case 'p': /* pointer */
1848 message ("Format code should not be used in scanf: %s",
1849 cstring_fromChars (origcode)),
1850 fileloc_isDefined (formatloc)
1851 ? formatloc : g_currentloc);
1853 expecttype = ctype_unknown;
1854 /*@switchbreak@*/ break;
1856 case 'n': /* pointer to int, modified by call! */
1857 expecttype = ctype_makePointer (ctype_int);
1858 /*@switchbreak@*/ break;
1861 expecttype = ctype_unknown;
1865 message ("Unrecognized format code: %s",
1866 cstring_fromChars (origcode)),
1867 fileloc_isDefined (formatloc)
1868 ? formatloc : g_currentloc);
1870 /*@switchbreak@*/ break;
1873 if (!(exprNode_matchArgType (expecttype, a)))
1875 if (ctype_isVoidPointer (expecttype)
1876 && ctype_isRealAbstract (a->typ)
1877 && (context_getFlag (FLG_ABSTVOIDP)))
1883 if (llgenformattypeerror
1884 (expecttype, exprNode_undefined,
1886 message ("Format argument %d to %q (%%%s) expects "
1889 uentry_getName (fcn),
1890 codetext, expecttype,
1891 a->typ, exprNode_unparse (a)),
1894 if (fileloc_isDefined (formatloc)
1895 && context_getFlag (FLG_SHOWCOL))
1898 (cstring_makeLiteral
1899 ("Corresponding format code"),
1906 uentry_setType (outArg, expecttype);
1907 checkOneArg (outArg, a, f, FALSE, i+1, nargs);
1908 uentry_setType (outArg, ctype_unknown);
1909 uentry_fixupSref (outArg);
1913 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1918 /* a->sref = defref; */
1925 cstring_free (codetext);
1930 voptgenerror (FLG_TYPE,
1931 message ("Format string for %q has %d arg%&, given %d",
1932 uentry_getName (fcn), i - argno, nargs - argno),
1938 /* no checking possible for compile-time unknown format strings */
1941 fileloc_free (formatloc);
1945 checkMessageArgs (/*@notnull@*/ /*@dependent@*/ exprNode f,
1948 /*@unused@*/ int argno)
1951 ** the last argument before the elips is the format string
1954 int nargs = exprNodeList_size (args);
1959 a = exprNodeList_getN (args, argno - 1);
1960 formatloc = fileloc_copy (exprNode_loc (a));
1962 if (ctype_isUnknown (cstringType)) {
1963 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
1965 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
1969 if (ctype_isUnknown (ctypeType)) {
1970 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
1972 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
1976 if (ctype_isUnknown (filelocType)) {
1977 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
1979 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
1983 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1984 && exprNode_knownStringValue (a))
1986 cstring format = multiVal_forceString (exprNode_getValue (a));
1987 char *code = cstring_toCharsSafe (format);
1990 nargs = exprNodeList_size (args);
1992 while ((code = strchr (code, '%')) != NULL)
1994 char *origcode = code;
1995 char key = *(++code);
1996 cstring codetext = cstring_newEmpty ();
1997 bool isOnly = FALSE;
1999 codetext = cstring_appendChar (codetext, key);
2001 fileloc_addColumn (formatloc, code - ocode);
2003 while (key >= '0' && key <= '9')
2006 codetext = cstring_appendChar (codetext, key);
2007 fileloc_incColumn (formatloc);
2011 fileloc_incColumn (formatloc);
2015 if (key == '&') /* plural marker */
2024 message ("Message missing format arg %d (%%%s): \"%s\"",
2025 i + 1, codetext, format),
2031 a = exprNodeList_getN (args, i);
2035 if (!exprNode_isError (a))
2039 /*@-loopswitchbreak@*/
2045 expecttype = ctype_char; break;
2047 expecttype = cstringType; break;
2049 expecttype = cstringType; isOnly = TRUE; break;
2051 expecttype = cstringType; isOnly = TRUE; break;
2052 case 'd': expecttype = ctype_int; break;
2053 case 'u': expecttype = ctype_uint; break;
2054 case 'w': expecttype = ctype_ulint; break;
2055 case 'f': expecttype = ctype_float; break;
2056 case 'b': expecttype = ctype_bool; break;
2057 case 't': expecttype = ctypeType; break;
2058 case 'l': expecttype = filelocType; break;
2059 case '&': /* a wee bit of a hack methinks */
2060 expecttype = ctype_int;
2062 case 'r': expecttype = ctype_bool; break;
2064 expecttype = ctype_unknown;
2067 message ("Unrecognized format code: %s",
2068 cstring_fromChars (origcode)),
2069 fileloc_isDefined (formatloc)
2070 ? formatloc : g_currentloc);
2073 /*@=loopswitchbreak@*/
2075 if (!(exprNode_matchArgType (expecttype, a)))
2077 if (ctype_isVoidPointer (expecttype)
2078 && ctype_isRealAbstract (a->typ)
2079 && (context_getFlag (FLG_ABSTVOIDP)))
2085 if (llgenformattypeerror
2086 (expecttype, exprNode_undefined,
2088 message ("Format argument %d to %q (%%%s) expects "
2091 uentry_getName (fcn),
2092 codetext, expecttype,
2093 a->typ, exprNode_unparse (a)),
2096 if (fileloc_isDefined (formatloc)
2097 && context_getFlag (FLG_SHOWCOL))
2100 (cstring_makeLiteral
2101 ("Corresponding format code"),
2108 if (ctype_equal (expecttype, cstringType))
2112 checkOneArg (csOnlyArg, a, f, FALSE, i+1, nargs);
2113 uentry_fixupSref (csOnlyArg);
2117 checkOneArg (csArg, a, f, FALSE, i+1, nargs);
2118 uentry_fixupSref (csArg);
2123 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
2124 uentry_fixupSref (regArg);
2130 cstring_free (codetext);
2135 voptgenerror (FLG_TYPE,
2136 message ("Format string for %q has %d arg%&, given %d",
2137 uentry_getName (fcn), i - argno, nargs -argno),
2143 /* no checking possible for compile-time unknown format strings */
2146 fileloc_free (formatloc);
2150 checkExpressionDefinedAux (/*@notnull@*/ exprNode e1,
2151 /*@notnull@*/ exprNode e2,
2157 bool hadUncon = FALSE;
2159 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e1->sref)) &&
2160 sRefSet_hasUnconstrained (sets2))
2163 (FLG_EVALORDERUNCON,
2165 ("Expression may have undefined behavior (%q used in right operand "
2166 "may set global variable %q used in left operand): %s %s %s",
2167 sRefSet_unparseUnconstrained (sets2),
2168 sRef_unparse (sRef_getRootBase (e1->sref)),
2169 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2173 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e2->sref)) &&
2174 sRefSet_hasUnconstrained (sets1))
2177 (FLG_EVALORDERUNCON,
2179 ("Expression has undefined behavior (%q used in left operand "
2180 "may set global variable %q used in right operand): %s %s %s",
2181 sRefSet_unparseUnconstrained (sets1),
2182 sRef_unparse (e2->sref),
2183 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2187 sRefSet_realElements (e1->uses, sr)
2189 if (sRef_isMeaningful (sr) && sRefSet_member (sets2, sr))
2194 ("Expression has undefined behavior (left operand uses %q, "
2195 "modified by right operand): %s %s %s",
2197 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2200 } end_sRefSet_realElements;
2202 sRefSet_realElements (sets1, sr)
2204 if (sRef_isMeaningful (sr))
2206 if (sRef_same (sr, e2->sref))
2211 ("Expression has undefined behavior (value of right operand "
2212 "modified by left operand): %s %s %s",
2213 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2216 else if (sRefSet_member (e2->uses, sr))
2221 ("Expression has undefined behavior (left operand modifies %q, "
2222 "used by right operand): %s %s %s",
2224 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2229 if (sRefSet_member (sets2, sr))
2231 if (sRef_isUnconstrained (sr))
2239 hadUncon = optgenerror
2240 (FLG_EVALORDERUNCON,
2242 ("Expression may have undefined behavior. Left operand "
2243 "calls %q; right operand calls %q. The unconstrained "
2244 "functions may modify global state used by "
2245 "the other operand): %s %s %s",
2246 sRefSet_unparseUnconstrained (sets1),
2247 sRefSet_unparseUnconstrained (sets2),
2248 exprNode_unparse (e1), lltok_unparse (op),
2249 exprNode_unparse (e2)),
2258 ("Expression has undefined behavior (both "
2259 "operands modify %q): %s %s %s",
2261 exprNode_unparse (e1),
2262 lltok_unparse (op), exprNode_unparse (e2)),
2268 } end_sRefSet_realElements;
2271 static void checkExpressionDefined (exprNode e1, exprNode e2, lltok op)
2273 bool hasError = FALSE;
2275 if (exprNode_isError (e1) || exprNode_isError (e2))
2280 if (sRefSet_member (e2->sets, e1->sref))
2282 if (e2->kind == XPR_CALL)
2288 hasError = optgenerror
2290 message ("Expression has undefined behavior "
2291 "(value of left operand %s is modified "
2292 "by right operand %s): %s %s %s",
2293 exprNode_unparse (e1),
2294 exprNode_unparse (e2),
2295 exprNode_unparse (e1), lltok_unparse (op),
2296 exprNode_unparse (e2)),
2301 if (context_getFlag (FLG_EVALORDERUNCON))
2303 if (sRefSet_member (e2->msets, e1->sref))
2305 if (e2->kind == XPR_CALL)
2311 hasError = optgenerror
2314 ("Expression has undefined behavior (value of left "
2315 "operand may be modified by right operand): %s %s %s",
2316 exprNode_unparse (e1), lltok_unparse (op),
2317 exprNode_unparse (e2)),
2325 checkExpressionDefinedAux (e1, e2, e1->sets, e2->sets, op, FLG_EVALORDER);
2327 if (context_maybeSet (FLG_EVALORDERUNCON))
2329 checkExpressionDefinedAux (e1, e2, e1->msets,
2330 e2->msets, op, FLG_EVALORDERUNCON);
2335 static void checkSequencing (exprNode p_f, exprNodeList p_args);
2338 checkArgsReal (uentry fcn, /*@dependent@*/ exprNode f, uentryList cl,
2339 exprNodeList args, bool isIter, exprNode ret)
2343 if (!exprNode_isError (f))
2345 if (!uentryList_isMissingParams (cl))
2347 int nargs = exprNodeList_size (args);
2348 int expectargs = uentryList_size (cl);
2352 if (expectargs == 0)
2360 message ("Iter %q invoked with %d args, "
2362 uentry_getName (fcn),
2370 message ("Function %s called with %d args, "
2372 exprNode_unparse (f), nargs),
2379 last = uentry_getType (uentryList_getN (cl, expectargs - 1));
2381 exprNodeList_reset (args);
2383 uentryList_elements (cl, current)
2385 ctype ct = uentry_getType (current);
2388 if (ctype_isElips (ct))
2391 ** do special checking for printf/scanf library functions
2393 ** this is kludgey code, just for handling the special case
2397 if (uentry_isPrintfLike (fcn))
2399 checkPrintfArgs (f, fcn, args, ret, i);
2402 else if (uentry_isScanfLike (fcn))
2404 checkScanfArgs (f, fcn, args, ret, i);
2407 else if (uentry_isMessageLike (fcn))
2409 checkMessageArgs (f, fcn, args, i);
2414 llassert (!uentry_isSpecialFunction (fcn));
2417 nargs = expectargs; /* avoid errors */
2422 if (i >= nargs) break;
2424 a = exprNodeList_current (args);
2425 exprNodeList_advance (args);
2429 if (exprNode_isError (a))
2436 probably necessary? I'm not sure about this one
2437 checkMacroParen (a);
2440 f->guards = guardSet_union (f->guards, a->guards);
2442 DPRINTF (("match arg: %s / %s", ctype_unparse (ct), ctype_unparse (a->typ)));
2444 if (!(exprNode_matchArgType (ct, a)))
2446 DPRINTF (("Args mismatch!"));
2448 if (ctype_isVoidPointer (ct)
2449 && (ctype_isPointer (a->typ)
2450 && (ctype_isRealAbstract (ctype_baseArrayPtr (a->typ)))))
2455 ("Pointer to abstract type (%t) used "
2457 "(arg %d to %q): %s",
2459 uentry_getName (fcn),
2460 exprNode_unparse (a)),
2468 (ct, exprNode_undefined,
2471 ("Iter %q expects arg %d to "
2472 "be %t gets %t: %s",
2473 uentry_getName (fcn),
2474 i, ct, a->typ, exprNode_unparse (a)),
2485 ("Function %q expects arg %d to be %t gets %t: %s",
2486 uentry_getName (fcn),
2487 i, ct, a->typ, exprNode_unparse (a)),
2490 DPRINTF (("Types: %s / %s",
2492 ctype_unparse (a->typ)));
2496 ** Clear null marker for abstract types.
2497 ** (It is not revealed, so suppress future messages.)
2500 if (ctype_isAbstract (a->typ))
2502 sRef_setNullUnknown (exprNode_getSref (a), a->loc);
2509 } end_uentryList_elements ;
2512 if (expectargs != nargs) /* note: not != since we may have ... */
2514 if (ctype_isElips (last))
2518 message ("Function %s called with %d args, expects at least %d",
2519 exprNode_unparse (f),
2520 nargs, expectargs - 1),
2529 message ("Iter %q invoked with %d args, expects %d",
2530 uentry_getName (fcn), nargs, expectargs),
2537 message ("Function %s called with %d args, expects %d",
2538 exprNode_unparse (f),
2551 ** Check for undefined code sequences in function arguments:
2553 ** one parameter sets something used by another parameter
2554 ** one parameter sets something set by another parameter
2558 checkSequencingOne (exprNode f, exprNodeList args,
2559 /*@notnull@*/ exprNode el, int argno)
2562 ** Do second loop, iff +undefunspec
2566 int numloops = context_maybeSet (FLG_EVALORDERUNCON) ? 2 : 1;
2568 for (checkloop = 0; checkloop < numloops; checkloop++)
2574 thissets = el->sets;
2578 llassert (checkloop == 1);
2579 thissets = el->msets;
2582 sRefSet_realElements (thissets, thisset)
2586 /*@access exprNodeList@*/
2587 for (j = 0; j < args->nelements; j++)
2589 exprNode jl = args->elements[j];
2590 int thisargno = j + 1;
2592 if (thisargno != argno && exprNode_isDefined (jl))
2594 sRefSet otheruses = jl->uses;
2596 if (sRef_isFileOrGlobalScope (sRef_getRootBase (jl->sref)) &&
2597 sRefSet_hasUnconstrained (thissets))
2600 (FLG_EVALORDERUNCON,
2603 ("%q used in argument %d may set "
2604 "global variable %q used by argument %d: %s(%q)",
2605 cstring_capitalizeFree (sRefSet_unparseUnconstrained (thissets)),
2608 sRef_unparse (sRef_getRootBase (jl->sref)),
2610 exprNode_unparse (f), exprNodeList_unparse (args)),
2614 if (sRefSet_member (otheruses, thisset))
2616 if (sRef_isUnconstrained (thisset))
2619 (FLG_EVALORDERUNCON,
2621 ("Unconstrained functions used in arguments %d (%q) "
2622 "and %d (%s) may modify "
2623 "or use global state in undefined way: %s(%q)",
2625 sRefSet_unparseUnconstrainedPlain (otheruses),
2627 sRef_unconstrainedName (thisset),
2628 exprNode_unparse (f),
2629 exprNodeList_unparse (args)),
2637 ("Argument %d modifies %q, used by argument %d "
2638 "(order of evaluation of actual parameters is "
2639 "undefined): %s(%q)",
2640 argno, sRef_unparse (thisset), thisargno,
2641 exprNode_unparse (f), exprNodeList_unparse (args)),
2647 sRefSet othersets = jl->sets;
2649 if (sRefSet_member (othersets, thisset))
2651 if (sRef_isUnconstrained (thisset))
2654 (FLG_EVALORDERUNCON,
2656 ("Unconstrained functions used in "
2657 "arguments %d (%q) and %d (%s) may modify "
2658 "or use global state in undefined way: %s(%q)",
2660 sRefSet_unparseUnconstrainedPlain (othersets),
2662 sRef_unconstrainedName (thisset),
2663 exprNode_unparse (f), exprNodeList_unparse (args)),
2671 ("Argument %d modifies %q, set by argument %d (order of"
2672 " evaluation of actual parameters is undefined): %s(%q)",
2673 argno, sRef_unparse (thisset), thisargno,
2674 exprNode_unparse (f), exprNodeList_unparse (args)),
2681 /*@noaccess exprNodeList@*/
2682 } end_sRefSet_realElements;
2687 checkSequencing (exprNode f, exprNodeList args)
2689 if (exprNodeList_size (args) > 1)
2694 /*@access exprNodeList*/
2696 for (i = 0; i < args->nelements; i++)
2698 el = args->elements[i];
2700 if (!exprNode_isError (el))
2702 checkSequencingOne (f, args, el, i + 1);
2705 /*@noaccess exprNodeList*/
2710 ** requires le = exprNode_getUentry (f)
2714 checkGlobMods (/*@notnull@*/ /*@dependent@*/ exprNode f,
2715 uentry le, exprNodeList args,
2716 /*@notnull@*/ exprNode ret, int specialArgs)
2718 bool isSpec = FALSE;
2719 bool hasMods = FALSE;
2721 globSet usesGlobs = globSet_undefined;
2722 sRefSet mods = sRefSet_undefined;
2723 bool freshMods = FALSE;
2724 uentryList params = uentryList_undefined;
2726 DPRINTF (("Check glob mods: %s", exprNode_unparse (ret)));
2729 ** check globals and modifies
2734 if (!uentry_isValid (le))
2736 ctype fr = ctype_realType (f->typ);
2738 if (ctype_isFunction (fr))
2740 params = ctype_argsFunction (fr);
2744 params = uentryList_missingParams;
2747 if (!context_getFlag (FLG_MODNOMODS)
2748 && !context_getFlag (FLG_GLOBUNSPEC))
2750 checkUnspecCall (f, params, args);
2756 fname = uentry_rawName (le);
2760 if (uentry_isFunction (le))
2762 params = uentry_getParams (le);
2763 mods = uentry_getMods (le);
2764 hasMods = uentry_hasMods (le);
2765 usesGlobs = uentry_getGlobs (le);
2766 isSpec = uentry_isSpecified (le);
2768 else /* not a function */
2770 ctype ct = ctype_realType (uentry_getType (le));
2772 llassertprint (uentry_isVar (le) && ctype_isFunction (ct),
2773 ("checkModGlobs: uentry not a function: %s",
2774 uentry_unparse (le)));
2776 params = ctype_argsFunction (ct);
2777 return; /*@32 ! remove this? */
2786 globSet_allElements (usesGlobs, el)
2788 if (sRef_isValid (el))
2790 if (sRef_isInternalState (el) || sRef_isSystemState (el))
2792 context_usedGlobal (el);
2793 exprNode_checkUse (f, el, f->loc);
2795 if (context_checkInternalUse ())
2797 if (!context_globAccess (el))
2799 if (sRef_isSystemState (el)
2800 && !context_getFlag (FLG_MODFILESYSTEM))
2809 ("Called procedure %s may access %q, but "
2810 "globals list does not include globals %s",
2811 exprNode_unparse (f),
2813 cstring_makeLiteralTemp (sRef_isInternalState (el)
2821 else if (sRef_isNothing (el) || sRef_isSpecState (el))
2827 uentry gle = sRef_getUentry (el);
2828 sRef sr = sRef_updateSref (el);
2830 if (sRef_isUndefGlob (el))
2832 sRef_setDefined (sr, f->loc);
2833 exprNode_checkSet (f, sr);
2841 if (sRef_isAllocated (el))
2843 exprNode_checkSet (f, sr);
2847 if (sRef_isStateUndefined (sr))
2852 ("%s %q used by function undefined before call: %s",
2853 sRef_getScopeName (sr),
2855 exprNode_unparse (f)),
2857 sRef_setDefined (sr, f->loc);
2859 exprNode_checkUse (f, sr, f->loc);
2862 checkGlobUse (gle, TRUE, f);
2865 if (sRef_isKilledGlob (el))
2867 sRef_kill (sr, f->loc);
2868 context_usedGlobal (sr);
2872 } end_globSet_allElements;
2878 if (context_hasMods () || context_getFlag (FLG_MODNOMODS))
2880 sRefSet smods = sRefSet_undefined;
2883 ** NEED to check for modifies anything
2887 ** check each sRef that called function modifies (ml), is
2893 sRefSet_allElements (mods, s) /* s is something which may be modified */
2895 DPRINTF (("Check modify: %s", sRef_unparse (s)));
2897 if (sRef_isKindSpecial (s))
2899 if (sRef_isSpecInternalState (s))
2901 if (context_getFlag (FLG_MODINTERNALSTRICT))
2903 exprNode_checkCallModifyVal (s, args, f, ret);
2907 sRefSet mmods = context_modList ();
2909 sRefSet_allElements (mmods, el)
2911 if (sRef_isInternalState (el))
2913 sRef_setModified (el);
2915 } end_sRefSet_allElements ;
2920 exprNode_checkCallModifyVal (s, args, f, ret);
2925 sRef rb = sRef_getRootBase (s);
2927 if (sRef_isFileOrGlobalScope (rb))
2929 context_usedGlobal (rb);
2932 if (sRef_isFileStatic (s)
2933 && !fileId_equal (fileloc_fileId (f->loc),
2934 fileloc_fileId (uentry_whereDefined (le))))
2936 smods = sRefSet_insert (smods, s);
2940 exprNode_checkCallModifyVal (s, args, f, ret);
2943 } end_sRefSet_allElements;
2948 ** Static elements in modifies set can have nasty consequences.
2949 ** (I think...have not been able to reproduce a possible bug.)
2952 if (!sRefSet_isDefined (smods))
2954 mods = sRefSet_newCopy (mods);
2957 sRefSet_allElements (smods, el)
2959 bool res = sRefSet_delete (mods, el);
2962 } end_sRefSet_allElements;
2964 sRefSet_free (smods);
2969 else if (sRefSet_isDefined (mods))
2970 { /* just check observers */
2973 sRefSet_allElements (mods, s) /* s is something which may be modified */
2975 sRef rb = sRef_getRootBase (s);
2979 if (sRef_isParam (rb))
2981 sRef b = sRef_fixBaseParam (s, args);
2983 if (sRef_isObserver (b))
2985 exprNode e = exprNodeList_nth (args, sRef_getParam (rb));
2989 message ("Function call may modify observer%q: %s",
2990 sRef_unparsePreOpt (b), exprNode_unparse (e)),
2993 sRef_showExpInfo (b);
2997 } end_sRefSet_allElements;
3001 if (!hasMods) /* no specified modifications */
3003 if (context_getFlag (FLG_MODOBSERVERUNCON))
3005 exprNodeList_elements (args, e)
3007 if (exprNode_isDefined (e))
3009 sRef s = exprNode_getSref (e);
3011 if (sRef_isObserver (s)
3012 && ctype_isMutable (sRef_getType (s)))
3015 (FLG_MODOBSERVERUNCON,
3017 ("Call to unconstrained function %s may modify observer%q: %s",
3018 exprNode_unparse (f),
3019 sRef_unparsePreOpt (s), exprNode_unparse (e)),
3022 sRef_showExpInfo (s);
3026 } end_exprNodeList_elements;
3031 checkAnyCall (f, fname, params, args, hasMods, mods, isSpec, specialArgs);
3033 ret->uses = sRefSet_union (ret->uses, f->uses);
3034 ret->sets = sRefSet_union (ret->sets, f->sets);
3035 ret->msets = sRefSet_union (ret->msets, f->msets);
3040 ** Spurious errors reported, because lclint can't tell
3041 ** mods must be fresh if freshMods is true.
3044 /*@i@*/ sRefSet_free (mods);
3050 void checkGlobUse (uentry glob, bool isCall, /*@notnull@*/ exprNode e)
3052 if (uentry_isVar (glob))
3054 if (context_inFunctionLike ())
3056 sRef sr = uentry_getSref (glob);
3058 context_usedGlobal (sr);
3060 if (context_checkGlobUse (glob))
3062 if (!context_globAccess (sr))
3068 message ("Called procedure %s may access %s %q",
3069 exprNode_unparse (e),
3070 sRef_unparseScope (sr),
3071 uentry_getName (glob)),
3078 message ("Undocumented use of %s %s",
3079 sRef_unparseScope (sr),
3080 exprNode_unparse (e)),
3089 llbug (message ("Global not variable: %q", uentry_unparse (glob)));
3094 reflectEnsuresClause (exprNode ret, uentry le, exprNode f, exprNodeList args)
3096 DPRINTF (("Reflect ensures clause: %s(%s) / %s / %s",
3097 exprNode_unparse (f), exprNodeList_unparse (args),
3098 uentry_unparseFull (le),
3099 stateClauseList_unparse (uentry_getStateClauseList (le))));
3101 if (uentry_isValid (le) && uentry_isFunction (le))
3103 stateClauseList sclauses = uentry_getStateClauseList (le);
3105 if (stateClauseList_isDefined (sclauses))
3107 DPRINTF (("Reflect ensures: %s / %s / %s",
3108 uentry_unparse (le),
3109 exprNode_unparse (f), exprNodeList_unparse (args)));
3111 stateClauseList_elements (sclauses, cl)
3113 if (stateClause_hasEnsures (cl))
3115 /* Same in usymtab.c:1904 */
3116 if (stateClause_setsMetaState (cl))
3118 qual q = stateClause_getMetaQual (cl);
3119 annotationInfo ainfo = qual_getAnnotationInfo (q);
3120 metaStateInfo minfo = annotationInfo_getState (ainfo);
3121 cstring key = metaStateInfo_getName (minfo);
3122 int mvalue = annotationInfo_getValue (ainfo);
3124 sRefSet osrs = sRefSet_undefined;
3127 if (stateClause_isGlobal (cl))
3129 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3134 srs = stateClause_getRefs (cl);
3137 DPRINTF (("Reflect ensures clause: %s", stateClause_unparse (cl)));
3140 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
3142 sRefSet_elements (srs, sel)
3146 if (sRef_isResult (sRef_getRootBase (sel)))
3148 s = exprNode_getSref (ret);
3152 s = sRef_fixBaseParam (sel, args);
3155 DPRINTF (("Reflecting state clause on: %s / %s",
3156 sRef_unparse (sel), sRef_unparse (s)));
3158 sRef_setMetaStateValueComplete (s, key, mvalue, exprNode_loc (f));
3159 } end_sRefSet_elements;
3161 sRefSet_free (osrs);
3165 sRefSet srs = stateClause_getRefs (cl);
3166 sRefModVal modf = stateClause_getEnsuresFunction (cl);
3167 int eparam = stateClause_getStateParameter (cl);
3169 DPRINTF (("Reflect after clause: %s / %s",
3170 stateClause_unparse (cl),
3171 sRefSet_unparse (srs)));
3173 sRefSet_elements (srs, sel)
3177 DPRINTF (("elements: %s", sRef_unparse (sel)));
3178 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3180 if (sRef_isResult (sRef_getRootBase (sel)))
3182 DPRINTF (("Fix base: %s / %s",
3183 sRef_unparse (sel), sRef_unparse (exprNode_getSref (ret))));
3184 s = sRef_fixBase (sel, exprNode_getSref (ret));
3185 DPRINTF (("==> %s", sRef_unparseFull (s)));
3189 s = sRef_fixBaseParam (sel, args);
3192 DPRINTF (("elements: %s", sRef_unparse (s)));
3193 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3195 DPRINTF (("Reflecting state clause on: %s / %s",
3196 sRef_unparse (sel), sRef_unparse (s)));
3198 modf (s, eparam, exprNode_loc (f));
3199 } end_sRefSet_elements;
3202 } end_stateClauseList_elements ;
3205 DPRINTF (("Here: %s / %s",
3206 uentry_unparseFull (le),
3207 bool_unparse (uentry_hasMetaStateEnsures (le))));
3209 if (uentry_hasMetaStateEnsures (le))
3211 metaStateConstraintList mscl = uentry_getMetaStateEnsures (le);
3213 metaStateConstraintList_elements (mscl, msc)
3215 metaStateSpecifier msspec = metaStateConstraint_getSpecifier (msc);
3216 metaStateInfo msinfo = metaStateSpecifier_getMetaStateInfo (msspec);
3217 metaStateExpression msexpr = metaStateConstraint_getExpression (msc);
3218 cstring key = metaStateInfo_getName (msinfo);
3219 sRef mlsr = metaStateSpecifier_getSref (msspec);
3221 sRef lastref = sRef_undefined;
3222 stateValue sval = stateValue_undefined;
3224 DPRINTF (("Meta state constraint for %s: %s", uentry_unparse (le),
3225 metaStateConstraint_unparse (msc)));
3226 DPRINTF (("Matches left: %s", sRef_unparseDebug (mlsr)));
3228 if (sRef_isResult (sRef_getRootBase (mlsr)))
3230 s = exprNode_getSref (ret);
3234 s = sRef_fixBaseParam (mlsr, args);
3237 DPRINTF (("Setting state: %s", sRef_unparseFull (s)));
3239 while (metaStateExpression_isDefined (msexpr))
3241 metaStateSpecifier ms = metaStateExpression_getSpecifier (msexpr);
3242 sRef msr = metaStateSpecifier_getSref (ms);
3243 metaStateInfo msi = metaStateSpecifier_getMetaStateInfo (ms);
3246 DPRINTF (("Check expression: %s", metaStateExpression_unparse (msexpr)));
3248 if (metaStateExpression_isMerge (msexpr))
3250 msexpr = metaStateExpression_getRest (msexpr);
3254 msexpr = metaStateExpression_undefined;
3257 if (metaStateInfo_isDefined (msi))
3259 /* Must match lhs state */
3260 llassert (metaStateInfo_equal (msinfo, msi));
3263 llassert (sRef_isParam (sRef_getRootBase (msr)));
3264 fs = sRef_fixBaseParam (msr, args);
3266 if (stateValue_isDefined (sval))
3268 /* Use combination table to merge old state value with new one: */
3269 stateValue tval = sRef_getMetaStateValue (fs, key);
3271 if (stateValue_isDefined (tval))
3273 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3274 cstring msg = cstring_undefined;
3275 int nval = stateCombinationTable_lookup (sctable,
3276 stateValue_getValue (sval),
3277 stateValue_getValue (tval),
3279 DPRINTF (("Combining: %s + %s -> %d",
3280 stateValue_unparseValue (sval, msinfo),
3281 stateValue_unparseValue (tval, msinfo),
3284 if (nval == stateValue_error)
3286 llassert (cstring_isDefined (msg));
3291 ("Attributes merged in ensures clause in states that "
3292 "cannot be combined (%q is %q, %q is %q): %s",
3293 sRef_unparse (lastref),
3294 stateValue_unparseValue (sval, msinfo),
3296 stateValue_unparseValue (tval, msinfo),
3300 sRef_showMetaStateInfo (fs, key);
3304 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3308 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3313 sval = sRef_getMetaStateValue (fs, key);
3318 if (stateValue_isError (sval))
3320 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3324 DPRINTF (("Setting: %s:%s <- %s", sRef_unparse (s), key, stateValue_unparse (sval)));
3326 if (stateValue_isDefined (sval))
3328 sRef_setMetaStateValueComplete (s, key, stateValue_getValue (sval), exprNode_loc (f));
3332 DPRINTF (("Undefined state: %s", cstring_toCharsSafe (sRef_unparse (s))));
3334 } end_metaStateConstraintList_elements ;
3336 metaStateConstraintList_free (mscl);
3342 checkRequiresClause (uentry le, exprNode f, exprNodeList args)
3344 DPRINTF (("Check requires clause: %s(%s) / %s / %s",
3345 exprNode_unparse (f), exprNodeList_unparse (args),
3346 uentry_unparseFull (le),
3347 stateClauseList_unparse (uentry_getStateClauseList (le))));
3349 if (uentry_isValid (le) && uentry_isFunction (le))
3351 stateClauseList sclauses = uentry_getStateClauseList (le);
3353 if (stateClauseList_isDefined (sclauses))
3355 DPRINTF (("Check requires: %s / %s / %s",
3356 uentry_unparse (le),
3357 exprNode_unparse (f), exprNodeList_unparse (args)));
3359 stateClauseList_elements (sclauses, cl)
3361 DPRINTF (("Check clause: %s / %s",
3362 stateClause_unparse (cl),
3363 bool_unparse (stateClause_hasRequires (cl))));
3365 if (stateClause_hasRequires (cl))
3367 sRefSet osrs = sRefSet_undefined;
3370 if (stateClause_isGlobal (cl))
3372 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3377 srs = stateClause_getRefs (cl);
3380 DPRINTF (("Refs: %s", sRefSet_unparse (srs)));
3382 if (stateClause_setsMetaState (cl))
3384 qual q = stateClause_getMetaQual (cl);
3385 annotationInfo ainfo = qual_getAnnotationInfo (q);
3386 metaStateInfo minfo = annotationInfo_getState (ainfo);
3387 cstring key = metaStateInfo_getName (minfo);
3388 int mvalue = annotationInfo_getValue (ainfo);
3390 DPRINTF (("Requires meta state! %s = %d", key, mvalue));
3392 sRefSet_elements (srs, sel)
3394 sRef s = sRef_fixBaseParam (sel, args);
3396 if (sRef_isResult (sRef_getRootBase (sel)))
3402 DPRINTF (("Checking state clause on: %s / %s / %s = %d",
3403 sRef_unparseFull (sel), sRef_unparseFull (s),
3406 if (!sRef_checkMetaStateValue (s, key, mvalue))
3408 DPRINTF (("HERE: %s", sRef_unparse (s)));
3412 ("Requires clause of called function %q not satisfied%q (state is %q): %q",
3413 uentry_getName (le),
3414 sRef_isGlobalMarker (s)
3416 : message (" by %q", sRef_unparse (s)),
3417 stateValue_unparseValue (sRef_getMetaStateValue (s, key),
3419 stateClause_unparse (cl)),
3422 sRef_showAliasInfo (s);
3426 DPRINTF (("Error supressed!"));
3427 DPRINTF (("Loc: %s", fileloc_unparse (exprNode_loc (f))));
3428 DPRINTF (("Context supress: %s",
3429 bool_unparse (context_suppressFlagMsg (FLG_STATETRANSFER, exprNode_loc (f)))));
3433 } end_sRefSet_elements;
3437 sRefModVal modf = stateClause_getRequiresBodyFunction (cl);
3438 int eparam = stateClause_getStateParameter (cl);
3440 DPRINTF (("Reflect after clause: %s / %s",
3441 stateClause_unparse (cl),
3442 sRefSet_unparse (srs)));
3444 sRefSet_elements (srs, sel)
3448 DPRINTF (("elements: %s", sRef_unparse (sel)));
3449 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3451 s = sRef_fixBaseParam (sel, args);
3453 DPRINTF (("elements: %s", sRef_unparse (s)));
3454 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3456 if (sRef_isResult (sRef_getRootBase (sel)))
3458 ; /*@i423 what do we do about results */
3462 DPRINTF (("Reflecting state clause on: %s / %s",
3463 sRef_unparse (sel), sRef_unparse (s)));
3465 modf (s, eparam, exprNode_loc (f));
3467 } end_sRefSet_elements;
3470 sRefSet_free (osrs);
3472 } end_stateClauseList_elements ;
3477 static /*@only@*/ exprNode
3478 functionCallSafe (/*@only@*/ /*@notnull@*/ exprNode f,
3479 ctype t, /*@keep@*/ exprNodeList args)
3481 /* requires f is a non-error exprNode, with type function */
3482 cstring fname = exprNode_unparse (f);
3483 uentry le = exprNode_getUentry (f);
3484 exprNode ret = exprNode_createPartialCopy (f);
3489 DPRINTF (("Call: %s %s",exprNode_unparse (f), exprNodeList_unparse (args)));
3491 ret->typ = ctype_getReturnType (t);
3492 ret->kind = XPR_CALL;
3494 ret->edata = exprData_makeCall (f, args);
3497 ** Order of these steps is very important!
3499 ** Must check for argument dependencies before messing up uses and sets.
3502 if (context_getFlag (FLG_EVALORDER))
3504 exprNodeList_elements (args, current)
3506 if (exprNode_isDefined (current))
3508 exprNode_addUse (current, current->sref);
3510 } end_exprNodeList_elements;
3512 if (context_maybeSet (FLG_EVALORDER) || context_maybeSet (FLG_EVALORDERUNCON))
3514 checkSequencing (f, args);
3517 exprNodeList_elements (args, current)
3519 if (exprNode_isDefined (current) && sRef_isMeaningful (current->sref))
3521 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3523 } end_exprNodeList_elements ;
3526 special = checkArgs (le, f, t, args, ret);
3527 checkGlobMods (f, le, args, ret, special);
3528 checkRequiresClause (le, f, args);
3531 if (uentry_isValid (le)
3532 && (uentry_isFunction (le)
3533 || (uentry_isVariable (le)
3534 && ctype_isFunction (uentry_getType (le)))))
3536 exitkind exk = uentry_getExitCode (le);
3538 /* f->typ is already set to the return type */
3540 DPRINTF (("Function: %s", uentry_unparseFull (le)));
3541 ret->sref = uentry_returnedRef (le, args);
3542 DPRINTF (("Returned: %s / %s",
3543 uentry_unparseFull (le),
3544 sRef_unparseFull (ret->sref)));
3546 if (uentry_isFunction (le) && exprNodeList_size (args) >= 1)
3548 qual nullPred = uentry_nullPred (le);
3550 if (qual_isTrueNull (nullPred))
3552 exprNode arg = exprNodeList_head (args);
3554 if (exprNode_isDefined (arg))
3556 ret->guards = guardSet_addFalseGuard (ret->guards, arg->sref);
3559 else if (qual_isFalseNull (nullPred))
3561 exprNode arg = exprNodeList_head (args);
3563 if (exprNode_isDefined (arg))
3565 ret->guards = guardSet_addTrueGuard (ret->guards, arg->sref);
3570 llassert (qual_isUnknown (nullPred));
3574 if (exitkind_isConditionalExit (exk))
3578 ** if (arg0) then { exit! } else { ; }
3580 ** if (arg0) then { ; } else { exit! }
3585 llassert (!exprNodeList_isEmpty (args));
3586 firstArg = exprNodeList_head (args);
3588 if (exprNode_isDefined (firstArg)
3589 && !guardSet_isEmpty (firstArg->guards))
3591 usymtab_trueBranch (guardSet_undefined);
3592 usymtab_altBranch (guardSet_undefined);
3594 if (exitkind_isTrueExit (exk))
3596 usymtab_popBranches (firstArg,
3597 exprNode_makeMustExit (),
3599 TRUE, TRUEEXITCLAUSE);
3603 usymtab_popBranches (firstArg,
3605 exprNode_makeMustExit (),
3606 TRUE, FALSEEXITCLAUSE);
3610 ret->exitCode = XK_MAYEXIT;
3612 else if (exitkind_mustExit (exk))
3614 ret->exitCode = XK_MUSTEXIT;
3616 else if (exitkind_couldExit (exk))
3618 ret->exitCode = XK_MAYEXIT;
3625 if (cstring_equalLit (fname, "exit"))
3627 if (exprNodeList_size (args) == 1)
3629 exprNode arg = exprNodeList_head (args);
3631 if (exprNode_isDefined (arg) && exprNode_knownIntValue (arg))
3633 long int val = multiVal_forceInt (exprNode_getValue (arg));
3640 ("Argument to exit has implementation defined behavior: %s",
3641 exprNode_unparse (arg)),
3642 exprNode_loc (arg));
3651 exprNode_checkSetAny (ret, uentry_rawName (le));
3654 DPRINTF (("Before reflect: %s", sRef_unparseFull (ret->sref)));
3655 DPRINTF (("Reflect: %s", uentry_unparseFull (le)));
3656 reflectEnsuresClause (ret, le, f, args);
3663 ** this is yucky! should keep the uentry as part of exprNode!
3666 /*@observer@*/ uentry
3667 exprNode_getUentry (exprNode e)
3669 if (exprNode_isError (e))
3671 return uentry_undefined;
3675 cstring s = exprNode_rootVarName (e);
3676 uentry ue = usymtab_lookupSafe (s);
3683 exprNode_makeInitBlock (lltok brace, /*@only@*/ exprNodeList inits)
3685 exprNode ret = exprNode_createPlain (ctype_unknown);
3687 ret->kind = XPR_INITBLOCK;
3688 ret->edata = exprData_makeCall (exprNode_undefined, inits);
3689 ret->loc = fileloc_update (ret->loc, lltok_getLoc (brace));
3695 exprNode_functionCall (/*@only@*/ exprNode f, /*@only@*/ exprNodeList args)
3701 if (exprNode_isUndefined (f))
3704 exprNodeList_free (args);
3705 return exprNode_undefined;
3708 t = exprNode_getType (f);
3710 if (sRef_isLocalVar (f->sref))
3712 exprNode_checkUse (f, f->sref, f->loc);
3714 if (sRef_possiblyNull (f->sref))
3716 if (!usymtab_isGuarded (f->sref))
3718 if (optgenerror (FLG_NULLDEREF,
3719 message ("Function call using %s pointer %q",
3720 sRef_nullMessage (f->sref),
3721 sRef_unparse (f->sref)),
3724 sRef_showNullInfo (f->sref);
3725 sRef_setNullError (f->sref);
3733 if (ctype_isRealFunction (t))
3735 exprNode ret = functionCallSafe (f, t, args);
3739 else if (ctype_isUnknown (t))
3741 exprNode ret = exprNode_createPartialCopy (f);
3747 exprNodeList_elements (args, current)
3749 if (exprNode_isDefined (current))
3751 exprNode_checkUse (ret, current->sref, ret->loc);
3754 ** also, anything derivable from current->sref may be used
3757 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3758 exprNode_mergeUSs (ret, current);
3760 } end_exprNodeList_elements;
3762 ret->edata = exprData_makeCall (f, args);
3763 ret->kind = XPR_CALL;
3765 tstring = cstring_copy (exprNode_unparse (f));
3767 cstring_markOwned (tstring);
3768 exprNode_checkSetAny (ret, tstring);
3774 voptgenerror (FLG_TYPE,
3775 message ("Call to non-function (type %t): %s", t,
3776 exprNode_unparse (f)),
3779 exprNodeList_free (args);
3781 return (exprNode_makeError ());
3786 exprNode_fieldAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
3787 /*@only@*/ cstring f)
3789 exprNode ret = exprNode_createPartialCopy (s);
3791 ret->kind = XPR_FACCESS;
3793 if (exprNode_isError (s))
3795 ret->edata = exprData_makeField (s, f);
3800 ctype t = exprNode_getType (s);
3801 ctype tr = ctype_realType (t);
3803 checkMacroParen (s);
3805 ret->edata = exprData_makeField (s, f);
3807 if (ctype_isStructorUnion (tr))
3809 uentry tf = uentryList_lookupField (ctype_getFields (tr), f);
3811 if (uentry_isUndefined (tf))
3813 voptgenerror (FLG_TYPE,
3814 message ("Access non-existent field %s of %t: %s", f, t,
3815 exprNode_unparse (ret)),
3817 /*! cstring_free (f); */ /* evans 2001-03-25 self-detect */
3822 uentry_setUsed (tf, exprNode_loc (ret));
3824 ret->typ = uentry_getType (tf);
3825 checkSafeUse (ret, s->sref);
3827 ret->sref = sRef_makeField (s->sref, uentry_rawName (tf));
3828 /*!? exprNode_free (s); */ /* evans 2001-03-25 self-detect */
3832 else /* isStructorUnion */
3834 if (ctype_isRealAbstract (tr))
3838 message ("Access field of abstract type (%t): %s.%s",
3839 t, exprNode_unparse (s), f),
3841 ret->typ = ctype_unknown;
3845 if (ctype_isKnown (tr))
3850 ("Access field of non-struct or union (%t): %s.%s",
3851 t, exprNode_unparse (s), f),
3854 ret->typ = ctype_unknown;
3858 cstring sn = cstring_copy (f);
3860 checkSafeUse (ret, s->sref);
3861 cstring_markOwned (sn);
3862 ret->sref = sRef_makeField (s->sref, sn);
3874 exprNode_fieldAccess (/*@only@*/ exprNode s, /*@only@*/ lltok dot,
3875 /*@only@*/ cstring f)
3877 exprNode res = exprNode_fieldAccessAux (s, lltok_getLoc (dot), f);
3878 lltok_release (dot);
3883 exprNode_addParens (/*@only@*/ lltok lpar, /*@only@*/ exprNode e)
3885 exprNode ret = exprNode_createPartialCopy (e);
3887 ret->loc = fileloc_update (ret->loc, lltok_getLoc (lpar));
3888 ret->kind = XPR_PARENS;
3889 ret->edata = exprData_makeUop (e, lpar);
3891 if (!exprNode_isError (e))
3893 ret->exitCode = e->exitCode;
3894 ret->canBreak = e->canBreak;
3895 ret->mustBreak = e->mustBreak;
3896 ret->isJumpPoint = e->isJumpPoint;
3897 ret->sref = e->sref;
3904 exprNode_arrowAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
3905 /*@only@*/ cstring f)
3907 exprNode ret = exprNode_createPartialCopy (s);
3909 ret->edata = exprData_makeField (s, f);
3910 ret->kind = XPR_ARROW;
3912 if (exprNode_isError (s))
3918 ctype t = exprNode_getType (s);
3919 ctype tr = ctype_realType (t);
3921 checkMacroParen (s);
3923 (void) ctype_fixArrayPtr (tr); /* REWRITE THIS */
3925 if (ctype_isRealPointer (tr))
3927 ctype b = ctype_realType (ctype_baseArrayPtr (tr));
3929 if (ctype_isStructorUnion (b))
3931 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
3933 if (sRef_isKnown (s->sref) && sRef_possiblyNull (s->sref))
3935 if (!usymtab_isGuarded (s->sref) && !context_inProtectVars ())
3939 message ("Arrow access from %s pointer%q: %s",
3940 sRef_nullMessage (s->sref),
3941 sRef_unparsePreOpt (s->sref),
3942 exprNode_unparse (ret)),
3945 sRef_showNullInfo (s->sref);
3946 sRef_setNullError (s->sref);
3951 if (uentry_isUndefined (fentry))
3955 message ("Access non-existent field %s of %t: %s",
3956 f, t, exprNode_unparse (ret)),
3958 ret->typ = ctype_unknown;
3964 ** was safeUse: shouldn't be safe!
3967 ** rec must be defined,
3968 ** *rec must be allocated
3969 ** rec->field need only be defined it if is an rvalue
3972 uentry_setUsed (fentry, exprNode_loc (ret));
3973 ret->typ = uentry_getType (fentry);
3975 exprNode_checkUse (ret, s->sref, s->loc);
3977 /* exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc); */
3978 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
3982 else /* Pointer to something that is not a struct or union*/
3984 if (ctype_isRealAbstract (tr))
3986 ctype xrt = ctype_forceRealType (tr);
3990 message ("Arrow access field of abstract type (%t): %s->%s",
3991 t, exprNode_unparse (s), f),
3995 ** Set the state correctly, as if the abstraction is broken.
3998 if (ctype_isRealPointer (xrt) &&
3999 (b = ctype_realType (ctype_baseArrayPtr (xrt)),
4000 ctype_isStructorUnion (b)))
4002 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4003 ret->typ = uentry_getType (fentry);
4004 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4008 ret->typ = ctype_unknown;
4009 ret->sref = sRef_undefined;
4012 else /* not a struct, union or abstract */
4014 if (ctype_isUnknown (tr)) {
4015 cstring sn = cstring_copy (f);
4017 DPRINTF (("Here: %s", exprNode_unparse (s)));
4019 exprNode_checkUse (ret, s->sref, s->loc);
4020 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4022 cstring_markOwned (sn);
4023 ret->sref = sRef_makeArrow (s->sref, sn);
4025 ret->kind = XPR_ARROW;
4030 message ("Arrow access field of non-struct or union "
4031 "pointer (%t): %s->%s",
4032 t, exprNode_unparse (s), f),
4035 ret->typ = ctype_unknown;
4036 ret->sref = sRef_undefined;
4041 else /* its not a pointer */
4043 if (!ctype_isUnknown (tr))
4047 message ("Arrow access of non-pointer (%t): %s->%s",
4048 t, exprNode_unparse (s), f),
4051 ret->typ = ctype_unknown;
4052 ret->sref = sRef_undefined;
4056 cstring sn = cstring_copy (f);
4058 DPRINTF (("Here: %s", exprNode_unparse (s)));
4060 exprNode_checkUse (ret, s->sref, s->loc);
4061 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4063 cstring_markOwned (sn);
4064 ret->sref = sRef_makeArrow (s->sref, sn);
4066 ret->kind = XPR_ARROW;
4077 exprNode_arrowAccess (/*@only@*/ exprNode s,
4078 /*@only@*/ lltok arrow,
4079 /*@only@*/ cstring f)
4081 exprNode res = exprNode_arrowAccessAux (s, lltok_getLoc (arrow), f);
4082 lltok_release (arrow);
4087 ** only postOp's in C: i++ and i--
4091 exprNode_postOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4093 /* check modification also */
4094 /* cstring opname = lltok_unparse (op);*/
4096 exprNode ret = exprNode_createPartialCopy (e);
4098 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4099 ret->kind = XPR_POSTOP;
4100 ret->edata = exprData_makeUop (e, op);
4102 if (!exprNode_isDefined (e))
4107 checkMacroParen (e);
4109 exprNode_checkUse (ret, e->sref, e->loc);
4110 exprNode_checkSet (ret, e->sref);
4112 t = exprNode_getType (e);
4114 if (sRef_isUnsafe (e->sref))
4116 voptgenerror (FLG_MACROPARAMS,
4117 message ("Operand of %s is macro parameter (non-functional): %s%s",
4118 lltok_unparse (op), exprNode_unparse (e), lltok_unparse (op)),
4120 sRef_makeSafe (e->sref);
4121 sRef_makeSafe (ret->sref);
4124 if (ctype_isForceRealNumeric (&t) || ctype_isRealAP (t))
4130 if (ctype_isRealAbstract (t))
4134 message ("Operand of %s is abstract type (%t): %s",
4135 lltok_unparse (op), t, exprNode_unparse (e)),
4142 message ("Operand of %s is non-numeric (%t): %s",
4143 lltok_unparse (op), t, exprNode_unparse (e)),
4146 ret->typ = ctype_unknown;
4149 /* if (ctype_isZero (t)) e->typ = ctype_int; */
4151 exprNode_checkModify (e, ret);
4153 /* added 7/11/2000 D.L */
4155 /*DRL 6/8/01 I decided to disable all LCLint Warning here since the code
4156 probably needs a rewrite any way */
4160 // updateEnvironmentForPostOp (e);
4162 /* start modifications */
4163 /* added by Seejo on 4/16/2000 */
4165 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4167 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4169 ret->sref = sRef_copy (e->sref);
4172 if (lltok_getTok (op) == INC_OP) {
4173 if (sRef_getSize(e->sref) > 0) {
4175 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4177 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4178 /* Assumption: there is only 1 \0 in the buffer */
4179 /* This will not be correct if there are 2 \0's in the buffer */
4180 sRef_setNotNullTerminatedState(ret->sref);
4181 sRef_resetLen(ret->sref);
4183 sRef_setNullTerminatedState(ret->sref);
4184 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4186 if (sRef_isNullTerminated (ret->sref))
4187 printf ("ret->sref is Null Terminated\n");
4188 else if (sRef_isPossiblyNullTerminated (ret->sref))
4189 printf ("ret->sref is Possibly Null Terminated\n");
4190 else if (sRef_isNotNullTerminated (ret->sref))
4191 printf ("ret->sref is Not Null Terminated\n");
4196 if (lltok_getTok (op) == DEC_OP) {
4197 if (sRef_getSize(e->sref) >= 0) {
4198 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4199 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4204 /* end modifications */
4210 exprNode_preOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4212 bool checkMod = FALSE;
4214 int opid = lltok_getTok (op);
4215 exprNode ret = exprNode_createSemiCopy (e);
4217 exprNode_copySets (ret, e);
4219 multiVal_free (ret->val);
4220 ret->val = multiVal_undefined;
4221 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4222 ret->kind = XPR_PREOP;
4223 ret->edata = exprData_makeUop (e, op);
4225 if (exprNode_isError (e))
4230 checkMacroParen (e);
4232 te = exprNode_getType (e);
4233 tr = ctype_realType (te);
4235 if (opid != TAMPERSAND)
4237 exprNode_checkUse (ret, e->sref, e->loc);
4239 if (ctype_isRealAbstract (tr)
4240 && (!(ctype_isRealBool (te) && (opid == TEXCL))))
4242 if (optgenerror (FLG_ABSTRACT,
4243 message ("Operand of %s is abstract type (%t): %s",
4244 lltok_unparse (op), tr,
4245 exprNode_unparse (ret)),
4248 tr = te = ctype_unknown;
4249 ret->typ = ctype_unknown;
4250 sRef_setNullError (e->sref);
4258 case DEC_OP: /* should also check modification! */
4259 if (sRef_isMacroParamRef (e->sref))
4263 message ("Operand of %s is macro parameter (non-functional): %s",
4264 lltok_unparse (op), exprNode_unparse (ret)),
4269 exprNode_checkSet (ret, e->sref);
4272 if (ctype_isForceRealNumeric (&tr) || ctype_isRealAP (tr))
4277 if (context_msgStrictOps ())
4281 message ("Operand of %s is non-numeric (%t): %s",
4282 lltok_unparse (op), te, exprNode_unparse (ret)),
4285 ret->typ = ctype_int;
4288 /* start modifications */
4289 /* added by Seejo on 4/16/2000 */
4291 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4293 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4295 ret->sref = sRef_copy (e->sref);
4298 if (lltok_getTok (op) == INC_OP) {
4299 if (sRef_getSize(e->sref) > 0) {
4301 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4303 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4304 /* Assumption: there is only 1 \0 in the buffer */
4305 /* This will not be correct if there are 2 \0's in the buffer */
4306 sRef_setNotNullTerminatedState(ret->sref);
4307 sRef_resetLen (ret->sref);
4309 sRef_setNullTerminatedState(ret->sref);
4310 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4316 if (lltok_getTok (op) == DEC_OP) {
4317 if (sRef_getSize(e->sref) >= 0) {
4318 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4319 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4324 /* end modifications */
4331 if (ctype_isForceRealNumeric (&tr))
4335 ret->val = multiVal_invert (exprNode_getValue (e));
4339 ret->val = multiVal_copy (exprNode_getValue (e));
4344 if (context_msgStrictOps ())
4348 message ("Operand of %s is non-numeric (%t): %s",
4349 lltok_unparse (op), te, exprNode_unparse (ret)),
4353 ret->typ = ctype_int;
4357 case TEXCL: /* maybe this should be restricted */
4358 guardSet_flip (ret->guards);
4360 if (ctype_isRealBool (te) || ctype_isUnknown (te))
4366 if (ctype_isRealPointer (tr))
4368 if (sRef_isKnown (e->sref))
4370 ret->guards = guardSet_addFalseGuard (ret->guards, e->sref);
4374 (FLG_BOOLOPS, FLG_PTRNEGATE,
4375 message ("Operand of %s is non-boolean (%t): %s",
4376 lltok_unparse (op), te, exprNode_unparse (ret)),
4383 message ("Operand of %s is non-boolean (%t): %s",
4384 lltok_unparse (op), te, exprNode_unparse (ret)),
4388 ret->typ = ctype_bool;
4393 if (ctype_isForceRealInt (&tr))
4398 if (context_msgStrictOps ())
4402 message ("Operand of %s is non-integer (%t): %s",
4403 lltok_unparse (op), te, exprNode_unparse (ret)),
4407 if (ctype_isInt (e->typ))
4413 ret->typ = ctype_int;
4419 ret->typ = ctype_makePointer (e->typ);
4421 if (sRef_isKnown (e->sref))
4423 ret->sref = sRef_makeAddress (e->sref);
4430 if (ctype_isAP (tr))
4432 ret->typ = ctype_baseArrayPtr (e->typ);
4436 if (ctype_isKnown (te))
4438 if (ctype_isFunction (te))
4444 message ("Dereference of function type (%t): %s",
4445 te, exprNode_unparse (ret)),
4450 voptgenerror (FLG_TYPE,
4451 message ("Dereference of non-pointer (%t): %s",
4452 te, exprNode_unparse (ret)),
4454 ret->typ = ctype_unknown;
4459 ret->typ = ctype_unknown;
4464 if (sRef_isKnown (e->sref))
4466 if (sRef_possiblyNull (e->sref))
4468 if (!usymtab_isGuarded (e->sref) && !context_inProtectVars ())
4472 message ("Dereference of %s pointer %q: %s",
4473 sRef_nullMessage (e->sref),
4474 sRef_unparse (e->sref),
4475 exprNode_unparse (ret)),
4478 sRef_showNullInfo (e->sref);
4479 sRef_setNotNull (e->sref, e->loc); /* suppress future messages */
4484 ret->sref = sRef_makePointer (e->sref);
4489 llbug (message ("exprNode_preOp: unhandled op: %s", lltok_unparse (op)));
4494 exprNode_checkModify (e, ret);
4501 ** any reason to disallow sizeof (abstract type) ?
4505 ** used by both sizeof
4509 ctype sizeof_resultType (void)
4511 static ctype sizet = ctype_unknown;
4513 if (ctype_isUnknown (sizet))
4515 if (usymtab_existsType (cstring_makeLiteralTemp ("size_t")))
4517 sizet = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("size_t")));
4521 sizet = ctype_ulint;
4528 exprNode_sizeofType (/*@only@*/ qtype qt)
4530 exprNode ret = exprNode_create (sizeof_resultType ());
4531 ctype ct = qtype_getType (qt);
4533 ret->kind = XPR_SIZEOFT;
4534 ret->edata = exprData_makeSizeofType (qt);
4536 voptgenerror (FLG_SIZEOFTYPE,
4537 message ("Parameter to sizeof is type %s: %s",
4539 exprNode_unparse (ret)),
4546 exprNode_alignofType (/*@only@*/ qtype qt)
4548 exprNode ret = exprNode_create (sizeof_resultType ());
4549 ctype ct = qtype_getType (qt);
4551 ret->kind = XPR_ALIGNOFT;
4552 ret->edata = exprData_makeSizeofType (qt);
4554 voptgenerror (FLG_SIZEOFTYPE,
4555 message ("Parameter to alignof is type %s: %s",
4557 exprNode_unparse (ret)),
4563 exprNode exprNode_offsetof (qtype qt, cstringList s)
4565 exprNode ret = exprNode_create (sizeof_resultType ());
4566 ctype ct = qtype_getType (qt);
4568 ret->kind = XPR_OFFSETOF;
4569 ret->edata = exprData_makeOffsetof (qt, s);
4571 if (!ctype_isRealSU (ct))
4573 voptgenerror (FLG_TYPE,
4574 message ("First parameter to offsetof is not a "
4575 "struct or union type (type %s): %s",
4577 exprNode_unparse (ret)),
4584 cstringList_elements (s, el) {
4588 if (ctype_isUndefined (lt))
4592 else if (!ctype_isRealSU (lt))
4594 voptgenerror (FLG_TYPE,
4595 message ("Inner offsetof type is not a "
4596 "struct or union type (type %s before field %s): %s",
4597 ctype_unparse (lt), el,
4598 exprNode_unparse (ret)),
4604 fields = ctype_getFields (ctype_realType (lt));
4605 fld = uentryList_lookupField (fields, el);
4606 DPRINTF (("Try: %s / %s", ctype_unparse (lt), el));
4608 if (uentry_isUndefined (fld))
4610 if (ctype_equal (lt, ct)) {
4611 voptgenerror (FLG_TYPE,
4612 message ("Field %s in offsetof is not the "
4613 "name of a field of %s: %s",
4616 exprNode_unparse (ret)),
4619 voptgenerror (FLG_TYPE,
4620 message ("Deep field %s in offsetof is not the "
4621 "name of a field of %s: %s",
4624 exprNode_unparse (ret)),
4630 lt = uentry_getType (fld);
4633 } end_cstringList_elements;
4635 /* Should report error if its a bit field - behavior is undefined! */
4642 exprNode_sizeofExpr (/*@only@*/ exprNode e)
4646 if (exprNode_isUndefined (e))
4648 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4649 ret->edata = exprData_makeSingle (e);
4650 ret->typ = sizeof_resultType ();
4651 ret->kind = XPR_SIZEOF;
4655 uentry u = exprNode_getUentry (e);
4657 ret = exprNode_createPartialCopy (e);
4658 ret->edata = exprData_makeSingle (e);
4660 ret->typ = sizeof_resultType ();
4661 ret->kind = XPR_SIZEOF;
4663 if (uentry_isValid (u)
4664 && uentry_isRefParam (u)
4665 && ctype_isRealArray (uentry_getType (u)))
4668 (FLG_SIZEOFFORMALARRAY,
4669 message ("Parameter to sizeof is an array-type function parameter: %s",
4670 exprNode_unparse (ret)),
4676 ** sizeof (x) doesn't "really" use x
4683 exprNode_alignofExpr (/*@only@*/ exprNode e)
4687 if (exprNode_isUndefined (e))
4689 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4693 ret = exprNode_createPartialCopy (e);
4696 ret->edata = exprData_makeSingle (e);
4697 ret->typ = sizeof_resultType ();
4698 ret->kind = XPR_ALIGNOF;
4701 ** sizeof (x) doesn't "really" use x
4708 exprNode_cast (/*@only@*/ lltok tok, /*@only@*/ exprNode e, /*@only@*/ qtype q)
4714 if (exprNode_isError (e))
4717 lltok_release (tok);
4718 return exprNode_undefined;
4721 checkMacroParen (e);
4723 c = qtype_getType (q);
4724 t = exprNode_getType (e);
4726 ret = exprNode_createPartialCopy (e);
4728 ret->loc = fileloc_update (ret->loc, lltok_getLoc (tok));
4730 ret->kind = XPR_CAST;
4731 ret->edata = exprData_makeCast (tok, e, q);
4733 if (ctype_isRealSU (ctype_getBaseType (sRef_getType (e->sref))))
4736 ** This is a bit of a hack to avoid a problem
4737 ** when the code does,
4738 ** (some other struct) x
4743 ret->sref = sRef_copy (e->sref);
4744 usymtab_addForceMustAlias (ret->sref, e->sref);
4745 sRef_setTypeFull (ret->sref, c);
4746 DPRINTF (("Cast: %s -> %s", sRef_unparseFull (e->sref),
4747 sRef_unparseFull (ret->sref)));
4751 ret->sref = e->sref;
4752 sRef_setTypeFull (ret->sref, c);
4753 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
4760 ** void * <-> abstract * (if FLG_ABSTVOIDP)
4761 ** abstract * <-> void * (if FLG_ABSTVOIDP)
4764 if (ctype_isVoid (c)) /* cast to void is always okay --- discard value */
4768 else if (ctype_isRealAP (c)) /* casting to array or pointer */
4770 ctype bc = ctype_getBaseType (c);
4771 ctype bt = ctype_getBaseType (t);
4772 ctype rt = ctype_realType (t);
4774 if (ctype_isFunction (ctype_baseArrayPtr (ctype_realType (c)))
4775 && (ctype_isArrayPtr (rt)
4776 && !ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4780 message ("Cast from function pointer type (%t) to "
4781 "non-function pointer (%t): %s",
4782 c, t, exprNode_unparse (ret)),
4786 if (!ctype_isFunction (ctype_baseArrayPtr (c))
4787 && (ctype_isArrayPtr (rt)
4788 && ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4792 message ("Cast from non-function pointer type (%t) to "
4793 "function pointer (%t): %s",
4794 c, t, exprNode_unparse (ret)),
4798 if (exprNode_isZero (e) && context_getFlag (FLG_ZEROPTR) &&
4799 !(ctype_isRealAbstract (bc)
4800 && context_hasAccess (ctype_typeId (bc))))
4802 ; /* okay to cast zero */
4806 if (ctype_isRealAbstract (bc)
4807 && !context_hasAccess (ctype_typeId (bc)))
4809 if (ctype_isVoidPointer (t) || ctype_isUnknown (t))
4813 message ("Cast to underlying abstract type %t: %s",
4814 c, exprNode_unparse (ret)),
4821 message ("Cast to underlying abstract type %t: %s",
4822 c, exprNode_unparse (ret)),
4827 if (ctype_isRealAbstract (bt)
4828 && !context_hasAccess (ctype_typeId (bt)))
4830 if (ctype_isUnknown (c) || ctype_isVoidPointer (c))
4834 message ("Cast from underlying abstract type %t: %s",
4835 t, exprNode_unparse (ret)),
4842 message ("Cast from underlying abstract type %t: %s",
4843 t, exprNode_unparse (ret)),
4851 ctype bt = ctype_realType (ctype_getBaseType (t));
4852 ctype bc = ctype_realType (ctype_getBaseType (c));
4854 if (ctype_isAbstract (bt) && !context_hasAccess (ctype_typeId (bt)))
4856 if (ctype_match (c, t))
4858 if (ctype_equal (c, t))
4862 message ("Redundant cast involving abstract type %t: %s",
4863 bt, exprNode_unparse (ret)),
4871 message ("Cast from abstract type %t: %s",
4872 bt, exprNode_unparse (ret)),
4877 if (ctype_isAbstract (bc)
4878 && !context_hasAccess (ctype_typeId (bc)))
4880 if (ctype_match (c, t))
4886 DPRINTF (("No access to: %s / %d",
4887 ctype_unparse (bc), ctype_typeId (bc)));
4888 DPRINTF (("Context %s %s",
4889 bool_unparse (context_inFunctionLike ()),
4890 context_unparse ()));
4893 message ("Cast to abstract type %t: %s", bc,
4894 exprNode_unparse (ret)),
4900 if (ctype_isAbstract (c))
4902 if (sRef_isExposed (e->sref) || sRef_isOnly (e->sref))
4904 /* okay, cast exposed to abstract */
4905 sRef_clearExKindComplete (ret->sref, fileloc_undefined);
4909 if (ctype_isVisiblySharable (t)
4910 && sRef_isExternallyVisible (e->sref)
4911 && !(ctype_isAbstract (t)
4912 && context_hasAccess (ctype_typeId (t))))
4916 message ("Cast to abstract type from externally visible "
4917 "mutable storage exposes rep of %s: %s",
4919 exprNode_unparse (e)),
4929 evaluationOrderUndefined (lltok op)
4931 int opid = lltok_getTok (op);
4933 return (opid != AND_OP && opid != OR_OP);
4936 static bool checkIntegral (/*@notnull@*/ exprNode e1,
4937 /*@notnull@*/ exprNode e2,
4938 /*@notnull@*/ exprNode ret,
4943 ctype te1 = exprNode_getType (e1);
4944 ctype te2 = exprNode_getType (e2);
4946 ctype tr1 = ctype_realishType (te1);
4947 ctype tr2 = ctype_realishType (te2);
4949 if (ctype_isForceRealInt (&tr1) && ctype_isForceRealInt (&tr2))
4955 if (context_msgStrictOps ())
4957 if (!ctype_isInt (tr1) && !ctype_isInt (tr2))
4959 if (ctype_sameName (te1, te2))
4963 message ("Operands of %s are non-integer (%t): %s",
4964 lltok_unparse (op), te1,
4965 exprNode_unparse (ret)),
4972 message ("Operands of %s are non-integers (%t, %t): %s",
4973 lltok_unparse (op), te1, te2,
4974 exprNode_unparse (ret)),
4978 else if (!ctype_isInt (tr1))
4982 message ("Left operand of %s is non-integer (%t): %s",
4983 lltok_unparse (op), te1, exprNode_unparse (ret)),
4987 /* !ctype_isInt (te2) */
4991 message ("Right operand of %s is non-integer (%t): %s",
4992 lltok_unparse (op), te2, exprNode_unparse (ret)),
5002 ** returns exprNode representing e1 op e2
5004 ** uses msg if there are errors
5005 ** can be used for both assignment ops and regular ops
5010 static /*@only@*/ /*@notnull@*/ exprNode
5011 exprNode_makeOp (/*@keep@*/ exprNode e1, /*@keep@*/ exprNode e2,
5012 /*@keep@*/ lltok op)
5014 ctype te1, te2, tr1, tr2, tret;
5015 int opid = lltok_getTok (op);
5016 bool hasError = FALSE;
5019 if (exprNode_isError (e1))
5021 ret = exprNode_createPartialNVCopy (e2);
5025 ret = exprNode_createPartialNVCopy (e1);
5028 ret->val = multiVal_undefined;
5030 ret->edata = exprData_makeOp (e1, e2, op);
5032 if (exprNode_isError (e1) || exprNode_isError (e2))
5034 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5035 || opid == EQ_OP || opid == NE_OP
5036 || opid == AND_OP || opid == OR_OP)
5038 ret->typ = ctype_bool;
5041 if (exprNode_isDefined (e1))
5043 exprNode_checkUse (ret, e1->sref, e1->loc);
5046 if (exprNode_isDefined (e2))
5048 exprNode_mergeUSs (ret, e2);
5049 exprNode_checkUse (ret, e2->sref, e2->loc);
5055 tret = ctype_unknown;
5056 te1 = exprNode_getType (e1);
5057 DPRINTF (("te1 = %s / %s", exprNode_unparse (e1), ctype_unparse (te1)));
5059 te2 = exprNode_getType (e2);
5061 tr1 = ctype_realishType (te1);
5062 tr2 = ctype_realishType (te2);
5066 ret->guards = guardSet_or (ret->guards, e2->guards);
5068 else if (opid == AND_OP)
5070 ret->guards = guardSet_and (ret->guards, e2->guards);
5077 if (opid == EQ_OP || opid == NE_OP)
5079 exprNode temp1 = e1, temp2 = e2;
5081 /* could do NULL == x */
5083 if (exprNode_isNullValue (e1) || exprNode_isUnknownConstant (e1))
5085 temp1 = e2; temp2 = e1;
5088 if (exprNode_isNullValue (temp2) || exprNode_isUnknownConstant (temp2))
5090 reflectNullTest (temp1, (opid == NE_OP));
5091 guardSet_free (ret->guards);
5092 ret->guards = guardSet_copy (temp1->guards);
5096 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5097 || opid == EQ_OP || opid == NE_OP || opid == AND_OP || opid == OR_OP)
5102 if (anyAbstract (tr1, tr2) &&
5103 (!((ctype_isRealBool (te1) || ctype_isRealBool (te2)) &&
5104 (opid == AND_OP || opid == OR_OP
5105 || opid == EQ_OP || opid == NE_OP))))
5107 abstractOpError (tr1, tr2, op, e1, e2, e1->loc, e2->loc);
5109 else if (ctype_isUnknown (te1) || ctype_isUnknown (te2))
5111 /* unknown types, no comparisons possible */
5117 case TMULT: /* multiplication and division: */
5119 case MUL_ASSIGN: /* numeric, numeric -> numeric */
5120 case DIV_ASSIGN: /* */
5122 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5125 case TPLUS: /* addition and subtraction: */
5126 case TMINUS: /* pointer, int -> pointer */
5127 case SUB_ASSIGN: /* int, pointer -> pointer */
5128 case ADD_ASSIGN: /* numeric, numeric -> numeric */
5130 tr1 = ctype_fixArrayPtr (tr1);
5132 if ((ctype_isRealPointer (tr1) && !exprNode_isNullValue (e1))
5133 && (!ctype_isRealPointer (tr2) && ctype_isRealInt (tr2)))
5137 if (context_msgPointerArith ())
5141 message ("Pointer arithmetic (%t, %t): %s",
5142 te1, te2, exprNode_unparse (ret)),
5147 ** Swap terms so e1 is always the pointer
5150 if (ctype_isRealPointer (tr1))
5156 exprNode_swap (e1, e2);
5160 if (sRef_possiblyNull (e1->sref)
5161 && !usymtab_isGuarded (e1->sref))
5164 (FLG_NULLPOINTERARITH,
5165 message ("Pointer arithmetic involving possibly "
5166 "null pointer %s: %s",
5167 exprNode_unparse (e1),
5168 exprNode_unparse (ret)),
5172 ret->sref = sRef_copy (e1->sref);
5174 /* start modifications */
5175 /* added by Seejo on 4/16/2000 */
5177 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5179 if ((sRef_isPossiblyNullTerminated (e1->sref)) || (sRef_isNullTerminated(e1->sref))) {
5180 //if (sRef_isKnown (e->sref)) {
5181 //ret->sref = sRef_makeAddress (e->sref);
5186 added ugly fixed to stop
5187 program from crashing on point + int +int
5188 one day I'll fix this or ask Seejo wtf the codes supposed to do. */
5190 if (!multiVal_isInt (e2->val) )
5194 val = (int) multiVal_forceInt (e2->val);
5196 /* Operator : + or += */
5197 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5198 if (sRef_getSize(e1->sref) >= val) {/* Incrementing the pointer by
5199 val should not result in a
5200 size < 0 (size = 0 is ok !) */
5202 sRef_setSize (ret->sref, sRef_getSize(e1->sref) - val);
5204 if (sRef_getLen(e1->sref) == val) { /* i.e. the character at posn val is \0 */
5205 sRef_setNotNullTerminatedState(ret->sref);
5206 sRef_resetLen (ret->sref);
5208 sRef_setNullTerminatedState(ret->sref);
5209 sRef_setLen (ret->sref, sRef_getLen(e1->sref) - val);
5214 /* Operator : - or -= */
5215 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5216 if (sRef_getSize(e1->sref) >= 0) {
5217 sRef_setSize (ret->sref, sRef_getSize(e1->sref) + val);
5218 sRef_setLen (ret->sref, sRef_getLen(e1->sref) + val);
5223 /* end modifications */
5225 sRef_setNullError (ret->sref);
5228 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5229 ** since is points to storage that should not be deallocated
5230 ** through this pointer.
5233 if (sRef_isOnly (ret->sref)
5234 || sRef_isFresh (ret->sref))
5236 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5241 else if ((!ctype_isRealPointer(tr1) && ctype_isRealInt (tr1))
5242 && (ctype_isRealPointer (tr2) && !exprNode_isNullValue (e2)))
5244 if (context_msgPointerArith ())
5248 message ("Pointer arithmetic (%t, %t): %s",
5249 te1, te2, exprNode_unparse (ret)),
5253 if (sRef_possiblyNull (e1->sref)
5254 && !usymtab_isGuarded (e1->sref))
5257 (FLG_NULLPOINTERARITH,
5258 message ("Pointer arithmetic involving possibly "
5259 "null pointer %s: %s",
5260 exprNode_unparse (e2),
5261 exprNode_unparse (ret)),
5265 ret->sref = sRef_copy (e2->sref);
5267 /* start modifications */
5268 /* added by Seejo on 4/16/2000 */
5270 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5273 if ((sRef_isPossiblyNullTerminated (e2->sref)) || (sRef_isNullTerminated(e2->sref))) {
5274 //if (sRef_isKnown (e->sref)) {
5275 //ret->sref = sRef_makeAddress (e->sref);
5278 int val = (int) multiVal_forceInt (e1->val);
5280 /* Operator : + or += */
5281 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5282 if (sRef_getSize(e2->sref) >= val) {/* Incrementing the pointer by
5283 val should not result in a
5284 size < 0 (size = 0 is ok !) */
5286 sRef_setSize (ret->sref, sRef_getSize(e2->sref) - val);
5288 if (sRef_getLen(e2->sref) == val) { /* i.e. the character at posn val is \0 */
5289 sRef_setNotNullTerminatedState(ret->sref);
5290 sRef_resetLen (ret->sref);
5292 sRef_setNullTerminatedState(ret->sref);
5293 sRef_setLen (ret->sref, sRef_getLen(e2->sref) - val);
5298 /* Operator : - or -= */
5299 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5300 if (sRef_getSize(e2->sref) >= 0) {
5301 sRef_setSize (ret->sref, sRef_getSize(e2->sref) + val);
5302 sRef_setLen (ret->sref, sRef_getLen(e2->sref) + val);
5307 /* end modifications */
5309 sRef_setNullError (ret->sref);
5312 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5313 ** since is points to storage that should not be deallocated
5314 ** through this pointer.
5317 if (sRef_isOnly (ret->sref)
5318 || sRef_isFresh (ret->sref)) {
5319 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5323 ret->sref = e2->sref;
5327 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5332 case LEFT_ASSIGN: /* Shifts: should be unsigned values */
5336 case TAMPERSAND: /* bitwise & */
5338 case TCIRC: /* ^ (XOR) */
5343 bool reported = FALSE;
5344 flagcode code = FLG_BITWISEOPS;
5346 if (opid == LEFT_OP || opid == LEFT_ASSIGN
5347 || opid == RIGHT_OP || opid == RIGHT_ASSIGN) {
5348 code = FLG_SHIFTSIGNED;
5351 if (!ctype_isUnsigned (tr1))
5353 if (exprNode_isNonNegative (e1)) {
5356 reported = optgenerror
5358 message ("Left operand of %s is not unsigned value (%t): %s",
5359 lltok_unparse (op), te1,
5360 exprNode_unparse (ret)),
5370 /* right need not be signed for shifts */
5371 if (code != FLG_SHIFTSIGNED
5372 && !ctype_isUnsigned (tr2))
5374 if (!exprNode_isNonNegative (e2)) {
5375 reported = optgenerror
5377 message ("Right operand of %s is not unsigned value (%t): %s",
5378 lltok_unparse (op), te2,
5379 exprNode_unparse (ret)),
5387 if (!checkIntegral (e1, e2, ret, op)) {
5388 te1 = ctype_unknown;
5392 DPRINTF (("Set: %s", ctype_unparse (te1)));
5395 ** tret is the widest type of te1 and te2
5398 tret = ctype_widest (te1, te2);
5403 if (checkIntegral (e1, e2, ret, op)) {
5406 tret = ctype_unknown;
5411 case TLT: /* comparisons */
5412 case TGT: /* numeric, numeric -> bool */
5413 if ((ctype_isReal (tr1) && !ctype_isInt (tr1))
5414 || (ctype_isReal (tr2) && !ctype_isInt (tr2)))
5417 bool fepsilon = FALSE;
5419 if (!ctype_isReal (rtype) || ctype_isInt (rtype))
5424 if (opid == TLT || opid == TGT)
5426 uentry ue1 = exprNode_getUentry (e1);
5427 uentry ue2 = exprNode_getUentry (e2);
5430 ** FLT_EPSILON, etc. really is a variable, not
5434 if (uentry_isVariable (ue1))
5436 cstring uname = uentry_rawName (ue1);
5438 if (cstring_equalLit (uname, "FLT_EPSILON")
5439 || cstring_equalLit (uname, "DBL_EPSILON")
5440 || cstring_equalLit (uname, "LDBL_EPSILON"))
5446 if (uentry_isVariable (ue2))
5448 cstring uname = uentry_rawName (ue2);
5450 if (cstring_equalLit (uname, "FLT_EPSILON")
5451 || cstring_equalLit (uname, "DBL_EPSILON")
5452 || cstring_equalLit (uname, "LDBL_EPSILON"))
5461 ; /* Don't complain. */
5467 message ("Dangerous comparison involving %s types: %s",
5468 ctype_unparse (rtype),
5469 exprNode_unparse (ret)),
5478 ** Types should match.
5481 if (!exprNode_matchTypes (e1, e2))
5483 hasError = gentypeerror
5485 message ("Operands of %s have incompatible types (%t, %t): %s",
5486 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5492 || (ctype_isForceRealNumeric (&tr1)
5493 && ctype_isForceRealNumeric (&tr2)) ||
5494 (ctype_isRealPointer (tr1) && ctype_isRealPointer (tr2)))
5500 if ((ctype_isRealNumeric (tr1) && ctype_isRealPointer (tr2)) ||
5501 (ctype_isRealPointer (tr1) && ctype_isRealNumeric (tr2)))
5505 message ("Comparison of pointer and numeric (%t, %t): %s",
5506 te1, te2, exprNode_unparse (ret)),
5511 (void) checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5516 /* EQ_OP should NOT be used with booleans (unless one is FALSE) */
5518 if ((opid == EQ_OP || opid == NE_OP) &&
5519 ctype_isDirectBool (tr1) && ctype_isDirectBool (tr2))
5522 ** is one a variable?
5525 if (uentry_isVariable (exprNode_getUentry (e1))
5526 || uentry_isVariable (exprNode_getUentry (e2)))
5529 ** comparisons with FALSE are okay
5532 if (exprNode_isFalseConstant (e1)
5533 || exprNode_isFalseConstant (e2))
5542 ("Use of %q with %s variables (risks inconsistency because "
5543 "of multiple true values): %s",
5544 cstring_makeLiteral ((opid == EQ_OP) ? "==" : "!="),
5545 context_printBoolName (), exprNode_unparse (ret)),
5552 case AND_OP: /* bool, bool -> bool */
5555 if (ctype_isForceRealBool (&tr1) && ctype_isForceRealBool (&tr2))
5561 if (context_maybeSet (FLG_BOOLOPS))
5563 if (!ctype_isRealBool (te1) && !ctype_isRealBool (te2))
5565 if (ctype_sameName (te1, te2))
5569 message ("Operands of %s are non-boolean (%t): %s",
5570 lltok_unparse (op), te1,
5571 exprNode_unparse (ret)),
5579 ("Operands of %s are non-booleans (%t, %t): %s",
5580 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5584 else if (!ctype_isRealBool (te1))
5588 message ("Left operand of %s is non-boolean (%t): %s",
5589 lltok_unparse (op), te1, exprNode_unparse (ret)),
5592 else if (!ctype_isRealBool (te2))
5596 message ("Right operand of %s is non-boolean (%t): %s",
5597 lltok_unparse (op), te2, exprNode_unparse (ret)),
5610 (cstring_makeLiteral
5611 ("There has been a problem in the parser. This is believed to result "
5612 "from a problem with bison v. 1.25. Please try rebuidling LCLint "
5613 "using the pre-compiled grammar files by commenting out the "
5614 "BISON= line in the top-level Makefile."));
5619 DPRINTF (("Return type: %s", ctype_unparse (tret)));
5622 exprNode_checkUse (ret, e1->sref, e1->loc);
5623 exprNode_mergeUSs (ret, e2);
5624 exprNode_checkUse (ret, e2->sref, e2->loc);
5630 exprNode_op (/*@only@*/ exprNode e1, /*@keep@*/ exprNode e2,
5631 /*@only@*/ lltok op)
5635 checkMacroParen (e1);
5636 checkMacroParen (e2);
5638 if (evaluationOrderUndefined (op) && context_maybeSet (FLG_EVALORDER))
5640 checkExpressionDefined (e1, e2, op);
5643 ret = exprNode_makeOp (e1, e2, op);
5648 void exprNode_checkAssignMod (exprNode e1, exprNode ret)
5651 ** This is somewhat bogus!
5653 ** Assigning to a nested observer in a non-observer datatype
5654 ** should not produce an error.
5657 sRef ref = exprNode_getSref (e1);
5659 DPRINTF (("Check assign mod: %s",
5660 sRef_unparseFull (ref)));
5662 if (sRef_isObserver (ref)
5663 || ((sRef_isFileStatic (ref) || sRef_isFileOrGlobalScope (ref))
5664 && ctype_isArray (ctype_realType (sRef_getType (ref)))))
5666 sRef base = sRef_getBase (ref);
5668 if (sRef_isValid (base) && sRef_isObserver (base))
5670 exprNode_checkModify (e1, ret);
5674 exprNode_checkModifyVal (e1, ret);
5679 exprNode_checkModify (e1, ret);
5684 exprNode_assign (/*@only@*/ exprNode e1,
5685 /*@only@*/ exprNode e2, /*@only@*/ lltok op)
5687 bool isalloc = FALSE;
5688 bool isjustalloc = FALSE;
5691 DPRINTF (("%s [%s] <- %s [%s]",
5692 exprNode_unparse (e1),
5693 ctype_unparse (e1->typ),
5694 exprNode_unparse (e2),
5695 ctype_unparse (e2->typ)));
5697 if (lltok_getTok (op) != TASSIGN)
5699 ret = exprNode_makeOp (e1, e2, op);
5703 ret = exprNode_createPartialCopy (e1);
5704 ret->kind = XPR_ASSIGN;
5705 ret->edata = exprData_makeOp (e1, e2, op);
5707 if (!exprNode_isError (e2))
5709 ret->sets = sRefSet_union (ret->sets, e2->sets);
5710 ret->msets = sRefSet_union (ret->msets, e2->msets);
5711 ret->uses = sRefSet_union (ret->uses, e2->uses);
5715 checkExpressionDefined (e1, e2, op);
5717 if (exprNode_isError (e1))
5719 if (!exprNode_isError (e2))
5721 ret->loc = fileloc_update (ret->loc, e2->loc);
5725 ret->loc = fileloc_update (ret->loc, g_currentloc);
5729 if (!exprNode_isError (e2))
5731 checkMacroParen (e2);
5734 if (exprNode_isDefined (e1))
5736 if (sRef_isMacroParamRef (e1->sref))
5738 if (context_inIterDef ())
5740 uentry ue = sRef_getUentry (e1->sref);
5742 if (uentry_isYield (ue))
5748 if (fileloc_isDefined (e1->loc))
5752 message ("Assignment to non-yield iter parameter: %q",
5753 sRef_unparse (e1->sref)),
5760 message ("Assignment to non-yield iter parameter: %q",
5761 sRef_unparse (e1->sref)),
5768 if (fileloc_isDefined (e1->loc))
5772 message ("Assignment to macro parameter: %q",
5773 sRef_unparse (e1->sref)),
5780 message ("Assignment to macro parameter: %q",
5781 sRef_unparse (e1->sref)),
5785 exprNode_checkAssignMod (e1, ret); /* evans 2001-07-22 */
5790 exprNode_checkAssignMod (e1, ret);
5793 if (exprNode_isDefined (e2))
5795 if (lltok_getTok (op) == TASSIGN)
5797 ctype te1 = exprNode_getType (e1);
5798 ctype te2 = exprNode_getType (e2);
5800 if (!ctype_forceMatch (te1, te2))
5802 if (exprNode_matchLiteral (te1, e2))
5810 message ("Assignment of %t to %t: %s %s %s",
5811 te2, te1, exprNode_unparse (e1),
5813 exprNode_unparse (e2)),
5819 exprNode_mergeUSs (ret, e2);
5820 exprNode_checkUse (ret, e2->sref, e2->loc);
5822 doAssign (e1, e2, FALSE);
5823 ret->sref = e1->sref;
5827 if (exprNode_isDefined (e2))
5829 exprNode_mergeUSs (ret, e2);
5830 exprNode_checkUse (ret, e2->sref, e2->loc);
5834 if (sRef_isPointer (e1->sref) && !sRef_isMacroParamRef (e1->sref))
5836 exprNode_checkUse (ret, sRef_getBase (e1->sref), e1->loc);
5839 isjustalloc = sRef_isJustAllocated (e1->sref);
5840 isalloc = sRef_isAllocated (e1->sref);
5842 if (sRef_isField (e1->sref))
5844 sRef root = sRef_getRootBase (sRef_getBase (e1->sref));
5846 if (!sRef_isAllocated (root) && !sRef_isMacroParamRef (root))
5848 exprNode_checkUse (ret, root, e1->loc);
5854 ** be careful! this defines e1->sref.
5857 /* evans 2001-07-22: removed if (!sRef_isMacroParamRef (e1->sref)) */
5859 exprNode_checkSet (ret, e1->sref);
5863 sRef_setAllocatedComplete (e1->sref, exprNode_isDefined (e2)
5864 ? e2->loc : e1->loc);
5870 sRef_setAllocatedShallowComplete (e1->sref, exprNode_loc (e2));
5879 exprNode_cond (/*@keep@*/ exprNode pred, /*@keep@*/ exprNode ifclause,
5880 /*@keep@*/ exprNode elseclause)
5884 if (!exprNode_isError (pred))
5886 ret = exprNode_createPartialCopy (pred);
5887 checkMacroParen (pred);
5888 exprNode_checkPred (cstring_makeLiteralTemp ("conditional"), pred);
5890 if (!exprNode_isError (ifclause))
5892 checkMacroParen (ifclause); /* update macro counts! */
5894 if (!exprNode_isError (elseclause))
5896 checkMacroParen (elseclause);
5898 if (!exprNode_matchTypes (ifclause, elseclause))
5901 (exprNode_getType (ifclause),
5903 exprNode_getType (elseclause),
5905 message ("Conditional clauses are not of same type: "
5907 exprNode_unparse (ifclause),
5908 exprNode_getType (ifclause),
5909 exprNode_unparse (elseclause),
5910 exprNode_getType (elseclause)),
5913 ret->sref = sRef_undefined;
5914 ret->typ = ctype_unknown;
5919 /* for now...should merge the states */
5920 ret->sref = ifclause->sref;
5921 ret->typ = ifclause->typ;
5923 if (exprNode_isNullValue (ifclause))
5925 ret->typ = elseclause->typ;
5929 exprNode_checkUse (ret, pred->sref, pred->loc);
5930 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5931 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5933 exprNode_mergeCondUSs (ret, ifclause, elseclause);
5938 ret->typ = ifclause->typ;
5940 exprNode_checkUse (pred, pred->sref, pred->loc);
5941 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5943 exprNode_mergeCondUSs (ret, ifclause, exprNode_undefined);
5948 if (!exprNode_isError (elseclause))
5950 ret->typ = elseclause->typ;
5952 exprNode_checkUse (pred, pred->sref, pred->loc);
5953 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5955 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
5959 else /* pred is error */
5961 if (!exprNode_isError (ifclause))
5963 ret = exprNode_createSemiCopy (ifclause);
5965 checkMacroParen (ifclause); /* update macro counts! */
5967 if (!exprNode_isError (elseclause))
5969 checkMacroParen (elseclause);
5971 ret->typ = ifclause->typ;
5973 if (!ctype_forceMatch (ifclause->typ, elseclause->typ))
5976 (exprNode_getType (ifclause),
5978 exprNode_getType (elseclause),
5980 message ("Conditional clauses are not of same type: "
5982 exprNode_unparse (ifclause),
5983 exprNode_getType (ifclause),
5984 exprNode_unparse (elseclause),
5985 exprNode_getType (elseclause)),
5988 ret->typ = ctype_unknown;
5992 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5993 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5995 exprNode_mergeCondUSs (ret, ifclause, elseclause);
5998 else if (!exprNode_isError (elseclause)) /* pred, if errors */
6000 ret = exprNode_createSemiCopy (ifclause);
6002 ret->typ = elseclause->typ;
6003 checkMacroParen (elseclause);
6005 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6006 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6008 else /* all errors! */
6010 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6014 ret->kind = XPR_COND;
6015 ret->edata = exprData_makeCond (pred, ifclause, elseclause);
6017 if (exprNode_isDefined (ifclause) && exprNode_isDefined (elseclause))
6019 exprNode_combineControl (ret, ifclause, elseclause);
6026 exprNode_vaArg (/*@only@*/ lltok tok, /*@only@*/ exprNode arg, /*@only@*/ qtype qt)
6028 ctype totype = qtype_getType (qt);
6030 exprNode_createPartialLocCopy (arg, fileloc_copy (lltok_getLoc (tok)));
6034 ** check use of va_arg : <valist>, type -> type
6037 if (exprNode_isError (arg))
6042 targ = exprNode_getType (arg);
6045 ** arg should have be a pointer
6048 if (!ctype_isUA (targ) ||
6049 (!usymId_equal (ctype_typeId (targ),
6050 usymtab_getTypeId (cstring_makeLiteralTemp ("va_list")))))
6054 message ("First argument to va_arg is not a va_list (type %t): %s",
6055 targ, exprNode_unparse (arg)),
6059 exprNode_checkSet (ret, arg->sref);
6063 ** return type is totype
6067 ret->kind = XPR_VAARG;
6068 ret->edata = exprData_makeCast (tok, arg, qt);
6073 exprNode exprNode_labelMarker (/*@only@*/ cstring label)
6075 exprNode ret = exprNode_createPlain (ctype_undefined);
6076 ret->kind = XPR_LABEL;
6077 ret->edata = exprData_makeLiteral (label);
6078 ret->isJumpPoint = TRUE;
6080 return (ret); /* for now, ignore label */
6083 exprNode exprNode_notReached (/*@returned@*/ exprNode stmt)
6085 if (exprNode_isDefined (stmt))
6087 stmt->isJumpPoint = TRUE;
6089 /* This prevent stray no return path errors, etc. */
6090 stmt->exitCode = XK_MUSTEXIT;
6096 bool exprNode_isDefaultMarker (exprNode e)
6098 if (exprNode_isDefined (e))
6100 return (e->kind == XPR_DEFAULT || e->kind == XPR_FTDEFAULT);
6106 bool exprNode_isCaseMarker (exprNode e)
6108 if (exprNode_isDefined (e))
6110 return (e->kind == XPR_FTCASE || e->kind == XPR_CASE);
6116 bool exprNode_isLabelMarker (exprNode e)
6118 if (exprNode_isDefined (e))
6120 return (e->kind == XPR_LABEL);
6126 exprNode exprNode_caseMarker (/*@only@*/ exprNode test, bool fallThrough)
6128 exprNode ret = exprNode_createPartialCopy (test);
6130 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6132 if (exprNode_isError (test)) {
6136 exprNode_checkUse (ret, test->sref, test->loc);
6138 usymtab_setExitCode (ret->exitCode);
6142 usymtab_setMustBreak ();
6145 ret->edata = exprData_makeSingle (test);
6146 ret->isJumpPoint = TRUE;
6152 exprNode exprNode_caseStatement (/*@only@*/ exprNode test, /*@only@*/ exprNode stmt, bool fallThrough)
6154 exprNode ret = exprNode_createPartialCopy (test);
6156 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6157 ret->edata = exprData_makePair (test, stmt);
6158 ret->isJumpPoint = TRUE;
6160 if (exprNode_isError (test))
6165 exprNode_checkUse (ret, test->sref, test->loc);
6167 if (exprNode_isError (stmt))
6172 exprNode_mergeUSs (ret, stmt);
6174 ret->exitCode = stmt->exitCode;
6175 ret->mustBreak = stmt->mustBreak;
6176 ret->canBreak = stmt->canBreak;
6178 usymtab_setExitCode (ret->exitCode);
6182 usymtab_setMustBreak ();
6189 /*@notnull@*/ /*@only@*/ exprNode
6190 exprNode_defaultMarker (/*@only@*/ lltok def, bool fallThrough)
6192 exprNode ret = exprNode_createTok (def);
6194 ret->isJumpPoint = TRUE;
6195 ret->kind = fallThrough ? XPR_FTDEFAULT : XPR_DEFAULT;
6200 exprNode_mayEscape (exprNode e)
6202 if (exprNode_isDefined (e))
6204 return exitkind_couldEscape (e->exitCode);
6210 exprNode_mustBreak (exprNode e)
6212 if (exprNode_isDefined (e))
6214 return e->mustBreak;
6220 exprNode_mustEscape (exprNode e)
6222 if (exprNode_isDefined (e))
6224 return exitkind_mustEscape (e->exitCode) || exprNode_mustBreak (e);
6231 exprNode_errorEscape (exprNode e)
6233 if (exprNode_isDefined (e))
6235 return exitkind_isError (e->exitCode);
6241 exprNode exprNode_concat (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
6243 exprNode ret = exprNode_createPartialCopy (e1);
6245 DPRINTF (("Concat: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6247 ret->edata = exprData_makePair (e1, e2);
6248 ret->kind = XPR_STMTLIST;
6250 if (exprNode_isDefined (e1))
6252 ret->isJumpPoint = e1->isJumpPoint;
6253 ret->canBreak = e1->canBreak;
6257 if (exprNode_isDefined (e2))
6259 ret->loc = fileloc_update (ret->loc, e2->loc);
6263 if (exprNode_isDefined (e2))
6265 ret->exitCode = e2->exitCode;
6266 ret->mustBreak = e2->mustBreak;
6267 if (e2->canBreak) ret->canBreak = TRUE;
6271 ** if e1 must return, then e2 is unreachable!
6274 if (exprNode_isDefined (e1) && exprNode_isDefined (e2))
6276 if ((exprNode_mustEscape (e1) || exprNode_mustBreak (e1))
6277 && !(e2->isJumpPoint))
6279 if (context_getFlag (FLG_UNREACHABLE))
6283 if (e2->kind == XPR_STMT)
6285 nr = exprData_getUopNode (e2->edata);
6288 if ((nr->kind == XPR_TOK
6289 && lltok_isSemi (exprData_getTok (nr->edata))))
6291 /* okay to have unreachable ";" */
6292 ret->exitCode = XK_MUSTEXIT;
6293 ret->canBreak = TRUE;
6297 if (optgenerror (FLG_UNREACHABLE,
6298 message ("Unreachable code: %s",
6299 exprNode_unparseFirst (nr)),
6302 ret->isJumpPoint = TRUE;
6303 ret->mustBreak = FALSE;
6304 ret->exitCode = XK_ERROR;
6305 DPRINTF (("Jump point: %s", exprNode_unparse (ret)));
6309 ret->exitCode = XK_MUSTEXIT;
6310 ret->canBreak = TRUE;
6318 if ((e2->kind == XPR_CASE || e2->kind == XPR_DEFAULT))
6321 ** We want a warning anytime we have:
6323 ** yyy; <<<- no break or return
6327 exprNode lastStmt = exprNode_lastStatement (e1);
6329 if (exprNode_isDefined (lastStmt)
6330 && !exprNode_mustEscape (lastStmt)
6331 && !exprNode_mustBreak (lastStmt)
6332 && !exprNode_isCaseMarker (lastStmt)
6333 && !exprNode_isDefaultMarker (lastStmt)
6334 && !exprNode_isLabelMarker (lastStmt))
6336 voptgenerror (FLG_CASEBREAK,
6338 ("Fall through case (no preceeding break)"),
6345 exprNode_mergeUSs (ret, e2);
6347 usymtab_setExitCode (ret->exitCode);
6351 usymtab_setMustBreak ();
6357 exprNode exprNode_createTok (/*@only@*/ lltok t)
6359 exprNode ret; /*@i23 if on same line, bad things happen...!@*/
6360 ret = exprNode_create (ctype_unknown);
6361 ret->kind = XPR_TOK;
6362 ret->edata = exprData_makeTok (t);
6366 exprNode exprNode_statement (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6368 if (!exprNode_isError (e))
6370 exprNode_checkStatement(e);
6373 return (exprNode_statementError (e, t));
6376 static exprNode exprNode_statementError (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6378 exprNode ret = exprNode_createPartialCopy (e);
6380 if (!exprNode_isError (e))
6382 if (e->kind != XPR_ASSIGN)
6384 exprNode_checkUse (ret, e->sref, e->loc);
6387 ret->exitCode = e->exitCode;
6388 ret->canBreak = e->canBreak;
6389 ret->mustBreak = e->mustBreak;
6392 ret->edata = exprData_makeUop (e, t);
6393 ret->kind = XPR_STMT;
6398 exprNode exprNode_checkExpr (/*@returned@*/ exprNode e)
6400 if (!exprNode_isError (e))
6402 if (e->kind != XPR_ASSIGN)
6404 exprNode_checkUse (e, e->sref, e->loc);
6411 void exprNode_produceGuards (exprNode pred)
6413 if (!exprNode_isError (pred))
6415 if (ctype_isRealPointer (pred->typ))
6417 pred->guards = guardSet_addTrueGuard (pred->guards, pred->sref);
6420 exprNode_checkUse (pred, pred->sref, pred->loc);
6421 exprNode_resetSref (pred);
6425 exprNode exprNode_makeBlock (/*@only@*/ exprNode e)
6427 exprNode ret = exprNode_createPartialCopy (e);
6429 if (!exprNode_isError (e))
6431 ret->exitCode = e->exitCode;
6432 ret->canBreak = e->canBreak;
6433 ret->mustBreak = e->mustBreak;
6436 ret->edata = exprData_makeSingle (e);
6437 ret->kind = XPR_BLOCK;
6441 bool exprNode_isBlock (exprNode e)
6443 return (exprNode_isDefined (e)
6444 && ((e)->kind == XPR_BLOCK));
6447 bool exprNode_isAssign (exprNode e)
6449 if (exprNode_isDefined (e))
6451 return (e->kind == XPR_ASSIGN);
6457 bool exprNode_isEmptyStatement (exprNode e)
6459 return (exprNode_isDefined (e)
6460 && (e->kind == XPR_TOK)
6461 && (lltok_isSemi (exprData_getTok (e->edata))));
6464 exprNode exprNode_if (/*@only@*/ exprNode pred, /*@only@*/ exprNode tclause)
6467 bool emptyErr = FALSE;
6469 if (context_maybeSet (FLG_IFEMPTY))
6471 if (exprNode_isEmptyStatement (tclause))
6473 emptyErr = optgenerror (FLG_IFEMPTY,
6475 ("Body of if statement is empty"),
6476 exprNode_loc (tclause));
6480 if (!emptyErr && context_maybeSet (FLG_IFBLOCK))
6482 if (exprNode_isDefined (tclause)
6483 && !exprNode_isBlock (tclause))
6485 voptgenerror (FLG_IFBLOCK,
6487 ("Body of if statement is not a block: %s",
6488 exprNode_unparse (tclause)),
6489 exprNode_loc (tclause));
6493 if (exprNode_isError (pred))
6495 if (exprNode_isError (tclause))
6497 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6501 ret = exprNode_createPartialCopy (tclause);
6506 if (exprNode_mustEscape (pred))
6510 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6511 exprNode_loc (pred));
6514 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6515 exprNode_checkUse (pred, pred->sref, pred->loc);
6517 if (!exprNode_isError (tclause))
6519 exprNode_mergeCondUSs (pred, tclause, exprNode_undefined);
6522 ret = exprNode_createPartialCopy (pred);
6526 ret->edata = exprData_makePair (pred, tclause);
6528 ret->exitCode = XK_UNKNOWN;
6530 if (exprNode_isDefined (tclause))
6532 ret->exitCode = exitkind_makeConditional (tclause->exitCode);
6533 ret->canBreak = tclause->canBreak;
6534 ret->sets = sRefSet_union (ret->sets, tclause->sets);
6535 ret->msets = sRefSet_union (ret->msets, tclause->msets);
6536 ret->uses = sRefSet_union (ret->uses, tclause->uses);
6539 ret->mustBreak = FALSE;
6544 exprNode exprNode_ifelse (/*@only@*/ exprNode pred,
6545 /*@only@*/ exprNode tclause,
6546 /*@only@*/ exprNode eclause)
6549 bool tEmptyErr = FALSE;
6550 bool eEmptyErr = FALSE;
6552 if (context_maybeSet (FLG_IFEMPTY))
6554 if (exprNode_isEmptyStatement (tclause))
6556 tEmptyErr = optgenerror
6559 ("Body of if clause of if statement is empty"),
6560 exprNode_loc (tclause));
6563 if (exprNode_isEmptyStatement (eclause))
6565 eEmptyErr = optgenerror
6568 ("Body of else clause of if statement is empty"),
6569 exprNode_loc (eclause));
6573 if (context_maybeSet (FLG_IFBLOCK))
6576 && exprNode_isDefined (tclause)
6577 && !exprNode_isBlock (tclause))
6579 voptgenerror (FLG_IFBLOCK,
6581 ("Body of if clause of if statement is not a block: %s",
6582 exprNode_unparse (tclause)),
6583 exprNode_loc (tclause));
6587 && exprNode_isDefined (eclause)
6588 && !exprNode_isBlock (eclause)
6589 && !(eclause->kind == XPR_IF)
6590 && !(eclause->kind == XPR_IFELSE))
6595 ("Body of else clause of if statement is not a block: %s",
6596 exprNode_unparse (eclause)),
6597 exprNode_loc (eclause));
6601 if (context_maybeSet (FLG_ELSEIFCOMPLETE))
6603 if (exprNode_isDefined (eclause)
6604 && (eclause->kind == XPR_IF))
6606 voptgenerror (FLG_ELSEIFCOMPLETE,
6607 message ("Incomplete else if logic (no final else): %s",
6608 exprNode_unparse (eclause)),
6609 exprNode_loc (eclause));
6613 if (exprNode_isError (pred))
6615 if (exprNode_isError (tclause))
6617 if (exprNode_isError (eclause))
6619 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6623 ret = exprNode_createPartialCopy (eclause);
6628 ret = exprNode_createPartialCopy (tclause);
6631 else /* pred is okay */
6633 ret = exprNode_createPartialCopy (pred);
6635 if (exprNode_mustEscape (pred))
6639 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6640 exprNode_loc (pred));
6643 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6644 exprNode_checkUse (ret, pred->sref, pred->loc);
6646 exprNode_mergeCondUSs (ret, tclause, eclause);
6649 ret->kind = XPR_IFELSE;
6650 ret->edata = exprData_makeCond (pred, tclause, eclause);
6652 if (exprNode_isDefined (tclause) && exprNode_isDefined (eclause))
6654 exprNode_combineControl (ret, tclause, eclause);
6655 ret->loc = fileloc_update (ret->loc, eclause->loc);
6662 ** *allpaths <- TRUE iff all executions paths must go through the switch
6666 checkSwitchExpr (exprNode test, /*@dependent@*/ exprNode e, /*@out@*/ bool *allpaths)
6668 exprNodeSList el = exprNode_flatten (e);
6669 bool mustReturn = TRUE; /* find a branch that doesn't */
6670 bool thisReturn = FALSE;
6671 bool hasDefault = FALSE;
6672 bool hasAllMembers = FALSE;
6673 bool inSwitch = FALSE;
6674 bool isEnumSwitch = FALSE;
6675 bool canBreak = FALSE;
6676 bool fallThrough = FALSE;
6677 ctype ct = ctype_unknown;
6678 enumNameSList usedEnums;
6681 if (exprNode_isDefined (test))
6686 ttype = ctype_realType (ct);
6688 if (ctype_isEnum (ttype))
6690 isEnumSwitch = TRUE;
6691 enums = ctype_elist (ttype);
6692 usedEnums = enumNameSList_new ();
6696 exprNodeSList_elements (el, current)
6698 if (exprNode_isDefined (current))
6700 switch (current->kind)
6708 message ("Duplicate default cases in switch"),
6709 exprNode_loc (current));
6714 if (current->kind == XPR_DEFAULT || current->kind == XPR_FTDEFAULT)
6722 exprNode st = exprData_getSingle (current->edata);
6723 uentry ue = exprNode_getUentry (st);
6725 if (uentry_isValid (ue))
6727 cstring cname = uentry_rawName (ue);
6729 if (enumNameList_member (/*@-usedef@*/enums/*@=usedef@*/, cname))
6731 if (enumNameSList_member
6732 (/*@-usedef@*/usedEnums/*@=usedef@*/, cname))
6736 message ("Duplicate case in switch: %s",
6742 enumNameSList_addh (usedEnums, cname);
6749 message ("Case in switch not %s member: %s",
6750 ctype_unparse (ct), cname),
6757 if (inSwitch && !fallThrough)
6759 if (!thisReturn || canBreak)
6769 /*@switchbreak@*/ break;
6771 thisReturn = thisReturn || exprNode_mustEscape (current);
6772 canBreak = canBreak || current->canBreak;
6773 if (canBreak) fallThrough = FALSE;
6776 } end_exprNodeSList_elements;
6778 if (inSwitch) /* check the last one! */
6780 if (!thisReturn || canBreak)
6789 && (enumNameSList_size (/*@-usedef@*/usedEnums/*@=usedef@*/) !=
6790 enumNameList_size (/*@-usedef@*/enums/*@=usedef@*/)))
6792 enumNameSList unused = enumNameSList_subtract (enums, usedEnums);
6794 voptgenerror (FLG_MISSCASE,
6795 message ("Missing case%s in switch: %q",
6796 cstring_makeLiteralTemp
6797 ((enumNameSList_size (unused) > 1) ? "s" : ""),
6798 enumNameSList_unparse (unused)),
6801 enumNameSList_free (unused);
6805 hasAllMembers = TRUE;
6809 enumNameSList_free (usedEnums);
6813 *allpaths = hasDefault;
6816 exprNodeSList_free (el);
6817 return ((hasDefault || hasAllMembers) && mustReturn);
6820 exprNode exprNode_switch (/*@only@*/ exprNode e, /*@only@*/ exprNode s)
6822 exprNode ret = exprNode_createPartialCopy (e);
6825 DPRINTF (("Switch: %s", exprNode_unparse (s)));
6827 ret->kind = XPR_SWITCH;
6828 ret->edata = exprData_makePair (e, s);
6830 if (!exprNode_isError (s))
6832 exprNode fs = exprNode_firstStatement (s);
6833 ret->loc = fileloc_update (ret->loc, s->loc);
6835 if (exprNode_isUndefined (fs)
6836 || exprNode_isCaseMarker (fs) || exprNode_isLabelMarker (fs)
6837 || exprNode_isDefaultMarker (fs)) {
6840 voptgenerror (FLG_FIRSTCASE,
6842 ("Statement after switch is not a case: %s", exprNode_unparse (fs)),
6847 if (!exprNode_isError (e))
6849 if (checkSwitchExpr (e, s, &allpaths))
6851 ret->exitCode = XK_MUSTRETURN;
6855 ret->exitCode = e->exitCode;
6858 ret->canBreak = e->canBreak;
6859 ret->mustBreak = e->mustBreak;
6863 ** exprNode.c:3883,32: Variable allpaths used before definition
6870 DPRINTF (("Context exit switch!"));
6871 context_exitSwitch (ret, allpaths);
6872 DPRINTF (("Context exit switch done!"));
6877 static void checkInfiniteLoop (/*@notnull@*/ exprNode test,
6878 /*@notnull@*/ exprNode body)
6880 sRefSet tuses = test->uses;
6882 if (!sRefSet_isEmpty (test->uses))
6884 sRefSet sets = sRefSet_newCopy (body->sets);
6885 bool hasError = TRUE;
6886 bool innerState = FALSE;
6887 sRefSet tuncon = sRefSet_undefined;
6889 sets = sRefSet_union (sets, test->sets);
6890 sets = sRefSet_union (sets, body->msets);
6891 sets = sRefSet_union (sets, test->msets);
6893 sRefSet_allElements (tuses, el)
6895 if (sRef_isUnconstrained (el))
6897 tuncon = sRefSet_insert (tuncon, el);
6901 if (sRefSet_member (sets, el))
6908 if (sRef_isInternalState (el)
6909 || sRef_isFileStatic (sRef_getRootBase (el)))
6913 } end_sRefSet_allElements ;
6917 sRefSet suncon = sRefSet_undefined;
6918 bool sinner = FALSE;
6920 sRefSet_allElements (sets, el)
6922 if (sRef_isUnconstrained (el))
6924 suncon = sRefSet_insert (suncon, el);
6926 else if (sRef_isInternalState (el))
6934 } end_sRefSet_allElements ;
6936 if (sinner && innerState)
6940 else if (sRefSet_isEmpty (tuncon)
6941 && sRefSet_isEmpty (suncon))
6946 ("Suspected infinite loop. No value used in loop test (%q) "
6947 "is modified by test or loop body.",
6948 sRefSet_unparsePlain (tuses)),
6953 if (sRefSet_isEmpty (tuncon))
6957 message ("Suspected infinite loop. No condition values "
6958 "modified. Modification possible through "
6959 "unconstrained calls: %q",
6960 sRefSet_unparsePlain (suncon)),
6967 message ("Suspected infinite loop. No condition values "
6968 "modified. Possible undetected dependency through "
6969 "unconstrained calls in loop test: %q",
6970 sRefSet_unparsePlain (tuncon)),
6976 sRefSet_free (sets);
6980 exprNode exprNode_while (/*@keep@*/ exprNode t, /*@keep@*/ exprNode b)
6983 bool emptyErr = FALSE;
6985 if (context_maybeSet (FLG_WHILEEMPTY))
6987 if (exprNode_isEmptyStatement (b))
6989 emptyErr = optgenerror
6992 ("Body of while statement is empty"),
6997 if (!emptyErr && context_maybeSet (FLG_WHILEBLOCK))
6999 if (exprNode_isDefined (b)
7000 && !exprNode_isBlock (b))
7002 if (context_inIterDef ()
7003 && (b->kind == XPR_STMTLIST
7004 || b->kind == XPR_TOK))
7010 voptgenerror (FLG_WHILEBLOCK,
7012 ("Body of while statement is not a block: %s",
7013 exprNode_unparse (b)),
7019 if (exprNode_isError (t))
7021 if (exprNode_isError (b))
7023 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
7027 ret = exprNode_createPartialCopy (b);
7034 ret = exprNode_createPartialCopy (t);
7036 llassert (t->kind == XPR_WHILEPRED);
7038 test = exprData_getSingle (t->edata);
7040 if (!exprNode_isError (b) && exprNode_isDefined (test))
7042 if (context_maybeSet (FLG_INFLOOPS)
7043 || context_maybeSet (FLG_INFLOOPSUNCON))
7046 ** check that some variable in the predicate is set by the body
7047 ** if the predicate uses any variables
7050 checkInfiniteLoop (test, b);
7053 exprNode_mergeUSs (ret, b);
7055 if (exprNode_isDefined (b))
7057 ret->exitCode = exitkind_makeConditional (b->exitCode);
7062 ret->edata = exprData_makePair (t, b);
7063 ret->kind = XPR_WHILE;
7065 if (exprNode_isDefined (t) && exprNode_mustEscape (t))
7069 message ("Predicate always exits: %s", exprNode_unparse (t)),
7073 ret->exitCode = XK_NEVERESCAPE;
7076 ** If loop is infinite, and there is no break inside,
7077 ** exit code is never reach.
7080 if (exprNode_knownIntValue (t))
7082 if (!exprNode_isZero (t))
7084 if (exprNode_isDefined (b))
7088 /* Really, it means never reached. */
7089 ret->exitCode = XK_MUSTEXIT;
7099 ret->canBreak = FALSE;
7100 ret->mustBreak = FALSE;
7106 ** do { b } while (t);
7108 ** note: body passed as first argument
7111 exprNode exprNode_doWhile (/*@only@*/ exprNode b, /*@only@*/ exprNode t)
7115 if (exprNode_isError (t))
7117 if (exprNode_isError (b))
7119 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
7123 ret = exprNode_createPartialCopy (b);
7125 ret->exitCode = exitkind_makeConditional (b->exitCode);
7126 exprNode_checkUse (ret, b->sref, b->loc);
7127 ret->exitCode = b->exitCode;
7128 ret->canBreak = b->canBreak;
7129 ret->mustBreak = b->mustBreak;
7134 ret = exprNode_createPartialCopy (t);
7135 exprNode_checkPred (cstring_makeLiteralTemp ("while"), t);
7137 if (!exprNode_isError (b))
7140 ** forgot the copy's --- why wasn't this detected??
7143 ret->sets = sRefSet_copyInto (ret->sets, b->sets);
7144 ret->msets = sRefSet_copyInto (ret->msets, b->msets);
7145 ret->uses = sRefSet_copyInto (ret->uses, b->uses);
7147 /* left this out --- causes and aliasing bug (infinite loop)
7148 should be detected?? */
7150 exprNode_checkUse (ret, b->sref, b->loc);
7151 exprNode_mergeUSs (ret, t);
7152 exprNode_checkUse (ret, t->sref, t->loc);
7154 ret->exitCode = b->exitCode;
7155 ret->canBreak = b->canBreak;
7156 ret->mustBreak = b->mustBreak;
7160 context_exitDoWhileClause (t);
7162 ret->kind = XPR_DOWHILE;
7163 ret->edata = exprData_makePair (t, b);
7167 exprNode exprNode_for (/*@keep@*/ exprNode inc, /*@keep@*/ exprNode body)
7170 bool emptyErr = FALSE;
7172 if (context_maybeSet (FLG_FOREMPTY))
7174 if (exprNode_isEmptyStatement (body))
7176 emptyErr = optgenerror
7179 ("Body of for statement is empty"),
7180 exprNode_loc (body));
7184 if (!emptyErr && context_maybeSet (FLG_FORBLOCK))
7186 if (exprNode_isDefined (body)
7187 && !exprNode_isBlock (body))
7189 if (context_inIterDef ()
7190 && (body->kind == XPR_STMTLIST
7191 || body->kind == XPR_TOK))
7197 voptgenerror (FLG_FORBLOCK,
7199 ("Body of for statement is not a block: %s",
7200 exprNode_unparse (body)),
7201 exprNode_loc (body));
7207 ** for ud purposes: (alreadly) init -> test -> (now) LOOP: body + inc + test
7210 if (exprNode_isError (body))
7212 ret = exprNode_createPartialCopy (inc);
7216 ret = exprNode_createPartialCopy (body);
7218 ret->exitCode = exitkind_makeConditional (body->exitCode);
7220 exprNode_mergeUSs (inc, body);
7222 if (exprNode_isDefined (inc))
7226 context_setMessageAnnote (cstring_makeLiteral ("in post loop increment"));
7229 tmp = exprNode_effect (exprData_getTripleInc (inc->edata));
7230 exprNode_freeShallow (tmp);
7232 context_clearMessageAnnote ();
7233 context_setMessageAnnote (cstring_makeLiteral ("in post loop test"));
7235 tmp = exprNode_effect (exprData_getTripleTest (inc->edata));
7236 exprNode_freeShallow (tmp);
7238 context_clearMessageAnnote ();
7240 ret->uses = sRefSet_copyInto (ret->uses, inc->uses);
7241 ret->sets = sRefSet_copyInto (ret->sets, inc->sets);
7242 ret->msets = sRefSet_copyInto (ret->msets, inc->msets);
7246 ret->kind = XPR_FOR;
7247 ret->edata = exprData_makePair (inc, body);
7249 if (exprNode_isDefined (inc)) {
7250 exprNode test = exprData_getTripleTest (inc->edata);
7252 if (exprNode_isUndefined (test)) {
7253 if (exprNode_isDefined (body)) {
7254 if (!body->canBreak) {
7255 /* Really, it means never reached. */
7256 ret->exitCode = XK_MUSTEXIT;
7266 ** for (init; test; inc)
7269 ** while (test) { body; inc; }
7271 ** Now: check use of init (may set vars for test)
7272 ** check use of test
7276 /*@observer@*/ guardSet exprNode_getForGuards (exprNode pred)
7280 if (exprNode_isError (pred)) return guardSet_undefined;
7282 llassert (pred->kind == XPR_FORPRED);
7284 test = exprData_getTripleTest (pred->edata);
7286 if (!exprNode_isError (test))
7288 return (test->guards);
7291 return guardSet_undefined;
7294 exprNode exprNode_whilePred (/*@only@*/ exprNode test)
7296 exprNode ret = exprNode_createSemiCopy (test);
7298 if (exprNode_isDefined (test))
7300 exprNode_copySets (ret, test);
7301 exprNode_checkPred (cstring_makeLiteralTemp ("while"), test);
7302 exprNode_checkUse (ret, test->sref, test->loc);
7304 exprNode_produceGuards (test);
7306 ret->guards = guardSet_copy (test->guards);
7309 ret->edata = exprData_makeSingle (test);
7310 ret->kind = XPR_WHILEPRED;
7314 exprNode exprNode_forPred (/*@only@*/ exprNode init, /*@only@*/ exprNode test,
7315 /*@only@*/ exprNode inc)
7320 ** for ud purposes: init -> test -> LOOP: [ body, inc ]
7323 exprNode_checkPred (cstring_makeLiteralTemp ("for"), test);
7325 if (!exprNode_isError (inc))
7327 ret = exprNode_createPartialCopy (inc);
7331 if (!exprNode_isError (init))
7333 ret = exprNode_createPartialCopy (init);
7335 else if (!exprNode_isError (test))
7337 ret = exprNode_createPartialCopy (test);
7341 ret = exprNode_createUnknown ();
7345 exprNode_mergeUSs (ret, init);
7347 if (exprNode_isDefined (init))
7349 exprNode_checkUse (ret, init->sref, init->loc);
7352 exprNode_mergeUSs (ret, test);
7354 if (exprNode_isDefined (test))
7356 exprNode_checkUse (ret, test->sref, test->loc);
7359 ret->kind = XPR_FORPRED;
7360 ret->edata = exprData_makeFor (init, test, inc);
7364 /*@notnull@*/ /*@only@*/ exprNode exprNode_goto (/*@only@*/ cstring label)
7366 exprNode ret = exprNode_createUnknown ();
7368 if (context_inMacro ())
7370 voptgenerror (FLG_MACROSTMT,
7371 message ("Macro %s uses goto (not functional)",
7372 context_inFunctionName ()),
7376 ret->kind = XPR_GOTO;
7377 ret->edata = exprData_makeLiteral (label);
7378 ret->mustBreak = TRUE;
7379 ret->exitCode = XK_GOTO;
7380 ret->canBreak = TRUE;
7384 exprNode exprNode_continue (/*@only@*/ lltok l, int qcontinue)
7386 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7388 ret->kind = XPR_CONTINUE;
7389 ret->edata = exprData_makeTok (l);
7390 ret->canBreak = TRUE;
7391 ret->mustBreak = TRUE;
7393 if (qcontinue == QSAFEBREAK)
7397 else if (qcontinue == QINNERCONTINUE)
7399 if (!context_inDeepLoop ())
7402 (FLG_LOOPLOOPCONTINUE,
7403 cstring_makeLiteral ("Continue statement marked with innercontinue "
7404 "is not inside a nested loop"),
7405 exprNode_loc (ret));
7408 else if (qcontinue == BADTOK)
7410 if (context_inDeepLoop ())
7413 (FLG_LOOPLOOPCONTINUE,
7414 cstring_makeLiteral ("Continue statement in nested loop"),
7415 exprNode_loc (ret));
7420 llbuglit ("exprNode_continue: bad qcontinue");
7426 exprNode exprNode_break (/*@only@*/ lltok l, int bqual)
7428 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7429 clause breakClause = context_breakClause ();
7431 ret->kind = XPR_BREAK;
7432 ret->edata = exprData_makeTok (l);
7433 ret->canBreak = TRUE;
7434 ret->mustBreak = TRUE;
7436 if (breakClause == NOCLAUSE)
7440 cstring_makeLiteral ("Break not inside while, for or switch statement"),
7441 exprNode_loc (ret));
7445 if (bqual != BADTOK)
7452 if (breakClause == SWITCHCLAUSE)
7454 if (!context_inDeepSwitch ())
7456 voptgenerror (FLG_SYNTAX,
7458 ("Break preceded by innerbreak is not in a deep switch"),
7459 exprNode_loc (ret));
7464 if (!context_inDeepLoop ())
7466 voptgenerror (FLG_SYNTAX,
7468 ("Break preceded by innerbreak is not in a deep loop"),
7469 exprNode_loc (ret));
7474 if (breakClause == SWITCHCLAUSE)
7476 voptgenerror (FLG_SYNTAX,
7478 ("Break preceded by loopbreak is breaking a switch"),
7479 exprNode_loc (ret));
7483 if (breakClause != SWITCHCLAUSE)
7487 message ("Break preceded by switchbreak is breaking %s",
7488 cstring_makeLiteralTemp
7489 ((breakClause == WHILECLAUSE
7490 || breakClause == DOWHILECLAUSE) ? "a while loop"
7491 : (breakClause == FORCLAUSE) ? "a for loop"
7492 : (breakClause == ITERCLAUSE) ? "an iterator"
7494 exprNode_loc (ret));
7502 if (breakClause == SWITCHCLAUSE)
7504 clause nextBreakClause = context_nextBreakClause ();
7506 switch (nextBreakClause)
7508 case NOCLAUSE: break;
7514 (FLG_LOOPSWITCHBREAK,
7515 cstring_makeLiteral ("Break statement in switch inside loop"),
7516 exprNode_loc (ret));
7520 (FLG_SWITCHSWITCHBREAK,
7521 cstring_makeLiteral ("Break statement in switch inside switch"),
7522 exprNode_loc (ret));
7529 if (context_inDeepLoop ())
7533 cstring_makeLiteral ("Break statement in nested loop"),
7534 exprNode_loc (ret));
7538 if (context_inDeepLoopSwitch ())
7541 (FLG_SWITCHLOOPBREAK,
7542 cstring_makeLiteral ("Break statement in loop inside switch"),
7543 exprNode_loc (ret));
7553 exprNode exprNode_nullReturn (/*@only@*/ lltok t)
7555 fileloc loc = lltok_getLoc (t);
7556 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
7558 context_returnFunction ();
7559 exprChecks_checkNullReturn (loc);
7561 ret->kind = XPR_NULLRETURN;
7562 ret->edata = exprData_makeTok (t);
7563 ret->exitCode = XK_MUSTRETURN;
7567 exprNode exprNode_return (/*@only@*/ exprNode e)
7571 if (exprNode_isError (e))
7573 ret = exprNode_createUnknown ();
7577 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (e->loc));
7579 exprNode_checkUse (ret, e->sref, e->loc);
7580 exprNode_checkReturn (e);
7583 context_returnFunction ();
7584 ret->kind = XPR_RETURN;
7585 ret->edata = exprData_makeSingle (e);
7586 ret->exitCode = XK_MUSTRETURN;
7591 exprNode exprNode_comma (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
7595 if (exprNode_isError (e1))
7597 if (exprNode_isError (e2))
7599 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
7603 ret = exprNode_createPartialCopy (e2);
7604 exprNode_checkUse (ret, e2->sref, e2->loc);
7605 ret->sref = e2->sref;
7610 ret = exprNode_createPartialCopy (e1);
7612 exprNode_checkUse (ret, e1->sref, e1->loc);
7614 if (!exprNode_isError (e2))
7616 exprNode_mergeUSs (ret, e2);
7617 exprNode_checkUse (ret, e2->sref, e2->loc);
7618 ret->sref = e2->sref;
7622 ret->kind = XPR_COMMA;
7623 ret->edata = exprData_makePair (e1, e2);
7625 if (exprNode_isDefined (e1))
7627 if (exprNode_isDefined (e2))
7631 if (exprNode_mustEscape (e1) || e1->mustBreak)
7635 message ("Second clause of comma expression is unreachable: %s",
7636 exprNode_unparse (e2)),
7640 ret->exitCode = exitkind_combine (e1->exitCode, e2->exitCode);
7641 ret->mustBreak = e1->mustBreak || e2->mustBreak;
7642 ret->canBreak = e1->canBreak || e2->canBreak;
7646 if (exprNode_mustEscape (e1) || e1->mustBreak)
7650 message ("Second clause of comma expression is unreachable: %s",
7651 exprNode_unparse (e2)),
7655 ret->exitCode = e1->exitCode;
7656 ret->canBreak = e1->canBreak;
7661 if (exprNode_isDefined (e2))
7663 ret->exitCode = e2->exitCode;
7664 ret->mustBreak = e2->mustBreak;
7665 ret->canBreak = e2->canBreak;
7672 static bool exprNode_checkOneInit (/*@notnull@*/ exprNode el, exprNode val)
7674 ctype t1 = exprNode_getType (el);
7675 ctype t2 = exprNode_getType (val);
7676 bool hasError = FALSE;
7678 if (ctype_isUnknown (t1))
7680 voptgenerror (FLG_IMPTYPE,
7681 message ("Variable has unknown (implicitly int) type: %s",
7682 exprNode_unparse (el)),
7686 el->typ = ctype_int;
7689 if (exprNode_isDefined (val) && val->kind == XPR_INITBLOCK)
7691 exprNodeList vals = exprData_getArgs (val->edata);
7693 if (ctype_isRealAP (t1))
7698 /*@i423 check number of entries int a[3] = { 1, 2, 3, 4 } ; */
7700 exprNodeList_elements (vals, oneval)
7702 cstring istring = message ("%d", i);
7705 (exprNode_fakeCopy (el),
7706 exprNode_numLiteral (ctype_int, istring,
7707 fileloc_copy (el->loc), i));
7709 if (exprNode_isDefined (newel))
7711 if (exprNodeList_size (vals) == 1
7712 && ctype_isString (exprNode_getType (oneval))
7713 && ctype_isChar (exprNode_getType (newel)))
7715 exprNode_freeIniter (newel);
7719 if (exprNode_checkOneInit (newel, oneval))
7724 if (nerrors > 3 && exprNodeList_size (vals) > 6)
7727 (message ("Additional initialization errors "
7728 "for %s not reported",
7729 exprNode_unparse (el)),
7731 exprNode_freeIniter (newel);
7736 exprNode_freeIniter (newel);
7741 exprNode_freeIniter (newel);
7746 cstring_free (istring);
7749 } end_exprNodeList_elements;
7752 else if (ctype_isStruct (ctype_realType (t1)))
7754 uentryList fields = ctype_getFields (t1);
7757 if (uentryList_size (fields) != exprNodeList_size (vals))
7759 if (uentryList_size (fields) > exprNodeList_size (vals))
7761 hasError = optgenerror
7763 message ("Initializer block for "
7764 "%s has %d field%&, but %s has %d field%&: %q",
7765 exprNode_unparse (el),
7766 exprNodeList_size (vals),
7768 uentryList_size (fields),
7769 exprNodeList_unparse (vals)),
7774 hasError = optgenerror
7776 message ("Initializer block for "
7777 "%s has %d field%&, but %s has %d field%&: %q",
7778 exprNode_unparse (el),
7779 exprNodeList_size (vals),
7781 uentryList_size (fields),
7782 exprNodeList_unparse (vals)),
7788 exprNodeList_elements (vals, oneval)
7790 uentry thisfield = uentryList_getN (fields, i);
7792 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
7794 uentry_getName (thisfield));
7796 if (exprNode_isDefined (newel))
7798 if (exprNode_checkOneInit (newel, oneval))
7803 exprNode_freeIniter (newel);
7807 } end_exprNodeList_elements;
7812 hasError = optgenerror
7814 message ("Initializer block used for "
7815 "%s where %t is expected: %s",
7816 exprNode_unparse (el), t1, exprNode_unparse (val)),
7822 if (exprNode_isDefined (val))
7824 doAssign (el, val, TRUE);
7826 if (!exprNode_matchType (t1, val))
7828 hasError = gentypeerror
7830 message ("Initial value of %s is type %t, "
7832 exprNode_unparse (el),
7833 t2, t1, exprNode_unparse (val)),
7843 exprNode_makeInitializationAux (/*@temp@*/ idDecl t)
7847 if (usymtab_exists (idDecl_observeId (t)))
7849 uentry ue = usymtab_lookup (idDecl_observeId (t));
7850 ret = exprNode_createId (ue);
7852 /*@i723 don't do this...but why? */
7854 ct = ctype_realishType (ret->typ);
7856 DPRINTF (("Type: %s", ctype_unparse (ret->typ)));
7858 if (ctype_isUnknown (ct))
7860 if (uentry_isAnyTag (ue))
7864 message ("%s used but not previously declared: %s",
7865 uentry_ekindName (ue),
7866 idDecl_getName (t)),
7874 message ("Variable has unknown (implicitly int) type: %s",
7875 idDecl_getName (t)),
7885 uentry ue = uentry_makeUnrecognized (idDecl_observeId (t),
7887 ret = exprNode_fromIdentifierAux (ue);
7890 ** No error - this happens in old style declarations:
7894 message ("Unrecognized identifier in intializer: %s", idDecl_observeId (t)),
7901 exprData_free (ret->edata, ret->kind);
7902 ret->edata = exprData_undefined;
7903 ret->exitCode = XK_NEVERESCAPE;
7904 ret->mustBreak = FALSE;
7905 ret->kind = XPR_INIT;
7909 exprNode exprNode_makeEmptyInitialization (/*@only@*/ idDecl t)
7911 exprNode ret = exprNode_makeInitializationAux (t);
7912 llassert (ret->edata == exprData_undefined);
7913 ret->edata = exprData_makeInit (t, exprNode_undefined);
7917 exprNode exprNode_makeInitialization (/*@only@*/ idDecl t,
7918 /*@only@*/ exprNode e)
7920 uentry ue = usymtab_lookup (idDecl_observeId (t));
7921 exprNode ret = exprNode_makeInitializationAux (t);
7922 fileloc loc = exprNode_loc (e);
7924 if (exprNode_isError (e))
7926 e = exprNode_createUnknown ();
7929 /* error: assume initializer is defined */
7930 sRef_setDefined (ret->sref, g_currentloc);
7934 ctype ct = ctype_realishType (ret->typ);
7939 ** was addSafeUse --- what's the problem?
7941 ** int x = 3, y = x ?
7944 exprData_free (ret->edata, ret->kind);
7945 ret->edata = exprData_makeInit (t, e);
7947 exprNode_checkUse (ret, e->sref, e->loc);
7949 if (ctype_isUnknown (e->typ) && uentry_isValid (ue))
7951 exprNode lhs = exprNode_createId (ue);
7954 ** static storage should be undefined before initializing
7957 if (uentry_isStatic (ue))
7959 sRef_setDefState (lhs->sref, SS_PARTIAL, fileloc_undefined);
7962 (void) exprNode_checkOneInit (lhs, e);
7964 if (uentry_isStatic (ue))
7966 sRef_setDefState (lhs->sref, SS_DEFINED, fileloc_undefined);
7969 exprNode_free (lhs);
7973 if (!exprNode_matchType (ct, e))
7975 if (exprNode_isZero (e) && ctype_isArrayPtr (ct))
7982 (exprNode_getType (e), e, exprNode_getType (ret), ret,
7984 ("Variable %q initialized to type %t, expects %t: %s",
7985 uentry_getName (ue), exprNode_getType (e),
7986 exprNode_getType (ret),
7987 exprNode_unparse (e)),
7993 if (uentry_isStatic (ue))
7995 sRef_setDefState (ret->sref, SS_PARTIAL, fileloc_undefined);
7998 doAssign (ret, e, TRUE);
8000 if (uentry_isStatic (ue))
8002 sRef_setDefState (ret->sref, SS_DEFINED, fileloc_undefined);
8006 if (context_inIterDef ())
8008 /* should check if it is yield */
8009 uentry_setUsed (ue, loc);
8016 exprNode_mergeUSs (ret, e);
8020 exprNode exprNode_iter (/*@observer@*/ uentry name,
8021 /*@only@*/ exprNodeList alist,
8022 /*@only@*/ exprNode body,
8023 /*@observer@*/ uentry end)
8028 llassert (uentry_isValid (name));
8030 uentry_setUsed (name, exprNode_loc (body));
8032 ret = exprNode_createPartialCopy (body);
8033 iname = uentry_getName (name);
8035 if (uentry_isInvalid (end))
8038 message ("Iter %s not balanced with end_%s", iname, iname));
8042 cstring ename = uentry_getName (end);
8044 if (!cstring_equalPrefix (ename, "end_"))
8046 llerror (FLG_ITER, message ("Iter %s not balanced with end_%s: %s",
8047 iname, iname, ename));
8051 if (!cstring_equal (iname, cstring_suffix (ename, 4)))
8054 message ("Iter %s not balanced with end_%s: %s",
8055 iname, iname, ename));
8059 cstring_free (ename);
8062 context_exitIterClause (body);
8064 ret->kind = XPR_ITER;
8065 ret->edata = exprData_makeIter (name, alist, body, end);
8067 if (uentry_isIter (name))
8069 (void) checkArgsReal (name, body,
8070 uentry_getParams (name), alist, TRUE, ret);
8073 cstring_free (iname);
8079 exprNode_iterNewId (/*@only@*/ cstring s)
8081 exprNode e = exprNode_new ();
8082 uentry ue = uentryList_getN (uentry_getParams (getCurrentIter ()), iterParamNo ());
8084 llassert (processingIterVars ());
8086 e->loc = context_getSaveLocation ();
8088 if (fileloc_isUndefined (e->loc))
8090 fileloc_free (e->loc);
8091 e->loc = fileloc_copy (g_currentloc);
8094 e->uses = sRefSet_new ();
8095 e->sets = sRefSet_new ();
8096 e->msets = sRefSet_new ();
8098 e->val = multiVal_unknown ();
8099 e->guards = guardSet_new ();
8101 e->isJumpPoint = FALSE;
8102 e->exitCode = XK_NEVERESCAPE;
8104 /*> missing fields, detected by lclint <*/
8105 e->canBreak = FALSE;
8106 e->mustBreak = FALSE;
8107 e->etext = cstring_undefined;
8109 if (uentry_isYield (ue))
8111 uentry uue = uentry_makeVariable (s, uentry_getType (ue),
8112 fileloc_copy (e->loc),
8116 uue = usymtab_supEntrySrefReturn (uue);
8118 sr = uentry_getSref (uue);
8119 sRef_mergeStateQuiet (sr, uentry_getSref (ue));
8120 sr = uentry_getSref (uue);
8121 sRef_setDefined (sr, e->loc);
8123 e->typ = uentry_getType (uue);
8125 e->edata = exprData_makeId (uue);
8126 uentry_setUsed (uue, g_currentloc);
8132 sRef_setGlobalScope ();
8133 uue = uentry_makeVariableLoc (s, ctype_unknown);
8135 e->typ = ctype_unknown;
8136 e->edata = exprData_makeId (uue);
8138 uentry_setUsed (uue, e->loc);
8139 uentry_setHasNameError (uue);
8141 if (context_getFlag (FLG_REPEATUNRECOG))
8143 uentry_markOwned (uue);
8147 usymtab_supGlobalEntry (uue);
8150 sRef_clearGlobalScope ();
8152 voptgenerror (FLG_UNRECOG, message ("Unrecognized identifier: %s", s),
8159 exprNode_defineConstraints(e);
8164 exprNode_iterExpr (/*@returned@*/ exprNode e)
8166 if (!processingIterVars ())
8168 llcontbuglit ("checkIterParam: not in iter");
8172 if (uentry_isYield (uentryList_getN (uentry_getParams (getCurrentIter ()),
8175 if (exprNode_isDefined (e))
8177 if (fileloc_isDefined (e->loc))
8181 message ("Yield parameter is not simple identifier: %s",
8182 exprNode_unparse (e)),
8189 message ("Yield parameter is not simple identifier: %s",
8190 exprNode_unparse (e)),
8200 exprNode_iterId (/*@observer@*/ uentry c)
8204 llassert (processingIterVars ());
8206 ue = uentryList_getN (uentry_getParams (getCurrentIter ()),
8209 if (uentry_isYield (ue))
8211 ctype ct = uentry_getType (ue);
8212 exprNode e = exprNode_createPlain (ct);
8213 cstring name = uentry_getName (c);
8214 uentry le = uentry_makeVariable (name, ct, fileloc_undefined, FALSE);
8216 uentry_setUsed (ue, g_currentloc);
8217 uentry_setHasNameError (ue);
8219 cstring_free (name);
8222 e->edata = exprData_makeId (le);
8223 e->loc = context_getSaveLocation ();
8224 e->sref = uentry_getSref (le);
8226 usymtab_supEntrySref (le);
8228 if (!context_inHeader ())
8232 message ("Yield parameter shadows local declaration: %q",
8233 uentry_getName (c)),
8234 fileloc_isDefined (e->loc) ? e->loc : g_currentloc))
8236 uentry_showWhereDeclared (c);
8243 return (exprNode_fromIdentifierAux (c));
8246 exprNode exprNode_iterStart (/*@observer@*/ uentry name, /*@only@*/ exprNodeList alist)
8248 exprNode ret = exprNode_create (ctype_unknown);
8250 ret->kind = XPR_ITERCALL;
8251 ret->edata = exprData_makeIterCall (name, alist);
8253 if (uentry_isIter (name))
8255 uentryList params = uentry_getParams (name);
8257 if (context_inIterDef ()
8258 && uentryList_size (params) == exprNodeList_size (alist))
8262 exprNodeList_elements (alist, arg)
8264 uentry parg = uentryList_getN (params, i);
8266 if (uentry_isYield (parg))
8268 uentry ue = exprNode_getUentry (arg);
8270 if (uentry_isValid (ue))
8277 } end_exprNodeList_elements;
8280 (void) checkArgsReal (name, ret, params, alist, TRUE, ret);
8281 checkUnspecCall (ret, params, alist);
8287 /*@exposed@*/ sRef exprNode_getSref (exprNode e)
8289 if (exprNode_isDefined (e))
8292 if (e->sref == defref) /*@noaccess sRef@*/
8295 e->sref = sRef_makeUnknown ();
8296 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
8307 return sRef_undefined;
8311 /*@observer@*/ cstring
8312 exprNode_unparseFirst (exprNode e)
8314 if (exprNode_isDefined (e))
8318 if (e->kind == XPR_STMTLIST
8319 || e->kind == XPR_COMMA || e->kind == XPR_COND)
8321 exprNode first = exprData_getPairA (e->edata);
8323 if (exprNode_isDefined (first))
8325 return (exprNode_unparseFirst (exprData_getPairA (e->edata)));
8329 return (cstring_makeLiteralTemp ("..."));
8333 ret = cstring_elide (exprNode_unparse (e), 20);
8334 cstring_markOwned (ret);
8340 return cstring_makeLiteralTemp ("<error>");
8344 /*@observer@*/ cstring
8345 exprNode_unparse (/*@temp@*/ exprNode e)
8347 if (exprNode_isError (e))
8349 return cstring_makeLiteralTemp ("<error>");
8352 if (cstring_isDefined (e->etext))
8358 cstring ret = exprNode_doUnparse (e);
8360 /*@-modifies@*/ /* benevolent */
8367 /*@observer@*/ fileloc
8368 exprNode_loc (exprNode e)
8370 if (exprNode_isError (e))
8372 return (g_currentloc);
8381 ** executes exprNode e
8382 ** recursively rexecutes as though in original parse using
8383 ** information in e->edata
8386 static /*@only@*/ exprNodeList exprNodeList_effect (exprNodeList e)
8388 exprNodeList ret = exprNodeList_new ();
8390 exprNodeList_elements (e, current)
8392 exprNodeList_addh (ret, exprNode_effect (current));
8393 } end_exprNodeList_elements;
8398 static /*@only@*/ exprNode exprNode_effect (exprNode e)
8399 /*@globals internalState@*/
8401 bool innerEffect = inEffect;
8407 context_clearJustPopped ();
8409 if (exprNode_isError (e))
8411 ret = exprNode_undefined;
8416 ** Turn off expose and dependent transfer checking.
8417 ** Need to pass exposed internal nodes,
8418 ** [ copying would be a waste! ]
8419 ** [ Actually, I think I wasted a lot more time than its worth ]
8420 ** [ trying to do this. ]
8424 /*@-observertrans@*/
8425 /*@-dependenttrans@*/
8432 ret = exprNode_addParens (exprData_getUopTok (data),
8433 exprNode_effect (exprData_getUopNode (data)));
8436 ret = exprNode_assign (exprNode_effect (exprData_getOpA (data)),
8437 exprNode_effect (exprData_getOpB (data)),
8438 exprData_getOpTok (data));
8441 ret = exprNode_undefined;
8444 ret = exprNode_functionCall (exprNode_effect (exprData_getFcn (data)),
8445 exprNodeList_effect (exprData_getArgs (data)));
8458 cstring id = exprData_getId (data);
8459 uentry ue = usymtab_lookupSafe (id);
8461 ret = exprNode_fromIdentifierAux (ue);
8462 ret->loc = fileloc_update (ret->loc, e->loc);
8469 ret = exprNode_arrayFetch (exprNode_effect (exprData_getPairA (data)),
8470 exprNode_effect (exprData_getPairB (data)));
8473 ret = exprNode_op (exprNode_effect (exprData_getOpA (data)),
8474 exprNode_effect (exprData_getOpB (data)),
8475 exprData_getOpTok (data));
8479 ret = exprNode_postOp (exprNode_effect (exprData_getUopNode (data)),
8480 exprData_getUopTok (data));
8483 ret = exprNode_preOp (exprNode_effect (exprData_getUopNode (data)),
8484 exprData_getUopTok (data));
8496 ret = exprNode_vaArg (exprData_getCastTok (data),
8497 exprNode_effect (exprData_getCastNode (data)),
8498 exprData_getCastType (data));
8502 ret = exprNode_cast (exprData_getCastTok (data),
8503 exprNode_effect (exprData_getCastNode (data)),
8504 exprData_getCastType (data));
8507 ret = exprNode_iterStart (exprData_getIterCallIter (data),
8509 (exprData_getIterCallArgs (data)));
8513 ret = exprNode_iter (exprData_getIterSname (data),
8514 exprNodeList_effect (exprData_getIterAlist (data)),
8515 exprNode_effect (exprData_getIterBody (data)),
8516 exprData_getIterEname (data));
8520 ret = exprNode_for (exprNode_effect (exprData_getPairA (data)),
8521 exprNode_effect (exprData_getPairB (data)));
8525 ret = exprNode_forPred (exprNode_effect (exprData_getTripleInit (data)),
8526 exprNode_effect (exprData_getTripleTest (data)),
8527 exprNode_effect (exprData_getTripleInc (data)));
8531 ret = exprNode_createTok (exprData_getTok (data));
8535 ret = exprNode_goto (exprData_getLiteral (data));
8536 ret->loc = fileloc_update (ret->loc, e->loc);
8540 ret = exprNode_continue (exprData_getTok (data), QSAFEBREAK);
8544 ret = exprNode_break (exprData_getTok (data), QSAFEBREAK);
8548 ret = exprNode_return (exprNode_effect (exprData_getSingle (data)));
8551 case XPR_NULLRETURN:
8552 ret = exprNode_nullReturn (exprData_getTok (data));
8556 ret = exprNode_comma (exprNode_effect (exprData_getPairA (data)),
8557 exprNode_effect (exprData_getPairB (data)));
8561 ret = exprNode_cond (exprNode_effect (exprData_getTriplePred (data)),
8562 exprNode_effect (exprData_getTripleTrue (data)),
8563 exprNode_effect (exprData_getTripleFalse (data)));
8566 ret = exprNode_if (exprNode_effect (exprData_getPairA (data)),
8567 exprNode_effect (exprData_getPairB (data)));
8571 ret = exprNode_ifelse (exprNode_effect (exprData_getTriplePred (data)),
8572 exprNode_effect (exprData_getTripleTrue (data)),
8573 exprNode_effect (exprData_getTripleFalse (data)));
8576 ret = exprNode_whilePred (exprData_getSingle (data));
8580 ret = exprNode_while (exprNode_effect (exprData_getPairA (data)),
8581 exprNode_effect (exprData_getPairB (data)));
8585 ret = exprNode_doWhile (exprNode_effect (exprData_getPairA (data)),
8586 exprNode_effect (exprData_getPairB (data)));
8590 ret = exprNode_makeBlock (exprNode_effect (exprData_getSingle (data)));
8594 ret = exprNode_statement (exprNode_effect (exprData_getUopNode (data)),
8595 exprData_getUopTok (data));
8599 ret = exprNode_concat (exprNode_effect (exprData_getPairA (data)),
8600 exprNode_effect (exprData_getPairB (data)));
8605 ret = exprNode_caseMarker
8606 (exprNode_effect (exprData_getSingle (data)),
8612 ret = exprNode_createTok (exprData_getTok (data));
8616 ret = exprNode_switch (exprNode_effect (exprData_getPairA (data)),
8617 exprNode_effect (exprData_getPairB (data)));
8621 ret = exprNode_makeInitialization
8622 (exprData_getInitId (data),
8623 exprNode_effect (exprData_getInitNode (data)));
8627 ret = exprNode_fieldAccessAux
8628 (exprNode_effect (exprData_getFieldNode (data)),
8629 exprNode_loc (exprData_getFieldNode (data)),
8630 cstring_copy (exprData_getFieldName (data)));
8634 ret = exprNode_arrowAccessAux
8635 (exprNode_effect (exprData_getFieldNode (data)),
8636 exprNode_loc (exprData_getFieldNode (data)),
8637 cstring_copy (exprData_getFieldName (data)));
8640 case XPR_STRINGLITERAL:
8654 /*@=observertrans@*/
8656 /*@=dependenttrans@*/
8667 static /*@observer@*/ cstring exprNode_rootVarName (exprNode e)
8672 if (exprNode_isError (e))
8674 return cstring_undefined;
8682 ret = exprNode_rootVarName (exprData_getUopNode (data));
8685 ret = exprNode_rootVarName (exprData_getOpA (data));
8689 ret = exprData_getId (data);
8714 case XPR_NULLRETURN:
8737 case XPR_STRINGLITERAL:
8738 ret = cstring_undefined;
8745 static /*@only@*/ cstring exprNode_doUnparse (exprNode e)
8750 if (exprNode_isError (e))
8752 static /*@only@*/ cstring error = cstring_undefined;
8754 if (!cstring_isDefined (error))
8756 error = cstring_makeLiteral ("<error>");
8767 ret = message ("(%s)", exprNode_unparse (exprData_getUopNode (e->edata)));
8770 ret = message ("%s %s %s",
8771 exprNode_unparse (exprData_getOpA (data)),
8772 lltok_unparse (exprData_getOpTok (data)),
8773 exprNode_unparse (exprData_getOpB (data)));
8776 ret = message ("%s(%q)",
8777 exprNode_unparse (exprData_getFcn (data)),
8778 exprNodeList_unparse (exprData_getArgs (data)));
8781 ret = message ("{ %q }", exprNodeList_unparse (exprData_getArgs (data)));
8784 ret = cstring_undefined;
8787 ret = message ("%s:", exprData_getId (data));
8791 ret = cstring_copy (exprData_getId (data));
8794 ret = message ("%s[%s]", exprNode_unparse (exprData_getPairA (data)),
8795 exprNode_unparse (exprData_getPairB (data)));
8798 ret = message ("<body>");
8801 ret = message ("%s %s %s",
8802 exprNode_unparse (exprData_getOpA (data)),
8803 lltok_unparse (exprData_getOpTok (data)),
8804 exprNode_unparse (exprData_getOpB (data)));
8808 ret = message ("%s%s",
8809 lltok_unparse (exprData_getUopTok (data)),
8810 exprNode_unparse (exprData_getUopNode (data)));
8814 ret = message ("%s%s",
8815 exprNode_unparse (exprData_getUopNode (data)),
8816 lltok_unparse (exprData_getUopTok (data)));
8820 ret = message ("offsetof(%s,%q)",
8821 ctype_unparse (qtype_getType (exprData_getOffsetType (data))),
8822 cstringList_unparseSep (exprData_getOffsetName (data), cstring_makeLiteralTemp (".")));
8826 ret = message ("sizeof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
8830 ret = message ("sizeof(%s)", exprNode_unparse (exprData_getSingle (data)));
8834 ret = message ("alignof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
8838 ret = message ("alignof(%s)", exprNode_unparse (exprData_getSingle (data)));
8842 ret = message ("va_arg(%s, %q)",
8843 exprNode_unparse (exprData_getCastNode (data)),
8844 qtype_unparse (exprData_getCastType (data)));
8848 ret = message ("%q(%q)",
8849 uentry_getName (exprData_getIterCallIter (data)),
8850 exprNodeList_unparse (exprData_getIterCallArgs (data)));
8853 ret = message ("%q(%q) %s %q",
8854 uentry_getName (exprData_getIterSname (data)),
8855 exprNodeList_unparse (exprData_getIterAlist (data)),
8856 exprNode_unparse (exprData_getIterBody (data)),
8857 uentry_getName (exprData_getIterEname (data)));
8860 ret = message ("(%q)%s",
8861 qtype_unparse (exprData_getCastType (data)),
8862 exprNode_unparse (exprData_getCastNode (data)));
8866 ret = message ("%s %s",
8867 exprNode_unparse (exprData_getPairA (data)),
8868 exprNode_unparse (exprData_getPairB (data)));
8872 ret = message ("for (%s; %s; %s)",
8873 exprNode_unparse (exprData_getTripleInit (data)),
8874 exprNode_unparse (exprData_getTripleTest (data)),
8875 exprNode_unparse (exprData_getTripleInc (data)));
8879 ret = message ("goto %s", exprData_getLiteral (data));
8883 ret = cstring_makeLiteral ("continue");
8887 ret = cstring_makeLiteral ("break");
8891 ret = message ("return %s", exprNode_unparse (exprData_getSingle (data)));
8894 case XPR_NULLRETURN:
8895 ret = cstring_makeLiteral ("return");
8899 ret = message ("%s, %s",
8900 exprNode_unparse (exprData_getPairA (data)),
8901 exprNode_unparse (exprData_getPairB (data)));
8905 ret = message ("%s ? %s : %s",
8906 exprNode_unparse (exprData_getTriplePred (data)),
8907 exprNode_unparse (exprData_getTripleTrue (data)),
8908 exprNode_unparse (exprData_getTripleFalse (data)));
8911 ret = message ("if (%s) %s",
8912 exprNode_unparse (exprData_getPairA (data)),
8913 exprNode_unparse (exprData_getPairB (data)));
8917 ret = message ("if (%s) %s else %s",
8918 exprNode_unparse (exprData_getTriplePred (data)),
8919 exprNode_unparse (exprData_getTripleTrue (data)),
8920 exprNode_unparse (exprData_getTripleFalse (data)));
8923 ret = message ("while (%s) %s",
8924 exprNode_unparse (exprData_getPairA (data)),
8925 exprNode_unparse (exprData_getPairB (data)));
8929 ret = cstring_copy (exprNode_unparse (exprData_getSingle (data)));
8933 ret = cstring_copy (lltok_unparse (exprData_getTok (data)));
8937 ret = message ("do { %s } while (%s)",
8938 exprNode_unparse (exprData_getPairB (data)),
8939 exprNode_unparse (exprData_getPairA (data)));
8943 ret = message ("{ %s }", exprNode_unparseFirst (exprData_getSingle (data)));
8947 ret = cstring_copy (exprNode_unparse (exprData_getUopNode (data)));
8951 ret = message ("%s; %s",
8952 exprNode_unparse (exprData_getPairA (data)),
8953 exprNode_unparse (exprData_getPairB (data)));
8958 ret = cstring_makeLiteral ("default:");
8962 ret = message ("switch (%s) %s",
8963 exprNode_unparse (exprData_getPairA (data)),
8964 exprNode_unparse (exprData_getPairB (data)));
8969 ret = message ("case %s:",
8970 exprNode_unparse (exprData_getSingle (data)));
8974 if (exprNode_isError (exprData_getInitNode (data)))
8976 ret = message ("%q", idDecl_unparseC (exprData_getInitId (data)));
8980 ret = message ("%q = %s",
8981 idDecl_unparseC (exprData_getInitId (data)),
8982 exprNode_unparse (exprData_getInitNode (data)));
8987 ret = message ("%s.%s",
8988 exprNode_unparse (exprData_getFieldNode (data)),
8989 exprData_getFieldName (data));
8993 ret = message ("%s->%s",
8994 exprNode_unparse (exprData_getFieldNode (data)),
8995 exprData_getFieldName (data));
8998 case XPR_STRINGLITERAL:
8999 ret = message ("\"%s\"", exprData_getLiteral (data));
9003 ret = cstring_copy (exprData_getLiteral (data));
9007 ret = cstring_makeLiteral ("<node>");
9015 exprNode_isInitializer (exprNode e)
9017 return (exprNode_isDefined (e)
9018 && e->kind == XPR_INIT);
9022 exprNode_isCharLit (exprNode e)
9024 if (exprNode_isDefined (e))
9026 return (multiVal_isChar (exprNode_getValue (e)));
9035 exprNode_isNumLit (exprNode e)
9037 if (exprNode_isDefined (e))
9039 return (multiVal_isInt (exprNode_getValue (e)));
9048 exprNode_isFalseConstant (exprNode e)
9050 if (exprNode_isDefined (e))
9052 cstring s = exprNode_rootVarName (e);
9054 if (cstring_equal (s, context_getFalseName ()))
9064 exprNode_matchLiteral (ctype expected, exprNode e)
9066 if (exprNode_isDefined (e))
9068 multiVal m = exprNode_getValue (e);
9070 if (multiVal_isDefined (m))
9072 if (multiVal_isInt (m))
9074 long int val = multiVal_forceInt (m);
9076 if (ctype_isDirectBool (ctype_realishType (expected)))
9080 return FALSE; /* really?! return TRUE; allow use of 0 for FALSE */
9088 if (ctype_isRealInt (expected))
9091 ** unsigned <- [ constant >= 0 is okay ]
9094 if (ctype_isUnsigned (expected))
9103 ** No checks on sizes of integers...maybe add
9107 DPRINTF (("Here: %s => %s", exprNode_unparse (e), ctype_unparse (expected)));
9108 DPRINTF (("Type: %s / %s", ctype_unparse (exprNode_getType (e)),
9109 bool_unparse (ctype_isInt (exprNode_getType (e)))));
9111 if (context_getFlag (FLG_NUMLITERAL)
9112 && (ctype_isRegularInt (exprNode_getType (e)) || val == 0)) {
9118 return FALSE; /* evs 2000-05-17: previously, always returned TRUE */
9122 else if (ctype_isChar (expected))
9126 else if (ctype_isArrayPtr (expected))
9130 else if (ctype_isAnyFloat (expected))
9132 return (context_getFlag (FLG_NUMLITERAL));
9139 else if (multiVal_isDouble (m))
9141 if (ctype_isAnyFloat (expected))
9146 else if (multiVal_isChar (m))
9148 char val = multiVal_forceChar (m);
9150 if (ctype_isChar (expected))
9152 if (ctype_isUnsigned (expected) && ((int)val) < 0)
9173 exprNode_matchType (ctype expected, exprNode e)
9177 if (!exprNode_isDefined (e)) return TRUE;
9179 actual = ctype_realishType (exprNode_getType (e));
9181 if (ctype_match (ctype_realishType (expected), actual))
9186 llassert (!exprNode_isError (e));
9187 return (exprNode_matchLiteral (expected, e));
9191 exprNode_matchTypes (exprNode e1, exprNode e2)
9196 if (!exprNode_isDefined (e1)) return TRUE;
9197 if (!exprNode_isDefined (e2)) return TRUE;
9200 ** realish type --- keep bools, bools
9203 t1 = ctype_realishType (exprNode_getType (e1));
9204 t2 = ctype_realishType (exprNode_getType (e2));
9206 if (ctype_match (t1, t2))
9211 return (exprNode_matchLiteral (t1, e2) || exprNode_matchLiteral (t2, e1));
9219 exprNode_matchArgType (ctype ct, exprNode e)
9223 if (!exprNode_isDefined (e))
9228 et = ctype_realType (exprNode_getType (e));
9230 if (ctype_matchArg (ct, et)) return TRUE;
9232 llassert (!exprNode_isError (e));
9233 return (exprNode_matchLiteral (ct, e));
9236 static /*@only@*/ exprNodeSList
9237 exprNode_flatten (/*@dependent@*/ exprNode e) /*@*/
9239 if (exprNode_isDefined (e))
9241 if (e->kind == XPR_STMTLIST)
9243 return (exprNodeSList_append
9244 (exprNode_flatten (exprData_getPairA (e->edata)),
9245 exprNode_flatten (exprData_getPairB (e->edata))));
9247 else if (e->kind == XPR_BLOCK)
9249 return (exprNode_flatten (exprData_getSingle (e->edata)));
9253 return (exprNodeSList_singleton (e));
9257 return exprNodeSList_new ();
9260 static /*@exposed@*/ exprNode
9261 exprNode_lastStatement (/*@returned@*/ exprNode e)
9263 if (exprNode_isDefined (e))
9265 if (e->kind == XPR_STMTLIST)
9267 exprNode b = exprData_getPairB (e->edata);
9269 if (exprNode_isDefined (b))
9271 return exprNode_lastStatement (b);
9275 return exprNode_lastStatement (exprData_getPairA (e->edata));
9278 else if (e->kind == XPR_BLOCK)
9280 return (exprNode_lastStatement (exprData_getSingle (e->edata)));
9288 return exprNode_undefined;
9291 static /*@exposed@*/ exprNode
9292 exprNode_firstStatement (/*@returned@*/ exprNode e)
9294 if (exprNode_isDefined (e))
9296 if (e->kind == XPR_STMTLIST)
9298 exprNode b = exprData_getPairA (e->edata);
9300 if (exprNode_isDefined (b))
9302 return exprNode_firstStatement (b);
9306 return exprNode_firstStatement (exprData_getPairB (e->edata));
9309 else if (e->kind == XPR_BLOCK)
9311 return (exprNode_firstStatement (exprData_getSingle (e->edata)));
9319 return exprNode_undefined;
9323 exprNode_mergeUSs (exprNode res, exprNode other)
9325 if (exprNode_isDefined (res) && exprNode_isDefined (other))
9327 res->msets = sRefSet_union (res->msets, other->msets);
9328 res->sets = sRefSet_union (res->sets, other->sets);
9329 res->uses = sRefSet_union (res->uses, other->uses);
9334 exprNode_mergeCondUSs (exprNode res, exprNode other1, exprNode other2)
9336 if (exprNode_isDefined (res))
9338 if (exprNode_isDefined (other1))
9340 res->sets = sRefSet_union (res->sets, other1->sets);
9341 res->msets = sRefSet_union (res->msets, other1->msets);
9342 res->uses = sRefSet_union (res->uses, other1->uses);
9344 if (exprNode_isDefined (other2))
9346 res->sets = sRefSet_union (res->sets, other2->sets);
9347 res->msets = sRefSet_union (res->msets, other2->msets);
9348 res->uses = sRefSet_union (res->uses, other2->uses);
9356 ** Reports errors is s is not defined.
9360 exprNode_addUse (exprNode e, /*@exposed@*/ sRef s)
9362 if (exprNode_isDefined (e))
9364 e->uses = sRefSet_insert (e->uses, s);
9369 exprNode_checkUse (exprNode e, /*@exposed@*/ sRef s, fileloc loc)
9371 if (sRef_isKnown (s) && !sRef_isConst (s))
9374 ** need to check all outer types are useable
9377 DPRINTF (("Check use: %s / %s",
9378 exprNode_unparse (e), sRef_unparse (s)));
9380 exprNode_addUse (e, s);
9382 if (!context_inProtectVars ())
9385 ** only report the deepest error
9388 sRef errorRef = sRef_undefined;
9389 sRef lastRef = sRef_undefined;
9390 bool deadRef = FALSE;
9391 bool unuseable = FALSE;
9392 bool errorMaybe = FALSE;
9394 while (sRef_isValid (s) && sRef_isKnown (s))
9396 ynm readable = sRef_isReadable (s);
9398 DPRINTF (("Readable: %s / %s",
9399 sRef_unparseFull (s), ynm_unparse (readable)));
9401 if (!(ynm_toBoolStrict (readable)))
9403 if (ynm_isMaybe (readable))
9407 DPRINTF (("Setting ERROR: %s", sRef_unparseFull (s)));
9408 deadRef = sRef_isPossiblyDead (errorRef);
9409 unuseable = sRef_isUnuseable (errorRef);
9416 deadRef = sRef_isDead (errorRef);
9417 unuseable = sRef_isUnuseable (errorRef);
9421 if (!sRef_isPartial (s))
9423 DPRINTF (("Defining! %s", sRef_unparseFull (s)));
9424 sRef_setDefined (s, fileloc_undefined);
9428 s = sRef_getBaseSafe (s);
9431 if (sRef_isValid (errorRef))
9433 if (sRef_isValid (lastRef) && sRef_isField (lastRef)
9434 && sRef_isPointer (errorRef))
9441 if (sRef_isThroughArrayFetch (errorRef))
9444 (FLG_STRICTUSERELEASED,
9445 message ("%q %q may be used after being released",
9446 sRef_unparseKindNamePlain (errorRef),
9447 sRef_unparse (errorRef)),
9450 sRef_showRefKilled (errorRef);
9452 if (sRef_isKept (errorRef))
9454 sRef_clearAliasState (errorRef, loc);
9460 DPRINTF (("HERE: %s", sRef_unparse (errorRef)));
9464 message ("%q %q %qused after being released",
9465 sRef_unparseKindNamePlain (errorRef),
9466 sRef_unparse (errorRef),
9467 cstring_makeLiteral (errorMaybe
9471 sRef_showRefKilled (errorRef);
9473 if (sRef_isKept (errorRef))
9475 sRef_clearAliasState (errorRef, loc);
9484 message ("%q %q%qused in inconsistent state",
9485 sRef_unparseKindName (errorRef),
9486 sRef_unparseOpt (errorRef),
9487 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9490 sRef_showStateInconsistent (errorRef);
9495 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
9499 message ("%q %q%qused before definition",
9500 sRef_unparseKindName (errorRef),
9501 sRef_unparseOpt (errorRef),
9502 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9505 DPRINTF (("Error: %s", sRef_unparseFull (errorRef)));
9508 sRef_setDefined (errorRef, loc);
9510 if (sRef_isAddress (errorRef))
9512 sRef_setDefined (sRef_getRootBase (errorRef), loc);
9514 } /* end is error */
9522 checkSafeUse (exprNode e, /*@exposed@*/ sRef s)
9524 if (exprNode_isDefined (e) && sRef_isKnown (s))
9526 e->uses = sRefSet_insert (e->uses, s);
9531 exprNode_checkSetAny (exprNode e, /*@dependent@*/ cstring name)
9533 if (exprNode_isDefined (e))
9535 e->sets = sRefSet_insert (e->sets, sRef_makeUnconstrained (name));
9540 exprNode_checkSet (exprNode e, /*@exposed@*/ sRef s)
9542 sRef defines = sRef_undefined;
9544 if (sRef_isValid (s) && !sRef_isNothing (s))
9546 uentry ue = sRef_getBaseUentry (s);
9548 if (uentry_isValid (ue))
9550 uentry_setLset (ue);
9553 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
9555 voptgenerror (FLG_USEDEF,
9556 message ("Attempt to set unuseable storage: %q",
9561 if (sRef_isMeaningful (s))
9564 if (sRef_isDead (s))
9566 sRef base = sRef_getBaseSafe (s);
9568 if (sRef_isValid (base)
9569 && sRef_isDead (base))
9571 sRef_setPartial (s, exprNode_loc (e));
9574 defines = s; /* okay - modifies for only param */
9576 else if (sRef_isPartial (s))
9578 sRef eref = exprNode_getSref (e);
9580 if (!sRef_isPartial (eref))
9583 ** should do something different here???
9586 sRef_setDefinedComplete (eref, exprNode_loc (e));
9590 sRef_setPartialDefinedComplete (eref, exprNode_loc (e));
9593 if (sRef_isMeaningful (eref))
9602 else if (sRef_isAllocated (s))
9604 sRef eref = exprNode_getSref (e);
9607 if (!sRef_isAllocated (eref))
9609 sRef_setDefinedComplete (eref, exprNode_loc (e));
9613 sRef base = sRef_getBaseSafe (eref);
9615 if (sRef_isValid (base))
9617 sRef_setPdefined (base, exprNode_loc (e));
9625 sRef_setDefinedNCComplete (s, exprNode_loc (e));
9630 else /* not meaningful...but still need to insert it */
9636 if (exprNode_isDefined (e) && sRef_isValid (defines))
9638 e->sets = sRefSet_insert (e->sets, defines);
9643 exprNode_checkMSet (exprNode e, /*@exposed@*/ sRef s)
9645 if (sRef_isValid (s) && !sRef_isNothing (s))
9647 uentry ue = sRef_getBaseUentry (s);
9649 if (uentry_isValid (ue))
9651 uentry_setLset (ue);
9654 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
9656 voptgenerror (FLG_USEDEF,
9657 message ("Attempt to set unuseable storage: %q", sRef_unparse (s)),
9661 if (sRef_isMeaningful (s))
9663 sRef_setDefinedComplete (s, exprNode_loc (e));
9666 if (exprNode_isDefined (e))
9668 e->msets = sRefSet_insert (e->msets, s);
9674 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode fcn, uentryList params, exprNodeList args)
9676 checkAnyCall (fcn, cstring_undefined, params, args,
9677 FALSE, sRefSet_undefined, FALSE, 0);
9681 checkOneArg (uentry ucurrent, /*@notnull@*/ exprNode current,
9682 /*@dependent@*/ exprNode fcn, bool isSpec, int argno, int totargs)
9686 if (uentry_isYield (ucurrent))
9688 sRef_setDefined (exprNode_getSref (current), exprNode_loc (current));
9689 exprNode_checkSet (current, current->sref);
9693 if (uentry_isSefParam (ucurrent))
9695 sRefSet sets = current->sets;
9696 sRef ref = exprNode_getSref (current);
9698 if (sRef_isMacroParamRef (ref))
9700 uentry ue = sRef_getUentry (ref);
9702 if (!uentry_isSefParam (ue))
9707 ("Parameter %d to %s is declared sef, but "
9708 "the argument is a macro parameter declared "
9710 argno, exprNode_unparse (fcn),
9711 exprNode_unparse (current)),
9712 exprNode_loc (current));
9716 if (!sRefSet_isEmpty (sets))
9718 sRefSet reported = sRefSet_undefined;
9720 sRefSet_realElements (current->sets, el)
9722 if (sRefSet_isSameNameMember (reported, el))
9724 ; /* don't report again */
9728 if (sRef_isUnconstrained (el))
9733 ("Parameter %d to %s is declared sef, but "
9734 "the argument calls unconstrained function %s "
9735 "(no guarantee it will not modify something): %s",
9736 argno, exprNode_unparse (fcn),
9737 sRef_unconstrainedName (el),
9738 exprNode_unparse (current)),
9739 exprNode_loc (current));
9746 ("Parameter %d to %s is declared sef, but "
9747 "the argument may modify %q: %s",
9748 argno, exprNode_unparse (fcn),
9750 exprNode_unparse (current)),
9751 exprNode_loc (current));
9754 } end_sRefSet_realElements;
9758 checkPassTransfer (current, ucurrent, isSpec, fcn, argno, totargs);
9759 exprNode_mergeUSs (fcn, current);
9764 checkAnyCall (/*@dependent@*/ exprNode fcn,
9765 /*@dependent@*/ cstring fname,
9768 bool hasMods, sRefSet mods,
9773 int nargs = exprNodeList_size (args);
9778 ** concat all args ud's to f, add each arg sref as a use unless
9779 ** it was specified as "out", in which case it is a def.
9782 uentryList_reset (pn);
9787 ** if paramn is only or unique, no other arg may alias argn
9790 exprNodeList_elements (args, current)
9794 if (exprNode_isDefined (current))
9796 if ((!uentryList_isUndefined (pn) && !uentryList_isFinished (pn)))
9798 uentry ucurrent = uentryList_current (pn);
9800 if (specialArgs == 0
9801 || (paramno < specialArgs))
9803 checkOneArg (ucurrent, current, fcn, isSpec, paramno, nargs);
9805 if (context_maybeSet (FLG_ALIASUNIQUE))
9807 if (uentry_isOnly (ucurrent)
9808 || uentry_isUnique (ucurrent))
9810 checkUniqueParams (fcn, current, args,
9816 else /* uentry is undefined */
9818 if (specialArgs == 0)
9820 exprNode_checkUseParam (current);
9823 exprNode_mergeUSs (fcn, current);
9826 uentryList_advanceSafe (pn);
9827 } end_exprNodeList_elements;
9833 sRefSet_allElements (mods, s)
9836 sRef rb = sRef_getRootBase (s);
9838 if (sRef_isFileOrGlobalScope (rb))
9840 context_usedGlobal (rb);
9843 fb = sRef_fixBaseParam (s, args);
9845 if (!sRef_isMacroParamRef (fb))
9847 if (sRef_isNothing (fb))
9853 if (sRef_isValid (fb))
9855 uentry ue = sRef_getBaseUentry (s);
9857 if (uentry_isValid (ue))
9859 uentry_setLset (ue);
9863 fcn->sets = sRefSet_insert (fcn->sets, fb);
9866 sRef_clearDerivedComplete (s);
9867 } end_sRefSet_allElements;
9873 if (context_hasMods ())
9875 if (context_maybeSet (FLG_MODUNCON))
9879 message ("Undetected modification possible "
9880 "from call to unconstrained function %s: %s",
9882 exprNode_unparse (fcn)),
9883 exprNode_loc (fcn));
9888 if (context_maybeSet (FLG_MODUNCONNOMODS)
9889 && !(context_inIterDef () || context_inIterEnd ()))
9892 (FLG_MODUNCONNOMODS,
9893 message ("Undetected modification possible "
9894 "from call to unconstrained function %s: %s",
9896 exprNode_unparse (fcn)),
9897 exprNode_loc (fcn));
9901 exprNode_checkSetAny (fcn, fname);
9905 void exprNode_checkUseParam (exprNode current)
9907 if (exprNode_isDefined (current))
9909 exprNode_checkUse (current, current->sref, current->loc);
9914 checkNumerics (ctype tr1, ctype tr2, ctype te1, ctype te2,
9915 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
9920 if (!ctype_match (tr1, tr2))
9922 if ((ctype_isRealInt (tr1) || ctype_isReal (tr1)) &&
9923 (ctype_isRealInt (tr2) || ctype_isReal (tr2)))
9931 message ("Incompatible types for %s (%s, %s): %s %s %s",
9933 ctype_unparse (te1),
9934 ctype_unparse (te2),
9935 exprNode_unparse (e1), lltok_unparse (op),
9936 exprNode_unparse (e2)),
9939 ret = ctype_unknown;
9943 if (ctype_isForceRealNumeric (&tr1) && ctype_isForceRealNumeric (&tr2))
9945 ret = ctype_resolveNumerics (tr1, tr2);
9947 else if (!context_msgStrictOps ())
9949 if (ctype_isPointer (tr1))
9951 if (ctype_isPointer (tr2) && !exprNode_isNullValue (e2))
9955 else if (ctype_isInt (tr2))
9961 ret = ctype_unknown;
9964 else if (ctype_isPointer (tr2))
9966 if (ctype_isPointer (tr1))
9970 else if (ctype_isInt (tr1))
9976 ret = ctype_unknown;
9981 ret = ctype_resolveNumerics (tr1, tr2);
9986 int opid = lltok_getTok (op);
9987 bool comparop = (opid == EQ_OP || opid == NE_OP
9988 || opid == TLT || opid == TGT
9989 || opid == LE_OP || opid == GE_OP);
9991 if (!ctype_isNumeric (tr1) && !ctype_isNumeric (tr2))
9994 && ((ctype_isEnum (tr1) && ctype_isEnum (tr2))
9995 || (ctype_isBool (tr1) && ctype_isBool (tr2))
9996 || (ctype_isChar (tr1) && ctype_isChar (tr2))))
10002 if (ctype_sameName (te1, te2))
10006 message ("Operands of %s are non-numeric (%t): %s %s %s",
10007 lltok_unparse (op), te1,
10008 exprNode_unparse (e1), lltok_unparse (op),
10009 exprNode_unparse (e2)),
10016 message ("Operands of %s are non-numerics (%t, %t): %s %s %s",
10017 lltok_unparse (op), te1, te2,
10018 exprNode_unparse (e1), lltok_unparse (op),
10019 exprNode_unparse (e2)),
10024 else if (!ctype_isNumeric (tr1))
10028 message ("Right operand of %s is non-numeric (%t): %s %s %s",
10029 lltok_unparse (op), te1,
10030 exprNode_unparse (e1), lltok_unparse (op),
10031 exprNode_unparse (e2)),
10036 if (!ctype_isNumeric (tr2))
10040 message ("Left operand of %s is non-numeric (%t): %s %s %s",
10041 lltok_unparse (op), te2,
10042 exprNode_unparse (e1), lltok_unparse (op),
10043 exprNode_unparse (e2)),
10048 ret = ctype_unknown;
10056 abstractOpError (ctype tr1, ctype tr2, lltok op,
10057 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10058 fileloc loc1, fileloc loc2)
10060 if (ctype_isRealAbstract (tr1) && ctype_isRealAbstract (tr2))
10062 if (ctype_match (tr1, tr2))
10066 message ("Operands of %s are abstract type (%t): %s %s %s",
10067 lltok_unparse (op), tr1,
10068 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10075 message ("Operands of %s are abstract types (%t, %t): %s %s %s",
10076 lltok_unparse (op), tr1, tr2,
10077 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10081 else if (ctype_isRealAbstract (tr1))
10085 message ("Left operand of %s is abstract type (%t): %s %s %s",
10086 lltok_unparse (op), tr1,
10087 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10092 if (ctype_isRealAbstract (tr2))
10096 message ("Right operand of %s is abstract type (%t): %s %s %s",
10097 lltok_unparse (op), tr2,
10098 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10107 ** requies e1 and e2 and not error exprNode's.
10111 ** If e1 is a component of an abstract type, and e2 is mutable and client-visible,
10112 ** the rep of the abstract type is exposed.
10114 ** The order is very important:
10116 ** check rep expose (move into check transfer)
10122 ** This isn't really a sensible procedure, but the indententation
10123 ** was getting too deep.
10127 checkOneRepExpose (sRef ysr, sRef base,
10128 /*@notnull@*/ exprNode e1,
10129 /*@notnull@*/ exprNode e2, ctype ct,
10132 if (!(sRef_isOnly (ysr) || sRef_isKeep (ysr)
10133 || sRef_isOwned (ysr)
10134 || sRef_isExposed (ysr)))
10136 if (sRef_isAnyParam (base) && !sRef_isExposed (base)
10137 && !sRef_isObserver (base)) /* evans 2001-07-11: added isObserver */
10140 if (sRef_isIReference (ysr))
10142 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10147 ("Assignment of mutable component of parameter %q "
10148 "to component of abstract "
10149 "type %s exposes rep: %s = %s",
10150 sRef_unparse (base),
10151 ctype_unparse (ct),
10152 exprNode_unparse (e1), exprNode_unparse (e2)),
10160 ("Assignment of mutable component of parameter %q "
10161 "(through alias %q) to component of abstract "
10162 "type %s exposes rep: %s = %s",
10163 sRef_unparse (base),
10164 sRef_unparse (e2->sref),
10165 ctype_unparse (ct),
10166 exprNode_unparse (e1), exprNode_unparse (e2)),
10172 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10176 message ("Assignment of mutable parameter %q "
10177 "to component of abstract type %s "
10178 "exposes rep: %s = %s",
10179 sRef_unparse (base),
10180 ctype_unparse (ct),
10181 exprNode_unparse (e1),
10182 exprNode_unparse (e2)),
10189 message ("Assignment of mutable parameter %q "
10190 "(through alias %q) to "
10191 "component of abstract type %s exposes "
10193 sRef_unparse (base),
10194 sRef_unparse (e2->sref),
10195 ctype_unparse (ct),
10196 exprNode_unparse (e1),
10197 exprNode_unparse (e2)),
10203 if (sRef_isFileOrGlobalScope (s2b))
10205 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10209 message ("Assignment of global %q "
10211 "abstract type %s exposes rep: %s = %s",
10212 sRef_unparse (base),
10213 ctype_unparse (ct),
10214 exprNode_unparse (e1), exprNode_unparse (e2)),
10221 message ("Assignment of global %q (through alias %q) "
10223 "abstract type %s exposes rep: %s = %s",
10224 sRef_unparse (base),
10225 sRef_unparse (e2->sref),
10226 ctype_unparse (ct),
10227 exprNode_unparse (e1), exprNode_unparse (e2)),
10235 doAssign (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2, bool isInit)
10237 if (ctype_isRealFunction (exprNode_getType (e1))
10238 && !ctype_isRealPointer (exprNode_getType (e1)))
10242 message ("Invalid left-hand side of assignment (function type %s): %s",
10243 ctype_unparse (exprNode_getType (e1)),
10244 exprNode_unparse (e1)),
10248 if (context_getFlag (FLG_ASSIGNEXPOSE) && ctype_isMutable (e2->typ))
10250 ctype t2 = exprNode_getType (e2);
10251 sRef sr = sRef_getRootBase (e1->sref);
10252 ctype ct = sRef_getType (sr);
10254 if (ctype_isAbstract (t2)
10255 && !(uentry_isMutableDatatype (usymtab_getTypeEntry (ctype_typeId (t2)))))
10257 /* it is immutable, okay to reference */
10258 goto donerepexpose;
10261 if (ctype_isAbstract (ct) && sRef_isIReference (e1->sref))
10263 sRef s2b = sRef_getRootBase (e2->sref);
10264 sRef s1 = e1->sref;
10265 sRef s1b = sRef_getRootBase (s1);
10268 aliases = usymtab_canAlias (e2->sref);
10270 if (!sRef_similar (s2b, s1b)
10271 && !sRef_isExposed (s1)
10272 && !(sRef_isOnly (s2b) || sRef_isKeep (s2b) || sRef_isExposed (s2b)))
10274 if (sRef_isAnyParam (s2b) && !sRef_isOnly (s2b)
10275 && !sRef_isOwned (s2b) && !sRef_isKeep (s2b)
10276 && !sRef_isExposed (s2b))
10278 if (sRef_isIReference (e2->sref))
10283 ("Assignment of mutable component of parameter %q "
10284 "to component of abstract type %s exposes rep: %s = %s",
10285 sRef_unparse (s2b),
10286 ctype_unparse (ct),
10287 exprNode_unparse (e1), exprNode_unparse (e2)),
10294 message ("Assignment of mutable parameter %q to "
10295 "component of abstract type %s exposes rep: %s = %s",
10296 sRef_unparse (s2b),
10297 ctype_unparse (ct),
10298 exprNode_unparse (e1), exprNode_unparse (e2)),
10303 if (sRef_isFileOrGlobalScope (s2b))
10307 message ("Assignment of global %q to component of "
10308 "abstract type %s exposes rep: %s = %s",
10309 sRef_unparse (s2b),
10310 ctype_unparse (ct),
10311 exprNode_unparse (e1), exprNode_unparse (e2)),
10315 sRefSet_realElements (aliases, ysr)
10317 sRef base = sRef_getRootBase (ysr);
10319 if (sRef_similar (ysr, s2b) || sRef_similar (s1b, base)
10320 || sRef_sameName (base, s1b))
10322 ; /* error already reported or same sref */
10326 checkOneRepExpose (ysr, base, e1, e2, ct, s2b);
10328 } end_sRefSet_realElements;
10330 sRefSet_free (aliases);
10337 ** function variables don't really work...
10340 if (!ctype_isFunction (ctype_realType (e2->typ)))
10344 DPRINTF (("Check init: %s / %s",
10345 exprNode_unparse (e1), exprNode_unparse (e2)));
10346 checkInitTransfer (e1, e2);
10350 checkAssignTransfer (e1, e2);
10355 sRef fref = e2->sref;
10357 sRef_setDefState (e1->sref, sRef_getDefState (fref), e1->loc);
10358 sRef_setNullState (e1->sref, sRef_getNullState (fref), e1->loc);
10360 /* Need to typecheck the annotation on the parameters */
10362 if (ctype_isRealFunction (e1->typ)) {
10363 uentryList e1p = ctype_argsFunction (ctype_realType (e1->typ));
10364 uentryList e2p = ctype_argsFunction (ctype_realType (e2->typ));
10366 if (!uentryList_isMissingParams (e1p)
10367 && !uentryList_isMissingParams (e2p)
10368 && uentryList_size (e1p) > 0) {
10369 if (uentryList_size (e1p) == uentryList_size (e2p)) {
10372 uentryList_elements (e1p, el1) {
10375 el2 = uentryList_getN (e2p, n);
10377 uentry_checkMatchParam (el1, el2, n, e2);
10378 } end_uentryList_elements;
10384 if (isInit && sRef_isFileOrGlobalScope (e1->sref))
10390 DPRINTF (("Update aliases: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
10391 updateAliases (e1, e2);
10396 checkMacroParen (exprNode e)
10398 if (exprNode_isError (e) || e->kind == XPR_CAST)
10404 if (sRef_isUnsafe (e->sref) && !exprNode_isInParens (e))
10408 message ("Macro parameter used without parentheses: %s",
10409 exprNode_unparse (e)),
10416 reflectNullTest (/*@notnull@*/ exprNode e, bool isnull)
10420 e->guards = guardSet_addTrueGuard (e->guards, e->sref);
10424 e->guards = guardSet_addFalseGuard (e->guards, e->sref);
10431 ** if e2 is a parameter or global derived location which
10432 ** can be modified (that is, e2 is a mutable abstract type,
10433 ** or a derived pointer), then e1 can alias e2.
10435 ** e1 can alias everything which e2 can alias.
10437 ** Also, if e1 is guarded, remove from guard sets!
10440 static void updateAliases (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2)
10442 if (!context_inProtectVars ())
10445 ** depends on types of e1 and e2
10448 sRef s1 = e1->sref;
10449 sRef s2 = e2->sref;
10450 ctype t1 = exprNode_getType (e1);
10452 /* handle pointer sRefs, record fields, arrays, etc... */
10454 if (!ctype_isRealSU (t1))
10456 DPRINTF (("Copying real! %s", ctype_unparse (t1)));
10457 sRef_copyRealDerivedComplete (s1, s2);
10462 ** Fields should alias
10465 DPRINTF (("Not COPYING!: %s", ctype_unparse (t1)));
10468 if (ctype_isMutable (t1) && sRef_isKnown (s1))
10470 usymtab_clearAlias (s1);
10471 usymtab_addMustAlias (s1, s2);
10472 DPRINTF (("Add must alias: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
10476 DPRINTF (("Not mutable: %s", ctype_unparse (t1)));
10479 if (sRef_possiblyNull (s1) && usymtab_isGuarded (s1))
10481 usymtab_unguard (s1);
10486 exprNode exprNode_updateLocation (/*@returned@*/ exprNode e, /*@temp@*/ fileloc loc)
10488 if (exprNode_isDefined (e))
10490 e->loc = fileloc_update (e->loc, loc);
10494 e = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
10500 static void checkUniqueParams (exprNode fcn,
10501 /*@notnull@*/ exprNode current,
10503 int paramno, uentry ucurrent)
10506 sRef thisref = exprNode_getSref (current);
10509 ** Check if any argument could match this argument.
10512 exprNodeList_elements (args, icurrent)
10516 if (iparamno != paramno)
10518 sRef sr = exprNode_getSref (icurrent);
10520 if (sRef_similarRelaxed (thisref, sr))
10522 if (!sRef_isConst (thisref) && !sRef_isConst (sr))
10527 ("Parameter %d (%s) to function %s is declared %s but "
10528 "is aliased by parameter %d (%s)",
10530 exprNode_unparse (current),
10531 exprNode_unparse (fcn),
10532 alkind_unparse (uentry_getAliasKind (ucurrent)),
10533 iparamno, exprNode_unparse (icurrent)),
10539 sRefSet aliases = usymtab_canAlias (sr);
10541 sRefSet_allElements (aliases, asr)
10543 if (ctype_isUnknown (sRef_getType (thisref)))
10545 sRef_setType (thisref, uentry_getType (ucurrent));
10548 if (sRef_similarRelaxed (thisref, asr))
10550 if (sRef_isExternal (asr))
10552 if (sRef_isLocalState (thisref))
10558 sRef base = sRef_getRootBase (asr);
10560 if (!sRef_similar (sRef_getBase (asr), thisref))
10562 if (sRef_isUnique (base) || sRef_isOnly (base)
10563 || sRef_isKept (base)
10564 || (sRef_isAddress (asr) && sRef_isLocalVar (base))
10565 || (sRef_isAddress (thisref)
10566 && sRef_isLocalVar (sRef_getRootBase (thisref))))
10568 ; /* okay, no error */
10573 (FLG_MAYALIASUNIQUE,
10575 ("Parameter %d (%s) to function %s is declared %s but "
10576 "may be aliased externally by parameter %d (%s)",
10578 exprNode_unparse (current),
10579 exprNode_unparse (fcn),
10580 alkind_unparse (uentry_getAliasKind (ucurrent)),
10581 iparamno, exprNode_unparse (icurrent)),
10592 ("Parameter %d (%s) to function %s is declared %s but "
10593 "is aliased externally by parameter %d (%s) through "
10596 exprNode_unparse (current),
10597 exprNode_unparse (fcn),
10598 alkind_unparse (uentry_getAliasKind (ucurrent)),
10599 iparamno, exprNode_unparse (icurrent),
10600 sRef_unparse (asr)),
10604 } end_sRefSet_allElements;
10605 sRefSet_free (aliases);
10608 } end_exprNodeList_elements;
10611 long exprNode_getLongValue (exprNode e) {
10614 if (exprNode_hasValue (e)
10615 && multiVal_isInt (exprNode_getValue (e)))
10617 value = multiVal_forceInt (exprNode_getValue (e));
10627 /*@observer@*/ fileloc exprNode_getfileloc (exprNode p_e)
10629 if (exprNode_isDefined (p_e) )
10630 return ( p_e->loc );
10632 return fileloc_undefined;
10635 /*@only@*/ fileloc exprNode_getNextSequencePoint (exprNode e)
10638 ** Returns the location of the sequence point following e.
10640 ** Only works for statements (for now).
10643 if (exprNode_isDefined (e) && e->kind == XPR_STMT) {
10644 lltok t = exprData_getUopTok (e->edata);
10645 return fileloc_copy(lltok_getLoc (t));
10647 //drl possible problem : warning fix
10648 // llcontbug (message ("Cannot get next sequence point: %s", exprNode_unparse (e)));
10649 return fileloc_undefined;
10653 exprNode exprNode_createNew(ctype c)
10657 ret = exprNode_createPlain (c);