2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
25 # include "splintMacros.nf"
28 static /*@observer@*/ cstring stateAction_unparse (stateAction p_sa) /*@*/ ;
30 void stateInfo_free (/*@only@*/ stateInfo a)
34 fileloc_free (a->loc);
39 /*@only@*/ stateInfo stateInfo_update (/*@only@*/ stateInfo old, stateInfo newinfo)
41 ** returns an stateInfo with the same value as new. May reuse the
42 ** storage of old. (i.e., same effect as copy, but more
48 DPRINTF (("Update state ==> %s", stateInfo_unparse (newinfo)));
49 return stateInfo_copy (newinfo);
51 else if (newinfo == NULL)
58 if (fileloc_equal (old->loc, newinfo->loc)
59 && old->action == newinfo->action
60 /*@-abstractcompare@*/ && old->ref == newinfo->ref /*@=abstractcompare@*/)
63 ** Duplicate (change through alias most likely)
64 ** don't add this info
71 stateInfo snew = stateInfo_makeRefLoc (newinfo->ref,
72 newinfo->loc, newinfo->action);
73 llassert (snew->previous == NULL);
75 DPRINTF (("Update state ==> %s", stateInfo_unparse (snew)));
81 static /*@observer@*/ stateInfo stateInfo_sort (/*@temp@*/ stateInfo sinfo)
82 /* Sorts in reverse location order */
84 DPRINTF (("Sorting: %s", stateInfo_unparse (sinfo)));
86 if (sinfo == NULL || sinfo->previous == NULL)
92 stateInfo snext = stateInfo_sort (sinfo->previous);
93 stateInfo sfirst = snext;
95 DPRINTF (("sinfo/sext: %s // %s", stateInfo_unparse (sinfo), stateInfo_unparse (snext)));
96 llassert (snext != NULL);
98 if (!fileloc_lessthan (sinfo->loc, snext->loc))
100 /*@i2@*/ sinfo->previous = sfirst; /* spurious? */
101 DPRINTF (("Sorted ==> %s", stateInfo_unparse (sinfo)));
102 /*@i2@*/ return sinfo; /* spurious? */
106 while (snext != NULL && fileloc_lessthan (sinfo->loc, snext->loc))
111 fileloc tloc = snext->loc;
112 stateAction taction = snext->action;
113 sRef tref = snext->ref;
115 DPRINTF (("in while: sinfo/sext: %s // %s", stateInfo_unparse (sinfo), stateInfo_unparse (snext)));
117 snext->loc = sinfo->loc;
118 snext->action = sinfo->action;
120 snext->ref = sinfo->ref; /* Doesn't actually modifie sfirst */
124 sinfo->action = taction;
127 sinfo->previous = snext->previous;
129 snext = snext->previous;
130 DPRINTF (("in while: sinfo/sext: %s // %s", stateInfo_unparse (sinfo), stateInfo_unparse (snext)));
133 DPRINTF (("Sorted ==> %s", stateInfo_unparse (sfirst)));
142 stateInfo_updateLoc (/*@only@*/ stateInfo old, stateAction action, fileloc loc)
144 if (fileloc_isUndefined (loc)) {
145 loc = fileloc_copy (g_currentloc);
148 if (old != NULL && fileloc_equal (old->loc, loc) && old->action == action)
151 ** Duplicate (change through alias most likely)
152 ** don't add this info
159 stateInfo snew = stateInfo_makeLoc (loc, action);
160 llassert (snew->previous == NULL);
161 snew->previous = old;
162 DPRINTF (("Update state ==> %s", stateInfo_unparse (snew)));
168 stateInfo_updateRefLoc (/*@only@*/ stateInfo old, /*@exposed@*/ sRef ref,
169 stateAction action, fileloc loc)
171 if (fileloc_isUndefined (loc)) {
172 loc = fileloc_copy (g_currentloc);
175 if (old != NULL && fileloc_equal (old->loc, loc)
176 && old->action == action
177 /*@-abstractcompare*/ && old->ref == ref /*@=abstractcompare@*/)
180 ** Duplicate (change through alias most likely)
181 ** don't add this info
188 stateInfo snew = stateInfo_makeRefLoc (ref, loc, action);
189 llassert (snew->previous == NULL);
190 snew->previous = old;
191 DPRINTF (("Update state ==> %s", stateInfo_unparse (snew)));
196 /*@only@*/ stateInfo stateInfo_copy (stateInfo a)
204 stateInfo ret = (stateInfo) dmalloc (sizeof (*ret));
206 ret->loc = fileloc_copy (a->loc); /*< should report bug without copy! >*/
208 ret->action = a->action;
209 ret->previous = stateInfo_copy (a->previous);
215 /*@only@*/ /*@notnull@*/ stateInfo
216 stateInfo_currentLoc (void)
218 return stateInfo_makeLoc (g_currentloc, SA_DECLARED);
221 /*@only@*/ /*@notnull@*/ stateInfo
222 stateInfo_makeLoc (fileloc loc, stateAction action)
224 stateInfo ret = (stateInfo) dmalloc (sizeof (*ret));
226 if (fileloc_isUndefined (loc)) {
227 ret->loc = fileloc_copy (g_currentloc);
229 ret->loc = fileloc_copy (loc);
232 ret->ref = sRef_undefined;
233 ret->action = action;
234 ret->previous = stateInfo_undefined;
236 DPRINTF (("Make loc ==> %s", stateInfo_unparse (ret)));
240 /*@only@*/ /*@notnull@*/ stateInfo
241 stateInfo_makeRefLoc (/*@exposed@*/ sRef ref, fileloc loc, stateAction action)
242 /*@post:isnull result->previous@*/
244 stateInfo ret = (stateInfo) dmalloc (sizeof (*ret));
246 if (fileloc_isUndefined (loc)) {
247 ret->loc = fileloc_copy (g_currentloc);
249 ret->loc = fileloc_copy (loc);
253 ret->action = action;
254 ret->previous = stateInfo_undefined;
260 stateInfo_unparse (stateInfo s)
262 cstring res = cstring_makeLiteral ("");
264 while (stateInfo_isDefined (s)) {
265 res = message ("%q%q: ", res, fileloc_unparse (s->loc));
266 if (sRef_isValid (s->ref)) {
267 res = message ("%q through alias %q ", res, sRef_unparse (s->ref));
270 res = message ("%q%s; ", res, stateAction_unparse (s->action));
277 fileloc stateInfo_getLoc (stateInfo info)
279 if (stateInfo_isDefined (info))
284 return fileloc_undefined;
287 stateAction stateAction_fromNState (nstate ns)
296 return SA_BECOMESNONNULL;
301 return SA_BECOMESPOSSIBLYNULL;
303 return SA_BECOMESNULL;
305 return SA_BECOMESPOSSIBLYNULL;
309 stateAction stateAction_fromExkind (exkind ex)
323 /*@notreached@*/ return SA_UNKNOWN;
326 stateAction stateAction_fromAlkind (alkind ak)
355 return SA_REFCOUNTED;
366 case AK_IMPDEPENDENT:
367 return SA_IMPDEPENDENT;
375 /*@notreached@*/ return SA_UNKNOWN;
378 stateAction stateAction_fromSState (sstate ss)
382 case SS_UNKNOWN: return SA_DECLARED;
383 case SS_UNUSEABLE: return SA_KILLED;
384 case SS_UNDEFINED: return SA_UNDEFINED;
385 case SS_MUNDEFINED: return SA_MUNDEFINED;
386 case SS_ALLOCATED: return SA_ALLOCATED;
387 case SS_PDEFINED: return SA_PDEFINED;
388 case SS_DEFINED: return SA_DEFINED;
389 case SS_PARTIAL: return SA_PDEFINED;
390 case SS_DEAD: return SA_RELEASED;
391 case SS_HOFFA: return SA_PKILLED;
392 case SS_SPECIAL: return SA_DECLARED;
393 case SS_RELDEF: return SA_DECLARED;
399 llbug (message ("Unexpected sstate: %s", sstate_unparse (ss)));
400 /*@notreached@*/ return SA_UNKNOWN;
404 static /*@observer@*/ cstring stateAction_unparse (stateAction sa)
408 case SA_UNKNOWN: return cstring_makeLiteralTemp ("changed <unknown modification>");
409 case SA_CHANGED: return cstring_makeLiteralTemp ("changed");
411 case SA_CREATED: return cstring_makeLiteralTemp ("created");
412 case SA_DECLARED: return cstring_makeLiteralTemp ("declared");
413 case SA_DEFINED: return cstring_makeLiteralTemp ("defined");
414 case SA_PDEFINED: return cstring_makeLiteralTemp ("partially defined");
415 case SA_RELEASED: return cstring_makeLiteralTemp ("released");
416 case SA_ALLOCATED: return cstring_makeLiteralTemp ("allocated");
417 case SA_KILLED: return cstring_makeLiteralTemp ("released");
418 case SA_PKILLED: return cstring_makeLiteralTemp ("possibly released");
419 case SA_MERGED: return cstring_makeLiteralTemp ("merged");
420 case SA_UNDEFINED: return cstring_makeLiteralTemp ("becomes undefined");
421 case SA_MUNDEFINED: return cstring_makeLiteralTemp ("possibly undefined");
423 case SA_SHARED: return cstring_makeLiteralTemp ("becomes shared");
424 case SA_ONLY: return cstring_makeLiteralTemp ("becomes only");
425 case SA_IMPONLY: return cstring_makeLiteralTemp ("becomes implicitly only");
426 case SA_OWNED: return cstring_makeLiteralTemp ("becomes owned");
427 case SA_DEPENDENT: return cstring_makeLiteralTemp ("becomes dependent");
428 case SA_IMPDEPENDENT: return cstring_makeLiteralTemp ("becomes implicitly dependent");
429 case SA_KEPT: return cstring_makeLiteralTemp ("becomes kept");
430 case SA_KEEP: return cstring_makeLiteralTemp ("becomes keep");
431 case SA_FRESH: return cstring_makeLiteralTemp ("becomes fresh");
432 case SA_TEMP: return cstring_makeLiteralTemp ("becomes temp");
433 case SA_IMPTEMP: return cstring_makeLiteralTemp ("becomes implicitly temp");
434 case SA_XSTACK: return cstring_makeLiteralTemp ("becomes stack-allocated storage");
435 case SA_STATIC: return cstring_makeLiteralTemp ("becomes static");
436 case SA_LOCAL: return cstring_makeLiteralTemp ("becomes local");
438 case SA_REFCOUNTED: return cstring_makeLiteralTemp ("becomes refcounted");
439 case SA_REFS: return cstring_makeLiteralTemp ("becomes refs");
440 case SA_NEWREF: return cstring_makeLiteralTemp ("becomes newref");
441 case SA_KILLREF: return cstring_makeLiteralTemp ("becomes killref");
443 case SA_OBSERVER: return cstring_makeLiteralTemp ("becomes observer");
444 case SA_EXPOSED: return cstring_makeLiteralTemp ("becomes exposed");
446 case SA_BECOMESNULL: return cstring_makeLiteralTemp ("becomes null");
447 case SA_BECOMESNONNULL: return cstring_makeLiteralTemp ("becomes non-null");
448 case SA_BECOMESPOSSIBLYNULL: return cstring_makeLiteralTemp ("becomes possibly null");
451 DPRINTF (("Bad state action: %d", sa));
455 void stateInfo_display (stateInfo s, cstring sname)
457 bool showdeep = context_flagOn (FLG_SHOWDEEPHISTORY, g_currentloc);
459 s = stateInfo_sort (s);
461 while (stateInfo_isDefined (s))
463 cstring msg = message ("%s%s", sname, stateAction_unparse (s->action));
465 if (sRef_isValid (s->ref)) {
466 msg = message ("%q (through alias %q)", msg, sRef_unparse (s->ref));
469 llgenindentmsg (msg, s->loc);
478 cstring_free (sname);