]> andersk Git - splint.git/blame - src/cpperror.c
Fixed all /*@i...@*/ tags (except 1).
[splint.git] / src / cpperror.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
c59f5181 3** Copyright (C) 1994-2003 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"
b73d1009 87# include "basic.h"
616915dd 88# include "cpplib.h"
89# include "cpperror.h"
90
80489f0a 91static cstring cppReader_unparseLoc (cppReader *p_pfile);
92
616915dd 93static void cppReader_warningWithLine (cppReader *p_pfile,
94 int p_line, int p_column,
95 /*@only@*/ cstring p_msg);
96
97/* Print the file names and line numbers of the #include
98 commands which led to the current file. */
99
b072092f 100void cppReader_printContainingFiles (cppReader *pfile)
616915dd 101{
102 cppBuffer *ip;
103 int first = 1;
104
105 if (pfile == NULL) {
106 /* Error processing command line. */
107 return;
108 }
109
110 /* If stack of files hasn't changed since we last printed
111 this info, don't repeat it. */
112 if (pfile->input_stack_listing_current)
113 {
114 return;
115 }
116
117 ip = cppReader_fileBuffer (pfile);
118
119 /* Give up if we don't find a source file. */
120 if (ip == NULL)
121 {
122 return;
123 }
124
125 /* Find the other, outer source files. */
126 while ((ip = cppBuffer_prevBuffer (ip)),
127 ip != cppReader_nullBuffer (pfile))
128 {
129 int line, col;
130 cstring temps;
131
80489f0a 132 cppBuffer_getLineAndColumn (ip, &line, &col);
616915dd 133 if (ip->fname != NULL)
134 {
135 if (first)
136 {
137 first = 0;
80489f0a 138 fprintf (g_warningstream, " In file included");
616915dd 139 }
140 else
80489f0a 141 fprintf (g_warningstream, ",\n ");
616915dd 142 }
143
80489f0a 144 fprintf (g_warningstream, " from %s",
616915dd 145 cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line)));
b072092f 146
616915dd 147 cstring_free (temps);
148 }
149
150 if (!first)
151 {
80489f0a 152 fprintf (g_warningstream, "\n");
616915dd 153 }
154
155 /* Record we have printed the status as of this time. */
156 pfile->input_stack_listing_current = 1;
157}
158
80489f0a 159static /*@only@*/ cstring
160cppReader_unparseLoc (cppReader *pfile)
616915dd 161{
80489f0a 162 DPRINTF (("unparse loc: %s",
163 fileloc_unparse (cppReader_getLoc (pfile))));
164 return (fileloc_unparse (cppReader_getLoc (pfile)));
616915dd 165}
166
167/* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
168
169static void
170cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg)
171{
172 if (!is_error)
173 {
174 /* fprintf (stderr, "warning: "); */
175 }
176 else if (is_error == 2)
177 {
178 pfile->errors = cppReader_fatalErrorLimit;
179 }
180 else if (pfile->errors < cppReader_fatalErrorLimit)
181 {
182 pfile->errors++;
183 }
184 else
185 {
186 ;
187 }
188
189 fprintf (stderr, "%s", cstring_toCharsSafe (msg));
190 fprintf (stderr, "\n");
191}
192
193/* Same as cppReader_error, except we consider the error to be "fatal",
194 such as inconsistent options. I.e. there is little point in continuing.
195 (We do not exit, to support use of cpplib as a library.
196 Instead, it is the caller's responsibility to check
3e3ec469 197 cpplib_fatalErrors. */
616915dd 198
199void
200cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str)
201{
202 fprintf (stderr, "preprocessor: ");
203 cppReader_message (pfile, 2, str);
204}
205
206void
207cppReader_pfatalWithName (cppReader *pfile, cstring name)
208{
209 cppReader_perrorWithName (pfile, name);
210 exit (FATAL_EXIT_CODE);
211}
212
b072092f 213/*@only@*/ fileloc
214cppReader_getLoc (cppReader *pfile)
215{
216 cppBuffer *ip = cppReader_fileBuffer (pfile);
217
6fcd0b1e 218 if (ip != NULL && ip->buf != NULL)
b072092f 219 {
220 int line, col;
221 cstring fname = ip->nominal_fname;
222 fileId fid = fileTable_lookup (context_fileTable (), fname);
223
ec579bb3 224 if (!fileId_isValid (fid))
225 {
226 /* evans 2002-02-09
227 ** filename used in #line comment is new
228 */
229
230 fid = fileTable_addFile (context_fileTable (), fname);
231 }
232
80489f0a 233 cppBuffer_getLineAndColumn (ip, &line, &col);
b072092f 234 return fileloc_create (fid, line, col);
235 }
236 else
237 {
238 return fileloc_createBuiltin ();
239 }
240}
241
616915dd 242void
243cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg)
244{
245 cppReader_error (pfile, cstring_copy (msg));
246}
247
248void
249cppReader_error (cppReader *pfile, /*@only@*/ cstring msg)
250{
80489f0a 251 if (cppoptgenerror (FLG_PREPROC, msg, pfile))
252 {
253 pfile->errors++;
254 }
616915dd 255}
256
257/* Print error message but don't count it. */
258
259void
260cppReader_warningLit (cppReader *pfile, cstring msg)
261{
262 cppReader_warning (pfile, cstring_copy (msg));
263}
264
265void
266cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg)
267{
616915dd 268 if (CPPOPTIONS (pfile)->warnings_are_errors)
269 pfile->errors++;
80489f0a 270
271 cppoptgenerror (FLG_PREPROC, msg, pfile);
616915dd 272}
273
274/* Print an error message and maybe count it. */
275
276extern void
277cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg)
278{
279 cppReader_pedwarn (pfile, cstring_copy (msg));
280}
281
282extern void
283cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg)
284{
285 if (CPPOPTIONS (pfile)->pedantic_errors)
286 {
287 cppReader_error (pfile, msg);
288 }
289 else
290 {
291 cppReader_warning (pfile, msg);
292 }
293}
294
295void
80489f0a 296cppReader_errorWithLine (cppReader *pfile, int line, int column,
297 /*@only@*/ cstring msg)
616915dd 298{
80489f0a 299 fileloc loc = cppReader_getLoc (pfile);
300 fileloc_setLineno (loc, line);
301 fileloc_setColumn (loc, column);
302
303 cppoptgenerror (FLG_PREPROC, message ("%s: %s",
304 fileloc_unparse (loc),
305 msg),
306 pfile);
616915dd 307}
308
309void
310cppReader_warningWithLine (cppReader *pfile,
80489f0a 311 int line, int column,
312 /*@only@*/ cstring msg)
616915dd 313{
616915dd 314 if (CPPOPTIONS (pfile)->warnings_are_errors)
315 pfile->errors++;
316
80489f0a 317 cppReader_errorWithLine (pfile, line, column, msg);
616915dd 318}
319
320void
321cppReader_pedwarnWithLine (cppReader *pfile, int line, int column,
322 /*@only@*/ cstring msg)
323{
324 if (CPPOPTIONS (pfile)->pedantic_errors)
325 {
326 cppReader_errorWithLine (pfile, column, line, msg);
327 }
328 else
329 {
330 cppReader_warningWithLine (pfile, line, column, msg);
331 }
332}
333
334void cppReader_perrorWithName (cppReader *pfile, cstring name)
335{
80489f0a 336 cppoptgenerror (FLG_PREPROC,
337 message ("%s: Preprocessing error: %s",
338 name, lldecodeerror (errno)),
339 pfile);
616915dd 340}
341
342
343
344
345
346
347
348
This page took 0.121464 seconds and 5 git commands to generate.