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 static void exprNode_defineConstraints(/*@sef@*/ /*@special@*/ /*@notnull@*/ exprNode p_e)
109 /*@defines p_e->requiresConstraints, p_e->ensuresConstraints, p_e->trueEnsuresConstraints, p_e->falseEnsuresConstraints @*/
112 # define exprNode_defineConstraints(e) \
113 do{ (e)->requiresConstraints = constraintList_makeNew(); \
114 (e)->ensuresConstraints = constraintList_makeNew(); \
115 (e)->trueEnsuresConstraints = constraintList_makeNew(); \
116 (e)->falseEnsuresConstraints = constraintList_makeNew(); } while(FALSE)
120 ** must occur after library has been read
123 void exprNode_initMod (void)
124 /*@globals undef regArg, undef outArg, undef outStringArg,
125 undef csOnlyArg, undef csArg;
132 cstringType = ctype_unknown;
133 ctypeType = ctype_unknown;
134 filelocType = ctype_unknown;
136 defref = sRef_undefined;
138 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
140 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
143 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
145 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
148 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
150 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
153 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdin")))
155 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdin"));
157 else /* define stdin */
159 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdin"),
161 fileloc_getBuiltin (),
163 uentry_setHasNameError (ue);
164 ue = usymtab_supGlobalEntryReturn (ue);
167 stdinRef = sRef_makePointer (uentry_getSref (ue));
169 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdout")))
171 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdout"));
175 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdout"),
177 fileloc_getBuiltin (),
179 uentry_setHasNameError (ue);
180 ue = usymtab_supGlobalEntryReturn (ue);
183 stdoutRef = sRef_makePointer (uentry_getSref (ue));
185 tmp = idDecl_create (cstring_undefined, qtype_create (ctype_unknown));
187 regArg = uentry_makeParam (tmp, PARAMUNKNOWN);
190 qtype_addQual (qtype_create (ctype_makePointer (ctype_unknown)),
193 outArg = uentry_makeParam (tmp, PARAMUNKNOWN);
195 idDecl_setTyp (tmp, qtype_addQual (qtype_create (ctype_string),
198 outStringArg = uentry_makeParam (tmp, PARAMUNKNOWN);
200 idDecl_setTyp (tmp, qtype_addQual (qtype_addQual (qtype_create (cstringType),
202 qual_createNull ()));
204 csOnlyArg = uentry_makeParam (tmp, PARAMUNKNOWN);
206 idDecl_setTyp (tmp, qtype_addQual (qtype_create (cstringType), qual_createNull ()));
207 csArg = uentry_makeParam (tmp, PARAMUNKNOWN);
213 exprNode_destroyMod (void)
214 /*@globals killed regArg, killed outArg, killed outStringArg,
215 killed mustExitNode, initMod @*/
219 uentry_free (regArg);
220 uentry_free (outArg);
221 uentry_free (outStringArg);
223 exprNode_free (mustExitNode);
230 static void exprNode_resetSref (/*@notnull@*/ exprNode e)
235 exprNode exprNode_fakeCopy (exprNode e)
237 /*@-temptrans@*/ /*@-retalias@*/
239 /*@=temptrans@*/ /*@=retalias@*/
242 static bool isFlagKey (char key)
244 return (key == '-' || key == '+' || key == ' ' || key == '#');
247 static void exprNode_combineControl (/*@notnull@*/ exprNode ret,
248 /*@notnull@*/ exprNode ifclause,
249 /*@notnull@*/ exprNode elseclause)
251 ret->canBreak = ifclause->canBreak || elseclause->canBreak;
254 (ifclause->mustBreak || exprNode_mustEscape (ifclause))
255 && (elseclause->mustBreak || exprNode_mustEscape (elseclause));
257 ret->exitCode = exitkind_combine (ifclause->exitCode,
258 elseclause->exitCode);
263 ** For exprNode's returned by exprNode_effect.
266 static bool shallowKind (exprKind kind)
268 return (kind == XPR_STRINGLITERAL
269 || kind == XPR_NUMLIT
272 || kind == XPR_NODE);
276 exprNode_freeIniter (/*@only@*/ exprNode e)
278 if (!exprNode_isError (e))
284 ** Its a fake copy, don't free the field->rec and field->field
289 sfree (e->edata->field);
295 exprNode_free (e->edata->op->b);
296 /*@-compdestroy@*/ sfree (e->edata->op); /*@=compdestroy@*/
300 llbug (message ("other: %s", exprNode_unparse (e)));
303 multiVal_free (e->val);
304 cstring_free (e->etext);
305 fileloc_free (e->loc);
306 sRefSet_free (e->uses);
307 sRefSet_free (e->sets);
308 sRefSet_free (e->msets);
309 guardSet_free (e->guards);
311 constraintList_free(e->requiresConstraints);
312 constraintList_free(e->ensuresConstraints);
313 constraintList_free(e->trueEnsuresConstraints);
314 constraintList_free(e->falseEnsuresConstraints);
316 e->requiresConstraints = NULL;
317 e->ensuresConstraints = NULL;
318 e->trueEnsuresConstraints = NULL;
319 e->falseEnsuresConstraints = NULL;
326 exprNode_freeShallow (/*@only@*/ exprNode e)
328 if (!exprNode_isError (e))
330 if (shallowKind (e->kind))
337 if (e->kind == XPR_EMPTY
338 || e->kind == XPR_BODY
339 || e->kind == XPR_STRINGLITERAL
340 || e->kind == XPR_NUMLIT
341 || e->kind == XPR_NODE
342 || e->kind == XPR_OFFSETOF
343 || e->kind == XPR_ALIGNOFT
344 || e->kind == XPR_ALIGNOF
345 || e->kind == XPR_SIZEOFT
346 || e->kind == XPR_SIZEOF)
348 /* don't free anything */
352 /* multiVal_free (e->val); */
353 cstring_free (e->etext);
354 fileloc_free (e->loc);
355 sRefSet_free (e->uses);
356 sRefSet_free (e->sets);
357 sRefSet_free (e->msets);
358 guardSet_free (e->guards);
359 exprData_freeShallow (e->edata, e->kind);
361 /*@-compdestroy@*/ sfree (e); /*@=compdestroy@*/
370 exprNode_free (exprNode e)
372 if (!exprNode_isError (e))
376 multiVal_free (e->val);
377 cstring_free (e->etext);
378 fileloc_free (e->loc);
379 sRefSet_free (e->uses);
380 sRefSet_free (e->sets);
381 sRefSet_free (e->msets);
382 guardSet_free (e->guards);
383 exprData_free (e->edata, e->kind);
385 constraintList_free(e->requiresConstraints);
386 constraintList_free(e->ensuresConstraints);
387 constraintList_free(e->trueEnsuresConstraints);
388 constraintList_free(e->falseEnsuresConstraints);
390 e->requiresConstraints = NULL;
391 e->ensuresConstraints = NULL;
392 e->trueEnsuresConstraints = NULL;
393 e->falseEnsuresConstraints = NULL;
403 exprNode_makeError ()
405 return exprNode_undefined;
408 static /*@out@*/ /*@only@*/ /*@notnull@*/ exprNode
411 exprNode ret = (exprNode) dmalloc (sizeof (*ret));
412 /* static int lastexpnodes = 0; */
417 if (nowalloc > maxalloc)
425 static /*@notnull@*/ /*@special@*/ exprNode
426 exprNode_createPlain (ctype c)
428 /*@ensures isnull result->edata, result->loc, result->val, result->guards,
429 result->uses, result->sets, result->msets, result->etext @*/
432 exprNode e = exprNode_new ();
436 e->val = multiVal_undefined;
438 e->etext = cstring_undefined;
439 e->loc = fileloc_undefined;
440 e->guards = guardSet_undefined;
441 e->uses = sRefSet_undefined;
442 e->sets = sRefSet_undefined;
443 e->msets = sRefSet_undefined;
444 e->edata = exprData_undefined;
445 e->exitCode = XK_NEVERESCAPE;
447 e->mustBreak = FALSE;
448 e->isJumpPoint = FALSE;
450 exprNode_defineConstraints(e);
455 /*@observer@*/ exprNode exprNode_makeMustExit (void)
457 if (exprNode_isUndefined (mustExitNode))
459 mustExitNode = exprNode_createPlain (ctype_unknown);
460 mustExitNode->exitCode = XK_MUSTEXIT;
467 static /*@notnull@*/ /*@special@*/ exprNode exprNode_create (ctype c)
469 /*@post:isnull result->edata, result->guards, result->val,
470 result->uses, result->sets, result->msets@*/
473 exprNode e = exprNode_createPlain (c);
474 e->loc = fileloc_copy (g_currentloc);
478 static /*@notnull@*/ /*@special@*/ exprNode exprNode_createUnknown (void)
480 /*@post:isnull result->edata, result->guards,
481 result->uses, result->sets, result->msets@*/
484 return (exprNode_create (ctype_unknown));
487 static /*@notnull@*/ /*@special@*/ exprNode
488 exprNode_createLoc (ctype c, /*@keep@*/ fileloc loc)
490 /*@post:isnull result->edata, result->guards, result->val,
491 result->uses, result->sets, result->msets@*/
494 exprNode e = exprNode_createPlain (c);
500 exprNode_copySets (/*@special@*/ /*@notnull@*/ exprNode ret, exprNode e)
501 /*@defines ret->guards, ret->uses, ret->sets, ret->msets@*/
503 if (exprNode_isDefined (e))
505 ret->guards = guardSet_copy (e->guards);
506 ret->uses = sRefSet_newCopy (e->uses);
507 ret->sets = sRefSet_newCopy (e->sets);
508 ret->msets = sRefSet_newCopy (e->msets);
512 ret->guards = guardSet_undefined;
513 ret->uses = sRefSet_undefined;
514 ret->sets = sRefSet_undefined;
515 ret->msets = sRefSet_undefined;
519 static /*@notnull@*/ /*@special@*/ exprNode
520 exprNode_createPartialLocCopy (exprNode e, /*@only@*/ fileloc loc)
522 /*@post:isnull result->edata, result->etext@*/
525 exprNode ret = exprNode_new ();
527 if (exprNode_isError (e))
529 ret->typ = ctype_unknown;
530 ret->val = multiVal_undefined;
532 ret->guards = guardSet_undefined;
533 ret->uses = sRefSet_undefined;
534 ret->sets = sRefSet_undefined;
535 ret->msets = sRefSet_undefined;
540 ret->val = multiVal_copy (e->val);
542 ret->guards = guardSet_copy (e->guards);
543 ret->uses = sRefSet_newCopy (e->uses);
544 ret->sets = sRefSet_newCopy (e->sets);
545 ret->msets = sRefSet_newCopy (e->msets);
548 ret->kind = XPR_EMPTY;
550 ret->etext = cstring_undefined;
551 ret->exitCode = XK_NEVERESCAPE;
552 ret->canBreak = FALSE;
553 ret->mustBreak = FALSE;
554 ret->isJumpPoint = FALSE;
555 ret->edata = exprData_undefined;
557 exprNode_defineConstraints(ret);
563 static /*@notnull@*/ /*@special@*/ exprNode
564 exprNode_createPartialCopy (exprNode e)
566 /*@post:isnull result->edata, result->etext@*/
569 return (exprNode_createPartialLocCopy (e, fileloc_copy (exprNode_loc (e))));
572 static /*@notnull@*/ /*@special@*/ exprNode
573 exprNode_createPartialNVCopy (exprNode e)
575 /*@post:isnull result->edata, result->etext, result->val @*/
578 exprNode ret = exprNode_new ();
580 if (exprNode_isError (e))
582 ret->typ = ctype_unknown;
583 ret->loc = fileloc_undefined;
584 ret->guards = guardSet_undefined;
585 ret->uses = sRefSet_undefined;
586 ret->sets = sRefSet_undefined;
587 ret->msets = sRefSet_undefined;
592 ret->loc = fileloc_copy (e->loc);
593 ret->guards = guardSet_copy (e->guards);
594 ret->uses = sRefSet_newCopy (e->uses);
595 ret->sets = sRefSet_newCopy (e->sets);
596 ret->msets = sRefSet_newCopy (e->msets);
599 ret->val = multiVal_undefined;
600 ret->kind = XPR_EMPTY;
602 ret->etext = cstring_undefined;
603 ret->exitCode = XK_NEVERESCAPE;
604 ret->canBreak = FALSE;
605 ret->mustBreak = FALSE;
606 ret->isJumpPoint = FALSE;
607 ret->edata = exprData_undefined;
609 exprNode_defineConstraints(ret);
614 static /*@notnull@*/ /*@special@*/ exprNode
615 exprNode_createSemiCopy (exprNode e)
617 /*@post:isnull result->edata, result->etext, result->sets,
618 result->msets, result->uses, result->guards@*/
621 if (exprNode_isError (e))
623 return exprNode_createPlain (ctype_unknown);
627 exprNode ret = exprNode_new ();
630 ret->val = multiVal_copy (e->val);
631 ret->loc = fileloc_copy (e->loc);
632 ret->guards = guardSet_undefined;
633 ret->uses = sRefSet_undefined;
634 ret->sets = sRefSet_undefined;
635 ret->msets = sRefSet_undefined;
637 ret->kind = XPR_EMPTY;
639 ret->etext = cstring_undefined;
640 ret->exitCode = XK_NEVERESCAPE;
641 ret->canBreak = FALSE;
642 ret->mustBreak = FALSE;
643 ret->isJumpPoint = FALSE;
644 ret->edata = exprData_undefined;
646 exprNode_defineConstraints(ret);
653 exprNode_isNullValue (exprNode e)
655 if (exprNode_isDefined (e))
657 multiVal m = exprNode_getValue (e);
659 if (multiVal_isInt (m))
661 return (multiVal_forceInt (m) == 0);
669 exprNode_isUnknownConstant (/*@notnull@*/ exprNode e)
671 while (e->kind == XPR_PARENS)
673 e = exprData_getUopNode (e->edata);
674 llassert (exprNode_isDefined (e));
677 if (e->kind == XPR_CONST)
679 multiVal m = exprNode_getValue (e);
681 if (multiVal_isUnknown (m))
691 exprNode_numLiteral (ctype c, /*@temp@*/ cstring t,
692 /*@only@*/ fileloc loc, long val)
694 exprNode e = exprNode_createLoc (c, loc);
696 e->kind = XPR_NUMLIT;
698 llassert (multiVal_isUndefined (e->val));
699 e->val = multiVal_makeInt (val);
700 e->edata = exprData_makeLiteral (cstring_copy (t));
704 e->sref = sRef_makeUnknown ();
705 sRef_setDefNull (e->sref, e->loc);
708 DPRINTF (("Num lit: %s / %s", exprNode_unparse (e), ctype_unparse (exprNode_getType (e))));
713 exprNode_charLiteral (char c, cstring text, /*@only@*/ fileloc loc)
715 exprNode e = exprNode_createLoc (ctype_char, loc);
717 if (context_getFlag (FLG_CHARINTLITERAL))
719 e->typ = ctype_makeConj (ctype_char, ctype_int);
722 e->kind = XPR_NUMLIT;
723 e->val = multiVal_makeChar (c);
725 e->edata = exprData_makeLiteral (cstring_copy (text));
730 exprNode_floatLiteral (double d, ctype ct, cstring text, /*@only@*/ fileloc loc)
732 exprNode e = exprNode_createLoc (ct, loc);
734 e->kind = XPR_NUMLIT;
735 e->val = multiVal_makeDouble (d);
736 e->edata = exprData_makeLiteral (cstring_copy (text));
740 multiVal exprNode_getValue (exprNode e)
742 while (exprNode_isInParens (e)) {
743 if (e->edata != NULL) {
744 e = exprData_getUopNode (e->edata);
750 if (exprNode_isDefined (e)) {
753 return multiVal_undefined;
758 exprNode_combineLiterals (exprNode e, exprNode rest)
762 /* Both must be string literals. */
764 if (exprNode_isUndefined (rest) || exprNode_isUndefined (e))
766 exprNode_free (rest);
770 if (!exprNode_isStringLiteral (e))
774 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e),
775 exprNode_unparse (rest)),
777 exprNode_free (rest);
781 if (!exprNode_isStringLiteral (rest))
785 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e), exprNode_unparse (rest)),
788 exprNode_free (rest);
792 ns = cstring_concat (multiVal_forceString (exprNode_getValue (e)),
793 multiVal_forceString (exprNode_getValue (rest)));
795 multiVal_free (e->val);
796 exprData_free (e->edata, e->kind);
797 e->edata = exprData_makeLiteral (cstring_copy (ns));
798 e->val = multiVal_makeString (ns);
799 exprNode_free (rest);
804 exprNode_rawStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
806 exprNode e = exprNode_createLoc (ctype_string, loc);
807 int len = cstring_length (t);
809 if (context_getFlag (FLG_STRINGLITERALLEN))
811 if (len > context_getValue (FLG_STRINGLITERALLEN))
813 voptgenerror (FLG_STRINGLITERALLEN,
815 ("String literal length (%d) exceeds maximum "
816 "length (%d): \"%s\"",
818 context_getValue (FLG_STRINGLITERALLEN),
824 e->kind = XPR_STRINGLITERAL;
825 e->val = multiVal_makeString (cstring_copy (t));
826 e->edata = exprData_makeLiteral (t);
827 e->sref = sRef_makeType (ctype_string);
829 if (context_getFlag (FLG_READONLYSTRINGS))
831 sRef_setAliasKind (e->sref, AK_STATIC, fileloc_undefined);
832 sRef_setExKind (e->sref, XO_OBSERVER, loc);
836 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
839 return (e); /* s released */
843 exprNode_stringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
845 int len = cstring_length (t) - 2;
846 char *ts = cstring_toCharsSafe (t);
847 char *s = cstring_toCharsSafe (cstring_create (len + 1));
849 llassert (*ts == '\"' && *(ts + len + 1) == '\"');
850 strncpy (s, ts+1, size_fromInt (len));
853 return exprNode_rawStringLiteral (cstring_fromCharsO (s), loc);
856 exprNode exprNode_fromUIO (cstring c)
858 fileloc loc = context_getSaveLocation ();
859 exprNode e = exprNode_createPlain (ctype_unknown);
863 if (fileloc_isUndefined (loc))
865 loc = fileloc_copy (g_currentloc);
868 e->loc = loc; /* save loc was mangled */
871 if (usymtab_exists (c))
873 uentry ue = usymtab_lookupEither (c);
875 if (uentry_isDatatype (ue)
876 && uentry_isSpecified (ue))
879 (message ("%q: Specified datatype %s used in code, but not defined. "
880 "(Cannot continue reasonably from this error.)",
881 fileloc_unparse (e->loc), c));
889 llassertprint (!usymtab_exists (c), ("Entry exists: %s", c));
892 ** was supercedeGlobalEntry...is this better?
895 if (!context_inIterEnd ())
897 if (context_inMacro ())
899 if (context_getFlag (FLG_UNRECOG))
903 message ("Unrecognized identifier in macro definition: %s", c), e->loc);
907 flagcode_recordSuppressed (FLG_UNRECOG);
913 (FLG_UNRECOG, message ("Unrecognized identifier: %s", c), e->loc);
917 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
919 /* No alias errors for unrecognized identifiers */
920 sRef_setAliasKind (e->sref, AK_ERROR, loc);
925 exprNode exprNode_createId (/*@observer@*/ uentry c)
927 if (uentry_isValid (c))
929 exprNode e = exprNode_new ();
931 e->typ = uentry_getType (c);
933 if (uentry_isFunction (c)
934 && !sRef_isLocalVar (uentry_getSref (c)))
936 e->sref = sRef_undefined;
940 e->sref = uentry_getSref (c);
943 if (sRef_isStateUnknown (e->sref) && uentry_isNonLocal (c))
945 sRef_setDefined (e->sref, fileloc_undefined);
949 ** yoikes! leaving this out was a heinous bug...that would have been
950 ** caught if i had lclint working first. gag!
953 e->etext = cstring_undefined;
955 if (uentry_isEitherConstant (c))
958 e->val = multiVal_copy (uentry_getConstantValue (c));
963 e->val = multiVal_unknown ();
966 e->edata = exprData_makeId (c);
967 e->loc = context_getSaveLocation ();
969 if (fileloc_isUndefined (e->loc))
971 fileloc_free (e->loc);
972 e->loc = fileloc_copy (g_currentloc);
975 e->guards = guardSet_new ();
976 e->sets = sRefSet_new ();
977 e->msets = sRefSet_new ();
978 e->uses = sRefSet_new ();
980 /*> missing fields, detected by lclint <*/
981 e->exitCode = XK_NEVERESCAPE;
982 e->isJumpPoint = FALSE;
984 e->mustBreak = FALSE;
986 exprNode_defineConstraints(e);
992 return exprNode_createUnknown ();
996 /*@notnull@*/ exprNode
997 exprNode_fromIdentifier (/*@observer@*/ uentry c)
1001 if (context_justPopped ()) /* watch out! c could be dead */
1003 uentry ce = usymtab_lookupSafe (LastIdentifier ());
1005 if (uentry_isValid (ce))
1011 llbuglit ("Looks like Aunt Millie forgot to walk to dog again.");
1015 ret = exprNode_fromIdentifierAux (c);
1021 static /*@only@*/ /*@notnull@*/ exprNode
1022 exprNode_fromIdentifierAux (/*@observer@*/ uentry c)
1024 exprNode e = exprNode_createId (c);
1027 uentry_setUsed (c, e->loc);
1029 if (uentry_isVar (c) && sRef_isFileOrGlobalScope (sr))
1031 checkGlobUse (c, FALSE, e);
1038 exprNode_isZero (exprNode e)
1040 if (exprNode_isDefined (e))
1042 multiVal m = exprNode_getValue (e);
1044 if (multiVal_isInt (m))
1046 return (multiVal_forceInt (m) == 0);
1054 exprNode_isNonNegative (exprNode e)
1056 if (exprNode_isDefined (e))
1058 multiVal m = exprNode_getValue (e);
1060 if (multiVal_isInt (m))
1062 return (multiVal_forceInt (m) >= 0);
1070 ** a[x] - uses a but NOT a[]
1071 ** result sref = a[] (set/use in assignment)
1073 ** The syntax x[a] is also legal in C, and has the same
1074 ** semantics. If ind is an array, and arr is an int, flip
1079 exprNode_arrayFetch (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
1082 ** error in arr, error propagates (no new messages)
1083 ** error in ind, assume valid and continue
1086 if (exprNode_isError (e1))
1089 return (exprNode_makeError ());
1095 ctype carr = exprNode_getType (e1);
1096 ctype crarr = ctype_realType (carr);
1099 ** this sets up funny aliasing, that leads to spurious
1100 ** lclint errors. Hence, the i2 comments.
1103 if (!ctype_isRealArray (crarr)
1104 && ctype_isRealNumeric (crarr)
1105 && !exprNode_isError (e2)
1106 && ctype_isRealAP (exprNode_getType (e2))) /* fetch like 3[a] */
1111 carr = exprNode_getType (arr);
1112 crarr = ctype_realType (carr);
1120 if (sRef_possiblyNull (arr->sref))
1122 if (!usymtab_isGuarded (arr->sref))
1124 if (optgenerror (FLG_NULLDEREF,
1125 message ("Index of %s pointer %q: %s",
1126 sRef_nullMessage (arr->sref),
1127 sRef_unparse (arr->sref),
1128 exprNode_unparse (arr)),
1131 sRef_showNullInfo (arr->sref);
1133 /* suppress future messages */
1134 sRef_setNullError (arr->sref);
1139 if (exprNode_isError (ind))
1141 if ((ctype_isArrayPtr (crarr)
1142 && !ctype_isFunction (crarr))
1143 || ctype_isUnknown (carr))
1145 exprNode ret = exprNode_createPartialCopy (arr);
1147 if (ctype_isKnown (carr))
1149 ret->typ = ctype_baseArrayPtr (crarr);
1153 ret->typ = ctype_unknown;
1156 ret->sref = sRef_makeArrayFetch (arr->sref);
1158 ret->kind = XPR_FETCH;
1161 ** Because of funny aliasing (when arr and ind are
1162 ** flipped) spurious errors would be reported here.
1165 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1166 checkSafeUse (ret, arr->sref);
1171 voptgenerror (FLG_TYPE,
1172 message ("Array fetch from non-array (%t): %s[%s]", carr,
1173 exprNode_unparse (e1), exprNode_unparse (e2)),
1175 exprNode_free (arr);
1176 return (exprNode_makeError ());
1181 if (!ctype_isForceRealInt (&(ind->typ)))
1183 ctype rt = ctype_realType (ind->typ);
1185 if (ctype_isChar (rt))
1189 message ("Array fetch using non-integer, %t: %s[%s]",
1191 exprNode_unparse (e1), exprNode_unparse (e2)),
1194 else if (ctype_isEnum (rt))
1198 message ("Array fetch using non-integer, %t: %s[%s]",
1200 exprNode_unparse (e1), exprNode_unparse (e2)),
1207 message ("Array fetch using non-integer, %t: %s[%s]",
1209 exprNode_unparse (e1), exprNode_unparse (e2)),
1213 multiVal_free (ind->val);
1214 ind->val = multiVal_unknown ();
1217 if (ctype_isArrayPtr (crarr) && !ctype_isFunction (crarr))
1219 exprNode ret = exprNode_createSemiCopy (arr);
1220 multiVal m = exprNode_getValue (ind);
1222 ret->typ = ctype_baseArrayPtr (crarr);
1223 ret->kind = XPR_FETCH;
1225 if (multiVal_isInt (m))
1227 int i = (int) multiVal_forceInt (m);
1229 if (sRef_isValid (arr->sref)) {
1230 ret->sref = sRef_makeArrayFetchKnown (arr->sref, i);
1232 ret->sref = sRef_undefined;
1237 ret->sref = sRef_makeArrayFetch (arr->sref);
1240 ret->sets = sRefSet_realNewUnion (arr->sets, ind->sets);
1241 ret->msets = sRefSet_realNewUnion (arr->msets, ind->msets);
1242 ret->uses = sRefSet_realNewUnion (arr->uses, ind->uses);
1244 /* (see comment on spurious errors above) */
1245 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1247 exprNode_checkUse (ret, ind->sref, ind->loc);
1248 exprNode_checkUse (ret, arr->sref, arr->loc);
1254 if (ctype_isUnknown (carr))
1256 exprNode ret = exprNode_createPartialCopy (arr);
1258 ret->kind = XPR_FETCH;
1259 ret->typ = ctype_unknown;
1260 ret->sets = sRefSet_union (ret->sets, ind->sets);
1261 ret->msets = sRefSet_union (ret->msets, ind->msets);
1262 ret->uses = sRefSet_union (ret->uses, ind->uses);
1264 /* (see comment on spurious errors above) */
1265 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1267 exprNode_checkUse (ret, ind->sref, ind->loc);
1268 exprNode_checkUse (ret, arr->sref, arr->loc);
1275 message ("Array fetch from non-array (%t): %s[%s]", carr,
1276 exprNode_unparse (e1), exprNode_unparse (e2)),
1279 exprNode_free (arr);
1280 exprNode_free (ind);
1282 return (exprNode_makeError ());
1292 checkArgs (uentry fcn, /*@dependent@*/ exprNode f, ctype t,
1293 exprNodeList args, exprNode ret)
1295 return (checkArgsReal (fcn, f, ctype_argsFunction (t), args, FALSE, ret));
1299 ** checkPrintfArgs --- checks arguments for printf-like functions
1300 ** Arguments before ... have already been checked.
1301 ** The argument before the ... is a char *.
1302 ** argno is the format string argument.
1306 checkPrintfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1307 exprNodeList args, exprNode ret, int argno)
1310 ** the last argument before the elips is the format string
1315 int nargs = exprNodeList_size (args);
1316 uentryList params = uentry_getParams (fcn);
1320 ** These should be ensured by checkSpecialFunction
1323 llassert (uentryList_size (params) == argno + 1);
1324 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1326 a = exprNodeList_getN (args, argno - 1);
1327 formatloc = fileloc_copy (exprNode_loc (a));
1329 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1330 && exprNode_knownStringValue (a))
1332 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1333 char *code = format;
1336 nargs = exprNodeList_size (args);
1338 while ((code = strchr (code, '%')) != NULL)
1340 char *origcode = code;
1341 cstring codetext = cstring_newEmpty ();
1342 char key = *(++code);
1343 ctype modtype = ctype_int;
1344 bool modified = FALSE;
1346 fileloc_addColumn (formatloc, code - ocode);
1348 codetext = cstring_appendChar (codetext, key);
1351 while (isFlagKey (key))
1354 codetext = cstring_appendChar (codetext, key);
1355 fileloc_incColumn (formatloc);
1358 if (key == 'm') /* skipped in syslog */
1363 /* ignore field width */
1364 while (isdigit ((int) key) != 0)
1367 codetext = cstring_appendChar (codetext, key);
1368 fileloc_incColumn (formatloc);
1371 /* ignore precision */
1375 codetext = cstring_appendChar (codetext, key);
1376 fileloc_incColumn (formatloc);
1379 ** In printf, '*' means: read the next arg as an int for the
1380 ** field width. This seems to be missing from my copy of the
1381 ** standard x3.159-1989. Setion 4.9.6.1 refers to * (described
1382 ** later) but never does.
1387 ; /* don't do anything --- handle later */
1391 while (isdigit ((int) key) != 0)
1394 codetext = cstring_appendChar (codetext, key);
1395 fileloc_incColumn (formatloc);
1402 modtype = ctype_sint; /* short */
1404 codetext = cstring_appendChar (codetext, key);
1405 fileloc_incColumn (formatloc);
1407 else if (key == 'l' || key == 'L')
1409 modtype = ctype_lint; /* long */
1411 codetext = cstring_appendChar (codetext, key);
1412 fileloc_incColumn (formatloc);
1414 if (key == 'l' || key == 'L') {
1415 modtype = ctype_llint; /* long long */
1417 codetext = cstring_appendChar (codetext, key);
1418 fileloc_incColumn (formatloc);
1426 /* now, key = type of conversion to apply */
1428 fileloc_incColumn (formatloc);
1436 message ("No argument corresponding to %q format "
1437 "code %d (%%%s): \"%s\"",
1438 uentry_getName (fcn),
1440 cstring_fromChars (format)),
1443 if (fileloc_isDefined (formatloc)
1444 && context_getFlag (FLG_SHOWCOL))
1446 llgenindentmsg (cstring_makeLiteral ("Corresponding format code"),
1454 a = exprNodeList_getN (args, i);
1457 if (!exprNode_isError (a))
1463 case '*': /* int argument for fieldwidth */
1464 expecttype = ctype_int;
1465 *(--code) = '%'; /* convert it for next code */
1466 fileloc_subColumn (formatloc, 1);
1467 /*@switchbreak@*/ break;
1470 expecttype = ctype_combine (ctype_uint, modtype);
1471 /*@switchbreak@*/ break;
1473 case 'i': /* int argument */
1475 expecttype = ctype_combine (ctype_int, modtype);
1476 /*@switchbreak@*/ break;
1478 case 'x': /* unsigned int */
1480 expecttype = ctype_combine (ctype_uint, modtype);
1482 /*@switchbreak@*/ break;
1488 case 'f': /* double */
1489 expecttype = ctype_combine (ctype_double, modtype);
1490 /*@switchbreak@*/ break;
1492 case 'c': /* int converted to char (check its a char?) */
1493 expecttype = ctype_makeConj (ctype_char, ctype_uchar);
1494 /*@switchbreak@*/ break;
1496 case 's': /* string */
1497 expecttype = ctype_string;
1498 /*@switchbreak@*/ break;
1501 while (((key = *(++code)) != ']')
1504 codetext = cstring_appendChar (codetext, key);
1505 fileloc_incColumn (formatloc);
1511 (message ("Bad character set format: %s",
1512 cstring_fromChars (origcode)));
1515 expecttype = ctype_string;
1516 /*@switchbreak@*/ break;
1518 case 'p': /* pointer */
1519 expecttype = ctype_makePointer (ctype_void);
1520 uentry_setDefState (regArg, SS_RELDEF); /* need not be defined */
1521 sRef_setPosNull (uentry_getSref (regArg), fileloc_undefined); /* could be null */
1522 /*@switchbreak@*/ break;
1524 case 'n': /* pointer to int, modified by call! */
1525 expecttype = ctype_combine (ctype_makePointer (ctype_int), modtype);
1527 uentry_setDefState (regArg, SS_ALLOCATED); /* corresponds to out */
1528 /*@switchbreak@*/ break;
1530 case 'm': /* in a syslog, it doesn't consume an argument */
1531 /* should check we're really doing syslog */
1533 /*@switchbreak@*/ break;
1537 expecttype = ctype_unknown;
1541 message ("Unrecognized format code: %s",
1542 cstring_fromChars (origcode)),
1543 fileloc_isDefined (formatloc)
1544 ? formatloc : g_currentloc);
1546 /*@switchbreak@*/ break;
1549 if (!(exprNode_matchArgType (expecttype, a)))
1551 if (ctype_isVoidPointer (expecttype)
1552 && ctype_isRealAbstract (a->typ)
1553 && (context_getFlag (FLG_ABSTVOIDP)))
1559 if (llgenformattypeerror
1560 (expecttype, exprNode_undefined,
1562 message ("Format argument %d to %q (%%%s) expects "
1565 uentry_getName (fcn),
1568 a->typ, exprNode_unparse (a)),
1571 if (fileloc_isDefined (formatloc)
1572 && context_getFlag (FLG_SHOWCOL))
1575 (cstring_makeLiteral
1576 ("Corresponding format code"),
1583 uentry_setType (regArg, expecttype);
1584 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
1586 if (ctype_equal (expecttype, ctype_string))
1588 exprNode_checkUse (a, sRef_makePointer (a->sref), a->loc);
1591 uentry_setType (regArg, ctype_unknown);
1592 uentry_fixupSref (regArg);
1596 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1607 cstring_free (codetext);
1612 voptgenerror (FLG_TYPE,
1613 message ("Format string for %q has %d arg%&, given %d",
1614 uentry_getName (fcn), i - argno, nargs - argno),
1620 /* no checking possible for compile-time unknown format strings */
1621 if (exprNode_isDefined (a))
1625 message ("Format string parameter to %s is not a compile-time constant: %s",
1626 exprNode_unparse (f),
1627 exprNode_unparse (a)),
1632 fileloc_free (formatloc);
1636 checkScanfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1637 exprNodeList args, exprNode ret, int argno)
1641 int nargs = exprNodeList_size (args);
1642 uentryList params = uentry_getParams (fcn);
1646 ** These should be ensured by checkSpecialFunction
1649 llassert (uentryList_size (params) == argno + 1);
1650 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1652 a = exprNodeList_getN (args, argno - 1);
1653 formatloc = fileloc_copy (exprNode_loc (a));
1655 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1656 && exprNode_knownStringValue (a))
1658 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1659 char *code = format;
1662 nargs = exprNodeList_size (args);
1664 while ((code = strchr (code, '%')) != NULL)
1666 char *origcode = code;
1667 char key = *(++code);
1668 cstring codetext = cstring_newEmpty ();
1669 ctype modtype = ctype_int;
1670 char modifier = '\0';
1671 bool modified = TRUE;
1672 bool ignore = FALSE;
1674 codetext = cstring_appendChar (codetext, key);
1675 fileloc_addColumn (formatloc, code - ocode);
1678 ** this is based on ANSI standard library description of fscanf
1679 ** (from ANSI standard X3.159-1989, 4.9.6.1)
1682 /* '*' suppresses assignment (does not need match argument) */
1687 codetext = cstring_appendChar (codetext, key);
1690 fileloc_incColumn (formatloc);
1693 /* ignore field width */
1694 while (isdigit ((int) key) != 0)
1697 codetext = cstring_appendChar (codetext, key);
1698 fileloc_incColumn (formatloc);
1703 modtype = ctype_sint; /* short */
1705 codetext = cstring_appendChar (codetext, key);
1706 fileloc_incColumn (formatloc);
1708 else if (key == 'l' || key == 'L')
1710 modtype = ctype_lint; /* long */
1714 codetext = cstring_appendChar (codetext, key);
1716 fileloc_incColumn (formatloc);
1718 if (key == 'l' || key == 'L') {
1719 modtype = ctype_llint; /* long long */
1721 codetext = cstring_appendChar (codetext, key);
1722 fileloc_incColumn (formatloc);
1730 /* now, key = type of conversion to apply */
1732 fileloc_incColumn (formatloc);
1746 message ("No argument corresponding to %q format "
1747 "code %d (%%%s): \"%s\"",
1748 uentry_getName (fcn),
1750 cstring_fromChars (format)),
1753 if (fileloc_isDefined (formatloc)
1754 && context_getFlag (FLG_SHOWCOL))
1757 (cstring_makeLiteral ("Corresponding format code"),
1765 a = exprNodeList_getN (args, i);
1768 if (!exprNode_isError (a))
1774 case '*': /* int argument for fieldwidth */
1775 expecttype = ctype_makePointer (ctype_int);
1776 *(--code) = '%'; /* convert it for next code */
1777 fileloc_subColumn (formatloc, 1);
1778 /*@switchbreak@*/ break;
1781 expecttype = ctype_makePointer (ctype_combine (ctype_uint, modtype));
1782 /*@switchbreak@*/ break;
1787 case 'X': /* unsigned int */
1788 expecttype = ctype_makePointer (ctype_combine (ctype_int, modtype));
1789 /*@switchbreak@*/ break;
1796 /* printf is double, scanf is float! */
1798 if (modifier == 'l')
1800 expecttype = ctype_makePointer (ctype_double);
1802 else if (modifier == 'L')
1804 expecttype = ctype_makePointer (ctype_ldouble);
1808 llassert (modifier == '\0');
1809 expecttype = ctype_makePointer (ctype_float);
1811 /*@switchbreak@*/ break;
1813 case 'c': /* int converted to char (check its a char?) */
1814 expecttype = ctype_makePointer (ctype_makeConj (ctype_char, ctype_uchar));
1815 /*@switchbreak@*/ break;
1817 case 's': /* string */
1818 expecttype = ctype_string;
1819 /*@switchbreak@*/ break;
1823 while (((key = *(++code)) != ']')
1826 codetext = cstring_appendChar (codetext, key);
1827 fileloc_incColumn (formatloc);
1833 (message ("Bad character set format: %s",
1834 cstring_fromChars (origcode)));
1837 expecttype = ctype_string;
1838 /*@switchbreak@*/ break;
1841 case 'p': /* pointer */
1844 message ("Format code should not be used in scanf: %s",
1845 cstring_fromChars (origcode)),
1846 fileloc_isDefined (formatloc)
1847 ? formatloc : g_currentloc);
1849 expecttype = ctype_unknown;
1850 /*@switchbreak@*/ break;
1852 case 'n': /* pointer to int, modified by call! */
1853 expecttype = ctype_makePointer (ctype_int);
1854 /*@switchbreak@*/ break;
1857 expecttype = ctype_unknown;
1861 message ("Unrecognized format code: %s",
1862 cstring_fromChars (origcode)),
1863 fileloc_isDefined (formatloc)
1864 ? formatloc : g_currentloc);
1866 /*@switchbreak@*/ break;
1869 if (!(exprNode_matchArgType (expecttype, a)))
1871 if (ctype_isVoidPointer (expecttype)
1872 && ctype_isRealAbstract (a->typ)
1873 && (context_getFlag (FLG_ABSTVOIDP)))
1879 if (llgenformattypeerror
1880 (expecttype, exprNode_undefined,
1882 message ("Format argument %d to %q (%%%s) expects "
1885 uentry_getName (fcn),
1886 codetext, expecttype,
1887 a->typ, exprNode_unparse (a)),
1890 if (fileloc_isDefined (formatloc)
1891 && context_getFlag (FLG_SHOWCOL))
1894 (cstring_makeLiteral
1895 ("Corresponding format code"),
1902 uentry_setType (outArg, expecttype);
1903 checkOneArg (outArg, a, f, FALSE, i+1, nargs);
1904 uentry_setType (outArg, ctype_unknown);
1905 uentry_fixupSref (outArg);
1909 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1914 /* a->sref = defref; */
1921 cstring_free (codetext);
1926 voptgenerror (FLG_TYPE,
1927 message ("Format string for %q has %d arg%&, given %d",
1928 uentry_getName (fcn), i - argno, nargs - argno),
1934 /* no checking possible for compile-time unknown format strings */
1937 fileloc_free (formatloc);
1941 checkMessageArgs (/*@notnull@*/ /*@dependent@*/ exprNode f,
1944 /*@unused@*/ int argno)
1947 ** the last argument before the elips is the format string
1950 int nargs = exprNodeList_size (args);
1955 a = exprNodeList_getN (args, argno - 1);
1956 formatloc = fileloc_copy (exprNode_loc (a));
1958 if (ctype_isUnknown (cstringType)) {
1959 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
1961 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
1965 if (ctype_isUnknown (ctypeType)) {
1966 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
1968 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
1972 if (ctype_isUnknown (filelocType)) {
1973 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
1975 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
1979 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1980 && exprNode_knownStringValue (a))
1982 cstring format = multiVal_forceString (exprNode_getValue (a));
1983 char *code = cstring_toCharsSafe (format);
1986 nargs = exprNodeList_size (args);
1988 while ((code = strchr (code, '%')) != NULL)
1990 char *origcode = code;
1991 char key = *(++code);
1992 cstring codetext = cstring_newEmpty ();
1993 bool isOnly = FALSE;
1995 codetext = cstring_appendChar (codetext, key);
1997 fileloc_addColumn (formatloc, code - ocode);
1999 while (key >= '0' && key <= '9')
2002 codetext = cstring_appendChar (codetext, key);
2003 fileloc_incColumn (formatloc);
2007 fileloc_incColumn (formatloc);
2011 if (key == '&') /* plural marker */
2020 message ("Message missing format arg %d (%%%s): \"%s\"",
2021 i + 1, codetext, format),
2027 a = exprNodeList_getN (args, i);
2031 if (!exprNode_isError (a))
2035 /*@-loopswitchbreak@*/
2041 expecttype = ctype_char; break;
2043 expecttype = cstringType; break;
2045 expecttype = cstringType; isOnly = TRUE; break;
2047 expecttype = cstringType; isOnly = TRUE; break;
2048 case 'd': expecttype = ctype_int; break;
2049 case 'u': expecttype = ctype_uint; break;
2050 case 'w': expecttype = ctype_ulint; break;
2051 case 'f': expecttype = ctype_float; break;
2052 case 'b': expecttype = ctype_bool; break;
2053 case 't': expecttype = ctypeType; break;
2054 case 'l': expecttype = filelocType; break;
2055 case '&': /* a wee bit of a hack methinks */
2056 expecttype = ctype_int;
2058 case 'r': expecttype = ctype_bool; break;
2060 expecttype = ctype_unknown;
2063 message ("Unrecognized format code: %s",
2064 cstring_fromChars (origcode)),
2065 fileloc_isDefined (formatloc)
2066 ? formatloc : g_currentloc);
2069 /*@=loopswitchbreak@*/
2071 if (!(exprNode_matchArgType (expecttype, a)))
2073 if (ctype_isVoidPointer (expecttype)
2074 && ctype_isRealAbstract (a->typ)
2075 && (context_getFlag (FLG_ABSTVOIDP)))
2081 if (llgenformattypeerror
2082 (expecttype, exprNode_undefined,
2084 message ("Format argument %d to %q (%%%s) expects "
2087 uentry_getName (fcn),
2088 codetext, expecttype,
2089 a->typ, exprNode_unparse (a)),
2092 if (fileloc_isDefined (formatloc)
2093 && context_getFlag (FLG_SHOWCOL))
2096 (cstring_makeLiteral
2097 ("Corresponding format code"),
2104 if (ctype_equal (expecttype, cstringType))
2108 checkOneArg (csOnlyArg, a, f, FALSE, i+1, nargs);
2109 uentry_fixupSref (csOnlyArg);
2113 checkOneArg (csArg, a, f, FALSE, i+1, nargs);
2114 uentry_fixupSref (csArg);
2119 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
2120 uentry_fixupSref (regArg);
2126 cstring_free (codetext);
2131 voptgenerror (FLG_TYPE,
2132 message ("Format string for %q has %d arg%&, given %d",
2133 uentry_getName (fcn), i - argno, nargs -argno),
2139 /* no checking possible for compile-time unknown format strings */
2142 fileloc_free (formatloc);
2146 checkExpressionDefinedAux (/*@notnull@*/ exprNode e1,
2147 /*@notnull@*/ exprNode e2,
2153 bool hadUncon = FALSE;
2155 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e1->sref)) &&
2156 sRefSet_hasUnconstrained (sets2))
2159 (FLG_EVALORDERUNCON,
2161 ("Expression may have undefined behavior (%q used in right operand "
2162 "may set global variable %q used in left operand): %s %s %s",
2163 sRefSet_unparseUnconstrained (sets2),
2164 sRef_unparse (sRef_getRootBase (e1->sref)),
2165 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2169 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e2->sref)) &&
2170 sRefSet_hasUnconstrained (sets1))
2173 (FLG_EVALORDERUNCON,
2175 ("Expression has undefined behavior (%q used in left operand "
2176 "may set global variable %q used in right operand): %s %s %s",
2177 sRefSet_unparseUnconstrained (sets1),
2178 sRef_unparse (e2->sref),
2179 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2183 sRefSet_realElements (e1->uses, sr)
2185 if (sRef_isMeaningful (sr) && sRefSet_member (sets2, sr))
2190 ("Expression has undefined behavior (left operand uses %q, "
2191 "modified by right operand): %s %s %s",
2193 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2196 } end_sRefSet_realElements;
2198 sRefSet_realElements (sets1, sr)
2200 if (sRef_isMeaningful (sr))
2202 if (sRef_same (sr, e2->sref))
2207 ("Expression has undefined behavior (value of right operand "
2208 "modified by left operand): %s %s %s",
2209 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2212 else if (sRefSet_member (e2->uses, sr))
2217 ("Expression has undefined behavior (left operand modifies %q, "
2218 "used by right operand): %s %s %s",
2220 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2225 if (sRefSet_member (sets2, sr))
2227 if (sRef_isUnconstrained (sr))
2235 hadUncon = optgenerror
2236 (FLG_EVALORDERUNCON,
2238 ("Expression may have undefined behavior. Left operand "
2239 "calls %q; right operand calls %q. The unconstrained "
2240 "functions may modify global state used by "
2241 "the other operand): %s %s %s",
2242 sRefSet_unparseUnconstrained (sets1),
2243 sRefSet_unparseUnconstrained (sets2),
2244 exprNode_unparse (e1), lltok_unparse (op),
2245 exprNode_unparse (e2)),
2254 ("Expression has undefined behavior (both "
2255 "operands modify %q): %s %s %s",
2257 exprNode_unparse (e1),
2258 lltok_unparse (op), exprNode_unparse (e2)),
2264 } end_sRefSet_realElements;
2267 static void checkExpressionDefined (exprNode e1, exprNode e2, lltok op)
2269 bool hasError = FALSE;
2271 if (exprNode_isError (e1) || exprNode_isError (e2))
2276 if (sRefSet_member (e2->sets, e1->sref))
2278 if (e2->kind == XPR_CALL)
2284 hasError = optgenerror
2286 message ("Expression has undefined behavior "
2287 "(value of left operand is modified "
2288 "by right operand): %s %s %s",
2289 exprNode_unparse (e1), lltok_unparse (op),
2290 exprNode_unparse (e2)),
2295 if (context_getFlag (FLG_EVALORDERUNCON))
2297 if (sRefSet_member (e2->msets, e1->sref))
2299 if (e2->kind == XPR_CALL)
2305 hasError = optgenerror
2308 ("Expression has undefined behavior (value of left "
2309 "operand may be modified by right operand): %s %s %s",
2310 exprNode_unparse (e1), lltok_unparse (op),
2311 exprNode_unparse (e2)),
2319 checkExpressionDefinedAux (e1, e2, e1->sets, e2->sets, op, FLG_EVALORDER);
2321 if (context_maybeSet (FLG_EVALORDERUNCON))
2323 checkExpressionDefinedAux (e1, e2, e1->msets,
2324 e2->msets, op, FLG_EVALORDERUNCON);
2329 static void checkSequencing (exprNode p_f, exprNodeList p_args);
2332 checkArgsReal (uentry fcn, /*@dependent@*/ exprNode f, uentryList cl,
2333 exprNodeList args, bool isIter, exprNode ret)
2337 if (!exprNode_isError (f))
2339 if (!uentryList_isMissingParams (cl))
2341 int nargs = exprNodeList_size (args);
2342 int expectargs = uentryList_size (cl);
2346 if (expectargs == 0)
2354 message ("Iter %q invoked with %d args, "
2356 uentry_getName (fcn),
2364 message ("Function %s called with %d args, "
2366 exprNode_unparse (f), nargs),
2373 last = uentry_getType (uentryList_getN (cl, expectargs - 1));
2375 exprNodeList_reset (args);
2377 uentryList_elements (cl, current)
2379 ctype ct = uentry_getType (current);
2382 if (ctype_isElips (ct))
2385 ** do special checking for printf/scanf library functions
2387 ** this is kludgey code, just for handling the special case
2391 if (uentry_isPrintfLike (fcn))
2393 checkPrintfArgs (f, fcn, args, ret, i);
2396 else if (uentry_isScanfLike (fcn))
2398 checkScanfArgs (f, fcn, args, ret, i);
2401 else if (uentry_isMessageLike (fcn))
2403 checkMessageArgs (f, fcn, args, i);
2408 llassert (!uentry_isSpecialFunction (fcn));
2411 nargs = expectargs; /* avoid errors */
2416 if (i >= nargs) break;
2418 a = exprNodeList_current (args);
2419 exprNodeList_advance (args);
2423 if (exprNode_isError (a))
2430 probably necessary? I'm not sure about this one
2431 checkMacroParen (a);
2434 f->guards = guardSet_union (f->guards, a->guards);
2436 DPRINTF (("match arg: %s / %s", ctype_unparse (ct), ctype_unparse (a->typ)));
2438 if (!(exprNode_matchArgType (ct, a)))
2440 DPRINTF (("Args mismatch!"));
2442 if (ctype_isVoidPointer (ct)
2443 && (ctype_isPointer (a->typ)
2444 && (ctype_isRealAbstract (ctype_baseArrayPtr (a->typ)))))
2449 ("Pointer to abstract type (%t) used "
2451 "(arg %d to %q): %s",
2453 uentry_getName (fcn),
2454 exprNode_unparse (a)),
2462 (ct, exprNode_undefined,
2465 ("Iter %q expects arg %d to "
2466 "be %t gets %t: %s",
2467 uentry_getName (fcn),
2468 i, ct, a->typ, exprNode_unparse (a)),
2479 ("Function %q expects arg %d to be %t gets %t: %s",
2480 uentry_getName (fcn),
2481 i, ct, a->typ, exprNode_unparse (a)),
2484 DPRINTF (("Types: %s / %s",
2486 ctype_unparse (a->typ)));
2490 ** Clear null marker for abstract types.
2491 ** (It is not revealed, so suppress future messages.)
2494 if (ctype_isAbstract (a->typ))
2496 sRef_setNullUnknown (exprNode_getSref (a), a->loc);
2503 } end_uentryList_elements ;
2506 if (expectargs != nargs) /* note: not != since we may have ... */
2508 if (ctype_isElips (last))
2512 message ("Function %s called with %d args, expects at least %d",
2513 exprNode_unparse (f),
2514 nargs, expectargs - 1),
2523 message ("Iter %q invoked with %d args, expects %d",
2524 uentry_getName (fcn), nargs, expectargs),
2531 message ("Function %s called with %d args, expects %d",
2532 exprNode_unparse (f),
2545 ** Check for undefined code sequences in function arguments:
2547 ** one parameter sets something used by another parameter
2548 ** one parameter sets something set by another parameter
2552 checkSequencingOne (exprNode f, exprNodeList args,
2553 /*@notnull@*/ exprNode el, int argno)
2556 ** Do second loop, iff +undefunspec
2560 int numloops = context_maybeSet (FLG_EVALORDERUNCON) ? 2 : 1;
2562 for (checkloop = 0; checkloop < numloops; checkloop++)
2568 thissets = el->sets;
2572 llassert (checkloop == 1);
2573 thissets = el->msets;
2576 sRefSet_realElements (thissets, thisset)
2580 /*@access exprNodeList@*/
2581 for (j = 0; j < args->nelements; j++)
2583 exprNode jl = args->elements[j];
2584 int thisargno = j + 1;
2586 if (thisargno != argno && exprNode_isDefined (jl))
2588 sRefSet otheruses = jl->uses;
2590 if (sRef_isFileOrGlobalScope (sRef_getRootBase (jl->sref)) &&
2591 sRefSet_hasUnconstrained (thissets))
2594 (FLG_EVALORDERUNCON,
2597 ("%q used in argument %d may set "
2598 "global variable %q used by argument %d: %s(%q)",
2599 cstring_capitalizeFree (sRefSet_unparseUnconstrained (thissets)),
2602 sRef_unparse (sRef_getRootBase (jl->sref)),
2604 exprNode_unparse (f), exprNodeList_unparse (args)),
2608 if (sRefSet_member (otheruses, thisset))
2610 if (sRef_isUnconstrained (thisset))
2613 (FLG_EVALORDERUNCON,
2615 ("Unconstrained functions used in arguments %d (%q) "
2616 "and %d (%s) may modify "
2617 "or use global state in undefined way: %s(%q)",
2619 sRefSet_unparseUnconstrainedPlain (otheruses),
2621 sRef_unconstrainedName (thisset),
2622 exprNode_unparse (f),
2623 exprNodeList_unparse (args)),
2631 ("Argument %d modifies %q, used by argument %d "
2632 "(order of evaluation of actual parameters is "
2633 "undefined): %s(%q)",
2634 argno, sRef_unparse (thisset), thisargno,
2635 exprNode_unparse (f), exprNodeList_unparse (args)),
2641 sRefSet othersets = jl->sets;
2643 if (sRefSet_member (othersets, thisset))
2645 if (sRef_isUnconstrained (thisset))
2648 (FLG_EVALORDERUNCON,
2650 ("Unconstrained functions used in "
2651 "arguments %d (%q) and %d (%s) may modify "
2652 "or use global state in undefined way: %s(%q)",
2654 sRefSet_unparseUnconstrainedPlain (othersets),
2656 sRef_unconstrainedName (thisset),
2657 exprNode_unparse (f), exprNodeList_unparse (args)),
2665 ("Argument %d modifies %q, set by argument %d (order of"
2666 " evaluation of actual parameters is undefined): %s(%q)",
2667 argno, sRef_unparse (thisset), thisargno,
2668 exprNode_unparse (f), exprNodeList_unparse (args)),
2675 /*@noaccess exprNodeList@*/
2676 } end_sRefSet_realElements;
2681 checkSequencing (exprNode f, exprNodeList args)
2683 if (exprNodeList_size (args) > 1)
2688 /*@access exprNodeList*/
2690 for (i = 0; i < args->nelements; i++)
2692 el = args->elements[i];
2694 if (!exprNode_isError (el))
2696 checkSequencingOne (f, args, el, i + 1);
2699 /*@noaccess exprNodeList*/
2704 ** requires le = exprNode_getUentry (f)
2708 checkGlobMods (/*@notnull@*/ /*@dependent@*/ exprNode f,
2709 uentry le, exprNodeList args,
2710 /*@notnull@*/ exprNode ret, int specialArgs)
2712 bool isSpec = FALSE;
2713 bool hasMods = FALSE;
2715 globSet usesGlobs = globSet_undefined;
2716 sRefSet mods = sRefSet_undefined;
2717 bool freshMods = FALSE;
2718 uentryList params = uentryList_undefined;
2720 DPRINTF (("Check glob mods: %s", exprNode_unparse (ret)));
2723 ** check globals and modifies
2728 if (!uentry_isValid (le))
2730 ctype fr = ctype_realType (f->typ);
2732 if (ctype_isFunction (fr))
2734 params = ctype_argsFunction (fr);
2738 params = uentryList_missingParams;
2741 if (!context_getFlag (FLG_MODNOMODS)
2742 && !context_getFlag (FLG_GLOBUNSPEC))
2744 checkUnspecCall (f, params, args);
2750 fname = uentry_rawName (le);
2754 if (uentry_isFunction (le))
2756 params = uentry_getParams (le);
2757 mods = uentry_getMods (le);
2758 hasMods = uentry_hasMods (le);
2759 usesGlobs = uentry_getGlobs (le);
2760 isSpec = uentry_isSpecified (le);
2762 else /* not a function */
2764 ctype ct = ctype_realType (uentry_getType (le));
2766 llassertprint (uentry_isVar (le) && ctype_isFunction (ct),
2767 ("checkModGlobs: uentry not a function: %s",
2768 uentry_unparse (le)));
2770 params = ctype_argsFunction (ct);
2771 return; /*@32 ! remove this? */
2780 globSet_allElements (usesGlobs, el)
2782 if (sRef_isValid (el))
2784 if (sRef_isInternalState (el) || sRef_isSystemState (el))
2786 context_usedGlobal (el);
2787 exprNode_checkUse (f, el, f->loc);
2789 if (context_checkInternalUse ())
2791 if (!context_globAccess (el))
2793 if (sRef_isSystemState (el)
2794 && !context_getFlag (FLG_MODFILESYSTEM))
2803 ("Called procedure %s may access %q, but "
2804 "globals list does not include globals %s",
2805 exprNode_unparse (f),
2807 cstring_makeLiteralTemp (sRef_isInternalState (el)
2815 else if (sRef_isNothing (el) || sRef_isSpecState (el))
2821 uentry gle = sRef_getUentry (el);
2822 sRef sr = sRef_updateSref (el);
2824 if (sRef_isUndefGlob (el))
2826 sRef_setDefined (sr, f->loc);
2827 exprNode_checkSet (f, sr);
2835 if (sRef_isAllocated (el))
2837 exprNode_checkSet (f, sr);
2841 if (sRef_isStateUndefined (sr))
2846 ("%s %q used by function undefined before call: %s",
2847 sRef_getScopeName (sr),
2849 exprNode_unparse (f)),
2851 sRef_setDefined (sr, f->loc);
2853 exprNode_checkUse (f, sr, f->loc);
2856 checkGlobUse (gle, TRUE, f);
2859 if (sRef_isKilledGlob (el))
2861 sRef_kill (sr, f->loc);
2862 context_usedGlobal (sr);
2866 } end_globSet_allElements;
2872 if (context_hasMods () || context_getFlag (FLG_MODNOMODS))
2874 sRefSet smods = sRefSet_undefined;
2877 ** NEED to check for modifies anything
2881 ** check each sRef that called function modifies (ml), is
2887 sRefSet_allElements (mods, s) /* s is something which may be modified */
2889 DPRINTF (("Check modify: %s", sRef_unparse (s)));
2891 if (sRef_isKindSpecial (s))
2893 if (sRef_isSpecInternalState (s))
2895 if (context_getFlag (FLG_MODINTERNALSTRICT))
2897 exprNode_checkCallModifyVal (s, args, f, ret);
2901 sRefSet mmods = context_modList ();
2903 sRefSet_allElements (mmods, el)
2905 if (sRef_isInternalState (el))
2907 sRef_setModified (el);
2909 } end_sRefSet_allElements ;
2914 exprNode_checkCallModifyVal (s, args, f, ret);
2919 sRef rb = sRef_getRootBase (s);
2921 if (sRef_isFileOrGlobalScope (rb))
2923 context_usedGlobal (rb);
2926 if (sRef_isFileStatic (s)
2927 && !fileId_equal (fileloc_fileId (f->loc),
2928 fileloc_fileId (uentry_whereDefined (le))))
2930 smods = sRefSet_insert (smods, s);
2934 exprNode_checkCallModifyVal (s, args, f, ret);
2937 } end_sRefSet_allElements;
2942 ** Static elements in modifies set can have nasty consequences.
2943 ** (I think...have not been able to reproduce a possible bug.)
2946 if (!sRefSet_isDefined (smods))
2948 mods = sRefSet_newCopy (mods);
2951 sRefSet_allElements (smods, el)
2953 bool res = sRefSet_delete (mods, el);
2956 } end_sRefSet_allElements;
2958 sRefSet_free (smods);
2963 else if (sRefSet_isDefined (mods))
2964 { /* just check observers */
2967 sRefSet_allElements (mods, s) /* s is something which may be modified */
2969 sRef rb = sRef_getRootBase (s);
2973 if (sRef_isParam (rb))
2975 sRef b = sRef_fixBaseParam (s, args);
2977 if (sRef_isObserver (b))
2979 exprNode e = exprNodeList_nth (args, sRef_getParam (rb));
2983 message ("Function call may modify observer%q: %s",
2984 sRef_unparsePreOpt (b), exprNode_unparse (e)),
2987 sRef_showExpInfo (b);
2991 } end_sRefSet_allElements;
2995 if (!hasMods) /* no specified modifications */
2997 if (context_getFlag (FLG_MODOBSERVERUNCON))
2999 exprNodeList_elements (args, e)
3001 if (exprNode_isDefined (e))
3003 sRef s = exprNode_getSref (e);
3005 if (sRef_isObserver (s)
3006 && ctype_isMutable (sRef_getType (s)))
3009 (FLG_MODOBSERVERUNCON,
3011 ("Call to unconstrained function %s may modify observer%q: %s",
3012 exprNode_unparse (f),
3013 sRef_unparsePreOpt (s), exprNode_unparse (e)),
3016 sRef_showExpInfo (s);
3020 } end_exprNodeList_elements;
3025 checkAnyCall (f, fname, params, args, hasMods, mods, isSpec, specialArgs);
3027 ret->uses = sRefSet_union (ret->uses, f->uses);
3028 ret->sets = sRefSet_union (ret->sets, f->sets);
3029 ret->msets = sRefSet_union (ret->msets, f->msets);
3034 ** Spurious errors reported, because lclint can't tell
3035 ** mods must be fresh if freshMods is true.
3038 /*@i@*/ sRefSet_free (mods);
3044 void checkGlobUse (uentry glob, bool isCall, /*@notnull@*/ exprNode e)
3046 if (uentry_isVar (glob))
3048 if (context_inFunctionLike ())
3050 sRef sr = uentry_getSref (glob);
3052 context_usedGlobal (sr);
3054 if (context_checkGlobUse (glob))
3056 if (!context_globAccess (sr))
3062 message ("Called procedure %s may access %s %q",
3063 exprNode_unparse (e),
3064 sRef_unparseScope (sr),
3065 uentry_getName (glob)),
3072 message ("Undocumented use of %s %s",
3073 sRef_unparseScope (sr),
3074 exprNode_unparse (e)),
3083 llbug (message ("Global not variable: %q", uentry_unparse (glob)));
3088 reflectEnsuresClause (uentry le, exprNode f, exprNodeList args)
3090 DPRINTF (("Reflect ensures clause: %s(%s) / %s / %s",
3091 exprNode_unparse (f), exprNodeList_unparse (args),
3092 uentry_unparseFull (le),
3093 stateClauseList_unparse (uentry_getStateClauseList (le))));
3095 if (uentry_isValid (le) && uentry_isFunction (le))
3097 stateClauseList sclauses = uentry_getStateClauseList (le);
3099 if (stateClauseList_isDefined (sclauses))
3101 DPRINTF (("Reflect ensures: %s / %s / %s",
3102 uentry_unparse (le),
3103 exprNode_unparse (f), exprNodeList_unparse (args)));
3105 stateClauseList_elements (sclauses, cl)
3107 if (stateClause_hasEnsures (cl))
3109 /* Same in usymtab.c:1904 */
3110 if (stateClause_setsMetaState (cl))
3112 qual q = stateClause_getMetaQual (cl);
3113 annotationInfo ainfo = qual_getAnnotationInfo (q);
3114 metaStateInfo minfo = annotationInfo_getState (ainfo);
3115 cstring key = metaStateInfo_getName (minfo);
3116 int mvalue = annotationInfo_getValue (ainfo);
3118 sRefSet osrs = sRefSet_undefined;
3121 if (stateClause_isGlobal (cl))
3123 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3128 srs = stateClause_getRefs (cl);
3131 DPRINTF (("Reflect ensures clause: %s", stateClause_unparse (cl)));
3134 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
3136 sRefSet_elements (srs, sel)
3140 if (sRef_isResult (sRef_getRootBase (sel)))
3142 ; /*@i423 what do we do about results */
3146 s = sRef_fixBaseParam (sel, args);
3147 DPRINTF (("Reflecting state clause on: %s / %s",
3148 sRef_unparse (sel), sRef_unparse (s)));
3150 sRef_setMetaStateValueComplete (s, key, mvalue, exprNode_loc (f));
3152 } end_sRefSet_elements;
3154 sRefSet_free (osrs);
3158 sRefSet srs = stateClause_getRefs (cl);
3159 sRefModVal modf = stateClause_getEnsuresFunction (cl);
3160 int eparam = stateClause_getStateParameter (cl);
3162 DPRINTF (("Reflect after clause: %s / %s",
3163 stateClause_unparse (cl),
3164 sRefSet_unparse (srs)));
3166 sRefSet_elements (srs, sel)
3170 DPRINTF (("elements: %s", sRef_unparse (sel)));
3171 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3173 if (sRef_isResult (sRef_getRootBase (sel)))
3175 ; /*@i423 what do we do about results */
3179 s = sRef_fixBaseParam (sel, args);
3181 DPRINTF (("elements: %s", sRef_unparse (s)));
3182 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3184 DPRINTF (("Reflecting state clause on: %s / %s",
3185 sRef_unparse (sel), sRef_unparse (s)));
3187 modf (s, eparam, exprNode_loc (f));
3189 } end_sRefSet_elements;
3192 } end_stateClauseList_elements ;
3199 checkRequiresClause (uentry le, exprNode f, exprNodeList args)
3201 DPRINTF (("Check requires clause: %s(%s) / %s / %s",
3202 exprNode_unparse (f), exprNodeList_unparse (args),
3203 uentry_unparseFull (le),
3204 stateClauseList_unparse (uentry_getStateClauseList (le))));
3206 if (uentry_isValid (le) && uentry_isFunction (le))
3208 stateClauseList sclauses = uentry_getStateClauseList (le);
3210 if (stateClauseList_isDefined (sclauses))
3212 DPRINTF (("Check requires: %s / %s / %s",
3213 uentry_unparse (le),
3214 exprNode_unparse (f), exprNodeList_unparse (args)));
3216 stateClauseList_elements (sclauses, cl)
3218 DPRINTF (("Check clause: %s / %s",
3219 stateClause_unparse (cl),
3220 bool_unparse (stateClause_hasRequires (cl))));
3222 if (stateClause_hasRequires (cl))
3224 sRefSet osrs = sRefSet_undefined;
3227 if (stateClause_isGlobal (cl))
3229 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3234 srs = stateClause_getRefs (cl);
3237 DPRINTF (("Refs: %s", sRefSet_unparse (srs)));
3239 if (stateClause_setsMetaState (cl))
3241 qual q = stateClause_getMetaQual (cl);
3242 annotationInfo ainfo = qual_getAnnotationInfo (q);
3243 metaStateInfo minfo = annotationInfo_getState (ainfo);
3244 cstring key = metaStateInfo_getName (minfo);
3245 int mvalue = annotationInfo_getValue (ainfo);
3247 DPRINTF (("Requires meta state! %s = %d", key, mvalue));
3249 sRefSet_elements (srs, sel)
3251 sRef s = sRef_fixBaseParam (sel, args);
3253 if (sRef_isResult (sRef_getRootBase (sel)))
3255 ; /*@i423 what do we do about results */
3259 DPRINTF (("Checking state clause on: %s / %s / %s = %d",
3260 sRef_unparseFull (sel), sRef_unparseFull (s),
3263 if (!sRef_checkMetaStateValue (s, key, mvalue))
3265 DPRINTF (("HERE: %s", sRef_unparse (s)));
3269 ("Requires clause of called function %q not satisfied%q (state is %q): %q",
3270 uentry_getName (le),
3271 sRef_isGlobalMarker (s)
3273 : message (" by %q", sRef_unparse (s)),
3274 stateValue_unparseValue (sRef_getMetaStateValue (s, key),
3276 stateClause_unparse (cl)),
3279 sRef_showAliasInfo (s);
3283 DPRINTF (("Error supressed!"));
3284 DPRINTF (("Loc: %s", fileloc_unparse (exprNode_loc (f))));
3285 DPRINTF (("Context supress: %s",
3286 bool_unparse (context_suppressFlagMsg (FLG_STATETRANSFER, exprNode_loc (f)))));
3290 } end_sRefSet_elements;
3294 sRefModVal modf = stateClause_getRequiresBodyFunction (cl);
3295 int eparam = stateClause_getStateParameter (cl);
3297 DPRINTF (("Reflect after clause: %s / %s",
3298 stateClause_unparse (cl),
3299 sRefSet_unparse (srs)));
3301 sRefSet_elements (srs, sel)
3305 DPRINTF (("elements: %s", sRef_unparse (sel)));
3306 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3308 s = sRef_fixBaseParam (sel, args);
3310 DPRINTF (("elements: %s", sRef_unparse (s)));
3311 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3313 if (sRef_isResult (sRef_getRootBase (sel)))
3315 ; /*@i423 what do we do about results */
3319 DPRINTF (("Reflecting state clause on: %s / %s",
3320 sRef_unparse (sel), sRef_unparse (s)));
3322 modf (s, eparam, exprNode_loc (f));
3324 } end_sRefSet_elements;
3327 sRefSet_free (osrs);
3329 } end_stateClauseList_elements ;
3334 static /*@only@*/ exprNode
3335 functionCallSafe (/*@only@*/ /*@notnull@*/ exprNode f,
3336 ctype t, /*@keep@*/ exprNodeList args)
3338 /* requires f is a non-error exprNode, with type function */
3339 cstring fname = exprNode_unparse (f);
3340 uentry le = exprNode_getUentry (f);
3341 exprNode ret = exprNode_createPartialCopy (f);
3346 DPRINTF (("Call: %s %s",exprNode_unparse (f), exprNodeList_unparse (args)));
3348 ret->typ = ctype_getReturnType (t);
3349 ret->kind = XPR_CALL;
3351 ret->edata = exprData_makeCall (f, args);
3354 ** Order of these steps is very important!
3356 ** Must check for argument dependencies before messing up uses and sets.
3359 if (context_getFlag (FLG_EVALORDER))
3361 exprNodeList_elements (args, current)
3363 if (exprNode_isDefined (current))
3365 exprNode_addUse (current, current->sref);
3367 } end_exprNodeList_elements;
3369 if (context_maybeSet (FLG_EVALORDER) || context_maybeSet (FLG_EVALORDERUNCON))
3371 checkSequencing (f, args);
3374 exprNodeList_elements (args, current)
3376 if (exprNode_isDefined (current) && sRef_isMeaningful (current->sref))
3378 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3380 } end_exprNodeList_elements ;
3383 special = checkArgs (le, f, t, args, ret);
3384 checkGlobMods (f, le, args, ret, special);
3385 checkRequiresClause (le, f, args);
3388 DPRINTF (("Reflect: %s", uentry_unparseFull (le)));
3389 reflectEnsuresClause (le, f, args);
3392 if (uentry_isValid (le)
3393 && (uentry_isFunction (le)
3394 || (uentry_isVariable (le)
3395 && ctype_isFunction (uentry_getType (le)))))
3397 exitkind exk = uentry_getExitCode (le);
3399 /* f->typ is already set to the return type */
3401 ret->sref = uentry_returnedRef (le, args);
3402 DPRINTF (("Returned: %s / %s",
3403 uentry_unparseFull (le),
3404 sRef_unparseFull (ret->sref)));
3406 if (uentry_isFunction (le) && exprNodeList_size (args) >= 1)
3408 qual nullPred = uentry_nullPred (le);
3410 if (qual_isTrueNull (nullPred))
3412 exprNode arg = exprNodeList_head (args);
3414 if (exprNode_isDefined (arg))
3416 ret->guards = guardSet_addFalseGuard (ret->guards, arg->sref);
3419 else if (qual_isFalseNull (nullPred))
3421 exprNode arg = exprNodeList_head (args);
3423 if (exprNode_isDefined (arg))
3425 ret->guards = guardSet_addTrueGuard (ret->guards, arg->sref);
3430 llassert (qual_isUnknown (nullPred));
3434 if (exitkind_isConditionalExit (exk))
3438 ** if (arg0) then { exit! } else { ; }
3440 ** if (arg0) then { ; } else { exit! }
3445 llassert (!exprNodeList_isEmpty (args));
3446 firstArg = exprNodeList_head (args);
3448 if (exprNode_isDefined (firstArg)
3449 && !guardSet_isEmpty (firstArg->guards))
3451 usymtab_trueBranch (guardSet_undefined);
3452 usymtab_altBranch (guardSet_undefined);
3454 if (exitkind_isTrueExit (exk))
3456 usymtab_popBranches (firstArg,
3457 exprNode_makeMustExit (),
3459 TRUE, TRUEEXITCLAUSE);
3463 usymtab_popBranches (firstArg,
3465 exprNode_makeMustExit (),
3466 TRUE, FALSEEXITCLAUSE);
3470 ret->exitCode = XK_MAYEXIT;
3472 else if (exitkind_mustExit (exk))
3474 ret->exitCode = XK_MUSTEXIT;
3476 else if (exitkind_couldExit (exk))
3478 ret->exitCode = XK_MAYEXIT;
3485 if (cstring_equalLit (fname, "exit"))
3487 if (exprNodeList_size (args) == 1)
3489 exprNode arg = exprNodeList_head (args);
3491 if (exprNode_isDefined (arg) && exprNode_knownIntValue (arg))
3493 long int val = multiVal_forceInt (exprNode_getValue (arg));
3500 ("Argument to exit has implementation defined behavior: %s",
3501 exprNode_unparse (arg)),
3502 exprNode_loc (arg));
3511 exprNode_checkSetAny (ret, uentry_rawName (le));
3518 ** this is yucky! should keep the uentry as part of exprNode!
3521 /*@observer@*/ uentry
3522 exprNode_getUentry (exprNode e)
3524 if (exprNode_isError (e))
3526 return uentry_undefined;
3530 cstring s = exprNode_rootVarName (e);
3531 uentry ue = usymtab_lookupSafe (s);
3538 exprNode_makeInitBlock (lltok brace, /*@only@*/ exprNodeList inits)
3540 exprNode ret = exprNode_createPlain (ctype_unknown);
3542 ret->kind = XPR_INITBLOCK;
3543 ret->edata = exprData_makeCall (exprNode_undefined, inits);
3544 ret->loc = fileloc_update (ret->loc, lltok_getLoc (brace));
3550 exprNode_functionCall (/*@only@*/ exprNode f, /*@only@*/ exprNodeList args)
3556 if (exprNode_isUndefined (f))
3559 exprNodeList_free (args);
3560 return exprNode_undefined;
3563 t = exprNode_getType (f);
3565 if (sRef_isLocalVar (f->sref))
3567 exprNode_checkUse (f, f->sref, f->loc);
3569 if (sRef_possiblyNull (f->sref))
3571 if (!usymtab_isGuarded (f->sref))
3573 if (optgenerror (FLG_NULLDEREF,
3574 message ("Function call using %s pointer %q",
3575 sRef_nullMessage (f->sref),
3576 sRef_unparse (f->sref)),
3579 sRef_showNullInfo (f->sref);
3580 sRef_setNullError (f->sref);
3588 if (ctype_isRealFunction (t))
3590 exprNode ret = functionCallSafe (f, t, args);
3594 else if (ctype_isUnknown (t))
3596 exprNode ret = exprNode_createPartialCopy (f);
3602 exprNodeList_elements (args, current)
3604 if (exprNode_isDefined (current))
3606 exprNode_checkUse (ret, current->sref, ret->loc);
3609 ** also, anything derivable from current->sref may be used
3612 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3613 exprNode_mergeUSs (ret, current);
3615 } end_exprNodeList_elements;
3617 ret->edata = exprData_makeCall (f, args);
3618 ret->kind = XPR_CALL;
3620 tstring = cstring_copy (exprNode_unparse (f));
3622 cstring_markOwned (tstring);
3623 exprNode_checkSetAny (ret, tstring);
3629 voptgenerror (FLG_TYPE,
3630 message ("Call to non-function (type %t): %s", t,
3631 exprNode_unparse (f)),
3634 exprNodeList_free (args);
3636 return (exprNode_makeError ());
3641 exprNode_fieldAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
3642 /*@only@*/ cstring f)
3644 exprNode ret = exprNode_createPartialCopy (s);
3646 ret->kind = XPR_FACCESS;
3648 if (exprNode_isError (s))
3650 ret->edata = exprData_makeField (s, f);
3655 ctype t = exprNode_getType (s);
3656 ctype tr = ctype_realType (t);
3658 checkMacroParen (s);
3660 ret->edata = exprData_makeField (s, f);
3662 if (ctype_isStructorUnion (tr))
3664 uentry tf = uentryList_lookupField (ctype_getFields (tr), f);
3666 if (uentry_isUndefined (tf))
3668 voptgenerror (FLG_TYPE,
3669 message ("Access non-existent field %s of %t: %s", f, t,
3670 exprNode_unparse (ret)),
3672 /*! cstring_free (f); */ /* evans 2001-03-25 self-detect */
3677 uentry_setUsed (tf, exprNode_loc (ret));
3679 ret->typ = uentry_getType (tf);
3680 checkSafeUse (ret, s->sref);
3682 ret->sref = sRef_makeField (s->sref, uentry_rawName (tf));
3683 /*!? exprNode_free (s); */ /* evans 2001-03-25 self-detect */
3687 else /* isStructorUnion */
3689 if (ctype_isRealAbstract (tr))
3693 message ("Access field of abstract type (%t): %s.%s",
3694 t, exprNode_unparse (s), f),
3696 ret->typ = ctype_unknown;
3700 if (ctype_isKnown (tr))
3705 ("Access field of non-struct or union (%t): %s.%s",
3706 t, exprNode_unparse (s), f),
3709 ret->typ = ctype_unknown;
3713 cstring sn = cstring_copy (f);
3715 checkSafeUse (ret, s->sref);
3716 cstring_markOwned (sn);
3717 ret->sref = sRef_makeField (s->sref, sn);
3729 exprNode_fieldAccess (/*@only@*/ exprNode s, /*@only@*/ lltok dot,
3730 /*@only@*/ cstring f)
3732 exprNode res = exprNode_fieldAccessAux (s, lltok_getLoc (dot), f);
3733 lltok_release (dot);
3738 exprNode_addParens (/*@only@*/ lltok lpar, /*@only@*/ exprNode e)
3740 exprNode ret = exprNode_createPartialCopy (e);
3742 ret->loc = fileloc_update (ret->loc, lltok_getLoc (lpar));
3743 ret->kind = XPR_PARENS;
3744 ret->edata = exprData_makeUop (e, lpar);
3746 if (!exprNode_isError (e))
3748 ret->exitCode = e->exitCode;
3749 ret->canBreak = e->canBreak;
3750 ret->mustBreak = e->mustBreak;
3751 ret->isJumpPoint = e->isJumpPoint;
3752 ret->sref = e->sref;
3759 exprNode_arrowAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
3760 /*@only@*/ cstring f)
3762 exprNode ret = exprNode_createPartialCopy (s);
3764 ret->edata = exprData_makeField (s, f);
3765 ret->kind = XPR_ARROW;
3767 if (exprNode_isError (s))
3773 ctype t = exprNode_getType (s);
3774 ctype tr = ctype_realType (t);
3776 checkMacroParen (s);
3778 (void) ctype_fixArrayPtr (tr); /* REWRITE THIS */
3780 if (ctype_isRealPointer (tr))
3782 ctype b = ctype_realType (ctype_baseArrayPtr (tr));
3784 if (ctype_isStructorUnion (b))
3786 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
3788 if (sRef_isKnown (s->sref) && sRef_possiblyNull (s->sref))
3790 if (!usymtab_isGuarded (s->sref) && !context_inProtectVars ())
3794 message ("Arrow access from %s pointer%q: %s",
3795 sRef_nullMessage (s->sref),
3796 sRef_unparsePreOpt (s->sref),
3797 exprNode_unparse (ret)),
3800 sRef_showNullInfo (s->sref);
3801 sRef_setNullError (s->sref);
3806 if (uentry_isUndefined (fentry))
3810 message ("Access non-existent field %s of %t: %s",
3811 f, t, exprNode_unparse (ret)),
3813 ret->typ = ctype_unknown;
3819 ** was safeUse: shouldn't be safe!
3822 ** rec must be defined,
3823 ** *rec must be allocated
3824 ** rec->field need only be defined it if is an rvalue
3827 uentry_setUsed (fentry, exprNode_loc (ret));
3828 ret->typ = uentry_getType (fentry);
3830 exprNode_checkUse (ret, s->sref, s->loc);
3832 /* exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc); */
3833 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
3837 else /* Pointer to something that is not a struct or union*/
3839 if (ctype_isRealAbstract (tr))
3841 ctype xrt = ctype_forceRealType (tr);
3845 message ("Arrow access field of abstract type (%t): %s->%s",
3846 t, exprNode_unparse (s), f),
3850 ** Set the state correctly, as if the abstraction is broken.
3853 if (ctype_isRealPointer (xrt) &&
3854 (b = ctype_realType (ctype_baseArrayPtr (xrt)),
3855 ctype_isStructorUnion (b)))
3857 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
3858 ret->typ = uentry_getType (fentry);
3859 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
3863 ret->typ = ctype_unknown;
3864 ret->sref = sRef_undefined;
3867 else /* not a struct, union or abstract */
3869 if (ctype_isUnknown (tr)) {
3870 cstring sn = cstring_copy (f);
3872 DPRINTF (("Here: %s", exprNode_unparse (s)));
3874 exprNode_checkUse (ret, s->sref, s->loc);
3875 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
3877 cstring_markOwned (sn);
3878 ret->sref = sRef_makeArrow (s->sref, sn);
3880 ret->kind = XPR_ARROW;
3885 message ("Arrow access field of non-struct or union "
3886 "pointer (%t): %s->%s",
3887 t, exprNode_unparse (s), f),
3890 ret->typ = ctype_unknown;
3891 ret->sref = sRef_undefined;
3896 else /* its not a pointer */
3898 if (!ctype_isUnknown (tr))
3902 message ("Arrow access of non-pointer (%t): %s->%s",
3903 t, exprNode_unparse (s), f),
3906 ret->typ = ctype_unknown;
3907 ret->sref = sRef_undefined;
3911 cstring sn = cstring_copy (f);
3913 DPRINTF (("Here: %s", exprNode_unparse (s)));
3915 exprNode_checkUse (ret, s->sref, s->loc);
3916 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
3918 cstring_markOwned (sn);
3919 ret->sref = sRef_makeArrow (s->sref, sn);
3921 ret->kind = XPR_ARROW;
3932 exprNode_arrowAccess (/*@only@*/ exprNode s,
3933 /*@only@*/ lltok arrow,
3934 /*@only@*/ cstring f)
3936 exprNode res = exprNode_arrowAccessAux (s, lltok_getLoc (arrow), f);
3937 lltok_release (arrow);
3942 ** only postOp's in C: i++ and i--
3946 exprNode_postOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
3948 /* check modification also */
3949 /* cstring opname = lltok_unparse (op);*/
3951 exprNode ret = exprNode_createPartialCopy (e);
3953 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
3954 ret->kind = XPR_POSTOP;
3955 ret->edata = exprData_makeUop (e, op);
3957 if (!exprNode_isDefined (e))
3962 checkMacroParen (e);
3964 exprNode_checkUse (ret, e->sref, e->loc);
3965 exprNode_checkSet (ret, e->sref);
3967 t = exprNode_getType (e);
3969 if (sRef_isUnsafe (e->sref))
3971 voptgenerror (FLG_MACROPARAMS,
3972 message ("Operand of %s is macro parameter (non-functional): %s%s",
3973 lltok_unparse (op), exprNode_unparse (e), lltok_unparse (op)),
3975 sRef_makeSafe (e->sref);
3976 sRef_makeSafe (ret->sref);
3979 if (ctype_isForceRealNumeric (&t) || ctype_isRealAP (t))
3985 if (ctype_isRealAbstract (t))
3989 message ("Operand of %s is abstract type (%t): %s",
3990 lltok_unparse (op), t, exprNode_unparse (e)),
3997 message ("Operand of %s is non-numeric (%t): %s",
3998 lltok_unparse (op), t, exprNode_unparse (e)),
4001 ret->typ = ctype_unknown;
4004 /* if (ctype_isZero (t)) e->typ = ctype_int; */
4006 exprNode_checkModify (e, ret);
4008 /* added 7/11/2000 D.L */
4010 /*DRL 6/8/01 I decided to disable all LCLint Warning here since the code
4011 probably needs a rewrite any way */
4015 // updateEnvironmentForPostOp (e);
4017 /* start modifications */
4018 /* added by Seejo on 4/16/2000 */
4020 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4022 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4024 ret->sref = sRef_copy (e->sref);
4027 if (lltok_getTok (op) == INC_OP) {
4028 if (sRef_getSize(e->sref) > 0) {
4030 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4032 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4033 /* Assumption: there is only 1 \0 in the buffer */
4034 /* This will not be correct if there are 2 \0's in the buffer */
4035 sRef_setNotNullTerminatedState(ret->sref);
4036 sRef_resetLen(ret->sref);
4038 sRef_setNullTerminatedState(ret->sref);
4039 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4041 if (sRef_isNullTerminated (ret->sref))
4042 printf ("ret->sref is Null Terminated\n");
4043 else if (sRef_isPossiblyNullTerminated (ret->sref))
4044 printf ("ret->sref is Possibly Null Terminated\n");
4045 else if (sRef_isNotNullTerminated (ret->sref))
4046 printf ("ret->sref is Not Null Terminated\n");
4051 if (lltok_getTok (op) == DEC_OP) {
4052 if (sRef_getSize(e->sref) >= 0) {
4053 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4054 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4059 /* end modifications */
4065 exprNode_preOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4067 bool checkMod = FALSE;
4069 int opid = lltok_getTok (op);
4070 exprNode ret = exprNode_createSemiCopy (e);
4072 exprNode_copySets (ret, e);
4074 multiVal_free (ret->val);
4075 ret->val = multiVal_undefined;
4076 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4077 ret->kind = XPR_PREOP;
4078 ret->edata = exprData_makeUop (e, op);
4080 if (exprNode_isError (e))
4085 checkMacroParen (e);
4087 te = exprNode_getType (e);
4088 tr = ctype_realType (te);
4090 if (opid != TAMPERSAND)
4092 exprNode_checkUse (ret, e->sref, e->loc);
4094 if (ctype_isRealAbstract (tr)
4095 && (!(ctype_isRealBool (te) && (opid == TEXCL))))
4097 if (optgenerror (FLG_ABSTRACT,
4098 message ("Operand of %s is abstract type (%t): %s",
4099 lltok_unparse (op), tr,
4100 exprNode_unparse (ret)),
4103 tr = te = ctype_unknown;
4104 ret->typ = ctype_unknown;
4105 sRef_setNullError (e->sref);
4113 case DEC_OP: /* should also check modification! */
4114 if (sRef_isMacroParamRef (e->sref))
4118 message ("Operand of %s is macro parameter (non-functional): %s",
4119 lltok_unparse (op), exprNode_unparse (ret)),
4124 exprNode_checkSet (ret, e->sref);
4127 if (ctype_isForceRealNumeric (&tr) || ctype_isRealAP (tr))
4132 if (context_msgStrictOps ())
4136 message ("Operand of %s is non-numeric (%t): %s",
4137 lltok_unparse (op), te, exprNode_unparse (ret)),
4140 ret->typ = ctype_int;
4143 /* start modifications */
4144 /* added by Seejo on 4/16/2000 */
4146 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4148 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4150 ret->sref = sRef_copy (e->sref);
4153 if (lltok_getTok (op) == INC_OP) {
4154 if (sRef_getSize(e->sref) > 0) {
4156 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4158 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4159 /* Assumption: there is only 1 \0 in the buffer */
4160 /* This will not be correct if there are 2 \0's in the buffer */
4161 sRef_setNotNullTerminatedState(ret->sref);
4162 sRef_resetLen (ret->sref);
4164 sRef_setNullTerminatedState(ret->sref);
4165 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4171 if (lltok_getTok (op) == DEC_OP) {
4172 if (sRef_getSize(e->sref) >= 0) {
4173 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4174 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4179 /* end modifications */
4186 if (ctype_isForceRealNumeric (&tr))
4190 ret->val = multiVal_invert (exprNode_getValue (e));
4194 ret->val = multiVal_copy (exprNode_getValue (e));
4199 if (context_msgStrictOps ())
4203 message ("Operand of %s is non-numeric (%t): %s",
4204 lltok_unparse (op), te, exprNode_unparse (ret)),
4208 ret->typ = ctype_int;
4212 case TEXCL: /* maybe this should be restricted */
4213 guardSet_flip (ret->guards);
4215 if (ctype_isRealBool (te) || ctype_isUnknown (te))
4221 if (ctype_isRealPointer (tr))
4223 if (sRef_isKnown (e->sref))
4225 ret->guards = guardSet_addFalseGuard (ret->guards, e->sref);
4229 (FLG_BOOLOPS, FLG_PTRNEGATE,
4230 message ("Operand of %s is non-boolean (%t): %s",
4231 lltok_unparse (op), te, exprNode_unparse (ret)),
4238 message ("Operand of %s is non-boolean (%t): %s",
4239 lltok_unparse (op), te, exprNode_unparse (ret)),
4243 ret->typ = ctype_bool;
4248 if (ctype_isForceRealInt (&tr))
4253 if (context_msgStrictOps ())
4257 message ("Operand of %s is non-integer (%t): %s",
4258 lltok_unparse (op), te, exprNode_unparse (ret)),
4262 if (ctype_isInt (e->typ))
4268 ret->typ = ctype_int;
4274 ret->typ = ctype_makePointer (e->typ);
4276 if (sRef_isKnown (e->sref))
4278 ret->sref = sRef_makeAddress (e->sref);
4285 if (ctype_isAP (tr))
4287 ret->typ = ctype_baseArrayPtr (e->typ);
4291 if (ctype_isKnown (te))
4293 if (ctype_isFunction (te))
4299 message ("Dereference of function type (%t): %s",
4300 te, exprNode_unparse (ret)),
4305 voptgenerror (FLG_TYPE,
4306 message ("Dereference of non-pointer (%t): %s",
4307 te, exprNode_unparse (ret)),
4309 ret->typ = ctype_unknown;
4314 ret->typ = ctype_unknown;
4319 if (sRef_isKnown (e->sref))
4321 if (sRef_possiblyNull (e->sref))
4323 if (!usymtab_isGuarded (e->sref) && !context_inProtectVars ())
4327 message ("Dereference of %s pointer %q: %s",
4328 sRef_nullMessage (e->sref),
4329 sRef_unparse (e->sref),
4330 exprNode_unparse (ret)),
4333 sRef_showNullInfo (e->sref);
4334 sRef_setNotNull (e->sref, e->loc); /* suppress future messages */
4339 ret->sref = sRef_makePointer (e->sref);
4344 llbug (message ("exprNode_preOp: unhandled op: %s", lltok_unparse (op)));
4349 exprNode_checkModify (e, ret);
4356 ** any reason to disallow sizeof (abstract type) ?
4360 ** used by both sizeof
4364 ctype sizeof_resultType (void)
4366 static ctype sizet = ctype_unknown;
4368 if (ctype_isUnknown (sizet))
4370 if (usymtab_existsType (cstring_makeLiteralTemp ("size_t")))
4372 sizet = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("size_t")));
4376 sizet = ctype_ulint;
4383 exprNode_sizeofType (/*@only@*/ qtype qt)
4385 exprNode ret = exprNode_create (sizeof_resultType ());
4386 ctype ct = qtype_getType (qt);
4388 ret->kind = XPR_SIZEOFT;
4389 ret->edata = exprData_makeSizeofType (qt);
4391 voptgenerror (FLG_SIZEOFTYPE,
4392 message ("Parameter to sizeof is type %s: %s",
4394 exprNode_unparse (ret)),
4401 exprNode_alignofType (/*@only@*/ qtype qt)
4403 exprNode ret = exprNode_create (sizeof_resultType ());
4404 ctype ct = qtype_getType (qt);
4406 ret->kind = XPR_ALIGNOFT;
4407 ret->edata = exprData_makeSizeofType (qt);
4409 voptgenerror (FLG_SIZEOFTYPE,
4410 message ("Parameter to alignof is type %s: %s",
4412 exprNode_unparse (ret)),
4418 exprNode exprNode_offsetof (qtype qt, cstringList s)
4420 exprNode ret = exprNode_create (sizeof_resultType ());
4421 ctype ct = qtype_getType (qt);
4423 ret->kind = XPR_OFFSETOF;
4424 ret->edata = exprData_makeOffsetof (qt, s);
4426 if (!ctype_isRealSU (ct))
4428 voptgenerror (FLG_TYPE,
4429 message ("First parameter to offsetof is not a "
4430 "struct or union type (type %s): %s",
4432 exprNode_unparse (ret)),
4439 cstringList_elements (s, el) {
4443 if (ctype_isUndefined (lt))
4447 else if (!ctype_isRealSU (lt))
4449 voptgenerror (FLG_TYPE,
4450 message ("Inner offsetof type is not a "
4451 "struct or union type (type %s before field %s): %s",
4452 ctype_unparse (lt), el,
4453 exprNode_unparse (ret)),
4459 fields = ctype_getFields (ctype_realType (lt));
4460 fld = uentryList_lookupField (fields, el);
4461 DPRINTF (("Try: %s / %s", ctype_unparse (lt), el));
4463 if (uentry_isUndefined (fld))
4465 if (ctype_equal (lt, ct)) {
4466 voptgenerror (FLG_TYPE,
4467 message ("Field %s in offsetof is not the "
4468 "name of a field of %s: %s",
4471 exprNode_unparse (ret)),
4474 voptgenerror (FLG_TYPE,
4475 message ("Deep field %s in offsetof is not the "
4476 "name of a field of %s: %s",
4479 exprNode_unparse (ret)),
4485 lt = uentry_getType (fld);
4488 } end_cstringList_elements;
4490 /* Should report error if its a bit field - behavior is undefined! */
4497 exprNode_sizeofExpr (/*@only@*/ exprNode e)
4501 if (exprNode_isUndefined (e))
4503 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4504 ret->edata = exprData_makeSingle (e);
4505 ret->typ = sizeof_resultType ();
4506 ret->kind = XPR_SIZEOF;
4510 uentry u = exprNode_getUentry (e);
4512 ret = exprNode_createPartialCopy (e);
4513 ret->edata = exprData_makeSingle (e);
4515 ret->typ = sizeof_resultType ();
4516 ret->kind = XPR_SIZEOF;
4518 if (uentry_isValid (u)
4519 && uentry_isRefParam (u)
4520 && ctype_isRealArray (uentry_getType (u)))
4523 (FLG_SIZEOFFORMALARRAY,
4524 message ("Parameter to sizeof is an array-type function parameter: %s",
4525 exprNode_unparse (ret)),
4531 ** sizeof (x) doesn't "really" use x
4538 exprNode_alignofExpr (/*@only@*/ exprNode e)
4542 if (exprNode_isUndefined (e))
4544 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4548 ret = exprNode_createPartialCopy (e);
4551 ret->edata = exprData_makeSingle (e);
4552 ret->typ = sizeof_resultType ();
4553 ret->kind = XPR_ALIGNOF;
4556 ** sizeof (x) doesn't "really" use x
4563 exprNode_cast (/*@only@*/ lltok tok, /*@only@*/ exprNode e, /*@only@*/ qtype q)
4569 if (exprNode_isError (e))
4572 lltok_release (tok);
4573 return exprNode_undefined;
4576 checkMacroParen (e);
4578 c = qtype_getType (q);
4579 t = exprNode_getType (e);
4581 ret = exprNode_createPartialCopy (e);
4583 ret->loc = fileloc_update (ret->loc, lltok_getLoc (tok));
4585 ret->kind = XPR_CAST;
4586 ret->edata = exprData_makeCast (tok, e, q);
4588 if (ctype_isRealSU (ctype_getBaseType (sRef_getType (e->sref))))
4591 ** This is a bit of a hack to avoid a problem
4592 ** when the code does,
4593 ** (some other struct) x
4598 ret->sref = sRef_copy (e->sref);
4599 usymtab_addForceMustAlias (ret->sref, e->sref);
4600 sRef_setTypeFull (ret->sref, c);
4601 DPRINTF (("Cast: %s -> %s", sRef_unparseFull (e->sref),
4602 sRef_unparseFull (ret->sref)));
4606 ret->sref = e->sref;
4607 sRef_setTypeFull (ret->sref, c);
4608 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
4615 ** void * <-> abstract * (if FLG_ABSTVOIDP)
4616 ** abstract * <-> void * (if FLG_ABSTVOIDP)
4619 if (ctype_isVoid (c)) /* cast to void is always okay --- discard value */
4623 else if (ctype_isRealAP (c)) /* casting to array or pointer */
4625 ctype bc = ctype_getBaseType (c);
4626 ctype bt = ctype_getBaseType (t);
4627 ctype rt = ctype_realType (t);
4629 if (ctype_isFunction (ctype_baseArrayPtr (ctype_realType (c)))
4630 && (ctype_isArrayPtr (rt)
4631 && !ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4635 message ("Cast from function pointer type (%t) to "
4636 "non-function pointer (%t): %s",
4637 c, t, exprNode_unparse (ret)),
4641 if (!ctype_isFunction (ctype_baseArrayPtr (c))
4642 && (ctype_isArrayPtr (rt)
4643 && ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4647 message ("Cast from non-function pointer type (%t) to "
4648 "function pointer (%t): %s",
4649 c, t, exprNode_unparse (ret)),
4653 if (exprNode_isZero (e) && context_getFlag (FLG_ZEROPTR) &&
4654 !(ctype_isRealAbstract (bc)
4655 && context_hasAccess (ctype_typeId (bc))))
4657 ; /* okay to cast zero */
4661 if (ctype_isRealAbstract (bc)
4662 && !context_hasAccess (ctype_typeId (bc)))
4664 if (ctype_isVoidPointer (t) || ctype_isUnknown (t))
4668 message ("Cast to underlying abstract type %t: %s",
4669 c, exprNode_unparse (ret)),
4676 message ("Cast to underlying abstract type %t: %s",
4677 c, exprNode_unparse (ret)),
4682 if (ctype_isRealAbstract (bt)
4683 && !context_hasAccess (ctype_typeId (bt)))
4685 if (ctype_isUnknown (c) || ctype_isVoidPointer (c))
4689 message ("Cast from underlying abstract type %t: %s",
4690 t, exprNode_unparse (ret)),
4697 message ("Cast from underlying abstract type %t: %s",
4698 t, exprNode_unparse (ret)),
4706 ctype bt = ctype_realType (ctype_getBaseType (t));
4707 ctype bc = ctype_realType (ctype_getBaseType (c));
4709 if (ctype_isAbstract (bt) && !context_hasAccess (ctype_typeId (bt)))
4711 if (ctype_match (c, t))
4713 if (ctype_equal (c, t))
4717 message ("Redundant cast involving abstract type %t: %s",
4718 bt, exprNode_unparse (ret)),
4726 message ("Cast from abstract type %t: %s",
4727 bt, exprNode_unparse (ret)),
4732 if (ctype_isAbstract (bc)
4733 && !context_hasAccess (ctype_typeId (bc)))
4735 if (ctype_match (c, t))
4741 DPRINTF (("No access to: %s / %d",
4742 ctype_unparse (bc), ctype_typeId (bc)));
4743 DPRINTF (("Context %s %s",
4744 bool_unparse (context_inFunctionLike ()),
4745 context_unparse ()));
4748 message ("Cast to abstract type %t: %s", bc,
4749 exprNode_unparse (ret)),
4755 if (ctype_isAbstract (c))
4757 if (sRef_isExposed (e->sref) || sRef_isOnly (e->sref))
4759 /* okay, cast exposed to abstract */
4760 sRef_clearExKindComplete (ret->sref, fileloc_undefined);
4764 if (ctype_isVisiblySharable (t)
4765 && sRef_isExternallyVisible (e->sref)
4766 && !(ctype_isAbstract (t)
4767 && context_hasAccess (ctype_typeId (t))))
4771 message ("Cast to abstract type from externally visible "
4772 "mutable storage exposes rep of %s: %s",
4774 exprNode_unparse (e)),
4784 evaluationOrderUndefined (lltok op)
4786 int opid = lltok_getTok (op);
4788 return (opid != AND_OP && opid != OR_OP);
4791 static bool checkIntegral (/*@notnull@*/ exprNode e1,
4792 /*@notnull@*/ exprNode e2,
4793 /*@notnull@*/ exprNode ret,
4798 ctype te1 = exprNode_getType (e1);
4799 ctype te2 = exprNode_getType (e2);
4801 ctype tr1 = ctype_realishType (te1);
4802 ctype tr2 = ctype_realishType (te2);
4804 if (ctype_isForceRealInt (&tr1) && ctype_isForceRealInt (&tr2))
4810 if (context_msgStrictOps ())
4812 if (!ctype_isInt (tr1) && !ctype_isInt (tr2))
4814 if (ctype_sameName (te1, te2))
4818 message ("Operands of %s are non-integer (%t): %s",
4819 lltok_unparse (op), te1,
4820 exprNode_unparse (ret)),
4827 message ("Operands of %s are non-integers (%t, %t): %s",
4828 lltok_unparse (op), te1, te2,
4829 exprNode_unparse (ret)),
4833 else if (!ctype_isInt (tr1))
4837 message ("Left operand of %s is non-integer (%t): %s",
4838 lltok_unparse (op), te1, exprNode_unparse (ret)),
4842 /* !ctype_isInt (te2) */
4846 message ("Right operand of %s is non-integer (%t): %s",
4847 lltok_unparse (op), te2, exprNode_unparse (ret)),
4857 ** returns exprNode representing e1 op e2
4859 ** uses msg if there are errors
4860 ** can be used for both assignment ops and regular ops
4865 static /*@only@*/ /*@notnull@*/ exprNode
4866 exprNode_makeOp (/*@keep@*/ exprNode e1, /*@keep@*/ exprNode e2,
4867 /*@keep@*/ lltok op)
4869 ctype te1, te2, tr1, tr2, tret;
4870 int opid = lltok_getTok (op);
4871 bool hasError = FALSE;
4874 if (exprNode_isError (e1))
4876 ret = exprNode_createPartialNVCopy (e2);
4880 ret = exprNode_createPartialNVCopy (e1);
4883 ret->val = multiVal_undefined;
4885 ret->edata = exprData_makeOp (e1, e2, op);
4887 if (exprNode_isError (e1) || exprNode_isError (e2))
4889 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
4890 || opid == EQ_OP || opid == NE_OP
4891 || opid == AND_OP || opid == OR_OP)
4893 ret->typ = ctype_bool;
4896 if (exprNode_isDefined (e1))
4898 exprNode_checkUse (ret, e1->sref, e1->loc);
4901 if (exprNode_isDefined (e2))
4903 exprNode_mergeUSs (ret, e2);
4904 exprNode_checkUse (ret, e2->sref, e2->loc);
4910 tret = ctype_unknown;
4911 te1 = exprNode_getType (e1);
4912 DPRINTF (("te1 = %s / %s", exprNode_unparse (e1), ctype_unparse (te1)));
4914 te2 = exprNode_getType (e2);
4916 tr1 = ctype_realishType (te1);
4917 tr2 = ctype_realishType (te2);
4921 ret->guards = guardSet_or (ret->guards, e2->guards);
4923 else if (opid == AND_OP)
4925 ret->guards = guardSet_and (ret->guards, e2->guards);
4932 if (opid == EQ_OP || opid == NE_OP)
4934 exprNode temp1 = e1, temp2 = e2;
4936 /* could do NULL == x */
4938 if (exprNode_isNullValue (e1) || exprNode_isUnknownConstant (e1))
4940 temp1 = e2; temp2 = e1;
4943 if (exprNode_isNullValue (temp2) || exprNode_isUnknownConstant (temp2))
4945 reflectNullTest (temp1, (opid == NE_OP));
4946 guardSet_free (ret->guards);
4947 ret->guards = guardSet_copy (temp1->guards);
4951 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
4952 || opid == EQ_OP || opid == NE_OP || opid == AND_OP || opid == OR_OP)
4957 if (anyAbstract (tr1, tr2) &&
4958 (!((ctype_isRealBool (te1) || ctype_isRealBool (te2)) &&
4959 (opid == AND_OP || opid == OR_OP
4960 || opid == EQ_OP || opid == NE_OP))))
4962 abstractOpError (tr1, tr2, op, e1, e2, e1->loc, e2->loc);
4964 else if (ctype_isUnknown (te1) || ctype_isUnknown (te2))
4966 /* unknown types, no comparisons possible */
4972 case TMULT: /* multiplication and division: */
4974 case MUL_ASSIGN: /* numeric, numeric -> numeric */
4975 case DIV_ASSIGN: /* */
4977 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
4980 case TPLUS: /* addition and subtraction: */
4981 case TMINUS: /* pointer, int -> pointer */
4982 case SUB_ASSIGN: /* int, pointer -> pointer */
4983 case ADD_ASSIGN: /* numeric, numeric -> numeric */
4985 tr1 = ctype_fixArrayPtr (tr1);
4987 if ((ctype_isRealPointer (tr1) && !exprNode_isNullValue (e1))
4988 && (!ctype_isRealPointer (tr2) && ctype_isRealInt (tr2)))
4992 if (context_msgPointerArith ())
4996 message ("Pointer arithmetic (%t, %t): %s",
4997 te1, te2, exprNode_unparse (ret)),
5001 if (sRef_possiblyNull (e1->sref)
5002 && !usymtab_isGuarded (e1->sref))
5005 (FLG_NULLPOINTERARITH,
5006 message ("Pointer arithmetic involving possibly "
5007 "null pointer %s: %s",
5008 exprNode_unparse (e1),
5009 exprNode_unparse (ret)),
5013 ret->sref = sRef_copy (e1->sref);
5015 /* start modifications */
5016 /* added by Seejo on 4/16/2000 */
5018 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5020 if ((sRef_isPossiblyNullTerminated (e1->sref)) || (sRef_isNullTerminated(e1->sref))) {
5021 //if (sRef_isKnown (e->sref)) {
5022 //ret->sref = sRef_makeAddress (e->sref);
5027 added ugly fixed to stop
5028 program from crashing on point + int +int
5029 one day I'll fix this or ask Seejo wtf the codes supposed to do. */
5031 if (!multiVal_isInt (e2->val) )
5035 val = (int) multiVal_forceInt (e2->val);
5037 /* Operator : + or += */
5038 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5039 if (sRef_getSize(e1->sref) >= val) {/* Incrementing the pointer by
5040 val should not result in a
5041 size < 0 (size = 0 is ok !) */
5043 sRef_setSize (ret->sref, sRef_getSize(e1->sref) - val);
5045 if (sRef_getLen(e1->sref) == val) { /* i.e. the character at posn val is \0 */
5046 sRef_setNotNullTerminatedState(ret->sref);
5047 sRef_resetLen (ret->sref);
5049 sRef_setNullTerminatedState(ret->sref);
5050 sRef_setLen (ret->sref, sRef_getLen(e1->sref) - val);
5055 /* Operator : - or -= */
5056 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5057 if (sRef_getSize(e1->sref) >= 0) {
5058 sRef_setSize (ret->sref, sRef_getSize(e1->sref) + val);
5059 sRef_setLen (ret->sref, sRef_getLen(e1->sref) + val);
5064 /* end modifications */
5066 sRef_setNullError (ret->sref);
5069 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5070 ** since is points to storage that should not be deallocated
5071 ** through this pointer.
5074 if (sRef_isOnly (ret->sref)
5075 || sRef_isFresh (ret->sref))
5077 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5082 else if ((!ctype_isRealPointer(tr1) && ctype_isRealInt (tr1))
5083 && (ctype_isRealPointer (tr2) && !exprNode_isNullValue (e2)))
5085 if (context_msgPointerArith ())
5089 message ("Pointer arithmetic (%t, %t): %s",
5090 te1, te2, exprNode_unparse (ret)),
5094 if (sRef_possiblyNull (e1->sref)
5095 && !usymtab_isGuarded (e1->sref))
5098 (FLG_NULLPOINTERARITH,
5099 message ("Pointer arithmetic involving possibly "
5100 "null pointer %s: %s",
5101 exprNode_unparse (e2),
5102 exprNode_unparse (ret)),
5106 ret->sref = sRef_copy (e2->sref);
5108 /* start modifications */
5109 /* added by Seejo on 4/16/2000 */
5111 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5114 if ((sRef_isPossiblyNullTerminated (e2->sref)) || (sRef_isNullTerminated(e2->sref))) {
5115 //if (sRef_isKnown (e->sref)) {
5116 //ret->sref = sRef_makeAddress (e->sref);
5119 int val = (int) multiVal_forceInt (e1->val);
5121 /* Operator : + or += */
5122 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5123 if (sRef_getSize(e2->sref) >= val) {/* Incrementing the pointer by
5124 val should not result in a
5125 size < 0 (size = 0 is ok !) */
5127 sRef_setSize (ret->sref, sRef_getSize(e2->sref) - val);
5129 if (sRef_getLen(e2->sref) == val) { /* i.e. the character at posn val is \0 */
5130 sRef_setNotNullTerminatedState(ret->sref);
5131 sRef_resetLen (ret->sref);
5133 sRef_setNullTerminatedState(ret->sref);
5134 sRef_setLen (ret->sref, sRef_getLen(e2->sref) - val);
5139 /* Operator : - or -= */
5140 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5141 if (sRef_getSize(e2->sref) >= 0) {
5142 sRef_setSize (ret->sref, sRef_getSize(e2->sref) + val);
5143 sRef_setLen (ret->sref, sRef_getLen(e2->sref) + val);
5148 /* end modifications */
5150 sRef_setNullError (ret->sref);
5153 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5154 ** since is points to storage that should not be deallocated
5155 ** through this pointer.
5158 if (sRef_isOnly (ret->sref)
5159 || sRef_isFresh (ret->sref)) {
5160 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5164 ret->sref = e2->sref;
5168 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5173 case LEFT_ASSIGN: /* Shifts: should be unsigned values */
5177 case TAMPERSAND: /* bitwise & */
5179 case TCIRC: /* ^ (XOR) */
5184 bool reported = FALSE;
5185 flagcode code = FLG_BITWISEOPS;
5187 if (opid == LEFT_OP || opid == LEFT_ASSIGN
5188 || opid == RIGHT_OP || opid == RIGHT_ASSIGN) {
5189 code = FLG_SHIFTSIGNED;
5192 if (!ctype_isUnsigned (tr1))
5194 if (exprNode_isNonNegative (e1)) {
5197 reported = optgenerror
5199 message ("Left operand of %s is not unsigned value (%t): %s",
5200 lltok_unparse (op), te1,
5201 exprNode_unparse (ret)),
5211 /* right need not be signed for shifts */
5212 if (code != FLG_SHIFTSIGNED
5213 && !ctype_isUnsigned (tr2))
5215 if (!exprNode_isNonNegative (e2)) {
5216 reported = optgenerror
5218 message ("Right operand of %s is not unsigned value (%t): %s",
5219 lltok_unparse (op), te2,
5220 exprNode_unparse (ret)),
5228 if (!checkIntegral (e1, e2, ret, op)) {
5229 te1 = ctype_unknown;
5233 DPRINTF (("Set: %s", ctype_unparse (te1)));
5236 ** tret is the widest type of te1 and te2
5239 tret = ctype_widest (te1, te2);
5244 if (checkIntegral (e1, e2, ret, op)) {
5247 tret = ctype_unknown;
5252 case TLT: /* comparisons */
5253 case TGT: /* numeric, numeric -> bool */
5254 if ((ctype_isReal (tr1) && !ctype_isInt (tr1))
5255 || (ctype_isReal (tr2) && !ctype_isInt (tr2)))
5258 bool fepsilon = FALSE;
5260 if (!ctype_isReal (rtype) || ctype_isInt (rtype))
5265 if (opid == TLT || opid == TGT)
5267 uentry ue1 = exprNode_getUentry (e1);
5268 uentry ue2 = exprNode_getUentry (e2);
5271 ** FLT_EPSILON, etc. really is a variable, not
5275 if (uentry_isVariable (ue1))
5277 cstring uname = uentry_rawName (ue1);
5279 if (cstring_equalLit (uname, "FLT_EPSILON")
5280 || cstring_equalLit (uname, "DBL_EPSILON")
5281 || cstring_equalLit (uname, "LDBL_EPSILON"))
5287 if (uentry_isVariable (ue2))
5289 cstring uname = uentry_rawName (ue2);
5291 if (cstring_equalLit (uname, "FLT_EPSILON")
5292 || cstring_equalLit (uname, "DBL_EPSILON")
5293 || cstring_equalLit (uname, "LDBL_EPSILON"))
5302 ; /* Don't complain. */
5308 message ("Dangerous comparison involving %s types: %s",
5309 ctype_unparse (rtype),
5310 exprNode_unparse (ret)),
5319 ** Types should match.
5322 if (!exprNode_matchTypes (e1, e2))
5324 hasError = gentypeerror
5326 message ("Operands of %s have incompatible types (%t, %t): %s",
5327 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5333 || (ctype_isForceRealNumeric (&tr1)
5334 && ctype_isForceRealNumeric (&tr2)) ||
5335 (ctype_isRealPointer (tr1) && ctype_isRealPointer (tr2)))
5341 if ((ctype_isRealNumeric (tr1) && ctype_isRealPointer (tr2)) ||
5342 (ctype_isRealPointer (tr1) && ctype_isRealNumeric (tr2)))
5346 message ("Comparison of pointer and numeric (%t, %t): %s",
5347 te1, te2, exprNode_unparse (ret)),
5352 (void) checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5357 /* EQ_OP should NOT be used with booleans (unless one is FALSE) */
5359 if ((opid == EQ_OP || opid == NE_OP) &&
5360 ctype_isDirectBool (tr1) && ctype_isDirectBool (tr2))
5363 ** is one a variable?
5366 if (uentry_isVariable (exprNode_getUentry (e1))
5367 || uentry_isVariable (exprNode_getUentry (e2)))
5370 ** comparisons with FALSE are okay
5373 if (exprNode_isFalseConstant (e1)
5374 || exprNode_isFalseConstant (e2))
5383 ("Use of %q with %s variables (risks inconsistency because "
5384 "of multiple true values): %s",
5385 cstring_makeLiteral ((opid == EQ_OP) ? "==" : "!="),
5386 context_printBoolName (), exprNode_unparse (ret)),
5393 case AND_OP: /* bool, bool -> bool */
5396 if (ctype_isForceRealBool (&tr1) && ctype_isForceRealBool (&tr2))
5402 if (context_maybeSet (FLG_BOOLOPS))
5404 if (!ctype_isRealBool (te1) && !ctype_isRealBool (te2))
5406 if (ctype_sameName (te1, te2))
5410 message ("Operands of %s are non-boolean (%t): %s",
5411 lltok_unparse (op), te1,
5412 exprNode_unparse (ret)),
5420 ("Operands of %s are non-booleans (%t, %t): %s",
5421 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5425 else if (!ctype_isRealBool (te1))
5429 message ("Left operand of %s is non-boolean (%t): %s",
5430 lltok_unparse (op), te1, exprNode_unparse (ret)),
5433 else if (!ctype_isRealBool (te2))
5437 message ("Right operand of %s is non-boolean (%t): %s",
5438 lltok_unparse (op), te2, exprNode_unparse (ret)),
5451 (cstring_makeLiteral
5452 ("There has been a problem in the parser. This is believed to result "
5453 "from a problem with bison v. 1.25. Please try rebuidling LCLint "
5454 "using the pre-compiled grammar files by commenting out the "
5455 "BISON= line in the top-level Makefile."));
5460 DPRINTF (("Return type: %s", ctype_unparse (tret)));
5463 exprNode_checkUse (ret, e1->sref, e1->loc);
5464 exprNode_mergeUSs (ret, e2);
5465 exprNode_checkUse (ret, e2->sref, e2->loc);
5471 exprNode_op (/*@only@*/ exprNode e1, /*@keep@*/ exprNode e2,
5472 /*@only@*/ lltok op)
5476 checkMacroParen (e1);
5477 checkMacroParen (e2);
5479 if (evaluationOrderUndefined (op) && context_maybeSet (FLG_EVALORDER))
5481 checkExpressionDefined (e1, e2, op);
5484 ret = exprNode_makeOp (e1, e2, op);
5489 void exprNode_checkAssignMod (exprNode e1, exprNode ret)
5492 ** This is somewhat bogus!
5494 ** Assigning to a nested observer in a non-observer datatype
5495 ** should not produce an error.
5498 sRef ref = exprNode_getSref (e1);
5500 DPRINTF (("Check assign mod: %s",
5501 sRef_unparseFull (ref)));
5503 if (sRef_isObserver (ref)
5504 || ((sRef_isFileStatic (ref) || sRef_isFileOrGlobalScope (ref))
5505 && ctype_isArray (ctype_realType (sRef_getType (ref)))))
5507 sRef base = sRef_getBase (ref);
5509 if (sRef_isValid (base) && sRef_isObserver (base))
5511 exprNode_checkModify (e1, ret);
5515 exprNode_checkModifyVal (e1, ret);
5520 exprNode_checkModify (e1, ret);
5525 exprNode_assign (/*@only@*/ exprNode e1,
5526 /*@only@*/ exprNode e2, /*@only@*/ lltok op)
5528 bool isalloc = FALSE;
5529 bool isjustalloc = FALSE;
5532 DPRINTF (("%s [%s] <- %s [%s]",
5533 exprNode_unparse (e1),
5534 ctype_unparse (e1->typ),
5535 exprNode_unparse (e2),
5536 ctype_unparse (e2->typ)));
5538 if (lltok_getTok (op) != TASSIGN)
5540 ret = exprNode_makeOp (e1, e2, op);
5544 ret = exprNode_createPartialCopy (e1);
5545 ret->kind = XPR_ASSIGN;
5546 ret->edata = exprData_makeOp (e1, e2, op);
5548 if (!exprNode_isError (e2))
5550 ret->sets = sRefSet_union (ret->sets, e2->sets);
5551 ret->msets = sRefSet_union (ret->msets, e2->msets);
5552 ret->uses = sRefSet_union (ret->uses, e2->uses);
5556 checkExpressionDefined (e1, e2, op);
5558 if (exprNode_isError (e1))
5560 if (!exprNode_isError (e2))
5562 ret->loc = fileloc_update (ret->loc, e2->loc);
5566 ret->loc = fileloc_update (ret->loc, g_currentloc);
5570 if (!exprNode_isError (e2))
5572 checkMacroParen (e2);
5575 if (exprNode_isDefined (e1))
5577 if (sRef_isMacroParamRef (e1->sref))
5579 if (context_inIterDef ())
5581 uentry ue = sRef_getUentry (e1->sref);
5583 if (uentry_isYield (ue))
5589 if (fileloc_isDefined (e1->loc))
5593 message ("Assignment to non-yield iter parameter: %q",
5594 sRef_unparse (e1->sref)),
5601 message ("Assignment to non-yield iter parameter: %q",
5602 sRef_unparse (e1->sref)),
5609 if (fileloc_isDefined (e1->loc))
5613 message ("Assignment to macro parameter: %q",
5614 sRef_unparse (e1->sref)),
5621 message ("Assignment to macro parameter: %q",
5622 sRef_unparse (e1->sref)),
5629 exprNode_checkAssignMod (e1, ret);
5632 if (exprNode_isDefined (e2))
5634 if (lltok_getTok (op) == TASSIGN)
5636 ctype te1 = exprNode_getType (e1);
5637 ctype te2 = exprNode_getType (e2);
5639 if (!ctype_forceMatch (te1, te2))
5641 if (exprNode_matchLiteral (te1, e2))
5649 message ("Assignment of %t to %t: %s %s %s",
5650 te2, te1, exprNode_unparse (e1),
5652 exprNode_unparse (e2)),
5658 exprNode_mergeUSs (ret, e2);
5659 exprNode_checkUse (ret, e2->sref, e2->loc);
5661 doAssign (e1, e2, FALSE);
5662 ret->sref = e1->sref;
5666 if (exprNode_isDefined (e2))
5668 exprNode_mergeUSs (ret, e2);
5669 exprNode_checkUse (ret, e2->sref, e2->loc);
5673 if (sRef_isPointer (e1->sref) && !sRef_isMacroParamRef (e1->sref))
5675 exprNode_checkUse (ret, sRef_getBase (e1->sref), e1->loc);
5678 isjustalloc = sRef_isJustAllocated (e1->sref);
5679 isalloc = sRef_isAllocated (e1->sref);
5681 if (sRef_isField (e1->sref))
5683 sRef root = sRef_getRootBase (sRef_getBase (e1->sref));
5685 if (!sRef_isAllocated (root) && !sRef_isMacroParamRef (root))
5687 exprNode_checkUse (ret, root, e1->loc);
5693 ** be careful! this defines e1->sref.
5696 if (!sRef_isMacroParamRef (e1->sref))
5698 exprNode_checkSet (ret, e1->sref);
5703 sRef_setAllocatedComplete (e1->sref, exprNode_isDefined (e2)
5704 ? e2->loc : e1->loc);
5710 sRef_setAllocatedShallowComplete (e1->sref, exprNode_loc (e2));
5719 exprNode_cond (/*@keep@*/ exprNode pred, /*@keep@*/ exprNode ifclause,
5720 /*@keep@*/ exprNode elseclause)
5724 if (!exprNode_isError (pred))
5726 ret = exprNode_createPartialCopy (pred);
5727 checkMacroParen (pred);
5728 exprNode_checkPred (cstring_makeLiteralTemp ("conditional"), pred);
5730 if (!exprNode_isError (ifclause))
5732 checkMacroParen (ifclause); /* update macro counts! */
5734 if (!exprNode_isError (elseclause))
5736 checkMacroParen (elseclause);
5738 if (!exprNode_matchTypes (ifclause, elseclause))
5741 (exprNode_getType (ifclause),
5743 exprNode_getType (elseclause),
5745 message ("Conditional clauses are not of same type: "
5747 exprNode_unparse (ifclause),
5748 exprNode_getType (ifclause),
5749 exprNode_unparse (elseclause),
5750 exprNode_getType (elseclause)),
5753 ret->sref = sRef_undefined;
5754 ret->typ = ctype_unknown;
5759 /* for now...should merge the states */
5760 ret->sref = ifclause->sref;
5761 ret->typ = ifclause->typ;
5763 if (exprNode_isNullValue (ifclause))
5765 ret->typ = elseclause->typ;
5769 exprNode_checkUse (ret, pred->sref, pred->loc);
5770 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5771 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5773 exprNode_mergeCondUSs (ret, ifclause, elseclause);
5778 ret->typ = ifclause->typ;
5780 exprNode_checkUse (pred, pred->sref, pred->loc);
5781 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5783 exprNode_mergeCondUSs (ret, ifclause, exprNode_undefined);
5788 if (!exprNode_isError (elseclause))
5790 ret->typ = elseclause->typ;
5792 exprNode_checkUse (pred, pred->sref, pred->loc);
5793 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5795 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
5799 else /* pred is error */
5801 if (!exprNode_isError (ifclause))
5803 ret = exprNode_createSemiCopy (ifclause);
5805 checkMacroParen (ifclause); /* update macro counts! */
5807 if (!exprNode_isError (elseclause))
5809 checkMacroParen (elseclause);
5811 ret->typ = ifclause->typ;
5813 if (!ctype_forceMatch (ifclause->typ, elseclause->typ))
5816 (exprNode_getType (ifclause),
5818 exprNode_getType (elseclause),
5820 message ("Conditional clauses are not of same type: "
5822 exprNode_unparse (ifclause),
5823 exprNode_getType (ifclause),
5824 exprNode_unparse (elseclause),
5825 exprNode_getType (elseclause)),
5828 ret->typ = ctype_unknown;
5832 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5833 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5835 exprNode_mergeCondUSs (ret, ifclause, elseclause);
5838 else if (!exprNode_isError (elseclause)) /* pred, if errors */
5840 ret = exprNode_createSemiCopy (ifclause);
5842 ret->typ = elseclause->typ;
5843 checkMacroParen (elseclause);
5845 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5846 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
5848 else /* all errors! */
5850 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
5854 ret->kind = XPR_COND;
5855 ret->edata = exprData_makeCond (pred, ifclause, elseclause);
5857 if (exprNode_isDefined (ifclause) && exprNode_isDefined (elseclause))
5859 exprNode_combineControl (ret, ifclause, elseclause);
5866 exprNode_vaArg (/*@only@*/ lltok tok, /*@only@*/ exprNode arg, /*@only@*/ qtype qt)
5868 ctype totype = qtype_getType (qt);
5870 exprNode_createPartialLocCopy (arg, fileloc_copy (lltok_getLoc (tok)));
5874 ** check use of va_arg : <valist>, type -> type
5877 if (exprNode_isError (arg))
5882 targ = exprNode_getType (arg);
5885 ** arg should have be a pointer
5888 if (!ctype_isUA (targ) ||
5889 (!usymId_equal (ctype_typeId (targ),
5890 usymtab_getTypeId (cstring_makeLiteralTemp ("va_list")))))
5894 message ("First argument to va_arg is not a va_list (type %t): %s",
5895 targ, exprNode_unparse (arg)),
5899 exprNode_checkSet (ret, arg->sref);
5903 ** return type is totype
5907 ret->kind = XPR_VAARG;
5908 ret->edata = exprData_makeCast (tok, arg, qt);
5913 exprNode exprNode_labelMarker (/*@only@*/ cstring label)
5915 exprNode ret = exprNode_createPlain (ctype_undefined);
5916 ret->kind = XPR_LABEL;
5917 ret->edata = exprData_makeLiteral (label);
5918 ret->isJumpPoint = TRUE;
5920 return (ret); /* for now, ignore label */
5923 exprNode exprNode_notReached (/*@returned@*/ exprNode stmt)
5925 if (exprNode_isDefined (stmt))
5927 stmt->isJumpPoint = TRUE;
5929 /* This prevent stray no return path errors, etc. */
5930 stmt->exitCode = XK_MUSTEXIT;
5936 bool exprNode_isDefaultMarker (exprNode e)
5938 if (exprNode_isDefined (e))
5940 return (e->kind == XPR_DEFAULT || e->kind == XPR_FTDEFAULT);
5946 bool exprNode_isCaseMarker (exprNode e)
5948 if (exprNode_isDefined (e))
5950 return (e->kind == XPR_FTCASE || e->kind == XPR_CASE);
5956 bool exprNode_isLabelMarker (exprNode e)
5958 if (exprNode_isDefined (e))
5960 return (e->kind == XPR_LABEL);
5966 exprNode exprNode_caseMarker (/*@only@*/ exprNode test, bool fallThrough)
5968 exprNode ret = exprNode_createPartialCopy (test);
5970 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
5972 if (exprNode_isError (test)) {
5976 exprNode_checkUse (ret, test->sref, test->loc);
5978 usymtab_setExitCode (ret->exitCode);
5982 usymtab_setMustBreak ();
5985 ret->edata = exprData_makeSingle (test);
5986 ret->isJumpPoint = TRUE;
5992 exprNode exprNode_caseStatement (/*@only@*/ exprNode test, /*@only@*/ exprNode stmt, bool fallThrough)
5994 exprNode ret = exprNode_createPartialCopy (test);
5996 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
5997 ret->edata = exprData_makePair (test, stmt);
5998 ret->isJumpPoint = TRUE;
6000 if (exprNode_isError (test))
6005 exprNode_checkUse (ret, test->sref, test->loc);
6007 if (exprNode_isError (stmt))
6012 exprNode_mergeUSs (ret, stmt);
6014 ret->exitCode = stmt->exitCode;
6015 ret->mustBreak = stmt->mustBreak;
6016 ret->canBreak = stmt->canBreak;
6018 usymtab_setExitCode (ret->exitCode);
6022 usymtab_setMustBreak ();
6029 /*@notnull@*/ /*@only@*/ exprNode
6030 exprNode_defaultMarker (/*@only@*/ lltok def, bool fallThrough)
6032 exprNode ret = exprNode_createTok (def);
6034 ret->isJumpPoint = TRUE;
6035 ret->kind = fallThrough ? XPR_FTDEFAULT : XPR_DEFAULT;
6040 exprNode_mayEscape (exprNode e)
6042 if (exprNode_isDefined (e))
6044 return exitkind_couldEscape (e->exitCode);
6050 exprNode_mustBreak (exprNode e)
6052 if (exprNode_isDefined (e))
6054 return e->mustBreak;
6060 exprNode_mustEscape (exprNode e)
6062 if (exprNode_isDefined (e))
6064 return exitkind_mustEscape (e->exitCode) || exprNode_mustBreak (e);
6071 exprNode_errorEscape (exprNode e)
6073 if (exprNode_isDefined (e))
6075 return exitkind_isError (e->exitCode);
6081 exprNode exprNode_concat (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
6083 exprNode ret = exprNode_createPartialCopy (e1);
6085 DPRINTF (("Concat: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6087 ret->edata = exprData_makePair (e1, e2);
6088 ret->kind = XPR_STMTLIST;
6090 if (exprNode_isDefined (e1))
6092 ret->isJumpPoint = e1->isJumpPoint;
6093 ret->canBreak = e1->canBreak;
6097 if (exprNode_isDefined (e2))
6099 ret->loc = fileloc_update (ret->loc, e2->loc);
6103 if (exprNode_isDefined (e2))
6105 ret->exitCode = e2->exitCode;
6106 ret->mustBreak = e2->mustBreak;
6107 if (e2->canBreak) ret->canBreak = TRUE;
6111 ** if e1 must return, then e2 is unreachable!
6114 if (exprNode_isDefined (e1) && exprNode_isDefined (e2))
6116 if ((exprNode_mustEscape (e1) || exprNode_mustBreak (e1))
6117 && !(e2->isJumpPoint))
6119 if (context_getFlag (FLG_UNREACHABLE))
6123 if (e2->kind == XPR_STMT)
6125 nr = exprData_getUopNode (e2->edata);
6128 if ((nr->kind == XPR_TOK
6129 && lltok_isSemi (exprData_getTok (nr->edata))))
6131 /* okay to have unreachable ";" */
6132 ret->exitCode = XK_MUSTEXIT;
6133 ret->canBreak = TRUE;
6137 if (optgenerror (FLG_UNREACHABLE,
6138 message ("Unreachable code: %s",
6139 exprNode_unparseFirst (nr)),
6142 ret->isJumpPoint = TRUE;
6143 ret->mustBreak = FALSE;
6144 ret->exitCode = XK_ERROR;
6145 DPRINTF (("Jump point: %s", exprNode_unparse (ret)));
6149 ret->exitCode = XK_MUSTEXIT;
6150 ret->canBreak = TRUE;
6158 if ((e2->kind == XPR_CASE || e2->kind == XPR_DEFAULT))
6161 ** We want a warning anytime we have:
6163 ** yyy; <<<- no break or return
6167 exprNode lastStmt = exprNode_lastStatement (e1);
6169 if (exprNode_isDefined (lastStmt)
6170 && !exprNode_mustEscape (lastStmt)
6171 && !exprNode_mustBreak (lastStmt)
6172 && !exprNode_isCaseMarker (lastStmt)
6173 && !exprNode_isDefaultMarker (lastStmt)
6174 && !exprNode_isLabelMarker (lastStmt))
6176 voptgenerror (FLG_CASEBREAK,
6178 ("Fall through case (no preceeding break)"),
6185 exprNode_mergeUSs (ret, e2);
6187 usymtab_setExitCode (ret->exitCode);
6191 usymtab_setMustBreak ();
6197 exprNode exprNode_createTok (/*@only@*/ lltok t)
6199 exprNode ret; /*@i23 if on same line, bad things happen...!@*/
6200 ret = exprNode_create (ctype_unknown);
6201 ret->kind = XPR_TOK;
6202 ret->edata = exprData_makeTok (t);
6206 exprNode exprNode_statement (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6208 if (!exprNode_isError (e))
6210 exprNode_checkStatement(e);
6213 return (exprNode_statementError (e, t));
6216 static exprNode exprNode_statementError (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6218 exprNode ret = exprNode_createPartialCopy (e);
6220 if (!exprNode_isError (e))
6222 if (e->kind != XPR_ASSIGN)
6224 exprNode_checkUse (ret, e->sref, e->loc);
6227 ret->exitCode = e->exitCode;
6228 ret->canBreak = e->canBreak;
6229 ret->mustBreak = e->mustBreak;
6232 ret->edata = exprData_makeUop (e, t);
6233 ret->kind = XPR_STMT;
6238 exprNode exprNode_checkExpr (/*@returned@*/ exprNode e)
6240 if (!exprNode_isError (e))
6242 if (e->kind != XPR_ASSIGN)
6244 exprNode_checkUse (e, e->sref, e->loc);
6251 void exprNode_produceGuards (exprNode pred)
6253 if (!exprNode_isError (pred))
6255 if (ctype_isRealPointer (pred->typ))
6257 pred->guards = guardSet_addTrueGuard (pred->guards, pred->sref);
6260 exprNode_checkUse (pred, pred->sref, pred->loc);
6261 exprNode_resetSref (pred);
6265 exprNode exprNode_makeBlock (/*@only@*/ exprNode e)
6267 exprNode ret = exprNode_createPartialCopy (e);
6269 if (!exprNode_isError (e))
6271 ret->exitCode = e->exitCode;
6272 ret->canBreak = e->canBreak;
6273 ret->mustBreak = e->mustBreak;
6276 ret->edata = exprData_makeSingle (e);
6277 ret->kind = XPR_BLOCK;
6281 bool exprNode_isBlock (exprNode e)
6283 return (exprNode_isDefined (e)
6284 && ((e)->kind == XPR_BLOCK));
6287 bool exprNode_isAssign (exprNode e)
6289 if (exprNode_isDefined (e))
6291 return (e->kind == XPR_ASSIGN);
6297 bool exprNode_isEmptyStatement (exprNode e)
6299 return (exprNode_isDefined (e)
6300 && (e->kind == XPR_TOK)
6301 && (lltok_isSemi (exprData_getTok (e->edata))));
6304 exprNode exprNode_if (/*@only@*/ exprNode pred, /*@only@*/ exprNode tclause)
6307 bool emptyErr = FALSE;
6309 if (context_maybeSet (FLG_IFEMPTY))
6311 if (exprNode_isEmptyStatement (tclause))
6313 emptyErr = optgenerror (FLG_IFEMPTY,
6315 ("Body of if statement is empty"),
6316 exprNode_loc (tclause));
6320 if (!emptyErr && context_maybeSet (FLG_IFBLOCK))
6322 if (exprNode_isDefined (tclause)
6323 && !exprNode_isBlock (tclause))
6325 voptgenerror (FLG_IFBLOCK,
6327 ("Body of if statement is not a block: %s",
6328 exprNode_unparse (tclause)),
6329 exprNode_loc (tclause));
6333 if (exprNode_isError (pred))
6335 if (exprNode_isError (tclause))
6337 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6341 ret = exprNode_createPartialCopy (tclause);
6346 if (exprNode_mustEscape (pred))
6350 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6351 exprNode_loc (pred));
6354 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6355 exprNode_checkUse (pred, pred->sref, pred->loc);
6357 if (!exprNode_isError (tclause))
6359 exprNode_mergeCondUSs (pred, tclause, exprNode_undefined);
6362 ret = exprNode_createPartialCopy (pred);
6366 ret->edata = exprData_makePair (pred, tclause);
6368 ret->exitCode = XK_UNKNOWN;
6370 if (exprNode_isDefined (tclause))
6372 ret->exitCode = exitkind_makeConditional (tclause->exitCode);
6373 ret->canBreak = tclause->canBreak;
6374 ret->sets = sRefSet_union (ret->sets, tclause->sets);
6375 ret->msets = sRefSet_union (ret->msets, tclause->msets);
6376 ret->uses = sRefSet_union (ret->uses, tclause->uses);
6379 ret->mustBreak = FALSE;
6384 exprNode exprNode_ifelse (/*@only@*/ exprNode pred,
6385 /*@only@*/ exprNode tclause,
6386 /*@only@*/ exprNode eclause)
6389 bool tEmptyErr = FALSE;
6390 bool eEmptyErr = FALSE;
6392 if (context_maybeSet (FLG_IFEMPTY))
6394 if (exprNode_isEmptyStatement (tclause))
6396 tEmptyErr = optgenerror
6399 ("Body of if clause of if statement is empty"),
6400 exprNode_loc (tclause));
6403 if (exprNode_isEmptyStatement (eclause))
6405 eEmptyErr = optgenerror
6408 ("Body of else clause of if statement is empty"),
6409 exprNode_loc (eclause));
6413 if (context_maybeSet (FLG_IFBLOCK))
6416 && exprNode_isDefined (tclause)
6417 && !exprNode_isBlock (tclause))
6419 voptgenerror (FLG_IFBLOCK,
6421 ("Body of if clause of if statement is not a block: %s",
6422 exprNode_unparse (tclause)),
6423 exprNode_loc (tclause));
6427 && exprNode_isDefined (eclause)
6428 && !exprNode_isBlock (eclause)
6429 && !(eclause->kind == XPR_IF)
6430 && !(eclause->kind == XPR_IFELSE))
6435 ("Body of else clause of if statement is not a block: %s",
6436 exprNode_unparse (eclause)),
6437 exprNode_loc (eclause));
6441 if (context_maybeSet (FLG_ELSEIFCOMPLETE))
6443 if (exprNode_isDefined (eclause)
6444 && (eclause->kind == XPR_IF))
6446 voptgenerror (FLG_ELSEIFCOMPLETE,
6447 message ("Incomplete else if logic (no final else): %s",
6448 exprNode_unparse (eclause)),
6449 exprNode_loc (eclause));
6453 if (exprNode_isError (pred))
6455 if (exprNode_isError (tclause))
6457 if (exprNode_isError (eclause))
6459 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6463 ret = exprNode_createPartialCopy (eclause);
6468 ret = exprNode_createPartialCopy (tclause);
6471 else /* pred is okay */
6473 ret = exprNode_createPartialCopy (pred);
6475 if (exprNode_mustEscape (pred))
6479 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6480 exprNode_loc (pred));
6483 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6484 exprNode_checkUse (ret, pred->sref, pred->loc);
6486 exprNode_mergeCondUSs (ret, tclause, eclause);
6489 ret->kind = XPR_IFELSE;
6490 ret->edata = exprData_makeCond (pred, tclause, eclause);
6492 if (exprNode_isDefined (tclause) && exprNode_isDefined (eclause))
6494 exprNode_combineControl (ret, tclause, eclause);
6495 ret->loc = fileloc_update (ret->loc, eclause->loc);
6502 ** *allpaths <- TRUE iff all executions paths must go through the switch
6506 checkSwitchExpr (exprNode test, /*@dependent@*/ exprNode e, /*@out@*/ bool *allpaths)
6508 exprNodeSList el = exprNode_flatten (e);
6509 bool mustReturn = TRUE; /* find a branch that doesn't */
6510 bool thisReturn = FALSE;
6511 bool hasDefault = FALSE;
6512 bool hasAllMembers = FALSE;
6513 bool inSwitch = FALSE;
6514 bool isEnumSwitch = FALSE;
6515 bool canBreak = FALSE;
6516 bool fallThrough = FALSE;
6517 ctype ct = ctype_unknown;
6518 enumNameSList usedEnums;
6521 if (exprNode_isDefined (test))
6526 ttype = ctype_realType (ct);
6528 if (ctype_isEnum (ttype))
6530 isEnumSwitch = TRUE;
6531 enums = ctype_elist (ttype);
6532 usedEnums = enumNameSList_new ();
6536 exprNodeSList_elements (el, current)
6538 if (exprNode_isDefined (current))
6540 switch (current->kind)
6548 message ("Duplicate default cases in switch"),
6549 exprNode_loc (current));
6554 if (current->kind == XPR_DEFAULT || current->kind == XPR_FTDEFAULT)
6562 exprNode st = exprData_getSingle (current->edata);
6563 uentry ue = exprNode_getUentry (st);
6565 if (uentry_isValid (ue))
6567 cstring cname = uentry_rawName (ue);
6569 if (enumNameList_member (/*@-usedef@*/enums/*@=usedef@*/, cname))
6571 if (enumNameSList_member
6572 (/*@-usedef@*/usedEnums/*@=usedef@*/, cname))
6576 message ("Duplicate case in switch: %s",
6582 enumNameSList_addh (usedEnums, cname);
6589 message ("Case in switch not %s member: %s",
6590 ctype_unparse (ct), cname),
6597 if (inSwitch && !fallThrough)
6599 if (!thisReturn || canBreak)
6609 /*@switchbreak@*/ break;
6611 thisReturn = thisReturn || exprNode_mustEscape (current);
6612 canBreak = canBreak || current->canBreak;
6613 if (canBreak) fallThrough = FALSE;
6616 } end_exprNodeSList_elements;
6618 if (inSwitch) /* check the last one! */
6620 if (!thisReturn || canBreak)
6629 && (enumNameSList_size (/*@-usedef@*/usedEnums/*@=usedef@*/) !=
6630 enumNameList_size (/*@-usedef@*/enums/*@=usedef@*/)))
6632 enumNameSList unused = enumNameSList_subtract (enums, usedEnums);
6634 voptgenerror (FLG_MISSCASE,
6635 message ("Missing case%s in switch: %q",
6636 cstring_makeLiteralTemp
6637 ((enumNameSList_size (unused) > 1) ? "s" : ""),
6638 enumNameSList_unparse (unused)),
6641 enumNameSList_free (unused);
6645 hasAllMembers = TRUE;
6649 enumNameSList_free (usedEnums);
6653 *allpaths = hasDefault;
6656 exprNodeSList_free (el);
6657 return ((hasDefault || hasAllMembers) && mustReturn);
6660 exprNode exprNode_switch (/*@only@*/ exprNode e, /*@only@*/ exprNode s)
6662 exprNode ret = exprNode_createPartialCopy (e);
6665 DPRINTF (("Switch: %s", exprNode_unparse (s)));
6667 ret->kind = XPR_SWITCH;
6668 ret->edata = exprData_makePair (e, s);
6670 if (!exprNode_isError (s))
6672 exprNode fs = exprNode_firstStatement (s);
6673 ret->loc = fileloc_update (ret->loc, s->loc);
6675 if (exprNode_isUndefined (fs)
6676 || exprNode_isCaseMarker (fs) || exprNode_isLabelMarker (fs)
6677 || exprNode_isDefaultMarker (fs)) {
6680 voptgenerror (FLG_FIRSTCASE,
6682 ("Statement after switch is not a case: %s", exprNode_unparse (fs)),
6687 if (!exprNode_isError (e))
6689 if (checkSwitchExpr (e, s, &allpaths))
6691 ret->exitCode = XK_MUSTRETURN;
6695 ret->exitCode = e->exitCode;
6698 ret->canBreak = e->canBreak;
6699 ret->mustBreak = e->mustBreak;
6703 ** exprNode.c:3883,32: Variable allpaths used before definition
6710 DPRINTF (("Context exit switch!"));
6711 context_exitSwitch (ret, allpaths);
6712 DPRINTF (("Context exit switch done!"));
6717 static void checkInfiniteLoop (/*@notnull@*/ exprNode test,
6718 /*@notnull@*/ exprNode body)
6720 sRefSet tuses = test->uses;
6722 if (!sRefSet_isEmpty (test->uses))
6724 sRefSet sets = sRefSet_newCopy (body->sets);
6725 bool hasError = TRUE;
6726 bool innerState = FALSE;
6727 sRefSet tuncon = sRefSet_undefined;
6729 sets = sRefSet_union (sets, test->sets);
6730 sets = sRefSet_union (sets, body->msets);
6731 sets = sRefSet_union (sets, test->msets);
6733 sRefSet_allElements (tuses, el)
6735 if (sRef_isUnconstrained (el))
6737 tuncon = sRefSet_insert (tuncon, el);
6741 if (sRefSet_member (sets, el))
6748 if (sRef_isInternalState (el)
6749 || sRef_isFileStatic (sRef_getRootBase (el)))
6753 } end_sRefSet_allElements ;
6757 sRefSet suncon = sRefSet_undefined;
6758 bool sinner = FALSE;
6760 sRefSet_allElements (sets, el)
6762 if (sRef_isUnconstrained (el))
6764 suncon = sRefSet_insert (suncon, el);
6766 else if (sRef_isInternalState (el))
6774 } end_sRefSet_allElements ;
6776 if (sinner && innerState)
6780 else if (sRefSet_isEmpty (tuncon)
6781 && sRefSet_isEmpty (suncon))
6786 ("Suspected infinite loop. No value used in loop test (%q) "
6787 "is modified by test or loop body.",
6788 sRefSet_unparsePlain (tuses)),
6793 if (sRefSet_isEmpty (tuncon))
6797 message ("Suspected infinite loop. No condition values "
6798 "modified. Modification possible through "
6799 "unconstrained calls: %q",
6800 sRefSet_unparsePlain (suncon)),
6807 message ("Suspected infinite loop. No condition values "
6808 "modified. Possible undetected dependency through "
6809 "unconstrained calls in loop test: %q",
6810 sRefSet_unparsePlain (tuncon)),
6816 sRefSet_free (sets);
6820 exprNode exprNode_while (/*@keep@*/ exprNode t, /*@keep@*/ exprNode b)
6823 bool emptyErr = FALSE;
6825 if (context_maybeSet (FLG_WHILEEMPTY))
6827 if (exprNode_isEmptyStatement (b))
6829 emptyErr = optgenerror
6832 ("Body of while statement is empty"),
6837 if (!emptyErr && context_maybeSet (FLG_WHILEBLOCK))
6839 if (exprNode_isDefined (b)
6840 && !exprNode_isBlock (b))
6842 if (context_inIterDef ()
6843 && (b->kind == XPR_STMTLIST
6844 || b->kind == XPR_TOK))
6850 voptgenerror (FLG_WHILEBLOCK,
6852 ("Body of while statement is not a block: %s",
6853 exprNode_unparse (b)),
6859 if (exprNode_isError (t))
6861 if (exprNode_isError (b))
6863 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6867 ret = exprNode_createPartialCopy (b);
6874 ret = exprNode_createPartialCopy (t);
6876 llassert (t->kind == XPR_WHILEPRED);
6878 test = exprData_getSingle (t->edata);
6880 if (!exprNode_isError (b) && exprNode_isDefined (test))
6882 if (context_maybeSet (FLG_INFLOOPS)
6883 || context_maybeSet (FLG_INFLOOPSUNCON))
6886 ** check that some variable in the predicate is set by the body
6887 ** if the predicate uses any variables
6890 checkInfiniteLoop (test, b);
6893 exprNode_mergeUSs (ret, b);
6895 if (exprNode_isDefined (b))
6897 ret->exitCode = exitkind_makeConditional (b->exitCode);
6902 ret->edata = exprData_makePair (t, b);
6903 ret->kind = XPR_WHILE;
6905 if (exprNode_isDefined (t) && exprNode_mustEscape (t))
6909 message ("Predicate always exits: %s", exprNode_unparse (t)),
6913 ret->exitCode = XK_NEVERESCAPE;
6916 ** If loop is infinite, and there is no break inside,
6917 ** exit code is never reach.
6920 if (exprNode_knownIntValue (t))
6922 if (!exprNode_isZero (t))
6924 if (exprNode_isDefined (b))
6928 /* Really, it means never reached. */
6929 ret->exitCode = XK_MUSTEXIT;
6939 ret->canBreak = FALSE;
6940 ret->mustBreak = FALSE;
6946 ** do { b } while (t);
6948 ** note: body passed as first argument
6951 exprNode exprNode_doWhile (/*@only@*/ exprNode b, /*@only@*/ exprNode t)
6955 if (exprNode_isError (t))
6957 if (exprNode_isError (b))
6959 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6963 ret = exprNode_createPartialCopy (b);
6965 ret->exitCode = exitkind_makeConditional (b->exitCode);
6966 exprNode_checkUse (ret, b->sref, b->loc);
6967 ret->exitCode = b->exitCode;
6968 ret->canBreak = b->canBreak;
6969 ret->mustBreak = b->mustBreak;
6974 ret = exprNode_createPartialCopy (t);
6975 exprNode_checkPred (cstring_makeLiteralTemp ("while"), t);
6977 if (!exprNode_isError (b))
6980 ** forgot the copy's --- why wasn't this detected??
6983 ret->sets = sRefSet_copyInto (ret->sets, b->sets);
6984 ret->msets = sRefSet_copyInto (ret->msets, b->msets);
6985 ret->uses = sRefSet_copyInto (ret->uses, b->uses);
6987 /* left this out --- causes and aliasing bug (infinite loop)
6988 should be detected?? */
6990 exprNode_checkUse (ret, b->sref, b->loc);
6991 exprNode_mergeUSs (ret, t);
6992 exprNode_checkUse (ret, t->sref, t->loc);
6994 ret->exitCode = b->exitCode;
6995 ret->canBreak = b->canBreak;
6996 ret->mustBreak = b->mustBreak;
7000 context_exitDoWhileClause (t);
7002 ret->kind = XPR_DOWHILE;
7003 ret->edata = exprData_makePair (t, b);
7007 exprNode exprNode_for (/*@keep@*/ exprNode inc, /*@keep@*/ exprNode body)
7010 bool emptyErr = FALSE;
7012 if (context_maybeSet (FLG_FOREMPTY))
7014 if (exprNode_isEmptyStatement (body))
7016 emptyErr = optgenerror
7019 ("Body of for statement is empty"),
7020 exprNode_loc (body));
7024 if (!emptyErr && context_maybeSet (FLG_FORBLOCK))
7026 if (exprNode_isDefined (body)
7027 && !exprNode_isBlock (body))
7029 if (context_inIterDef ()
7030 && (body->kind == XPR_STMTLIST
7031 || body->kind == XPR_TOK))
7037 voptgenerror (FLG_FORBLOCK,
7039 ("Body of for statement is not a block: %s",
7040 exprNode_unparse (body)),
7041 exprNode_loc (body));
7047 ** for ud purposes: (alreadly) init -> test -> (now) LOOP: body + inc + test
7050 if (exprNode_isError (body))
7052 ret = exprNode_createPartialCopy (inc);
7056 ret = exprNode_createPartialCopy (body);
7058 ret->exitCode = exitkind_makeConditional (body->exitCode);
7060 exprNode_mergeUSs (inc, body);
7062 if (exprNode_isDefined (inc))
7066 context_setMessageAnnote (cstring_makeLiteral ("in post loop increment"));
7069 tmp = exprNode_effect (exprData_getTripleInc (inc->edata));
7070 exprNode_freeShallow (tmp);
7072 context_clearMessageAnnote ();
7073 context_setMessageAnnote (cstring_makeLiteral ("in post loop test"));
7075 tmp = exprNode_effect (exprData_getTripleTest (inc->edata));
7076 exprNode_freeShallow (tmp);
7078 context_clearMessageAnnote ();
7080 ret->uses = sRefSet_copyInto (ret->uses, inc->uses);
7081 ret->sets = sRefSet_copyInto (ret->sets, inc->sets);
7082 ret->msets = sRefSet_copyInto (ret->msets, inc->msets);
7086 ret->kind = XPR_FOR;
7087 ret->edata = exprData_makePair (inc, body);
7089 if (exprNode_isDefined (inc)) {
7090 exprNode test = exprData_getTripleTest (inc->edata);
7092 if (exprNode_isUndefined (test)) {
7093 if (exprNode_isDefined (body)) {
7094 if (!body->canBreak) {
7095 /* Really, it means never reached. */
7096 ret->exitCode = XK_MUSTEXIT;
7106 ** for (init; test; inc)
7109 ** while (test) { body; inc; }
7111 ** Now: check use of init (may set vars for test)
7112 ** check use of test
7116 /*@observer@*/ guardSet exprNode_getForGuards (exprNode pred)
7120 if (exprNode_isError (pred)) return guardSet_undefined;
7122 llassert (pred->kind == XPR_FORPRED);
7124 test = exprData_getTripleTest (pred->edata);
7126 if (!exprNode_isError (test))
7128 return (test->guards);
7131 return guardSet_undefined;
7134 exprNode exprNode_whilePred (/*@only@*/ exprNode test)
7136 exprNode ret = exprNode_createSemiCopy (test);
7138 if (exprNode_isDefined (test))
7140 exprNode_copySets (ret, test);
7141 exprNode_checkPred (cstring_makeLiteralTemp ("while"), test);
7142 exprNode_checkUse (ret, test->sref, test->loc);
7144 exprNode_produceGuards (test);
7146 ret->guards = guardSet_copy (test->guards);
7149 ret->edata = exprData_makeSingle (test);
7150 ret->kind = XPR_WHILEPRED;
7154 exprNode exprNode_forPred (/*@only@*/ exprNode init, /*@only@*/ exprNode test,
7155 /*@only@*/ exprNode inc)
7160 ** for ud purposes: init -> test -> LOOP: [ body, inc ]
7163 exprNode_checkPred (cstring_makeLiteralTemp ("for"), test);
7165 if (!exprNode_isError (inc))
7167 ret = exprNode_createPartialCopy (inc);
7171 if (!exprNode_isError (init))
7173 ret = exprNode_createPartialCopy (init);
7175 else if (!exprNode_isError (test))
7177 ret = exprNode_createPartialCopy (test);
7181 ret = exprNode_createUnknown ();
7185 exprNode_mergeUSs (ret, init);
7187 if (exprNode_isDefined (init))
7189 exprNode_checkUse (ret, init->sref, init->loc);
7192 exprNode_mergeUSs (ret, test);
7194 if (exprNode_isDefined (test))
7196 exprNode_checkUse (ret, test->sref, test->loc);
7199 ret->kind = XPR_FORPRED;
7200 ret->edata = exprData_makeFor (init, test, inc);
7204 /*@notnull@*/ /*@only@*/ exprNode exprNode_goto (/*@only@*/ cstring label)
7206 exprNode ret = exprNode_createUnknown ();
7208 if (context_inMacro ())
7210 voptgenerror (FLG_MACROSTMT,
7211 message ("Macro %s uses goto (not functional)",
7212 context_inFunctionName ()),
7216 ret->kind = XPR_GOTO;
7217 ret->edata = exprData_makeLiteral (label);
7218 ret->mustBreak = TRUE;
7219 ret->exitCode = XK_GOTO;
7220 ret->canBreak = TRUE;
7224 exprNode exprNode_continue (/*@only@*/ lltok l, int qcontinue)
7226 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7228 ret->kind = XPR_CONTINUE;
7229 ret->edata = exprData_makeTok (l);
7230 ret->canBreak = TRUE;
7231 ret->mustBreak = TRUE;
7233 if (qcontinue == QSAFEBREAK)
7237 else if (qcontinue == QINNERCONTINUE)
7239 if (!context_inDeepLoop ())
7242 (FLG_LOOPLOOPCONTINUE,
7243 cstring_makeLiteral ("Continue statement marked with innercontinue "
7244 "is not inside a nested loop"),
7245 exprNode_loc (ret));
7248 else if (qcontinue == BADTOK)
7250 if (context_inDeepLoop ())
7253 (FLG_LOOPLOOPCONTINUE,
7254 cstring_makeLiteral ("Continue statement in nested loop"),
7255 exprNode_loc (ret));
7260 llbuglit ("exprNode_continue: bad qcontinue");
7266 exprNode exprNode_break (/*@only@*/ lltok l, int bqual)
7268 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7269 clause breakClause = context_breakClause ();
7271 ret->kind = XPR_BREAK;
7272 ret->edata = exprData_makeTok (l);
7273 ret->canBreak = TRUE;
7274 ret->mustBreak = TRUE;
7276 if (breakClause == NOCLAUSE)
7280 cstring_makeLiteral ("Break not inside while, for or switch statement"),
7281 exprNode_loc (ret));
7285 if (bqual != BADTOK)
7292 if (breakClause == SWITCHCLAUSE)
7294 if (!context_inDeepSwitch ())
7296 voptgenerror (FLG_SYNTAX,
7298 ("Break preceded by innerbreak is not in a deep switch"),
7299 exprNode_loc (ret));
7304 if (!context_inDeepLoop ())
7306 voptgenerror (FLG_SYNTAX,
7308 ("Break preceded by innerbreak is not in a deep loop"),
7309 exprNode_loc (ret));
7314 if (breakClause == SWITCHCLAUSE)
7316 voptgenerror (FLG_SYNTAX,
7318 ("Break preceded by loopbreak is breaking a switch"),
7319 exprNode_loc (ret));
7323 if (breakClause != SWITCHCLAUSE)
7327 message ("Break preceded by switchbreak is breaking %s",
7328 cstring_makeLiteralTemp
7329 ((breakClause == WHILECLAUSE
7330 || breakClause == DOWHILECLAUSE) ? "a while loop"
7331 : (breakClause == FORCLAUSE) ? "a for loop"
7332 : (breakClause == ITERCLAUSE) ? "an iterator"
7334 exprNode_loc (ret));
7342 if (breakClause == SWITCHCLAUSE)
7344 clause nextBreakClause = context_nextBreakClause ();
7346 switch (nextBreakClause)
7348 case NOCLAUSE: break;
7354 (FLG_LOOPSWITCHBREAK,
7355 cstring_makeLiteral ("Break statement in switch inside loop"),
7356 exprNode_loc (ret));
7360 (FLG_SWITCHSWITCHBREAK,
7361 cstring_makeLiteral ("Break statement in switch inside switch"),
7362 exprNode_loc (ret));
7369 if (context_inDeepLoop ())
7373 cstring_makeLiteral ("Break statement in nested loop"),
7374 exprNode_loc (ret));
7378 if (context_inDeepLoopSwitch ())
7381 (FLG_SWITCHLOOPBREAK,
7382 cstring_makeLiteral ("Break statement in loop inside switch"),
7383 exprNode_loc (ret));
7393 exprNode exprNode_nullReturn (/*@only@*/ lltok t)
7395 fileloc loc = lltok_getLoc (t);
7396 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
7398 context_returnFunction ();
7399 exprChecks_checkNullReturn (loc);
7401 ret->kind = XPR_NULLRETURN;
7402 ret->edata = exprData_makeTok (t);
7403 ret->exitCode = XK_MUSTRETURN;
7407 exprNode exprNode_return (/*@only@*/ exprNode e)
7411 if (exprNode_isError (e))
7413 ret = exprNode_createUnknown ();
7417 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (e->loc));
7419 exprNode_checkUse (ret, e->sref, e->loc);
7420 exprNode_checkReturn (e);
7423 context_returnFunction ();
7424 ret->kind = XPR_RETURN;
7425 ret->edata = exprData_makeSingle (e);
7426 ret->exitCode = XK_MUSTRETURN;
7431 exprNode exprNode_comma (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
7435 if (exprNode_isError (e1))
7437 if (exprNode_isError (e2))
7439 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
7443 ret = exprNode_createPartialCopy (e2);
7444 exprNode_checkUse (ret, e2->sref, e2->loc);
7445 ret->sref = e2->sref;
7450 ret = exprNode_createPartialCopy (e1);
7452 exprNode_checkUse (ret, e1->sref, e1->loc);
7454 if (!exprNode_isError (e2))
7456 exprNode_mergeUSs (ret, e2);
7457 exprNode_checkUse (ret, e2->sref, e2->loc);
7458 ret->sref = e2->sref;
7462 ret->kind = XPR_COMMA;
7463 ret->edata = exprData_makePair (e1, e2);
7465 if (exprNode_isDefined (e1))
7467 if (exprNode_isDefined (e2))
7471 if (exprNode_mustEscape (e1) || e1->mustBreak)
7475 message ("Second clause of comma expression is unreachable: %s",
7476 exprNode_unparse (e2)),
7480 ret->exitCode = exitkind_combine (e1->exitCode, e2->exitCode);
7481 ret->mustBreak = e1->mustBreak || e2->mustBreak;
7482 ret->canBreak = e1->canBreak || e2->canBreak;
7486 if (exprNode_mustEscape (e1) || e1->mustBreak)
7490 message ("Second clause of comma expression is unreachable: %s",
7491 exprNode_unparse (e2)),
7495 ret->exitCode = e1->exitCode;
7496 ret->canBreak = e1->canBreak;
7501 if (exprNode_isDefined (e2))
7503 ret->exitCode = e2->exitCode;
7504 ret->mustBreak = e2->mustBreak;
7505 ret->canBreak = e2->canBreak;
7512 static bool exprNode_checkOneInit (/*@notnull@*/ exprNode el, exprNode val)
7514 ctype t1 = exprNode_getType (el);
7515 ctype t2 = exprNode_getType (val);
7516 bool hasError = FALSE;
7518 if (ctype_isUnknown (t1))
7520 voptgenerror (FLG_IMPTYPE,
7521 message ("Variable has unknown (implicitly int) type: %s",
7522 exprNode_unparse (el)),
7526 el->typ = ctype_int;
7529 if (exprNode_isDefined (val) && val->kind == XPR_INITBLOCK)
7531 exprNodeList vals = exprData_getArgs (val->edata);
7533 if (ctype_isRealAP (t1))
7538 exprNodeList_elements (vals, oneval)
7540 cstring istring = message ("%d", i);
7543 (exprNode_fakeCopy (el),
7544 exprNode_numLiteral (ctype_int, istring,
7545 fileloc_copy (el->loc), i));
7547 if (exprNode_isDefined (newel))
7549 if (exprNodeList_size (vals) == 1
7550 && ctype_isString (exprNode_getType (oneval))
7551 && ctype_isChar (exprNode_getType (newel)))
7553 exprNode_freeIniter (newel);
7557 if (exprNode_checkOneInit (newel, oneval))
7562 if (nerrors > 3 && exprNodeList_size (vals) > 6)
7565 (message ("Additional initialization errors "
7566 "for %s not reported",
7567 exprNode_unparse (el)),
7569 exprNode_freeIniter (newel);
7574 exprNode_freeIniter (newel);
7579 exprNode_freeIniter (newel);
7584 cstring_free (istring);
7587 } end_exprNodeList_elements;
7590 else if (ctype_isStruct (ctype_realType (t1)))
7592 uentryList fields = ctype_getFields (t1);
7595 if (uentryList_size (fields) != exprNodeList_size (vals))
7597 if (uentryList_size (fields) > exprNodeList_size (vals))
7599 hasError = optgenerror
7601 message ("Initializer block for "
7602 "%s has %d field%&, but %s has %d field%&: %q",
7603 exprNode_unparse (el),
7604 exprNodeList_size (vals),
7606 uentryList_size (fields),
7607 exprNodeList_unparse (vals)),
7612 hasError = optgenerror
7614 message ("Initializer block for "
7615 "%s has %d field%&, but %s has %d field%&: %q",
7616 exprNode_unparse (el),
7617 exprNodeList_size (vals),
7619 uentryList_size (fields),
7620 exprNodeList_unparse (vals)),
7626 exprNodeList_elements (vals, oneval)
7628 uentry thisfield = uentryList_getN (fields, i);
7630 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
7632 uentry_getName (thisfield));
7634 if (exprNode_isDefined (newel))
7636 if (exprNode_checkOneInit (newel, oneval))
7641 exprNode_freeIniter (newel);
7645 } end_exprNodeList_elements;
7650 hasError = optgenerror
7652 message ("Initializer block used for "
7653 "%s where %t is expected: %s",
7654 exprNode_unparse (el), t1, exprNode_unparse (val)),
7660 if (exprNode_isDefined (val))
7662 doAssign (el, val, TRUE);
7664 if (!exprNode_matchType (t1, val))
7666 hasError = gentypeerror
7668 message ("Initial value of %s is type %t, "
7670 exprNode_unparse (el),
7671 t2, t1, exprNode_unparse (val)),
7681 exprNode_makeInitializationAux (/*@temp@*/ idDecl t)
7685 if (usymtab_exists (idDecl_observeId (t)))
7687 uentry ue = usymtab_lookup (idDecl_observeId (t));
7688 ret = exprNode_createId (ue);
7690 /*@i723 don't do this...but why? */
7692 ct = ctype_realishType (ret->typ);
7694 DPRINTF (("Type: %s", ctype_unparse (ret->typ)));
7696 if (ctype_isUnknown (ct))
7698 if (uentry_isAnyTag (ue))
7702 message ("%s used but not previously declared: %s",
7703 uentry_ekindName (ue),
7704 idDecl_getName (t)),
7712 message ("Variable has unknown (implicitly int) type: %s",
7713 idDecl_getName (t)),
7723 uentry ue = uentry_makeUnrecognized (idDecl_observeId (t),
7725 ret = exprNode_fromIdentifierAux (ue);
7728 ** No error - this happens in old style declarations:
7732 message ("Unrecognized identifier in intializer: %s", idDecl_observeId (t)),
7739 exprData_free (ret->edata, ret->kind);
7740 ret->edata = exprData_undefined;
7741 ret->exitCode = XK_NEVERESCAPE;
7742 ret->mustBreak = FALSE;
7743 ret->kind = XPR_INIT;
7747 exprNode exprNode_makeEmptyInitialization (/*@only@*/ idDecl t)
7749 exprNode ret = exprNode_makeInitializationAux (t);
7750 llassert (ret->edata == exprData_undefined);
7751 ret->edata = exprData_makeInit (t, exprNode_undefined);
7755 exprNode exprNode_makeInitialization (/*@only@*/ idDecl t,
7756 /*@only@*/ exprNode e)
7758 uentry ue = usymtab_lookup (idDecl_observeId (t));
7759 exprNode ret = exprNode_makeInitializationAux (t);
7760 fileloc loc = exprNode_loc (e);
7762 if (exprNode_isError (e))
7764 e = exprNode_createUnknown ();
7767 /* error: assume initializer is defined */
7768 sRef_setDefined (ret->sref, g_currentloc);
7772 ctype ct = ctype_realishType (ret->typ);
7777 ** was addSafeUse --- what's the problem?
7779 ** int x = 3, y = x ?
7782 exprData_free (ret->edata, ret->kind);
7783 ret->edata = exprData_makeInit (t, e);
7785 exprNode_checkUse (ret, e->sref, e->loc);
7787 if (ctype_isUnknown (e->typ) && uentry_isValid (ue))
7789 exprNode lhs = exprNode_createId (ue);
7792 ** static storage should be undefined before initializing
7795 if (uentry_isStatic (ue))
7797 sRef_setDefState (lhs->sref, SS_PARTIAL, fileloc_undefined);
7800 (void) exprNode_checkOneInit (lhs, e);
7802 if (uentry_isStatic (ue))
7804 sRef_setDefState (lhs->sref, SS_DEFINED, fileloc_undefined);
7807 exprNode_free (lhs);
7811 if (!exprNode_matchType (ct, e))
7813 if (exprNode_isZero (e) && ctype_isArrayPtr (ct))
7820 (exprNode_getType (e), e, exprNode_getType (ret), ret,
7822 ("Variable %q initialized to type %t, expects %t: %s",
7823 uentry_getName (ue), exprNode_getType (e),
7824 exprNode_getType (ret),
7825 exprNode_unparse (e)),
7831 if (uentry_isStatic (ue))
7833 sRef_setDefState (ret->sref, SS_PARTIAL, fileloc_undefined);
7836 doAssign (ret, e, TRUE);
7838 if (uentry_isStatic (ue))
7840 sRef_setDefState (ret->sref, SS_DEFINED, fileloc_undefined);
7844 if (context_inIterDef ())
7846 /* should check if it is yield */
7847 uentry_setUsed (ue, loc);
7854 exprNode_mergeUSs (ret, e);
7858 exprNode exprNode_iter (/*@observer@*/ uentry name,
7859 /*@only@*/ exprNodeList alist,
7860 /*@only@*/ exprNode body,
7861 /*@observer@*/ uentry end)
7866 llassert (uentry_isValid (name));
7868 uentry_setUsed (name, exprNode_loc (body));
7870 ret = exprNode_createPartialCopy (body);
7871 iname = uentry_getName (name);
7873 if (uentry_isInvalid (end))
7876 message ("Iter %s not balanced with end_%s", iname, iname));
7880 cstring ename = uentry_getName (end);
7882 if (!cstring_equalPrefix (ename, "end_"))
7884 llerror (FLG_ITER, message ("Iter %s not balanced with end_%s: %s",
7885 iname, iname, ename));
7889 if (!cstring_equal (iname, cstring_suffix (ename, 4)))
7892 message ("Iter %s not balanced with end_%s: %s",
7893 iname, iname, ename));
7897 cstring_free (ename);
7900 context_exitIterClause (body);
7902 ret->kind = XPR_ITER;
7903 ret->edata = exprData_makeIter (name, alist, body, end);
7905 if (uentry_isIter (name))
7907 (void) checkArgsReal (name, body,
7908 uentry_getParams (name), alist, TRUE, ret);
7911 cstring_free (iname);
7917 exprNode_iterNewId (/*@only@*/ cstring s)
7919 exprNode e = exprNode_new ();
7920 uentry ue = uentryList_getN (uentry_getParams (getCurrentIter ()), iterParamNo ());
7922 llassert (processingIterVars ());
7924 e->loc = context_getSaveLocation ();
7926 if (fileloc_isUndefined (e->loc))
7928 fileloc_free (e->loc);
7929 e->loc = fileloc_copy (g_currentloc);
7932 e->uses = sRefSet_new ();
7933 e->sets = sRefSet_new ();
7934 e->msets = sRefSet_new ();
7936 e->val = multiVal_unknown ();
7937 e->guards = guardSet_new ();
7939 e->isJumpPoint = FALSE;
7940 e->exitCode = XK_NEVERESCAPE;
7942 /*> missing fields, detected by lclint <*/
7943 e->canBreak = FALSE;
7944 e->mustBreak = FALSE;
7945 e->etext = cstring_undefined;
7947 if (uentry_isYield (ue))
7949 uentry uue = uentry_makeVariable (s, uentry_getType (ue),
7950 fileloc_copy (e->loc),
7954 uue = usymtab_supEntrySrefReturn (uue);
7956 sr = uentry_getSref (uue);
7957 sRef_mergeStateQuiet (sr, uentry_getSref (ue));
7958 sr = uentry_getSref (uue);
7959 sRef_setDefined (sr, e->loc);
7961 e->typ = uentry_getType (uue);
7963 e->edata = exprData_makeId (uue);
7964 uentry_setUsed (uue, g_currentloc);
7970 sRef_setGlobalScope ();
7971 uue = uentry_makeVariableLoc (s, ctype_unknown);
7973 e->typ = ctype_unknown;
7974 e->edata = exprData_makeId (uue);
7976 uentry_setUsed (uue, e->loc);
7977 uentry_setHasNameError (uue);
7979 if (context_getFlag (FLG_REPEATUNRECOG))
7981 uentry_markOwned (uue);
7985 usymtab_supGlobalEntry (uue);
7988 sRef_clearGlobalScope ();
7990 voptgenerror (FLG_UNRECOG, message ("Unrecognized identifier: %s", s),
7997 exprNode_defineConstraints(e);
8002 exprNode_iterExpr (/*@returned@*/ exprNode e)
8004 if (!processingIterVars ())
8006 llcontbuglit ("checkIterParam: not in iter");
8010 if (uentry_isYield (uentryList_getN (uentry_getParams (getCurrentIter ()),
8013 if (exprNode_isDefined (e))
8015 if (fileloc_isDefined (e->loc))
8019 message ("Yield parameter is not simple identifier: %s",
8020 exprNode_unparse (e)),
8027 message ("Yield parameter is not simple identifier: %s",
8028 exprNode_unparse (e)),
8038 exprNode_iterId (/*@observer@*/ uentry c)
8042 llassert (processingIterVars ());
8044 ue = uentryList_getN (uentry_getParams (getCurrentIter ()),
8047 if (uentry_isYield (ue))
8049 ctype ct = uentry_getType (ue);
8050 exprNode e = exprNode_createPlain (ct);
8051 cstring name = uentry_getName (c);
8052 uentry le = uentry_makeVariable (name, ct, fileloc_undefined, FALSE);
8054 uentry_setUsed (ue, g_currentloc);
8055 uentry_setHasNameError (ue);
8057 cstring_free (name);
8060 e->edata = exprData_makeId (le);
8061 e->loc = context_getSaveLocation ();
8062 e->sref = uentry_getSref (le);
8064 usymtab_supEntrySref (le);
8066 if (!context_inHeader ())
8070 message ("Yield parameter shadows local declaration: %q",
8071 uentry_getName (c)),
8072 fileloc_isDefined (e->loc) ? e->loc : g_currentloc))
8074 uentry_showWhereDeclared (c);
8081 return (exprNode_fromIdentifierAux (c));
8084 exprNode exprNode_iterStart (/*@observer@*/ uentry name, /*@only@*/ exprNodeList alist)
8086 exprNode ret = exprNode_create (ctype_unknown);
8088 ret->kind = XPR_ITERCALL;
8089 ret->edata = exprData_makeIterCall (name, alist);
8091 if (uentry_isIter (name))
8093 uentryList params = uentry_getParams (name);
8095 if (context_inIterDef ()
8096 && uentryList_size (params) == exprNodeList_size (alist))
8100 exprNodeList_elements (alist, arg)
8102 uentry parg = uentryList_getN (params, i);
8104 if (uentry_isYield (parg))
8106 uentry ue = exprNode_getUentry (arg);
8108 if (uentry_isValid (ue))
8115 } end_exprNodeList_elements;
8118 (void) checkArgsReal (name, ret, params, alist, TRUE, ret);
8119 checkUnspecCall (ret, params, alist);
8125 /*@exposed@*/ sRef exprNode_getSref (exprNode e)
8127 if (exprNode_isDefined (e))
8130 if (e->sref == defref) /*@noaccess sRef@*/
8133 e->sref = sRef_makeUnknown ();
8134 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
8145 return sRef_undefined;
8149 /*@observer@*/ cstring
8150 exprNode_unparseFirst (exprNode e)
8152 if (exprNode_isDefined (e))
8156 if (e->kind == XPR_STMTLIST
8157 || e->kind == XPR_COMMA || e->kind == XPR_COND)
8159 exprNode first = exprData_getPairA (e->edata);
8161 if (exprNode_isDefined (first))
8163 return (exprNode_unparseFirst (exprData_getPairA (e->edata)));
8167 return (cstring_makeLiteralTemp ("..."));
8171 ret = cstring_elide (exprNode_unparse (e), 20);
8172 cstring_markOwned (ret);
8178 return cstring_makeLiteralTemp ("<error>");
8182 /*@observer@*/ cstring
8183 exprNode_unparse (/*@temp@*/ exprNode e)
8185 if (exprNode_isError (e))
8187 return cstring_makeLiteralTemp ("<error>");
8190 if (cstring_isDefined (e->etext))
8196 cstring ret = exprNode_doUnparse (e);
8198 /*@-modifies@*/ /* benevolent */
8205 /*@observer@*/ fileloc
8206 exprNode_loc (exprNode e)
8208 if (exprNode_isError (e))
8210 return (g_currentloc);
8219 ** executes exprNode e
8220 ** recursively rexecutes as though in original parse using
8221 ** information in e->edata
8224 static /*@only@*/ exprNodeList exprNodeList_effect (exprNodeList e)
8226 exprNodeList ret = exprNodeList_new ();
8228 exprNodeList_elements (e, current)
8230 exprNodeList_addh (ret, exprNode_effect (current));
8231 } end_exprNodeList_elements;
8236 static /*@only@*/ exprNode exprNode_effect (exprNode e)
8237 /*@globals internalState@*/
8239 bool innerEffect = inEffect;
8245 context_clearJustPopped ();
8247 if (exprNode_isError (e))
8249 ret = exprNode_undefined;
8254 ** Turn off expose and dependent transfer checking.
8255 ** Need to pass exposed internal nodes,
8256 ** [ copying would be a waste! ]
8257 ** [ Actually, I think I wasted a lot more time than its worth ]
8258 ** [ trying to do this. ]
8262 /*@-observertrans@*/
8263 /*@-dependenttrans@*/
8270 ret = exprNode_addParens (exprData_getUopTok (data),
8271 exprNode_effect (exprData_getUopNode (data)));
8274 ret = exprNode_assign (exprNode_effect (exprData_getOpA (data)),
8275 exprNode_effect (exprData_getOpB (data)),
8276 exprData_getOpTok (data));
8279 ret = exprNode_undefined;
8282 ret = exprNode_functionCall (exprNode_effect (exprData_getFcn (data)),
8283 exprNodeList_effect (exprData_getArgs (data)));
8296 cstring id = exprData_getId (data);
8297 uentry ue = usymtab_lookupSafe (id);
8299 ret = exprNode_fromIdentifierAux (ue);
8300 ret->loc = fileloc_update (ret->loc, e->loc);
8307 ret = exprNode_arrayFetch (exprNode_effect (exprData_getPairA (data)),
8308 exprNode_effect (exprData_getPairB (data)));
8311 ret = exprNode_op (exprNode_effect (exprData_getOpA (data)),
8312 exprNode_effect (exprData_getOpB (data)),
8313 exprData_getOpTok (data));
8317 ret = exprNode_postOp (exprNode_effect (exprData_getUopNode (data)),
8318 exprData_getUopTok (data));
8321 ret = exprNode_preOp (exprNode_effect (exprData_getUopNode (data)),
8322 exprData_getUopTok (data));
8334 ret = exprNode_vaArg (exprData_getCastTok (data),
8335 exprNode_effect (exprData_getCastNode (data)),
8336 exprData_getCastType (data));
8340 ret = exprNode_cast (exprData_getCastTok (data),
8341 exprNode_effect (exprData_getCastNode (data)),
8342 exprData_getCastType (data));
8345 ret = exprNode_iterStart (exprData_getIterCallIter (data),
8347 (exprData_getIterCallArgs (data)));
8351 ret = exprNode_iter (exprData_getIterSname (data),
8352 exprNodeList_effect (exprData_getIterAlist (data)),
8353 exprNode_effect (exprData_getIterBody (data)),
8354 exprData_getIterEname (data));
8358 ret = exprNode_for (exprNode_effect (exprData_getPairA (data)),
8359 exprNode_effect (exprData_getPairB (data)));
8363 ret = exprNode_forPred (exprNode_effect (exprData_getTripleInit (data)),
8364 exprNode_effect (exprData_getTripleTest (data)),
8365 exprNode_effect (exprData_getTripleInc (data)));
8369 ret = exprNode_createTok (exprData_getTok (data));
8373 ret = exprNode_goto (exprData_getLiteral (data));
8374 ret->loc = fileloc_update (ret->loc, e->loc);
8378 ret = exprNode_continue (exprData_getTok (data), QSAFEBREAK);
8382 ret = exprNode_break (exprData_getTok (data), QSAFEBREAK);
8386 ret = exprNode_return (exprNode_effect (exprData_getSingle (data)));
8389 case XPR_NULLRETURN:
8390 ret = exprNode_nullReturn (exprData_getTok (data));
8394 ret = exprNode_comma (exprNode_effect (exprData_getPairA (data)),
8395 exprNode_effect (exprData_getPairB (data)));
8399 ret = exprNode_cond (exprNode_effect (exprData_getTriplePred (data)),
8400 exprNode_effect (exprData_getTripleTrue (data)),
8401 exprNode_effect (exprData_getTripleFalse (data)));
8404 ret = exprNode_if (exprNode_effect (exprData_getPairA (data)),
8405 exprNode_effect (exprData_getPairB (data)));
8409 ret = exprNode_ifelse (exprNode_effect (exprData_getTriplePred (data)),
8410 exprNode_effect (exprData_getTripleTrue (data)),
8411 exprNode_effect (exprData_getTripleFalse (data)));
8414 ret = exprNode_whilePred (exprData_getSingle (data));
8418 ret = exprNode_while (exprNode_effect (exprData_getPairA (data)),
8419 exprNode_effect (exprData_getPairB (data)));
8423 ret = exprNode_doWhile (exprNode_effect (exprData_getPairA (data)),
8424 exprNode_effect (exprData_getPairB (data)));
8428 ret = exprNode_makeBlock (exprNode_effect (exprData_getSingle (data)));
8432 ret = exprNode_statement (exprNode_effect (exprData_getUopNode (data)),
8433 exprData_getUopTok (data));
8437 ret = exprNode_concat (exprNode_effect (exprData_getPairA (data)),
8438 exprNode_effect (exprData_getPairB (data)));
8443 ret = exprNode_caseMarker
8444 (exprNode_effect (exprData_getSingle (data)),
8450 ret = exprNode_createTok (exprData_getTok (data));
8454 ret = exprNode_switch (exprNode_effect (exprData_getPairA (data)),
8455 exprNode_effect (exprData_getPairB (data)));
8459 ret = exprNode_makeInitialization
8460 (exprData_getInitId (data),
8461 exprNode_effect (exprData_getInitNode (data)));
8465 ret = exprNode_fieldAccessAux
8466 (exprNode_effect (exprData_getFieldNode (data)),
8467 exprNode_loc (exprData_getFieldNode (data)),
8468 cstring_copy (exprData_getFieldName (data)));
8472 ret = exprNode_arrowAccessAux
8473 (exprNode_effect (exprData_getFieldNode (data)),
8474 exprNode_loc (exprData_getFieldNode (data)),
8475 cstring_copy (exprData_getFieldName (data)));
8478 case XPR_STRINGLITERAL:
8492 /*@=observertrans@*/
8494 /*@=dependenttrans@*/
8505 static /*@observer@*/ cstring exprNode_rootVarName (exprNode e)
8510 if (exprNode_isError (e))
8512 return cstring_undefined;
8520 ret = exprNode_rootVarName (exprData_getUopNode (data));
8523 ret = exprNode_rootVarName (exprData_getOpA (data));
8527 ret = exprData_getId (data);
8552 case XPR_NULLRETURN:
8575 case XPR_STRINGLITERAL:
8576 ret = cstring_undefined;
8583 static /*@only@*/ cstring exprNode_doUnparse (exprNode e)
8588 if (exprNode_isError (e))
8590 static /*@only@*/ cstring error = cstring_undefined;
8592 if (!cstring_isDefined (error))
8594 error = cstring_makeLiteral ("<error>");
8605 ret = message ("(%s)", exprNode_unparse (exprData_getUopNode (e->edata)));
8608 ret = message ("%s %s %s",
8609 exprNode_unparse (exprData_getOpA (data)),
8610 lltok_unparse (exprData_getOpTok (data)),
8611 exprNode_unparse (exprData_getOpB (data)));
8614 ret = message ("%s(%q)",
8615 exprNode_unparse (exprData_getFcn (data)),
8616 exprNodeList_unparse (exprData_getArgs (data)));
8619 ret = message ("{ %q }", exprNodeList_unparse (exprData_getArgs (data)));
8622 ret = cstring_undefined;
8625 ret = message ("%s:", exprData_getId (data));
8629 ret = cstring_copy (exprData_getId (data));
8632 ret = message ("%s[%s]", exprNode_unparse (exprData_getPairA (data)),
8633 exprNode_unparse (exprData_getPairB (data)));
8636 ret = message ("<body>");
8639 ret = message ("%s %s %s",
8640 exprNode_unparse (exprData_getOpA (data)),
8641 lltok_unparse (exprData_getOpTok (data)),
8642 exprNode_unparse (exprData_getOpB (data)));
8646 ret = message ("%s%s",
8647 lltok_unparse (exprData_getUopTok (data)),
8648 exprNode_unparse (exprData_getUopNode (data)));
8652 ret = message ("%s%s",
8653 exprNode_unparse (exprData_getUopNode (data)),
8654 lltok_unparse (exprData_getUopTok (data)));
8658 ret = message ("offsetof(%s,%q)",
8659 ctype_unparse (qtype_getType (exprData_getOffsetType (data))),
8660 cstringList_unparseSep (exprData_getOffsetName (data), cstring_makeLiteralTemp (".")));
8664 ret = message ("sizeof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
8668 ret = message ("sizeof(%s)", exprNode_unparse (exprData_getSingle (data)));
8672 ret = message ("alignof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
8676 ret = message ("alignof(%s)", exprNode_unparse (exprData_getSingle (data)));
8680 ret = message ("va_arg(%s, %q)",
8681 exprNode_unparse (exprData_getCastNode (data)),
8682 qtype_unparse (exprData_getCastType (data)));
8686 ret = message ("%q(%q)",
8687 uentry_getName (exprData_getIterCallIter (data)),
8688 exprNodeList_unparse (exprData_getIterCallArgs (data)));
8691 ret = message ("%q(%q) %s %q",
8692 uentry_getName (exprData_getIterSname (data)),
8693 exprNodeList_unparse (exprData_getIterAlist (data)),
8694 exprNode_unparse (exprData_getIterBody (data)),
8695 uentry_getName (exprData_getIterEname (data)));
8698 ret = message ("(%q)%s",
8699 qtype_unparse (exprData_getCastType (data)),
8700 exprNode_unparse (exprData_getCastNode (data)));
8704 ret = message ("%s %s",
8705 exprNode_unparse (exprData_getPairA (data)),
8706 exprNode_unparse (exprData_getPairB (data)));
8710 ret = message ("for (%s; %s; %s)",
8711 exprNode_unparse (exprData_getTripleInit (data)),
8712 exprNode_unparse (exprData_getTripleTest (data)),
8713 exprNode_unparse (exprData_getTripleInc (data)));
8717 ret = message ("goto %s", exprData_getLiteral (data));
8721 ret = cstring_makeLiteral ("continue");
8725 ret = cstring_makeLiteral ("break");
8729 ret = message ("return %s", exprNode_unparse (exprData_getSingle (data)));
8732 case XPR_NULLRETURN:
8733 ret = cstring_makeLiteral ("return");
8737 ret = message ("%s, %s",
8738 exprNode_unparse (exprData_getPairA (data)),
8739 exprNode_unparse (exprData_getPairB (data)));
8743 ret = message ("%s ? %s : %s",
8744 exprNode_unparse (exprData_getTriplePred (data)),
8745 exprNode_unparse (exprData_getTripleTrue (data)),
8746 exprNode_unparse (exprData_getTripleFalse (data)));
8749 ret = message ("if (%s) %s",
8750 exprNode_unparse (exprData_getPairA (data)),
8751 exprNode_unparse (exprData_getPairB (data)));
8755 ret = message ("if (%s) %s else %s",
8756 exprNode_unparse (exprData_getTriplePred (data)),
8757 exprNode_unparse (exprData_getTripleTrue (data)),
8758 exprNode_unparse (exprData_getTripleFalse (data)));
8761 ret = message ("while (%s) %s",
8762 exprNode_unparse (exprData_getPairA (data)),
8763 exprNode_unparse (exprData_getPairB (data)));
8767 ret = cstring_copy (exprNode_unparse (exprData_getSingle (data)));
8771 ret = cstring_copy (lltok_unparse (exprData_getTok (data)));
8775 ret = message ("do { %s } while (%s)",
8776 exprNode_unparse (exprData_getPairB (data)),
8777 exprNode_unparse (exprData_getPairA (data)));
8781 ret = message ("{ %s }", exprNode_unparseFirst (exprData_getSingle (data)));
8785 ret = cstring_copy (exprNode_unparse (exprData_getUopNode (data)));
8789 ret = message ("%s; %s",
8790 exprNode_unparse (exprData_getPairA (data)),
8791 exprNode_unparse (exprData_getPairB (data)));
8796 ret = cstring_makeLiteral ("default:");
8800 ret = message ("switch (%s) %s",
8801 exprNode_unparse (exprData_getPairA (data)),
8802 exprNode_unparse (exprData_getPairB (data)));
8807 ret = message ("case %s:",
8808 exprNode_unparse (exprData_getSingle (data)));
8812 if (exprNode_isError (exprData_getInitNode (data)))
8814 ret = message ("%q", idDecl_unparseC (exprData_getInitId (data)));
8818 ret = message ("%q = %s",
8819 idDecl_unparseC (exprData_getInitId (data)),
8820 exprNode_unparse (exprData_getInitNode (data)));
8825 ret = message ("%s.%s",
8826 exprNode_unparse (exprData_getFieldNode (data)),
8827 exprData_getFieldName (data));
8831 ret = message ("%s->%s",
8832 exprNode_unparse (exprData_getFieldNode (data)),
8833 exprData_getFieldName (data));
8836 case XPR_STRINGLITERAL:
8837 ret = message ("\"%s\"", exprData_getLiteral (data));
8841 ret = cstring_copy (exprData_getLiteral (data));
8845 ret = cstring_makeLiteral ("<node>");
8853 exprNode_isInitializer (exprNode e)
8855 return (exprNode_isDefined (e)
8856 && e->kind == XPR_INIT);
8860 exprNode_isCharLit (exprNode e)
8862 if (exprNode_isDefined (e))
8864 return (multiVal_isChar (exprNode_getValue (e)));
8873 exprNode_isNumLit (exprNode e)
8875 if (exprNode_isDefined (e))
8877 return (multiVal_isInt (exprNode_getValue (e)));
8886 exprNode_isFalseConstant (exprNode e)
8888 if (exprNode_isDefined (e))
8890 cstring s = exprNode_rootVarName (e);
8892 if (cstring_equal (s, context_getFalseName ()))
8902 exprNode_matchLiteral (ctype expected, exprNode e)
8904 if (exprNode_isDefined (e))
8906 multiVal m = exprNode_getValue (e);
8908 if (multiVal_isDefined (m))
8910 if (multiVal_isInt (m))
8912 long int val = multiVal_forceInt (m);
8914 if (ctype_isDirectBool (ctype_realishType (expected)))
8918 return FALSE; /* really?! return TRUE; allow use of 0 for FALSE */
8926 if (ctype_isRealInt (expected))
8929 ** unsigned <- [ constant >= 0 is okay ]
8932 if (ctype_isUnsigned (expected))
8941 ** No checks on sizes of integers...maybe add
8945 DPRINTF (("Here: %s => %s", exprNode_unparse (e), ctype_unparse (expected)));
8946 DPRINTF (("Type: %s / %s", ctype_unparse (exprNode_getType (e)),
8947 bool_unparse (ctype_isInt (exprNode_getType (e)))));
8949 if (context_getFlag (FLG_NUMLITERAL)
8950 && (ctype_isRegularInt (exprNode_getType (e)) || val == 0)) {
8956 return FALSE; /* evs 2000-05-17: previously, always returned TRUE */
8960 else if (ctype_isChar (expected))
8964 else if (ctype_isArrayPtr (expected))
8968 else if (ctype_isAnyFloat (expected))
8970 return (context_getFlag (FLG_NUMLITERAL));
8977 else if (multiVal_isDouble (m))
8979 if (ctype_isAnyFloat (expected))
8984 else if (multiVal_isChar (m))
8986 char val = multiVal_forceChar (m);
8988 if (ctype_isChar (expected))
8990 if (ctype_isUnsigned (expected) && ((int)val) < 0)
9011 exprNode_matchType (ctype expected, exprNode e)
9015 if (!exprNode_isDefined (e)) return TRUE;
9017 actual = ctype_realishType (exprNode_getType (e));
9019 if (ctype_match (ctype_realishType (expected), actual))
9024 llassert (!exprNode_isError (e));
9025 return (exprNode_matchLiteral (expected, e));
9029 exprNode_matchTypes (exprNode e1, exprNode e2)
9034 if (!exprNode_isDefined (e1)) return TRUE;
9035 if (!exprNode_isDefined (e2)) return TRUE;
9038 ** realish type --- keep bools, bools
9041 t1 = ctype_realishType (exprNode_getType (e1));
9042 t2 = ctype_realishType (exprNode_getType (e2));
9044 if (ctype_match (t1, t2))
9049 return (exprNode_matchLiteral (t1, e2) || exprNode_matchLiteral (t2, e1));
9057 exprNode_matchArgType (ctype ct, exprNode e)
9061 if (!exprNode_isDefined (e))
9066 et = ctype_realType (exprNode_getType (e));
9068 if (ctype_matchArg (ct, et)) return TRUE;
9070 llassert (!exprNode_isError (e));
9071 return (exprNode_matchLiteral (ct, e));
9074 static /*@only@*/ exprNodeSList
9075 exprNode_flatten (/*@dependent@*/ exprNode e) /*@*/
9077 if (exprNode_isDefined (e))
9079 if (e->kind == XPR_STMTLIST)
9081 return (exprNodeSList_append
9082 (exprNode_flatten (exprData_getPairA (e->edata)),
9083 exprNode_flatten (exprData_getPairB (e->edata))));
9085 else if (e->kind == XPR_BLOCK)
9087 return (exprNode_flatten (exprData_getSingle (e->edata)));
9091 return (exprNodeSList_singleton (e));
9095 return exprNodeSList_new ();
9098 static /*@exposed@*/ exprNode
9099 exprNode_lastStatement (/*@returned@*/ exprNode e)
9101 if (exprNode_isDefined (e))
9103 if (e->kind == XPR_STMTLIST)
9105 exprNode b = exprData_getPairB (e->edata);
9107 if (exprNode_isDefined (b))
9109 return exprNode_lastStatement (b);
9113 return exprNode_lastStatement (exprData_getPairA (e->edata));
9116 else if (e->kind == XPR_BLOCK)
9118 return (exprNode_lastStatement (exprData_getSingle (e->edata)));
9126 return exprNode_undefined;
9129 static /*@exposed@*/ exprNode
9130 exprNode_firstStatement (/*@returned@*/ exprNode e)
9132 if (exprNode_isDefined (e))
9134 if (e->kind == XPR_STMTLIST)
9136 exprNode b = exprData_getPairA (e->edata);
9138 if (exprNode_isDefined (b))
9140 return exprNode_firstStatement (b);
9144 return exprNode_firstStatement (exprData_getPairB (e->edata));
9147 else if (e->kind == XPR_BLOCK)
9149 return (exprNode_firstStatement (exprData_getSingle (e->edata)));
9157 return exprNode_undefined;
9161 exprNode_mergeUSs (exprNode res, exprNode other)
9163 if (exprNode_isDefined (res) && exprNode_isDefined (other))
9165 res->msets = sRefSet_union (res->msets, other->msets);
9166 res->sets = sRefSet_union (res->sets, other->sets);
9167 res->uses = sRefSet_union (res->uses, other->uses);
9172 exprNode_mergeCondUSs (exprNode res, exprNode other1, exprNode other2)
9174 if (exprNode_isDefined (res))
9176 if (exprNode_isDefined (other1))
9178 res->sets = sRefSet_union (res->sets, other1->sets);
9179 res->msets = sRefSet_union (res->msets, other1->msets);
9180 res->uses = sRefSet_union (res->uses, other1->uses);
9182 if (exprNode_isDefined (other2))
9184 res->sets = sRefSet_union (res->sets, other2->sets);
9185 res->msets = sRefSet_union (res->msets, other2->msets);
9186 res->uses = sRefSet_union (res->uses, other2->uses);
9194 ** Reports errors is s is not defined.
9198 exprNode_addUse (exprNode e, /*@exposed@*/ sRef s)
9200 if (exprNode_isDefined (e))
9202 e->uses = sRefSet_insert (e->uses, s);
9207 exprNode_checkUse (exprNode e, /*@exposed@*/ sRef s, fileloc loc)
9209 if (sRef_isKnown (s) && !sRef_isConst (s))
9212 ** need to check all outer types are useable
9215 DPRINTF (("Check use: %s / %s",
9216 exprNode_unparse (e), sRef_unparse (s)));
9218 exprNode_addUse (e, s);
9220 if (!context_inProtectVars ())
9223 ** only report the deepest error
9226 sRef errorRef = sRef_undefined;
9227 sRef lastRef = sRef_undefined;
9228 bool deadRef = FALSE;
9229 bool unuseable = FALSE;
9230 bool errorMaybe = FALSE;
9232 while (sRef_isValid (s) && sRef_isKnown (s))
9234 ynm readable = sRef_isReadable (s);
9236 DPRINTF (("Readable: %s / %s",
9237 sRef_unparseFull (s), ynm_unparse (readable)));
9239 if (!(ynm_toBoolStrict (readable)))
9241 if (ynm_isMaybe (readable))
9245 deadRef = sRef_isPossiblyDead (errorRef);
9246 unuseable = sRef_isUnuseable (errorRef);
9253 deadRef = sRef_isDead (errorRef);
9254 unuseable = sRef_isUnuseable (errorRef);
9258 if (!sRef_isPartial (s))
9260 sRef_setDefined (s, fileloc_undefined);
9264 s = sRef_getBaseSafe (s);
9267 if (sRef_isValid (errorRef))
9269 if (sRef_isValid (lastRef) && sRef_isField (lastRef)
9270 && sRef_isPointer (errorRef))
9277 if (sRef_isThroughArrayFetch (errorRef))
9280 (FLG_STRICTUSERELEASED,
9281 message ("%q %q may be used after being released",
9282 sRef_unparseKindNamePlain (errorRef),
9283 sRef_unparse (errorRef)),
9286 sRef_showRefKilled (errorRef);
9288 if (sRef_isKept (errorRef))
9290 sRef_clearAliasState (errorRef, loc);
9296 DPRINTF (("HERE: %s", sRef_unparse (errorRef)));
9300 message ("%q %q %qused after being released",
9301 sRef_unparseKindNamePlain (errorRef),
9302 sRef_unparse (errorRef),
9303 cstring_makeLiteral (errorMaybe
9307 sRef_showRefKilled (errorRef);
9309 if (sRef_isKept (errorRef))
9311 sRef_clearAliasState (errorRef, loc);
9320 message ("%q %q%qused in inconsistent state",
9321 sRef_unparseKindName (errorRef),
9322 sRef_unparseOpt (errorRef),
9323 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9326 sRef_showStateInconsistent (errorRef);
9331 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
9335 message ("%q %q%qused before definition",
9336 sRef_unparseKindName (errorRef),
9337 sRef_unparseOpt (errorRef),
9338 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9342 sRef_setDefined (errorRef, loc);
9344 if (sRef_isAddress (errorRef))
9346 sRef_setDefined (sRef_getRootBase (errorRef), loc);
9348 } /* end is error */
9356 checkSafeUse (exprNode e, /*@exposed@*/ sRef s)
9358 if (exprNode_isDefined (e) && sRef_isKnown (s))
9360 e->uses = sRefSet_insert (e->uses, s);
9365 exprNode_checkSetAny (exprNode e, /*@dependent@*/ cstring name)
9367 if (exprNode_isDefined (e))
9369 e->sets = sRefSet_insert (e->sets, sRef_makeUnconstrained (name));
9374 exprNode_checkSet (exprNode e, /*@exposed@*/ sRef s)
9376 sRef defines = sRef_undefined;
9378 if (sRef_isValid (s) && !sRef_isNothing (s))
9380 uentry ue = sRef_getBaseUentry (s);
9382 if (uentry_isValid (ue))
9384 uentry_setLset (ue);
9387 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
9389 voptgenerror (FLG_USEDEF,
9390 message ("Attempt to set unuseable storage: %q",
9395 if (sRef_isMeaningful (s))
9398 if (sRef_isDead (s))
9400 sRef base = sRef_getBaseSafe (s);
9402 if (sRef_isValid (base)
9403 && sRef_isDead (base))
9405 sRef_setPartial (s, exprNode_loc (e));
9408 defines = s; /* okay - modifies for only param */
9410 else if (sRef_isPartial (s))
9412 sRef eref = exprNode_getSref (e);
9414 if (!sRef_isPartial (eref))
9417 ** should do something different here???
9420 sRef_setDefinedComplete (eref, exprNode_loc (e));
9424 sRef_setPartialDefinedComplete (eref, exprNode_loc (e));
9427 if (sRef_isMeaningful (eref))
9436 else if (sRef_isAllocated (s))
9438 sRef eref = exprNode_getSref (e);
9441 if (!sRef_isAllocated (eref))
9443 sRef_setDefinedComplete (eref, exprNode_loc (e));
9447 sRef base = sRef_getBaseSafe (eref);
9449 if (sRef_isValid (base))
9451 sRef_setPdefined (base, exprNode_loc (e));
9459 sRef_setDefinedNCComplete (s, exprNode_loc (e));
9464 else /* not meaningful...but still need to insert it */
9470 if (exprNode_isDefined (e) && sRef_isValid (defines))
9472 e->sets = sRefSet_insert (e->sets, defines);
9477 exprNode_checkMSet (exprNode e, /*@exposed@*/ sRef s)
9479 if (sRef_isValid (s) && !sRef_isNothing (s))
9481 uentry ue = sRef_getBaseUentry (s);
9483 if (uentry_isValid (ue))
9485 uentry_setLset (ue);
9488 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
9490 voptgenerror (FLG_USEDEF,
9491 message ("Attempt to set unuseable storage: %q", sRef_unparse (s)),
9495 if (sRef_isMeaningful (s))
9497 sRef_setDefinedComplete (s, exprNode_loc (e));
9500 if (exprNode_isDefined (e))
9502 e->msets = sRefSet_insert (e->msets, s);
9508 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode fcn, uentryList params, exprNodeList args)
9510 checkAnyCall (fcn, cstring_undefined, params, args,
9511 FALSE, sRefSet_undefined, FALSE, 0);
9515 checkOneArg (uentry ucurrent, /*@notnull@*/ exprNode current,
9516 /*@dependent@*/ exprNode fcn, bool isSpec, int argno, int totargs)
9520 if (uentry_isYield (ucurrent))
9522 sRef_setDefined (exprNode_getSref (current), exprNode_loc (current));
9523 exprNode_checkSet (current, current->sref);
9527 if (uentry_isSefParam (ucurrent))
9529 sRefSet sets = current->sets;
9530 sRef ref = exprNode_getSref (current);
9532 if (sRef_isMacroParamRef (ref))
9534 uentry ue = sRef_getUentry (ref);
9536 if (!uentry_isSefParam (ue))
9541 ("Parameter %d to %s is declared sef, but "
9542 "the argument is a macro parameter declared "
9544 argno, exprNode_unparse (fcn),
9545 exprNode_unparse (current)),
9546 exprNode_loc (current));
9550 if (!sRefSet_isEmpty (sets))
9552 sRefSet reported = sRefSet_undefined;
9554 sRefSet_realElements (current->sets, el)
9556 if (sRefSet_isSameNameMember (reported, el))
9558 ; /* don't report again */
9562 if (sRef_isUnconstrained (el))
9567 ("Parameter %d to %s is declared sef, but "
9568 "the argument calls unconstrained function %s "
9569 "(no guarantee it will not modify something): %s",
9570 argno, exprNode_unparse (fcn),
9571 sRef_unconstrainedName (el),
9572 exprNode_unparse (current)),
9573 exprNode_loc (current));
9580 ("Parameter %d to %s is declared sef, but "
9581 "the argument may modify %q: %s",
9582 argno, exprNode_unparse (fcn),
9584 exprNode_unparse (current)),
9585 exprNode_loc (current));
9588 } end_sRefSet_realElements;
9592 checkPassTransfer (current, ucurrent, isSpec, fcn, argno, totargs);
9593 exprNode_mergeUSs (fcn, current);
9598 checkAnyCall (/*@dependent@*/ exprNode fcn,
9599 /*@dependent@*/ cstring fname,
9602 bool hasMods, sRefSet mods,
9607 int nargs = exprNodeList_size (args);
9612 ** concat all args ud's to f, add each arg sref as a use unless
9613 ** it was specified as "out", in which case it is a def.
9616 uentryList_reset (pn);
9621 ** if paramn is only or unique, no other arg may alias argn
9624 exprNodeList_elements (args, current)
9628 if (exprNode_isDefined (current))
9630 if ((!uentryList_isUndefined (pn) && !uentryList_isFinished (pn)))
9632 uentry ucurrent = uentryList_current (pn);
9634 if (specialArgs == 0
9635 || (paramno < specialArgs))
9637 checkOneArg (ucurrent, current, fcn, isSpec, paramno, nargs);
9639 if (context_maybeSet (FLG_ALIASUNIQUE))
9641 if (uentry_isOnly (ucurrent)
9642 || uentry_isUnique (ucurrent))
9644 checkUniqueParams (fcn, current, args,
9650 else /* uentry is undefined */
9652 if (specialArgs == 0)
9654 exprNode_checkUseParam (current);
9657 exprNode_mergeUSs (fcn, current);
9660 uentryList_advanceSafe (pn);
9661 } end_exprNodeList_elements;
9667 sRefSet_allElements (mods, s)
9670 sRef rb = sRef_getRootBase (s);
9672 if (sRef_isFileOrGlobalScope (rb))
9674 context_usedGlobal (rb);
9677 fb = sRef_fixBaseParam (s, args);
9679 if (!sRef_isMacroParamRef (fb))
9681 if (sRef_isNothing (fb))
9687 if (sRef_isValid (fb))
9689 uentry ue = sRef_getBaseUentry (s);
9691 if (uentry_isValid (ue))
9693 uentry_setLset (ue);
9697 fcn->sets = sRefSet_insert (fcn->sets, fb);
9700 sRef_clearDerivedComplete (s);
9701 } end_sRefSet_allElements;
9707 if (context_hasMods ())
9709 if (context_maybeSet (FLG_MODUNCON))
9713 message ("Undetected modification possible "
9714 "from call to unconstrained function %s: %s",
9716 exprNode_unparse (fcn)),
9717 exprNode_loc (fcn));
9722 if (context_maybeSet (FLG_MODUNCONNOMODS)
9723 && !(context_inIterDef () || context_inIterEnd ()))
9726 (FLG_MODUNCONNOMODS,
9727 message ("Undetected modification possible "
9728 "from call to unconstrained function %s: %s",
9730 exprNode_unparse (fcn)),
9731 exprNode_loc (fcn));
9735 exprNode_checkSetAny (fcn, fname);
9739 void exprNode_checkUseParam (exprNode current)
9741 if (exprNode_isDefined (current))
9743 exprNode_checkUse (current, current->sref, current->loc);
9748 checkNumerics (ctype tr1, ctype tr2, ctype te1, ctype te2,
9749 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
9754 if (!ctype_match (tr1, tr2))
9756 if ((ctype_isRealInt (tr1) || ctype_isReal (tr1)) &&
9757 (ctype_isRealInt (tr2) || ctype_isReal (tr2)))
9765 message ("Incompatible types for %s (%s, %s): %s %s %s",
9767 ctype_unparse (te1),
9768 ctype_unparse (te2),
9769 exprNode_unparse (e1), lltok_unparse (op),
9770 exprNode_unparse (e2)),
9773 ret = ctype_unknown;
9777 if (ctype_isForceRealNumeric (&tr1) && ctype_isForceRealNumeric (&tr2))
9779 ret = ctype_resolveNumerics (tr1, tr2);
9781 else if (!context_msgStrictOps ())
9783 if (ctype_isPointer (tr1))
9785 if (ctype_isPointer (tr2) && !exprNode_isNullValue (e2))
9789 else if (ctype_isInt (tr2))
9795 ret = ctype_unknown;
9798 else if (ctype_isPointer (tr2))
9800 if (ctype_isPointer (tr1))
9804 else if (ctype_isInt (tr1))
9810 ret = ctype_unknown;
9815 ret = ctype_resolveNumerics (tr1, tr2);
9820 int opid = lltok_getTok (op);
9821 bool comparop = (opid == EQ_OP || opid == NE_OP
9822 || opid == TLT || opid == TGT
9823 || opid == LE_OP || opid == GE_OP);
9825 if (!ctype_isNumeric (tr1) && !ctype_isNumeric (tr2))
9828 && ((ctype_isEnum (tr1) && ctype_isEnum (tr2))
9829 || (ctype_isBool (tr1) && ctype_isBool (tr2))
9830 || (ctype_isChar (tr1) && ctype_isChar (tr2))))
9836 if (ctype_sameName (te1, te2))
9840 message ("Operands of %s are non-numeric (%t): %s %s %s",
9841 lltok_unparse (op), te1,
9842 exprNode_unparse (e1), lltok_unparse (op),
9843 exprNode_unparse (e2)),
9850 message ("Operands of %s are non-numerics (%t, %t): %s %s %s",
9851 lltok_unparse (op), te1, te2,
9852 exprNode_unparse (e1), lltok_unparse (op),
9853 exprNode_unparse (e2)),
9858 else if (!ctype_isNumeric (tr1))
9862 message ("Right operand of %s is non-numeric (%t): %s %s %s",
9863 lltok_unparse (op), te1,
9864 exprNode_unparse (e1), lltok_unparse (op),
9865 exprNode_unparse (e2)),
9870 if (!ctype_isNumeric (tr2))
9874 message ("Left operand of %s is non-numeric (%t): %s %s %s",
9875 lltok_unparse (op), te2,
9876 exprNode_unparse (e1), lltok_unparse (op),
9877 exprNode_unparse (e2)),
9882 ret = ctype_unknown;
9890 abstractOpError (ctype tr1, ctype tr2, lltok op,
9891 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
9892 fileloc loc1, fileloc loc2)
9894 if (ctype_isRealAbstract (tr1) && ctype_isRealAbstract (tr2))
9896 if (ctype_match (tr1, tr2))
9900 message ("Operands of %s are abstract type (%t): %s %s %s",
9901 lltok_unparse (op), tr1,
9902 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9909 message ("Operands of %s are abstract types (%t, %t): %s %s %s",
9910 lltok_unparse (op), tr1, tr2,
9911 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9915 else if (ctype_isRealAbstract (tr1))
9919 message ("Left operand of %s is abstract type (%t): %s %s %s",
9920 lltok_unparse (op), tr1,
9921 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9926 if (ctype_isRealAbstract (tr2))
9930 message ("Right operand of %s is abstract type (%t): %s %s %s",
9931 lltok_unparse (op), tr2,
9932 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9941 ** requies e1 and e2 and not error exprNode's.
9945 ** If e1 is a component of an abstract type, and e2 is mutable and client-visible,
9946 ** the rep of the abstract type is exposed.
9948 ** The order is very important:
9950 ** check rep expose (move into check transfer)
9956 ** This isn't really a sensible procedure, but the indententation
9957 ** was getting too deep.
9961 checkOneRepExpose (sRef ysr, sRef base,
9962 /*@notnull@*/ exprNode e1,
9963 /*@notnull@*/ exprNode e2, ctype ct,
9966 if (!(sRef_isOnly (ysr) || sRef_isKeep (ysr)
9967 || sRef_isOwned (ysr) || sRef_isExposed (ysr)))
9969 if (sRef_isAnyParam (base) && !sRef_isExposed (base))
9971 if (sRef_isIReference (ysr))
9973 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
9978 ("Assignment of mutable component of parameter %q "
9979 "to component of abstract "
9980 "type %s exposes rep: %s = %s",
9981 sRef_unparse (base),
9983 exprNode_unparse (e1), exprNode_unparse (e2)),
9991 ("Assignment of mutable component of parameter %q "
9992 "(through alias %q) to component of abstract "
9993 "type %s exposes rep: %s = %s",
9994 sRef_unparse (base),
9995 sRef_unparse (e2->sref),
9997 exprNode_unparse (e1), exprNode_unparse (e2)),
10003 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10007 message ("Assignment of mutable parameter %q "
10008 "to component of abstract type %s "
10009 "exposes rep: %s = %s",
10010 sRef_unparse (base),
10011 ctype_unparse (ct),
10012 exprNode_unparse (e1),
10013 exprNode_unparse (e2)),
10020 message ("Assignment of mutable parameter %q "
10021 "(through alias %q) to "
10022 "component of abstract type %s exposes "
10024 sRef_unparse (base),
10025 sRef_unparse (e2->sref),
10026 ctype_unparse (ct),
10027 exprNode_unparse (e1),
10028 exprNode_unparse (e2)),
10034 if (sRef_isFileOrGlobalScope (s2b))
10036 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10040 message ("Assignment of global %q "
10042 "abstract type %s exposes rep: %s = %s",
10043 sRef_unparse (base),
10044 ctype_unparse (ct),
10045 exprNode_unparse (e1), exprNode_unparse (e2)),
10052 message ("Assignment of global %q (through alias %q) "
10054 "abstract type %s exposes rep: %s = %s",
10055 sRef_unparse (base),
10056 sRef_unparse (e2->sref),
10057 ctype_unparse (ct),
10058 exprNode_unparse (e1), exprNode_unparse (e2)),
10066 doAssign (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2, bool isInit)
10068 if (ctype_isRealFunction (exprNode_getType (e1))
10069 && !ctype_isRealPointer (exprNode_getType (e1)))
10073 message ("Invalid left-hand side of assignment (function type %s): %s",
10074 ctype_unparse (exprNode_getType (e1)),
10075 exprNode_unparse (e1)),
10079 if (context_getFlag (FLG_ASSIGNEXPOSE) && ctype_isMutable (e2->typ))
10081 ctype t2 = exprNode_getType (e2);
10082 sRef sr = sRef_getRootBase (e1->sref);
10083 ctype ct = sRef_getType (sr);
10085 if (ctype_isAbstract (t2)
10086 && !(uentry_isMutableDatatype (usymtab_getTypeEntry (ctype_typeId (t2)))))
10088 /* it is immutable, okay to reference */
10089 goto donerepexpose;
10092 if (ctype_isAbstract (ct) && sRef_isIReference (e1->sref))
10094 sRef s2b = sRef_getRootBase (e2->sref);
10095 sRef s1 = e1->sref;
10096 sRef s1b = sRef_getRootBase (s1);
10099 aliases = usymtab_canAlias (e2->sref);
10101 if (!sRef_similar (s2b, s1b)
10102 && !sRef_isExposed (s1)
10103 && !(sRef_isOnly (s2b) || sRef_isKeep (s2b) || sRef_isExposed (s2b)))
10105 if (sRef_isAnyParam (s2b) && !sRef_isOnly (s2b)
10106 && !sRef_isOwned (s2b) && !sRef_isKeep (s2b)
10107 && !sRef_isExposed (s2b))
10109 if (sRef_isIReference (e2->sref))
10114 ("Assignment of mutable component of parameter %q "
10115 "to component of abstract type %s exposes rep: %s = %s",
10116 sRef_unparse (s2b),
10117 ctype_unparse (ct),
10118 exprNode_unparse (e1), exprNode_unparse (e2)),
10125 message ("Assignment of mutable parameter %q to "
10126 "component of abstract type %s exposes rep: %s = %s",
10127 sRef_unparse (s2b),
10128 ctype_unparse (ct),
10129 exprNode_unparse (e1), exprNode_unparse (e2)),
10134 if (sRef_isFileOrGlobalScope (s2b))
10138 message ("Assignment of global %q to component of "
10139 "abstract type %s exposes rep: %s = %s",
10140 sRef_unparse (s2b),
10141 ctype_unparse (ct),
10142 exprNode_unparse (e1), exprNode_unparse (e2)),
10146 sRefSet_realElements (aliases, ysr)
10148 sRef base = sRef_getRootBase (ysr);
10150 if (sRef_similar (ysr, s2b) || sRef_similar (s1b, base)
10151 || sRef_sameName (base, s1b))
10153 ; /* error already reported or same sref */
10157 checkOneRepExpose (ysr, base, e1, e2, ct, s2b);
10159 } end_sRefSet_realElements;
10161 sRefSet_free (aliases);
10168 ** function variables don't really work...
10171 if (!ctype_isFunction (ctype_realType (e2->typ)))
10175 DPRINTF (("Check init: %s / %s",
10176 exprNode_unparse (e1), exprNode_unparse (e2)));
10177 checkInitTransfer (e1, e2);
10181 checkAssignTransfer (e1, e2);
10186 sRef fref = e2->sref;
10188 sRef_setDefState (e1->sref, sRef_getDefState (fref), e1->loc);
10189 sRef_setNullState (e1->sref, sRef_getNullState (fref), e1->loc);
10191 /* Need to typecheck the annotation on the parameters */
10193 if (ctype_isRealFunction (e1->typ)) {
10194 uentryList e1p = ctype_argsFunction (ctype_realType (e1->typ));
10195 uentryList e2p = ctype_argsFunction (ctype_realType (e2->typ));
10197 if (!uentryList_isMissingParams (e1p)
10198 && !uentryList_isMissingParams (e2p)
10199 && uentryList_size (e1p) > 0) {
10200 if (uentryList_size (e1p) == uentryList_size (e2p)) {
10203 uentryList_elements (e1p, el1) {
10206 el2 = uentryList_getN (e2p, n);
10208 uentry_checkMatchParam (el1, el2, n, e2);
10209 } end_uentryList_elements;
10215 if (isInit && sRef_isFileOrGlobalScope (e1->sref))
10221 DPRINTF (("Update aliases: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
10222 updateAliases (e1, e2);
10227 checkMacroParen (exprNode e)
10229 if (exprNode_isError (e) || e->kind == XPR_CAST)
10235 if (sRef_isUnsafe (e->sref) && !exprNode_isInParens (e))
10239 message ("Macro parameter used without parentheses: %s",
10240 exprNode_unparse (e)),
10247 reflectNullTest (/*@notnull@*/ exprNode e, bool isnull)
10251 e->guards = guardSet_addTrueGuard (e->guards, e->sref);
10255 e->guards = guardSet_addFalseGuard (e->guards, e->sref);
10262 ** if e2 is a parameter or global derived location which
10263 ** can be modified (that is, e2 is a mutable abstract type,
10264 ** or a derived pointer), then e1 can alias e2.
10266 ** e1 can alias everything which e2 can alias.
10268 ** Also, if e1 is guarded, remove from guard sets!
10271 static void updateAliases (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2)
10273 if (!context_inProtectVars ())
10276 ** depends on types of e1 and e2
10279 sRef s1 = e1->sref;
10280 sRef s2 = e2->sref;
10281 ctype t1 = exprNode_getType (e1);
10283 /* handle pointer sRefs, record fields, arrays, etc... */
10285 if (!ctype_isRealSU (t1))
10287 DPRINTF (("Copying real! %s", ctype_unparse (t1)));
10288 sRef_copyRealDerivedComplete (s1, s2);
10293 ** Fields should alias
10296 DPRINTF (("Not COPYING!: %s", ctype_unparse (t1)));
10299 if (ctype_isMutable (t1) && sRef_isKnown (s1))
10301 usymtab_clearAlias (s1);
10302 usymtab_addMustAlias (s1, s2);
10303 DPRINTF (("Add must alias: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
10307 DPRINTF (("Not mutable: %s", ctype_unparse (t1)));
10310 if (sRef_possiblyNull (s1) && usymtab_isGuarded (s1))
10312 usymtab_unguard (s1);
10317 exprNode exprNode_updateLocation (/*@returned@*/ exprNode e, /*@temp@*/ fileloc loc)
10319 if (exprNode_isDefined (e))
10321 e->loc = fileloc_update (e->loc, loc);
10325 e = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
10331 static void checkUniqueParams (exprNode fcn,
10332 /*@notnull@*/ exprNode current,
10334 int paramno, uentry ucurrent)
10337 sRef thisref = exprNode_getSref (current);
10340 ** Check if any argument could match this argument.
10343 exprNodeList_elements (args, icurrent)
10347 if (iparamno != paramno)
10349 sRef sr = exprNode_getSref (icurrent);
10351 if (sRef_similarRelaxed (thisref, sr))
10353 if (!sRef_isConst (thisref) && !sRef_isConst (sr))
10358 ("Parameter %d (%s) to function %s is declared %s but "
10359 "is aliased by parameter %d (%s)",
10361 exprNode_unparse (current),
10362 exprNode_unparse (fcn),
10363 alkind_unparse (uentry_getAliasKind (ucurrent)),
10364 iparamno, exprNode_unparse (icurrent)),
10370 sRefSet aliases = usymtab_canAlias (sr);
10372 sRefSet_allElements (aliases, asr)
10374 if (ctype_isUnknown (sRef_getType (thisref)))
10376 sRef_setType (thisref, uentry_getType (ucurrent));
10379 if (sRef_similarRelaxed (thisref, asr))
10381 if (sRef_isExternal (asr))
10383 if (sRef_isLocalState (thisref))
10389 sRef base = sRef_getRootBase (asr);
10391 if (!sRef_similar (sRef_getBase (asr), thisref))
10393 if (sRef_isUnique (base) || sRef_isOnly (base)
10394 || sRef_isKept (base)
10395 || (sRef_isAddress (asr) && sRef_isLocalVar (base))
10396 || (sRef_isAddress (thisref)
10397 && sRef_isLocalVar (sRef_getRootBase (thisref))))
10399 ; /* okay, no error */
10404 (FLG_MAYALIASUNIQUE,
10406 ("Parameter %d (%s) to function %s is declared %s but "
10407 "may be aliased externally by parameter %d (%s)",
10409 exprNode_unparse (current),
10410 exprNode_unparse (fcn),
10411 alkind_unparse (uentry_getAliasKind (ucurrent)),
10412 iparamno, exprNode_unparse (icurrent)),
10423 ("Parameter %d (%s) to function %s is declared %s but "
10424 "is aliased externally by parameter %d (%s) through "
10427 exprNode_unparse (current),
10428 exprNode_unparse (fcn),
10429 alkind_unparse (uentry_getAliasKind (ucurrent)),
10430 iparamno, exprNode_unparse (icurrent),
10431 sRef_unparse (asr)),
10435 } end_sRefSet_allElements;
10436 sRefSet_free (aliases);
10439 } end_exprNodeList_elements;
10442 long exprNode_getLongValue (exprNode e) {
10445 if (exprNode_hasValue (e)
10446 && multiVal_isInt (exprNode_getValue (e)))
10448 value = multiVal_forceInt (exprNode_getValue (e));
10458 /*@observer@*/ fileloc exprNode_getfileloc (exprNode p_e)
10460 if (exprNode_isDefined (p_e) )
10461 return ( p_e->loc );
10463 return fileloc_undefined;
10466 /*@only@*/ fileloc exprNode_getNextSequencePoint (exprNode e)
10469 ** Returns the location of the sequence point following e.
10471 ** Only works for statements (for now).
10474 if (exprNode_isDefined (e) && e->kind == XPR_STMT) {
10475 lltok t = exprData_getUopTok (e->edata);
10476 return fileloc_copy(lltok_getLoc (t));
10478 //drl possible problem : warning fix
10479 // llcontbug (message ("Cannot get next sequence point: %s", exprNode_unparse (e)));
10480 return fileloc_undefined;
10484 exprNode exprNode_createNew(ctype c)
10488 ret = exprNode_createPlain (c);