]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
2 | ** LCLint - annotation-assisted static program checker | |
28bf4b0b | 3 | ** Copyright (C) 1994-2001 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 | ** | |
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 | |
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 | ||
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. | |
37 | ||
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. | |
42 | ||
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. | |
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@*/ | |
77 | /*@-shiftsigned@*/ | |
78 | /*@-onlyunqglobaltrans@*/ | |
79 | /*@-macroconstdecl@*/ | |
80 | ||
81 | # include <stdlib.h> | |
82 | # include <errno.h> | |
83 | # include <stdio.h> | |
84 | ||
85 | # define FATAL_EXIT_CODE EXIT_FAILURE | |
86 | ||
87 | # include "lclintMacros.nf" | |
88 | # include "llbasic.h" | |
89 | # include "cpp.h" | |
90 | # include "cpplib.h" | |
91 | # include "cpperror.h" | |
92 | ||
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); | |
97 | ||
98 | /* Print the file names and line numbers of the #include | |
99 | commands which led to the current file. */ | |
100 | ||
101 | static void cppReader_printContainingFiles (cppReader *pfile) | |
102 | { | |
103 | cppBuffer *ip; | |
104 | int first = 1; | |
105 | ||
106 | if (pfile == NULL) { | |
107 | /* Error processing command line. */ | |
108 | return; | |
109 | } | |
110 | ||
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) | |
114 | { | |
115 | return; | |
116 | } | |
117 | ||
118 | ip = cppReader_fileBuffer (pfile); | |
119 | ||
120 | /* Give up if we don't find a source file. */ | |
121 | if (ip == NULL) | |
122 | { | |
123 | return; | |
124 | } | |
125 | ||
126 | /* Find the other, outer source files. */ | |
127 | while ((ip = cppBuffer_prevBuffer (ip)), | |
128 | ip != cppReader_nullBuffer (pfile)) | |
129 | { | |
130 | int line, col; | |
131 | cstring temps; | |
132 | ||
133 | cppBuffer_lineAndColumn (ip, &line, &col); | |
134 | if (ip->fname != NULL) | |
135 | { | |
136 | if (first) | |
137 | { | |
138 | first = 0; | |
139 | fprintf (stderr, "In file included"); | |
140 | } | |
141 | else | |
142 | fprintf (stderr, ",\n "); | |
143 | } | |
144 | ||
145 | fprintf (stderr, " from %s", | |
146 | cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line))); | |
147 | ||
148 | cstring_free (temps); | |
149 | } | |
150 | ||
151 | if (!first) | |
152 | { | |
153 | fprintf (stderr, ":\n"); | |
154 | } | |
155 | ||
156 | /* Record we have printed the status as of this time. */ | |
157 | pfile->input_stack_listing_current = 1; | |
158 | } | |
159 | ||
160 | static void | |
161 | cppReader_fileLineForMessage (cstring filename, long line, long column) | |
162 | { | |
163 | if (column > 0) | |
164 | { | |
165 | cstring temps; | |
166 | ||
167 | if (filename != NULL) | |
168 | { | |
169 | fprintf (stderr, "%s: ", | |
170 | cstring_toCharsSafe (temps = fileloc_unparseRawCol (filename, (int) line, (int) column))); | |
171 | } | |
172 | else | |
173 | { | |
174 | fprintf (stderr, "%s: ", | |
175 | cstring_toCharsSafe (temps = fileloc_unparseRawCol (cstring_makeLiteralTemp ("<no file>"), | |
176 | (int) line, (int) column))); | |
177 | } | |
178 | ||
179 | cstring_free (temps); | |
180 | } | |
181 | else | |
182 | { | |
183 | cstring temps; | |
184 | ||
185 | if (filename != NULL) | |
186 | { | |
187 | fprintf (stderr, "%s: ", | |
188 | cstring_toCharsSafe (temps = fileloc_unparseRaw (filename, (int) line))); | |
189 | ||
190 | } | |
191 | else | |
192 | { | |
193 | fprintf (stderr, "%s: ", | |
194 | cstring_toCharsSafe (temps = fileloc_unparseRaw (cstring_makeLiteralTemp ("<no file>"), | |
195 | (int) line))); | |
196 | } | |
197 | ||
198 | cstring_free (temps); | |
199 | } | |
200 | } | |
201 | ||
202 | /* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */ | |
203 | ||
204 | static void | |
205 | cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg) | |
206 | { | |
207 | if (!is_error) | |
208 | { | |
209 | /* fprintf (stderr, "warning: "); */ | |
210 | } | |
211 | else if (is_error == 2) | |
212 | { | |
213 | pfile->errors = cppReader_fatalErrorLimit; | |
214 | } | |
215 | else if (pfile->errors < cppReader_fatalErrorLimit) | |
216 | { | |
217 | pfile->errors++; | |
218 | } | |
219 | else | |
220 | { | |
221 | ; | |
222 | } | |
223 | ||
224 | fprintf (stderr, "%s", cstring_toCharsSafe (msg)); | |
225 | fprintf (stderr, "\n"); | |
226 | } | |
227 | ||
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 | |
232 | cppFatalErrors. */ | |
233 | ||
234 | void | |
235 | cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str) | |
236 | { | |
237 | fprintf (stderr, "preprocessor: "); | |
238 | cppReader_message (pfile, 2, str); | |
239 | } | |
240 | ||
241 | void | |
242 | cppReader_pfatalWithName (cppReader *pfile, cstring name) | |
243 | { | |
244 | cppReader_perrorWithName (pfile, name); | |
245 | exit (FATAL_EXIT_CODE); | |
246 | } | |
247 | ||
248 | void | |
249 | cppReader_printFileAndLine (cppReader *pfile) | |
250 | { | |
251 | cppBuffer *ip = cppReader_fileBuffer (pfile); | |
252 | ||
253 | if (ip != NULL) | |
254 | { | |
255 | int line, col; | |
256 | ||
257 | cppBuffer_lineAndColumn (ip, &line, &col); | |
258 | cppReader_fileLineForMessage (ip->nominal_fname, | |
259 | line, pfile->show_column ? col : -1); | |
260 | } | |
261 | else | |
262 | { | |
263 | fprintf (stderr, "Command Line: "); | |
264 | } | |
265 | } | |
266 | ||
267 | void | |
268 | cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg) | |
269 | { | |
270 | cppReader_error (pfile, cstring_copy (msg)); | |
271 | } | |
272 | ||
273 | void | |
274 | cppReader_error (cppReader *pfile, /*@only@*/ cstring msg) | |
275 | { | |
276 | prepareMessage (); | |
277 | cppReader_printContainingFiles (pfile); | |
278 | cppReader_printFileAndLine (pfile); | |
279 | cppReader_message (pfile, 1, msg); | |
280 | closeMessage (); | |
281 | } | |
282 | ||
283 | /* Print error message but don't count it. */ | |
284 | ||
285 | void | |
286 | cppReader_warningLit (cppReader *pfile, cstring msg) | |
287 | { | |
288 | cppReader_warning (pfile, cstring_copy (msg)); | |
289 | } | |
290 | ||
291 | void | |
292 | cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg) | |
293 | { | |
294 | if (CPPOPTIONS (pfile)->inhibit_warnings) | |
295 | return; | |
296 | ||
297 | if (CPPOPTIONS (pfile)->warnings_are_errors) | |
298 | pfile->errors++; | |
299 | ||
300 | prepareMessage (); | |
301 | cppReader_printContainingFiles (pfile); | |
302 | cppReader_printFileAndLine (pfile); | |
303 | cppReader_message (pfile, 0, msg); | |
304 | closeMessage (); | |
305 | } | |
306 | ||
307 | /* Print an error message and maybe count it. */ | |
308 | ||
309 | extern void | |
310 | cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg) | |
311 | { | |
312 | cppReader_pedwarn (pfile, cstring_copy (msg)); | |
313 | } | |
314 | ||
315 | extern void | |
316 | cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg) | |
317 | { | |
318 | if (CPPOPTIONS (pfile)->pedantic_errors) | |
319 | { | |
320 | cppReader_error (pfile, msg); | |
321 | } | |
322 | else | |
323 | { | |
324 | cppReader_warning (pfile, msg); | |
325 | } | |
326 | } | |
327 | ||
328 | void | |
329 | cppReader_errorWithLine (cppReader *pfile, long line, long column, | |
330 | /*@only@*/ cstring msg) | |
331 | { | |
332 | cppBuffer *ip = cppReader_fileBuffer (pfile); | |
333 | ||
334 | prepareMessage (); | |
335 | cppReader_printContainingFiles (pfile); | |
336 | ||
337 | if (ip != NULL) | |
338 | cppReader_fileLineForMessage (ip->nominal_fname, line, column); | |
339 | ||
340 | cppReader_message (pfile, 1, msg); | |
341 | closeMessage (); | |
342 | } | |
343 | ||
344 | void | |
345 | cppReader_warningWithLine (cppReader *pfile, | |
346 | int line, int column, | |
347 | /*@only@*/ cstring msg) | |
348 | { | |
349 | cppBuffer *ip; | |
350 | ||
351 | if (CPPOPTIONS (pfile)->inhibit_warnings) | |
352 | return; | |
353 | ||
354 | if (CPPOPTIONS (pfile)->warnings_are_errors) | |
355 | pfile->errors++; | |
356 | ||
357 | prepareMessage (); | |
358 | ||
359 | cppReader_printContainingFiles (pfile); | |
360 | ||
361 | ip = cppReader_fileBuffer (pfile); | |
362 | ||
363 | if (ip != NULL) | |
364 | { | |
365 | cppReader_fileLineForMessage (ip->nominal_fname, line, column); | |
366 | } | |
367 | ||
368 | cppReader_message (pfile, 0, msg); | |
369 | closeMessage (); | |
370 | } | |
371 | ||
372 | void | |
373 | cppReader_pedwarnWithLine (cppReader *pfile, int line, int column, | |
374 | /*@only@*/ cstring msg) | |
375 | { | |
376 | if (CPPOPTIONS (pfile)->pedantic_errors) | |
377 | { | |
378 | cppReader_errorWithLine (pfile, column, line, msg); | |
379 | } | |
380 | else | |
381 | { | |
382 | cppReader_warningWithLine (pfile, line, column, msg); | |
383 | } | |
384 | } | |
385 | ||
386 | void cppReader_perrorWithName (cppReader *pfile, cstring name) | |
387 | { | |
388 | cppReader_message (pfile, 1, | |
28bf4b0b | 389 | message ("Preprocessing error for %s: %s", |
616915dd | 390 | name, lldecodeerror (errno))); |
391 | } | |
392 | ||
393 | ||
394 | ||
395 | ||
396 | ||
397 | ||
398 | ||
399 |