]> andersk Git - splint.git/blame - src/cpperror.c
Fixed bug with unrecognized files in #line commands.
[splint.git] / src / cpperror.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
77d37419 3** Copyright (C) 1994-2002 University of Virginia,
616915dd 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.
19**
155af98d 20** For information on splint: info@splint.org
21** To report a bug: splint-bug@splint.org
11db3170 22** For more information: http://www.splint.org
616915dd 23*/
24/*
25** cpperror.c
26*/
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
32
33This program is free software; you can redistribute it and/or modify it
34under the terms of the GNU General Public License as published by the
35Free Software Foundation; either version 2, or (at your option) any
36later version.
37
38This program is distributed in the hope that it will be useful,
39but WITHOUT ANY WARRANTY; without even the implied warranty of
40MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41GNU General Public License for more details.
42
43You should have received a copy of the GNU General Public License
44along with this program; if not, write to the Free Software
45Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46
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! */
50
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@*/
616915dd 77/*@-onlyunqglobaltrans@*/
78/*@-macroconstdecl@*/
79
80# include <stdlib.h>
81# include <errno.h>
82# include <stdio.h>
83
84# define FATAL_EXIT_CODE EXIT_FAILURE
85
1b8ae690 86# include "splintMacros.nf"
616915dd 87# include "llbasic.h"
616915dd 88# include "cpplib.h"
89# include "cpperror.h"
90
91static void cppReader_printFileAndLine (cppReader *p_pfile);
92static void cppReader_warningWithLine (cppReader *p_pfile,
93 int p_line, int p_column,
94 /*@only@*/ cstring p_msg);
95
96/* Print the file names and line numbers of the #include
97 commands which led to the current file. */
98
b072092f 99void cppReader_printContainingFiles (cppReader *pfile)
616915dd 100{
101 cppBuffer *ip;
102 int first = 1;
103
104 if (pfile == NULL) {
105 /* Error processing command line. */
106 return;
107 }
108
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)
112 {
113 return;
114 }
115
116 ip = cppReader_fileBuffer (pfile);
117
118 /* Give up if we don't find a source file. */
119 if (ip == NULL)
120 {
121 return;
122 }
123
124 /* Find the other, outer source files. */
125 while ((ip = cppBuffer_prevBuffer (ip)),
126 ip != cppReader_nullBuffer (pfile))
127 {
128 int line, col;
129 cstring temps;
130
131 cppBuffer_lineAndColumn (ip, &line, &col);
132 if (ip->fname != NULL)
133 {
134 if (first)
135 {
136 first = 0;
b072092f 137 fprintf (g_msgstream, " In file included");
616915dd 138 }
139 else
b072092f 140 fprintf (g_msgstream, ",\n ");
616915dd 141 }
142
b072092f 143 fprintf (g_msgstream, " from %s",
616915dd 144 cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line)));
b072092f 145
616915dd 146 cstring_free (temps);
147 }
148
149 if (!first)
150 {
b072092f 151 fprintf (g_msgstream, "\n");
616915dd 152 }
153
154 /* Record we have printed the status as of this time. */
155 pfile->input_stack_listing_current = 1;
156}
157
158static void
159cppReader_fileLineForMessage (cstring filename, long line, long column)
160{
161 if (column > 0)
162 {
163 cstring temps;
164
165 if (filename != NULL)
166 {
167 fprintf (stderr, "%s: ",
168 cstring_toCharsSafe (temps = fileloc_unparseRawCol (filename, (int) line, (int) column)));
169 }
170 else
171 {
172 fprintf (stderr, "%s: ",
173 cstring_toCharsSafe (temps = fileloc_unparseRawCol (cstring_makeLiteralTemp ("<no file>"),
174 (int) line, (int) column)));
175 }
176
177 cstring_free (temps);
178 }
179 else
180 {
181 cstring temps;
182
183 if (filename != NULL)
184 {
185 fprintf (stderr, "%s: ",
186 cstring_toCharsSafe (temps = fileloc_unparseRaw (filename, (int) line)));
187
188 }
189 else
190 {
191 fprintf (stderr, "%s: ",
192 cstring_toCharsSafe (temps = fileloc_unparseRaw (cstring_makeLiteralTemp ("<no file>"),
193 (int) line)));
194 }
195
196 cstring_free (temps);
197 }
198}
199
200/* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
201
202static void
203cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg)
204{
205 if (!is_error)
206 {
207 /* fprintf (stderr, "warning: "); */
208 }
209 else if (is_error == 2)
210 {
211 pfile->errors = cppReader_fatalErrorLimit;
212 }
213 else if (pfile->errors < cppReader_fatalErrorLimit)
214 {
215 pfile->errors++;
216 }
217 else
218 {
219 ;
220 }
221
222 fprintf (stderr, "%s", cstring_toCharsSafe (msg));
223 fprintf (stderr, "\n");
224}
225
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
3e3ec469 230 cpplib_fatalErrors. */
616915dd 231
232void
233cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str)
234{
235 fprintf (stderr, "preprocessor: ");
236 cppReader_message (pfile, 2, str);
237}
238
239void
240cppReader_pfatalWithName (cppReader *pfile, cstring name)
241{
242 cppReader_perrorWithName (pfile, name);
243 exit (FATAL_EXIT_CODE);
244}
245
b072092f 246/*@only@*/ fileloc
247cppReader_getLoc (cppReader *pfile)
248{
249 cppBuffer *ip = cppReader_fileBuffer (pfile);
250
251 if (ip != NULL)
252 {
253 int line, col;
254 cstring fname = ip->nominal_fname;
255 fileId fid = fileTable_lookup (context_fileTable (), fname);
256
ec579bb3 257 if (!fileId_isValid (fid))
258 {
259 /* evans 2002-02-09
260 ** filename used in #line comment is new
261 */
262
263 fid = fileTable_addFile (context_fileTable (), fname);
264 }
265
b072092f 266 cppBuffer_lineAndColumn (ip, &line, &col);
267
268 return fileloc_create (fid, line, col);
269 }
270 else
271 {
272 return fileloc_createBuiltin ();
273 }
274}
275
616915dd 276void
277cppReader_printFileAndLine (cppReader *pfile)
278{
279 cppBuffer *ip = cppReader_fileBuffer (pfile);
280
281 if (ip != NULL)
282 {
283 int line, col;
284
285 cppBuffer_lineAndColumn (ip, &line, &col);
286 cppReader_fileLineForMessage (ip->nominal_fname,
287 line, pfile->show_column ? col : -1);
288 }
289 else
290 {
291 fprintf (stderr, "Command Line: ");
292 }
293}
294
295void
296cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg)
297{
298 cppReader_error (pfile, cstring_copy (msg));
299}
300
301void
302cppReader_error (cppReader *pfile, /*@only@*/ cstring msg)
303{
304 prepareMessage ();
305 cppReader_printContainingFiles (pfile);
306 cppReader_printFileAndLine (pfile);
307 cppReader_message (pfile, 1, msg);
308 closeMessage ();
309}
310
311/* Print error message but don't count it. */
312
313void
314cppReader_warningLit (cppReader *pfile, cstring msg)
315{
316 cppReader_warning (pfile, cstring_copy (msg));
317}
318
319void
320cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg)
321{
616915dd 322 if (CPPOPTIONS (pfile)->warnings_are_errors)
323 pfile->errors++;
b072092f 324
616915dd 325 prepareMessage ();
326 cppReader_printContainingFiles (pfile);
327 cppReader_printFileAndLine (pfile);
328 cppReader_message (pfile, 0, msg);
329 closeMessage ();
330}
331
332/* Print an error message and maybe count it. */
333
334extern void
335cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg)
336{
337 cppReader_pedwarn (pfile, cstring_copy (msg));
338}
339
340extern void
341cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg)
342{
343 if (CPPOPTIONS (pfile)->pedantic_errors)
344 {
345 cppReader_error (pfile, msg);
346 }
347 else
348 {
349 cppReader_warning (pfile, msg);
350 }
351}
352
353void
354cppReader_errorWithLine (cppReader *pfile, long line, long column,
355 /*@only@*/ cstring msg)
356{
357 cppBuffer *ip = cppReader_fileBuffer (pfile);
358
359 prepareMessage ();
360 cppReader_printContainingFiles (pfile);
361
362 if (ip != NULL)
363 cppReader_fileLineForMessage (ip->nominal_fname, line, column);
364
365 cppReader_message (pfile, 1, msg);
366 closeMessage ();
367}
368
369void
370cppReader_warningWithLine (cppReader *pfile,
371 int line, int column,
372 /*@only@*/ cstring msg)
373{
374 cppBuffer *ip;
375
616915dd 376 if (CPPOPTIONS (pfile)->warnings_are_errors)
377 pfile->errors++;
378
379 prepareMessage ();
380
381 cppReader_printContainingFiles (pfile);
382
383 ip = cppReader_fileBuffer (pfile);
384
385 if (ip != NULL)
386 {
387 cppReader_fileLineForMessage (ip->nominal_fname, line, column);
388 }
389
390 cppReader_message (pfile, 0, msg);
391 closeMessage ();
392}
393
394void
395cppReader_pedwarnWithLine (cppReader *pfile, int line, int column,
396 /*@only@*/ cstring msg)
397{
398 if (CPPOPTIONS (pfile)->pedantic_errors)
399 {
400 cppReader_errorWithLine (pfile, column, line, msg);
401 }
402 else
403 {
404 cppReader_warningWithLine (pfile, line, column, msg);
405 }
406}
407
408void cppReader_perrorWithName (cppReader *pfile, cstring name)
409{
410 cppReader_message (pfile, 1,
28bf4b0b 411 message ("Preprocessing error for %s: %s",
616915dd 412 name, lldecodeerror (errno)));
413}
414
415
416
417
418
419
420
421
This page took 0.116802 seconds and 5 git commands to generate.