2010-11-27 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / libcpp / errors.c
blobb7783f54d31ef09ff41db98aab88897dda35f139
1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
3 2001, 2002, 2004, 2008, 2009 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
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 3, or (at your option) any
11 later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>.
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! */
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "internal.h"
31 /* Print a diagnostic at the location of the previously lexed token. */
33 ATTRIBUTE_FPTR_PRINTF(4,0)
34 static bool
35 cpp_diagnostic (cpp_reader * pfile, int level, int reason,
36 const char *msgid, va_list *ap)
38 source_location src_loc;
39 bool ret;
41 if (CPP_OPTION (pfile, traditional))
43 if (pfile->state.in_directive)
44 src_loc = pfile->directive_line;
45 else
46 src_loc = pfile->line_table->highest_line;
48 /* We don't want to refer to a token before the beginning of the
49 current run -- that is invalid. */
50 else if (pfile->cur_token == pfile->cur_run->base)
52 if (pfile->cur_run->prev != NULL)
53 src_loc = pfile->cur_run->prev->limit->src_loc;
54 else
55 src_loc = 0;
57 else
59 src_loc = pfile->cur_token[-1].src_loc;
62 if (!pfile->cb.error)
63 abort ();
64 ret = pfile->cb.error (pfile, level, reason, src_loc, 0, _(msgid), ap);
66 return ret;
69 /* Print a warning or error, depending on the value of LEVEL. */
71 bool
72 cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
74 va_list ap;
75 bool ret;
77 va_start (ap, msgid);
79 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
81 va_end (ap);
82 return ret;
85 /* Print a warning. The warning reason may be given in REASON. */
87 bool
88 cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...)
90 va_list ap;
91 bool ret;
93 va_start (ap, msgid);
95 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
97 va_end (ap);
98 return ret;
101 /* Print a pedantic warning. The warning reason may be given in REASON. */
103 bool
104 cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...)
106 va_list ap;
107 bool ret;
109 va_start (ap, msgid);
111 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
113 va_end (ap);
114 return ret;
117 /* Print a warning, including system headers. The warning reason may be
118 given in REASON. */
120 bool
121 cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...)
123 va_list ap;
124 bool ret;
126 va_start (ap, msgid);
128 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
130 va_end (ap);
131 return ret;
134 /* Print a diagnostic at a specific location. */
136 ATTRIBUTE_FPTR_PRINTF(6,0)
137 static bool
138 cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason,
139 source_location src_loc, unsigned int column,
140 const char *msgid, va_list *ap)
142 bool ret;
144 if (!pfile->cb.error)
145 abort ();
146 ret = pfile->cb.error (pfile, level, reason, src_loc, column, _(msgid), ap);
148 return ret;
151 /* Print a warning or error, depending on the value of LEVEL. */
153 bool
154 cpp_error_with_line (cpp_reader *pfile, int level,
155 source_location src_loc, unsigned int column,
156 const char *msgid, ...)
158 va_list ap;
159 bool ret;
161 va_start (ap, msgid);
163 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
164 column, msgid, &ap);
166 va_end (ap);
167 return ret;
170 /* Print a warning. The warning reason may be given in REASON. */
172 bool
173 cpp_warning_with_line (cpp_reader *pfile, int reason,
174 source_location src_loc, unsigned int column,
175 const char *msgid, ...)
177 va_list ap;
178 bool ret;
180 va_start (ap, msgid);
182 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
183 column, msgid, &ap);
185 va_end (ap);
186 return ret;
189 /* Print a pedantic warning. The warning reason may be given in REASON. */
191 bool
192 cpp_pedwarning_with_line (cpp_reader *pfile, int reason,
193 source_location src_loc, unsigned int column,
194 const char *msgid, ...)
196 va_list ap;
197 bool ret;
199 va_start (ap, msgid);
201 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
202 column, msgid, &ap);
204 va_end (ap);
205 return ret;
208 /* Print a warning, including system headers. The warning reason may be
209 given in REASON. */
211 bool
212 cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason,
213 source_location src_loc, unsigned int column,
214 const char *msgid, ...)
216 va_list ap;
217 bool ret;
219 va_start (ap, msgid);
221 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
222 column, msgid, &ap);
224 va_end (ap);
225 return ret;
228 /* Print a warning or error, depending on the value of LEVEL. Include
229 information from errno. */
231 bool
232 cpp_errno (cpp_reader *pfile, int level, const char *msgid)
234 if (msgid[0] == '\0')
235 msgid = _("stdout");
237 return cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));