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
28 # include "splintMacros.nf"
31 /* patch for linux? solaris? */
33 static char strbuf[64];
39 XCHAR, XSTRING, XSTRINGFREE, XTSTRINGFREE, XINT, XFLOAT, XBOOL, XUENTRY,
40 XPERCENT, XCTYPE, XPLURAL, XREPREFIX, XFILELOC, XPOINTER
43 /*@function void GETPRINTF (char *p_s, anytype p_v) modifies strbuf@*/
47 /* ISO requires this, but not all implementations (e.g., Microsoft's) provide it */
48 # define GETPRINTF(s,v) (snprintf (strbuf, 64, s, v), mstring_copy (strbuf))
50 /* MS provides _snprintf instead */
51 # define GETPRINTF(s,v) (_snprintf (strbuf, 64, s, v), mstring_copy (strbuf))
55 ** returns control code indicated by *c, and
56 ** advances *c to next character.
59 static ccode identify_control (char **s)
71 if (c >= '0' && c <= '9')
73 modcode = reader_getInt (s);
81 ** handle single-char codes
100 return (XINT); /* unsigned */
102 return (XINT); /* unsigned long */
118 llcontbug (message ("Message: invalid code: %h (%s)", c,
119 cstring_fromChars (*s)));
127 ** returns a cstring containing the message, as formated by s.
129 ** the format codes are similar to printf:
131 ** %s cstring (don't free after print)
132 ** %q cstring (free after print)
135 ** %b bool (uses bool_unparse)
144 message (fmt, va_alist)
148 /*@messagelike@*/ /*@only@*/ cstring
149 message (/*@temp@*/ char *fmt, ...)
154 char *ret = mstring_createEmpty ();
161 va_start (pvar, fmt);
164 while ((c = *fmt++) != '\0')
168 /*@-loopswitchbreak@*/
170 switch (identify_control (&fmt))
174 ret = mstring_concatFree1 (ret, "%");
180 ** some systems don't handle char va_arg correctly, so it must be
181 ** passed as an int here
184 char lc = (char) va_arg (pvar, int);
186 ret = mstring_append (ret, lc);
191 cstring s = va_arg (pvar, cstring);
195 ret = mstring_concatFree (ret, cstring_toCharsSafe
196 (cstring_fill (s, size_fromInt (modcode))));
200 if (cstring_isDefined (s))
202 ret = mstring_concatFree1 (ret, cstring_toCharsSafe (s));
210 cstring s = va_arg (pvar, cstring);
214 ret = mstring_concatFree (ret, cstring_toCharsSafe
215 (cstring_fill (s, size_fromInt (modcode))));
219 if (cstring_isDefined (s))
221 ret = mstring_concatFree
222 (ret, cstring_toCharsSafe (s));
228 lastint = va_arg (pvar, int);
232 ret = mstring_concatFree1 (ret, "re");
238 ret = mstring_concatFree1 (ret, "s");
242 lastint = va_arg (pvar, int);
243 ret = mstring_concatFree (ret, GETPRINTF ("%d", lastint));
246 ret = mstring_concatFree (ret, GETPRINTF ("%.2lf", va_arg (pvar, double)));
249 ret = mstring_concatFree1
250 (ret, cstring_toCharsSafe
251 (bool_unparse (bool_fromInt ((va_arg (pvar, int))))));
252 /* va_arg should not use bool type */
255 ret = mstring_concatFree (ret, cstring_toCharsSafe
256 (uentry_unparse (va_arg (pvar, uentry))));
259 /* cannot free ctype_unparse */
260 ret = mstring_concatFree1 (ret, cstring_toCharsSafe
261 (ctype_unparse (va_arg (pvar, ctype))));
264 ret = mstring_concatFree (ret, GETPRINTF ("%p", va_arg (pvar, void *)));
268 ret = mstring_concatFree (ret, cstring_toCharsSafe
269 (fileloc_unparse (va_arg (pvar, fileloc))));
273 llcontbug (cstring_makeLiteral ("message: bad control flag"));
274 fprintf (stdout, "\tFormat string: %s", ofmt);
276 /*@=loopswitchbreak@*/
280 ret = mstring_append (ret, c);
287 ** cstring_fromChars returns the same storage (exposed)
290 /*@-mustfree@*/ return (cstring_fromChars (ret)); /*@=mustfree@*/