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);
1521 /*@switchbreak@*/ break;
1523 case 'n': /* pointer to int, modified by call! */
1524 expecttype = ctype_combine (ctype_makePointer (ctype_int), modtype);
1526 uentry_setDefState (regArg, SS_ALLOCATED); /* corresponds to out */
1527 /*@switchbreak@*/ break;
1529 case 'm': /* in a syslog, it doesn't consume an argument */
1530 /* should check we're really doing syslog */
1532 /*@switchbreak@*/ break;
1536 expecttype = ctype_unknown;
1540 message ("Unrecognized format code: %s",
1541 cstring_fromChars (origcode)),
1542 fileloc_isDefined (formatloc)
1543 ? formatloc : g_currentloc);
1545 /*@switchbreak@*/ break;
1548 if (!(exprNode_matchArgType (expecttype, a)))
1550 if (ctype_isVoidPointer (expecttype)
1551 && ctype_isRealAbstract (a->typ)
1552 && (context_getFlag (FLG_ABSTVOIDP)))
1558 if (llgenformattypeerror
1559 (expecttype, exprNode_undefined,
1561 message ("Format argument %d to %q (%%%s) expects "
1564 uentry_getName (fcn),
1567 a->typ, exprNode_unparse (a)),
1570 if (fileloc_isDefined (formatloc)
1571 && context_getFlag (FLG_SHOWCOL))
1574 (cstring_makeLiteral
1575 ("Corresponding format code"),
1582 uentry_setType (regArg, expecttype);
1583 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
1585 if (ctype_equal (expecttype, ctype_string))
1587 exprNode_checkUse (a, sRef_makePointer (a->sref), a->loc);
1590 uentry_setType (regArg, ctype_unknown);
1591 uentry_fixupSref (regArg);
1595 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1606 cstring_free (codetext);
1611 voptgenerror (FLG_TYPE,
1612 message ("Format string for %q has %d arg%&, given %d",
1613 uentry_getName (fcn), i - argno, nargs - argno),
1619 /* no checking possible for compile-time unknown format strings */
1620 if (exprNode_isDefined (a))
1624 message ("Format string parameter to %s is not a compile-time constant: %s",
1625 exprNode_unparse (f),
1626 exprNode_unparse (a)),
1631 fileloc_free (formatloc);
1635 checkScanfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1636 exprNodeList args, exprNode ret, int argno)
1640 int nargs = exprNodeList_size (args);
1641 uentryList params = uentry_getParams (fcn);
1645 ** These should be ensured by checkSpecialFunction
1648 llassert (uentryList_size (params) == argno + 1);
1649 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1651 a = exprNodeList_getN (args, argno - 1);
1652 formatloc = fileloc_copy (exprNode_loc (a));
1654 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1655 && exprNode_knownStringValue (a))
1657 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1658 char *code = format;
1661 nargs = exprNodeList_size (args);
1663 while ((code = strchr (code, '%')) != NULL)
1665 char *origcode = code;
1666 char key = *(++code);
1667 cstring codetext = cstring_newEmpty ();
1668 ctype modtype = ctype_int;
1669 char modifier = '\0';
1670 bool modified = TRUE;
1671 bool ignore = FALSE;
1673 codetext = cstring_appendChar (codetext, key);
1674 fileloc_addColumn (formatloc, code - ocode);
1677 ** this is based on ANSI standard library description of fscanf
1678 ** (from ANSI standard X3.159-1989, 4.9.6.1)
1681 /* '*' suppresses assignment (does not need match argument) */
1686 codetext = cstring_appendChar (codetext, key);
1689 fileloc_incColumn (formatloc);
1692 /* ignore field width */
1693 while (isdigit ((int) key) != 0)
1696 codetext = cstring_appendChar (codetext, key);
1697 fileloc_incColumn (formatloc);
1702 modtype = ctype_sint; /* short */
1704 codetext = cstring_appendChar (codetext, key);
1705 fileloc_incColumn (formatloc);
1707 else if (key == 'l' || key == 'L')
1709 modtype = ctype_lint; /* long */
1713 codetext = cstring_appendChar (codetext, key);
1715 fileloc_incColumn (formatloc);
1717 if (key == 'l' || key == 'L') {
1718 modtype = ctype_llint; /* long long */
1720 codetext = cstring_appendChar (codetext, key);
1721 fileloc_incColumn (formatloc);
1729 /* now, key = type of conversion to apply */
1731 fileloc_incColumn (formatloc);
1745 message ("No argument corresponding to %q format "
1746 "code %d (%%%s): \"%s\"",
1747 uentry_getName (fcn),
1749 cstring_fromChars (format)),
1752 if (fileloc_isDefined (formatloc)
1753 && context_getFlag (FLG_SHOWCOL))
1756 (cstring_makeLiteral ("Corresponding format code"),
1764 a = exprNodeList_getN (args, i);
1767 if (!exprNode_isError (a))
1773 case '*': /* int argument for fieldwidth */
1774 expecttype = ctype_makePointer (ctype_int);
1775 *(--code) = '%'; /* convert it for next code */
1776 fileloc_subColumn (formatloc, 1);
1777 /*@switchbreak@*/ break;
1780 expecttype = ctype_makePointer (ctype_combine (ctype_uint, modtype));
1781 /*@switchbreak@*/ break;
1786 case 'X': /* unsigned int */
1787 expecttype = ctype_makePointer (ctype_combine (ctype_int, modtype));
1788 /*@switchbreak@*/ break;
1795 /* printf is double, scanf is float! */
1797 if (modifier == 'l')
1799 expecttype = ctype_makePointer (ctype_double);
1801 else if (modifier == 'L')
1803 expecttype = ctype_makePointer (ctype_ldouble);
1807 llassert (modifier == '\0');
1808 expecttype = ctype_makePointer (ctype_float);
1810 /*@switchbreak@*/ break;
1812 case 'c': /* int converted to char (check its a char?) */
1813 expecttype = ctype_makePointer (ctype_makeConj (ctype_char, ctype_uchar));
1814 /*@switchbreak@*/ break;
1816 case 's': /* string */
1817 expecttype = ctype_string;
1818 /*@switchbreak@*/ break;
1822 while (((key = *(++code)) != ']')
1825 codetext = cstring_appendChar (codetext, key);
1826 fileloc_incColumn (formatloc);
1832 (message ("Bad character set format: %s",
1833 cstring_fromChars (origcode)));
1836 expecttype = ctype_string;
1837 /*@switchbreak@*/ break;
1839 case 'p': /* pointer */
1840 expecttype = ctype_unknown;
1842 /*@switchbreak@*/ break;
1844 case 'n': /* pointer to int, modified by call! */
1845 expecttype = ctype_makePointer (ctype_int);
1846 /*@switchbreak@*/ break;
1849 expecttype = ctype_unknown;
1853 message ("Unrecognized format code: %s",
1854 cstring_fromChars (origcode)),
1855 fileloc_isDefined (formatloc)
1856 ? formatloc : g_currentloc);
1858 /*@switchbreak@*/ break;
1861 if (!(exprNode_matchArgType (expecttype, a)))
1863 if (ctype_isVoidPointer (expecttype)
1864 && ctype_isRealAbstract (a->typ)
1865 && (context_getFlag (FLG_ABSTVOIDP)))
1871 if (llgenformattypeerror
1872 (expecttype, exprNode_undefined,
1874 message ("Format argument %d to %q (%%%s) expects "
1877 uentry_getName (fcn),
1878 codetext, expecttype,
1879 a->typ, exprNode_unparse (a)),
1882 if (fileloc_isDefined (formatloc)
1883 && context_getFlag (FLG_SHOWCOL))
1886 (cstring_makeLiteral
1887 ("Corresponding format code"),
1894 uentry_setType (outArg, expecttype);
1895 checkOneArg (outArg, a, f, FALSE, i+1, nargs);
1896 uentry_setType (outArg, ctype_unknown);
1897 uentry_fixupSref (outArg);
1901 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1906 /* a->sref = defref; */
1913 cstring_free (codetext);
1918 voptgenerror (FLG_TYPE,
1919 message ("Format string for %q has %d arg%&, given %d",
1920 uentry_getName (fcn), i - argno, nargs - argno),
1926 /* no checking possible for compile-time unknown format strings */
1929 fileloc_free (formatloc);
1933 checkMessageArgs (/*@notnull@*/ /*@dependent@*/ exprNode f,
1936 /*@unused@*/ int argno)
1939 ** the last argument before the elips is the format string
1942 int nargs = exprNodeList_size (args);
1947 a = exprNodeList_getN (args, argno - 1);
1948 formatloc = fileloc_copy (exprNode_loc (a));
1950 if (ctype_isUnknown (cstringType)) {
1951 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
1953 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
1957 if (ctype_isUnknown (ctypeType)) {
1958 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
1960 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
1964 if (ctype_isUnknown (filelocType)) {
1965 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
1967 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
1971 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1972 && exprNode_knownStringValue (a))
1974 cstring format = multiVal_forceString (exprNode_getValue (a));
1975 char *code = cstring_toCharsSafe (format);
1978 nargs = exprNodeList_size (args);
1980 while ((code = strchr (code, '%')) != NULL)
1982 char *origcode = code;
1983 char key = *(++code);
1984 cstring codetext = cstring_newEmpty ();
1985 bool isOnly = FALSE;
1987 codetext = cstring_appendChar (codetext, key);
1989 fileloc_addColumn (formatloc, code - ocode);
1991 while (key >= '0' && key <= '9')
1994 codetext = cstring_appendChar (codetext, key);
1995 fileloc_incColumn (formatloc);
1999 fileloc_incColumn (formatloc);
2003 if (key == '&') /* plural marker */
2012 message ("Message missing format arg %d (%%%s): \"%s\"",
2013 i + 1, codetext, format),
2019 a = exprNodeList_getN (args, i);
2023 if (!exprNode_isError (a))
2027 /*@-loopswitchbreak@*/
2033 expecttype = ctype_char; break;
2035 expecttype = cstringType; break;
2037 expecttype = cstringType; isOnly = TRUE; break;
2039 expecttype = cstringType; isOnly = TRUE; break;
2040 case 'd': expecttype = ctype_int; break;
2041 case 'u': expecttype = ctype_uint; break;
2042 case 'w': expecttype = ctype_ulint; break;
2043 case 'f': expecttype = ctype_float; break;
2044 case 'b': expecttype = ctype_bool; break;
2045 case 't': expecttype = ctypeType; break;
2046 case 'l': expecttype = filelocType; break;
2047 case '&': /* a wee bit of a hack methinks */
2048 expecttype = ctype_int;
2050 case 'r': expecttype = ctype_bool; break;
2052 expecttype = ctype_unknown;
2055 message ("Unrecognized format code: %s",
2056 cstring_fromChars (origcode)),
2057 fileloc_isDefined (formatloc)
2058 ? formatloc : g_currentloc);
2061 /*@=loopswitchbreak@*/
2063 if (!(exprNode_matchArgType (expecttype, a)))
2065 if (ctype_isVoidPointer (expecttype)
2066 && ctype_isRealAbstract (a->typ)
2067 && (context_getFlag (FLG_ABSTVOIDP)))
2073 if (llgenformattypeerror
2074 (expecttype, exprNode_undefined,
2076 message ("Format argument %d to %q (%%%s) expects "
2079 uentry_getName (fcn),
2080 codetext, expecttype,
2081 a->typ, exprNode_unparse (a)),
2084 if (fileloc_isDefined (formatloc)
2085 && context_getFlag (FLG_SHOWCOL))
2088 (cstring_makeLiteral
2089 ("Corresponding format code"),
2096 if (ctype_equal (expecttype, cstringType))
2100 checkOneArg (csOnlyArg, a, f, FALSE, i+1, nargs);
2101 uentry_fixupSref (csOnlyArg);
2105 checkOneArg (csArg, a, f, FALSE, i+1, nargs);
2106 uentry_fixupSref (csArg);
2111 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
2112 uentry_fixupSref (regArg);
2118 cstring_free (codetext);
2123 voptgenerror (FLG_TYPE,
2124 message ("Format string for %q has %d arg%&, given %d",
2125 uentry_getName (fcn), i - argno, nargs -argno),
2131 /* no checking possible for compile-time unknown format strings */
2134 fileloc_free (formatloc);
2138 checkExpressionDefinedAux (/*@notnull@*/ exprNode e1,
2139 /*@notnull@*/ exprNode e2,
2145 bool hadUncon = FALSE;
2147 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e1->sref)) &&
2148 sRefSet_hasUnconstrained (sets2))
2151 (FLG_EVALORDERUNCON,
2153 ("Expression may have undefined behavior (%q used in right operand "
2154 "may set global variable %q used in left operand): %s %s %s",
2155 sRefSet_unparseUnconstrained (sets2),
2156 sRef_unparse (sRef_getRootBase (e1->sref)),
2157 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2161 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e2->sref)) &&
2162 sRefSet_hasUnconstrained (sets1))
2165 (FLG_EVALORDERUNCON,
2167 ("Expression has undefined behavior (%q used in left operand "
2168 "may set global variable %q used in right operand): %s %s %s",
2169 sRefSet_unparseUnconstrained (sets1),
2170 sRef_unparse (e2->sref),
2171 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2175 sRefSet_realElements (e1->uses, sr)
2177 if (sRef_isMeaningful (sr) && sRefSet_member (sets2, sr))
2182 ("Expression has undefined behavior (left operand uses %q, "
2183 "modified by right operand): %s %s %s",
2185 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2188 } end_sRefSet_realElements;
2190 sRefSet_realElements (sets1, sr)
2192 if (sRef_isMeaningful (sr))
2194 if (sRef_same (sr, e2->sref))
2199 ("Expression has undefined behavior (value of right operand "
2200 "modified by left operand): %s %s %s",
2201 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2204 else if (sRefSet_member (e2->uses, sr))
2209 ("Expression has undefined behavior (left operand modifies %q, "
2210 "used by right operand): %s %s %s",
2212 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2217 if (sRefSet_member (sets2, sr))
2219 if (sRef_isUnconstrained (sr))
2227 hadUncon = optgenerror
2228 (FLG_EVALORDERUNCON,
2230 ("Expression may have undefined behavior. Left operand "
2231 "calls %q; right operand calls %q. The unconstrained "
2232 "functions may modify global state used by "
2233 "the other operand): %s %s %s",
2234 sRefSet_unparseUnconstrained (sets1),
2235 sRefSet_unparseUnconstrained (sets2),
2236 exprNode_unparse (e1), lltok_unparse (op),
2237 exprNode_unparse (e2)),
2246 ("Expression has undefined behavior (both "
2247 "operands modify %q): %s %s %s",
2249 exprNode_unparse (e1),
2250 lltok_unparse (op), exprNode_unparse (e2)),
2256 } end_sRefSet_realElements;
2259 static void checkExpressionDefined (exprNode e1, exprNode e2, lltok op)
2261 bool hasError = FALSE;
2263 if (exprNode_isError (e1) || exprNode_isError (e2))
2268 if (sRefSet_member (e2->sets, e1->sref))
2270 if (e2->kind == XPR_CALL)
2276 hasError = optgenerror
2278 message ("Expression has undefined behavior "
2279 "(value of left operand is modified "
2280 "by right operand): %s %s %s",
2281 exprNode_unparse (e1), lltok_unparse (op),
2282 exprNode_unparse (e2)),
2287 if (context_getFlag (FLG_EVALORDERUNCON))
2289 if (sRefSet_member (e2->msets, e1->sref))
2291 if (e2->kind == XPR_CALL)
2297 hasError = optgenerror
2300 ("Expression has undefined behavior (value of left "
2301 "operand may be modified by right operand): %s %s %s",
2302 exprNode_unparse (e1), lltok_unparse (op),
2303 exprNode_unparse (e2)),
2311 checkExpressionDefinedAux (e1, e2, e1->sets, e2->sets, op, FLG_EVALORDER);
2313 if (context_maybeSet (FLG_EVALORDERUNCON))
2315 checkExpressionDefinedAux (e1, e2, e1->msets,
2316 e2->msets, op, FLG_EVALORDERUNCON);
2321 static void checkSequencing (exprNode p_f, exprNodeList p_args);
2324 checkArgsReal (uentry fcn, /*@dependent@*/ exprNode f, uentryList cl,
2325 exprNodeList args, bool isIter, exprNode ret)
2329 if (!exprNode_isError (f))
2331 if (!uentryList_isMissingParams (cl))
2333 int nargs = exprNodeList_size (args);
2334 int expectargs = uentryList_size (cl);
2338 if (expectargs == 0)
2346 message ("Iter %q invoked with %d args, "
2348 uentry_getName (fcn),
2356 message ("Function %s called with %d args, "
2358 exprNode_unparse (f), nargs),
2365 last = uentry_getType (uentryList_getN (cl, expectargs - 1));
2367 exprNodeList_reset (args);
2369 uentryList_elements (cl, current)
2371 ctype ct = uentry_getType (current);
2374 if (ctype_isElips (ct))
2377 ** do special checking for printf/scanf library functions
2379 ** this is kludgey code, just for handling the special case
2383 if (uentry_isPrintfLike (fcn))
2385 checkPrintfArgs (f, fcn, args, ret, i);
2388 else if (uentry_isScanfLike (fcn))
2390 checkScanfArgs (f, fcn, args, ret, i);
2393 else if (uentry_isMessageLike (fcn))
2395 checkMessageArgs (f, fcn, args, i);
2400 llassert (!uentry_isSpecialFunction (fcn));
2403 nargs = expectargs; /* avoid errors */
2408 if (i >= nargs) break;
2410 a = exprNodeList_current (args);
2411 exprNodeList_advance (args);
2415 if (exprNode_isError (a))
2422 probably necessary? I'm not sure about this one
2423 checkMacroParen (a);
2426 f->guards = guardSet_union (f->guards, a->guards);
2428 DPRINTF (("match arg: %s / %s", ctype_unparse (ct), ctype_unparse (a->typ)));
2430 if (!(exprNode_matchArgType (ct, a)))
2432 DPRINTF (("Args mismatch!"));
2434 if (ctype_isVoidPointer (ct)
2435 && (ctype_isPointer (a->typ)
2436 && (ctype_isRealAbstract (ctype_baseArrayPtr (a->typ)))))
2441 ("Pointer to abstract type (%t) used "
2443 "(arg %d to %q): %s",
2445 uentry_getName (fcn),
2446 exprNode_unparse (a)),
2454 (ct, exprNode_undefined,
2457 ("Iter %q expects arg %d to "
2458 "be %t gets %t: %s",
2459 uentry_getName (fcn),
2460 i, ct, a->typ, exprNode_unparse (a)),
2471 ("Function %q expects arg %d to be %t gets %t: %s",
2472 uentry_getName (fcn),
2473 i, ct, a->typ, exprNode_unparse (a)),
2476 DPRINTF (("Types: %s / %s",
2478 ctype_unparse (a->typ)));
2482 ** Clear null marker for abstract types.
2483 ** (It is not revealed, so suppress future messages.)
2486 if (ctype_isAbstract (a->typ))
2488 sRef_setNullUnknown (exprNode_getSref (a), a->loc);
2495 } end_uentryList_elements ;
2498 if (expectargs != nargs) /* note: not != since we may have ... */
2500 if (ctype_isElips (last))
2504 message ("Function %s called with %d args, expects at least %d",
2505 exprNode_unparse (f),
2506 nargs, expectargs - 1),
2515 message ("Iter %q invoked with %d args, expects %d",
2516 uentry_getName (fcn), nargs, expectargs),
2523 message ("Function %s called with %d args, expects %d",
2524 exprNode_unparse (f),
2537 ** Check for undefined code sequences in function arguments:
2539 ** one parameter sets something used by another parameter
2540 ** one parameter sets something set by another parameter
2544 checkSequencingOne (exprNode f, exprNodeList args,
2545 /*@notnull@*/ exprNode el, int argno)
2548 ** Do second loop, iff +undefunspec
2552 int numloops = context_maybeSet (FLG_EVALORDERUNCON) ? 2 : 1;
2554 for (checkloop = 0; checkloop < numloops; checkloop++)
2560 thissets = el->sets;
2564 llassert (checkloop == 1);
2565 thissets = el->msets;
2568 sRefSet_realElements (thissets, thisset)
2572 /*@access exprNodeList@*/
2573 for (j = 0; j < args->nelements; j++)
2575 exprNode jl = args->elements[j];
2576 int thisargno = j + 1;
2578 if (thisargno != argno && exprNode_isDefined (jl))
2580 sRefSet otheruses = jl->uses;
2582 if (sRef_isFileOrGlobalScope (sRef_getRootBase (jl->sref)) &&
2583 sRefSet_hasUnconstrained (thissets))
2586 (FLG_EVALORDERUNCON,
2589 ("%q used in argument %d may set "
2590 "global variable %q used by argument %d: %s(%q)",
2591 cstring_capitalizeFree (sRefSet_unparseUnconstrained (thissets)),
2594 sRef_unparse (sRef_getRootBase (jl->sref)),
2596 exprNode_unparse (f), exprNodeList_unparse (args)),
2600 if (sRefSet_member (otheruses, thisset))
2602 if (sRef_isUnconstrained (thisset))
2605 (FLG_EVALORDERUNCON,
2607 ("Unconstrained functions used in arguments %d (%q) "
2608 "and %d (%s) may modify "
2609 "or use global state in undefined way: %s(%q)",
2611 sRefSet_unparseUnconstrainedPlain (otheruses),
2613 sRef_unconstrainedName (thisset),
2614 exprNode_unparse (f),
2615 exprNodeList_unparse (args)),
2623 ("Argument %d modifies %q, used by argument %d "
2624 "(order of evaluation of actual parameters is "
2625 "undefined): %s(%q)",
2626 argno, sRef_unparse (thisset), thisargno,
2627 exprNode_unparse (f), exprNodeList_unparse (args)),
2633 sRefSet othersets = jl->sets;
2635 if (sRefSet_member (othersets, thisset))
2637 if (sRef_isUnconstrained (thisset))
2640 (FLG_EVALORDERUNCON,
2642 ("Unconstrained functions used in "
2643 "arguments %d (%q) and %d (%s) may modify "
2644 "or use global state in undefined way: %s(%q)",
2646 sRefSet_unparseUnconstrainedPlain (othersets),
2648 sRef_unconstrainedName (thisset),
2649 exprNode_unparse (f), exprNodeList_unparse (args)),
2657 ("Argument %d modifies %q, set by argument %d (order of"
2658 " evaluation of actual parameters is undefined): %s(%q)",
2659 argno, sRef_unparse (thisset), thisargno,
2660 exprNode_unparse (f), exprNodeList_unparse (args)),
2667 /*@noaccess exprNodeList@*/
2668 } end_sRefSet_realElements;
2673 checkSequencing (exprNode f, exprNodeList args)
2675 if (exprNodeList_size (args) > 1)
2680 /*@access exprNodeList*/
2682 for (i = 0; i < args->nelements; i++)
2684 el = args->elements[i];
2686 if (!exprNode_isError (el))
2688 checkSequencingOne (f, args, el, i + 1);
2691 /*@noaccess exprNodeList*/
2696 ** requires le = exprNode_getUentry (f)
2700 checkGlobMods (/*@notnull@*/ /*@dependent@*/ exprNode f,
2701 uentry le, exprNodeList args,
2702 /*@notnull@*/ exprNode ret, int specialArgs)
2704 bool isSpec = FALSE;
2705 bool hasMods = FALSE;
2707 globSet usesGlobs = globSet_undefined;
2708 sRefSet mods = sRefSet_undefined;
2709 bool freshMods = FALSE;
2710 uentryList params = uentryList_undefined;
2712 DPRINTF (("Check glob mods: %s", exprNode_unparse (ret)));
2715 ** check globals and modifies
2720 if (!uentry_isValid (le))
2722 ctype fr = ctype_realType (f->typ);
2724 if (ctype_isFunction (fr))
2726 params = ctype_argsFunction (fr);
2730 params = uentryList_missingParams;
2733 if (!context_getFlag (FLG_MODNOMODS)
2734 && !context_getFlag (FLG_GLOBUNSPEC))
2736 checkUnspecCall (f, params, args);
2742 fname = uentry_rawName (le);
2746 if (uentry_isFunction (le))
2748 params = uentry_getParams (le);
2749 mods = uentry_getMods (le);
2750 hasMods = uentry_hasMods (le);
2751 usesGlobs = uentry_getGlobs (le);
2752 isSpec = uentry_isSpecified (le);
2754 else /* not a function */
2756 ctype ct = ctype_realType (uentry_getType (le));
2758 llassertprint (uentry_isVar (le) && ctype_isFunction (ct),
2759 ("checkModGlobs: uentry not a function: %s",
2760 uentry_unparse (le)));
2762 params = ctype_argsFunction (ct);
2763 return; /*@32 ! remove this? */
2772 globSet_allElements (usesGlobs, el)
2774 if (sRef_isValid (el))
2776 if (sRef_isInternalState (el) || sRef_isSystemState (el))
2778 context_usedGlobal (el);
2779 exprNode_checkUse (f, el, f->loc);
2781 if (context_checkInternalUse ())
2783 if (!context_globAccess (el))
2785 if (sRef_isSystemState (el)
2786 && !context_getFlag (FLG_MODFILESYSTEM))
2795 ("Called procedure %s may access %q, but "
2796 "globals list does not include globals %s",
2797 exprNode_unparse (f),
2799 cstring_makeLiteralTemp (sRef_isInternalState (el)
2807 else if (sRef_isNothing (el) || sRef_isSpecState (el))
2813 uentry gle = sRef_getUentry (el);
2814 sRef sr = sRef_updateSref (el);
2816 if (sRef_isUndefGlob (el))
2818 sRef_setDefined (sr, f->loc);
2819 exprNode_checkSet (f, sr);
2827 if (sRef_isAllocated (el))
2829 exprNode_checkSet (f, sr);
2833 if (sRef_isStateUndefined (sr))
2838 ("%s %q used by function undefined before call: %s",
2839 sRef_getScopeName (sr),
2841 exprNode_unparse (f)),
2843 sRef_setDefined (sr, f->loc);
2845 exprNode_checkUse (f, sr, f->loc);
2848 checkGlobUse (gle, TRUE, f);
2851 if (sRef_isKilledGlob (el))
2853 sRef_kill (sr, f->loc);
2854 context_usedGlobal (sr);
2858 } end_globSet_allElements;
2864 if (context_hasMods () || context_getFlag (FLG_MODNOMODS))
2866 sRefSet smods = sRefSet_undefined;
2869 ** NEED to check for modifies anything
2873 ** check each sRef that called function modifies (ml), is
2879 sRefSet_allElements (mods, s) /* s is something which may be modified */
2881 DPRINTF (("Check modify: %s", sRef_unparse (s)));
2883 if (sRef_isKindSpecial (s))
2885 if (sRef_isSpecInternalState (s))
2887 if (context_getFlag (FLG_MODINTERNALSTRICT))
2889 exprNode_checkCallModifyVal (s, args, f, ret);
2893 sRefSet mmods = context_modList ();
2895 sRefSet_allElements (mmods, el)
2897 if (sRef_isInternalState (el))
2899 sRef_setModified (el);
2901 } end_sRefSet_allElements ;
2906 exprNode_checkCallModifyVal (s, args, f, ret);
2911 sRef rb = sRef_getRootBase (s);
2913 if (sRef_isFileOrGlobalScope (rb))
2915 context_usedGlobal (rb);
2918 if (sRef_isFileStatic (s)
2919 && !fileId_equal (fileloc_fileId (f->loc),
2920 fileloc_fileId (uentry_whereDefined (le))))
2922 smods = sRefSet_insert (smods, s);
2926 exprNode_checkCallModifyVal (s, args, f, ret);
2929 } end_sRefSet_allElements;
2934 ** Static elements in modifies set can have nasty consequences.
2935 ** (I think...have not been able to reproduce a possible bug.)
2938 if (!sRefSet_isDefined (smods))
2940 mods = sRefSet_newCopy (mods);
2943 sRefSet_allElements (smods, el)
2945 bool res = sRefSet_delete (mods, el);
2948 } end_sRefSet_allElements;
2950 sRefSet_free (smods);
2955 else if (sRefSet_isDefined (mods))
2956 { /* just check observers */
2959 sRefSet_allElements (mods, s) /* s is something which may be modified */
2961 sRef rb = sRef_getRootBase (s);
2965 if (sRef_isParam (rb))
2967 sRef b = sRef_fixBaseParam (s, args);
2969 if (sRef_isObserver (b))
2971 exprNode e = exprNodeList_nth (args, sRef_getParam (rb));
2975 message ("Function call may modify observer%q: %s",
2976 sRef_unparsePreOpt (b), exprNode_unparse (e)),
2979 sRef_showExpInfo (b);
2983 } end_sRefSet_allElements;
2987 if (!hasMods) /* no specified modifications */
2989 if (context_getFlag (FLG_MODOBSERVERUNCON))
2991 exprNodeList_elements (args, e)
2993 if (exprNode_isDefined (e))
2995 sRef s = exprNode_getSref (e);
2997 if (sRef_isObserver (s)
2998 && ctype_isMutable (sRef_getType (s)))
3001 (FLG_MODOBSERVERUNCON,
3003 ("Call to unconstrained function %s may modify observer%q: %s",
3004 exprNode_unparse (f),
3005 sRef_unparsePreOpt (s), exprNode_unparse (e)),
3008 sRef_showExpInfo (s);
3012 } end_exprNodeList_elements;
3017 checkAnyCall (f, fname, params, args, hasMods, mods, isSpec, specialArgs);
3019 ret->uses = sRefSet_union (ret->uses, f->uses);
3020 ret->sets = sRefSet_union (ret->sets, f->sets);
3021 ret->msets = sRefSet_union (ret->msets, f->msets);
3026 ** Spurious errors reported, because lclint can't tell
3027 ** mods must be fresh if freshMods is true.
3030 /*@i@*/ sRefSet_free (mods);
3036 void checkGlobUse (uentry glob, bool isCall, /*@notnull@*/ exprNode e)
3038 if (uentry_isVar (glob))
3040 if (context_inFunctionLike ())
3042 sRef sr = uentry_getSref (glob);
3044 context_usedGlobal (sr);
3046 if (context_checkGlobUse (glob))
3048 if (!context_globAccess (sr))
3054 message ("Called procedure %s may access %s %q",
3055 exprNode_unparse (e),
3056 sRef_unparseScope (sr),
3057 uentry_getName (glob)),
3064 message ("Undocumented use of %s %s",
3065 sRef_unparseScope (sr),
3066 exprNode_unparse (e)),
3075 llbug (message ("Global not variable: %q", uentry_unparse (glob)));
3080 reflectEnsuresClause (uentry le, exprNode f, exprNodeList args)
3082 DPRINTF (("Reflect ensures clause: %s(%s) / %s / %s",
3083 exprNode_unparse (f), exprNodeList_unparse (args),
3084 uentry_unparseFull (le),
3085 stateClauseList_unparse (uentry_getStateClauseList (le))));
3087 if (uentry_isValid (le) && uentry_isFunction (le))
3089 stateClauseList sclauses = uentry_getStateClauseList (le);
3091 if (stateClauseList_isDefined (sclauses))
3093 DPRINTF (("Reflect ensures: %s / %s / %s",
3094 uentry_unparse (le),
3095 exprNode_unparse (f), exprNodeList_unparse (args)));
3097 stateClauseList_elements (sclauses, cl)
3099 if (stateClause_hasEnsures (cl))
3101 /* Same in usymtab.c:1904 */
3102 if (stateClause_setsMetaState (cl))
3104 qual q = stateClause_getMetaQual (cl);
3105 annotationInfo ainfo = qual_getAnnotationInfo (q);
3106 metaStateInfo minfo = annotationInfo_getState (ainfo);
3107 cstring key = metaStateInfo_getName (minfo);
3108 int mvalue = annotationInfo_getValue (ainfo);
3110 sRefSet osrs = sRefSet_undefined;
3113 if (stateClause_isGlobal (cl))
3115 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3120 srs = stateClause_getRefs (cl);
3123 DPRINTF (("Reflect ensures clause: %s", stateClause_unparse (cl)));
3126 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
3128 sRefSet_elements (srs, sel)
3132 if (sRef_isResult (sRef_getRootBase (sel)))
3134 ; /*@i423 what do we do about results */
3138 s = sRef_fixBaseParam (sel, args);
3139 DPRINTF (("Reflecting state clause on: %s / %s",
3140 sRef_unparse (sel), sRef_unparse (s)));
3142 sRef_setMetaStateValueComplete (s, key, mvalue, exprNode_loc (f));
3144 } end_sRefSet_elements;
3146 sRefSet_free (osrs);
3150 sRefSet srs = stateClause_getRefs (cl);
3151 sRefModVal modf = stateClause_getEnsuresFunction (cl);
3152 int eparam = stateClause_getStateParameter (cl);
3154 DPRINTF (("Reflect after clause: %s / %s",
3155 stateClause_unparse (cl),
3156 sRefSet_unparse (srs)));
3158 sRefSet_elements (srs, sel)
3162 DPRINTF (("elements: %s", sRef_unparse (sel)));
3163 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3165 if (sRef_isResult (sRef_getRootBase (sel)))
3167 ; /*@i423 what do we do about results */
3171 s = sRef_fixBaseParam (sel, args);
3173 DPRINTF (("elements: %s", sRef_unparse (s)));
3174 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3176 DPRINTF (("Reflecting state clause on: %s / %s",
3177 sRef_unparse (sel), sRef_unparse (s)));
3179 modf (s, eparam, exprNode_loc (f));
3181 } end_sRefSet_elements;
3184 } end_stateClauseList_elements ;
3191 checkRequiresClause (uentry le, exprNode f, exprNodeList args)
3193 DPRINTF (("Check requires clause: %s(%s) / %s / %s",
3194 exprNode_unparse (f), exprNodeList_unparse (args),
3195 uentry_unparseFull (le),
3196 stateClauseList_unparse (uentry_getStateClauseList (le))));
3198 if (uentry_isValid (le) && uentry_isFunction (le))
3200 stateClauseList sclauses = uentry_getStateClauseList (le);
3202 if (stateClauseList_isDefined (sclauses))
3204 DPRINTF (("Check requires: %s / %s / %s",
3205 uentry_unparse (le),
3206 exprNode_unparse (f), exprNodeList_unparse (args)));
3208 stateClauseList_elements (sclauses, cl)
3210 DPRINTF (("Check clause: %s / %s",
3211 stateClause_unparse (cl),
3212 bool_unparse (stateClause_hasRequires (cl))));
3214 if (stateClause_hasRequires (cl))
3216 sRefSet osrs = sRefSet_undefined;
3219 if (stateClause_isGlobal (cl))
3221 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3226 srs = stateClause_getRefs (cl);
3229 DPRINTF (("Refs: %s", sRefSet_unparse (srs)));
3231 if (stateClause_setsMetaState (cl))
3233 qual q = stateClause_getMetaQual (cl);
3234 annotationInfo ainfo = qual_getAnnotationInfo (q);
3235 metaStateInfo minfo = annotationInfo_getState (ainfo);
3236 cstring key = metaStateInfo_getName (minfo);
3237 int mvalue = annotationInfo_getValue (ainfo);
3239 DPRINTF (("Requires meta state! %s = %d", key, mvalue));
3241 sRefSet_elements (srs, sel)
3243 sRef s = sRef_fixBaseParam (sel, args);
3245 if (sRef_isResult (sRef_getRootBase (sel)))
3247 ; /*@i423 what do we do about results */
3251 DPRINTF (("Checking state clause on: %s / %s / %s = %d",
3252 sRef_unparseFull (sel), sRef_unparseFull (s),
3255 if (!sRef_checkMetaStateValue (s, key, mvalue))
3257 DPRINTF (("HERE: %s", sRef_unparse (s)));
3261 ("Requires clause of called function %q not satisfied%q (state is %s): %q",
3262 uentry_getName (le),
3263 sRef_isGlobalMarker (s)
3265 : message (" by %q", sRef_unparse (s)),
3266 stateValue_unparseValue (sRef_getMetaStateValue (s, key),
3268 stateClause_unparse (cl)),
3271 sRef_showAliasInfo (s);
3275 DPRINTF (("Error supressed!"));
3276 DPRINTF (("Loc: %s", fileloc_unparse (exprNode_loc (f))));
3277 DPRINTF (("Context supress: %s",
3278 bool_unparse (context_suppressFlagMsg (FLG_STATETRANSFER, exprNode_loc (f)))));
3282 } end_sRefSet_elements;
3286 sRefModVal modf = stateClause_getRequiresBodyFunction (cl);
3287 int eparam = stateClause_getStateParameter (cl);
3289 DPRINTF (("Reflect after clause: %s / %s",
3290 stateClause_unparse (cl),
3291 sRefSet_unparse (srs)));
3293 sRefSet_elements (srs, sel)
3297 DPRINTF (("elements: %s", sRef_unparse (sel)));
3298 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3300 s = sRef_fixBaseParam (sel, args);
3302 DPRINTF (("elements: %s", sRef_unparse (s)));
3303 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3305 if (sRef_isResult (sRef_getRootBase (sel)))
3307 ; /*@i423 what do we do about results */
3311 DPRINTF (("Reflecting state clause on: %s / %s",
3312 sRef_unparse (sel), sRef_unparse (s)));
3314 modf (s, eparam, exprNode_loc (f));
3316 } end_sRefSet_elements;
3319 sRefSet_free (osrs);
3321 } end_stateClauseList_elements ;
3326 static /*@only@*/ exprNode
3327 functionCallSafe (/*@only@*/ /*@notnull@*/ exprNode f,
3328 ctype t, /*@keep@*/ exprNodeList args)
3330 /* requires f is a non-error exprNode, with type function */
3331 cstring fname = exprNode_unparse (f);
3332 uentry le = exprNode_getUentry (f);
3333 exprNode ret = exprNode_createPartialCopy (f);
3338 DPRINTF (("Call: %s %s",exprNode_unparse (f), exprNodeList_unparse (args)));
3340 ret->typ = ctype_getReturnType (t);
3341 ret->kind = XPR_CALL;
3343 ret->edata = exprData_makeCall (f, args);
3346 ** Order of these steps is very important!
3348 ** Must check for argument dependencies before messing up uses and sets.
3351 if (context_getFlag (FLG_EVALORDER))
3353 exprNodeList_elements (args, current)
3355 if (exprNode_isDefined (current))
3357 exprNode_addUse (current, current->sref);
3359 } end_exprNodeList_elements;
3361 if (context_maybeSet (FLG_EVALORDER) || context_maybeSet (FLG_EVALORDERUNCON))
3363 checkSequencing (f, args);
3366 exprNodeList_elements (args, current)
3368 if (exprNode_isDefined (current) && sRef_isMeaningful (current->sref))
3370 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3372 } end_exprNodeList_elements ;
3375 special = checkArgs (le, f, t, args, ret);
3376 checkGlobMods (f, le, args, ret, special);
3377 checkRequiresClause (le, f, args);
3380 DPRINTF (("Reflect: %s", uentry_unparseFull (le)));
3381 reflectEnsuresClause (le, f, args);
3384 if (uentry_isValid (le)
3385 && (uentry_isFunction (le)
3386 || (uentry_isVariable (le)
3387 && ctype_isFunction (uentry_getType (le)))))
3389 exitkind exk = uentry_getExitCode (le);
3391 /* f->typ is already set to the return type */
3393 ret->sref = uentry_returnedRef (le, args);
3394 DPRINTF (("Returned: %s / %s",
3395 uentry_unparseFull (le),
3396 sRef_unparseFull (ret->sref)));
3398 if (uentry_isFunction (le) && exprNodeList_size (args) >= 1)
3400 qual nullPred = uentry_nullPred (le);
3402 if (qual_isTrueNull (nullPred))
3404 exprNode arg = exprNodeList_head (args);
3406 if (exprNode_isDefined (arg))
3408 ret->guards = guardSet_addFalseGuard (ret->guards, arg->sref);
3411 else if (qual_isFalseNull (nullPred))
3413 exprNode arg = exprNodeList_head (args);
3415 if (exprNode_isDefined (arg))
3417 ret->guards = guardSet_addTrueGuard (ret->guards, arg->sref);
3422 llassert (qual_isUnknown (nullPred));
3426 if (exitkind_isConditionalExit (exk))
3430 ** if (arg0) then { exit! } else { ; }
3432 ** if (arg0) then { ; } else { exit! }
3437 llassert (!exprNodeList_isEmpty (args));
3438 firstArg = exprNodeList_head (args);
3440 if (exprNode_isDefined (firstArg)
3441 && !guardSet_isEmpty (firstArg->guards))
3443 usymtab_trueBranch (guardSet_undefined);
3444 usymtab_altBranch (guardSet_undefined);
3446 if (exitkind_isTrueExit (exk))
3448 usymtab_popBranches (firstArg,
3449 exprNode_makeMustExit (),
3451 TRUE, TRUEEXITCLAUSE);
3455 usymtab_popBranches (firstArg,
3457 exprNode_makeMustExit (),
3458 TRUE, FALSEEXITCLAUSE);
3462 ret->exitCode = XK_MAYEXIT;
3464 else if (exitkind_mustExit (exk))
3466 ret->exitCode = XK_MUSTEXIT;
3468 else if (exitkind_couldExit (exk))
3470 ret->exitCode = XK_MAYEXIT;
3477 if (cstring_equalLit (fname, "exit"))
3479 if (exprNodeList_size (args) == 1)
3481 exprNode arg = exprNodeList_head (args);
3483 if (exprNode_isDefined (arg) && exprNode_knownIntValue (arg))
3485 long int val = multiVal_forceInt (exprNode_getValue (arg));
3492 ("Argument to exit has implementation defined behavior: %s",
3493 exprNode_unparse (arg)),
3494 exprNode_loc (arg));
3503 exprNode_checkSetAny (ret, uentry_rawName (le));
3510 ** this is yucky! should keep the uentry as part of exprNode!
3513 /*@observer@*/ uentry
3514 exprNode_getUentry (exprNode e)
3516 if (exprNode_isError (e))
3518 return uentry_undefined;
3522 cstring s = exprNode_rootVarName (e);
3523 uentry ue = usymtab_lookupSafe (s);
3530 exprNode_makeInitBlock (lltok brace, /*@only@*/ exprNodeList inits)
3532 exprNode ret = exprNode_createPlain (ctype_unknown);
3534 ret->kind = XPR_INITBLOCK;
3535 ret->edata = exprData_makeCall (exprNode_undefined, inits);
3536 ret->loc = fileloc_update (ret->loc, lltok_getLoc (brace));
3542 exprNode_functionCall (/*@only@*/ exprNode f, /*@only@*/ exprNodeList args)
3548 if (exprNode_isUndefined (f))
3551 exprNodeList_free (args);
3552 return exprNode_undefined;
3555 t = exprNode_getType (f);
3557 if (sRef_isLocalVar (f->sref))
3559 exprNode_checkUse (f, f->sref, f->loc);
3561 if (sRef_possiblyNull (f->sref))
3563 if (!usymtab_isGuarded (f->sref))
3565 if (optgenerror (FLG_NULLDEREF,
3566 message ("Function call using %s pointer %q",
3567 sRef_nullMessage (f->sref),
3568 sRef_unparse (f->sref)),
3571 sRef_showNullInfo (f->sref);
3572 sRef_setNullError (f->sref);
3580 if (ctype_isRealFunction (t))
3582 exprNode ret = functionCallSafe (f, t, args);
3586 else if (ctype_isUnknown (t))
3588 exprNode ret = exprNode_createPartialCopy (f);
3594 exprNodeList_elements (args, current)
3596 if (exprNode_isDefined (current))
3598 exprNode_checkUse (ret, current->sref, ret->loc);
3601 ** also, anything derivable from current->sref may be used
3604 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3605 exprNode_mergeUSs (ret, current);
3607 } end_exprNodeList_elements;
3609 ret->edata = exprData_makeCall (f, args);
3610 ret->kind = XPR_CALL;
3612 tstring = cstring_copy (exprNode_unparse (f));
3614 cstring_markOwned (tstring);
3615 exprNode_checkSetAny (ret, tstring);
3621 voptgenerror (FLG_TYPE,
3622 message ("Call to non-function (type %t): %s", t,
3623 exprNode_unparse (f)),
3626 exprNodeList_free (args);
3628 return (exprNode_makeError ());
3633 exprNode_fieldAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
3634 /*@only@*/ cstring f)
3636 exprNode ret = exprNode_createPartialCopy (s);
3638 ret->kind = XPR_FACCESS;
3640 if (exprNode_isError (s))
3642 ret->edata = exprData_makeField (s, f);
3647 ctype t = exprNode_getType (s);
3648 ctype tr = ctype_realType (t);
3650 checkMacroParen (s);
3652 ret->edata = exprData_makeField (s, f);
3654 if (ctype_isStructorUnion (tr))
3656 uentry tf = uentryList_lookupField (ctype_getFields (tr), f);
3658 if (uentry_isUndefined (tf))
3660 voptgenerror (FLG_TYPE,
3661 message ("Access non-existent field %s of %t: %s", f, t,
3662 exprNode_unparse (ret)),
3664 /*! cstring_free (f); */ /* evans 2001-03-25 self-detect */
3669 uentry_setUsed (tf, exprNode_loc (ret));
3671 ret->typ = uentry_getType (tf);
3672 checkSafeUse (ret, s->sref);
3674 ret->sref = sRef_makeField (s->sref, uentry_rawName (tf));
3675 /*!? exprNode_free (s); */ /* evans 2001-03-25 self-detect */
3679 else /* isStructorUnion */
3681 if (ctype_isRealAbstract (tr))
3685 message ("Access field of abstract type (%t): %s.%s",
3686 t, exprNode_unparse (s), f),
3688 ret->typ = ctype_unknown;
3692 if (ctype_isKnown (tr))
3697 ("Access field of non-struct or union (%t): %s.%s",
3698 t, exprNode_unparse (s), f),
3701 ret->typ = ctype_unknown;
3705 cstring sn = cstring_copy (f);
3707 checkSafeUse (ret, s->sref);
3708 cstring_markOwned (sn);
3709 ret->sref = sRef_makeField (s->sref, sn);
3721 exprNode_fieldAccess (/*@only@*/ exprNode s, /*@only@*/ lltok dot,
3722 /*@only@*/ cstring f)
3724 exprNode res = exprNode_fieldAccessAux (s, lltok_getLoc (dot), f);
3725 lltok_release (dot);
3730 exprNode_addParens (/*@only@*/ lltok lpar, /*@only@*/ exprNode e)
3732 exprNode ret = exprNode_createPartialCopy (e);
3734 ret->loc = fileloc_update (ret->loc, lltok_getLoc (lpar));
3735 ret->kind = XPR_PARENS;
3736 ret->edata = exprData_makeUop (e, lpar);
3738 if (!exprNode_isError (e))
3740 ret->exitCode = e->exitCode;
3741 ret->canBreak = e->canBreak;
3742 ret->mustBreak = e->mustBreak;
3743 ret->isJumpPoint = e->isJumpPoint;
3744 ret->sref = e->sref;
3751 exprNode_arrowAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
3752 /*@only@*/ cstring f)
3754 exprNode ret = exprNode_createPartialCopy (s);
3756 ret->edata = exprData_makeField (s, f);
3757 ret->kind = XPR_ARROW;
3759 if (exprNode_isError (s))
3765 ctype t = exprNode_getType (s);
3766 ctype tr = ctype_realType (t);
3768 checkMacroParen (s);
3770 (void) ctype_fixArrayPtr (tr); /* REWRITE THIS */
3772 if (ctype_isRealPointer (tr))
3774 ctype b = ctype_realType (ctype_baseArrayPtr (tr));
3776 if (ctype_isStructorUnion (b))
3778 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
3780 if (sRef_isKnown (s->sref) && sRef_possiblyNull (s->sref))
3782 if (!usymtab_isGuarded (s->sref) && !context_inProtectVars ())
3786 message ("Arrow access from %s pointer%q: %s",
3787 sRef_nullMessage (s->sref),
3788 sRef_unparsePreOpt (s->sref),
3789 exprNode_unparse (ret)),
3792 sRef_showNullInfo (s->sref);
3793 sRef_setNullError (s->sref);
3798 if (uentry_isUndefined (fentry))
3802 message ("Access non-existent field %s of %t: %s",
3803 f, t, exprNode_unparse (ret)),
3805 ret->typ = ctype_unknown;
3811 ** was safeUse: shouldn't be safe!
3814 ** rec must be defined,
3815 ** *rec must be allocated
3816 ** rec->field need only be defined it if is an rvalue
3819 uentry_setUsed (fentry, exprNode_loc (ret));
3820 ret->typ = uentry_getType (fentry);
3822 exprNode_checkUse (ret, s->sref, s->loc);
3824 /* exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc); */
3825 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
3829 else /* Pointer to something that is not a struct or union*/
3831 if (ctype_isRealAbstract (tr))
3833 ctype xrt = ctype_forceRealType (tr);
3837 message ("Arrow access field of abstract type (%t): %s->%s",
3838 t, exprNode_unparse (s), f),
3842 ** Set the state correctly, as if the abstraction is broken.
3845 if (ctype_isRealPointer (xrt) &&
3846 (b = ctype_realType (ctype_baseArrayPtr (xrt)),
3847 ctype_isStructorUnion (b)))
3849 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
3850 ret->typ = uentry_getType (fentry);
3851 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
3855 ret->typ = ctype_unknown;
3856 ret->sref = sRef_undefined;
3859 else /* not a struct, union or abstract */
3861 if (ctype_isUnknown (tr)) {
3862 cstring sn = cstring_copy (f);
3864 DPRINTF (("Here: %s", exprNode_unparse (s)));
3866 exprNode_checkUse (ret, s->sref, s->loc);
3867 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
3869 cstring_markOwned (sn);
3870 ret->sref = sRef_makeArrow (s->sref, sn);
3872 ret->kind = XPR_ARROW;
3877 message ("Arrow access field of non-struct or union "
3878 "pointer (%t): %s->%s",
3879 t, exprNode_unparse (s), f),
3882 ret->typ = ctype_unknown;
3883 ret->sref = sRef_undefined;
3888 else /* its not a pointer */
3890 if (!ctype_isUnknown (tr))
3894 message ("Arrow access of non-pointer (%t): %s->%s",
3895 t, exprNode_unparse (s), f),
3898 ret->typ = ctype_unknown;
3899 ret->sref = sRef_undefined;
3903 cstring sn = cstring_copy (f);
3905 DPRINTF (("Here: %s", exprNode_unparse (s)));
3907 exprNode_checkUse (ret, s->sref, s->loc);
3908 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
3910 cstring_markOwned (sn);
3911 ret->sref = sRef_makeArrow (s->sref, sn);
3913 ret->kind = XPR_ARROW;
3924 exprNode_arrowAccess (/*@only@*/ exprNode s,
3925 /*@only@*/ lltok arrow,
3926 /*@only@*/ cstring f)
3928 exprNode res = exprNode_arrowAccessAux (s, lltok_getLoc (arrow), f);
3929 lltok_release (arrow);
3934 ** only postOp's in C: i++ and i--
3938 exprNode_postOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
3940 /* check modification also */
3941 /* cstring opname = lltok_unparse (op);*/
3943 exprNode ret = exprNode_createPartialCopy (e);
3945 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
3946 ret->kind = XPR_POSTOP;
3947 ret->edata = exprData_makeUop (e, op);
3949 if (!exprNode_isDefined (e))
3954 checkMacroParen (e);
3956 exprNode_checkUse (ret, e->sref, e->loc);
3957 exprNode_checkSet (ret, e->sref);
3959 t = exprNode_getType (e);
3961 if (sRef_isUnsafe (e->sref))
3963 voptgenerror (FLG_MACROPARAMS,
3964 message ("Operand of %s is macro parameter (non-functional): %s%s",
3965 lltok_unparse (op), exprNode_unparse (e), lltok_unparse (op)),
3967 sRef_makeSafe (e->sref);
3968 sRef_makeSafe (ret->sref);
3971 if (ctype_isForceRealNumeric (&t) || ctype_isRealAP (t))
3977 if (ctype_isRealAbstract (t))
3981 message ("Operand of %s is abstract type (%t): %s",
3982 lltok_unparse (op), t, exprNode_unparse (e)),
3989 message ("Operand of %s is non-numeric (%t): %s",
3990 lltok_unparse (op), t, exprNode_unparse (e)),
3993 ret->typ = ctype_unknown;
3996 /* if (ctype_isZero (t)) e->typ = ctype_int; */
3998 exprNode_checkModify (e, ret);
4000 /* added 7/11/2000 D.L */
4002 /*DRL 6/8/01 I decided to disable all LCLint Warning here since the code
4003 probably needs a rewrite any way */
4007 // updateEnvironmentForPostOp (e);
4009 /* start modifications */
4010 /* added by Seejo on 4/16/2000 */
4012 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4014 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4016 ret->sref = sRef_copy (e->sref);
4019 if (lltok_getTok (op) == INC_OP) {
4020 if (sRef_getSize(e->sref) > 0) {
4022 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4024 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4025 /* Assumption: there is only 1 \0 in the buffer */
4026 /* This will not be correct if there are 2 \0's in the buffer */
4027 sRef_setNotNullTerminatedState(ret->sref);
4028 sRef_resetLen(ret->sref);
4030 sRef_setNullTerminatedState(ret->sref);
4031 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4033 if (sRef_isNullTerminated (ret->sref))
4034 printf ("ret->sref is Null Terminated\n");
4035 else if (sRef_isPossiblyNullTerminated (ret->sref))
4036 printf ("ret->sref is Possibly Null Terminated\n");
4037 else if (sRef_isNotNullTerminated (ret->sref))
4038 printf ("ret->sref is Not Null Terminated\n");
4043 if (lltok_getTok (op) == DEC_OP) {
4044 if (sRef_getSize(e->sref) >= 0) {
4045 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4046 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4051 /* end modifications */
4057 exprNode_preOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4059 bool checkMod = FALSE;
4061 int opid = lltok_getTok (op);
4062 exprNode ret = exprNode_createSemiCopy (e);
4064 exprNode_copySets (ret, e);
4066 multiVal_free (ret->val);
4067 ret->val = multiVal_undefined;
4068 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4069 ret->kind = XPR_PREOP;
4070 ret->edata = exprData_makeUop (e, op);
4072 if (exprNode_isError (e))
4077 checkMacroParen (e);
4079 te = exprNode_getType (e);
4080 tr = ctype_realType (te);
4082 if (opid != TAMPERSAND)
4084 exprNode_checkUse (ret, e->sref, e->loc);
4086 if (ctype_isRealAbstract (tr)
4087 && (!(ctype_isRealBool (te) && (opid == TEXCL))))
4089 if (optgenerror (FLG_ABSTRACT,
4090 message ("Operand of %s is abstract type (%t): %s",
4091 lltok_unparse (op), tr,
4092 exprNode_unparse (ret)),
4095 tr = te = ctype_unknown;
4096 ret->typ = ctype_unknown;
4097 sRef_setNullError (e->sref);
4105 case DEC_OP: /* should also check modification! */
4106 if (sRef_isMacroParamRef (e->sref))
4110 message ("Operand of %s is macro parameter (non-functional): %s",
4111 lltok_unparse (op), exprNode_unparse (ret)),
4116 exprNode_checkSet (ret, e->sref);
4119 if (ctype_isForceRealNumeric (&tr) || ctype_isRealAP (tr))
4124 if (context_msgStrictOps ())
4128 message ("Operand of %s is non-numeric (%t): %s",
4129 lltok_unparse (op), te, exprNode_unparse (ret)),
4132 ret->typ = ctype_int;
4135 /* start modifications */
4136 /* added by Seejo on 4/16/2000 */
4138 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4140 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4142 ret->sref = sRef_copy (e->sref);
4145 if (lltok_getTok (op) == INC_OP) {
4146 if (sRef_getSize(e->sref) > 0) {
4148 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4150 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4151 /* Assumption: there is only 1 \0 in the buffer */
4152 /* This will not be correct if there are 2 \0's in the buffer */
4153 sRef_setNotNullTerminatedState(ret->sref);
4154 sRef_resetLen (ret->sref);
4156 sRef_setNullTerminatedState(ret->sref);
4157 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4163 if (lltok_getTok (op) == DEC_OP) {
4164 if (sRef_getSize(e->sref) >= 0) {
4165 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4166 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4171 /* end modifications */
4178 if (ctype_isForceRealNumeric (&tr))
4182 ret->val = multiVal_invert (exprNode_getValue (e));
4186 ret->val = multiVal_copy (exprNode_getValue (e));
4191 if (context_msgStrictOps ())
4195 message ("Operand of %s is non-numeric (%t): %s",
4196 lltok_unparse (op), te, exprNode_unparse (ret)),
4200 ret->typ = ctype_int;
4204 case TEXCL: /* maybe this should be restricted */
4205 guardSet_flip (ret->guards);
4207 if (ctype_isRealBool (te) || ctype_isUnknown (te))
4213 if (ctype_isRealPointer (tr))
4215 if (sRef_isKnown (e->sref))
4217 ret->guards = guardSet_addFalseGuard (ret->guards, e->sref);
4221 (FLG_BOOLOPS, FLG_PTRNEGATE,
4222 message ("Operand of %s is non-boolean (%t): %s",
4223 lltok_unparse (op), te, exprNode_unparse (ret)),
4230 message ("Operand of %s is non-boolean (%t): %s",
4231 lltok_unparse (op), te, exprNode_unparse (ret)),
4235 ret->typ = ctype_bool;
4240 if (ctype_isForceRealInt (&tr))
4245 if (context_msgStrictOps ())
4249 message ("Operand of %s is non-integer (%t): %s",
4250 lltok_unparse (op), te, exprNode_unparse (ret)),
4254 if (ctype_isInt (e->typ))
4260 ret->typ = ctype_int;
4266 ret->typ = ctype_makePointer (e->typ);
4268 if (sRef_isKnown (e->sref))
4270 ret->sref = sRef_makeAddress (e->sref);
4277 if (ctype_isAP (tr))
4279 ret->typ = ctype_baseArrayPtr (e->typ);
4283 if (ctype_isKnown (te))
4285 if (ctype_isFunction (te))
4291 message ("Dereference of function type (%t): %s",
4292 te, exprNode_unparse (ret)),
4297 voptgenerror (FLG_TYPE,
4298 message ("Dereference of non-pointer (%t): %s",
4299 te, exprNode_unparse (ret)),
4301 ret->typ = ctype_unknown;
4306 ret->typ = ctype_unknown;
4311 if (sRef_isKnown (e->sref))
4313 if (sRef_possiblyNull (e->sref))
4315 if (!usymtab_isGuarded (e->sref) && !context_inProtectVars ())
4319 message ("Dereference of %s pointer %q: %s",
4320 sRef_nullMessage (e->sref),
4321 sRef_unparse (e->sref),
4322 exprNode_unparse (ret)),
4325 sRef_showNullInfo (e->sref);
4326 sRef_setNotNull (e->sref, e->loc); /* suppress future messages */
4331 ret->sref = sRef_makePointer (e->sref);
4336 llbug (message ("exprNode_preOp: unhandled op: %s", lltok_unparse (op)));
4341 exprNode_checkModify (e, ret);
4348 ** any reason to disallow sizeof (abstract type) ?
4352 ** used by both sizeof
4356 ctype sizeof_resultType (void)
4358 static ctype sizet = ctype_unknown;
4360 if (ctype_isUnknown (sizet))
4362 if (usymtab_existsType (cstring_makeLiteralTemp ("size_t")))
4364 sizet = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("size_t")));
4368 sizet = ctype_ulint;
4375 exprNode_sizeofType (/*@only@*/ qtype qt)
4377 exprNode ret = exprNode_create (sizeof_resultType ());
4378 ctype ct = qtype_getType (qt);
4380 ret->kind = XPR_SIZEOFT;
4381 ret->edata = exprData_makeSizeofType (qt);
4383 voptgenerror (FLG_SIZEOFTYPE,
4384 message ("Parameter to sizeof is type %s: %s",
4386 exprNode_unparse (ret)),
4393 exprNode_alignofType (/*@only@*/ qtype qt)
4395 exprNode ret = exprNode_create (sizeof_resultType ());
4396 ctype ct = qtype_getType (qt);
4398 ret->kind = XPR_ALIGNOFT;
4399 ret->edata = exprData_makeSizeofType (qt);
4401 voptgenerror (FLG_SIZEOFTYPE,
4402 message ("Parameter to alignof is type %s: %s",
4404 exprNode_unparse (ret)),
4410 exprNode exprNode_offsetof (qtype qt, cstringList s)
4412 exprNode ret = exprNode_create (sizeof_resultType ());
4413 ctype ct = qtype_getType (qt);
4415 ret->kind = XPR_OFFSETOF;
4416 ret->edata = exprData_makeOffsetof (qt, s);
4418 if (!ctype_isRealSU (ct))
4420 voptgenerror (FLG_TYPE,
4421 message ("First parameter to offsetof is not a "
4422 "struct or union type (type %s): %s",
4424 exprNode_unparse (ret)),
4431 cstringList_elements (s, el) {
4435 if (ctype_isUndefined (lt))
4439 else if (!ctype_isRealSU (lt))
4441 voptgenerror (FLG_TYPE,
4442 message ("Inner offsetof type is not a "
4443 "struct or union type (type %s before field %s): %s",
4444 ctype_unparse (lt), el,
4445 exprNode_unparse (ret)),
4451 fields = ctype_getFields (ctype_realType (lt));
4452 fld = uentryList_lookupField (fields, el);
4453 DPRINTF (("Try: %s / %s", ctype_unparse (lt), el));
4455 if (uentry_isUndefined (fld))
4457 if (ctype_equal (lt, ct)) {
4458 voptgenerror (FLG_TYPE,
4459 message ("Field %s in offsetof is not the "
4460 "name of a field of %s: %s",
4463 exprNode_unparse (ret)),
4466 voptgenerror (FLG_TYPE,
4467 message ("Deep field %s in offsetof is not the "
4468 "name of a field of %s: %s",
4471 exprNode_unparse (ret)),
4477 lt = uentry_getType (fld);
4480 } end_cstringList_elements;
4482 /* Should report error if its a bit field - behavior is undefined! */
4489 exprNode_sizeofExpr (/*@only@*/ exprNode e)
4493 if (exprNode_isUndefined (e))
4495 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4496 ret->edata = exprData_makeSingle (e);
4497 ret->typ = sizeof_resultType ();
4498 ret->kind = XPR_SIZEOF;
4502 uentry u = exprNode_getUentry (e);
4504 ret = exprNode_createPartialCopy (e);
4505 ret->edata = exprData_makeSingle (e);
4507 ret->typ = sizeof_resultType ();
4508 ret->kind = XPR_SIZEOF;
4510 if (uentry_isValid (u)
4511 && uentry_isRefParam (u)
4512 && ctype_isRealArray (uentry_getType (u)))
4515 (FLG_SIZEOFFORMALARRAY,
4516 message ("Parameter to sizeof is an array-type function parameter: %s",
4517 exprNode_unparse (ret)),
4523 ** sizeof (x) doesn't "really" use x
4530 exprNode_alignofExpr (/*@only@*/ exprNode e)
4534 if (exprNode_isUndefined (e))
4536 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4540 ret = exprNode_createPartialCopy (e);
4543 ret->edata = exprData_makeSingle (e);
4544 ret->typ = sizeof_resultType ();
4545 ret->kind = XPR_ALIGNOF;
4548 ** sizeof (x) doesn't "really" use x
4555 exprNode_cast (/*@only@*/ lltok tok, /*@only@*/ exprNode e, /*@only@*/ qtype q)
4561 if (exprNode_isError (e))
4564 lltok_release (tok);
4565 return exprNode_undefined;
4568 checkMacroParen (e);
4570 c = qtype_getType (q);
4571 t = exprNode_getType (e);
4573 ret = exprNode_createPartialCopy (e);
4575 ret->loc = fileloc_update (ret->loc, lltok_getLoc (tok));
4577 ret->kind = XPR_CAST;
4578 ret->edata = exprData_makeCast (tok, e, q);
4580 if (ctype_isRealSU (ctype_getBaseType (sRef_getType (e->sref))))
4583 ** This is a bit of a hack to avoid a problem
4584 ** when the code does,
4585 ** (some other struct) x
4590 ret->sref = sRef_copy (e->sref);
4591 usymtab_addForceMustAlias (ret->sref, e->sref);
4592 sRef_setTypeFull (ret->sref, c);
4593 DPRINTF (("Cast: %s -> %s", sRef_unparseFull (e->sref),
4594 sRef_unparseFull (ret->sref)));
4598 ret->sref = e->sref;
4599 sRef_setTypeFull (ret->sref, c);
4600 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
4607 ** void * <-> abstract * (if FLG_ABSTVOIDP)
4608 ** abstract * <-> void * (if FLG_ABSTVOIDP)
4611 if (ctype_isVoid (c)) /* cast to void is always okay --- discard value */
4615 else if (ctype_isRealAP (c)) /* casting to array or pointer */
4617 ctype bc = ctype_getBaseType (c);
4618 ctype bt = ctype_getBaseType (t);
4619 ctype rt = ctype_realType (t);
4621 if (ctype_isFunction (ctype_baseArrayPtr (ctype_realType (c)))
4622 && (ctype_isArrayPtr (rt)
4623 && !ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4627 message ("Cast from function pointer type (%t) to "
4628 "non-function pointer (%t): %s",
4629 c, t, exprNode_unparse (ret)),
4633 if (!ctype_isFunction (ctype_baseArrayPtr (c))
4634 && (ctype_isArrayPtr (rt)
4635 && ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4639 message ("Cast from non-function pointer type (%t) to "
4640 "function pointer (%t): %s",
4641 c, t, exprNode_unparse (ret)),
4645 if (exprNode_isZero (e) && context_getFlag (FLG_ZEROPTR) &&
4646 !(ctype_isRealAbstract (bc)
4647 && context_hasAccess (ctype_typeId (bc))))
4649 ; /* okay to cast zero */
4653 if (ctype_isRealAbstract (bc)
4654 && !context_hasAccess (ctype_typeId (bc)))
4656 if (ctype_isVoidPointer (t) || ctype_isUnknown (t))
4660 message ("Cast to underlying abstract type %t: %s",
4661 c, exprNode_unparse (ret)),
4668 message ("Cast to underlying abstract type %t: %s",
4669 c, exprNode_unparse (ret)),
4674 if (ctype_isRealAbstract (bt)
4675 && !context_hasAccess (ctype_typeId (bt)))
4677 if (ctype_isUnknown (c) || ctype_isVoidPointer (c))
4681 message ("Cast from underlying abstract type %t: %s",
4682 t, exprNode_unparse (ret)),
4689 message ("Cast from underlying abstract type %t: %s",
4690 t, exprNode_unparse (ret)),
4698 ctype bt = ctype_realType (ctype_getBaseType (t));
4699 ctype bc = ctype_realType (ctype_getBaseType (c));
4701 if (ctype_isAbstract (bt) && !context_hasAccess (ctype_typeId (bt)))
4703 if (ctype_match (c, t))
4705 if (ctype_equal (c, t))
4709 message ("Redundant cast involving abstract type %t: %s",
4710 bt, exprNode_unparse (ret)),
4718 message ("Cast from abstract type %t: %s",
4719 bt, exprNode_unparse (ret)),
4724 if (ctype_isAbstract (bc)
4725 && !context_hasAccess (ctype_typeId (bc)))
4727 if (ctype_match (c, t))
4733 DPRINTF (("No access to: %s / %d",
4734 ctype_unparse (bc), ctype_typeId (bc)));
4735 DPRINTF (("Context %s %s",
4736 bool_unparse (context_inFunctionLike ()),
4737 context_unparse ()));
4740 message ("Cast to abstract type %t: %s", bc,
4741 exprNode_unparse (ret)),
4747 if (ctype_isAbstract (c))
4749 if (sRef_isExposed (e->sref) || sRef_isOnly (e->sref))
4751 /* okay, cast exposed to abstract */
4752 sRef_clearExKindComplete (ret->sref, fileloc_undefined);
4756 if (ctype_isVisiblySharable (t)
4757 && sRef_isExternallyVisible (e->sref)
4758 && !(ctype_isAbstract (t)
4759 && context_hasAccess (ctype_typeId (t))))
4763 message ("Cast to abstract type from externally visible "
4764 "mutable storage exposes rep of %s: %s",
4766 exprNode_unparse (e)),
4776 evaluationOrderUndefined (lltok op)
4778 int opid = lltok_getTok (op);
4780 return (opid != AND_OP && opid != OR_OP);
4783 static bool checkIntegral (/*@notnull@*/ exprNode e1,
4784 /*@notnull@*/ exprNode e2,
4785 /*@notnull@*/ exprNode ret,
4790 ctype te1 = exprNode_getType (e1);
4791 ctype te2 = exprNode_getType (e2);
4793 ctype tr1 = ctype_realishType (te1);
4794 ctype tr2 = ctype_realishType (te2);
4796 if (ctype_isForceRealInt (&tr1) && ctype_isForceRealInt (&tr2))
4802 if (context_msgStrictOps ())
4804 if (!ctype_isInt (tr1) && !ctype_isInt (tr2))
4806 if (ctype_sameName (te1, te2))
4810 message ("Operands of %s are non-integer (%t): %s",
4811 lltok_unparse (op), te1,
4812 exprNode_unparse (ret)),
4819 message ("Operands of %s are non-integers (%t, %t): %s",
4820 lltok_unparse (op), te1, te2,
4821 exprNode_unparse (ret)),
4825 else if (!ctype_isInt (tr1))
4829 message ("Left operand of %s is non-integer (%t): %s",
4830 lltok_unparse (op), te1, exprNode_unparse (ret)),
4834 /* !ctype_isInt (te2) */
4838 message ("Right operand of %s is non-integer (%t): %s",
4839 lltok_unparse (op), te2, exprNode_unparse (ret)),
4849 ** returns exprNode representing e1 op e2
4851 ** uses msg if there are errors
4852 ** can be used for both assignment ops and regular ops
4857 static /*@only@*/ /*@notnull@*/ exprNode
4858 exprNode_makeOp (/*@keep@*/ exprNode e1, /*@keep@*/ exprNode e2,
4859 /*@keep@*/ lltok op)
4861 ctype te1, te2, tr1, tr2, tret;
4862 int opid = lltok_getTok (op);
4863 bool hasError = FALSE;
4866 if (exprNode_isError (e1))
4868 ret = exprNode_createPartialNVCopy (e2);
4872 ret = exprNode_createPartialNVCopy (e1);
4875 ret->val = multiVal_undefined;
4877 ret->edata = exprData_makeOp (e1, e2, op);
4879 if (exprNode_isError (e1) || exprNode_isError (e2))
4881 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
4882 || opid == EQ_OP || opid == NE_OP
4883 || opid == AND_OP || opid == OR_OP)
4885 ret->typ = ctype_bool;
4888 if (exprNode_isDefined (e1))
4890 exprNode_checkUse (ret, e1->sref, e1->loc);
4893 if (exprNode_isDefined (e2))
4895 exprNode_mergeUSs (ret, e2);
4896 exprNode_checkUse (ret, e2->sref, e2->loc);
4902 tret = ctype_unknown;
4903 te1 = exprNode_getType (e1);
4904 DPRINTF (("te1 = %s / %s", exprNode_unparse (e1), ctype_unparse (te1)));
4906 te2 = exprNode_getType (e2);
4908 tr1 = ctype_realishType (te1);
4909 tr2 = ctype_realishType (te2);
4913 ret->guards = guardSet_or (ret->guards, e2->guards);
4915 else if (opid == AND_OP)
4917 ret->guards = guardSet_and (ret->guards, e2->guards);
4924 if (opid == EQ_OP || opid == NE_OP)
4926 exprNode temp1 = e1, temp2 = e2;
4928 /* could do NULL == x */
4930 if (exprNode_isNullValue (e1) || exprNode_isUnknownConstant (e1))
4932 temp1 = e2; temp2 = e1;
4935 if (exprNode_isNullValue (temp2) || exprNode_isUnknownConstant (temp2))
4937 reflectNullTest (temp1, (opid == NE_OP));
4938 guardSet_free (ret->guards);
4939 ret->guards = guardSet_copy (temp1->guards);
4943 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
4944 || opid == EQ_OP || opid == NE_OP || opid == AND_OP || opid == OR_OP)
4949 if (anyAbstract (tr1, tr2) &&
4950 (!((ctype_isRealBool (te1) || ctype_isRealBool (te2)) &&
4951 (opid == AND_OP || opid == OR_OP
4952 || opid == EQ_OP || opid == NE_OP))))
4954 abstractOpError (tr1, tr2, op, e1, e2, e1->loc, e2->loc);
4956 else if (ctype_isUnknown (te1) || ctype_isUnknown (te2))
4958 /* unknown types, no comparisons possible */
4964 case TMULT: /* multiplication and division: */
4966 case MUL_ASSIGN: /* numeric, numeric -> numeric */
4967 case DIV_ASSIGN: /* */
4969 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
4972 case TPLUS: /* addition and subtraction: */
4973 case TMINUS: /* pointer, int -> pointer */
4974 case SUB_ASSIGN: /* int, pointer -> pointer */
4975 case ADD_ASSIGN: /* numeric, numeric -> numeric */
4977 tr1 = ctype_fixArrayPtr (tr1);
4979 if ((ctype_isRealPointer (tr1) && !exprNode_isNullValue (e1))
4980 && (!ctype_isRealPointer (tr2) && ctype_isRealInt (tr2)))
4984 if (context_msgPointerArith ())
4988 message ("Pointer arithmetic (%t, %t): %s",
4989 te1, te2, exprNode_unparse (ret)),
4993 if (sRef_possiblyNull (e1->sref)
4994 && !usymtab_isGuarded (e1->sref))
4997 (FLG_NULLPOINTERARITH,
4998 message ("Pointer arithmetic involving possibly "
4999 "null pointer %s: %s",
5000 exprNode_unparse (e1),
5001 exprNode_unparse (ret)),
5005 ret->sref = sRef_copy (e1->sref);
5007 /* start modifications */
5008 /* added by Seejo on 4/16/2000 */
5010 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5012 if ((sRef_isPossiblyNullTerminated (e1->sref)) || (sRef_isNullTerminated(e1->sref))) {
5013 //if (sRef_isKnown (e->sref)) {
5014 //ret->sref = sRef_makeAddress (e->sref);
5019 added ugly fixed to stop
5020 program from crashing on point + int +int
5021 one day I'll fix this or ask Seejo wtf the codes supposed to do. */
5023 if (!multiVal_isInt (e2->val) )
5027 val = (int) multiVal_forceInt (e2->val);
5029 /* Operator : + or += */
5030 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5031 if (sRef_getSize(e1->sref) >= val) {/* Incrementing the pointer by
5032 val should not result in a
5033 size < 0 (size = 0 is ok !) */
5035 sRef_setSize (ret->sref, sRef_getSize(e1->sref) - val);
5037 if (sRef_getLen(e1->sref) == val) { /* i.e. the character at posn val is \0 */
5038 sRef_setNotNullTerminatedState(ret->sref);
5039 sRef_resetLen (ret->sref);
5041 sRef_setNullTerminatedState(ret->sref);
5042 sRef_setLen (ret->sref, sRef_getLen(e1->sref) - val);
5047 /* Operator : - or -= */
5048 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5049 if (sRef_getSize(e1->sref) >= 0) {
5050 sRef_setSize (ret->sref, sRef_getSize(e1->sref) + val);
5051 sRef_setLen (ret->sref, sRef_getLen(e1->sref) + val);
5056 /* end modifications */
5058 sRef_setNullError (ret->sref);
5061 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5062 ** since is points to storage that should not be deallocated
5063 ** through this pointer.
5066 if (sRef_isOnly (ret->sref)
5067 || sRef_isFresh (ret->sref))
5069 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5074 else if ((!ctype_isRealPointer(tr1) && ctype_isRealInt (tr1))
5075 && (ctype_isRealPointer (tr2) && !exprNode_isNullValue (e2)))
5077 if (context_msgPointerArith ())
5081 message ("Pointer arithmetic (%t, %t): %s",
5082 te1, te2, exprNode_unparse (ret)),
5086 if (sRef_possiblyNull (e1->sref)
5087 && !usymtab_isGuarded (e1->sref))
5090 (FLG_NULLPOINTERARITH,
5091 message ("Pointer arithmetic involving possibly "
5092 "null pointer %s: %s",
5093 exprNode_unparse (e2),
5094 exprNode_unparse (ret)),
5098 ret->sref = sRef_copy (e2->sref);
5100 /* start modifications */
5101 /* added by Seejo on 4/16/2000 */
5103 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5106 if ((sRef_isPossiblyNullTerminated (e2->sref)) || (sRef_isNullTerminated(e2->sref))) {
5107 //if (sRef_isKnown (e->sref)) {
5108 //ret->sref = sRef_makeAddress (e->sref);
5111 int val = (int) multiVal_forceInt (e1->val);
5113 /* Operator : + or += */
5114 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5115 if (sRef_getSize(e2->sref) >= val) {/* Incrementing the pointer by
5116 val should not result in a
5117 size < 0 (size = 0 is ok !) */
5119 sRef_setSize (ret->sref, sRef_getSize(e2->sref) - val);
5121 if (sRef_getLen(e2->sref) == val) { /* i.e. the character at posn val is \0 */
5122 sRef_setNotNullTerminatedState(ret->sref);
5123 sRef_resetLen (ret->sref);
5125 sRef_setNullTerminatedState(ret->sref);
5126 sRef_setLen (ret->sref, sRef_getLen(e2->sref) - val);
5131 /* Operator : - or -= */
5132 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5133 if (sRef_getSize(e2->sref) >= 0) {
5134 sRef_setSize (ret->sref, sRef_getSize(e2->sref) + val);
5135 sRef_setLen (ret->sref, sRef_getLen(e2->sref) + val);
5140 /* end modifications */
5142 sRef_setNullError (ret->sref);
5145 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5146 ** since is points to storage that should not be deallocated
5147 ** through this pointer.
5150 if (sRef_isOnly (ret->sref)
5151 || sRef_isFresh (ret->sref)) {
5152 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5156 ret->sref = e2->sref;
5160 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5165 case LEFT_ASSIGN: /* Shifts: should be unsigned values */
5169 case TAMPERSAND: /* bitwise & */
5171 case TCIRC: /* ^ (XOR) */
5176 bool reported = FALSE;
5177 flagcode code = FLG_BITWISEOPS;
5179 if (opid == LEFT_OP || opid == LEFT_ASSIGN
5180 || opid == RIGHT_OP || opid == RIGHT_ASSIGN) {
5181 code = FLG_SHIFTSIGNED;
5184 if (!ctype_isUnsigned (tr1))
5186 if (exprNode_isNonNegative (e1)) {
5189 reported = optgenerror
5191 message ("Left operand of %s is not unsigned value (%t): %s",
5192 lltok_unparse (op), te1,
5193 exprNode_unparse (ret)),
5203 /* right need not be signed for shifts */
5204 if (code != FLG_SHIFTSIGNED
5205 && !ctype_isUnsigned (tr2))
5207 if (!exprNode_isNonNegative (e2)) {
5208 reported = optgenerror
5210 message ("Right operand of %s is not unsigned value (%t): %s",
5211 lltok_unparse (op), te2,
5212 exprNode_unparse (ret)),
5220 if (!checkIntegral (e1, e2, ret, op)) {
5221 te1 = ctype_unknown;
5225 DPRINTF (("Set: %s", ctype_unparse (te1)));
5228 ** tret is the widest type of te1 and te2
5231 tret = ctype_widest (te1, te2);
5236 if (checkIntegral (e1, e2, ret, op)) {
5239 tret = ctype_unknown;
5244 case TLT: /* comparisons */
5245 case TGT: /* numeric, numeric -> bool */
5246 if ((ctype_isReal (tr1) && !ctype_isInt (tr1))
5247 || (ctype_isReal (tr2) && !ctype_isInt (tr2)))
5250 bool fepsilon = FALSE;
5252 if (!ctype_isReal (rtype) || ctype_isInt (rtype))
5257 if (opid == TLT || opid == TGT)
5259 uentry ue1 = exprNode_getUentry (e1);
5260 uentry ue2 = exprNode_getUentry (e2);
5263 ** FLT_EPSILON, etc. really is a variable, not
5267 if (uentry_isVariable (ue1))
5269 cstring uname = uentry_rawName (ue1);
5271 if (cstring_equalLit (uname, "FLT_EPSILON")
5272 || cstring_equalLit (uname, "DBL_EPSILON")
5273 || cstring_equalLit (uname, "LDBL_EPSILON"))
5279 if (uentry_isVariable (ue2))
5281 cstring uname = uentry_rawName (ue2);
5283 if (cstring_equalLit (uname, "FLT_EPSILON")
5284 || cstring_equalLit (uname, "DBL_EPSILON")
5285 || cstring_equalLit (uname, "LDBL_EPSILON"))
5294 ; /* Don't complain. */
5300 message ("Dangerous comparison involving %s types: %s",
5301 ctype_unparse (rtype),
5302 exprNode_unparse (ret)),
5311 ** Types should match.
5314 if (!exprNode_matchTypes (e1, e2))
5316 hasError = gentypeerror
5318 message ("Operands of %s have incompatible types (%t, %t): %s",
5319 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5325 || (ctype_isForceRealNumeric (&tr1)
5326 && ctype_isForceRealNumeric (&tr2)) ||
5327 (ctype_isRealPointer (tr1) && ctype_isRealPointer (tr2)))
5333 if ((ctype_isRealNumeric (tr1) && ctype_isRealPointer (tr2)) ||
5334 (ctype_isRealPointer (tr1) && ctype_isRealNumeric (tr2)))
5338 message ("Comparison of pointer and numeric (%t, %t): %s",
5339 te1, te2, exprNode_unparse (ret)),
5344 (void) checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5349 /* EQ_OP should NOT be used with booleans (unless one is FALSE) */
5351 if ((opid == EQ_OP || opid == NE_OP) &&
5352 ctype_isDirectBool (tr1) && ctype_isDirectBool (tr2))
5355 ** is one a variable?
5358 if (uentry_isVariable (exprNode_getUentry (e1))
5359 || uentry_isVariable (exprNode_getUentry (e2)))
5362 ** comparisons with FALSE are okay
5365 if (exprNode_isFalseConstant (e1)
5366 || exprNode_isFalseConstant (e2))
5375 ("Use of %q with %s variables (risks inconsistency because "
5376 "of multiple true values): %s",
5377 cstring_makeLiteral ((opid == EQ_OP) ? "==" : "!="),
5378 context_printBoolName (), exprNode_unparse (ret)),
5385 case AND_OP: /* bool, bool -> bool */
5388 if (ctype_isForceRealBool (&tr1) && ctype_isForceRealBool (&tr2))
5394 if (context_maybeSet (FLG_BOOLOPS))
5396 if (!ctype_isRealBool (te1) && !ctype_isRealBool (te2))
5398 if (ctype_sameName (te1, te2))
5402 message ("Operands of %s are non-boolean (%t): %s",
5403 lltok_unparse (op), te1,
5404 exprNode_unparse (ret)),
5412 ("Operands of %s are non-booleans (%t, %t): %s",
5413 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5417 else if (!ctype_isRealBool (te1))
5421 message ("Left operand of %s is non-boolean (%t): %s",
5422 lltok_unparse (op), te1, exprNode_unparse (ret)),
5425 else if (!ctype_isRealBool (te2))
5429 message ("Right operand of %s is non-boolean (%t): %s",
5430 lltok_unparse (op), te2, exprNode_unparse (ret)),
5443 (cstring_makeLiteral
5444 ("There has been a problem in the parser. This is believed to result "
5445 "from a problem with bison v. 1.25. Please try rebuidling LCLint "
5446 "using the pre-compiled grammar files by commenting out the "
5447 "BISON= line in the top-level Makefile."));
5452 DPRINTF (("Return type: %s", ctype_unparse (tret)));
5455 exprNode_checkUse (ret, e1->sref, e1->loc);
5456 exprNode_mergeUSs (ret, e2);
5457 exprNode_checkUse (ret, e2->sref, e2->loc);
5463 exprNode_op (/*@only@*/ exprNode e1, /*@keep@*/ exprNode e2,
5464 /*@only@*/ lltok op)
5468 checkMacroParen (e1);
5469 checkMacroParen (e2);
5471 if (evaluationOrderUndefined (op) && context_maybeSet (FLG_EVALORDER))
5473 checkExpressionDefined (e1, e2, op);
5476 ret = exprNode_makeOp (e1, e2, op);
5481 void exprNode_checkAssignMod (exprNode e1, exprNode ret)
5484 ** This is somewhat bogus!
5486 ** Assigning to a nested observer in a non-observer datatype
5487 ** should not produce an error.
5490 sRef ref = exprNode_getSref (e1);
5492 DPRINTF (("Check assign mod: %s",
5493 sRef_unparseFull (ref)));
5495 if (sRef_isObserver (ref)
5496 || ((sRef_isFileStatic (ref) || sRef_isFileOrGlobalScope (ref))
5497 && ctype_isArray (ctype_realType (sRef_getType (ref)))))
5499 sRef base = sRef_getBase (ref);
5501 if (sRef_isValid (base) && sRef_isObserver (base))
5503 exprNode_checkModify (e1, ret);
5507 exprNode_checkModifyVal (e1, ret);
5512 exprNode_checkModify (e1, ret);
5517 exprNode_assign (/*@only@*/ exprNode e1,
5518 /*@only@*/ exprNode e2, /*@only@*/ lltok op)
5520 bool isalloc = FALSE;
5521 bool isjustalloc = FALSE;
5524 DPRINTF (("%s [%s] <- %s [%s]",
5525 exprNode_unparse (e1),
5526 ctype_unparse (e1->typ),
5527 exprNode_unparse (e2),
5528 ctype_unparse (e2->typ)));
5530 if (lltok_getTok (op) != TASSIGN)
5532 ret = exprNode_makeOp (e1, e2, op);
5536 ret = exprNode_createPartialCopy (e1);
5537 ret->kind = XPR_ASSIGN;
5538 ret->edata = exprData_makeOp (e1, e2, op);
5540 if (!exprNode_isError (e2))
5542 ret->sets = sRefSet_union (ret->sets, e2->sets);
5543 ret->msets = sRefSet_union (ret->msets, e2->msets);
5544 ret->uses = sRefSet_union (ret->uses, e2->uses);
5548 checkExpressionDefined (e1, e2, op);
5550 if (exprNode_isError (e1))
5552 if (!exprNode_isError (e2))
5554 ret->loc = fileloc_update (ret->loc, e2->loc);
5558 ret->loc = fileloc_update (ret->loc, g_currentloc);
5562 if (!exprNode_isError (e2))
5564 checkMacroParen (e2);
5567 if (exprNode_isDefined (e1))
5569 if (sRef_isMacroParamRef (e1->sref))
5571 if (context_inIterDef ())
5573 uentry ue = sRef_getUentry (e1->sref);
5575 if (uentry_isYield (ue))
5581 if (fileloc_isDefined (e1->loc))
5585 message ("Assignment to non-yield iter parameter: %q",
5586 sRef_unparse (e1->sref)),
5593 message ("Assignment to non-yield iter parameter: %q",
5594 sRef_unparse (e1->sref)),
5601 if (fileloc_isDefined (e1->loc))
5605 message ("Assignment to macro parameter: %q",
5606 sRef_unparse (e1->sref)),
5613 message ("Assignment to macro parameter: %q",
5614 sRef_unparse (e1->sref)),
5621 exprNode_checkAssignMod (e1, ret);
5624 if (exprNode_isDefined (e2))
5626 if (lltok_getTok (op) == TASSIGN)
5628 ctype te1 = exprNode_getType (e1);
5629 ctype te2 = exprNode_getType (e2);
5631 if (!ctype_forceMatch (te1, te2))
5633 if (exprNode_matchLiteral (te1, e2))
5641 message ("Assignment of %t to %t: %s %s %s",
5642 te2, te1, exprNode_unparse (e1),
5644 exprNode_unparse (e2)),
5650 exprNode_mergeUSs (ret, e2);
5651 exprNode_checkUse (ret, e2->sref, e2->loc);
5653 doAssign (e1, e2, FALSE);
5654 ret->sref = e1->sref;
5658 if (exprNode_isDefined (e2))
5660 exprNode_mergeUSs (ret, e2);
5661 exprNode_checkUse (ret, e2->sref, e2->loc);
5665 if (sRef_isPointer (e1->sref) && !sRef_isMacroParamRef (e1->sref))
5667 exprNode_checkUse (ret, sRef_getBase (e1->sref), e1->loc);
5670 isjustalloc = sRef_isJustAllocated (e1->sref);
5671 isalloc = sRef_isAllocated (e1->sref);
5673 if (sRef_isField (e1->sref))
5675 sRef root = sRef_getRootBase (sRef_getBase (e1->sref));
5677 if (!sRef_isAllocated (root) && !sRef_isMacroParamRef (root))
5679 exprNode_checkUse (ret, root, e1->loc);
5685 ** be careful! this defines e1->sref.
5688 if (!sRef_isMacroParamRef (e1->sref))
5690 exprNode_checkSet (ret, e1->sref);
5695 sRef_setAllocatedComplete (e1->sref, exprNode_isDefined (e2)
5696 ? e2->loc : e1->loc);
5702 sRef_setAllocatedShallowComplete (e1->sref, exprNode_loc (e2));
5711 exprNode_cond (/*@keep@*/ exprNode pred, /*@keep@*/ exprNode ifclause,
5712 /*@keep@*/ exprNode elseclause)
5716 if (!exprNode_isError (pred))
5718 ret = exprNode_createPartialCopy (pred);
5719 checkMacroParen (pred);
5720 exprNode_checkPred (cstring_makeLiteralTemp ("conditional"), pred);
5722 if (!exprNode_isError (ifclause))
5724 checkMacroParen (ifclause); /* update macro counts! */
5726 if (!exprNode_isError (elseclause))
5728 checkMacroParen (elseclause);
5730 if (!exprNode_matchTypes (ifclause, elseclause))
5733 (exprNode_getType (ifclause),
5735 exprNode_getType (elseclause),
5737 message ("Conditional clauses are not of same type: "
5739 exprNode_unparse (ifclause),
5740 exprNode_getType (ifclause),
5741 exprNode_unparse (elseclause),
5742 exprNode_getType (elseclause)),
5745 ret->sref = sRef_undefined;
5746 ret->typ = ctype_unknown;
5751 /* for now...should merge the states */
5752 ret->sref = ifclause->sref;
5753 ret->typ = ifclause->typ;
5755 if (exprNode_isNullValue (ifclause))
5757 ret->typ = elseclause->typ;
5761 exprNode_checkUse (ret, pred->sref, pred->loc);
5762 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5763 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5765 exprNode_mergeCondUSs (ret, ifclause, elseclause);
5770 ret->typ = ifclause->typ;
5772 exprNode_checkUse (pred, pred->sref, pred->loc);
5773 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5775 exprNode_mergeCondUSs (ret, ifclause, exprNode_undefined);
5780 if (!exprNode_isError (elseclause))
5782 ret->typ = elseclause->typ;
5784 exprNode_checkUse (pred, pred->sref, pred->loc);
5785 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5787 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
5791 else /* pred is error */
5793 if (!exprNode_isError (ifclause))
5795 ret = exprNode_createSemiCopy (ifclause);
5797 checkMacroParen (ifclause); /* update macro counts! */
5799 if (!exprNode_isError (elseclause))
5801 checkMacroParen (elseclause);
5803 ret->typ = ifclause->typ;
5805 if (!ctype_forceMatch (ifclause->typ, elseclause->typ))
5808 (exprNode_getType (ifclause),
5810 exprNode_getType (elseclause),
5812 message ("Conditional clauses are not of same type: "
5814 exprNode_unparse (ifclause),
5815 exprNode_getType (ifclause),
5816 exprNode_unparse (elseclause),
5817 exprNode_getType (elseclause)),
5820 ret->typ = ctype_unknown;
5824 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
5825 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5827 exprNode_mergeCondUSs (ret, ifclause, elseclause);
5830 else if (!exprNode_isError (elseclause)) /* pred, if errors */
5832 ret = exprNode_createSemiCopy (ifclause);
5834 ret->typ = elseclause->typ;
5835 checkMacroParen (elseclause);
5837 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
5838 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
5840 else /* all errors! */
5842 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
5846 ret->kind = XPR_COND;
5847 ret->edata = exprData_makeCond (pred, ifclause, elseclause);
5849 if (exprNode_isDefined (ifclause) && exprNode_isDefined (elseclause))
5851 exprNode_combineControl (ret, ifclause, elseclause);
5858 exprNode_vaArg (/*@only@*/ lltok tok, /*@only@*/ exprNode arg, /*@only@*/ qtype qt)
5860 ctype totype = qtype_getType (qt);
5862 exprNode_createPartialLocCopy (arg, fileloc_copy (lltok_getLoc (tok)));
5866 ** check use of va_arg : <valist>, type -> type
5869 if (exprNode_isError (arg))
5874 targ = exprNode_getType (arg);
5877 ** arg should have be a pointer
5880 if (!ctype_isUA (targ) ||
5881 (!usymId_equal (ctype_typeId (targ),
5882 usymtab_getTypeId (cstring_makeLiteralTemp ("va_list")))))
5886 message ("First argument to va_arg is not a va_list (type %t): %s",
5887 targ, exprNode_unparse (arg)),
5891 exprNode_checkSet (ret, arg->sref);
5895 ** return type is totype
5899 ret->kind = XPR_VAARG;
5900 ret->edata = exprData_makeCast (tok, arg, qt);
5905 exprNode exprNode_labelMarker (/*@only@*/ cstring label)
5907 exprNode ret = exprNode_createPlain (ctype_undefined);
5908 ret->kind = XPR_LABEL;
5909 ret->edata = exprData_makeLiteral (label);
5910 ret->isJumpPoint = TRUE;
5912 return (ret); /* for now, ignore label */
5915 exprNode exprNode_notReached (/*@returned@*/ exprNode stmt)
5917 if (exprNode_isDefined (stmt))
5919 stmt->isJumpPoint = TRUE;
5921 /* This prevent stray no return path errors, etc. */
5922 stmt->exitCode = XK_MUSTEXIT;
5928 bool exprNode_isDefaultMarker (exprNode e)
5930 if (exprNode_isDefined (e))
5932 return (e->kind == XPR_DEFAULT || e->kind == XPR_FTDEFAULT);
5938 bool exprNode_isCaseMarker (exprNode e)
5940 if (exprNode_isDefined (e))
5942 return (e->kind == XPR_FTCASE || e->kind == XPR_CASE);
5948 bool exprNode_isLabelMarker (exprNode e)
5950 if (exprNode_isDefined (e))
5952 return (e->kind == XPR_LABEL);
5958 exprNode exprNode_caseMarker (/*@only@*/ exprNode test, bool fallThrough)
5960 exprNode ret = exprNode_createPartialCopy (test);
5962 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
5964 if (exprNode_isError (test)) {
5968 exprNode_checkUse (ret, test->sref, test->loc);
5970 usymtab_setExitCode (ret->exitCode);
5974 usymtab_setMustBreak ();
5977 ret->edata = exprData_makeSingle (test);
5978 ret->isJumpPoint = TRUE;
5984 exprNode exprNode_caseStatement (/*@only@*/ exprNode test, /*@only@*/ exprNode stmt, bool fallThrough)
5986 exprNode ret = exprNode_createPartialCopy (test);
5988 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
5989 ret->edata = exprData_makePair (test, stmt);
5990 ret->isJumpPoint = TRUE;
5992 if (exprNode_isError (test))
5997 exprNode_checkUse (ret, test->sref, test->loc);
5999 if (exprNode_isError (stmt))
6004 exprNode_mergeUSs (ret, stmt);
6006 ret->exitCode = stmt->exitCode;
6007 ret->mustBreak = stmt->mustBreak;
6008 ret->canBreak = stmt->canBreak;
6010 usymtab_setExitCode (ret->exitCode);
6014 usymtab_setMustBreak ();
6021 /*@notnull@*/ /*@only@*/ exprNode
6022 exprNode_defaultMarker (/*@only@*/ lltok def, bool fallThrough)
6024 exprNode ret = exprNode_createTok (def);
6026 ret->isJumpPoint = TRUE;
6027 ret->kind = fallThrough ? XPR_FTDEFAULT : XPR_DEFAULT;
6032 exprNode_mayEscape (exprNode e)
6034 if (exprNode_isDefined (e))
6036 return exitkind_couldEscape (e->exitCode);
6042 exprNode_mustBreak (exprNode e)
6044 if (exprNode_isDefined (e))
6046 return e->mustBreak;
6052 exprNode_mustEscape (exprNode e)
6054 if (exprNode_isDefined (e))
6056 return exitkind_mustEscape (e->exitCode) || exprNode_mustBreak (e);
6063 exprNode_errorEscape (exprNode e)
6065 if (exprNode_isDefined (e))
6067 return exitkind_isError (e->exitCode);
6073 exprNode exprNode_concat (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
6075 exprNode ret = exprNode_createPartialCopy (e1);
6077 DPRINTF (("Concat: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6079 ret->edata = exprData_makePair (e1, e2);
6080 ret->kind = XPR_STMTLIST;
6082 if (exprNode_isDefined (e1))
6084 ret->isJumpPoint = e1->isJumpPoint;
6085 ret->canBreak = e1->canBreak;
6089 if (exprNode_isDefined (e2))
6091 ret->loc = fileloc_update (ret->loc, e2->loc);
6095 if (exprNode_isDefined (e2))
6097 ret->exitCode = e2->exitCode;
6098 ret->mustBreak = e2->mustBreak;
6099 if (e2->canBreak) ret->canBreak = TRUE;
6103 ** if e1 must return, then e2 is unreachable!
6106 if (exprNode_isDefined (e1) && exprNode_isDefined (e2))
6108 if ((exprNode_mustEscape (e1) || exprNode_mustBreak (e1))
6109 && !(e2->isJumpPoint))
6111 if (context_getFlag (FLG_UNREACHABLE))
6115 if (e2->kind == XPR_STMT)
6117 nr = exprData_getUopNode (e2->edata);
6120 if ((nr->kind == XPR_TOK
6121 && lltok_isSemi (exprData_getTok (nr->edata))))
6123 /* okay to have unreachable ";" */
6124 ret->exitCode = XK_MUSTEXIT;
6125 ret->canBreak = TRUE;
6129 if (optgenerror (FLG_UNREACHABLE,
6130 message ("Unreachable code: %s",
6131 exprNode_unparseFirst (nr)),
6134 ret->isJumpPoint = TRUE;
6135 ret->mustBreak = FALSE;
6136 ret->exitCode = XK_ERROR;
6137 DPRINTF (("Jump point: %s", exprNode_unparse (ret)));
6141 ret->exitCode = XK_MUSTEXIT;
6142 ret->canBreak = TRUE;
6150 if ((e2->kind == XPR_CASE || e2->kind == XPR_DEFAULT))
6153 ** We want a warning anytime we have:
6155 ** yyy; <<<- no break or return
6159 exprNode lastStmt = exprNode_lastStatement (e1);
6161 if (exprNode_isDefined (lastStmt)
6162 && !exprNode_mustEscape (lastStmt)
6163 && !exprNode_mustBreak (lastStmt)
6164 && !exprNode_isCaseMarker (lastStmt)
6165 && !exprNode_isDefaultMarker (lastStmt)
6166 && !exprNode_isLabelMarker (lastStmt))
6168 voptgenerror (FLG_CASEBREAK,
6170 ("Fall through case (no preceeding break)"),
6177 exprNode_mergeUSs (ret, e2);
6179 usymtab_setExitCode (ret->exitCode);
6183 usymtab_setMustBreak ();
6189 exprNode exprNode_createTok (/*@only@*/ lltok t)
6191 exprNode ret; /*@i23 if on same line, bad things happen...!@*/
6192 ret = exprNode_create (ctype_unknown);
6193 ret->kind = XPR_TOK;
6194 ret->edata = exprData_makeTok (t);
6198 exprNode exprNode_statement (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6200 if (!exprNode_isError (e))
6202 exprNode_checkStatement(e);
6205 return (exprNode_statementError (e, t));
6208 static exprNode exprNode_statementError (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6210 exprNode ret = exprNode_createPartialCopy (e);
6212 if (!exprNode_isError (e))
6214 if (e->kind != XPR_ASSIGN)
6216 exprNode_checkUse (ret, e->sref, e->loc);
6219 ret->exitCode = e->exitCode;
6220 ret->canBreak = e->canBreak;
6221 ret->mustBreak = e->mustBreak;
6224 ret->edata = exprData_makeUop (e, t);
6225 ret->kind = XPR_STMT;
6230 exprNode exprNode_checkExpr (/*@returned@*/ exprNode e)
6232 if (!exprNode_isError (e))
6234 if (e->kind != XPR_ASSIGN)
6236 exprNode_checkUse (e, e->sref, e->loc);
6243 void exprNode_produceGuards (exprNode pred)
6245 if (!exprNode_isError (pred))
6247 if (ctype_isRealPointer (pred->typ))
6249 pred->guards = guardSet_addTrueGuard (pred->guards, pred->sref);
6252 exprNode_checkUse (pred, pred->sref, pred->loc);
6253 exprNode_resetSref (pred);
6257 exprNode exprNode_makeBlock (/*@only@*/ exprNode e)
6259 exprNode ret = exprNode_createPartialCopy (e);
6261 if (!exprNode_isError (e))
6263 ret->exitCode = e->exitCode;
6264 ret->canBreak = e->canBreak;
6265 ret->mustBreak = e->mustBreak;
6268 ret->edata = exprData_makeSingle (e);
6269 ret->kind = XPR_BLOCK;
6273 bool exprNode_isBlock (exprNode e)
6275 return (exprNode_isDefined (e)
6276 && ((e)->kind == XPR_BLOCK));
6279 bool exprNode_isAssign (exprNode e)
6281 if (exprNode_isDefined (e))
6283 return (e->kind == XPR_ASSIGN);
6289 bool exprNode_isEmptyStatement (exprNode e)
6291 return (exprNode_isDefined (e)
6292 && (e->kind == XPR_TOK)
6293 && (lltok_isSemi (exprData_getTok (e->edata))));
6296 exprNode exprNode_if (/*@only@*/ exprNode pred, /*@only@*/ exprNode tclause)
6299 bool emptyErr = FALSE;
6301 if (context_maybeSet (FLG_IFEMPTY))
6303 if (exprNode_isEmptyStatement (tclause))
6305 emptyErr = optgenerror (FLG_IFEMPTY,
6307 ("Body of if statement is empty"),
6308 exprNode_loc (tclause));
6312 if (!emptyErr && context_maybeSet (FLG_IFBLOCK))
6314 if (exprNode_isDefined (tclause)
6315 && !exprNode_isBlock (tclause))
6317 voptgenerror (FLG_IFBLOCK,
6319 ("Body of if statement is not a block: %s",
6320 exprNode_unparse (tclause)),
6321 exprNode_loc (tclause));
6325 if (exprNode_isError (pred))
6327 if (exprNode_isError (tclause))
6329 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6333 ret = exprNode_createPartialCopy (tclause);
6338 if (exprNode_mustEscape (pred))
6342 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6343 exprNode_loc (pred));
6346 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6347 exprNode_checkUse (pred, pred->sref, pred->loc);
6349 if (!exprNode_isError (tclause))
6351 exprNode_mergeCondUSs (pred, tclause, exprNode_undefined);
6354 ret = exprNode_createPartialCopy (pred);
6358 ret->edata = exprData_makePair (pred, tclause);
6360 ret->exitCode = XK_UNKNOWN;
6362 if (exprNode_isDefined (tclause))
6364 ret->exitCode = exitkind_makeConditional (tclause->exitCode);
6365 ret->canBreak = tclause->canBreak;
6366 ret->sets = sRefSet_union (ret->sets, tclause->sets);
6367 ret->msets = sRefSet_union (ret->msets, tclause->msets);
6368 ret->uses = sRefSet_union (ret->uses, tclause->uses);
6371 ret->mustBreak = FALSE;
6376 exprNode exprNode_ifelse (/*@only@*/ exprNode pred,
6377 /*@only@*/ exprNode tclause,
6378 /*@only@*/ exprNode eclause)
6381 bool tEmptyErr = FALSE;
6382 bool eEmptyErr = FALSE;
6384 if (context_maybeSet (FLG_IFEMPTY))
6386 if (exprNode_isEmptyStatement (tclause))
6388 tEmptyErr = optgenerror
6391 ("Body of if clause of if statement is empty"),
6392 exprNode_loc (tclause));
6395 if (exprNode_isEmptyStatement (eclause))
6397 eEmptyErr = optgenerror
6400 ("Body of else clause of if statement is empty"),
6401 exprNode_loc (eclause));
6405 if (context_maybeSet (FLG_IFBLOCK))
6408 && exprNode_isDefined (tclause)
6409 && !exprNode_isBlock (tclause))
6411 voptgenerror (FLG_IFBLOCK,
6413 ("Body of if clause of if statement is not a block: %s",
6414 exprNode_unparse (tclause)),
6415 exprNode_loc (tclause));
6419 && exprNode_isDefined (eclause)
6420 && !exprNode_isBlock (eclause)
6421 && !(eclause->kind == XPR_IF)
6422 && !(eclause->kind == XPR_IFELSE))
6427 ("Body of else clause of if statement is not a block: %s",
6428 exprNode_unparse (eclause)),
6429 exprNode_loc (eclause));
6433 if (context_maybeSet (FLG_ELSEIFCOMPLETE))
6435 if (exprNode_isDefined (eclause)
6436 && (eclause->kind == XPR_IF))
6438 voptgenerror (FLG_ELSEIFCOMPLETE,
6439 message ("Incomplete else if logic (no final else): %s",
6440 exprNode_unparse (eclause)),
6441 exprNode_loc (eclause));
6445 if (exprNode_isError (pred))
6447 if (exprNode_isError (tclause))
6449 if (exprNode_isError (eclause))
6451 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6455 ret = exprNode_createPartialCopy (eclause);
6460 ret = exprNode_createPartialCopy (tclause);
6463 else /* pred is okay */
6465 ret = exprNode_createPartialCopy (pred);
6467 if (exprNode_mustEscape (pred))
6471 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6472 exprNode_loc (pred));
6475 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6476 exprNode_checkUse (ret, pred->sref, pred->loc);
6478 exprNode_mergeCondUSs (ret, tclause, eclause);
6481 ret->kind = XPR_IFELSE;
6482 ret->edata = exprData_makeCond (pred, tclause, eclause);
6484 if (exprNode_isDefined (tclause) && exprNode_isDefined (eclause))
6486 exprNode_combineControl (ret, tclause, eclause);
6487 ret->loc = fileloc_update (ret->loc, eclause->loc);
6494 ** *allpaths <- TRUE iff all executions paths must go through the switch
6498 checkSwitchExpr (exprNode test, /*@dependent@*/ exprNode e, /*@out@*/ bool *allpaths)
6500 exprNodeSList el = exprNode_flatten (e);
6501 bool mustReturn = TRUE; /* find a branch that doesn't */
6502 bool thisReturn = FALSE;
6503 bool hasDefault = FALSE;
6504 bool hasAllMembers = FALSE;
6505 bool inSwitch = FALSE;
6506 bool isEnumSwitch = FALSE;
6507 bool canBreak = FALSE;
6508 bool fallThrough = FALSE;
6509 ctype ct = ctype_unknown;
6510 enumNameSList usedEnums;
6513 if (exprNode_isDefined (test))
6518 ttype = ctype_realType (ct);
6520 if (ctype_isEnum (ttype))
6522 isEnumSwitch = TRUE;
6523 enums = ctype_elist (ttype);
6524 usedEnums = enumNameSList_new ();
6528 exprNodeSList_elements (el, current)
6530 if (exprNode_isDefined (current))
6532 switch (current->kind)
6540 message ("Duplicate default cases in switch"),
6541 exprNode_loc (current));
6546 if (current->kind == XPR_DEFAULT || current->kind == XPR_FTDEFAULT)
6554 exprNode st = exprData_getSingle (current->edata);
6555 uentry ue = exprNode_getUentry (st);
6557 if (uentry_isValid (ue))
6559 cstring cname = uentry_rawName (ue);
6561 if (enumNameList_member (/*@-usedef@*/enums/*@=usedef@*/, cname))
6563 if (enumNameSList_member
6564 (/*@-usedef@*/usedEnums/*@=usedef@*/, cname))
6568 message ("Duplicate case in switch: %s",
6574 enumNameSList_addh (usedEnums, cname);
6581 message ("Case in switch not %s member: %s",
6582 ctype_unparse (ct), cname),
6589 if (inSwitch && !fallThrough)
6591 if (!thisReturn || canBreak)
6601 /*@switchbreak@*/ break;
6603 thisReturn = thisReturn || exprNode_mustEscape (current);
6604 canBreak = canBreak || current->canBreak;
6605 if (canBreak) fallThrough = FALSE;
6608 } end_exprNodeSList_elements;
6610 if (inSwitch) /* check the last one! */
6612 if (!thisReturn || canBreak)
6621 && (enumNameSList_size (/*@-usedef@*/usedEnums/*@=usedef@*/) !=
6622 enumNameList_size (/*@-usedef@*/enums/*@=usedef@*/)))
6624 enumNameSList unused = enumNameSList_subtract (enums, usedEnums);
6626 voptgenerror (FLG_MISSCASE,
6627 message ("Missing case%s in switch: %q",
6628 cstring_makeLiteralTemp
6629 ((enumNameSList_size (unused) > 1) ? "s" : ""),
6630 enumNameSList_unparse (unused)),
6633 enumNameSList_free (unused);
6637 hasAllMembers = TRUE;
6641 enumNameSList_free (usedEnums);
6645 *allpaths = hasDefault;
6648 exprNodeSList_free (el);
6649 return ((hasDefault || hasAllMembers) && mustReturn);
6652 exprNode exprNode_switch (/*@only@*/ exprNode e, /*@only@*/ exprNode s)
6654 exprNode ret = exprNode_createPartialCopy (e);
6657 DPRINTF (("Switch: %s", exprNode_unparse (s)));
6659 ret->kind = XPR_SWITCH;
6660 ret->edata = exprData_makePair (e, s);
6662 if (!exprNode_isError (s))
6664 exprNode fs = exprNode_firstStatement (s);
6665 ret->loc = fileloc_update (ret->loc, s->loc);
6667 if (exprNode_isUndefined (fs)
6668 || exprNode_isCaseMarker (fs) || exprNode_isLabelMarker (fs)
6669 || exprNode_isDefaultMarker (fs)) {
6672 voptgenerror (FLG_FIRSTCASE,
6674 ("Statement after switch is not a case: %s", exprNode_unparse (fs)),
6679 if (!exprNode_isError (e))
6681 if (checkSwitchExpr (e, s, &allpaths))
6683 ret->exitCode = XK_MUSTRETURN;
6687 ret->exitCode = e->exitCode;
6690 ret->canBreak = e->canBreak;
6691 ret->mustBreak = e->mustBreak;
6695 ** exprNode.c:3883,32: Variable allpaths used before definition
6702 DPRINTF (("Context exit switch!"));
6703 context_exitSwitch (ret, allpaths);
6704 DPRINTF (("Context exit switch done!"));
6709 static void checkInfiniteLoop (/*@notnull@*/ exprNode test,
6710 /*@notnull@*/ exprNode body)
6712 sRefSet tuses = test->uses;
6714 if (!sRefSet_isEmpty (test->uses))
6716 sRefSet sets = sRefSet_newCopy (body->sets);
6717 bool hasError = TRUE;
6718 bool innerState = FALSE;
6719 sRefSet tuncon = sRefSet_undefined;
6721 sets = sRefSet_union (sets, test->sets);
6722 sets = sRefSet_union (sets, body->msets);
6723 sets = sRefSet_union (sets, test->msets);
6725 sRefSet_allElements (tuses, el)
6727 if (sRef_isUnconstrained (el))
6729 tuncon = sRefSet_insert (tuncon, el);
6733 if (sRefSet_member (sets, el))
6740 if (sRef_isInternalState (el)
6741 || sRef_isFileStatic (sRef_getRootBase (el)))
6745 } end_sRefSet_allElements ;
6749 sRefSet suncon = sRefSet_undefined;
6750 bool sinner = FALSE;
6752 sRefSet_allElements (sets, el)
6754 if (sRef_isUnconstrained (el))
6756 suncon = sRefSet_insert (suncon, el);
6758 else if (sRef_isInternalState (el))
6766 } end_sRefSet_allElements ;
6768 if (sinner && innerState)
6772 else if (sRefSet_isEmpty (tuncon)
6773 && sRefSet_isEmpty (suncon))
6778 ("Suspected infinite loop. No value used in loop test (%q) "
6779 "is modified by test or loop body.",
6780 sRefSet_unparsePlain (tuses)),
6785 if (sRefSet_isEmpty (tuncon))
6789 message ("Suspected infinite loop. No condition values "
6790 "modified. Modification possible through "
6791 "unconstrained calls: %q",
6792 sRefSet_unparsePlain (suncon)),
6799 message ("Suspected infinite loop. No condition values "
6800 "modified. Possible undetected dependency through "
6801 "unconstrained calls in loop test: %q",
6802 sRefSet_unparsePlain (tuncon)),
6808 sRefSet_free (sets);
6812 exprNode exprNode_while (/*@keep@*/ exprNode t, /*@keep@*/ exprNode b)
6815 bool emptyErr = FALSE;
6817 if (context_maybeSet (FLG_WHILEEMPTY))
6819 if (exprNode_isEmptyStatement (b))
6821 emptyErr = optgenerror
6824 ("Body of while statement is empty"),
6829 if (!emptyErr && context_maybeSet (FLG_WHILEBLOCK))
6831 if (exprNode_isDefined (b)
6832 && !exprNode_isBlock (b))
6834 if (context_inIterDef ()
6835 && (b->kind == XPR_STMTLIST
6836 || b->kind == XPR_TOK))
6842 voptgenerror (FLG_WHILEBLOCK,
6844 ("Body of while statement is not a block: %s",
6845 exprNode_unparse (b)),
6851 if (exprNode_isError (t))
6853 if (exprNode_isError (b))
6855 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6859 ret = exprNode_createPartialCopy (b);
6866 ret = exprNode_createPartialCopy (t);
6868 llassert (t->kind == XPR_WHILEPRED);
6870 test = exprData_getSingle (t->edata);
6872 if (!exprNode_isError (b) && exprNode_isDefined (test))
6874 if (context_maybeSet (FLG_INFLOOPS)
6875 || context_maybeSet (FLG_INFLOOPSUNCON))
6878 ** check that some variable in the predicate is set by the body
6879 ** if the predicate uses any variables
6882 checkInfiniteLoop (test, b);
6885 exprNode_mergeUSs (ret, b);
6887 if (exprNode_isDefined (b))
6889 ret->exitCode = exitkind_makeConditional (b->exitCode);
6894 ret->edata = exprData_makePair (t, b);
6895 ret->kind = XPR_WHILE;
6897 if (exprNode_isDefined (t) && exprNode_mustEscape (t))
6901 message ("Predicate always exits: %s", exprNode_unparse (t)),
6905 ret->exitCode = XK_NEVERESCAPE;
6908 ** If loop is infinite, and there is no break inside,
6909 ** exit code is never reach.
6912 if (exprNode_knownIntValue (t))
6914 if (!exprNode_isZero (t))
6916 if (exprNode_isDefined (b))
6920 /* Really, it means never reached. */
6921 ret->exitCode = XK_MUSTEXIT;
6931 ret->canBreak = FALSE;
6932 ret->mustBreak = FALSE;
6938 ** do { b } while (t);
6940 ** note: body passed as first argument
6943 exprNode exprNode_doWhile (/*@only@*/ exprNode b, /*@only@*/ exprNode t)
6947 if (exprNode_isError (t))
6949 if (exprNode_isError (b))
6951 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
6955 ret = exprNode_createPartialCopy (b);
6957 ret->exitCode = exitkind_makeConditional (b->exitCode);
6958 exprNode_checkUse (ret, b->sref, b->loc);
6959 ret->exitCode = b->exitCode;
6960 ret->canBreak = b->canBreak;
6961 ret->mustBreak = b->mustBreak;
6966 ret = exprNode_createPartialCopy (t);
6967 exprNode_checkPred (cstring_makeLiteralTemp ("while"), t);
6969 if (!exprNode_isError (b))
6972 ** forgot the copy's --- why wasn't this detected??
6975 ret->sets = sRefSet_copyInto (ret->sets, b->sets);
6976 ret->msets = sRefSet_copyInto (ret->msets, b->msets);
6977 ret->uses = sRefSet_copyInto (ret->uses, b->uses);
6979 /* left this out --- causes and aliasing bug (infinite loop)
6980 should be detected?? */
6982 exprNode_checkUse (ret, b->sref, b->loc);
6983 exprNode_mergeUSs (ret, t);
6984 exprNode_checkUse (ret, t->sref, t->loc);
6986 ret->exitCode = b->exitCode;
6987 ret->canBreak = b->canBreak;
6988 ret->mustBreak = b->mustBreak;
6992 context_exitDoWhileClause (t);
6994 ret->kind = XPR_DOWHILE;
6995 ret->edata = exprData_makePair (t, b);
6999 exprNode exprNode_for (/*@keep@*/ exprNode inc, /*@keep@*/ exprNode body)
7002 bool emptyErr = FALSE;
7004 if (context_maybeSet (FLG_FOREMPTY))
7006 if (exprNode_isEmptyStatement (body))
7008 emptyErr = optgenerror
7011 ("Body of for statement is empty"),
7012 exprNode_loc (body));
7016 if (!emptyErr && context_maybeSet (FLG_FORBLOCK))
7018 if (exprNode_isDefined (body)
7019 && !exprNode_isBlock (body))
7021 if (context_inIterDef ()
7022 && (body->kind == XPR_STMTLIST
7023 || body->kind == XPR_TOK))
7029 voptgenerror (FLG_FORBLOCK,
7031 ("Body of for statement is not a block: %s",
7032 exprNode_unparse (body)),
7033 exprNode_loc (body));
7039 ** for ud purposes: (alreadly) init -> test -> (now) LOOP: body + inc + test
7042 if (exprNode_isError (body))
7044 ret = exprNode_createPartialCopy (inc);
7048 ret = exprNode_createPartialCopy (body);
7050 ret->exitCode = exitkind_makeConditional (body->exitCode);
7052 exprNode_mergeUSs (inc, body);
7054 if (exprNode_isDefined (inc))
7058 context_setMessageAnnote (cstring_makeLiteral ("in post loop increment"));
7061 tmp = exprNode_effect (exprData_getTripleInc (inc->edata));
7062 exprNode_freeShallow (tmp);
7064 context_clearMessageAnnote ();
7065 context_setMessageAnnote (cstring_makeLiteral ("in post loop test"));
7067 tmp = exprNode_effect (exprData_getTripleTest (inc->edata));
7068 exprNode_freeShallow (tmp);
7070 context_clearMessageAnnote ();
7072 ret->uses = sRefSet_copyInto (ret->uses, inc->uses);
7073 ret->sets = sRefSet_copyInto (ret->sets, inc->sets);
7074 ret->msets = sRefSet_copyInto (ret->msets, inc->msets);
7078 ret->kind = XPR_FOR;
7079 ret->edata = exprData_makePair (inc, body);
7081 if (exprNode_isDefined (inc)) {
7082 exprNode test = exprData_getTripleTest (inc->edata);
7084 if (exprNode_isUndefined (test)) {
7085 if (exprNode_isDefined (body)) {
7086 if (!body->canBreak) {
7087 /* Really, it means never reached. */
7088 ret->exitCode = XK_MUSTEXIT;
7098 ** for (init; test; inc)
7101 ** while (test) { body; inc; }
7103 ** Now: check use of init (may set vars for test)
7104 ** check use of test
7108 /*@observer@*/ guardSet exprNode_getForGuards (exprNode pred)
7112 if (exprNode_isError (pred)) return guardSet_undefined;
7114 llassert (pred->kind == XPR_FORPRED);
7116 test = exprData_getTripleTest (pred->edata);
7118 if (!exprNode_isError (test))
7120 return (test->guards);
7123 return guardSet_undefined;
7126 exprNode exprNode_whilePred (/*@only@*/ exprNode test)
7128 exprNode ret = exprNode_createSemiCopy (test);
7130 if (exprNode_isDefined (test))
7132 exprNode_copySets (ret, test);
7133 exprNode_checkPred (cstring_makeLiteralTemp ("while"), test);
7134 exprNode_checkUse (ret, test->sref, test->loc);
7136 exprNode_produceGuards (test);
7138 ret->guards = guardSet_copy (test->guards);
7141 ret->edata = exprData_makeSingle (test);
7142 ret->kind = XPR_WHILEPRED;
7146 exprNode exprNode_forPred (/*@only@*/ exprNode init, /*@only@*/ exprNode test,
7147 /*@only@*/ exprNode inc)
7152 ** for ud purposes: init -> test -> LOOP: [ body, inc ]
7155 exprNode_checkPred (cstring_makeLiteralTemp ("for"), test);
7157 if (!exprNode_isError (inc))
7159 ret = exprNode_createPartialCopy (inc);
7163 if (!exprNode_isError (init))
7165 ret = exprNode_createPartialCopy (init);
7167 else if (!exprNode_isError (test))
7169 ret = exprNode_createPartialCopy (test);
7173 ret = exprNode_createUnknown ();
7177 exprNode_mergeUSs (ret, init);
7179 if (exprNode_isDefined (init))
7181 exprNode_checkUse (ret, init->sref, init->loc);
7184 exprNode_mergeUSs (ret, test);
7186 if (exprNode_isDefined (test))
7188 exprNode_checkUse (ret, test->sref, test->loc);
7191 ret->kind = XPR_FORPRED;
7192 ret->edata = exprData_makeFor (init, test, inc);
7196 /*@notnull@*/ /*@only@*/ exprNode exprNode_goto (/*@only@*/ cstring label)
7198 exprNode ret = exprNode_createUnknown ();
7200 if (context_inMacro ())
7202 voptgenerror (FLG_MACROSTMT,
7203 message ("Macro %s uses goto (not functional)",
7204 context_inFunctionName ()),
7208 ret->kind = XPR_GOTO;
7209 ret->edata = exprData_makeLiteral (label);
7210 ret->mustBreak = TRUE;
7211 ret->exitCode = XK_GOTO;
7212 ret->canBreak = TRUE;
7216 exprNode exprNode_continue (/*@only@*/ lltok l, int qcontinue)
7218 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7220 ret->kind = XPR_CONTINUE;
7221 ret->edata = exprData_makeTok (l);
7222 ret->canBreak = TRUE;
7223 ret->mustBreak = TRUE;
7225 if (qcontinue == QSAFEBREAK)
7229 else if (qcontinue == QINNERCONTINUE)
7231 if (!context_inDeepLoop ())
7234 (FLG_LOOPLOOPCONTINUE,
7235 cstring_makeLiteral ("Continue statement marked with innercontinue "
7236 "is not inside a nested loop"),
7237 exprNode_loc (ret));
7240 else if (qcontinue == BADTOK)
7242 if (context_inDeepLoop ())
7245 (FLG_LOOPLOOPCONTINUE,
7246 cstring_makeLiteral ("Continue statement in nested loop"),
7247 exprNode_loc (ret));
7252 llbuglit ("exprNode_continue: bad qcontinue");
7258 exprNode exprNode_break (/*@only@*/ lltok l, int bqual)
7260 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7261 clause breakClause = context_breakClause ();
7263 ret->kind = XPR_BREAK;
7264 ret->edata = exprData_makeTok (l);
7265 ret->canBreak = TRUE;
7266 ret->mustBreak = TRUE;
7268 if (breakClause == NOCLAUSE)
7272 cstring_makeLiteral ("Break not inside while, for or switch statement"),
7273 exprNode_loc (ret));
7277 if (bqual != BADTOK)
7284 if (breakClause == SWITCHCLAUSE)
7286 if (!context_inDeepSwitch ())
7288 voptgenerror (FLG_SYNTAX,
7290 ("Break preceded by innerbreak is not in a deep switch"),
7291 exprNode_loc (ret));
7296 if (!context_inDeepLoop ())
7298 voptgenerror (FLG_SYNTAX,
7300 ("Break preceded by innerbreak is not in a deep loop"),
7301 exprNode_loc (ret));
7306 if (breakClause == SWITCHCLAUSE)
7308 voptgenerror (FLG_SYNTAX,
7310 ("Break preceded by loopbreak is breaking a switch"),
7311 exprNode_loc (ret));
7315 if (breakClause != SWITCHCLAUSE)
7319 message ("Break preceded by switchbreak is breaking %s",
7320 cstring_makeLiteralTemp
7321 ((breakClause == WHILECLAUSE
7322 || breakClause == DOWHILECLAUSE) ? "a while loop"
7323 : (breakClause == FORCLAUSE) ? "a for loop"
7324 : (breakClause == ITERCLAUSE) ? "an iterator"
7326 exprNode_loc (ret));
7334 if (breakClause == SWITCHCLAUSE)
7336 clause nextBreakClause = context_nextBreakClause ();
7338 switch (nextBreakClause)
7340 case NOCLAUSE: break;
7346 (FLG_LOOPSWITCHBREAK,
7347 cstring_makeLiteral ("Break statement in switch inside loop"),
7348 exprNode_loc (ret));
7352 (FLG_SWITCHSWITCHBREAK,
7353 cstring_makeLiteral ("Break statement in switch inside switch"),
7354 exprNode_loc (ret));
7361 if (context_inDeepLoop ())
7365 cstring_makeLiteral ("Break statement in nested loop"),
7366 exprNode_loc (ret));
7370 if (context_inDeepLoopSwitch ())
7373 (FLG_SWITCHLOOPBREAK,
7374 cstring_makeLiteral ("Break statement in loop inside switch"),
7375 exprNode_loc (ret));
7385 exprNode exprNode_nullReturn (/*@only@*/ lltok t)
7387 fileloc loc = lltok_getLoc (t);
7388 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
7390 context_returnFunction ();
7391 exprChecks_checkNullReturn (loc);
7393 ret->kind = XPR_NULLRETURN;
7394 ret->edata = exprData_makeTok (t);
7395 ret->exitCode = XK_MUSTRETURN;
7399 exprNode exprNode_return (/*@only@*/ exprNode e)
7403 if (exprNode_isError (e))
7405 ret = exprNode_createUnknown ();
7409 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (e->loc));
7411 exprNode_checkUse (ret, e->sref, e->loc);
7412 exprNode_checkReturn (e);
7415 context_returnFunction ();
7416 ret->kind = XPR_RETURN;
7417 ret->edata = exprData_makeSingle (e);
7418 ret->exitCode = XK_MUSTRETURN;
7423 exprNode exprNode_comma (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
7427 if (exprNode_isError (e1))
7429 if (exprNode_isError (e2))
7431 ret = exprNode_createLoc (ctype_unknown, g_currentloc);
7435 ret = exprNode_createPartialCopy (e2);
7436 exprNode_checkUse (ret, e2->sref, e2->loc);
7437 ret->sref = e2->sref;
7442 ret = exprNode_createPartialCopy (e1);
7444 exprNode_checkUse (ret, e1->sref, e1->loc);
7446 if (!exprNode_isError (e2))
7448 exprNode_mergeUSs (ret, e2);
7449 exprNode_checkUse (ret, e2->sref, e2->loc);
7450 ret->sref = e2->sref;
7454 ret->kind = XPR_COMMA;
7455 ret->edata = exprData_makePair (e1, e2);
7457 if (exprNode_isDefined (e1))
7459 if (exprNode_isDefined (e2))
7463 if (exprNode_mustEscape (e1) || e1->mustBreak)
7467 message ("Second clause of comma expression is unreachable: %s",
7468 exprNode_unparse (e2)),
7472 ret->exitCode = exitkind_combine (e1->exitCode, e2->exitCode);
7473 ret->mustBreak = e1->mustBreak || e2->mustBreak;
7474 ret->canBreak = e1->canBreak || e2->canBreak;
7478 if (exprNode_mustEscape (e1) || e1->mustBreak)
7482 message ("Second clause of comma expression is unreachable: %s",
7483 exprNode_unparse (e2)),
7487 ret->exitCode = e1->exitCode;
7488 ret->canBreak = e1->canBreak;
7493 if (exprNode_isDefined (e2))
7495 ret->exitCode = e2->exitCode;
7496 ret->mustBreak = e2->mustBreak;
7497 ret->canBreak = e2->canBreak;
7504 static bool exprNode_checkOneInit (/*@notnull@*/ exprNode el, exprNode val)
7506 ctype t1 = exprNode_getType (el);
7507 ctype t2 = exprNode_getType (val);
7508 bool hasError = FALSE;
7510 if (ctype_isUnknown (t1))
7512 voptgenerror (FLG_IMPTYPE,
7513 message ("Variable has unknown (implicitly int) type: %s",
7514 exprNode_unparse (el)),
7518 el->typ = ctype_int;
7521 if (exprNode_isDefined (val) && val->kind == XPR_INITBLOCK)
7523 exprNodeList vals = exprData_getArgs (val->edata);
7525 if (ctype_isRealAP (t1))
7530 exprNodeList_elements (vals, oneval)
7532 cstring istring = message ("%d", i);
7535 (exprNode_fakeCopy (el),
7536 exprNode_numLiteral (ctype_int, istring,
7537 fileloc_copy (el->loc), i));
7539 if (exprNode_isDefined (newel))
7541 if (exprNodeList_size (vals) == 1
7542 && ctype_isString (exprNode_getType (oneval))
7543 && ctype_isChar (exprNode_getType (newel)))
7545 exprNode_freeIniter (newel);
7549 if (exprNode_checkOneInit (newel, oneval))
7554 if (nerrors > 3 && exprNodeList_size (vals) > 6)
7557 (message ("Additional initialization errors "
7558 "for %s not reported",
7559 exprNode_unparse (el)),
7561 exprNode_freeIniter (newel);
7566 exprNode_freeIniter (newel);
7571 exprNode_freeIniter (newel);
7576 cstring_free (istring);
7579 } end_exprNodeList_elements;
7582 else if (ctype_isStruct (ctype_realType (t1)))
7584 uentryList fields = ctype_getFields (t1);
7587 if (uentryList_size (fields) != exprNodeList_size (vals))
7589 if (uentryList_size (fields) > exprNodeList_size (vals))
7591 hasError = optgenerror
7593 message ("Initializer block for "
7594 "%s has %d field%&, but %s has %d field%&: %q",
7595 exprNode_unparse (el),
7596 exprNodeList_size (vals),
7598 uentryList_size (fields),
7599 exprNodeList_unparse (vals)),
7604 hasError = optgenerror
7606 message ("Initializer block for "
7607 "%s has %d field%&, but %s has %d field%&: %q",
7608 exprNode_unparse (el),
7609 exprNodeList_size (vals),
7611 uentryList_size (fields),
7612 exprNodeList_unparse (vals)),
7618 exprNodeList_elements (vals, oneval)
7620 uentry thisfield = uentryList_getN (fields, i);
7622 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
7624 uentry_getName (thisfield));
7626 if (exprNode_isDefined (newel))
7628 if (exprNode_checkOneInit (newel, oneval))
7633 exprNode_freeIniter (newel);
7637 } end_exprNodeList_elements;
7642 hasError = optgenerror
7644 message ("Initializer block used for "
7645 "%s where %t is expected: %s",
7646 exprNode_unparse (el), t1, exprNode_unparse (val)),
7652 if (exprNode_isDefined (val))
7654 doAssign (el, val, TRUE);
7656 if (!exprNode_matchType (t1, val))
7658 hasError = gentypeerror
7660 message ("Initial value of %s is type %t, "
7662 exprNode_unparse (el),
7663 t2, t1, exprNode_unparse (val)),
7673 exprNode_makeInitializationAux (/*@temp@*/ idDecl t)
7677 if (usymtab_exists (idDecl_observeId (t)))
7679 uentry ue = usymtab_lookup (idDecl_observeId (t));
7680 ret = exprNode_createId (ue);
7682 /*@i723 don't do this...but why? */
7684 ct = ctype_realishType (ret->typ);
7686 DPRINTF (("Type: %s", ctype_unparse (ret->typ)));
7688 if (ctype_isUnknown (ct))
7690 if (uentry_isAnyTag (ue))
7694 message ("%s used but not previously declared: %s",
7695 uentry_ekindName (ue),
7696 idDecl_getName (t)),
7704 message ("Variable has unknown (implicitly int) type: %s",
7705 idDecl_getName (t)),
7715 uentry ue = uentry_makeUnrecognized (idDecl_observeId (t),
7717 ret = exprNode_fromIdentifierAux (ue);
7720 ** No error - this happens in old style declarations:
7724 message ("Unrecognized identifier in intializer: %s", idDecl_observeId (t)),
7731 exprData_free (ret->edata, ret->kind);
7732 ret->edata = exprData_undefined;
7733 ret->exitCode = XK_NEVERESCAPE;
7734 ret->mustBreak = FALSE;
7735 ret->kind = XPR_INIT;
7739 exprNode exprNode_makeEmptyInitialization (/*@only@*/ idDecl t)
7741 exprNode ret = exprNode_makeInitializationAux (t);
7742 llassert (ret->edata == exprData_undefined);
7743 ret->edata = exprData_makeInit (t, exprNode_undefined);
7747 exprNode exprNode_makeInitialization (/*@only@*/ idDecl t,
7748 /*@only@*/ exprNode e)
7750 uentry ue = usymtab_lookup (idDecl_observeId (t));
7751 exprNode ret = exprNode_makeInitializationAux (t);
7752 fileloc loc = exprNode_loc (e);
7754 if (exprNode_isError (e))
7756 e = exprNode_createUnknown ();
7759 /* error: assume initializer is defined */
7760 sRef_setDefined (ret->sref, g_currentloc);
7764 ctype ct = ctype_realishType (ret->typ);
7769 ** was addSafeUse --- what's the problem?
7771 ** int x = 3, y = x ?
7774 exprData_free (ret->edata, ret->kind);
7775 ret->edata = exprData_makeInit (t, e);
7777 exprNode_checkUse (ret, e->sref, e->loc);
7779 if (ctype_isUnknown (e->typ) && uentry_isValid (ue))
7781 exprNode lhs = exprNode_createId (ue);
7784 ** static storage should be undefined before initializing
7787 if (uentry_isStatic (ue))
7789 sRef_setDefState (lhs->sref, SS_PARTIAL, fileloc_undefined);
7792 (void) exprNode_checkOneInit (lhs, e);
7794 if (uentry_isStatic (ue))
7796 sRef_setDefState (lhs->sref, SS_DEFINED, fileloc_undefined);
7799 exprNode_free (lhs);
7803 if (!exprNode_matchType (ct, e))
7805 if (exprNode_isZero (e) && ctype_isArrayPtr (ct))
7812 (exprNode_getType (e), e, exprNode_getType (ret), ret,
7814 ("Variable %q initialized to type %t, expects %t: %s",
7815 uentry_getName (ue), exprNode_getType (e),
7816 exprNode_getType (ret),
7817 exprNode_unparse (e)),
7823 if (uentry_isStatic (ue))
7825 sRef_setDefState (ret->sref, SS_PARTIAL, fileloc_undefined);
7828 doAssign (ret, e, TRUE);
7830 if (uentry_isStatic (ue))
7832 sRef_setDefState (ret->sref, SS_DEFINED, fileloc_undefined);
7836 if (context_inIterDef ())
7838 /* should check if it is yield */
7839 uentry_setUsed (ue, loc);
7846 exprNode_mergeUSs (ret, e);
7850 exprNode exprNode_iter (/*@observer@*/ uentry name,
7851 /*@only@*/ exprNodeList alist,
7852 /*@only@*/ exprNode body,
7853 /*@observer@*/ uentry end)
7858 llassert (uentry_isValid (name));
7860 uentry_setUsed (name, exprNode_loc (body));
7862 ret = exprNode_createPartialCopy (body);
7863 iname = uentry_getName (name);
7865 if (uentry_isInvalid (end))
7868 message ("Iter %s not balanced with end_%s", iname, iname));
7872 cstring ename = uentry_getName (end);
7874 if (!cstring_equalPrefix (ename, "end_"))
7876 llerror (FLG_ITER, message ("Iter %s not balanced with end_%s: %s",
7877 iname, iname, ename));
7881 if (!cstring_equal (iname, cstring_suffix (ename, 4)))
7884 message ("Iter %s not balanced with end_%s: %s",
7885 iname, iname, ename));
7889 cstring_free (ename);
7892 context_exitIterClause (body);
7894 ret->kind = XPR_ITER;
7895 ret->edata = exprData_makeIter (name, alist, body, end);
7897 if (uentry_isIter (name))
7899 (void) checkArgsReal (name, body,
7900 uentry_getParams (name), alist, TRUE, ret);
7903 cstring_free (iname);
7909 exprNode_iterNewId (/*@only@*/ cstring s)
7911 exprNode e = exprNode_new ();
7912 uentry ue = uentryList_getN (uentry_getParams (getCurrentIter ()), iterParamNo ());
7914 llassert (processingIterVars ());
7916 e->loc = context_getSaveLocation ();
7918 if (fileloc_isUndefined (e->loc))
7920 fileloc_free (e->loc);
7921 e->loc = fileloc_copy (g_currentloc);
7924 e->uses = sRefSet_new ();
7925 e->sets = sRefSet_new ();
7926 e->msets = sRefSet_new ();
7928 e->val = multiVal_unknown ();
7929 e->guards = guardSet_new ();
7931 e->isJumpPoint = FALSE;
7932 e->exitCode = XK_NEVERESCAPE;
7934 /*> missing fields, detected by lclint <*/
7935 e->canBreak = FALSE;
7936 e->mustBreak = FALSE;
7937 e->etext = cstring_undefined;
7939 if (uentry_isYield (ue))
7941 uentry uue = uentry_makeVariable (s, uentry_getType (ue),
7942 fileloc_copy (e->loc),
7946 uue = usymtab_supEntrySrefReturn (uue);
7948 sr = uentry_getSref (uue);
7949 sRef_mergeStateQuiet (sr, uentry_getSref (ue));
7950 sr = uentry_getSref (uue);
7951 sRef_setDefined (sr, e->loc);
7953 e->typ = uentry_getType (uue);
7955 e->edata = exprData_makeId (uue);
7956 uentry_setUsed (uue, g_currentloc);
7962 sRef_setGlobalScope ();
7963 uue = uentry_makeVariableLoc (s, ctype_unknown);
7965 e->typ = ctype_unknown;
7966 e->edata = exprData_makeId (uue);
7968 uentry_setUsed (uue, e->loc);
7969 uentry_setHasNameError (uue);
7971 if (context_getFlag (FLG_REPEATUNRECOG))
7973 uentry_markOwned (uue);
7977 usymtab_supGlobalEntry (uue);
7980 sRef_clearGlobalScope ();
7982 voptgenerror (FLG_UNRECOG, message ("Unrecognized identifier: %s", s),
7989 exprNode_defineConstraints(e);
7994 exprNode_iterExpr (/*@returned@*/ exprNode e)
7996 if (!processingIterVars ())
7998 llcontbuglit ("checkIterParam: not in iter");
8002 if (uentry_isYield (uentryList_getN (uentry_getParams (getCurrentIter ()),
8005 if (exprNode_isDefined (e))
8007 if (fileloc_isDefined (e->loc))
8011 message ("Yield parameter is not simple identifier: %s",
8012 exprNode_unparse (e)),
8019 message ("Yield parameter is not simple identifier: %s",
8020 exprNode_unparse (e)),
8030 exprNode_iterId (/*@observer@*/ uentry c)
8034 llassert (processingIterVars ());
8036 ue = uentryList_getN (uentry_getParams (getCurrentIter ()),
8039 if (uentry_isYield (ue))
8041 ctype ct = uentry_getType (ue);
8042 exprNode e = exprNode_createPlain (ct);
8043 cstring name = uentry_getName (c);
8044 uentry le = uentry_makeVariable (name, ct, fileloc_undefined, FALSE);
8046 uentry_setUsed (ue, g_currentloc);
8047 uentry_setHasNameError (ue);
8049 cstring_free (name);
8052 e->edata = exprData_makeId (le);
8053 e->loc = context_getSaveLocation ();
8054 e->sref = uentry_getSref (le);
8056 usymtab_supEntrySref (le);
8058 if (!context_inHeader ())
8062 message ("Yield parameter shadows local declaration: %q",
8063 uentry_getName (c)),
8064 fileloc_isDefined (e->loc) ? e->loc : g_currentloc))
8066 uentry_showWhereDeclared (c);
8073 return (exprNode_fromIdentifierAux (c));
8076 exprNode exprNode_iterStart (/*@observer@*/ uentry name, /*@only@*/ exprNodeList alist)
8078 exprNode ret = exprNode_create (ctype_unknown);
8080 ret->kind = XPR_ITERCALL;
8081 ret->edata = exprData_makeIterCall (name, alist);
8083 if (uentry_isIter (name))
8085 uentryList params = uentry_getParams (name);
8087 if (context_inIterDef ()
8088 && uentryList_size (params) == exprNodeList_size (alist))
8092 exprNodeList_elements (alist, arg)
8094 uentry parg = uentryList_getN (params, i);
8096 if (uentry_isYield (parg))
8098 uentry ue = exprNode_getUentry (arg);
8100 if (uentry_isValid (ue))
8107 } end_exprNodeList_elements;
8110 (void) checkArgsReal (name, ret, params, alist, TRUE, ret);
8111 checkUnspecCall (ret, params, alist);
8117 /*@exposed@*/ sRef exprNode_getSref (exprNode e)
8119 if (exprNode_isDefined (e))
8122 if (e->sref == defref) /*@noaccess sRef@*/
8125 e->sref = sRef_makeUnknown ();
8126 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
8137 return sRef_undefined;
8141 /*@observer@*/ cstring
8142 exprNode_unparseFirst (exprNode e)
8144 if (exprNode_isDefined (e))
8148 if (e->kind == XPR_STMTLIST
8149 || e->kind == XPR_COMMA || e->kind == XPR_COND)
8151 exprNode first = exprData_getPairA (e->edata);
8153 if (exprNode_isDefined (first))
8155 return (exprNode_unparseFirst (exprData_getPairA (e->edata)));
8159 return (cstring_makeLiteralTemp ("..."));
8163 ret = cstring_elide (exprNode_unparse (e), 20);
8164 cstring_markOwned (ret);
8170 return cstring_makeLiteralTemp ("<error>");
8174 /*@observer@*/ cstring
8175 exprNode_unparse (/*@temp@*/ exprNode e)
8177 if (exprNode_isError (e))
8179 return cstring_makeLiteralTemp ("<error>");
8182 if (cstring_isDefined (e->etext))
8188 cstring ret = exprNode_doUnparse (e);
8190 /*@-modifies@*/ /* benevolent */
8197 /*@observer@*/ fileloc
8198 exprNode_loc (exprNode e)
8200 if (exprNode_isError (e))
8202 return (g_currentloc);
8211 ** executes exprNode e
8212 ** recursively rexecutes as though in original parse using
8213 ** information in e->edata
8216 static /*@only@*/ exprNodeList exprNodeList_effect (exprNodeList e)
8218 exprNodeList ret = exprNodeList_new ();
8220 exprNodeList_elements (e, current)
8222 exprNodeList_addh (ret, exprNode_effect (current));
8223 } end_exprNodeList_elements;
8228 static /*@only@*/ exprNode exprNode_effect (exprNode e)
8229 /*@globals internalState@*/
8231 bool innerEffect = inEffect;
8237 context_clearJustPopped ();
8239 if (exprNode_isError (e))
8241 ret = exprNode_undefined;
8246 ** Turn off expose and dependent transfer checking.
8247 ** Need to pass exposed internal nodes,
8248 ** [ copying would be a waste! ]
8249 ** [ Actually, I think I wasted a lot more time than its worth ]
8250 ** [ trying to do this. ]
8254 /*@-observertrans@*/
8255 /*@-dependenttrans@*/
8262 ret = exprNode_addParens (exprData_getUopTok (data),
8263 exprNode_effect (exprData_getUopNode (data)));
8266 ret = exprNode_assign (exprNode_effect (exprData_getOpA (data)),
8267 exprNode_effect (exprData_getOpB (data)),
8268 exprData_getOpTok (data));
8271 ret = exprNode_undefined;
8274 ret = exprNode_functionCall (exprNode_effect (exprData_getFcn (data)),
8275 exprNodeList_effect (exprData_getArgs (data)));
8288 cstring id = exprData_getId (data);
8289 uentry ue = usymtab_lookupSafe (id);
8291 ret = exprNode_fromIdentifierAux (ue);
8292 ret->loc = fileloc_update (ret->loc, e->loc);
8299 ret = exprNode_arrayFetch (exprNode_effect (exprData_getPairA (data)),
8300 exprNode_effect (exprData_getPairB (data)));
8303 ret = exprNode_op (exprNode_effect (exprData_getOpA (data)),
8304 exprNode_effect (exprData_getOpB (data)),
8305 exprData_getOpTok (data));
8309 ret = exprNode_postOp (exprNode_effect (exprData_getUopNode (data)),
8310 exprData_getUopTok (data));
8313 ret = exprNode_preOp (exprNode_effect (exprData_getUopNode (data)),
8314 exprData_getUopTok (data));
8326 ret = exprNode_vaArg (exprData_getCastTok (data),
8327 exprNode_effect (exprData_getCastNode (data)),
8328 exprData_getCastType (data));
8332 ret = exprNode_cast (exprData_getCastTok (data),
8333 exprNode_effect (exprData_getCastNode (data)),
8334 exprData_getCastType (data));
8337 ret = exprNode_iterStart (exprData_getIterCallIter (data),
8339 (exprData_getIterCallArgs (data)));
8343 ret = exprNode_iter (exprData_getIterSname (data),
8344 exprNodeList_effect (exprData_getIterAlist (data)),
8345 exprNode_effect (exprData_getIterBody (data)),
8346 exprData_getIterEname (data));
8350 ret = exprNode_for (exprNode_effect (exprData_getPairA (data)),
8351 exprNode_effect (exprData_getPairB (data)));
8355 ret = exprNode_forPred (exprNode_effect (exprData_getTripleInit (data)),
8356 exprNode_effect (exprData_getTripleTest (data)),
8357 exprNode_effect (exprData_getTripleInc (data)));
8361 ret = exprNode_createTok (exprData_getTok (data));
8365 ret = exprNode_goto (exprData_getLiteral (data));
8366 ret->loc = fileloc_update (ret->loc, e->loc);
8370 ret = exprNode_continue (exprData_getTok (data), QSAFEBREAK);
8374 ret = exprNode_break (exprData_getTok (data), QSAFEBREAK);
8378 ret = exprNode_return (exprNode_effect (exprData_getSingle (data)));
8381 case XPR_NULLRETURN:
8382 ret = exprNode_nullReturn (exprData_getTok (data));
8386 ret = exprNode_comma (exprNode_effect (exprData_getPairA (data)),
8387 exprNode_effect (exprData_getPairB (data)));
8391 ret = exprNode_cond (exprNode_effect (exprData_getTriplePred (data)),
8392 exprNode_effect (exprData_getTripleTrue (data)),
8393 exprNode_effect (exprData_getTripleFalse (data)));
8396 ret = exprNode_if (exprNode_effect (exprData_getPairA (data)),
8397 exprNode_effect (exprData_getPairB (data)));
8401 ret = exprNode_ifelse (exprNode_effect (exprData_getTriplePred (data)),
8402 exprNode_effect (exprData_getTripleTrue (data)),
8403 exprNode_effect (exprData_getTripleFalse (data)));
8406 ret = exprNode_whilePred (exprData_getSingle (data));
8410 ret = exprNode_while (exprNode_effect (exprData_getPairA (data)),
8411 exprNode_effect (exprData_getPairB (data)));
8415 ret = exprNode_doWhile (exprNode_effect (exprData_getPairA (data)),
8416 exprNode_effect (exprData_getPairB (data)));
8420 ret = exprNode_makeBlock (exprNode_effect (exprData_getSingle (data)));
8424 ret = exprNode_statement (exprNode_effect (exprData_getUopNode (data)),
8425 exprData_getUopTok (data));
8429 ret = exprNode_concat (exprNode_effect (exprData_getPairA (data)),
8430 exprNode_effect (exprData_getPairB (data)));
8435 ret = exprNode_caseMarker
8436 (exprNode_effect (exprData_getSingle (data)),
8442 ret = exprNode_createTok (exprData_getTok (data));
8446 ret = exprNode_switch (exprNode_effect (exprData_getPairA (data)),
8447 exprNode_effect (exprData_getPairB (data)));
8451 ret = exprNode_makeInitialization
8452 (exprData_getInitId (data),
8453 exprNode_effect (exprData_getInitNode (data)));
8457 ret = exprNode_fieldAccessAux
8458 (exprNode_effect (exprData_getFieldNode (data)),
8459 exprNode_loc (exprData_getFieldNode (data)),
8460 cstring_copy (exprData_getFieldName (data)));
8464 ret = exprNode_arrowAccessAux
8465 (exprNode_effect (exprData_getFieldNode (data)),
8466 exprNode_loc (exprData_getFieldNode (data)),
8467 cstring_copy (exprData_getFieldName (data)));
8470 case XPR_STRINGLITERAL:
8484 /*@=observertrans@*/
8486 /*@=dependenttrans@*/
8497 static /*@observer@*/ cstring exprNode_rootVarName (exprNode e)
8502 if (exprNode_isError (e))
8504 return cstring_undefined;
8512 ret = exprNode_rootVarName (exprData_getUopNode (data));
8515 ret = exprNode_rootVarName (exprData_getOpA (data));
8519 ret = exprData_getId (data);
8544 case XPR_NULLRETURN:
8567 case XPR_STRINGLITERAL:
8568 ret = cstring_undefined;
8575 static /*@only@*/ cstring exprNode_doUnparse (exprNode e)
8580 if (exprNode_isError (e))
8582 static /*@only@*/ cstring error = cstring_undefined;
8584 if (!cstring_isDefined (error))
8586 error = cstring_makeLiteral ("<error>");
8597 ret = message ("(%s)", exprNode_unparse (exprData_getUopNode (e->edata)));
8600 ret = message ("%s %s %s",
8601 exprNode_unparse (exprData_getOpA (data)),
8602 lltok_unparse (exprData_getOpTok (data)),
8603 exprNode_unparse (exprData_getOpB (data)));
8606 ret = message ("%s(%q)",
8607 exprNode_unparse (exprData_getFcn (data)),
8608 exprNodeList_unparse (exprData_getArgs (data)));
8611 ret = message ("{ %q }", exprNodeList_unparse (exprData_getArgs (data)));
8614 ret = cstring_undefined;
8617 ret = message ("%s:", exprData_getId (data));
8621 ret = cstring_copy (exprData_getId (data));
8624 ret = message ("%s[%s]", exprNode_unparse (exprData_getPairA (data)),
8625 exprNode_unparse (exprData_getPairB (data)));
8628 ret = message ("<body>");
8631 ret = message ("%s %s %s",
8632 exprNode_unparse (exprData_getOpA (data)),
8633 lltok_unparse (exprData_getOpTok (data)),
8634 exprNode_unparse (exprData_getOpB (data)));
8638 ret = message ("%s%s",
8639 lltok_unparse (exprData_getUopTok (data)),
8640 exprNode_unparse (exprData_getUopNode (data)));
8644 ret = message ("%s%s",
8645 exprNode_unparse (exprData_getUopNode (data)),
8646 lltok_unparse (exprData_getUopTok (data)));
8650 ret = message ("offsetof(%s,%q)",
8651 ctype_unparse (qtype_getType (exprData_getOffsetType (data))),
8652 cstringList_unparseSep (exprData_getOffsetName (data), cstring_makeLiteralTemp (".")));
8656 ret = message ("sizeof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
8660 ret = message ("sizeof(%s)", exprNode_unparse (exprData_getSingle (data)));
8664 ret = message ("alignof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
8668 ret = message ("alignof(%s)", exprNode_unparse (exprData_getSingle (data)));
8672 ret = message ("va_arg(%s, %q)",
8673 exprNode_unparse (exprData_getCastNode (data)),
8674 qtype_unparse (exprData_getCastType (data)));
8678 ret = message ("%q(%q)",
8679 uentry_getName (exprData_getIterCallIter (data)),
8680 exprNodeList_unparse (exprData_getIterCallArgs (data)));
8683 ret = message ("%q(%q) %s %q",
8684 uentry_getName (exprData_getIterSname (data)),
8685 exprNodeList_unparse (exprData_getIterAlist (data)),
8686 exprNode_unparse (exprData_getIterBody (data)),
8687 uentry_getName (exprData_getIterEname (data)));
8690 ret = message ("(%q)%s",
8691 qtype_unparse (exprData_getCastType (data)),
8692 exprNode_unparse (exprData_getCastNode (data)));
8696 ret = message ("%s %s",
8697 exprNode_unparse (exprData_getPairA (data)),
8698 exprNode_unparse (exprData_getPairB (data)));
8702 ret = message ("for (%s; %s; %s)",
8703 exprNode_unparse (exprData_getTripleInit (data)),
8704 exprNode_unparse (exprData_getTripleTest (data)),
8705 exprNode_unparse (exprData_getTripleInc (data)));
8709 ret = message ("goto %s", exprData_getLiteral (data));
8713 ret = cstring_makeLiteral ("continue");
8717 ret = cstring_makeLiteral ("break");
8721 ret = message ("return %s", exprNode_unparse (exprData_getSingle (data)));
8724 case XPR_NULLRETURN:
8725 ret = cstring_makeLiteral ("return");
8729 ret = message ("%s, %s",
8730 exprNode_unparse (exprData_getPairA (data)),
8731 exprNode_unparse (exprData_getPairB (data)));
8735 ret = message ("%s ? %s : %s",
8736 exprNode_unparse (exprData_getTriplePred (data)),
8737 exprNode_unparse (exprData_getTripleTrue (data)),
8738 exprNode_unparse (exprData_getTripleFalse (data)));
8741 ret = message ("if (%s) %s",
8742 exprNode_unparse (exprData_getPairA (data)),
8743 exprNode_unparse (exprData_getPairB (data)));
8747 ret = message ("if (%s) %s else %s",
8748 exprNode_unparse (exprData_getTriplePred (data)),
8749 exprNode_unparse (exprData_getTripleTrue (data)),
8750 exprNode_unparse (exprData_getTripleFalse (data)));
8753 ret = message ("while (%s) %s",
8754 exprNode_unparse (exprData_getPairA (data)),
8755 exprNode_unparse (exprData_getPairB (data)));
8759 ret = cstring_copy (exprNode_unparse (exprData_getSingle (data)));
8763 ret = cstring_copy (lltok_unparse (exprData_getTok (data)));
8767 ret = message ("do { %s } while (%s)",
8768 exprNode_unparse (exprData_getPairB (data)),
8769 exprNode_unparse (exprData_getPairA (data)));
8773 ret = message ("{ %s }", exprNode_unparseFirst (exprData_getSingle (data)));
8777 ret = cstring_copy (exprNode_unparse (exprData_getUopNode (data)));
8781 ret = message ("%s; %s",
8782 exprNode_unparse (exprData_getPairA (data)),
8783 exprNode_unparse (exprData_getPairB (data)));
8788 ret = cstring_makeLiteral ("default:");
8792 ret = message ("switch (%s) %s",
8793 exprNode_unparse (exprData_getPairA (data)),
8794 exprNode_unparse (exprData_getPairB (data)));
8799 ret = message ("case %s:",
8800 exprNode_unparse (exprData_getSingle (data)));
8804 if (exprNode_isError (exprData_getInitNode (data)))
8806 ret = message ("%q", idDecl_unparseC (exprData_getInitId (data)));
8810 ret = message ("%q = %s",
8811 idDecl_unparseC (exprData_getInitId (data)),
8812 exprNode_unparse (exprData_getInitNode (data)));
8817 ret = message ("%s.%s",
8818 exprNode_unparse (exprData_getFieldNode (data)),
8819 exprData_getFieldName (data));
8823 ret = message ("%s->%s",
8824 exprNode_unparse (exprData_getFieldNode (data)),
8825 exprData_getFieldName (data));
8828 case XPR_STRINGLITERAL:
8829 ret = message ("\"%s\"", exprData_getLiteral (data));
8833 ret = cstring_copy (exprData_getLiteral (data));
8837 ret = cstring_makeLiteral ("<node>");
8845 exprNode_isInitializer (exprNode e)
8847 return (exprNode_isDefined (e)
8848 && e->kind == XPR_INIT);
8852 exprNode_isCharLit (exprNode e)
8854 if (exprNode_isDefined (e))
8856 return (multiVal_isChar (exprNode_getValue (e)));
8865 exprNode_isNumLit (exprNode e)
8867 if (exprNode_isDefined (e))
8869 return (multiVal_isInt (exprNode_getValue (e)));
8878 exprNode_isFalseConstant (exprNode e)
8880 if (exprNode_isDefined (e))
8882 cstring s = exprNode_rootVarName (e);
8884 if (cstring_equal (s, context_getFalseName ()))
8894 exprNode_matchLiteral (ctype expected, exprNode e)
8896 if (exprNode_isDefined (e))
8898 multiVal m = exprNode_getValue (e);
8900 if (multiVal_isDefined (m))
8902 if (multiVal_isInt (m))
8904 long int val = multiVal_forceInt (m);
8906 if (ctype_isDirectBool (ctype_realishType (expected)))
8910 return FALSE; /* really?! return TRUE; allow use of 0 for FALSE */
8918 if (ctype_isRealInt (expected))
8921 ** unsigned <- [ constant >= 0 is okay ]
8924 if (ctype_isUnsigned (expected))
8933 ** No checks on sizes of integers...maybe add
8937 DPRINTF (("Here: %s => %s", exprNode_unparse (e), ctype_unparse (expected)));
8938 DPRINTF (("Type: %s / %s", ctype_unparse (exprNode_getType (e)),
8939 bool_unparse (ctype_isInt (exprNode_getType (e)))));
8941 if (context_getFlag (FLG_NUMLITERAL)
8942 && (ctype_isRegularInt (exprNode_getType (e)) || val == 0)) {
8948 return FALSE; /* evs 2000-05-17: previously, always returned TRUE */
8952 else if (ctype_isChar (expected))
8956 else if (ctype_isArrayPtr (expected))
8960 else if (ctype_isAnyFloat (expected))
8962 return (context_getFlag (FLG_NUMLITERAL));
8969 else if (multiVal_isDouble (m))
8971 if (ctype_isAnyFloat (expected))
8976 else if (multiVal_isChar (m))
8978 char val = multiVal_forceChar (m);
8980 if (ctype_isChar (expected))
8982 if (ctype_isUnsigned (expected) && ((int)val) < 0)
9003 exprNode_matchType (ctype expected, exprNode e)
9007 if (!exprNode_isDefined (e)) return TRUE;
9009 actual = ctype_realishType (exprNode_getType (e));
9011 if (ctype_match (ctype_realishType (expected), actual))
9016 llassert (!exprNode_isError (e));
9017 return (exprNode_matchLiteral (expected, e));
9021 exprNode_matchTypes (exprNode e1, exprNode e2)
9026 if (!exprNode_isDefined (e1)) return TRUE;
9027 if (!exprNode_isDefined (e2)) return TRUE;
9030 ** realish type --- keep bools, bools
9033 t1 = ctype_realishType (exprNode_getType (e1));
9034 t2 = ctype_realishType (exprNode_getType (e2));
9036 if (ctype_match (t1, t2))
9041 return (exprNode_matchLiteral (t1, e2) || exprNode_matchLiteral (t2, e1));
9049 exprNode_matchArgType (ctype ct, exprNode e)
9053 if (!exprNode_isDefined (e))
9058 et = ctype_realType (exprNode_getType (e));
9060 if (ctype_matchArg (ct, et)) return TRUE;
9062 llassert (!exprNode_isError (e));
9063 return (exprNode_matchLiteral (ct, e));
9066 static /*@only@*/ exprNodeSList
9067 exprNode_flatten (/*@dependent@*/ exprNode e) /*@*/
9069 if (exprNode_isDefined (e))
9071 if (e->kind == XPR_STMTLIST)
9073 return (exprNodeSList_append
9074 (exprNode_flatten (exprData_getPairA (e->edata)),
9075 exprNode_flatten (exprData_getPairB (e->edata))));
9077 else if (e->kind == XPR_BLOCK)
9079 return (exprNode_flatten (exprData_getSingle (e->edata)));
9083 return (exprNodeSList_singleton (e));
9087 return exprNodeSList_new ();
9090 static /*@exposed@*/ exprNode
9091 exprNode_lastStatement (/*@returned@*/ exprNode e)
9093 if (exprNode_isDefined (e))
9095 if (e->kind == XPR_STMTLIST)
9097 exprNode b = exprData_getPairB (e->edata);
9099 if (exprNode_isDefined (b))
9101 return exprNode_lastStatement (b);
9105 return exprNode_lastStatement (exprData_getPairA (e->edata));
9108 else if (e->kind == XPR_BLOCK)
9110 return (exprNode_lastStatement (exprData_getSingle (e->edata)));
9118 return exprNode_undefined;
9121 static /*@exposed@*/ exprNode
9122 exprNode_firstStatement (/*@returned@*/ exprNode e)
9124 if (exprNode_isDefined (e))
9126 if (e->kind == XPR_STMTLIST)
9128 exprNode b = exprData_getPairA (e->edata);
9130 if (exprNode_isDefined (b))
9132 return exprNode_firstStatement (b);
9136 return exprNode_firstStatement (exprData_getPairB (e->edata));
9139 else if (e->kind == XPR_BLOCK)
9141 return (exprNode_firstStatement (exprData_getSingle (e->edata)));
9149 return exprNode_undefined;
9153 exprNode_mergeUSs (exprNode res, exprNode other)
9155 if (exprNode_isDefined (res) && exprNode_isDefined (other))
9157 res->msets = sRefSet_union (res->msets, other->msets);
9158 res->sets = sRefSet_union (res->sets, other->sets);
9159 res->uses = sRefSet_union (res->uses, other->uses);
9164 exprNode_mergeCondUSs (exprNode res, exprNode other1, exprNode other2)
9166 if (exprNode_isDefined (res))
9168 if (exprNode_isDefined (other1))
9170 res->sets = sRefSet_union (res->sets, other1->sets);
9171 res->msets = sRefSet_union (res->msets, other1->msets);
9172 res->uses = sRefSet_union (res->uses, other1->uses);
9174 if (exprNode_isDefined (other2))
9176 res->sets = sRefSet_union (res->sets, other2->sets);
9177 res->msets = sRefSet_union (res->msets, other2->msets);
9178 res->uses = sRefSet_union (res->uses, other2->uses);
9186 ** Reports errors is s is not defined.
9190 exprNode_addUse (exprNode e, /*@exposed@*/ sRef s)
9192 if (exprNode_isDefined (e))
9194 e->uses = sRefSet_insert (e->uses, s);
9199 exprNode_checkUse (exprNode e, /*@exposed@*/ sRef s, fileloc loc)
9201 if (sRef_isKnown (s) && !sRef_isConst (s))
9204 ** need to check all outer types are useable
9207 DPRINTF (("Check use: %s / %s",
9208 exprNode_unparse (e), sRef_unparse (s)));
9210 exprNode_addUse (e, s);
9212 if (!context_inProtectVars ())
9215 ** only report the deepest error
9218 sRef errorRef = sRef_undefined;
9219 sRef lastRef = sRef_undefined;
9220 bool deadRef = FALSE;
9221 bool unuseable = FALSE;
9222 bool errorMaybe = FALSE;
9224 while (sRef_isValid (s) && sRef_isKnown (s))
9226 ynm readable = sRef_isReadable (s);
9228 DPRINTF (("Readable: %s / %s",
9229 sRef_unparseFull (s), ynm_unparse (readable)));
9231 if (!(ynm_toBoolStrict (readable)))
9233 if (ynm_isMaybe (readable))
9237 deadRef = sRef_isPossiblyDead (errorRef);
9238 unuseable = sRef_isUnuseable (errorRef);
9245 deadRef = sRef_isDead (errorRef);
9246 unuseable = sRef_isUnuseable (errorRef);
9250 if (!sRef_isPartial (s))
9252 sRef_setDefined (s, fileloc_undefined);
9256 s = sRef_getBaseSafe (s);
9259 if (sRef_isValid (errorRef))
9261 if (sRef_isValid (lastRef) && sRef_isField (lastRef)
9262 && sRef_isPointer (errorRef))
9269 if (sRef_isThroughArrayFetch (errorRef))
9272 (FLG_STRICTUSERELEASED,
9273 message ("%q %q may be used after being released",
9274 sRef_unparseKindNamePlain (errorRef),
9275 sRef_unparse (errorRef)),
9278 sRef_showRefKilled (errorRef);
9280 if (sRef_isKept (errorRef))
9282 sRef_clearAliasState (errorRef, loc);
9288 DPRINTF (("HERE: %s", sRef_unparse (errorRef)));
9292 message ("%q %q %qused after being released",
9293 sRef_unparseKindNamePlain (errorRef),
9294 sRef_unparse (errorRef),
9295 cstring_makeLiteral (errorMaybe
9299 sRef_showRefKilled (errorRef);
9301 if (sRef_isKept (errorRef))
9303 sRef_clearAliasState (errorRef, loc);
9312 message ("%q %q%qused in inconsistent state",
9313 sRef_unparseKindName (errorRef),
9314 sRef_unparseOpt (errorRef),
9315 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9318 sRef_showStateInconsistent (errorRef);
9323 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
9327 message ("%q %q%qused before definition",
9328 sRef_unparseKindName (errorRef),
9329 sRef_unparseOpt (errorRef),
9330 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9334 sRef_setDefined (errorRef, loc);
9336 if (sRef_isAddress (errorRef))
9338 sRef_setDefined (sRef_getRootBase (errorRef), loc);
9340 } /* end is error */
9348 checkSafeUse (exprNode e, /*@exposed@*/ sRef s)
9350 if (exprNode_isDefined (e) && sRef_isKnown (s))
9352 e->uses = sRefSet_insert (e->uses, s);
9357 exprNode_checkSetAny (exprNode e, /*@dependent@*/ cstring name)
9359 if (exprNode_isDefined (e))
9361 e->sets = sRefSet_insert (e->sets, sRef_makeUnconstrained (name));
9366 exprNode_checkSet (exprNode e, /*@exposed@*/ sRef s)
9368 sRef defines = sRef_undefined;
9370 if (sRef_isValid (s) && !sRef_isNothing (s))
9372 uentry ue = sRef_getBaseUentry (s);
9374 if (uentry_isValid (ue))
9376 uentry_setLset (ue);
9379 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
9381 voptgenerror (FLG_USEDEF,
9382 message ("Attempt to set unuseable storage: %q",
9387 if (sRef_isMeaningful (s))
9390 if (sRef_isDead (s))
9392 sRef base = sRef_getBaseSafe (s);
9394 if (sRef_isValid (base)
9395 && sRef_isDead (base))
9397 sRef_setPartial (s, exprNode_loc (e));
9400 defines = s; /* okay - modifies for only param */
9402 else if (sRef_isPartial (s))
9404 sRef eref = exprNode_getSref (e);
9406 if (!sRef_isPartial (eref))
9409 ** should do something different here???
9412 sRef_setDefinedComplete (eref, exprNode_loc (e));
9416 sRef_setPartialDefinedComplete (eref, exprNode_loc (e));
9419 if (sRef_isMeaningful (eref))
9428 else if (sRef_isAllocated (s))
9430 sRef eref = exprNode_getSref (e);
9433 if (!sRef_isAllocated (eref))
9435 sRef_setDefinedComplete (eref, exprNode_loc (e));
9439 sRef base = sRef_getBaseSafe (eref);
9441 if (sRef_isValid (base))
9443 sRef_setPdefined (base, exprNode_loc (e));
9451 sRef_setDefinedNCComplete (s, exprNode_loc (e));
9456 else /* not meaningful...but still need to insert it */
9462 if (exprNode_isDefined (e) && sRef_isValid (defines))
9464 e->sets = sRefSet_insert (e->sets, defines);
9469 exprNode_checkMSet (exprNode e, /*@exposed@*/ sRef s)
9471 if (sRef_isValid (s) && !sRef_isNothing (s))
9473 uentry ue = sRef_getBaseUentry (s);
9475 if (uentry_isValid (ue))
9477 uentry_setLset (ue);
9480 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
9482 voptgenerror (FLG_USEDEF,
9483 message ("Attempt to set unuseable storage: %q", sRef_unparse (s)),
9487 if (sRef_isMeaningful (s))
9489 sRef_setDefinedComplete (s, exprNode_loc (e));
9492 if (exprNode_isDefined (e))
9494 e->msets = sRefSet_insert (e->msets, s);
9500 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode fcn, uentryList params, exprNodeList args)
9502 checkAnyCall (fcn, cstring_undefined, params, args,
9503 FALSE, sRefSet_undefined, FALSE, 0);
9507 checkOneArg (uentry ucurrent, /*@notnull@*/ exprNode current,
9508 /*@dependent@*/ exprNode fcn, bool isSpec, int argno, int totargs)
9512 if (uentry_isYield (ucurrent))
9514 sRef_setDefined (exprNode_getSref (current), exprNode_loc (current));
9515 exprNode_checkSet (current, current->sref);
9519 if (uentry_isSefParam (ucurrent))
9521 sRefSet sets = current->sets;
9522 sRef ref = exprNode_getSref (current);
9524 if (sRef_isMacroParamRef (ref))
9526 uentry ue = sRef_getUentry (ref);
9528 if (!uentry_isSefParam (ue))
9533 ("Parameter %d to %s is declared sef, but "
9534 "the argument is a macro parameter declared "
9536 argno, exprNode_unparse (fcn),
9537 exprNode_unparse (current)),
9538 exprNode_loc (current));
9542 if (!sRefSet_isEmpty (sets))
9544 sRefSet reported = sRefSet_undefined;
9546 sRefSet_realElements (current->sets, el)
9548 if (sRefSet_isSameNameMember (reported, el))
9550 ; /* don't report again */
9554 if (sRef_isUnconstrained (el))
9559 ("Parameter %d to %s is declared sef, but "
9560 "the argument calls unconstrained function %s "
9561 "(no guarantee it will not modify something): %s",
9562 argno, exprNode_unparse (fcn),
9563 sRef_unconstrainedName (el),
9564 exprNode_unparse (current)),
9565 exprNode_loc (current));
9572 ("Parameter %d to %s is declared sef, but "
9573 "the argument may modify %q: %s",
9574 argno, exprNode_unparse (fcn),
9576 exprNode_unparse (current)),
9577 exprNode_loc (current));
9580 } end_sRefSet_realElements;
9584 checkPassTransfer (current, ucurrent, isSpec, fcn, argno, totargs);
9585 exprNode_mergeUSs (fcn, current);
9590 checkAnyCall (/*@dependent@*/ exprNode fcn,
9591 /*@dependent@*/ cstring fname,
9594 bool hasMods, sRefSet mods,
9599 int nargs = exprNodeList_size (args);
9604 ** concat all args ud's to f, add each arg sref as a use unless
9605 ** it was specified as "out", in which case it is a def.
9608 uentryList_reset (pn);
9613 ** if paramn is only or unique, no other arg may alias argn
9616 exprNodeList_elements (args, current)
9620 if (exprNode_isDefined (current))
9622 if ((!uentryList_isUndefined (pn) && !uentryList_isFinished (pn)))
9624 uentry ucurrent = uentryList_current (pn);
9626 if (specialArgs == 0
9627 || (paramno < specialArgs))
9629 checkOneArg (ucurrent, current, fcn, isSpec, paramno, nargs);
9631 if (context_maybeSet (FLG_ALIASUNIQUE))
9633 if (uentry_isOnly (ucurrent)
9634 || uentry_isUnique (ucurrent))
9636 checkUniqueParams (fcn, current, args,
9642 else /* uentry is undefined */
9644 if (specialArgs == 0)
9646 exprNode_checkUseParam (current);
9649 exprNode_mergeUSs (fcn, current);
9652 uentryList_advanceSafe (pn);
9653 } end_exprNodeList_elements;
9659 sRefSet_allElements (mods, s)
9662 sRef rb = sRef_getRootBase (s);
9664 if (sRef_isFileOrGlobalScope (rb))
9666 context_usedGlobal (rb);
9669 fb = sRef_fixBaseParam (s, args);
9671 if (!sRef_isMacroParamRef (fb))
9673 if (sRef_isNothing (fb))
9679 if (sRef_isValid (fb))
9681 uentry ue = sRef_getBaseUentry (s);
9683 if (uentry_isValid (ue))
9685 uentry_setLset (ue);
9689 fcn->sets = sRefSet_insert (fcn->sets, fb);
9692 sRef_clearDerivedComplete (s);
9693 } end_sRefSet_allElements;
9699 if (context_hasMods ())
9701 if (context_maybeSet (FLG_MODUNCON))
9705 message ("Undetected modification possible "
9706 "from call to unconstrained function %s: %s",
9708 exprNode_unparse (fcn)),
9709 exprNode_loc (fcn));
9714 if (context_maybeSet (FLG_MODUNCONNOMODS)
9715 && !(context_inIterDef () || context_inIterEnd ()))
9718 (FLG_MODUNCONNOMODS,
9719 message ("Undetected modification possible "
9720 "from call to unconstrained function %s: %s",
9722 exprNode_unparse (fcn)),
9723 exprNode_loc (fcn));
9727 exprNode_checkSetAny (fcn, fname);
9731 void exprNode_checkUseParam (exprNode current)
9733 if (exprNode_isDefined (current))
9735 exprNode_checkUse (current, current->sref, current->loc);
9740 checkNumerics (ctype tr1, ctype tr2, ctype te1, ctype te2,
9741 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
9746 if (!ctype_match (tr1, tr2))
9748 if ((ctype_isRealInt (tr1) || ctype_isReal (tr1)) &&
9749 (ctype_isRealInt (tr2) || ctype_isReal (tr2)))
9757 message ("Incompatible types for %s (%s, %s): %s %s %s",
9759 ctype_unparse (te1),
9760 ctype_unparse (te2),
9761 exprNode_unparse (e1), lltok_unparse (op),
9762 exprNode_unparse (e2)),
9765 ret = ctype_unknown;
9769 if (ctype_isForceRealNumeric (&tr1) && ctype_isForceRealNumeric (&tr2))
9771 ret = ctype_resolveNumerics (tr1, tr2);
9773 else if (!context_msgStrictOps ())
9775 if (ctype_isPointer (tr1))
9777 if (ctype_isPointer (tr2) && !exprNode_isNullValue (e2))
9781 else if (ctype_isInt (tr2))
9787 ret = ctype_unknown;
9790 else if (ctype_isPointer (tr2))
9792 if (ctype_isPointer (tr1))
9796 else if (ctype_isInt (tr1))
9802 ret = ctype_unknown;
9807 ret = ctype_resolveNumerics (tr1, tr2);
9812 int opid = lltok_getTok (op);
9813 bool comparop = (opid == EQ_OP || opid == NE_OP
9814 || opid == TLT || opid == TGT
9815 || opid == LE_OP || opid == GE_OP);
9817 if (!ctype_isNumeric (tr1) && !ctype_isNumeric (tr2))
9820 && ((ctype_isEnum (tr1) && ctype_isEnum (tr2))
9821 || (ctype_isBool (tr1) && ctype_isBool (tr2))
9822 || (ctype_isChar (tr1) && ctype_isChar (tr2))))
9828 if (ctype_sameName (te1, te2))
9832 message ("Operands of %s are non-numeric (%t): %s %s %s",
9833 lltok_unparse (op), te1,
9834 exprNode_unparse (e1), lltok_unparse (op),
9835 exprNode_unparse (e2)),
9842 message ("Operands of %s are non-numerics (%t, %t): %s %s %s",
9843 lltok_unparse (op), te1, te2,
9844 exprNode_unparse (e1), lltok_unparse (op),
9845 exprNode_unparse (e2)),
9850 else if (!ctype_isNumeric (tr1))
9854 message ("Right operand of %s is non-numeric (%t): %s %s %s",
9855 lltok_unparse (op), te1,
9856 exprNode_unparse (e1), lltok_unparse (op),
9857 exprNode_unparse (e2)),
9862 if (!ctype_isNumeric (tr2))
9866 message ("Left operand of %s is non-numeric (%t): %s %s %s",
9867 lltok_unparse (op), te2,
9868 exprNode_unparse (e1), lltok_unparse (op),
9869 exprNode_unparse (e2)),
9874 ret = ctype_unknown;
9882 abstractOpError (ctype tr1, ctype tr2, lltok op,
9883 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
9884 fileloc loc1, fileloc loc2)
9886 if (ctype_isRealAbstract (tr1) && ctype_isRealAbstract (tr2))
9888 if (ctype_match (tr1, tr2))
9892 message ("Operands of %s are abstract type (%t): %s %s %s",
9893 lltok_unparse (op), tr1,
9894 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9901 message ("Operands of %s are abstract types (%t, %t): %s %s %s",
9902 lltok_unparse (op), tr1, tr2,
9903 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9907 else if (ctype_isRealAbstract (tr1))
9911 message ("Left operand of %s is abstract type (%t): %s %s %s",
9912 lltok_unparse (op), tr1,
9913 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9918 if (ctype_isRealAbstract (tr2))
9922 message ("Right operand of %s is abstract type (%t): %s %s %s",
9923 lltok_unparse (op), tr2,
9924 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
9933 ** requies e1 and e2 and not error exprNode's.
9937 ** If e1 is a component of an abstract type, and e2 is mutable and client-visible,
9938 ** the rep of the abstract type is exposed.
9940 ** The order is very important:
9942 ** check rep expose (move into check transfer)
9948 ** This isn't really a sensible procedure, but the indententation
9949 ** was getting too deep.
9953 checkOneRepExpose (sRef ysr, sRef base,
9954 /*@notnull@*/ exprNode e1,
9955 /*@notnull@*/ exprNode e2, ctype ct,
9958 if (!(sRef_isOnly (ysr) || sRef_isKeep (ysr)
9959 || sRef_isOwned (ysr) || sRef_isExposed (ysr)))
9961 if (sRef_isAnyParam (base) && !sRef_isExposed (base))
9963 if (sRef_isIReference (ysr))
9965 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
9970 ("Assignment of mutable component of parameter %q "
9971 "to component of abstract "
9972 "type %s exposes rep: %s = %s",
9973 sRef_unparse (base),
9975 exprNode_unparse (e1), exprNode_unparse (e2)),
9983 ("Assignment of mutable component of parameter %q "
9984 "(through alias %q) to component of abstract "
9985 "type %s exposes rep: %s = %s",
9986 sRef_unparse (base),
9987 sRef_unparse (e2->sref),
9989 exprNode_unparse (e1), exprNode_unparse (e2)),
9995 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
9999 message ("Assignment of mutable parameter %q "
10000 "to component of abstract type %s "
10001 "exposes rep: %s = %s",
10002 sRef_unparse (base),
10003 ctype_unparse (ct),
10004 exprNode_unparse (e1),
10005 exprNode_unparse (e2)),
10012 message ("Assignment of mutable parameter %q "
10013 "(through alias %q) to "
10014 "component of abstract type %s exposes "
10016 sRef_unparse (base),
10017 sRef_unparse (e2->sref),
10018 ctype_unparse (ct),
10019 exprNode_unparse (e1),
10020 exprNode_unparse (e2)),
10026 if (sRef_isFileOrGlobalScope (s2b))
10028 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10032 message ("Assignment of global %q "
10034 "abstract type %s exposes rep: %s = %s",
10035 sRef_unparse (base),
10036 ctype_unparse (ct),
10037 exprNode_unparse (e1), exprNode_unparse (e2)),
10044 message ("Assignment of global %q (through alias %q) "
10046 "abstract type %s exposes rep: %s = %s",
10047 sRef_unparse (base),
10048 sRef_unparse (e2->sref),
10049 ctype_unparse (ct),
10050 exprNode_unparse (e1), exprNode_unparse (e2)),
10058 doAssign (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2, bool isInit)
10060 if (ctype_isRealFunction (exprNode_getType (e1))
10061 && !ctype_isRealPointer (exprNode_getType (e1)))
10065 message ("Invalid left-hand side of assignment (function type %s): %s",
10066 ctype_unparse (exprNode_getType (e1)),
10067 exprNode_unparse (e1)),
10071 if (context_getFlag (FLG_ASSIGNEXPOSE) && ctype_isMutable (e2->typ))
10073 ctype t2 = exprNode_getType (e2);
10074 sRef sr = sRef_getRootBase (e1->sref);
10075 ctype ct = sRef_getType (sr);
10077 if (ctype_isAbstract (t2)
10078 && !(uentry_isMutableDatatype (usymtab_getTypeEntry (ctype_typeId (t2)))))
10080 /* it is immutable, okay to reference */
10081 goto donerepexpose;
10084 if (ctype_isAbstract (ct) && sRef_isIReference (e1->sref))
10086 sRef s2b = sRef_getRootBase (e2->sref);
10087 sRef s1 = e1->sref;
10088 sRef s1b = sRef_getRootBase (s1);
10091 aliases = usymtab_canAlias (e2->sref);
10093 if (!sRef_similar (s2b, s1b)
10094 && !sRef_isExposed (s1)
10095 && !(sRef_isOnly (s2b) || sRef_isKeep (s2b) || sRef_isExposed (s2b)))
10097 if (sRef_isAnyParam (s2b) && !sRef_isOnly (s2b)
10098 && !sRef_isOwned (s2b) && !sRef_isKeep (s2b)
10099 && !sRef_isExposed (s2b))
10101 if (sRef_isIReference (e2->sref))
10106 ("Assignment of mutable component of parameter %q "
10107 "to component of abstract type %s exposes rep: %s = %s",
10108 sRef_unparse (s2b),
10109 ctype_unparse (ct),
10110 exprNode_unparse (e1), exprNode_unparse (e2)),
10117 message ("Assignment of mutable parameter %q to "
10118 "component of abstract type %s exposes rep: %s = %s",
10119 sRef_unparse (s2b),
10120 ctype_unparse (ct),
10121 exprNode_unparse (e1), exprNode_unparse (e2)),
10126 if (sRef_isFileOrGlobalScope (s2b))
10130 message ("Assignment of global %q to component of "
10131 "abstract type %s exposes rep: %s = %s",
10132 sRef_unparse (s2b),
10133 ctype_unparse (ct),
10134 exprNode_unparse (e1), exprNode_unparse (e2)),
10138 sRefSet_realElements (aliases, ysr)
10140 sRef base = sRef_getRootBase (ysr);
10142 if (sRef_similar (ysr, s2b) || sRef_similar (s1b, base)
10143 || sRef_sameName (base, s1b))
10145 ; /* error already reported or same sref */
10149 checkOneRepExpose (ysr, base, e1, e2, ct, s2b);
10151 } end_sRefSet_realElements;
10153 sRefSet_free (aliases);
10160 ** function variables don't really work...
10163 if (!ctype_isFunction (ctype_realType (e2->typ)))
10167 DPRINTF (("Check init: %s / %s",
10168 exprNode_unparse (e1), exprNode_unparse (e2)));
10169 checkInitTransfer (e1, e2);
10173 checkAssignTransfer (e1, e2);
10178 sRef fref = e2->sref;
10180 sRef_setDefState (e1->sref, sRef_getDefState (fref), e1->loc);
10181 sRef_setNullState (e1->sref, sRef_getNullState (fref), e1->loc);
10183 /* Need to typecheck the annotation on the parameters */
10185 if (ctype_isRealFunction (e1->typ)) {
10186 uentryList e1p = ctype_argsFunction (ctype_realType (e1->typ));
10187 uentryList e2p = ctype_argsFunction (ctype_realType (e2->typ));
10189 if (!uentryList_isMissingParams (e1p)
10190 && !uentryList_isMissingParams (e2p)
10191 && uentryList_size (e1p) > 0) {
10192 if (uentryList_size (e1p) == uentryList_size (e2p)) {
10195 uentryList_elements (e1p, el1) {
10198 el2 = uentryList_getN (e2p, n);
10200 uentry_checkMatchParam (el1, el2, n, e2);
10201 } end_uentryList_elements;
10207 if (isInit && sRef_isFileOrGlobalScope (e1->sref))
10213 DPRINTF (("Update aliases: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
10214 updateAliases (e1, e2);
10219 checkMacroParen (exprNode e)
10221 if (exprNode_isError (e) || e->kind == XPR_CAST)
10227 if (sRef_isUnsafe (e->sref) && !exprNode_isInParens (e))
10231 message ("Macro parameter used without parentheses: %s",
10232 exprNode_unparse (e)),
10239 reflectNullTest (/*@notnull@*/ exprNode e, bool isnull)
10243 e->guards = guardSet_addTrueGuard (e->guards, e->sref);
10247 e->guards = guardSet_addFalseGuard (e->guards, e->sref);
10254 ** if e2 is a parameter or global derived location which
10255 ** can be modified (that is, e2 is a mutable abstract type,
10256 ** or a derived pointer), then e1 can alias e2.
10258 ** e1 can alias everything which e2 can alias.
10260 ** Also, if e1 is guarded, remove from guard sets!
10263 static void updateAliases (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2)
10265 if (!context_inProtectVars ())
10268 ** depends on types of e1 and e2
10271 sRef s1 = e1->sref;
10272 sRef s2 = e2->sref;
10273 ctype t1 = exprNode_getType (e1);
10275 /* handle pointer sRefs, record fields, arrays, etc... */
10277 if (!ctype_isRealSU (t1))
10279 DPRINTF (("Copying real! %s", ctype_unparse (t1)));
10280 sRef_copyRealDerivedComplete (s1, s2);
10285 ** Fields should alias
10288 DPRINTF (("Not COPYING!: %s", ctype_unparse (t1)));
10291 if (ctype_isMutable (t1) && sRef_isKnown (s1))
10293 usymtab_clearAlias (s1);
10294 usymtab_addMustAlias (s1, s2);
10295 DPRINTF (("Add must alias: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
10299 DPRINTF (("Not mutable: %s", ctype_unparse (t1)));
10302 if (sRef_possiblyNull (s1) && usymtab_isGuarded (s1))
10304 usymtab_unguard (s1);
10309 exprNode exprNode_updateLocation (/*@returned@*/ exprNode e, /*@temp@*/ fileloc loc)
10311 if (exprNode_isDefined (e))
10313 e->loc = fileloc_update (e->loc, loc);
10317 e = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
10323 static void checkUniqueParams (exprNode fcn,
10324 /*@notnull@*/ exprNode current,
10326 int paramno, uentry ucurrent)
10329 sRef thisref = exprNode_getSref (current);
10332 ** Check if any argument could match this argument.
10335 exprNodeList_elements (args, icurrent)
10339 if (iparamno != paramno)
10341 sRef sr = exprNode_getSref (icurrent);
10343 if (sRef_similarRelaxed (thisref, sr))
10345 if (!sRef_isConst (thisref) && !sRef_isConst (sr))
10350 ("Parameter %d (%s) to function %s is declared %s but "
10351 "is aliased by parameter %d (%s)",
10353 exprNode_unparse (current),
10354 exprNode_unparse (fcn),
10355 alkind_unparse (uentry_getAliasKind (ucurrent)),
10356 iparamno, exprNode_unparse (icurrent)),
10362 sRefSet aliases = usymtab_canAlias (sr);
10364 sRefSet_allElements (aliases, asr)
10366 if (ctype_isUnknown (sRef_getType (thisref)))
10368 sRef_setType (thisref, uentry_getType (ucurrent));
10371 if (sRef_similarRelaxed (thisref, asr))
10373 if (sRef_isExternal (asr))
10375 if (sRef_isLocalState (thisref))
10381 sRef base = sRef_getRootBase (asr);
10383 if (!sRef_similar (sRef_getBase (asr), thisref))
10385 if (sRef_isUnique (base) || sRef_isOnly (base)
10386 || sRef_isKept (base)
10387 || (sRef_isAddress (asr) && sRef_isLocalVar (base))
10388 || (sRef_isAddress (thisref)
10389 && sRef_isLocalVar (sRef_getRootBase (thisref))))
10391 ; /* okay, no error */
10396 (FLG_MAYALIASUNIQUE,
10398 ("Parameter %d (%s) to function %s is declared %s but "
10399 "may be aliased externally by parameter %d (%s)",
10401 exprNode_unparse (current),
10402 exprNode_unparse (fcn),
10403 alkind_unparse (uentry_getAliasKind (ucurrent)),
10404 iparamno, exprNode_unparse (icurrent)),
10415 ("Parameter %d (%s) to function %s is declared %s but "
10416 "is aliased externally by parameter %d (%s) through "
10419 exprNode_unparse (current),
10420 exprNode_unparse (fcn),
10421 alkind_unparse (uentry_getAliasKind (ucurrent)),
10422 iparamno, exprNode_unparse (icurrent),
10423 sRef_unparse (asr)),
10427 } end_sRefSet_allElements;
10428 sRefSet_free (aliases);
10431 } end_exprNodeList_elements;
10434 long exprNode_getLongValue (exprNode e) {
10437 if (exprNode_hasValue (e)
10438 && multiVal_isInt (exprNode_getValue (e)))
10440 value = multiVal_forceInt (exprNode_getValue (e));
10450 /*@observer@*/ fileloc exprNode_getfileloc (exprNode p_e)
10452 if (exprNode_isDefined (p_e) )
10453 return ( p_e->loc );
10455 return fileloc_undefined;
10458 /*@only@*/ fileloc exprNode_getNextSequencePoint (exprNode e)
10461 ** Returns the location of the sequence point following e.
10463 ** Only works for statements (for now).
10466 if (exprNode_isDefined (e) && e->kind == XPR_STMT) {
10467 lltok t = exprData_getUopTok (e->edata);
10468 return fileloc_copy(lltok_getLoc (t));
10470 //drl possible problem : warning fix
10471 // llcontbug (message ("Cannot get next sequence point: %s", exprNode_unparse (e)));
10472 return fileloc_undefined;
10476 exprNode exprNode_createNew(ctype c)
10480 ret = exprNode_createPlain (c);