Merge trunk version 204119 into gupc branch.
[official-gcc.git] / libcpp / errors.c
blob0a1200d79080b24e04fef9d420c055cf1e9b0faf
1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986-2013 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
30 /* Print a diagnostic at the location of the previously lexed token. */
32 ATTRIBUTE_FPTR_PRINTF(4,0)
33 static bool
34 cpp_diagnostic (cpp_reader * pfile, int level, int reason,
35 const char *msgid, va_list *ap)
37 source_location src_loc;
38 bool ret;
40 if (CPP_OPTION (pfile, traditional))
42 if (pfile->state.in_directive)
43 src_loc = pfile->directive_line;
44 else
45 src_loc = pfile->line_table->highest_line;
47 /* We don't want to refer to a token before the beginning of the
48 current run -- that is invalid. */
49 else if (pfile->cur_token == pfile->cur_run->base)
51 if (pfile->cur_run->prev != NULL)
52 src_loc = pfile->cur_run->prev->limit->src_loc;
53 else
54 src_loc = 0;
56 else
58 src_loc = pfile->cur_token[-1].src_loc;
61 if (!pfile->cb.error)
62 abort ();
63 ret = pfile->cb.error (pfile, level, reason, src_loc, 0, _(msgid), ap);
65 return ret;
68 /* Print a warning or error, depending on the value of LEVEL. */
70 bool
71 cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
73 va_list ap;
74 bool ret;
76 va_start (ap, msgid);
78 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
80 va_end (ap);
81 return ret;
84 /* Print a warning. The warning reason may be given in REASON. */
86 bool
87 cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...)
89 va_list ap;
90 bool ret;
92 va_start (ap, msgid);
94 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
96 va_end (ap);
97 return ret;
100 /* Print a pedantic warning. The warning reason may be given in REASON. */
102 bool
103 cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...)
105 va_list ap;
106 bool ret;
108 va_start (ap, msgid);
110 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
112 va_end (ap);
113 return ret;
116 /* Print a warning, including system headers. The warning reason may be
117 given in REASON. */
119 bool
120 cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...)
122 va_list ap;
123 bool ret;
125 va_start (ap, msgid);
127 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
129 va_end (ap);
130 return ret;
133 /* Print a diagnostic at a specific location. */
135 ATTRIBUTE_FPTR_PRINTF(6,0)
136 static bool
137 cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason,
138 source_location src_loc, unsigned int column,
139 const char *msgid, va_list *ap)
141 bool ret;
143 if (!pfile->cb.error)
144 abort ();
145 ret = pfile->cb.error (pfile, level, reason, src_loc, column, _(msgid), ap);
147 return ret;
150 /* Print a warning or error, depending on the value of LEVEL. */
152 bool
153 cpp_error_with_line (cpp_reader *pfile, int level,
154 source_location src_loc, unsigned int column,
155 const char *msgid, ...)
157 va_list ap;
158 bool ret;
160 va_start (ap, msgid);
162 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
163 column, msgid, &ap);
165 va_end (ap);
166 return ret;
169 /* Print a warning. The warning reason may be given in REASON. */
171 bool
172 cpp_warning_with_line (cpp_reader *pfile, int reason,
173 source_location src_loc, unsigned int column,
174 const char *msgid, ...)
176 va_list ap;
177 bool ret;
179 va_start (ap, msgid);
181 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
182 column, msgid, &ap);
184 va_end (ap);
185 return ret;
188 /* Print a pedantic warning. The warning reason may be given in REASON. */
190 bool
191 cpp_pedwarning_with_line (cpp_reader *pfile, int reason,
192 source_location src_loc, unsigned int column,
193 const char *msgid, ...)
195 va_list ap;
196 bool ret;
198 va_start (ap, msgid);
200 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
201 column, msgid, &ap);
203 va_end (ap);
204 return ret;
207 /* Print a warning, including system headers. The warning reason may be
208 given in REASON. */
210 bool
211 cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason,
212 source_location src_loc, unsigned int column,
213 const char *msgid, ...)
215 va_list ap;
216 bool ret;
218 va_start (ap, msgid);
220 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
221 column, msgid, &ap);
223 va_end (ap);
224 return ret;
227 /* Print a warning or error, depending on the value of LEVEL. Include
228 information from errno. */
230 bool
231 cpp_errno (cpp_reader *pfile, int level, const char *msgid)
233 if (msgid[0] == '\0')
234 msgid = _("stdout");
236 return cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));