File: stateInfo.c

package info (click to toggle)
splint 1%3A3.1.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 21,004 kB
  • sloc: ansic: 150,869; yacc: 3,465; sh: 3,034; makefile: 2,157; lex: 412
file content (490 lines) | stat: -rw-r--r-- 13,486 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
** Splint - annotation-assisted static program checker
** Copyright (C) 1994-2003 University of Virginia,
**         Massachusetts Institute of Technology
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2 of the License, or (at your
** option) any later version.
** 
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** General Public License for more details.
** 
** The GNU General Public License is available from http://www.gnu.org/ or
** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
** MA 02111-1307, USA.
**
** For information on splint: info@splint.org
** To report a bug: splint-bug@splint.org
** For more information: http://www.splint.org
*/

# include "splintMacros.nf"
# include "basic.h"

# ifdef WIN32
/*
** Make Microsoft VC++ happy: its control checking produces too
** many spurious warnings.
*/

# pragma warning (disable:4715) 
# endif

static /*@observer@*/ cstring stateAction_unparse (stateAction p_sa) /*@*/ ;

void stateInfo_free (/*@only@*/ stateInfo a)
{
  if (a != NULL)
    {
      fileloc_free (a->loc);
      sfree (a);
    }
}

/*@only@*/ stateInfo stateInfo_update (/*@only@*/ stateInfo old, stateInfo newinfo)
     /*
     ** returns an stateInfo with the same value as new.  May reuse the
     ** storage of old.  (i.e., same effect as copy, but more
     ** efficient.)
     */
{
  if (old == NULL) 
    {
      DPRINTF (("Update state ==> %s", stateInfo_unparse (newinfo)));
      return stateInfo_copy (newinfo);
    }
  else if (newinfo == NULL)
    {
      stateInfo_free (old);
      return NULL;
    }
  else
    {
      if (fileloc_equal (old->loc, newinfo->loc)
	  && old->action == newinfo->action
	  /*@-abstractcompare@*/ && old->ref == newinfo->ref /*@=abstractcompare@*/)
	{
	  /*
	  ** Duplicate (change through alias most likely)
	  ** don't add this info
	  */
	  
	  return old;
	}
      else
	{
	  stateInfo snew = stateInfo_makeRefLoc (newinfo->ref, 
						 newinfo->loc, newinfo->action);
	  llassert (snew->previous == NULL);
	  snew->previous = old;
	  DPRINTF (("Update state ==> %s", stateInfo_unparse (snew)));
	  return snew;
	}
    }
}

static /*@observer@*/ stateInfo stateInfo_sort (/*@temp@*/ stateInfo stinfo)
     /* Sorts in reverse location order */
{
  DPRINTF (("Sorting: %s", stateInfo_unparse (stinfo)));

  if (stinfo == NULL || stinfo->previous == NULL) 
    {
      return stinfo;
    }
  else
    {
      stateInfo snext = stateInfo_sort (stinfo->previous);
      stateInfo sfirst = snext;

      DPRINTF (("stinfo/sext: %s // %s", stateInfo_unparse (stinfo), stateInfo_unparse (snext)));
      llassert (snext != NULL);

      if (!fileloc_lessthan (stinfo->loc, snext->loc))
	{
	  /*@i2@*/ stinfo->previous = sfirst; /* spurious? */
	  DPRINTF (("Sorted ==> %s", stateInfo_unparse (stinfo)));
	  /*@i2@*/ return stinfo; /* spurious? */
	}
      else
	{
	  while (snext != NULL && fileloc_lessthan (stinfo->loc, snext->loc))
	    {
	      /*
	      ** swap the order
	      */
	      fileloc tloc = snext->loc;
	      stateAction taction = snext->action;
	      sRef tref = snext->ref;
	      
	      DPRINTF (("in while: stinfo/sext: %s // %s", stateInfo_unparse (stinfo), stateInfo_unparse (snext)));
      
	      snext->loc = stinfo->loc;
	      snext->action = stinfo->action;
	      /*@-modobserver@*/
	      snext->ref = stinfo->ref; /* Doesn't actually modifie sfirst */ 
	      /*@=modobserver@*/
	      
	      stinfo->loc = tloc;
	      stinfo->action = taction;
	      stinfo->ref = tref;
	      /*@-mustfreeonly@*/
	      stinfo->previous = snext->previous;
	      /*@=mustfreeonly@*/
	      snext = snext->previous;
	      DPRINTF (("in while: stinfo/sext: %s // %s", stateInfo_unparse (stinfo), stateInfo_unparse (snext)));
	    }
	  
	  DPRINTF (("Sorted ==> %s", stateInfo_unparse (sfirst)));
	  /*@-compmempass@*/
	  return sfirst;
	  /*@=compmempass@*/
	}
    }
}

