Some consistency changes to library & headers flags.
[splint-patched.git] / src / cpperror.c
blob69f4559da2b4e0015d2c5bad47c973a7d623a16a
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 ** cpperror.c
27 /* Default error handlers for CPP Library.
28 Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
29 Written by Per Bothner, 1994.
30 Based on CCCP program by by Paul Rubin, June 1986
31 Adapted to ANSI C, Richard Stallman, Jan 1987
33 This program is free software; you can redistribute it and/or modify it
34 under the terms of the GNU General Public License as published by the
35 Free Software Foundation; either version 2, or (at your option) any
36 later version.
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
43 You should have received a copy of the GNU General Public License
44 along with this program; if not, write to the Free Software
45 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 In other words, you are welcome to use, share and improve this program.
48 You are forbidden to forbid anyone else to use, share and improve
49 what you give them. Help stamp out software-hoarding! */
51 /*@-macroconstdecl@*/
52 /*@-macrofcndecl@*/
53 /*@-evalorderuncon@*/
54 /*@+ptrnegate@*/
55 /*@-memchecks@*/
56 /*@+charintliteral@*/
57 /*@-infloopsuncon@*/
58 /*@-loopswitchbreak@*/
59 /*@-switchloopbreak@*/
60 /*@-whileblock@*/
61 /*@-forblock@*/
62 /*@-elseifcomplete@*/
63 /*@-abstract@*/
64 /*@-usedef@*/
65 /*@-retvalint@*/
66 /*@-macroparams@*/
67 /*@-readonlytrans@*/
68 /*@-boolops@*/
69 /*@-sizeoftype@*/
70 /*@-predboolint@*/
71 /*@-predboolptr@*/
72 /*@+boolint@*/
73 /*@+charint@*/
74 /*@+ignorequals@*/
75 /*@+ignoresigns@*/
76 /*@+matchanyintegral@*/
77 /*@-onlyunqglobaltrans@*/
78 /*@-macroconstdecl@*/
80 # include <stdlib.h>
81 # include <errno.h>
82 # include <stdio.h>
84 # include "splintMacros.nf"
85 # include "basic.h"
86 # include "cpplib.h"
87 # include "cpperror.h"
89 static void cppReader_warningWithLine (cppReader *p_pfile,
90 int p_line, int p_column,
91 /*@only@*/ cstring p_msg);
93 static /*@only@*/ fileloc cppReader_getLoc (cppReader *);
95 /* Print the file names and line numbers of the #include
96 commands which led to the current file. */
98 static void cppReader_printContainingFiles (cppReader *pfile)
99 /*@modifies g_warningstream, p_pfile@*/
101 cppBuffer *ip;
102 int first = 1;
104 if (pfile == NULL) {
105 /* Error processing command line. */
106 return;
109 /* If stack of files hasn't changed since we last printed
110 this info, don't repeat it. */
111 if (pfile->input_stack_listing_current)
113 return;
116 ip = cppReader_fileBuffer (pfile);
118 /* Give up if we don't find a source file. */
119 if (ip == NULL)
121 return;
124 /* Find the other, outer source files. */
125 while ((ip = cppBuffer_prevBuffer (ip)) != cppReader_nullBuffer (pfile))
127 int line, col;
128 cstring temps;
130 cppBuffer_getLineAndColumn (ip, &line, &col);
131 if (ip->fname != NULL)
133 if (first)
135 first = 0;
136 fprintf (g_warningstream, " In file included");
138 else
139 fprintf (g_warningstream, ",\n ");
142 fprintf (g_warningstream, " from %s",
143 cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line)));
145 cstring_free (temps);
148 if (!first)
150 fprintf (g_warningstream, "\n");
153 /* Record we have printed the status as of this time. */
154 pfile->input_stack_listing_current = TRUE;
157 bool xcppoptgenerror (const char *srcFile, int srcLine,
158 flagcode o,
159 /*@only@*/ cstring s,
160 cppReader *pfile)
162 bool res = FALSE;
163 fileloc loc = cppReader_getLoc (pfile);
165 if (context_flagOn (o, loc))
167 if (xlloptgenerror (srcFile, srcLine, o, s, loc))
169 cppReader_printContainingFiles (pfile);
170 res = TRUE;
173 else
175 cstring_free (s);
178 fileloc_free (loc);
180 return res;
183 /* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
185 static void
186 cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg)
188 if (!is_error)
190 /* fprintf (stderr, "warning: "); */
192 else if (is_error == 2)
194 pfile->errors = cppReader_fatalErrorLimit;
196 else if (pfile->errors < cppReader_fatalErrorLimit)
198 pfile->errors++;
200 else
205 fprintf (stderr, "%s", cstring_toCharsSafe (msg));
206 fprintf (stderr, "\n");
209 /* Same as cppReader_error, except we consider the error to be "fatal",
210 such as inconsistent options. I.e. there is little point in continuing.
211 (We do not exit, to support use of cpplib as a library.
212 Instead, it is the caller's responsibility to check
213 cpplib_fatalErrors. */
215 void
216 cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str)
218 fprintf (stderr, "preprocessor: ");
219 cppReader_message (pfile, 2, str);
222 # ifdef DEADCODE
223 void
224 cppReader_pfatalWithName (cppReader *pfile, cstring name)
226 cppReader_perrorWithName (pfile, name);
227 exit (FATAL_EXIT_CODE);
229 # endif /* DEADCODE */
231 static /*@only@*/ fileloc
232 cppReader_getLoc (cppReader *pfile)
234 cppBuffer *ip = cppReader_fileBuffer (pfile);
236 if (ip != NULL && ip->buf != NULL)
238 int line, col;
239 cstring fname = ip->nominal_fname;
240 fileId fid = fileTable_lookup (context_fileTable (), fname);
242 if (!fileId_isValid (fid))
244 /* evans 2002-02-09
245 ** filename used in #line comment is new
248 fid = fileTable_addFile (context_fileTable (), FILE_NORMAL, fname);
251 cppBuffer_getLineAndColumn (ip, &line, &col);
252 return fileloc_create (fid, line, col);
254 else
256 return fileloc_createBuiltin ();
260 void
261 cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg)
263 cppReader_error (pfile, cstring_copy (msg));
266 void
267 cppReader_error (cppReader *pfile, /*@only@*/ cstring msg)
269 if (cppoptgenerror (FLG_PREPROC, msg, pfile))
271 pfile->errors++;
275 /* Print error message but don't count it. */
277 void
278 cppReader_warningLit (cppReader *pfile, cstring msg)
280 cppReader_warning (pfile, cstring_copy (msg));
283 void
284 cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg)
286 if (CPPOPTIONS (pfile)->warnings_are_errors)
287 pfile->errors++;
289 cppoptgenerror (FLG_PREPROC, msg, pfile);
292 /* Print an error message and maybe count it. */
294 extern void
295 cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg)
297 cppReader_pedwarn (pfile, cstring_copy (msg));
300 extern void
301 cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg)
303 if (CPPOPTIONS (pfile)->pedantic_errors)
305 cppReader_error (pfile, msg);
307 else
309 cppReader_warning (pfile, msg);
313 void
314 cppReader_errorWithLine (cppReader *pfile, int line, int column,
315 /*@only@*/ cstring msg)
317 fileloc loc = cppReader_getLoc (pfile);
318 fileloc_setLineno (loc, line);
319 fileloc_setColumn (loc, column);
321 cppoptgenerror (FLG_PREPROC, message ("%s: %s",
322 fileloc_unparse (loc),
323 msg),
324 pfile);
327 void
328 cppReader_warningWithLine (cppReader *pfile,
329 int line, int column,
330 /*@only@*/ cstring msg)
332 if (CPPOPTIONS (pfile)->warnings_are_errors)
333 pfile->errors++;
335 cppReader_errorWithLine (pfile, line, column, msg);
338 void
339 cppReader_pedwarnWithLine (cppReader *pfile, int line, int column,
340 /*@only@*/ cstring msg)
342 if (CPPOPTIONS (pfile)->pedantic_errors)
344 cppReader_errorWithLine (pfile, column, line, msg);
346 else
348 cppReader_warningWithLine (pfile, line, column, msg);
352 void cppReader_perrorWithName (cppReader *pfile, cstring name)
354 cppoptgenerror (FLG_PREPROC,
355 message ("%s: Preprocessing error: %s",
356 name, lldecodeerror (errno)),
357 pfile);