2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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
27 ** error reporting procedures
30 # include "splintMacros.nf"
35 # include "cpperror.h"
36 # include "Headers/version.h" /* Visual C++ finds a different version.h on some path! */
38 /* Don't allow possibly-recursive assertion failures. */
40 # define llassert llassertprotect
42 static void printIndentMessage (FILE *p_stream, /*@only@*/ cstring p_sc, int p_indent)
43 /*@modifies *p_stream@*/ ;
45 static int lclerrors = 0;
46 static size_t lastfileloclen = 10;
47 static /*@only@*/ cstring lastmsg = cstring_undefined;
48 static int mcount = 0;
49 static /*@only@*/ cstring saveOneMessage = cstring_undefined;
50 static /*@only@*/ fileloc lastparseerror = fileloc_undefined;
51 static /*@only@*/ fileloc lastbug = fileloc_undefined;
52 static bool llgenerrorreal (flagcode p_code,
53 char *p_srcFile, int p_srcLine,
54 /*@only@*/ cstring p_s,
55 /*@temp@*/ cstring p_addtext,
56 fileloc p_fl, bool p_iserror, bool p_indent)
57 /*@modifies g_warningstream@*/ ;
58 static bool llgenerroraux (flagcode p_code, char *p_srcFile, int p_srcLine,
59 /*@only@*/ cstring p_s,
60 /*@temp@*/ cstring p_addtext,
61 fileloc p_fl, bool p_iserror, bool p_indent)
62 /*@modifies g_warningstream@*/ ;
64 static void generateCSV (flagcode p_code, cstring p_s, cstring p_addtext, fileloc p_fl)
65 /*@modifies g_csvstream@*/ ;
67 static void printError (FILE *p_stream, /*@only@*/ cstring p_sc)
68 /*@globals lastfileloclen @*/
69 /*@modifies *p_stream@*/ ;
70 static void printMessage (FILE *p_stream, /*@only@*/ cstring p_s)
71 /*@modifies *p_stream@*/ ;
73 static void llgenhint (/*@only@*/ cstring p_s) /*@modifies g_warningstream@*/ ;
75 static void showSourceLoc (char *srcFile, int srcLine)
76 /*@modifies g_warningstream@*/
78 if (context_getFlag (FLG_SHOWSOURCELOC)) {
79 llgenhint (message ("%s:%d: Source code error generation point.",
80 cstring_fromChars (srcFile), srcLine));
85 static /*@null@*/ char *
86 maxcp (/*@null@*/ /*@returned@*/ char *a, /*@null@*/ /*@returned@*/ char *b)
95 fprintf (g_errorstream, " *** Please report bug to %s ***\n",
98 /* don't exit (EXIT_FAILURE); */
101 static bool s_needsPrepare = TRUE;
103 void prepareMessage ()
105 DPRINTF (("Prepare message: %s", bool_unparse (context_loadingLibrary ())));
108 if ((context_isPreprocessing () || context_loadingLibrary ())
110 && context_getFlag (FLG_SHOWSCAN))
114 s_needsPrepare = FALSE;
120 void closeMessage (void)
122 if (context_isPreprocessing ()
123 && context_getFlag (FLG_SHOWSCAN))
126 displayScanOpen (cstring_makeLiteral ("more preprocessing ."));
127 llassertprotect (!s_needsPrepare);
128 s_needsPrepare = TRUE;
137 llmsg (/*@only@*/ cstring s)
139 context_setNeednl ();
141 printMessage (g_messagestream, s);
146 lldiagmsg (/*@only@*/ cstring s)
148 static bool inmsg = FALSE;
152 fprintf (g_errorstream,
153 "Recursive message call detected: %s\n",
154 cstring_toCharsSafe (s));
160 context_setNeednl ();
162 printMessage (g_messagestream, s);
169 llmsgplain (/*@only@*/ cstring s)
171 context_setNeednl ();
173 printMessage (g_messagestream, s);
177 void llerror_flagWarning (cstring s)
179 if (context_getFlag (FLG_WARNFLAGS))
181 llgenmsg (s, g_currentloc);
190 llgenhint (/*@only@*/ cstring s) /*@modifies g_warningstream@*/
192 int indent = context_getIndentSpaces () - 1;
194 if (indent < 0) indent = 0;
196 context_setNeednl ();
197 printIndentMessage (g_warningstream, s, indent);
203 if (context_getFlag (FLG_HINTS) &&
204 !(context_inSuppressRegion () || context_inSuppressZone (g_currentloc)))
215 llshowhint (flagcode f)
217 if (context_getFlag (FLG_HINTS))
219 if ((flagcode_numReported (f) == 0) || context_getFlag (FLG_FORCEHINTS))
221 cstring desc = flagcodeHint (f);
223 if (cstring_isDefined (desc))
225 llgenhint (cstring_copy (desc));
232 llsuppresshint2 (char c, flagcode f1, flagcode f2)
235 if (context_getFlag (FLG_HINTS))
237 if ((flagcode_numReported (f1) == 0
238 || flagcode_numReported (f2) == 0)
239 || context_getFlag (FLG_FORCEHINTS))
241 cstring desc = flagcodeHint (f1);
242 context_setNeednl ();
245 if (cstring_isUndefined (desc))
247 desc = flagcodeHint (f2);
250 if (flagcode_isNamePrefixFlag (f1))
255 if (flagcode_isNamePrefixFlag (f2))
262 if (cstring_isDefined (desc))
264 llgenhint (message ("%s (Use %h%s to inhibit warning)", desc,
266 flagcode_unparse (f1)));
270 llgenhint (message ("(Use %h%s to inhibit warning)",
271 c, flagcode_unparse (f1)));
276 if (cstring_isDefined (desc))
278 llgenhint (message ("%s (Use either %h%s or %h%s to inhibit warning)", desc,
280 flagcode_unparse (f1),
282 flagcode_unparse (f2)));
286 llgenhint (message ("(Use either %h%s or %h%s to inhibit warning)", c,
287 flagcode_unparse (f1),
288 c, flagcode_unparse (f2)));
296 llsuppresshint (char c, flagcode f)
298 if (context_getFlag (FLG_HINTS))
300 if ((flagcode_numReported (f) == 0) || context_getFlag (FLG_FORCEHINTS))
302 cstring desc = flagcodeHint (f);
303 context_setNeednl ();
306 if (flagcode_isNamePrefixFlag (f))
311 if (cstring_isDefined (desc))
313 llgenhint (message ("%s (Use %h%s to inhibit warning)", desc, c,
314 flagcode_unparse (f)));
318 llgenhint (message ("(Use %h%s to inhibit warning)", c,
319 flagcode_unparse (f)));
326 llnosuppresshint (flagcode f)
328 if (context_getFlag (FLG_FORCEHINTS))
330 cstring desc = flagcodeHint (f);
331 context_setNeednl ();
334 if (cstring_isDefined (desc))
336 printError (g_warningstream, message (" %s", desc));
341 /*@constant int MAXSEARCH; @*/
342 # define MAXSEARCH 20
344 /*@constant int MINLINE; @*/
347 typedef /*@null@*/ /*@dependent@*/ char *nd_charp;
350 mstring_split (/*@returned@*/ char **sp,
351 /*@out@*/ nd_charp *tp,
352 int maxline, /*@in@*/ int *indentchars)
361 DPRINTF (("Split: %s / %d", *sp, maxline));
363 if (maxline < MINLINELEN)
365 maxline = MINLINELEN;
368 if (*indentchars > 0)
370 s = *sp = mstring_concatFree1 (mstring_spaces (*indentchars), s);
374 nl = strchr (s, '\n');
379 ** if there is a newline in first maxline characters, split there
380 ** if line len is <= maxline, return no split
381 ** if there is a ':' or ';' or ',' followed by ' ' in first maxline characters,
382 ** split there unless the ' ' is followed by a '}', then
384 ** of the ';' is inside quotation marks
385 ** if there is a space or tab in last maxsearch characters, split there
386 ** else, split at maxline
388 ** special code: slash [1-9] after a newline means indent the rest <n> chars
392 if ((nl != NULL) && ((nl - s) < maxline))
399 llassertprotect (*tp == NULL || (*tp > osp));
403 if (*t >= '\1' && *t <= '\7')
405 *indentchars += (int) (*t - '\1') + 1;
412 else if (size_toInt (strlen (s)) < maxline)
414 llassertprotect (*tp == NULL || (*tp > osp));
421 char *lcolon, *lsemi, *lcomma;
430 lcolon = strrchr (s, ':');
431 lsemi = strrchr (s, ';');
432 lcomma = strrchr (s, ',');
436 splitat = maxcp (lcolon, lsemi);
438 if (splitat != NULL && ((int)(splitat - s) > MINLINE)
439 && *(splitat) != '\0'
440 && *(splitat + 1) == ' '
441 && (*(splitat + 2) != '}'
442 && *(splitat + 2) != ','
443 && (*(splitat + 2) != '\0')))
445 *(splitat + 1) = '\0';
448 llassertprotect (*tp == NULL || (*tp > osp));
452 if (lcomma != NULL && ((lcomma - s) > maxline - 5))
456 if (splitat != NULL && ((int)(splitat - s) > MINLINE)
457 && *(splitat) != '\0'
458 && *(splitat + 1) == ' '
459 && (*(splitat + 2) != '}'
460 && (*(splitat + 2) != '\0')))
462 *(splitat + 1) = '\0';
465 llassertprotect (*tp == NULL || (*tp > osp));
470 while (*t != ' ' && *t != '\t' && i < MAXSEARCH)
476 if (*t != ' ' && *t != '\t')
478 llassertprotect (maxline > 0);
479 t = mstring_copy (s + maxline);
480 *(s + maxline) = '\0';
485 llassertprotect (*tp == NULL || (*tp > osp));
489 mstring_markFree (t);
491 /* Won't be true since t is a copy: llassertprotect (*tp == NULL || (*tp > osp)); */
500 if (*t == '\0') return;
503 ** returns unqualified as only
509 /* Hack to prevent error case for wierd strings. */
515 llassertprotect (*tp == NULL || (*tp > osp));
526 void limitmessage (/*@only@*/ cstring s, fileloc loc)
528 if (mcount > context_getLimit () + 1)
534 cstring flstring = fileloc_unparse (loc);
536 lastfileloclen = cstring_length (flstring);
537 cstring_free (saveOneMessage);
538 saveOneMessage = message ("%q: %q", flstring, s);
542 static int parseerrorcount = 0;
544 void cleanupMessages ()
548 if (context_unlimitedMessages ())
554 int unprinted = mcount - context_getLimit ();
558 if (unprinted == 1 && cstring_isDefined (saveOneMessage))
561 printError (g_warningstream, saveOneMessage);
563 saveOneMessage = cstring_undefined;
567 if (cstring_isDefined (saveOneMessage))
569 /* cstring_free (saveOneMessage); */
570 saveOneMessage = cstring_undefined;
573 fprintf (g_warningstream, "%s: (%d more similar errors unprinted)\n",
574 cstring_toCharsSafe (fileloc_filename (g_currentloc)),
575 mcount - context_getLimit ());
584 llgenmsg (/*@only@*/ cstring s, fileloc fl)
586 cstring flstring = fileloc_unparse (fl);
587 lastfileloclen = cstring_length (flstring);
590 (void) printError (g_warningstream, message ("%q: %q", flstring, s));
595 llgenindentmsg (/*@only@*/ cstring s, fileloc fl)
597 cstring flstring = fileloc_unparse (fl);
600 (void) printIndentMessage (g_warningstream, message ("%q: %q", flstring, s), context_getIndentSpaces ());
605 llgenindentmsgnoloc (/*@only@*/ cstring s)
608 (void) printIndentMessage (g_warningstream, s, context_getIndentSpaces ());
613 llgentypeerroraux (char *srcFile, int srcLine,
614 flagcode ocode, ctype t1, exprNode e1, ctype t2, exprNode e2,
615 /*@only@*/ cstring s, fileloc fl)
617 cstring hint = cstring_undefined;
618 flagcode code = ocode;
619 flagcode hcode = INVALID_FLAG;
623 DPRINTF (("Type error [%s]: %s / %s : %s / %s",
624 flagcode_unparse (ocode),
625 exprNode_unparse (e1), exprNode_unparse (e2),
626 ctype_unparse (t1), ctype_unparse (t2)));
628 DPRINTF (("Bool: %s / %s",
629 bool_unparse (ctype_isBool (t1)),
630 bool_unparse (ctype_isBool (t2))));
633 ** Set the flag using the underlying types that didn't match.
636 while (ctype_isPointer (ut1) && ctype_isPointer (ut2)) {
637 ut1 = ctype_baseArrayPtr (ut1);
638 ut2 = ctype_baseArrayPtr (ut2);
641 if ((ctype_isFloat (ut1) && ctype_isDouble (ut2))
642 || (ctype_isFloat (ut1) && ctype_isDouble (ut2)))
644 hcode = FLG_FLOATDOUBLE;
646 else if ((exprNode_isCharLit (e1) && ctype_isInt (ut2))
647 || (exprNode_isCharLit (e2) && ctype_isInt (ut1)))
649 hcode = FLG_CHARINTLITERAL;
651 else if ((exprNode_isNumLit (e1) && ctype_isReal (ut2))
652 || (exprNode_isNumLit (e2) && ctype_isReal (ut1)))
654 hcode = FLG_NUMLITERAL;
656 else if ((ctype_isManifestBool (ut1) && ctype_isInt (ut2))
657 || (ctype_isInt (ut1) && ctype_isManifestBool (ut2)))
658 /* evs 2000-07-24: was ctype_isDirectBool */
662 else if (((ctype_isChar (ut1) && !ctype_isInt (ut1)) && ctype_isInt (ut2))
663 || ((ctype_isInt (ut1) && (ctype_isChar (ut2) && !ctype_isInt (ut2)))))
667 else if ((ctype_isInt (ut1) && ctype_isInt (ut2))
668 || (ctype_isChar (ut1) && ctype_isChar (ut2))
669 || (ctype_isDouble (ut1) && ctype_isDouble (ut2)))
671 if (!bool_equal (ctype_isSigned (ut1), ctype_isSigned (ut2)))
673 if (ctype_isArbitraryIntegral (ctype_realType (ut1))
674 && !ctype_isArbitraryIntegral (ctype_realType (ut2)))
676 hcode = FLG_MATCHANYINTEGRAL;
678 else if (ctype_isArbitraryIntegral (ctype_realType (ut2))
679 && !ctype_isArbitraryIntegral (ctype_realType (ut1)))
681 hcode = FLG_MATCHANYINTEGRAL;
685 hcode = FLG_IGNORESIGNS;
690 hcode = FLG_IGNOREQUALS;
693 else if (ctype_isArbitraryIntegral (ctype_realType (ut1)))
695 if (ctype_isArbitraryIntegral (ctype_realType (ut2)))
697 hcode = FLG_MATCHANYINTEGRAL;
699 else if (ctype_match (ut2, ctype_ulint))
701 hcode = FLG_LONGUNSIGNEDINTEGRAL;
703 else if (ctype_match (ut2, ctype_lint))
705 hcode = FLG_LONGINTEGRAL;
707 else if (ctype_isInt (ut2))
709 hcode = FLG_MATCHANYINTEGRAL;
716 else if (ctype_isArbitraryIntegral (ctype_realType (ut2)))
718 ctype tr = ctype_realType (ut1);
720 if (ctype_isArbitraryIntegral (tr))
722 hcode = FLG_MATCHANYINTEGRAL;
724 else if (ctype_match (ut1, ctype_ulint))
726 if (ctype_isUnsignedIntegral (tr))
728 hcode = FLG_LONGUNSIGNEDUNSIGNEDINTEGRAL;
730 else if (ctype_isSignedIntegral (tr))
736 hcode = FLG_LONGUNSIGNEDINTEGRAL;
739 else if (ctype_match (ut1, ctype_lint))
741 if (ctype_isSignedIntegral (tr))
743 hcode = FLG_LONGSIGNEDINTEGRAL;
745 else if (ctype_isSignedIntegral (tr))
751 hcode = FLG_LONGINTEGRAL;
754 else if (ctype_isInt (ut1))
756 hcode = FLG_MATCHANYINTEGRAL;
768 if (hcode == INVALID_FLAG)
770 DPRINTF (("[%s] %s - %s / %s",
772 bool_unparse (ctype_isEnum (ut1)),
773 bool_unparse (ctype_isEnum (ctype_realType (ut1))),
774 bool_unparse (ctype_isInt (ut2))));
776 if (ctype_isAbstract (ut1) && !ctype_isAbstract (ut2))
778 uentry ue1 = usymtab_getTypeEntry (ctype_typeId (ut1));
779 ctype ct = uentry_getType (ue1);
781 if (ctype_match (ct, ut2))
784 hint = message ("Underlying types match, but %s is an "
785 "abstract type that is not accessible here.",
789 else if (ctype_isAbstract (ut2) && !ctype_isAbstract (ut1))
791 uentry ue = usymtab_getTypeEntry (ctype_typeId (ut2));
792 ctype ct = uentry_getType (ue);
794 if (ctype_match (ct, ut1))
797 hint = message ("Underlying types match, but %s is an "
798 "abstract type that is not accessible here.",
804 ; /* Not an abstract mismatch. */
808 if (hcode == INVALID_FLAG)
810 if ((ctype_isEnum (ut1) && ctype_isInt (ut2))
811 || (ctype_isEnum (ut2) && ctype_isInt (ut1)))
815 else if ((ctype_isEnum (ut1) && ctype_isInt (ut2))
816 || (ctype_isEnum (ut2) && ctype_isInt (ut1)))
820 else if ((ctype_isSignedChar (ut1) && ctype_isUnsignedChar (ut2))
821 || (ctype_isUnsignedChar (ut1) && ctype_isSignedChar (ut2)))
823 hcode = FLG_CHARUNSIGNEDCHAR;
825 else if (ctype_isNumeric (ut1) && ctype_isNumeric (ut2))
827 hcode = FLG_RELAXTYPES;
828 DPRINTF (("Setting relax types!"));
832 DPRINTF (("No special type rule: %s / %s", ctype_unparse (ut1),
833 ctype_unparse (ut2)));
838 if (cstring_isDefined (hint))
840 if (!context_suppressFlagMsg (ocode, fl))
842 return llgenhinterror (code, s, hint, fl);
853 if (hcode != INVALID_FLAG && hcode != ocode)
858 if (llgenerroraux (ocode, srcFile, srcLine, s, flagcodeHint (code), fl, TRUE, FALSE))
862 if (context_flagOn (code, fl))
864 /* The flag is alreay set, something buggy in the flag code */
865 llcontbug (message ("No hint available, flag %s is already set.",
866 flagcode_unparse (code)));
875 llsuppresshint ('-', code);
878 flagcode_recordError (code);
887 xllgentypeerror (char *srcFile, int srcLine,
888 ctype t1, exprNode e1, ctype t2, exprNode e2,
889 /*@only@*/ cstring s, fileloc fl)
891 return llgentypeerroraux (srcFile, srcLine, FLG_TYPE, t1, e1, t2, e2, s, fl);
895 xllgenformattypeerror (char *srcFile, int srcLine,
896 ctype t1, exprNode e1, ctype t2, exprNode e2,
897 /*@only@*/ cstring s, fileloc fl)
899 if (!context_suppressFlagMsg (FLG_FORMATTYPE, fl))
901 return llgentypeerroraux (srcFile, srcLine, FLG_FORMATTYPE, t1, e1, t2, e2, s, fl);
911 xllgenerror (char *srcFile, int srcLine, flagcode o, /*@only@*/ cstring s, fileloc fl)
913 if (llgenerroraux (o, srcFile, srcLine, s, flagcodeHint (o), fl, TRUE, FALSE))
915 llnosuppresshint (o);
916 flagcode_recordError (o);
922 flagcode_recordSuppressed (o);
928 xllgenhinterror (char *srcFile, int srcLine,
929 flagcode o, /*@only@*/ cstring s, /*@only@*/ cstring hint,
932 if (!context_suppressFlagMsg (o, fl))
934 if (llgenerroraux (o, srcFile, srcLine, s, hint, fl, TRUE, FALSE))
936 flagcode_recordError (o);
938 if (context_getFlag (FLG_HINTS))
959 flagcode_recordSuppressed (o);
964 llrealerror (flagcode code, char *srcFile, int srcLine, /*@only@*/ cstring s, /*@temp@*/ cstring addtext, fileloc fl)
966 return (llgenerrorreal (code, srcFile, srcLine, s, addtext, fl, TRUE, FALSE));
970 llgenerroraux (flagcode code,
971 char *srcFile, int srcLine,
972 /*@only@*/ cstring s,
974 fileloc fl, bool iserror, bool indent)
976 if (context_inSuppressZone (fl))
982 if (llgenerrorreal (code, srcFile, srcLine, s, addtext, fl, iserror, indent)) {
990 xllforceerror (char *srcFile, int srcLine,
991 flagcode code, /*@only@*/ cstring s, fileloc fl)
993 flagcode_recordError (code);
995 if (llgenerrorreal (code, srcFile, srcLine, s, cstring_undefined, fl, TRUE, FALSE)) {
1003 static void generateCSV (flagcode code, cstring s, cstring addtext, fileloc fl)
1006 if (g_csvstream != NULL) {
1007 /* Warning, Flag Code, Flag Name, Priority, File, Line, Column, Warning Text, Additional Text */
1008 fprintf (g_csvstream, "%d,%d,%s,%d,%s,%d,%d,\"%s\"",
1009 context_numErrors (),
1010 (int) code, /* flag code */
1011 cstring_toCharsSafe (flagcode_unparse (code)), /* flag name */
1012 flagcode_priority (code), /* priority */
1013 cstring_toCharsSafe (fileloc_outputFilename (fl)),
1014 fileloc_lineno (fl),
1015 fileloc_column (fl),
1016 cstring_toCharsSafe (s));
1018 if (cstring_isDefined (addtext)) {
1019 fprintf (g_csvstream, ",\"%s\"\n", cstring_toCharsSafe (addtext));
1021 fprintf (g_csvstream, "\n");
1027 llgenerrorreal (flagcode code, char *srcFile, int srcLine,
1028 /*@only@*/ cstring s,
1030 fileloc fl, bool iserror, bool indent)
1034 /* duplicate message (rescanning a header file */
1036 if (!messageLog_add (context_messageLog (), fl, s))
1038 DPRINTF (("Duplicate message suppressed! %s / %s",
1039 fileloc_unparse (fl), s));
1045 ** If herald has not been displayed, display it before the first message.
1050 if (iserror) context_hasError ();
1052 if (context_unlimitedMessages ())
1059 ** suppress excessive messages:
1064 char *sc = cstring_toCharsSafe (s);
1065 char *tmpmsg = strchr (sc, ':');
1073 char *savechar = tmpmsg;
1079 if (cstring_equal (lastmsg, cstring_fromChars (tmpmsg)))
1082 if (mcount == (context_getLimit () + 1))
1084 limitmessage (s, fl);
1088 if (mcount > (context_getLimit ()))
1098 cstring_free (lastmsg);
1099 lastmsg = cstring_fromCharsNew (tmpmsg);
1103 DPRINTF (("Here..."));
1105 if (context_hasAliasAnnote ())
1107 char *sc = cstring_toCharsSafe (s);
1108 char *fcolon = strchr (sc, ':');
1109 cstring a = context_getAliasAnnote ();
1114 s = message ("%q (%q)", s, a);
1121 afterColon = cstring_fromCharsNew (fcolon + 1);
1123 s = message ("%q (%q):%q", s, a, afterColon);
1127 if (context_hasMessageAnnote ())
1129 char *fcolon = strchr (cstring_toCharsSafe (s), ':');
1134 /*@-dependenttrans@*/ /* s becomes dependent for fcolon */
1135 s = message ("%q (%q)", s, context_getMessageAnnote ());
1136 /*@=dependenttrans@*/
1143 afterColon = cstring_fromCharsNew (fcolon + 1);
1145 /*@-dependenttrans@*/ /* s becomes dependent for fcolon */
1146 s = message ("%q (%q):%q", s,
1147 context_getMessageAnnote (), afterColon);
1148 /*@=dependenttrans@*/
1152 context_setNeednl ();
1155 if (context_showFunction ())
1157 cstring fname = fileloc_unparseFilename (g_currentloc);
1159 if (context_inIterDef ())
1161 fprintf (g_warningstream, "%s: (in iter %s)\n",
1162 cstring_toCharsSafe (fname),
1163 cstring_toCharsSafe (context_inFunctionName ()));
1165 else if (context_inIterEnd ())
1167 fprintf (g_warningstream, "%s: (in iter finalizer %s)\n",
1168 cstring_toCharsSafe (fname),
1169 cstring_toCharsSafe (context_inFunctionName ()));
1171 else if (context_inMacro ())
1173 fprintf (g_warningstream, "%s: (in macro %s)\n", cstring_toCharsSafe (fname),
1174 cstring_toCharsSafe (context_inFunctionName ()));
1178 fprintf (g_warningstream, "%s: (in function %s)\n",
1179 cstring_toCharsSafe (fname),
1180 cstring_toCharsSafe (context_inFunctionName ()));
1183 cstring_free (fname);
1184 context_setShownFunction ();
1187 flstring = fileloc_unparse (fl);
1188 lastfileloclen = cstring_length (flstring);
1190 generateCSV (code, s, addtext, fl);
1194 printError (g_warningstream, message (" %q: %q", flstring, s));
1198 printError (g_warningstream, message ("%q: %q", flstring, s));
1201 showSourceLoc (srcFile, srcLine);
1209 ** message contains no '\n'
1210 ** message fits in one line: print it
1211 ** message fits in two lines with 3-space indent after fileloc: print it
1212 ** split line with 5-space indent from left margin: print it
1217 void printMessage (FILE *stream, /*@only@*/ cstring s)
1219 printIndentMessage (stream, s, 0);
1223 void printIndentMessage (FILE *stream, /*@only@*/ cstring sc, int indent)
1225 static bool inbody = FALSE;
1226 int maxlen = context_getLineLen ();
1227 char *s = cstring_toCharsSafe (sc);
1230 llassertprotect (!inbody);
1238 llassertprotect (st != olds);
1240 mstring_split (&st, &t, maxlen, &indent);
1241 fprintf (stream, "%s\n", st);
1242 llassertprotect (t != s);
1244 } while (s != NULL) ;
1251 void printError (FILE *stream, /*@only@*/ cstring sc)
1253 int maxlen = context_getLineLen ();
1254 size_t nspaces = lastfileloclen + 5;
1255 int nextlen = maxlen - nspaces;
1256 size_t len = cstring_length (sc);
1258 char *s = cstring_toCharsSafe (sc);
1262 DPRINTF (("Print error: [%s]", sc));
1264 if (size_toInt (len) < (maxlen + nextlen) && (strchr (s, '\n') == NULL))
1266 mstring_split (&s, &t, maxlen, &indent);
1268 fprintf (stream, "%s\n", s);
1272 len = mstring_length (t);
1274 if (size_toInt (len) < (maxlen - 3) && (strchr (t, '\n') == NULL)
1275 && size_toInt (len) > (nextlen - 1))
1277 fprintf (stream, " %s\n", t);
1281 char *spaces = (char *) dmalloc ((nspaces + 1) * sizeof (*spaces));
1284 for (i = 0; i < size_toInt (nspaces); i++)
1289 spaces[nspaces] = '\0';
1294 mstring_split (&st, &t, nextlen, &indent);
1295 fprintf (stream, "%s%s\n", spaces, st);
1304 DPRINTF (("Here 1: [%s]", sc));
1306 if (size_toInt (len) < (maxlen + maxlen - 1) && (strchr (s, '\n') != NULL))
1308 nspaces = ((maxlen + maxlen - 1) - len) / 2;
1310 if (nspaces < 1) nspaces = 1;
1312 nextlen = maxlen - nspaces;
1314 mstring_split (&s, &t, maxlen, &indent);
1316 fprintf (stream, "%s\n", s);
1320 char *spaces = (char *) dmalloc ((nspaces + 1) * sizeof (*spaces));
1323 for (i = 0; i < size_toInt (nspaces); i++)
1328 spaces[nspaces] = '\0';
1334 mstring_split (&st, &t, nextlen, &indent);
1335 fprintf (stream, "%s%s\n", spaces, st);
1344 nextlen = maxlen - nspaces;
1346 DPRINTF (("Here 2: [%s]", s));
1347 mstring_split (&s, &t, maxlen, &indent);
1348 DPRINTF (("Here 3: [%s] [%s]", s, t));
1350 fprintf (stream, "%s\n", s);
1354 char *spaces = (char *) dmalloc ((nspaces + 1) * sizeof (*spaces));
1357 for (i = 0; i < nspaces; i++)
1362 spaces[nspaces] = '\0';
1367 DPRINTF (("Loop: [%s]", t));
1368 mstring_split (&st, &t, nextlen, &indent);
1369 DPRINTF (("Split: [%s] [%s]", st, t));
1370 fprintf (stream, "%s%s\n", spaces, st);
1371 DPRINTF (("Next..."));
1384 xllfatalbug (char *srcFile, int srcLine, /*@only@*/ cstring s)
1387 printError (g_errorstream, message ("%q: *** Fatal bug: %q",
1388 fileloc_unparse (g_currentloc), s));
1389 showSourceLoc (srcFile, srcLine);
1396 lclfatalbug (char *msg)
1399 printError (g_errorstream,
1400 message ("*** Fatal Bug: %s", cstring_fromChars (msg)));
1407 checkParseError (void)
1409 if (fileloc_withinLines (lastparseerror, g_currentloc, 10))
1411 llfatalerror (message ("%q: Cannot recover from parse error.",
1412 fileloc_unparse (g_currentloc)));
1416 void llbugaux (cstring file, int line, /*@only@*/ cstring s)
1419 static int numbugs = 0;
1420 static bool inbug = FALSE;
1424 cstring temps = fileloc_unparseRaw (file, line);
1426 fprintf (g_errorstream,
1427 "%s: Recursive bug detected: %s\n",
1428 cstring_toCharsSafe (temps),
1429 cstring_toCharsSafe (s));
1430 cstring_free (temps);
1439 if (fileloc_withinLines (lastparseerror, g_currentloc, 7))
1441 llfatalerror (message ("%q: Cannot recover from parse error.",
1442 fileloc_unparse (g_currentloc)));
1445 (void) fflush (g_warningstream);
1447 printError (g_errorstream,
1448 message ("%q: *** Internal Bug at %q: %q [errno: %d]",
1449 fileloc_unparse (g_currentloc),
1450 fileloc_unparseRaw (file, line),
1453 /* printCodePoint (); no longer useful */
1458 ** This is confusing, and hardly ever useful.
1462 perror ("Possible system error diagnostic: ");
1473 if (numbugs > context_getBugsLimit () && fileloc_withinLines (lastbug, g_currentloc, 2))
1476 (message ("%q: Cannot recover from last bug. "
1477 "(If you really want Splint to try to continue, use -bugslimit <n>.)",
1478 fileloc_unparse (g_currentloc)));
1481 fprintf (g_errorstream, " (attempting to continue, results may be incorrect)\n");
1482 fileloc_free (lastbug);
1483 lastbug = fileloc_copy (g_currentloc);
1489 lclbug (/*@only@*/ cstring s)
1492 printError (g_errorstream, message ("*** Internal Bug: %q", s));
1495 fprintf (g_errorstream, " (attempting to continue, results may be incorrect)\n");
1500 xllfatalerror (char *srcFile, int srcLine, cstring s)
1503 printError (g_errorstream, s);
1504 printError (g_errorstream, cstring_makeLiteral ("*** Cannot continue."));
1505 showSourceLoc (srcFile, srcLine);
1510 xllfatalerrorLoc (char *srcFile, int srcLine, /*@only@*/ cstring s)
1513 (void) fflush (g_warningstream);
1514 printError (g_errorstream, message ("%q: %q", fileloc_unparse (g_currentloc), s));
1515 printError (g_errorstream, cstring_makeLiteral ("*** Cannot continue."));
1516 showSourceLoc (srcFile, srcLine);
1517 (void) fflush (g_warningstream);
1524 return (lclerrors > 0);
1528 lclHadNewError (void)
1530 static int lastcall = 0;
1532 if (lclerrors > lastcall)
1534 lastcall = lclerrors;
1544 lclNumberErrors (void)
1550 xlclerror (char *srcFile, int srcLine, ltoken t, /*@only@*/ cstring msg)
1554 if (ltoken_getCode (t) != NOTTOKEN)
1556 cstring loc = ltoken_unparseLoc (t);
1557 lastfileloclen = cstring_length (loc);
1559 printError (g_warningstream, message ("%q: %q", loc, msg));
1560 showSourceLoc (srcFile, srcLine);
1564 printError (g_warningstream, msg);
1565 showSourceLoc (srcFile, srcLine);
1570 lclplainerror (/*@only@*/ cstring msg)
1573 printError (g_warningstream, msg);
1577 lclfatalerror (ltoken t, /*@only@*/ cstring msg)
1579 if (ltoken_getCode (t) != NOTTOKEN)
1581 cstring loc = ltoken_unparseLoc (t);
1582 lastfileloclen = cstring_length (loc);
1583 printError (g_errorstream, message ("%q: %q", loc, msg));
1587 printError (g_errorstream, msg);
1590 printError (g_errorstream, cstring_makeLiteral ("*** Cannot continue"));
1595 lclplainfatalerror (/*@only@*/ cstring msg)
1597 (void) fflush (g_warningstream);
1598 printError (g_errorstream, message ("*** Cannot continue: %q", msg));
1603 lclRedeclarationError (ltoken id)
1605 cstring s = ltoken_getRawString (id);
1608 if (usymtab_existsEither (s))
1610 uentry le = usymtab_lookupEither (s);
1612 lclerror (id, message ("Respecification of %s", s));
1613 llgenindentmsg (message ("Previous specification of %q",
1614 uentry_getName (le)),
1615 uentry_whereSpecified (le));
1619 lclerror (id, message ("Identifier redeclared: %s", s));
1623 void genppllerror (flagcode code, /*@only@*/ cstring s)
1625 if (context_inSuppressZone (g_currentloc))
1631 if (context_getFlag (code))
1633 if (!context_isInCommandLine ())
1635 displayScanClose ();
1640 if (code != FLG_PREPROC)
1642 llsuppresshint ('-', code);
1645 if (!context_isInCommandLine ())
1647 displayScanOpen (cstring_makeLiteral ("< more preprocessing ."));
1657 void genppllerrorhint (flagcode code, /*@only@*/ cstring s,
1658 /*@only@*/ cstring hint)
1660 if (context_inSuppressZone (g_currentloc))
1663 cstring_free (hint);
1667 if (context_getFlag (code))
1669 generateCSV (code, s, hint, g_currentloc);
1671 context_clearPreprocessing ();
1674 context_setPreprocessing ();
1680 cstring_free (hint);
1685 void ppllerror (/*@only@*/ cstring s)
1687 genppllerror (FLG_PREPROC, s);
1690 void pplldiagmsg (cstring s)
1692 if (!context_isInCommandLine ())
1694 displayScanClose ();
1696 displayScanOpen (cstring_makeLiteral ("< more preprocessing ."));
1704 void loadllmsg (cstring s)
1706 displayScanClose ();
1708 displayScanOpen (cstring_makeLiteral ("< ."));
1711 static void llreportparseerror (/*@only@*/ cstring s)
1713 if (fileloc_withinLines (lastparseerror, g_currentloc, 5))
1719 llerror (FLG_SYNTAX, s);
1721 fileloc_free (lastparseerror);
1722 lastparseerror = fileloc_copy (g_currentloc);
1726 bool xcppoptgenerror (char *srcFile, int srcLine,
1728 /*@only@*/ cstring s,
1732 fileloc loc = cppReader_getLoc (pfile);
1734 if (context_flagOn (o, loc))
1736 if (xlloptgenerror (srcFile, srcLine, o, s, loc))
1738 cppReader_printContainingFiles (pfile);
1752 bool xlloptgenerror (char *srcFile, int srcLine,
1753 flagcode o, /*@only@*/ cstring s, fileloc loc)
1755 DPRINTF (("xllopt: %s", s));
1757 if (llrealerror (o, srcFile, srcLine, s, flagcodeHint (o), loc))
1759 DPRINTF (("Here we are!"));
1760 llsuppresshint ('-', o);
1762 flagcode_recordError (o);
1767 DPRINTF (("Suppressed!"));
1768 flagcode_recordSuppressed (o);
1773 bool xoptgenerror2 (char *srcFile, int srcLine,
1774 flagcode f1, flagcode f2, /*@only@*/ cstring s, fileloc loc)
1776 if (context_suppressFlagMsg (f1, loc))
1778 flagcode_recordSuppressed (f1);
1783 if (context_suppressFlagMsg (f2, loc))
1785 flagcode_recordSuppressed (f2);
1790 if (llrealerror (f1, srcFile, srcLine, s, flagcodeHint (f1), loc))
1792 llsuppresshint2 ('-', f1, f2);
1793 flagcode_recordError (f2);
1799 flagcode_recordSuppressed (f2);
1807 bool xoptgenerror2n (char *srcFile, int srcLine,
1808 flagcode f1, flagcode f2, /*@only@*/ cstring s, fileloc loc)
1811 if (context_suppressFlagMsg (f1, loc))
1813 flagcode_recordSuppressed (f1);
1818 if (context_suppressNotFlagMsg (f2, loc))
1820 flagcode_recordSuppressed (f2);
1825 if (llrealerror (f1, srcFile, srcLine, s, flagcodeHint (f2), loc))
1827 llsuppresshint ('+', f2);
1828 flagcode_recordError (f2);
1833 flagcode_recordSuppressed (f2);
1839 bool xllnoptgenerror (char *srcFile, int srcLine,
1840 flagcode o, /*@only@*/ cstring s, fileloc loc)
1842 if (llrealerror (o, srcFile, srcLine, s, flagcodeHint (o), loc))
1844 llsuppresshint ('+', o);
1845 flagcode_recordError (o);
1850 flagcode_recordSuppressed (o);
1854 void xllparseerror (char *srcFile, int srcLine, cstring s)
1856 if (context_getFlag (FLG_TRYTORECOVER))
1860 if (parseerrorcount > GIVEUPPARSE)
1862 if (cstring_isDefined (s))
1864 xllfatalerror (srcFile, srcLine,
1865 message ("%q: Parse Error: %q. "
1866 "Too many errors, giving up.",
1867 fileloc_unparse (g_currentloc), s));
1871 xllfatalerror (srcFile, srcLine,
1872 message ("%q: Parse Error. Too many errors, giving up.",
1873 fileloc_unparse (g_currentloc)));
1878 if (cstring_isDefined (s))
1880 llreportparseerror (message ("Parse Error: %q. Attempting to continue.",
1882 showSourceLoc (srcFile, srcLine);
1886 llreportparseerror (message ("Parse Error. Attempting to continue."));
1887 showSourceLoc (srcFile, srcLine);
1895 if (cstring_isDefined (s))
1897 msg = message ("Parse Error: %q.", s);
1901 msg = message ("Parse Error.");
1906 message ("%q: %s (For help on parse errors, "
1907 "see splint -help parseerrors.)",
1908 fileloc_unparse (g_currentloc), msg));
1912 bool xfsgenerror (char *srcFile, int srcLine,
1913 flagSpec fs, /*@only@*/ cstring s, fileloc fl)
1915 if (flagSpec_isOn (fs, fl))
1917 flagcode firston = flagSpec_getFirstOn (fs, fl);
1919 if (llgenerroraux (firston, srcFile, srcLine, s,
1920 flagcodeHint (firston),
1923 llsuppresshint ('-', firston);
1924 flagcode_recordError (firston);
1929 flagcode_recordSuppressed (firston);
1935 flagcode_recordSuppressed (flagSpec_getDominant (fs));
1941 bool doCheck (bool x, cstring pred, cstring file, int line)
1944 llbug (message ("%q: Check Failed: %s",
1945 fileloc_unparseRaw (file, line),
1952 /*@observer@*/ cstring lldecodeerror (/*@unused@*/ int errnum)
1957 #ifndef HAVE_STRERROR
1960 result = strerror (errnum);
1963 /* VAXCRTL's strerror() takes an optional second argument, which only
1964 matters when the first argument is EVMSERR. However, it's simplest
1965 just to pass it unconditionally. `vaxc$errno' is declared in
1966 <errno.h>, and maintained by the library in parallel with `errno'.
1967 We assume that caller's `errnum' either matches the last setting of
1968 `errno' by the library or else does not have the value `EVMSERR'. */
1970 result = strerror (errnum, vaxc$errno);
1975 result = cstring_toCharsSafe (message ("undocumented I/O error: %d", errnum));
1978 return cstring_fromChars (result);
1981 void llquietbugaux (cstring s, /*@unused@*/ cstring file, /*@unused@*/ int line)
1986 printError (g_errorstream, message ("%q: *** Internal Bug at %q: %q [errno: %d]",
1987 fileloc_unparse (g_currentloc),
1988 fileloc_unparseRaw (file, line),
2000 (void) fflush (g_warningstream);
2001 (void) fflush (g_messagestream);
2004 static bool s_scanOpen = FALSE;
2006 void displayScan (cstring msg)
2010 displayScanClose ();
2013 llassert (!s_scanOpen);
2015 if (context_getFlag (FLG_SHOWSCAN))
2018 fprintf (g_messagestream, "< %s >\n", cstring_toCharsSafe (msg));
2019 (void) fflush (g_messagestream);
2025 void displayScanOpen (cstring msg)
2029 displayScanClose ();
2032 llassert (!s_scanOpen);
2035 if (context_getFlag (FLG_SHOWSCAN))
2037 fprintf (g_messagestream, "< %s", cstring_toCharsSafe (msg));
2038 (void) fflush (g_messagestream);
2044 void displayScanContinue (/*@temp@*/ cstring msg)
2046 if (context_getFlag (FLG_SHOWSCAN))
2050 fprintf (g_messagestream, "%s", cstring_toCharsSafe (msg));
2051 (void) fflush (g_messagestream);
2056 ** Don't call bug recursively
2059 fprintf (stderr, "*** Bug: scan continue scan not open\n");
2064 void displayScanClose (void)
2068 if (context_getFlag (FLG_SHOWSCAN))
2070 fprintf (g_messagestream, " >\n");
2071 (void) fflush (g_messagestream);
2077 ** Don't call bug recursively
2080 fprintf (stderr, "*** Bug: scan close scan not open\n");