1404b540aSrobert /* Default error handlers for CPP Library.
2404b540aSrobert Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
3404b540aSrobert 2001, 2002, 2004 Free Software Foundation, Inc.
4404b540aSrobert Written by Per Bothner, 1994.
5404b540aSrobert Based on CCCP program by Paul Rubin, June 1986
6404b540aSrobert Adapted to ANSI C, Richard Stallman, Jan 1987
7404b540aSrobert
8404b540aSrobert This program is free software; you can redistribute it and/or modify it
9404b540aSrobert under the terms of the GNU General Public License as published by the
10404b540aSrobert Free Software Foundation; either version 2, or (at your option) any
11404b540aSrobert later version.
12404b540aSrobert
13404b540aSrobert This program is distributed in the hope that it will be useful,
14404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
15404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16404b540aSrobert GNU General Public License for more details.
17404b540aSrobert
18404b540aSrobert You should have received a copy of the GNU General Public License
19404b540aSrobert along with this program; if not, write to the Free Software
20404b540aSrobert Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21404b540aSrobert
22404b540aSrobert In other words, you are welcome to use, share and improve this program.
23404b540aSrobert You are forbidden to forbid anyone else to use, share and improve
24404b540aSrobert what you give them. Help stamp out software-hoarding! */
25404b540aSrobert
26404b540aSrobert #include "config.h"
27404b540aSrobert #include "system.h"
28404b540aSrobert #include "cpplib.h"
29404b540aSrobert #include "internal.h"
30404b540aSrobert
31404b540aSrobert static void print_location (cpp_reader *, source_location, unsigned int);
32404b540aSrobert
33404b540aSrobert /* Print the logical file location (LINE, COL) in preparation for a
34404b540aSrobert diagnostic. Outputs the #include chain if it has changed. A line
35404b540aSrobert of zero suppresses the include stack, and outputs the program name
36404b540aSrobert instead. */
37404b540aSrobert static void
print_location(cpp_reader * pfile,source_location line,unsigned int col)38404b540aSrobert print_location (cpp_reader *pfile, source_location line, unsigned int col)
39404b540aSrobert {
40404b540aSrobert if (line == 0)
41404b540aSrobert fprintf (stderr, "%s: ", progname);
42404b540aSrobert else
43404b540aSrobert {
44404b540aSrobert const struct line_map *map;
45404b540aSrobert unsigned int lin;
46404b540aSrobert
47404b540aSrobert map = linemap_lookup (pfile->line_table, line);
48404b540aSrobert linemap_print_containing_files (pfile->line_table, map);
49404b540aSrobert
50404b540aSrobert lin = SOURCE_LINE (map, line);
51404b540aSrobert if (col == 0)
52404b540aSrobert {
53404b540aSrobert col = SOURCE_COLUMN (map, line);
54404b540aSrobert if (col == 0)
55404b540aSrobert col = 1;
56404b540aSrobert }
57404b540aSrobert
58404b540aSrobert if (lin == 0)
59404b540aSrobert fprintf (stderr, "%s:", map->to_file);
60404b540aSrobert else if (CPP_OPTION (pfile, show_column) == 0)
61404b540aSrobert fprintf (stderr, "%s:%u:", map->to_file, lin);
62404b540aSrobert else
63404b540aSrobert fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col);
64404b540aSrobert
65404b540aSrobert fputc (' ', stderr);
66404b540aSrobert }
67404b540aSrobert }
68404b540aSrobert
69404b540aSrobert /* Set up for a diagnostic: print the file and line, bump the error
70404b540aSrobert counter, etc. SRC_LOC is the logical line number; zero means to print
71404b540aSrobert at the location of the previously lexed token, which tends to be
72404b540aSrobert the correct place by default. The column number can be specified either
73404b540aSrobert using COLUMN or (if COLUMN==0) extracting SOURCE_COLUMN from SRC_LOC.
74404b540aSrobert (This may seem redundant, but is useful when pre-scanning (cleaning) a line,
75404b540aSrobert when we haven't yet verified whether the current line_map has a
76404b540aSrobert big enough max_column_hint.)
77404b540aSrobert
78404b540aSrobert Returns 0 if the error has been suppressed. */
79404b540aSrobert int
_cpp_begin_message(cpp_reader * pfile,int code,source_location src_loc,unsigned int column)80404b540aSrobert _cpp_begin_message (cpp_reader *pfile, int code,
81404b540aSrobert source_location src_loc, unsigned int column)
82404b540aSrobert {
83404b540aSrobert int level = CPP_DL_EXTRACT (code);
84404b540aSrobert
85404b540aSrobert switch (level)
86404b540aSrobert {
87404b540aSrobert case CPP_DL_WARNING:
88404b540aSrobert case CPP_DL_PEDWARN:
89404b540aSrobert if (cpp_in_system_header (pfile)
90404b540aSrobert && ! CPP_OPTION (pfile, warn_system_headers))
91404b540aSrobert return 0;
92404b540aSrobert /* Fall through. */
93404b540aSrobert
94404b540aSrobert case CPP_DL_WARNING_SYSHDR:
95404b540aSrobert if (CPP_OPTION (pfile, warnings_are_errors)
96404b540aSrobert || (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
97404b540aSrobert {
98404b540aSrobert if (CPP_OPTION (pfile, inhibit_errors))
99404b540aSrobert return 0;
100404b540aSrobert level = CPP_DL_ERROR;
101404b540aSrobert pfile->errors++;
102404b540aSrobert }
103404b540aSrobert else if (CPP_OPTION (pfile, inhibit_warnings))
104404b540aSrobert return 0;
105404b540aSrobert break;
106404b540aSrobert
107404b540aSrobert case CPP_DL_ERROR:
108404b540aSrobert if (CPP_OPTION (pfile, inhibit_errors))
109404b540aSrobert return 0;
110404b540aSrobert /* ICEs cannot be inhibited. */
111404b540aSrobert case CPP_DL_ICE:
112404b540aSrobert pfile->errors++;
113404b540aSrobert break;
114404b540aSrobert }
115404b540aSrobert
116404b540aSrobert print_location (pfile, src_loc, column);
117404b540aSrobert if (CPP_DL_WARNING_P (level))
118404b540aSrobert fputs (_("warning: "), stderr);
119404b540aSrobert else if (level == CPP_DL_ICE)
120404b540aSrobert fputs (_("internal error: "), stderr);
121404b540aSrobert else
122404b540aSrobert fputs (_("error: "), stderr);
123404b540aSrobert
124404b540aSrobert return 1;
125404b540aSrobert }
126404b540aSrobert
127404b540aSrobert /* Don't remove the blank before do, as otherwise the exgettext
128404b540aSrobert script will mistake this as a function definition */
129404b540aSrobert #define v_message(msgid, ap) \
130404b540aSrobert do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
131404b540aSrobert
132404b540aSrobert /* Exported interface. */
133404b540aSrobert
134404b540aSrobert /* Print an error at the location of the previously lexed token. */
135404b540aSrobert void
cpp_error(cpp_reader * pfile,int level,const char * msgid,...)136404b540aSrobert cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
137404b540aSrobert {
138404b540aSrobert source_location src_loc;
139404b540aSrobert va_list ap;
140404b540aSrobert
141404b540aSrobert va_start (ap, msgid);
142404b540aSrobert
143404b540aSrobert if (CPP_OPTION (pfile, client_diagnostic))
144404b540aSrobert pfile->cb.error (pfile, level, _(msgid), &ap);
145404b540aSrobert else
146404b540aSrobert {
147404b540aSrobert if (CPP_OPTION (pfile, traditional))
148404b540aSrobert {
149404b540aSrobert if (pfile->state.in_directive)
150404b540aSrobert src_loc = pfile->directive_line;
151404b540aSrobert else
152404b540aSrobert src_loc = pfile->line_table->highest_line;
153404b540aSrobert }
154404b540aSrobert else
155404b540aSrobert {
156*b5762533Sespie /* Find actual previous token. */
157*b5762533Sespie cpp_token *t;
158*b5762533Sespie
159*b5762533Sespie if (pfile->cur_token != pfile->cur_run->base)
160*b5762533Sespie t = pfile->cur_token - 1;
161*b5762533Sespie else
162*b5762533Sespie {
163*b5762533Sespie if (pfile->cur_run->prev != NULL)
164*b5762533Sespie t = pfile->cur_run->prev->limit;
165*b5762533Sespie else
166*b5762533Sespie t = NULL;
167*b5762533Sespie }
168*b5762533Sespie /* Retrieve corresponding source location, unless we failed. */
169*b5762533Sespie src_loc = t ? t->src_loc : 0;
170404b540aSrobert }
171404b540aSrobert
172404b540aSrobert if (_cpp_begin_message (pfile, level, src_loc, 0))
173404b540aSrobert v_message (msgid, ap);
174404b540aSrobert }
175404b540aSrobert
176404b540aSrobert va_end (ap);
177404b540aSrobert }
178404b540aSrobert
179404b540aSrobert /* Print an error at a specific location. */
180404b540aSrobert void
cpp_error_with_line(cpp_reader * pfile,int level,source_location src_loc,unsigned int column,const char * msgid,...)181404b540aSrobert cpp_error_with_line (cpp_reader *pfile, int level,
182404b540aSrobert source_location src_loc, unsigned int column,
183404b540aSrobert const char *msgid, ...)
184404b540aSrobert {
185404b540aSrobert va_list ap;
186404b540aSrobert
187404b540aSrobert va_start (ap, msgid);
188404b540aSrobert
189404b540aSrobert if (_cpp_begin_message (pfile, level, src_loc, column))
190404b540aSrobert v_message (msgid, ap);
191404b540aSrobert
192404b540aSrobert va_end (ap);
193404b540aSrobert }
194404b540aSrobert
195404b540aSrobert void
cpp_errno(cpp_reader * pfile,int level,const char * msgid)196404b540aSrobert cpp_errno (cpp_reader *pfile, int level, const char *msgid)
197404b540aSrobert {
198404b540aSrobert if (msgid[0] == '\0')
199404b540aSrobert msgid = _("stdout");
200404b540aSrobert
201404b540aSrobert cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
202404b540aSrobert }
203