]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/errors.c
line-map.h (linenum_type): New typedef.
[thirdparty/gcc.git] / libcpp / errors.c
CommitLineData
7f2935c7 1/* Default error handlers for CPP Library.
5d8ebbd8 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
bf048bea 3 2001, 2002, 2004, 2008 Free Software Foundation, Inc.
7f2935c7 4 Written by Per Bothner, 1994.
38e01259 5 Based on CCCP program by Paul Rubin, June 1986
7f2935c7
PB
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
200031d1 20Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
7f2935c7
PB
21
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
25
84ee6fd4 26#include "config.h"
b04cd507 27#include "system.h"
f32da1f6 28#include "cpplib.h"
4f4e53dd 29#include "internal.h"
f32da1f6 30
12f9df4e 31static void print_location (cpp_reader *, source_location, unsigned int);
146ef880 32
5d8ebbd8 33/* Print the logical file location (LINE, COL) in preparation for a
ebef4e8c
NB
34 diagnostic. Outputs the #include chain if it has changed. A line
35 of zero suppresses the include stack, and outputs the program name
36 instead. */
c1212d2f 37static void
12f9df4e 38print_location (cpp_reader *pfile, source_location line, unsigned int col)
7f2935c7 39{
75ee800b 40 if (line == 0)
0bda4760 41 fprintf (stderr, "%s: ", progname);
14fbab6d 42 else
0bda4760 43 {
bb74c963 44 const struct line_map *map;
1bb64668 45 linenum_type lin;
0bda4760 46
50f59cd7
PB
47 map = linemap_lookup (pfile->line_table, line);
48 linemap_print_containing_files (pfile->line_table, map);
0bda4760 49
dc3786d8 50 lin = SOURCE_LINE (map, line);
bb74c963 51 if (col == 0)
12f9df4e
PB
52 {
53 col = SOURCE_COLUMN (map, line);
54 if (col == 0)
55 col = 1;
56 }
0bda4760 57
dc3786d8 58 if (lin == 0)
bb74c963 59 fprintf (stderr, "%s:", map->to_file);
0bda4760 60 else if (CPP_OPTION (pfile, show_column) == 0)
dc3786d8 61 fprintf (stderr, "%s:%u:", map->to_file, lin);
0bda4760 62 else
dc3786d8 63 fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col);
0bda4760 64
d82fc108 65 fputc (' ', stderr);
0bda4760 66 }
7f2935c7
PB
67}
68
ebef4e8c 69/* Set up for a diagnostic: print the file and line, bump the error
12f9df4e 70 counter, etc. SRC_LOC is the logical line number; zero means to print
ebef4e8c 71 at the location of the previously lexed token, which tends to be
12f9df4e
PB
72 the correct place by default. The column number can be specified either
73 using COLUMN or (if COLUMN==0) extracting SOURCE_COLUMN from SRC_LOC.
74 (This may seem redundant, but is useful when pre-scanning (cleaning) a line,
75 when we haven't yet verified whether the current line_map has a
76 big enough max_column_hint.)
77
78 Returns 0 if the error has been suppressed. */
5d6342eb 79static int
12f9df4e
PB
80_cpp_begin_message (cpp_reader *pfile, int code,
81 source_location src_loc, unsigned int column)
7f2935c7 82{
0527bc4e 83 int level = CPP_DL_EXTRACT (code);
c1212d2f 84
ebef4e8c 85 switch (level)
c1212d2f 86 {
0527bc4e
JDA
87 case CPP_DL_WARNING:
88 case CPP_DL_PEDWARN:
12f9df4e 89 if (cpp_in_system_header (pfile)
d8090680
NB
90 && ! CPP_OPTION (pfile, warn_system_headers))
91 return 0;
ebef4e8c
NB
92 /* Fall through. */
93
0527bc4e 94 case CPP_DL_WARNING_SYSHDR:
c933c209 95 if (CPP_OPTION (pfile, warnings_are_errors)
0527bc4e 96 || (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
58fea6af
ZW
97 {
98 if (CPP_OPTION (pfile, inhibit_errors))
99 return 0;
0527bc4e 100 level = CPP_DL_ERROR;
bdee42b1 101 pfile->errors++;
58fea6af 102 }
ebef4e8c
NB
103 else if (CPP_OPTION (pfile, inhibit_warnings))
104 return 0;
58fea6af 105 break;
df383483 106
0527bc4e 107 case CPP_DL_ERROR:
58fea6af
ZW
108 if (CPP_OPTION (pfile, inhibit_errors))
109 return 0;
bdee42b1 110 /* ICEs cannot be inhibited. */
0527bc4e 111 case CPP_DL_ICE:
bdee42b1 112 pfile->errors++;
c1212d2f 113 break;
ab87f8c8
JL
114 }
115
12f9df4e 116 print_location (pfile, src_loc, column);
0527bc4e 117 if (CPP_DL_WARNING_P (level))
58fea6af 118 fputs (_("warning: "), stderr);
0527bc4e 119 else if (level == CPP_DL_ICE)
ebef4e8c 120 fputs (_("internal error: "), stderr);
ac24fc25
JM
121 else
122 fputs (_("error: "), stderr);
58fea6af
ZW
123
124 return 1;
7f2935c7
PB
125}
126
ebef4e8c
NB
127/* Don't remove the blank before do, as otherwise the exgettext
128 script will mistake this as a function definition */
129#define v_message(msgid, ap) \
130 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
b649398a 131
ebef4e8c 132/* Exported interface. */
c1212d2f 133
5d8ebbd8 134/* Print an error at the location of the previously lexed token. */
c1212d2f 135void
e34d07f2 136cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
c1212d2f 137{
12f9df4e 138 source_location src_loc;
e34d07f2
KG
139 va_list ap;
140
141 va_start (ap, msgid);
c1212d2f 142
178b58b5 143 if (CPP_OPTION (pfile, client_diagnostic))
a63607ed 144 pfile->cb.error (pfile, level, _(msgid), &ap);
ebef4e8c 145 else
75ee800b 146 {
178b58b5
JM
147 if (CPP_OPTION (pfile, traditional))
148 {
149 if (pfile->state.in_directive)
150 src_loc = pfile->directive_line;
151 else
152 src_loc = pfile->line_table->highest_line;
153 }
bf048bea
TT
154 /* We don't want to refer to a token before the beginning of the
155 current run -- that is invalid. */
156 else if (pfile->cur_token == pfile->cur_run->base)
157 {
158 if (pfile->cur_run->prev != NULL)
159 src_loc = pfile->cur_run->prev->limit->src_loc;
160 else
161 src_loc = 0;
162 }
178b58b5
JM
163 else
164 {
165 src_loc = pfile->cur_token[-1].src_loc;
166 }
c1212d2f 167
178b58b5
JM
168 if (_cpp_begin_message (pfile, level, src_loc, 0))
169 v_message (msgid, ap);
170 }
b649398a 171
e34d07f2 172 va_end (ap);
c1212d2f
ZW
173}
174
ebef4e8c 175/* Print an error at a specific location. */
c1212d2f 176void
e34d07f2 177cpp_error_with_line (cpp_reader *pfile, int level,
12f9df4e 178 source_location src_loc, unsigned int column,
e34d07f2 179 const char *msgid, ...)
c1212d2f 180{
e34d07f2
KG
181 va_list ap;
182
183 va_start (ap, msgid);
ab87f8c8 184
12f9df4e 185 if (_cpp_begin_message (pfile, level, src_loc, column))
58fea6af 186 v_message (msgid, ap);
b649398a 187
e34d07f2 188 va_end (ap);
c1212d2f
ZW
189}
190
c1212d2f 191void
6cf87ca4 192cpp_errno (cpp_reader *pfile, int level, const char *msgid)
c1212d2f 193{
ebef4e8c
NB
194 if (msgid[0] == '\0')
195 msgid = _("stdout");
196
197 cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
c1212d2f 198}