blob: e3d56292848c2cba59455d05faa81062a0442e2a [file] [log] [blame]
Per Bothner7f2935c1995-03-16 13:59:07 -08001/* Default error handlers for CPP Library.
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
Tom Tromeybf048be2008-04-18 00:58:58 +00003 2001, 2002, 2004, 2008 Free Software Foundation, Inc.
Per Bothner7f2935c1995-03-16 13:59:07 -08004 Written by Per Bothner, 1994.
Jeff Law38e01251998-05-06 15:09:07 -06005 Based on CCCP program by Paul Rubin, June 1986
Per Bothner7f2935c1995-03-16 13:59:07 -08006 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
Kelley Cook200031d2005-06-29 02:34:39 +000020Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Per Bothner7f2935c1995-03-16 13:59:07 -080021
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
Richard Kenner84ee6fd1995-05-16 18:02:38 -040026#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000027#include "system.h"
Richard Kennerf32da1f1995-05-20 06:51:33 -040028#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000029#include "internal.h"
Richard Kennerf32da1f1995-05-20 06:51:33 -040030
Per Bothner12f9df42004-02-11 07:29:30 -080031static void print_location (cpp_reader *, source_location, unsigned int);
Philipp Thomas146ef882001-04-22 22:33:45 +000032
Neil Booth5d8ebbd2002-01-03 21:43:09 +000033/* Print the logical file location (LINE, COL) in preparation for a
Neil Boothebef4e82002-04-14 18:42:47 +000034 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. */
Zack Weinbergc1212d22000-02-06 23:46:18 +000037static void
Per Bothner12f9df42004-02-11 07:29:30 -080038print_location (cpp_reader *pfile, source_location line, unsigned int col)
Per Bothner7f2935c1995-03-16 13:59:07 -080039{
Per Bothner75ee8002003-08-28 00:21:34 +000040 if (line == 0)
Neil Booth0bda4762000-12-11 07:45:16 +000041 fprintf (stderr, "%s: ", progname);
Neil Booth14fbab62000-11-17 19:21:15 +000042 else
Neil Booth0bda4762000-12-11 07:45:16 +000043 {
Neil Boothbb74c962001-08-17 22:23:49 +000044 const struct line_map *map;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +000045 linenum_type lin;
Neil Booth0bda4762000-12-11 07:45:16 +000046
Per Bothner50f59cd2004-01-19 21:30:18 -080047 map = linemap_lookup (pfile->line_table, line);
48 linemap_print_containing_files (pfile->line_table, map);
Neil Boothd82fc102001-08-02 23:03:31 +000049
Per Bothnerdc3786d2003-08-26 22:53:22 +000050 lin = SOURCE_LINE (map, line);
Neil Boothbb74c962001-08-17 22:23:49 +000051 if (col == 0)
Per Bothner12f9df42004-02-11 07:29:30 -080052 {
53 col = SOURCE_COLUMN (map, line);
54 if (col == 0)
55 col = 1;
56 }
Neil Booth0bda4762000-12-11 07:45:16 +000057
Per Bothnerdc3786d2003-08-26 22:53:22 +000058 if (lin == 0)
Neil Boothbb74c962001-08-17 22:23:49 +000059 fprintf (stderr, "%s:", map->to_file);
Neil Booth0bda4762000-12-11 07:45:16 +000060 else if (CPP_OPTION (pfile, show_column) == 0)
Per Bothnerdc3786d2003-08-26 22:53:22 +000061 fprintf (stderr, "%s:%u:", map->to_file, lin);
Neil Booth0bda4762000-12-11 07:45:16 +000062 else
Per Bothnerdc3786d2003-08-26 22:53:22 +000063 fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col);
Neil Booth0bda4762000-12-11 07:45:16 +000064
Neil Boothd82fc102001-08-02 23:03:31 +000065 fputc (' ', stderr);
Neil Booth0bda4762000-12-11 07:45:16 +000066 }
Per Bothner7f2935c1995-03-16 13:59:07 -080067}
68
Neil Boothebef4e82002-04-14 18:42:47 +000069/* Set up for a diagnostic: print the file and line, bump the error
Per Bothner12f9df42004-02-11 07:29:30 -080070 counter, etc. SRC_LOC is the logical line number; zero means to print
Neil Boothebef4e82002-04-14 18:42:47 +000071 at the location of the previously lexed token, which tends to be
Per Bothner12f9df42004-02-11 07:29:30 -080072 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. */
Tom Tromey5d6342e2008-05-21 21:52:57 +000079static int
Per Bothner12f9df42004-02-11 07:29:30 -080080_cpp_begin_message (cpp_reader *pfile, int code,
81 source_location src_loc, unsigned int column)
Per Bothner7f2935c1995-03-16 13:59:07 -080082{
John David Anglin0527bc42003-11-01 22:56:54 +000083 int level = CPP_DL_EXTRACT (code);
Zack Weinberg58fea6a2000-08-02 01:13:45 +000084
Neil Boothebef4e82002-04-14 18:42:47 +000085 switch (level)
Zack Weinberg58fea6a2000-08-02 01:13:45 +000086 {
John David Anglin0527bc42003-11-01 22:56:54 +000087 case CPP_DL_WARNING:
88 case CPP_DL_PEDWARN:
Per Bothner12f9df42004-02-11 07:29:30 -080089 if (cpp_in_system_header (pfile)
Neil Boothd8090682000-09-28 05:55:22 +000090 && ! CPP_OPTION (pfile, warn_system_headers))
91 return 0;
Neil Boothebef4e82002-04-14 18:42:47 +000092 /* Fall through. */
93
John David Anglin0527bc42003-11-01 22:56:54 +000094 case CPP_DL_WARNING_SYSHDR:
Chris Demetriouc933c202001-01-23 19:20:14 +000095 if (CPP_OPTION (pfile, warnings_are_errors)
John David Anglin0527bc42003-11-01 22:56:54 +000096 || (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
Zack Weinberg58fea6a2000-08-02 01:13:45 +000097 {
98 if (CPP_OPTION (pfile, inhibit_errors))
99 return 0;
John David Anglin0527bc42003-11-01 22:56:54 +0000100 level = CPP_DL_ERROR;
Neil Boothbdee42b2002-05-23 06:07:45 +0000101 pfile->errors++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000102 }
Neil Boothebef4e82002-04-14 18:42:47 +0000103 else if (CPP_OPTION (pfile, inhibit_warnings))
104 return 0;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000105 break;
Kazu Hiratadf383482002-05-22 22:02:16 +0000106
John David Anglin0527bc42003-11-01 22:56:54 +0000107 case CPP_DL_ERROR:
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000108 if (CPP_OPTION (pfile, inhibit_errors))
109 return 0;
Neil Boothbdee42b2002-05-23 06:07:45 +0000110 /* ICEs cannot be inhibited. */
John David Anglin0527bc42003-11-01 22:56:54 +0000111 case CPP_DL_ICE:
Neil Boothbdee42b2002-05-23 06:07:45 +0000112 pfile->errors++;
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000113 break;
114 }
Zack Weinbergc1212d22000-02-06 23:46:18 +0000115
Per Bothner12f9df42004-02-11 07:29:30 -0800116 print_location (pfile, src_loc, column);
John David Anglin0527bc42003-11-01 22:56:54 +0000117 if (CPP_DL_WARNING_P (level))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000118 fputs (_("warning: "), stderr);
John David Anglin0527bc42003-11-01 22:56:54 +0000119 else if (level == CPP_DL_ICE)
Neil Boothebef4e82002-04-14 18:42:47 +0000120 fputs (_("internal error: "), stderr);
Joseph Myersac24fc22004-10-14 01:56:22 +0100121 else
122 fputs (_("error: "), stderr);
Jeffrey A Lawab87f8c1999-01-27 01:43:17 +0000123
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000124 return 1;
Per Bothner7f2935c1995-03-16 13:59:07 -0800125}
126
Neil Boothebef4e82002-04-14 18:42:47 +0000127/* 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)
131
Zack Weinbergc1212d22000-02-06 23:46:18 +0000132/* Exported interface. */
133
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000134/* Print an error at the location of the previously lexed token. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800135void
Kaveh R. Ghazie34d07f2003-05-17 22:21:35 +0000136cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
Per Bothner7f2935c1995-03-16 13:59:07 -0800137{
Per Bothner12f9df42004-02-11 07:29:30 -0800138 source_location src_loc;
Kaveh R. Ghazie34d07f2003-05-17 22:21:35 +0000139 va_list ap;
140
141 va_start (ap, msgid);
Zack Weinbergc1212d22000-02-06 23:46:18 +0000142
Joseph Myers178b58b2005-11-03 23:08:18 +0000143 if (CPP_OPTION (pfile, client_diagnostic))
Joseph Myersa63607e2005-11-04 01:16:32 +0000144 pfile->cb.error (pfile, level, _(msgid), &ap);
Neil Boothebef4e82002-04-14 18:42:47 +0000145 else
Per Bothner75ee8002003-08-28 00:21:34 +0000146 {
Joseph Myers178b58b2005-11-03 23:08:18 +0000147 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 }
Tom Tromeybf048be2008-04-18 00:58:58 +0000154 /* 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 }
Joseph Myers178b58b2005-11-03 23:08:18 +0000163 else
164 {
165 src_loc = pfile->cur_token[-1].src_loc;
166 }
Neil Boothebef4e82002-04-14 18:42:47 +0000167
Joseph Myers178b58b2005-11-03 23:08:18 +0000168 if (_cpp_begin_message (pfile, level, src_loc, 0))
169 v_message (msgid, ap);
170 }
Zack Weinbergb6493982001-08-18 20:46:45 +0000171
Kaveh R. Ghazie34d07f2003-05-17 22:21:35 +0000172 va_end (ap);
Per Bothner7f2935c1995-03-16 13:59:07 -0800173}
Jeffrey A Lawab87f8c1999-01-27 01:43:17 +0000174
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000175/* Print an error at a specific location. */
Jeffrey A Lawab87f8c1999-01-27 01:43:17 +0000176void
Kaveh R. Ghazie34d07f2003-05-17 22:21:35 +0000177cpp_error_with_line (cpp_reader *pfile, int level,
Per Bothner12f9df42004-02-11 07:29:30 -0800178 source_location src_loc, unsigned int column,
Kaveh R. Ghazie34d07f2003-05-17 22:21:35 +0000179 const char *msgid, ...)
Zack Weinbergc1212d22000-02-06 23:46:18 +0000180{
Kaveh R. Ghazie34d07f2003-05-17 22:21:35 +0000181 va_list ap;
182
183 va_start (ap, msgid);
Jeffrey A Lawab87f8c1999-01-27 01:43:17 +0000184
Per Bothner12f9df42004-02-11 07:29:30 -0800185 if (_cpp_begin_message (pfile, level, src_loc, column))
Zack Weinberg58fea6a2000-08-02 01:13:45 +0000186 v_message (msgid, ap);
Zack Weinbergb6493982001-08-18 20:46:45 +0000187
Kaveh R. Ghazie34d07f2003-05-17 22:21:35 +0000188 va_end (ap);
Jeffrey A Lawab87f8c1999-01-27 01:43:17 +0000189}
Zack Weinbergc1212d22000-02-06 23:46:18 +0000190
Zack Weinbergc1212d22000-02-06 23:46:18 +0000191void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000192cpp_errno (cpp_reader *pfile, int level, const char *msgid)
Zack Weinbergc1212d22000-02-06 23:46:18 +0000193{
Neil Boothebef4e82002-04-14 18:42:47 +0000194 if (msgid[0] == '\0')
195 msgid = _("stdout");
Zack Weinbergc1212d22000-02-06 23:46:18 +0000196
Neil Boothebef4e82002-04-14 18:42:47 +0000197 cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
Zack Weinbergc1212d22000-02-06 23:46:18 +0000198}