Some consistency changes to library & headers flags.
[splint-patched.git] / src / cprim.c
blobb77db8c0758d21537a1093bc4b26d9f19fd1c7d7
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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.
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 ** cprim.c
28 # include "splintMacros.nf"
29 # include "basic.h"
31 static bool cprim_isReal (cprim c)
33 return (cprim_isAnyReal (c));
36 static bool cprim_isNumeric (cprim c)
38 return (cprim_isReal (c) || cprim_isInt (c));
41 cprim
42 cprim_fromInt (int i)
44 if (i < CTX_UNKNOWN || i > CTX_LAST)
46 llcontbug (message ("cprim_fromInt: out of range: %d", i));
47 return CTX_UNKNOWN;
49 return (cprim) i;
54 ** not symmetric: c1 := c2 or c2 is passed as c1
55 ** (if RELAXQUALS, c1 must be "bigger" than c2)
58 static bool cprim_closeEnoughAux (cprim p_c1, cprim p_c2, bool p_deep);
60 bool
61 cprim_closeEnoughDeep (cprim c1, cprim c2)
64 ** If * c2 is passed as * c1
65 ** Comparison is slightly different since it is safe to pass int as long,
66 ** but not to pass int * as long *!
68 ** For deep comparisons, +relaxquals does not permit the long/int break.
71 return cprim_closeEnoughAux (c1, c2, TRUE);
74 bool
75 cprim_closeEnough (cprim c1, cprim c2)
77 return cprim_closeEnoughAux (c1, c2, FALSE);
80 static bool
81 cprim_closeEnoughAux (cprim c1, cprim c2, bool deep)
83 if (c1 == c2) return TRUE;
85 DPRINTF (("cprim close: %s / %s", cprim_unparse (c1), cprim_unparse (c2)));
87 if (c1 == CTX_ANYINTEGRAL)
89 if (context_getFlag (FLG_MATCHANYINTEGRAL)
90 || context_getFlag (FLG_IGNOREQUALS))
92 return (cprim_isAnyInt (c2)
93 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
95 else if (context_getFlag (FLG_LONGINTEGRAL))
97 return (cprim_closeEnough (CTX_LINT, c2));
99 else if (context_getFlag (FLG_LONGUNSIGNEDINTEGRAL))
101 return (cprim_closeEnough (CTX_ULINT, c2));
103 else
105 return FALSE;
109 if (c1 == CTX_UNSIGNEDINTEGRAL)
111 /* We allow signed ints to match any integral if matchanyintegral is set */
112 if (context_getFlag (FLG_MATCHANYINTEGRAL)) {
113 return (cprim_isAnyInt (c2)
114 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
117 if (context_getFlag (FLG_IGNOREQUALS))
119 if (context_getFlag (FLG_IGNORESIGNS))
121 return (cprim_isAnyUnsignedInt (c2)
122 || (cprim_isUnsignedChar (c2) && context_msgCharInt ()));
124 else
126 return (cprim_isAnyInt (c2)
127 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
130 else if (context_getFlag (FLG_LONGUNSIGNEDUNSIGNEDINTEGRAL))
132 return (cprim_closeEnough (CTX_ULINT, c2));
134 else
136 return FALSE;
140 if (c1 == CTX_SIGNEDINTEGRAL)
142 /* We allow signed ints to match any integral if matchanyintegral is set */
143 if (context_getFlag (FLG_MATCHANYINTEGRAL)) {
144 return (cprim_isAnyInt (c2)
145 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
148 if (context_getFlag (FLG_IGNOREQUALS))
150 return (cprim_isAnyInt (c2)
151 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
153 else if (context_getFlag (FLG_LONGSIGNEDINTEGRAL))
155 return (cprim_closeEnough (CTX_LINT, c2));
157 else
159 return FALSE;
163 if (c2 == CTX_ANYINTEGRAL)
165 if (context_getFlag (FLG_MATCHANYINTEGRAL))
167 return (cprim_isAnyInt (c1)
168 || (cprim_isAnyChar (c1) && context_msgCharInt ()));
170 else if (context_getFlag (FLG_LONGINTEGRAL))
172 return (cprim_closeEnough (c1, CTX_LINT));
174 else if (context_getFlag (FLG_LONGUNSIGNEDINTEGRAL))
176 return (cprim_closeEnough (c1, CTX_ULINT));
178 else
180 return FALSE;
184 if (c2 == CTX_UNSIGNEDINTEGRAL)
186 if (context_getFlag (FLG_MATCHANYINTEGRAL))
188 return (cprim_isAnyInt (c1)
189 || (cprim_isAnyChar (c1) && context_msgCharInt ()));
191 else if (context_getFlag (FLG_LONGUNSIGNEDUNSIGNEDINTEGRAL))
193 return (cprim_closeEnough (c1, CTX_ULINT));
195 else
197 return FALSE;
201 if (c2 == CTX_SIGNEDINTEGRAL)
203 if (context_getFlag (FLG_MATCHANYINTEGRAL))
205 return (cprim_isAnyInt (c2)
206 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
208 else if (context_getFlag (FLG_LONGSIGNEDINTEGRAL))
210 return (cprim_closeEnough (c1, CTX_LINT));
212 else
214 return FALSE;
219 DPRINTF (("cprim close: %s / %s", cprim_unparse (c1), cprim_unparse (c2)));
221 if (context_getFlag (FLG_RELAXTYPES))
223 if (cprim_isNumeric (c1) && cprim_isNumeric (c2)) return TRUE;
226 if (context_getFlag (FLG_IGNOREQUALS))
228 switch (c1)
230 case CTX_CHAR:
231 case CTX_UCHAR:
232 if (cprim_isAnyChar (c2)
233 || (cprim_isAnyInt (c2) && (context_msgCharInt ()))) {
234 return TRUE;
236 break;
237 case CTX_DOUBLE:
238 case CTX_FLOAT:
239 case CTX_LDOUBLE:
240 if (c2 == CTX_DOUBLE || c2 == CTX_FLOAT || c2 == CTX_LDOUBLE) {
241 return TRUE;
243 break;
244 case CTX_INT:
245 case CTX_LINT:
246 case CTX_LLINT:
247 case CTX_ULLINT:
248 case CTX_SINT:
249 case CTX_UINT:
250 case CTX_ULINT:
251 case CTX_USINT:
252 if (cprim_isAnyInt (c2)
253 || (cprim_isAnyChar (c2) && context_msgCharInt ())) {
254 return TRUE;
256 /*@fallthrough@*/
257 default:
262 if (context_getFlag (FLG_IGNORESIGNS))
264 if (c1 == CTX_UCHAR)
266 c1 = CTX_CHAR;
268 else if (c1 == CTX_UINT)
270 c1 = CTX_INT;
272 else if (c1 == CTX_ULINT)
274 c1 = CTX_LINT;
276 /* 2001-06-10: This fix provided by Jim Zelenka: */
277 else if (c1 == CTX_ULLINT)
279 c1 = CTX_LLINT;
281 /* End fix */
282 else if (c1 == CTX_USINT)
284 c1 = CTX_SINT;
286 else
291 if (c2 == CTX_UCHAR)
293 c2 = CTX_CHAR;
295 else if (c2 == CTX_UINT)
297 c2 = CTX_INT;
299 else if (c2 == CTX_ULINT)
301 c2 = CTX_LINT;
303 /* 2001-06-10: This fix provided by Jim Zelenka: */
304 else if (c2 == CTX_ULLINT)
306 c2 = CTX_LLINT;
308 /* End fix */
309 else if (c2 == CTX_USINT)
311 c2 = CTX_SINT;
313 else
319 if (c1 == c2) return TRUE;
321 if (context_getFlag (FLG_FLOATDOUBLE))
323 if (c1 == CTX_FLOAT && c2 == CTX_DOUBLE)
325 return TRUE;
327 if (c2 == CTX_FLOAT && c1 == CTX_DOUBLE)
329 return TRUE;
333 DPRINTF (("cprim close: %s / %s", cprim_unparse (c1), cprim_unparse (c2)));
335 if (!deep && context_getFlag (FLG_RELAXQUALS))
337 switch (c1)
339 case CTX_DOUBLE:
340 return (c2 == CTX_FLOAT);
341 case CTX_LDOUBLE:
342 return (c2 == CTX_DOUBLE || c2 == CTX_FLOAT);
343 case CTX_SINT:
344 return ((c2 == CTX_CHAR && context_msgCharInt ())
345 || (c2 == CTX_INT && context_msgShortInt ())
346 || (c2 == CTX_LINT && context_msgShortInt () && context_msgLongInt ()));
348 case CTX_INT:
349 return ((c2 == CTX_SINT
350 || (cprim_isAnyChar (c2) && context_msgCharInt ())
351 || (c2 == CTX_LINT && context_msgLongInt ())));
353 case CTX_LLINT:
354 return (c2 == CTX_SINT
355 || c2 == CTX_INT
356 || c2 == CTX_LINT
357 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
358 case CTX_ULLINT:
359 return (c2 == CTX_USINT
360 || c2 == CTX_UINT
361 || c2 == CTX_ULINT
362 /* 2001-06-10: This fix provided by Jim Zelenka: */
363 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
364 case CTX_LINT:
365 return (c2 == CTX_SINT
366 || c2 == CTX_INT
367 || (cprim_isAnyChar (c2) && context_msgCharInt ()));
368 case CTX_UINT:
369 return (c2 == CTX_USINT
370 || (c2 == CTX_UCHAR && context_msgCharInt ()));
371 case CTX_USINT:
372 return (c2 == CTX_UCHAR && context_msgCharInt ());
373 case CTX_ULINT:
374 /* 2001-06-10: This fix provided by Jim Zelenka: */
375 return (c2 == CTX_UINT || c2 == CTX_USINT
376 || (c2 == CTX_UCHAR && context_msgCharInt()));
377 case CTX_UCHAR:
378 return (c2 == CTX_UINT && context_msgCharInt ());
379 case CTX_CHAR:
380 return ((c2 == CTX_INT || c2 == CTX_SINT)
381 && context_msgCharInt ());
382 default:
383 return FALSE;
386 else
388 switch (c1)
390 case CTX_DOUBLE:
391 case CTX_LDOUBLE:
392 return FALSE;
393 case CTX_SINT:
394 if (c2 == CTX_INT && context_msgShortInt ()) {
395 return TRUE;
397 /*@fallthrough@*/
398 case CTX_INT:
399 if (c2 == CTX_INT && context_msgLongInt ()) {
400 return TRUE;
403 if (c2 == CTX_SINT && context_msgShortInt ()) {
404 return TRUE;
406 /*@fallthrough@*/
407 case CTX_LINT:
408 if (c2 == CTX_INT && context_msgLongInt ()) {
409 return TRUE;
411 /*@fallthrough@*/
412 case CTX_LLINT:
413 return (c2 == CTX_CHAR && context_msgCharInt ());
414 case CTX_UINT:
415 case CTX_USINT:
416 case CTX_ULINT:
417 case CTX_ULLINT:
418 return (c2 == CTX_UCHAR && context_msgCharInt ());
419 case CTX_UCHAR:
420 return (c2 == CTX_UINT && context_msgCharInt ());
421 case CTX_CHAR:
422 return ((c2 == CTX_INT || c2 == CTX_SINT)
423 && context_msgCharInt ());
424 default:
425 return FALSE;
430 /*@only@*/ cstring
431 cprim_unparse (cprim c)
433 switch (c)
435 case CTX_UNKNOWN:
436 return cstring_makeLiteral ("-");
437 case CTX_VOID:
438 return cstring_makeLiteral ("void");
439 case CTX_CHAR:
440 return cstring_makeLiteral ("char");
441 case CTX_UCHAR:
442 return cstring_makeLiteral ("unsigned char");
443 case CTX_DOUBLE:
444 return cstring_makeLiteral ("double");
445 case CTX_LDOUBLE:
446 return cstring_makeLiteral ("long double");
447 case CTX_FLOAT:
448 return cstring_makeLiteral ("float");
449 case CTX_INT:
450 return cstring_makeLiteral ("int");
451 case CTX_LINT:
452 return cstring_makeLiteral ("long int");
453 case CTX_LLINT:
454 return cstring_makeLiteral ("long long");
455 case CTX_ULLINT:
456 return cstring_makeLiteral ("unsigned long long");
457 case CTX_SINT:
458 return cstring_makeLiteral ("short int");
459 case CTX_UINT:
460 return cstring_makeLiteral ("unsigned int");
461 case CTX_ULINT:
462 return cstring_makeLiteral ("unsigned long int");
463 case CTX_USINT:
464 return cstring_makeLiteral ("unsigned short int");
465 case CTX_UNSIGNEDINTEGRAL:
466 return cstring_makeLiteral ("arbitrary unsigned integral type");
467 case CTX_SIGNEDINTEGRAL:
468 return cstring_makeLiteral ("arbitrary signed integral type");
469 case CTX_ANYINTEGRAL:
470 return cstring_makeLiteral ("arbitrary integral type");
471 default:
472 return cstring_makeLiteral ("unknown prim");
476 bool cprim_isInt (cprim c)
478 return (cprim_isAnyInt (c)
479 || (cprim_isAnyChar (c) && context_msgCharInt ()));
482 int cprim_getExpectedBits (cprim c)
484 /* Any basis to these numbers? Just guesses for now..., check ISO spec */
485 switch (c)
487 case CTX_UNKNOWN:
488 return 0;
489 case CTX_VOID:
490 return 0;
491 case CTX_CHAR:
492 return 8;
493 case CTX_UCHAR:
494 return 8;
495 case CTX_DOUBLE:
496 return 64;
497 case CTX_LDOUBLE:
498 return 128;
499 case CTX_FLOAT:
500 return 32;
501 case CTX_INT:
502 return 32;
503 case CTX_LINT:
504 return 64;
505 case CTX_LLINT:
506 return 128;
507 case CTX_ULLINT:
508 return 128;
509 case CTX_SINT:
510 return 8;
511 case CTX_UINT:
512 return 32;
513 case CTX_ULINT:
514 return 64;
515 case CTX_USINT:
516 return 8;
517 case CTX_UNSIGNEDINTEGRAL:
518 return 64;
519 case CTX_SIGNEDINTEGRAL:
520 return 64;
521 case CTX_ANYINTEGRAL:
522 return 64;
523 default:
524 return 0;