OSDN Git Service

Support procedures for testing profile-directed optimizations.
[pf3gnuchains/gcc-fork.git] / gcc / cpperror.c
1 /* Default error handlers for CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000
3    Free Software Foundation, Inc.
4    Written by Per Bothner, 1994.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22  In other words, you are welcome to use, share and improve this program.
23  You are forbidden to forbid anyone else to use, share and improve
24  what you give them.   Help stamp out software-hoarding!  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "intl.h"
31
32 static void print_location              PARAMS ((cpp_reader *,
33                                                  const cpp_lexer_pos *));
34
35 /* Don't remove the blank before do, as otherwise the exgettext
36    script will mistake this as a function definition */
37 #define v_message(msgid, ap) \
38  do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
39
40 static void
41 print_location (pfile, pos)
42      cpp_reader *pfile;
43      const cpp_lexer_pos *pos;
44 {
45   cpp_buffer *buffer = pfile->buffer;
46
47   if (!buffer)
48     fprintf (stderr, "%s: ", progname);
49   else
50     {
51       unsigned int line, col;
52       const struct line_map *map;
53
54       if (pos == 0)
55         pos = cpp_get_line (pfile);
56       map = lookup_line (&pfile->line_maps, pos->line);
57
58       print_containing_files (&pfile->line_maps, map);
59
60       line = SOURCE_LINE (map, pos->line);
61       col = pos->col;
62       if (col == 0)
63         col = 1;
64
65       if (line == 0)
66         fprintf (stderr, "%s:", map->to_file);
67       else if (CPP_OPTION (pfile, show_column) == 0)
68         fprintf (stderr, "%s:%u:", map->to_file, line);
69       else
70         fprintf (stderr, "%s:%u:%u:", map->to_file, line, col);
71
72       fputc (' ', stderr);
73     }
74 }
75
76 /* Set up for an error message: print the file and line, bump the error
77    counter, etc.
78    If it returns 0, this error has been suppressed.  */
79
80 int
81 _cpp_begin_message (pfile, code, pos)
82      cpp_reader *pfile;
83      enum error_type code;
84      const cpp_lexer_pos *pos;
85 {
86   int is_warning = 0;
87
88   switch (code)
89     {
90     case PEDWARN:
91     case WARNING:
92       if (CPP_IN_SYSTEM_HEADER (pfile)
93           && ! CPP_OPTION (pfile, warn_system_headers))
94         return 0;
95     case WARNING_SYSHDR:
96       if (CPP_OPTION (pfile, warnings_are_errors)
97           || (code == PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
98         {
99           if (CPP_OPTION (pfile, inhibit_errors))
100             return 0;
101           if (pfile->errors < CPP_FATAL_LIMIT)
102             pfile->errors++;
103         }
104       else
105         {
106           if (CPP_OPTION (pfile, inhibit_warnings))
107             return 0;
108           is_warning = 1;
109         }
110       break;
111         
112     case ERROR:
113       if (CPP_OPTION (pfile, inhibit_errors))
114         return 0;
115       if (pfile->errors < CPP_FATAL_LIMIT)
116         pfile->errors++;
117       break;
118       /* Fatal errors cannot be inhibited.  */
119     case FATAL:
120       pfile->errors = CPP_FATAL_LIMIT;
121       break;
122     case ICE:
123       fprintf (stderr, _("internal error: "));
124       pfile->errors = CPP_FATAL_LIMIT;
125       break;
126     }
127
128   print_location (pfile, pos);
129   if (is_warning)
130     fputs (_("warning: "), stderr);
131
132   return 1;
133 }
134
135 /* Exported interface.  */
136
137 /* For reporting internal errors.  Prints "internal error: " for you,
138    otherwise identical to cpp_fatal.  */
139
140 void
141 cpp_ice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
142 {  
143   VA_OPEN (ap, msgid);
144   VA_FIXEDARG (ap, cpp_reader *, pfile);
145   VA_FIXEDARG (ap, const char *, msgid);
146
147   if (_cpp_begin_message (pfile, ICE, 0))
148     v_message (msgid, ap);
149
150   VA_CLOSE (ap);
151 }
152
153 /* Same as cpp_error, except we consider the error to be "fatal",
154    such as inconsistent options.  I.e. there is little point in continuing.
155    (We do not exit, to support use of cpplib as a library.
156    Instead, it is the caller's responsibility to check
157    CPP_FATAL_ERRORS.  */
158
159 void
160 cpp_fatal VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
161 {  
162   VA_OPEN (ap, msgid);
163   VA_FIXEDARG (ap, cpp_reader *, pfile);
164   VA_FIXEDARG (ap, const char *, msgid);
165
166   if (_cpp_begin_message (pfile, FATAL, 0))
167     v_message (msgid, ap);
168
169   VA_CLOSE (ap);
170 }
171
172 void
173 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
174 {
175   VA_OPEN (ap, msgid);
176   VA_FIXEDARG (ap, cpp_reader *, pfile);
177   VA_FIXEDARG (ap, const char *, msgid);
178
179   if (_cpp_begin_message (pfile, ERROR, 0))
180     v_message (msgid, ap);
181
182   VA_CLOSE (ap);
183 }
184
185 void
186 cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
187                              const char *msgid, ...))
188 {
189   cpp_lexer_pos pos;
190   
191   VA_OPEN (ap, msgid);
192   VA_FIXEDARG (ap, cpp_reader *, pfile);
193   VA_FIXEDARG (ap, int, line);
194   VA_FIXEDARG (ap, int, column);
195   VA_FIXEDARG (ap, const char *, msgid);
196
197   pos.line = line;
198   pos.col = column;
199   if (_cpp_begin_message (pfile, ERROR, &pos))
200     v_message (msgid, ap);
201
202   VA_CLOSE (ap);
203 }
204
205 /* Error including a message from `errno'.  */
206 void
207 cpp_error_from_errno (pfile, name)
208      cpp_reader *pfile;
209      const char *name;
210 {
211   cpp_error (pfile, "%s: %s", name, xstrerror (errno));
212 }
213
214 void
215 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
216 {
217   VA_OPEN (ap, msgid);
218   VA_FIXEDARG (ap, cpp_reader *, pfile);
219   VA_FIXEDARG (ap, const char *, msgid);
220
221   if (_cpp_begin_message (pfile, WARNING, 0))
222     v_message (msgid, ap);
223
224   VA_CLOSE (ap);
225 }
226
227 void
228 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
229                                const char *msgid, ...))
230 {
231   cpp_lexer_pos pos;
232
233   VA_OPEN (ap, msgid);
234   VA_FIXEDARG (ap, cpp_reader *, pfile);
235   VA_FIXEDARG (ap, int, line);
236   VA_FIXEDARG (ap, int, column);
237   VA_FIXEDARG (ap, const char *, msgid);
238
239   pos.line = line;
240   pos.col = column;
241   if (_cpp_begin_message (pfile, WARNING, &pos))
242     v_message (msgid, ap);
243
244   VA_CLOSE (ap);
245 }
246
247 void
248 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
249 {
250   VA_OPEN (ap, msgid);
251   VA_FIXEDARG (ap, cpp_reader *, pfile);
252   VA_FIXEDARG (ap, const char *, msgid);
253
254   if (_cpp_begin_message (pfile, PEDWARN, 0))
255     v_message (msgid, ap);
256
257   VA_CLOSE (ap);
258 }
259
260 void
261 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
262                                const char *msgid, ...))
263 {
264   cpp_lexer_pos pos;
265   
266   VA_OPEN (ap, msgid);
267   VA_FIXEDARG (ap, cpp_reader *, pfile);
268   VA_FIXEDARG (ap, int, line);
269   VA_FIXEDARG (ap, int, column);
270   VA_FIXEDARG (ap, const char *, msgid);
271
272   pos.line = line;
273   pos.col = column;
274   if (_cpp_begin_message (pfile, PEDWARN, &pos))
275     v_message (msgid, ap);
276
277   VA_CLOSE (ap);
278 }
279
280 /* Print an error message not associated with a file.  */
281 void
282 cpp_notice VPARAMS ((cpp_reader *pfile, const char *msgid, ...))
283 {
284   VA_OPEN (ap, msgid);
285   VA_FIXEDARG (ap, cpp_reader *, pfile);
286   VA_FIXEDARG (ap, const char *, msgid);
287
288   if (pfile->errors < CPP_FATAL_LIMIT)
289     pfile->errors++;
290
291   v_message (msgid, ap);
292
293   VA_CLOSE (ap);
294 }
295
296 void
297 cpp_notice_from_errno (pfile, name)
298      cpp_reader *pfile;
299      const char *name;
300 {
301   if (name[0] == '\0')
302     name = "stdout";
303   cpp_notice (pfile, "%s: %s", name, xstrerror (errno));
304 }