/*@only@*/ stateInfo 
stateInfo_updateLoc (/*@only@*/ stateInfo old, stateAction action, fileloc loc)
{
  if (fileloc_isUndefined (loc)) {
    loc = fileloc_copy (g_currentloc);
  }

  if (old != NULL && fileloc_equal (old->loc, loc) && old->action == action)
    {
      /*
      ** Duplicate (change through alias most likely)
      ** don't add this info
      */
      
      return old;
    }
  else
    {
      stateInfo snew = stateInfo_makeLoc (loc, action);
      llassert (snew->previous == NULL);
      snew->previous = old;
      DPRINTF (("Update state ==> %s", stateInfo_unparse (snew)));
      return snew;
    }
}

/*@only@*/ stateInfo 
stateInfo_updateRefLoc (/*@only@*/ stateInfo old, /*@exposed@*/ sRef ref, 
			stateAction action, fileloc loc)
{
  if (fileloc_isUndefined (loc)) {
    loc = fileloc_copy (g_currentloc);
  }

  if (old != NULL && fileloc_equal (old->loc, loc)
      && old->action == action
      /*@-abstractcompare*/ && old->ref == ref /*@=abstractcompare@*/)
    {
      /*
      ** Duplicate (change through alias most likely)
      ** don't add this info
      */
      
      return old;
    }
  else
    {
      stateInfo snew = stateInfo_makeRefLoc (ref, loc, action);
      llassert (snew->previous == NULL);
      snew->previous = old;
      DPRINTF (("Update state ==> %s", stateInfo_unparse (snew)));
      return snew;
    }
}

/*@only@*/ stateInfo stateInfo_copy (stateInfo a)
{
  if (a == NULL)
    {
      return NULL;
    }
  else
    {
      stateInfo ret = (stateInfo) dmalloc (sizeof (*ret));
      
      ret->loc = fileloc_copy (a->loc); /*< should report bug without copy! >*/
      ret->ref = a->ref;
      ret->action = a->action;
      ret->previous = stateInfo_copy (a->previous); 

      return ret;
    }
}

/*@only@*/ /*@notnull@*/ stateInfo
stateInfo_currentLoc (void)
{
  return stateInfo_makeLoc (g_currentloc, SA_DECLARED);
}

/*@only@*/ /*@notnull@*/ stateInfo
stateInfo_makeLoc (fileloc loc, stateAction action)
{
  stateInfo ret = (stateInfo) dmalloc (sizeof (*ret));

  if (fileloc_isUndefined (loc)) {
    ret->loc = fileloc_copy (g_currentloc);
  } else {
    ret->loc = fileloc_copy (loc);
  }

  ret->ref = sRef_undefined;
  ret->action = action;
  ret->previous = stateInfo_undefined;

  DPRINTF (("Make loc ==> %s", stateInfo_unparse (ret)));
  return ret;
}

/*@only@*/ /*@notnull@*/ stateInfo
stateInfo_makeRefLoc (/*@exposed@*/ sRef ref, fileloc loc, stateAction action)
     /*@post:isnull result->previous@*/
{
  stateInfo ret = (stateInfo) dmalloc (sizeof (*ret));

  if (fileloc_isUndefined (loc)) {
    ret->loc = fileloc_copy (g_currentloc);
  } else {
    ret->loc = fileloc_copy (loc);
  }

  ret->ref = ref;
  ret->action = action;
  ret->previous = stateInfo_undefined;

  return ret;
}

