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 "lclintMacros.nf"
30 # include "cgrammar.h"
31 # include "cgrammar_tokens.h"
34 stateClause_createRaw (stateConstraint st, stateClauseKind sk, /*@only@*/ sRefSet s)
36 stateClause ret = (stateClause) dmalloc (sizeof (*ret));
40 ret->squal = qual_createUnknown ();
42 ret->loc = fileloc_undefined;
47 stateClause_create (lltok tok, qual q, sRefSet s)
49 stateClause ret = (stateClause) dmalloc (sizeof (*ret));
51 if (lltok_getTok (tok) == QPRECLAUSE)
53 ret->state = TK_BEFORE;
55 else if (lltok_getTok (tok) == QPOSTCLAUSE)
57 ret->state = TK_AFTER;
64 ret->loc = fileloc_copy (lltok_getLoc (tok));
69 if (sRefSet_isDefined (s))
75 ret->kind = SP_GLOBAL;
81 bool stateClause_isBeforeOnly (stateClause cl)
83 return (cl->state == TK_BEFORE);
86 bool stateClause_isBefore (stateClause cl)
88 return (cl->state == TK_BEFORE || cl->state == TK_BOTH);
91 bool stateClause_isAfter (stateClause cl)
93 return (cl->state == TK_AFTER || cl->state == TK_BOTH);
96 bool stateClause_isEnsures (stateClause cl)
98 return (cl->state == TK_AFTER);
101 bool stateClause_isMemoryAllocation (stateClause cl)
115 return (qual_isMemoryAllocation (cl->squal)
116 || qual_isSharing (cl->squal));
123 ** An error is reported if the test is NOT true.
126 sRefTest stateClause_getPreTestFunction (stateClause cl)
131 return sRef_isStrictReadable;
133 return sRef_hasNoStorage;
135 return sRef_hasNoStorage;
137 return sRef_isNotUndefined;
139 return sRef_isNotUndefined;
144 if (qual_isOnly (cl->squal)) {
146 } else if (qual_isShared (cl->squal)) {
147 return sRef_isShared;
148 } else if (qual_isDependent (cl->squal)) {
149 return sRef_isDependent;
150 } else if (qual_isOwned (cl->squal)) {
152 } else if (qual_isObserver (cl->squal)) {
153 return sRef_isObserver;
154 } else if (qual_isExposed (cl->squal)) {
155 return sRef_isExposed;
156 } else if (qual_isNotNull (cl->squal)) {
157 return sRef_isNotNull;
158 } else if (qual_isIsNull (cl->squal)) {
159 return sRef_isDefinitelyNull;
169 sRefTest stateClause_getPostTestFunction (stateClause cl)
171 llassert (stateClause_isAfter (cl));
178 return sRef_isAllocated;
180 return sRef_isReallyDefined;
182 return sRef_isReallyDefined;
184 return sRef_isDeadStorage;
188 if (qual_isOnly (cl->squal)) {
190 } else if (qual_isShared (cl->squal)) {
191 return sRef_isShared;
192 } else if (qual_isDependent (cl->squal)) {
193 return sRef_isDependent;
194 } else if (qual_isOwned (cl->squal)) {
196 } else if (qual_isObserver (cl->squal)) {
197 return sRef_isObserver;
198 } else if (qual_isExposed (cl->squal)) {
199 return sRef_isExposed;
200 } else if (qual_isNotNull (cl->squal)) {
201 return sRef_isNotNull;
202 } else if (qual_isIsNull (cl->squal)) {
203 return sRef_isDefinitelyNull;
212 sRefShower stateClause_getPostTestShower (stateClause cl)
221 return sRef_showNotReallyDefined;
227 if (qual_isMemoryAllocation (cl->squal)) {
228 return sRef_showAliasInfo;
229 } else if (qual_isSharing (cl->squal)) {
230 return sRef_showExpInfo;
231 } else if (qual_isIsNull (cl->squal) || qual_isNotNull (cl->squal)) {
232 return sRef_showNullInfo;
241 sRefMod stateClause_getEntryFunction (stateClause cl)
243 if (cl->state == TK_BEFORE || cl->state == TK_BOTH)
248 return sRef_setDefinedComplete;
254 return sRef_setAllocatedComplete;
256 return sRef_setDefinedComplete;
260 if (qual_isOnly (cl->squal)) {
262 } else if (qual_isShared (cl->squal)) {
263 return sRef_setShared;
264 } else if (qual_isDependent (cl->squal)) {
265 return sRef_setDependent;
266 } else if (qual_isOwned (cl->squal)) {
267 return sRef_setOwned;
268 } else if (qual_isObserver (cl->squal)) {
269 return sRef_setObserver;
270 } else if (qual_isExposed (cl->squal)) {
271 return sRef_setExposed;
272 } else if (qual_isNotNull (cl->squal)) {
273 return sRef_setNotNull;
274 } else if (qual_isIsNull (cl->squal)) {
275 return sRef_setDefNull;
277 DPRINTF (("Here we are: %s",
278 qual_unparse (cl->squal)));
293 sRefMod stateClause_getEffectFunction (stateClause cl)
295 if (cl->state == TK_AFTER || cl->state == TK_BOTH)
302 return sRef_setAllocatedComplete;
304 return sRef_setDefinedNCComplete;
306 return sRef_setDefinedNCComplete;
308 return sRef_killComplete;
312 if (qual_isOnly (cl->squal)) {
314 } else if (qual_isShared (cl->squal)) {
315 return sRef_setShared;
316 } else if (qual_isDependent (cl->squal)) {
317 return sRef_setDependent;
318 } else if (qual_isOwned (cl->squal)) {
319 return sRef_setOwned;
320 } else if (qual_isObserver (cl->squal)) {
321 return sRef_setObserver;
322 } else if (qual_isExposed (cl->squal)) {
323 return sRef_setExposed;
324 } else if (qual_isNotNull (cl->squal)) {
325 return sRef_setNotNull;
326 } else if (qual_isIsNull (cl->squal)) {
327 return sRef_setDefNull;
343 sRefMod stateClause_getReturnEffectFunction (stateClause cl)
345 if (cl->state == TK_AFTER || cl->state == TK_BOTH)
358 if (qual_isOnly (cl->squal)) {
359 return sRef_killComplete;
375 static flagcode stateClause_qualErrorCode (stateClause cl)
377 if (qual_isOnly (cl->squal)) {
378 return FLG_ONLYTRANS;
379 } else if (qual_isShared (cl->squal)) {
380 return FLG_SHAREDTRANS;
381 } else if (qual_isDependent (cl->squal)) {
382 return FLG_DEPENDENTTRANS;
383 } else if (qual_isOwned (cl->squal)) {
384 return FLG_OWNEDTRANS;
385 } else if (qual_isObserver (cl->squal)) {
386 return FLG_OBSERVERTRANS;
387 } else if (qual_isExposed (cl->squal)) {
388 return FLG_EXPOSETRANS;
389 } else if (qual_isIsNull (cl->squal)
390 || qual_isNotNull (cl->squal)) {
391 return FLG_NULLSTATE;
397 flagcode stateClause_preErrorCode (stateClause cl)
399 llassert (cl->state == TK_BOTH || cl->state == TK_BEFORE);
405 case SP_ALLOCATES: /*@fallthrough@*/
413 return stateClause_qualErrorCode (cl);
419 static /*@observer@*/ cstring stateClause_qualErrorString (stateClause cl, sRef sr)
421 if (qual_isMemoryAllocation (cl->squal)) {
422 return alkind_capName (sRef_getAliasKind (sr));
423 } else if (qual_isObserver (cl->squal)) {
424 return cstring_makeLiteralTemp ("Non-observer");
425 } else if (qual_isExposed (cl->squal)) {
426 if (sRef_isObserver (sr))
428 return cstring_makeLiteralTemp ("Observer");
432 return cstring_makeLiteralTemp ("Non-exposed");
434 } else if (qual_isNotNull (cl->squal)) {
435 if (sRef_isDefinitelyNull (sr))
437 return cstring_makeLiteralTemp ("Null");
441 return cstring_makeLiteralTemp ("Possibly null");
443 } else if (qual_isIsNull (cl->squal)) {
444 return cstring_makeLiteralTemp ("Non-null");
452 cstring stateClause_preErrorString (stateClause cl, sRef sr)
454 llassert (cl->state == TK_BOTH || cl->state == TK_BEFORE);
459 if (sRef_isDead (sr))
460 return cstring_makeLiteralTemp ("Dead");
462 return cstring_makeLiteralTemp ("Undefined");
463 case SP_ALLOCATES: /*@fallthrough@*/
466 return cstring_makeLiteralTemp ("Allocated");
468 if (sRef_isDead (sr))
470 return cstring_makeLiteralTemp ("Dead");
472 else if (sRef_isDependent (sr)
473 || sRef_isShared (sr))
475 return alkind_unparse (sRef_getAliasKind (sr));
477 else if (sRef_isObserver (sr) || sRef_isExposed (sr))
479 return exkind_unparse (sRef_getExKind (sr));
483 return cstring_makeLiteralTemp ("Undefined");
488 return stateClause_qualErrorString (cl, sr);
494 flagcode stateClause_postErrorCode (stateClause cl)
496 llassert (cl->state == TK_BOTH || cl->state == TK_AFTER);
512 return stateClause_qualErrorCode (cl);
518 cstring stateClause_postErrorString (stateClause cl, sRef sr)
520 llassert (cl->state == TK_BOTH || cl->state == TK_AFTER);
526 return cstring_makeLiteralTemp ("<ERROR>");
528 return cstring_makeLiteralTemp ("Unallocated");
531 return cstring_makeLiteralTemp ("Undefined");
533 return cstring_makeLiteralTemp ("Unreleased");
537 return stateClause_qualErrorString (cl, sr);
543 cstring stateClause_dump (stateClause s)
545 return (message ("%d.%d.%q.%q",
548 qual_dump (s->squal),
549 sRefSet_dump (s->refs)));
552 stateClause stateClause_undump (char **s)
554 stateClause ret = (stateClause) dmalloc (sizeof (*ret));
556 ret->loc = fileloc_undefined;
557 ret->state = (stateConstraint) reader_getInt (s);
558 reader_checkChar (s, '.');
559 ret->kind = (stateClauseKind) reader_getInt (s);
560 reader_checkChar (s, '.');
561 ret->squal = qual_undump (s);
562 reader_checkChar (s, '.');
563 ret->refs = sRefSet_undump (s);
568 stateClause stateClause_copy (stateClause s)
570 stateClause ret = (stateClause) dmalloc (sizeof (*ret));
572 ret->state = s->state;
574 ret->squal = s->squal;
575 ret->refs = sRefSet_newCopy (s->refs);
576 ret->loc = fileloc_copy (s->loc);
581 bool stateClause_sameKind (stateClause s1, stateClause s2)
583 return (s1->state == s2->state
584 && s1->kind == s2->kind
585 && qual_match (s1->squal, s2->squal));
588 void stateClause_free (stateClause s)
590 sRefSet_free (s->refs);
591 fileloc_free (s->loc);
595 static /*@observer@*/ cstring
596 stateClauseKind_unparse (stateClause s)
601 return cstring_makeLiteralTemp ("uses");
603 return cstring_makeLiteralTemp ("defines");
605 return cstring_makeLiteralTemp ("allocates");
607 return cstring_makeLiteralTemp ("releases");
609 return cstring_makeLiteralTemp ("sets");
611 return qual_unparse (s->squal);
613 return qual_unparse (s->squal);
619 cstring stateClause_unparseKind (stateClause s)
623 cstring_makeLiteralTemp (s->state == TK_BEFORE
625 : (s->state == TK_AFTER
627 stateClauseKind_unparse (s)));
630 cstring stateClause_unparse (stateClause s)
632 return (message ("%q %q",
633 stateClause_unparseKind (s), sRefSet_unparsePlain (s->refs)));
636 stateClause stateClause_createDefines (sRefSet s)
638 return (stateClause_createRaw (TK_BOTH, SP_DEFINES, s));
641 stateClause stateClause_createUses (sRefSet s)
643 return (stateClause_createRaw (TK_BOTH, SP_USES, s));
646 stateClause stateClause_createSets (sRefSet s)
648 return (stateClause_createRaw (TK_BOTH, SP_SETS, s));
651 stateClause stateClause_createReleases (sRefSet s)
653 return (stateClause_createRaw (TK_BOTH, SP_RELEASES, s));
656 stateClause stateClause_createPlain (lltok tok, sRefSet s)
658 switch (lltok_getTok (tok))
661 return stateClause_createUses (s);
663 return stateClause_createDefines (s);
665 return stateClause_createAllocates (s);
667 return stateClause_createSets (s);
669 return stateClause_createReleases (s);
676 stateClause stateClause_createAllocates (sRefSet s)
678 return (stateClause_createRaw (TK_BOTH, SP_ALLOCATES, s));
681 bool stateClause_matchKind (stateClause s1, stateClause s2)
683 return (s1->state == s2->state && s1->kind == s2->kind
684 && qual_match (s1->squal, s2->squal));
687 bool stateClause_hasEnsures (stateClause cl)
689 return (cl->state == TK_AFTER && (cl->kind == SP_QUAL || cl->kind == SP_GLOBAL));
692 bool stateClause_hasRequires (stateClause cl)
694 return (cl->state == TK_BEFORE && (cl->kind == SP_QUAL || cl->kind == SP_GLOBAL));
697 bool stateClause_setsMetaState (stateClause cl)
699 return ((cl->kind == SP_QUAL || cl->kind == SP_GLOBAL)
700 && qual_isMetaState (cl->squal));
703 qual stateClause_getMetaQual (stateClause cl)
705 llassert (cl->kind == SP_QUAL || cl->kind == SP_GLOBAL);
709 static sRefModVal stateClause_getStateFunction (stateClause cl)
713 llassert (cl->kind == SP_QUAL || cl->kind == SP_GLOBAL);
719 if (qual_isNullStateQual (sq))
721 return (sRefModVal) sRef_setNullState;
723 else if (qual_isExQual (sq))
725 return (sRefModVal) sRef_setExKind;
727 else if (qual_isAliasQual (sq))
729 return (sRefModVal) sRef_setAliasKind; /*@i23 complete? @*/
733 DPRINTF (("Unhandled ensures qual: %s", qual_unparse (sq)));
739 int stateClause_getStateParameter (stateClause cl)
743 llassert (cl->kind == SP_QUAL || cl->kind == SP_GLOBAL);
747 /*@+relaxtypes@*/ /*@i523 this is wrong, remove the enumint@*/
750 if (qual_isNotNull (sq))
754 else if (qual_isIsNull (sq))
758 else if (qual_isNull (sq))
762 else if (qual_isRelNull (sq))
766 else if (qual_isExposed (sq))
770 else if (qual_isObserver (sq))
774 else if (qual_isAliasQual (sq))
776 if (qual_isOnly (sq)) return AK_ONLY;
777 if (qual_isImpOnly (sq)) return AK_IMPONLY;
778 if (qual_isTemp (sq)) return AK_TEMP;
779 if (qual_isOwned (sq)) return AK_OWNED;
780 if (qual_isShared (sq)) return AK_SHARED;
781 if (qual_isUnique (sq)) return AK_UNIQUE;
782 if (qual_isDependent (sq)) return AK_DEPENDENT;
783 if (qual_isKeep (sq)) return AK_KEEP;
784 if (qual_isKept (sq)) return AK_KEPT;
789 DPRINTF (("Unhandled ensures qual: %s", qual_unparse (sq)));
797 sRefModVal stateClause_getEnsuresFunction (stateClause cl)
799 llassertprint (cl->state == TK_AFTER, ("Not after: %s", stateClause_unparse (cl)));
800 llassert (cl->kind == SP_QUAL || cl->kind == SP_GLOBAL);
801 return stateClause_getStateFunction (cl);
804 sRefModVal stateClause_getRequiresBodyFunction (stateClause cl)
806 llassertprint (cl->state == TK_BEFORE, ("Not before: %s", stateClause_unparse (cl)));
807 llassert (cl->kind == SP_QUAL || cl->kind == SP_GLOBAL);
808 return stateClause_getStateFunction (cl);
811 /*@observer@*/ fileloc stateClause_loc (stateClause s)