]> andersk Git - splint.git/blob - src/cpperror.c
*** empty log message ***
[splint.git] / src / cpperror.c
1 /*
2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 University of Virginia,
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 "cpplib.h"
90 # include "cpperror.h"
91
92 static void cppReader_printFileAndLine (cppReader *p_pfile);
93 static 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
100 void cppReader_printContainingFiles (cppReader *pfile)
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       
132       cppBuffer_lineAndColumn (ip, &line, &col);
133       if (ip->fname != NULL)
134         {
135           if (first)
136             {
137               first = 0;
138               fprintf (g_msgstream, "   In file included");
139             }
140           else
141             fprintf (g_msgstream, ",\n                ");
142         }
143
144       fprintf (g_msgstream, " from %s", 
145                cstring_toCharsSafe (temps = fileloc_unparseRaw (ip->nominal_fname, line)));
146       
147       cstring_free (temps);
148     }
149   
150   if (!first)
151     {
152       fprintf (g_msgstream, "\n");
153     }
154
155   /* Record we have printed the status as of this time.  */
156   pfile->input_stack_listing_current = 1;
157 }
158
159 static void
160 cppReader_fileLineForMessage (cstring filename, long line, long column)
161 {
162   if (column > 0)
163     {
164       cstring temps;
165
166       if (filename != NULL)
167         {
168           fprintf (stderr, "%s: ",
169                    cstring_toCharsSafe (temps = fileloc_unparseRawCol (filename, (int) line, (int) column)));
170         }
171       else 
172         {
173           fprintf (stderr, "%s: ",
174                    cstring_toCharsSafe (temps = fileloc_unparseRawCol (cstring_makeLiteralTemp ("<no file>"),
175                                                                        (int) line, (int) column)));
176         }
177
178       cstring_free (temps);
179     }
180   else
181     {
182       cstring temps;
183
184       if (filename != NULL) 
185         {
186           fprintf (stderr, "%s: ", 
187                    cstring_toCharsSafe (temps = fileloc_unparseRaw (filename, (int) line)));
188           
189         }
190       else
191         {
192           fprintf (stderr, "%s: ",
193                    cstring_toCharsSafe (temps = fileloc_unparseRaw (cstring_makeLiteralTemp ("<no file>"),
194                                                                     (int) line)));
195         }
196
197       cstring_free (temps);
198     }
199 }
200
201 /* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
202
203 static void
204 cppReader_message (cppReader *pfile, int is_error, /*@only@*/ cstring msg)
205 {
206   if (!is_error)
207     {
208       /* fprintf (stderr, "warning: "); */
209     }
210   else if (is_error == 2)
211     {
212       pfile->errors = cppReader_fatalErrorLimit;
213     }
214   else if (pfile->errors < cppReader_fatalErrorLimit)
215     {
216       pfile->errors++;
217     }
218   else
219     {
220       ;
221     }
222
223   fprintf (stderr, "%s", cstring_toCharsSafe (msg));
224   fprintf (stderr, "\n");
225 }
226
227 /* Same as cppReader_error, except we consider the error to be "fatal",
228    such as inconsistent options.  I.e. there is little point in continuing.
229    (We do not exit, to support use of cpplib as a library.
230    Instead, it is the caller's responsibility to check
231    cppFatalErrors.  */
232
233 void
234 cppReader_fatalError (cppReader *pfile, /*@only@*/ cstring str)
235 {
236   fprintf (stderr, "preprocessor: ");
237   cppReader_message (pfile, 2, str);
238 }
239
240 void
241 cppReader_pfatalWithName (cppReader *pfile, cstring name)
242 {
243   cppReader_perrorWithName (pfile, name);
244   exit (FATAL_EXIT_CODE);
245 }
246
247 /*@only@*/ fileloc
248 cppReader_getLoc (cppReader *pfile)
249 {
250   cppBuffer *ip = cppReader_fileBuffer (pfile);
251
252   if (ip != NULL)
253     {
254       int line, col;
255       cstring fname = ip->nominal_fname;
256       fileId fid = fileTable_lookup (context_fileTable (), fname);
257
258       llassert (fileId_isValid (fid));
259           
260       cppBuffer_lineAndColumn (ip, &line, &col);
261       
262       return fileloc_create (fid, line, col);
263     }
264   else
265     {
266       return fileloc_createBuiltin ();
267     }
268 }
269
270 void
271 cppReader_printFileAndLine (cppReader *pfile)
272 {
273   cppBuffer *ip = cppReader_fileBuffer (pfile);
274
275   if (ip != NULL)
276     {
277       int line, col;
278
279       cppBuffer_lineAndColumn (ip, &line, &col);
280       cppReader_fileLineForMessage (ip->nominal_fname,
281                                     line, pfile->show_column ? col : -1);
282     }
283   else
284     {
285       fprintf (stderr, "Command Line: ");
286     }
287 }
288
289 void
290 cppReader_errorLit (cppReader *pfile, /*@observer@*/ cstring msg)
291 {
292   cppReader_error (pfile, cstring_copy (msg));
293 }
294
295 void
296 cppReader_error (cppReader *pfile, /*@only@*/ cstring msg)
297 {
298   prepareMessage ();
299   cppReader_printContainingFiles (pfile);
300   cppReader_printFileAndLine (pfile);
301   cppReader_message (pfile, 1, msg);
302   closeMessage ();
303 }
304
305 /* Print error message but don't count it.  */
306
307 void
308 cppReader_warningLit (cppReader *pfile, cstring msg)
309 {
310   cppReader_warning (pfile, cstring_copy (msg));
311 }
312
313 void
314 cppReader_warning (cppReader *pfile, /*@only@*/ cstring msg)
315 {
316   if (CPPOPTIONS (pfile)->warnings_are_errors)
317     pfile->errors++;
318   
319   prepareMessage ();
320   cppReader_printContainingFiles (pfile);
321   cppReader_printFileAndLine (pfile);
322   cppReader_message (pfile, 0, msg);
323   closeMessage ();
324 }
325
326 /* Print an error message and maybe count it.  */
327
328 extern void
329 cppReader_pedwarnLit (cppReader *pfile, /*@observer@*/ cstring msg)
330 {
331   cppReader_pedwarn (pfile, cstring_copy (msg));
332 }
333
334 extern void
335 cppReader_pedwarn (cppReader *pfile, /*@only@*/ cstring msg)
336 {
337   if (CPPOPTIONS (pfile)->pedantic_errors)
338     {
339       cppReader_error (pfile, msg);
340     }
341   else
342     {
343       cppReader_warning (pfile, msg);
344     }
345 }
346
347 void
348 cppReader_errorWithLine (cppReader *pfile, long line, long column, 
349                      /*@only@*/ cstring msg)
350 {
351   cppBuffer *ip = cppReader_fileBuffer (pfile);
352
353   prepareMessage ();
354   cppReader_printContainingFiles (pfile);
355
356   if (ip != NULL)
357     cppReader_fileLineForMessage (ip->nominal_fname, line, column);
358
359   cppReader_message (pfile, 1, msg);
360   closeMessage ();
361 }
362
363 void
364 cppReader_warningWithLine (cppReader *pfile, 
365                        int line, int column, 
366                        /*@only@*/ cstring msg)
367 {
368   cppBuffer *ip;
369
370   if (CPPOPTIONS (pfile)->warnings_are_errors)
371     pfile->errors++;
372
373   prepareMessage ();
374
375   cppReader_printContainingFiles (pfile);
376
377   ip = cppReader_fileBuffer (pfile);
378
379   if (ip != NULL)
380     {
381       cppReader_fileLineForMessage (ip->nominal_fname, line, column);
382     }
383
384   cppReader_message (pfile, 0, msg);
385   closeMessage ();
386 }
387
388 void
389 cppReader_pedwarnWithLine (cppReader *pfile, int line, int column,
390                        /*@only@*/ cstring msg)
391 {
392   if (CPPOPTIONS (pfile)->pedantic_errors)
393     {
394       cppReader_errorWithLine (pfile, column, line, msg);
395     }
396   else
397     {
398       cppReader_warningWithLine (pfile, line, column, msg);
399     }
400 }
401
402 void cppReader_perrorWithName (cppReader *pfile, cstring name)
403 {
404   cppReader_message (pfile, 1, 
405                      message ("Preprocessing error for %s: %s",
406                               name, lldecodeerror (errno)));
407 }
408
409
410
411
412
413
414
415
This page took 0.067513 seconds and 5 git commands to generate.