2003-11-17 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / cpperror.c
blob61763cc1826f7e53c14139c2f84f65a3b657d431
1 /* Default error handlers for CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
3 2001, 2002 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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! */
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "intl.h"
32 static void print_location (cpp_reader *, fileline, unsigned int);
34 /* Print the logical file location (LINE, COL) in preparation for a
35 diagnostic. Outputs the #include chain if it has changed. A line
36 of zero suppresses the include stack, and outputs the program name
37 instead. */
38 static void
39 print_location (cpp_reader *pfile, fileline line, unsigned int col)
41 if (line == 0)
42 fprintf (stderr, "%s: ", progname);
43 else
45 const struct line_map *map;
46 unsigned int lin;
48 map = linemap_lookup (&pfile->line_maps, line);
49 linemap_print_containing_files (&pfile->line_maps, map);
51 lin = SOURCE_LINE (map, line);
52 if (col == 0)
53 col = 1;
55 if (lin == 0)
56 fprintf (stderr, "%s:", map->to_file);
57 else if (CPP_OPTION (pfile, show_column) == 0)
58 fprintf (stderr, "%s:%u:", map->to_file, lin);
59 else
60 fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col);
62 fputc (' ', stderr);
66 /* Set up for a diagnostic: print the file and line, bump the error
67 counter, etc. LINE is the logical line number; zero means to print
68 at the location of the previously lexed token, which tends to be
69 the correct place by default. Returns 0 if the error has been
70 suppressed. */
71 int
72 _cpp_begin_message (cpp_reader *pfile, int code, fileline line,
73 unsigned int column)
75 int level = CPP_DL_EXTRACT (code);
77 switch (level)
79 case CPP_DL_WARNING:
80 case CPP_DL_PEDWARN:
81 if (CPP_IN_SYSTEM_HEADER (pfile)
82 && ! CPP_OPTION (pfile, warn_system_headers))
83 return 0;
84 /* Fall through. */
86 case CPP_DL_WARNING_SYSHDR:
87 if (CPP_OPTION (pfile, warnings_are_errors)
88 || (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
90 if (CPP_OPTION (pfile, inhibit_errors))
91 return 0;
92 level = CPP_DL_ERROR;
93 pfile->errors++;
95 else if (CPP_OPTION (pfile, inhibit_warnings))
96 return 0;
97 break;
99 case CPP_DL_ERROR:
100 if (CPP_OPTION (pfile, inhibit_errors))
101 return 0;
102 /* ICEs cannot be inhibited. */
103 case CPP_DL_ICE:
104 pfile->errors++;
105 break;
108 print_location (pfile, line, column);
109 if (CPP_DL_WARNING_P (level))
110 fputs (_("warning: "), stderr);
111 else if (level == CPP_DL_ICE)
112 fputs (_("internal error: "), stderr);
114 return 1;
117 /* Don't remove the blank before do, as otherwise the exgettext
118 script will mistake this as a function definition */
119 #define v_message(msgid, ap) \
120 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
122 /* Exported interface. */
124 /* Print an error at the location of the previously lexed token. */
125 void
126 cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
128 fileline line;
129 unsigned int column;
130 va_list ap;
132 va_start (ap, msgid);
134 if (CPP_OPTION (pfile, traditional))
136 if (pfile->state.in_directive)
137 line = pfile->directive_line;
138 else
139 line = pfile->line;
140 column = 0;
142 else
144 line = pfile->cur_token[-1].line;
145 column = pfile->cur_token[-1].col;
148 if (_cpp_begin_message (pfile, level, line, column))
149 v_message (msgid, ap);
151 va_end (ap);
154 /* Print an error at a specific location. */
155 void
156 cpp_error_with_line (cpp_reader *pfile, int level,
157 fileline line, unsigned int column,
158 const char *msgid, ...)
160 va_list ap;
162 va_start (ap, msgid);
164 if (_cpp_begin_message (pfile, level, line, column))
165 v_message (msgid, ap);
167 va_end (ap);
170 void
171 cpp_errno (cpp_reader *pfile, int level, const char *msgid)
173 if (msgid[0] == '\0')
174 msgid = _("stdout");
176 cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));