136ac495dSmrg /* Default error handlers for CPP Library.
2*8feb0f0bSmrg Copyright (C) 1986-2020 Free Software Foundation, Inc.
336ac495dSmrg Written by Per Bothner, 1994.
436ac495dSmrg Based on CCCP program by Paul Rubin, June 1986
536ac495dSmrg Adapted to ANSI C, Richard Stallman, Jan 1987
636ac495dSmrg
736ac495dSmrg This program is free software; you can redistribute it and/or modify it
836ac495dSmrg under the terms of the GNU General Public License as published by the
936ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
1036ac495dSmrg later version.
1136ac495dSmrg
1236ac495dSmrg This program is distributed in the hope that it will be useful,
1336ac495dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
1436ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1536ac495dSmrg GNU General Public License for more details.
1636ac495dSmrg
1736ac495dSmrg You should have received a copy of the GNU General Public License
1836ac495dSmrg along with this program; see the file COPYING3. If not see
1936ac495dSmrg <http://www.gnu.org/licenses/>.
2036ac495dSmrg
2136ac495dSmrg In other words, you are welcome to use, share and improve this program.
2236ac495dSmrg You are forbidden to forbid anyone else to use, share and improve
2336ac495dSmrg what you give them. Help stamp out software-hoarding! */
2436ac495dSmrg
2536ac495dSmrg #include "config.h"
2636ac495dSmrg #include "system.h"
2736ac495dSmrg #include "cpplib.h"
2836ac495dSmrg #include "internal.h"
2936ac495dSmrg
3036ac495dSmrg /* Print a diagnostic at the given location. */
3136ac495dSmrg
3236ac495dSmrg ATTRIBUTE_FPTR_PRINTF(5,0)
3336ac495dSmrg static bool
cpp_diagnostic_at(cpp_reader * pfile,enum cpp_diagnostic_level level,enum cpp_warning_reason reason,rich_location * richloc,const char * msgid,va_list * ap)34c0a68be4Smrg cpp_diagnostic_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
35c0a68be4Smrg enum cpp_warning_reason reason, rich_location *richloc,
3636ac495dSmrg const char *msgid, va_list *ap)
3736ac495dSmrg {
3836ac495dSmrg bool ret;
3936ac495dSmrg
40c0a68be4Smrg if (!pfile->cb.diagnostic)
4136ac495dSmrg abort ();
42c0a68be4Smrg ret = pfile->cb.diagnostic (pfile, level, reason, richloc, _(msgid), ap);
4336ac495dSmrg
4436ac495dSmrg return ret;
4536ac495dSmrg }
4636ac495dSmrg
4736ac495dSmrg /* Print a diagnostic at the location of the previously lexed token. */
4836ac495dSmrg
4936ac495dSmrg ATTRIBUTE_FPTR_PRINTF(4,0)
5036ac495dSmrg static bool
cpp_diagnostic(cpp_reader * pfile,enum cpp_diagnostic_level level,enum cpp_warning_reason reason,const char * msgid,va_list * ap)51c0a68be4Smrg cpp_diagnostic (cpp_reader * pfile, enum cpp_diagnostic_level level,
52c0a68be4Smrg enum cpp_warning_reason reason,
5336ac495dSmrg const char *msgid, va_list *ap)
5436ac495dSmrg {
55c0a68be4Smrg location_t src_loc;
5636ac495dSmrg
5736ac495dSmrg if (CPP_OPTION (pfile, traditional))
5836ac495dSmrg {
5936ac495dSmrg if (pfile->state.in_directive)
6036ac495dSmrg src_loc = pfile->directive_line;
6136ac495dSmrg else
6236ac495dSmrg src_loc = pfile->line_table->highest_line;
6336ac495dSmrg }
6436ac495dSmrg /* We don't want to refer to a token before the beginning of the
6536ac495dSmrg current run -- that is invalid. */
6636ac495dSmrg else if (pfile->cur_token == pfile->cur_run->base)
6736ac495dSmrg {
6836ac495dSmrg src_loc = 0;
6936ac495dSmrg }
7036ac495dSmrg else
7136ac495dSmrg {
7236ac495dSmrg src_loc = pfile->cur_token[-1].src_loc;
7336ac495dSmrg }
74a2dc1f3fSmrg rich_location richloc (pfile->line_table, src_loc);
75a2dc1f3fSmrg return cpp_diagnostic_at (pfile, level, reason, &richloc, msgid, ap);
7636ac495dSmrg }
7736ac495dSmrg
7836ac495dSmrg /* Print a warning or error, depending on the value of LEVEL. */
7936ac495dSmrg
8036ac495dSmrg bool
cpp_error(cpp_reader * pfile,enum cpp_diagnostic_level level,const char * msgid,...)81c0a68be4Smrg cpp_error (cpp_reader * pfile, enum cpp_diagnostic_level level,
82c0a68be4Smrg const char *msgid, ...)
8336ac495dSmrg {
8436ac495dSmrg va_list ap;
8536ac495dSmrg bool ret;
8636ac495dSmrg
8736ac495dSmrg va_start (ap, msgid);
8836ac495dSmrg
8936ac495dSmrg ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
9036ac495dSmrg
9136ac495dSmrg va_end (ap);
9236ac495dSmrg return ret;
9336ac495dSmrg }
9436ac495dSmrg
9536ac495dSmrg /* Print a warning. The warning reason may be given in REASON. */
9636ac495dSmrg
9736ac495dSmrg bool
cpp_warning(cpp_reader * pfile,enum cpp_warning_reason reason,const char * msgid,...)98c0a68be4Smrg cpp_warning (cpp_reader * pfile, enum cpp_warning_reason reason,
99c0a68be4Smrg const char *msgid, ...)
10036ac495dSmrg {
10136ac495dSmrg va_list ap;
10236ac495dSmrg bool ret;
10336ac495dSmrg
10436ac495dSmrg va_start (ap, msgid);
10536ac495dSmrg
10636ac495dSmrg ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
10736ac495dSmrg
10836ac495dSmrg va_end (ap);
10936ac495dSmrg return ret;
11036ac495dSmrg }
11136ac495dSmrg
11236ac495dSmrg /* Print a pedantic warning. The warning reason may be given in REASON. */
11336ac495dSmrg
11436ac495dSmrg bool
cpp_pedwarning(cpp_reader * pfile,enum cpp_warning_reason reason,const char * msgid,...)115c0a68be4Smrg cpp_pedwarning (cpp_reader * pfile, enum cpp_warning_reason reason,
116c0a68be4Smrg const char *msgid, ...)
11736ac495dSmrg {
11836ac495dSmrg va_list ap;
11936ac495dSmrg bool ret;
12036ac495dSmrg
12136ac495dSmrg va_start (ap, msgid);
12236ac495dSmrg
12336ac495dSmrg ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
12436ac495dSmrg
12536ac495dSmrg va_end (ap);
12636ac495dSmrg return ret;
12736ac495dSmrg }
12836ac495dSmrg
12936ac495dSmrg /* Print a warning, including system headers. The warning reason may be
13036ac495dSmrg given in REASON. */
13136ac495dSmrg
13236ac495dSmrg bool
cpp_warning_syshdr(cpp_reader * pfile,enum cpp_warning_reason reason,const char * msgid,...)133c0a68be4Smrg cpp_warning_syshdr (cpp_reader * pfile, enum cpp_warning_reason reason,
134c0a68be4Smrg const char *msgid, ...)
13536ac495dSmrg {
13636ac495dSmrg va_list ap;
13736ac495dSmrg bool ret;
13836ac495dSmrg
13936ac495dSmrg va_start (ap, msgid);
14036ac495dSmrg
14136ac495dSmrg ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
14236ac495dSmrg
14336ac495dSmrg va_end (ap);
14436ac495dSmrg return ret;
14536ac495dSmrg }
14636ac495dSmrg
14736ac495dSmrg /* Print a diagnostic at a specific location. */
14836ac495dSmrg
14936ac495dSmrg ATTRIBUTE_FPTR_PRINTF(6,0)
15036ac495dSmrg static bool
cpp_diagnostic_with_line(cpp_reader * pfile,enum cpp_diagnostic_level level,enum cpp_warning_reason reason,location_t src_loc,unsigned int column,const char * msgid,va_list * ap)151c0a68be4Smrg cpp_diagnostic_with_line (cpp_reader * pfile, enum cpp_diagnostic_level level,
152c0a68be4Smrg enum cpp_warning_reason reason,
153c0a68be4Smrg location_t src_loc, unsigned int column,
15436ac495dSmrg const char *msgid, va_list *ap)
15536ac495dSmrg {
15636ac495dSmrg bool ret;
15736ac495dSmrg
158c0a68be4Smrg if (!pfile->cb.diagnostic)
15936ac495dSmrg abort ();
16036ac495dSmrg rich_location richloc (pfile->line_table, src_loc);
16136ac495dSmrg if (column)
16236ac495dSmrg richloc.override_column (column);
163c0a68be4Smrg ret = pfile->cb.diagnostic (pfile, level, reason, &richloc, _(msgid), ap);
16436ac495dSmrg
16536ac495dSmrg return ret;
16636ac495dSmrg }
16736ac495dSmrg
16836ac495dSmrg /* Print a warning or error, depending on the value of LEVEL. */
16936ac495dSmrg
17036ac495dSmrg bool
cpp_error_with_line(cpp_reader * pfile,enum cpp_diagnostic_level level,location_t src_loc,unsigned int column,const char * msgid,...)171c0a68be4Smrg cpp_error_with_line (cpp_reader *pfile, enum cpp_diagnostic_level level,
172c0a68be4Smrg location_t src_loc, unsigned int column,
17336ac495dSmrg const char *msgid, ...)
17436ac495dSmrg {
17536ac495dSmrg va_list ap;
17636ac495dSmrg bool ret;
17736ac495dSmrg
17836ac495dSmrg va_start (ap, msgid);
17936ac495dSmrg
18036ac495dSmrg ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
18136ac495dSmrg column, msgid, &ap);
18236ac495dSmrg
18336ac495dSmrg va_end (ap);
18436ac495dSmrg return ret;
18536ac495dSmrg }
18636ac495dSmrg
18736ac495dSmrg /* Print a warning. The warning reason may be given in REASON. */
18836ac495dSmrg
18936ac495dSmrg bool
cpp_warning_with_line(cpp_reader * pfile,enum cpp_warning_reason reason,location_t src_loc,unsigned int column,const char * msgid,...)190c0a68be4Smrg cpp_warning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason,
191c0a68be4Smrg location_t src_loc, unsigned int column,
19236ac495dSmrg const char *msgid, ...)
19336ac495dSmrg {
19436ac495dSmrg va_list ap;
19536ac495dSmrg bool ret;
19636ac495dSmrg
19736ac495dSmrg va_start (ap, msgid);
19836ac495dSmrg
19936ac495dSmrg ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
20036ac495dSmrg column, msgid, &ap);
20136ac495dSmrg
20236ac495dSmrg va_end (ap);
20336ac495dSmrg return ret;
20436ac495dSmrg }
20536ac495dSmrg
20636ac495dSmrg /* Print a pedantic warning. The warning reason may be given in REASON. */
20736ac495dSmrg
20836ac495dSmrg bool
cpp_pedwarning_with_line(cpp_reader * pfile,enum cpp_warning_reason reason,location_t src_loc,unsigned int column,const char * msgid,...)209c0a68be4Smrg cpp_pedwarning_with_line (cpp_reader *pfile, enum cpp_warning_reason reason,
210c0a68be4Smrg location_t src_loc, unsigned int column,
21136ac495dSmrg const char *msgid, ...)
21236ac495dSmrg {
21336ac495dSmrg va_list ap;
21436ac495dSmrg bool ret;
21536ac495dSmrg
21636ac495dSmrg va_start (ap, msgid);
21736ac495dSmrg
21836ac495dSmrg ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
21936ac495dSmrg column, msgid, &ap);
22036ac495dSmrg
22136ac495dSmrg va_end (ap);
22236ac495dSmrg return ret;
22336ac495dSmrg }
22436ac495dSmrg
22536ac495dSmrg /* Print a warning, including system headers. The warning reason may be
22636ac495dSmrg given in REASON. */
22736ac495dSmrg
22836ac495dSmrg bool
cpp_warning_with_line_syshdr(cpp_reader * pfile,enum cpp_warning_reason reason,location_t src_loc,unsigned int column,const char * msgid,...)229c0a68be4Smrg cpp_warning_with_line_syshdr (cpp_reader *pfile, enum cpp_warning_reason reason,
230c0a68be4Smrg location_t src_loc, unsigned int column,
23136ac495dSmrg const char *msgid, ...)
23236ac495dSmrg {
23336ac495dSmrg va_list ap;
23436ac495dSmrg bool ret;
23536ac495dSmrg
23636ac495dSmrg va_start (ap, msgid);
23736ac495dSmrg
23836ac495dSmrg ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
23936ac495dSmrg column, msgid, &ap);
24036ac495dSmrg
24136ac495dSmrg va_end (ap);
24236ac495dSmrg return ret;
24336ac495dSmrg }
24436ac495dSmrg
24536ac495dSmrg /* As cpp_error, but use SRC_LOC as the location of the error, without
24636ac495dSmrg a column override. */
24736ac495dSmrg
24836ac495dSmrg bool
cpp_error_at(cpp_reader * pfile,enum cpp_diagnostic_level level,location_t src_loc,const char * msgid,...)249c0a68be4Smrg cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
250c0a68be4Smrg location_t src_loc, const char *msgid, ...)
25136ac495dSmrg {
25236ac495dSmrg va_list ap;
25336ac495dSmrg bool ret;
25436ac495dSmrg
25536ac495dSmrg va_start (ap, msgid);
25636ac495dSmrg
257a2dc1f3fSmrg rich_location richloc (pfile->line_table, src_loc);
258a2dc1f3fSmrg ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, &richloc,
25936ac495dSmrg msgid, &ap);
26036ac495dSmrg
26136ac495dSmrg va_end (ap);
26236ac495dSmrg return ret;
26336ac495dSmrg }
26436ac495dSmrg
26536ac495dSmrg /* As cpp_error, but use RICHLOC as the location of the error, without
26636ac495dSmrg a column override. */
26736ac495dSmrg
26836ac495dSmrg bool
cpp_error_at(cpp_reader * pfile,enum cpp_diagnostic_level level,rich_location * richloc,const char * msgid,...)269c0a68be4Smrg cpp_error_at (cpp_reader * pfile, enum cpp_diagnostic_level level,
270c0a68be4Smrg rich_location *richloc, const char *msgid, ...)
27136ac495dSmrg {
27236ac495dSmrg va_list ap;
27336ac495dSmrg bool ret;
27436ac495dSmrg
27536ac495dSmrg va_start (ap, msgid);
27636ac495dSmrg
277a2dc1f3fSmrg ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, richloc,
27836ac495dSmrg msgid, &ap);
27936ac495dSmrg
28036ac495dSmrg va_end (ap);
28136ac495dSmrg return ret;
28236ac495dSmrg }
28336ac495dSmrg
28436ac495dSmrg /* Print a warning or error, depending on the value of LEVEL. Include
28536ac495dSmrg information from errno. */
28636ac495dSmrg
28736ac495dSmrg bool
cpp_errno(cpp_reader * pfile,enum cpp_diagnostic_level level,const char * msgid)288c0a68be4Smrg cpp_errno (cpp_reader *pfile, enum cpp_diagnostic_level level,
289c0a68be4Smrg const char *msgid)
29036ac495dSmrg {
29136ac495dSmrg return cpp_error (pfile, level, "%s: %s", _(msgid), xstrerror (errno));
29236ac495dSmrg }
29336ac495dSmrg
29436ac495dSmrg /* Print a warning or error, depending on the value of LEVEL. Include
29536ac495dSmrg information from errno. Unlike cpp_errno, the argument is a filename
29636ac495dSmrg that is not localized, but "" is replaced with localized "stdout". */
29736ac495dSmrg
29836ac495dSmrg bool
cpp_errno_filename(cpp_reader * pfile,enum cpp_diagnostic_level level,const char * filename,location_t loc)299c0a68be4Smrg cpp_errno_filename (cpp_reader *pfile, enum cpp_diagnostic_level level,
300c0a68be4Smrg const char *filename,
301c0a68be4Smrg location_t loc)
30236ac495dSmrg {
30336ac495dSmrg if (filename[0] == '\0')
30436ac495dSmrg filename = _("stdout");
30536ac495dSmrg
30636ac495dSmrg return cpp_error_at (pfile, level, loc, "%s: %s", filename,
30736ac495dSmrg xstrerror (errno));
30836ac495dSmrg }
309