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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
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
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! */
58 /*@-loopswitchbreak@*/
59 /*@-switchloopbreak@*/
76 /*@+matchanyintegral@*/
77 /*@-onlyunqglobaltrans@*/
84 # define FATAL_EXIT_CODE EXIT_FAILURE
86 # include "lclintMacros.nf"
89 # include "cpperror.h"
91 static void cppReader_printFileAndLine (cppReader *p_pfile);
92 static void cppReader_warningWithLine (cppReader *p_pfile,
93 int p_line, int p_column,
94 /*@only@*/ cstring p_msg);
96 /* Print the file names and line numbers of the #include
97 commands which led to the current file. */
99 void cppReader_printContainingFiles (cppReader *pfile)
105 /* Error processing command line. */
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)
116 ip = cppReader_fileBuffer (pfile);
118 /* Give up if we don't find a source file. */
124 /* Find the other, outer source files. */
125 while ((ip = cppBuffer_prevBuffer (ip)),
126 ip != cppReader_nullBuffer (pfile))
131 cppBuffer_lineAndColumn (ip, &line, &col);
132 if (ip->fname != NULL)
137 fprintf (g_msgstream, " In file included");
140 fprintf (g_msgstream, ",\n ");
143 fprintf (g_msgstream, " from %s",
144 cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line)));
146 cstring_free (temps);
151 fprintf (g_msgstream, "\n");
154 /* Record we have printed the status as of this time. */
155 pfile->input_stack_listing_current = 1;
159 cppReader_fileLineForMessage (cstring filename, long line, long column)
165 if (filename != NULL)
167 fprintf (stderr, "%s: ",
168 cstring_toCharsSafe (temps = fileloc_unparseRawCol (filename, (int) line, (int) column)));
172 fprintf (stderr, "%s: ",
173 cstring_toCharsSafe (temps = fileloc_unparseRawCol (cstring_makeLiteralTemp ("<no file>"),
174 (int) line, (int) column)));
177 cstring_free (temps);
183 if (filename != NULL)
185 fprintf (stderr, "%s: ",
186 cstring_toCharsSafe (temps = fileloc_unparseRaw (filename, (int) line)));
191 fprintf (stderr, "%s: ",
192 cstring_toCharsSafe (temps = fileloc_unparseRaw (cstring_makeLiteralTemp ("<no file>"),
196 cstring_free (temps);
200 /* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
203 cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg)
207 /* fprintf (stderr, "warning: "); */
209 else if (is_error == 2)
211 pfile->errors = cppReader_fatalErrorLimit;
213 else if (pfile->errors < cppReader_fatalErrorLimit)
222 fprintf (stderr, "%s", cstring_toCharsSafe (msg));
223 fprintf (stderr, "\n");
226 /* Same as cppReader_error, except we consider the error to be "fatal",
227 such as inconsistent options. I.e. there is little point in continuing.
228 (We do not exit, to support use of cpplib as a library.
229 Instead, it is the caller's responsibility to check
230 cpplib_fatalErrors. */
233 cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str)
235 fprintf (stderr, "preprocessor: ");
236 cppReader_message (pfile, 2, str);
240 cppReader_pfatalWithName (cppReader *pfile, cstring name)
242 cppReader_perrorWithName (pfile, name);
243 exit (FATAL_EXIT_CODE);
247 cppReader_getLoc (cppReader *pfile)
249 cppBuffer *ip = cppReader_fileBuffer (pfile);
254 cstring fname = ip->nominal_fname;
255 fileId fid = fileTable_lookup (context_fileTable (), fname);
257 llassert (fileId_isValid (fid));
259 cppBuffer_lineAndColumn (ip, &line, &col);
261 return fileloc_create (fid, line, col);
265 return fileloc_createBuiltin ();
270 cppReader_printFileAndLine (cppReader *pfile)
272 cppBuffer *ip = cppReader_fileBuffer (pfile);
278 cppBuffer_lineAndColumn (ip, &line, &col);
279 cppReader_fileLineForMessage (ip->nominal_fname,
280 line, pfile->show_column ? col : -1);
284 fprintf (stderr, "Command Line: ");
289 cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg)
291 cppReader_error (pfile, cstring_copy (msg));
295 cppReader_error (cppReader *pfile, /*@only@*/ cstring msg)
298 cppReader_printContainingFiles (pfile);
299 cppReader_printFileAndLine (pfile);
300 cppReader_message (pfile, 1, msg);
304 /* Print error message but don't count it. */
307 cppReader_warningLit (cppReader *pfile, cstring msg)
309 cppReader_warning (pfile, cstring_copy (msg));
313 cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg)
315 if (CPPOPTIONS (pfile)->warnings_are_errors)
319 cppReader_printContainingFiles (pfile);
320 cppReader_printFileAndLine (pfile);
321 cppReader_message (pfile, 0, msg);
325 /* Print an error message and maybe count it. */
328 cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg)
330 cppReader_pedwarn (pfile, cstring_copy (msg));
334 cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg)
336 if (CPPOPTIONS (pfile)->pedantic_errors)
338 cppReader_error (pfile, msg);
342 cppReader_warning (pfile, msg);
347 cppReader_errorWithLine (cppReader *pfile, long line, long column,
348 /*@only@*/ cstring msg)
350 cppBuffer *ip = cppReader_fileBuffer (pfile);
353 cppReader_printContainingFiles (pfile);
356 cppReader_fileLineForMessage (ip->nominal_fname, line, column);
358 cppReader_message (pfile, 1, msg);
363 cppReader_warningWithLine (cppReader *pfile,
364 int line, int column,
365 /*@only@*/ cstring msg)
369 if (CPPOPTIONS (pfile)->warnings_are_errors)
374 cppReader_printContainingFiles (pfile);
376 ip = cppReader_fileBuffer (pfile);
380 cppReader_fileLineForMessage (ip->nominal_fname, line, column);
383 cppReader_message (pfile, 0, msg);
388 cppReader_pedwarnWithLine (cppReader *pfile, int line, int column,
389 /*@only@*/ cstring msg)
391 if (CPPOPTIONS (pfile)->pedantic_errors)
393 cppReader_errorWithLine (pfile, column, line, msg);
397 cppReader_warningWithLine (pfile, line, column, msg);
401 void cppReader_perrorWithName (cppReader *pfile, cstring name)
403 cppReader_message (pfile, 1,
404 message ("Preprocessing error for %s: %s",
405 name, lldecodeerror (errno)));