/*@only@*/ cstring
stateInfo_unparse (stateInfo s)
{
  cstring res = cstring_makeLiteral ("");

  while (stateInfo_isDefined (s)) {
    res = message ("%q%q: ", res, fileloc_unparse (s->loc));
    if (sRef_isValid (s->ref)) {
      res = message ("%q through alias %q ", res, sRef_unparse (s->ref));
    }

    res = message ("%q%s; ", res, stateAction_unparse (s->action)); 
    s = s->previous;
  }

  return res;
}

fileloc stateInfo_getLoc (stateInfo info)
{
    if (stateInfo_isDefined (info)) 
      {
	return info->loc;
      }
    
    return fileloc_undefined;
}

stateAction stateAction_fromNState (nstate ns)
{
  switch (ns) 
    {
    case NS_ERROR:
    case NS_UNKNOWN:
      return SA_UNKNOWN;
    case NS_NOTNULL:
    case NS_MNOTNULL:
      return SA_BECOMESNONNULL;
    case NS_RELNULL:
    case NS_CONSTNULL:
      return SA_DECLARED;
    case NS_POSNULL:
      return SA_BECOMESPOSSIBLYNULL;
    case NS_DEFNULL:
      return SA_BECOMESNULL;
    case NS_ABSNULL:
      return SA_BECOMESPOSSIBLYNULL;
    }
}

stateAction stateAction_fromExkind (exkind ex)
{
  switch (ex) 
    {
    case XO_UNKNOWN:
    case XO_NORMAL:
      return SA_UNKNOWN;
    case XO_EXPOSED:
      return SA_EXPOSED;
    case XO_OBSERVER:
      return SA_OBSERVER;
    }

  BADBRANCH;
  /*@notreached@*/ return SA_UNKNOWN;
}

stateAction stateAction_fromAlkind (alkind ak)
{
  switch (ak)
    {
    case AK_UNKNOWN:
    case AK_ERROR:
      return SA_UNKNOWN;
    case AK_ONLY:
      return SA_ONLY;
    case AK_IMPONLY:
      return SA_IMPONLY;
    case AK_KEEP:
      return SA_KEEP;
    case AK_KEPT:
      return SA_KEPT;
    case AK_TEMP:
      return SA_TEMP;
    case AK_IMPTEMP:
      return SA_IMPTEMP;
    case AK_SHARED:
      return SA_SHARED;
    case AK_UNIQUE:
    case AK_RETURNED:
      return SA_DECLARED;
    case AK_FRESH:
      return SA_FRESH;
    case AK_STACK:
      return SA_XSTACK;
    case AK_REFCOUNTED:
      return SA_REFCOUNTED;
    case AK_REFS:
      return SA_REFS;
    case AK_KILLREF:
      return SA_KILLREF;
    case AK_NEWREF:
      return SA_NEWREF;
    case AK_OWNED:
      return SA_OWNED;
    case AK_DEPENDENT:
      return SA_DEPENDENT;
    case AK_IMPDEPENDENT:
      return SA_IMPDEPENDENT;
    case AK_STATIC:
      return SA_STATIC;
    case AK_LOCAL:
      return SA_LOCAL;
    }

  BADBRANCH;
  /*@notreached@*/ return SA_UNKNOWN;
}

stateAction stateAction_fromSState (sstate ss)
{
  switch (ss) 
    {
    case SS_UNKNOWN: return SA_DECLARED;
    case SS_UNUSABLE: return SA_KILLED;
    case SS_UNDEFINED: return SA_UNDEFINED;
    case SS_MUNDEFINED: return SA_MUNDEFINED;
    case SS_ALLOCATED: return SA_ALLOCATED;
    case SS_PDEFINED: return SA_PDEFINED;
    case SS_DEFINED: return SA_DEFINED;
    case SS_PARTIAL: return SA_PDEFINED;
    case SS_DEAD: return SA_RELEASED;
    case SS_HOFFA: return SA_PKILLED;
    case SS_SPECIAL: return SA_DECLARED;
    case SS_RELDEF: return SA_DECLARED;
    case SS_FIXED:
    case SS_UNDEFGLOB: 
    case SS_KILLED:     
    case SS_UNDEFKILLED:
    case SS_LAST:
      llbug (message ("Unexpected sstate: %s", sstate_unparse (ss)));
      /*@notreached@*/ return SA_UNKNOWN;
    }
}

