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
27 ** error reporting procedures
30 # include "lclintMacros.nf"
35 # include "cpperror.h"
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@*/ ;
46 static int lclerrors = 0;
49 static int lastfileloclen = 10;
50 static /*@only@*/ cstring lastmsg = cstring_undefined;
51 static int mcount = 0;
52 static /*@only@*/ cstring saveOneMessage = cstring_undefined;
53 static /*@only@*/ fileloc lastparseerror = fileloc_undefined;
54 static /*@only@*/ fileloc lastbug = fileloc_undefined;
55 static bool llgenerrorreal (char *p_srcFile, int p_srcLine,
56 /*@only@*/ cstring p_s, fileloc p_fl, bool p_iserror, bool p_indent)
57 /*@modifies g_msgstream@*/ ;
58 static bool llgenerroraux (char *p_srcFile, int p_srcLine,
59 /*@only@*/ cstring p_s, fileloc p_fl, bool p_iserror, bool p_indent)
60 /*@modifies g_msgstream@*/ ;
62 static void printError (FILE *p_stream, /*@only@*/ cstring p_sc)
63 /*@globals lastfileloclen @*/
64 /*@modifies *p_stream@*/ ;
65 static void printMessage (FILE *p_stream, /*@only@*/ cstring p_s)
66 /*@modifies *p_stream@*/ ;
68 static void llgenhint (/*@only@*/ cstring p_s) /*@modifies g_msgstream@*/ ;
70 static void showSourceLoc (char *srcFile, int srcLine)
71 /*@modifies g_msgstream@*/
73 if (context_getFlag (FLG_SHOWSOURCELOC)) {
74 llgenhint (message ("%s:%d: Source code error generation point.",
75 cstring_fromChars (srcFile), srcLine));
80 static /*@null@*/ char *
81 maxcp (/*@null@*/ /*@returned@*/ char *a, /*@null@*/ /*@returned@*/ char *b)
90 fprintf (stderr, " *** Please report bug to %s ***\n",
93 /* don't exit (EXIT_FAILURE); */
96 static bool s_needsPrepare = TRUE;
98 void prepareMessage (void)
100 DPRINTF (("Prepare message: %s", bool_unparse (context_loadingLibrary ())));
102 if ((context_isPreprocessing () || context_loadingLibrary ())
104 && context_getDebug (FLG_SHOWSCAN))
107 fprintf (stderr, " >\n");
108 s_needsPrepare = FALSE;
114 void closeMessage (void)
116 if (context_isPreprocessing ()
117 && context_getDebug (FLG_SHOWSCAN))
120 fprintf (stderr, "< more preprocessing .");
122 llassertprotect (!s_needsPrepare);
123 s_needsPrepare = TRUE;
132 llmsg (/*@only@*/ cstring s)
134 context_setNeednl ();
136 printMessage (g_msgstream, s);
141 lldiagmsg (/*@only@*/ cstring s)
143 context_setNeednl ();
145 printMessage (stderr, s);
150 llmsgplain (/*@only@*/ cstring s)
152 context_setNeednl ();
154 printMessage (g_msgstream, s);
158 void llerror_flagWarning (cstring s)
160 if (context_getFlag (FLG_WARNFLAGS))
164 if (fileloc_isBuiltin (g_currentloc))
166 llmsg (message ("Warning: %q", s));
170 llgenmsg (message ("Warning: %q", s), g_currentloc);
180 llgenhint (/*@only@*/ cstring s) /*@modifies g_msgstream@*/
182 int indent = context_getIndentSpaces () - 1;
184 if (indent < 0) indent = 0;
186 context_setNeednl ();
187 printIndentMessage (g_msgstream, s, indent);
193 if (context_getFlag (FLG_HINTS) &&
194 !(context_inSuppressRegion () || context_inSuppressZone (g_currentloc)))
205 llshowhint (flagcode f)
207 if (context_getFlag (FLG_HINTS))
209 if ((flagcode_numReported (f) == 0) || context_getFlag (FLG_FORCEHINTS))
211 cstring desc = flagcodeHint (f);
213 if (cstring_isDefined (desc))
215 llgenhint (cstring_copy (desc));
222 llsuppresshint2 (char c, flagcode f1, flagcode f2)
225 if (context_getFlag (FLG_HINTS))
227 if ((flagcode_numReported (f1) == 0
228 || flagcode_numReported (f2) == 0)
229 || context_getFlag (FLG_FORCEHINTS))
231 cstring desc = flagcodeHint (f1);
232 context_setNeednl ();
235 if (cstring_isUndefined (desc))
237 desc = flagcodeHint (f2);
240 if (flagcode_isNamePrefixFlag (f1))
245 if (flagcode_isNamePrefixFlag (f2))
252 if (cstring_isDefined (desc))
254 llgenhint (message ("%s (Setting %h%s will suppress message)", desc,
256 flagcode_unparse (f1)));
260 llgenhint (message ("(Setting %h%s will suppress message)",
261 c, flagcode_unparse (f1)));
266 if (cstring_isDefined (desc))
268 llgenhint (message ("%s (Setting either %h%s or %h%s will suppress message)", desc,
270 flagcode_unparse (f1),
272 flagcode_unparse (f2)));
276 llgenhint (message ("(Setting either %h%s or %h%s will suppress message)", c,
277 flagcode_unparse (f1),
278 c, flagcode_unparse (f2)));
286 llsuppresshint (char c, flagcode f)
288 if (context_getFlag (FLG_HINTS))
290 if ((flagcode_numReported (f) == 0) || context_getFlag (FLG_FORCEHINTS))
292 cstring desc = flagcodeHint (f);
293 context_setNeednl ();
296 if (flagcode_isNamePrefixFlag (f))
301 if (cstring_isDefined (desc))
303 llgenhint (message ("%s (%h%s will suppress message)", desc, c,
304 flagcode_unparse (f)));
308 llgenhint (message ("(%h%s will suppress message)", c,
309 flagcode_unparse (f)));
316 llnosuppresshint (flagcode f)
318 if (context_getFlag (FLG_FORCEHINTS))
320 cstring desc = flagcodeHint (f);
321 context_setNeednl ();
324 if (cstring_isDefined (desc))
326 printError (g_msgstream, message (" %s", desc));
331 /*@constant int MAXSEARCH; @*/
332 # define MAXSEARCH 20
334 /*@constant int MINLINE; @*/
337 typedef /*@null@*/ /*@dependent@*/ char *nd_charp;
340 mstring_split (/*@returned@*/ char **sp,
341 /*@out@*/ nd_charp *tp,
342 int maxline, /*@in@*/ int *indentchars)
351 DPRINTF (("Split: %s / %d", *sp, maxline));
353 if (maxline < MINLINELEN)
355 maxline = MINLINELEN;
358 if (*indentchars > 0)
360 s = *sp = mstring_concatFree1 (mstring_spaces (*indentchars), s);
364 nl = strchr (s, '\n');
369 ** if there is a newline in first maxline characters, split there
370 ** if line len is <= maxline, return no split
371 ** if there is a ':' or ';' followed by ' ' in first maxline characters,
372 ** split there unless the ' ' is followed by a '}', then
374 ** of the ';' is inside quotation marks
375 ** if there is a space or tab in last maxsearch characters, split there
376 ** else, split at maxline
378 ** special code: slash [1-9] after a newline means indent the rest <n> chars
382 if ((nl != NULL) && ((nl - s) < maxline))
389 llassertprotect (*tp == NULL || (*tp > osp));
393 if (*t >= '\1' && *t <= '\7')
395 *indentchars += (int) (*t - '\1') + 1;
402 else if (size_toInt (strlen (s)) < maxline)
404 llassertprotect (*tp == NULL || (*tp > osp));
411 char *lcolon, *lsemi, *splitat;
419 lcolon = strrchr (s, ':');
420 lsemi = strrchr (s, ';');
423 splitat = maxcp (lcolon, lsemi);
425 if (splitat != NULL && ((int)(splitat - s) > MINLINE)
426 && *(splitat) != '\0'
427 && *(splitat + 1) == ' '
428 && (*(splitat + 2) != '}' && (*(splitat + 2) != '\0')))
430 *(splitat + 1) = '\0';
433 llassertprotect (*tp == NULL || (*tp > osp));
437 while (*t != ' ' && *t != '\t' && i < MAXSEARCH)
443 if (*t != ' ' && *t != '\t')
445 llassertprotect (maxline > 0);
446 t = mstring_copy (s + maxline);
447 *(s + maxline) = '\0';
452 llassertprotect (*tp == NULL || (*tp > osp));
456 mstring_markFree (t);
458 /* Won't be true since t is a copy: llassertprotect (*tp == NULL || (*tp > osp)); */
467 if (*t == '\0') return;
470 ** returns unqualified as only
476 /* Hack to prevent error case for wierd strings. */
482 llassertprotect (*tp == NULL || (*tp > osp));
493 void limitmessage (/*@only@*/ cstring s, fileloc loc)
495 if (mcount > context_getLimit () + 1)
501 cstring flstring = fileloc_unparse (loc);
503 lastfileloclen = cstring_length (flstring);
504 cstring_free (saveOneMessage);
505 saveOneMessage = message ("%q: %q", flstring, s);
509 static int parseerrorcount = 0;
511 void cleanupMessages ()
515 if (context_unlimitedMessages ())
521 int unprinted = mcount - context_getLimit ();
525 if (unprinted == 1 && cstring_isDefined (saveOneMessage))
528 printError (g_msgstream, saveOneMessage);
530 saveOneMessage = cstring_undefined;
534 if (cstring_isDefined (saveOneMessage))
536 /* cstring_free (saveOneMessage); */
537 saveOneMessage = cstring_undefined;
540 fprintf (g_msgstream, "%s: (%d more similar errors unprinted)\n",
541 cstring_toCharsSafe (fileloc_filename (g_currentloc)),
542 mcount - context_getLimit ());
551 llgenmsg (/*@only@*/ cstring s, fileloc fl)
553 cstring flstring = fileloc_unparse (fl);
554 lastfileloclen = cstring_length (flstring);
557 (void) printError (g_msgstream, message ("%q: %q", flstring, s));
562 llgenindentmsg (/*@only@*/ cstring s, fileloc fl)
564 cstring flstring = fileloc_unparse (fl);
567 (void) printIndentMessage (g_msgstream, message ("%q: %q", flstring, s), context_getIndentSpaces ());
572 llgenindentmsgnoloc (/*@only@*/ cstring s)
575 (void) printIndentMessage (g_msgstream, s, context_getIndentSpaces ());
580 llgentypeerroraux (char *srcFile, int srcLine,
581 flagcode ocode, ctype t1, exprNode e1, ctype t2, exprNode e2,
582 /*@only@*/ cstring s, fileloc fl)
584 cstring hint = cstring_undefined;
585 flagcode code = ocode;
586 flagcode hcode = INVALID_FLAG;
590 DPRINTF (("Type error [%s]: %s / %s : %s / %s",
591 flagcode_unparse (ocode),
592 exprNode_unparse (e1), exprNode_unparse (e2),
593 ctype_unparse (t1), ctype_unparse (t2)));
595 DPRINTF (("Bool: %s / %s",
596 bool_unparse (ctype_isBool (t1)),
597 bool_unparse (ctype_isBool (t2))));
600 ** Set the flag using the underlying types that didn't match.
603 while (ctype_isPointer (ut1) && ctype_isPointer (ut2)) {
604 ut1 = ctype_baseArrayPtr (ut1);
605 ut2 = ctype_baseArrayPtr (ut2);
608 if ((ctype_isFloat (ut1) && ctype_isDouble (ut2))
609 || (ctype_isFloat (ut1) && ctype_isDouble (ut2)))
611 hcode = FLG_FLOATDOUBLE;
613 else if ((exprNode_isCharLit (e1) && ctype_isInt (ut2))
614 || (exprNode_isCharLit (e2) && ctype_isInt (ut1)))
616 hcode = FLG_CHARINTLITERAL;
618 else if ((exprNode_isNumLit (e1) && ctype_isReal (ut2))
619 || (exprNode_isNumLit (e2) && ctype_isReal (ut1)))
621 hcode = FLG_NUMLITERAL;
623 else if ((ctype_isManifestBool (ut1) && ctype_isInt (ut2))
624 || (ctype_isInt (ut1) && ctype_isManifestBool (ut2)))
625 /* evs 2000-07-24: was ctype_isDirectBool */
629 else if (((ctype_isChar (ut1) && !ctype_isInt (ut1)) && ctype_isInt (ut2))
630 || ((ctype_isInt (ut1) && (ctype_isChar (ut2) && !ctype_isInt (ut2)))))
634 else if ((ctype_isInt (ut1) && ctype_isInt (ut2))
635 || (ctype_isChar (ut1) && ctype_isChar (ut2))
636 || (ctype_isDouble (ut1) && ctype_isDouble (ut2)))
638 if (!bool_equal (ctype_isSigned (ut1), ctype_isSigned (ut2)))
640 hcode = FLG_IGNORESIGNS;
644 hcode = FLG_IGNOREQUALS;
647 else if (ctype_isArbitraryIntegral (ctype_realType (ut1)))
649 if (ctype_isArbitraryIntegral (ctype_realType (ut2)))
651 hcode = FLG_MATCHANYINTEGRAL;
653 else if (ctype_match (ut2, ctype_ulint))
655 hcode = FLG_LONGUNSIGNEDINTEGRAL;
657 else if (ctype_match (ut2, ctype_lint))
659 hcode = FLG_LONGINTEGRAL;
661 else if (ctype_isInt (ut2))
663 hcode = FLG_MATCHANYINTEGRAL;
670 else if (ctype_isArbitraryIntegral (ctype_realType (ut2)))
672 ctype tr = ctype_realType (ut1);
674 if (ctype_isArbitraryIntegral (tr))
676 hcode = FLG_MATCHANYINTEGRAL;
678 else if (ctype_match (ut1, ctype_ulint))
680 if (ctype_isUnsignedIntegral (tr))
682 hcode = FLG_LONGUNSIGNEDUNSIGNEDINTEGRAL;
684 else if (ctype_isSignedIntegral (tr))
690 hcode = FLG_LONGUNSIGNEDINTEGRAL;
693 else if (ctype_match (ut1, ctype_lint))
695 if (ctype_isSignedIntegral (tr))
697 hcode = FLG_LONGSIGNEDINTEGRAL;
699 else if (ctype_isSignedIntegral (tr))
705 hcode = FLG_LONGINTEGRAL;
708 else if (ctype_isInt (ut1))
710 hcode = FLG_MATCHANYINTEGRAL;
722 if (hcode == INVALID_FLAG)
724 DPRINTF (("[%s] %s - %s / %s",
726 bool_unparse (ctype_isEnum (ut1)),
727 bool_unparse (ctype_isEnum (ctype_realType (ut1))),
728 bool_unparse (ctype_isInt (ut2))));
730 if (ctype_isAbstract (ut1) && !ctype_isAbstract (ut2))
732 uentry ue1 = usymtab_getTypeEntry (ctype_typeId (ut1));
733 ctype ct = uentry_getType (ue1);
735 if (ctype_match (ct, ut2))
738 hint = message ("Underlying types match, but %s is an "
739 "abstract type that is not accessible here.",
743 else if (ctype_isAbstract (ut2) && !ctype_isAbstract (ut1))
745 uentry ue = usymtab_getTypeEntry (ctype_typeId (ut2));
746 ctype ct = uentry_getType (ue);
748 if (ctype_match (ct, ut1))
751 hint = message ("Underlying types match, but %s is an "
752 "abstract type that is not accessible here.",
758 ; /* Not an abstract mismatch. */
762 if (hcode == INVALID_FLAG)
764 if ((ctype_isEnum (ut1) && ctype_isInt (ut2))
765 || (ctype_isEnum (ut2) && ctype_isInt (ut1)))
769 else if ((ctype_isEnum (ut1) && ctype_isInt (ut2))
770 || (ctype_isEnum (ut2) && ctype_isInt (ut1)))
774 else if ((ctype_isSignedChar (ut1) && ctype_isUnsignedChar (ut2))
775 || (ctype_isUnsignedChar (ut1) && ctype_isSignedChar (ut2)))
777 hcode = FLG_CHARUNSIGNEDCHAR;
779 else if (ctype_isNumeric (ut1) && ctype_isNumeric (ut2))
781 hcode = FLG_RELAXTYPES;
782 DPRINTF (("Setting relax types!"));
786 DPRINTF (("No special type rule: %s / %s", ctype_unparse (ut1),
787 ctype_unparse (ut2)));
792 if (cstring_isDefined (hint))
794 if (!context_suppressFlagMsg (ocode, fl))
796 return llgenhinterror (code, s, hint, fl);
807 if (llgenerroraux (srcFile, srcLine, s, fl, TRUE, FALSE))
809 if (hcode != INVALID_FLAG && hcode != ocode)
817 llsuppresshint ('-', code);
820 flagcode_recordError (code);
829 xllgentypeerror (char *srcFile, int srcLine,
830 ctype t1, exprNode e1, ctype t2, exprNode e2,
831 /*@only@*/ cstring s, fileloc fl)
833 return llgentypeerroraux (srcFile, srcLine, FLG_TYPE, t1, e1, t2, e2, s, fl);
837 xllgenformattypeerror (char *srcFile, int srcLine,
838 ctype t1, exprNode e1, ctype t2, exprNode e2,
839 /*@only@*/ cstring s, fileloc fl)
841 return llgentypeerroraux (srcFile, srcLine, FLG_FORMATTYPE, t1, e1, t2, e2, s, fl);
845 xllgenerror (char *srcFile, int srcLine, flagcode o, /*@only@*/ cstring s, fileloc fl)
847 if (llgenerroraux (srcFile, srcLine, s, fl, TRUE, FALSE))
849 llnosuppresshint (o);
850 flagcode_recordError (o);
856 flagcode_recordSuppressed (o);
862 xllgenhinterror (char *srcFile, int srcLine,
863 flagcode o, /*@only@*/ cstring s, /*@only@*/ cstring hint,
866 if (!context_suppressFlagMsg (o, fl))
868 if (llgenerroraux (srcFile, srcLine, s, fl, TRUE, FALSE))
870 flagcode_recordError (o);
872 if (context_getFlag (FLG_HINTS))
893 flagcode_recordSuppressed (o);
898 llrealerror (char *srcFile, int srcLine, /*@only@*/ cstring s, fileloc fl)
900 return (llgenerrorreal (srcFile, srcLine, s, fl, TRUE, FALSE));
904 llgenerroraux (char *srcFile, int srcLine,
905 /*@only@*/ cstring s, fileloc fl, bool iserror, bool indent)
907 if (context_inSuppressZone (fl))
917 if (llgenerrorreal (srcFile, srcLine, s, fl, iserror, indent)) {
925 xllforceerror (char *srcFile, int srcLine,
926 flagcode code, /*@only@*/ cstring s, fileloc fl)
928 flagcode_recordError (code);
930 if (llgenerrorreal (srcFile, srcLine, s, fl, TRUE, FALSE)) {
939 llgenerrorreal (char *srcFile, int srcLine,
940 /*@only@*/ cstring s, fileloc fl, bool iserror, bool indent)
944 /* duplicate message (rescanning a header file */
946 if (!messageLog_add (context_messageLog (), fl, s))
948 DPRINTF (("Duplicate message suppressed! %s / %s",
949 fileloc_unparse (fl), s));
954 if (iserror) context_hasError ();
956 if (context_unlimitedMessages ())
963 ** suppress excessive messages:
968 char *sc = cstring_toCharsSafe (s);
969 char *tmpmsg = strchr (sc, ':');
977 char *savechar = tmpmsg;
983 if (cstring_equal (lastmsg, cstring_fromChars (tmpmsg)))
986 if (mcount == (context_getLimit () + 1))
988 limitmessage (s, fl);
992 if (mcount > (context_getLimit ()))
1002 cstring_free (lastmsg);
1003 lastmsg = cstring_fromCharsNew (tmpmsg);
1007 DPRINTF (("Here..."));
1009 if (context_hasAliasAnnote ())
1011 char *sc = cstring_toCharsSafe (s);
1012 char *fcolon = strchr (sc, ':');
1013 cstring a = context_getAliasAnnote ();
1018 s = message ("%q (%q)", s, a);
1025 afterColon = cstring_fromCharsNew (fcolon + 1);
1027 s = message ("%q (%q):%q", s, a, afterColon);
1031 if (context_hasMessageAnnote ())
1033 char *fcolon = strchr (cstring_toCharsSafe (s), ':');
1038 /*@-dependenttrans@*/ /* s becomes dependent for fcolon */
1039 s = message ("%q (%q)", s, context_getMessageAnnote ());
1040 /*@=dependenttrans@*/
1047 afterColon = cstring_fromCharsNew (fcolon + 1);
1049 /*@-dependenttrans@*/ /* s becomes dependent for fcolon */
1050 s = message ("%q (%q):%q", s,
1051 context_getMessageAnnote (), afterColon);
1052 /*@=dependenttrans@*/
1056 context_setNeednl ();
1059 if (context_showFunction ())
1061 cstring fname = fileloc_unparseFilename (g_currentloc);
1063 if (context_inIterDef ())
1065 fprintf (g_msgstream, "%s: (in iter %s)\n",
1066 cstring_toCharsSafe (fname),
1067 cstring_toCharsSafe (context_inFunctionName ()));
1069 else if (context_inIterEnd ())
1071 fprintf (g_msgstream, "%s: (in iter finalizer %s)\n",
1072 cstring_toCharsSafe (fname),
1073 cstring_toCharsSafe (context_inFunctionName ()));
1075 else if (context_inMacro ())
1077 fprintf (g_msgstream, "%s: (in macro %s)\n", cstring_toCharsSafe (fname),
1078 cstring_toCharsSafe (context_inFunctionName ()));
1082 fprintf (g_msgstream, "%s: (in function %s)\n",
1083 cstring_toCharsSafe (fname),
1084 cstring_toCharsSafe (context_inFunctionName ()));
1087 cstring_free (fname);
1088 context_setShownFunction ();
1091 flstring = fileloc_unparse (fl);
1092 lastfileloclen = cstring_length (flstring);
1096 printError (g_msgstream, message (" %q: %q", flstring, s));
1100 printError (g_msgstream, message ("%q: %q", flstring, s));
1103 showSourceLoc (srcFile, srcLine);
1110 ** message contains no '\n'
1111 ** message fits in one line: print it
1112 ** message fits in two lines with 3-space indent after fileloc: print it
1113 ** split line with 5-space indent from left margin: print it
1118 void printMessage (FILE *stream, /*@only@*/ cstring s)
1120 printIndentMessage (stream, s, 0);
1124 void printIndentMessage (FILE *stream, /*@only@*/ cstring sc, int indent)
1126 static bool inbody = FALSE;
1127 int maxlen = context_getLineLen ();
1128 char *s = cstring_toCharsSafe (sc);
1131 llassertprotect (!inbody);
1139 llassertprotect (st != olds);
1141 mstring_split (&st, &t, maxlen, &indent);
1142 fprintf (stream, "%s\n", st);
1143 llassertprotect (t != s);
1145 } while (s != NULL) ;
1152 void printError (FILE *stream, /*@only@*/ cstring sc)
1154 int maxlen = context_getLineLen ();
1155 int nspaces = lastfileloclen + 5;
1156 int nextlen = maxlen - nspaces;
1157 int len = cstring_length (sc);
1159 char *s = cstring_toCharsSafe (sc);
1163 DPRINTF (("Print error: [%s]", sc));
1165 if (len < (maxlen + nextlen) && (strchr (s, '\n') == NULL))
1167 mstring_split (&s, &t, maxlen, &indent);
1169 fprintf (stream, "%s\n", s);
1173 len = mstring_length (t);
1175 if (len < (maxlen - 3) && (strchr (t, '\n') == NULL)
1176 && len > (nextlen - 1))
1178 fprintf (stream, " %s\n", t);
1182 char *spaces = (char *) dmalloc ((nspaces + 1) * sizeof (*spaces));
1185 for (i = 0; i < nspaces; i++)
1190 spaces[nspaces] = '\0';
1195 mstring_split (&st, &t, nextlen, &indent);
1196 fprintf (stream, "%s%s\n", spaces, st);
1205 DPRINTF (("Here 1: [%s]", sc));
1207 if (len < (maxlen + maxlen - 1) && (strchr (s, '\n') != NULL))
1209 nspaces = ((maxlen + maxlen - 1) - len) / 2;
1211 if (nspaces < 1) nspaces = 1;
1213 nextlen = maxlen - nspaces;
1215 mstring_split (&s, &t, maxlen, &indent);
1217 fprintf (stream, "%s\n", s);
1221 char *spaces = (char *) dmalloc ((nspaces + 1) * sizeof (*spaces));
1224 for (i = 0; i < nspaces; i++)
1229 spaces[nspaces] = '\0';
1235 mstring_split (&st, &t, nextlen, &indent);
1236 fprintf (stream, "%s%s\n", spaces, st);
1245 nextlen = maxlen - nspaces;
1247 DPRINTF (("Here 2: [%s]", s));
1248 mstring_split (&s, &t, maxlen, &indent);
1249 DPRINTF (("Here 3: [%s] [%s]", s, t));
1251 fprintf (stream, "%s\n", s);
1255 char *spaces = (char *) dmalloc ((nspaces + 1) * sizeof (*spaces));
1258 for (i = 0; i < nspaces; i++)
1263 spaces[nspaces] = '\0';
1268 DPRINTF (("Loop: [%s]", t));
1269 mstring_split (&st, &t, nextlen, &indent);
1270 DPRINTF (("Split: [%s] [%s]", st, t));
1271 fprintf (stream, "%s%s\n", spaces, st);
1272 DPRINTF (("Next..."));
1285 xllfatalbug (char *srcFile, int srcLine, /*@only@*/ cstring s)
1288 printError (stderr, message ("%q: *** Fatal bug: %q",
1289 fileloc_unparse (g_currentloc), s));
1290 showSourceLoc (srcFile, srcLine);
1298 lclfatalbug (char *msg)
1302 message ("*** Fatal Bug: %s", cstring_fromChars (msg)));
1310 checkParseError (void)
1312 if (fileloc_withinLines (lastparseerror, g_currentloc, 10))
1314 llfatalerror (message ("%q: Cannot recover from parse error.",
1315 fileloc_unparse (g_currentloc)));
1319 void llbugaux (cstring file, int line, /*@only@*/ cstring s)
1322 static int numbugs = 0;
1323 static bool inbug = FALSE;
1327 cstring temps = fileloc_unparseRaw (file, line);
1329 fprintf (stderr, "%s: Recursive bug detected: %s\n",
1330 cstring_toCharsSafe (temps),
1331 cstring_toCharsSafe (s));
1332 cstring_free (temps);
1343 if (fileloc_isRealLib (g_currentloc))
1345 DPRINTF (("Here we are!"));
1346 llfatalerror (message ("%q: Library file appears to be corrupted. Error: %q: %s",
1347 fileloc_unparse (g_currentloc),
1348 fileloc_unparseRaw (file, line),
1353 if (fileloc_withinLines (lastparseerror, g_currentloc, 7))
1355 llfatalerror (message ("%q: Cannot recover from parse error.",
1356 fileloc_unparse (g_currentloc)));
1359 (void) fflush (g_msgstream);
1360 printError (stderr, message ("%q: *** Internal Bug at %q: %q [errno: %d]",
1361 fileloc_unparse (g_currentloc),
1362 fileloc_unparseRaw (file, line),
1366 (void) fflush (stderr);
1369 perror ("Possible system error diagnostic: ");
1371 (void) fflush (stderr);
1377 if (numbugs > context_getBugsLimit () && fileloc_withinLines (lastbug, g_currentloc, 2))
1379 llfatalerror (message ("%q: Cannot recover from last bug. (If you really want LCLint to try to continue, use -bugslimit <n>.)",
1380 fileloc_unparse (g_currentloc)));
1383 fprintf (stderr, " (attempting to continue, results may be incorrect)\n");
1384 fileloc_free (lastbug);
1385 lastbug = fileloc_copy (g_currentloc);
1388 (void) fflush (stderr);
1394 lclbug (/*@only@*/ cstring s)
1397 printError (stderr, message ("*** Internal Bug: %q", s));
1400 fprintf (stderr, " (attempting to continue, results may be incorrect)\n");
1406 llfatalerror (cstring s)
1409 printError (stderr, s);
1410 printError (stderr, cstring_makeLiteral ("*** Cannot continue."));
1415 llfatalerrorLoc (/*@only@*/ cstring s)
1418 (void) fflush (g_msgstream);
1419 printError (stderr, message ("%q: %q", fileloc_unparse (g_currentloc), s));
1420 printError (stderr, cstring_makeLiteral ("*** Cannot continue."));
1421 (void) fflush (g_msgstream);
1429 return (lclerrors > 0);
1433 lclHadNewError (void)
1435 static int lastcall = 0;
1437 if (lclerrors > lastcall)
1439 lastcall = lclerrors;
1449 lclNumberErrors (void)
1455 xlclerror (char *srcFile, int srcLine, ltoken t, /*@only@*/ cstring msg)
1459 if (ltoken_getCode (t) != NOTTOKEN)
1461 cstring loc = ltoken_unparseLoc (t);
1463 lastfileloclen = cstring_length (loc);
1465 printError (g_msgstream, message ("%q: %q", loc, msg));
1466 showSourceLoc (srcFile, srcLine);
1470 printError (g_msgstream, msg);
1471 showSourceLoc (srcFile, srcLine);
1476 lclplainerror (/*@only@*/ cstring msg)
1480 printError (g_msgstream, msg);
1484 lclfatalerror (ltoken t, /*@only@*/ cstring msg)
1487 (void) fflush (g_msgstream);
1488 printError (stderr, cstring_makeLiteral ("*** Cannot continue"));
1493 lclplainfatalerror (/*@only@*/ cstring msg)
1495 (void) fflush (g_msgstream);
1496 printError (stderr, message ("*** Cannot continue: %q", msg));
1501 lclRedeclarationError (ltoken id)
1503 cstring s = ltoken_getRawString (id);
1506 if (usymtab_existsEither (s))
1508 uentry le = usymtab_lookupEither (s);
1510 lclerror (id, message ("Respecification of %s", s));
1511 llgenindentmsg (message ("Previous specification of %q",
1512 uentry_getName (le)),
1513 uentry_whereSpecified (le));
1517 lclerror (id, message ("Identifier redeclared: %s", s));
1522 void genppllerror (flagcode code, /*@only@*/ cstring s)
1524 if (context_inSuppressZone (g_currentloc))
1530 if (context_getFlag (code))
1532 if (context_getFlag (FLG_SHOWSCAN) && !context_isInCommandLine ())
1534 fprintf (g_msgstream, " >\n");
1539 if (code != FLG_PREPROC)
1541 llsuppresshint ('-', code);
1544 if (context_getFlag (FLG_SHOWSCAN) && !context_isInCommandLine ())
1546 fprintf (stderr, "< more preprocessing .");
1556 void genppllerrorhint (flagcode code, /*@only@*/ cstring s,
1557 /*@only@*/ cstring hint)
1559 if (context_inSuppressZone (g_currentloc))
1562 cstring_free (hint);
1566 if (context_getFlag (code))
1569 context_clearPreprocessing ();
1572 context_setPreprocessing ();
1578 cstring_free (hint);
1583 void ppllerror (/*@only@*/ cstring s)
1585 genppllerror (FLG_PREPROC, s);
1588 void pplldiagmsg (cstring s)
1590 if (context_getDebug (FLG_SHOWSCAN) && !context_isInCommandLine ())
1592 fprintf (stderr, " >\n");
1594 fprintf (stderr, "< more preprocessing .");
1602 void loadllmsg (cstring s)
1604 if (context_getDebug (FLG_SHOWSCAN))
1606 fprintf (stderr, " >\n");
1608 fprintf (stderr, "< .");
1616 static void llreportparseerror (/*@only@*/ cstring s)
1618 if (fileloc_withinLines (lastparseerror, g_currentloc, 5))
1624 llerror (FLG_SYNTAX, s);
1626 fileloc_free (lastparseerror);
1627 lastparseerror = fileloc_copy (g_currentloc);
1631 bool xcppoptgenerror (char *srcFile, int srcLine,
1633 /*@only@*/ cstring s,
1637 fileloc loc = cppReader_getLoc (pfile);
1639 if (context_flagOn (o, loc))
1641 if (xlloptgenerror (srcFile, srcLine, o, s, loc))
1643 cppReader_printContainingFiles (pfile);
1657 bool xlloptgenerror (char *srcFile, int srcLine,
1658 flagcode o, /*@only@*/ cstring s, fileloc loc)
1660 DPRINTF (("xllopt: %s", s));
1662 if (llrealerror (srcFile, srcLine, s, loc))
1664 DPRINTF (("Here we are!"));
1665 llsuppresshint ('-', o);
1667 flagcode_recordError (o);
1672 DPRINTF (("Suppressed!"));
1673 flagcode_recordSuppressed (o);
1678 bool xoptgenerror2 (char *srcFile, int srcLine,
1679 flagcode f1, flagcode f2, /*@only@*/ cstring s, fileloc loc)
1681 if (context_suppressFlagMsg (f1, loc))
1683 flagcode_recordSuppressed (f1);
1688 if (context_suppressFlagMsg (f2, loc))
1690 flagcode_recordSuppressed (f2);
1695 if (llrealerror (srcFile, srcLine, s, loc))
1697 llsuppresshint2 ('-', f1, f2);
1698 flagcode_recordError (f2);
1704 flagcode_recordSuppressed (f2);
1712 bool xoptgenerror2n (char *srcFile, int srcLine,
1713 flagcode f1, flagcode f2, /*@only@*/ cstring s, fileloc loc)
1716 if (context_suppressFlagMsg (f1, loc))
1718 flagcode_recordSuppressed (f1);
1723 if (context_suppressNotFlagMsg (f2, loc))
1725 flagcode_recordSuppressed (f2);
1730 if (llrealerror (srcFile, srcLine, s, loc))
1732 llsuppresshint ('+', f2);
1733 flagcode_recordError (f2);
1738 flagcode_recordSuppressed (f2);
1744 bool xllnoptgenerror (char *srcFile, int srcLine,
1745 flagcode o, /*@only@*/ cstring s, fileloc loc)
1747 if (llrealerror (srcFile, srcLine, s, loc))
1749 llsuppresshint ('+', o);
1750 flagcode_recordError (o);
1755 flagcode_recordSuppressed (o);
1759 void llparseerror (cstring s)
1761 if (context_getFlag (FLG_TRYTORECOVER))
1765 if (parseerrorcount > GIVEUPPARSE)
1767 if (cstring_isDefined (s))
1769 llfatalerror (message ("%q: Parse Error: %q. "
1770 "Too many errors, giving up.",
1771 fileloc_unparse (g_currentloc), s));
1775 llfatalerror (message ("%q: Parse Error. Too many errors, giving up.",
1776 fileloc_unparse (g_currentloc)));
1781 if (cstring_isDefined (s))
1783 llreportparseerror (message ("Parse Error: %q. Attempting to continue.",
1788 llreportparseerror (message ("Parse Error. Attempting to continue."));
1796 if (cstring_isDefined (s))
1798 msg = message ("Parse Error: %q.", s);
1802 msg = message ("Parse Error.");
1806 (message ("%q: %s (For help on parse errors, "
1807 "see lclint -help parseerrors.)",
1808 fileloc_unparse (g_currentloc), msg));
1812 bool xfsgenerror (char *srcFile, int srcLine,
1813 flagSpec fs, /*@only@*/ cstring s, fileloc fl)
1815 if (flagSpec_isOn (fs, fl))
1817 if (llgenerroraux (srcFile, srcLine, s, fl, TRUE, FALSE))
1819 llsuppresshint ('-', flagSpec_getFirstOn (fs, fl));
1820 flagcode_recordError (flagSpec_getFirstOn (fs, fl));
1825 flagcode_recordSuppressed (flagSpec_getFirstOn (fs, fl));
1831 flagcode_recordSuppressed (flagSpec_getDominant (fs));
1837 bool doCheck (bool x, cstring pred, cstring file, int line)
1840 llbug (message ("%q: Check Failed: %s",
1841 fileloc_unparseRaw (file, line),
1848 /*@observer@*/ cstring lldecodeerror (/*@unused@*/ int errnum)
1853 #ifndef HAVE_STRERROR
1856 result = strerror (errnum);
1859 /* VAXCRTL's strerror() takes an optional second argument, which only
1860 matters when the first argument is EVMSERR. However, it's simplest
1861 just to pass it unconditionally. `vaxc$errno' is declared in
1862 <errno.h>, and maintained by the library in parallel with `errno'.
1863 We assume that caller's `errnum' either matches the last setting of
1864 `errno' by the library or else does not have the value `EVMSERR'. */
1866 result = strerror (errnum, vaxc$errno);
1871 result = cstring_toCharsSafe (message ("undocumented I/O error: %d", errnum));
1874 return cstring_fromChars (result);
1877 void llquietbugaux (cstring s, /*@unused@*/ cstring file, /*@unused@*/ int line)
1882 printError (stderr, message ("%q: *** Internal Bug at %q: %q [errno: %d]",
1883 fileloc_unparse (g_currentloc),
1884 fileloc_unparseRaw (file, line),
1896 (void) fflush (g_msgstream);
1897 (void) fflush (stderr);