]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/errors.c
Update copyright years.
[thirdparty/gcc.git] / libcpp / errors.c
CommitLineData
7f2935c7 1/* Default error handlers for CPP Library.
85ec4feb 2 Copyright (C) 1986-2018 Free Software Foundation, Inc.
7f2935c7 3 Written by Per Bothner, 1994.
38e01259 4 Based on CCCP program by Paul Rubin, June 1986
7f2935c7
PB
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6
7This program is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
748086b7 9Free Software Foundation; either version 3, or (at your option) any
7f2935c7
PB
10later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
748086b7
JJ
18along with this program; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>.
7f2935c7
PB
20
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! */
24
84ee6fd4 25#include "config.h"
b04cd507 26#include "system.h"
f32da1f6 27#include "cpplib.h"
4f4e53dd 28#include "internal.h"
f32da1f6 29
ac81cf0b
DM
30/* Print a diagnostic at the given location. */
31
32ATTRIBUTE_FPTR_PRINTF(5,0)
33static bool
34cpp_diagnostic_at (cpp_reader * pfile, int level, int reason,
64a5912c 35 rich_location *richloc,
ac81cf0b
DM
36 const char *msgid, va_list *ap)
37{
38 bool ret;
39
40 if (!pfile->cb.error)
41 abort ();
64a5912c 42 ret = pfile->cb.error (pfile, level, reason, richloc, _(msgid), ap);
ac81cf0b
DM
43
44 return ret;
45}
46
87cf0651
SB
47/* Print a diagnostic at the location of the previously lexed token. */
48
49ATTRIBUTE_FPTR_PRINTF(4,0)
50static bool
51cpp_diagnostic (cpp_reader * pfile, int level, int reason,
52 const char *msgid, va_list *ap)
c1212d2f 53{
12f9df4e 54 source_location src_loc;
148e4216 55
148e4216 56 if (CPP_OPTION (pfile, traditional))
75ee800b 57 {
148e4216
JM
58 if (pfile->state.in_directive)
59 src_loc = pfile->directive_line;
178b58b5 60 else
148e4216
JM
61 src_loc = pfile->line_table->highest_line;
62 }
63 /* We don't want to refer to a token before the beginning of the
64 current run -- that is invalid. */
65 else if (pfile->cur_token == pfile->cur_run->base)
66 {
cc811a8a 67 src_loc = 0;
178b58b5 68 }
148e4216
JM
69 else
70 {
71 src_loc = pfile->cur_token[-1].src_loc;
72 }
64a5912c
DM
73 rich_location richloc (pfile->line_table, src_loc);
74 return cpp_diagnostic_at (pfile, level, reason, &richloc, msgid, ap);
87cf0651
SB
75}
76
77/* Print a warning or error, depending on the value of LEVEL. */
78
79bool
80cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
81{
82 va_list ap;
83 bool ret;
84
85 va_start (ap, msgid);
86
87 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
b649398a 88
e34d07f2 89 va_end (ap);
148e4216 90 return ret;
c1212d2f
ZW
91}
92
87cf0651
SB
93/* Print a warning. The warning reason may be given in REASON. */
94
95bool
96cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...)
97{
98 va_list ap;
99 bool ret;
100
101 va_start (ap, msgid);
102
103 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
104
105 va_end (ap);
106 return ret;
107}
108
109/* Print a pedantic warning. The warning reason may be given in REASON. */
110
111bool
112cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...)
113{
114 va_list ap;
115 bool ret;
116
117 va_start (ap, msgid);
118
119 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
120
121 va_end (ap);
122 return ret;
123}
124
125/* Print a warning, including system headers. The warning reason may be
126 given in REASON. */
127
128bool
129cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...)
130{
131 va_list ap;
132 bool ret;
133
134 va_start (ap, msgid);
135
136 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
137
138 va_end (ap);
139 return ret;
140}
141
142/* Print a diagnostic at a specific location. */
143
144ATTRIBUTE_FPTR_PRINTF(6,0)
145static bool
146cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason,
147 source_location src_loc, unsigned int column,
148 const char *msgid, va_list *ap)
149{
150 bool ret;
151
152 if (!pfile->cb.error)
153 abort ();
ebedc9a3 154 rich_location richloc (pfile->line_table, src_loc);
44714d8c
DM
155 if (column)
156 richloc.override_column (column);
8a645150 157 ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap);
87cf0651
SB
158
159 return ret;
160}
161
162/* Print a warning or error, depending on the value of LEVEL. */
163
148e4216 164bool
e34d07f2 165cpp_error_with_line (cpp_reader *pfile, int level,
12f9df4e 166 source_location src_loc, unsigned int column,
e34d07f2 167 const char *msgid, ...)
c1212d2f 168{
e34d07f2 169 va_list ap;
148e4216 170 bool ret;
87cf0651 171
e34d07f2 172 va_start (ap, msgid);
ab87f8c8 173
87cf0651
SB
174 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
175 column, msgid, &ap);
176
177 va_end (ap);
178 return ret;
179}
180
181/* Print a warning. The warning reason may be given in REASON. */
182
183bool
184cpp_warning_with_line (cpp_reader *pfile, int reason,
185 source_location src_loc, unsigned int column,
186 const char *msgid, ...)
187{
188 va_list ap;
189 bool ret;
190
191 va_start (ap, msgid);
192
193 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
194 column, msgid, &ap);
195
196 va_end (ap);
197 return ret;
198}
199
200/* Print a pedantic warning. The warning reason may be given in REASON. */
201
202bool
203cpp_pedwarning_with_line (cpp_reader *pfile, int reason,
204 source_location src_loc, unsigned int column,
205 const char *msgid, ...)
206{
207 va_list ap;
208 bool ret;
209
210 va_start (ap, msgid);
211
212 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
213 column, msgid, &ap);
b649398a 214
e34d07f2 215 va_end (ap);
148e4216 216 return ret;
c1212d2f
ZW
217}
218
87cf0651
SB
219/* Print a warning, including system headers. The warning reason may be
220 given in REASON. */
221
222bool
223cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason,
224 source_location src_loc, unsigned int column,
225 const char *msgid, ...)
226{
227 va_list ap;
228 bool ret;
229
230 va_start (ap, msgid);
231
232 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
233 column, msgid, &ap);
234
235 va_end (ap);
236 return ret;
237}
238
ac81cf0b
DM
239/* As cpp_error, but use SRC_LOC as the location of the error, without
240 a column override. */
241
242bool
243cpp_error_at (cpp_reader * pfile, int level, source_location src_loc,
244 const char *msgid, ...)
245{
246 va_list ap;
247 bool ret;
248
249 va_start (ap, msgid);
250
64a5912c
DM
251 rich_location richloc (pfile->line_table, src_loc);
252 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, &richloc,
ac81cf0b
DM
253 msgid, &ap);
254
255 va_end (ap);
256 return ret;
257}
258
cb18fd07
DM
259/* As cpp_error, but use RICHLOC as the location of the error, without
260 a column override. */
261
262bool
64a5912c
DM
263cpp_error_at (cpp_reader * pfile, int level, rich_location *richloc,
264 const char *msgid, ...)
cb18fd07
DM
265{
266 va_list ap;
267 bool ret;
268
269 va_start (ap, msgid);
270
64a5912c
DM
271 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, richloc,
272 msgid, &ap);
cb18fd07
DM
273
274 va_end (ap);
275 return ret;
276}
277
87cf0651
SB
278/* Print a warning or error, depending on the value of LEVEL. Include
279 information from errno. */
280
148e4216 281bool
6cf87ca4 282cpp_errno (cpp_reader *pfile, int level, const char *msgid)
c1212d2f 283{
46ce03de
JJ
284 return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno));
285}
286
287/* Print a warning or error, depending on the value of LEVEL. Include
288 information from errno. Unlike cpp_errno, the argument is a filename
289 that is not localized, but "" is replaced with localized "stdout". */
290
291bool
ac81cf0b
DM
292cpp_errno_filename (cpp_reader *pfile, int level, const char *filename,
293 source_location loc)
46ce03de
JJ
294{
295 if (filename[0] == '\0')
296 filename = _("stdout");
ebef4e8c 297
ac81cf0b
DM
298 return cpp_error_at (pfile, level, loc, "%s: %s", filename,
299 xstrerror (errno));
c1212d2f 300}