static /*@observer@*/ cstring stateAction_unparse (stateAction sa)
{
  switch (sa) 
    {
    case SA_UNKNOWN: return cstring_makeLiteralTemp ("changed <unknown modification>");
    case SA_CHANGED: return cstring_makeLiteralTemp ("changed");

    case SA_CREATED: return cstring_makeLiteralTemp ("created");
    case SA_DECLARED: return cstring_makeLiteralTemp ("declared");
    case SA_DEFINED: return cstring_makeLiteralTemp ("defined");
    case SA_PDEFINED: return cstring_makeLiteralTemp ("partially defined");
    case SA_RELEASED: return cstring_makeLiteralTemp ("released");
    case SA_ALLOCATED: return cstring_makeLiteralTemp ("allocated");
    case SA_KILLED: return cstring_makeLiteralTemp ("released");
    case SA_PKILLED: return cstring_makeLiteralTemp ("possibly released");
    case SA_MERGED: return cstring_makeLiteralTemp ("merged");
    case SA_UNDEFINED: return cstring_makeLiteralTemp ("becomes undefined");
    case SA_MUNDEFINED: return cstring_makeLiteralTemp ("possibly undefined");

    case SA_SHARED: return cstring_makeLiteralTemp ("becomes shared");
    case SA_ONLY: return cstring_makeLiteralTemp ("becomes only");
    case SA_IMPONLY: return cstring_makeLiteralTemp ("becomes implicitly only");
    case SA_OWNED: return cstring_makeLiteralTemp ("becomes owned");
    case SA_DEPENDENT: return cstring_makeLiteralTemp ("becomes dependent");
    case SA_IMPDEPENDENT: return cstring_makeLiteralTemp ("becomes implicitly dependent");
    case SA_KEPT: return cstring_makeLiteralTemp ("becomes kept");
    case SA_KEEP: return cstring_makeLiteralTemp ("becomes keep");
    case SA_FRESH: return cstring_makeLiteralTemp ("becomes fresh");
    case SA_TEMP: return cstring_makeLiteralTemp ("becomes temp");
    case SA_IMPTEMP: return cstring_makeLiteralTemp ("becomes implicitly temp");
    case SA_XSTACK: return cstring_makeLiteralTemp ("becomes stack-allocated storage");
    case SA_STATIC: return cstring_makeLiteralTemp ("becomes static");
    case SA_LOCAL: return cstring_makeLiteralTemp ("becomes local");

    case SA_REFCOUNTED: return cstring_makeLiteralTemp ("becomes refcounted");
    case SA_REFS: return cstring_makeLiteralTemp ("becomes refs");
    case SA_NEWREF: return cstring_makeLiteralTemp ("becomes newref");
    case SA_KILLREF: return cstring_makeLiteralTemp ("becomes killref");

    case SA_OBSERVER: return cstring_makeLiteralTemp ("becomes observer");
    case SA_EXPOSED: return cstring_makeLiteralTemp ("becomes exposed");

    case SA_BECOMESNULL: return cstring_makeLiteralTemp ("becomes null");
    case SA_BECOMESNONNULL: return cstring_makeLiteralTemp ("becomes non-null");
    case SA_BECOMESPOSSIBLYNULL: return cstring_makeLiteralTemp ("becomes possibly null");
    } 
 
  DPRINTF (("Bad state action: %d", sa));
  BADBRANCH;
}

void stateInfo_display (stateInfo s, cstring sname)
{
  bool showdeep = context_flagOn (FLG_SHOWDEEPHISTORY, g_currentloc);

  s = stateInfo_sort (s);
  
  while (stateInfo_isDefined (s))
    {
      cstring msg = message ("%s%s", sname, stateAction_unparse (s->action)); 
      
      if (sRef_isValid (s->ref)) {
	msg = message ("%q (through alias %q)", msg, sRef_unparse (s->ref));
      }
      
      llgenindentmsg (msg, s->loc);

      if (!showdeep) {
	break;
      }

      s = s->previous;
    }

  cstring_free (sname);
}