]> 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.
99dee823 2 Copyright (C) 1986-2021 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
c24300ba
DM
34cpp_diagnostic_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
35 enum cpp_warning_reason reason, rich_location *richloc,
ac81cf0b
DM
36 const char *msgid, va_list *ap)
37{
38 bool ret;
39
c24300ba 40 if (!pfile->cb.diagnostic)
ac81cf0b 41 abort ();
c24300ba 42 ret = pfile->cb.diagnostic (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
c24300ba
DM
51cpp_diagnostic (cpp_reader * pfile, enum cpp_diagnostic_level level,
52 enum cpp_warning_reason reason,
53 const char *msgid, va_list *ap)
c1212d2f 54{
620e594b 55 location_t src_loc;
148e4216 56
148e4216 57 if (CPP_OPTION (pfile, traditional))
75ee800b 58 {
148e4216
JM
59 if (pfile->state.in_directive)
60 src_loc = pfile->directive_line;
178b58b5 61 else
148e4216
JM
62 src_loc = pfile->line_table->highest_line;
63 }
64 /* We don't want to refer to a token before the beginning of the
65 current run -- that is invalid. */
66 else if (pfile->cur_token == pfile->cur_run->base)
67 {
cc811a8a 68 src_loc = 0;
178b58b5 69 }
148e4216
JM
70 else
71 {
72 src_loc = pfile->cur_token[-1].src_loc;
73 }
64a5912c
DM
74 rich_location richloc (pfile->line_table, src_loc);
75 return cpp_diagnostic_at (pfile, level, reason, &richloc, msgid, ap);
87cf0651
SB
76}
77
78/* Print a warning or error, depending on the value of LEVEL. */
79
80bool
c24300ba
DM
81cpp_error (cpp_reader * pfile, enum cpp_diagnostic_level level,
82 const char *msgid, ...)
87cf0651
SB
83{
84 va_list ap;
85 bool ret;
86
87 va_start (ap, msgid);
88
89 ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
b649398a 90
e34d07f2 91 va_end (ap);
148e4216 92 return ret;
c1212d2f
ZW
93}
94
87cf0651
SB
95/* Print a warning. The warning reason may be given in REASON. */
96
97bool
c24300ba
DM
98cpp_warning (cpp_reader * pfile, enum cpp_warning_reason reason,
99 const char *msgid, ...)
87cf0651
SB
100{
101 va_list ap;
102 bool ret;
103
104 va_start (ap, msgid);
105
106 ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
107
108 va_end (ap);
109 return ret;
110}
111
112/* Print a pedantic warning. The warning reason may be given in REASON. */
113
114bool
c24300ba
DM
115cpp_pedwarning (cpp_reader * pfile, enum cpp_warning_reason reason,
116 const char *msgid, ...)
87cf0651
SB
117{
118 va_list ap;
119 bool ret;
120
121 va_start (ap, msgid);
122
123 ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
124
125 va_end (ap);
126 return ret;
127}
128
129/* Print a warning, including system headers. The warning reason may be
130 given in REASON. */
131
132bool
c24300ba
DM
133cpp_warning_syshdr (cpp_reader * pfile, enum cpp_warning_reason reason,
134 const char *msgid, ...)
87cf0651
SB
135{
136 va_list ap;
137 bool ret;
138
139 va_start (ap, msgid);
140
141 ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
142
143 va_end (ap);
144 return ret;
145}
146
147/* Print a diagnostic at a specific location. */
148
149ATTRIBUTE_FPTR_PRINTF(6,0)
150static bool
c24300ba
DM
151cpp_diagnostic_with_line (cpp_reader * pfile, enum cpp_diagnostic_level level,
152 enum cpp_warning_reason reason,
620e594b 153 location_t src_loc, unsigned int column,
c24300ba 154 const char *msgid, va_list *ap)
87cf0651
SB
155{
156 bool ret;
157
c24300ba 158 if (!pfile->cb.diagnostic)
87cf0651 159 abort ();
ebedc9a3 160 rich_location richloc (pfile->line_table, src_loc);
44714d8c
DM
161 if (column)
162 richloc.override_column (column);
c24300ba 163 ret = pfile->cb.diagnostic (pfile, level, reason, &richloc, _(msgid), ap);
87cf0651
SB
164
165 return ret;
166}
167
168/* Print a warning or error, depending on the value of LEVEL. */
169
148e4216 170bool
c24300ba 171cpp_error_with_line (cpp_reader *pfile, enum cpp_diagnostic_level level,
620e594b 172 location_t src_loc, unsigned int column,
e34d07f2 173 const char *msgid, ...)
c1212d2f 174{
e34d07f2 175 va_list ap;
148e4216 176 bool ret;
87cf0651 177
e34d07f2 178 va_start (ap, msgid);
ab87f8c8 179
87cf0651
SB
180 ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
181 column, msgid, &ap);
182
183 va_end (ap);
184 return ret;
185}
186
187/* Print a warning. The warning reason may be given in REASON. */
188
189bool
c24300ba 190cpp_warning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason,
620e594b 191 location_t src_loc, unsigned int column,
87cf0651
SB
192 const char *msgid, ...)
193{
194 va_list ap;
195 bool ret;
196
197 va_start (ap, msgid);
198
199 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
200 column, msgid, &ap);
201
202 va_end (ap);
203 return ret;
204}
205
206/* Print a pedantic warning. The warning reason may be given in REASON. */
207
208bool
c24300ba 209cpp_pedwarning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason,
620e594b 210 location_t src_loc, unsigned int column,
c24300ba 211 const char *msgid, ...)
87cf0651
SB
212{
213 va_list ap;
214 bool ret;
215
216 va_start (ap, msgid);
217
218 ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
219 column, msgid, &ap);
b649398a 220
e34d07f2 221 va_end (ap);
148e4216 222 return ret;
c1212d2f
ZW
223}
224
87cf0651
SB
225/* Print a warning, including system headers. The warning reason may be
226 given in REASON. */
227
228bool
c24300ba 229cpp_warning_with_line_syshdr (cpp_reader *pfile, enum cpp_warning_reason reason,
620e594b 230 location_t src_loc, unsigned int column,
c24300ba 231 const char *msgid, ...)
87cf0651
SB
232{
233 va_list ap;
234 bool ret;
235
236 va_start (ap, msgid);
237
238 ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
239 column, msgid, &ap);
240
241 va_end (ap);
242 return ret;
243}
244
ac81cf0b
DM
245/* As cpp_error, but use SRC_LOC as the location of the error, without
246 a column override. */
247
248bool
c24300ba 249cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
620e594b 250 location_t src_loc, const char *msgid, ...)
ac81cf0b
DM
251{
252 va_list ap;
253 bool ret;
254
255 va_start (ap, msgid);
256
64a5912c
DM
257 rich_location richloc (pfile->line_table, src_loc);
258 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, &richloc,
ac81cf0b
DM
259 msgid, &ap);
260
261 va_end (ap);
262 return ret;
263}
264
cb18fd07
DM
265/* As cpp_error, but use RICHLOC as the location of the error, without
266 a column override. */
267
268bool
c24300ba
DM
269cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
270 rich_location *richloc, const char *msgid, ...)
cb18fd07
DM
271{
272 va_list ap;
273 bool ret;
274
275 va_start (ap, msgid);
276
64a5912c
DM
277 ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, richloc,
278 msgid, &ap);
cb18fd07
DM
279
280 va_end (ap);
281 return ret;
282}
283
87cf0651
SB
284/* Print a warning or error, depending on the value of LEVEL. Include
285 information from errno. */
286
148e4216 287bool
c24300ba
DM
288cpp_errno (cpp_reader *pfile, enum cpp_diagnostic_level level,
289 const char *msgid)
c1212d2f 290{
46ce03de
JJ
291 return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno));
292}
293
294/* Print a warning or error, depending on the value of LEVEL. Include
295 information from errno. Unlike cpp_errno, the argument is a filename
296 that is not localized, but "" is replaced with localized "stdout". */
297
298bool
c24300ba
DM
299cpp_errno_filename (cpp_reader *pfile, enum cpp_diagnostic_level level,
300 const char *filename,
620e594b 301 location_t loc)
46ce03de
JJ
302{
303 if (filename[0] == '\0')
304 filename = _("stdout");
ebef4e8c 305
ac81cf0b
DM
306 return cpp_error_at (pfile, level, loc, "%s: %s", filename,
307 xstrerror (errno));
c1212d2f 308}