2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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 /* 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@*/
78 /*@-onlyunqglobaltrans@*/
85 # define FATAL_EXIT_CODE EXIT_FAILURE
87 # include "lclintMacros.nf"
91 # include "cpperror.h"
93 static void cppReader_printFileAndLine (cppReader *p_pfile);
94 static void cppReader_warningWithLine (cppReader *p_pfile,
95 int p_line, int p_column,
96 /*@only@*/ cstring p_msg);
98 /* Print the file names and line numbers of the #include
99 commands which led to the current file. */
101 static void cppReader_printContainingFiles (cppReader *pfile)
107 /* Error processing command line. */
111 /* If stack of files hasn't changed since we last printed
112 this info, don't repeat it. */
113 if (pfile->input_stack_listing_current)
118 ip = cppReader_fileBuffer (pfile);
120 /* Give up if we don't find a source file. */
126 /* Find the other, outer source files. */
127 while ((ip = cppBuffer_prevBuffer (ip)),
128 ip != cppReader_nullBuffer (pfile))
133 cppBuffer_lineAndColumn (ip, &line, &col);
134 if (ip->fname != NULL)
139 fprintf (stderr, "In file included");
142 fprintf (stderr, ",\n ");
145 fprintf (stderr, " from %s",
146 cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line)));
148 cstring_free (temps);
153 fprintf (stderr, ":\n");
156 /* Record we have printed the status as of this time. */
157 pfile->input_stack_listing_current = 1;
161 cppReader_fileLineForMessage (cstring filename, long line, long column)
167 if (filename != NULL)
169 fprintf (stderr, "%s: ",
170 cstring_toCharsSafe (temps = fileloc_unparseRawCol (filename, (int) line, (int) column)));
174 fprintf (stderr, "%s: ",
175 cstring_toCharsSafe (temps = fileloc_unparseRawCol (cstring_makeLiteralTemp ("<no file>"),
176 (int) line, (int) column)));
179 cstring_free (temps);
185 if (filename != NULL)
187 fprintf (stderr, "%s: ",
188 cstring_toCharsSafe (temps = fileloc_unparseRaw (filename, (int) line)));
193 fprintf (stderr, "%s: ",
194 cstring_toCharsSafe (temps = fileloc_unparseRaw (cstring_makeLiteralTemp ("<no file>"),
198 cstring_free (temps);
202 /* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
205 cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg)
209 /* fprintf (stderr, "warning: "); */
211 else if (is_error == 2)
213 pfile->errors = cppReader_fatalErrorLimit;
215 else if (pfile->errors < cppReader_fatalErrorLimit)
224 fprintf (stderr, "%s", cstring_toCharsSafe (msg));
225 fprintf (stderr, "\n");
228 /* Same as cppReader_error, except we consider the error to be "fatal",
229 such as inconsistent options. I.e. there is little point in continuing.
230 (We do not exit, to support use of cpplib as a library.
231 Instead, it is the caller's responsibility to check
235 cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str)
237 fprintf (stderr, "preprocessor: ");
238 cppReader_message (pfile, 2, str);
242 cppReader_pfatalWithName (cppReader *pfile, cstring name)
244 cppReader_perrorWithName (pfile, name);
245 exit (FATAL_EXIT_CODE);
249 cppReader_printFileAndLine (cppReader *pfile)
251 cppBuffer *ip = cppReader_fileBuffer (pfile);
257 cppBuffer_lineAndColumn (ip, &line, &col);
258 cppReader_fileLineForMessage (ip->nominal_fname,
259 line, pfile->show_column ? col : -1);
263 fprintf (stderr, "Command Line: ");
268 cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg)
270 cppReader_error (pfile, cstring_copy (msg));
274 cppReader_error (cppReader *pfile, /*@only@*/ cstring msg)
277 cppReader_printContainingFiles (pfile);
278 cppReader_printFileAndLine (pfile);
279 cppReader_message (pfile, 1, msg);
283 /* Print error message but don't count it. */
286 cppReader_warningLit (cppReader *pfile, cstring msg)
288 cppReader_warning (pfile, cstring_copy (msg));
292 cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg)
294 if (CPPOPTIONS (pfile)->inhibit_warnings)
297 if (CPPOPTIONS (pfile)->warnings_are_errors)
301 cppReader_printContainingFiles (pfile);
302 cppReader_printFileAndLine (pfile);
303 cppReader_message (pfile, 0, msg);
307 /* Print an error message and maybe count it. */
310 cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg)
312 cppReader_pedwarn (pfile, cstring_copy (msg));
316 cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg)
318 if (CPPOPTIONS (pfile)->pedantic_errors)
320 cppReader_error (pfile, msg);
324 cppReader_warning (pfile, msg);
329 cppReader_errorWithLine (cppReader *pfile, long line, long column,
330 /*@only@*/ cstring msg)
332 cppBuffer *ip = cppReader_fileBuffer (pfile);
335 cppReader_printContainingFiles (pfile);
338 cppReader_fileLineForMessage (ip->nominal_fname, line, column);
340 cppReader_message (pfile, 1, msg);
345 cppReader_warningWithLine (cppReader *pfile,
346 int line, int column,
347 /*@only@*/ cstring msg)
351 if (CPPOPTIONS (pfile)->inhibit_warnings)
354 if (CPPOPTIONS (pfile)->warnings_are_errors)
359 cppReader_printContainingFiles (pfile);
361 ip = cppReader_fileBuffer (pfile);
365 cppReader_fileLineForMessage (ip->nominal_fname, line, column);
368 cppReader_message (pfile, 0, msg);
373 cppReader_pedwarnWithLine (cppReader *pfile, int line, int column,
374 /*@only@*/ cstring msg)
376 if (CPPOPTIONS (pfile)->pedantic_errors)
378 cppReader_errorWithLine (pfile, column, line, msg);
382 cppReader_warningWithLine (pfile, line, column, msg);
386 void cppReader_perrorWithName (cppReader *pfile, cstring name)
388 cppReader_message (pfile, 1,
389 message ("preprocessing: %s: %s",
390 name, lldecodeerror (errno)));