]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ** LCLint - annotation-assisted static program checker | |
3 | ** Copyright (C) 1994-2001 University of Virginia, | |
4 | ** Massachusetts Institute of Technology | |
5 | ** | |
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. | |
10 | ** | |
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. | |
15 | ** | |
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. | |
19 | ** | |
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 | |
23 | */ | |
24 | /* | |
25 | ** mtContextNode.c | |
26 | */ | |
27 | ||
28 | # include "lclintMacros.nf" | |
29 | # include "basic.h" | |
30 | # include "mtgrammar.h" | |
31 | ||
32 | static bool mtContextNode_matchesType (mtContextNode, ctype) /*@*/ ; | |
33 | ||
34 | static /*@observer@*/ cstring mtContextKind_unparse (mtContextKind ck) | |
35 | { | |
36 | switch (ck) | |
37 | { | |
38 | case MTC_ANY: return cstring_makeLiteralTemp ("any"); | |
39 | case MTC_PARAM: return cstring_makeLiteralTemp ("param"); | |
40 | case MTC_REFERENCE: return cstring_makeLiteralTemp ("reference"); | |
41 | case MTC_CLAUSE: return cstring_makeLiteralTemp ("clause"); | |
42 | } | |
43 | ||
44 | BADBRANCH; | |
45 | } | |
46 | ||
47 | static mtContextNode mtContextNode_create (mtContextKind context, ctype ct) | |
48 | { | |
49 | mtContextNode res = (mtContextNode) dmalloc (sizeof (*res)); | |
50 | res->context = context; | |
51 | res->type = ct; | |
52 | DPRINTF (("Create: %s", mtContextNode_unparse (res))); | |
53 | return res; | |
54 | } | |
55 | ||
56 | extern mtContextNode mtContextNode_createAny () | |
57 | { | |
58 | return mtContextNode_create (MTC_ANY, ctype_unknown); | |
59 | } | |
60 | ||
61 | extern mtContextNode mtContextNode_createParameter (ctype ct) | |
62 | { | |
63 | return mtContextNode_create (MTC_PARAM, ct); | |
64 | } | |
65 | ||
66 | extern mtContextNode mtContextNode_createReference (ctype ct) | |
67 | { | |
68 | return mtContextNode_create (MTC_REFERENCE, ct); | |
69 | } | |
70 | ||
71 | extern mtContextNode mtContextNode_createClause (ctype ct) | |
72 | { | |
73 | return mtContextNode_create (MTC_CLAUSE, ct); | |
74 | } | |
75 | ||
76 | extern void mtContextNode_free (/*@only@*/ mtContextNode node) | |
77 | { | |
78 | sfree (node); | |
79 | } | |
80 | ||
81 | bool mtContextNode_matchesEntry (mtContextNode context, uentry ue) | |
82 | { | |
83 | ctype ct; | |
84 | ||
85 | llassert (mtContextNode_isDefined (context)); | |
86 | ||
87 | DPRINTF (("Matches context: %s / %s", | |
88 | mtContextNode_unparse (context), uentry_unparse (ue))); | |
89 | ||
90 | switch (context->context) | |
91 | { | |
92 | case MTC_ANY: break; /* everything matches */ | |
93 | case MTC_PARAM: | |
94 | if (!uentry_isParam (ue)) | |
95 | { | |
96 | return FALSE; | |
97 | } | |
98 | break; | |
99 | case MTC_REFERENCE: | |
100 | break; | |
101 | case MTC_CLAUSE: | |
102 | BADBRANCH; | |
103 | } | |
104 | ||
105 | if (uentry_isFunction (ue)) | |
106 | { | |
107 | ct = ctype_getReturnType (uentry_getType (ue)); | |
108 | } | |
109 | else | |
110 | { | |
111 | ct = uentry_getType (ue); | |
112 | } | |
113 | ||
114 | return mtContextNode_matchesType (context, ct); | |
115 | } | |
116 | ||
117 | bool mtContextNode_matchesRef (mtContextNode context, sRef sr) | |
118 | { | |
119 | ctype ct; | |
120 | ||
121 | llassert (mtContextNode_isDefined (context)); | |
122 | ||
123 | DPRINTF (("Matches context: %s / %s", | |
124 | mtContextNode_unparse (context), sRef_unparse (sr))); | |
125 | ||
126 | switch (context->context) | |
127 | { | |
128 | case MTC_ANY: break; /* everything matches */ | |
129 | case MTC_PARAM: | |
130 | if (!sRef_isParam (sr)) | |
131 | { | |
132 | return FALSE; | |
133 | } | |
134 | break; | |
135 | case MTC_REFERENCE: | |
136 | break; | |
137 | case MTC_CLAUSE: | |
138 | BADBRANCH; | |
139 | } | |
140 | ||
141 | ct = sRef_getType (sr); | |
142 | return mtContextNode_matchesType (context, ct); | |
143 | } | |
144 | ||
145 | bool mtContextNode_matchesRefStrict (mtContextNode context, sRef s) | |
146 | { | |
147 | if (mtContextNode_matchesRef (context, s)) | |
148 | { | |
149 | if (ctype_isKnown (context->type) | |
150 | && (ctype_isUnknown (sRef_getType (s)) | |
151 | || ctype_isVoidPointer (sRef_getType (s)))) | |
152 | { | |
153 | return FALSE; | |
154 | } | |
155 | else | |
156 | { | |
157 | return TRUE; | |
158 | } | |
159 | } | |
160 | ||
161 | return FALSE; | |
162 | } | |
163 | ||
164 | bool mtContextNode_matchesType (mtContextNode context, ctype ct) | |
165 | { | |
166 | DPRINTF (("Context type...")); | |
167 | ||
168 | if (!ctype_match (context->type, ct)) | |
169 | { | |
170 | DPRINTF (("Type mismatch: %s / %s", | |
171 | ctype_unparse (context->type), | |
172 | ctype_unparse (ct))); | |
173 | return FALSE; | |
174 | } | |
175 | else | |
176 | { | |
177 | DPRINTF (("Type okay: %s / %s", | |
178 | ctype_unparse (context->type), | |
179 | ctype_unparse (ct))); | |
180 | } | |
181 | ||
182 | return TRUE; | |
183 | } | |
184 | ||
185 | cstring mtContextNode_unparse (mtContextNode node) | |
186 | { | |
187 | if (ctype_isKnown (node->type)) | |
188 | { | |
189 | return message ("%s %s", mtContextKind_unparse (node->context), | |
190 | ctype_unparse (node->type)); | |
191 | } | |
192 | else | |
193 | { | |
194 | return message ("%s", mtContextKind_unparse (node->context)); | |
195 | } | |
196 | } | |
197 | ||
198 | bool mtContextNode_isClause (mtContextNode n) | |
199 | { | |
200 | llassert (mtContextNode_isDefined (n)); | |
201 | return (n->context == MTC_CLAUSE); | |
202 | } | |
203 | ||
204 | bool mtContextNode_isParameter (mtContextNode n) | |
205 | { | |
206 | llassert (mtContextNode_isDefined (n)); | |
207 | return (n->context == MTC_PARAM); | |
208 | } | |
209 | ||
210 | bool mtContextNode_isRef (mtContextNode n) | |
211 | { | |
212 | llassert (mtContextNode_isDefined (n)); | |
213 | return (n->context == MTC_REFERENCE); | |
214 | } | |
215 | ||
216 | ||
217 | ||
218 |